diff options
Diffstat (limited to 'src/test/test_entrynodes.c')
-rw-r--r-- | src/test/test_entrynodes.c | 109 |
1 files changed, 100 insertions, 9 deletions
diff --git a/src/test/test_entrynodes.c b/src/test/test_entrynodes.c index ede93fb43a..bddc0f11e0 100644 --- a/src/test/test_entrynodes.c +++ b/src/test/test_entrynodes.c @@ -139,7 +139,7 @@ test_choose_random_entry_no_guards(void *arg) /* Unintuitively, we actually pick a random node as our entry, because router_choose_random_node() relaxes its constraints if it can't find a proper entry guard. */ - test_assert(chosen_entry); + tt_assert(chosen_entry); done: ; @@ -201,7 +201,7 @@ populate_live_entry_guards_test_helper(int num_needed) SMARTLIST_FOREACH_BEGIN(our_nodelist, const node_t *, node) { const node_t *node_tmp; node_tmp = add_an_entry_guard(node, 0, 1, 0, 0); - test_assert(node_tmp); + tt_assert(node_tmp); } SMARTLIST_FOREACH_END(node); /* Make sure the nodes were added as entry guards. */ @@ -325,6 +325,19 @@ state_lines_free(smartlist_t *entry_guard_lines) smartlist_free(entry_guard_lines); } +/* Return a statically allocated string representing yesterday's date + * in ISO format. We use it so that state file items are not found to + * be outdated. */ +static const char * +get_yesterday_date_str(void) +{ + static char buf[ISO_TIME_LEN+1]; + + time_t yesterday = time(NULL) - 24*60*60; + format_iso_time(buf, yesterday); + return buf; +} + /* Tests entry_guards_parse_state(). It creates a fake Tor state with a saved entry guard and makes sure that Tor can parse it and creates the right entry node out of it. @@ -342,7 +355,7 @@ test_entry_guards_parse_state_simple(void *arg) const char *nickname = "hagbard"; const char *fpr = "B29D536DD1752D542E1FBB3C9CE4449D51298212"; const char *tor_version = "0.2.5.3-alpha-dev"; - const char *added_at = "2014-05-22 02:40:47"; + const char *added_at = get_yesterday_date_str(); const char *unlisted_since = "2014-06-08 16:16:50"; (void) arg; @@ -418,6 +431,7 @@ test_entry_guards_parse_state_simple(void *arg) done: state_lines_free(entry_state_lines); or_state_free(state); + tor_free(msg); } /** Similar to test_entry_guards_parse_state_simple() but aims to test @@ -457,7 +471,7 @@ test_entry_guards_parse_state_pathbias(void *arg) smartlist_add_asprintf(state_line, "EntryGuardAddedBy"); smartlist_add_asprintf(state_line, "B29D536DD1752D542E1FBB3C9CE4449D51298212 0.2.5.3-alpha-dev " - "2014-05-22 02:40:47"); + "%s", get_yesterday_date_str()); smartlist_add(entry_state_lines, state_line); state_line = smartlist_new(); @@ -502,6 +516,7 @@ test_entry_guards_parse_state_pathbias(void *arg) done: or_state_free(state); state_lines_free(entry_state_lines); + tor_free(msg); } /* Simple test of entry_guards_set_from_config() by specifying a @@ -538,6 +553,80 @@ test_entry_guards_set_from_config(void *arg) routerset_free(options->EntryNodes); } +static void +test_entry_is_time_to_retry(void *arg) +{ + entry_guard_t *test_guard; + time_t now; + int retval; + (void)arg; + + now = time(NULL); + + test_guard = tor_malloc_zero(sizeof(entry_guard_t)); + + test_guard->last_attempted = now - 10; + test_guard->unreachable_since = now - 1; + + retval = entry_is_time_to_retry(test_guard,now); + tt_int_op(retval,==,1); + + test_guard->unreachable_since = now - (6*60*60 - 1); + test_guard->last_attempted = now - (60*60 + 1); + + retval = entry_is_time_to_retry(test_guard,now); + tt_int_op(retval,==,1); + + test_guard->last_attempted = now - (60*60 - 1); + + retval = entry_is_time_to_retry(test_guard,now); + tt_int_op(retval,==,0); + + test_guard->unreachable_since = now - (6*60*60 + 1); + test_guard->last_attempted = now - (4*60*60 + 1); + + retval = entry_is_time_to_retry(test_guard,now); + tt_int_op(retval,==,1); + + test_guard->unreachable_since = now - (3*24*60*60 - 1); + test_guard->last_attempted = now - (4*60*60 + 1); + + retval = entry_is_time_to_retry(test_guard,now); + tt_int_op(retval,==,1); + + test_guard->unreachable_since = now - (3*24*60*60 + 1); + test_guard->last_attempted = now - (18*60*60 + 1); + + retval = entry_is_time_to_retry(test_guard,now); + tt_int_op(retval,==,1); + + test_guard->unreachable_since = now - (7*24*60*60 - 1); + test_guard->last_attempted = now - (18*60*60 + 1); + + retval = entry_is_time_to_retry(test_guard,now); + tt_int_op(retval,==,1); + + test_guard->last_attempted = now - (18*60*60 - 1); + + retval = entry_is_time_to_retry(test_guard,now); + tt_int_op(retval,==,0); + + test_guard->unreachable_since = now - (7*24*60*60 + 1); + test_guard->last_attempted = now - (36*60*60 + 1); + + retval = entry_is_time_to_retry(test_guard,now); + tt_int_op(retval,==,1); + + test_guard->unreachable_since = now - (7*24*60*60 + 1); + test_guard->last_attempted = now - (36*60*60 + 1); + + retval = entry_is_time_to_retry(test_guard,now); + tt_int_op(retval,==,1); + + done: + tor_free(test_guard); +} + /** XXX Do some tests that entry_is_live() */ static void test_entry_is_live(void *arg) @@ -561,7 +650,7 @@ test_entry_is_live(void *arg) SMARTLIST_FOREACH_BEGIN(our_nodelist, const node_t *, node) { const node_t *node_tmp; node_tmp = add_an_entry_guard(node, 0, 1, 0, 0); - test_assert(node_tmp); + tt_assert(node_tmp); tt_int_op(node->is_stable, ==, 0); tt_int_op(node->is_fast, ==, 0); @@ -581,22 +670,22 @@ test_entry_is_live(void *arg) test_node = entry_is_live(test_entry, ENTRY_NEED_UPTIME | ENTRY_ASSUME_REACHABLE, &msg); - test_assert(!test_node); + tt_assert(!test_node); /* Require the node to be fast, but it's not. Should fail. */ test_node = entry_is_live(test_entry, ENTRY_NEED_CAPACITY | ENTRY_ASSUME_REACHABLE, &msg); - test_assert(!test_node); + tt_assert(!test_node); /* Don't impose any restrictions on the node. Should succeed. */ test_node = entry_is_live(test_entry, 0, &msg); - test_assert(test_node); + tt_assert(test_node); tt_ptr_op(test_node, ==, node_get_by_id(test_entry->identity)); /* Require descriptor for this node. It has one so it should succeed. */ test_node = entry_is_live(test_entry, ENTRY_NEED_DESCRIPTOR, &msg); - test_assert(test_node); + tt_assert(test_node); tt_ptr_op(test_node, ==, node_get_by_id(test_entry->identity)); done: @@ -608,6 +697,8 @@ static const struct testcase_setup_t fake_network = { }; struct testcase_t entrynodes_tests[] = { + { "entry_is_time_to_retry", test_entry_is_time_to_retry, + TT_FORK, NULL, NULL }, { "choose_random_entry_no_guards", test_choose_random_entry_no_guards, TT_FORK, &fake_network, NULL }, { "choose_random_entry_one_possibleguard", |