]> git.ipfire.org Git - thirdparty/tor.git/commitdiff
Merge branch 'bug23816_029_squashed' into maint-0.3.2
authorNick Mathewson <nickm@torproject.org>
Wed, 8 Nov 2017 13:11:22 +0000 (08:11 -0500)
committerNick Mathewson <nickm@torproject.org>
Wed, 8 Nov 2017 13:11:22 +0000 (08:11 -0500)
1  2 
src/or/directory.c
src/or/directory.h
src/test/test_dir.c

Simple merge
index 79984be32d5ec004766a33a5f7655b4e295440d1,c055e60d9880d496d423e6105cc7b284fe07a6d2..d26d83537799653db409113d239c122c5d7956ce
@@@ -226,23 -167,15 +226,31 @@@ STATIC const smartlist_t *find_dl_sched
  STATIC void find_dl_min_and_max_delay(download_status_t *dls,
                                        const or_options_t *options,
                                        int *min, int *max);
- STATIC int next_random_exponential_delay(int delay, int max_delay);
++
+ STATIC int next_random_exponential_delay(int delay,
+                                          int base_delay,
+                                          int max_delay);
+ STATIC void next_random_exponential_delay_range(int *low_bound_out,
+                                                 int *high_bound_out,
+                                                 int delay,
+                                                 int base_delay);
 -#endif
  
 -#endif
 +STATIC int parse_hs_version_from_post(const char *url, const char *prefix,
 +                                      const char **end_pos);
 +
 +STATIC unsigned parse_accept_encoding_header(const char *h);
 +#endif /* defined(TOR_UNIT_TESTS) */
 +
 +#if defined(TOR_UNIT_TESTS) || defined(DIRECTORY_PRIVATE)
 +/* Used only by directory.c and test_dir.c */
 +
 +/* no more than quadruple the previous delay (multiplier + 1) */
 +#define DIR_DEFAULT_RANDOM_MULTIPLIER (3)
 +/* no more than triple the previous delay */
 +#define DIR_TEST_NET_RANDOM_MULTIPLIER (2)
 +
 +#endif /* defined(TOR_UNIT_TESTS) || defined(DIRECTORY_PRIVATE) */
 +
 +#endif /* !defined(TOR_DIRECTORY_H) */
  
index 87b86c38b822dd2c5c18aad02ca97d9e00730383,1c6662147f0ee3105d173f76778f6cba7fcba4cd..ee4a9780b127c2126eb09fc2235ef52c492fbb21
@@@ -4124,11 -3608,16 +4124,11 @@@ download_status_random_backoff_helper(i
      { 0, 0, 0, DL_SCHED_GENERIC, DL_WANT_AUTHORITY,
                 DL_SCHED_INCREMENT_FAILURE, DL_SCHED_RANDOM_EXPONENTIAL, 0, 0 };
    int increment = -1;
 -  int old_increment;
 +  int old_increment = -1;
    time_t current_time = time(NULL);
-   const int exponent = DIR_DEFAULT_RANDOM_MULTIPLIER + 1;
 -  const int min_delay = 0;
 -  const int max_delay = 1000000;
 -
 -  (void)arg;
  
    /* Check the random backoff cases */
 -  old_increment = 0;
+   int n_attempts = 0;
    do {
      increment = download_status_schedule_get_delay(&dls_random,
                                                     NULL,
      /* Test */
      tt_int_op(increment, OP_GE, min_delay);
      tt_int_op(increment, OP_LE, max_delay);
-     if (dls_random.last_backoff_position == 0) {
-       /* regression tests for 17750
-        * Always use the minimum delay for the first increment */
-       tt_int_op(increment, OP_EQ, min_delay);
-     } else {
-       /* It's times like these I'd love a good saturating arithmetic
-        * implementation */
-       int min_inc = INT_MAX;
-       if (old_increment <= INT_MAX - 1) {
-         min_inc = old_increment + 1;
-       }
-       int max_inc = INT_MAX;
-       if (old_increment <= (INT_MAX - 1)/exponent) {
-         max_inc = (exponent * old_increment) + 1;
-       }
-       /* Regression test for 20534 and friends:
-        * increment must always increase after the first */
-       tt_int_op(increment, OP_GE, min_inc);
-       /* We at most quadruple, and always add one */
-       tt_int_op(increment, OP_LE, max_inc);
-     }
  
 -    if (old_increment)
 -      tt_int_op(increment, OP_LE, old_increment * 3);
 -
      /* Advance */
 -    current_time += increment;
--    ++(dls_random.n_download_attempts);
--    ++(dls_random.n_download_failures);
++    if (dls_random.n_download_attempts < IMPOSSIBLE_TO_DOWNLOAD - 1) {
++      ++(dls_random.n_download_attempts);
++      ++(dls_random.n_download_failures);
++    }
  
      /* Try another maybe */
      old_increment = increment;
    return;
  }
  
 +static void
 +test_dir_download_status_random_backoff(void *arg)
 +{
 +  (void)arg;
 +
 +  /* Do a standard test */
 +  download_status_random_backoff_helper(0, 1000000);
 +  /* Regression test for 20534 and friends:
 +   * try tighter bounds */
 +  download_status_random_backoff_helper(0, 100);
 +  /* regression tests for 17750: initial delay */
 +  download_status_random_backoff_helper(10, 1000);
 +  download_status_random_backoff_helper(20, 30);
 +
 +  /* Pathological cases */
 +  download_status_random_backoff_helper(0, 0);
 +  download_status_random_backoff_helper(1, 1);
 +  download_status_random_backoff_helper(0, INT_MAX);
 +  download_status_random_backoff_helper(INT_MAX/2, INT_MAX);
 +}
 +
+ static void
+ test_dir_download_status_random_backoff_ranges(void *arg)
+ {
+   (void)arg;
+   int lo, hi;
+   next_random_exponential_delay_range(&lo, &hi, 0, 10);
+   tt_int_op(lo, OP_EQ, 10);
+   tt_int_op(hi, OP_EQ, 11);
+   next_random_exponential_delay_range(&lo, &hi, 6, 10);
+   tt_int_op(lo, OP_EQ, 10);
+   tt_int_op(hi, OP_EQ, 6*3);
+   next_random_exponential_delay_range(&lo, &hi, 13, 10);
+   tt_int_op(lo, OP_EQ, 10);
+   tt_int_op(hi, OP_EQ, 13 * 3);
+   next_random_exponential_delay_range(&lo, &hi, 37, 10);
+   tt_int_op(lo, OP_EQ, 10);
+   tt_int_op(hi, OP_EQ, 111);
+   next_random_exponential_delay_range(&lo, &hi, 123, 10);
+   tt_int_op(lo, OP_EQ, 10);
+   tt_int_op(hi, OP_EQ, 369);
+   next_random_exponential_delay_range(&lo, &hi, INT_MAX-5, 10);
+   tt_int_op(lo, OP_EQ, 10);
+   tt_int_op(hi, OP_EQ, INT_MAX);
+  done:
+   ;
+ }
  static void
  test_dir_download_status_increment(void *arg)
  {