diff options
author | iphydf <iphydf@users.noreply.github.com> | 2018-02-27 01:07:46 +0000 |
---|---|---|
committer | iphydf <iphydf@users.noreply.github.com> | 2018-05-20 19:35:28 +0000 |
commit | 21675ce0d2581597b0e0a727ab4cf6cfb796a037 (patch) | |
tree | e1ac4ca63c5cc2b2dc78fcd5b20a8202eab98d9d /auto_tests | |
parent | 4f6ab0708c85f3e3da7726f6caecc381c6d21370 (diff) |
Finish @Diadlo's network Family abstraction.
The Family stuff in toxcore is a big mess. I'm sure I saw a bunch of bugs
on the way, but I'm not verifying that code now, so the bugs stay.
Diffstat (limited to 'auto_tests')
-rw-r--r-- | auto_tests/TCP_test.c | 11 | ||||
-rw-r--r-- | auto_tests/dht_test.c | 18 | ||||
-rw-r--r-- | auto_tests/network_test.c | 21 | ||||
-rw-r--r-- | auto_tests/onion_test.c | 6 |
4 files changed, 29 insertions, 27 deletions
diff --git a/auto_tests/TCP_test.c b/auto_tests/TCP_test.c index 41125116..8b61c4b1 100644 --- a/auto_tests/TCP_test.c +++ b/auto_tests/TCP_test.c | |||
@@ -29,18 +29,17 @@ | |||
29 | #endif | 29 | #endif |
30 | 30 | ||
31 | #if !USE_IPV6 | 31 | #if !USE_IPV6 |
32 | #undef TOX_AF_INET6 | 32 | #define net_family_ipv6 net_family_ipv4 |
33 | #define TOX_AF_INET6 TOX_AF_INET | ||
34 | #endif | 33 | #endif |
35 | 34 | ||
36 | static inline IP get_loopback() | 35 | static inline IP get_loopback() |
37 | { | 36 | { |
38 | IP ip; | 37 | IP ip; |
39 | #if USE_IPV6 | 38 | #if USE_IPV6 |
40 | ip.family = TOX_AF_INET6; | 39 | ip.family = net_family_ipv6; |
41 | ip.ip.v6 = get_ip6_loopback(); | 40 | ip.ip.v6 = get_ip6_loopback(); |
42 | #else | 41 | #else |
43 | ip.family = TOX_AF_INET; | 42 | ip.family = net_family_ipv4; |
44 | ip.ip.v4 = get_ip4_loopback(); | 43 | ip.ip.v4 = get_ip4_loopback(); |
45 | #endif | 44 | #endif |
46 | return ip; | 45 | return ip; |
@@ -57,7 +56,7 @@ START_TEST(test_basic) | |||
57 | ck_assert_msg(tcp_s != nullptr, "Failed to create TCP relay server"); | 56 | ck_assert_msg(tcp_s != nullptr, "Failed to create TCP relay server"); |
58 | ck_assert_msg(tcp_server_listen_count(tcp_s) == NUM_PORTS, "Failed to bind to all ports"); | 57 | ck_assert_msg(tcp_server_listen_count(tcp_s) == NUM_PORTS, "Failed to bind to all ports"); |
59 | 58 | ||
60 | Socket sock = net_socket(TOX_AF_INET6, TOX_SOCK_STREAM, TOX_PROTO_TCP); | 59 | Socket sock = net_socket(net_family_ipv6, TOX_SOCK_STREAM, TOX_PROTO_TCP); |
61 | IP_Port ip_port_loopback; | 60 | IP_Port ip_port_loopback; |
62 | ip_port_loopback.ip = get_loopback(); | 61 | ip_port_loopback.ip = get_loopback(); |
63 | ip_port_loopback.port = net_htons(ports[random_u32() % NUM_PORTS]); | 62 | ip_port_loopback.port = net_htons(ports[random_u32() % NUM_PORTS]); |
@@ -157,7 +156,7 @@ struct sec_TCP_con { | |||
157 | static struct sec_TCP_con *new_TCP_con(TCP_Server *tcp_s) | 156 | static struct sec_TCP_con *new_TCP_con(TCP_Server *tcp_s) |
158 | { | 157 | { |
159 | struct sec_TCP_con *sec_c = (struct sec_TCP_con *)malloc(sizeof(struct sec_TCP_con)); | 158 | struct sec_TCP_con *sec_c = (struct sec_TCP_con *)malloc(sizeof(struct sec_TCP_con)); |
160 | Socket sock = net_socket(TOX_AF_INET6, TOX_SOCK_STREAM, TOX_PROTO_TCP); | 159 | Socket sock = net_socket(net_family_ipv6, TOX_SOCK_STREAM, TOX_PROTO_TCP); |
161 | 160 | ||
162 | IP_Port ip_port_loopback; | 161 | IP_Port ip_port_loopback; |
163 | ip_port_loopback.ip = get_loopback(); | 162 | ip_port_loopback.ip = get_loopback(); |
diff --git a/auto_tests/dht_test.c b/auto_tests/dht_test.c index 803dad76..17f4563b 100644 --- a/auto_tests/dht_test.c +++ b/auto_tests/dht_test.c | |||
@@ -31,10 +31,10 @@ static inline IP get_loopback() | |||
31 | { | 31 | { |
32 | IP ip; | 32 | IP ip; |
33 | #if USE_IPV6 | 33 | #if USE_IPV6 |
34 | ip.family = TOX_AF_INET6; | 34 | ip.family = net_family_ipv6; |
35 | ip.ip.v6 = get_ip6_loopback(); | 35 | ip.ip.v6 = get_ip6_loopback(); |
36 | #else | 36 | #else |
37 | ip.family = TOX_AF_INET; | 37 | ip.family = net_family_ipv4; |
38 | ip.ip.v4 = get_ip4_loopback(); | 38 | ip.ip.v4 = get_ip4_loopback(); |
39 | #endif | 39 | #endif |
40 | return ip; | 40 | return ip; |
@@ -113,7 +113,7 @@ static void test_addto_lists_update(DHT *dht, | |||
113 | int used, test, test1, test2, found; | 113 | int used, test, test1, test2, found; |
114 | IP_Port test_ipp; | 114 | IP_Port test_ipp; |
115 | uint8_t test_id[CRYPTO_PUBLIC_KEY_SIZE]; | 115 | uint8_t test_id[CRYPTO_PUBLIC_KEY_SIZE]; |
116 | uint8_t ipv6 = ip_port->ip.family == TOX_AF_INET6 ? 1 : 0; | 116 | uint8_t ipv6 = net_family_is_ipv6(ip_port->ip.family) ? 1 : 0; |
117 | 117 | ||
118 | // check id update for existing ip_port | 118 | // check id update for existing ip_port |
119 | test = random_u32() % length; | 119 | test = random_u32() % length; |
@@ -188,7 +188,7 @@ static void test_addto_lists_bad(DHT *dht, | |||
188 | int used, test1, test2, test3; | 188 | int used, test1, test2, test3; |
189 | uint8_t public_key[CRYPTO_PUBLIC_KEY_SIZE], test_id1[CRYPTO_PUBLIC_KEY_SIZE], test_id2[CRYPTO_PUBLIC_KEY_SIZE], | 189 | uint8_t public_key[CRYPTO_PUBLIC_KEY_SIZE], test_id1[CRYPTO_PUBLIC_KEY_SIZE], test_id2[CRYPTO_PUBLIC_KEY_SIZE], |
190 | test_id3[CRYPTO_PUBLIC_KEY_SIZE]; | 190 | test_id3[CRYPTO_PUBLIC_KEY_SIZE]; |
191 | uint8_t ipv6 = ip_port->ip.family == TOX_AF_INET6 ? 1 : 0; | 191 | uint8_t ipv6 = net_family_is_ipv6(ip_port->ip.family) ? 1 : 0; |
192 | 192 | ||
193 | random_bytes(public_key, sizeof(public_key)); | 193 | random_bytes(public_key, sizeof(public_key)); |
194 | mark_all_good(list, length, ipv6); | 194 | mark_all_good(list, length, ipv6); |
@@ -232,7 +232,7 @@ static void test_addto_lists_possible_bad(DHT *dht, | |||
232 | int used, test1, test2, test3; | 232 | int used, test1, test2, test3; |
233 | uint8_t public_key[CRYPTO_PUBLIC_KEY_SIZE], test_id1[CRYPTO_PUBLIC_KEY_SIZE], test_id2[CRYPTO_PUBLIC_KEY_SIZE], | 233 | uint8_t public_key[CRYPTO_PUBLIC_KEY_SIZE], test_id1[CRYPTO_PUBLIC_KEY_SIZE], test_id2[CRYPTO_PUBLIC_KEY_SIZE], |
234 | test_id3[CRYPTO_PUBLIC_KEY_SIZE]; | 234 | test_id3[CRYPTO_PUBLIC_KEY_SIZE]; |
235 | uint8_t ipv6 = ip_port->ip.family == TOX_AF_INET6 ? 1 : 0; | 235 | uint8_t ipv6 = net_family_is_ipv6(ip_port->ip.family) ? 1 : 0; |
236 | 236 | ||
237 | random_bytes(public_key, sizeof(public_key)); | 237 | random_bytes(public_key, sizeof(public_key)); |
238 | mark_all_good(list, length, ipv6); | 238 | mark_all_good(list, length, ipv6); |
@@ -294,7 +294,7 @@ static void test_addto_lists_good(DHT *dht, | |||
294 | const uint8_t *comp_client_id) | 294 | const uint8_t *comp_client_id) |
295 | { | 295 | { |
296 | uint8_t public_key[CRYPTO_PUBLIC_KEY_SIZE]; | 296 | uint8_t public_key[CRYPTO_PUBLIC_KEY_SIZE]; |
297 | uint8_t ipv6 = ip_port->ip.family == TOX_AF_INET6 ? 1 : 0; | 297 | uint8_t ipv6 = net_family_is_ipv6(ip_port->ip.family) ? 1 : 0; |
298 | 298 | ||
299 | mark_all_good(list, length, ipv6); | 299 | mark_all_good(list, length, ipv6); |
300 | 300 | ||
@@ -547,7 +547,7 @@ static void test_list_main(void) | |||
547 | ck_assert_msg(count == 1, "Nodes in search don't know ip of friend. %u %u %u", i, j, count); | 547 | ck_assert_msg(count == 1, "Nodes in search don't know ip of friend. %u %u %u", i, j, count); |
548 | 548 | ||
549 | Node_format ln[MAX_SENT_NODES]; | 549 | Node_format ln[MAX_SENT_NODES]; |
550 | int n = get_close_nodes(dhts[(l + j) % NUM_DHT], dhts[l]->self_public_key, ln, 0, 1, 0); | 550 | int n = get_close_nodes(dhts[(l + j) % NUM_DHT], dhts[l]->self_public_key, ln, net_family_unspec, 1, 0); |
551 | ck_assert_msg(n == MAX_SENT_NODES, "bad num close %u | %u %u", n, i, j); | 551 | ck_assert_msg(n == MAX_SENT_NODES, "bad num close %u | %u %u", n, i, j); |
552 | 552 | ||
553 | count = 0; | 553 | count = 0; |
@@ -744,8 +744,8 @@ static void random_ip(IP_Port *ipp, int family) | |||
744 | uint8_t *port = (uint8_t *)&ipp->port; | 744 | uint8_t *port = (uint8_t *)&ipp->port; |
745 | random_bytes(port, sizeof(ipp->port)); | 745 | random_bytes(port, sizeof(ipp->port)); |
746 | random_bytes(ip, size); | 746 | random_bytes(ip, size); |
747 | ipp->ip.family = family; | 747 | // TODO(iphydf): Pass the net_family variant to random_ip. |
748 | 748 | ipp->ip.family.value = family; | |
749 | } | 749 | } |
750 | 750 | ||
751 | #define PACKED_NODES_SIZE (SIZE_IPPORT + CRYPTO_PUBLIC_KEY_SIZE) | 751 | #define PACKED_NODES_SIZE (SIZE_IPPORT + CRYPTO_PUBLIC_KEY_SIZE) |
diff --git a/auto_tests/network_test.c b/auto_tests/network_test.c index 6f535ee1..0abd14a4 100644 --- a/auto_tests/network_test.c +++ b/auto_tests/network_test.c | |||
@@ -44,7 +44,7 @@ START_TEST(test_addr_resolv_localhost) | |||
44 | net_kill_strerror(strerror); | 44 | net_kill_strerror(strerror); |
45 | 45 | ||
46 | char ip_str[IP_NTOA_LEN]; | 46 | char ip_str[IP_NTOA_LEN]; |
47 | ck_assert_msg(ip.family == TOX_AF_INET, "Expected family TOX_AF_INET, got %u.", ip.family); | 47 | ck_assert_msg(net_family_is_ipv4(ip.family), "Expected family TOX_AF_INET, got %u.", ip.family.value); |
48 | const uint32_t loopback = get_ip4_loopback().uint32; | 48 | const uint32_t loopback = get_ip4_loopback().uint32; |
49 | ck_assert_msg(ip.ip.v4.uint32 == loopback, "Expected 127.0.0.1, got %s.", | 49 | ck_assert_msg(ip.ip.v4.uint32 == loopback, "Expected 127.0.0.1, got %s.", |
50 | ip_ntoa(&ip, ip_str, sizeof(ip_str))); | 50 | ip_ntoa(&ip, ip_str, sizeof(ip_str))); |
@@ -62,7 +62,8 @@ START_TEST(test_addr_resolv_localhost) | |||
62 | ck_assert_msg(res > 0, "Resolver failed: %d, %s", error, strerror); | 62 | ck_assert_msg(res > 0, "Resolver failed: %d, %s", error, strerror); |
63 | net_kill_strerror(strerror); | 63 | net_kill_strerror(strerror); |
64 | 64 | ||
65 | ck_assert_msg(ip.family == TOX_AF_INET6, "Expected family TOX_AF_INET6 (%u), got %u.", TOX_AF_INET6, ip.family); | 65 | ck_assert_msg(net_family_is_ipv6(ip.family), "Expected family TOX_AF_INET6 (%u), got %u.", TOX_AF_INET6, |
66 | ip.family.value); | ||
66 | IP6 ip6_loopback = get_ip6_loopback(); | 67 | IP6 ip6_loopback = get_ip6_loopback(); |
67 | ck_assert_msg(!memcmp(&ip.ip.v6, &ip6_loopback, sizeof(IP6)), "Expected ::1, got %s.", | 68 | ck_assert_msg(!memcmp(&ip.ip.v6, &ip6_loopback, sizeof(IP6)), "Expected ::1, got %s.", |
68 | ip_ntoa(&ip, ip_str, sizeof(ip_str))); | 69 | ip_ntoa(&ip, ip_str, sizeof(ip_str))); |
@@ -73,7 +74,7 @@ START_TEST(test_addr_resolv_localhost) | |||
73 | } | 74 | } |
74 | 75 | ||
75 | ip_init(&ip, 1); // ipv6enabled = 1 | 76 | ip_init(&ip, 1); // ipv6enabled = 1 |
76 | ip.family = TOX_AF_UNSPEC; | 77 | ip.family = net_family_unspec; |
77 | IP extra; | 78 | IP extra; |
78 | ip_reset(&extra); | 79 | ip_reset(&extra); |
79 | res = addr_resolve(localhost, &ip, &extra); | 80 | res = addr_resolve(localhost, &ip, &extra); |
@@ -83,15 +84,17 @@ START_TEST(test_addr_resolv_localhost) | |||
83 | net_kill_strerror(strerror); | 84 | net_kill_strerror(strerror); |
84 | 85 | ||
85 | #if USE_IPV6 | 86 | #if USE_IPV6 |
86 | ck_assert_msg(ip.family == TOX_AF_INET6, "Expected family TOX_AF_INET6 (%u), got %u.", TOX_AF_INET6, ip.family); | 87 | ck_assert_msg(net_family_is_ipv6(ip.family), "Expected family TOX_AF_INET6 (%u), got %u.", TOX_AF_INET6, |
88 | ip.family.value); | ||
87 | ck_assert_msg(!memcmp(&ip.ip.v6, &ip6_loopback, sizeof(IP6)), "Expected ::1, got %s.", | 89 | ck_assert_msg(!memcmp(&ip.ip.v6, &ip6_loopback, sizeof(IP6)), "Expected ::1, got %s.", |
88 | ip_ntoa(&ip, ip_str, sizeof(ip_str))); | 90 | ip_ntoa(&ip, ip_str, sizeof(ip_str))); |
89 | 91 | ||
90 | ck_assert_msg(extra.family == TOX_AF_INET, "Expected family TOX_AF_INET (%u), got %u.", TOX_AF_INET, extra.family); | 92 | ck_assert_msg(net_family_is_ipv4(extra.family), "Expected family TOX_AF_INET (%d), got %u.", TOX_AF_INET, |
93 | extra.family.value); | ||
91 | ck_assert_msg(extra.ip.v4.uint32 == loopback, "Expected 127.0.0.1, got %s.", | 94 | ck_assert_msg(extra.ip.v4.uint32 == loopback, "Expected 127.0.0.1, got %s.", |
92 | ip_ntoa(&ip, ip_str, sizeof(ip_str))); | 95 | ip_ntoa(&ip, ip_str, sizeof(ip_str))); |
93 | #else | 96 | #else |
94 | ck_assert_msg(ip.family == TOX_AF_INET, "Expected family TOX_AF_INET (%u), got %u.", TOX_AF_INET, ip.family); | 97 | ck_assert_msg(net_family_is_ipv4(ip.family), "Expected family TOX_AF_INET (%d), got %u.", TOX_AF_INET, ip.family.value); |
95 | ck_assert_msg(ip.ip.v4.uint32 == loopback, "Expected 127.0.0.1, got %s.", | 98 | ck_assert_msg(ip.ip.v4.uint32 == loopback, "Expected 127.0.0.1, got %s.", |
96 | ip_ntoa(&ip, ip_str, sizeof(ip_str))); | 99 | ip_ntoa(&ip, ip_str, sizeof(ip_str))); |
97 | #endif | 100 | #endif |
@@ -114,14 +117,14 @@ START_TEST(test_ip_equal) | |||
114 | res = ip_equal(nullptr, &ip1); | 117 | res = ip_equal(nullptr, &ip1); |
115 | ck_assert_msg(res == 0, "ip_equal(NULL, PTR): expected result 0, got %u.", res); | 118 | ck_assert_msg(res == 0, "ip_equal(NULL, PTR): expected result 0, got %u.", res); |
116 | 119 | ||
117 | ip1.family = TOX_AF_INET; | 120 | ip1.family = net_family_ipv4; |
118 | ip1.ip.v4.uint32 = net_htonl(0x7F000001); | 121 | ip1.ip.v4.uint32 = net_htonl(0x7F000001); |
119 | 122 | ||
120 | res = ip_equal(&ip1, &ip2); | 123 | res = ip_equal(&ip1, &ip2); |
121 | ck_assert_msg(res == 0, "ip_equal( {TOX_AF_INET, 127.0.0.1}, {TOX_AF_UNSPEC, 0} ): " | 124 | ck_assert_msg(res == 0, "ip_equal( {TOX_AF_INET, 127.0.0.1}, {TOX_AF_UNSPEC, 0} ): " |
122 | "expected result 0, got %u.", res); | 125 | "expected result 0, got %u.", res); |
123 | 126 | ||
124 | ip2.family = TOX_AF_INET; | 127 | ip2.family = net_family_ipv4; |
125 | ip2.ip.v4.uint32 = net_htonl(0x7F000001); | 128 | ip2.ip.v4.uint32 = net_htonl(0x7F000001); |
126 | 129 | ||
127 | res = ip_equal(&ip1, &ip2); | 130 | res = ip_equal(&ip1, &ip2); |
@@ -134,7 +137,7 @@ START_TEST(test_ip_equal) | |||
134 | ck_assert_msg(res == 0, "ip_equal( {TOX_AF_INET, 127.0.0.1}, {TOX_AF_INET, 127.0.0.2} ): " | 137 | ck_assert_msg(res == 0, "ip_equal( {TOX_AF_INET, 127.0.0.1}, {TOX_AF_INET, 127.0.0.2} ): " |
135 | "expected result 0, got %u.", res); | 138 | "expected result 0, got %u.", res); |
136 | 139 | ||
137 | ip2.family = TOX_AF_INET6; | 140 | ip2.family = net_family_ipv6; |
138 | ip2.ip.v6.uint32[0] = 0; | 141 | ip2.ip.v6.uint32[0] = 0; |
139 | ip2.ip.v6.uint32[1] = 0; | 142 | ip2.ip.v6.uint32[1] = 0; |
140 | ip2.ip.v6.uint32[2] = net_htonl(0xFFFF); | 143 | ip2.ip.v6.uint32[2] = net_htonl(0xFFFF); |
diff --git a/auto_tests/onion_test.c b/auto_tests/onion_test.c index 501114a5..c60d3e1a 100644 --- a/auto_tests/onion_test.c +++ b/auto_tests/onion_test.c | |||
@@ -29,10 +29,10 @@ static inline IP get_loopback() | |||
29 | { | 29 | { |
30 | IP ip; | 30 | IP ip; |
31 | #if USE_IPV6 | 31 | #if USE_IPV6 |
32 | ip.family = TOX_AF_INET6; | 32 | ip.family = net_family_ipv6; |
33 | ip.ip.v6 = get_ip6_loopback(); | 33 | ip.ip.v6 = get_ip6_loopback(); |
34 | #else | 34 | #else |
35 | ip.family = TOX_AF_INET; | 35 | ip.family = net_family_ipv4; |
36 | ip.ip.v4 = get_ip4_loopback(); | 36 | ip.ip.v4 = get_ip4_loopback(); |
37 | #endif | 37 | #endif |
38 | return ip; | 38 | return ip; |
@@ -375,7 +375,7 @@ static Onions *new_onions(uint16_t port, uint32_t *index) | |||
375 | return nullptr; | 375 | return nullptr; |
376 | } | 376 | } |
377 | 377 | ||
378 | TCP_Proxy_Info inf = {{{0}}}; | 378 | TCP_Proxy_Info inf = {{{{0}}}}; |
379 | on->onion_c = new_onion_client(new_net_crypto(on->log, dht, &inf)); | 379 | on->onion_c = new_onion_client(new_net_crypto(on->log, dht, &inf)); |
380 | 380 | ||
381 | if (!on->onion_c) { | 381 | if (!on->onion_c) { |