diff options
author | teor <teor@torproject.org> | 2019-06-14 00:00:05 +1000 |
---|---|---|
committer | Nick Mathewson <nickm@torproject.org> | 2019-06-26 09:55:37 -0400 |
commit | 1c92d7f3882e6bd43e871ff1b17ca8c4a65b1e08 (patch) | |
tree | 05cbffcbf88e18fd306ba3d6d6d3937fe824c079 /src/test/test_addr.c | |
parent | 60ce431c5440ee08e100a1f70a4d7224b7dc9c21 (diff) | |
download | tor-1c92d7f3882e6bd43e871ff1b17ca8c4a65b1e08.tar.gz tor-1c92d7f3882e6bd43e871ff1b17ca8c4a65b1e08.zip |
test/addr: test that tor_lookup_hostname() handles IPv4 addresses
And that it fails on IPv6 and host:port, and does something sensible with
host.
Tests for 30721.
Diffstat (limited to 'src/test/test_addr.c')
-rw-r--r-- | src/test/test_addr.c | 142 |
1 files changed, 128 insertions, 14 deletions
diff --git a/src/test/test_addr.c b/src/test/test_addr.c index 1b25c4c643..53f41bcf1b 100644 --- a/src/test/test_addr.c +++ b/src/test/test_addr.c @@ -793,71 +793,187 @@ test_addr_ip6_helpers(void *arg) tt_int_op(port, OP_EQ, 0); \ STMT_END +/* Test that addr_str successfully parses as an IPv4 address using + * tor_lookup_hostname(), and: + * - the fmt_addr32() of the result is expect_str. + */ +#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); \ + STMT_END + +/* Test that bad_str fails to parse using tor_lookup_hostname(), with a + * permanent failure, and: + * - the returned address is 0. + */ +#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); \ + STMT_END + +/* Test that looking up host_str as an IPv4 address using tor_lookup_hostname() + * does something sensible: + * - the result is -1, 0, or 1. + * - if the result is a failure, the returned address is 0. + * We can't rely on the result of this function, because it depends on the + * network. + */ +#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); \ + if (r != 0) \ + tt_int_op(addr32h, OP_EQ, 0); \ + STMT_END + /* Test that addr_str successfully parses as a canonical IPv4 address. - * Use tor_addr_parse(), and tor_addr_port_parse() with a default port. - * Also check that tor_addr_port_parse() fails without a default port. */ + * Check for successful parsing using: + * - tor_addr_parse(), + * - tor_addr_port_parse() with a default port, + * - tor_lookup_hostname(). + * Check for failures using: + * - tor_addr_port_parse() without a default port, because there is no port. + */ #define TEST_ADDR_V4_PARSE_CANONICAL(addr_str) \ STMT_BEGIN \ TEST_ADDR_PARSE_FMT(addr_str, AF_INET, 0, addr_str); \ TEST_ADDR_PORT_PARSE_FMT(addr_str, 111, AF_INET, 0, \ addr_str, 111); \ TEST_ADDR_PORT_PARSE_XFAIL(addr_str, -1); \ + TEST_ADDR_V4_LOOKUP_HOSTNAME(addr_str, addr_str); \ STMT_END /* Test that addr_str successfully parses as a canonical fmt_decorated * IPv6 address. - * Use tor_addr_parse(), and tor_addr_port_parse() with a default port. - * Also check that tor_addr_port_parse() fails without a default port. */ + * Check for successful parsing using: + * - tor_addr_parse(), + * - tor_addr_port_parse() with a default port. + * Check for failures using: + * - tor_addr_port_parse() without a default port, because there is no port, + * - tor_lookup_hostname(), because it only supports IPv4. + */ #define TEST_ADDR_V6_PARSE_CANONICAL(addr_str, fmt_decorated) \ STMT_BEGIN \ TEST_ADDR_PARSE_FMT(addr_str, AF_INET6, fmt_decorated, addr_str); \ TEST_ADDR_PORT_PARSE_FMT(addr_str, 222, AF_INET6, fmt_decorated, \ addr_str, 222); \ TEST_ADDR_PORT_PARSE_XFAIL(addr_str, -1); \ + TEST_ADDR_V4_LOOKUP_XFAIL(addr_str); \ STMT_END /* Test that addr_str successfully parses, and the fmt_decorated canonical * IPv6 string is expect_str. - * Use tor_addr_parse(), and tor_addr_port_parse() with a default port. - * Also check that tor_addr_port_parse() fails without a default port. */ + * Check for successful parsing using: + * - tor_addr_parse(), + * - tor_addr_port_parse() with a default port. + * Check for failures using: + * - tor_addr_port_parse() without a default port, because there is no port. + * - tor_lookup_hostname(), because it only supports IPv4. + */ #define TEST_ADDR_V6_PARSE(addr_str, fmt_decorated, expect_str) \ STMT_BEGIN \ TEST_ADDR_PARSE_FMT(addr_str, AF_INET6, fmt_decorated, expect_str); \ TEST_ADDR_PORT_PARSE_FMT(addr_str, 333, AF_INET6, fmt_decorated, \ expect_str, 333); \ TEST_ADDR_PORT_PARSE_XFAIL(addr_str, -1); \ + TEST_ADDR_V4_LOOKUP_XFAIL(addr_str); \ STMT_END /* Test that addr_port_str successfully parses to the canonical IPv4 address * string expect_str, and port expect_port. - * Check with and without a default port. */ + * Check for successful parsing using: + * - tor_addr_port_parse() without a default port, + * - tor_addr_port_parse() with a default port. + * Check for failures using: + * - tor_addr_parse(), because there is a port, + * - tor_lookup_hostname(), because there is a port. + */ #define TEST_ADDR_V4_PORT_PARSE(addr_port_str, expect_str, expect_port) \ STMT_BEGIN \ TEST_ADDR_PORT_PARSE_FMT(addr_port_str, -1, AF_INET, 0, expect_str, \ expect_port); \ TEST_ADDR_PORT_PARSE_FMT(addr_port_str, 444, AF_INET, 0, expect_str, \ expect_port); \ + TEST_ADDR_PARSE_XFAIL(addr_port_str); \ + TEST_ADDR_V4_LOOKUP_XFAIL(addr_port_str); \ STMT_END /* Test that addr_port_str successfully parses to the canonical undecorated * IPv6 address string expect_str, and port expect_port. - * Check with and without a default port. */ + * Check for successful parsing using: + * - tor_addr_port_parse() without a default port, + * - tor_addr_port_parse() with a default port. + * Check for failures using: + * - tor_addr_parse(), because there is a port, + * - tor_lookup_hostname(), because there is a port, and because it only + * supports IPv4. + */ #define TEST_ADDR_V6_PORT_PARSE(addr_port_str, expect_str, expect_port) \ STMT_BEGIN \ TEST_ADDR_PORT_PARSE_FMT(addr_port_str, -1, AF_INET6, 0, expect_str, \ expect_port); \ TEST_ADDR_PORT_PARSE_FMT(addr_port_str, 555, AF_INET6, 0, expect_str, \ expect_port); \ + TEST_ADDR_PARSE_XFAIL(addr_port_str); \ + TEST_ADDR_V4_LOOKUP_XFAIL(addr_port_str); \ STMT_END /* Test that addr_str fails to parse due to a bad address or port. - * Use tor_addr_parse(), and tor_addr_port_parse() with and without a - * default port. */ + * Check for failures using: + * - tor_addr_parse(), + * - tor_addr_port_parse() without a default port, + * - tor_addr_port_parse() with a default port. + * - tor_lookup_hostname(). + */ #define TEST_ADDR_PARSE_XFAIL_MALFORMED(addr_str) \ STMT_BEGIN \ TEST_ADDR_PARSE_XFAIL(addr_str); \ TEST_ADDR_PORT_PARSE_XFAIL(addr_str, -1); \ TEST_ADDR_PORT_PARSE_XFAIL(addr_str, 666); \ + TEST_ADDR_V4_LOOKUP_XFAIL(addr_str); \ + STMT_END + +/* Test that host_str is treated as a hostname, and not an address. + * Check for success or failure using: + * - tor_lookup_hostname(), because it depends on the network. + * Check for failures using: + * - tor_addr_parse(), + * - tor_addr_port_parse() without a default port, + * - tor_addr_port_parse() with a default port. + */ +#define TEST_HOSTNAME(host_str) \ + STMT_BEGIN \ + TEST_HOST_V4_LOOKUP(host_str); \ + TEST_ADDR_PARSE_XFAIL(host_str); \ + TEST_ADDR_PORT_PARSE_XFAIL(host_str, -1); \ + TEST_ADDR_PORT_PARSE_XFAIL(host_str, 777); \ + STMT_END + +/* Test that host_port_str is treated as a hostname and port, and not a + * hostname or an address. + * Check for failures using: + * - tor_lookup_hostname(), because it doesn't support ports, + * - tor_addr_parse(), + * - tor_addr_port_parse() without a default port, + * - tor_addr_port_parse() with a default port. + */ +#define TEST_HOSTNAME_PORT(host_port_str) \ + STMT_BEGIN \ + TEST_ADDR_V4_LOOKUP_XFAIL(host_port_str); \ + TEST_ADDR_PARSE_XFAIL(host_port_str); \ + TEST_ADDR_PORT_PARSE_XFAIL(host_port_str, -1); \ + TEST_ADDR_PORT_PARSE_XFAIL(host_port_str, 888); \ STMT_END /** Test tor_addr_parse() and tor_addr_port_parse(). */ @@ -924,7 +1040,8 @@ test_addr_parse(void *arg) TEST_ADDR_V6_PORT_PARSE("[::1]:1234", "::1", 1234); /* Domain name. */ - TEST_ADDR_PARSE_XFAIL_MALFORMED("torproject.org:1234"); + TEST_HOSTNAME("torproject.org"); + TEST_HOSTNAME_PORT("torproject.org:1234"); /* Only IP. */ TEST_ADDR_V4_PARSE_CANONICAL("192.0.2.2"); @@ -937,9 +1054,6 @@ test_addr_parse(void *arg) /* Bad port. */ TEST_ADDR_PARSE_XFAIL_MALFORMED("192.0.2.2:66666"); - /* Only domain name */ - TEST_ADDR_PARSE_XFAIL_MALFORMED("torproject.org"); - /* Bad IPv4 address */ TEST_ADDR_PARSE_XFAIL_MALFORMED("192.0.2:1234"); |