test_consdiffmgr.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589
  1. /* Copyright (c) 2017, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. #define CONSDIFFMGR_PRIVATE
  4. #include "or.h"
  5. #include "config.h"
  6. #include "conscache.h"
  7. #include "consdiff.h"
  8. #include "consdiffmgr.h"
  9. #include "cpuworker.h"
  10. #include "networkstatus.h"
  11. #include "workqueue.h"
  12. #include "test.h"
  13. #include "log_test_helpers.h"
  14. // ============================== Setup/teardown the consdiffmgr
  15. // These functions get run before/after each test in this module
  16. static void *
  17. consdiffmgr_test_setup(const struct testcase_t *arg)
  18. {
  19. (void)arg;
  20. char *ddir_fname = tor_strdup(get_fname_rnd("datadir_cdm"));
  21. tor_free(get_options_mutable()->DataDirectory);
  22. get_options_mutable()->DataDirectory = ddir_fname; // now owns the pointer.
  23. check_private_dir(ddir_fname, CPD_CREATE, NULL);
  24. consdiff_cfg_t consdiff_cfg = { 7200, 300 };
  25. consdiffmgr_configure(&consdiff_cfg);
  26. return (void *)1; // must return something non-null.
  27. }
  28. static int
  29. consdiffmgr_test_teardown(const struct testcase_t *arg, void *ignore)
  30. {
  31. (void)arg;
  32. (void)ignore;
  33. consdiffmgr_free_all();
  34. return 1;
  35. }
  36. static struct testcase_setup_t setup_diffmgr = {
  37. consdiffmgr_test_setup,
  38. consdiffmgr_test_teardown
  39. };
  40. // ============================== NS faking functions
  41. // These functions are for making quick fake consensus objects and
  42. // strings that are just good enough for consdiff and consdiffmgr.
  43. static networkstatus_t *
  44. fake_ns_new(consensus_flavor_t flav, time_t valid_after)
  45. {
  46. networkstatus_t *ns = tor_malloc_zero(sizeof(networkstatus_t));
  47. ns->type = NS_TYPE_CONSENSUS;
  48. ns->flavor = flav;
  49. ns->valid_after = valid_after;
  50. return ns;
  51. }
  52. static char *
  53. fake_ns_body_new(consensus_flavor_t flav, time_t valid_after)
  54. {
  55. const char *flavor_string = flav == FLAV_NS ? "" : " microdesc";
  56. char valid_after_string[ISO_TIME_LEN+1];
  57. format_iso_time(valid_after_string, valid_after);
  58. char *random_stuff = crypto_random_hostname(3, 25, "junk ", "");
  59. char *consensus;
  60. tor_asprintf(&consensus,
  61. "network-status-version 3%s\n"
  62. "vote-status consensus\n"
  63. "valid-after %s\n"
  64. "r name ccccccccccccccccc etc\nsample\n"
  65. "r name eeeeeeeeeeeeeeeee etc\nbar\n"
  66. "%s\n",
  67. flavor_string,
  68. valid_after_string,
  69. random_stuff);
  70. tor_free(random_stuff);
  71. return consensus;
  72. }
  73. // ============================== Cpuworker mocking code
  74. // These mocking functions and types capture the cpuworker calls
  75. // so we can inspect them and run them in the main thread.
  76. static smartlist_t *fake_cpuworker_queue = NULL;
  77. typedef struct fake_work_queue_ent_t {
  78. enum workqueue_reply_t (*fn)(void *, void *);
  79. void (*reply_fn)(void *);
  80. void *arg;
  81. } fake_work_queue_ent_t;
  82. static struct workqueue_entry_s *
  83. mock_cpuworker_queue_work(enum workqueue_reply_t (*fn)(void *, void *),
  84. void (*reply_fn)(void *),
  85. void *arg)
  86. {
  87. if (! fake_cpuworker_queue)
  88. fake_cpuworker_queue = smartlist_new();
  89. fake_work_queue_ent_t *ent = tor_malloc_zero(sizeof(*ent));
  90. ent->fn = fn;
  91. ent->reply_fn = reply_fn;
  92. ent->arg = arg;
  93. smartlist_add(fake_cpuworker_queue, ent);
  94. return (struct workqueue_entry_s *)ent;
  95. }
  96. static int
  97. mock_cpuworker_run_work(void)
  98. {
  99. if (! fake_cpuworker_queue)
  100. return 0;
  101. SMARTLIST_FOREACH(fake_cpuworker_queue, fake_work_queue_ent_t *, ent, {
  102. enum workqueue_reply_t r = ent->fn(NULL, ent->arg);
  103. if (r != WQ_RPL_REPLY)
  104. return -1;
  105. });
  106. return 0;
  107. }
  108. static void
  109. mock_cpuworker_handle_replies(void)
  110. {
  111. if (! fake_cpuworker_queue)
  112. return;
  113. SMARTLIST_FOREACH(fake_cpuworker_queue, fake_work_queue_ent_t *, ent, {
  114. ent->reply_fn(ent->arg);
  115. });
  116. smartlist_free(fake_cpuworker_queue);
  117. fake_cpuworker_queue = NULL;
  118. }
  119. // ============================== Beginning of tests
  120. #if 0
  121. static int got_failure = 0;
  122. static void
  123. got_assertion_failure(void)
  124. {
  125. ++got_failure;
  126. }
  127. /* XXXX This test won't work, because there is currently no way to actually
  128. * XXXX capture a real assertion failure. */
  129. static void
  130. test_consdiffmgr_init_failure(void *arg)
  131. {
  132. (void)arg;
  133. // Capture assertions and bugs.
  134. /* As in ...test_setup, but do not create the datadir. The missing directory
  135. * will cause a failure. */
  136. char *ddir_fname = tor_strdup(get_fname_rnd("datadir_cdm"));
  137. tor_free(get_options_mutable()->DataDirectory);
  138. get_options_mutable()->DataDirectory = ddir_fname; // now owns the pointer.
  139. consdiff_cfg_t consdiff_cfg = { 7200, 300 };
  140. tor_set_failed_assertion_callback(got_assertion_failure);
  141. tor_capture_bugs_(1);
  142. consdiffmgr_configure(&consdiff_cfg); // This should fail.
  143. tt_int_op(got_failure, OP_EQ, 1);
  144. const smartlist_t *bugs = tor_get_captured_bug_log_();
  145. tt_int_op(smartlist_len(bugs), OP_EQ, 1);
  146. done:
  147. tor_end_capture_bugs_();
  148. }
  149. #endif
  150. static void
  151. test_consdiffmgr_add(void *arg)
  152. {
  153. (void) arg;
  154. time_t now = approx_time();
  155. consensus_cache_entry_t *ent = NULL;
  156. networkstatus_t *ns_tmp = fake_ns_new(FLAV_NS, now);
  157. const char *dummy = "foo";
  158. int r = consdiffmgr_add_consensus(dummy, ns_tmp);
  159. tt_int_op(r, OP_EQ, 0);
  160. /* If we add it again, it won't work */
  161. setup_capture_of_logs(LOG_INFO);
  162. dummy = "bar";
  163. r = consdiffmgr_add_consensus(dummy, ns_tmp);
  164. tt_int_op(r, OP_EQ, -1);
  165. expect_single_log_msg_containing("We already have a copy of that "
  166. "consensus");
  167. mock_clean_saved_logs();
  168. /* But it will work fine if the flavor is different */
  169. dummy = "baz";
  170. ns_tmp->flavor = FLAV_MICRODESC;
  171. r = consdiffmgr_add_consensus(dummy, ns_tmp);
  172. tt_int_op(r, OP_EQ, 0);
  173. /* And it will work fine if the time is different */
  174. dummy = "quux";
  175. ns_tmp->flavor = FLAV_NS;
  176. ns_tmp->valid_after = now - 60;
  177. r = consdiffmgr_add_consensus(dummy, ns_tmp);
  178. tt_int_op(r, OP_EQ, 0);
  179. /* If we add one a long long time ago, it will fail. */
  180. dummy = "xyzzy";
  181. ns_tmp->valid_after = 86400 * 100; /* A few months into 1970 */
  182. r = consdiffmgr_add_consensus(dummy, ns_tmp);
  183. tt_int_op(r, OP_EQ, -1);
  184. expect_single_log_msg_containing("it's too old.");
  185. /* Try looking up a consensuses. */
  186. ent = cdm_cache_lookup_consensus(FLAV_NS, now-60);
  187. tt_assert(ent);
  188. consensus_cache_entry_incref(ent);
  189. size_t s;
  190. const uint8_t *body;
  191. r = consensus_cache_entry_get_body(ent, &body, &s);
  192. tt_int_op(r, OP_EQ, 0);
  193. tt_int_op(s, OP_EQ, 4);
  194. tt_mem_op(body, OP_EQ, "quux", 4);
  195. /* Try looking up another entry, but fail */
  196. tt_assert(NULL == cdm_cache_lookup_consensus(FLAV_MICRODESC, now-60));
  197. tt_assert(NULL == cdm_cache_lookup_consensus(FLAV_NS, now-61));
  198. done:
  199. networkstatus_vote_free(ns_tmp);
  200. teardown_capture_of_logs();
  201. consensus_cache_entry_decref(ent);
  202. }
  203. static void
  204. test_consdiffmgr_make_diffs(void *arg)
  205. {
  206. (void)arg;
  207. networkstatus_t *ns = NULL;
  208. char *ns_body = NULL, *md_ns_body = NULL, *md_ns_body_2 = NULL;
  209. char *applied = NULL, *diff_text = NULL;
  210. time_t now = approx_time();
  211. int r;
  212. consensus_cache_entry_t *diff = NULL;
  213. uint8_t md_ns_sha3[DIGEST256_LEN];
  214. consdiff_status_t diff_status;
  215. MOCK(cpuworker_queue_work, mock_cpuworker_queue_work);
  216. // Try rescan with no consensuses: shouldn't crash or queue work.
  217. consdiffmgr_rescan();
  218. tt_ptr_op(NULL, OP_EQ, fake_cpuworker_queue);
  219. // Make two consensuses, 1 hour sec ago.
  220. ns = fake_ns_new(FLAV_NS, now-3600);
  221. ns_body = fake_ns_body_new(FLAV_NS, now-3600);
  222. r = consdiffmgr_add_consensus(ns_body, ns);
  223. networkstatus_vote_free(ns);
  224. tor_free(ns_body);
  225. tt_int_op(r, OP_EQ, 0);
  226. ns = fake_ns_new(FLAV_MICRODESC, now-3600);
  227. md_ns_body = fake_ns_body_new(FLAV_MICRODESC, now-3600);
  228. r = consdiffmgr_add_consensus(md_ns_body, ns);
  229. crypto_digest256((char*)md_ns_sha3, md_ns_body, strlen(md_ns_body),
  230. DIGEST_SHA3_256);
  231. networkstatus_vote_free(ns);
  232. tt_int_op(r, OP_EQ, 0);
  233. // No diffs will be generated.
  234. consdiffmgr_rescan();
  235. tt_ptr_op(NULL, OP_EQ, fake_cpuworker_queue);
  236. // Add a MD consensus from 45 minutes ago. This should cause one diff
  237. // worth of work to get queued.
  238. ns = fake_ns_new(FLAV_MICRODESC, now-45*60);
  239. md_ns_body_2 = fake_ns_body_new(FLAV_MICRODESC, now-45*60);
  240. r = consdiffmgr_add_consensus(md_ns_body_2, ns);
  241. networkstatus_vote_free(ns);
  242. tt_int_op(r, OP_EQ, 0);
  243. consdiffmgr_rescan();
  244. tt_ptr_op(NULL, OP_NE, fake_cpuworker_queue);
  245. tt_int_op(1, OP_EQ, smartlist_len(fake_cpuworker_queue));
  246. // XXXX not working yet
  247. /*
  248. diff_status = consdiffmgr_find_diff_from(&diff, FLAV_MICRODESC,
  249. DIGEST_SHA3_256,
  250. md_ns_sha3, DIGEST256_LEN);
  251. tt_int_op(CONSDIFF_IN_PROGRESS, OP_EQ, diff_status);
  252. */
  253. // Now run that process and get the diff.
  254. r = mock_cpuworker_run_work();
  255. tt_int_op(r, OP_EQ, 0);
  256. mock_cpuworker_handle_replies();
  257. // At this point we should be able to get that diff.
  258. diff_status = consdiffmgr_find_diff_from(&diff, FLAV_MICRODESC,
  259. DIGEST_SHA3_256,
  260. md_ns_sha3, DIGEST256_LEN);
  261. tt_int_op(CONSDIFF_AVAILABLE, OP_EQ, diff_status);
  262. tt_assert(diff);
  263. /* Make sure applying the diff actually works */
  264. const uint8_t *diff_body;
  265. size_t diff_size;
  266. r = consensus_cache_entry_get_body(diff, &diff_body, &diff_size);
  267. tt_int_op(r, OP_EQ, 0);
  268. diff_text = tor_memdup_nulterm(diff_body, diff_size);
  269. applied = consensus_diff_apply(md_ns_body, diff_text);
  270. tt_assert(applied);
  271. tt_str_op(applied, OP_EQ, md_ns_body_2);
  272. /* Rescan again: no more work to do. */
  273. consdiffmgr_rescan();
  274. tt_ptr_op(NULL, OP_EQ, fake_cpuworker_queue);
  275. done:
  276. tor_free(md_ns_body);
  277. tor_free(md_ns_body_2);
  278. tor_free(diff_text);
  279. tor_free(applied);
  280. }
  281. static void
  282. test_consdiffmgr_diff_rules(void *arg)
  283. {
  284. (void)arg;
  285. #define N 6
  286. char *md_body[N], *ns_body[N];
  287. networkstatus_t *md_ns[N], *ns_ns[N];
  288. uint8_t md_ns_sha3[N][DIGEST256_LEN], ns_ns_sha3[N][DIGEST256_LEN];
  289. int i;
  290. MOCK(cpuworker_queue_work, mock_cpuworker_queue_work);
  291. /* Create a bunch of consensus things at 15-second intervals. */
  292. time_t start = approx_time() - 120;
  293. for (i = 0; i < N; ++i) {
  294. time_t when = start + i * 15;
  295. md_body[i] = fake_ns_body_new(FLAV_MICRODESC, when);
  296. ns_body[i] = fake_ns_body_new(FLAV_NS, when);
  297. md_ns[i] = fake_ns_new(FLAV_MICRODESC, when);
  298. ns_ns[i] = fake_ns_new(FLAV_NS, when);
  299. crypto_digest256((char *)md_ns_sha3[i], md_body[i], strlen(md_body[i]),
  300. DIGEST_SHA3_256);
  301. crypto_digest256((char *)ns_ns_sha3[i], ns_body[i], strlen(ns_body[i]),
  302. DIGEST_SHA3_256);
  303. }
  304. /* For the MD consensuses: add 4 of them, and make sure that
  305. * diffs are created to one consensus (the most recent) only. */
  306. tt_int_op(0, OP_EQ, consdiffmgr_add_consensus(md_body[1], md_ns[1]));
  307. tt_int_op(0, OP_EQ, consdiffmgr_add_consensus(md_body[2], md_ns[2]));
  308. tt_int_op(0, OP_EQ, consdiffmgr_add_consensus(md_body[3], md_ns[3]));
  309. tt_int_op(0, OP_EQ, consdiffmgr_add_consensus(md_body[4], md_ns[4]));
  310. consdiffmgr_rescan();
  311. tt_ptr_op(NULL, OP_NE, fake_cpuworker_queue);
  312. tt_int_op(3, OP_EQ, smartlist_len(fake_cpuworker_queue));
  313. tt_int_op(0, OP_EQ, mock_cpuworker_run_work());
  314. mock_cpuworker_handle_replies();
  315. tt_ptr_op(NULL, OP_EQ, fake_cpuworker_queue);
  316. /* For the NS consensuses: add 3, generate, and add one older one and
  317. * make sure that older one is the only one whose diff is generated */
  318. tt_int_op(0, OP_EQ, consdiffmgr_add_consensus(ns_body[0], ns_ns[0]));
  319. tt_int_op(0, OP_EQ, consdiffmgr_add_consensus(ns_body[1], ns_ns[1]));
  320. tt_int_op(0, OP_EQ, consdiffmgr_add_consensus(ns_body[5], ns_ns[5]));
  321. consdiffmgr_rescan();
  322. tt_ptr_op(NULL, OP_NE, fake_cpuworker_queue);
  323. tt_int_op(2, OP_EQ, smartlist_len(fake_cpuworker_queue));
  324. tt_int_op(0, OP_EQ, mock_cpuworker_run_work());
  325. mock_cpuworker_handle_replies();
  326. tt_int_op(0, OP_EQ, consdiffmgr_add_consensus(ns_body[2], ns_ns[2]));
  327. consdiffmgr_rescan();
  328. tt_ptr_op(NULL, OP_NE, fake_cpuworker_queue);
  329. tt_int_op(1, OP_EQ, smartlist_len(fake_cpuworker_queue));
  330. tt_int_op(0, OP_EQ, mock_cpuworker_run_work());
  331. mock_cpuworker_handle_replies();
  332. done:
  333. for (i = 0; i < N; ++i) {
  334. tor_free(md_body[i]);
  335. tor_free(ns_body[i]);
  336. networkstatus_vote_free(md_ns[i]);
  337. networkstatus_vote_free(ns_ns[i]);
  338. }
  339. UNMOCK(cpuworker_queue_work);
  340. #undef N
  341. }
  342. static void
  343. test_consdiffmgr_diff_failure(void *arg)
  344. {
  345. (void)arg;
  346. MOCK(cpuworker_queue_work, mock_cpuworker_queue_work);
  347. /* We're going to make sure that if we have a bogus request where
  348. * we can't actually compute a diff, the world must not end. */
  349. networkstatus_t *ns1 = NULL;
  350. networkstatus_t *ns2 = NULL;
  351. int r;
  352. ns1 = fake_ns_new(FLAV_NS, approx_time()-100);
  353. ns2 = fake_ns_new(FLAV_NS, approx_time()-50);
  354. r = consdiffmgr_add_consensus("foo bar baz\n", ns1);
  355. tt_int_op(r, OP_EQ, 0);
  356. // We refuse to compute a diff to or from a line holding only a single dot.
  357. // We can add it here, though.
  358. r = consdiffmgr_add_consensus("foo bar baz\n.\n.\n", ns2);
  359. tt_int_op(r, OP_EQ, 0);
  360. consdiffmgr_rescan();
  361. tt_ptr_op(NULL, OP_NE, fake_cpuworker_queue);
  362. setup_capture_of_logs(LOG_WARN);
  363. tt_int_op(1, OP_EQ, smartlist_len(fake_cpuworker_queue));
  364. tt_int_op(0, OP_EQ, mock_cpuworker_run_work());
  365. expect_single_log_msg_containing("one of the lines to be added is \".\".");
  366. mock_clean_saved_logs();
  367. mock_cpuworker_handle_replies();
  368. expect_single_log_msg_containing("Worker was unable to compute consensus "
  369. "diff from ");
  370. done:
  371. teardown_capture_of_logs();
  372. UNMOCK(cpuworker_queue_work);
  373. networkstatus_vote_free(ns1);
  374. networkstatus_vote_free(ns2);
  375. }
  376. static void
  377. test_consdiffmgr_cleanup_old(void *arg)
  378. {
  379. (void)arg;
  380. config_line_t *labels = NULL;
  381. consensus_cache_entry_t *ent = NULL;
  382. consensus_cache_t *cache = cdm_cache_get(); // violate abstraction barrier
  383. /* This item will be will be cleanable because it has a valid-after
  384. * time far in the past. */
  385. config_line_prepend(&labels, "document-type", "confribble-blarg");
  386. config_line_prepend(&labels, "consensus-valid-after",
  387. "1980-10-10T10:10:10");
  388. ent = consensus_cache_add(cache, labels, (const uint8_t*)"Foo", 3);
  389. tt_assert(ent);
  390. consensus_cache_entry_decref(ent);
  391. setup_capture_of_logs(LOG_DEBUG);
  392. tt_int_op(1, OP_EQ, consdiffmgr_cleanup());
  393. expect_log_msg_containing("Deleting entry because its consensus-valid-"
  394. "after value (1980-10-10T10:10:10) was too old");
  395. done:
  396. teardown_capture_of_logs();
  397. config_free_lines(labels);
  398. }
  399. static void
  400. test_consdiffmgr_cleanup_bad_valid_after(void *arg)
  401. {
  402. /* This will seem cleanable, but isn't, because its valid-after time is
  403. * misformed. */
  404. (void)arg;
  405. config_line_t *labels = NULL;
  406. consensus_cache_entry_t *ent = NULL;
  407. consensus_cache_t *cache = cdm_cache_get(); // violate abstraction barrier
  408. config_line_prepend(&labels, "document-type", "consensus");
  409. config_line_prepend(&labels, "consensus-valid-after",
  410. "whan that aprille with his shoures soote"); // (~1385?)
  411. ent = consensus_cache_add(cache, labels, (const uint8_t*)"Foo", 3);
  412. tt_assert(ent);
  413. consensus_cache_entry_decref(ent);
  414. setup_capture_of_logs(LOG_DEBUG);
  415. tt_int_op(0, OP_EQ, consdiffmgr_cleanup());
  416. expect_log_msg_containing("Ignoring entry because its consensus-valid-"
  417. "after value (\"whan that aprille with his "
  418. "shoures soote\") was unparseable");
  419. done:
  420. teardown_capture_of_logs();
  421. config_free_lines(labels);
  422. }
  423. static void
  424. test_consdiffmgr_cleanup_no_valid_after(void *arg)
  425. {
  426. (void)arg;
  427. config_line_t *labels = NULL;
  428. consensus_cache_entry_t *ent = NULL;
  429. consensus_cache_t *cache = cdm_cache_get(); // violate abstraction barrier
  430. /* This item will be will be uncleanable because it has no recognized
  431. * valid-after. */
  432. config_line_prepend(&labels, "document-type", "consensus");
  433. config_line_prepend(&labels, "confrooble-voolid-oofter",
  434. "2010-10-10T09:08:07");
  435. ent = consensus_cache_add(cache, labels, (const uint8_t*)"Foo", 3);
  436. tt_assert(ent);
  437. consensus_cache_entry_decref(ent);
  438. setup_capture_of_logs(LOG_DEBUG);
  439. tt_int_op(0, OP_EQ, consdiffmgr_cleanup());
  440. expect_log_msg_containing("Ignoring entry because it had no consensus-"
  441. "valid-after label");
  442. done:
  443. teardown_capture_of_logs();
  444. config_free_lines(labels);
  445. }
  446. static void
  447. test_consdiffmgr_cleanup_old_diffs(void *arg)
  448. {
  449. (void)arg;
  450. #define N 4
  451. char *md_body[N];
  452. networkstatus_t *md_ns[N];
  453. uint8_t md_ns_sha3[N][DIGEST256_LEN];
  454. int i;
  455. /* Make sure that the cleanup function removes diffs to the not-most-recent
  456. * consensus. */
  457. MOCK(cpuworker_queue_work, mock_cpuworker_queue_work);
  458. /* Create a bunch of consensus things at 15-second intervals. */
  459. time_t start = approx_time() - 120;
  460. for (i = 0; i < N; ++i) {
  461. time_t when = start + i * 15;
  462. md_body[i] = fake_ns_body_new(FLAV_MICRODESC, when);
  463. md_ns[i] = fake_ns_new(FLAV_MICRODESC, when);
  464. crypto_digest256((char *)md_ns_sha3[i], md_body[i], strlen(md_body[i]),
  465. DIGEST_SHA3_256);
  466. }
  467. /* add the first 3. */
  468. tt_int_op(0, OP_EQ, consdiffmgr_add_consensus(md_body[0], md_ns[0]));
  469. tt_int_op(0, OP_EQ, consdiffmgr_add_consensus(md_body[1], md_ns[1]));
  470. tt_int_op(0, OP_EQ, consdiffmgr_add_consensus(md_body[2], md_ns[2]));
  471. /* Make diffs. */
  472. consdiffmgr_rescan();
  473. tt_ptr_op(NULL, OP_NE, fake_cpuworker_queue);
  474. tt_int_op(2, OP_EQ, smartlist_len(fake_cpuworker_queue));
  475. tt_int_op(0, OP_EQ, mock_cpuworker_run_work());
  476. mock_cpuworker_handle_replies();
  477. tt_ptr_op(NULL, OP_EQ, fake_cpuworker_queue);
  478. /* Nothing is deletable now */
  479. tt_int_op(0, OP_EQ, consdiffmgr_cleanup());
  480. /* Now add an even-more-recent consensus; this should make all previous
  481. * diffs deletable */
  482. tt_int_op(0, OP_EQ, consdiffmgr_add_consensus(md_body[3], md_ns[3]));
  483. tt_int_op(2, OP_EQ, consdiffmgr_cleanup());
  484. /* Everything should be valid at this point */
  485. tt_int_op(0, OP_EQ, consdiffmgr_validate());
  486. done:
  487. for (i = 0; i < N; ++i) {
  488. tor_free(md_body[i]);
  489. networkstatus_vote_free(md_ns[i]);
  490. }
  491. UNMOCK(cpuworker_queue_work);
  492. #undef N
  493. }
  494. #define TEST(name) \
  495. { #name, test_consdiffmgr_ ## name , TT_FORK, &setup_diffmgr, NULL }
  496. struct testcase_t consdiffmgr_tests[] = {
  497. #if 0
  498. { "init_failure", test_consdiffmgr_init_failure, TT_FORK, NULL, NULL },
  499. #endif
  500. TEST(add),
  501. TEST(make_diffs),
  502. TEST(diff_rules),
  503. TEST(diff_failure),
  504. TEST(cleanup_old),
  505. TEST(cleanup_bad_valid_after),
  506. TEST(cleanup_no_valid_after),
  507. TEST(cleanup_old_diffs),
  508. END_OF_TESTCASES
  509. };