Refactor exit port statistics code and add unit tests.
[tor/rransom.git] / src / test / test_addr.c
blobbafc0a968ef01380b6697713db8d10154c064767
1 /* Copyright (c) 2001-2004, Roger Dingledine.
2 * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
3 * Copyright (c) 2007-2010, The Tor Project, Inc. */
4 /* See LICENSE for licensing information */
6 #include "orconfig.h"
7 #include "or.h"
8 #include "test.h"
10 static void
11 test_addr_basic(void)
13 uint32_t u32;
14 uint16_t u16;
15 char *cp;
17 /* Test parse_addr_port */
18 cp = NULL; u32 = 3; u16 = 3;
19 test_assert(!parse_addr_port(LOG_WARN, "1.2.3.4", &cp, &u32, &u16));
20 test_streq(cp, "1.2.3.4");
21 test_eq(u32, 0x01020304u);
22 test_eq(u16, 0);
23 tor_free(cp);
24 test_assert(!parse_addr_port(LOG_WARN, "4.3.2.1:99", &cp, &u32, &u16));
25 test_streq(cp, "4.3.2.1");
26 test_eq(u32, 0x04030201u);
27 test_eq(u16, 99);
28 tor_free(cp);
29 test_assert(!parse_addr_port(LOG_WARN, "nonexistent.address:4040",
30 &cp, NULL, &u16));
31 test_streq(cp, "nonexistent.address");
32 test_eq(u16, 4040);
33 tor_free(cp);
34 test_assert(!parse_addr_port(LOG_WARN, "localhost:9999", &cp, &u32, &u16));
35 test_streq(cp, "localhost");
36 test_eq(u32, 0x7f000001u);
37 test_eq(u16, 9999);
38 tor_free(cp);
39 u32 = 3;
40 test_assert(!parse_addr_port(LOG_WARN, "localhost", NULL, &u32, &u16));
41 test_eq(cp, NULL);
42 test_eq(u32, 0x7f000001u);
43 test_eq(u16, 0);
44 tor_free(cp);
45 test_eq(0, addr_mask_get_bits(0x0u));
46 test_eq(32, addr_mask_get_bits(0xFFFFFFFFu));
47 test_eq(16, addr_mask_get_bits(0xFFFF0000u));
48 test_eq(31, addr_mask_get_bits(0xFFFFFFFEu));
49 test_eq(1, addr_mask_get_bits(0x80000000u));
51 /* Test inet_ntop */
53 char tmpbuf[TOR_ADDR_BUF_LEN];
54 const char *ip = "176.192.208.224";
55 struct in_addr in;
56 tor_inet_pton(AF_INET, ip, &in);
57 tor_inet_ntop(AF_INET, &in, tmpbuf, sizeof(tmpbuf));
58 test_streq(tmpbuf, ip);
61 done:
65 #define _test_op_ip6(a,op,b,e1,e2) \
66 STMT_BEGIN \
67 tt_assert_test_fmt_type(a,b,e1" "#op" "e2,struct in6_addr*, \
68 (memcmp(_val1->s6_addr, _val2->s6_addr, 16) op 0), \
69 char *, "%s", \
70 { int i; char *cp; \
71 cp = _print = tor_malloc(64); \
72 for (i=0;i<16;++i) { \
73 tor_snprintf(cp, 3,"%02x", (unsigned)_value->s6_addr[i]);\
74 cp += 2; \
75 if (i != 15) *cp++ = ':'; \
76 } \
77 }, { tor_free(_print); } \
78 ); \
79 STMT_END
81 /** Helper: Assert that two strings both decode as IPv6 addresses with
82 * tor_inet_pton(), and both decode to the same address. */
83 #define test_pton6_same(a,b) STMT_BEGIN \
84 test_eq(tor_inet_pton(AF_INET6, a, &a1), 1); \
85 test_eq(tor_inet_pton(AF_INET6, b, &a2), 1); \
86 _test_op_ip6(&a1,==,&a2,#a,#b); \
87 STMT_END
89 /** Helper: Assert that <b>a</b> is recognized as a bad IPv6 address by
90 * tor_inet_pton(). */
91 #define test_pton6_bad(a) \
92 test_eq(0, tor_inet_pton(AF_INET6, a, &a1))
94 /** Helper: assert that <b>a</b>, when parsed by tor_inet_pton() and displayed
95 * with tor_inet_ntop(), yields <b>b</b>. Also assert that <b>b</b> parses to
96 * the same value as <b>a</b>. */
97 #define test_ntop6_reduces(a,b) STMT_BEGIN \
98 test_eq(tor_inet_pton(AF_INET6, a, &a1), 1); \
99 test_streq(tor_inet_ntop(AF_INET6, &a1, buf, sizeof(buf)), b); \
100 test_eq(tor_inet_pton(AF_INET6, b, &a2), 1); \
101 _test_op_ip6(&a1, ==, &a2, a, b); \
102 STMT_END
104 /** Helper: assert that <b>a</b> parses by tor_inet_pton() into a address that
105 * passes tor_addr_is_internal() with <b>for_listening</b>. */
106 #define test_internal_ip(a,for_listening) STMT_BEGIN \
107 test_eq(tor_inet_pton(AF_INET6, a, &t1.addr.in6_addr), 1); \
108 t1.family = AF_INET6; \
109 if (!tor_addr_is_internal(&t1, for_listening)) \
110 test_fail_msg( a "was not internal."); \
111 STMT_END
113 /** Helper: assert that <b>a</b> parses by tor_inet_pton() into a address that
114 * does not pass tor_addr_is_internal() with <b>for_listening</b>. */
115 #define test_external_ip(a,for_listening) STMT_BEGIN \
116 test_eq(tor_inet_pton(AF_INET6, a, &t1.addr.in6_addr), 1); \
117 t1.family = AF_INET6; \
118 if (tor_addr_is_internal(&t1, for_listening)) \
119 test_fail_msg(a "was not external."); \
120 STMT_END
122 /** Helper: Assert that <b>a</b> and <b>b</b>, when parsed by
123 * tor_inet_pton(), give addresses that compare in the order defined by
124 * <b>op</b> with tor_addr_compare(). */
125 #define test_addr_compare(a, op, b) STMT_BEGIN \
126 test_eq(tor_inet_pton(AF_INET6, a, &t1.addr.in6_addr), 1); \
127 test_eq(tor_inet_pton(AF_INET6, b, &t2.addr.in6_addr), 1); \
128 t1.family = t2.family = AF_INET6; \
129 r = tor_addr_compare(&t1,&t2,CMP_SEMANTIC); \
130 if (!(r op 0)) \
131 test_fail_msg("failed: tor_addr_compare("a","b") "#op" 0"); \
132 STMT_END
134 /** Helper: Assert that <b>a</b> and <b>b</b>, when parsed by
135 * tor_inet_pton(), give addresses that compare in the order defined by
136 * <b>op</b> with tor_addr_compare_masked() with <b>m</b> masked. */
137 #define test_addr_compare_masked(a, op, b, m) STMT_BEGIN \
138 test_eq(tor_inet_pton(AF_INET6, a, &t1.addr.in6_addr), 1); \
139 test_eq(tor_inet_pton(AF_INET6, b, &t2.addr.in6_addr), 1); \
140 t1.family = t2.family = AF_INET6; \
141 r = tor_addr_compare_masked(&t1,&t2,m,CMP_SEMANTIC); \
142 if (!(r op 0)) \
143 test_fail_msg("failed: tor_addr_compare_masked("a","b","#m") "#op" 0"); \
144 STMT_END
146 /** Helper: assert that <b>xx</b> is parseable as a masked IPv6 address with
147 * ports by tor_parse_mask_addr_ports(), with family <b>f</b>, IP address
148 * as 4 32-bit words <b>ip1...ip4</b>, mask bits as <b>mm</b>, and port range
149 * as <b>pt1..pt2</b>. */
150 #define test_addr_mask_ports_parse(xx, f, ip1, ip2, ip3, ip4, mm, pt1, pt2) \
151 STMT_BEGIN \
152 test_eq(tor_addr_parse_mask_ports(xx, &t1, &mask, &port1, &port2), f); \
153 p1=tor_inet_ntop(AF_INET6, &t1.addr.in6_addr, bug, sizeof(bug)); \
154 test_eq(htonl(ip1), tor_addr_to_in6_addr32(&t1)[0]); \
155 test_eq(htonl(ip2), tor_addr_to_in6_addr32(&t1)[1]); \
156 test_eq(htonl(ip3), tor_addr_to_in6_addr32(&t1)[2]); \
157 test_eq(htonl(ip4), tor_addr_to_in6_addr32(&t1)[3]); \
158 test_eq(mask, mm); \
159 test_eq(port1, pt1); \
160 test_eq(port2, pt2); \
161 STMT_END
163 /** Run unit tests for IPv6 encoding/decoding/manipulation functions. */
164 static void
165 test_addr_ip6_helpers(void)
167 char buf[TOR_ADDR_BUF_LEN], bug[TOR_ADDR_BUF_LEN];
168 struct in6_addr a1, a2;
169 tor_addr_t t1, t2;
170 int r, i;
171 uint16_t port1, port2;
172 maskbits_t mask;
173 const char *p1;
174 struct sockaddr_storage sa_storage;
175 struct sockaddr_in *sin;
176 struct sockaddr_in6 *sin6;
178 // struct in_addr b1, b2;
179 /* Test tor_inet_ntop and tor_inet_pton: IPv6 */
181 /* ==== Converting to and from sockaddr_t. */
182 sin = (struct sockaddr_in *)&sa_storage;
183 sin->sin_family = AF_INET;
184 sin->sin_port = 9090;
185 sin->sin_addr.s_addr = htonl(0x7f7f0102); /*127.127.1.2*/
186 tor_addr_from_sockaddr(&t1, (struct sockaddr *)sin, NULL);
187 test_eq(tor_addr_family(&t1), AF_INET);
188 test_eq(tor_addr_to_ipv4h(&t1), 0x7f7f0102);
190 memset(&sa_storage, 0, sizeof(sa_storage));
191 test_eq(sizeof(struct sockaddr_in),
192 tor_addr_to_sockaddr(&t1, 1234, (struct sockaddr *)&sa_storage,
193 sizeof(sa_storage)));
194 test_eq(1234, ntohs(sin->sin_port));
195 test_eq(0x7f7f0102, ntohl(sin->sin_addr.s_addr));
197 memset(&sa_storage, 0, sizeof(sa_storage));
198 sin6 = (struct sockaddr_in6 *)&sa_storage;
199 sin6->sin6_family = AF_INET6;
200 sin6->sin6_port = htons(7070);
201 sin6->sin6_addr.s6_addr[0] = 128;
202 tor_addr_from_sockaddr(&t1, (struct sockaddr *)sin6, NULL);
203 test_eq(tor_addr_family(&t1), AF_INET6);
204 p1 = tor_addr_to_str(buf, &t1, sizeof(buf), 0);
205 test_streq(p1, "8000::");
207 memset(&sa_storage, 0, sizeof(sa_storage));
208 test_eq(sizeof(struct sockaddr_in6),
209 tor_addr_to_sockaddr(&t1, 9999, (struct sockaddr *)&sa_storage,
210 sizeof(sa_storage)));
211 test_eq(AF_INET6, sin6->sin6_family);
212 test_eq(9999, ntohs(sin6->sin6_port));
213 test_eq(0x80000000, ntohl(S6_ADDR32(sin6->sin6_addr)[0]));
215 /* ==== tor_addr_lookup: static cases. (Can't test dns without knowing we
216 * have a good resolver. */
217 test_eq(0, tor_addr_lookup("127.128.129.130", AF_UNSPEC, &t1));
218 test_eq(AF_INET, tor_addr_family(&t1));
219 test_eq(tor_addr_to_ipv4h(&t1), 0x7f808182);
221 test_eq(0, tor_addr_lookup("9000::5", AF_UNSPEC, &t1));
222 test_eq(AF_INET6, tor_addr_family(&t1));
223 test_eq(0x90, tor_addr_to_in6_addr8(&t1)[0]);
224 test_assert(tor_mem_is_zero((char*)tor_addr_to_in6_addr8(&t1)+1, 14));
225 test_eq(0x05, tor_addr_to_in6_addr8(&t1)[15]);
227 /* === Test pton: valid af_inet6 */
228 /* Simple, valid parsing. */
229 r = tor_inet_pton(AF_INET6,
230 "0102:0304:0506:0708:090A:0B0C:0D0E:0F10", &a1);
231 test_assert(r==1);
232 for (i=0;i<16;++i) { test_eq(i+1, (int)a1.s6_addr[i]); }
233 /* ipv4 ending. */
234 test_pton6_same("0102:0304:0506:0708:090A:0B0C:0D0E:0F10",
235 "0102:0304:0506:0708:090A:0B0C:13.14.15.16");
236 /* shortened words. */
237 test_pton6_same("0001:0099:BEEF:0000:0123:FFFF:0001:0001",
238 "1:99:BEEF:0:0123:FFFF:1:1");
239 /* zeros at the beginning */
240 test_pton6_same("0000:0000:0000:0000:0009:C0A8:0001:0001",
241 "::9:c0a8:1:1");
242 test_pton6_same("0000:0000:0000:0000:0009:C0A8:0001:0001",
243 "::9:c0a8:0.1.0.1");
244 /* zeros in the middle. */
245 test_pton6_same("fe80:0000:0000:0000:0202:1111:0001:0001",
246 "fe80::202:1111:1:1");
247 /* zeros at the end. */
248 test_pton6_same("1000:0001:0000:0007:0000:0000:0000:0000",
249 "1000:1:0:7::");
251 /* === Test ntop: af_inet6 */
252 test_ntop6_reduces("0:0:0:0:0:0:0:0", "::");
254 test_ntop6_reduces("0001:0099:BEEF:0006:0123:FFFF:0001:0001",
255 "1:99:beef:6:123:ffff:1:1");
257 //test_ntop6_reduces("0:0:0:0:0:0:c0a8:0101", "::192.168.1.1");
258 test_ntop6_reduces("0:0:0:0:0:ffff:c0a8:0101", "::ffff:192.168.1.1");
259 test_ntop6_reduces("002:0:0000:0:3::4", "2::3:0:0:4");
260 test_ntop6_reduces("0:0::1:0:3", "::1:0:3");
261 test_ntop6_reduces("008:0::0", "8::");
262 test_ntop6_reduces("0:0:0:0:0:ffff::1", "::ffff:0.0.0.1");
263 test_ntop6_reduces("abcd:0:0:0:0:0:7f00::", "abcd::7f00:0");
264 test_ntop6_reduces("0000:0000:0000:0000:0009:C0A8:0001:0001",
265 "::9:c0a8:1:1");
266 test_ntop6_reduces("fe80:0000:0000:0000:0202:1111:0001:0001",
267 "fe80::202:1111:1:1");
268 test_ntop6_reduces("1000:0001:0000:0007:0000:0000:0000:0000",
269 "1000:1:0:7::");
271 /* === Test pton: invalid in6. */
272 test_pton6_bad("foobar.");
273 test_pton6_bad("55555::");
274 test_pton6_bad("9:-60::");
275 test_pton6_bad("1:2:33333:4:0002:3::");
276 //test_pton6_bad("1:2:3333:4:00002:3::");// BAD, but glibc doesn't say so.
277 test_pton6_bad("1:2:3333:4:fish:3::");
278 test_pton6_bad("1:2:3:4:5:6:7:8:9");
279 test_pton6_bad("1:2:3:4:5:6:7");
280 test_pton6_bad("1:2:3:4:5:6:1.2.3.4.5");
281 test_pton6_bad("1:2:3:4:5:6:1.2.3");
282 test_pton6_bad("::1.2.3");
283 test_pton6_bad("::1.2.3.4.5");
284 test_pton6_bad("99");
285 test_pton6_bad("");
286 test_pton6_bad("1::2::3:4");
287 test_pton6_bad("a:::b:c");
288 test_pton6_bad(":::a:b:c");
289 test_pton6_bad("a:b:c:::");
291 /* test internal checking */
292 test_external_ip("fbff:ffff::2:7", 0);
293 test_internal_ip("fc01::2:7", 0);
294 test_internal_ip("fdff:ffff::f:f", 0);
295 test_external_ip("fe00::3:f", 0);
297 test_external_ip("fe7f:ffff::2:7", 0);
298 test_internal_ip("fe80::2:7", 0);
299 test_internal_ip("febf:ffff::f:f", 0);
301 test_internal_ip("fec0::2:7:7", 0);
302 test_internal_ip("feff:ffff::e:7:7", 0);
303 test_external_ip("ff00::e:7:7", 0);
305 test_internal_ip("::", 0);
306 test_internal_ip("::1", 0);
307 test_internal_ip("::1", 1);
308 test_internal_ip("::", 0);
309 test_external_ip("::", 1);
310 test_external_ip("::2", 0);
311 test_external_ip("2001::", 0);
312 test_external_ip("ffff::", 0);
314 test_external_ip("::ffff:0.0.0.0", 1);
315 test_internal_ip("::ffff:0.0.0.0", 0);
316 test_internal_ip("::ffff:0.255.255.255", 0);
317 test_external_ip("::ffff:1.0.0.0", 0);
319 test_external_ip("::ffff:9.255.255.255", 0);
320 test_internal_ip("::ffff:10.0.0.0", 0);
321 test_internal_ip("::ffff:10.255.255.255", 0);
322 test_external_ip("::ffff:11.0.0.0", 0);
324 test_external_ip("::ffff:126.255.255.255", 0);
325 test_internal_ip("::ffff:127.0.0.0", 0);
326 test_internal_ip("::ffff:127.255.255.255", 0);
327 test_external_ip("::ffff:128.0.0.0", 0);
329 test_external_ip("::ffff:172.15.255.255", 0);
330 test_internal_ip("::ffff:172.16.0.0", 0);
331 test_internal_ip("::ffff:172.31.255.255", 0);
332 test_external_ip("::ffff:172.32.0.0", 0);
334 test_external_ip("::ffff:192.167.255.255", 0);
335 test_internal_ip("::ffff:192.168.0.0", 0);
336 test_internal_ip("::ffff:192.168.255.255", 0);
337 test_external_ip("::ffff:192.169.0.0", 0);
339 test_external_ip("::ffff:169.253.255.255", 0);
340 test_internal_ip("::ffff:169.254.0.0", 0);
341 test_internal_ip("::ffff:169.254.255.255", 0);
342 test_external_ip("::ffff:169.255.0.0", 0);
343 test_assert(is_internal_IP(0x7f000001, 0));
345 /* tor_addr_compare(tor_addr_t x2) */
346 test_addr_compare("ffff::", ==, "ffff::0");
347 test_addr_compare("0::3:2:1", <, "0::ffff:0.3.2.1");
348 test_addr_compare("0::2:2:1", <, "0::ffff:0.3.2.1");
349 test_addr_compare("0::ffff:0.3.2.1", >, "0::0:0:0");
350 test_addr_compare("0::ffff:5.2.2.1", <, "::ffff:6.0.0.0"); /* XXXX wrong. */
351 tor_addr_parse_mask_ports("[::ffff:2.3.4.5]", &t1, NULL, NULL, NULL);
352 tor_addr_parse_mask_ports("2.3.4.5", &t2, NULL, NULL, NULL);
353 test_assert(tor_addr_compare(&t1, &t2, CMP_SEMANTIC) == 0);
354 tor_addr_parse_mask_ports("[::ffff:2.3.4.4]", &t1, NULL, NULL, NULL);
355 tor_addr_parse_mask_ports("2.3.4.5", &t2, NULL, NULL, NULL);
356 test_assert(tor_addr_compare(&t1, &t2, CMP_SEMANTIC) < 0);
358 /* test compare_masked */
359 test_addr_compare_masked("ffff::", ==, "ffff::0", 128);
360 test_addr_compare_masked("ffff::", ==, "ffff::0", 64);
361 test_addr_compare_masked("0::2:2:1", <, "0::8000:2:1", 81);
362 test_addr_compare_masked("0::2:2:1", ==, "0::8000:2:1", 80);
364 /* Test decorated addr_to_string. */
365 test_eq(AF_INET6, tor_addr_from_str(&t1, "[123:45:6789::5005:11]"));
366 p1 = tor_addr_to_str(buf, &t1, sizeof(buf), 1);
367 test_streq(p1, "[123:45:6789::5005:11]");
368 test_eq(AF_INET, tor_addr_from_str(&t1, "18.0.0.1"));
369 p1 = tor_addr_to_str(buf, &t1, sizeof(buf), 1);
370 test_streq(p1, "18.0.0.1");
372 /* Test tor_addr_parse_reverse_lookup_name */
373 i = tor_addr_parse_reverse_lookup_name(&t1, "Foobar.baz", AF_UNSPEC, 0);
374 test_eq(0, i);
375 i = tor_addr_parse_reverse_lookup_name(&t1, "Foobar.baz", AF_UNSPEC, 1);
376 test_eq(0, i);
377 i = tor_addr_parse_reverse_lookup_name(&t1, "1.0.168.192.in-addr.arpa",
378 AF_UNSPEC, 1);
379 test_eq(1, i);
380 test_eq(tor_addr_family(&t1), AF_INET);
381 p1 = tor_addr_to_str(buf, &t1, sizeof(buf), 1);
382 test_streq(p1, "192.168.0.1");
383 i = tor_addr_parse_reverse_lookup_name(&t1, "192.168.0.99", AF_UNSPEC, 0);
384 test_eq(0, i);
385 i = tor_addr_parse_reverse_lookup_name(&t1, "192.168.0.99", AF_UNSPEC, 1);
386 test_eq(1, i);
387 p1 = tor_addr_to_str(buf, &t1, sizeof(buf), 1);
388 test_streq(p1, "192.168.0.99");
389 memset(&t1, 0, sizeof(t1));
390 i = tor_addr_parse_reverse_lookup_name(&t1,
391 "0.1.2.3.4.5.6.7.8.9.a.b.c.d.e.f."
392 "f.e.e.b.1.e.b.e.e.f.f.e.e.e.d.9."
393 "ip6.ARPA",
394 AF_UNSPEC, 0);
395 test_eq(1, i);
396 p1 = tor_addr_to_str(buf, &t1, sizeof(buf), 1);
397 test_streq(p1, "[9dee:effe:ebe1:beef:fedc:ba98:7654:3210]");
398 /* Failing cases. */
399 i = tor_addr_parse_reverse_lookup_name(&t1,
400 "6.7.8.9.a.b.c.d.e.f."
401 "f.e.e.b.1.e.b.e.e.f.f.e.e.e.d.9."
402 "ip6.ARPA",
403 AF_UNSPEC, 0);
404 test_eq(i, -1);
405 i = tor_addr_parse_reverse_lookup_name(&t1,
406 "6.7.8.9.a.b.c.d.e.f.a.b.c.d.e.f.0."
407 "f.e.e.b.1.e.b.e.e.f.f.e.e.e.d.9."
408 "ip6.ARPA",
409 AF_UNSPEC, 0);
410 test_eq(i, -1);
411 i = tor_addr_parse_reverse_lookup_name(&t1,
412 "6.7.8.9.a.b.c.d.e.f.X.0.0.0.0.9."
413 "f.e.e.b.1.e.b.e.e.f.f.e.e.e.d.9."
414 "ip6.ARPA",
415 AF_UNSPEC, 0);
416 test_eq(i, -1);
417 i = tor_addr_parse_reverse_lookup_name(&t1, "32.1.1.in-addr.arpa",
418 AF_UNSPEC, 0);
419 test_eq(i, -1);
420 i = tor_addr_parse_reverse_lookup_name(&t1, ".in-addr.arpa",
421 AF_UNSPEC, 0);
422 test_eq(i, -1);
423 i = tor_addr_parse_reverse_lookup_name(&t1, "1.2.3.4.5.in-addr.arpa",
424 AF_UNSPEC, 0);
425 test_eq(i, -1);
426 i = tor_addr_parse_reverse_lookup_name(&t1, "1.2.3.4.5.in-addr.arpa",
427 AF_INET6, 0);
428 test_eq(i, -1);
429 i = tor_addr_parse_reverse_lookup_name(&t1,
430 "6.7.8.9.a.b.c.d.e.f.a.b.c.d.e.0."
431 "f.e.e.b.1.e.b.e.e.f.f.e.e.e.d.9."
432 "ip6.ARPA",
433 AF_INET, 0);
434 test_eq(i, -1);
436 /* test tor_addr_parse_mask_ports */
437 test_addr_mask_ports_parse("[::f]/17:47-95", AF_INET6,
438 0, 0, 0, 0x0000000f, 17, 47, 95);
439 //test_addr_parse("[::fefe:4.1.1.7/120]:999-1000");
440 //test_addr_parse_check("::fefe:401:107", 120, 999, 1000);
441 test_addr_mask_ports_parse("[::ffff:4.1.1.7]/120:443", AF_INET6,
442 0, 0, 0x0000ffff, 0x04010107, 120, 443, 443);
443 test_addr_mask_ports_parse("[abcd:2::44a:0]:2-65000", AF_INET6,
444 0xabcd0002, 0, 0, 0x044a0000, 128, 2, 65000);
446 r=tor_addr_parse_mask_ports("[fefef::]/112", &t1, NULL, NULL, NULL);
447 test_assert(r == -1);
448 r=tor_addr_parse_mask_ports("efef::/112", &t1, NULL, NULL, NULL);
449 test_assert(r == -1);
450 r=tor_addr_parse_mask_ports("[f:f:f:f:f:f:f:f::]", &t1, NULL, NULL, NULL);
451 test_assert(r == -1);
452 r=tor_addr_parse_mask_ports("[::f:f:f:f:f:f:f:f]", &t1, NULL, NULL, NULL);
453 test_assert(r == -1);
454 r=tor_addr_parse_mask_ports("[f:f:f:f:f:f:f:f:f]", &t1, NULL, NULL, NULL);
455 test_assert(r == -1);
456 /* Test for V4-mapped address with mask < 96. (arguably not valid) */
457 r=tor_addr_parse_mask_ports("[::ffff:1.1.2.2/33]", &t1, &mask, NULL, NULL);
458 test_assert(r == -1);
459 r=tor_addr_parse_mask_ports("1.1.2.2/33", &t1, &mask, NULL, NULL);
460 test_assert(r == -1);
461 r=tor_addr_parse_mask_ports("1.1.2.2/31", &t1, &mask, NULL, NULL);
462 test_assert(r == AF_INET);
463 r=tor_addr_parse_mask_ports("[efef::]/112", &t1, &mask, &port1, &port2);
464 test_assert(r == AF_INET6);
465 test_assert(port1 == 1);
466 test_assert(port2 == 65535);
468 /* make sure inet address lengths >= max */
469 test_assert(INET_NTOA_BUF_LEN >= sizeof("255.255.255.255"));
470 test_assert(TOR_ADDR_BUF_LEN >=
471 sizeof("ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255"));
473 test_assert(sizeof(tor_addr_t) >= sizeof(struct in6_addr));
475 /* get interface addresses */
476 r = get_interface_address6(LOG_DEBUG, AF_INET, &t1);
477 i = get_interface_address6(LOG_DEBUG, AF_INET6, &t2);
478 #if 0
479 tor_inet_ntop(AF_INET, &t1.sa.sin_addr, buf, sizeof(buf));
480 printf("\nv4 address: %s (family=%i)", buf, IN_FAMILY(&t1));
481 tor_inet_ntop(AF_INET6, &t2.sa6.sin6_addr, buf, sizeof(buf));
482 printf("\nv6 address: %s (family=%i)", buf, IN_FAMILY(&t2));
483 #endif
485 done:
489 #define ADDR_LEGACY(name) \
490 { #name, legacy_test_helper, 0, &legacy_setup, test_addr_ ## name }
492 struct testcase_t addr_tests[] = {
493 ADDR_LEGACY(basic),
494 ADDR_LEGACY(ip6_helpers),
495 END_OF_TESTCASES