summaryrefslogtreecommitdiff
path: root/src/test/test_shared_random.c
diff options
context:
space:
mode:
authorNick Mathewson <nickm@torproject.org>2017-11-07 09:58:53 -0500
committerNick Mathewson <nickm@torproject.org>2017-11-07 09:58:53 -0500
commitc6c4a421fd7d8a0d4baba8f34276acaa245548f1 (patch)
tree261e1193e7a18a8dc3c02950c35fa164b108ce27 /src/test/test_shared_random.c
parent0227aab1b7084de271cfb7702cb3386bfb05a5bb (diff)
parent9d848ca58a4624df7abae298d8977ae82bcfb543 (diff)
downloadtor-c6c4a421fd7d8a0d4baba8f34276acaa245548f1.tar.gz
tor-c6c4a421fd7d8a0d4baba8f34276acaa245548f1.zip
Merge branch 'maint-0.3.2'
Diffstat (limited to 'src/test/test_shared_random.c')
-rw-r--r--src/test/test_shared_random.c149
1 files changed, 61 insertions, 88 deletions
diff --git a/src/test/test_shared_random.c b/src/test/test_shared_random.c
index 80494f64c0..b0278e8a1d 100644
--- a/src/test/test_shared_random.c
+++ b/src/test/test_shared_random.c
@@ -134,7 +134,15 @@ test_get_sr_protocol_phase(void *arg)
;
}
-static networkstatus_t *mock_consensus = NULL;
+static networkstatus_t mock_consensus;
+
+/* Mock function to immediately return our local 'mock_consensus'. */
+static networkstatus_t *
+mock_networkstatus_get_live_consensus(time_t now)
+{
+ (void) now;
+ return &mock_consensus;
+}
static void
test_get_state_valid_until_time(void *arg)
@@ -146,11 +154,23 @@ test_get_state_valid_until_time(void *arg)
(void) arg;
+ MOCK(networkstatus_get_live_consensus,
+ mock_networkstatus_get_live_consensus);
+
+ retval = parse_rfc1123_time("Mon, 20 Apr 2015 01:00:00 UTC",
+ &mock_consensus.fresh_until);
+ tt_int_op(retval, OP_EQ, 0);
+
+ retval = parse_rfc1123_time("Mon, 20 Apr 2015 00:00:00 UTC",
+ &mock_consensus.valid_after);
+ tt_int_op(retval, OP_EQ, 0);
+
{
/* Get the valid until time if called at 00:00:01 */
retval = parse_rfc1123_time("Mon, 20 Apr 2015 00:00:01 UTC",
&current_time);
tt_int_op(retval, OP_EQ, 0);
+ dirvote_recalculate_timing(get_options(), current_time);
valid_until_time = get_state_valid_until_time(current_time);
/* Compare it with the correct result */
@@ -162,6 +182,7 @@ test_get_state_valid_until_time(void *arg)
retval = parse_rfc1123_time("Mon, 20 Apr 2015 19:22:00 UTC",
&current_time);
tt_int_op(retval, OP_EQ, 0);
+ dirvote_recalculate_timing(get_options(), current_time);
valid_until_time = get_state_valid_until_time(current_time);
format_iso_time(tbuf, valid_until_time);
@@ -172,6 +193,7 @@ test_get_state_valid_until_time(void *arg)
retval = parse_rfc1123_time("Mon, 20 Apr 2015 23:59:00 UTC",
&current_time);
tt_int_op(retval, OP_EQ, 0);
+ dirvote_recalculate_timing(get_options(), current_time);
valid_until_time = get_state_valid_until_time(current_time);
format_iso_time(tbuf, valid_until_time);
@@ -182,6 +204,7 @@ test_get_state_valid_until_time(void *arg)
retval = parse_rfc1123_time("Mon, 20 Apr 2015 00:00:00 UTC",
&current_time);
tt_int_op(retval, OP_EQ, 0);
+ dirvote_recalculate_timing(get_options(), current_time);
valid_until_time = get_state_valid_until_time(current_time);
format_iso_time(tbuf, valid_until_time);
@@ -189,7 +212,7 @@ test_get_state_valid_until_time(void *arg)
}
done:
- ;
+ UNMOCK(networkstatus_get_live_consensus);
}
/** Test the function that calculates the start time of the current SRV
@@ -203,11 +226,23 @@ test_get_start_time_of_current_run(void *arg)
(void) arg;
+ MOCK(networkstatus_get_live_consensus,
+ mock_networkstatus_get_live_consensus);
+
+ retval = parse_rfc1123_time("Mon, 20 Apr 2015 01:00:00 UTC",
+ &mock_consensus.fresh_until);
+ tt_int_op(retval, OP_EQ, 0);
+
+ retval = parse_rfc1123_time("Mon, 20 Apr 2015 00:00:00 UTC",
+ &mock_consensus.valid_after);
+ tt_int_op(retval, OP_EQ, 0);
+
{
/* Get start time if called at 00:00:01 */
retval = parse_rfc1123_time("Mon, 20 Apr 2015 00:00:01 UTC",
&current_time);
tt_int_op(retval, OP_EQ, 0);
+ dirvote_recalculate_timing(get_options(), current_time);
run_start_time =
sr_state_get_start_time_of_current_protocol_run(current_time);
@@ -220,6 +255,7 @@ test_get_start_time_of_current_run(void *arg)
retval = parse_rfc1123_time("Mon, 20 Apr 2015 23:59:59 UTC",
&current_time);
tt_int_op(retval, OP_EQ, 0);
+ dirvote_recalculate_timing(get_options(), current_time);
run_start_time =
sr_state_get_start_time_of_current_protocol_run(current_time);
@@ -232,6 +268,7 @@ test_get_start_time_of_current_run(void *arg)
retval = parse_rfc1123_time("Mon, 20 Apr 2015 00:00:00 UTC",
&current_time);
tt_int_op(retval, OP_EQ, 0);
+ dirvote_recalculate_timing(get_options(), current_time);
run_start_time =
sr_state_get_start_time_of_current_protocol_run(current_time);
@@ -240,6 +277,10 @@ test_get_start_time_of_current_run(void *arg)
tt_str_op("2015-04-20 00:00:00", OP_EQ, tbuf);
}
+ /* Next test is testing it without a consensus to use the testing voting
+ * interval . */
+ UNMOCK(networkstatus_get_live_consensus);
+
/* Now let's alter the voting schedule and check the correctness of the
* function. Voting interval of 10 seconds, means that an SRV protocol run
* takes 10 seconds * 24 rounds = 4 mins */
@@ -249,8 +290,8 @@ test_get_start_time_of_current_run(void *arg)
options->TestingV3AuthInitialVotingInterval = 10;
retval = parse_rfc1123_time("Mon, 20 Apr 2015 00:15:32 UTC",
&current_time);
-
tt_int_op(retval, OP_EQ, 0);
+ dirvote_recalculate_timing(get_options(), current_time);
run_start_time =
sr_state_get_start_time_of_current_protocol_run(current_time);
@@ -269,22 +310,31 @@ static void
test_get_start_time_functions(void *arg)
{
(void) arg;
- time_t now = approx_time();
+ int retval;
+
+ MOCK(networkstatus_get_live_consensus,
+ mock_networkstatus_get_live_consensus);
+
+ retval = parse_rfc1123_time("Mon, 20 Apr 2015 01:00:00 UTC",
+ &mock_consensus.fresh_until);
+ tt_int_op(retval, OP_EQ, 0);
+ retval = parse_rfc1123_time("Mon, 20 Apr 2015 00:00:00 UTC",
+ &mock_consensus.valid_after);
+ tt_int_op(retval, OP_EQ, 0);
+ time_t now = mock_consensus.valid_after;
+
+ dirvote_recalculate_timing(get_options(), now);
time_t start_time_of_protocol_run =
sr_state_get_start_time_of_current_protocol_run(now);
tt_assert(start_time_of_protocol_run);
/* Check that the round start time of the beginning of the run, is itself */
- tt_int_op(get_start_time_of_current_round(start_time_of_protocol_run), OP_EQ,
+ tt_int_op(get_start_time_of_current_round(), OP_EQ,
start_time_of_protocol_run);
- /* Check that even if we increment the start time, we still get the start
- time of the run as the beginning of the round. */
- tt_int_op(get_start_time_of_current_round(start_time_of_protocol_run+1),
- OP_EQ, start_time_of_protocol_run);
-
- done: ;
+ done:
+ UNMOCK(networkstatus_get_live_consensus);
}
static void
@@ -306,81 +356,6 @@ test_get_sr_protocol_duration(void *arg)
done: ;
}
-/* Mock function to immediately return our local 'mock_consensus'. */
-static networkstatus_t *
-mock_networkstatus_get_live_consensus(time_t now)
-{
- (void) now;
- return mock_consensus;
-}
-
-/** Test the get_next_valid_after_time() function. */
-static void
-test_get_next_valid_after_time(void *arg)
-{
- time_t current_time;
- time_t valid_after_time;
- char tbuf[ISO_TIME_LEN + 1];
- int retval;
-
- (void) arg;
-
- {
- /* Setup a fake consensus just to get the times out of it, since
- get_next_valid_after_time() needs them. */
- mock_consensus = tor_malloc_zero(sizeof(networkstatus_t));
-
- retval = parse_rfc1123_time("Mon, 13 Jan 2016 16:00:00 UTC",
- &mock_consensus->fresh_until);
- tt_int_op(retval, OP_EQ, 0);
-
- retval = parse_rfc1123_time("Mon, 13 Jan 2016 15:00:00 UTC",
- &mock_consensus->valid_after);
- tt_int_op(retval, OP_EQ, 0);
-
- MOCK(networkstatus_get_live_consensus,
- mock_networkstatus_get_live_consensus);
- }
-
- {
- /* Get the valid after time if called at 00:00:00 */
- retval = parse_rfc1123_time("Mon, 20 Apr 2015 00:00:00 UTC",
- &current_time);
- tt_int_op(retval, OP_EQ, 0);
- valid_after_time = get_next_valid_after_time(current_time);
-
- /* Compare it with the correct result */
- format_iso_time(tbuf, valid_after_time);
- tt_str_op("2015-04-20 01:00:00", OP_EQ, tbuf);
- }
-
- {
- /* Get the valid until time if called at 00:00:01 */
- retval = parse_rfc1123_time("Mon, 20 Apr 2015 00:00:01 UTC",
- &current_time);
- tt_int_op(retval, OP_EQ, 0);
- valid_after_time = get_next_valid_after_time(current_time);
-
- /* Compare it with the correct result */
- format_iso_time(tbuf, valid_after_time);
- tt_str_op("2015-04-20 01:00:00", OP_EQ, tbuf);
- }
-
- {
- retval = parse_rfc1123_time("Mon, 20 Apr 2015 23:30:01 UTC",
- &current_time);
- tt_int_op(retval, OP_EQ, 0);
- valid_after_time = get_next_valid_after_time(current_time);
-
- /* Compare it with the correct result */
- format_iso_time(tbuf, valid_after_time);
- tt_str_op("2015-04-21 00:00:00", OP_EQ, tbuf);
- }
-
- done:
- networkstatus_vote_free(mock_consensus);
-}
-
/* In this test we are going to generate a sr_commit_t object and validate
* it. We first generate our values, and then we parse them as if they were
* received from the network. After we parse both the commit and the reveal,
@@ -1389,8 +1364,6 @@ struct testcase_t sr_tests[] = {
NULL, NULL },
{ "encoding", test_encoding, TT_FORK,
NULL, NULL },
- { "get_next_valid_after_time", test_get_next_valid_after_time, TT_FORK,
- NULL, NULL },
{ "get_start_time_of_current_run", test_get_start_time_of_current_run,
TT_FORK, NULL, NULL },
{ "get_start_time_functions", test_get_start_time_functions,