mirror of
https://gitlab.torproject.org/tpo/core/tor.git
synced 2024-11-10 21:23:58 +01:00
prop224: Compute start time of next time period.
This commit is contained in:
parent
0b22b7fce3
commit
2cd5f9a2fc
@ -23,6 +23,7 @@
|
||||
#include "rendservice.h"
|
||||
#include "router.h"
|
||||
#include "shared_random.h"
|
||||
#include "shared_random_state.h"
|
||||
|
||||
/* Ed25519 Basepoint value. Taken from section 5 of
|
||||
* https://tools.ietf.org/html/draft-josefsson-eddsa-ed25519-03 */
|
||||
@ -216,6 +217,21 @@ hs_get_next_time_period_num(time_t now)
|
||||
return hs_get_time_period_num(now) + 1;
|
||||
}
|
||||
|
||||
/* Return the start time of the upcoming time period based on <b>now</b>. */
|
||||
time_t
|
||||
hs_get_start_time_of_next_time_period(time_t now)
|
||||
{
|
||||
uint64_t time_period_length = get_time_period_length();
|
||||
|
||||
/* Get start time of next time period */
|
||||
uint64_t next_time_period_num = hs_get_next_time_period_num(now);
|
||||
uint64_t start_of_next_tp_in_mins = next_time_period_num *time_period_length;
|
||||
|
||||
/* Apply rotation offset as specified by prop224 section [TIME-PERIODS] */
|
||||
unsigned int time_period_rotation_offset = sr_state_get_phase_duration();
|
||||
return start_of_next_tp_in_mins * 60 + time_period_rotation_offset;
|
||||
}
|
||||
|
||||
/* Create a new rend_data_t for a specific given <b>version</b>.
|
||||
* Return a pointer to the newly allocated data structure. */
|
||||
static rend_data_t *
|
||||
|
@ -195,6 +195,7 @@ void hs_get_subcredential(const ed25519_public_key_t *identity_pk,
|
||||
|
||||
uint64_t hs_get_time_period_num(time_t now);
|
||||
uint64_t hs_get_next_time_period_num(time_t now);
|
||||
time_t hs_get_start_time_of_next_time_period(time_t now);
|
||||
|
||||
link_specifier_t *hs_link_specifier_dup(const link_specifier_t *lspec);
|
||||
|
||||
|
@ -14,6 +14,7 @@
|
||||
#include "hs_test_helpers.h"
|
||||
|
||||
#include "hs_common.h"
|
||||
#include "config.h"
|
||||
|
||||
static void
|
||||
test_validate_address(void *arg)
|
||||
@ -132,6 +133,64 @@ test_time_period(void *arg)
|
||||
;
|
||||
}
|
||||
|
||||
static void
|
||||
test_start_time_of_next_time_period(void *arg)
|
||||
{
|
||||
(void) arg;
|
||||
int retval;
|
||||
time_t fake_time;
|
||||
char tbuf[ISO_TIME_LEN + 1];
|
||||
time_t next_tp_start_time;
|
||||
|
||||
/* Do some basic tests */
|
||||
retval = parse_rfc1123_time("Wed, 13 Apr 2016 11:00:00 UTC",
|
||||
&fake_time);
|
||||
tt_int_op(retval, ==, 0);
|
||||
next_tp_start_time = hs_get_start_time_of_next_time_period(fake_time);
|
||||
/* Compare it with the correct result */
|
||||
format_iso_time(tbuf, next_tp_start_time);
|
||||
tt_str_op("2016-04-13 12:00:00", OP_EQ, tbuf);
|
||||
|
||||
/* Another test with an edge-case time (start of TP) */
|
||||
retval = parse_rfc1123_time("Wed, 13 Apr 2016 12:00:00 UTC",
|
||||
&fake_time);
|
||||
tt_int_op(retval, ==, 0);
|
||||
next_tp_start_time = hs_get_start_time_of_next_time_period(fake_time);
|
||||
format_iso_time(tbuf, next_tp_start_time);
|
||||
tt_str_op("2016-04-14 12:00:00", OP_EQ, tbuf);
|
||||
|
||||
{
|
||||
/* Now pretend we are on a testing network and alter the voting schedule to
|
||||
be every 10 seconds. This means that a time period has length 10*24
|
||||
seconds (4 minutes). It also means that we apply a rotational offset of
|
||||
120 seconds to the time period, so that it starts at 00:02:00 instead of
|
||||
00:00:00. */
|
||||
or_options_t *options = get_options_mutable();
|
||||
options->TestingTorNetwork = 1;
|
||||
options->V3AuthVotingInterval = 10;
|
||||
options->TestingV3AuthInitialVotingInterval = 10;
|
||||
|
||||
retval = parse_rfc1123_time("Wed, 13 Apr 2016 00:00:00 UTC",
|
||||
&fake_time);
|
||||
tt_int_op(retval, ==, 0);
|
||||
next_tp_start_time = hs_get_start_time_of_next_time_period(fake_time);
|
||||
/* Compare it with the correct result */
|
||||
format_iso_time(tbuf, next_tp_start_time);
|
||||
tt_str_op("2016-04-13 00:02:00", OP_EQ, tbuf);
|
||||
|
||||
retval = parse_rfc1123_time("Wed, 13 Apr 2016 00:02:00 UTC",
|
||||
&fake_time);
|
||||
tt_int_op(retval, ==, 0);
|
||||
next_tp_start_time = hs_get_start_time_of_next_time_period(fake_time);
|
||||
/* Compare it with the correct result */
|
||||
format_iso_time(tbuf, next_tp_start_time);
|
||||
tt_str_op("2016-04-13 00:06:00", OP_EQ, tbuf);
|
||||
}
|
||||
|
||||
done:
|
||||
;
|
||||
}
|
||||
|
||||
/** Test that our HS overlap period functions work properly. */
|
||||
static void
|
||||
test_desc_overlap_period(void *arg)
|
||||
@ -186,6 +245,8 @@ struct testcase_t hs_common_tests[] = {
|
||||
NULL, NULL },
|
||||
{ "time_period", test_time_period, TT_FORK,
|
||||
NULL, NULL },
|
||||
{ "start_time_of_next_time_period", test_start_time_of_next_time_period,
|
||||
TT_FORK, NULL, NULL },
|
||||
{ "desc_overlap_period", test_desc_overlap_period, TT_FORK,
|
||||
NULL, NULL },
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user