aboutsummaryrefslogtreecommitdiff
path: root/src/test/test_routerlist.c
diff options
context:
space:
mode:
authorTaylor Yu <catalyst@torproject.org>2018-05-03 21:53:24 -0500
committerTaylor Yu <catalyst@torproject.org>2018-05-08 17:59:03 -0500
commit4921670a8c155965e919debc3048a76b3e868802 (patch)
treef1cf65f2ae0b10071330faa7eff9e672a5796e55 /src/test/test_routerlist.c
parentbe8306c1fb344d9ff3c03b5dc1bb8815706d4ae7 (diff)
downloadtor-4921670a8c155965e919debc3048a76b3e868802.tar.gz
tor-4921670a8c155965e919debc3048a76b3e868802.zip
Test early-consensus clock skew warnings
Diffstat (limited to 'src/test/test_routerlist.c')
-rw-r--r--src/test/test_routerlist.c105
1 files changed, 105 insertions, 0 deletions
diff --git a/src/test/test_routerlist.c b/src/test/test_routerlist.c
index 88894391b7..6dd58d3138 100644
--- a/src/test/test_routerlist.c
+++ b/src/test/test_routerlist.c
@@ -599,11 +599,111 @@ test_routerlist_router_is_already_dir_fetching(void *arg)
#undef TEST_ADDR_STR
#undef TEST_DIR_PORT
+static long mock_apparent_skew = 0;
+
+/** Store apparent_skew and assert that the other arguments are as
+ * expected. */
+static void
+mock_clock_skew_warning(const connection_t *conn, long apparent_skew,
+ int trusted, log_domain_mask_t domain,
+ const char *received, const char *source)
+{
+ (void)conn;
+ mock_apparent_skew = apparent_skew;
+ tt_int_op(trusted, OP_EQ, 1);
+ tt_int_op(domain, OP_EQ, LD_GENERAL);
+ tt_str_op(received, OP_EQ, "microdesc flavor consensus");
+ tt_str_op(source, OP_EQ, "CONSENSUS");
+ done:
+ ;
+}
+
+/** Do common setup for test_timely_consensus() and
+ * test_early_consensus(). Call networkstatus_set_current_consensus()
+ * on a constructed consensus and with an appropriately-modified
+ * approx_time. Callers expect presence or absence of appropriate log
+ * messages and control events. */
+static int
+test_skew_common(void *arg, time_t now, unsigned long *offset)
+{
+ char *consensus = NULL;
+ int retval = 0;
+
+ *offset = strtoul(arg, NULL, 10);
+
+ /* Initialize the SRV subsystem */
+ MOCK(get_my_v3_authority_cert, get_my_v3_authority_cert_m);
+ mock_cert = authority_cert_parse_from_string(AUTHORITY_CERT_1, NULL);
+ sr_init(0);
+ UNMOCK(get_my_v3_authority_cert);
+
+ construct_consensus(&consensus, now);
+ tt_assert(consensus);
+
+ update_approx_time(now + *offset);
+
+ mock_apparent_skew = 0;
+ /* Caller will call UNMOCK() */
+ MOCK(clock_skew_warning, mock_clock_skew_warning);
+ /* Caller will call teardown_capture_of_logs() */
+ setup_capture_of_logs(LOG_WARN);
+ retval = networkstatus_set_current_consensus(consensus, "microdesc", 0,
+ NULL);
+
+ done:
+ tor_free(consensus);
+ return retval;
+}
+
+/** Test non-early consensus */
+static void
+test_timely_consensus(void *arg)
+{
+ time_t now = time(NULL);
+ unsigned long offset = 0;
+ int retval = 0;
+
+ retval = test_skew_common(arg, now, &offset);
+ (void)offset;
+ expect_no_log_msg_containing("behind the time published in the consensus");
+ tt_int_op(retval, OP_EQ, 0);
+ tt_int_op(mock_apparent_skew, OP_EQ, 0);
+ done:
+ teardown_capture_of_logs();
+ UNMOCK(clock_skew_warning);
+}
+
+/** Test early consensus */
+static void
+test_early_consensus(void *arg)
+{
+ time_t now = time(NULL);
+ unsigned long offset = 0;
+ int retval = 0;
+
+ retval = test_skew_common(arg, now, &offset);
+ /* Can't use expect_single_log_msg() because of unrecognized authorities */
+ expect_log_msg_containing("behind the time published in the consensus");
+ tt_int_op(retval, OP_EQ, 0);
+ /* This depends on construct_consensus() setting valid_after=now+1000 */
+ tt_int_op(mock_apparent_skew, OP_EQ, offset - 1000);
+ done:
+ teardown_capture_of_logs();
+ UNMOCK(clock_skew_warning);
+}
+
#define NODE(name, flags) \
{ #name, test_routerlist_##name, (flags), NULL, NULL }
#define ROUTER(name,flags) \
{ #name, test_router_##name, (flags), NULL, NULL }
+#define TIMELY(name, arg) \
+ { name, test_timely_consensus, TT_FORK, &passthrough_setup, \
+ (char *)(arg) }
+#define EARLY(name, arg) \
+ { name, test_early_consensus, TT_FORK, &passthrough_setup, \
+ (char *)(arg) }
+
struct testcase_t routerlist_tests[] = {
NODE(initiate_descriptor_downloads, 0),
NODE(launch_descriptor_downloads, 0),
@@ -611,6 +711,11 @@ struct testcase_t routerlist_tests[] = {
ROUTER(pick_directory_server_impl, TT_FORK),
{ "directory_guard_fetch_with_no_dirinfo",
test_directory_guard_fetch_with_no_dirinfo, TT_FORK, NULL, NULL },
+ /* These depend on construct_consensus() setting valid_after=now+1000 */
+ TIMELY("timely_consensus1", "1010"),
+ TIMELY("timely_consensus2", "1000"),
+ TIMELY("timely_consensus3", "940"),
+ EARLY("early_consensus1", "939"),
END_OF_TESTCASES
};