Переглянути джерело

Use {mock,unmock}_hostname_resolver() in relevant tests

These tests all invoke the hostname resolver in one way or another,
and therefore potentially block if our DNS server is missing,
absent, or extremely slow.  Closes ticket 31841.
Nick Mathewson 4 роки тому
батько
коміт
fdfb4b196b

+ 5 - 0
changes/ticket31841

@@ -0,0 +1,5 @@
+  o Minor features (testing):
+    - When running tests that attempt to look up hostname, replace the libc
+      name lookup functions with ones that do not actually touch the network.
+      This way, the tests complete more quickly in the presence of a slow or
+      missing DNS resolver. Closes ticket 31841.

+ 5 - 1
src/test/test_addr.c

@@ -12,6 +12,7 @@
 #include "test/log_test_helpers.h"
 #include "test/log_test_helpers.h"
 #include "lib/net/resolve.h"
 #include "lib/net/resolve.h"
 #include "test/rng_test_helpers.h"
 #include "test/rng_test_helpers.h"
+#include "test/resolve_test_helpers.h"
 
 
 #ifdef HAVE_SYS_UN_H
 #ifdef HAVE_SYS_UN_H
 #include <sys/un.h>
 #include <sys/un.h>
@@ -1160,6 +1161,7 @@ test_addr_parse_canonical(void *arg)
 static void
 static void
 test_addr_parse(void *arg)
 test_addr_parse(void *arg)
 {
 {
+
   int r;
   int r;
   tor_addr_t addr;
   tor_addr_t addr;
   uint16_t port;
   uint16_t port;
@@ -1169,6 +1171,8 @@ test_addr_parse(void *arg)
 
 
   (void)arg;
   (void)arg;
 
 
+  mock_hostname_resolver();
+
   /* IPv6-mapped IPv4 addresses. Tor doesn't really use these. */
   /* IPv6-mapped IPv4 addresses. Tor doesn't really use these. */
   TEST_ADDR_V6_PARSE("11:22:33:44:55:66:1.2.3.4", 0,
   TEST_ADDR_V6_PARSE("11:22:33:44:55:66:1.2.3.4", 0,
                      "11:22:33:44:55:66:102:304");
                      "11:22:33:44:55:66:102:304");
@@ -1273,7 +1277,7 @@ test_addr_parse(void *arg)
                            "11:22::88",99);
                            "11:22::88",99);
 
 
  done:
  done:
-  ;
+  unmock_hostname_resolver();
 }
 }
 
 
 static void
 static void

+ 4 - 0
src/test/test_config.c

@@ -45,6 +45,7 @@
 #include "app/config/statefile.h"
 #include "app/config/statefile.h"
 
 
 #include "test/test_helpers.h"
 #include "test/test_helpers.h"
+#include "test/resolve_test_helpers.h"
 
 
 #include "feature/dirclient/dir_server_st.h"
 #include "feature/dirclient/dir_server_st.h"
 #include "core/or/port_cfg_st.h"
 #include "core/or/port_cfg_st.h"
@@ -4068,6 +4069,8 @@ test_config_parse_port_config__ports__ports_given(void *data)
 
 
   slout = smartlist_new();
   slout = smartlist_new();
 
 
+  mock_hostname_resolver();
+
   // Test error when encounters an invalid Port specification
   // Test error when encounters an invalid Port specification
   config_port_invalid = mock_config_line("DNSPort", "");
   config_port_invalid = mock_config_line("DNSPort", "");
   ret = parse_port_config(NULL, config_port_invalid, "DNS", 0, NULL,
   ret = parse_port_config(NULL, config_port_invalid, "DNS", 0, NULL,
@@ -4764,6 +4767,7 @@ test_config_parse_port_config__ports__ports_given(void *data)
 #endif /* defined(_WIN32) */
 #endif /* defined(_WIN32) */
 
 
  done:
  done:
+  unmock_hostname_resolver();
   if (slout)
   if (slout)
     SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
     SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
   smartlist_free(slout);
   smartlist_free(slout);

+ 5 - 3
src/test/test_hs_config.c

@@ -12,6 +12,7 @@
 #include "test/test.h"
 #include "test/test.h"
 #include "test/test_helpers.h"
 #include "test/test_helpers.h"
 #include "test/log_test_helpers.h"
 #include "test/log_test_helpers.h"
+#include "test/resolve_test_helpers.h"
 
 
 #include "app/config/config.h"
 #include "app/config/config.h"
 #include "feature/hs/hs_common.h"
 #include "feature/hs/hs_common.h"
@@ -272,6 +273,7 @@ test_valid_service_v2(void *arg)
   int ret;
   int ret;
 
 
   (void) arg;
   (void) arg;
+  mock_hostname_resolver();
 
 
   /* Valid complex configuration. Basic client authorization. */
   /* Valid complex configuration. Basic client authorization. */
   {
   {
@@ -314,7 +316,7 @@ test_valid_service_v2(void *arg)
   }
   }
 
 
  done:
  done:
-  ;
+  unmock_hostname_resolver();
 }
 }
 
 
 static void
 static void
@@ -392,6 +394,7 @@ test_valid_service_v3(void *arg)
   int ret;
   int ret;
 
 
   (void) arg;
   (void) arg;
+  mock_hostname_resolver();
 
 
   /* Valid complex configuration. */
   /* Valid complex configuration. */
   {
   {
@@ -448,7 +451,7 @@ test_valid_service_v3(void *arg)
   }
   }
 
 
  done:
  done:
-  ;
+  unmock_hostname_resolver();
 }
 }
 
 
 static void
 static void
@@ -623,4 +626,3 @@ struct testcase_t hs_config_tests[] = {
 
 
   END_OF_TESTCASES
   END_OF_TESTCASES
 };
 };
-

+ 3 - 0
src/test/test_options.c

@@ -14,6 +14,7 @@
 #include "feature/nodelist/routerset.h"
 #include "feature/nodelist/routerset.h"
 #include "core/mainloop/mainloop.h"
 #include "core/mainloop/mainloop.h"
 #include "test/log_test_helpers.h"
 #include "test/log_test_helpers.h"
+#include "test/resolve_test_helpers.h"
 
 
 #include "lib/sandbox/sandbox.h"
 #include "lib/sandbox/sandbox.h"
 #include "lib/memarea/memarea.h"
 #include "lib/memarea/memarea.h"
@@ -241,6 +242,7 @@ test_options_validate(void *arg)
   (void)arg;
   (void)arg;
   setup_log_callback();
   setup_log_callback();
   sandbox_disable_getaddrinfo_cache();
   sandbox_disable_getaddrinfo_cache();
+  mock_hostname_resolver();
 
 
   WANT_ERR("ExtORPort 500000", "Invalid ExtORPort", PH_VALIDATE);
   WANT_ERR("ExtORPort 500000", "Invalid ExtORPort", PH_VALIDATE);
 
 
@@ -282,6 +284,7 @@ test_options_validate(void *arg)
 
 
   close_temp_logs();
   close_temp_logs();
   clear_log_messages();
   clear_log_messages();
+  unmock_hostname_resolver();
   return;
   return;
 }
 }