diff options
Diffstat (limited to 'src/test/test_addr.c')
-rw-r--r-- | src/test/test_addr.c | 72 |
1 files changed, 30 insertions, 42 deletions
diff --git a/src/test/test_addr.c b/src/test/test_addr.c index 09faa540f4..44f3cd9ec1 100644 --- a/src/test/test_addr.c +++ b/src/test/test_addr.c @@ -12,6 +12,7 @@ #include "test/log_test_helpers.h" #include "lib/net/resolve.h" #include "test/rng_test_helpers.h" +#include "test/resolve_test_helpers.h" #ifdef HAVE_SYS_UN_H #include <sys/un.h> @@ -735,10 +736,6 @@ test_addr_ip6_helpers(void *arg) #define TEST_ADDR_PARSE_FMT(addr_str, expect_family, fmt_decorated, \ expect_str) \ STMT_BEGIN \ - int r; \ - tor_addr_t addr; \ - char buf[TOR_ADDR_BUF_LEN]; \ - const char *sv; \ r = tor_addr_parse(&addr, addr_str); \ tt_int_op(r, OP_EQ, expect_family); \ sv = tor_addr_to_str(buf, &addr, sizeof(buf), fmt_decorated); \ @@ -751,8 +748,6 @@ test_addr_ip6_helpers(void *arg) */ #define TEST_ADDR_PARSE_XFAIL(addr_str) \ STMT_BEGIN \ - int r; \ - tor_addr_t addr; \ r = tor_addr_parse(&addr, addr_str); \ tt_int_op(r, OP_EQ, -1); \ tt_assert(tor_addr_is_null(&addr)); \ @@ -766,11 +761,6 @@ test_addr_ip6_helpers(void *arg) #define TEST_ADDR_PORT_PARSE_FMT(addr_port_str, default_port, expect_family, \ fmt_decorated, expect_str, expect_port) \ STMT_BEGIN \ - int r; \ - tor_addr_t addr; \ - uint16_t port; \ - char buf[TOR_ADDR_BUF_LEN]; \ - const char *sv; \ r = tor_addr_port_parse(LOG_DEBUG, addr_port_str, &addr, &port, \ default_port); \ tt_int_op(r, OP_EQ, 0); \ @@ -787,9 +777,6 @@ test_addr_ip6_helpers(void *arg) */ #define TEST_ADDR_PORT_PARSE_XFAIL(addr_port_str, default_port) \ STMT_BEGIN \ - int r; \ - tor_addr_t addr; \ - uint16_t port; \ r = tor_addr_port_parse(LOG_DEBUG, addr_port_str, &addr, &port, \ default_port); \ tt_int_op(r, OP_EQ, -1); \ @@ -803,8 +790,6 @@ test_addr_ip6_helpers(void *arg) */ #define TEST_ADDR_V4_LOOKUP_HOSTNAME(addr_str, expect_str) \ STMT_BEGIN \ - int r; \ - uint32_t addr32h; \ r = tor_lookup_hostname(addr_str, &addr32h); \ tt_int_op(r, OP_EQ, 0); \ tt_str_op(fmt_addr32(addr32h), OP_EQ, expect_str); \ @@ -816,8 +801,6 @@ test_addr_ip6_helpers(void *arg) */ #define TEST_ADDR_V4_LOOKUP_XFAIL(bad_str) \ STMT_BEGIN \ - int r; \ - uint32_t addr32h; \ r = tor_lookup_hostname(bad_str, &addr32h); \ tt_int_op(r, OP_EQ, -1); \ tt_int_op(addr32h, OP_EQ, 0); \ @@ -832,8 +815,6 @@ test_addr_ip6_helpers(void *arg) */ #define TEST_HOST_V4_LOOKUP(host_str) \ STMT_BEGIN \ - int r; \ - uint32_t addr32h; \ r = tor_lookup_hostname(host_str, &addr32h); \ tt_int_op(r, OP_GE, -1); \ tt_int_op(r, OP_LE, 1); \ @@ -849,10 +830,6 @@ test_addr_ip6_helpers(void *arg) #define TEST_ADDR_LOOKUP_FMT(addr_str, require_family, expect_family, \ fmt_decorated, expect_str) \ STMT_BEGIN \ - int r; \ - tor_addr_t addr; \ - char buf[TOR_ADDR_BUF_LEN]; \ - const char *sv; \ r = tor_addr_lookup(addr_str, require_family, &addr); \ tt_int_op(r, OP_EQ, 0); \ tt_int_op(tor_addr_family(&addr), OP_EQ, expect_family); \ @@ -867,8 +844,6 @@ test_addr_ip6_helpers(void *arg) */ #define TEST_ADDR_LOOKUP_XFAIL(bad_str, require_family) \ STMT_BEGIN \ - int r; \ - tor_addr_t addr; \ r = tor_addr_lookup(bad_str, require_family, &addr); \ tt_int_op(r, OP_EQ, -1); \ tt_assert(tor_addr_is_null(&addr)); \ @@ -883,8 +858,6 @@ test_addr_ip6_helpers(void *arg) */ #define TEST_HOST_LOOKUP(host_str, require_family) \ STMT_BEGIN \ - int r; \ - tor_addr_t addr; \ r = tor_addr_lookup(host_str, require_family, &addr); \ tt_int_op(r, OP_GE, -1); \ tt_int_op(r, OP_LE, 1); \ @@ -901,11 +874,6 @@ test_addr_ip6_helpers(void *arg) #define TEST_ADDR_PORT_LOOKUP_FMT(addr_port_str, expect_family, \ fmt_decorated, expect_str, expect_port) \ STMT_BEGIN \ - int r; \ - tor_addr_t addr; \ - uint16_t port; \ - char buf[TOR_ADDR_BUF_LEN]; \ - const char *sv; \ r = tor_addr_port_lookup(addr_port_str, &addr, &port); \ tt_int_op(r, OP_EQ, 0); \ tt_int_op(tor_addr_family(&addr), OP_EQ, expect_family); \ @@ -922,9 +890,6 @@ test_addr_ip6_helpers(void *arg) */ #define TEST_ADDR_PORT_LOOKUP_XFAIL(bad_str) \ STMT_BEGIN \ - int r; \ - tor_addr_t addr; \ - uint16_t port; \ r = tor_addr_port_lookup(bad_str, &addr, &port); \ tt_int_op(r, OP_EQ, -1); \ tt_assert(tor_addr_is_null(&addr)); \ @@ -943,9 +908,6 @@ test_addr_ip6_helpers(void *arg) */ #define TEST_HOST_PORT_LOOKUP(host_port_str, expect_success_port) \ STMT_BEGIN \ - int r; \ - tor_addr_t addr; \ - uint16_t port; \ r = tor_addr_port_lookup(host_port_str, &addr, &port); \ tt_int_op(r, OP_GE, -1); \ tt_int_op(r, OP_LE, 0); \ @@ -1166,10 +1128,16 @@ test_addr_ip6_helpers(void *arg) TEST_ADDR_LOOKUP_XFAIL(host_port_str, AF_INET6); \ STMT_END -/** Test tor_addr_parse() and tor_addr_port_parse(). */ static void -test_addr_parse(void *arg) +test_addr_parse_canonical(void *arg) { + int r; + tor_addr_t addr; + uint16_t port; + const char *sv; + uint32_t addr32h; + char buf[TOR_ADDR_BUF_LEN]; + (void)arg; /* Correct calls. */ @@ -1189,6 +1157,25 @@ test_addr_parse(void *arg) TEST_ADDR_V6_PARSE_CANONICAL("::", 0); TEST_ADDR_V6_PARSE_CANONICAL("2::", 0); TEST_ADDR_V6_PARSE_CANONICAL("11:22:33:44:55:66:77:88", 0); + done: + ; +} + +/** Test tor_addr_parse() and tor_addr_port_parse(). */ +static void +test_addr_parse(void *arg) +{ + + int r; + tor_addr_t addr; + uint16_t port; + const char *sv; + uint32_t addr32h; + char buf[TOR_ADDR_BUF_LEN]; + + (void)arg; + + mock_hostname_resolver(); /* IPv6-mapped IPv4 addresses. Tor doesn't really use these. */ TEST_ADDR_V6_PARSE("11:22:33:44:55:66:1.2.3.4", 0, @@ -1294,7 +1281,7 @@ test_addr_parse(void *arg) "11:22::88",99); done: - ; + unmock_hostname_resolver(); } static void @@ -1675,6 +1662,7 @@ struct testcase_t addr_tests[] = { ADDR_LEGACY(basic), ADDR_LEGACY(ip6_helpers), ADDR_LEGACY(parse), + ADDR_LEGACY(parse_canonical), { "virtaddr", test_virtaddrmap, 0, NULL, NULL }, { "virtaddr_persist", test_virtaddrmap_persist, TT_FORK, NULL, NULL }, { "localname", test_addr_localname, 0, NULL, NULL }, |