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