1 /* Copyright (c) 2001-2004, Roger Dingledine.
2 * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
3 * Copyright (c) 2007-2021, The Tor Project, Inc. */
4 /* See LICENSE for licensing information */
6 #define ADDRESSMAP_PRIVATE
8 #include "core/or/or.h"
9 #include "lib/crypt_ops/crypto_rand.h"
10 #include "test/test.h"
11 #include "feature/client/addressmap.h"
12 #include "test/log_test_helpers.h"
13 #include "lib/net/resolve.h"
14 #include "test/rng_test_helpers.h"
15 #include "test/resolve_test_helpers.h"
22 test_addr_basic(void *arg
)
26 tt_int_op(0,OP_EQ
, addr_mask_get_bits(0x0u
));
27 tt_int_op(32,OP_EQ
, addr_mask_get_bits(0xFFFFFFFFu
));
28 tt_int_op(16,OP_EQ
, addr_mask_get_bits(0xFFFF0000u
));
29 tt_int_op(31,OP_EQ
, addr_mask_get_bits(0xFFFFFFFEu
));
30 tt_int_op(1,OP_EQ
, addr_mask_get_bits(0x80000000u
));
34 char tmpbuf
[TOR_ADDR_BUF_LEN
];
35 const char *ip
= "176.192.208.224";
39 tt_int_op(tor_inet_pton(AF_INET
, ip
, &in
), OP_EQ
, 1);
40 tt_ptr_op(tor_inet_ntop(AF_INET
, &in
, tmpbuf
, sizeof(tmpbuf
)),
42 tt_str_op(tmpbuf
,OP_EQ
, ip
);
44 /* just enough buffer length */
45 tt_str_op(tor_inet_ntop(AF_INET
, &in
, tmpbuf
, strlen(ip
) + 1), OP_EQ
, ip
);
47 /* too short buffer */
48 tt_ptr_op(tor_inet_ntop(AF_INET
, &in
, tmpbuf
, strlen(ip
)),OP_EQ
, NULL
);
56 #define test_op_ip6_(a,op,b,e1,e2) \
58 tt_assert_test_fmt_type(a,b,e1" "#op" "e2,struct in6_addr*, \
59 (fast_memcmp(val1_->s6_addr, val2_->s6_addr, 16) op 0), \
62 cp = print_ = tor_malloc(64); \
63 for (int ii_=0;ii_<16;++ii_) { \
64 tor_snprintf(cp, 3,"%02x", (unsigned)value_->s6_addr[ii_]); \
66 if (ii_ != 15) *cp++ = ':'; \
69 { tor_free(print_); }, \
70 TT_EXIT_TEST_FUNCTION \
73 #endif /* !defined(COCCI) */
75 /** Helper: Assert that two strings both decode as IPv6 addresses with
76 * tor_inet_pton(), and both decode to the same address. */
77 #define test_pton6_same(a,b) STMT_BEGIN \
78 tt_int_op(tor_inet_pton(AF_INET6, a, &a1), OP_EQ, 1); \
79 tt_int_op(tor_inet_pton(AF_INET6, b, &a2), OP_EQ, 1); \
80 test_op_ip6_(&a1,OP_EQ,&a2,#a,#b); \
83 /** Helper: Assert that <b>a</b> is recognized as a bad IPv6 address by
85 #define test_pton6_bad(a) \
86 tt_int_op(0, OP_EQ, tor_inet_pton(AF_INET6, a, &a1))
88 /** Helper: assert that <b>a</b>, when parsed by tor_inet_pton() and displayed
89 * with tor_inet_ntop(), yields <b>b</b>. Also assert that <b>b</b> parses to
90 * the same value as <b>a</b>. */
91 #define test_ntop6_reduces(a,b) STMT_BEGIN \
92 tt_int_op(tor_inet_pton(AF_INET6, a, &a1), OP_EQ, 1); \
93 tt_str_op(tor_inet_ntop(AF_INET6, &a1, buf, sizeof(buf)), OP_EQ, b); \
94 tt_int_op(tor_inet_pton(AF_INET6, b, &a2), OP_EQ, 1); \
95 test_op_ip6_(&a1, OP_EQ, &a2, a, b); \
98 /** Helper: assert that <b>a</b> parses by tor_inet_pton() into a address that
99 * passes tor_addr_is_internal() with <b>for_listening</b>. */
100 #define test_internal_ip(a,for_listening) STMT_BEGIN \
101 tt_int_op(tor_inet_pton(AF_INET6, a, &t1.addr.in6_addr), OP_EQ, 1); \
102 t1.family = AF_INET6; \
103 if (!tor_addr_is_internal(&t1, for_listening)) \
104 TT_DIE(("%s was not internal", a)); \
107 /** Helper: assert that <b>a</b> parses by tor_inet_pton() into a address that
108 * does not pass tor_addr_is_internal() with <b>for_listening</b>. */
109 #define test_external_ip(a,for_listening) STMT_BEGIN \
110 tt_int_op(tor_inet_pton(AF_INET6, a, &t1.addr.in6_addr), OP_EQ, 1); \
111 t1.family = AF_INET6; \
112 if (tor_addr_is_internal(&t1, for_listening)) \
113 TT_DIE(("%s was internal", a)); \
117 /** Helper: Assert that <b>a</b> and <b>b</b>, when parsed by
118 * tor_inet_pton(), give addresses that compare in the order defined by
119 * <b>op</b> with tor_addr_compare(). */
120 #define test_addr_compare(a, op, b) STMT_BEGIN \
121 tt_int_op(tor_inet_pton(AF_INET6, a, &t1.addr.in6_addr), OP_EQ, 1); \
122 tt_int_op(tor_inet_pton(AF_INET6, b, &t2.addr.in6_addr), OP_EQ, 1); \
123 t1.family = t2.family = AF_INET6; \
124 r = tor_addr_compare(&t1,&t2,CMP_SEMANTIC); \
126 TT_DIE(("Failed: tor_addr_compare(%s,%s) %s 0", a, b, #op));\
129 /** Helper: Assert that <b>a</b> and <b>b</b>, when parsed by
130 * tor_inet_pton(), give addresses that compare in the order defined by
131 * <b>op</b> with tor_addr_compare_masked() with <b>m</b> masked. */
132 #define test_addr_compare_masked(a, op, b, m) STMT_BEGIN \
133 tt_int_op(tor_inet_pton(AF_INET6, a, &t1.addr.in6_addr), OP_EQ, 1); \
134 tt_int_op(tor_inet_pton(AF_INET6, b, &t2.addr.in6_addr), OP_EQ, 1); \
135 t1.family = t2.family = AF_INET6; \
136 r = tor_addr_compare_masked(&t1,&t2,m,CMP_SEMANTIC); \
138 TT_DIE(("Failed: tor_addr_compare_masked(%s,%s,%d) %s 0", \
141 #endif /* !defined(COCCI) */
143 /** Helper: assert that <b>xx</b> is parseable as a masked IPv6 address with
144 * ports by tor_parse_mask_addr_ports(), with family <b>f</b>, IP address
145 * as 4 32-bit words <b>ip1...ip4</b>, mask bits as <b>mm</b>, and port range
146 * as <b>pt1..pt2</b>. */
147 #define test_addr_mask_ports_parse(xx, f, ip1, ip2, ip3, ip4, mm, pt1, pt2) \
149 tt_int_op(tor_addr_parse_mask_ports(xx, 0, &t1, &mask, &port1, &port2), \
151 p1=tor_inet_ntop(AF_INET6, &t1.addr.in6_addr, bug, sizeof(bug)); \
152 tt_int_op(htonl(ip1), OP_EQ, tor_addr_to_in6_addr32(&t1)[0]); \
153 tt_int_op(htonl(ip2), OP_EQ, tor_addr_to_in6_addr32(&t1)[1]); \
154 tt_int_op(htonl(ip3), OP_EQ, tor_addr_to_in6_addr32(&t1)[2]); \
155 tt_int_op(htonl(ip4), OP_EQ, tor_addr_to_in6_addr32(&t1)[3]); \
156 tt_int_op(mask, OP_EQ, mm); \
157 tt_uint_op(port1, OP_EQ, pt1); \
158 tt_uint_op(port2, OP_EQ, pt2); \
161 /** Run unit tests for IPv6 encoding/decoding/manipulation functions. */
163 test_addr_ip6_helpers(void *arg
)
165 char buf
[TOR_ADDR_BUF_LEN
], bug
[TOR_ADDR_BUF_LEN
];
166 char rbuf
[REVERSE_LOOKUP_NAME_BUF_LEN
];
167 struct in6_addr a1
, a2
;
170 uint16_t port1
, port2
;
173 struct sockaddr_storage sa_storage
;
174 struct sockaddr_in
*sin
;
175 struct sockaddr_in6
*sin6
;
177 /* Test tor_inet_ntop and tor_inet_pton: IPv6 */
180 const char *ip
= "2001::1234";
181 const char *ip_ffff
= "::ffff:192.168.1.2";
183 /* good round trip */
184 tt_int_op(tor_inet_pton(AF_INET6
, ip
, &a1
),OP_EQ
, 1);
185 tt_ptr_op(tor_inet_ntop(AF_INET6
, &a1
, buf
, sizeof(buf
)),OP_EQ
, &buf
);
186 tt_str_op(buf
,OP_EQ
, ip
);
188 /* good round trip - ::ffff:0:0 style */
189 tt_int_op(tor_inet_pton(AF_INET6
, ip_ffff
, &a2
),OP_EQ
, 1);
190 tt_ptr_op(tor_inet_ntop(AF_INET6
, &a2
, buf
, sizeof(buf
)),OP_EQ
, &buf
);
191 tt_str_op(buf
,OP_EQ
, ip_ffff
);
193 /* just long enough buffer (remember \0) */
194 tt_str_op(tor_inet_ntop(AF_INET6
, &a1
, buf
, strlen(ip
)+1),OP_EQ
, ip
);
195 tt_str_op(tor_inet_ntop(AF_INET6
, &a2
, buf
, strlen(ip_ffff
)+1),OP_EQ
,
198 /* too short buffer (remember \0) */
199 tt_ptr_op(tor_inet_ntop(AF_INET6
, &a1
, buf
, strlen(ip
)),OP_EQ
, NULL
);
200 tt_ptr_op(tor_inet_ntop(AF_INET6
, &a2
, buf
, strlen(ip_ffff
)),OP_EQ
, NULL
);
203 /* ==== Converting to and from sockaddr_t. */
204 sin
= (struct sockaddr_in
*)&sa_storage
;
205 sin
->sin_family
= AF_INET
;
206 sin
->sin_port
= htons(9090);
207 sin
->sin_addr
.s_addr
= htonl(0x7f7f0102); /*127.127.1.2*/
208 tor_addr_from_sockaddr(&t1
, (struct sockaddr
*)sin
, &port1
);
209 tt_int_op(tor_addr_family(&t1
),OP_EQ
, AF_INET
);
210 tt_int_op(tor_addr_to_ipv4h(&t1
),OP_EQ
, 0x7f7f0102);
211 tt_int_op(port1
, OP_EQ
, 9090);
213 memset(&sa_storage
, 0, sizeof(sa_storage
));
214 tt_int_op(sizeof(struct sockaddr_in
),OP_EQ
,
215 tor_addr_to_sockaddr(&t1
, 1234, (struct sockaddr
*)&sa_storage
,
216 sizeof(sa_storage
)));
217 tt_int_op(1234,OP_EQ
, ntohs(sin
->sin_port
));
218 tt_int_op(0x7f7f0102,OP_EQ
, ntohl(sin
->sin_addr
.s_addr
));
220 memset(&sa_storage
, 0, sizeof(sa_storage
));
221 sin6
= (struct sockaddr_in6
*)&sa_storage
;
222 sin6
->sin6_family
= AF_INET6
;
223 sin6
->sin6_port
= htons(7070);
224 sin6
->sin6_addr
.s6_addr
[0] = 128;
225 tor_addr_from_sockaddr(&t1
, (struct sockaddr
*)sin6
, &port1
);
226 tt_int_op(tor_addr_family(&t1
),OP_EQ
, AF_INET6
);
227 tt_int_op(port1
, OP_EQ
, 7070);
228 p1
= tor_addr_to_str(buf
, &t1
, sizeof(buf
), 0);
229 tt_str_op(p1
,OP_EQ
, "8000::");
231 memset(&sa_storage
, 0, sizeof(sa_storage
));
232 tt_int_op(sizeof(struct sockaddr_in6
),OP_EQ
,
233 tor_addr_to_sockaddr(&t1
, 9999, (struct sockaddr
*)&sa_storage
,
234 sizeof(sa_storage
)));
235 tt_int_op(AF_INET6
,OP_EQ
, sin6
->sin6_family
);
236 tt_int_op(9999,OP_EQ
, ntohs(sin6
->sin6_port
));
237 tt_int_op(0x80000000,OP_EQ
, ntohl(S6_ADDR32(sin6
->sin6_addr
)[0]));
239 /* ==== tor_addr_lookup: static cases. (Can't test dns without knowing we
240 * have a good resolver. */
241 tt_int_op(0,OP_EQ
, tor_addr_lookup("127.128.129.130", AF_UNSPEC
, &t1
));
242 tt_int_op(AF_INET
,OP_EQ
, tor_addr_family(&t1
));
243 tt_int_op(tor_addr_to_ipv4h(&t1
),OP_EQ
, 0x7f808182);
245 tt_int_op(0,OP_EQ
, tor_addr_lookup("9000::5", AF_UNSPEC
, &t1
));
246 tt_int_op(AF_INET6
,OP_EQ
, tor_addr_family(&t1
));
247 tt_int_op(0x90,OP_EQ
, tor_addr_to_in6_addr8(&t1
)[0]);
248 tt_assert(fast_mem_is_zero((char*)tor_addr_to_in6_addr8(&t1
)+1, 14));
249 tt_int_op(0x05,OP_EQ
, tor_addr_to_in6_addr8(&t1
)[15]);
251 /* === Test pton: valid af_inet6 */
252 /* Simple, valid parsing. */
253 r
= tor_inet_pton(AF_INET6
,
254 "0102:0304:0506:0708:090A:0B0C:0D0E:0F10", &a1
);
255 tt_int_op(r
, OP_EQ
, 1);
256 for (i
=0;i
<16;++i
) { tt_int_op(i
+1,OP_EQ
, (int)a1
.s6_addr
[i
]); }
258 test_pton6_same("0102:0304:0506:0708:090A:0B0C:0D0E:0F10",
259 "0102:0304:0506:0708:090A:0B0C:13.14.15.16");
260 /* shortened words. */
261 test_pton6_same("0001:0099:BEEF:0000:0123:FFFF:0001:0001",
262 "1:99:BEEF:0:0123:FFFF:1:1");
263 /* zeros at the beginning */
264 test_pton6_same("0000:0000:0000:0000:0009:C0A8:0001:0001",
266 test_pton6_same("0000:0000:0000:0000:0009:C0A8:0001:0001",
268 /* zeros in the middle. */
269 test_pton6_same("fe80:0000:0000:0000:0202:1111:0001:0001",
270 "fe80::202:1111:1:1");
271 /* zeros at the end. */
272 test_pton6_same("1000:0001:0000:0007:0000:0000:0000:0000",
275 /* === Test ntop: af_inet6 */
276 test_ntop6_reduces("0:0:0:0:0:0:0:0", "::");
278 test_ntop6_reduces("0001:0099:BEEF:0006:0123:FFFF:0001:0001",
279 "1:99:beef:6:123:ffff:1:1");
281 //test_ntop6_reduces("0:0:0:0:0:0:c0a8:0101", "::192.168.1.1");
282 test_ntop6_reduces("0:0:0:0:0:ffff:c0a8:0101", "::ffff:192.168.1.1");
283 test_ntop6_reduces("0:0:0:0:0:0:c0a8:0101", "::192.168.1.1");
284 test_ntop6_reduces("002:0:0000:0:3::4", "2::3:0:0:4");
285 test_ntop6_reduces("0:0::1:0:3", "::1:0:3");
286 test_ntop6_reduces("008:0::0", "8::");
287 test_ntop6_reduces("0:0:0:0:0:ffff::1", "::ffff:0.0.0.1");
288 test_ntop6_reduces("abcd:0:0:0:0:0:7f00::", "abcd::7f00:0");
289 test_ntop6_reduces("0000:0000:0000:0000:0009:C0A8:0001:0001",
291 test_ntop6_reduces("fe80:0000:0000:0000:0202:1111:0001:0001",
292 "fe80::202:1111:1:1");
293 test_ntop6_reduces("1000:0001:0000:0007:0000:0000:0000:0000",
297 tt_int_op(tor_inet_pton(AF_UNSPEC
, 0, 0),OP_EQ
, -1);
299 /* === Test pton: invalid in6. */
300 test_pton6_bad("foobar.");
301 test_pton6_bad("-1::");
302 test_pton6_bad("00001::");
303 test_pton6_bad("10000::");
304 test_pton6_bad("::10000");
305 test_pton6_bad("55555::");
306 test_pton6_bad("9:-60::");
307 test_pton6_bad("9:+60::");
308 test_pton6_bad("9|60::");
309 test_pton6_bad("0x60::");
310 test_pton6_bad("::0x60");
311 test_pton6_bad("9:0x60::");
312 test_pton6_bad("1:2:33333:4:0002:3::");
313 test_pton6_bad("1:2:3333:4:fish:3::");
314 test_pton6_bad("1:2:3:4:5:6:7:8:9");
315 test_pton6_bad("1:2:3:4:5:6:7");
316 test_pton6_bad("1:2:3:4:5:6:1.2.3.4.5");
317 test_pton6_bad("1:2:3:4:5:6:1.2.3");
318 test_pton6_bad("::1.2.3");
319 test_pton6_bad("::1.2.3.4.5");
320 test_pton6_bad("::ffff:0xff.0.0.0");
321 test_pton6_bad("::ffff:ff.0.0.0");
322 test_pton6_bad("::ffff:256.0.0.0");
323 test_pton6_bad("::ffff:-1.0.0.0");
324 test_pton6_bad("99");
328 test_pton6_bad("1::2::3:4");
329 test_pton6_bad("a:::b:c");
330 test_pton6_bad(":::a:b:c");
331 test_pton6_bad("a:b:c:::");
332 test_pton6_bad("1.2.3.4");
333 test_pton6_bad(":1.2.3.4");
334 test_pton6_bad(".2.3.4");
335 /* Regression tests for 22789. */
336 test_pton6_bad("0xfoo");
337 test_pton6_bad("0x88");
338 test_pton6_bad("0xyxxy");
339 test_pton6_bad("0XFOO");
340 test_pton6_bad("0X88");
341 test_pton6_bad("0XYXXY");
342 test_pton6_bad("0x");
343 test_pton6_bad("0X");
344 test_pton6_bad("2000::1a00::1000:fc098");
346 /* test internal checking */
347 test_external_ip("fbff:ffff::2:7", 0);
348 test_internal_ip("fc01::2:7", 0);
349 test_internal_ip("fc01::02:7", 0);
350 test_internal_ip("fc01::002:7", 0);
351 test_internal_ip("fc01::0002:7", 0);
352 test_internal_ip("fdff:ffff::f:f", 0);
353 test_external_ip("fe00::3:f", 0);
355 test_external_ip("fe7f:ffff::2:7", 0);
356 test_internal_ip("fe80::2:7", 0);
357 test_internal_ip("febf:ffff::f:f", 0);
359 test_internal_ip("fec0::2:7:7", 0);
360 test_internal_ip("feff:ffff::e:7:7", 0);
361 test_external_ip("ff00::e:7:7", 0);
363 test_internal_ip("::", 0);
364 test_internal_ip("::1", 0);
365 test_internal_ip("::1", 1);
366 test_internal_ip("::", 0);
367 test_external_ip("::", 1);
368 test_external_ip("::2", 0);
369 test_external_ip("2001::", 0);
370 test_external_ip("ffff::", 0);
372 test_external_ip("::ffff:0.0.0.0", 1);
373 test_internal_ip("::ffff:0.0.0.0", 0);
374 test_internal_ip("::ffff:0.255.255.255", 0);
375 test_external_ip("::ffff:1.0.0.0", 0);
377 test_external_ip("::ffff:9.255.255.255", 0);
378 test_internal_ip("::ffff:10.0.0.0", 0);
379 test_internal_ip("::ffff:10.255.255.255", 0);
380 test_external_ip("::ffff:11.0.0.0", 0);
382 test_external_ip("::ffff:126.255.255.255", 0);
383 test_internal_ip("::ffff:127.0.0.0", 0);
384 test_internal_ip("::ffff:127.255.255.255", 0);
385 test_external_ip("::ffff:128.0.0.0", 0);
387 test_external_ip("::ffff:172.15.255.255", 0);
388 test_internal_ip("::ffff:172.16.0.0", 0);
389 test_internal_ip("::ffff:172.31.255.255", 0);
390 test_external_ip("::ffff:172.32.0.0", 0);
392 test_external_ip("::ffff:192.167.255.255", 0);
393 test_internal_ip("::ffff:192.168.0.0", 0);
394 test_internal_ip("::ffff:192.168.255.255", 0);
395 test_external_ip("::ffff:192.169.0.0", 0);
397 test_external_ip("::ffff:169.253.255.255", 0);
398 test_internal_ip("::ffff:169.254.0.0", 0);
399 test_internal_ip("::ffff:169.254.255.255", 0);
400 test_external_ip("::ffff:169.255.0.0", 0);
402 /* tor_addr_compare(tor_addr_t x2) */
403 test_addr_compare("ffff::", OP_EQ
, "ffff::0");
404 test_addr_compare("0::3:2:1", OP_LT
, "0::ffff:0.3.2.1");
405 test_addr_compare("0::2:2:1", OP_LT
, "0::ffff:0.3.2.1");
406 test_addr_compare("0::ffff:0.3.2.1", OP_GT
, "0::0:0:0");
407 test_addr_compare("0::ffff:5.2.2.1", OP_LT
,
408 "::ffff:6.0.0.0"); /* XXXX wrong. */
409 tor_addr_parse_mask_ports("[::ffff:2.3.4.5]", 0, &t1
, NULL
, NULL
, NULL
);
410 tor_addr_parse_mask_ports("2.3.4.5", 0, &t2
, NULL
, NULL
, NULL
);
411 tt_int_op(tor_addr_compare(&t1
, &t2
, CMP_SEMANTIC
), OP_EQ
, 0);
412 tor_addr_parse_mask_ports("[::ffff:2.3.4.4]", 0, &t1
, NULL
, NULL
, NULL
);
413 tor_addr_parse_mask_ports("2.3.4.5", 0, &t2
, NULL
, NULL
, NULL
);
414 tt_int_op(tor_addr_compare(&t1
, &t2
, CMP_SEMANTIC
), OP_LT
, 0);
416 /* test compare_masked */
417 test_addr_compare_masked("ffff::", OP_EQ
, "ffff::0", 128);
418 test_addr_compare_masked("ffff::", OP_EQ
, "ffff::0", 64);
419 test_addr_compare_masked("0::2:2:1", OP_LT
, "0::8000:2:1", 81);
420 test_addr_compare_masked("0::2:2:1", OP_EQ
, "0::8000:2:1", 80);
422 /* Test undecorated tor_addr_to_str */
423 tt_int_op(AF_INET6
,OP_EQ
, tor_addr_parse(&t1
, "[123:45:6789::5005:11]"));
424 p1
= tor_addr_to_str(buf
, &t1
, sizeof(buf
), 0);
425 tt_str_op(p1
,OP_EQ
, "123:45:6789::5005:11");
426 tt_int_op(AF_INET
,OP_EQ
, tor_addr_parse(&t1
, "18.0.0.1"));
427 p1
= tor_addr_to_str(buf
, &t1
, sizeof(buf
), 0);
428 tt_str_op(p1
,OP_EQ
, "18.0.0.1");
430 /* Test decorated tor_addr_to_str */
431 tt_int_op(AF_INET6
,OP_EQ
, tor_addr_parse(&t1
, "[123:45:6789::5005:11]"));
432 p1
= tor_addr_to_str(buf
, &t1
, sizeof(buf
), 1);
433 tt_str_op(p1
,OP_EQ
, "[123:45:6789::5005:11]");
434 tt_int_op(AF_INET
,OP_EQ
, tor_addr_parse(&t1
, "18.0.0.1"));
435 p1
= tor_addr_to_str(buf
, &t1
, sizeof(buf
), 1);
436 tt_str_op(p1
,OP_EQ
, "18.0.0.1");
438 /* Test buffer bounds checking of tor_addr_to_str */
439 tt_int_op(AF_INET6
,OP_EQ
, tor_addr_parse(&t1
, "::")); /* 2 + \0 */
440 tt_ptr_op(tor_addr_to_str(buf
, &t1
, 2, 0),OP_EQ
, NULL
); /* too short buf */
441 tt_str_op(tor_addr_to_str(buf
, &t1
, 3, 0),OP_EQ
, "::");
442 tt_ptr_op(tor_addr_to_str(buf
, &t1
, 4, 1),OP_EQ
, NULL
); /* too short buf */
443 tt_str_op(tor_addr_to_str(buf
, &t1
, 5, 1),OP_EQ
, "[::]");
445 tt_int_op(AF_INET6
,OP_EQ
, tor_addr_parse(&t1
, "2000::1337")); /* 10 + \0 */
446 tt_ptr_op(tor_addr_to_str(buf
, &t1
, 10, 0),OP_EQ
, NULL
); /* too short buf */
447 tt_str_op(tor_addr_to_str(buf
, &t1
, 11, 0),OP_EQ
, "2000::1337");
448 tt_ptr_op(tor_addr_to_str(buf
, &t1
, 12, 1),OP_EQ
, NULL
); /* too short buf */
449 tt_str_op(tor_addr_to_str(buf
, &t1
, 13, 1),OP_EQ
, "[2000::1337]");
451 tt_int_op(AF_INET
,OP_EQ
, tor_addr_parse(&t1
, "1.2.3.4")); /* 7 + \0 */
452 tt_ptr_op(tor_addr_to_str(buf
, &t1
, 7, 0),OP_EQ
, NULL
); /* too short buf */
453 tt_str_op(tor_addr_to_str(buf
, &t1
, 8, 0),OP_EQ
, "1.2.3.4");
455 tt_int_op(AF_INET
, OP_EQ
,
456 tor_addr_parse(&t1
, "255.255.255.255")); /* 15 + \0 */
457 tt_ptr_op(tor_addr_to_str(buf
, &t1
, 15, 0),OP_EQ
, NULL
); /* too short buf */
458 tt_str_op(tor_addr_to_str(buf
, &t1
, 16, 0),OP_EQ
, "255.255.255.255");
459 tt_ptr_op(tor_addr_to_str(buf
, &t1
, 15, 1),OP_EQ
, NULL
); /* too short buf */
460 tt_str_op(tor_addr_to_str(buf
, &t1
, 16, 1),OP_EQ
, "255.255.255.255");
462 t1
.family
= AF_UNSPEC
;
463 tt_ptr_op(tor_addr_to_str(buf
, &t1
, sizeof(buf
), 0),OP_EQ
, NULL
);
465 /* Test tor_addr_parse_PTR_name */
466 i
= tor_addr_parse_PTR_name(&t1
, "Foobar.baz", AF_UNSPEC
, 0);
467 tt_int_op(0,OP_EQ
, i
);
468 i
= tor_addr_parse_PTR_name(&t1
, "Foobar.baz", AF_UNSPEC
, 1);
469 tt_int_op(0,OP_EQ
, i
);
470 i
= tor_addr_parse_PTR_name(&t1
, "9999999999999999999999999999.in-addr.arpa",
472 tt_int_op(-1,OP_EQ
, i
);
473 i
= tor_addr_parse_PTR_name(&t1
, "1.0.168.192.in-addr.arpa",
475 tt_int_op(1,OP_EQ
, i
);
476 tt_int_op(tor_addr_family(&t1
),OP_EQ
, AF_INET
);
477 p1
= tor_addr_to_str(buf
, &t1
, sizeof(buf
), 1);
478 tt_str_op(p1
,OP_EQ
, "192.168.0.1");
479 i
= tor_addr_parse_PTR_name(&t1
, "192.168.0.99", AF_UNSPEC
, 0);
480 tt_int_op(0,OP_EQ
, i
);
481 i
= tor_addr_parse_PTR_name(&t1
, "192.168.0.99", AF_UNSPEC
, 1);
482 tt_int_op(1,OP_EQ
, i
);
483 p1
= tor_addr_to_str(buf
, &t1
, sizeof(buf
), 1);
484 tt_str_op(p1
,OP_EQ
, "192.168.0.99");
485 memset(&t1
, 0, sizeof(t1
));
486 i
= tor_addr_parse_PTR_name(&t1
,
487 "0.1.2.3.4.5.6.7.8.9.a.b.c.d.e.f."
488 "f.e.e.b.1.e.b.e.e.f.f.e.e.e.d.9."
491 tt_int_op(1,OP_EQ
, i
);
492 p1
= tor_addr_to_str(buf
, &t1
, sizeof(buf
), 1);
493 tt_str_op(p1
,OP_EQ
, "[9dee:effe:ebe1:beef:fedc:ba98:7654:3210]");
495 i
= tor_addr_parse_PTR_name(&t1
,
496 "6.7.8.9.a.b.c.d.e.f."
497 "f.e.e.b.1.e.b.e.e.f.f.e.e.e.d.9."
500 tt_int_op(i
,OP_EQ
, -1);
501 i
= tor_addr_parse_PTR_name(&t1
,
502 "6.7.8.9.a.b.c.d.e.f.a.b.c.d.e.f.0."
503 "f.e.e.b.1.e.b.e.e.f.f.e.e.e.d.9."
506 tt_int_op(i
,OP_EQ
, -1);
507 i
= tor_addr_parse_PTR_name(&t1
,
508 "6.7.8.9.a.b.c.d.e.f.X.0.0.0.0.9."
509 "f.e.e.b.1.e.b.e.e.f.f.e.e.e.d.9."
512 tt_int_op(i
,OP_EQ
, -1);
513 i
= tor_addr_parse_PTR_name(&t1
, "32.1.1.in-addr.arpa",
515 tt_int_op(i
,OP_EQ
, -1);
516 i
= tor_addr_parse_PTR_name(&t1
, ".in-addr.arpa",
518 tt_int_op(i
,OP_EQ
, -1);
519 i
= tor_addr_parse_PTR_name(&t1
, "1.2.3.4.5.in-addr.arpa",
521 tt_int_op(i
,OP_EQ
, -1);
522 i
= tor_addr_parse_PTR_name(&t1
, "1.2.3.4.5.in-addr.arpa",
524 tt_int_op(i
,OP_EQ
, -1);
525 i
= tor_addr_parse_PTR_name(&t1
,
526 "6.7.8.9.a.b.c.d.e.f.a.b.c.d.e.0."
527 "f.e.e.b.1.e.b.e.e.f.f.e.e.e.d.9."
530 tt_int_op(i
,OP_EQ
, -1);
532 /* === Test tor_addr_to_PTR_name */
534 /* Stage IPv4 addr */
535 memset(&sa_storage
, 0, sizeof(sa_storage
));
536 sin
= (struct sockaddr_in
*)&sa_storage
;
537 sin
->sin_family
= AF_INET
;
538 sin
->sin_addr
.s_addr
= htonl(0x7f010203); /* 127.1.2.3 */
539 tor_addr_from_sockaddr(&t1
, (struct sockaddr
*)sin
, NULL
);
541 /* Check IPv4 PTR - too short buffer */
542 tt_int_op(tor_addr_to_PTR_name(rbuf
, 1, &t1
),OP_EQ
, -1);
543 tt_int_op(tor_addr_to_PTR_name(rbuf
,
544 strlen("3.2.1.127.in-addr.arpa") - 1,
547 /* Check IPv4 PTR - valid addr */
548 tt_int_op(tor_addr_to_PTR_name(rbuf
, sizeof(rbuf
), &t1
),OP_EQ
,
549 strlen("3.2.1.127.in-addr.arpa"));
550 tt_str_op(rbuf
,OP_EQ
, "3.2.1.127.in-addr.arpa");
552 /* Invalid addr family */
553 t1
.family
= AF_UNSPEC
;
554 tt_int_op(tor_addr_to_PTR_name(rbuf
, sizeof(rbuf
), &t1
),OP_EQ
, -1);
556 /* Stage IPv6 addr */
557 memset(&sa_storage
, 0, sizeof(sa_storage
));
558 sin6
= (struct sockaddr_in6
*)&sa_storage
;
559 sin6
->sin6_family
= AF_INET6
;
560 sin6
->sin6_addr
.s6_addr
[0] = 0x80; /* 8000::abcd */
561 sin6
->sin6_addr
.s6_addr
[14] = 0xab;
562 sin6
->sin6_addr
.s6_addr
[15] = 0xcd;
564 tor_addr_from_sockaddr(&t1
, (struct sockaddr
*)sin6
, NULL
);
567 const char* addr_PTR
= "d.c.b.a.0.0.0.0.0.0.0.0.0.0.0.0."
568 "0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.8.ip6.arpa";
570 /* Check IPv6 PTR - too short buffer */
571 tt_int_op(tor_addr_to_PTR_name(rbuf
, 0, &t1
),OP_EQ
, -1);
572 tt_int_op(tor_addr_to_PTR_name(rbuf
, strlen(addr_PTR
) - 1, &t1
),OP_EQ
, -1);
574 /* Check IPv6 PTR - valid addr */
575 tt_int_op(tor_addr_to_PTR_name(rbuf
, sizeof(rbuf
), &t1
),OP_EQ
,
577 tt_str_op(rbuf
,OP_EQ
, addr_PTR
);
580 /* XXXX turn this into a separate function; it's not all IPv6. */
581 /* test tor_addr_parse_mask_ports */
582 test_addr_mask_ports_parse("[::f]/17:47-95", AF_INET6
,
583 0, 0, 0, 0x0000000f, 17, 47, 95);
584 tt_str_op(p1
,OP_EQ
, "::f");
585 //test_addr_parse("[::fefe:4.1.1.7/120]:999-1000");
586 //test_addr_parse_check("::fefe:401:107", 120, 999, 1000);
587 test_addr_mask_ports_parse("[::ffff:4.1.1.7]/120:443", AF_INET6
,
588 0, 0, 0x0000ffff, 0x04010107, 120, 443, 443);
589 tt_str_op(p1
,OP_EQ
, "::ffff:4.1.1.7");
590 test_addr_mask_ports_parse("[abcd:2::44a:0]:2-65000", AF_INET6
,
591 0xabcd0002, 0, 0, 0x044a0000, 128, 2, 65000);
593 tt_str_op(p1
,OP_EQ
, "abcd:2::44a:0");
594 /* Try some long addresses. */
595 r
=tor_addr_parse_mask_ports("[ffff:1111:1111:1111:1111:1111:1111:1111]",
596 0, &t1
, NULL
, NULL
, NULL
);
597 tt_int_op(r
, OP_EQ
, AF_INET6
);
598 r
=tor_addr_parse_mask_ports("[ffff:1111:1111:1111:1111:1111:1111:11111]",
599 0, &t1
, NULL
, NULL
, NULL
);
600 tt_int_op(r
, OP_EQ
, -1);
601 r
=tor_addr_parse_mask_ports("[ffff:1111:1111:1111:1111:1111:1111:1111:1]",
602 0, &t1
, NULL
, NULL
, NULL
);
603 tt_int_op(r
, OP_EQ
, -1);
604 r
=tor_addr_parse_mask_ports(
605 "[ffff:1111:1111:1111:1111:1111:1111:ffff:"
606 "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff:"
607 "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff:"
608 "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff]",
609 0, &t1
, NULL
, NULL
, NULL
);
610 tt_int_op(r
, OP_EQ
, -1);
611 /* Try some failing cases. */
612 r
=tor_addr_parse_mask_ports("[fefef::]/112", 0, &t1
, NULL
, NULL
, NULL
);
613 tt_int_op(r
, OP_EQ
, -1);
614 r
=tor_addr_parse_mask_ports("[fefe::/112", 0, &t1
, NULL
, NULL
, NULL
);
615 tt_int_op(r
, OP_EQ
, -1);
616 r
=tor_addr_parse_mask_ports("[fefe::", 0, &t1
, NULL
, NULL
, NULL
);
617 tt_int_op(r
, OP_EQ
, -1);
618 r
=tor_addr_parse_mask_ports("[fefe::X]", 0, &t1
, NULL
, NULL
, NULL
);
619 tt_int_op(r
, OP_EQ
, -1);
620 r
=tor_addr_parse_mask_ports("efef::/112", 0, &t1
, NULL
, NULL
, NULL
);
621 tt_int_op(r
, OP_EQ
, -1);
622 r
=tor_addr_parse_mask_ports("[f:f:f:f:f:f:f:f::]",0,&t1
, NULL
, NULL
, NULL
);
623 tt_int_op(r
, OP_EQ
, -1);
624 r
=tor_addr_parse_mask_ports("[::f:f:f:f:f:f:f:f]",0,&t1
, NULL
, NULL
, NULL
);
625 tt_int_op(r
, OP_EQ
, -1);
626 r
=tor_addr_parse_mask_ports("[f:f:f:f:f:f:f:f:f]",0,&t1
, NULL
, NULL
, NULL
);
627 tt_int_op(r
, OP_EQ
, -1);
628 r
=tor_addr_parse_mask_ports("[f:f:f:f:f::]/fred",0,&t1
,&mask
, NULL
, NULL
);
629 tt_int_op(r
, OP_EQ
, -1);
630 r
=tor_addr_parse_mask_ports("[f:f:f:f:f::]/255.255.0.0",
631 0,&t1
, NULL
, NULL
, NULL
);
632 tt_int_op(r
, OP_EQ
, -1);
633 /* This one will get rejected because it isn't a pure prefix. */
634 r
=tor_addr_parse_mask_ports("1.1.2.3/255.255.64.0",0,&t1
, &mask
,NULL
,NULL
);
635 tt_int_op(r
, OP_EQ
, -1);
636 /* Test for V4-mapped address with mask < 96. (arguably not valid) */
637 r
=tor_addr_parse_mask_ports("[::ffff:1.1.2.2/33]",0,&t1
, &mask
, NULL
, NULL
);
638 tt_int_op(r
, OP_EQ
, -1);
639 r
=tor_addr_parse_mask_ports("1.1.2.2/33",0,&t1
, &mask
, NULL
, NULL
);
640 tt_int_op(r
, OP_EQ
, -1);
641 /* Try extended wildcard addresses with out TAPMP_EXTENDED_STAR*/
642 r
=tor_addr_parse_mask_ports("*4",0,&t1
, &mask
, NULL
, NULL
);
643 tt_int_op(r
, OP_EQ
, -1);
644 r
=tor_addr_parse_mask_ports("*6",0,&t1
, &mask
, NULL
, NULL
);
645 tt_int_op(r
, OP_EQ
, -1);
646 tt_int_op(r
, OP_EQ
, -1);
647 /* Try a mask with a wildcard. */
648 r
=tor_addr_parse_mask_ports("*/16",0,&t1
, &mask
, NULL
, NULL
);
649 tt_int_op(r
, OP_EQ
, -1);
650 r
=tor_addr_parse_mask_ports("*4/16",TAPMP_EXTENDED_STAR
,
651 &t1
, &mask
, NULL
, NULL
);
652 tt_int_op(r
, OP_EQ
, -1);
653 r
=tor_addr_parse_mask_ports("*6/30",TAPMP_EXTENDED_STAR
,
654 &t1
, &mask
, NULL
, NULL
);
655 tt_int_op(r
, OP_EQ
, -1);
656 /* Basic mask tests*/
657 r
=tor_addr_parse_mask_ports("1.1.2.2/31",0,&t1
, &mask
, NULL
, NULL
);
658 tt_int_op(r
, OP_EQ
, AF_INET
);
659 tt_int_op(mask
,OP_EQ
,31);
660 tt_int_op(tor_addr_family(&t1
),OP_EQ
,AF_INET
);
661 tt_int_op(tor_addr_to_ipv4h(&t1
),OP_EQ
,0x01010202);
662 r
=tor_addr_parse_mask_ports("3.4.16.032:1-2",0,&t1
, &mask
, &port1
, &port2
);
663 tt_int_op(r
, OP_EQ
, -1);
664 r
=tor_addr_parse_mask_ports("1.1.2.3/255.255.128.0",0,&t1
, &mask
,NULL
,NULL
);
665 tt_int_op(r
, OP_EQ
, AF_INET
);
666 tt_int_op(mask
,OP_EQ
,17);
667 tt_int_op(tor_addr_family(&t1
),OP_EQ
,AF_INET
);
668 tt_int_op(tor_addr_to_ipv4h(&t1
),OP_EQ
,0x01010203);
669 r
=tor_addr_parse_mask_ports("[efef::]/112",0,&t1
, &mask
, &port1
, &port2
);
670 tt_int_op(r
, OP_EQ
, AF_INET6
);
671 tt_uint_op(port1
, OP_EQ
, 1);
672 tt_uint_op(port2
, OP_EQ
, 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
,OP_EQ
,AF_INET
); /* Old users of this always get inet */
676 tt_int_op(tor_addr_family(&t1
),OP_EQ
,AF_INET
);
677 tt_int_op(tor_addr_to_ipv4h(&t1
),OP_EQ
,0);
678 tt_int_op(mask
,OP_EQ
,0);
679 tt_int_op(port1
,OP_EQ
,80);
680 tt_int_op(port2
,OP_EQ
,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
,OP_EQ
,AF_UNSPEC
);
685 tt_int_op(tor_addr_family(&t1
),OP_EQ
,AF_UNSPEC
);
686 tt_int_op(mask
,OP_EQ
,0);
687 tt_int_op(port1
,OP_EQ
,8000);
688 tt_int_op(port2
,OP_EQ
,9000);
689 r
=tor_addr_parse_mask_ports("*4:6667",TAPMP_EXTENDED_STAR
,
690 &t1
,&mask
,&port1
,&port2
);
691 tt_int_op(r
,OP_EQ
,AF_INET
);
692 tt_int_op(tor_addr_family(&t1
),OP_EQ
,AF_INET
);
693 tt_int_op(tor_addr_to_ipv4h(&t1
),OP_EQ
,0);
694 tt_int_op(mask
,OP_EQ
,0);
695 tt_int_op(port1
,OP_EQ
,6667);
696 tt_int_op(port2
,OP_EQ
,6667);
697 r
=tor_addr_parse_mask_ports("*6",TAPMP_EXTENDED_STAR
,
698 &t1
,&mask
,&port1
,&port2
);
699 tt_int_op(r
,OP_EQ
,AF_INET6
);
700 tt_int_op(tor_addr_family(&t1
),OP_EQ
,AF_INET6
);
701 tt_assert(fast_mem_is_zero((const char*)tor_addr_to_in6_addr32(&t1
), 16));
702 tt_int_op(mask
,OP_EQ
,0);
703 tt_int_op(port1
,OP_EQ
,1);
704 tt_int_op(port2
,OP_EQ
,65535);
706 /* make sure inet address lengths >= max */
707 tt_int_op(INET_NTOA_BUF_LEN
, OP_GE
, sizeof("255.255.255.255"));
708 tt_int_op(TOR_ADDR_BUF_LEN
, OP_GE
,
709 sizeof("ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255"));
711 tt_assert(sizeof(tor_addr_t
) >= sizeof(struct in6_addr
));
713 /* get interface addresses */
714 r
= get_interface_address6(LOG_DEBUG
, AF_INET
, &t1
);
715 tt_int_op(r
, OP_LE
, 0); // "it worked or it didn't"
716 i
= get_interface_address6(LOG_DEBUG
, AF_INET6
, &t2
);
717 tt_int_op(i
, OP_LE
, 0); // "it worked or it didn't"
719 TT_BLATHER(("v4 address: %s (family=%d)", fmt_addr(&t1
),
720 tor_addr_family(&t1
)));
721 TT_BLATHER(("v6 address: %s (family=%d)", fmt_addr(&t2
),
722 tor_addr_family(&t2
)));
728 /* Test that addr_str successfully parses, and:
729 * - the address has family expect_family,
730 * - the fmt_decorated result of tor_addr_to_str() is expect_str.
732 #define TEST_ADDR_PARSE_FMT(addr_str, expect_family, fmt_decorated, \
735 r = tor_addr_parse(&addr, addr_str); \
736 tt_int_op(r, OP_EQ, expect_family); \
737 sv = tor_addr_to_str(buf, &addr, sizeof(buf), fmt_decorated); \
738 tt_str_op(sv, OP_EQ, buf); \
739 tt_str_op(buf, OP_EQ, expect_str); \
742 /* Test that addr_str fails to parse, and:
743 * - the returned address is null.
745 #define TEST_ADDR_PARSE_XFAIL(addr_str) \
747 r = tor_addr_parse(&addr, addr_str); \
748 tt_int_op(r, OP_EQ, -1); \
749 tt_assert(tor_addr_is_null(&addr)); \
752 /* Test that addr_port_str and default_port successfully parse, and:
753 * - the address has family expect_family,
754 * - the fmt_decorated result of tor_addr_to_str() is expect_str,
755 * - the port is expect_port.
757 #define TEST_ADDR_PORT_PARSE_FMT(addr_port_str, default_port, expect_family, \
758 fmt_decorated, expect_str, expect_port) \
760 r = tor_addr_port_parse(LOG_DEBUG, addr_port_str, &addr, &port, \
762 tt_int_op(r, OP_EQ, 0); \
763 tt_int_op(tor_addr_family(&addr), OP_EQ, expect_family); \
764 sv = tor_addr_to_str(buf, &addr, sizeof(buf), fmt_decorated); \
765 tt_str_op(sv, OP_EQ, buf); \
766 tt_str_op(buf, OP_EQ, expect_str); \
767 tt_int_op(port, OP_EQ, expect_port); \
770 /* Test that addr_port_str and default_port fail to parse, and:
771 * - the returned address is null,
772 * - the returned port is 0.
774 #define TEST_ADDR_PORT_PARSE_XFAIL(addr_port_str, default_port) \
776 r = tor_addr_port_parse(LOG_DEBUG, addr_port_str, &addr, &port, \
778 tt_int_op(r, OP_EQ, -1); \
779 tt_assert(tor_addr_is_null(&addr)); \
780 tt_int_op(port, OP_EQ, 0); \
783 /* Test that addr_str successfully parses as an IPv4 address using
784 * tor_lookup_hostname(), and:
785 * - the fmt_addr32() of the result is expect_str.
787 #define TEST_ADDR_V4_LOOKUP_HOSTNAME(addr_str, expect_str) \
789 r = tor_lookup_hostname(addr_str, &addr32h); \
790 tt_int_op(r, OP_EQ, 0); \
791 tt_str_op(fmt_addr32(addr32h), OP_EQ, expect_str); \
794 /* Test that bad_str fails to parse using tor_lookup_hostname(), with a
795 * permanent failure, and:
796 * - the returned address is 0.
798 #define TEST_ADDR_V4_LOOKUP_XFAIL(bad_str) \
800 r = tor_lookup_hostname(bad_str, &addr32h); \
801 tt_int_op(r, OP_EQ, -1); \
802 tt_int_op(addr32h, OP_EQ, 0); \
805 /* Test that looking up host_str as an IPv4 address using tor_lookup_hostname()
806 * does something sensible:
807 * - the result is -1, 0, or 1.
808 * - if the result is a failure, the returned address is 0.
809 * We can't rely on the result of this function, because it depends on the
812 #define TEST_HOST_V4_LOOKUP(host_str) \
814 r = tor_lookup_hostname(host_str, &addr32h); \
815 tt_int_op(r, OP_GE, -1); \
816 tt_int_op(r, OP_LE, 1); \
818 tt_int_op(addr32h, OP_EQ, 0); \
821 /* Test that addr_str successfully parses as a require_family IP address using
822 * tor_addr_lookup(), and:
823 * - the address has family expect_family,
824 * - the fmt_decorated result of tor_addr_to_str() is expect_str.
826 #define TEST_ADDR_LOOKUP_FMT(addr_str, require_family, expect_family, \
827 fmt_decorated, expect_str) \
829 r = tor_addr_lookup(addr_str, require_family, &addr); \
830 tt_int_op(r, OP_EQ, 0); \
831 tt_int_op(tor_addr_family(&addr), OP_EQ, expect_family); \
832 sv = tor_addr_to_str(buf, &addr, sizeof(buf), fmt_decorated); \
833 tt_str_op(sv, OP_EQ, buf); \
834 tt_str_op(buf, OP_EQ, expect_str); \
837 /* Test that bad_str fails to parse as a require_family IP address using
838 * tor_addr_lookup(), with a permanent failure, and:
839 * - the returned address is null.
841 #define TEST_ADDR_LOOKUP_XFAIL(bad_str, require_family) \
843 r = tor_addr_lookup(bad_str, require_family, &addr); \
844 tt_int_op(r, OP_EQ, -1); \
845 tt_assert(tor_addr_is_null(&addr)); \
848 /* Test that looking up host_string as a require_family IP address using
849 * tor_addr_lookup(), does something sensible:
850 * - the result is -1, 0, or 1.
851 * - if the result is a failure, the returned address is null.
852 * We can't rely on the result of this function, because it depends on the
855 #define TEST_HOST_LOOKUP(host_str, require_family) \
857 r = tor_addr_lookup(host_str, require_family, &addr); \
858 tt_int_op(r, OP_GE, -1); \
859 tt_int_op(r, OP_LE, 1); \
861 tt_assert(tor_addr_is_null(&addr)); \
864 /* Test that addr_port_str successfully parses as an IP address and port
865 * using tor_addr_port_lookup(), and:
866 * - the address has family expect_family,
867 * - the fmt_decorated result of tor_addr_to_str() is expect_str,
868 * - the port is expect_port.
870 #define TEST_ADDR_PORT_LOOKUP_FMT(addr_port_str, expect_family, \
871 fmt_decorated, expect_str, expect_port) \
873 r = tor_addr_port_lookup(addr_port_str, &addr, &port); \
874 tt_int_op(r, OP_EQ, 0); \
875 tt_int_op(tor_addr_family(&addr), OP_EQ, expect_family); \
876 sv = tor_addr_to_str(buf, &addr, sizeof(buf), fmt_decorated); \
877 tt_str_op(sv, OP_EQ, buf); \
878 tt_str_op(buf, OP_EQ, expect_str); \
879 tt_int_op(port, OP_EQ, expect_port); \
882 /* Test that bad_str fails to parse as an IP address and port
883 * using tor_addr_port_lookup(), and:
884 * - the returned address is null,
885 * - the returned port is 0.
887 #define TEST_ADDR_PORT_LOOKUP_XFAIL(bad_str) \
889 r = tor_addr_port_lookup(bad_str, &addr, &port); \
890 tt_int_op(r, OP_EQ, -1); \
891 tt_assert(tor_addr_is_null(&addr)); \
892 tt_int_op(port, OP_EQ, 0); \
895 /* Test that looking up host_port_str as an IP address using
896 * tor_addr_port_lookup(), does something sensible:
897 * - the result is -1 or 0.
898 * - if the result is a failure, the returned address is null, and the
899 * returned port is zero,
900 * - if the result is a success, the returned port is expect_success_port,
901 * and the returned family is AF_INET or AF_INET6.
902 * We can't rely on the result of this function, because it depends on the
905 #define TEST_HOST_PORT_LOOKUP(host_port_str, expect_success_port) \
907 r = tor_addr_port_lookup(host_port_str, &addr, &port); \
908 tt_int_op(r, OP_GE, -1); \
909 tt_int_op(r, OP_LE, 0); \
911 tt_assert(tor_addr_is_null(&addr)); \
912 tt_int_op(port, OP_EQ, 0); \
914 tt_assert(tor_addr_family(&addr) == AF_INET || \
915 tor_addr_family(&addr) == AF_INET6); \
916 tt_int_op(port, OP_EQ, expect_success_port); \
920 /* Test that addr_str successfully parses as a canonical IPv4 address.
921 * Check for successful parsing using:
922 * - tor_addr_parse(),
923 * - tor_addr_port_parse() with a default port,
924 * - tor_lookup_hostname(),
925 * - tor_addr_lookup() with AF_INET,
926 * - tor_addr_lookup() with AF_UNSPEC,
927 * - tor_addr_port_lookup(), with a zero port.
928 * Check for failures using:
929 * - tor_addr_port_parse() without a default port, because there is no port,
930 * - tor_addr_lookup() with AF_INET6,
931 * - tor_addr_port_lookup(), because there is no port.
933 #define TEST_ADDR_V4_PARSE_CANONICAL(addr_str) \
935 TEST_ADDR_PARSE_FMT(addr_str, AF_INET, 0, addr_str); \
936 TEST_ADDR_PORT_PARSE_FMT(addr_str, 111, AF_INET, 0, \
938 TEST_ADDR_V4_LOOKUP_HOSTNAME(addr_str, addr_str); \
939 TEST_ADDR_PORT_LOOKUP_FMT(addr_str, AF_INET, 0, addr_str, 0); \
940 TEST_ADDR_LOOKUP_FMT(addr_str, AF_INET, AF_INET, 0, addr_str); \
941 TEST_ADDR_LOOKUP_FMT(addr_str, AF_UNSPEC, AF_INET, 0, addr_str); \
942 TEST_ADDR_PORT_PARSE_XFAIL(addr_str, -1); \
943 TEST_ADDR_LOOKUP_XFAIL(addr_str, AF_INET6); \
946 /* Test that addr_str successfully parses as a canonical fmt_decorated
948 * Check for successful parsing using:
949 * - tor_addr_parse(),
950 * - tor_addr_port_parse() with a default port,
951 * - tor_addr_lookup() with AF_INET6,
952 * - tor_addr_lookup() with AF_UNSPEC,
953 * - tor_addr_port_lookup(), with a zero port.
954 * Check for failures using:
955 * - tor_addr_port_parse() without a default port, because there is no port,
956 * - tor_lookup_hostname(), because it only supports IPv4,
957 * - tor_addr_lookup() with AF_INET.
959 #define TEST_ADDR_V6_PARSE_CANONICAL(addr_str, fmt_decorated) \
961 TEST_ADDR_PARSE_FMT(addr_str, AF_INET6, fmt_decorated, addr_str); \
962 TEST_ADDR_PORT_PARSE_FMT(addr_str, 222, AF_INET6, fmt_decorated, \
964 TEST_ADDR_LOOKUP_FMT(addr_str, AF_INET6, AF_INET6, fmt_decorated, \
966 TEST_ADDR_LOOKUP_FMT(addr_str, AF_UNSPEC, AF_INET6, fmt_decorated, \
968 TEST_ADDR_PORT_LOOKUP_FMT(addr_str, AF_INET6, fmt_decorated, addr_str, \
970 TEST_ADDR_PORT_PARSE_XFAIL(addr_str, -1); \
971 TEST_ADDR_V4_LOOKUP_XFAIL(addr_str); \
972 TEST_ADDR_LOOKUP_XFAIL(addr_str, AF_INET); \
975 /* Test that addr_str successfully parses, and the fmt_decorated canonical
976 * IPv6 string is expect_str.
977 * Check for successful parsing using:
978 * - tor_addr_parse(),
979 * - tor_addr_port_parse() with a default port,
980 * - tor_addr_lookup() with AF_INET6,
981 * - tor_addr_lookup() with AF_UNSPEC,
982 * - tor_addr_port_lookup(), with a zero port.
983 * Check for failures using:
984 * - tor_addr_port_parse() without a default port, because there is no port.
985 * - tor_lookup_hostname(), because it only supports IPv4,
986 * - tor_addr_lookup() with AF_INET.
988 #define TEST_ADDR_V6_PARSE(addr_str, fmt_decorated, expect_str) \
990 TEST_ADDR_PARSE_FMT(addr_str, AF_INET6, fmt_decorated, expect_str); \
991 TEST_ADDR_PORT_PARSE_FMT(addr_str, 333, AF_INET6, fmt_decorated, \
993 TEST_ADDR_LOOKUP_FMT(addr_str, AF_INET6, AF_INET6, fmt_decorated, \
995 TEST_ADDR_LOOKUP_FMT(addr_str, AF_UNSPEC, AF_INET6, fmt_decorated, \
997 TEST_ADDR_PORT_LOOKUP_FMT(addr_str, AF_INET6, fmt_decorated, expect_str, \
999 TEST_ADDR_PORT_PARSE_XFAIL(addr_str, -1); \
1000 TEST_ADDR_V4_LOOKUP_XFAIL(addr_str); \
1001 TEST_ADDR_LOOKUP_XFAIL(addr_str, AF_INET); \
1004 /* Test that addr_port_str successfully parses to the canonical IPv4 address
1005 * string expect_str, and port expect_port.
1006 * Check for successful parsing using:
1007 * - tor_addr_port_parse() without a default port,
1008 * - tor_addr_port_parse() with a default port,
1009 * - tor_addr_port_lookup().
1010 * Check for failures using:
1011 * - tor_addr_parse(), because there is a port,
1012 * - tor_lookup_hostname(), because there is a port.
1013 * - tor_addr_lookup(), regardless of the address family, because there is a
1016 #define TEST_ADDR_V4_PORT_PARSE(addr_port_str, expect_str, expect_port) \
1018 TEST_ADDR_PORT_PARSE_FMT(addr_port_str, -1, AF_INET, 0, expect_str, \
1020 TEST_ADDR_PORT_PARSE_FMT(addr_port_str, 444, AF_INET, 0, expect_str, \
1022 TEST_ADDR_PORT_LOOKUP_FMT(addr_port_str, AF_INET, 0, expect_str, \
1024 TEST_ADDR_PARSE_XFAIL(addr_port_str); \
1025 TEST_ADDR_V4_LOOKUP_XFAIL(addr_port_str); \
1026 TEST_ADDR_LOOKUP_XFAIL(addr_port_str, AF_INET); \
1027 TEST_ADDR_LOOKUP_XFAIL(addr_port_str, AF_UNSPEC); \
1028 TEST_ADDR_LOOKUP_XFAIL(addr_port_str, AF_INET6); \
1031 /* Test that addr_port_str successfully parses to the canonical undecorated
1032 * IPv6 address string expect_str, and port expect_port.
1033 * Check for successful parsing using:
1034 * - tor_addr_port_parse() without a default port,
1035 * - tor_addr_port_parse() with a default port,
1036 * - tor_addr_port_lookup().
1037 * Check for failures using:
1038 * - tor_addr_parse(), because there is a port,
1039 * - tor_lookup_hostname(), because there is a port, and because it only
1041 * - tor_addr_lookup(), regardless of the address family, because there is a
1044 #define TEST_ADDR_V6_PORT_PARSE(addr_port_str, expect_str, expect_port) \
1046 TEST_ADDR_PORT_PARSE_FMT(addr_port_str, -1, AF_INET6, 0, expect_str, \
1048 TEST_ADDR_PORT_PARSE_FMT(addr_port_str, 555, AF_INET6, 0, expect_str, \
1050 TEST_ADDR_PORT_LOOKUP_FMT(addr_port_str, AF_INET6, 0, expect_str, \
1052 TEST_ADDR_PARSE_XFAIL(addr_port_str); \
1053 TEST_ADDR_V4_LOOKUP_XFAIL(addr_port_str); \
1054 TEST_ADDR_LOOKUP_XFAIL(addr_port_str, AF_INET6); \
1055 TEST_ADDR_LOOKUP_XFAIL(addr_port_str, AF_UNSPEC); \
1056 TEST_ADDR_LOOKUP_XFAIL(addr_port_str, AF_INET); \
1059 /* Test that bad_str fails to parse due to a bad address or port.
1060 * Check for failures using:
1061 * - tor_addr_parse(),
1062 * - tor_addr_port_parse() without a default port,
1063 * - tor_addr_port_parse() with a default port,
1064 * - tor_lookup_hostname(),
1065 * - tor_addr_lookup(), regardless of the address family,
1066 * - tor_addr_port_lookup().
1068 #define TEST_ADDR_PARSE_XFAIL_MALFORMED(bad_str) \
1070 TEST_ADDR_PARSE_XFAIL(bad_str); \
1071 TEST_ADDR_PORT_PARSE_XFAIL(bad_str, -1); \
1072 TEST_ADDR_PORT_PARSE_XFAIL(bad_str, 666); \
1073 TEST_ADDR_V4_LOOKUP_XFAIL(bad_str); \
1074 TEST_ADDR_LOOKUP_XFAIL(bad_str, AF_UNSPEC); \
1075 TEST_ADDR_LOOKUP_XFAIL(bad_str, AF_INET); \
1076 TEST_ADDR_LOOKUP_XFAIL(bad_str, AF_INET6); \
1077 TEST_ADDR_PORT_LOOKUP_XFAIL(bad_str); \
1080 /* Test that host_str is treated as a hostname, and not an address.
1081 * Check for success or failure using the network-dependent functions:
1082 * - tor_lookup_hostname(),
1083 * - tor_addr_lookup(), regardless of the address family,
1084 * - tor_addr_port_lookup(), expecting a zero port.
1085 * Check for failures using:
1086 * - tor_addr_parse(),
1087 * - tor_addr_port_parse() without a default port,
1088 * - tor_addr_port_parse() with a default port.
1090 #define TEST_HOSTNAME(host_str) \
1092 TEST_HOST_V4_LOOKUP(host_str); \
1093 TEST_HOST_LOOKUP(host_str, AF_UNSPEC); \
1094 TEST_HOST_LOOKUP(host_str, AF_INET); \
1095 TEST_HOST_LOOKUP(host_str, AF_INET6); \
1096 TEST_HOST_PORT_LOOKUP(host_str, 0); \
1097 TEST_ADDR_PARSE_XFAIL(host_str); \
1098 TEST_ADDR_PORT_PARSE_XFAIL(host_str, -1); \
1099 TEST_ADDR_PORT_PARSE_XFAIL(host_str, 777); \
1102 /* Test that host_port_str is treated as a hostname and port, and not a
1103 * hostname or an address.
1104 * Check for success or failure using the network-dependent function:
1105 * - tor_addr_port_lookup(), expecting expect_success_port if the lookup is
1107 * Check for failures using:
1108 * - tor_addr_parse(),
1109 * - tor_addr_port_parse() without a default port,
1110 * - tor_addr_port_parse() with a default port,
1111 * - tor_lookup_hostname(), because it doesn't support ports,
1112 * - tor_addr_lookup(), regardless of the address family, because it doesn't
1115 #define TEST_HOSTNAME_PORT(host_port_str, expect_success_port) \
1117 TEST_HOST_PORT_LOOKUP(host_port_str, expect_success_port); \
1118 TEST_ADDR_PARSE_XFAIL(host_port_str); \
1119 TEST_ADDR_PORT_PARSE_XFAIL(host_port_str, -1); \
1120 TEST_ADDR_PORT_PARSE_XFAIL(host_port_str, 888); \
1121 TEST_ADDR_V4_LOOKUP_XFAIL(host_port_str); \
1122 TEST_ADDR_LOOKUP_XFAIL(host_port_str, AF_UNSPEC); \
1123 TEST_ADDR_LOOKUP_XFAIL(host_port_str, AF_INET); \
1124 TEST_ADDR_LOOKUP_XFAIL(host_port_str, AF_INET6); \
1128 test_addr_parse_canonical(void *arg
)
1135 char buf
[TOR_ADDR_BUF_LEN
];
1139 /* Correct calls. */
1140 TEST_ADDR_V4_PARSE_CANONICAL("192.0.2.1");
1141 TEST_ADDR_V4_PARSE_CANONICAL("192.0.2.2");
1143 TEST_ADDR_V6_PARSE_CANONICAL("[11:22::33:44]", 1);
1144 TEST_ADDR_V6_PARSE_CANONICAL("[::1]", 1);
1145 TEST_ADDR_V6_PARSE_CANONICAL("[::]", 1);
1146 TEST_ADDR_V6_PARSE_CANONICAL("[2::]", 1);
1147 TEST_ADDR_V6_PARSE_CANONICAL("[11:22:33:44:55:66:77:88]", 1);
1149 /* Allow IPv6 without square brackets, when there is no port, but only if
1150 * there is a default port */
1151 TEST_ADDR_V6_PARSE_CANONICAL("11:22::33:44", 0);
1152 TEST_ADDR_V6_PARSE_CANONICAL("::1", 0);
1153 TEST_ADDR_V6_PARSE_CANONICAL("::", 0);
1154 TEST_ADDR_V6_PARSE_CANONICAL("2::", 0);
1155 TEST_ADDR_V6_PARSE_CANONICAL("11:22:33:44:55:66:77:88", 0);
1160 /** Test tor_addr_parse() and tor_addr_port_parse(). */
1162 test_addr_parse(void *arg
)
1170 char buf
[TOR_ADDR_BUF_LEN
];
1174 mock_hostname_resolver();
1176 /* IPv6-mapped IPv4 addresses. Tor doesn't really use these. */
1177 TEST_ADDR_V6_PARSE("11:22:33:44:55:66:1.2.3.4", 0,
1178 "11:22:33:44:55:66:102:304");
1180 TEST_ADDR_V6_PARSE("11:22::33:44:1.2.3.4", 0,
1181 "11:22::33:44:102:304");
1184 TEST_ADDR_V4_PORT_PARSE("192.0.2.1:1234", "192.0.2.1", 1234);
1185 TEST_ADDR_V6_PORT_PARSE("[::1]:1234", "::1", 1234);
1188 TEST_HOSTNAME("localhost");
1189 TEST_HOSTNAME_PORT("localhost:1234", 1234);
1190 TEST_HOSTNAME_PORT("localhost:0", 0);
1192 TEST_HOSTNAME("torproject.org");
1193 TEST_HOSTNAME_PORT("torproject.org:56", 56);
1195 TEST_HOSTNAME("probably-not-a-valid-dns.name-tld");
1196 TEST_HOSTNAME_PORT("probably-not-a-valid-dns.name-tld:789", 789);
1198 /* Malformed addresses. */
1200 TEST_ADDR_PARSE_XFAIL_MALFORMED("");
1202 /* Square brackets around IPv4 address. */
1203 TEST_ADDR_PARSE_XFAIL_MALFORMED("[192.0.2.1]");
1204 TEST_ADDR_PARSE_XFAIL_MALFORMED("[192.0.2.3]:12345");
1206 /* Only left square bracket. */
1207 TEST_ADDR_PARSE_XFAIL_MALFORMED("[11:22::33:44");
1209 /* Only right square bracket. */
1210 TEST_ADDR_PARSE_XFAIL_MALFORMED("11:22::33:44]");
1212 /* Leading colon. */
1213 TEST_ADDR_PARSE_XFAIL_MALFORMED(":11:22::33:44");
1215 /* Trailing colon. */
1216 TEST_ADDR_PARSE_XFAIL_MALFORMED("11:22::33:44:");
1217 TEST_ADDR_PARSE_XFAIL_MALFORMED("[::1]:");
1218 TEST_ADDR_PARSE_XFAIL_MALFORMED("localhost:");
1221 TEST_ADDR_PARSE_XFAIL_MALFORMED("192.0.2.2:66666");
1222 TEST_ADDR_PARSE_XFAIL_MALFORMED("[::1]:77777");
1223 TEST_ADDR_PARSE_XFAIL_MALFORMED("::1:88888");
1224 TEST_ADDR_PARSE_XFAIL_MALFORMED("localhost:99999");
1226 TEST_ADDR_PARSE_XFAIL_MALFORMED("192.0.2.2:-1");
1227 TEST_ADDR_PARSE_XFAIL_MALFORMED("[::1]:-2");
1228 TEST_ADDR_PARSE_XFAIL_MALFORMED("::1:-3");
1229 TEST_ADDR_PARSE_XFAIL_MALFORMED("localhost:-4");
1231 TEST_ADDR_PARSE_XFAIL_MALFORMED("192.0.2.2:1 bad");
1232 TEST_ADDR_PARSE_XFAIL_MALFORMED("192.0.2.2:bad-port");
1233 TEST_ADDR_PARSE_XFAIL_MALFORMED("[::1]:bad-port-1");
1234 TEST_ADDR_PARSE_XFAIL_MALFORMED("::1:1-bad-port");
1235 TEST_ADDR_PARSE_XFAIL_MALFORMED("localhost:1-bad-port");
1236 TEST_ADDR_PARSE_XFAIL_MALFORMED("localhost:1-bad-port-1");
1239 TEST_ADDR_PARSE_XFAIL_MALFORMED("definitely invalid");
1240 TEST_ADDR_PARSE_XFAIL_MALFORMED("definitely invalid:22222");
1242 /* Ambiguous cases */
1243 /* Too many hex words in IPv4-mapped IPv6 address.
1244 * But some OS host lookup routines accept it as a hostname, or
1245 * as an IP address?? (I assume they discard unused characters). */
1246 TEST_HOSTNAME("11:22:33:44:55:66:77:88:1.2.3.4");
1248 /* IPv6 address with port and no brackets
1249 * We reject it, but some OS host lookup routines accept it as an
1250 * IPv6 address:port ? */
1251 TEST_HOSTNAME_PORT("11:22::33:44:12345", 12345);
1252 /* Is it a port, or are there too many hex words?
1253 * We reject it either way, but some OS host lookup routines accept it as an
1254 * IPv6 address:port */
1255 TEST_HOSTNAME_PORT("11:22:33:44:55:66:77:88:99", 99);
1256 /* But we accept it if it has square brackets. */
1257 TEST_ADDR_V6_PORT_PARSE("[11:22:33:44:55:66:77:88]:99",
1258 "11:22:33:44:55:66:77:88",99);
1261 * We reject it, but some OS host lookup routines accept it as an
1262 * IPv4 address[:port], with a zero last octet */
1263 TEST_HOSTNAME("192.0.1");
1264 TEST_HOSTNAME_PORT("192.0.2:1234", 1234);
1266 /* More bad IPv6 addresses and ports: no brackets
1267 * We reject it, but some OS host lookup routines accept it as an
1268 * IPv6 address[:port] */
1269 TEST_HOSTNAME_PORT("::1:12345", 12345);
1270 TEST_HOSTNAME_PORT("11:22::33:44:12345", 12345);
1272 /* And this is an ambiguous case, which is interpreted as an IPv6 address. */
1273 TEST_ADDR_V6_PARSE_CANONICAL("11:22::88:99", 0);
1274 /* Use square brackets to resolve the ambiguity */
1275 TEST_ADDR_V6_PARSE_CANONICAL("[11:22::88:99]", 1);
1276 TEST_ADDR_V6_PORT_PARSE("[11:22::88]:99",
1280 unmock_hostname_resolver();
1284 update_difference(int ipv6
, uint8_t *d
,
1285 const tor_addr_t
*a
, const tor_addr_t
*b
)
1287 const int n_bytes
= ipv6
? 16 : 4;
1288 uint8_t a_tmp
[4], b_tmp
[4];
1289 const uint8_t *ba
, *bb
;
1293 ba
= tor_addr_to_in6_addr8(a
);
1294 bb
= tor_addr_to_in6_addr8(b
);
1296 set_uint32(a_tmp
, tor_addr_to_ipv4n(a
));
1297 set_uint32(b_tmp
, tor_addr_to_ipv4n(b
));
1298 ba
= a_tmp
; bb
= b_tmp
;
1301 for (i
= 0; i
< n_bytes
; ++i
) {
1302 d
[i
] |= ba
[i
] ^ bb
[i
];
1307 test_virtaddrmap(void *data
)
1309 /* Let's start with a bunch of random addresses. */
1310 int ipv6
, bits
, iter
, b
;
1311 virtual_addr_conf_t cfg
[2];
1316 tor_addr_parse(&cfg
[0].addr
, "64.65.0.0");
1317 tor_addr_parse(&cfg
[1].addr
, "3491:c0c0::");
1319 for (ipv6
= 0; ipv6
<= 1; ++ipv6
) {
1320 for (bits
= 0; bits
< 18; ++bits
) {
1322 cfg
[ipv6
].bits
= bits
;
1323 memset(bytes
, 0, sizeof(bytes
));
1324 tor_addr_copy(&last_a
, &cfg
[ipv6
].addr
);
1325 /* Generate 128 addresses with each addr/bits combination. */
1326 for (iter
= 0; iter
< 128; ++iter
) {
1329 get_random_virtual_addr(&cfg
[ipv6
], &a
);
1330 //printf("%s\n", fmt_addr(&a));
1331 /* Make sure that the first b bits match the configured network */
1332 tt_int_op(0, OP_EQ
, tor_addr_compare_masked(&a
, &cfg
[ipv6
].addr
,
1335 /* And track which bits have been different between pairs of
1337 update_difference(ipv6
, bytes
, &last_a
, &a
);
1340 /* Now make sure all but the first 'bits' bits of bytes are true */
1341 for (b
= bits
+1; b
< (ipv6
?128:32); ++b
) {
1342 tt_assert(1 & (bytes
[b
/8] >> (7-(b
&7))));
1352 test_virtaddrmap_persist(void *data
)
1355 const char *a
, *b
, *c
;
1358 const char *canned_data
;
1359 size_t canned_data_len
;
1364 a
= addressmap_register_virtual_address(RESOLVED_TYPE_HOSTNAME
,
1365 tor_strdup("foobar.baz"));
1367 tt_assert(!strcmpend(a
, ".virtual"));
1369 // mock crypto_rand to repeat the same result twice; make sure we get
1370 // different outcomes. (Because even though the odds for receiving the
1371 // same 80-bit address twice is only 1/2^40, it could still happen for
1372 // some user -- but running our test through 2^40 iterations isn't
1374 canned_data
= "1234567890" // the first call returns this.
1375 "1234567890" // the second call returns this.
1376 "abcdefghij"; // the third call returns this.
1377 canned_data_len
= 30;
1378 testing_enable_prefilled_rng(canned_data
, canned_data_len
);
1380 a
= addressmap_register_virtual_address(RESOLVED_TYPE_HOSTNAME
,
1381 tor_strdup("quuxit.baz"));
1382 b
= addressmap_register_virtual_address(RESOLVED_TYPE_HOSTNAME
,
1383 tor_strdup("nescio.baz"));
1386 tt_str_op(a
, OP_EQ
, "gezdgnbvgy3tqojq.virtual");
1387 tt_str_op(b
, OP_EQ
, "mfrggzdfmztwq2lk.virtual");
1388 testing_disable_prefilled_rng();
1390 // Now try something to get us an ipv4 address
1391 tt_int_op(0,OP_EQ
, parse_virtual_addr_network("192.168.0.0/16",
1393 a
= addressmap_register_virtual_address(RESOLVED_TYPE_IPV4
,
1394 tor_strdup("foobar.baz"));
1396 tt_assert(!strcmpstart(a
, "192.168."));
1397 tor_addr_parse(&addr
, a
);
1398 tt_int_op(AF_INET
, OP_EQ
, tor_addr_family(&addr
));
1400 b
= addressmap_register_virtual_address(RESOLVED_TYPE_IPV4
,
1401 tor_strdup("quuxit.baz"));
1402 tt_str_op(b
, OP_NE
, a
);
1403 tt_assert(!strcmpstart(b
, "192.168."));
1405 // Try some canned entropy and verify all the we discard duplicates,
1406 // addresses that end with 0, and addresses that end with 255.
1407 canned_data
= "\x01\x02\x03\x04" // okay
1408 "\x01\x02\x03\x04" // duplicate
1409 "\x03\x04\x00\x00" // bad ending 1
1410 "\x05\x05\x00\xff" // bad ending 2
1411 "\x05\x06\x07\xf0"; // okay
1412 canned_data_len
= 20;
1413 testing_enable_prefilled_rng(canned_data
, canned_data_len
);
1415 a
= addressmap_register_virtual_address(RESOLVED_TYPE_IPV4
,
1416 tor_strdup("wumble.onion"));
1417 b
= addressmap_register_virtual_address(RESOLVED_TYPE_IPV4
,
1418 tor_strdup("wumpus.onion"));
1419 tt_str_op(a
, OP_EQ
, "192.168.3.4");
1420 tt_str_op(b
, OP_EQ
, "192.168.7.240");
1421 testing_disable_prefilled_rng();
1424 tt_int_op(0,OP_EQ
, parse_virtual_addr_network("1010:F000::/20",
1425 AF_INET6
, 0, NULL
));
1426 a
= addressmap_register_virtual_address(RESOLVED_TYPE_IPV6
,
1427 tor_strdup("foobar.baz"));
1429 tt_assert(!strcmpstart(a
, "[1010:f"));
1430 tor_addr_parse(&addr
, a
);
1431 tt_int_op(AF_INET6
, OP_EQ
, tor_addr_family(&addr
));
1433 b
= addressmap_register_virtual_address(RESOLVED_TYPE_IPV6
,
1434 tor_strdup("quuxit.baz"));
1435 tt_str_op(b
, OP_NE
, a
);
1436 tt_assert(!strcmpstart(b
, "[1010:f"));
1438 // Try IPv6 with canned entropy, to make sure we detect duplicates.
1440 canned_data
= "acanthopterygian" // okay
1441 "cinematographist" // okay
1442 "acanthopterygian" // duplicate
1443 "acanthopterygian" // duplicate
1444 "acanthopterygian" // duplicate
1445 "cinematographist" // duplicate
1446 "coadministration"; // okay
1447 canned_data_len
= 16 * 7;
1448 testing_enable_prefilled_rng(canned_data
, canned_data_len
);
1450 a
= addressmap_register_virtual_address(RESOLVED_TYPE_IPV6
,
1451 tor_strdup("wuffle.baz"));
1452 b
= addressmap_register_virtual_address(RESOLVED_TYPE_IPV6
,
1453 tor_strdup("gribble.baz"));
1454 c
= addressmap_register_virtual_address(RESOLVED_TYPE_IPV6
,
1455 tor_strdup("surprisingly-legible.baz"));
1456 tt_str_op(a
, OP_EQ
, "[1010:f16e:7468:6f70:7465:7279:6769:616e]");
1457 tt_str_op(b
, OP_EQ
, "[1010:fe65:6d61:746f:6772:6170:6869:7374]");
1458 tt_str_op(c
, OP_EQ
, "[1010:f164:6d69:6e69:7374:7261:7469:6f6e]");
1460 // Try address exhaustion: make sure we can actually fail if we
1461 // get too many already-existing addresses.
1462 testing_disable_prefilled_rng();
1463 canned_data_len
= 128*1024;
1464 canned_data
= ones
= tor_malloc(canned_data_len
);
1465 memset(ones
, 1, canned_data_len
);
1466 testing_enable_prefilled_rng(canned_data
, canned_data_len
);
1467 // There is some chance this one will fail if a previous random
1468 // allocation gave out the address already.
1469 a
= addressmap_register_virtual_address(RESOLVED_TYPE_IPV4
,
1470 tor_strdup("might-work.onion"));
1472 tt_str_op(a
, OP_EQ
, "192.168.1.1");
1474 setup_capture_of_logs(LOG_WARN
);
1475 // This one will definitely fail, since we've set up the RNG to hand
1477 b
= addressmap_register_virtual_address(RESOLVED_TYPE_IPV4
,
1478 tor_strdup("wont-work.onion"));
1479 tt_assert(b
== NULL
);
1480 expect_single_log_msg_containing("Ran out of virtual addresses!");
1483 testing_disable_prefilled_rng();
1485 addressmap_free_all();
1486 teardown_capture_of_logs();
1490 test_addr_localname(void *arg
)
1493 tt_assert(tor_addr_hostname_is_local("localhost"));
1494 tt_assert(tor_addr_hostname_is_local("LOCALHOST"));
1495 tt_assert(tor_addr_hostname_is_local("LocalHost"));
1496 tt_assert(tor_addr_hostname_is_local("local"));
1497 tt_assert(tor_addr_hostname_is_local("LOCAL"));
1498 tt_assert(tor_addr_hostname_is_local("here.now.local"));
1499 tt_assert(tor_addr_hostname_is_local("here.now.LOCAL"));
1501 tt_assert(!tor_addr_hostname_is_local(" localhost"));
1502 tt_assert(!tor_addr_hostname_is_local("www.torproject.org"));
1508 test_addr_dup_ip(void *arg
)
1512 #define CHECK(ip, s) do { \
1513 v = tor_dup_ip(ip); \
1514 tt_str_op(v,OP_EQ,(s)); \
1518 CHECK(0xffffffff, "255.255.255.255");
1519 CHECK(0x00000000, "0.0.0.0");
1520 CHECK(0x7f000001, "127.0.0.1");
1521 CHECK(0x01020304, "1.2.3.4");
1529 test_addr_sockaddr_to_str(void *arg
)
1532 struct sockaddr_in sin
;
1533 struct sockaddr_in6 sin6
;
1534 struct sockaddr_storage ss
;
1535 #ifdef HAVE_SYS_UN_H
1536 struct sockaddr_un s_un
;
1538 #define CHECK(sa, s) do { \
1539 v = tor_sockaddr_to_str((const struct sockaddr*) &(sa)); \
1540 tt_str_op(v,OP_EQ,(s)); \
1545 memset(&ss
,0,sizeof(ss
));
1546 ss
.ss_family
= AF_UNSPEC
;
1547 CHECK(ss
, "unspec");
1549 memset(&sin
,0,sizeof(sin
));
1550 sin
.sin_family
= AF_INET
;
1551 sin
.sin_addr
.s_addr
= htonl(0x7f808001);
1552 sin
.sin_port
= htons(1234);
1553 CHECK(sin
, "127.128.128.1:1234");
1555 #ifdef HAVE_SYS_UN_H
1556 memset(&s_un
,0,sizeof(s_un
));
1557 s_un
.sun_family
= AF_UNIX
;
1558 strlcpy(s_un
.sun_path
, "/here/is/a/path", sizeof(s_un
.sun_path
));
1559 CHECK(s_un
, "unix:/here/is/a/path");
1560 #endif /* defined(HAVE_SYS_UN_H) */
1562 memset(&sin6
,0,sizeof(sin6
));
1563 sin6
.sin6_family
= AF_INET6
;
1564 memcpy(sin6
.sin6_addr
.s6_addr
, "\x20\x00\x00\x00\x00\x00\x00\x00"
1565 "\x00\x1a\x2b\x3c\x4d\x5e\x00\x01", 16);
1566 sin6
.sin6_port
= htons(1234);
1567 CHECK(sin6
, "[2000::1a:2b3c:4d5e:1]:1234");
1574 test_addr_is_loopback(void *data
)
1576 static const struct loopback_item
{
1579 } loopback_items
[] = {
1582 { "127.99.100.101", 1 },
1583 { "128.99.100.101", 0 },
1596 for (i
=0; loopback_items
[i
].name
; ++i
) {
1597 tt_int_op(tor_addr_parse(&addr
, loopback_items
[i
].name
), OP_GE
, 0);
1598 tt_int_op(tor_addr_is_loopback(&addr
), OP_EQ
,
1599 loopback_items
[i
].is_loopback
);
1602 tor_addr_make_unspec(&addr
);
1603 tt_int_op(tor_addr_is_loopback(&addr
), OP_EQ
, 0);
1610 test_addr_make_null(void *data
)
1612 tor_addr_t
*addr
= tor_malloc(sizeof(*addr
));
1613 tor_addr_t
*zeros
= tor_malloc_zero(sizeof(*addr
));
1614 char buf
[TOR_ADDR_BUF_LEN
];
1616 /* Ensure that before tor_addr_make_null, addr != 0's */
1617 memset(addr
, 1, sizeof(*addr
));
1618 tt_int_op(fast_memcmp(addr
, zeros
, sizeof(*addr
)), OP_NE
, 0);
1619 /* Test with AF == AF_INET */
1620 zeros
->family
= AF_INET
;
1621 tor_addr_make_null(addr
, AF_INET
);
1622 tt_int_op(fast_memcmp(addr
, zeros
, sizeof(*addr
)), OP_EQ
, 0);
1623 tt_str_op(tor_addr_to_str(buf
, addr
, sizeof(buf
), 0), OP_EQ
, "0.0.0.0");
1624 /* Test with AF == AF_INET6 */
1625 memset(addr
, 1, sizeof(*addr
));
1626 zeros
->family
= AF_INET6
;
1627 tor_addr_make_null(addr
, AF_INET6
);
1628 tt_int_op(fast_memcmp(addr
, zeros
, sizeof(*addr
)), OP_EQ
, 0);
1629 tt_str_op(tor_addr_to_str(buf
, addr
, sizeof(buf
), 0), OP_EQ
, "::");
1635 #define TEST_ADDR_INTERNAL(a, for_listening, rv) STMT_BEGIN \
1637 tt_int_op(tor_inet_pton(AF_INET, a, &t.addr.in_addr), OP_EQ, 1); \
1638 t.family = AF_INET; \
1639 tt_int_op(tor_addr_is_internal(&t, for_listening), OP_EQ, rv); \
1643 test_addr_rfc6598(void *arg
)
1646 TEST_ADDR_INTERNAL("100.64.0.1", 0, 1);
1647 TEST_ADDR_INTERNAL("100.64.0.1", 1, 0);
1652 #define TEST_ADDR_ATON(a, rv) STMT_BEGIN \
1653 struct in_addr addr; \
1654 tt_int_op(tor_inet_aton(a, &addr), OP_EQ, rv); \
1658 test_addr_octal(void *arg
)
1662 /* Test non-octal IP addresses. */
1663 TEST_ADDR_ATON("0.1.2.3", 1);
1664 TEST_ADDR_ATON("1.0.2.3", 1);
1665 TEST_ADDR_ATON("1.2.3.0", 1);
1667 /* Test octal IP addresses. */
1668 TEST_ADDR_ATON("01.1.2.3", 0);
1669 TEST_ADDR_ATON("1.02.3.4", 0);
1670 TEST_ADDR_ATON("1.2.3.04", 0);
1675 #define get_ipv4(test_addr, str, iprv) STMT_BEGIN \
1676 test_addr = tor_malloc(sizeof(tor_addr_t)); \
1677 test_addr->family = AF_INET; \
1678 iprv = tor_inet_aton(str, &test_addr->addr.in_addr); \
1682 #define get_ipv6(test_addr, str, iprv) STMT_BEGIN \
1683 test_addr = tor_malloc(sizeof(tor_addr_t)); \
1684 test_addr->family = AF_INET6; \
1685 iprv = tor_inet_pton(AF_INET6, str, &test_addr->addr.in6_addr); \
1689 #define get_af_unix(test_addr) STMT_BEGIN \
1690 test_addr = tor_malloc_zero(sizeof(tor_addr_t)); \
1691 test_addr->family = AF_UNIX; \
1694 #define get_af_unspec(test_addr) STMT_BEGIN \
1695 test_addr = tor_malloc_zero(sizeof(tor_addr_t)); \
1696 test_addr->family = AF_UNSPEC; \
1699 #define TEST_ADDR_VALIDITY(a, lis, rv) STMT_BEGIN \
1701 tt_int_op(tor_addr_is_valid(a, lis), OP_EQ, rv); \
1704 /* Here we can change the addresses we are testing for. */
1705 #define IP4_TEST_ADDR "123.98.45.1"
1706 #define IP6_TEST_ADDR "2001:0DB8:AC10:FE01::"
1709 test_addr_is_valid(void *arg
)
1712 tor_addr_t
*test_addr
;
1715 /* Tests for IPv4 addresses. */
1717 /* Test for null IPv4 address. */
1718 get_ipv4(test_addr
, "0.0.0.0", iprv
);
1719 TEST_ADDR_VALIDITY(test_addr
, 0, 0);
1720 TEST_ADDR_VALIDITY(test_addr
, 1, 1);
1721 tor_free(test_addr
);
1723 /* Test for non-null IPv4 address. */
1724 get_ipv4(test_addr
, IP4_TEST_ADDR
, iprv
);
1725 TEST_ADDR_VALIDITY(test_addr
, 0, 1);
1726 TEST_ADDR_VALIDITY(test_addr
, 1, 1);
1727 tor_free(test_addr
);
1729 /* Tests for IPv6 addresses. */
1731 /* Test for null IPv6 address. */
1732 get_ipv6(test_addr
, "::", iprv
);
1733 TEST_ADDR_VALIDITY(test_addr
, 0, 0);
1734 TEST_ADDR_VALIDITY(test_addr
, 1, 1);
1735 tor_free(test_addr
);
1737 /* Test for non-null IPv6 address. */
1738 get_ipv6(test_addr
, IP6_TEST_ADDR
, iprv
);
1739 TEST_ADDR_VALIDITY(test_addr
, 0, 1);
1740 TEST_ADDR_VALIDITY(test_addr
, 1, 1);
1741 tor_free(test_addr
);
1743 /* Test for address of type AF_UNIX. */
1745 get_af_unix(test_addr
);
1746 TEST_ADDR_VALIDITY(test_addr
, 0, 0);
1747 TEST_ADDR_VALIDITY(test_addr
, 1, 0);
1748 tor_free(test_addr
);
1750 /* Test for address of type AF_UNSPEC. */
1752 get_af_unspec(test_addr
);
1753 TEST_ADDR_VALIDITY(test_addr
, 0, 0);
1754 TEST_ADDR_VALIDITY(test_addr
, 1, 0);
1757 tor_free(test_addr
);
1760 #define TEST_ADDR_IS_NULL(a, rv) STMT_BEGIN \
1762 tt_int_op(tor_addr_is_null(a), OP_EQ, rv); \
1766 test_addr_is_null(void *arg
)
1769 tor_addr_t
*test_addr
;
1772 /* Test for null IPv4. */
1773 get_ipv4(test_addr
, "0.0.0.0", iprv
);
1774 TEST_ADDR_IS_NULL(test_addr
, 1);
1775 tor_free(test_addr
);
1777 /* Test for non-null IPv4. */
1778 get_ipv4(test_addr
, IP4_TEST_ADDR
, iprv
);
1779 TEST_ADDR_IS_NULL(test_addr
, 0);
1780 tor_free(test_addr
);
1782 /* Test for null IPv6. */
1783 get_ipv6(test_addr
, "::", iprv
);
1784 TEST_ADDR_IS_NULL(test_addr
, 1);
1785 tor_free(test_addr
);
1787 /* Test for non-null IPv6. */
1788 get_ipv6(test_addr
, IP6_TEST_ADDR
, iprv
);
1789 TEST_ADDR_IS_NULL(test_addr
, 0);
1790 tor_free(test_addr
);
1792 /* Test for address family AF_UNIX. */
1793 get_af_unix(test_addr
);
1794 TEST_ADDR_IS_NULL(test_addr
, 1);
1795 tor_free(test_addr
);
1797 /* Test for address family AF_UNSPEC. */
1798 get_af_unspec(test_addr
);
1799 TEST_ADDR_IS_NULL(test_addr
, 1);
1802 tor_free(test_addr
);
1806 #define ADDR_LEGACY(name) \
1807 { #name, test_addr_ ## name , 0, NULL, NULL }
1810 struct testcase_t addr_tests
[] = {
1812 ADDR_LEGACY(ip6_helpers
),
1814 ADDR_LEGACY(parse_canonical
),
1815 { "virtaddr", test_virtaddrmap
, 0, NULL
, NULL
},
1816 { "virtaddr_persist", test_virtaddrmap_persist
, TT_FORK
, NULL
, NULL
},
1817 { "localname", test_addr_localname
, 0, NULL
, NULL
},
1818 { "dup_ip", test_addr_dup_ip
, 0, NULL
, NULL
},
1819 { "sockaddr_to_str", test_addr_sockaddr_to_str
, 0, NULL
, NULL
},
1820 { "is_loopback", test_addr_is_loopback
, 0, NULL
, NULL
},
1821 { "make_null", test_addr_make_null
, 0, NULL
, NULL
},
1822 { "rfc6598", test_addr_rfc6598
, 0, NULL
, NULL
},
1823 { "octal", test_addr_octal
, 0, NULL
, NULL
},
1824 { "address_validity", test_addr_is_valid
, 0, NULL
, NULL
},
1825 { "address_is_null", test_addr_is_null
, 0, NULL
, NULL
},