Prop210: Refactor connection_get_* to produce lists and counts
[tor.git] / src / test / test_address.c
blob9f3d81c92c5cf11da27a64e8fb1b5c470a369cc8
1 /* Copyright (c) 2014-2015, The Tor Project, Inc. */
2 /* See LICENSE for licensing information */
4 #define ADDRESS_PRIVATE
6 #include "orconfig.h"
8 #ifdef _WIN32
9 #include <winsock2.h>
10 /* For access to structs needed by GetAdaptersAddresses */
11 #include <iphlpapi.h>
12 #endif
14 #ifdef HAVE_IFADDRS_TO_SMARTLIST
15 #include <net/if.h>
16 #include <ifaddrs.h>
17 #endif
19 #ifdef HAVE_IFCONF_TO_SMARTLIST
20 #ifdef HAVE_SYS_IOCTL_H
21 #include <sys/ioctl.h>
22 #endif
23 #include <net/if.h>
24 #endif
26 #include "or.h"
27 #include "address.h"
28 #include "test.h"
30 /** Return 1 iff <b>sockaddr1</b> and <b>sockaddr2</b> represent
31 * the same IP address and port combination. Otherwise, return 0.
33 static uint8_t
34 sockaddr_in_are_equal(struct sockaddr_in *sockaddr1,
35 struct sockaddr_in *sockaddr2)
37 return ((sockaddr1->sin_family == sockaddr2->sin_family) &&
38 (sockaddr1->sin_port == sockaddr2->sin_port) &&
39 (sockaddr1->sin_addr.s_addr == sockaddr2->sin_addr.s_addr));
42 /** Return 1 iff <b>sockaddr1</b> and <b>sockaddr2</b> represent
43 * the same IP address and port combination. Otherwise, return 0.
45 static uint8_t
46 sockaddr_in6_are_equal(struct sockaddr_in6 *sockaddr1,
47 struct sockaddr_in6 *sockaddr2)
49 return ((sockaddr1->sin6_family == sockaddr2->sin6_family) &&
50 (sockaddr1->sin6_port == sockaddr2->sin6_port) &&
51 (tor_memeq(sockaddr1->sin6_addr.s6_addr,
52 sockaddr2->sin6_addr.s6_addr,16)));
55 /** Create a sockaddr_in structure from IP address string <b>ip_str</b>.
57 * If <b>out</b> is not NULL, write the result
58 * to the memory address in <b>out</b>. Otherwise, allocate the memory
59 * for result. On success, return pointer to result. Otherwise, return
60 * NULL.
62 static struct sockaddr_in *
63 sockaddr_in_from_string(const char *ip_str, struct sockaddr_in *out)
65 // [FIXME: add some error checking?]
66 if (!out)
67 out = tor_malloc_zero(sizeof(struct sockaddr_in));
69 out->sin_family = AF_INET;
70 out->sin_port = 0;
71 tor_inet_pton(AF_INET,ip_str,&(out->sin_addr));
73 return out;
76 /** Return 1 iff <b>smartlist</b> contains a tor_addr_t structure
77 * that is an IPv4 or IPv6 localhost address. Otherwise, return 0.
79 static int
80 smartlist_contains_localhost_tor_addr(smartlist_t *smartlist)
82 SMARTLIST_FOREACH_BEGIN(smartlist, tor_addr_t *, tor_addr) {
83 if (tor_addr_is_loopback(tor_addr)) {
84 return 1;
86 } SMARTLIST_FOREACH_END(tor_addr);
88 return 0;
91 /** Return 1 iff <b>smartlist</b> contains a tor_addr_t structure
92 * that is an IPv4 or IPv6 multicast address. Otherwise, return 0.
94 static int
95 smartlist_contains_multicast_tor_addr(smartlist_t *smartlist)
97 SMARTLIST_FOREACH_BEGIN(smartlist, tor_addr_t *, tor_addr) {
98 if (tor_addr_is_multicast(tor_addr)) {
99 return 1;
101 } SMARTLIST_FOREACH_END(tor_addr);
103 return 0;
106 /** Return 1 iff <b>smartlist</b> contains a tor_addr_t structure
107 * that is an IPv4 or IPv6 internal address. Otherwise, return 0.
109 static int
110 smartlist_contains_internal_tor_addr(smartlist_t *smartlist)
112 SMARTLIST_FOREACH_BEGIN(smartlist, tor_addr_t *, tor_addr) {
113 if (tor_addr_is_internal(tor_addr, 0)) {
114 return 1;
116 } SMARTLIST_FOREACH_END(tor_addr);
118 return 0;
121 /** Return 1 iff <b>smartlist</b> contains a tor_addr_t structure
122 * that is NULL or the null tor_addr_t. Otherwise, return 0.
124 static int
125 smartlist_contains_null_tor_addr(smartlist_t *smartlist)
127 SMARTLIST_FOREACH_BEGIN(smartlist, tor_addr_t *, tor_addr) {
128 if (tor_addr == NULL || tor_addr_is_null(tor_addr)) {
129 return 1;
131 } SMARTLIST_FOREACH_END(tor_addr);
133 return 0;
136 /** Return 1 iff <b>smartlist</b> contains a tor_addr_t structure
137 * that is an IPv4 address. Otherwise, return 0.
139 static int
140 smartlist_contains_ipv4_tor_addr(smartlist_t *smartlist)
142 SMARTLIST_FOREACH_BEGIN(smartlist, tor_addr_t *, tor_addr) {
143 if (tor_addr_is_v4(tor_addr)) {
144 return 1;
146 } SMARTLIST_FOREACH_END(tor_addr);
148 return 0;
151 /** Return 1 iff <b>smartlist</b> contains a tor_addr_t structure
152 * that is an IPv6 address. Otherwise, return 0.
154 static int
155 smartlist_contains_ipv6_tor_addr(smartlist_t *smartlist)
157 SMARTLIST_FOREACH_BEGIN(smartlist, tor_addr_t *, tor_addr) {
158 /* Since there's no tor_addr_is_v6, assume all non-v4s are v6 */
159 if (!tor_addr_is_v4(tor_addr)) {
160 return 1;
162 } SMARTLIST_FOREACH_END(tor_addr);
164 return 0;
167 #ifdef HAVE_IFADDRS_TO_SMARTLIST
168 static void
169 test_address_ifaddrs_to_smartlist(void *arg)
171 struct ifaddrs *ifa = NULL;
172 struct ifaddrs *ifa_ipv4 = NULL;
173 struct ifaddrs *ifa_ipv6 = NULL;
174 struct sockaddr_in *ipv4_sockaddr_local = NULL;
175 struct sockaddr_in *netmask_slash8 = NULL;
176 struct sockaddr_in *ipv4_sockaddr_remote = NULL;
177 struct sockaddr_in6 *ipv6_sockaddr = NULL;
178 smartlist_t *smartlist = NULL;
179 tor_addr_t *tor_addr = NULL;
180 struct sockaddr *sockaddr_to_check = NULL;
181 socklen_t addr_len;
183 (void)arg;
185 netmask_slash8 = sockaddr_in_from_string("255.0.0.0",NULL);
186 ipv4_sockaddr_local = sockaddr_in_from_string("127.0.0.1",NULL);
187 ipv4_sockaddr_remote = sockaddr_in_from_string("128.52.160.20",NULL);
189 ipv6_sockaddr = tor_malloc(sizeof(struct sockaddr_in6));
190 ipv6_sockaddr->sin6_family = AF_INET6;
191 ipv6_sockaddr->sin6_port = 0;
192 tor_inet_pton(AF_INET6, "2001:db8:8714:3a90::12",
193 &(ipv6_sockaddr->sin6_addr));
195 ifa = tor_malloc(sizeof(struct ifaddrs));
196 ifa_ipv4 = tor_malloc(sizeof(struct ifaddrs));
197 ifa_ipv6 = tor_malloc(sizeof(struct ifaddrs));
199 ifa->ifa_next = ifa_ipv4;
200 ifa->ifa_name = tor_strdup("eth0");
201 ifa->ifa_flags = IFF_UP | IFF_RUNNING;
202 ifa->ifa_addr = (struct sockaddr *)ipv4_sockaddr_local;
203 ifa->ifa_netmask = (struct sockaddr *)netmask_slash8;
204 ifa->ifa_dstaddr = NULL;
205 ifa->ifa_data = NULL;
207 ifa_ipv4->ifa_next = ifa_ipv6;
208 ifa_ipv4->ifa_name = tor_strdup("eth1");
209 ifa_ipv4->ifa_flags = IFF_UP | IFF_RUNNING;
210 ifa_ipv4->ifa_addr = (struct sockaddr *)ipv4_sockaddr_remote;
211 ifa_ipv4->ifa_netmask = (struct sockaddr *)netmask_slash8;
212 ifa_ipv4->ifa_dstaddr = NULL;
213 ifa_ipv4->ifa_data = NULL;
215 ifa_ipv6->ifa_next = NULL;
216 ifa_ipv6->ifa_name = tor_strdup("eth2");
217 ifa_ipv6->ifa_flags = IFF_UP | IFF_RUNNING;
218 ifa_ipv6->ifa_addr = (struct sockaddr *)ipv6_sockaddr;
219 ifa_ipv6->ifa_netmask = NULL;
220 ifa_ipv6->ifa_dstaddr = NULL;
221 ifa_ipv6->ifa_data = NULL;
223 smartlist = ifaddrs_to_smartlist(ifa);
225 tt_assert(smartlist);
226 tt_assert(smartlist_len(smartlist) == 3);
228 sockaddr_to_check = tor_malloc(sizeof(struct sockaddr_in6));
230 tor_addr = smartlist_get(smartlist,0);
231 addr_len =
232 tor_addr_to_sockaddr(tor_addr,0,sockaddr_to_check,
233 sizeof(struct sockaddr_in));
235 tt_int_op(addr_len,==,sizeof(struct sockaddr_in));
236 tt_assert(sockaddr_in_are_equal((struct sockaddr_in *)sockaddr_to_check,
237 ipv4_sockaddr_local));
239 tor_addr = smartlist_get(smartlist,1);
240 addr_len =
241 tor_addr_to_sockaddr(tor_addr,0,sockaddr_to_check,
242 sizeof(struct sockaddr_in));
244 tt_int_op(addr_len,==,sizeof(struct sockaddr_in));
245 tt_assert(sockaddr_in_are_equal((struct sockaddr_in *)sockaddr_to_check,
246 ipv4_sockaddr_remote));
248 tor_addr = smartlist_get(smartlist,2);
249 addr_len =
250 tor_addr_to_sockaddr(tor_addr,0,sockaddr_to_check,
251 sizeof(struct sockaddr_in6));
253 tt_int_op(addr_len,==,sizeof(struct sockaddr_in6));
254 tt_assert(sockaddr_in6_are_equal((struct sockaddr_in6*)sockaddr_to_check,
255 ipv6_sockaddr));
257 done:
258 tor_free(netmask_slash8);
259 tor_free(ipv4_sockaddr_local);
260 tor_free(ipv4_sockaddr_remote);
261 tor_free(ipv6_sockaddr);
262 tor_free(ifa->ifa_name);
263 tor_free(ifa_ipv4->ifa_name);
264 tor_free(ifa_ipv6->ifa_name);
265 tor_free(ifa);
266 tor_free(ifa_ipv4);
267 tor_free(ifa_ipv6);
268 tor_free(sockaddr_to_check);
269 if (smartlist) {
270 SMARTLIST_FOREACH(smartlist, tor_addr_t *, t, tor_free(t));
271 smartlist_free(smartlist);
273 return;
276 static void
277 test_address_get_if_addrs_ifaddrs(void *arg)
280 smartlist_t *results = NULL;
282 (void)arg;
284 results = get_interface_addresses_ifaddrs(LOG_ERR);
286 tt_assert(results);
287 /* Some FreeBSD jails don't have localhost IP address. Instead, they only
288 * have the address assigned to the jail (whatever that may be).
289 * And a jail without a network connection might not have any addresses at
290 * all. */
291 tt_assert(!smartlist_contains_null_tor_addr(results));
293 /* If there are addresses, they must be IPv4 or IPv6 */
294 if (smartlist_len(results) > 0) {
295 tt_assert(smartlist_contains_ipv4_tor_addr(results)
296 || smartlist_contains_ipv6_tor_addr(results));
299 done:
300 SMARTLIST_FOREACH(results, tor_addr_t *, t, tor_free(t));
301 smartlist_free(results);
302 return;
305 #endif
307 #ifdef HAVE_IP_ADAPTER_TO_SMARTLIST
309 static void
310 test_address_get_if_addrs_win32(void *arg)
313 smartlist_t *results = NULL;
315 (void)arg;
317 results = get_interface_addresses_win32(LOG_ERR);
319 tt_int_op(smartlist_len(results),>=,1);
320 tt_assert(smartlist_contains_localhost_tor_addr(results));
321 tt_assert(!smartlist_contains_null_tor_addr(results));
323 /* If there are addresses, they must be IPv4 or IPv6 */
324 if (smartlist_len(results) > 0) {
325 tt_assert(smartlist_contains_ipv4_tor_addr(results)
326 || smartlist_contains_ipv6_tor_addr(results));
329 done:
330 SMARTLIST_FOREACH(results, tor_addr_t *, t, tor_free(t));
331 tor_free(results);
332 return;
335 static void
336 test_address_ip_adapter_addresses_to_smartlist(void *arg)
339 IP_ADAPTER_ADDRESSES *addrs1;
340 IP_ADAPTER_ADDRESSES *addrs2;
342 IP_ADAPTER_UNICAST_ADDRESS *unicast11;
343 IP_ADAPTER_UNICAST_ADDRESS *unicast12;
344 IP_ADAPTER_UNICAST_ADDRESS *unicast21;
346 smartlist_t *result = NULL;
348 struct sockaddr_in *sockaddr_test1;
349 struct sockaddr_in *sockaddr_test2;
350 struct sockaddr_in *sockaddr_localhost;
351 struct sockaddr_in *sockaddr_to_check;
353 tor_addr_t *tor_addr;
355 (void)arg;
356 (void)sockaddr_in6_are_equal;
358 sockaddr_to_check = tor_malloc_zero(sizeof(struct sockaddr_in));
360 addrs1 =
361 tor_malloc_zero(sizeof(IP_ADAPTER_ADDRESSES));
363 addrs1->FirstUnicastAddress =
364 unicast11 = tor_malloc_zero(sizeof(IP_ADAPTER_UNICAST_ADDRESS));
365 sockaddr_test1 = sockaddr_in_from_string("86.59.30.40",NULL);
366 unicast11->Address.lpSockaddr = (LPSOCKADDR)sockaddr_test1;
368 unicast11->Next = unicast12 =
369 tor_malloc_zero(sizeof(IP_ADAPTER_UNICAST_ADDRESS));
370 sockaddr_test2 = sockaddr_in_from_string("93.95.227.222", NULL);
371 unicast12->Address.lpSockaddr = (LPSOCKADDR)sockaddr_test2;
373 addrs1->Next = addrs2 =
374 tor_malloc_zero(sizeof(IP_ADAPTER_ADDRESSES));
376 addrs2->FirstUnicastAddress =
377 unicast21 = tor_malloc_zero(sizeof(IP_ADAPTER_UNICAST_ADDRESS));
378 sockaddr_localhost = sockaddr_in_from_string("127.0.0.1", NULL);
379 unicast21->Address.lpSockaddr = (LPSOCKADDR)sockaddr_localhost;
381 result = ip_adapter_addresses_to_smartlist(addrs1);
383 tt_assert(result);
384 tt_assert(smartlist_len(result) == 3);
386 tor_addr = smartlist_get(result,0);
388 tor_addr_to_sockaddr(tor_addr,0,(struct sockaddr *)sockaddr_to_check,
389 sizeof(struct sockaddr_in));
391 tt_assert(sockaddr_in_are_equal(sockaddr_test1,sockaddr_to_check));
393 tor_addr = smartlist_get(result,1);
395 tor_addr_to_sockaddr(tor_addr,0,(struct sockaddr *)sockaddr_to_check,
396 sizeof(struct sockaddr_in));
398 tt_assert(sockaddr_in_are_equal(sockaddr_test2,sockaddr_to_check));
400 tor_addr = smartlist_get(result,2);
402 tor_addr_to_sockaddr(tor_addr,0,(struct sockaddr *)sockaddr_to_check,
403 sizeof(struct sockaddr_in));
405 tt_assert(sockaddr_in_are_equal(sockaddr_localhost,sockaddr_to_check));
407 done:
408 SMARTLIST_FOREACH(result, tor_addr_t *, t, tor_free(t));
409 smartlist_free(result);
410 tor_free(addrs1);
411 tor_free(addrs2);
412 tor_free(unicast11->Address.lpSockaddr);
413 tor_free(unicast11);
414 tor_free(unicast12->Address.lpSockaddr);
415 tor_free(unicast12);
416 tor_free(unicast21->Address.lpSockaddr);
417 tor_free(unicast21);
418 tor_free(sockaddr_to_check);
419 return;
421 #endif
423 #ifdef HAVE_IFCONF_TO_SMARTLIST
425 static void
426 test_address_ifreq_to_smartlist(void *arg)
428 smartlist_t *results = NULL;
429 const tor_addr_t *tor_addr = NULL;
430 struct sockaddr_in *sockaddr = NULL;
431 struct sockaddr_in *sockaddr_eth1 = NULL;
432 struct sockaddr_in *sockaddr_to_check = NULL;
434 struct ifconf *ifc;
435 struct ifreq *ifr;
436 struct ifreq *ifr_next;
438 socklen_t addr_len;
440 (void)arg;
442 sockaddr_to_check = tor_malloc(sizeof(struct sockaddr_in));
444 ifr = tor_malloc(sizeof(struct ifreq));
445 memset(ifr,0,sizeof(struct ifreq));
446 strlcpy(ifr->ifr_name,"lo",3);
447 sockaddr = (struct sockaddr_in *) &(ifr->ifr_ifru.ifru_addr);
448 sockaddr_in_from_string("127.0.0.1",sockaddr);
450 ifc = tor_malloc(sizeof(struct ifconf));
451 memset(ifc,0,sizeof(struct ifconf));
452 ifc->ifc_len = sizeof(struct ifreq);
453 ifc->ifc_ifcu.ifcu_req = ifr;
455 results = ifreq_to_smartlist(ifc->ifc_buf,ifc->ifc_len);
456 tt_int_op(smartlist_len(results),==,1);
458 tor_addr = smartlist_get(results, 0);
459 addr_len =
460 tor_addr_to_sockaddr(tor_addr,0,(struct sockaddr *)sockaddr_to_check,
461 sizeof(struct sockaddr_in));
463 tt_int_op(addr_len,==,sizeof(struct sockaddr_in));
464 tt_assert(sockaddr_in_are_equal(sockaddr,sockaddr_to_check));
466 ifr = tor_realloc(ifr,2*sizeof(struct ifreq));
467 ifr_next = ifr+1;
468 strlcpy(ifr_next->ifr_name,"eth1",5);
469 ifc->ifc_len = 2*sizeof(struct ifreq);
470 ifc->ifc_ifcu.ifcu_req = ifr;
471 sockaddr = (struct sockaddr_in *) &(ifr->ifr_ifru.ifru_addr);
473 sockaddr_eth1 = (struct sockaddr_in *) &(ifr_next->ifr_ifru.ifru_addr);
474 sockaddr_in_from_string("192.168.10.55",sockaddr_eth1);
475 SMARTLIST_FOREACH(results, tor_addr_t *, t, tor_free(t));
476 smartlist_free(results);
478 results = ifreq_to_smartlist(ifc->ifc_buf,ifc->ifc_len);
479 tt_int_op(smartlist_len(results),==,2);
481 tor_addr = smartlist_get(results, 0);
482 addr_len =
483 tor_addr_to_sockaddr(tor_addr,0,(struct sockaddr *)sockaddr_to_check,
484 sizeof(struct sockaddr_in));
486 tt_int_op(addr_len,==,sizeof(struct sockaddr_in));
487 tt_assert(sockaddr_in_are_equal(sockaddr,sockaddr_to_check));
489 tor_addr = smartlist_get(results, 1);
490 addr_len =
491 tor_addr_to_sockaddr(tor_addr,0,(struct sockaddr *)sockaddr_to_check,
492 sizeof(struct sockaddr_in));
494 tt_int_op(addr_len,==,sizeof(struct sockaddr_in));
495 tt_assert(sockaddr_in_are_equal(sockaddr_eth1,sockaddr_to_check));
497 done:
498 tor_free(sockaddr_to_check);
499 SMARTLIST_FOREACH(results, tor_addr_t *, t, tor_free(t));
500 smartlist_free(results);
501 tor_free(ifc);
502 tor_free(ifr);
503 return;
506 static void
507 test_address_get_if_addrs_ioctl(void *arg)
510 smartlist_t *result = NULL;
512 (void)arg;
514 result = get_interface_addresses_ioctl(LOG_ERR);
516 /* On an IPv6-only system, this will fail and return NULL
517 tt_assert(result);
520 /* Some FreeBSD jails don't have localhost IP address. Instead, they only
521 * have the address assigned to the jail (whatever that may be).
522 * And a jail without a network connection might not have any addresses at
523 * all. */
524 if (result) {
525 tt_assert(!smartlist_contains_null_tor_addr(result));
527 /* If there are addresses, they must be IPv4 or IPv6.
528 * (AIX supports IPv6 from SIOCGIFCONF.) */
529 if (smartlist_len(result) > 0) {
530 tt_assert(smartlist_contains_ipv4_tor_addr(result)
531 || smartlist_contains_ipv6_tor_addr(result));
535 done:
536 if (result) {
537 SMARTLIST_FOREACH(result, tor_addr_t *, t, tor_free(t));
538 smartlist_free(result);
540 return;
543 #endif
545 #define FAKE_SOCKET_FD (42)
547 static tor_socket_t
548 fake_open_socket(int domain, int type, int protocol)
550 (void)domain;
551 (void)type;
552 (void)protocol;
554 return FAKE_SOCKET_FD;
557 static int last_connected_socket_fd = 0;
559 static int connect_retval = 0;
561 static tor_socket_t
562 pretend_to_connect(tor_socket_t socket, const struct sockaddr *address,
563 socklen_t address_len)
565 (void)address;
566 (void)address_len;
568 last_connected_socket_fd = socket;
570 return connect_retval;
573 static struct sockaddr *mock_addr = NULL;
575 static int
576 fake_getsockname(tor_socket_t socket, struct sockaddr *address,
577 socklen_t *address_len)
579 socklen_t bytes_to_copy = 0;
580 (void) socket;
582 if (!mock_addr)
583 return -1;
585 if (mock_addr->sa_family == AF_INET) {
586 bytes_to_copy = sizeof(struct sockaddr_in);
587 } else if (mock_addr->sa_family == AF_INET6) {
588 bytes_to_copy = sizeof(struct sockaddr_in6);
589 } else {
590 return -1;
593 if (*address_len < bytes_to_copy) {
594 return -1;
597 memcpy(address,mock_addr,bytes_to_copy);
598 *address_len = bytes_to_copy;
600 return 0;
603 static void
604 test_address_udp_socket_trick_whitebox(void *arg)
606 int hack_retval;
607 tor_addr_t *addr_from_hack = tor_malloc_zero(sizeof(tor_addr_t));
608 struct sockaddr_in6 *mock_addr6;
609 struct sockaddr_in6 *ipv6_to_check =
610 tor_malloc_zero(sizeof(struct sockaddr_in6));
612 (void)arg;
614 MOCK(tor_open_socket,fake_open_socket);
615 MOCK(tor_connect_socket,pretend_to_connect);
616 MOCK(tor_getsockname,fake_getsockname);
618 mock_addr = tor_malloc_zero(sizeof(struct sockaddr_storage));
619 sockaddr_in_from_string("23.32.246.118",(struct sockaddr_in *)mock_addr);
621 hack_retval =
622 get_interface_address6_via_udp_socket_hack(LOG_DEBUG,
623 AF_INET, addr_from_hack);
625 tt_int_op(hack_retval,==,0);
626 tt_assert(tor_addr_eq_ipv4h(addr_from_hack, 0x1720f676));
628 /* Now, lets do an IPv6 case. */
629 memset(mock_addr,0,sizeof(struct sockaddr_storage));
631 mock_addr6 = (struct sockaddr_in6 *)mock_addr;
632 mock_addr6->sin6_family = AF_INET6;
633 mock_addr6->sin6_port = 0;
634 tor_inet_pton(AF_INET6,"2001:cdba::3257:9652",&(mock_addr6->sin6_addr));
636 hack_retval =
637 get_interface_address6_via_udp_socket_hack(LOG_DEBUG,
638 AF_INET6, addr_from_hack);
640 tt_int_op(hack_retval,==,0);
642 tor_addr_to_sockaddr(addr_from_hack,0,(struct sockaddr *)ipv6_to_check,
643 sizeof(struct sockaddr_in6));
645 tt_assert(sockaddr_in6_are_equal(mock_addr6,ipv6_to_check));
647 UNMOCK(tor_open_socket);
648 UNMOCK(tor_connect_socket);
649 UNMOCK(tor_getsockname);
651 done:
652 tor_free(ipv6_to_check);
653 tor_free(mock_addr);
654 tor_free(addr_from_hack);
655 return;
658 static void
659 test_address_udp_socket_trick_blackbox(void *arg)
661 /* We want get_interface_address6_via_udp_socket_hack() to yield
662 * the same valid address that get_interface_address6() returns.
663 * If the latter is unable to find a valid address, we want
664 * _hack() to fail and return-1.
666 * Furthermore, we want _hack() never to crash, even if
667 * get_interface_addresses_raw() is returning NULL.
670 tor_addr_t addr4;
671 tor_addr_t addr4_to_check;
672 tor_addr_t addr6;
673 tor_addr_t addr6_to_check;
674 int retval, retval_reference;
676 (void)arg;
678 #if 0
679 retval_reference = get_interface_address6(LOG_DEBUG,AF_INET,&addr4);
680 retval = get_interface_address6_via_udp_socket_hack(LOG_DEBUG,
681 AF_INET,
682 &addr4_to_check);
684 tt_int_op(retval,==,retval_reference);
685 tt_assert( (retval == -1 && retval_reference == -1) ||
686 (tor_addr_compare(&addr4,&addr4_to_check,CMP_EXACT) == 0) );
688 retval_reference = get_interface_address6(LOG_DEBUG,AF_INET6,&addr6);
689 retval = get_interface_address6_via_udp_socket_hack(LOG_DEBUG,
690 AF_INET6,
691 &addr6_to_check);
693 tt_int_op(retval,==,retval_reference);
694 tt_assert( (retval == -1 && retval_reference == -1) ||
695 (tor_addr_compare(&addr6,&addr6_to_check,CMP_EXACT) == 0) );
697 #else
698 /* Both of the blackbox test cases fail horribly if:
699 * * The host has no external addreses.
700 * * There are multiple interfaces with either AF_INET or AF_INET6.
701 * * The last address isn't the one associated with the default route.
703 * The tests SHOULD be re-enabled when #12377 is fixed correctly, but till
704 * then this fails a lot, in situations we expect failures due to knowing
705 * about the code being broken.
708 (void)addr4_to_check;
709 (void)addr6_to_check;
710 (void)addr6;
711 (void) retval_reference;
712 #endif
714 /* When family is neither AF_INET nor AF_INET6, we want _hack to
715 * fail and return -1.
718 retval = get_interface_address6_via_udp_socket_hack(LOG_DEBUG,
719 AF_INET+AF_INET6,&addr4);
721 tt_assert(retval == -1);
723 done:
724 return;
727 static void
728 test_address_get_if_addrs_list_internal(void *arg)
730 smartlist_t *results = NULL;
732 (void)arg;
734 results = get_interface_address_list(LOG_ERR, 1);
736 tt_assert(results != NULL);
737 /* When the network is down, a system might not have any non-local
738 * non-multicast addresseses, not even internal ones.
739 * Unit tests shouldn't fail because of this. */
740 tt_int_op(smartlist_len(results),>=,0);
742 tt_assert(!smartlist_contains_localhost_tor_addr(results));
743 tt_assert(!smartlist_contains_multicast_tor_addr(results));
744 /* The list may or may not contain internal addresses */
745 tt_assert(!smartlist_contains_null_tor_addr(results));
747 /* if there are any addresses, they must be IPv4 */
748 if (smartlist_len(results) > 0) {
749 tt_assert(smartlist_contains_ipv4_tor_addr(results));
751 tt_assert(!smartlist_contains_ipv6_tor_addr(results));
753 done:
754 free_interface_address_list(results);
755 return;
758 static void
759 test_address_get_if_addrs_list_no_internal(void *arg)
761 smartlist_t *results = NULL;
763 (void)arg;
765 results = get_interface_address_list(LOG_ERR, 0);
767 tt_assert(results != NULL);
768 /* Work even on systems with only internal IPv4 addresses */
769 tt_int_op(smartlist_len(results),>=,0);
771 tt_assert(!smartlist_contains_localhost_tor_addr(results));
772 tt_assert(!smartlist_contains_multicast_tor_addr(results));
773 tt_assert(!smartlist_contains_internal_tor_addr(results));
774 tt_assert(!smartlist_contains_null_tor_addr(results));
776 /* if there are any addresses, they must be IPv4 */
777 if (smartlist_len(results) > 0) {
778 tt_assert(smartlist_contains_ipv4_tor_addr(results));
780 tt_assert(!smartlist_contains_ipv6_tor_addr(results));
782 done:
783 free_interface_address_list(results);
784 return;
787 static void
788 test_address_get_if_addrs6_list_internal(void *arg)
790 smartlist_t *results = NULL;
792 (void)arg;
794 results = get_interface_address6_list(LOG_ERR, AF_INET6, 1);
796 tt_assert(results != NULL);
797 /* Work even on systems without IPv6 interfaces */
798 tt_int_op(smartlist_len(results),>=,0);
800 tt_assert(!smartlist_contains_localhost_tor_addr(results));
801 tt_assert(!smartlist_contains_multicast_tor_addr(results));
802 /* The list may or may not contain internal addresses */
803 tt_assert(!smartlist_contains_null_tor_addr(results));
805 /* if there are any addresses, they must be IPv6 */
806 tt_assert(!smartlist_contains_ipv4_tor_addr(results));
807 if (smartlist_len(results) > 0) {
808 tt_assert(smartlist_contains_ipv6_tor_addr(results));
811 done:
812 free_interface_address6_list(results);
813 return;
816 static void
817 test_address_get_if_addrs6_list_no_internal(void *arg)
819 smartlist_t *results = NULL;
821 (void)arg;
823 results = get_interface_address6_list(LOG_ERR, AF_INET6, 0);
825 tt_assert(results != NULL);
826 /* Work even on systems without IPv6 interfaces */
827 tt_int_op(smartlist_len(results),>=,0);
829 tt_assert(!smartlist_contains_localhost_tor_addr(results));
830 tt_assert(!smartlist_contains_multicast_tor_addr(results));
831 tt_assert(!smartlist_contains_internal_tor_addr(results));
832 tt_assert(!smartlist_contains_null_tor_addr(results));
834 /* if there are any addresses, they must be IPv6 */
835 tt_assert(!smartlist_contains_ipv4_tor_addr(results));
836 if (smartlist_len(results) > 0) {
837 tt_assert(smartlist_contains_ipv6_tor_addr(results));
840 done:
841 free_interface_address6_list(results);
842 return;
845 static int called_get_interface_addresses_raw = 0;
847 static smartlist_t *
848 mock_get_interface_addresses_raw_fail(int severity)
850 (void)severity;
852 called_get_interface_addresses_raw++;
853 return smartlist_new();
856 static int called_get_interface_address6_via_udp_socket_hack = 0;
858 static int
859 mock_get_interface_address6_via_udp_socket_hack_fail(int severity,
860 sa_family_t family,
861 tor_addr_t *addr)
863 (void)severity;
864 (void)family;
865 (void)addr;
867 called_get_interface_address6_via_udp_socket_hack++;
868 return -1;
871 static void
872 test_address_get_if_addrs_internal_fail(void *arg)
874 smartlist_t *results1 = NULL, *results2 = NULL;
875 int rv = 0;
876 uint32_t ipv4h_addr = 0;
877 tor_addr_t ipv6_addr;
879 memset(&ipv6_addr, 0, sizeof(tor_addr_t));
881 (void)arg;
883 MOCK(get_interface_addresses_raw,
884 mock_get_interface_addresses_raw_fail);
885 MOCK(get_interface_address6_via_udp_socket_hack,
886 mock_get_interface_address6_via_udp_socket_hack_fail);
888 results1 = get_interface_address6_list(LOG_ERR, AF_INET6, 1);
889 tt_assert(results1 != NULL);
890 tt_int_op(smartlist_len(results1),==,0);
892 results2 = get_interface_address_list(LOG_ERR, 1);
893 tt_assert(results2 != NULL);
894 tt_int_op(smartlist_len(results2),==,0);
896 rv = get_interface_address6(LOG_ERR, AF_INET6, &ipv6_addr);
897 tt_assert(rv == -1);
899 rv = get_interface_address(LOG_ERR, &ipv4h_addr);
900 tt_assert(rv == -1);
902 done:
903 UNMOCK(get_interface_addresses_raw);
904 UNMOCK(get_interface_address6_via_udp_socket_hack);
905 free_interface_address6_list(results1);
906 free_interface_address6_list(results2);
907 return;
910 static void
911 test_address_get_if_addrs_no_internal_fail(void *arg)
913 smartlist_t *results1 = NULL, *results2 = NULL;
915 (void)arg;
917 MOCK(get_interface_addresses_raw,
918 mock_get_interface_addresses_raw_fail);
919 MOCK(get_interface_address6_via_udp_socket_hack,
920 mock_get_interface_address6_via_udp_socket_hack_fail);
922 results1 = get_interface_address6_list(LOG_ERR, AF_INET6, 0);
923 tt_assert(results1 != NULL);
924 tt_int_op(smartlist_len(results1),==,0);
926 results2 = get_interface_address_list(LOG_ERR, 0);
927 tt_assert(results2 != NULL);
928 tt_int_op(smartlist_len(results2),==,0);
930 done:
931 UNMOCK(get_interface_addresses_raw);
932 UNMOCK(get_interface_address6_via_udp_socket_hack);
933 free_interface_address6_list(results1);
934 free_interface_address6_list(results2);
935 return;
938 static void
939 test_address_get_if_addrs(void *arg)
941 int rv;
942 uint32_t addr_h = 0;
943 tor_addr_t tor_addr;
945 (void)arg;
947 rv = get_interface_address(LOG_ERR, &addr_h);
949 /* When the network is down, a system might not have any non-local
950 * non-multicast IPv4 addresses, not even internal ones.
951 * Unit tests shouldn't fail because of this. */
952 if (rv == 0) {
953 tor_addr_from_ipv4h(&tor_addr, addr_h);
955 tt_assert(!tor_addr_is_loopback(&tor_addr));
956 tt_assert(!tor_addr_is_multicast(&tor_addr));
957 /* The address may or may not be an internal address */
959 tt_assert(tor_addr_is_v4(&tor_addr));
962 done:
963 return;
966 static void
967 test_address_get_if_addrs6(void *arg)
969 int rv;
970 tor_addr_t tor_addr;
972 (void)arg;
974 rv = get_interface_address6(LOG_ERR, AF_INET6, &tor_addr);
976 /* Work even on systems without IPv6 interfaces */
977 if (rv == 0) {
978 tt_assert(!tor_addr_is_loopback(&tor_addr));
979 tt_assert(!tor_addr_is_multicast(&tor_addr));
980 /* The address may or may not be an internal address */
982 tt_assert(!tor_addr_is_v4(&tor_addr));
985 done:
986 return;
989 static void
990 test_address_tor_addr_to_in6(void *ignored)
992 (void)ignored;
993 tor_addr_t *a = tor_malloc_zero(sizeof(tor_addr_t));
994 const struct in6_addr *res;
995 uint8_t expected[16] = {42, 1, 2, 3, 4, 5, 6, 7, 8, 9,
996 10, 11, 12, 13, 14, 15};
998 a->family = AF_INET;
999 res = tor_addr_to_in6(a);
1000 tt_assert(!res);
1002 a->family = AF_INET6;
1003 memcpy(a->addr.in6_addr.s6_addr, expected, 16);
1004 res = tor_addr_to_in6(a);
1005 tt_assert(res);
1006 tt_mem_op(res->s6_addr, OP_EQ, expected, 16);
1008 done:
1009 tor_free(a);
1012 static void
1013 test_address_tor_addr_to_in(void *ignored)
1015 (void)ignored;
1016 tor_addr_t *a = tor_malloc_zero(sizeof(tor_addr_t));
1017 const struct in_addr *res;
1019 a->family = AF_INET6;
1020 res = tor_addr_to_in(a);
1021 tt_assert(!res);
1023 a->family = AF_INET;
1024 a->addr.in_addr.s_addr = 44;
1025 res = tor_addr_to_in(a);
1026 tt_assert(res);
1027 tt_int_op(res->s_addr, OP_EQ, 44);
1029 done:
1030 tor_free(a);
1033 static void
1034 test_address_tor_addr_to_ipv4n(void *ignored)
1036 (void)ignored;
1037 tor_addr_t *a = tor_malloc_zero(sizeof(tor_addr_t));
1038 uint32_t res;
1040 a->family = AF_INET6;
1041 res = tor_addr_to_ipv4n(a);
1042 tt_assert(!res);
1044 a->family = AF_INET;
1045 a->addr.in_addr.s_addr = 43;
1046 res = tor_addr_to_ipv4n(a);
1047 tt_assert(res);
1048 tt_int_op(res, OP_EQ, 43);
1050 done:
1051 tor_free(a);
1054 static void
1055 test_address_tor_addr_to_mapped_ipv4h(void *ignored)
1057 (void)ignored;
1058 tor_addr_t *a = tor_malloc_zero(sizeof(tor_addr_t));
1059 uint32_t res;
1060 uint8_t toset[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 255, 0, 0, 0, 42};
1062 a->family = AF_INET;
1063 res = tor_addr_to_mapped_ipv4h(a);
1064 tt_assert(!res);
1066 a->family = AF_INET6;
1068 memcpy(a->addr.in6_addr.s6_addr, toset, 16);
1069 res = tor_addr_to_mapped_ipv4h(a);
1070 tt_assert(res);
1071 tt_int_op(res, OP_EQ, 42);
1073 done:
1074 tor_free(a);
1077 static void
1078 test_address_tor_addr_eq_ipv4h(void *ignored)
1080 (void)ignored;
1081 tor_addr_t *a = tor_malloc_zero(sizeof(tor_addr_t));
1082 int res;
1084 a->family = AF_INET6;
1085 res = tor_addr_eq_ipv4h(a, 42);
1086 tt_assert(!res);
1088 a->family = AF_INET;
1089 a->addr.in_addr.s_addr = 52;
1090 res = tor_addr_eq_ipv4h(a, 42);
1091 tt_assert(!res);
1093 a->addr.in_addr.s_addr = 52;
1094 res = tor_addr_eq_ipv4h(a, ntohl(52));
1095 tt_assert(res);
1097 done:
1098 tor_free(a);
1101 #define ADDRESS_TEST(name, flags) \
1102 { #name, test_address_ ## name, flags, NULL, NULL }
1104 struct testcase_t address_tests[] = {
1105 ADDRESS_TEST(udp_socket_trick_whitebox, TT_FORK),
1106 ADDRESS_TEST(udp_socket_trick_blackbox, TT_FORK),
1107 ADDRESS_TEST(get_if_addrs_list_internal, 0),
1108 ADDRESS_TEST(get_if_addrs_list_no_internal, 0),
1109 ADDRESS_TEST(get_if_addrs6_list_internal, 0),
1110 ADDRESS_TEST(get_if_addrs6_list_no_internal, 0),
1111 ADDRESS_TEST(get_if_addrs_internal_fail, 0),
1112 ADDRESS_TEST(get_if_addrs_no_internal_fail, 0),
1113 ADDRESS_TEST(get_if_addrs, 0),
1114 ADDRESS_TEST(get_if_addrs6, 0),
1115 #ifdef HAVE_IFADDRS_TO_SMARTLIST
1116 ADDRESS_TEST(get_if_addrs_ifaddrs, TT_FORK),
1117 ADDRESS_TEST(ifaddrs_to_smartlist, 0),
1118 #endif
1119 #ifdef HAVE_IP_ADAPTER_TO_SMARTLIST
1120 ADDRESS_TEST(get_if_addrs_win32, TT_FORK),
1121 ADDRESS_TEST(ip_adapter_addresses_to_smartlist, 0),
1122 #endif
1123 #ifdef HAVE_IFCONF_TO_SMARTLIST
1124 ADDRESS_TEST(get_if_addrs_ioctl, TT_FORK),
1125 ADDRESS_TEST(ifreq_to_smartlist, 0),
1126 #endif
1127 ADDRESS_TEST(tor_addr_to_in6, 0),
1128 ADDRESS_TEST(tor_addr_to_in, 0),
1129 ADDRESS_TEST(tor_addr_to_ipv4n, 0),
1130 ADDRESS_TEST(tor_addr_to_mapped_ipv4h, 0),
1131 ADDRESS_TEST(tor_addr_eq_ipv4h, 0),
1132 END_OF_TESTCASES