prop224: Refactor the overlap function to not use absolute time.

We consider to be in overlap mode when we are in the period of time between a
fresh SRV and the beginning of the new time period (in the normal network this
is between 00:00 and 12:00 UTC). This commit edits that function to use the
above semantic logic instead of absolute times.

Signed-off-by: David Goulet <dgoulet@torproject.org>
This commit is contained in:
George Kadianakis 2017-07-18 16:06:12 +03:00 committed by Nick Mathewson
parent 6c00bd1f10
commit 2e5a2d64bd
3 changed files with 86 additions and 11 deletions

View File

@ -908,7 +908,8 @@ hs_build_blinded_keypair(const ed25519_keypair_t *kp,
MOCK_IMPL(int,
hs_overlap_mode_is_active, (const networkstatus_t *consensus, time_t now))
{
struct tm valid_after_tm;
time_t valid_after;
time_t srv_start_time, tp_start_time;
if (!consensus) {
consensus = networkstatus_get_live_consensus(now);
@ -917,16 +918,18 @@ hs_overlap_mode_is_active, (const networkstatus_t *consensus, time_t now))
}
}
/* XXX: Futur commits will change this to a slot system so it can be
* fine tuned better for testing networks in terms of timings. */
/* We consider to be in overlap mode when we are in the period of time
* between a fresh SRV and the beginning of the new time period (in the
* normal network this is between 00:00 (inclusive) and 12:00 UTC
* (exclusive)) */
valid_after = consensus->valid_after;
srv_start_time =sr_state_get_start_time_of_current_protocol_run(valid_after);
tp_start_time = hs_get_start_time_of_next_time_period(srv_start_time);
/* From the spec: "Specifically, when a hidden service fetches a consensus
* with "valid-after" between 00:00UTC and 12:00UTC, it goes into
* "descriptor overlap" mode." */
tor_gmtime_r(&consensus->valid_after, &valid_after_tm);
if (valid_after_tm.tm_hour > 0 && valid_after_tm.tm_hour < 12) {
if (valid_after >= srv_start_time && valid_after < tp_start_time) {
return 1;
}
return 0;
}

View File

@ -200,9 +200,9 @@ test_desc_overlap_period(void *arg)
time_t now = time(NULL);
networkstatus_t *dummy_consensus = NULL;
/* First try with a consensus inside the overlap period */
/* First try with a consensus just inside the overlap period */
dummy_consensus = tor_malloc_zero(sizeof(networkstatus_t));
retval = parse_rfc1123_time("Wed, 13 Apr 2016 10:00:00 UTC",
retval = parse_rfc1123_time("Wed, 13 Apr 2016 00:00:00 UTC",
&dummy_consensus->valid_after);
tt_int_op(retval, ==, 0);
@ -211,7 +211,7 @@ test_desc_overlap_period(void *arg)
/* Now increase the valid_after so that it goes to 11:00:00 UTC. Overlap
period is still active. */
dummy_consensus->valid_after += 3600;
dummy_consensus->valid_after += 3600*11;
retval = hs_overlap_mode_is_active(dummy_consensus, now);
tt_int_op(retval, ==, 1);
@ -238,6 +238,70 @@ test_desc_overlap_period(void *arg)
tor_free(dummy_consensus);
}
/* Test the overlap period functions on a testnet with altered voting
* schedule */
static void
test_desc_overlap_period_testnet(void *arg)
{
int retval;
time_t now = approx_time();
networkstatus_t *dummy_consensus = NULL;
or_options_t *options = get_options_mutable();
(void) arg;
/* Set the testnet option and a 10-second voting interval */
options->TestingTorNetwork = 1;
options->V3AuthVotingInterval = 10;
options->TestingV3AuthInitialVotingInterval = 10;
dummy_consensus = tor_malloc_zero(sizeof(networkstatus_t));
/* A 10-second voting interval means that the lengths of an SRV run and of a
* time period are both 10*24 seconds (4 minutes). The SRV gets published at
* 00:00:00 and the TP starts at 00:02:00 (rotation offset: 2 mins). Those
* two minutes between SRV publish and TP start is the overlap period
* window. Let's test it: */
retval = parse_rfc1123_time("Wed, 13 Apr 2016 00:00:00 UTC",
&dummy_consensus->valid_after);
tt_int_op(retval, ==, 0);
retval = hs_overlap_mode_is_active(dummy_consensus, now);
tt_int_op(retval, ==, 1);
retval = parse_rfc1123_time("Wed, 13 Apr 2016 00:01:59 UTC",
&dummy_consensus->valid_after);
tt_int_op(retval, ==, 0);
retval = hs_overlap_mode_is_active(dummy_consensus, now);
tt_int_op(retval, ==, 1);
retval = parse_rfc1123_time("Wed, 13 Apr 2016 00:02:00 UTC",
&dummy_consensus->valid_after);
tt_int_op(retval, ==, 0);
retval = hs_overlap_mode_is_active(dummy_consensus, now);
tt_int_op(retval, ==, 0);
retval = parse_rfc1123_time("Wed, 13 Apr 2016 00:04:00 UTC",
&dummy_consensus->valid_after);
tt_int_op(retval, ==, 0);
retval = hs_overlap_mode_is_active(dummy_consensus, now);
tt_int_op(retval, ==, 1);
retval = parse_rfc1123_time("Wed, 13 Apr 2016 00:05:59 UTC",
&dummy_consensus->valid_after);
tt_int_op(retval, ==, 0);
retval = hs_overlap_mode_is_active(dummy_consensus, now);
tt_int_op(retval, ==, 1);
retval = parse_rfc1123_time("Wed, 13 Apr 2016 00:06:00 UTC",
&dummy_consensus->valid_after);
tt_int_op(retval, ==, 0);
retval = hs_overlap_mode_is_active(dummy_consensus, now);
tt_int_op(retval, ==, 0);
done:
tor_free(dummy_consensus);
}
struct testcase_t hs_common_tests[] = {
{ "build_address", test_build_address, TT_FORK,
NULL, NULL },
@ -249,6 +313,8 @@ struct testcase_t hs_common_tests[] = {
TT_FORK, NULL, NULL },
{ "desc_overlap_period", test_desc_overlap_period, TT_FORK,
NULL, NULL },
{ "desc_overlap_period_testnet", test_desc_overlap_period_testnet, TT_FORK,
NULL, NULL },
END_OF_TESTCASES
};

View File

@ -936,6 +936,8 @@ test_rotate_descriptors(void *arg)
* overlap check doesn't care about the year. */
ret = parse_rfc1123_time("Sat, 26 Oct 1985 13:00:00 UTC",
&mock_ns.valid_after);
ret = parse_rfc1123_time("Sat, 26 Oct 1985 14:00:00 UTC",
&mock_ns.fresh_until);
tt_int_op(ret, OP_EQ, 0);
/* Create a service with a default descriptor and state. It's added to the
@ -954,6 +956,8 @@ test_rotate_descriptors(void *arg)
/* Entering an overlap period. */
ret = parse_rfc1123_time("Sat, 26 Oct 1985 01:00:00 UTC",
&mock_ns.valid_after);
ret = parse_rfc1123_time("Sat, 26 Oct 1985 02:00:00 UTC",
&mock_ns.fresh_until);
tt_int_op(ret, OP_EQ, 0);
desc_next = service_descriptor_new();
desc_next->next_upload_time = 42; /* Our marker to recognize it. */
@ -977,6 +981,8 @@ test_rotate_descriptors(void *arg)
/* Going out of the overlap period. */
ret = parse_rfc1123_time("Sat, 26 Oct 1985 12:00:00 UTC",
&mock_ns.valid_after);
ret = parse_rfc1123_time("Sat, 26 Oct 1985 13:00:00 UTC",
&mock_ns.fresh_until);
/* This should reset the state and not touch the current descriptor. */
tt_int_op(ret, OP_EQ, 0);
rotate_all_descriptors(now);