[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[tor-commits] [tor/master] Add unit tests for networkstatus_consensus_is_bootstrapping



commit f698b509d8efdc055cfc05c6feaf0aecee41b9d0
Author: teor (Tim Wilson-Brown) <teor2345@xxxxxxxxx>
Date:   Wed May 18 22:05:35 2016 -0400

    Add unit tests for networkstatus_consensus_is_bootstrapping
---
 src/or/microdesc.c         |  4 ++--
 src/or/microdesc.h         |  2 +-
 src/test/test_routerlist.c | 48 ++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 51 insertions(+), 3 deletions(-)

diff --git a/src/or/microdesc.c b/src/or/microdesc.c
index 2990429..5b5c29a 100644
--- a/src/or/microdesc.c
+++ b/src/or/microdesc.c
@@ -955,8 +955,8 @@ we_fetch_router_descriptors(const or_options_t *options)
 }
 
 /** Return the consensus flavor we actually want to use to build circuits. */
-int
-usable_consensus_flavor(void)
+MOCK_IMPL(int,
+usable_consensus_flavor,(void))
 {
   if (we_use_microdescriptors_for_circuits(get_options())) {
     return FLAV_MICRODESC;
diff --git a/src/or/microdesc.h b/src/or/microdesc.h
index 0675e23..40c8313 100644
--- a/src/or/microdesc.h
+++ b/src/or/microdesc.h
@@ -47,7 +47,7 @@ void microdesc_free_all(void);
 void update_microdesc_downloads(time_t now);
 void update_microdescs_from_networkstatus(time_t now);
 
-int usable_consensus_flavor(void);
+MOCK_DECL(int, usable_consensus_flavor,(void));
 int we_fetch_microdescriptors(const or_options_t *options);
 int we_fetch_router_descriptors(const or_options_t *options);
 int we_use_microdescriptors_for_circuits(const or_options_t *options);
diff --git a/src/test/test_routerlist.c b/src/test/test_routerlist.c
index 4976069..2cffa6e 100644
--- a/src/test/test_routerlist.c
+++ b/src/test/test_routerlist.c
@@ -15,6 +15,7 @@
 #include "container.h"
 #include "directory.h"
 #include "dirvote.h"
+#include "microdesc.h"
 #include "networkstatus.h"
 #include "nodelist.h"
 #include "policies.h"
@@ -190,6 +191,14 @@ construct_consensus(char **consensus_text_md)
   crypto_pk_free(sign_skey_leg);
 }
 
+static int mock_usable_consensus_flavor_value = FLAV_NS;
+
+static int
+mock_usable_consensus_flavor(void)
+{
+  return mock_usable_consensus_flavor_value;
+}
+
 static void
 test_router_pick_directory_server_impl(void *arg)
 {
@@ -209,6 +218,22 @@ test_router_pick_directory_server_impl(void *arg)
 
   (void)arg;
 
+  MOCK(usable_consensus_flavor, mock_usable_consensus_flavor);
+
+  /* With no consensus, we must be bootstrapping, regardless of time or flavor
+   */
+  mock_usable_consensus_flavor_value = FLAV_NS;
+  tt_assert(networkstatus_consensus_is_bootstrapping(now));
+  tt_assert(networkstatus_consensus_is_bootstrapping(now + 2000));
+  tt_assert(networkstatus_consensus_is_bootstrapping(now + 2*24*60*60));
+  tt_assert(networkstatus_consensus_is_bootstrapping(now - 2*24*60*60));
+
+  mock_usable_consensus_flavor_value = FLAV_MICRODESC;
+  tt_assert(networkstatus_consensus_is_bootstrapping(now));
+  tt_assert(networkstatus_consensus_is_bootstrapping(now + 2000));
+  tt_assert(networkstatus_consensus_is_bootstrapping(now + 2*24*60*60));
+  tt_assert(networkstatus_consensus_is_bootstrapping(now - 2*24*60*60));
+
   /* No consensus available, fail early */
   rs = router_pick_directory_server_impl(V3_DIRINFO, (const int) 0, NULL);
   tt_assert(rs == NULL);
@@ -223,6 +248,28 @@ test_router_pick_directory_server_impl(void *arg)
   tt_int_op(smartlist_len(con_md->routerstatus_list), ==, 3);
   tt_assert(!networkstatus_set_current_consensus_from_ns(con_md,
                                                  "microdesc"));
+
+  /* If the consensus time or flavor doesn't match, we are still
+   * bootstrapping */
+  mock_usable_consensus_flavor_value = FLAV_NS;
+  tt_assert(networkstatus_consensus_is_bootstrapping(now));
+  tt_assert(networkstatus_consensus_is_bootstrapping(now + 2000));
+  tt_assert(networkstatus_consensus_is_bootstrapping(now + 2*24*60*60));
+  tt_assert(networkstatus_consensus_is_bootstrapping(now - 2*24*60*60));
+
+  /* With a valid consensus for the current time and flavor, we stop
+   * bootstrapping, even if we have no certificates */
+  mock_usable_consensus_flavor_value = FLAV_MICRODESC;
+  tt_assert(!networkstatus_consensus_is_bootstrapping(now + 2000));
+  tt_assert(!networkstatus_consensus_is_bootstrapping(con_md->valid_after));
+  tt_assert(!networkstatus_consensus_is_bootstrapping(con_md->valid_until));
+  tt_assert(!networkstatus_consensus_is_bootstrapping(con_md->valid_until
+                                                      + 24*60*60));
+  /* These times are outside the test validity period */
+  tt_assert(networkstatus_consensus_is_bootstrapping(now));
+  tt_assert(networkstatus_consensus_is_bootstrapping(now + 2*24*60*60));
+  tt_assert(networkstatus_consensus_is_bootstrapping(now - 2*24*60*60));
+
   nodelist_set_consensus(con_md);
   nodelist_assert_ok();
 
@@ -362,6 +409,7 @@ test_router_pick_directory_server_impl(void *arg)
   node_router1->rs->last_dir_503_at = 0;
 
  done:
+  UNMOCK(usable_consensus_flavor);
   if (router1_id)
     tor_free(router1_id);
   if (router2_id)



_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits