From fc27d9dd9a6e9f7bed09de29c9d89023dd87b82a Mon Sep 17 00:00:00 2001 From: Dan Bechard Date: Tue, 17 Sep 2024 20:44:33 -0400 Subject: [PATCH] Remove unnecessary code duplication in socket_connect tests Signed-off-by: Dan Bechard --- netcode.c | 467 +++++------------------------------------------------- 1 file changed, 42 insertions(+), 425 deletions(-) diff --git a/netcode.c b/netcode.c index cbfac5d..e2b7a26 100755 --- a/netcode.c +++ b/netcode.c @@ -6862,458 +6862,75 @@ void test_client_server_connect() netcode_network_simulator_destroy( network_simulator ); } -void test_client_server_ipv4_socket_connect() +void client_server_socket_connect( NETCODE_CONST char * client_address, NETCODE_CONST char * client_address2, NETCODE_CONST char * server_address, NETCODE_CONST char * server_address2 ) { - { - double time = 0.0; - double delta_time = 1.0 / 10.0; - - struct netcode_client_config_t client_config; - netcode_default_client_config( &client_config ); - - struct netcode_client_t * client = netcode_client_create( "0.0.0.0:50000", &client_config, time ); - - check( client ); - - struct netcode_server_config_t server_config; - netcode_default_server_config( &server_config ); - server_config.protocol_id = TEST_PROTOCOL_ID; - memcpy( &server_config.private_key, private_key, NETCODE_KEY_BYTES ); - - struct netcode_server_t * server = netcode_server_create( "127.0.0.1:40000", &server_config, time ); - - check( server ); - - netcode_server_start( server, 1 ); - - NETCODE_CONST char * server_address = "127.0.0.1:40000"; - - uint8_t connect_token[NETCODE_CONNECT_TOKEN_BYTES]; - - uint64_t client_id = 0; - netcode_random_bytes( (uint8_t*) &client_id, 8 ); - - uint8_t user_data[NETCODE_USER_DATA_BYTES]; - netcode_random_bytes(user_data, NETCODE_USER_DATA_BYTES); - - check( netcode_generate_connect_token( 1, &server_address, &server_address, TEST_CONNECT_TOKEN_EXPIRY, TEST_TIMEOUT_SECONDS, client_id, TEST_PROTOCOL_ID, private_key, user_data, connect_token ) ); - - netcode_client_connect( client, connect_token ); - - while ( 1 ) - { - netcode_client_update( client, time ); - - netcode_server_update( server, time ); - - if ( netcode_client_state( client ) <= NETCODE_CLIENT_STATE_DISCONNECTED ) - break; - - if ( netcode_client_state( client ) == NETCODE_CLIENT_STATE_CONNECTED ) - break; - - time += delta_time; - } - - netcode_server_destroy( server ); - - netcode_client_destroy( client ); - } - - { - double time = 0.0; - double delta_time = 1.0 / 10.0; - - struct netcode_client_config_t client_config; - netcode_default_client_config( &client_config ); - - struct netcode_client_t * client = netcode_client_create( "0.0.0.0:50000", &client_config, time ); - - check( client ); - - struct netcode_server_config_t server_config; - netcode_default_server_config( &server_config ); - server_config.protocol_id = TEST_PROTOCOL_ID; - memcpy( &server_config.private_key, private_key, NETCODE_KEY_BYTES ); - - struct netcode_server_t * server = netcode_server_create_overload( "127.0.0.1:40000", "[::1]:40000", &server_config, time ); - - check( server ); - - netcode_server_start( server, 1 ); - - NETCODE_CONST char * server_address = "127.0.0.1:40000"; + double time = 0.0; + double delta_time = 1.0 / 10.0; - uint8_t connect_token[NETCODE_CONNECT_TOKEN_BYTES]; + struct netcode_client_config_t client_config; + netcode_default_client_config( &client_config ); - uint64_t client_id = 0; - netcode_random_bytes( (uint8_t*) &client_id, 8 ); + struct netcode_client_t * client = netcode_client_create_overload( client_address, client_address2, &client_config, time ); - uint8_t user_data[NETCODE_USER_DATA_BYTES]; - netcode_random_bytes(user_data, NETCODE_USER_DATA_BYTES); + check( client ); - check( netcode_generate_connect_token( 1, &server_address, &server_address, TEST_CONNECT_TOKEN_EXPIRY, TEST_TIMEOUT_SECONDS, client_id, TEST_PROTOCOL_ID, private_key, user_data, connect_token ) ); + struct netcode_server_config_t server_config; + netcode_default_server_config( &server_config ); + server_config.protocol_id = TEST_PROTOCOL_ID; + memcpy( &server_config.private_key, private_key, NETCODE_KEY_BYTES ); - netcode_client_connect( client, connect_token ); + struct netcode_server_t * server = netcode_server_create_overload( server_address, server_address2, &server_config, time ); - while ( 1 ) - { - netcode_client_update( client, time ); + check( server ); - netcode_server_update( server, time ); + netcode_server_start( server, 1 ); - if ( netcode_client_state( client ) <= NETCODE_CLIENT_STATE_DISCONNECTED ) - break; + uint64_t client_id = 0; + netcode_random_bytes( (uint8_t*) &client_id, 8 ); - if ( netcode_client_state( client ) == NETCODE_CLIENT_STATE_CONNECTED ) - break; + uint8_t user_data[NETCODE_USER_DATA_BYTES]; + netcode_random_bytes(user_data, NETCODE_USER_DATA_BYTES); - time += delta_time; - } + uint8_t connect_token[NETCODE_CONNECT_TOKEN_BYTES]; - netcode_server_destroy( server ); + check( netcode_generate_connect_token( 1, &server_address, &server_address, TEST_CONNECT_TOKEN_EXPIRY, TEST_TIMEOUT_SECONDS, client_id, TEST_PROTOCOL_ID, private_key, user_data, connect_token ) ); - netcode_client_destroy( client ); - } + netcode_client_connect( client, connect_token ); + while ( 1 ) { - double time = 0.0; - double delta_time = 1.0 / 10.0; - - struct netcode_client_config_t client_config; - netcode_default_client_config( &client_config ); - - struct netcode_client_t * client = netcode_client_create_overload( "0.0.0.0:50000", "[::]:50000", &client_config, time ); - - check( client ); - - struct netcode_server_config_t server_config; - netcode_default_server_config( &server_config ); - server_config.protocol_id = TEST_PROTOCOL_ID; - memcpy( &server_config.private_key, private_key, NETCODE_KEY_BYTES ); - - struct netcode_server_t * server = netcode_server_create( "127.0.0.1:40000", &server_config, time ); - - check( server ); - - netcode_server_start( server, 1 ); - - NETCODE_CONST char * server_address = "127.0.0.1:40000"; - - uint8_t connect_token[NETCODE_CONNECT_TOKEN_BYTES]; - - uint64_t client_id = 0; - netcode_random_bytes( (uint8_t*) &client_id, 8 ); - - uint8_t user_data[NETCODE_USER_DATA_BYTES]; - netcode_random_bytes(user_data, NETCODE_USER_DATA_BYTES); - - check( netcode_generate_connect_token( 1, &server_address, &server_address, TEST_CONNECT_TOKEN_EXPIRY, TEST_TIMEOUT_SECONDS, client_id, TEST_PROTOCOL_ID, private_key, user_data, connect_token ) ); - - netcode_client_connect( client, connect_token ); - - while ( 1 ) - { - netcode_client_update( client, time ); - - netcode_server_update( server, time ); - - if ( netcode_client_state( client ) <= NETCODE_CLIENT_STATE_DISCONNECTED ) - break; + netcode_client_update( client, time ); - if ( netcode_client_state( client ) == NETCODE_CLIENT_STATE_CONNECTED ) - break; + netcode_server_update( server, time ); - time += delta_time; - } + if ( netcode_client_state( client ) <= NETCODE_CLIENT_STATE_DISCONNECTED ) + break; - netcode_server_destroy( server ); + if ( netcode_client_state( client ) == NETCODE_CLIENT_STATE_CONNECTED ) + break; - netcode_client_destroy( client ); + time += delta_time; } - { - double time = 0.0; - double delta_time = 1.0 / 10.0; - - struct netcode_client_config_t client_config; - netcode_default_client_config( &client_config ); - - struct netcode_client_t * client = netcode_client_create_overload( "0.0.0.0:50000", "[::]:50000", &client_config, time ); - - check( client ); - - struct netcode_server_config_t server_config; - netcode_default_server_config( &server_config ); - server_config.protocol_id = TEST_PROTOCOL_ID; - memcpy( &server_config.private_key, private_key, NETCODE_KEY_BYTES ); - - struct netcode_server_t * server = netcode_server_create_overload( "127.0.0.1:40000", "[::1]:40000", &server_config, time ); - - check( server ); - - netcode_server_start( server, 1 ); - - NETCODE_CONST char * server_address = "127.0.0.1:40000"; - - uint8_t connect_token[NETCODE_CONNECT_TOKEN_BYTES]; - - uint64_t client_id = 0; - netcode_random_bytes( (uint8_t*) &client_id, 8 ); - - uint8_t user_data[NETCODE_USER_DATA_BYTES]; - netcode_random_bytes(user_data, NETCODE_USER_DATA_BYTES); - - check( netcode_generate_connect_token( 1, &server_address, &server_address, TEST_CONNECT_TOKEN_EXPIRY, TEST_TIMEOUT_SECONDS, client_id, TEST_PROTOCOL_ID, private_key, user_data, connect_token ) ); - - netcode_client_connect( client, connect_token ); - - while ( 1 ) - { - netcode_client_update( client, time ); - - netcode_server_update( server, time ); - - if ( netcode_client_state( client ) <= NETCODE_CLIENT_STATE_DISCONNECTED ) - break; - - if ( netcode_client_state( client ) == NETCODE_CLIENT_STATE_CONNECTED ) - break; - - time += delta_time; - } + netcode_server_destroy( server ); - netcode_server_destroy( server ); + netcode_client_destroy( client ); +} - netcode_client_destroy( client ); - } +void test_client_server_ipv4_socket_connect() +{ + client_server_socket_connect("0.0.0.0:50000", NULL , "127.0.0.1:40000", NULL ); + client_server_socket_connect("0.0.0.0:50000", NULL , "127.0.0.1:40000", "[::1]:40000"); + client_server_socket_connect("0.0.0.0:50000", "[::]:50000", "127.0.0.1:40000", NULL ); + client_server_socket_connect("0.0.0.0:50000", "[::]:50000", "127.0.0.1:40000", "[::1]:40000"); } void test_client_server_ipv6_socket_connect() { - { - double time = 0.0; - double delta_time = 1.0 / 10.0; - - struct netcode_client_config_t client_config; - netcode_default_client_config( &client_config ); - - struct netcode_client_t * client = netcode_client_create( "[::]:50000", &client_config, time ); - - check( client ); - - struct netcode_server_config_t server_config; - netcode_default_server_config( &server_config ); - server_config.protocol_id = TEST_PROTOCOL_ID; - memcpy( &server_config.private_key, private_key, NETCODE_KEY_BYTES ); - - struct netcode_server_t * server = netcode_server_create( "[::1]:40000", &server_config, time ); - - check( server ); - - netcode_server_start( server, 1 ); - - NETCODE_CONST char * server_address = "[::1]:40000"; - - uint8_t connect_token[NETCODE_CONNECT_TOKEN_BYTES]; - - uint64_t client_id = 0; - netcode_random_bytes( (uint8_t*) &client_id, 8 ); - - uint8_t user_data[NETCODE_USER_DATA_BYTES]; - netcode_random_bytes(user_data, NETCODE_USER_DATA_BYTES); - - check( netcode_generate_connect_token( 1, &server_address, &server_address, TEST_CONNECT_TOKEN_EXPIRY, TEST_TIMEOUT_SECONDS, client_id, TEST_PROTOCOL_ID, private_key, user_data, connect_token ) ); - - netcode_client_connect( client, connect_token ); - - while ( 1 ) - { - netcode_client_update( client, time ); - - netcode_server_update( server, time ); - - if ( netcode_client_state( client ) <= NETCODE_CLIENT_STATE_DISCONNECTED ) - break; - - if ( netcode_client_state( client ) == NETCODE_CLIENT_STATE_CONNECTED ) - break; - - time += delta_time; - } - - netcode_server_destroy( server ); - - netcode_client_destroy( client ); - } - - { - double time = 0.0; - double delta_time = 1.0 / 10.0; - - struct netcode_client_config_t client_config; - netcode_default_client_config( &client_config ); - - struct netcode_client_t * client = netcode_client_create( "[::]:50000", &client_config, time ); - - check( client ); - - struct netcode_server_config_t server_config; - netcode_default_server_config( &server_config ); - server_config.protocol_id = TEST_PROTOCOL_ID; - memcpy( &server_config.private_key, private_key, NETCODE_KEY_BYTES ); - - struct netcode_server_t * server = netcode_server_create_overload( "127.0.0.1:40000", "[::1]:40000", &server_config, time ); - - check( server ); - - netcode_server_start( server, 1 ); - - NETCODE_CONST char * server_address = "[::1]:40000"; - - uint8_t connect_token[NETCODE_CONNECT_TOKEN_BYTES]; - - uint64_t client_id = 0; - netcode_random_bytes( (uint8_t*) &client_id, 8 ); - - uint8_t user_data[NETCODE_USER_DATA_BYTES]; - netcode_random_bytes(user_data, NETCODE_USER_DATA_BYTES); - - check( netcode_generate_connect_token( 1, &server_address, &server_address, TEST_CONNECT_TOKEN_EXPIRY, TEST_TIMEOUT_SECONDS, client_id, TEST_PROTOCOL_ID, private_key, user_data, connect_token ) ); - - netcode_client_connect( client, connect_token ); - - while ( 1 ) - { - netcode_client_update( client, time ); - - netcode_server_update( server, time ); - - if ( netcode_client_state( client ) <= NETCODE_CLIENT_STATE_DISCONNECTED ) - break; - - if ( netcode_client_state( client ) == NETCODE_CLIENT_STATE_CONNECTED ) - break; - - time += delta_time; - } - - netcode_server_destroy( server ); - - netcode_client_destroy( client ); - } - - { - double time = 0.0; - double delta_time = 1.0 / 10.0; - - struct netcode_client_config_t client_config; - netcode_default_client_config( &client_config ); - - struct netcode_client_t * client = netcode_client_create_overload( "0.0.0.0:50000", "[::]:50000", &client_config, time ); - - check( client ); - - struct netcode_server_config_t server_config; - netcode_default_server_config( &server_config ); - server_config.protocol_id = TEST_PROTOCOL_ID; - memcpy( &server_config.private_key, private_key, NETCODE_KEY_BYTES ); - - struct netcode_server_t * server = netcode_server_create( "[::1]:40000", &server_config, time ); - - check( server ); - - netcode_server_start( server, 1 ); - - NETCODE_CONST char * server_address = "[::1]:40000"; - - uint8_t connect_token[NETCODE_CONNECT_TOKEN_BYTES]; - - uint64_t client_id = 0; - netcode_random_bytes( (uint8_t*) &client_id, 8 ); - - uint8_t user_data[NETCODE_USER_DATA_BYTES]; - netcode_random_bytes(user_data, NETCODE_USER_DATA_BYTES); - - check( netcode_generate_connect_token( 1, &server_address, &server_address, TEST_CONNECT_TOKEN_EXPIRY, TEST_TIMEOUT_SECONDS, client_id, TEST_PROTOCOL_ID, private_key, user_data, connect_token ) ); - - netcode_client_connect( client, connect_token ); - - while ( 1 ) - { - netcode_client_update( client, time ); - - netcode_server_update( server, time ); - - if ( netcode_client_state( client ) <= NETCODE_CLIENT_STATE_DISCONNECTED ) - break; - - if ( netcode_client_state( client ) == NETCODE_CLIENT_STATE_CONNECTED ) - break; - - time += delta_time; - } - - netcode_server_destroy( server ); - - netcode_client_destroy( client ); - } - - { - double time = 0.0; - double delta_time = 1.0 / 10.0; - - struct netcode_client_config_t client_config; - netcode_default_client_config( &client_config ); - - struct netcode_client_t * client = netcode_client_create_overload( "0.0.0.0:50000", "[::]:50000", &client_config, time ); - - check( client ); - - struct netcode_server_config_t server_config; - netcode_default_server_config( &server_config ); - server_config.protocol_id = TEST_PROTOCOL_ID; - memcpy( &server_config.private_key, private_key, NETCODE_KEY_BYTES ); - - struct netcode_server_t * server = netcode_server_create_overload( "127.0.0.1:40000", "[::1]:40000", &server_config, time ); - - check( server ); - - netcode_server_start( server, 1 ); - - NETCODE_CONST char * server_address = "[::1]:40000"; - - uint8_t connect_token[NETCODE_CONNECT_TOKEN_BYTES]; - - uint64_t client_id = 0; - netcode_random_bytes( (uint8_t*) &client_id, 8 ); - - uint8_t user_data[NETCODE_USER_DATA_BYTES]; - netcode_random_bytes(user_data, NETCODE_USER_DATA_BYTES); - - check( netcode_generate_connect_token( 1, &server_address, &server_address, TEST_CONNECT_TOKEN_EXPIRY, TEST_TIMEOUT_SECONDS, client_id, TEST_PROTOCOL_ID, private_key, user_data, connect_token ) ); - - netcode_client_connect( client, connect_token ); - - while ( 1 ) - { - netcode_client_update( client, time ); - - netcode_server_update( server, time ); - - if ( netcode_client_state( client ) <= NETCODE_CLIENT_STATE_DISCONNECTED ) - break; - - if ( netcode_client_state( client ) == NETCODE_CLIENT_STATE_CONNECTED ) - break; - - time += delta_time; - } - - netcode_server_destroy( server ); - - netcode_client_destroy( client ); - } + client_server_socket_connect("[::]:50000" , NULL , "[::1]:40000", NULL ); + client_server_socket_connect("[::]:50000" , NULL , "[::1]:40000", "127.0.0.1:40000"); + client_server_socket_connect("0.0.0.0:50000", "[::]:50000", "[::1]:40000", NULL ); + client_server_socket_connect("0.0.0.0:50000", "[::]:50000", "[::1]:40000", "127.0.0.1:40000"); } void test_client_server_keep_alive()