summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorteor (Tim Wilson-Brown) <teor2345@gmail.com>2016-05-18 22:05:35 -0400
committerNick Mathewson <nickm@torproject.org>2016-05-19 07:58:41 -0400
commitf698b509d8efdc055cfc05c6feaf0aecee41b9d0 (patch)
tree7d74711f1ee69f74def145f3517ecee2d69a202a /src
parentd5c70d71027644efb31d16d17b46d43484b46b18 (diff)
downloadtor-f698b509d8efdc055cfc05c6feaf0aecee41b9d0.tar.gz
tor-f698b509d8efdc055cfc05c6feaf0aecee41b9d0.zip
Add unit tests for networkstatus_consensus_is_bootstrapping
Diffstat (limited to 'src')
-rw-r--r--src/or/microdesc.c4
-rw-r--r--src/or/microdesc.h2
-rw-r--r--src/test/test_routerlist.c48
3 files changed, 51 insertions, 3 deletions
diff --git a/src/or/microdesc.c b/src/or/microdesc.c
index 299042995b..5b5c29a6d2 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 0675e233d6..40c83139e9 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 497606920d..2cffa6e801 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)