aboutsummaryrefslogtreecommitdiff
path: root/src/test/test_hs_cache.c
diff options
context:
space:
mode:
authorGeorge Kadianakis <desnacked@riseup.net>2017-09-12 12:50:36 +0300
committerNick Mathewson <nickm@torproject.org>2017-09-13 14:54:49 -0400
commit6b794c7ed0a86e5c8de03b3a82b2299758f9e017 (patch)
tree08146e82a41eabc02bc23efefc322ca9436689fd /src/test/test_hs_cache.c
parentcf8a2b156769023e583b6c6374ac6de0134712f1 (diff)
downloadtor-6b794c7ed0a86e5c8de03b3a82b2299758f9e017.tar.gz
tor-6b794c7ed0a86e5c8de03b3a82b2299758f9e017.zip
prop224 test: Test client desc expiration in tests.
We enrich the test_client_cache() test in two ways: a) We check that transitioning time periods also cleans up expired descriptors in client memory. b) We test hs_cache_lookup_as_client() instead of lookup_v3_desc_as_client(). The former is a higher level function which calls the latter and allows us to test deeper into the subsystem.
Diffstat (limited to 'src/test/test_hs_cache.c')
-rw-r--r--src/test/test_hs_cache.c57
1 files changed, 52 insertions, 5 deletions
diff --git a/src/test/test_hs_cache.c b/src/test/test_hs_cache.c
index 950c0483d1..91b13be862 100644
--- a/src/test/test_hs_cache.c
+++ b/src/test/test_hs_cache.c
@@ -14,6 +14,7 @@
#include "hs_cache.h"
#include "rendcache.h"
#include "directory.h"
+#include "networkstatus.h"
#include "connection.h"
#include "proto_http.h"
@@ -433,6 +434,15 @@ test_hsdir_revision_counter_check(void *arg)
tor_free(published_desc_str);
}
+static networkstatus_t mock_ns;
+
+static networkstatus_t *
+mock_networkstatus_get_live_consensus(time_t now)
+{
+ (void) now;
+ return &mock_ns;
+}
+
/** Test that we can store HS descriptors in the client HS cache. */
static void
test_client_cache(void *arg)
@@ -441,7 +451,7 @@ test_client_cache(void *arg)
ed25519_keypair_t signing_kp;
hs_descriptor_t *published_desc = NULL;
char *published_desc_str = NULL;
-
+ uint8_t wanted_subcredential[DIGEST256_LEN];
response_handler_args_t *args = NULL;
dir_connection_t *conn = NULL;
@@ -450,6 +460,17 @@ test_client_cache(void *arg)
/* Initialize HSDir cache subsystem */
init_test();
+ MOCK(networkstatus_get_live_consensus,
+ mock_networkstatus_get_live_consensus);
+
+ /* Set consensus time */
+ parse_rfc1123_time("Sat, 26 Oct 1985 13:00:00 UTC",
+ &mock_ns.valid_after);
+ parse_rfc1123_time("Sat, 26 Oct 1985 14:00:00 UTC",
+ &mock_ns.fresh_until);
+ parse_rfc1123_time("Sat, 26 Oct 1985 16:00:00 UTC",
+ &mock_ns.valid_until);
+
/* Generate a valid descriptor with normal values. */
{
retval = ed25519_keypair_generate(&signing_kp, 0);
@@ -459,6 +480,8 @@ test_client_cache(void *arg)
retval = hs_desc_encode_descriptor(published_desc, &signing_kp,
&published_desc_str);
tt_int_op(retval, OP_EQ, 0);
+ memcpy(wanted_subcredential, published_desc->subcredential, DIGEST256_LEN);
+ tt_assert(!tor_mem_is_zero((char*)wanted_subcredential, DIGEST256_LEN));
}
/* Test handle_response_fetch_hsdesc_v3() */
@@ -478,12 +501,36 @@ test_client_cache(void *arg)
retval = handle_response_fetch_hsdesc_v3(conn, args);
tt_int_op(retval, == , 0);
- /* fetch the descriptor and make sure it's there */
+ /* Progress time a bit and attempt to clean cache: our desc should not be
+ * cleaned since we still in the same TP. */
{
- hs_cache_client_descriptor_t *cached_desc = NULL;
- cached_desc = lookup_v3_desc_as_client(signing_kp.pubkey.pubkey);
+ parse_rfc1123_time("Sat, 27 Oct 1985 02:00:00 UTC",
+ &mock_ns.valid_after);
+ parse_rfc1123_time("Sat, 27 Oct 1985 03:00:00 UTC",
+ &mock_ns.fresh_until);
+ parse_rfc1123_time("Sat, 27 Oct 1985 05:00:00 UTC",
+ &mock_ns.valid_until);
+
+ /* fetch the descriptor and make sure it's there */
+ const hs_descriptor_t *cached_desc = NULL;
+ cached_desc = hs_cache_lookup_as_client(&signing_kp.pubkey);
tt_assert(cached_desc);
- tt_str_op(cached_desc->encoded_desc, OP_EQ, published_desc_str);
+ tt_mem_op(cached_desc->subcredential, OP_EQ, wanted_subcredential,
+ DIGEST256_LEN);
+ }
+
+ /* Progress time to next TP and check that desc was cleaned */
+ {
+ parse_rfc1123_time("Sat, 27 Oct 1985 12:00:00 UTC",
+ &mock_ns.valid_after);
+ parse_rfc1123_time("Sat, 27 Oct 1985 13:00:00 UTC",
+ &mock_ns.fresh_until);
+ parse_rfc1123_time("Sat, 27 Oct 1985 15:00:00 UTC",
+ &mock_ns.valid_until);
+
+ const hs_descriptor_t *cached_desc = NULL;
+ cached_desc = hs_cache_lookup_as_client(&signing_kp.pubkey);
+ tt_assert(!cached_desc);
}
done: