Line data Source code
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 */
5 :
6 : #define ADDRESSMAP_PRIVATE
7 : #include "orconfig.h"
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"
16 :
17 : #ifdef HAVE_SYS_UN_H
18 : #include <sys/un.h>
19 : #endif
20 :
21 : static void
22 1 : test_addr_basic(void *arg)
23 : {
24 1 : (void) arg;
25 :
26 1 : tt_int_op(0,OP_EQ, addr_mask_get_bits(0x0u));
27 1 : tt_int_op(32,OP_EQ, addr_mask_get_bits(0xFFFFFFFFu));
28 1 : tt_int_op(16,OP_EQ, addr_mask_get_bits(0xFFFF0000u));
29 1 : tt_int_op(31,OP_EQ, addr_mask_get_bits(0xFFFFFFFEu));
30 1 : tt_int_op(1,OP_EQ, addr_mask_get_bits(0x80000000u));
31 :
32 : /* Test inet_ntop */
33 : {
34 1 : char tmpbuf[TOR_ADDR_BUF_LEN];
35 1 : const char *ip = "176.192.208.224";
36 1 : struct in_addr in;
37 :
38 : /* good round trip */
39 1 : tt_int_op(tor_inet_pton(AF_INET, ip, &in), OP_EQ, 1);
40 1 : tt_ptr_op(tor_inet_ntop(AF_INET, &in, tmpbuf, sizeof(tmpbuf)),
41 : OP_EQ, &tmpbuf);
42 1 : tt_str_op(tmpbuf,OP_EQ, ip);
43 :
44 : /* just enough buffer length */
45 1 : tt_str_op(tor_inet_ntop(AF_INET, &in, tmpbuf, strlen(ip) + 1), OP_EQ, ip);
46 :
47 : /* too short buffer */
48 1 : tt_ptr_op(tor_inet_ntop(AF_INET, &in, tmpbuf, strlen(ip)),OP_EQ, NULL);
49 : }
50 :
51 1 : done:
52 1 : ;
53 1 : }
54 :
55 : #ifndef COCCI
56 : #define test_op_ip6_(a,op,b,e1,e2) \
57 : STMT_BEGIN \
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), \
60 : char *, "%s", \
61 : { char *cp; \
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_]); \
65 : cp += 2; \
66 : if (ii_ != 15) *cp++ = ':'; \
67 : } \
68 : }, \
69 : { tor_free(print_); }, \
70 : TT_EXIT_TEST_FUNCTION \
71 : ); \
72 : STMT_END
73 : #endif /* !defined(COCCI) */
74 :
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); \
81 : STMT_END
82 :
83 : /** Helper: Assert that <b>a</b> is recognized as a bad IPv6 address by
84 : * tor_inet_pton(). */
85 : #define test_pton6_bad(a) \
86 : tt_int_op(0, OP_EQ, tor_inet_pton(AF_INET6, a, &a1))
87 :
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); \
96 : STMT_END
97 :
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)); \
105 : STMT_END
106 :
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)); \
114 : STMT_END
115 :
116 : #ifndef COCCI
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); \
125 : if (!(r op 0)) \
126 : TT_DIE(("Failed: tor_addr_compare(%s,%s) %s 0", a, b, #op));\
127 : STMT_END
128 :
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); \
137 : if (!(r op 0)) \
138 : TT_DIE(("Failed: tor_addr_compare_masked(%s,%s,%d) %s 0", \
139 : a, b, m, #op)); \
140 : STMT_END
141 : #endif /* !defined(COCCI) */
142 :
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) \
148 : STMT_BEGIN \
149 : tt_int_op(tor_addr_parse_mask_ports(xx, 0, &t1, &mask, &port1, &port2), \
150 : OP_EQ, f); \
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); \
159 : STMT_END
160 :
161 : /** Run unit tests for IPv6 encoding/decoding/manipulation functions. */
162 : static void
163 1 : test_addr_ip6_helpers(void *arg)
164 : {
165 1 : char buf[TOR_ADDR_BUF_LEN], bug[TOR_ADDR_BUF_LEN];
166 1 : char rbuf[REVERSE_LOOKUP_NAME_BUF_LEN];
167 1 : struct in6_addr a1, a2;
168 1 : tor_addr_t t1, t2;
169 1 : int r, i;
170 1 : uint16_t port1, port2;
171 1 : maskbits_t mask;
172 1 : const char *p1;
173 1 : struct sockaddr_storage sa_storage;
174 1 : struct sockaddr_in *sin;
175 1 : struct sockaddr_in6 *sin6;
176 :
177 : /* Test tor_inet_ntop and tor_inet_pton: IPv6 */
178 1 : (void)arg;
179 : {
180 1 : const char *ip = "2001::1234";
181 1 : const char *ip_ffff = "::ffff:192.168.1.2";
182 :
183 : /* good round trip */
184 1 : tt_int_op(tor_inet_pton(AF_INET6, ip, &a1),OP_EQ, 1);
185 1 : tt_ptr_op(tor_inet_ntop(AF_INET6, &a1, buf, sizeof(buf)),OP_EQ, &buf);
186 1 : tt_str_op(buf,OP_EQ, ip);
187 :
188 : /* good round trip - ::ffff:0:0 style */
189 1 : tt_int_op(tor_inet_pton(AF_INET6, ip_ffff, &a2),OP_EQ, 1);
190 1 : tt_ptr_op(tor_inet_ntop(AF_INET6, &a2, buf, sizeof(buf)),OP_EQ, &buf);
191 1 : tt_str_op(buf,OP_EQ, ip_ffff);
192 :
193 : /* just long enough buffer (remember \0) */
194 1 : tt_str_op(tor_inet_ntop(AF_INET6, &a1, buf, strlen(ip)+1),OP_EQ, ip);
195 1 : tt_str_op(tor_inet_ntop(AF_INET6, &a2, buf, strlen(ip_ffff)+1),OP_EQ,
196 : ip_ffff);
197 :
198 : /* too short buffer (remember \0) */
199 1 : tt_ptr_op(tor_inet_ntop(AF_INET6, &a1, buf, strlen(ip)),OP_EQ, NULL);
200 1 : tt_ptr_op(tor_inet_ntop(AF_INET6, &a2, buf, strlen(ip_ffff)),OP_EQ, NULL);
201 : }
202 :
203 : /* ==== Converting to and from sockaddr_t. */
204 1 : sin = (struct sockaddr_in *)&sa_storage;
205 1 : sin->sin_family = AF_INET;
206 1 : sin->sin_port = htons(9090);
207 1 : sin->sin_addr.s_addr = htonl(0x7f7f0102); /*127.127.1.2*/
208 1 : tor_addr_from_sockaddr(&t1, (struct sockaddr *)sin, &port1);
209 1 : tt_int_op(tor_addr_family(&t1),OP_EQ, AF_INET);
210 1 : tt_int_op(tor_addr_to_ipv4h(&t1),OP_EQ, 0x7f7f0102);
211 1 : tt_int_op(port1, OP_EQ, 9090);
212 :
213 1 : memset(&sa_storage, 0, sizeof(sa_storage));
214 1 : 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 1 : tt_int_op(1234,OP_EQ, ntohs(sin->sin_port));
218 1 : tt_int_op(0x7f7f0102,OP_EQ, ntohl(sin->sin_addr.s_addr));
219 :
220 1 : memset(&sa_storage, 0, sizeof(sa_storage));
221 1 : sin6 = (struct sockaddr_in6 *)&sa_storage;
222 1 : sin6->sin6_family = AF_INET6;
223 1 : sin6->sin6_port = htons(7070);
224 1 : sin6->sin6_addr.s6_addr[0] = 128;
225 1 : tor_addr_from_sockaddr(&t1, (struct sockaddr *)sin6, &port1);
226 1 : tt_int_op(tor_addr_family(&t1),OP_EQ, AF_INET6);
227 1 : tt_int_op(port1, OP_EQ, 7070);
228 1 : p1 = tor_addr_to_str(buf, &t1, sizeof(buf), 0);
229 1 : tt_str_op(p1,OP_EQ, "8000::");
230 :
231 1 : memset(&sa_storage, 0, sizeof(sa_storage));
232 1 : 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 1 : tt_int_op(AF_INET6,OP_EQ, sin6->sin6_family);
236 1 : tt_int_op(9999,OP_EQ, ntohs(sin6->sin6_port));
237 1 : tt_int_op(0x80000000,OP_EQ, ntohl(S6_ADDR32(sin6->sin6_addr)[0]));
238 :
239 : /* ==== tor_addr_lookup: static cases. (Can't test dns without knowing we
240 : * have a good resolver. */
241 1 : tt_int_op(0,OP_EQ, tor_addr_lookup("127.128.129.130", AF_UNSPEC, &t1));
242 1 : tt_int_op(AF_INET,OP_EQ, tor_addr_family(&t1));
243 1 : tt_int_op(tor_addr_to_ipv4h(&t1),OP_EQ, 0x7f808182);
244 :
245 1 : tt_int_op(0,OP_EQ, tor_addr_lookup("9000::5", AF_UNSPEC, &t1));
246 1 : tt_int_op(AF_INET6,OP_EQ, tor_addr_family(&t1));
247 1 : tt_int_op(0x90,OP_EQ, tor_addr_to_in6_addr8(&t1)[0]);
248 1 : tt_assert(fast_mem_is_zero((char*)tor_addr_to_in6_addr8(&t1)+1, 14));
249 1 : tt_int_op(0x05,OP_EQ, tor_addr_to_in6_addr8(&t1)[15]);
250 :
251 : /* === Test pton: valid af_inet6 */
252 : /* Simple, valid parsing. */
253 1 : r = tor_inet_pton(AF_INET6,
254 : "0102:0304:0506:0708:090A:0B0C:0D0E:0F10", &a1);
255 1 : tt_int_op(r, OP_EQ, 1);
256 17 : for (i=0;i<16;++i) { tt_int_op(i+1,OP_EQ, (int)a1.s6_addr[i]); }
257 : /* ipv4 ending. */
258 1 : 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 1 : 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 1 : test_pton6_same("0000:0000:0000:0000:0009:C0A8:0001:0001",
265 : "::9:c0a8:1:1");
266 1 : test_pton6_same("0000:0000:0000:0000:0009:C0A8:0001:0001",
267 : "::9:c0a8:0.1.0.1");
268 : /* zeros in the middle. */
269 1 : test_pton6_same("fe80:0000:0000:0000:0202:1111:0001:0001",
270 : "fe80::202:1111:1:1");
271 : /* zeros at the end. */
272 1 : test_pton6_same("1000:0001:0000:0007:0000:0000:0000:0000",
273 : "1000:1:0:7::");
274 :
275 : /* === Test ntop: af_inet6 */
276 1 : test_ntop6_reduces("0:0:0:0:0:0:0:0", "::");
277 :
278 1 : test_ntop6_reduces("0001:0099:BEEF:0006:0123:FFFF:0001:0001",
279 : "1:99:beef:6:123:ffff:1:1");
280 :
281 : //test_ntop6_reduces("0:0:0:0:0:0:c0a8:0101", "::192.168.1.1");
282 1 : test_ntop6_reduces("0:0:0:0:0:ffff:c0a8:0101", "::ffff:192.168.1.1");
283 1 : test_ntop6_reduces("0:0:0:0:0:0:c0a8:0101", "::192.168.1.1");
284 1 : test_ntop6_reduces("002:0:0000:0:3::4", "2::3:0:0:4");
285 1 : test_ntop6_reduces("0:0::1:0:3", "::1:0:3");
286 1 : test_ntop6_reduces("008:0::0", "8::");
287 1 : test_ntop6_reduces("0:0:0:0:0:ffff::1", "::ffff:0.0.0.1");
288 1 : test_ntop6_reduces("abcd:0:0:0:0:0:7f00::", "abcd::7f00:0");
289 1 : test_ntop6_reduces("0000:0000:0000:0000:0009:C0A8:0001:0001",
290 : "::9:c0a8:1:1");
291 1 : test_ntop6_reduces("fe80:0000:0000:0000:0202:1111:0001:0001",
292 : "fe80::202:1111:1:1");
293 1 : test_ntop6_reduces("1000:0001:0000:0007:0000:0000:0000:0000",
294 : "1000:1:0:7::");
295 :
296 : /* Bad af param */
297 1 : tt_int_op(tor_inet_pton(AF_UNSPEC, 0, 0),OP_EQ, -1);
298 :
299 : /* === Test pton: invalid in6. */
300 1 : test_pton6_bad("foobar.");
301 1 : test_pton6_bad("-1::");
302 1 : test_pton6_bad("00001::");
303 1 : test_pton6_bad("10000::");
304 1 : test_pton6_bad("::10000");
305 1 : test_pton6_bad("55555::");
306 1 : test_pton6_bad("9:-60::");
307 1 : test_pton6_bad("9:+60::");
308 1 : test_pton6_bad("9|60::");
309 1 : test_pton6_bad("0x60::");
310 1 : test_pton6_bad("::0x60");
311 1 : test_pton6_bad("9:0x60::");
312 1 : test_pton6_bad("1:2:33333:4:0002:3::");
313 1 : test_pton6_bad("1:2:3333:4:fish:3::");
314 1 : test_pton6_bad("1:2:3:4:5:6:7:8:9");
315 1 : test_pton6_bad("1:2:3:4:5:6:7");
316 1 : test_pton6_bad("1:2:3:4:5:6:1.2.3.4.5");
317 1 : test_pton6_bad("1:2:3:4:5:6:1.2.3");
318 1 : test_pton6_bad("::1.2.3");
319 1 : test_pton6_bad("::1.2.3.4.5");
320 1 : test_pton6_bad("::ffff:0xff.0.0.0");
321 1 : test_pton6_bad("::ffff:ff.0.0.0");
322 1 : test_pton6_bad("::ffff:256.0.0.0");
323 1 : test_pton6_bad("::ffff:-1.0.0.0");
324 1 : test_pton6_bad("99");
325 1 : test_pton6_bad("");
326 1 : test_pton6_bad(".");
327 1 : test_pton6_bad(":");
328 1 : test_pton6_bad("1::2::3:4");
329 1 : test_pton6_bad("a:::b:c");
330 1 : test_pton6_bad(":::a:b:c");
331 1 : test_pton6_bad("a:b:c:::");
332 1 : test_pton6_bad("1.2.3.4");
333 1 : test_pton6_bad(":1.2.3.4");
334 1 : test_pton6_bad(".2.3.4");
335 : /* Regression tests for 22789. */
336 1 : test_pton6_bad("0xfoo");
337 1 : test_pton6_bad("0x88");
338 1 : test_pton6_bad("0xyxxy");
339 1 : test_pton6_bad("0XFOO");
340 1 : test_pton6_bad("0X88");
341 1 : test_pton6_bad("0XYXXY");
342 1 : test_pton6_bad("0x");
343 1 : test_pton6_bad("0X");
344 1 : test_pton6_bad("2000::1a00::1000:fc098");
345 :
346 : /* test internal checking */
347 1 : test_external_ip("fbff:ffff::2:7", 0);
348 1 : test_internal_ip("fc01::2:7", 0);
349 1 : test_internal_ip("fc01::02:7", 0);
350 1 : test_internal_ip("fc01::002:7", 0);
351 1 : test_internal_ip("fc01::0002:7", 0);
352 1 : test_internal_ip("fdff:ffff::f:f", 0);
353 1 : test_external_ip("fe00::3:f", 0);
354 :
355 1 : test_external_ip("fe7f:ffff::2:7", 0);
356 1 : test_internal_ip("fe80::2:7", 0);
357 1 : test_internal_ip("febf:ffff::f:f", 0);
358 :
359 1 : test_internal_ip("fec0::2:7:7", 0);
360 1 : test_internal_ip("feff:ffff::e:7:7", 0);
361 1 : test_external_ip("ff00::e:7:7", 0);
362 :
363 1 : test_internal_ip("::", 0);
364 1 : test_internal_ip("::1", 0);
365 1 : test_internal_ip("::1", 1);
366 1 : test_internal_ip("::", 0);
367 1 : test_external_ip("::", 1);
368 1 : test_external_ip("::2", 0);
369 1 : test_external_ip("2001::", 0);
370 1 : test_external_ip("ffff::", 0);
371 :
372 1 : test_external_ip("::ffff:0.0.0.0", 1);
373 1 : test_internal_ip("::ffff:0.0.0.0", 0);
374 1 : test_internal_ip("::ffff:0.255.255.255", 0);
375 1 : test_external_ip("::ffff:1.0.0.0", 0);
376 :
377 1 : test_external_ip("::ffff:9.255.255.255", 0);
378 1 : test_internal_ip("::ffff:10.0.0.0", 0);
379 1 : test_internal_ip("::ffff:10.255.255.255", 0);
380 1 : test_external_ip("::ffff:11.0.0.0", 0);
381 :
382 1 : test_external_ip("::ffff:126.255.255.255", 0);
383 1 : test_internal_ip("::ffff:127.0.0.0", 0);
384 1 : test_internal_ip("::ffff:127.255.255.255", 0);
385 1 : test_external_ip("::ffff:128.0.0.0", 0);
386 :
387 1 : test_external_ip("::ffff:172.15.255.255", 0);
388 1 : test_internal_ip("::ffff:172.16.0.0", 0);
389 1 : test_internal_ip("::ffff:172.31.255.255", 0);
390 1 : test_external_ip("::ffff:172.32.0.0", 0);
391 :
392 1 : test_external_ip("::ffff:192.167.255.255", 0);
393 1 : test_internal_ip("::ffff:192.168.0.0", 0);
394 1 : test_internal_ip("::ffff:192.168.255.255", 0);
395 1 : test_external_ip("::ffff:192.169.0.0", 0);
396 :
397 1 : test_external_ip("::ffff:169.253.255.255", 0);
398 1 : test_internal_ip("::ffff:169.254.0.0", 0);
399 1 : test_internal_ip("::ffff:169.254.255.255", 0);
400 1 : test_external_ip("::ffff:169.255.0.0", 0);
401 :
402 : /* tor_addr_compare(tor_addr_t x2) */
403 1 : test_addr_compare("ffff::", OP_EQ, "ffff::0");
404 1 : test_addr_compare("0::3:2:1", OP_LT, "0::ffff:0.3.2.1");
405 1 : test_addr_compare("0::2:2:1", OP_LT, "0::ffff:0.3.2.1");
406 1 : test_addr_compare("0::ffff:0.3.2.1", OP_GT, "0::0:0:0");
407 1 : test_addr_compare("0::ffff:5.2.2.1", OP_LT,
408 : "::ffff:6.0.0.0"); /* XXXX wrong. */
409 1 : tor_addr_parse_mask_ports("[::ffff:2.3.4.5]", 0, &t1, NULL, NULL, NULL);
410 1 : tor_addr_parse_mask_ports("2.3.4.5", 0, &t2, NULL, NULL, NULL);
411 1 : tt_int_op(tor_addr_compare(&t1, &t2, CMP_SEMANTIC), OP_EQ, 0);
412 1 : tor_addr_parse_mask_ports("[::ffff:2.3.4.4]", 0, &t1, NULL, NULL, NULL);
413 1 : tor_addr_parse_mask_ports("2.3.4.5", 0, &t2, NULL, NULL, NULL);
414 1 : tt_int_op(tor_addr_compare(&t1, &t2, CMP_SEMANTIC), OP_LT, 0);
415 :
416 : /* test compare_masked */
417 1 : test_addr_compare_masked("ffff::", OP_EQ, "ffff::0", 128);
418 1 : test_addr_compare_masked("ffff::", OP_EQ, "ffff::0", 64);
419 1 : test_addr_compare_masked("0::2:2:1", OP_LT, "0::8000:2:1", 81);
420 1 : test_addr_compare_masked("0::2:2:1", OP_EQ, "0::8000:2:1", 80);
421 :
422 : /* Test undecorated tor_addr_to_str */
423 1 : tt_int_op(AF_INET6,OP_EQ, tor_addr_parse(&t1, "[123:45:6789::5005:11]"));
424 1 : p1 = tor_addr_to_str(buf, &t1, sizeof(buf), 0);
425 1 : tt_str_op(p1,OP_EQ, "123:45:6789::5005:11");
426 1 : tt_int_op(AF_INET,OP_EQ, tor_addr_parse(&t1, "18.0.0.1"));
427 1 : p1 = tor_addr_to_str(buf, &t1, sizeof(buf), 0);
428 1 : tt_str_op(p1,OP_EQ, "18.0.0.1");
429 :
430 : /* Test decorated tor_addr_to_str */
431 1 : tt_int_op(AF_INET6,OP_EQ, tor_addr_parse(&t1, "[123:45:6789::5005:11]"));
432 1 : p1 = tor_addr_to_str(buf, &t1, sizeof(buf), 1);
433 1 : tt_str_op(p1,OP_EQ, "[123:45:6789::5005:11]");
434 1 : tt_int_op(AF_INET,OP_EQ, tor_addr_parse(&t1, "18.0.0.1"));
435 1 : p1 = tor_addr_to_str(buf, &t1, sizeof(buf), 1);
436 1 : tt_str_op(p1,OP_EQ, "18.0.0.1");
437 :
438 : /* Test buffer bounds checking of tor_addr_to_str */
439 1 : tt_int_op(AF_INET6,OP_EQ, tor_addr_parse(&t1, "::")); /* 2 + \0 */
440 1 : tt_ptr_op(tor_addr_to_str(buf, &t1, 2, 0),OP_EQ, NULL); /* too short buf */
441 1 : tt_str_op(tor_addr_to_str(buf, &t1, 3, 0),OP_EQ, "::");
442 1 : tt_ptr_op(tor_addr_to_str(buf, &t1, 4, 1),OP_EQ, NULL); /* too short buf */
443 1 : tt_str_op(tor_addr_to_str(buf, &t1, 5, 1),OP_EQ, "[::]");
444 :
445 1 : tt_int_op(AF_INET6,OP_EQ, tor_addr_parse(&t1, "2000::1337")); /* 10 + \0 */
446 1 : tt_ptr_op(tor_addr_to_str(buf, &t1, 10, 0),OP_EQ, NULL); /* too short buf */
447 1 : tt_str_op(tor_addr_to_str(buf, &t1, 11, 0),OP_EQ, "2000::1337");
448 1 : tt_ptr_op(tor_addr_to_str(buf, &t1, 12, 1),OP_EQ, NULL); /* too short buf */
449 1 : tt_str_op(tor_addr_to_str(buf, &t1, 13, 1),OP_EQ, "[2000::1337]");
450 :
451 1 : tt_int_op(AF_INET,OP_EQ, tor_addr_parse(&t1, "1.2.3.4")); /* 7 + \0 */
452 1 : tt_ptr_op(tor_addr_to_str(buf, &t1, 7, 0),OP_EQ, NULL); /* too short buf */
453 1 : tt_str_op(tor_addr_to_str(buf, &t1, 8, 0),OP_EQ, "1.2.3.4");
454 :
455 1 : tt_int_op(AF_INET, OP_EQ,
456 : tor_addr_parse(&t1, "255.255.255.255")); /* 15 + \0 */
457 1 : tt_ptr_op(tor_addr_to_str(buf, &t1, 15, 0),OP_EQ, NULL); /* too short buf */
458 1 : tt_str_op(tor_addr_to_str(buf, &t1, 16, 0),OP_EQ, "255.255.255.255");
459 1 : tt_ptr_op(tor_addr_to_str(buf, &t1, 15, 1),OP_EQ, NULL); /* too short buf */
460 1 : tt_str_op(tor_addr_to_str(buf, &t1, 16, 1),OP_EQ, "255.255.255.255");
461 :
462 1 : t1.family = AF_UNSPEC;
463 1 : tt_ptr_op(tor_addr_to_str(buf, &t1, sizeof(buf), 0),OP_EQ, NULL);
464 :
465 : /* Test tor_addr_parse_PTR_name */
466 1 : i = tor_addr_parse_PTR_name(&t1, "Foobar.baz", AF_UNSPEC, 0);
467 1 : tt_int_op(0,OP_EQ, i);
468 1 : i = tor_addr_parse_PTR_name(&t1, "Foobar.baz", AF_UNSPEC, 1);
469 1 : tt_int_op(0,OP_EQ, i);
470 1 : i = tor_addr_parse_PTR_name(&t1, "9999999999999999999999999999.in-addr.arpa",
471 : AF_UNSPEC, 1);
472 1 : tt_int_op(-1,OP_EQ, i);
473 1 : i = tor_addr_parse_PTR_name(&t1, "1.0.168.192.in-addr.arpa",
474 : AF_UNSPEC, 1);
475 1 : tt_int_op(1,OP_EQ, i);
476 1 : tt_int_op(tor_addr_family(&t1),OP_EQ, AF_INET);
477 1 : p1 = tor_addr_to_str(buf, &t1, sizeof(buf), 1);
478 1 : tt_str_op(p1,OP_EQ, "192.168.0.1");
479 1 : i = tor_addr_parse_PTR_name(&t1, "192.168.0.99", AF_UNSPEC, 0);
480 1 : tt_int_op(0,OP_EQ, i);
481 1 : i = tor_addr_parse_PTR_name(&t1, "192.168.0.99", AF_UNSPEC, 1);
482 1 : tt_int_op(1,OP_EQ, i);
483 1 : p1 = tor_addr_to_str(buf, &t1, sizeof(buf), 1);
484 1 : tt_str_op(p1,OP_EQ, "192.168.0.99");
485 1 : memset(&t1, 0, sizeof(t1));
486 1 : 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."
489 : "ip6.ARPA",
490 : AF_UNSPEC, 0);
491 1 : tt_int_op(1,OP_EQ, i);
492 1 : p1 = tor_addr_to_str(buf, &t1, sizeof(buf), 1);
493 1 : tt_str_op(p1,OP_EQ, "[9dee:effe:ebe1:beef:fedc:ba98:7654:3210]");
494 : /* Failing cases. */
495 1 : 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."
498 : "ip6.ARPA",
499 : AF_UNSPEC, 0);
500 1 : tt_int_op(i,OP_EQ, -1);
501 1 : 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."
504 : "ip6.ARPA",
505 : AF_UNSPEC, 0);
506 1 : tt_int_op(i,OP_EQ, -1);
507 1 : 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."
510 : "ip6.ARPA",
511 : AF_UNSPEC, 0);
512 1 : tt_int_op(i,OP_EQ, -1);
513 1 : i = tor_addr_parse_PTR_name(&t1, "32.1.1.in-addr.arpa",
514 : AF_UNSPEC, 0);
515 1 : tt_int_op(i,OP_EQ, -1);
516 1 : i = tor_addr_parse_PTR_name(&t1, ".in-addr.arpa",
517 : AF_UNSPEC, 0);
518 1 : tt_int_op(i,OP_EQ, -1);
519 1 : i = tor_addr_parse_PTR_name(&t1, "1.2.3.4.5.in-addr.arpa",
520 : AF_UNSPEC, 0);
521 1 : tt_int_op(i,OP_EQ, -1);
522 1 : i = tor_addr_parse_PTR_name(&t1, "1.2.3.4.5.in-addr.arpa",
523 : AF_INET6, 0);
524 1 : tt_int_op(i,OP_EQ, -1);
525 1 : 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."
528 : "ip6.ARPA",
529 : AF_INET, 0);
530 1 : tt_int_op(i,OP_EQ, -1);
531 :
532 : /* === Test tor_addr_to_PTR_name */
533 :
534 : /* Stage IPv4 addr */
535 1 : memset(&sa_storage, 0, sizeof(sa_storage));
536 1 : sin = (struct sockaddr_in *)&sa_storage;
537 1 : sin->sin_family = AF_INET;
538 1 : sin->sin_addr.s_addr = htonl(0x7f010203); /* 127.1.2.3 */
539 1 : tor_addr_from_sockaddr(&t1, (struct sockaddr *)sin, NULL);
540 :
541 : /* Check IPv4 PTR - too short buffer */
542 1 : tt_int_op(tor_addr_to_PTR_name(rbuf, 1, &t1),OP_EQ, -1);
543 1 : tt_int_op(tor_addr_to_PTR_name(rbuf,
544 : strlen("3.2.1.127.in-addr.arpa") - 1,
545 : &t1),OP_EQ, -1);
546 :
547 : /* Check IPv4 PTR - valid addr */
548 1 : tt_int_op(tor_addr_to_PTR_name(rbuf, sizeof(rbuf), &t1),OP_EQ,
549 : strlen("3.2.1.127.in-addr.arpa"));
550 1 : tt_str_op(rbuf,OP_EQ, "3.2.1.127.in-addr.arpa");
551 :
552 : /* Invalid addr family */
553 1 : t1.family = AF_UNSPEC;
554 1 : tt_int_op(tor_addr_to_PTR_name(rbuf, sizeof(rbuf), &t1),OP_EQ, -1);
555 :
556 : /* Stage IPv6 addr */
557 1 : memset(&sa_storage, 0, sizeof(sa_storage));
558 1 : sin6 = (struct sockaddr_in6 *)&sa_storage;
559 1 : sin6->sin6_family = AF_INET6;
560 1 : sin6->sin6_addr.s6_addr[0] = 0x80; /* 8000::abcd */
561 1 : sin6->sin6_addr.s6_addr[14] = 0xab;
562 1 : sin6->sin6_addr.s6_addr[15] = 0xcd;
563 :
564 1 : tor_addr_from_sockaddr(&t1, (struct sockaddr *)sin6, NULL);
565 :
566 : {
567 1 : 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";
569 :
570 : /* Check IPv6 PTR - too short buffer */
571 1 : tt_int_op(tor_addr_to_PTR_name(rbuf, 0, &t1),OP_EQ, -1);
572 1 : tt_int_op(tor_addr_to_PTR_name(rbuf, strlen(addr_PTR) - 1, &t1),OP_EQ, -1);
573 :
574 : /* Check IPv6 PTR - valid addr */
575 1 : tt_int_op(tor_addr_to_PTR_name(rbuf, sizeof(rbuf), &t1),OP_EQ,
576 : strlen(addr_PTR));
577 1 : tt_str_op(rbuf,OP_EQ, addr_PTR);
578 : }
579 :
580 : /* XXXX turn this into a separate function; it's not all IPv6. */
581 : /* test tor_addr_parse_mask_ports */
582 1 : test_addr_mask_ports_parse("[::f]/17:47-95", AF_INET6,
583 : 0, 0, 0, 0x0000000f, 17, 47, 95);
584 1 : 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 1 : test_addr_mask_ports_parse("[::ffff:4.1.1.7]/120:443", AF_INET6,
588 : 0, 0, 0x0000ffff, 0x04010107, 120, 443, 443);
589 1 : tt_str_op(p1,OP_EQ, "::ffff:4.1.1.7");
590 1 : test_addr_mask_ports_parse("[abcd:2::44a:0]:2-65000", AF_INET6,
591 : 0xabcd0002, 0, 0, 0x044a0000, 128, 2, 65000);
592 :
593 1 : tt_str_op(p1,OP_EQ, "abcd:2::44a:0");
594 : /* Try some long addresses. */
595 1 : r=tor_addr_parse_mask_ports("[ffff:1111:1111:1111:1111:1111:1111:1111]",
596 : 0, &t1, NULL, NULL, NULL);
597 1 : tt_int_op(r, OP_EQ, AF_INET6);
598 1 : r=tor_addr_parse_mask_ports("[ffff:1111:1111:1111:1111:1111:1111:11111]",
599 : 0, &t1, NULL, NULL, NULL);
600 1 : tt_int_op(r, OP_EQ, -1);
601 1 : r=tor_addr_parse_mask_ports("[ffff:1111:1111:1111:1111:1111:1111:1111:1]",
602 : 0, &t1, NULL, NULL, NULL);
603 1 : tt_int_op(r, OP_EQ, -1);
604 1 : 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 1 : tt_int_op(r, OP_EQ, -1);
611 : /* Try some failing cases. */
612 1 : r=tor_addr_parse_mask_ports("[fefef::]/112", 0, &t1, NULL, NULL, NULL);
613 1 : tt_int_op(r, OP_EQ, -1);
614 1 : r=tor_addr_parse_mask_ports("[fefe::/112", 0, &t1, NULL, NULL, NULL);
615 1 : tt_int_op(r, OP_EQ, -1);
616 1 : r=tor_addr_parse_mask_ports("[fefe::", 0, &t1, NULL, NULL, NULL);
617 1 : tt_int_op(r, OP_EQ, -1);
618 1 : r=tor_addr_parse_mask_ports("[fefe::X]", 0, &t1, NULL, NULL, NULL);
619 1 : tt_int_op(r, OP_EQ, -1);
620 1 : r=tor_addr_parse_mask_ports("efef::/112", 0, &t1, NULL, NULL, NULL);
621 1 : tt_int_op(r, OP_EQ, -1);
622 1 : r=tor_addr_parse_mask_ports("[f:f:f:f:f:f:f:f::]",0,&t1, NULL, NULL, NULL);
623 1 : tt_int_op(r, OP_EQ, -1);
624 1 : r=tor_addr_parse_mask_ports("[::f:f:f:f:f:f:f:f]",0,&t1, NULL, NULL, NULL);
625 1 : tt_int_op(r, OP_EQ, -1);
626 1 : r=tor_addr_parse_mask_ports("[f:f:f:f:f:f:f:f:f]",0,&t1, NULL, NULL, NULL);
627 1 : tt_int_op(r, OP_EQ, -1);
628 1 : r=tor_addr_parse_mask_ports("[f:f:f:f:f::]/fred",0,&t1,&mask, NULL, NULL);
629 1 : tt_int_op(r, OP_EQ, -1);
630 1 : r=tor_addr_parse_mask_ports("[f:f:f:f:f::]/255.255.0.0",
631 : 0,&t1, NULL, NULL, NULL);
632 1 : tt_int_op(r, OP_EQ, -1);
633 : /* This one will get rejected because it isn't a pure prefix. */
634 1 : r=tor_addr_parse_mask_ports("1.1.2.3/255.255.64.0",0,&t1, &mask,NULL,NULL);
635 1 : tt_int_op(r, OP_EQ, -1);
636 : /* Test for V4-mapped address with mask < 96. (arguably not valid) */
637 1 : r=tor_addr_parse_mask_ports("[::ffff:1.1.2.2/33]",0,&t1, &mask, NULL, NULL);
638 1 : tt_int_op(r, OP_EQ, -1);
639 1 : r=tor_addr_parse_mask_ports("1.1.2.2/33",0,&t1, &mask, NULL, NULL);
640 1 : tt_int_op(r, OP_EQ, -1);
641 : /* Try extended wildcard addresses with out TAPMP_EXTENDED_STAR*/
642 1 : r=tor_addr_parse_mask_ports("*4",0,&t1, &mask, NULL, NULL);
643 1 : tt_int_op(r, OP_EQ, -1);
644 1 : r=tor_addr_parse_mask_ports("*6",0,&t1, &mask, NULL, NULL);
645 1 : tt_int_op(r, OP_EQ, -1);
646 1 : tt_int_op(r, OP_EQ, -1);
647 : /* Try a mask with a wildcard. */
648 1 : r=tor_addr_parse_mask_ports("*/16",0,&t1, &mask, NULL, NULL);
649 1 : tt_int_op(r, OP_EQ, -1);
650 1 : r=tor_addr_parse_mask_ports("*4/16",TAPMP_EXTENDED_STAR,
651 : &t1, &mask, NULL, NULL);
652 1 : tt_int_op(r, OP_EQ, -1);
653 1 : r=tor_addr_parse_mask_ports("*6/30",TAPMP_EXTENDED_STAR,
654 : &t1, &mask, NULL, NULL);
655 1 : tt_int_op(r, OP_EQ, -1);
656 : /* Basic mask tests*/
657 1 : r=tor_addr_parse_mask_ports("1.1.2.2/31",0,&t1, &mask, NULL, NULL);
658 1 : tt_int_op(r, OP_EQ, AF_INET);
659 1 : tt_int_op(mask,OP_EQ,31);
660 1 : tt_int_op(tor_addr_family(&t1),OP_EQ,AF_INET);
661 1 : tt_int_op(tor_addr_to_ipv4h(&t1),OP_EQ,0x01010202);
662 1 : r=tor_addr_parse_mask_ports("3.4.16.032:1-2",0,&t1, &mask, &port1, &port2);
663 1 : tt_int_op(r, OP_EQ, -1);
664 1 : r=tor_addr_parse_mask_ports("1.1.2.3/255.255.128.0",0,&t1, &mask,NULL,NULL);
665 1 : tt_int_op(r, OP_EQ, AF_INET);
666 1 : tt_int_op(mask,OP_EQ,17);
667 1 : tt_int_op(tor_addr_family(&t1),OP_EQ,AF_INET);
668 1 : tt_int_op(tor_addr_to_ipv4h(&t1),OP_EQ,0x01010203);
669 1 : r=tor_addr_parse_mask_ports("[efef::]/112",0,&t1, &mask, &port1, &port2);
670 1 : tt_int_op(r, OP_EQ, AF_INET6);
671 1 : tt_uint_op(port1, OP_EQ, 1);
672 1 : tt_uint_op(port2, OP_EQ, 65535);
673 : /* Try regular wildcard behavior without TAPMP_EXTENDED_STAR */
674 1 : r=tor_addr_parse_mask_ports("*:80-443",0,&t1,&mask,&port1,&port2);
675 1 : tt_int_op(r,OP_EQ,AF_INET); /* Old users of this always get inet */
676 1 : tt_int_op(tor_addr_family(&t1),OP_EQ,AF_INET);
677 1 : tt_int_op(tor_addr_to_ipv4h(&t1),OP_EQ,0);
678 1 : tt_int_op(mask,OP_EQ,0);
679 1 : tt_int_op(port1,OP_EQ,80);
680 1 : tt_int_op(port2,OP_EQ,443);
681 : /* Now try wildcards *with* TAPMP_EXTENDED_STAR */
682 1 : r=tor_addr_parse_mask_ports("*:8000-9000",TAPMP_EXTENDED_STAR,
683 : &t1,&mask,&port1,&port2);
684 1 : tt_int_op(r,OP_EQ,AF_UNSPEC);
685 1 : tt_int_op(tor_addr_family(&t1),OP_EQ,AF_UNSPEC);
686 1 : tt_int_op(mask,OP_EQ,0);
687 1 : tt_int_op(port1,OP_EQ,8000);
688 1 : tt_int_op(port2,OP_EQ,9000);
689 1 : r=tor_addr_parse_mask_ports("*4:6667",TAPMP_EXTENDED_STAR,
690 : &t1,&mask,&port1,&port2);
691 1 : tt_int_op(r,OP_EQ,AF_INET);
692 1 : tt_int_op(tor_addr_family(&t1),OP_EQ,AF_INET);
693 1 : tt_int_op(tor_addr_to_ipv4h(&t1),OP_EQ,0);
694 1 : tt_int_op(mask,OP_EQ,0);
695 1 : tt_int_op(port1,OP_EQ,6667);
696 1 : tt_int_op(port2,OP_EQ,6667);
697 1 : r=tor_addr_parse_mask_ports("*6",TAPMP_EXTENDED_STAR,
698 : &t1,&mask,&port1,&port2);
699 1 : tt_int_op(r,OP_EQ,AF_INET6);
700 1 : tt_int_op(tor_addr_family(&t1),OP_EQ,AF_INET6);
701 1 : tt_assert(fast_mem_is_zero((const char*)tor_addr_to_in6_addr32(&t1), 16));
702 1 : tt_int_op(mask,OP_EQ,0);
703 1 : tt_int_op(port1,OP_EQ,1);
704 1 : tt_int_op(port2,OP_EQ,65535);
705 :
706 : /* make sure inet address lengths >= max */
707 1 : tt_int_op(INET_NTOA_BUF_LEN, OP_GE, sizeof("255.255.255.255"));
708 1 : tt_int_op(TOR_ADDR_BUF_LEN, OP_GE,
709 : sizeof("ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255"));
710 :
711 1 : tt_assert(sizeof(tor_addr_t) >= sizeof(struct in6_addr));
712 :
713 : /* get interface addresses */
714 1 : r = get_interface_address6(LOG_DEBUG, AF_INET, &t1);
715 1 : tt_int_op(r, OP_LE, 0); // "it worked or it didn't"
716 1 : i = get_interface_address6(LOG_DEBUG, AF_INET6, &t2);
717 1 : tt_int_op(i, OP_LE, 0); // "it worked or it didn't"
718 :
719 1 : TT_BLATHER(("v4 address: %s (family=%d)", fmt_addr(&t1),
720 : tor_addr_family(&t1)));
721 1 : TT_BLATHER(("v6 address: %s (family=%d)", fmt_addr(&t2),
722 : tor_addr_family(&t2)));
723 :
724 1 : done:
725 1 : ;
726 1 : }
727 :
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.
731 : */
732 : #define TEST_ADDR_PARSE_FMT(addr_str, expect_family, fmt_decorated, \
733 : expect_str) \
734 : STMT_BEGIN \
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); \
740 : STMT_END
741 :
742 : /* Test that addr_str fails to parse, and:
743 : * - the returned address is null.
744 : */
745 : #define TEST_ADDR_PARSE_XFAIL(addr_str) \
746 : STMT_BEGIN \
747 : r = tor_addr_parse(&addr, addr_str); \
748 : tt_int_op(r, OP_EQ, -1); \
749 : tt_assert(tor_addr_is_null(&addr)); \
750 : STMT_END
751 :
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.
756 : */
757 : #define TEST_ADDR_PORT_PARSE_FMT(addr_port_str, default_port, expect_family, \
758 : fmt_decorated, expect_str, expect_port) \
759 : STMT_BEGIN \
760 : r = tor_addr_port_parse(LOG_DEBUG, addr_port_str, &addr, &port, \
761 : default_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); \
768 : STMT_END
769 :
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.
773 : */
774 : #define TEST_ADDR_PORT_PARSE_XFAIL(addr_port_str, default_port) \
775 : STMT_BEGIN \
776 : r = tor_addr_port_parse(LOG_DEBUG, addr_port_str, &addr, &port, \
777 : default_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); \
781 : STMT_END
782 :
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.
786 : */
787 : #define TEST_ADDR_V4_LOOKUP_HOSTNAME(addr_str, expect_str) \
788 : STMT_BEGIN \
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); \
792 : STMT_END
793 :
794 : /* Test that bad_str fails to parse using tor_lookup_hostname(), with a
795 : * permanent failure, and:
796 : * - the returned address is 0.
797 : */
798 : #define TEST_ADDR_V4_LOOKUP_XFAIL(bad_str) \
799 : STMT_BEGIN \
800 : r = tor_lookup_hostname(bad_str, &addr32h); \
801 : tt_int_op(r, OP_EQ, -1); \
802 : tt_int_op(addr32h, OP_EQ, 0); \
803 : STMT_END
804 :
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
810 : * network.
811 : */
812 : #define TEST_HOST_V4_LOOKUP(host_str) \
813 : STMT_BEGIN \
814 : r = tor_lookup_hostname(host_str, &addr32h); \
815 : tt_int_op(r, OP_GE, -1); \
816 : tt_int_op(r, OP_LE, 1); \
817 : if (r != 0) \
818 : tt_int_op(addr32h, OP_EQ, 0); \
819 : STMT_END
820 :
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.
825 : */
826 : #define TEST_ADDR_LOOKUP_FMT(addr_str, require_family, expect_family, \
827 : fmt_decorated, expect_str) \
828 : STMT_BEGIN \
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); \
835 : STMT_END
836 :
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.
840 : */
841 : #define TEST_ADDR_LOOKUP_XFAIL(bad_str, require_family) \
842 : STMT_BEGIN \
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)); \
846 : STMT_END
847 :
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
853 : * network.
854 : */
855 : #define TEST_HOST_LOOKUP(host_str, require_family) \
856 : STMT_BEGIN \
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); \
860 : if (r != 0) \
861 : tt_assert(tor_addr_is_null(&addr)); \
862 : STMT_END
863 :
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.
869 : */
870 : #define TEST_ADDR_PORT_LOOKUP_FMT(addr_port_str, expect_family, \
871 : fmt_decorated, expect_str, expect_port) \
872 : STMT_BEGIN \
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); \
880 : STMT_END
881 :
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.
886 : */
887 : #define TEST_ADDR_PORT_LOOKUP_XFAIL(bad_str) \
888 : STMT_BEGIN \
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); \
893 : STMT_END
894 :
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
903 : * network.
904 : */
905 : #define TEST_HOST_PORT_LOOKUP(host_port_str, expect_success_port) \
906 : STMT_BEGIN \
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); \
910 : if (r == -1) { \
911 : tt_assert(tor_addr_is_null(&addr)); \
912 : tt_int_op(port, OP_EQ, 0); \
913 : } else { \
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); \
917 : } \
918 : STMT_END
919 :
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.
932 : */
933 : #define TEST_ADDR_V4_PARSE_CANONICAL(addr_str) \
934 : STMT_BEGIN \
935 : TEST_ADDR_PARSE_FMT(addr_str, AF_INET, 0, addr_str); \
936 : TEST_ADDR_PORT_PARSE_FMT(addr_str, 111, AF_INET, 0, \
937 : addr_str, 111); \
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); \
944 : STMT_END
945 :
946 : /* Test that addr_str successfully parses as a canonical fmt_decorated
947 : * IPv6 address.
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.
958 : */
959 : #define TEST_ADDR_V6_PARSE_CANONICAL(addr_str, fmt_decorated) \
960 : STMT_BEGIN \
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, \
963 : addr_str, 222); \
964 : TEST_ADDR_LOOKUP_FMT(addr_str, AF_INET6, AF_INET6, fmt_decorated, \
965 : addr_str); \
966 : TEST_ADDR_LOOKUP_FMT(addr_str, AF_UNSPEC, AF_INET6, fmt_decorated, \
967 : addr_str); \
968 : TEST_ADDR_PORT_LOOKUP_FMT(addr_str, AF_INET6, fmt_decorated, addr_str, \
969 : 0); \
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); \
973 : STMT_END
974 :
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.
987 : */
988 : #define TEST_ADDR_V6_PARSE(addr_str, fmt_decorated, expect_str) \
989 : STMT_BEGIN \
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, \
992 : expect_str, 333); \
993 : TEST_ADDR_LOOKUP_FMT(addr_str, AF_INET6, AF_INET6, fmt_decorated, \
994 : expect_str); \
995 : TEST_ADDR_LOOKUP_FMT(addr_str, AF_UNSPEC, AF_INET6, fmt_decorated, \
996 : expect_str); \
997 : TEST_ADDR_PORT_LOOKUP_FMT(addr_str, AF_INET6, fmt_decorated, expect_str, \
998 : 0); \
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); \
1002 : STMT_END
1003 :
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
1014 : * port.
1015 : */
1016 : #define TEST_ADDR_V4_PORT_PARSE(addr_port_str, expect_str, expect_port) \
1017 : STMT_BEGIN \
1018 : TEST_ADDR_PORT_PARSE_FMT(addr_port_str, -1, AF_INET, 0, expect_str, \
1019 : expect_port); \
1020 : TEST_ADDR_PORT_PARSE_FMT(addr_port_str, 444, AF_INET, 0, expect_str, \
1021 : expect_port); \
1022 : TEST_ADDR_PORT_LOOKUP_FMT(addr_port_str, AF_INET, 0, expect_str, \
1023 : expect_port); \
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); \
1029 : STMT_END
1030 :
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
1040 : * supports IPv4,
1041 : * - tor_addr_lookup(), regardless of the address family, because there is a
1042 : * port.
1043 : */
1044 : #define TEST_ADDR_V6_PORT_PARSE(addr_port_str, expect_str, expect_port) \
1045 : STMT_BEGIN \
1046 : TEST_ADDR_PORT_PARSE_FMT(addr_port_str, -1, AF_INET6, 0, expect_str, \
1047 : expect_port); \
1048 : TEST_ADDR_PORT_PARSE_FMT(addr_port_str, 555, AF_INET6, 0, expect_str, \
1049 : expect_port); \
1050 : TEST_ADDR_PORT_LOOKUP_FMT(addr_port_str, AF_INET6, 0, expect_str, \
1051 : expect_port); \
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); \
1057 : STMT_END
1058 :
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().
1067 : */
1068 : #define TEST_ADDR_PARSE_XFAIL_MALFORMED(bad_str) \
1069 : STMT_BEGIN \
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); \
1078 : STMT_END
1079 :
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.
1089 : */
1090 : #define TEST_HOSTNAME(host_str) \
1091 : STMT_BEGIN \
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); \
1100 : STMT_END
1101 :
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
1106 : * successful.
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
1113 : * support ports.
1114 : */
1115 : #define TEST_HOSTNAME_PORT(host_port_str, expect_success_port) \
1116 : STMT_BEGIN \
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); \
1125 : STMT_END
1126 :
1127 : static void
1128 1 : test_addr_parse_canonical(void *arg)
1129 : {
1130 1 : int r;
1131 1 : tor_addr_t addr;
1132 1 : uint16_t port;
1133 1 : const char *sv;
1134 1 : uint32_t addr32h;
1135 1 : char buf[TOR_ADDR_BUF_LEN];
1136 :
1137 1 : (void)arg;
1138 :
1139 : /* Correct calls. */
1140 1 : TEST_ADDR_V4_PARSE_CANONICAL("192.0.2.1");
1141 1 : TEST_ADDR_V4_PARSE_CANONICAL("192.0.2.2");
1142 :
1143 1 : TEST_ADDR_V6_PARSE_CANONICAL("[11:22::33:44]", 1);
1144 1 : TEST_ADDR_V6_PARSE_CANONICAL("[::1]", 1);
1145 1 : TEST_ADDR_V6_PARSE_CANONICAL("[::]", 1);
1146 1 : TEST_ADDR_V6_PARSE_CANONICAL("[2::]", 1);
1147 1 : TEST_ADDR_V6_PARSE_CANONICAL("[11:22:33:44:55:66:77:88]", 1);
1148 :
1149 : /* Allow IPv6 without square brackets, when there is no port, but only if
1150 : * there is a default port */
1151 1 : TEST_ADDR_V6_PARSE_CANONICAL("11:22::33:44", 0);
1152 1 : TEST_ADDR_V6_PARSE_CANONICAL("::1", 0);
1153 1 : TEST_ADDR_V6_PARSE_CANONICAL("::", 0);
1154 1 : TEST_ADDR_V6_PARSE_CANONICAL("2::", 0);
1155 1 : TEST_ADDR_V6_PARSE_CANONICAL("11:22:33:44:55:66:77:88", 0);
1156 1 : done:
1157 1 : ;
1158 1 : }
1159 :
1160 : /** Test tor_addr_parse() and tor_addr_port_parse(). */
1161 : static void
1162 1 : test_addr_parse(void *arg)
1163 : {
1164 :
1165 1 : int r;
1166 1 : tor_addr_t addr;
1167 1 : uint16_t port;
1168 1 : const char *sv;
1169 1 : uint32_t addr32h;
1170 1 : char buf[TOR_ADDR_BUF_LEN];
1171 :
1172 1 : (void)arg;
1173 :
1174 1 : mock_hostname_resolver();
1175 :
1176 : /* IPv6-mapped IPv4 addresses. Tor doesn't really use these. */
1177 1 : TEST_ADDR_V6_PARSE("11:22:33:44:55:66:1.2.3.4", 0,
1178 : "11:22:33:44:55:66:102:304");
1179 :
1180 1 : TEST_ADDR_V6_PARSE("11:22::33:44:1.2.3.4", 0,
1181 : "11:22::33:44:102:304");
1182 :
1183 : /* Ports. */
1184 1 : TEST_ADDR_V4_PORT_PARSE("192.0.2.1:1234", "192.0.2.1", 1234);
1185 1 : TEST_ADDR_V6_PORT_PARSE("[::1]:1234", "::1", 1234);
1186 :
1187 : /* Host names. */
1188 1 : TEST_HOSTNAME("localhost");
1189 1 : TEST_HOSTNAME_PORT("localhost:1234", 1234);
1190 1 : TEST_HOSTNAME_PORT("localhost:0", 0);
1191 :
1192 1 : TEST_HOSTNAME("torproject.org");
1193 1 : TEST_HOSTNAME_PORT("torproject.org:56", 56);
1194 :
1195 1 : TEST_HOSTNAME("probably-not-a-valid-dns.name-tld");
1196 1 : TEST_HOSTNAME_PORT("probably-not-a-valid-dns.name-tld:789", 789);
1197 :
1198 : /* Malformed addresses. */
1199 : /* Empty string. */
1200 1 : TEST_ADDR_PARSE_XFAIL_MALFORMED("");
1201 :
1202 : /* Square brackets around IPv4 address. */
1203 1 : TEST_ADDR_PARSE_XFAIL_MALFORMED("[192.0.2.1]");
1204 1 : TEST_ADDR_PARSE_XFAIL_MALFORMED("[192.0.2.3]:12345");
1205 :
1206 : /* Only left square bracket. */
1207 1 : TEST_ADDR_PARSE_XFAIL_MALFORMED("[11:22::33:44");
1208 :
1209 : /* Only right square bracket. */
1210 1 : TEST_ADDR_PARSE_XFAIL_MALFORMED("11:22::33:44]");
1211 :
1212 : /* Leading colon. */
1213 1 : TEST_ADDR_PARSE_XFAIL_MALFORMED(":11:22::33:44");
1214 :
1215 : /* Trailing colon. */
1216 1 : TEST_ADDR_PARSE_XFAIL_MALFORMED("11:22::33:44:");
1217 1 : TEST_ADDR_PARSE_XFAIL_MALFORMED("[::1]:");
1218 1 : TEST_ADDR_PARSE_XFAIL_MALFORMED("localhost:");
1219 :
1220 : /* Bad port. */
1221 1 : TEST_ADDR_PARSE_XFAIL_MALFORMED("192.0.2.2:66666");
1222 1 : TEST_ADDR_PARSE_XFAIL_MALFORMED("[::1]:77777");
1223 1 : TEST_ADDR_PARSE_XFAIL_MALFORMED("::1:88888");
1224 1 : TEST_ADDR_PARSE_XFAIL_MALFORMED("localhost:99999");
1225 :
1226 1 : TEST_ADDR_PARSE_XFAIL_MALFORMED("192.0.2.2:-1");
1227 1 : TEST_ADDR_PARSE_XFAIL_MALFORMED("[::1]:-2");
1228 1 : TEST_ADDR_PARSE_XFAIL_MALFORMED("::1:-3");
1229 1 : TEST_ADDR_PARSE_XFAIL_MALFORMED("localhost:-4");
1230 :
1231 1 : TEST_ADDR_PARSE_XFAIL_MALFORMED("192.0.2.2:1 bad");
1232 1 : TEST_ADDR_PARSE_XFAIL_MALFORMED("192.0.2.2:bad-port");
1233 1 : TEST_ADDR_PARSE_XFAIL_MALFORMED("[::1]:bad-port-1");
1234 1 : TEST_ADDR_PARSE_XFAIL_MALFORMED("::1:1-bad-port");
1235 1 : TEST_ADDR_PARSE_XFAIL_MALFORMED("localhost:1-bad-port");
1236 1 : TEST_ADDR_PARSE_XFAIL_MALFORMED("localhost:1-bad-port-1");
1237 :
1238 : /* Bad hostname */
1239 1 : TEST_ADDR_PARSE_XFAIL_MALFORMED("definitely invalid");
1240 1 : TEST_ADDR_PARSE_XFAIL_MALFORMED("definitely invalid:22222");
1241 :
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 1 : TEST_HOSTNAME("11:22:33:44:55:66:77:88:1.2.3.4");
1247 :
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 1 : 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 1 : TEST_HOSTNAME_PORT("11:22:33:44:55:66:77:88:99", 99);
1256 : /* But we accept it if it has square brackets. */
1257 1 : TEST_ADDR_V6_PORT_PARSE("[11:22:33:44:55:66:77:88]:99",
1258 : "11:22:33:44:55:66:77:88",99);
1259 :
1260 : /* Bad IPv4 address
1261 : * We reject it, but some OS host lookup routines accept it as an
1262 : * IPv4 address[:port], with a zero last octet */
1263 1 : TEST_HOSTNAME("192.0.1");
1264 1 : TEST_HOSTNAME_PORT("192.0.2:1234", 1234);
1265 :
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 1 : TEST_HOSTNAME_PORT("::1:12345", 12345);
1270 1 : TEST_HOSTNAME_PORT("11:22::33:44:12345", 12345);
1271 :
1272 : /* And this is an ambiguous case, which is interpreted as an IPv6 address. */
1273 1 : TEST_ADDR_V6_PARSE_CANONICAL("11:22::88:99", 0);
1274 : /* Use square brackets to resolve the ambiguity */
1275 1 : TEST_ADDR_V6_PARSE_CANONICAL("[11:22::88:99]", 1);
1276 1 : TEST_ADDR_V6_PORT_PARSE("[11:22::88]:99",
1277 : "11:22::88",99);
1278 :
1279 1 : done:
1280 1 : unmock_hostname_resolver();
1281 1 : }
1282 :
1283 : static void
1284 4608 : update_difference(int ipv6, uint8_t *d,
1285 : const tor_addr_t *a, const tor_addr_t *b)
1286 : {
1287 4608 : const int n_bytes = ipv6 ? 16 : 4;
1288 4608 : uint8_t a_tmp[4], b_tmp[4];
1289 4608 : const uint8_t *ba, *bb;
1290 4608 : int i;
1291 :
1292 4608 : if (ipv6) {
1293 2304 : ba = tor_addr_to_in6_addr8(a);
1294 2304 : bb = tor_addr_to_in6_addr8(b);
1295 : } else {
1296 2304 : set_uint32(a_tmp, tor_addr_to_ipv4n(a));
1297 2304 : set_uint32(b_tmp, tor_addr_to_ipv4n(b));
1298 2304 : ba = a_tmp; bb = b_tmp;
1299 : }
1300 :
1301 50688 : for (i = 0; i < n_bytes; ++i) {
1302 46080 : d[i] |= ba[i] ^ bb[i];
1303 : }
1304 4608 : }
1305 :
1306 : static void
1307 1 : test_virtaddrmap(void *data)
1308 : {
1309 : /* Let's start with a bunch of random addresses. */
1310 1 : int ipv6, bits, iter, b;
1311 1 : virtual_addr_conf_t cfg[2];
1312 1 : uint8_t bytes[16];
1313 :
1314 1 : (void)data;
1315 :
1316 1 : tor_addr_parse(&cfg[0].addr, "64.65.0.0");
1317 1 : tor_addr_parse(&cfg[1].addr, "3491:c0c0::");
1318 :
1319 4 : for (ipv6 = 0; ipv6 <= 1; ++ipv6) {
1320 38 : for (bits = 0; bits < 18; ++bits) {
1321 36 : tor_addr_t last_a;
1322 36 : cfg[ipv6].bits = bits;
1323 36 : memset(bytes, 0, sizeof(bytes));
1324 36 : tor_addr_copy(&last_a, &cfg[ipv6].addr);
1325 : /* Generate 128 addresses with each addr/bits combination. */
1326 4680 : for (iter = 0; iter < 128; ++iter) {
1327 4608 : tor_addr_t a;
1328 :
1329 4608 : 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 4608 : tt_int_op(0, OP_EQ, tor_addr_compare_masked(&a, &cfg[ipv6].addr,
1333 : bits, CMP_EXACT));
1334 :
1335 : /* And track which bits have been different between pairs of
1336 : * addresses */
1337 4608 : update_difference(ipv6, bytes, &last_a, &a);
1338 : }
1339 :
1340 : /* Now make sure all but the first 'bits' bits of bytes are true */
1341 2997 : for (b = bits+1; b < (ipv6?128:32); ++b) {
1342 2538 : tt_assert(1 & (bytes[b/8] >> (7-(b&7))));
1343 : }
1344 : }
1345 : }
1346 :
1347 1 : done:
1348 1 : ;
1349 1 : }
1350 :
1351 : static void
1352 1 : test_virtaddrmap_persist(void *data)
1353 : {
1354 1 : (void)data;
1355 1 : const char *a, *b, *c;
1356 1 : tor_addr_t addr;
1357 1 : char *ones = NULL;
1358 1 : const char *canned_data;
1359 1 : size_t canned_data_len;
1360 :
1361 1 : addressmap_init();
1362 :
1363 : // Try a hostname.
1364 1 : a = addressmap_register_virtual_address(RESOLVED_TYPE_HOSTNAME,
1365 : tor_strdup("foobar.baz"));
1366 1 : tt_assert(a);
1367 1 : tt_assert(!strcmpend(a, ".virtual"));
1368 :
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
1373 : // reasonable.)
1374 1 : canned_data = "1234567890" // the first call returns this.
1375 : "1234567890" // the second call returns this.
1376 : "abcdefghij"; // the third call returns this.
1377 1 : canned_data_len = 30;
1378 1 : testing_enable_prefilled_rng(canned_data, canned_data_len);
1379 :
1380 1 : a = addressmap_register_virtual_address(RESOLVED_TYPE_HOSTNAME,
1381 : tor_strdup("quuxit.baz"));
1382 1 : b = addressmap_register_virtual_address(RESOLVED_TYPE_HOSTNAME,
1383 : tor_strdup("nescio.baz"));
1384 1 : tt_assert(a);
1385 1 : tt_assert(b);
1386 1 : tt_str_op(a, OP_EQ, "gezdgnbvgy3tqojq.virtual");
1387 1 : tt_str_op(b, OP_EQ, "mfrggzdfmztwq2lk.virtual");
1388 1 : testing_disable_prefilled_rng();
1389 :
1390 : // Now try something to get us an ipv4 address
1391 1 : tt_int_op(0,OP_EQ, parse_virtual_addr_network("192.168.0.0/16",
1392 : AF_INET, 0, NULL));
1393 1 : a = addressmap_register_virtual_address(RESOLVED_TYPE_IPV4,
1394 : tor_strdup("foobar.baz"));
1395 1 : tt_assert(a);
1396 1 : tt_assert(!strcmpstart(a, "192.168."));
1397 1 : tor_addr_parse(&addr, a);
1398 1 : tt_int_op(AF_INET, OP_EQ, tor_addr_family(&addr));
1399 :
1400 1 : b = addressmap_register_virtual_address(RESOLVED_TYPE_IPV4,
1401 : tor_strdup("quuxit.baz"));
1402 1 : tt_str_op(b, OP_NE, a);
1403 1 : tt_assert(!strcmpstart(b, "192.168."));
1404 :
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 1 : 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 1 : canned_data_len = 20;
1413 1 : testing_enable_prefilled_rng(canned_data, canned_data_len);
1414 :
1415 1 : a = addressmap_register_virtual_address(RESOLVED_TYPE_IPV4,
1416 : tor_strdup("wumble.onion"));
1417 1 : b = addressmap_register_virtual_address(RESOLVED_TYPE_IPV4,
1418 : tor_strdup("wumpus.onion"));
1419 1 : tt_str_op(a, OP_EQ, "192.168.3.4");
1420 1 : tt_str_op(b, OP_EQ, "192.168.7.240");
1421 1 : testing_disable_prefilled_rng();
1422 :
1423 : // Now try IPv6!
1424 1 : tt_int_op(0,OP_EQ, parse_virtual_addr_network("1010:F000::/20",
1425 : AF_INET6, 0, NULL));
1426 1 : a = addressmap_register_virtual_address(RESOLVED_TYPE_IPV6,
1427 : tor_strdup("foobar.baz"));
1428 1 : tt_assert(a);
1429 1 : tt_assert(!strcmpstart(a, "[1010:f"));
1430 1 : tor_addr_parse(&addr, a);
1431 1 : tt_int_op(AF_INET6, OP_EQ, tor_addr_family(&addr));
1432 :
1433 1 : b = addressmap_register_virtual_address(RESOLVED_TYPE_IPV6,
1434 : tor_strdup("quuxit.baz"));
1435 1 : tt_str_op(b, OP_NE, a);
1436 1 : tt_assert(!strcmpstart(b, "[1010:f"));
1437 :
1438 : // Try IPv6 with canned entropy, to make sure we detect duplicates.
1439 :
1440 1 : canned_data = "acanthopterygian" // okay
1441 : "cinematographist" // okay
1442 : "acanthopterygian" // duplicate
1443 : "acanthopterygian" // duplicate
1444 : "acanthopterygian" // duplicate
1445 : "cinematographist" // duplicate
1446 : "coadministration"; // okay
1447 1 : canned_data_len = 16 * 7;
1448 1 : testing_enable_prefilled_rng(canned_data, canned_data_len);
1449 :
1450 1 : a = addressmap_register_virtual_address(RESOLVED_TYPE_IPV6,
1451 : tor_strdup("wuffle.baz"));
1452 1 : b = addressmap_register_virtual_address(RESOLVED_TYPE_IPV6,
1453 : tor_strdup("gribble.baz"));
1454 1 : c = addressmap_register_virtual_address(RESOLVED_TYPE_IPV6,
1455 : tor_strdup("surprisingly-legible.baz"));
1456 1 : tt_str_op(a, OP_EQ, "[1010:f16e:7468:6f70:7465:7279:6769:616e]");
1457 1 : tt_str_op(b, OP_EQ, "[1010:fe65:6d61:746f:6772:6170:6869:7374]");
1458 1 : tt_str_op(c, OP_EQ, "[1010:f164:6d69:6e69:7374:7261:7469:6f6e]");
1459 :
1460 : // Try address exhaustion: make sure we can actually fail if we
1461 : // get too many already-existing addresses.
1462 1 : testing_disable_prefilled_rng();
1463 1 : canned_data_len = 128*1024;
1464 1 : canned_data = ones = tor_malloc(canned_data_len);
1465 1 : memset(ones, 1, canned_data_len);
1466 1 : 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 1 : a = addressmap_register_virtual_address(RESOLVED_TYPE_IPV4,
1470 : tor_strdup("might-work.onion"));
1471 1 : if (a) {
1472 1 : tt_str_op(a, OP_EQ, "192.168.1.1");
1473 : }
1474 1 : setup_capture_of_logs(LOG_WARN);
1475 : // This one will definitely fail, since we've set up the RNG to hand
1476 : // out "1" forever.
1477 1 : b = addressmap_register_virtual_address(RESOLVED_TYPE_IPV4,
1478 : tor_strdup("wont-work.onion"));
1479 1 : tt_assert(b == NULL);
1480 1 : expect_single_log_msg_containing("Ran out of virtual addresses!");
1481 :
1482 1 : done:
1483 1 : testing_disable_prefilled_rng();
1484 1 : tor_free(ones);
1485 1 : addressmap_free_all();
1486 1 : teardown_capture_of_logs();
1487 1 : }
1488 :
1489 : static void
1490 1 : test_addr_localname(void *arg)
1491 : {
1492 1 : (void)arg;
1493 1 : tt_assert(tor_addr_hostname_is_local("localhost"));
1494 1 : tt_assert(tor_addr_hostname_is_local("LOCALHOST"));
1495 1 : tt_assert(tor_addr_hostname_is_local("LocalHost"));
1496 1 : tt_assert(tor_addr_hostname_is_local("local"));
1497 1 : tt_assert(tor_addr_hostname_is_local("LOCAL"));
1498 1 : tt_assert(tor_addr_hostname_is_local("here.now.local"));
1499 1 : tt_assert(tor_addr_hostname_is_local("here.now.LOCAL"));
1500 :
1501 1 : tt_assert(!tor_addr_hostname_is_local(" localhost"));
1502 1 : tt_assert(!tor_addr_hostname_is_local("www.torproject.org"));
1503 1 : done:
1504 1 : ;
1505 1 : }
1506 :
1507 : static void
1508 1 : test_addr_dup_ip(void *arg)
1509 : {
1510 1 : char *v = NULL;
1511 1 : (void)arg;
1512 : #define CHECK(ip, s) do { \
1513 : v = tor_dup_ip(ip); \
1514 : tt_str_op(v,OP_EQ,(s)); \
1515 : tor_free(v); \
1516 : } while (0)
1517 :
1518 1 : CHECK(0xffffffff, "255.255.255.255");
1519 1 : CHECK(0x00000000, "0.0.0.0");
1520 1 : CHECK(0x7f000001, "127.0.0.1");
1521 1 : CHECK(0x01020304, "1.2.3.4");
1522 :
1523 : #undef CHECK
1524 1 : done:
1525 1 : tor_free(v);
1526 1 : }
1527 :
1528 : static void
1529 1 : test_addr_sockaddr_to_str(void *arg)
1530 : {
1531 1 : char *v = NULL;
1532 1 : struct sockaddr_in sin;
1533 1 : struct sockaddr_in6 sin6;
1534 1 : struct sockaddr_storage ss;
1535 : #ifdef HAVE_SYS_UN_H
1536 1 : struct sockaddr_un s_un;
1537 : #endif
1538 : #define CHECK(sa, s) do { \
1539 : v = tor_sockaddr_to_str((const struct sockaddr*) &(sa)); \
1540 : tt_str_op(v,OP_EQ,(s)); \
1541 : tor_free(v); \
1542 : } while (0)
1543 1 : (void)arg;
1544 :
1545 1 : memset(&ss,0,sizeof(ss));
1546 1 : ss.ss_family = AF_UNSPEC;
1547 1 : CHECK(ss, "unspec");
1548 :
1549 1 : memset(&sin,0,sizeof(sin));
1550 1 : sin.sin_family = AF_INET;
1551 1 : sin.sin_addr.s_addr = htonl(0x7f808001);
1552 1 : sin.sin_port = htons(1234);
1553 1 : CHECK(sin, "127.128.128.1:1234");
1554 :
1555 : #ifdef HAVE_SYS_UN_H
1556 1 : memset(&s_un,0,sizeof(s_un));
1557 1 : s_un.sun_family = AF_UNIX;
1558 1 : strlcpy(s_un.sun_path, "/here/is/a/path", sizeof(s_un.sun_path));
1559 1 : CHECK(s_un, "unix:/here/is/a/path");
1560 : #endif /* defined(HAVE_SYS_UN_H) */
1561 :
1562 1 : memset(&sin6,0,sizeof(sin6));
1563 1 : sin6.sin6_family = AF_INET6;
1564 1 : 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 1 : sin6.sin6_port = htons(1234);
1567 1 : CHECK(sin6, "[2000::1a:2b3c:4d5e:1]:1234");
1568 :
1569 1 : done:
1570 1 : tor_free(v);
1571 1 : }
1572 :
1573 : static void
1574 1 : test_addr_is_loopback(void *data)
1575 : {
1576 1 : static const struct loopback_item {
1577 : const char *name;
1578 : int is_loopback;
1579 : } loopback_items[] = {
1580 : { "::1", 1 },
1581 : { "127.0.0.1", 1 },
1582 : { "127.99.100.101", 1 },
1583 : { "128.99.100.101", 0 },
1584 : { "8.8.8.8", 0 },
1585 : { "0.0.0.0", 0 },
1586 : { "::2", 0 },
1587 : { "::", 0 },
1588 : { "::1.0.0.0", 0 },
1589 : { NULL, 0 }
1590 : };
1591 :
1592 1 : int i;
1593 1 : tor_addr_t addr;
1594 1 : (void)data;
1595 :
1596 10 : for (i=0; loopback_items[i].name; ++i) {
1597 9 : tt_int_op(tor_addr_parse(&addr, loopback_items[i].name), OP_GE, 0);
1598 9 : tt_int_op(tor_addr_is_loopback(&addr), OP_EQ,
1599 : loopback_items[i].is_loopback);
1600 : }
1601 :
1602 1 : tor_addr_make_unspec(&addr);
1603 1 : tt_int_op(tor_addr_is_loopback(&addr), OP_EQ, 0);
1604 :
1605 1 : done:
1606 1 : ;
1607 1 : }
1608 :
1609 : static void
1610 1 : test_addr_make_null(void *data)
1611 : {
1612 1 : tor_addr_t *addr = tor_malloc(sizeof(*addr));
1613 1 : tor_addr_t *zeros = tor_malloc_zero(sizeof(*addr));
1614 1 : char buf[TOR_ADDR_BUF_LEN];
1615 1 : (void) data;
1616 : /* Ensure that before tor_addr_make_null, addr != 0's */
1617 1 : memset(addr, 1, sizeof(*addr));
1618 1 : tt_int_op(fast_memcmp(addr, zeros, sizeof(*addr)), OP_NE, 0);
1619 : /* Test with AF == AF_INET */
1620 1 : zeros->family = AF_INET;
1621 1 : tor_addr_make_null(addr, AF_INET);
1622 1 : tt_int_op(fast_memcmp(addr, zeros, sizeof(*addr)), OP_EQ, 0);
1623 1 : 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 1 : memset(addr, 1, sizeof(*addr));
1626 1 : zeros->family = AF_INET6;
1627 1 : tor_addr_make_null(addr, AF_INET6);
1628 1 : tt_int_op(fast_memcmp(addr, zeros, sizeof(*addr)), OP_EQ, 0);
1629 1 : tt_str_op(tor_addr_to_str(buf, addr, sizeof(buf), 0), OP_EQ, "::");
1630 1 : done:
1631 1 : tor_free(addr);
1632 1 : tor_free(zeros);
1633 1 : }
1634 :
1635 : #define TEST_ADDR_INTERNAL(a, for_listening, rv) STMT_BEGIN \
1636 : tor_addr_t t; \
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); \
1640 : STMT_END;
1641 :
1642 : static void
1643 1 : test_addr_rfc6598(void *arg)
1644 : {
1645 1 : (void)arg;
1646 1 : TEST_ADDR_INTERNAL("100.64.0.1", 0, 1);
1647 1 : TEST_ADDR_INTERNAL("100.64.0.1", 1, 0);
1648 1 : done:
1649 1 : ;
1650 1 : }
1651 :
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); \
1655 : STMT_END;
1656 :
1657 : static void
1658 1 : test_addr_octal(void *arg)
1659 : {
1660 1 : (void)arg;
1661 :
1662 : /* Test non-octal IP addresses. */
1663 1 : TEST_ADDR_ATON("0.1.2.3", 1);
1664 1 : TEST_ADDR_ATON("1.0.2.3", 1);
1665 1 : TEST_ADDR_ATON("1.2.3.0", 1);
1666 :
1667 : /* Test octal IP addresses. */
1668 1 : TEST_ADDR_ATON("01.1.2.3", 0);
1669 1 : TEST_ADDR_ATON("1.02.3.4", 0);
1670 1 : TEST_ADDR_ATON("1.2.3.04", 0);
1671 1 : done:
1672 1 : ;
1673 1 : }
1674 :
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); \
1679 : tor_assert(iprv); \
1680 : STMT_END;
1681 :
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); \
1686 : tor_assert(iprv); \
1687 : STMT_END;
1688 :
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; \
1692 : STMT_END;
1693 :
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; \
1697 : STMT_END;
1698 :
1699 : #define TEST_ADDR_VALIDITY(a, lis, rv) STMT_BEGIN \
1700 : tor_assert(a); \
1701 : tt_int_op(tor_addr_is_valid(a, lis), OP_EQ, rv); \
1702 : STMT_END;
1703 :
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::"
1707 :
1708 : static void
1709 1 : test_addr_is_valid(void *arg)
1710 : {
1711 1 : (void)arg;
1712 1 : tor_addr_t *test_addr;
1713 1 : int iprv;
1714 :
1715 : /* Tests for IPv4 addresses. */
1716 :
1717 : /* Test for null IPv4 address. */
1718 1 : get_ipv4(test_addr, "0.0.0.0", iprv);
1719 1 : TEST_ADDR_VALIDITY(test_addr, 0, 0);
1720 1 : TEST_ADDR_VALIDITY(test_addr, 1, 1);
1721 1 : tor_free(test_addr);
1722 :
1723 : /* Test for non-null IPv4 address. */
1724 1 : get_ipv4(test_addr, IP4_TEST_ADDR, iprv);
1725 1 : TEST_ADDR_VALIDITY(test_addr, 0, 1);
1726 1 : TEST_ADDR_VALIDITY(test_addr, 1, 1);
1727 1 : tor_free(test_addr);
1728 :
1729 : /* Tests for IPv6 addresses. */
1730 :
1731 : /* Test for null IPv6 address. */
1732 1 : get_ipv6(test_addr, "::", iprv);
1733 1 : TEST_ADDR_VALIDITY(test_addr, 0, 0);
1734 1 : TEST_ADDR_VALIDITY(test_addr, 1, 1);
1735 1 : tor_free(test_addr);
1736 :
1737 : /* Test for non-null IPv6 address. */
1738 1 : get_ipv6(test_addr, IP6_TEST_ADDR, iprv);
1739 1 : TEST_ADDR_VALIDITY(test_addr, 0, 1);
1740 1 : TEST_ADDR_VALIDITY(test_addr, 1, 1);
1741 1 : tor_free(test_addr);
1742 :
1743 : /* Test for address of type AF_UNIX. */
1744 :
1745 1 : get_af_unix(test_addr);
1746 1 : TEST_ADDR_VALIDITY(test_addr, 0, 0);
1747 1 : TEST_ADDR_VALIDITY(test_addr, 1, 0);
1748 1 : tor_free(test_addr);
1749 :
1750 : /* Test for address of type AF_UNSPEC. */
1751 :
1752 1 : get_af_unspec(test_addr);
1753 1 : TEST_ADDR_VALIDITY(test_addr, 0, 0);
1754 1 : TEST_ADDR_VALIDITY(test_addr, 1, 0);
1755 :
1756 1 : done:
1757 1 : tor_free(test_addr);
1758 1 : }
1759 :
1760 : #define TEST_ADDR_IS_NULL(a, rv) STMT_BEGIN \
1761 : tor_assert(a); \
1762 : tt_int_op(tor_addr_is_null(a), OP_EQ, rv); \
1763 : STMT_END;
1764 :
1765 : static void
1766 1 : test_addr_is_null(void *arg)
1767 : {
1768 1 : (void)arg;
1769 1 : tor_addr_t *test_addr;
1770 1 : int iprv;
1771 :
1772 : /* Test for null IPv4. */
1773 1 : get_ipv4(test_addr, "0.0.0.0", iprv);
1774 1 : TEST_ADDR_IS_NULL(test_addr, 1);
1775 1 : tor_free(test_addr);
1776 :
1777 : /* Test for non-null IPv4. */
1778 1 : get_ipv4(test_addr, IP4_TEST_ADDR, iprv);
1779 1 : TEST_ADDR_IS_NULL(test_addr, 0);
1780 1 : tor_free(test_addr);
1781 :
1782 : /* Test for null IPv6. */
1783 1 : get_ipv6(test_addr, "::", iprv);
1784 1 : TEST_ADDR_IS_NULL(test_addr, 1);
1785 1 : tor_free(test_addr);
1786 :
1787 : /* Test for non-null IPv6. */
1788 1 : get_ipv6(test_addr, IP6_TEST_ADDR, iprv);
1789 1 : TEST_ADDR_IS_NULL(test_addr, 0);
1790 1 : tor_free(test_addr);
1791 :
1792 : /* Test for address family AF_UNIX. */
1793 1 : get_af_unix(test_addr);
1794 1 : TEST_ADDR_IS_NULL(test_addr, 1);
1795 1 : tor_free(test_addr);
1796 :
1797 : /* Test for address family AF_UNSPEC. */
1798 1 : get_af_unspec(test_addr);
1799 1 : TEST_ADDR_IS_NULL(test_addr, 1);
1800 :
1801 1 : done:
1802 1 : tor_free(test_addr);
1803 1 : }
1804 :
1805 : #ifndef COCCI
1806 : #define ADDR_LEGACY(name) \
1807 : { #name, test_addr_ ## name , 0, NULL, NULL }
1808 : #endif
1809 :
1810 : struct testcase_t addr_tests[] = {
1811 : ADDR_LEGACY(basic),
1812 : ADDR_LEGACY(ip6_helpers),
1813 : ADDR_LEGACY(parse),
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 },
1826 : END_OF_TESTCASES
1827 : };
|