aboutsummaryrefslogtreecommitdiff
path: root/src/test/test_router.c
diff options
context:
space:
mode:
authorNick Mathewson <nickm@torproject.org>2020-03-19 09:50:36 -0400
committerteor <teor@torproject.org>2020-03-21 03:44:01 +1000
commit96ca14d98924fec8a18a592c491900a9142f6e71 (patch)
tree6ded43abe5a1bede73f73c79d3a3abf28ac296ba /src/test/test_router.c
parent1251265a0f49c7446f3e00854eda8091e84d9e96 (diff)
downloadtor-96ca14d98924fec8a18a592c491900a9142f6e71.tar.gz
tor-96ca14d98924fec8a18a592c491900a9142f6e71.zip
Add a test for the localhost case.
Diffstat (limited to 'src/test/test_router.c')
-rw-r--r--src/test/test_router.c46
1 files changed, 45 insertions, 1 deletions
diff --git a/src/test/test_router.c b/src/test/test_router.c
index 8c772c6817..e0d3adfdbd 100644
--- a/src/test/test_router.c
+++ b/src/test/test_router.c
@@ -248,7 +248,6 @@ static void
test_router_get_advertised_or_port(void *arg)
{
(void)arg;
- // uint16_t port;
int r, w=0, n=0;
char *msg=NULL;
or_options_t *opts = options_new();
@@ -305,6 +304,50 @@ test_router_get_advertised_or_port(void *arg)
UNMOCK(get_connection_array);
}
+static void
+test_router_get_advertised_or_port_localhost(void *arg)
+{
+ (void)arg;
+ int r, w=0, n=0;
+ char *msg=NULL;
+ or_options_t *opts = options_new();
+ tor_addr_port_t ipv6;
+
+ // Set up a couple of configured ports on localhost.
+ config_line_append(&opts->ORPort_lines, "ORPort", "[::1]:9999");
+ config_line_append(&opts->ORPort_lines, "ORPort", "127.0.0.1:8888");
+ r = parse_ports(opts, 0, &msg, &n, &w);
+ tt_assert(r == 0);
+
+ // We should refuse to advertise them, since we have default dirauths.
+ router_get_advertised_ipv6_or_ap(opts, &ipv6);
+ tt_str_op(fmt_addrport(&ipv6.addr, ipv6.port), OP_EQ, "[::]:0");
+ // But the lower-level function should still report the correct value
+ tt_int_op(9999, OP_EQ, router_get_advertised_or_port_by_af(opts, AF_INET6));
+
+ // The IPv4 checks are done in resolve_my_address(), which doesn't use
+ // ORPorts so we can't test them here. (See #33681.) Both these lower-level
+ // functions should still report the correct value.
+ tt_int_op(8888, OP_EQ, router_get_advertised_or_port_by_af(opts, AF_INET));
+ tt_int_op(8888, OP_EQ, router_get_advertised_or_port(opts));
+
+ // Now try with a fake authority set up.
+ config_line_append(&opts->DirAuthorities, "DirAuthority",
+ "127.0.0.1:1066 "
+ "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA");
+
+ tt_int_op(9999, OP_EQ, router_get_advertised_or_port_by_af(opts, AF_INET6));
+ router_get_advertised_ipv6_or_ap(opts, &ipv6);
+ tt_str_op(fmt_addrport(&ipv6.addr, ipv6.port), OP_EQ, "[::1]:9999");
+
+ tt_int_op(8888, OP_EQ, router_get_advertised_or_port_by_af(opts, AF_INET));
+ tt_int_op(8888, OP_EQ, router_get_advertised_or_port(opts));
+
+ done:
+ or_options_free(opts);
+ config_free_all();
+}
+
#define ROUTER_TEST(name, flags) \
{ #name, test_router_ ## name, flags, NULL, NULL }
@@ -312,5 +355,6 @@ struct testcase_t router_tests[] = {
ROUTER_TEST(check_descriptor_bandwidth_changed, TT_FORK),
ROUTER_TEST(dump_router_to_string_no_bridge_distribution_method, TT_FORK),
ROUTER_TEST(get_advertised_or_port, TT_FORK),
+ ROUTER_TEST(get_advertised_or_port_localhost, TT_FORK),
END_OF_TESTCASES
};