diff options
Diffstat (limited to 'auto_tests/network_test.c')
-rw-r--r-- | auto_tests/network_test.c | 154 |
1 files changed, 154 insertions, 0 deletions
diff --git a/auto_tests/network_test.c b/auto_tests/network_test.c new file mode 100644 index 00000000..593b1f40 --- /dev/null +++ b/auto_tests/network_test.c | |||
@@ -0,0 +1,154 @@ | |||
1 | #ifdef HAVE_CONFIG_H | ||
2 | #include "config.h" | ||
3 | #endif | ||
4 | |||
5 | #include <sys/types.h> | ||
6 | #include <stdint.h> | ||
7 | #include <string.h> | ||
8 | #include <check.h> | ||
9 | #include <stdlib.h> | ||
10 | #include <time.h> | ||
11 | |||
12 | #include "../toxcore/network.h" | ||
13 | |||
14 | START_TEST(test_addr_resolv_localhost) | ||
15 | { | ||
16 | #ifdef __CYGWIN__ | ||
17 | /* force initialization of network stack | ||
18 | * normally this should happen automatically | ||
19 | * cygwin doesn't do it for every network related function though | ||
20 | * e.g. not for getaddrinfo... */ | ||
21 | socket(0, 0, 0); | ||
22 | errno = 0; | ||
23 | #endif | ||
24 | |||
25 | const char localhost[] = "localhost"; | ||
26 | |||
27 | IP ip; | ||
28 | ip_init(&ip, 0); | ||
29 | |||
30 | int res = addr_resolve(localhost, &ip, NULL); | ||
31 | |||
32 | ck_assert_msg(res > 0, "Resolver failed: %u, %s (%x, %x)", errno, strerror(errno)); | ||
33 | |||
34 | if (res > 0) { | ||
35 | ck_assert_msg(ip.family == AF_INET, "Expected family AF_INET, got %u.", ip.family); | ||
36 | ck_assert_msg(ip.ip4.uint32 == htonl(0x7F000001), "Expected 127.0.0.1, got %s.", inet_ntoa(ip.ip4.in_addr)); | ||
37 | } | ||
38 | |||
39 | ip_init(&ip, 1); | ||
40 | res = addr_resolve(localhost, &ip, NULL); | ||
41 | |||
42 | ck_assert_msg(res > 0, "Resolver failed: %u, %s (%x, %x)", errno, strerror(errno)); | ||
43 | |||
44 | if (res > 0) { | ||
45 | ck_assert_msg(ip.family == AF_INET6, "Expected family AF_INET6, got %u.", ip.family); | ||
46 | ck_assert_msg(!memcmp(&ip.ip6, &in6addr_loopback, sizeof(IP6)), "Expected ::1, got %s.", ip_ntoa(&ip)); | ||
47 | } | ||
48 | |||
49 | ip_init(&ip, 1); | ||
50 | ip.family = AF_UNSPEC; | ||
51 | IP extra; | ||
52 | ip_reset(&extra); | ||
53 | res = addr_resolve(localhost, &ip, &extra); | ||
54 | |||
55 | ck_assert_msg(res > 0, "Resolver failed: %u, %s (%x, %x)", errno, strerror(errno)); | ||
56 | |||
57 | if (res > 0) { | ||
58 | ck_assert_msg(ip.family == AF_INET6, "Expected family AF_INET6, got %u.", ip.family); | ||
59 | ck_assert_msg(!memcmp(&ip.ip6, &in6addr_loopback, sizeof(IP6)), "Expected ::1, got %s.", ip_ntoa(&ip)); | ||
60 | |||
61 | ck_assert_msg(extra.family == AF_INET, "Expected family AF_INET, got %u.", extra.family); | ||
62 | ck_assert_msg(extra.ip4.uint32 == htonl(0x7F000001), "Expected 127.0.0.1, got %s.", inet_ntoa(extra.ip4.in_addr)); | ||
63 | } | ||
64 | } | ||
65 | END_TEST | ||
66 | |||
67 | START_TEST(test_ip_equal) | ||
68 | { | ||
69 | int res; | ||
70 | IP ip1, ip2; | ||
71 | ip_reset(&ip1); | ||
72 | ip_reset(&ip2); | ||
73 | |||
74 | res = ip_equal(NULL, NULL); | ||
75 | ck_assert_msg(res == 0, "ip_equal(NULL, NULL): expected result 0, got %u.", res); | ||
76 | |||
77 | res = ip_equal(&ip1, NULL); | ||
78 | ck_assert_msg(res == 0, "ip_equal(PTR, NULL): expected result 0, got %u.", res); | ||
79 | |||
80 | res = ip_equal(NULL, &ip1); | ||
81 | ck_assert_msg(res == 0, "ip_equal(NULL, PTR): expected result 0, got %u.", res); | ||
82 | |||
83 | ip1.family = AF_INET; | ||
84 | ip1.ip4.uint32 = htonl(0x7F000001); | ||
85 | |||
86 | res = ip_equal(&ip1, &ip2); | ||
87 | ck_assert_msg(res == 0, "ip_equal( {AF_INET, 127.0.0.1}, {AF_UNSPEC, 0} ): expected result 0, got %u.", res); | ||
88 | |||
89 | ip2.family = AF_INET; | ||
90 | ip2.ip4.uint32 = htonl(0x7F000001); | ||
91 | |||
92 | res = ip_equal(&ip1, &ip2); | ||
93 | ck_assert_msg(res != 0, "ip_equal( {AF_INET, 127.0.0.1}, {AF_INET, 127.0.0.1} ): expected result != 0, got 0."); | ||
94 | |||
95 | ip2.ip4.uint32 = htonl(0x7F000002); | ||
96 | |||
97 | res = ip_equal(&ip1, &ip2); | ||
98 | ck_assert_msg(res == 0, "ip_equal( {AF_INET, 127.0.0.1}, {AF_INET, 127.0.0.2} ): expected result 0, got %u.", res); | ||
99 | |||
100 | ip2.family = AF_INET6; | ||
101 | ip2.ip6.uint32[0] = 0; | ||
102 | ip2.ip6.uint32[1] = 0; | ||
103 | ip2.ip6.uint32[2] = htonl(0xFFFF); | ||
104 | ip2.ip6.uint32[3] = htonl(0x7F000001); | ||
105 | |||
106 | ck_assert_msg(IN6_IS_ADDR_V4MAPPED(&ip2.ip6) != 0, "IN6_IS_ADDR_V4MAPPED(::ffff:127.0.0.1): expected != 0, got 0."); | ||
107 | |||
108 | res = ip_equal(&ip1, &ip2); | ||
109 | ck_assert_msg(res != 0, "ip_equal( {AF_INET, 127.0.0.1}, {AF_INET6, ::ffff:127.0.0.1} ): expected result != 0, got 0."); | ||
110 | |||
111 | memcpy(&ip2.ip6, &in6addr_loopback, sizeof(IP6)); | ||
112 | res = ip_equal(&ip1, &ip2); | ||
113 | ck_assert_msg(res == 0, "ip_equal( {AF_INET, 127.0.0.1}, {AF_INET6, ::1} ): expected result 0, got %u.", res); | ||
114 | |||
115 | memcpy(&ip1, &ip2, sizeof(IP)); | ||
116 | res = ip_equal(&ip1, &ip2); | ||
117 | ck_assert_msg(res != 0, "ip_equal( {AF_INET6, ::1}, {AF_INET6, ::1} ): expected result != 0, got 0."); | ||
118 | |||
119 | ip2.ip6.uint8[15]++; | ||
120 | res = ip_equal(&ip1, &ip2); | ||
121 | ck_assert_msg(res == 0, "ip_equal( {AF_INET6, ::1}, {AF_INET6, ::2} ): expected result 0, got %res.", res); | ||
122 | } | ||
123 | END_TEST | ||
124 | |||
125 | #define DEFTESTCASE(NAME) \ | ||
126 | TCase *NAME = tcase_create(#NAME); \ | ||
127 | tcase_add_test(NAME, test_##NAME); \ | ||
128 | suite_add_tcase(s, NAME); | ||
129 | |||
130 | Suite *network_suite(void) | ||
131 | { | ||
132 | Suite *s = suite_create("Network"); | ||
133 | |||
134 | DEFTESTCASE(addr_resolv_localhost); | ||
135 | DEFTESTCASE(ip_equal); | ||
136 | |||
137 | return s; | ||
138 | } | ||
139 | |||
140 | int main(int argc, char *argv[]) | ||
141 | { | ||
142 | srand((unsigned int) time(NULL)); | ||
143 | |||
144 | Suite *network = network_suite(); | ||
145 | SRunner *test_runner = srunner_create(network); | ||
146 | int number_failed = 0; | ||
147 | |||
148 | srunner_run_all(test_runner, CK_NORMAL); | ||
149 | number_failed = srunner_ntests_failed(test_runner); | ||
150 | |||
151 | srunner_free(test_runner); | ||
152 | |||
153 | return number_failed; | ||
154 | } | ||