aboutsummaryrefslogtreecommitdiff
path: root/src/test/test_address.c
diff options
context:
space:
mode:
authorteor (Tim Wilson-Brown) <teor2345@gmail.com>2015-09-29 07:04:49 +0200
committerNick Mathewson <nickm@torproject.org>2015-09-29 10:17:05 +0200
commit7fa102b48783b51673095e1ddcb2f88050a2ba32 (patch)
tree2ca26db29d68f8a31397b1c04a16a752b5d9f04b /src/test/test_address.c
parent3ea834ce0a63f275fd806f9ebeec64a2bbcd1b55 (diff)
downloadtor-7fa102b48783b51673095e1ddcb2f88050a2ba32.tar.gz
tor-7fa102b48783b51673095e1ddcb2f88050a2ba32.zip
Add checks and unit tests for get_interface_address* failure
Ensure that either a valid address is returned in address pointers, or that the address data is zeroed on error. Ensure that free_interface_address6_list handles NULL lists. Add unit tests for get_interface_address* failure cases. Fixes bug #17173. Patch by fk/teor, not in any released version of tor.
Diffstat (limited to 'src/test/test_address.c')
-rw-r--r--src/test/test_address.c95
1 files changed, 95 insertions, 0 deletions
diff --git a/src/test/test_address.c b/src/test/test_address.c
index 72742df2cd..b442f4a871 100644
--- a/src/test/test_address.c
+++ b/src/test/test_address.c
@@ -779,6 +779,99 @@ test_address_get_if_addrs6_list_no_internal(void *arg)
return;
}
+static int called_get_interface_addresses_raw = 0;
+
+smartlist_t *
+mock_get_interface_addresses_raw_fail(int severity)
+{
+ (void)severity;
+
+ called_get_interface_addresses_raw++;
+ return smartlist_new();
+}
+
+static int called_get_interface_address6_via_udp_socket_hack = 0;
+
+int
+mock_get_interface_address6_via_udp_socket_hack_fail(int severity,
+ sa_family_t family,
+ tor_addr_t *addr)
+{
+ (void)severity;
+ (void)family;
+ (void)addr;
+
+ called_get_interface_address6_via_udp_socket_hack++;
+ return -1;
+}
+
+static void
+test_address_get_if_addrs_internal_fail(void *arg)
+{
+ smartlist_t *results1 = NULL, *results2 = NULL;
+ int rv = 0;
+ uint32_t ipv4h_addr = 0;
+ tor_addr_t ipv6_addr;
+
+ memset(&ipv6_addr, 0, sizeof(tor_addr_t));
+
+ (void)arg;
+
+ MOCK(get_interface_addresses_raw,
+ mock_get_interface_addresses_raw_fail);
+ MOCK(get_interface_address6_via_udp_socket_hack,
+ mock_get_interface_address6_via_udp_socket_hack_fail);
+
+ results1 = get_interface_address6_list(LOG_ERR, AF_INET6, 1);
+ tt_assert(results1 != NULL);
+ tt_int_op(smartlist_len(results1),==,0);
+
+ results2 = get_interface_address_list(LOG_ERR, 1);
+ tt_assert(results2 != NULL);
+ tt_int_op(smartlist_len(results2),==,0);
+
+ rv = get_interface_address6(LOG_ERR, AF_INET6, &ipv6_addr);
+ tt_assert(rv == -1);
+
+ rv = get_interface_address(LOG_ERR, &ipv4h_addr);
+ tt_assert(rv == -1);
+
+done:
+ UNMOCK(get_interface_addresses_raw);
+ UNMOCK(get_interface_address6_via_udp_socket_hack);
+ free_interface_address6_list(results1);
+ free_interface_address6_list(results2);
+ return;
+}
+
+static void
+test_address_get_if_addrs_no_internal_fail(void *arg)
+{
+ smartlist_t *results1 = NULL, *results2 = NULL;
+
+ (void)arg;
+
+ MOCK(get_interface_addresses_raw,
+ mock_get_interface_addresses_raw_fail);
+ MOCK(get_interface_address6_via_udp_socket_hack,
+ mock_get_interface_address6_via_udp_socket_hack_fail);
+
+ results1 = get_interface_address6_list(LOG_ERR, AF_INET6, 0);
+ tt_assert(results1 != NULL);
+ tt_int_op(smartlist_len(results1),==,0);
+
+ results2 = get_interface_address_list(LOG_ERR, 0);
+ tt_assert(results2 != NULL);
+ tt_int_op(smartlist_len(results2),==,0);
+
+done:
+ UNMOCK(get_interface_addresses_raw);
+ UNMOCK(get_interface_address6_via_udp_socket_hack);
+ free_interface_address6_list(results1);
+ free_interface_address6_list(results2);
+ return;
+}
+
static void
test_address_get_if_addrs(void *arg)
{
@@ -838,6 +931,8 @@ struct testcase_t address_tests[] = {
ADDRESS_TEST(get_if_addrs_list_no_internal, 0),
ADDRESS_TEST(get_if_addrs6_list_internal, 0),
ADDRESS_TEST(get_if_addrs6_list_no_internal, 0),
+ ADDRESS_TEST(get_if_addrs_internal_fail, 0),
+ ADDRESS_TEST(get_if_addrs_no_internal_fail, 0),
ADDRESS_TEST(get_if_addrs, 0),
ADDRESS_TEST(get_if_addrs6, 0),
#ifdef HAVE_IFADDRS_TO_SMARTLIST