[tor-commits] [tor/master] Restore and improve download schedule unit tests

nickm at torproject.org nickm at torproject.org
Thu May 19 12:30:29 UTC 2016


commit d5c70d71027644efb31d16d17b46d43484b46b18
Author: teor (Tim Wilson-Brown) <teor2345 at gmail.com>
Date:   Wed May 18 21:31:03 2016 -0400

    Restore and improve download schedule unit tests
---
 src/or/networkstatus.c |   8 +--
 src/or/networkstatus.h |   6 +-
 src/test/test_dir.c    | 153 ++++++++++++++++++++++++++++++++++++++++++++-----
 3 files changed, 146 insertions(+), 21 deletions(-)

diff --git a/src/or/networkstatus.c b/src/or/networkstatus.c
index 5c92830..5fa7a85 100644
--- a/src/or/networkstatus.c
+++ b/src/or/networkstatus.c
@@ -1237,8 +1237,8 @@ networkstatus_get_reasonably_live_consensus(time_t now, int flavor)
  * If we have no consensus, or our consensus is unusably old, return 1.
  * As soon as we have received a consensus, return 0, even if we don't have
  * enough certificates to validate it. */
-int
-networkstatus_consensus_is_bootstrapping(time_t now)
+MOCK_IMPL(int,
+networkstatus_consensus_is_bootstrapping,(time_t now))
 {
   /* If we have a validated, reasonably live consensus, we're not
    * bootstrapping a consensus at all. */
@@ -1273,8 +1273,8 @@ networkstatus_consensus_can_use_multiple_directories(
 /** Check if we can use fallback directory mirrors for a consensus download.
  * If we have fallbacks and don't want to fetch from the authorities,
  * we can use them. */
-int
-networkstatus_consensus_can_use_extra_fallbacks(const or_options_t *options)
+MOCK_IMPL(int,
+networkstatus_consensus_can_use_extra_fallbacks,(const or_options_t *options))
 {
   /* The list length comparisons are a quick way to check if we have any
    * non-authority fallback directories. If we ever have any authorities that
diff --git a/src/or/networkstatus.h b/src/or/networkstatus.h
index 609fb85..ac93e5d 100644
--- a/src/or/networkstatus.h
+++ b/src/or/networkstatus.h
@@ -70,11 +70,11 @@ MOCK_DECL(networkstatus_t *,networkstatus_get_latest_consensus_by_flavor,
 networkstatus_t *networkstatus_get_live_consensus(time_t now);
 networkstatus_t *networkstatus_get_reasonably_live_consensus(time_t now,
                                                              int flavor);
-int networkstatus_consensus_is_bootstrapping(time_t now);
+MOCK_DECL(int, networkstatus_consensus_is_bootstrapping,(time_t now));
 int networkstatus_consensus_can_use_multiple_directories(
                                                 const or_options_t *options);
-int networkstatus_consensus_can_use_extra_fallbacks(
-                                                const or_options_t *options);
+MOCK_DECL(int, networkstatus_consensus_can_use_extra_fallbacks,(
+                                                const or_options_t *options));
 int networkstatus_consensus_is_already_downloading(const char *resource);
 
 #define NSSET_FROM_CACHE 1
diff --git a/src/test/test_dir.c b/src/test/test_dir.c
index ea179fb..d9aff52 100644
--- a/src/test/test_dir.c
+++ b/src/test/test_dir.c
@@ -3995,12 +3995,56 @@ test_dir_choose_compression_level(void* data)
   done: ;
 }
 
+static int mock_networkstatus_consensus_is_bootstrapping_value = 0;
+static int
+mock_networkstatus_consensus_is_bootstrapping(time_t now)
+{
+  (void)now;
+  return mock_networkstatus_consensus_is_bootstrapping_value;
+}
+
+static int mock_networkstatus_consensus_can_use_extra_fallbacks_value = 0;
+static int
+mock_networkstatus_consensus_can_use_extra_fallbacks(
+                                                  const or_options_t *options)
+{
+  (void)options;
+  return mock_networkstatus_consensus_can_use_extra_fallbacks_value;
+}
+
+/* data is a 2 character nul-terminated string.
+ * If data[0] is 'b', set bootstrapping, anything else means not bootstrapping
+ * If data[1] is 'f', set extra fallbacks, anything else means no extra
+ * fallbacks.
+ */
 static void
 test_dir_find_dl_schedule(void* data)
 {
+  const char *str = (const char *)data;
+
+  tt_assert(strlen(data) == 2);
+
+  if (str[0] == 'b') {
+    mock_networkstatus_consensus_is_bootstrapping_value = 1;
+  } else {
+    mock_networkstatus_consensus_is_bootstrapping_value = 0;
+  }
+
+  if (str[1] == 'f') {
+    mock_networkstatus_consensus_can_use_extra_fallbacks_value = 1;
+  } else {
+    mock_networkstatus_consensus_can_use_extra_fallbacks_value = 0;
+  }
+
+  MOCK(networkstatus_consensus_is_bootstrapping,
+       mock_networkstatus_consensus_is_bootstrapping);
+  MOCK(networkstatus_consensus_can_use_extra_fallbacks,
+       mock_networkstatus_consensus_can_use_extra_fallbacks);
+
   download_status_t dls;
-  smartlist_t server, client, server_cons, client_cons, bridge;
-  (void)data;
+  smartlist_t server, client, server_cons, client_cons;
+  smartlist_t client_boot_auth_only_cons, client_boot_auth_cons;
+  smartlist_t client_boot_fallback_cons, bridge;
 
   mock_options = malloc(sizeof(or_options_t));
   reset_options(mock_options, &mock_get_options_calls);
@@ -4010,43 +4054,121 @@ test_dir_find_dl_schedule(void* data)
   mock_options->TestingClientDownloadSchedule = &client;
   mock_options->TestingServerConsensusDownloadSchedule = &server_cons;
   mock_options->TestingClientConsensusDownloadSchedule = &client_cons;
+  mock_options->ClientBootstrapConsensusAuthorityOnlyDownloadSchedule =
+    &client_boot_auth_only_cons;
+  mock_options->ClientBootstrapConsensusAuthorityDownloadSchedule =
+    &client_boot_auth_cons;
+  mock_options->ClientBootstrapConsensusFallbackDownloadSchedule =
+    &client_boot_fallback_cons;
   mock_options->TestingBridgeDownloadSchedule = &bridge;
 
   dls.schedule = DL_SCHED_GENERIC;
+  /* client */
   mock_options->ClientOnly = 1;
   tt_ptr_op(find_dl_schedule(&dls, mock_options), OP_EQ, &client);
   mock_options->ClientOnly = 0;
+
+  /* dir mode */
   mock_options->DirPort_set = 1;
-  mock_options->ORPort_set = 1;
   mock_options->DirCache = 1;
   tt_ptr_op(find_dl_schedule(&dls, mock_options), OP_EQ, &server);
+  mock_options->DirPort_set = 0;
+  mock_options->DirCache = 0;
 
-#if 0
   dls.schedule = DL_SCHED_CONSENSUS;
-  mock_options->ClientOnly = 1;
-  mock_options->DirCache = 0;
-  tt_ptr_op(find_dl_schedule(&dls, mock_options), OP_EQ, &client_cons);
-  mock_options->ClientOnly = 0;
-  mock_options->DirCache = 1;
+  /* public server mode */
+  mock_options->ORPort_set = 1;
   tt_ptr_op(find_dl_schedule(&dls, mock_options), OP_EQ, &server_cons);
-#endif
+  mock_options->ORPort_set = 0;
+
+  /* client and bridge modes */
+  if (networkstatus_consensus_is_bootstrapping(time(NULL))) {
+    if (networkstatus_consensus_can_use_extra_fallbacks(mock_options)) {
+      dls.want_authority = 1;
+      /* client */
+      mock_options->ClientOnly = 1;
+      tt_ptr_op(find_dl_schedule(&dls, mock_options), OP_EQ,
+                &client_boot_auth_cons);
+      mock_options->ClientOnly = 0;
+
+      /* bridge relay */
+      mock_options->ORPort_set = 1;
+      mock_options->BridgeRelay = 1;
+      tt_ptr_op(find_dl_schedule(&dls, mock_options), OP_EQ,
+                &client_boot_auth_cons);
+      mock_options->ORPort_set = 0;
+      mock_options->BridgeRelay = 0;
+
+      dls.want_authority = 0;
+      /* client */
+      mock_options->ClientOnly = 1;
+      tt_ptr_op(find_dl_schedule(&dls, mock_options), OP_EQ,
+                &client_boot_fallback_cons);
+      mock_options->ClientOnly = 0;
+
+      /* bridge relay */
+      mock_options->ORPort_set = 1;
+      mock_options->BridgeRelay = 1;
+      tt_ptr_op(find_dl_schedule(&dls, mock_options), OP_EQ,
+                &client_boot_fallback_cons);
+      mock_options->ORPort_set = 0;
+      mock_options->BridgeRelay = 0;
+
+    } else {
+      /* dls.want_authority is ignored */
+      /* client */
+      mock_options->ClientOnly = 1;
+      tt_ptr_op(find_dl_schedule(&dls, mock_options), OP_EQ,
+                &client_boot_auth_only_cons);
+      mock_options->ClientOnly = 0;
+
+      /* bridge relay */
+      mock_options->ORPort_set = 1;
+      mock_options->BridgeRelay = 1;
+      tt_ptr_op(find_dl_schedule(&dls, mock_options), OP_EQ,
+                &client_boot_auth_only_cons);
+      mock_options->ORPort_set = 0;
+      mock_options->BridgeRelay = 0;
+    }
+  } else {
+    /* client */
+    mock_options->ClientOnly = 1;
+    tt_ptr_op(find_dl_schedule(&dls, mock_options), OP_EQ,
+              &client_cons);
+    mock_options->ClientOnly = 0;
+
+    /* bridge relay */
+    mock_options->ORPort_set = 1;
+    mock_options->BridgeRelay = 1;
+    tt_ptr_op(find_dl_schedule(&dls, mock_options), OP_EQ,
+              &client_cons);
+    mock_options->ORPort_set = 0;
+    mock_options->BridgeRelay = 0;
+  }
 
   dls.schedule = DL_SCHED_BRIDGE;
+  /* client */
   mock_options->ClientOnly = 1;
   tt_ptr_op(find_dl_schedule(&dls, mock_options), OP_EQ, &bridge);
-  mock_options->ClientOnly = 0;
-  tt_ptr_op(find_dl_schedule(&dls, mock_options), OP_EQ, &bridge);
 
  done:
+  UNMOCK(networkstatus_consensus_is_bootstrapping);
+  UNMOCK(networkstatus_consensus_can_use_extra_fallbacks);
   UNMOCK(get_options);
+  free(mock_options);
+  mock_options = NULL;
 }
 
-#define DIR_LEGACY(name)                                                   \
+#define DIR_LEGACY(name)                             \
   { #name, test_dir_ ## name , TT_FORK, NULL, NULL }
 
 #define DIR(name,flags)                              \
   { #name, test_dir_##name, (flags), NULL, NULL }
 
+/* where arg is a string constant */
+#define DIR_ARG(name,flags,arg)                      \
+  { #name "_" arg, test_dir_##name, (flags), &passthrough_setup, arg }
+
 struct testcase_t dir_tests[] = {
   DIR_LEGACY(nicknames),
   DIR_LEGACY(formats),
@@ -4081,7 +4203,10 @@ struct testcase_t dir_tests[] = {
   DIR(should_not_init_request_to_dir_auths_without_v3_info, 0),
   DIR(should_init_request_to_dir_auths, 0),
   DIR(choose_compression_level, 0),
-  DIR(find_dl_schedule, 0),
+  DIR_ARG(find_dl_schedule, TT_FORK, "bf"),
+  DIR_ARG(find_dl_schedule, TT_FORK, "ba"),
+  DIR_ARG(find_dl_schedule, TT_FORK, "cf"),
+  DIR_ARG(find_dl_schedule, TT_FORK, "ca"),
   END_OF_TESTCASES
 };
 





More information about the tor-commits mailing list