[tor-commits] [tor/master] Sixth test case for dns_resolve_impl.

nickm at torproject.org nickm at torproject.org
Fri Oct 30 14:07:17 UTC 2015


commit f53dcf6a357e83a0b018d6f66bfb8a30734bd9a9
Author: rl1987 <rl1987 at sdf.lonestar.org>
Date:   Tue Oct 20 20:40:21 2015 +0300

    Sixth test case for dns_resolve_impl.
---
 src/or/dns.c        |   11 ++++-----
 src/or/dns.h        |    5 ++++
 src/test/test_dns.c |   66 ++++++++++++++++++++++++++++++++++++++++++++++++++-
 3 files changed, 74 insertions(+), 8 deletions(-)

diff --git a/src/or/dns.c b/src/or/dns.c
index 234dfbe..297ac6f 100644
--- a/src/or/dns.c
+++ b/src/or/dns.c
@@ -111,9 +111,6 @@ static int launch_resolve(cached_resolve_t *resolve);
 static void add_wildcarded_test_address(const char *address);
 static int configure_nameservers(int force);
 static int answer_is_wildcarded(const char *ip);
-static int set_exitconn_info_from_resolve(edge_connection_t *exitconn,
-                                          const cached_resolve_t *resolve,
-                                          char **hostname_out);
 static int evdns_err_is_transient(int err);
 static void inform_pending_connections(cached_resolve_t *resolve);
 static void make_pending_resolve_cached(cached_resolve_t *cached);
@@ -859,10 +856,10 @@ dns_resolve_impl,(edge_connection_t *exitconn, int is_resolve,
  * Return -2 on a transient error, -1 on a permenent error, and 1 on
  * a successful lookup.
  */
-static int
-set_exitconn_info_from_resolve(edge_connection_t *exitconn,
-                               const cached_resolve_t *resolve,
-                               char **hostname_out)
+MOCK_IMPL(STATIC int,
+set_exitconn_info_from_resolve,(edge_connection_t *exitconn,
+                                const cached_resolve_t *resolve,
+                                char **hostname_out))
 {
   int ipv4_ok, ipv6_ok, answer_with_ipv4, r;
   uint32_t begincell_flags;
diff --git a/src/or/dns.h b/src/or/dns.h
index 163d61b..64e970a 100644
--- a/src/or/dns.h
+++ b/src/or/dns.h
@@ -46,6 +46,11 @@ const char *hostname));
 cached_resolve_t *dns_get_cache_entry(cached_resolve_t *query);
 void dns_insert_cache_entry(cached_resolve_t *new_entry);
 
+MOCK_DECL(STATIC int,
+set_exitconn_info_from_resolve,(edge_connection_t *exitconn,
+                                const cached_resolve_t *resolve,
+                                char **hostname_out));
+
 #endif
 
 #endif
diff --git a/src/test/test_dns.c b/src/test/test_dns.c
index 3fbb6f2..0ed3c9d 100644
--- a/src/test/test_dns.c
+++ b/src/test/test_dns.c
@@ -576,12 +576,76 @@ NS(test_main)(void *arg)
 
 #define NS_SUBMODULE ASPECT(resolve_impl, cache_hit_cached)
 
+/* Given that a finished DNS resolve is available in our cache, we want
+ * dns_resolve_impl() return it to called via resolve_out and pass the
+ * handling to set_exitconn_info_from_resolve function.
+ */
+static int
+NS(router_my_exit_policy_is_reject_star)(void)
+{
+  return 0;
+}
+
+static edge_connection_t *last_exitconn = NULL;
+static cached_resolve_t *last_resolve = NULL;
+
+static int
+NS(set_exitconn_info_from_resolve)(edge_connection_t *exitconn,
+                                   const cached_resolve_t *resolve,
+                                   char **hostname_out)
+{
+  last_exitconn = exitconn;
+  last_resolve = (cached_resolve_t *)resolve;
+
+  return 0;
+}
+
 static void
 NS(test_main)(void *arg)
 {
-  tt_skip();
+  int retval;
+  int made_pending = 0;
+
+  edge_connection_t *exitconn = create_valid_exitconn();
+  or_circuit_t *on_circ = tor_malloc_zero(sizeof(or_circuit_t));
+
+  cached_resolve_t *resolve_out = NULL;
+
+  cached_resolve_t *cache_entry = tor_malloc_zero(sizeof(cached_resolve_t));
+  cache_entry->magic = CACHED_RESOLVE_MAGIC;
+  cache_entry->state = CACHE_STATE_CACHED;
+  cache_entry->minheap_idx = -1;
+  cache_entry->expire = time(NULL) + 60 * 60;
+
+  TO_CONN(exitconn)->address = tor_strdup("torproject.org");
+
+  strlcpy(cache_entry->address, TO_CONN(exitconn)->address,
+          sizeof(cache_entry->address));
+
+  NS_MOCK(router_my_exit_policy_is_reject_star);
+  NS_MOCK(set_exitconn_info_from_resolve);
+
+  dns_init();
+
+  dns_insert_cache_entry(cache_entry);
+
+  retval = dns_resolve_impl(exitconn, 1, on_circ, NULL, &made_pending,
+                            &resolve_out);
+
+  tt_int_op(retval,==,0);
+  tt_int_op(made_pending,==,0);
+  tt_assert(resolve_out == cache_entry);
+
+  tt_assert(last_exitconn == exitconn);
+  tt_assert(last_resolve == cache_entry);
 
   done:
+  NS_UNMOCK(router_my_exit_policy_is_reject_star);
+  NS_UNMOCK(set_exitconn_info_from_resolve);
+  tor_free(on_circ);
+  tor_free(TO_CONN(exitconn)->address);
+  tor_free(cache_entry->pending_connections);
+  tor_free(cache_entry);
   return;
 }
 





More information about the tor-commits mailing list