diff options
author | Nick Mathewson <nickm@torproject.org> | 2014-10-07 09:20:44 -0400 |
---|---|---|
committer | Nick Mathewson <nickm@torproject.org> | 2014-10-13 14:30:02 -0400 |
commit | 3efeb711f104b58b1f8a46c56c56246f0049e3e1 (patch) | |
tree | ba013ba65ad56ab8b19f1d1e3755703a9ec064f4 /src/test/test_microdesc.c | |
parent | 24dfbfda1db41d0e9feb5def09e1b7ede350de40 (diff) | |
download | tor-3efeb711f104b58b1f8a46c56c56246f0049e3e1.tar.gz tor-3efeb711f104b58b1f8a46c56c56246f0049e3e1.zip |
Unit tests for 11243: loading ri, ei, mds from lists
These tests make sure that entries are actually marked
undownloadable as appropriate.
Diffstat (limited to 'src/test/test_microdesc.c')
-rw-r--r-- | src/test/test_microdesc.c | 97 |
1 files changed, 97 insertions, 0 deletions
diff --git a/src/test/test_microdesc.c b/src/test/test_microdesc.c index 907303418b..bc6755b93e 100644 --- a/src/test/test_microdesc.c +++ b/src/test/test_microdesc.c @@ -7,6 +7,7 @@ #include "config.h" #include "dirvote.h" #include "microdesc.h" +#include "networkstatus.h" #include "routerlist.h" #include "routerparse.h" @@ -618,11 +619,107 @@ test_md_parse(void *arg) tor_free(mem_op_hex_tmp); } +static int mock_rgsbd_called = 0; +static routerstatus_t *mock_rgsbd_val_a = NULL; +static routerstatus_t *mock_rgsbd_val_b = NULL; +static routerstatus_t * +mock_router_get_status_by_digest(networkstatus_t *c, const char *d) +{ + (void) c; + ++mock_rgsbd_called; + + if (fast_memeq(d, "\x5d\x76", 2)) { + memcpy(mock_rgsbd_val_a->descriptor_digest, d, 32); + return mock_rgsbd_val_a; + } else if (fast_memeq(d, "\x20\xd1", 2)) { + memcpy(mock_rgsbd_val_b->descriptor_digest, d, 32); + return mock_rgsbd_val_b; + } else { + return NULL; + } +} + +static networkstatus_t *mock_ns_val = NULL; +static networkstatus_t * +mock_ns_get_by_flavor(consensus_flavor_t f) +{ + (void)f; + return mock_ns_val; +} + +static void +test_md_reject_cache(void *arg) +{ + (void) arg; + microdesc_cache_t *mc = NULL ; + smartlist_t *added = NULL, *wanted = smartlist_new(); + or_options_t *options = get_options_mutable(); + char buf[DIGEST256_LEN]; + + tor_free(options->DataDirectory); + options->DataDirectory = tor_strdup(get_fname("md_datadir_test_rej")); + mock_rgsbd_val_a = tor_malloc_zero(sizeof(routerstatus_t)); + mock_rgsbd_val_b = tor_malloc_zero(sizeof(routerstatus_t)); + mock_ns_val = tor_malloc_zero(sizeof(networkstatus_t)); + + mock_ns_val->valid_after = time(NULL) - 86400; + mock_ns_val->valid_until = time(NULL) + 86400; + mock_ns_val->flavor = FLAV_MICRODESC; + +#ifdef _WIN32 + tt_int_op(0, ==, mkdir(options->DataDirectory)); +#else + tt_int_op(0, ==, mkdir(options->DataDirectory, 0700)); +#endif + + MOCK(router_get_mutable_consensus_status_by_descriptor_digest, + mock_router_get_status_by_digest); + MOCK(networkstatus_get_latest_consensus_by_flavor, mock_ns_get_by_flavor); + + mc = get_microdesc_cache(); +#define ADD(hex) \ + do { \ + tt_int_op(0,==,base16_decode(buf,sizeof(buf),hex,strlen(hex))); \ + smartlist_add(wanted, tor_memdup(buf, DIGEST256_LEN)); \ + } while (0) + + /* invalid,0 */ + ADD("5d76bf1c6614e885614a1e0ad074e1ab4ea14655ebeefb1736a71b5ed8a15a51"); + /* invalid,2 */ + ADD("20d1576c5ab11bbcff0dedb1db4a3cfcc8bc8dd839d8cbfef92d00a1a7d7b294"); + /* valid, 6 */ + ADD("53f740bd222ab37f19f604b1d3759aa65eff1fbce9ac254bd0fa50d4af9b1bae"); + /* valid, 8 */ + ADD("a0a155562d8093d8fd0feb7b93b7226e17f056c2142aab7a4ea8c5867a0376d5"); + + added = microdescs_add_to_cache(mc, MD_PARSE_TEST_DATA, NULL, + SAVED_NOWHERE, 0, time(NULL), wanted); + + tt_int_op(smartlist_len(added), ==, 2); + tt_int_op(mock_rgsbd_called, ==, 2); + tt_int_op(mock_rgsbd_val_a->dl_status.n_download_failures, ==, 255); + tt_int_op(mock_rgsbd_val_b->dl_status.n_download_failures, ==, 255); + + done: + UNMOCK(networkstatus_get_latest_consensus_by_flavor); + UNMOCK(router_get_mutable_consensus_status_by_descriptor_digest); + if (options) + tor_free(options->DataDirectory); + microdesc_free_all(); + smartlist_free(added); + SMARTLIST_FOREACH(wanted, char *, cp, tor_free(cp)); + smartlist_free(wanted); + tor_free(mock_rgsbd_val_a); + tor_free(mock_rgsbd_val_b); + tor_free(mock_ns_val); +} + struct testcase_t microdesc_tests[] = { { "cache", test_md_cache, TT_FORK, NULL, NULL }, { "broken_cache", test_md_cache_broken, TT_FORK, NULL, NULL }, { "generate", test_md_generate, 0, NULL, NULL }, { "parse", test_md_parse, 0, NULL, NULL }, + { "reject_cache", test_md_reject_cache, TT_FORK, NULL, NULL }, END_OF_TESTCASES }; |