1 /* Copyright (c) 2001-2004, Roger Dingledine.
2 * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
3 * Copyright (c) 2007-2013, The Tor Project, Inc. */
4 /* See LICENSE for licensing information */
6 #define ADDRESSMAP_PRIVATE
10 #include "addressmap.h"
19 /* Test addr_port_lookup */
20 cp
= NULL
; u32
= 3; u16
= 3;
21 test_assert(!addr_port_lookup(LOG_WARN
, "1.2.3.4", &cp
, &u32
, &u16
));
22 test_streq(cp
, "1.2.3.4");
23 test_eq(u32
, 0x01020304u
);
26 test_assert(!addr_port_lookup(LOG_WARN
, "4.3.2.1:99", &cp
, &u32
, &u16
));
27 test_streq(cp
, "4.3.2.1");
28 test_eq(u32
, 0x04030201u
);
31 test_assert(!addr_port_lookup(LOG_WARN
, "nonexistent.address:4040",
33 test_streq(cp
, "nonexistent.address");
36 test_assert(!addr_port_lookup(LOG_WARN
, "localhost:9999", &cp
, &u32
, &u16
));
37 test_streq(cp
, "localhost");
38 test_eq(u32
, 0x7f000001u
);
42 test_assert(!addr_port_lookup(LOG_WARN
, "localhost", NULL
, &u32
, &u16
));
43 test_eq_ptr(cp
, NULL
);
44 test_eq(u32
, 0x7f000001u
);
47 test_eq(0, addr_mask_get_bits(0x0u
));
48 test_eq(32, addr_mask_get_bits(0xFFFFFFFFu
));
49 test_eq(16, addr_mask_get_bits(0xFFFF0000u
));
50 test_eq(31, addr_mask_get_bits(0xFFFFFFFEu
));
51 test_eq(1, addr_mask_get_bits(0x80000000u
));
55 char tmpbuf
[TOR_ADDR_BUF_LEN
];
56 const char *ip
= "176.192.208.224";
60 test_eq(tor_inet_pton(AF_INET
, ip
, &in
), 1);
61 test_eq_ptr(tor_inet_ntop(AF_INET
, &in
, tmpbuf
, sizeof(tmpbuf
)), &tmpbuf
);
62 test_streq(tmpbuf
, ip
);
64 /* just enough buffer length */
65 test_streq(tor_inet_ntop(AF_INET
, &in
, tmpbuf
, strlen(ip
) + 1), ip
);
67 /* too short buffer */
68 test_eq_ptr(tor_inet_ntop(AF_INET
, &in
, tmpbuf
, strlen(ip
)), NULL
);
75 #define test_op_ip6_(a,op,b,e1,e2) \
77 tt_assert_test_fmt_type(a,b,e1" "#op" "e2,struct in6_addr*, \
78 (memcmp(val1_->s6_addr, val2_->s6_addr, 16) op 0), \
81 cp = print_ = tor_malloc(64); \
82 for (i=0;i<16;++i) { \
83 tor_snprintf(cp, 3,"%02x", (unsigned)value_->s6_addr[i]);\
85 if (i != 15) *cp++ = ':'; \
88 { tor_free(print_); }, \
89 TT_EXIT_TEST_FUNCTION \
93 /** Helper: Assert that two strings both decode as IPv6 addresses with
94 * tor_inet_pton(), and both decode to the same address. */
95 #define test_pton6_same(a,b) STMT_BEGIN \
96 test_eq(tor_inet_pton(AF_INET6, a, &a1), 1); \
97 test_eq(tor_inet_pton(AF_INET6, b, &a2), 1); \
98 test_op_ip6_(&a1,==,&a2,#a,#b); \
101 /** Helper: Assert that <b>a</b> is recognized as a bad IPv6 address by
102 * tor_inet_pton(). */
103 #define test_pton6_bad(a) \
104 test_eq(0, tor_inet_pton(AF_INET6, a, &a1))
106 /** Helper: assert that <b>a</b>, when parsed by tor_inet_pton() and displayed
107 * with tor_inet_ntop(), yields <b>b</b>. Also assert that <b>b</b> parses to
108 * the same value as <b>a</b>. */
109 #define test_ntop6_reduces(a,b) STMT_BEGIN \
110 test_eq(tor_inet_pton(AF_INET6, a, &a1), 1); \
111 test_streq(tor_inet_ntop(AF_INET6, &a1, buf, sizeof(buf)), b); \
112 test_eq(tor_inet_pton(AF_INET6, b, &a2), 1); \
113 test_op_ip6_(&a1, ==, &a2, a, b); \
116 /** Helper: assert that <b>a</b> parses by tor_inet_pton() into a address that
117 * passes tor_addr_is_internal() with <b>for_listening</b>. */
118 #define test_internal_ip(a,for_listening) STMT_BEGIN \
119 test_eq(tor_inet_pton(AF_INET6, a, &t1.addr.in6_addr), 1); \
120 t1.family = AF_INET6; \
121 if (!tor_addr_is_internal(&t1, for_listening)) \
122 test_fail_msg( a "was not internal."); \
125 /** Helper: assert that <b>a</b> parses by tor_inet_pton() into a address that
126 * does not pass tor_addr_is_internal() with <b>for_listening</b>. */
127 #define test_external_ip(a,for_listening) STMT_BEGIN \
128 test_eq(tor_inet_pton(AF_INET6, a, &t1.addr.in6_addr), 1); \
129 t1.family = AF_INET6; \
130 if (tor_addr_is_internal(&t1, for_listening)) \
131 test_fail_msg(a "was not external."); \
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(). */
137 #define test_addr_compare(a, op, b) 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(&t1,&t2,CMP_SEMANTIC); \
143 test_fail_msg("failed: tor_addr_compare("a","b") "#op" 0"); \
146 /** Helper: Assert that <b>a</b> and <b>b</b>, when parsed by
147 * tor_inet_pton(), give addresses that compare in the order defined by
148 * <b>op</b> with tor_addr_compare_masked() with <b>m</b> masked. */
149 #define test_addr_compare_masked(a, op, b, m) STMT_BEGIN \
150 test_eq(tor_inet_pton(AF_INET6, a, &t1.addr.in6_addr), 1); \
151 test_eq(tor_inet_pton(AF_INET6, b, &t2.addr.in6_addr), 1); \
152 t1.family = t2.family = AF_INET6; \
153 r = tor_addr_compare_masked(&t1,&t2,m,CMP_SEMANTIC); \
155 test_fail_msg("failed: tor_addr_compare_masked("a","b","#m") "#op" 0"); \
158 /** Helper: assert that <b>xx</b> is parseable as a masked IPv6 address with
159 * ports by tor_parse_mask_addr_ports(), with family <b>f</b>, IP address
160 * as 4 32-bit words <b>ip1...ip4</b>, mask bits as <b>mm</b>, and port range
161 * as <b>pt1..pt2</b>. */
162 #define test_addr_mask_ports_parse(xx, f, ip1, ip2, ip3, ip4, mm, pt1, pt2) \
164 test_eq(tor_addr_parse_mask_ports(xx, 0, &t1, &mask, &port1, &port2), \
166 p1=tor_inet_ntop(AF_INET6, &t1.addr.in6_addr, bug, sizeof(bug)); \
167 test_eq(htonl(ip1), tor_addr_to_in6_addr32(&t1)[0]); \
168 test_eq(htonl(ip2), tor_addr_to_in6_addr32(&t1)[1]); \
169 test_eq(htonl(ip3), tor_addr_to_in6_addr32(&t1)[2]); \
170 test_eq(htonl(ip4), tor_addr_to_in6_addr32(&t1)[3]); \
172 test_eq(port1, pt1); \
173 test_eq(port2, pt2); \
176 /** Run unit tests for IPv6 encoding/decoding/manipulation functions. */
178 test_addr_ip6_helpers(void)
180 char buf
[TOR_ADDR_BUF_LEN
], bug
[TOR_ADDR_BUF_LEN
];
181 char rbuf
[REVERSE_LOOKUP_NAME_BUF_LEN
];
182 struct in6_addr a1
, a2
;
185 uint16_t port1
, port2
;
188 struct sockaddr_storage sa_storage
;
189 struct sockaddr_in
*sin
;
190 struct sockaddr_in6
*sin6
;
192 /* Test tor_inet_ntop and tor_inet_pton: IPv6 */
194 const char *ip
= "2001::1234";
195 const char *ip_ffff
= "::ffff:192.168.1.2";
197 /* good round trip */
198 test_eq(tor_inet_pton(AF_INET6
, ip
, &a1
), 1);
199 test_eq_ptr(tor_inet_ntop(AF_INET6
, &a1
, buf
, sizeof(buf
)), &buf
);
202 /* good round trip - ::ffff:0:0 style */
203 test_eq(tor_inet_pton(AF_INET6
, ip_ffff
, &a2
), 1);
204 test_eq_ptr(tor_inet_ntop(AF_INET6
, &a2
, buf
, sizeof(buf
)), &buf
);
205 test_streq(buf
, ip_ffff
);
207 /* just long enough buffer (remember \0) */
208 test_streq(tor_inet_ntop(AF_INET6
, &a1
, buf
, strlen(ip
)+1), ip
);
209 test_streq(tor_inet_ntop(AF_INET6
, &a2
, buf
, strlen(ip_ffff
)+1),
212 /* too short buffer (remember \0) */
213 test_eq_ptr(tor_inet_ntop(AF_INET6
, &a1
, buf
, strlen(ip
)), NULL
);
214 test_eq_ptr(tor_inet_ntop(AF_INET6
, &a2
, buf
, strlen(ip_ffff
)), NULL
);
217 /* ==== Converting to and from sockaddr_t. */
218 sin
= (struct sockaddr_in
*)&sa_storage
;
219 sin
->sin_family
= AF_INET
;
220 sin
->sin_port
= 9090;
221 sin
->sin_addr
.s_addr
= htonl(0x7f7f0102); /*127.127.1.2*/
222 tor_addr_from_sockaddr(&t1
, (struct sockaddr
*)sin
, NULL
);
223 test_eq(tor_addr_family(&t1
), AF_INET
);
224 test_eq(tor_addr_to_ipv4h(&t1
), 0x7f7f0102);
226 memset(&sa_storage
, 0, sizeof(sa_storage
));
227 test_eq(sizeof(struct sockaddr_in
),
228 tor_addr_to_sockaddr(&t1
, 1234, (struct sockaddr
*)&sa_storage
,
229 sizeof(sa_storage
)));
230 test_eq(1234, ntohs(sin
->sin_port
));
231 test_eq(0x7f7f0102, ntohl(sin
->sin_addr
.s_addr
));
233 memset(&sa_storage
, 0, sizeof(sa_storage
));
234 sin6
= (struct sockaddr_in6
*)&sa_storage
;
235 sin6
->sin6_family
= AF_INET6
;
236 sin6
->sin6_port
= htons(7070);
237 sin6
->sin6_addr
.s6_addr
[0] = 128;
238 tor_addr_from_sockaddr(&t1
, (struct sockaddr
*)sin6
, NULL
);
239 test_eq(tor_addr_family(&t1
), AF_INET6
);
240 p1
= tor_addr_to_str(buf
, &t1
, sizeof(buf
), 0);
241 test_streq(p1
, "8000::");
243 memset(&sa_storage
, 0, sizeof(sa_storage
));
244 test_eq(sizeof(struct sockaddr_in6
),
245 tor_addr_to_sockaddr(&t1
, 9999, (struct sockaddr
*)&sa_storage
,
246 sizeof(sa_storage
)));
247 test_eq(AF_INET6
, sin6
->sin6_family
);
248 test_eq(9999, ntohs(sin6
->sin6_port
));
249 test_eq(0x80000000, ntohl(S6_ADDR32(sin6
->sin6_addr
)[0]));
251 /* ==== tor_addr_lookup: static cases. (Can't test dns without knowing we
252 * have a good resolver. */
253 test_eq(0, tor_addr_lookup("127.128.129.130", AF_UNSPEC
, &t1
));
254 test_eq(AF_INET
, tor_addr_family(&t1
));
255 test_eq(tor_addr_to_ipv4h(&t1
), 0x7f808182);
257 test_eq(0, tor_addr_lookup("9000::5", AF_UNSPEC
, &t1
));
258 test_eq(AF_INET6
, tor_addr_family(&t1
));
259 test_eq(0x90, tor_addr_to_in6_addr8(&t1
)[0]);
260 test_assert(tor_mem_is_zero((char*)tor_addr_to_in6_addr8(&t1
)+1, 14));
261 test_eq(0x05, tor_addr_to_in6_addr8(&t1
)[15]);
263 /* === Test pton: valid af_inet6 */
264 /* Simple, valid parsing. */
265 r
= tor_inet_pton(AF_INET6
,
266 "0102:0304:0506:0708:090A:0B0C:0D0E:0F10", &a1
);
268 for (i
=0;i
<16;++i
) { test_eq(i
+1, (int)a1
.s6_addr
[i
]); }
270 test_pton6_same("0102:0304:0506:0708:090A:0B0C:0D0E:0F10",
271 "0102:0304:0506:0708:090A:0B0C:13.14.15.16");
272 /* shortened words. */
273 test_pton6_same("0001:0099:BEEF:0000:0123:FFFF:0001:0001",
274 "1:99:BEEF:0:0123:FFFF:1:1");
275 /* zeros at the beginning */
276 test_pton6_same("0000:0000:0000:0000:0009:C0A8:0001:0001",
278 test_pton6_same("0000:0000:0000:0000:0009:C0A8:0001:0001",
280 /* zeros in the middle. */
281 test_pton6_same("fe80:0000:0000:0000:0202:1111:0001:0001",
282 "fe80::202:1111:1:1");
283 /* zeros at the end. */
284 test_pton6_same("1000:0001:0000:0007:0000:0000:0000:0000",
287 /* === Test ntop: af_inet6 */
288 test_ntop6_reduces("0:0:0:0:0:0:0:0", "::");
290 test_ntop6_reduces("0001:0099:BEEF:0006:0123:FFFF:0001:0001",
291 "1:99:beef:6:123:ffff:1:1");
293 //test_ntop6_reduces("0:0:0:0:0:0:c0a8:0101", "::192.168.1.1");
294 test_ntop6_reduces("0:0:0:0:0:ffff:c0a8:0101", "::ffff:192.168.1.1");
295 test_ntop6_reduces("002:0:0000:0:3::4", "2::3:0:0:4");
296 test_ntop6_reduces("0:0::1:0:3", "::1:0:3");
297 test_ntop6_reduces("008:0::0", "8::");
298 test_ntop6_reduces("0:0:0:0:0:ffff::1", "::ffff:0.0.0.1");
299 test_ntop6_reduces("abcd:0:0:0:0:0:7f00::", "abcd::7f00:0");
300 test_ntop6_reduces("0000:0000:0000:0000:0009:C0A8:0001:0001",
302 test_ntop6_reduces("fe80:0000:0000:0000:0202:1111:0001:0001",
303 "fe80::202:1111:1:1");
304 test_ntop6_reduces("1000:0001:0000:0007:0000:0000:0000:0000",
308 test_eq(tor_inet_pton(AF_UNSPEC
, 0, 0), -1);
310 /* === Test pton: invalid in6. */
311 test_pton6_bad("foobar.");
312 test_pton6_bad("-1::");
313 test_pton6_bad("00001::");
314 test_pton6_bad("10000::");
315 test_pton6_bad("::10000");
316 test_pton6_bad("55555::");
317 test_pton6_bad("9:-60::");
318 test_pton6_bad("9:+60::");
319 test_pton6_bad("9|60::");
320 test_pton6_bad("0x60::");
321 test_pton6_bad("::0x60");
322 test_pton6_bad("9:0x60::");
323 test_pton6_bad("1:2:33333:4:0002:3::");
324 test_pton6_bad("1:2:3333:4:fish:3::");
325 test_pton6_bad("1:2:3:4:5:6:7:8:9");
326 test_pton6_bad("1:2:3:4:5:6:7");
327 test_pton6_bad("1:2:3:4:5:6:1.2.3.4.5");
328 test_pton6_bad("1:2:3:4:5:6:1.2.3");
329 test_pton6_bad("::1.2.3");
330 test_pton6_bad("::1.2.3.4.5");
331 test_pton6_bad("::ffff:0xff.0.0.0");
332 test_pton6_bad("::ffff:ff.0.0.0");
333 test_pton6_bad("::ffff:256.0.0.0");
334 test_pton6_bad("::ffff:-1.0.0.0");
335 test_pton6_bad("99");
339 test_pton6_bad("1::2::3:4");
340 test_pton6_bad("a:::b:c");
341 test_pton6_bad(":::a:b:c");
342 test_pton6_bad("a:b:c:::");
344 /* test internal checking */
345 test_external_ip("fbff:ffff::2:7", 0);
346 test_internal_ip("fc01::2:7", 0);
347 test_internal_ip("fc01::02:7", 0);
348 test_internal_ip("fc01::002:7", 0);
349 test_internal_ip("fc01::0002:7", 0);
350 test_internal_ip("fdff:ffff::f:f", 0);
351 test_external_ip("fe00::3:f", 0);
353 test_external_ip("fe7f:ffff::2:7", 0);
354 test_internal_ip("fe80::2:7", 0);
355 test_internal_ip("febf:ffff::f:f", 0);
357 test_internal_ip("fec0::2:7:7", 0);
358 test_internal_ip("feff:ffff::e:7:7", 0);
359 test_external_ip("ff00::e:7:7", 0);
361 test_internal_ip("::", 0);
362 test_internal_ip("::1", 0);
363 test_internal_ip("::1", 1);
364 test_internal_ip("::", 0);
365 test_external_ip("::", 1);
366 test_external_ip("::2", 0);
367 test_external_ip("2001::", 0);
368 test_external_ip("ffff::", 0);
370 test_external_ip("::ffff:0.0.0.0", 1);
371 test_internal_ip("::ffff:0.0.0.0", 0);
372 test_internal_ip("::ffff:0.255.255.255", 0);
373 test_external_ip("::ffff:1.0.0.0", 0);
375 test_external_ip("::ffff:9.255.255.255", 0);
376 test_internal_ip("::ffff:10.0.0.0", 0);
377 test_internal_ip("::ffff:10.255.255.255", 0);
378 test_external_ip("::ffff:11.0.0.0", 0);
380 test_external_ip("::ffff:126.255.255.255", 0);
381 test_internal_ip("::ffff:127.0.0.0", 0);
382 test_internal_ip("::ffff:127.255.255.255", 0);
383 test_external_ip("::ffff:128.0.0.0", 0);
385 test_external_ip("::ffff:172.15.255.255", 0);
386 test_internal_ip("::ffff:172.16.0.0", 0);
387 test_internal_ip("::ffff:172.31.255.255", 0);
388 test_external_ip("::ffff:172.32.0.0", 0);
390 test_external_ip("::ffff:192.167.255.255", 0);
391 test_internal_ip("::ffff:192.168.0.0", 0);
392 test_internal_ip("::ffff:192.168.255.255", 0);
393 test_external_ip("::ffff:192.169.0.0", 0);
395 test_external_ip("::ffff:169.253.255.255", 0);
396 test_internal_ip("::ffff:169.254.0.0", 0);
397 test_internal_ip("::ffff:169.254.255.255", 0);
398 test_external_ip("::ffff:169.255.0.0", 0);
399 test_assert(is_internal_IP(0x7f000001, 0));
401 /* tor_addr_compare(tor_addr_t x2) */
402 test_addr_compare("ffff::", ==, "ffff::0");
403 test_addr_compare("0::3:2:1", <, "0::ffff:0.3.2.1");
404 test_addr_compare("0::2:2:1", <, "0::ffff:0.3.2.1");
405 test_addr_compare("0::ffff:0.3.2.1", >, "0::0:0:0");
406 test_addr_compare("0::ffff:5.2.2.1", <, "::ffff:6.0.0.0"); /* XXXX wrong. */
407 tor_addr_parse_mask_ports("[::ffff:2.3.4.5]", 0, &t1
, NULL
, NULL
, NULL
);
408 tor_addr_parse_mask_ports("2.3.4.5", 0, &t2
, NULL
, NULL
, NULL
);
409 test_assert(tor_addr_compare(&t1
, &t2
, CMP_SEMANTIC
) == 0);
410 tor_addr_parse_mask_ports("[::ffff:2.3.4.4]", 0, &t1
, NULL
, NULL
, NULL
);
411 tor_addr_parse_mask_ports("2.3.4.5", 0, &t2
, NULL
, NULL
, NULL
);
412 test_assert(tor_addr_compare(&t1
, &t2
, CMP_SEMANTIC
) < 0);
414 /* test compare_masked */
415 test_addr_compare_masked("ffff::", ==, "ffff::0", 128);
416 test_addr_compare_masked("ffff::", ==, "ffff::0", 64);
417 test_addr_compare_masked("0::2:2:1", <, "0::8000:2:1", 81);
418 test_addr_compare_masked("0::2:2:1", ==, "0::8000:2:1", 80);
420 /* Test undecorated tor_addr_to_str */
421 test_eq(AF_INET6
, tor_addr_parse(&t1
, "[123:45:6789::5005:11]"));
422 p1
= tor_addr_to_str(buf
, &t1
, sizeof(buf
), 0);
423 test_streq(p1
, "123:45:6789::5005:11");
424 test_eq(AF_INET
, tor_addr_parse(&t1
, "18.0.0.1"));
425 p1
= tor_addr_to_str(buf
, &t1
, sizeof(buf
), 0);
426 test_streq(p1
, "18.0.0.1");
428 /* Test decorated tor_addr_to_str */
429 test_eq(AF_INET6
, tor_addr_parse(&t1
, "[123:45:6789::5005:11]"));
430 p1
= tor_addr_to_str(buf
, &t1
, sizeof(buf
), 1);
431 test_streq(p1
, "[123:45:6789::5005:11]");
432 test_eq(AF_INET
, tor_addr_parse(&t1
, "18.0.0.1"));
433 p1
= tor_addr_to_str(buf
, &t1
, sizeof(buf
), 1);
434 test_streq(p1
, "18.0.0.1");
436 /* Test buffer bounds checking of tor_addr_to_str */
437 test_eq(AF_INET6
, tor_addr_parse(&t1
, "::")); /* 2 + \0 */
438 test_eq_ptr(tor_addr_to_str(buf
, &t1
, 2, 0), NULL
); /* too short buf */
439 test_streq(tor_addr_to_str(buf
, &t1
, 3, 0), "::");
440 test_eq_ptr(tor_addr_to_str(buf
, &t1
, 4, 1), NULL
); /* too short buf */
441 test_streq(tor_addr_to_str(buf
, &t1
, 5, 1), "[::]");
443 test_eq(AF_INET6
, tor_addr_parse(&t1
, "2000::1337")); /* 10 + \0 */
444 test_eq_ptr(tor_addr_to_str(buf
, &t1
, 10, 0), NULL
); /* too short buf */
445 test_streq(tor_addr_to_str(buf
, &t1
, 11, 0), "2000::1337");
446 test_eq_ptr(tor_addr_to_str(buf
, &t1
, 12, 1), NULL
); /* too short buf */
447 test_streq(tor_addr_to_str(buf
, &t1
, 13, 1), "[2000::1337]");
449 test_eq(AF_INET
, tor_addr_parse(&t1
, "1.2.3.4")); /* 7 + \0 */
450 test_eq_ptr(tor_addr_to_str(buf
, &t1
, 7, 0), NULL
); /* too short buf */
451 test_streq(tor_addr_to_str(buf
, &t1
, 8, 0), "1.2.3.4");
453 test_eq(AF_INET
, tor_addr_parse(&t1
, "255.255.255.255")); /* 15 + \0 */
454 test_eq_ptr(tor_addr_to_str(buf
, &t1
, 15, 0), NULL
); /* too short buf */
455 test_streq(tor_addr_to_str(buf
, &t1
, 16, 0), "255.255.255.255");
456 test_eq_ptr(tor_addr_to_str(buf
, &t1
, 15, 1), NULL
); /* too short buf */
457 test_streq(tor_addr_to_str(buf
, &t1
, 16, 1), "255.255.255.255");
459 t1
.family
= AF_UNSPEC
;
460 test_eq_ptr(tor_addr_to_str(buf
, &t1
, sizeof(buf
), 0), NULL
);
462 /* Test tor_addr_parse_PTR_name */
463 i
= tor_addr_parse_PTR_name(&t1
, "Foobar.baz", AF_UNSPEC
, 0);
465 i
= tor_addr_parse_PTR_name(&t1
, "Foobar.baz", AF_UNSPEC
, 1);
467 i
= tor_addr_parse_PTR_name(&t1
, "1.0.168.192.in-addr.arpa",
470 test_eq(tor_addr_family(&t1
), AF_INET
);
471 p1
= tor_addr_to_str(buf
, &t1
, sizeof(buf
), 1);
472 test_streq(p1
, "192.168.0.1");
473 i
= tor_addr_parse_PTR_name(&t1
, "192.168.0.99", AF_UNSPEC
, 0);
475 i
= tor_addr_parse_PTR_name(&t1
, "192.168.0.99", AF_UNSPEC
, 1);
477 p1
= tor_addr_to_str(buf
, &t1
, sizeof(buf
), 1);
478 test_streq(p1
, "192.168.0.99");
479 memset(&t1
, 0, sizeof(t1
));
480 i
= tor_addr_parse_PTR_name(&t1
,
481 "0.1.2.3.4.5.6.7.8.9.a.b.c.d.e.f."
482 "f.e.e.b.1.e.b.e.e.f.f.e.e.e.d.9."
486 p1
= tor_addr_to_str(buf
, &t1
, sizeof(buf
), 1);
487 test_streq(p1
, "[9dee:effe:ebe1:beef:fedc:ba98:7654:3210]");
489 i
= tor_addr_parse_PTR_name(&t1
,
490 "6.7.8.9.a.b.c.d.e.f."
491 "f.e.e.b.1.e.b.e.e.f.f.e.e.e.d.9."
495 i
= tor_addr_parse_PTR_name(&t1
,
496 "6.7.8.9.a.b.c.d.e.f.a.b.c.d.e.f.0."
497 "f.e.e.b.1.e.b.e.e.f.f.e.e.e.d.9."
501 i
= tor_addr_parse_PTR_name(&t1
,
502 "6.7.8.9.a.b.c.d.e.f.X.0.0.0.0.9."
503 "f.e.e.b.1.e.b.e.e.f.f.e.e.e.d.9."
507 i
= tor_addr_parse_PTR_name(&t1
, "32.1.1.in-addr.arpa",
510 i
= tor_addr_parse_PTR_name(&t1
, ".in-addr.arpa",
513 i
= tor_addr_parse_PTR_name(&t1
, "1.2.3.4.5.in-addr.arpa",
516 i
= tor_addr_parse_PTR_name(&t1
, "1.2.3.4.5.in-addr.arpa",
519 i
= tor_addr_parse_PTR_name(&t1
,
520 "6.7.8.9.a.b.c.d.e.f.a.b.c.d.e.0."
521 "f.e.e.b.1.e.b.e.e.f.f.e.e.e.d.9."
526 /* === Test tor_addr_to_PTR_name */
528 /* Stage IPv4 addr */
529 memset(&sa_storage
, 0, sizeof(sa_storage
));
530 sin
= (struct sockaddr_in
*)&sa_storage
;
531 sin
->sin_family
= AF_INET
;
532 sin
->sin_addr
.s_addr
= htonl(0x7f010203); /* 127.1.2.3 */
533 tor_addr_from_sockaddr(&t1
, (struct sockaddr
*)sin
, NULL
);
535 /* Check IPv4 PTR - too short buffer */
536 test_eq(tor_addr_to_PTR_name(rbuf
, 1, &t1
), -1);
537 test_eq(tor_addr_to_PTR_name(rbuf
,
538 strlen("3.2.1.127.in-addr.arpa") - 1,
541 /* Check IPv4 PTR - valid addr */
542 test_eq(tor_addr_to_PTR_name(rbuf
, sizeof(rbuf
), &t1
),
543 strlen("3.2.1.127.in-addr.arpa"));
544 test_streq(rbuf
, "3.2.1.127.in-addr.arpa");
546 /* Invalid addr family */
547 t1
.family
= AF_UNSPEC
;
548 test_eq(tor_addr_to_PTR_name(rbuf
, sizeof(rbuf
), &t1
), -1);
550 /* Stage IPv6 addr */
551 memset(&sa_storage
, 0, sizeof(sa_storage
));
552 sin6
= (struct sockaddr_in6
*)&sa_storage
;
553 sin6
->sin6_family
= AF_INET6
;
554 sin6
->sin6_addr
.s6_addr
[0] = 0x80; /* 8000::abcd */
555 sin6
->sin6_addr
.s6_addr
[14] = 0xab;
556 sin6
->sin6_addr
.s6_addr
[15] = 0xcd;
558 tor_addr_from_sockaddr(&t1
, (struct sockaddr
*)sin6
, NULL
);
561 const char* addr_PTR
= "d.c.b.a.0.0.0.0.0.0.0.0.0.0.0.0."
562 "0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.8.ip6.arpa";
564 /* Check IPv6 PTR - too short buffer */
565 test_eq(tor_addr_to_PTR_name(rbuf
, 0, &t1
), -1);
566 test_eq(tor_addr_to_PTR_name(rbuf
, strlen(addr_PTR
) - 1, &t1
), -1);
568 /* Check IPv6 PTR - valid addr */
569 test_eq(tor_addr_to_PTR_name(rbuf
, sizeof(rbuf
), &t1
),
571 test_streq(rbuf
, addr_PTR
);
574 /* XXXX turn this into a separate function; it's not all IPv6. */
575 /* test tor_addr_parse_mask_ports */
576 test_addr_mask_ports_parse("[::f]/17:47-95", AF_INET6
,
577 0, 0, 0, 0x0000000f, 17, 47, 95);
578 test_streq(p1
, "::f");
579 //test_addr_parse("[::fefe:4.1.1.7/120]:999-1000");
580 //test_addr_parse_check("::fefe:401:107", 120, 999, 1000);
581 test_addr_mask_ports_parse("[::ffff:4.1.1.7]/120:443", AF_INET6
,
582 0, 0, 0x0000ffff, 0x04010107, 120, 443, 443);
583 test_streq(p1
, "::ffff:4.1.1.7");
584 test_addr_mask_ports_parse("[abcd:2::44a:0]:2-65000", AF_INET6
,
585 0xabcd0002, 0, 0, 0x044a0000, 128, 2, 65000);
587 test_streq(p1
, "abcd:2::44a:0");
588 /* Try some long addresses. */
589 r
=tor_addr_parse_mask_ports("[ffff:1111:1111:1111:1111:1111:1111:1111]",
590 0, &t1
, NULL
, NULL
, NULL
);
591 test_assert(r
== AF_INET6
);
592 r
=tor_addr_parse_mask_ports("[ffff:1111:1111:1111:1111:1111:1111:11111]",
593 0, &t1
, NULL
, NULL
, NULL
);
594 test_assert(r
== -1);
595 r
=tor_addr_parse_mask_ports("[ffff:1111:1111:1111:1111:1111:1111:1111:1]",
596 0, &t1
, NULL
, NULL
, NULL
);
597 test_assert(r
== -1);
598 r
=tor_addr_parse_mask_ports(
599 "[ffff:1111:1111:1111:1111:1111:1111:ffff:"
600 "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff:"
601 "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff:"
602 "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff]",
603 0, &t1
, NULL
, NULL
, NULL
);
604 test_assert(r
== -1);
605 /* Try some failing cases. */
606 r
=tor_addr_parse_mask_ports("[fefef::]/112", 0, &t1
, NULL
, NULL
, NULL
);
607 test_assert(r
== -1);
608 r
=tor_addr_parse_mask_ports("[fefe::/112", 0, &t1
, NULL
, NULL
, NULL
);
609 test_assert(r
== -1);
610 r
=tor_addr_parse_mask_ports("[fefe::", 0, &t1
, NULL
, NULL
, NULL
);
611 test_assert(r
== -1);
612 r
=tor_addr_parse_mask_ports("[fefe::X]", 0, &t1
, NULL
, NULL
, NULL
);
613 test_assert(r
== -1);
614 r
=tor_addr_parse_mask_ports("efef::/112", 0, &t1
, NULL
, NULL
, NULL
);
615 test_assert(r
== -1);
616 r
=tor_addr_parse_mask_ports("[f:f:f:f:f:f:f:f::]",0,&t1
, NULL
, NULL
, NULL
);
617 test_assert(r
== -1);
618 r
=tor_addr_parse_mask_ports("[::f:f:f:f:f:f:f:f]",0,&t1
, NULL
, NULL
, NULL
);
619 test_assert(r
== -1);
620 r
=tor_addr_parse_mask_ports("[f:f:f:f:f:f:f:f:f]",0,&t1
, NULL
, NULL
, NULL
);
621 test_assert(r
== -1);
622 r
=tor_addr_parse_mask_ports("[f:f:f:f:f::]/fred",0,&t1
,&mask
, NULL
, NULL
);
623 test_assert(r
== -1);
624 r
=tor_addr_parse_mask_ports("[f:f:f:f:f::]/255.255.0.0",
625 0,&t1
, NULL
, NULL
, NULL
);
626 test_assert(r
== -1);
627 /* This one will get rejected because it isn't a pure prefix. */
628 r
=tor_addr_parse_mask_ports("1.1.2.3/255.255.64.0",0,&t1
, &mask
,NULL
,NULL
);
629 test_assert(r
== -1);
630 /* Test for V4-mapped address with mask < 96. (arguably not valid) */
631 r
=tor_addr_parse_mask_ports("[::ffff:1.1.2.2/33]",0,&t1
, &mask
, NULL
, NULL
);
632 test_assert(r
== -1);
633 r
=tor_addr_parse_mask_ports("1.1.2.2/33",0,&t1
, &mask
, NULL
, NULL
);
634 test_assert(r
== -1);
635 /* Try extended wildcard addresses with out TAPMP_EXTENDED_STAR*/
636 r
=tor_addr_parse_mask_ports("*4",0,&t1
, &mask
, NULL
, NULL
);
637 test_assert(r
== -1);
638 r
=tor_addr_parse_mask_ports("*6",0,&t1
, &mask
, NULL
, NULL
);
639 test_assert(r
== -1);
641 /* Try a mask with a wildcard. */
642 r
=tor_addr_parse_mask_ports("*/16",0,&t1
, &mask
, NULL
, NULL
);
643 test_assert(r
== -1);
644 r
=tor_addr_parse_mask_ports("*4/16",TAPMP_EXTENDED_STAR
,
645 &t1
, &mask
, NULL
, NULL
);
646 test_assert(r
== -1);
647 r
=tor_addr_parse_mask_ports("*6/30",TAPMP_EXTENDED_STAR
,
648 &t1
, &mask
, NULL
, NULL
);
649 test_assert(r
== -1);
651 /* Basic mask tests*/
652 r
=tor_addr_parse_mask_ports("1.1.2.2/31",0,&t1
, &mask
, NULL
, NULL
);
653 test_assert(r
== AF_INET
);
654 tt_int_op(mask
,==,31);
655 tt_int_op(tor_addr_family(&t1
),==,AF_INET
);
656 tt_int_op(tor_addr_to_ipv4h(&t1
),==,0x01010202);
657 r
=tor_addr_parse_mask_ports("3.4.16.032:1-2",0,&t1
, &mask
, &port1
, &port2
);
658 test_assert(r
== AF_INET
);
659 tt_int_op(mask
,==,32);
660 tt_int_op(tor_addr_family(&t1
),==,AF_INET
);
661 tt_int_op(tor_addr_to_ipv4h(&t1
),==,0x03041020);
662 test_assert(port1
== 1);
663 test_assert(port2
== 2);
664 r
=tor_addr_parse_mask_ports("1.1.2.3/255.255.128.0",0,&t1
, &mask
,NULL
,NULL
);
665 test_assert(r
== AF_INET
);
666 tt_int_op(mask
,==,17);
667 tt_int_op(tor_addr_family(&t1
),==,AF_INET
);
668 tt_int_op(tor_addr_to_ipv4h(&t1
),==,0x01010203);
669 r
=tor_addr_parse_mask_ports("[efef::]/112",0,&t1
, &mask
, &port1
, &port2
);
670 test_assert(r
== AF_INET6
);
671 test_assert(port1
== 1);
672 test_assert(port2
== 65535);
673 /* Try regular wildcard behavior without TAPMP_EXTENDED_STAR */
674 r
=tor_addr_parse_mask_ports("*:80-443",0,&t1
,&mask
,&port1
,&port2
);
675 tt_int_op(r
,==,AF_INET
); /* Old users of this always get inet */
676 tt_int_op(tor_addr_family(&t1
),==,AF_INET
);
677 tt_int_op(tor_addr_to_ipv4h(&t1
),==,0);
678 tt_int_op(mask
,==,0);
679 tt_int_op(port1
,==,80);
680 tt_int_op(port2
,==,443);
681 /* Now try wildcards *with* TAPMP_EXTENDED_STAR */
682 r
=tor_addr_parse_mask_ports("*:8000-9000",TAPMP_EXTENDED_STAR
,
683 &t1
,&mask
,&port1
,&port2
);
684 tt_int_op(r
,==,AF_UNSPEC
);
685 tt_int_op(tor_addr_family(&t1
),==,AF_UNSPEC
);
686 tt_int_op(mask
,==,0);
687 tt_int_op(port1
,==,8000);
688 tt_int_op(port2
,==,9000);
689 r
=tor_addr_parse_mask_ports("*4:6667",TAPMP_EXTENDED_STAR
,
690 &t1
,&mask
,&port1
,&port2
);
691 tt_int_op(r
,==,AF_INET
);
692 tt_int_op(tor_addr_family(&t1
),==,AF_INET
);
693 tt_int_op(tor_addr_to_ipv4h(&t1
),==,0);
694 tt_int_op(mask
,==,0);
695 tt_int_op(port1
,==,6667);
696 tt_int_op(port2
,==,6667);
697 r
=tor_addr_parse_mask_ports("*6",TAPMP_EXTENDED_STAR
,
698 &t1
,&mask
,&port1
,&port2
);
699 tt_int_op(r
,==,AF_INET6
);
700 tt_int_op(tor_addr_family(&t1
),==,AF_INET6
);
701 tt_assert(tor_mem_is_zero((const char*)tor_addr_to_in6_addr32(&t1
), 16));
702 tt_int_op(mask
,==,0);
703 tt_int_op(port1
,==,1);
704 tt_int_op(port2
,==,65535);
706 /* make sure inet address lengths >= max */
707 test_assert(INET_NTOA_BUF_LEN
>= sizeof("255.255.255.255"));
708 test_assert(TOR_ADDR_BUF_LEN
>=
709 sizeof("ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255"));
711 test_assert(sizeof(tor_addr_t
) >= sizeof(struct in6_addr
));
713 /* get interface addresses */
714 r
= get_interface_address6(LOG_DEBUG
, AF_INET
, &t1
);
715 i
= get_interface_address6(LOG_DEBUG
, AF_INET6
, &t2
);
717 TT_BLATHER(("v4 address: %s (family=%d)", fmt_addr(&t1
),
718 tor_addr_family(&t1
)));
719 TT_BLATHER(("v6 address: %s (family=%d)", fmt_addr(&t2
),
720 tor_addr_family(&t2
)));
726 /** Test tor_addr_port_parse(). */
728 test_addr_parse(void)
732 char buf
[TOR_ADDR_BUF_LEN
];
736 r
= tor_addr_port_parse(LOG_DEBUG
,
740 tor_addr_to_str(buf
, &addr
, sizeof(buf
), 0);
741 test_streq(buf
, "192.0.2.1");
745 r
= tor_addr_port_parse(LOG_DEBUG
,
746 "torproject.org:1234",
748 test_assert(r
== -1);
751 r
= tor_addr_port_parse(LOG_DEBUG
,
754 test_assert(r
== -1);
757 r
= tor_addr_port_parse(LOG_DEBUG
,
760 test_assert(r
== -1);
762 /* Only domain name */
763 r
= tor_addr_port_parse(LOG_DEBUG
,
766 test_assert(r
== -1);
769 r
= tor_addr_port_parse(LOG_DEBUG
,
772 test_assert(r
== -1);
779 update_difference(int ipv6
, uint8_t *d
,
780 const tor_addr_t
*a
, const tor_addr_t
*b
)
782 const int n_bytes
= ipv6
? 16 : 4;
783 uint8_t a_tmp
[4], b_tmp
[4];
784 const uint8_t *ba
, *bb
;
788 ba
= tor_addr_to_in6_addr8(a
);
789 bb
= tor_addr_to_in6_addr8(b
);
791 set_uint32(a_tmp
, tor_addr_to_ipv4n(a
));
792 set_uint32(b_tmp
, tor_addr_to_ipv4n(b
));
793 ba
= a_tmp
; bb
= b_tmp
;
796 for (i
= 0; i
< n_bytes
; ++i
) {
797 d
[i
] |= ba
[i
] ^ bb
[i
];
802 test_virtaddrmap(void *data
)
804 /* Let's start with a bunch of random addresses. */
805 int ipv6
, bits
, iter
, b
;
806 virtual_addr_conf_t cfg
[2];
811 tor_addr_parse(&cfg
[0].addr
, "64.65.0.0");
812 tor_addr_parse(&cfg
[1].addr
, "3491:c0c0::");
814 for (ipv6
= 0; ipv6
<= 1; ++ipv6
) {
815 for (bits
= 0; bits
< 18; ++bits
) {
817 cfg
[ipv6
].bits
= bits
;
818 memset(bytes
, 0, sizeof(bytes
));
819 tor_addr_copy(&last_a
, &cfg
[ipv6
].addr
);
820 /* Generate 128 addresses with each addr/bits combination. */
821 for (iter
= 0; iter
< 128; ++iter
) {
824 get_random_virtual_addr(&cfg
[ipv6
], &a
);
825 //printf("%s\n", fmt_addr(&a));
826 /* Make sure that the first b bits match the configured network */
827 tt_int_op(0, ==, tor_addr_compare_masked(&a
, &cfg
[ipv6
].addr
,
830 /* And track which bits have been different between pairs of
832 update_difference(ipv6
, bytes
, &last_a
, &a
);
835 /* Now make sure all but the first 'bits' bits of bytes are true */
836 for (b
= bits
+1; b
< (ipv6
?128:32); ++b
) {
837 tt_assert(1 & (bytes
[b
/8] >> (7-(b
&7))));
847 test_addr_is_loopback(void *data
)
849 static const struct loopback_item
{
852 } loopback_items
[] = {
855 { "127.99.100.101", 1 },
856 { "128.99.100.101", 0 },
869 for (i
=0; loopback_items
[i
].name
; ++i
) {
870 tt_int_op(tor_addr_parse(&addr
, loopback_items
[i
].name
), >=, 0);
871 tt_int_op(tor_addr_is_loopback(&addr
), ==, loopback_items
[i
].is_loopback
);
874 tor_addr_make_unspec(&addr
);
875 tt_int_op(tor_addr_is_loopback(&addr
), ==, 0);
881 #define ADDR_LEGACY(name) \
882 { #name, legacy_test_helper, 0, &legacy_setup, test_addr_ ## name }
884 struct testcase_t addr_tests
[] = {
886 ADDR_LEGACY(ip6_helpers
),
888 { "virtaddr", test_virtaddrmap
, 0, NULL
, NULL
},
889 { "is_loopback", test_addr_is_loopback
, 0, NULL
, NULL
},