]> git.ipfire.org Git - thirdparty/tor.git/commitdiff
Unit testing entry_is_time_to_retry().
authorrl1987 <rl1987@sdf.lonestar.org>
Wed, 20 Aug 2014 19:07:08 +0000 (22:07 +0300)
committerNick Mathewson <nickm@torproject.org>
Wed, 20 Aug 2014 19:29:56 +0000 (15:29 -0400)
src/or/entrynodes.c
src/or/entrynodes.h
src/test/test_entrynodes.c

index ee4f447ee804781304f9c9fbd2cc05da49a8cedb..edf766bb87684c049bdc2811aab5207e6dbca557 100644 (file)
@@ -156,7 +156,7 @@ entry_guard_set_status(entry_guard_t *e, const node_t *node,
 
 /** Return true iff enough time has passed since we last tried to connect
  * to the unreachable guard <b>e</b> that we're willing to try again. */
-static int
+STATIC int
 entry_is_time_to_retry(const entry_guard_t *e, time_t now)
 {
   struct guard_retry_period_s {
index 5d91756aa4bc157f757c6604d64c221dc6d235ab..52b31a225d0e06dbc7e55fe0fd85b857973328d8 100644 (file)
@@ -104,6 +104,9 @@ typedef enum {
 STATIC const node_t *entry_is_live(const entry_guard_t *e,
                                    entry_is_live_flags_t flags,
                                    const char **msg);
+
+STATIC int entry_is_time_to_retry(const entry_guard_t *e, time_t now);
+
 #endif
 
 void remove_all_entry_guards(void);
index ede93fb43a77edd18c0b453f26eb29aa53ae4287..5fee0336da798d901d6cfd3c97c9f3b9c1631829 100644 (file)
@@ -538,6 +538,83 @@ test_entry_guards_set_from_config(void *arg)
   routerset_free(options->EntryNodes);
 }
 
+static void
+test_entry_is_time_to_retry(void *arg)
+{
+  (void)arg;
+
+  entry_guard_t *test_guard;
+  time_t now;
+  int retval;
+
+  now = time(NULL);
+
+  test_guard = tor_malloc(sizeof(entry_guard_t));
+  memset(test_guard,0,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);
+  return;
+}
+
 /** XXX Do some tests that entry_is_live() */
 static void
 test_entry_is_live(void *arg)
@@ -608,6 +685,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",