Browse Source

Add unit test for dump_desc_populate_fifo_from_directory()

Andrea Shepard 7 years ago
parent
commit
9580b99dab
3 changed files with 95 additions and 3 deletions
  1. 1 3
      src/or/routerparse.c
  2. 1 0
      src/or/routerparse.h
  3. 93 0
      src/test/test_dir.c

+ 1 - 3
src/or/routerparse.c

@@ -588,8 +588,6 @@ static int check_signature_token(const char *digest,
 
 /* Dump mechanism for unparseable descriptors */
 
-static void dump_desc_populate_fifo_from_directory(const char *dirname);
-
 /** List of dumped descriptors for FIFO cleanup purposes */
 STATIC smartlist_t *descs_dumped = NULL;
 /** Total size of dumped descriptors for FIFO cleanup */
@@ -972,7 +970,7 @@ dump_desc_compare_fifo_entries(const void **a_v, const void **b_v)
  * reconstructed order will be wrong, but will always be a permutation of
  * the original.
  */
-static void
+STATIC void
 dump_desc_populate_fifo_from_directory(const char *dirname)
 {
   smartlist_t *files = NULL;

+ 1 - 0
src/or/routerparse.h

@@ -109,6 +109,7 @@ STATIC int routerstatus_parse_guardfraction(const char *guardfraction_str,
                                             routerstatus_t *rs);
 MOCK_DECL(STATIC dumped_desc_t *, dump_desc_populate_one_file,
     (const char *dirname, const char *f));
+STATIC void dump_desc_populate_fifo_from_directory(const char *dirname);
 STATIC void dump_desc(const char *desc, const char *type);
 STATIC void dump_desc_fifo_cleanup(void);
 #endif

+ 93 - 0
src/test/test_dir.c

@@ -4865,6 +4865,7 @@ read_file_to_str_mock(const char *filename, int flags,
   return result;
 }
 
+/* This one tests dump_desc_populate_one_file() */
 static void
 test_dir_populate_dump_desc_fifo(void *data)
 {
@@ -4996,6 +4997,97 @@ test_dir_populate_dump_desc_fifo(void *data)
   return;
 }
 
+static smartlist_t *
+listdir_mock(const char *dname)
+{
+  smartlist_t *l;
+
+  /* Ignore the name, always return this list */
+  (void)dname;
+
+  l = smartlist_new();
+  smartlist_add(l, tor_strdup("foo"));
+  smartlist_add(l, tor_strdup("bar"));
+  smartlist_add(l, tor_strdup("baz"));
+
+  return l;
+}
+
+static dumped_desc_t *
+pop_one_mock(const char *dirname, const char *f)
+{
+  dumped_desc_t *ent = NULL;
+
+  if (dirname != NULL && strcmp(dirname, "d") == 0) {
+    if (f != NULL && strcmp(f, "foo") == 0) {
+      ent = tor_malloc_zero(sizeof(*ent));
+      ent->filename = strdup("d/foo");
+      ent->len = 123;
+      ent->digest_sha256[0] = 1;
+      ent->when = 1024;
+    } else if (f != NULL && strcmp(f, "bar") == 0) {
+      ent = tor_malloc_zero(sizeof(*ent));
+      ent->filename = strdup("d/bar");
+      ent->len = 456;
+      ent->digest_sha256[0] = 2;
+      /*
+       * Note that the timestamps are in a different order than
+       * listdir_mock() returns; we're testing the sort order.
+       */
+      ent->when = 512;
+    } else if (f != NULL && strcmp(f, "baz") == 0) {
+      ent = tor_malloc_zero(sizeof(*ent));
+      ent->filename = strdup("d/baz");
+      ent->len = 789;
+      ent->digest_sha256[0] = 3;
+      ent->when = 768;
+    }
+  }
+
+  return ent;
+}
+
+/* This one tests dump_desc_populate_fifo_from_directory() */
+static void
+test_dir_populate_dump_desc_fifo_2(void *data)
+{
+  dumped_desc_t *ent = NULL;
+
+  (void)data;
+
+  /* Set up the mocks */
+  MOCK(tor_listdir, listdir_mock);
+  MOCK(dump_desc_populate_one_file, pop_one_mock);
+
+  /* Run dump_desc_populate_fifo_from_directory() */
+  descs_dumped = NULL;
+  len_descs_dumped = 0;
+  dump_desc_populate_fifo_from_directory("d");
+  tt_assert(descs_dumped != NULL);
+  tt_int_op(smartlist_len(descs_dumped), OP_EQ, 3);
+  tt_int_op(len_descs_dumped, OP_EQ, 1368);
+  ent = smartlist_get(descs_dumped, 0);
+  tt_str_op(ent->filename, OP_EQ, "d/bar");
+  tt_int_op(ent->len, OP_EQ, 456);
+  tt_int_op(ent->when, OP_EQ, 512);
+  ent = smartlist_get(descs_dumped, 1);
+  tt_str_op(ent->filename, OP_EQ, "d/baz");
+  tt_int_op(ent->len, OP_EQ, 789);
+  tt_int_op(ent->when, OP_EQ, 768);
+  ent = smartlist_get(descs_dumped, 2);
+  tt_str_op(ent->filename, OP_EQ, "d/foo");
+  tt_int_op(ent->len, OP_EQ, 123);
+  tt_int_op(ent->when, OP_EQ, 1024);
+
+ done:
+  dump_desc_fifo_cleanup();
+
+  UNMOCK(dump_desc_populate_one_file);
+  UNMOCK(tor_listdir);
+
+  return;
+}
+
 static int mock_networkstatus_consensus_is_bootstrapping_value = 0;
 static int
 mock_networkstatus_consensus_is_bootstrapping(time_t now)
@@ -5207,6 +5299,7 @@ struct testcase_t dir_tests[] = {
   DIR(choose_compression_level, 0),
   DIR(dump_unparseable_descriptors, 0),
   DIR(populate_dump_desc_fifo, 0),
+  DIR(populate_dump_desc_fifo_2, 0),
   DIR_ARG(find_dl_schedule, TT_FORK, "bf"),
   DIR_ARG(find_dl_schedule, TT_FORK, "ba"),
   DIR_ARG(find_dl_schedule, TT_FORK, "cf"),