commit 69a1f9c8a7a9c8221029ce83f19d0e8d3e93f917 Author: teor teor@torproject.org Date: Thu Sep 19 16:17:17 2019 +1000
test: test dirserv_router_has_valid_address() with DirAllowPrivateAddresses
Part of 31793. --- src/test/test_address.c | 38 +++++++++++++++++++++++++++++++------- 1 file changed, 31 insertions(+), 7 deletions(-)
diff --git a/src/test/test_address.c b/src/test/test_address.c index ab95113dc..32fb4aa23 100644 --- a/src/test/test_address.c +++ b/src/test/test_address.c @@ -24,6 +24,7 @@ #endif /* defined(HAVE_IFCONF_TO_SMARTLIST) */
#include "core/or/or.h" +#include "app/config/config.h" #include "feature/dirauth/process_descs.h" #include "feature/nodelist/routerinfo_st.h" #include "feature/nodelist/node_st.h" @@ -1245,6 +1246,14 @@ test_address_tor_node_in_same_network_family(void *ignored) helper_free_mock_node(node_b); }
+static or_options_t mock_options; + +static const or_options_t * +mock_get_options(void) +{ + return &mock_options; +} + /* Test dirserv_router_has_valid_address() on a stub routerinfo, with only its * address fields set. Use IPv4 ipv4_addr_str and IPv6 ipv6_addr_str. * Fail if it does not return rv. */ @@ -1270,22 +1279,32 @@ test_address_tor_node_in_same_network_family(void *ignored) TEST_ROUTER_VALID_ADDRESS_HELPER("1.0.0.1", ipv6_addr_str, rv)
static void -test_address_dirserv_router_addr_private(void *ignored) +test_address_dirserv_router_addr_private(void *opt_dir_allow_private) { - (void)ignored; /* A stub routerinfo structure, with only its address fields set. */ routerinfo_t *ri = NULL; + /* The expected return value for private addresses. + * Modified if DirAllowPrivateAddresses is 1. */ + int private_rv = -1; + + memset(&mock_options, 0, sizeof(or_options_t)); + MOCK(get_options, mock_get_options); + + if (opt_dir_allow_private) { + mock_options.DirAllowPrivateAddresses = 1; + private_rv = 0; + }
CHECK_RI_ADDR("1.0.0.1", 0); - CHECK_RI_ADDR("10.0.0.1", -1); + CHECK_RI_ADDR("10.0.0.1", private_rv);
CHECK_RI_ADDR6("2600::1", 0); - CHECK_RI_ADDR6("fe80::1", -1); + CHECK_RI_ADDR6("fe80::1", private_rv);
/* Null addresses */ /* IPv4 null fails, regardless of IPv6 */ - CHECK_RI_ADDR("0.0.0.0", -1); - TEST_ROUTER_VALID_ADDRESS_HELPER("0.0.0.0", "::", -1); + CHECK_RI_ADDR("0.0.0.0", private_rv); + TEST_ROUTER_VALID_ADDRESS_HELPER("0.0.0.0", "::", private_rv);
/* IPv6 null succeeds, because IPv4 is not null */ CHECK_RI_ADDR6("::", 0); @@ -1294,7 +1313,7 @@ test_address_dirserv_router_addr_private(void *ignored) /* IPv4 null fails, regardless of IPv6 */ { ri = tor_malloc_zero(sizeof(routerinfo_t)); - tt_int_op(dirserv_router_has_valid_address(ri), OP_EQ, -1); + tt_int_op(dirserv_router_has_valid_address(ri), OP_EQ, private_rv); tor_free(ri); }
@@ -1308,10 +1327,14 @@ test_address_dirserv_router_addr_private(void *ignored)
done: tor_free(ri); + UNMOCK(get_options); }
#define ADDRESS_TEST(name, flags) \ { #name, test_address_ ## name, flags, NULL, NULL } +#define ADDRESS_TEST_STR_ARG(name, flags, str_arg) \ + { #name "/" str_arg, test_address_ ## name, flags, &passthrough_setup, \ + (void *)(str_arg) }
struct testcase_t address_tests[] = { ADDRESS_TEST(udp_socket_trick_whitebox, TT_FORK), @@ -1344,5 +1367,6 @@ struct testcase_t address_tests[] = { ADDRESS_TEST(tor_addr_in_same_network_family, 0), ADDRESS_TEST(tor_node_in_same_network_family, 0), ADDRESS_TEST(dirserv_router_addr_private, 0), + ADDRESS_TEST_STR_ARG(dirserv_router_addr_private, 0, "allow_private"), END_OF_TESTCASES };
tor-commits@lists.torproject.org