prop224: Add descriptor overlap mode function

The function has been added but not used except for the unit tests.

Signed-off-by: David Goulet <dgoulet@torproject.org>
This commit is contained in:
George Kadianakis 2017-02-13 15:32:13 +02:00 committed by Nick Mathewson
parent 0f104ddce5
commit f53b72baf7
4 changed files with 81 additions and 0 deletions

View File

@ -663,6 +663,34 @@ hs_build_blinded_keypair(const ed25519_keypair_t *kp,
ed25519_keypair_blind(blinded_kp_out, kp, param);
}
/* Return true if overlap mode is active given the date in consensus. If
* consensus is NULL, then we use the latest live consensus we can find. */
int
hs_overlap_mode_is_active(const networkstatus_t *consensus, time_t now)
{
struct tm valid_after_tm;
if (!consensus) {
consensus = networkstatus_get_live_consensus(now);
if (!consensus) {
return 0;
}
}
/* XXX: Futur commits will change this to a slot system so it can be
* fine tuned better for testing networks in terms of timings. */
/* 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) {
return 1;
}
return 0;
}
/* Initialize the entire HS subsytem. This is called in tor_init() before any
* torrc options are loaded. Only for >= v3. */
void

View File

@ -144,6 +144,8 @@ uint64_t hs_get_next_time_period_num(time_t now);
link_specifier_t *hs_link_specifier_dup(const link_specifier_t *lspec);
int hs_overlap_mode_is_active(const networkstatus_t *consensus, time_t now);
#ifdef HS_COMMON_PRIVATE
#ifdef TOR_UNIT_TESTS

View File

@ -9,8 +9,10 @@
#define HS_SERVICE_PRIVATE
#include "or.h"
#include "circpathbias.h"
#include "circuitlist.h"
#include "config.h"
#include "networkstatus.h"
#include "nodelist.h"
#include "relay.h"
#include "rendservice.h"

View File

@ -553,6 +553,53 @@ test_access_service(void *arg)
hs_free_all();
}
/** Test that our HS overlap period functions work properly. */
static void
test_desc_overlap_period(void *arg)
{
(void) arg;
int retval;
time_t now = time(NULL);
networkstatus_t *dummy_consensus = NULL;
/* First try with a consensus inside the overlap period */
dummy_consensus = tor_malloc_zero(sizeof(networkstatus_t));
retval = parse_rfc1123_time("Wed, 13 Apr 2016 10: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);
/* Now increase the valid_after so that it goes to 11:00:00 UTC. Overlap
period is still active. */
dummy_consensus->valid_after += 3600;
retval = hs_overlap_mode_is_active(dummy_consensus, now);
tt_int_op(retval, ==, 1);
/* Now increase the valid_after so that it goes to 11:59:59 UTC. Overlap
period is still active. */
dummy_consensus->valid_after += 3599;
retval = hs_overlap_mode_is_active(dummy_consensus, now);
tt_int_op(retval, ==, 1);
/* Now increase the valid_after so that it drifts to noon, and check that
overlap mode is not active anymore. */
dummy_consensus->valid_after += 1;
retval = hs_overlap_mode_is_active(dummy_consensus, now);
tt_int_op(retval, ==, 0);
/* Check that overlap mode is also inactive at 23:59:59 UTC */
retval = parse_rfc1123_time("Wed, 13 Apr 2016 23:59:59 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_service_tests[] = {
{ "gen_establish_intro_cell", test_gen_establish_intro_cell, TT_FORK,
NULL, NULL },
@ -572,6 +619,8 @@ struct testcase_t hs_service_tests[] = {
NULL, NULL },
{ "access_service", test_access_service, TT_FORK,
NULL, NULL },
{ "desc_overlap_period", test_desc_overlap_period, TT_FORK,
NULL, NULL },
END_OF_TESTCASES
};