[tor-commits] [tor/master] Merge branch 'mr_240_squashed'

nickm at torproject.org nickm at torproject.org
Mon Dec 21 18:23:51 UTC 2020


commit cce7d1edafd6c509d1fae9e2328f3fb923317523
Merge: db5f7b4250 f4cbcde2da
Author: Nick Mathewson <nickm at torproject.org>
Date:   Mon Dec 21 13:23:42 2020 -0500

    Merge branch 'mr_240_squashed'

 changes/ticket40226        |   5 +++
 src/feature/relay/router.c | 106 +++++++++++++++++++++++++++------------------
 src/feature/relay/router.h |   2 +
 src/test/test_stats.c      |  81 ++++++++++++++++++++++++++++++++++
 4 files changed, 151 insertions(+), 43 deletions(-)

diff --cc src/feature/relay/router.h
index f9df1f5637,2648bb5112..93239a7484
--- a/src/feature/relay/router.h
+++ b/src/feature/relay/router.h
@@@ -129,7 -129,8 +129,9 @@@ void router_free_all(void)
  STATIC void get_platform_str(char *platform, size_t len);
  STATIC int router_write_fingerprint(int hashed, int ed25519_identity);
  STATIC smartlist_t *get_my_declared_family(const or_options_t *options);
 +STATIC void router_announce_bridge_status_page(void);
+ STATIC int load_stats_file(const char *filename, const char *ts_tag,
+                            time_t now, char **out);
  
  #ifdef TOR_UNIT_TESTS
  extern time_t desc_clean_since;
diff --cc src/test/test_stats.c
index 25ee837f2f,d45afc7b15..617a36faba
--- a/src/test/test_stats.c
+++ b/src/test/test_stats.c
@@@ -33,7 -31,7 +33,8 @@@
  #define MAINLOOP_PRIVATE
  #define STATEFILE_PRIVATE
  #define BWHIST_PRIVATE
 +#define REPHIST_PRIVATE
+ #define ROUTER_PRIVATE
  
  #include "core/or/or.h"
  #include "lib/err/backtrace.h"
@@@ -496,133 -495,84 +498,211 @@@ test_get_bandwidth_lines(void *arg
    bwhist_free_all();
  }
  
 +static bool
 +mock_should_collect_v3_stats(void)
 +{
 +  return true;
 +}
 +
 +/* Test v3 metrics */
 +static void
 +test_rephist_v3_onions(void *arg)
 +{
 +  int ret;
 +
 +  char *stats_string = NULL;
 +  char *desc1_str = NULL;
 +  ed25519_keypair_t signing_kp1;
 +  hs_descriptor_t *desc1 = NULL;
 +
 +  const hs_v3_stats_t *hs_v3_stats = NULL;
 +
 +  (void) arg;
 +
 +  MOCK(should_collect_v3_stats, mock_should_collect_v3_stats);
 +
 +  get_options_mutable()->HiddenServiceStatistics = 1;
 +
 +  /* Initialize the subsystems */
 +  hs_cache_init();
 +  rep_hist_hs_stats_init(0);
 +
 +  /* Change time to 03-01-2002 23:36 UTC */
 +  update_approx_time(1010101010);
 +
 +  /* HS stats should be zero here */
 +  hs_v3_stats = rep_hist_get_hs_v3_stats();
 +  tt_int_op(digest256map_size(hs_v3_stats->v3_onions_seen_this_period),
 +            OP_EQ, 0);
 +
 +  /* Generate a valid descriptor */
 +  ret = ed25519_keypair_generate(&signing_kp1, 0);
 +  tt_int_op(ret, OP_EQ, 0);
 +  desc1 = hs_helper_build_hs_desc_with_rev_counter(&signing_kp1, 42);
 +  tt_assert(desc1);
 +  ret = hs_desc_encode_descriptor(desc1, &signing_kp1, NULL, &desc1_str);
 +  tt_int_op(ret, OP_EQ, 0);
 +
 +  /* Store descriptor and check that stats got updated */
 +  ret = hs_cache_store_as_dir(desc1_str);
 +  tt_int_op(ret, OP_EQ, 0);
 +  hs_v3_stats = rep_hist_get_hs_v3_stats();
 +  tt_int_op(digest256map_size(hs_v3_stats->v3_onions_seen_this_period),
 +            OP_EQ, 1);
 +
 +  /* cleanup */
 +  hs_descriptor_free(desc1);
 +  tor_free(desc1_str);
 +
 +  /* Generate another valid descriptor */
 +  ret = ed25519_keypair_generate(&signing_kp1, 0);
 +  tt_int_op(ret, OP_EQ, 0);
 +  desc1 = hs_helper_build_hs_desc_with_rev_counter(&signing_kp1, 42);
 +  tt_assert(desc1);
 +  ret = hs_desc_encode_descriptor(desc1, &signing_kp1, NULL, &desc1_str);
 +  tt_int_op(ret, OP_EQ, 0);
 +
 +  /* Store descriptor and check that stats are updated */
 +  ret = hs_cache_store_as_dir(desc1_str);
 +  tt_int_op(ret, OP_EQ, 0);
 +  hs_v3_stats = rep_hist_get_hs_v3_stats();
 +  tt_int_op(digest256map_size(hs_v3_stats->v3_onions_seen_this_period),
 +            OP_EQ, 2);
 +
 +  /* Check that storing the same descriptor twice does not work */
 +  ret = hs_cache_store_as_dir(desc1_str);
 +  tt_int_op(ret, OP_EQ, -1);
 +
 +  /* cleanup */
 +  hs_descriptor_free(desc1);
 +  tor_free(desc1_str);
 +
 +  /* Create a descriptor with the same identity key but diff rev counter and
 +     same blinded key */
 +  desc1 = hs_helper_build_hs_desc_with_rev_counter(&signing_kp1, 43);
 +  tt_assert(desc1);
 +  ret = hs_desc_encode_descriptor(desc1, &signing_kp1, NULL, &desc1_str);
 +  tt_int_op(ret, OP_EQ, 0);
 +
 +  /* Store descriptor and check that stats are updated */
 +  ret = hs_cache_store_as_dir(desc1_str);
 +  tt_int_op(ret, OP_EQ, 0);
 +  tt_int_op(digest256map_size(hs_v3_stats->v3_onions_seen_this_period),
 +            OP_EQ, 2);
 +
 +  /* cleanup */
 +  hs_descriptor_free(desc1);
 +  tor_free(desc1_str);
 +
 +  /* Now let's skip to four days forward so that the blinded key rolls
 +     forward */
 +  update_approx_time(approx_time() + 345600);
 +
 +  /* Now create a descriptor with the same identity key but diff rev counter
 +     and different blinded key */
 +  desc1 = hs_helper_build_hs_desc_with_rev_counter(&signing_kp1, 44);
 +  tt_assert(desc1);
 +  ret = hs_desc_encode_descriptor(desc1, &signing_kp1, NULL, &desc1_str);
 +  tt_int_op(ret, OP_EQ, 0);
 +
 +  /* Store descriptor and check that stats are updated */
 +  ret = hs_cache_store_as_dir(desc1_str);
 +  tt_int_op(ret, OP_EQ, 0);
 +  tt_int_op(digest256map_size(hs_v3_stats->v3_onions_seen_this_period),
 +            OP_EQ, 3);
 +
 +  /* cleanup */
 +  hs_descriptor_free(desc1);
 +  tor_free(desc1_str);
 +
 +  /* Because of differential privacy we can't actually check the stat value,
 +     but let's just check that it's formatted correctly. */
 +  stats_string = rep_hist_format_hs_stats(approx_time(), true);
 +  tt_assert(strstr(stats_string, "hidserv-dir-v3-onions-seen"));
 +
 + done:
 +  UNMOCK(should_collect_v3_stats);
 +  tor_free(stats_string);
 +}
 +
+ static void
+ test_load_stats_file(void *arg)
+ {
+   int ret;
+   char *content = NULL, *read_file_content = NULL, *fname = NULL;
+ 
+   (void) arg;
+ 
+   /* Load conn-stats. */
+   fname = get_datadir_fname("conn-stats");
+   tt_assert(fname);
+   read_file_content = tor_strdup(
+     "conn-bi-direct 2020-12-13 15:48:53 (86400 s) 12,34,56,78\n"
+     "ipv6-conn-bi-direct 2020-12-14 15:48:53 (86400 s) 21,43,65,87\n");
+   write_str_to_file(fname, read_file_content, 0);
+   ret = load_stats_file("conn-stats", "conn-bi-direct", 1607874000, &content);
+   tt_int_op(ret, OP_EQ, 1);
+   tt_str_op(read_file_content, OP_EQ, content);
+ 
+   /* Load hidserv-stats. */
+   tor_free(fname);
+   fname = get_datadir_fname("hidserv-stats");
+   tt_assert(fname);
+   tor_free(read_file_content);
+   read_file_content = tor_strdup(
+     "hidserv-stats-end 2020-12-13 15:48:53 (86400 s)\n"
+     "hidserv-rend-relayed-cells 48754891 delta_f=2048 epsilon=0.30 "
+       "bin_size=1024\n"
+     "hidserv-dir-onions-seen 53 delta_f=8 epsilon=0.30 bin_size=8\n");
+   write_str_to_file(fname, read_file_content, 0);
+   tor_free(content);
+   ret = load_stats_file("hidserv-stats", "hidserv-stats-end", 1607874000,
+                         &content);
+   tt_int_op(ret, OP_EQ, 1);
+   tt_str_op(read_file_content, OP_EQ, content);
+ 
+   /* Load dirreq-stats. */
+   tor_free(fname);
+   fname = get_datadir_fname("dirreq-stats");
+   tt_assert(fname);
+   tor_free(read_file_content);
+   read_file_content = tor_strdup(
+     "dirreq-stats-end 2020-12-13 15:48:53 (86400 s)\n"
+     "dirreq-v3-ips ru=1728,us=1144,de=696,ir=432,gb=328,fr=304,in=296,ua=232\n"
+     "dirreq-v3-reqs ru=3616,us=3576,de=1896,fr=800,gb=632,ir=616\n"
+     "dirreq-v3-resp ok=18472,not-enough-sigs=0,unavailable=0,not-found=0,"
+       "not-modified=3136,busy=0\n"
+     "dirreq-v3-direct-dl complete=0,timeout=0,running=0\n"
+     "dirreq-v3-tunneled-dl complete=18124,timeout=348,running=4,min=257,"
+       "d1=133653,d2=221050,q1=261242,d3=300622,d4=399758,md=539051,d6=721322,"
+       "d7=959866,q3=1103363,d8=1302035,d9=2046125,max=113404000\n");
+   write_str_to_file(fname, read_file_content, 0);
+   tor_free(content);
+   ret = load_stats_file("dirreq-stats", "dirreq-stats-end", 1607874000,
+                         &content);
+   tt_int_op(ret, OP_EQ, 1);
+   tt_str_op(read_file_content, OP_EQ, content);
+ 
+   /* Attempt to load future-stats file not starting with timestamp tag. */
+   tor_free(fname);
+   fname = get_datadir_fname("future-stats");
+   tt_assert(fname);
+   tor_free(read_file_content);
+   read_file_content = tor_strdup(
+     "future-stuff-at-file-start\n"
+     "future-stats 2020-12-13 15:48:53 (86400 s)\n");
+   write_str_to_file(fname, read_file_content, 0);
+   tor_free(content);
+   ret = load_stats_file("future-stats", "future-stats", 1607874000, &content);
+   tt_int_op(ret, OP_EQ, 1);
+   tt_str_op(read_file_content, OP_EQ, content);
+ 
+  done:
+   tor_free(fname);
+   tor_free(read_file_content);
+   tor_free(content);
+ }
+ 
  #define ENT(name)                                                       \
    { #name, test_ ## name , 0, NULL, NULL }
  #define FORK(name)                                                      \
@@@ -636,7 -586,7 +716,8 @@@ struct testcase_t stats_tests[] = 
    FORK(add_obs),
    FORK(fill_bandwidth_history),
    FORK(get_bandwidth_lines),
 +  FORK(rephist_v3_onions),
+   FORK(load_stats_file),
  
    END_OF_TESTCASES
  };



More information about the tor-commits mailing list