diff options
Diffstat (limited to 'auto_tests/TCP_test.c')
-rw-r--r-- | auto_tests/TCP_test.c | 52 |
1 files changed, 34 insertions, 18 deletions
diff --git a/auto_tests/TCP_test.c b/auto_tests/TCP_test.c index 898c5e08..36db0332 100644 --- a/auto_tests/TCP_test.c +++ b/auto_tests/TCP_test.c | |||
@@ -21,6 +21,29 @@ | |||
21 | 21 | ||
22 | #define NUM_PORTS 3 | 22 | #define NUM_PORTS 3 |
23 | 23 | ||
24 | #ifndef USE_IPV6 | ||
25 | #define USE_IPV6 1 | ||
26 | #endif | ||
27 | |||
28 | #if !USE_IPV6 | ||
29 | # undef TOX_AF_INET6 | ||
30 | # define TOX_AF_INET6 TOX_AF_INET | ||
31 | # define get_ip6_loopback get_ip4_loopback | ||
32 | #endif | ||
33 | |||
34 | static inline IP get_loopback() | ||
35 | { | ||
36 | IP ip; | ||
37 | #if USE_IPV6 | ||
38 | ip.family = TOX_AF_INET6; | ||
39 | ip.ip6 = get_ip6_loopback(); | ||
40 | #else | ||
41 | ip.family = TOX_AF_INET; | ||
42 | ip.ip4 = get_ip4_loopback(); | ||
43 | #endif | ||
44 | return ip; | ||
45 | } | ||
46 | |||
24 | static uint16_t ports[NUM_PORTS] = {1234, 33445, 25643}; | 47 | static uint16_t ports[NUM_PORTS] = {1234, 33445, 25643}; |
25 | 48 | ||
26 | START_TEST(test_basic) | 49 | START_TEST(test_basic) |
@@ -28,14 +51,13 @@ START_TEST(test_basic) | |||
28 | uint8_t self_public_key[CRYPTO_PUBLIC_KEY_SIZE]; | 51 | uint8_t self_public_key[CRYPTO_PUBLIC_KEY_SIZE]; |
29 | uint8_t self_secret_key[CRYPTO_SECRET_KEY_SIZE]; | 52 | uint8_t self_secret_key[CRYPTO_SECRET_KEY_SIZE]; |
30 | crypto_new_keypair(self_public_key, self_secret_key); | 53 | crypto_new_keypair(self_public_key, self_secret_key); |
31 | TCP_Server *tcp_s = new_TCP_server(1, NUM_PORTS, ports, self_secret_key, NULL); | 54 | TCP_Server *tcp_s = new_TCP_server(USE_IPV6, NUM_PORTS, ports, self_secret_key, NULL); |
32 | ck_assert_msg(tcp_s != NULL, "Failed to create TCP relay server"); | 55 | ck_assert_msg(tcp_s != NULL, "Failed to create TCP relay server"); |
33 | ck_assert_msg(tcp_server_listen_count(tcp_s) == NUM_PORTS, "Failed to bind to all ports"); | 56 | ck_assert_msg(tcp_server_listen_count(tcp_s) == NUM_PORTS, "Failed to bind to all ports"); |
34 | 57 | ||
35 | Socket sock = net_socket(TOX_AF_INET6, TOX_SOCK_STREAM, TOX_PROTO_TCP); | 58 | Socket sock = net_socket(TOX_AF_INET6, TOX_SOCK_STREAM, TOX_PROTO_TCP); |
36 | IP_Port ip_port_loopback; | 59 | IP_Port ip_port_loopback; |
37 | ip_port_loopback.ip.family = TOX_AF_INET6; | 60 | ip_port_loopback.ip = get_loopback(); |
38 | ip_port_loopback.ip.ip6 = get_ip6_loopback(); | ||
39 | ip_port_loopback.port = net_htons(ports[rand() % NUM_PORTS]); | 61 | ip_port_loopback.port = net_htons(ports[rand() % NUM_PORTS]); |
40 | 62 | ||
41 | int ret = net_connect(sock, ip_port_loopback); | 63 | int ret = net_connect(sock, ip_port_loopback); |
@@ -133,9 +155,7 @@ static struct sec_TCP_con *new_TCP_con(TCP_Server *tcp_s) | |||
133 | Socket sock = net_socket(TOX_AF_INET6, TOX_SOCK_STREAM, TOX_PROTO_TCP); | 155 | Socket sock = net_socket(TOX_AF_INET6, TOX_SOCK_STREAM, TOX_PROTO_TCP); |
134 | 156 | ||
135 | IP_Port ip_port_loopback; | 157 | IP_Port ip_port_loopback; |
136 | ip_port_loopback.ip.family = TOX_AF_INET6; | 158 | ip_port_loopback.ip = get_loopback(); |
137 | ip_port_loopback.ip.ip6.uint64[0] = 0; | ||
138 | ip_port_loopback.ip.ip4 = get_ip4_loopback(); | ||
139 | ip_port_loopback.port = net_htons(ports[rand() % NUM_PORTS]); | 159 | ip_port_loopback.port = net_htons(ports[rand() % NUM_PORTS]); |
140 | 160 | ||
141 | int ret = net_connect(sock, ip_port_loopback); | 161 | int ret = net_connect(sock, ip_port_loopback); |
@@ -215,7 +235,7 @@ START_TEST(test_some) | |||
215 | uint8_t self_public_key[CRYPTO_PUBLIC_KEY_SIZE]; | 235 | uint8_t self_public_key[CRYPTO_PUBLIC_KEY_SIZE]; |
216 | uint8_t self_secret_key[CRYPTO_SECRET_KEY_SIZE]; | 236 | uint8_t self_secret_key[CRYPTO_SECRET_KEY_SIZE]; |
217 | crypto_new_keypair(self_public_key, self_secret_key); | 237 | crypto_new_keypair(self_public_key, self_secret_key); |
218 | TCP_Server *tcp_s = new_TCP_server(1, NUM_PORTS, ports, self_secret_key, NULL); | 238 | TCP_Server *tcp_s = new_TCP_server(USE_IPV6, NUM_PORTS, ports, self_secret_key, NULL); |
219 | ck_assert_msg(tcp_s != NULL, "Failed to create TCP relay server"); | 239 | ck_assert_msg(tcp_s != NULL, "Failed to create TCP relay server"); |
220 | ck_assert_msg(tcp_server_listen_count(tcp_s) == NUM_PORTS, "Failed to bind to all ports"); | 240 | ck_assert_msg(tcp_server_listen_count(tcp_s) == NUM_PORTS, "Failed to bind to all ports"); |
221 | 241 | ||
@@ -392,7 +412,7 @@ START_TEST(test_client) | |||
392 | uint8_t self_public_key[CRYPTO_PUBLIC_KEY_SIZE]; | 412 | uint8_t self_public_key[CRYPTO_PUBLIC_KEY_SIZE]; |
393 | uint8_t self_secret_key[CRYPTO_SECRET_KEY_SIZE]; | 413 | uint8_t self_secret_key[CRYPTO_SECRET_KEY_SIZE]; |
394 | crypto_new_keypair(self_public_key, self_secret_key); | 414 | crypto_new_keypair(self_public_key, self_secret_key); |
395 | TCP_Server *tcp_s = new_TCP_server(1, NUM_PORTS, ports, self_secret_key, NULL); | 415 | TCP_Server *tcp_s = new_TCP_server(USE_IPV6, NUM_PORTS, ports, self_secret_key, NULL); |
396 | ck_assert_msg(tcp_s != NULL, "Failed to create TCP relay server"); | 416 | ck_assert_msg(tcp_s != NULL, "Failed to create TCP relay server"); |
397 | ck_assert_msg(tcp_server_listen_count(tcp_s) == NUM_PORTS, "Failed to bind to all ports"); | 417 | ck_assert_msg(tcp_server_listen_count(tcp_s) == NUM_PORTS, "Failed to bind to all ports"); |
398 | 418 | ||
@@ -402,8 +422,7 @@ START_TEST(test_client) | |||
402 | IP_Port ip_port_tcp_s; | 422 | IP_Port ip_port_tcp_s; |
403 | 423 | ||
404 | ip_port_tcp_s.port = net_htons(ports[rand() % NUM_PORTS]); | 424 | ip_port_tcp_s.port = net_htons(ports[rand() % NUM_PORTS]); |
405 | ip_port_tcp_s.ip.family = TOX_AF_INET6; | 425 | ip_port_tcp_s.ip = get_loopback(); |
406 | ip_port_tcp_s.ip.ip6 = get_ip6_loopback(); | ||
407 | TCP_Client_Connection *conn = new_TCP_connection(ip_port_tcp_s, self_public_key, f_public_key, f_secret_key, 0); | 426 | TCP_Client_Connection *conn = new_TCP_connection(ip_port_tcp_s, self_public_key, f_public_key, f_secret_key, 0); |
408 | c_sleep(50); | 427 | c_sleep(50); |
409 | do_TCP_connection(conn, NULL); | 428 | do_TCP_connection(conn, NULL); |
@@ -500,8 +519,7 @@ START_TEST(test_client_invalid) | |||
500 | IP_Port ip_port_tcp_s; | 519 | IP_Port ip_port_tcp_s; |
501 | 520 | ||
502 | ip_port_tcp_s.port = net_htons(ports[rand() % NUM_PORTS]); | 521 | ip_port_tcp_s.port = net_htons(ports[rand() % NUM_PORTS]); |
503 | ip_port_tcp_s.ip.family = TOX_AF_INET6; | 522 | ip_port_tcp_s.ip = get_loopback(); |
504 | ip_port_tcp_s.ip.ip6 = get_ip6_loopback(); | ||
505 | TCP_Client_Connection *conn = new_TCP_connection(ip_port_tcp_s, self_public_key, f_public_key, f_secret_key, 0); | 523 | TCP_Client_Connection *conn = new_TCP_connection(ip_port_tcp_s, self_public_key, f_public_key, f_secret_key, 0); |
506 | c_sleep(50); | 524 | c_sleep(50); |
507 | do_TCP_connection(conn, NULL); | 525 | do_TCP_connection(conn, NULL); |
@@ -553,7 +571,7 @@ START_TEST(test_tcp_connection) | |||
553 | uint8_t self_public_key[CRYPTO_PUBLIC_KEY_SIZE]; | 571 | uint8_t self_public_key[CRYPTO_PUBLIC_KEY_SIZE]; |
554 | uint8_t self_secret_key[CRYPTO_SECRET_KEY_SIZE]; | 572 | uint8_t self_secret_key[CRYPTO_SECRET_KEY_SIZE]; |
555 | crypto_new_keypair(self_public_key, self_secret_key); | 573 | crypto_new_keypair(self_public_key, self_secret_key); |
556 | TCP_Server *tcp_s = new_TCP_server(1, NUM_PORTS, ports, self_secret_key, NULL); | 574 | TCP_Server *tcp_s = new_TCP_server(USE_IPV6, NUM_PORTS, ports, self_secret_key, NULL); |
557 | ck_assert_msg(public_key_cmp(tcp_server_public_key(tcp_s), self_public_key) == 0, "Wrong public key"); | 575 | ck_assert_msg(public_key_cmp(tcp_server_public_key(tcp_s), self_public_key) == 0, "Wrong public key"); |
558 | 576 | ||
559 | TCP_Proxy_Info proxy_info; | 577 | TCP_Proxy_Info proxy_info; |
@@ -569,8 +587,7 @@ START_TEST(test_tcp_connection) | |||
569 | IP_Port ip_port_tcp_s; | 587 | IP_Port ip_port_tcp_s; |
570 | 588 | ||
571 | ip_port_tcp_s.port = net_htons(ports[rand() % NUM_PORTS]); | 589 | ip_port_tcp_s.port = net_htons(ports[rand() % NUM_PORTS]); |
572 | ip_port_tcp_s.ip.family = TOX_AF_INET6; | 590 | ip_port_tcp_s.ip = get_loopback(); |
573 | ip_port_tcp_s.ip.ip6 = get_ip6_loopback(); | ||
574 | 591 | ||
575 | int connection = new_tcp_connection_to(tc_1, tcp_connections_public_key(tc_2), 123); | 592 | int connection = new_tcp_connection_to(tc_1, tcp_connections_public_key(tc_2), 123); |
576 | ck_assert_msg(connection == 0, "Connection id wrong"); | 593 | ck_assert_msg(connection == 0, "Connection id wrong"); |
@@ -662,7 +679,7 @@ START_TEST(test_tcp_connection2) | |||
662 | uint8_t self_public_key[CRYPTO_PUBLIC_KEY_SIZE]; | 679 | uint8_t self_public_key[CRYPTO_PUBLIC_KEY_SIZE]; |
663 | uint8_t self_secret_key[CRYPTO_SECRET_KEY_SIZE]; | 680 | uint8_t self_secret_key[CRYPTO_SECRET_KEY_SIZE]; |
664 | crypto_new_keypair(self_public_key, self_secret_key); | 681 | crypto_new_keypair(self_public_key, self_secret_key); |
665 | TCP_Server *tcp_s = new_TCP_server(1, NUM_PORTS, ports, self_secret_key, NULL); | 682 | TCP_Server *tcp_s = new_TCP_server(USE_IPV6, NUM_PORTS, ports, self_secret_key, NULL); |
666 | ck_assert_msg(public_key_cmp(tcp_server_public_key(tcp_s), self_public_key) == 0, "Wrong public key"); | 683 | ck_assert_msg(public_key_cmp(tcp_server_public_key(tcp_s), self_public_key) == 0, "Wrong public key"); |
667 | 684 | ||
668 | TCP_Proxy_Info proxy_info; | 685 | TCP_Proxy_Info proxy_info; |
@@ -678,8 +695,7 @@ START_TEST(test_tcp_connection2) | |||
678 | IP_Port ip_port_tcp_s; | 695 | IP_Port ip_port_tcp_s; |
679 | 696 | ||
680 | ip_port_tcp_s.port = net_htons(ports[rand() % NUM_PORTS]); | 697 | ip_port_tcp_s.port = net_htons(ports[rand() % NUM_PORTS]); |
681 | ip_port_tcp_s.ip.family = TOX_AF_INET6; | 698 | ip_port_tcp_s.ip = get_loopback(); |
682 | ip_port_tcp_s.ip.ip6 = get_ip6_loopback(); | ||
683 | 699 | ||
684 | int connection = new_tcp_connection_to(tc_1, tcp_connections_public_key(tc_2), 123); | 700 | int connection = new_tcp_connection_to(tc_1, tcp_connections_public_key(tc_2), 123); |
685 | ck_assert_msg(connection == 0, "Connection id wrong"); | 701 | ck_assert_msg(connection == 0, "Connection id wrong"); |