From d827902cb1107593473552974be5fcbb1fbb4abe Mon Sep 17 00:00:00 2001 From: rl1987 Date: Sat, 20 Oct 2018 20:15:06 +0300 Subject: Unit test for DNS fallback in configure_nameservers --- src/test/test_dns.c | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) (limited to 'src/test') diff --git a/src/test/test_dns.c b/src/test/test_dns.c index 8369f844f6..ea59a49b5b 100644 --- a/src/test/test_dns.c +++ b/src/test/test_dns.c @@ -13,9 +13,69 @@ #include "core/or/edge_connection_st.h" #include "core/or/or_circuit_st.h" +#include "app/config/or_options_st.h" +#include "app/config/config.h" + +#include +#include #define NS_MODULE dns +#define NS_SUBMODULE configure_nameservers_fallback + +static or_options_t options = { + .ORPort_set = 1, +}; + +static const or_options_t * +mock_get_options(void) +{ + return &options; +} + +static void +NS(test_main)(void *arg) +{ + (void)arg; + tor_addr_t *nameserver_addr = NULL; + + MOCK(get_options, mock_get_options); + + options.ServerDNSResolvConfFile = (char *)"no_such_file!!!"; + + dns_init(); // calls configure_nameservers() + + tt_int_op(number_of_configured_nameservers(), OP_EQ, 1); + + nameserver_addr = configured_nameserver_address(0); + + tt_assert(tor_addr_family(nameserver_addr) == AF_INET); + tt_assert(tor_addr_eq_ipv4h(nameserver_addr, 0x7f000001)); + +#ifndef _WIN32 + tor_free(nameserver_addr); + + options.ServerDNSResolvConfFile = (char *)"/dev/null"; + + dns_init(); + + tt_int_op(number_of_configured_nameservers(), OP_EQ, 1); + + nameserver_addr = configured_nameserver_address(0); + + tt_assert(tor_addr_family(nameserver_addr) == AF_INET); + tt_assert(tor_addr_eq_ipv4h(nameserver_addr, 0x7f000001)); +#endif + + UNMOCK(get_options); + + done: + tor_free(nameserver_addr); + return; +} + +#undef NS_SUBMODULE + #define NS_SUBMODULE clip_ttl static void @@ -736,6 +796,7 @@ NS(test_main)(void *arg) #undef NS_SUBMODULE struct testcase_t dns_tests[] = { + TEST_CASE(configure_nameservers_fallback), TEST_CASE(clip_ttl), TEST_CASE(resolve), TEST_CASE_ASPECT(resolve_impl, addr_is_ip_no_need_to_resolve), -- cgit v1.2.3-54-g00ecf From 98cef6807eb70e7c459f6f80a06f894fac63100a Mon Sep 17 00:00:00 2001 From: rl1987 Date: Sat, 20 Oct 2018 20:32:26 +0300 Subject: Exclude test and a supporting function when evdns_base_get_nameserver_addr() is not available --- src/feature/relay/dns.c | 2 ++ src/feature/relay/dns.h | 2 ++ src/test/test_dns.c | 5 +++++ 3 files changed, 9 insertions(+) (limited to 'src/test') diff --git a/src/feature/relay/dns.c b/src/feature/relay/dns.c index 1b4914b49a..dfd84652ad 100644 --- a/src/feature/relay/dns.c +++ b/src/feature/relay/dns.c @@ -1366,6 +1366,7 @@ number_of_configured_nameservers(void) return evdns_base_count_nameservers(the_evdns_base); } +#ifdef HAVE_EVDNS_BASE_GET_NAMESERVER_ADDR /** * Return address of configured nameserver in the_evdns_base * at index idx. @@ -1386,6 +1387,7 @@ configured_nameserver_address(const size_t idx) return NULL; } +#endif /** Configure eventdns nameservers if force is true, or if the configuration * has changed since the last time we called this function, or if we failed on diff --git a/src/feature/relay/dns.h b/src/feature/relay/dns.h index 3a17ea7b36..5758ea4363 100644 --- a/src/feature/relay/dns.h +++ b/src/feature/relay/dns.h @@ -46,7 +46,9 @@ size_t dns_cache_handle_oom(time_t now, size_t min_remove_bytes); #include "feature/relay/dns_structs.h" size_t number_of_configured_nameservers(void); +#ifdef HAVE_EVDNS_BASE_GET_NAMESERVER_ADDR tor_addr_t *configured_nameserver_address(const size_t idx); +#endif MOCK_DECL(STATIC int,dns_resolve_impl,(edge_connection_t *exitconn, int is_resolve,or_circuit_t *oncirc, char **hostname_out, diff --git a/src/test/test_dns.c b/src/test/test_dns.c index ea59a49b5b..ea0fcf8c5e 100644 --- a/src/test/test_dns.c +++ b/src/test/test_dns.c @@ -1,6 +1,7 @@ /* Copyright (c) 2015-2018, The Tor Project, Inc. */ /* See LICENSE for licensing information */ +#include "orconfig.h" #include "core/or/or.h" #include "test/test.h" @@ -21,6 +22,7 @@ #define NS_MODULE dns +#ifdef HAVE_EVDNS_BASE_GET_NAMESERVER_ADDR #define NS_SUBMODULE configure_nameservers_fallback static or_options_t options = { @@ -75,6 +77,7 @@ NS(test_main)(void *arg) } #undef NS_SUBMODULE +#endif #define NS_SUBMODULE clip_ttl @@ -796,7 +799,9 @@ NS(test_main)(void *arg) #undef NS_SUBMODULE struct testcase_t dns_tests[] = { +#ifdef HAVE_EVDNS_BASE_GET_NAMESERVER_ADDR TEST_CASE(configure_nameservers_fallback), +#endif TEST_CASE(clip_ttl), TEST_CASE(resolve), TEST_CASE_ASPECT(resolve_impl, addr_is_ip_no_need_to_resolve), -- cgit v1.2.3-54-g00ecf