diff options
author | Neel Chauhan <neel@neelc.org> | 2018-10-16 20:14:27 -0400 |
---|---|---|
committer | Nick Mathewson <nickm@torproject.org> | 2018-12-01 14:55:57 -0500 |
commit | ad031b64ce355ac3af15adb716a4322858dce4a8 (patch) | |
tree | a05456c408e6c66bb9039c70f442f43e07bbf0d2 /src | |
parent | 81f2828d67f0853c028617511f3846c02f0d6628 (diff) | |
download | tor-ad031b64ce355ac3af15adb716a4322858dce4a8.tar.gz tor-ad031b64ce355ac3af15adb716a4322858dce4a8.zip |
Add regression test for ClientAutoIPv6ORPort
Diffstat (limited to 'src')
-rw-r--r-- | src/core/or/policies.c | 4 | ||||
-rw-r--r-- | src/core/or/policies.h | 1 | ||||
-rw-r--r-- | src/test/test_policy.c | 50 |
3 files changed, 53 insertions, 2 deletions
diff --git a/src/core/or/policies.c b/src/core/or/policies.c index 6da369bf36..c630c2fbdc 100644 --- a/src/core/or/policies.c +++ b/src/core/or/policies.c @@ -491,8 +491,8 @@ fascist_firewall_prefer_ipv6_impl(const or_options_t *options) /* Choose whether we prefer IPv4 or IPv6 by randomly choosing an address * family. Return 0 for IPv4, and 1 for IPv6. */ -static int -fascist_firewall_rand_prefer_ipv6_addr(void) +MOCK_IMPL(int, +fascist_firewall_rand_prefer_ipv6_addr, (void)) { /* TODO: Check for failures, and infer our preference based on this. */ return crypto_rand_int(2); diff --git a/src/core/or/policies.h b/src/core/or/policies.h index 7da3ba031f..0c64ecf378 100644 --- a/src/core/or/policies.h +++ b/src/core/or/policies.h @@ -70,6 +70,7 @@ typedef struct short_policy_t { int firewall_is_fascist_or(void); int firewall_is_fascist_dir(void); int fascist_firewall_use_ipv6(const or_options_t *options); +MOCK_DECL(int, fascist_firewall_rand_prefer_ipv6_addr, (void)); int fascist_firewall_prefer_ipv6_orport(const or_options_t *options); int fascist_firewall_prefer_ipv6_dirport(const or_options_t *options); diff --git a/src/test/test_policy.c b/src/test/test_policy.c index 6a07e5b1f8..cc9023d6a6 100644 --- a/src/test/test_policy.c +++ b/src/test/test_policy.c @@ -2023,6 +2023,20 @@ test_policies_fascist_firewall_allows_address(void *arg) expect_ap); \ STMT_END +/** Mock the preferred address function to return zero (prefer IPv4). */ +static int +mock_fascist_firewall_rand_prefer_ipv6_addr_use_ipv4(void) +{ + return 0; +} + +/** Mock the preferred address function to return one (prefer IPv6). */ +static int +mock_fascist_firewall_rand_prefer_ipv6_addr_use_ipv6(void) +{ + return 1; +} + /** Run unit tests for fascist_firewall_choose_address */ static void test_policies_fascist_firewall_choose_address(void *arg) @@ -2421,6 +2435,42 @@ test_policies_fascist_firewall_choose_address(void *arg) CHECK_CHOSEN_ADDR_RN(fake_rs, fake_node, FIREWALL_DIR_CONNECTION, 1, 1, ipv4_dir_ap); + /* Test ClientAutoIPv6ORPort and pretend we prefer IPv4. */ + memset(&mock_options, 0, sizeof(or_options_t)); + mock_options.ClientAutoIPv6ORPort = 1; + mock_options.ClientUseIPv4 = 1; + mock_options.ClientUseIPv6 = 1; + MOCK(fascist_firewall_rand_prefer_ipv6_addr, + mock_fascist_firewall_rand_prefer_ipv6_addr_use_ipv4); + /* Simulate the initialisation of fake_node.ipv6_preferred */ + fake_node.ipv6_preferred = fascist_firewall_prefer_ipv6_orport( + &mock_options); + + CHECK_CHOSEN_ADDR_RN(fake_rs, fake_node, FIREWALL_OR_CONNECTION, 0, 1, + ipv4_or_ap); + CHECK_CHOSEN_ADDR_RN(fake_rs, fake_node, FIREWALL_OR_CONNECTION, 1, 1, + ipv4_or_ap); + + UNMOCK(fascist_firewall_rand_prefer_ipv6_addr); + + /* Test ClientAutoIPv6ORPort and pretend we prefer IPv6. */ + memset(&mock_options, 0, sizeof(or_options_t)); + mock_options.ClientAutoIPv6ORPort = 1; + mock_options.ClientUseIPv4 = 1; + mock_options.ClientUseIPv6 = 1; + MOCK(fascist_firewall_rand_prefer_ipv6_addr, + mock_fascist_firewall_rand_prefer_ipv6_addr_use_ipv6); + /* Simulate the initialisation of fake_node.ipv6_preferred */ + fake_node.ipv6_preferred = fascist_firewall_prefer_ipv6_orport( + &mock_options); + + CHECK_CHOSEN_ADDR_RN(fake_rs, fake_node, FIREWALL_OR_CONNECTION, 0, 1, + ipv6_or_ap); + CHECK_CHOSEN_ADDR_RN(fake_rs, fake_node, FIREWALL_OR_CONNECTION, 1, 1, + ipv6_or_ap); + + UNMOCK(fascist_firewall_rand_prefer_ipv6_addr); + done: UNMOCK(get_options); } |