/work/auto_tests/network_test.c
Line | Count | Source |
1 | | #include <stdlib.h> |
2 | | #include <string.h> |
3 | | |
4 | | #include "../testing/misc_tools.h" |
5 | | #include "../toxcore/network.h" |
6 | | #include "check_compat.h" |
7 | | |
8 | | #ifndef USE_IPV6 |
9 | | #define USE_IPV6 1 |
10 | | #endif |
11 | | |
12 | | static void test_addr_resolv_localhost(void) |
13 | 3 | { |
14 | | #ifdef __CYGWIN__ |
15 | | /* force initialization of network stack |
16 | | * normally this should happen automatically |
17 | | * cygwin doesn't do it for every network related function though |
18 | | * e.g. not for getaddrinfo... */ |
19 | | net_socket(0, 0, 0); |
20 | | errno = 0; |
21 | | #endif |
22 | | |
23 | 3 | const Network *ns = os_network(); |
24 | 3 | ck_assert(ns != nullptr); |
25 | | |
26 | 3 | const char localhost[] = "localhost"; |
27 | | |
28 | 3 | IP ip; |
29 | 3 | ip_init(&ip, 0); // ipv6enabled = 0 |
30 | | |
31 | 3 | bool res = addr_resolve_or_parse_ip(ns, localhost, &ip, nullptr); |
32 | | |
33 | 3 | int error = net_error(); |
34 | 3 | char *strerror = net_new_strerror(error); |
35 | 3 | ck_assert_msg(res, "Resolver failed: %d, %s", error, strerror); |
36 | 3 | net_kill_strerror(strerror); |
37 | | |
38 | 3 | Ip_Ntoa ip_str; |
39 | 3 | ck_assert_msg(net_family_is_ipv4(ip.family), "Expected family TOX_AF_INET, got %u.", ip.family.value); |
40 | 3 | const uint32_t loopback = get_ip4_loopback().uint32; |
41 | 3 | ck_assert_msg(ip.ip.v4.uint32 == loopback, "Expected 127.0.0.1, got %s.", |
42 | 3 | net_ip_ntoa(&ip, &ip_str)); |
43 | | |
44 | 3 | ip_init(&ip, 1); // ipv6enabled = 1 |
45 | 3 | res = addr_resolve_or_parse_ip(ns, localhost, &ip, nullptr); |
46 | | |
47 | | #if USE_IPV6 |
48 | | |
49 | | int localhost_split = 0; |
50 | | |
51 | | if (!net_family_is_ipv6(ip.family)) { |
52 | | res = addr_resolve_or_parse_ip(ns, "ip6-localhost", &ip, nullptr); |
53 | | localhost_split = 1; |
54 | | } |
55 | | |
56 | | error = net_error(); |
57 | | strerror = net_new_strerror(error); |
58 | | ck_assert_msg(res, "Resolver failed: %d, %s", error, strerror); |
59 | | net_kill_strerror(strerror); |
60 | | |
61 | | ck_assert_msg(net_family_is_ipv6(ip.family), "Expected family TOX_AF_INET6 (%d), got %u.", TOX_AF_INET6, |
62 | | ip.family.value); |
63 | | IP6 ip6_loopback = get_ip6_loopback(); |
64 | | ck_assert_msg(!memcmp(&ip.ip.v6, &ip6_loopback, sizeof(IP6)), "Expected ::1, got %s.", |
65 | | net_ip_ntoa(&ip, &ip_str)); |
66 | | |
67 | | if (localhost_split) { |
68 | | printf("Localhost seems to be split in two.\n"); |
69 | | return; |
70 | | } |
71 | | |
72 | | #endif |
73 | | |
74 | 3 | ip_init(&ip, 1); // ipv6enabled = 1 |
75 | 3 | ip.family = net_family_unspec(); |
76 | 3 | IP extra; |
77 | 3 | ip_reset(&extra); |
78 | 3 | res = addr_resolve_or_parse_ip(ns, localhost, &ip, &extra); |
79 | 3 | error = net_error(); |
80 | 3 | strerror = net_new_strerror(error); |
81 | 3 | ck_assert_msg(res, "Resolver failed: %d, %s", error, strerror); |
82 | 3 | net_kill_strerror(strerror); |
83 | | |
84 | | #if USE_IPV6 |
85 | | ck_assert_msg(net_family_is_ipv6(ip.family), "Expected family TOX_AF_INET6 (%d), got %u.", TOX_AF_INET6, |
86 | | ip.family.value); |
87 | | ck_assert_msg(!memcmp(&ip.ip.v6, &ip6_loopback, sizeof(IP6)), "Expected ::1, got %s.", |
88 | | net_ip_ntoa(&ip, &ip_str)); |
89 | | |
90 | | ck_assert_msg(net_family_is_ipv4(extra.family), "Expected family TOX_AF_INET (%d), got %u.", TOX_AF_INET, |
91 | | extra.family.value); |
92 | | ck_assert_msg(extra.ip.v4.uint32 == loopback, "Expected 127.0.0.1, got %s.", |
93 | | net_ip_ntoa(&ip, &ip_str)); |
94 | | #elif 0 |
95 | | // TODO(iphydf): Fix this to work on IPv6-supporting systems. |
96 | | ck_assert_msg(net_family_is_ipv4(ip.family), "Expected family TOX_AF_INET (%d), got %u.", TOX_AF_INET, ip.family.value); |
97 | | ck_assert_msg(ip.ip.v4.uint32 == loopback, "Expected 127.0.0.1, got %s.", |
98 | | net_ip_ntoa(&ip, &ip_str)); |
99 | | #endif |
100 | 3 | } |
101 | | |
102 | | static void test_ip_equal(void) |
103 | 3 | { |
104 | 3 | int res; |
105 | 3 | IP ip1, ip2; |
106 | 3 | ip_reset(&ip1); |
107 | 3 | ip_reset(&ip2); |
108 | | |
109 | 3 | res = ip_equal(nullptr, nullptr); |
110 | 3 | ck_assert_msg(res == 0, "ip_equal(NULL, NULL): expected result 0, got %d.", res); |
111 | | |
112 | 3 | res = ip_equal(&ip1, nullptr); |
113 | 3 | ck_assert_msg(res == 0, "ip_equal(PTR, NULL): expected result 0, got %d.", res); |
114 | | |
115 | 3 | res = ip_equal(nullptr, &ip1); |
116 | 3 | ck_assert_msg(res == 0, "ip_equal(NULL, PTR): expected result 0, got %d.", res); |
117 | | |
118 | 3 | ip1.family = net_family_ipv4(); |
119 | 3 | ip1.ip.v4.uint32 = net_htonl(0x7F000001); |
120 | | |
121 | 3 | res = ip_equal(&ip1, &ip2); |
122 | 3 | ck_assert_msg(res == 0, "ip_equal( {TOX_AF_INET, 127.0.0.1}, {TOX_AF_UNSPEC, 0} ): " |
123 | 3 | "expected result 0, got %d.", res); |
124 | | |
125 | 3 | ip2.family = net_family_ipv4(); |
126 | 3 | ip2.ip.v4.uint32 = net_htonl(0x7F000001); |
127 | | |
128 | 3 | res = ip_equal(&ip1, &ip2); |
129 | 3 | ck_assert_msg(res != 0, "ip_equal( {TOX_AF_INET, 127.0.0.1}, {TOX_AF_INET, 127.0.0.1} ): " |
130 | 3 | "expected result != 0, got 0."); |
131 | | |
132 | 3 | ip2.ip.v4.uint32 = net_htonl(0x7F000002); |
133 | | |
134 | 3 | res = ip_equal(&ip1, &ip2); |
135 | 3 | ck_assert_msg(res == 0, "ip_equal( {TOX_AF_INET, 127.0.0.1}, {TOX_AF_INET, 127.0.0.2} ): " |
136 | 3 | "expected result 0, got %d.", res); |
137 | | |
138 | 3 | ip2.family = net_family_ipv6(); |
139 | 3 | ip2.ip.v6.uint32[0] = 0; |
140 | 3 | ip2.ip.v6.uint32[1] = 0; |
141 | 3 | ip2.ip.v6.uint32[2] = net_htonl(0xFFFF); |
142 | 3 | ip2.ip.v6.uint32[3] = net_htonl(0x7F000001); |
143 | | |
144 | 3 | ck_assert_msg(ipv6_ipv4_in_v6(&ip2.ip.v6) != 0, |
145 | 3 | "ipv6_ipv4_in_v6(::ffff:127.0.0.1): expected != 0, got 0."); |
146 | | |
147 | 3 | res = ip_equal(&ip1, &ip2); |
148 | 3 | ck_assert_msg(res != 0, "ip_equal( {TOX_AF_INET, 127.0.0.1}, {TOX_AF_INET6, ::ffff:127.0.0.1} ): " |
149 | 3 | "expected result != 0, got 0."); |
150 | | |
151 | 3 | IP6 ip6_loopback = get_ip6_loopback(); |
152 | 3 | memcpy(&ip2.ip.v6, &ip6_loopback, sizeof(IP6)); |
153 | 3 | res = ip_equal(&ip1, &ip2); |
154 | 3 | ck_assert_msg(res == 0, "ip_equal( {TOX_AF_INET, 127.0.0.1}, {TOX_AF_INET6, ::1} ): expected result 0, got %d.", res); |
155 | | |
156 | 3 | memcpy(&ip1, &ip2, sizeof(IP)); |
157 | 3 | res = ip_equal(&ip1, &ip2); |
158 | 3 | ck_assert_msg(res != 0, "ip_equal( {TOX_AF_INET6, ::1}, {TOX_AF_INET6, ::1} ): expected result != 0, got 0."); |
159 | | |
160 | 3 | ip2.ip.v6.uint8[15]++; |
161 | 3 | res = ip_equal(&ip1, &ip2); |
162 | 3 | ck_assert_msg(res == 0, "ip_equal( {TOX_AF_INET6, ::1}, {TOX_AF_INET6, ::2} ): expected result 0, got %d.", res); |
163 | 3 | } |
164 | | |
165 | | int main(void) |
166 | 721 | { |
167 | 721 | setvbuf(stdout, nullptr, _IONBF, 0); |
168 | | |
169 | 721 | test_addr_resolv_localhost(); |
170 | 721 | test_ip_equal(); |
171 | | |
172 | 721 | return 0; |
173 | 721 | } |