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