diff options
author | George Kadianakis <desnacked@riseup.net> | 2017-08-07 18:58:13 +0300 |
---|---|---|
committer | Nick Mathewson <nickm@torproject.org> | 2017-08-08 20:29:35 -0400 |
commit | 101ce6da01770ba0d05291ccafb98c4274cb616e (patch) | |
tree | 49b58808eb8a59d40ff86c9e984a686255796c14 /src/test/test_hs_common.c | |
parent | 8bac50d7559adba16e282d5c83b891a387a8a3d5 (diff) | |
download | tor-101ce6da01770ba0d05291ccafb98c4274cb616e.tar.gz tor-101ce6da01770ba0d05291ccafb98c4274cb616e.zip |
Fix the build_hs_index() function.
Also add a unittest for hs_get_responsible_hsdirs() which was used to
find and fix the bug.
Diffstat (limited to 'src/test/test_hs_common.c')
-rw-r--r-- | src/test/test_hs_common.c | 82 |
1 files changed, 82 insertions, 0 deletions
diff --git a/src/test/test_hs_common.c b/src/test/test_hs_common.c index cc62870cb6..3041d24a62 100644 --- a/src/test/test_hs_common.c +++ b/src/test/test_hs_common.c @@ -17,6 +17,8 @@ #include "hs_common.h" #include "hs_service.h" #include "config.h" +#include "networkstatus.h" +#include "nodelist.h" /** Test the validation of HS v3 addresses */ static void @@ -355,6 +357,83 @@ test_desc_overlap_period_testnet(void *arg) tor_free(dummy_consensus); } +static networkstatus_t *mock_ns = NULL; + +static networkstatus_t * +mock_networkstatus_get_latest_consensus(void) +{ + time_t now = approx_time(); + + /* If initialized, return it */ + if (mock_ns) { + return mock_ns; + } + + /* Initialize fake consensus */ + mock_ns = tor_malloc_zero(sizeof(networkstatus_t)); + + /* This consensus is live */ + mock_ns->valid_after = now-1; + mock_ns->fresh_until = now+1; + mock_ns->valid_until = now+2; + /* Create routerstatus list */ + mock_ns->routerstatus_list = smartlist_new(); + + return mock_ns; +} + +/** Test the responsible HSDirs calculation function */ +static void +test_responsible_hsdirs(void *arg) +{ + time_t now = approx_time(); + smartlist_t *responsible_dirs = smartlist_new(); + networkstatus_t *ns = NULL; + routerstatus_t *rs = tor_malloc_zero(sizeof(routerstatus_t)); + + (void) arg; + + hs_init(); + + MOCK(networkstatus_get_latest_consensus, + mock_networkstatus_get_latest_consensus); + + ns = networkstatus_get_latest_consensus(); + + { /* First router: HSdir */ + tor_addr_t ipv4_addr; + memset(rs->identity_digest, 'A', DIGEST_LEN); + rs->is_hs_dir = 1; + rs->supports_v3_hsdir = 1; + routerinfo_t ri; + memset(&ri, 0 ,sizeof(routerinfo_t)); + tor_addr_parse(&ipv4_addr, "127.0.0.1"); + ri.addr = tor_addr_to_ipv4h(&ipv4_addr); + ri.nickname = tor_strdup("fatal"); + ri.protocol_list = (char *) "HSDir=1-2 LinkAuth=3"; + memset(ri.cache_info.identity_digest, 'A', DIGEST_LEN); + tt_assert(nodelist_set_routerinfo(&ri, NULL)); + node_t *node = node_get_mutable_by_id(ri.cache_info.identity_digest); + memset(node->hsdir_index->current, 'Z', + sizeof(node->hsdir_index->current)); + smartlist_add(ns->routerstatus_list, rs); + } + + ed25519_public_key_t blinded_pk; + uint64_t time_period_num = hs_get_time_period_num(now); + hs_get_responsible_hsdirs(&blinded_pk, time_period_num, + 0, 0, responsible_dirs); + tt_int_op(smartlist_len(responsible_dirs), OP_EQ, 1); + + /** TODO: Build a bigger network and do more tests here */ + + done: + routerstatus_free(rs); + smartlist_free(responsible_dirs); + smartlist_clear(ns->routerstatus_list); + networkstatus_vote_free(mock_ns); +} + struct testcase_t hs_common_tests[] = { { "build_address", test_build_address, TT_FORK, NULL, NULL }, @@ -368,6 +447,9 @@ struct testcase_t hs_common_tests[] = { NULL, NULL }, { "desc_overlap_period_testnet", test_desc_overlap_period_testnet, TT_FORK, NULL, NULL }, + { "desc_responsible_hsdirs", test_responsible_hsdirs, TT_FORK, + NULL, NULL }, + END_OF_TESTCASES }; |