test_consdiffmgr.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688
  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. // ============================== Other helpers
  120. static consdiff_status_t
  121. lookup_diff_from(consensus_cache_entry_t **out,
  122. consensus_flavor_t flav,
  123. const char *str1)
  124. {
  125. uint8_t digest[DIGEST256_LEN];
  126. crypto_digest256((char*)digest, str1, strlen(str1), DIGEST_SHA3_256);
  127. return consdiffmgr_find_diff_from(out, flav,
  128. DIGEST_SHA3_256, digest, sizeof(digest));
  129. }
  130. static int
  131. lookup_apply_and_verify_diff(consensus_flavor_t flav,
  132. const char *str1,
  133. const char *str2)
  134. {
  135. char *diff_string = NULL;
  136. consensus_cache_entry_t *ent = NULL;
  137. consdiff_status_t status = lookup_diff_from(&ent, flav, str1);
  138. if (ent == NULL || status != CONSDIFF_AVAILABLE)
  139. return -1;
  140. consensus_cache_entry_incref(ent);
  141. size_t size;
  142. const uint8_t *body;
  143. int r = consensus_cache_entry_get_body(ent, &body, &size);
  144. if (r == 0)
  145. diff_string = tor_memdup_nulterm(body, size);
  146. consensus_cache_entry_decref(ent);
  147. if (diff_string == NULL)
  148. return -1;
  149. char *applied = consensus_diff_apply(str1, diff_string);
  150. tor_free(diff_string);
  151. if (applied == NULL)
  152. return -1;
  153. int match = !strcmp(applied, str2);
  154. tor_free(applied);
  155. return match ? 0 : -1;
  156. }
  157. // ============================== Beginning of tests
  158. #if 0
  159. static int got_failure = 0;
  160. static void
  161. got_assertion_failure(void)
  162. {
  163. ++got_failure;
  164. }
  165. /* XXXX This test won't work, because there is currently no way to actually
  166. * XXXX capture a real assertion failure. */
  167. static void
  168. test_consdiffmgr_init_failure(void *arg)
  169. {
  170. (void)arg;
  171. // Capture assertions and bugs.
  172. /* As in ...test_setup, but do not create the datadir. The missing directory
  173. * will cause a failure. */
  174. char *ddir_fname = tor_strdup(get_fname_rnd("datadir_cdm"));
  175. tor_free(get_options_mutable()->DataDirectory);
  176. get_options_mutable()->DataDirectory = ddir_fname; // now owns the pointer.
  177. consdiff_cfg_t consdiff_cfg = { 7200, 300 };
  178. tor_set_failed_assertion_callback(got_assertion_failure);
  179. tor_capture_bugs_(1);
  180. consdiffmgr_configure(&consdiff_cfg); // This should fail.
  181. tt_int_op(got_failure, OP_EQ, 1);
  182. const smartlist_t *bugs = tor_get_captured_bug_log_();
  183. tt_int_op(smartlist_len(bugs), OP_EQ, 1);
  184. done:
  185. tor_end_capture_bugs_();
  186. }
  187. #endif
  188. static void
  189. test_consdiffmgr_add(void *arg)
  190. {
  191. (void) arg;
  192. time_t now = approx_time();
  193. consensus_cache_entry_t *ent = NULL;
  194. networkstatus_t *ns_tmp = fake_ns_new(FLAV_NS, now);
  195. const char *dummy = "foo";
  196. int r = consdiffmgr_add_consensus(dummy, ns_tmp);
  197. tt_int_op(r, OP_EQ, 0);
  198. /* If we add it again, it won't work */
  199. setup_capture_of_logs(LOG_INFO);
  200. dummy = "bar";
  201. r = consdiffmgr_add_consensus(dummy, ns_tmp);
  202. tt_int_op(r, OP_EQ, -1);
  203. expect_single_log_msg_containing("We already have a copy of that "
  204. "consensus");
  205. mock_clean_saved_logs();
  206. /* But it will work fine if the flavor is different */
  207. dummy = "baz";
  208. ns_tmp->flavor = FLAV_MICRODESC;
  209. r = consdiffmgr_add_consensus(dummy, ns_tmp);
  210. tt_int_op(r, OP_EQ, 0);
  211. /* And it will work fine if the time is different */
  212. dummy = "quux";
  213. ns_tmp->flavor = FLAV_NS;
  214. ns_tmp->valid_after = now - 60;
  215. r = consdiffmgr_add_consensus(dummy, ns_tmp);
  216. tt_int_op(r, OP_EQ, 0);
  217. /* If we add one a long long time ago, it will fail. */
  218. dummy = "xyzzy";
  219. ns_tmp->valid_after = 86400 * 100; /* A few months into 1970 */
  220. r = consdiffmgr_add_consensus(dummy, ns_tmp);
  221. tt_int_op(r, OP_EQ, -1);
  222. expect_single_log_msg_containing("it's too old.");
  223. /* Try looking up a consensuses. */
  224. ent = cdm_cache_lookup_consensus(FLAV_NS, now-60);
  225. tt_assert(ent);
  226. consensus_cache_entry_incref(ent);
  227. size_t s;
  228. const uint8_t *body;
  229. r = consensus_cache_entry_get_body(ent, &body, &s);
  230. tt_int_op(r, OP_EQ, 0);
  231. tt_int_op(s, OP_EQ, 4);
  232. tt_mem_op(body, OP_EQ, "quux", 4);
  233. /* Try looking up another entry, but fail */
  234. tt_assert(NULL == cdm_cache_lookup_consensus(FLAV_MICRODESC, now-60));
  235. tt_assert(NULL == cdm_cache_lookup_consensus(FLAV_NS, now-61));
  236. done:
  237. networkstatus_vote_free(ns_tmp);
  238. teardown_capture_of_logs();
  239. consensus_cache_entry_decref(ent);
  240. }
  241. static void
  242. test_consdiffmgr_make_diffs(void *arg)
  243. {
  244. (void)arg;
  245. networkstatus_t *ns = NULL;
  246. char *ns_body = NULL, *md_ns_body = NULL, *md_ns_body_2 = NULL;
  247. char *applied = NULL, *diff_text = NULL;
  248. time_t now = approx_time();
  249. int r;
  250. consensus_cache_entry_t *diff = NULL;
  251. uint8_t md_ns_sha3[DIGEST256_LEN];
  252. consdiff_status_t diff_status;
  253. MOCK(cpuworker_queue_work, mock_cpuworker_queue_work);
  254. // Try rescan with no consensuses: shouldn't crash or queue work.
  255. consdiffmgr_rescan();
  256. tt_ptr_op(NULL, OP_EQ, fake_cpuworker_queue);
  257. // Make two consensuses, 1 hour sec ago.
  258. ns = fake_ns_new(FLAV_NS, now-3600);
  259. ns_body = fake_ns_body_new(FLAV_NS, now-3600);
  260. r = consdiffmgr_add_consensus(ns_body, ns);
  261. networkstatus_vote_free(ns);
  262. tor_free(ns_body);
  263. tt_int_op(r, OP_EQ, 0);
  264. ns = fake_ns_new(FLAV_MICRODESC, now-3600);
  265. md_ns_body = fake_ns_body_new(FLAV_MICRODESC, now-3600);
  266. r = consdiffmgr_add_consensus(md_ns_body, ns);
  267. crypto_digest256((char*)md_ns_sha3, md_ns_body, strlen(md_ns_body),
  268. DIGEST_SHA3_256);
  269. networkstatus_vote_free(ns);
  270. tt_int_op(r, OP_EQ, 0);
  271. // No diffs will be generated.
  272. consdiffmgr_rescan();
  273. tt_ptr_op(NULL, OP_EQ, fake_cpuworker_queue);
  274. // Add a MD consensus from 45 minutes ago. This should cause one diff
  275. // worth of work to get queued.
  276. ns = fake_ns_new(FLAV_MICRODESC, now-45*60);
  277. md_ns_body_2 = fake_ns_body_new(FLAV_MICRODESC, now-45*60);
  278. r = consdiffmgr_add_consensus(md_ns_body_2, ns);
  279. networkstatus_vote_free(ns);
  280. tt_int_op(r, OP_EQ, 0);
  281. consdiffmgr_rescan();
  282. tt_ptr_op(NULL, OP_NE, fake_cpuworker_queue);
  283. tt_int_op(1, OP_EQ, smartlist_len(fake_cpuworker_queue));
  284. diff_status = consdiffmgr_find_diff_from(&diff, FLAV_MICRODESC,
  285. DIGEST_SHA3_256,
  286. md_ns_sha3, DIGEST256_LEN);
  287. tt_int_op(CONSDIFF_IN_PROGRESS, OP_EQ, diff_status);
  288. // Now run that process and get the diff.
  289. r = mock_cpuworker_run_work();
  290. tt_int_op(r, OP_EQ, 0);
  291. mock_cpuworker_handle_replies();
  292. // At this point we should be able to get that diff.
  293. diff_status = consdiffmgr_find_diff_from(&diff, FLAV_MICRODESC,
  294. DIGEST_SHA3_256,
  295. md_ns_sha3, DIGEST256_LEN);
  296. tt_int_op(CONSDIFF_AVAILABLE, OP_EQ, diff_status);
  297. tt_assert(diff);
  298. /* Make sure applying the diff actually works */
  299. const uint8_t *diff_body;
  300. size_t diff_size;
  301. r = consensus_cache_entry_get_body(diff, &diff_body, &diff_size);
  302. tt_int_op(r, OP_EQ, 0);
  303. diff_text = tor_memdup_nulterm(diff_body, diff_size);
  304. applied = consensus_diff_apply(md_ns_body, diff_text);
  305. tt_assert(applied);
  306. tt_str_op(applied, OP_EQ, md_ns_body_2);
  307. /* Rescan again: no more work to do. */
  308. consdiffmgr_rescan();
  309. tt_ptr_op(NULL, OP_EQ, fake_cpuworker_queue);
  310. done:
  311. tor_free(md_ns_body);
  312. tor_free(md_ns_body_2);
  313. tor_free(diff_text);
  314. tor_free(applied);
  315. }
  316. static void
  317. test_consdiffmgr_diff_rules(void *arg)
  318. {
  319. (void)arg;
  320. #define N 6
  321. char *md_body[N], *ns_body[N];
  322. networkstatus_t *md_ns[N], *ns_ns[N];
  323. uint8_t md_ns_sha3[N][DIGEST256_LEN], ns_ns_sha3[N][DIGEST256_LEN];
  324. int i;
  325. MOCK(cpuworker_queue_work, mock_cpuworker_queue_work);
  326. /* Create a bunch of consensus things at 15-second intervals. */
  327. time_t start = approx_time() - 120;
  328. for (i = 0; i < N; ++i) {
  329. time_t when = start + i * 15;
  330. md_body[i] = fake_ns_body_new(FLAV_MICRODESC, when);
  331. ns_body[i] = fake_ns_body_new(FLAV_NS, when);
  332. md_ns[i] = fake_ns_new(FLAV_MICRODESC, when);
  333. ns_ns[i] = fake_ns_new(FLAV_NS, when);
  334. crypto_digest256((char *)md_ns_sha3[i], md_body[i], strlen(md_body[i]),
  335. DIGEST_SHA3_256);
  336. crypto_digest256((char *)ns_ns_sha3[i], ns_body[i], strlen(ns_body[i]),
  337. DIGEST_SHA3_256);
  338. }
  339. /* For the MD consensuses: add 4 of them, and make sure that
  340. * diffs are created to one consensus (the most recent) only. */
  341. tt_int_op(0, OP_EQ, consdiffmgr_add_consensus(md_body[1], md_ns[1]));
  342. tt_int_op(0, OP_EQ, consdiffmgr_add_consensus(md_body[2], md_ns[2]));
  343. tt_int_op(0, OP_EQ, consdiffmgr_add_consensus(md_body[3], md_ns[3]));
  344. tt_int_op(0, OP_EQ, consdiffmgr_add_consensus(md_body[4], md_ns[4]));
  345. consdiffmgr_rescan();
  346. tt_ptr_op(NULL, OP_NE, fake_cpuworker_queue);
  347. tt_int_op(3, OP_EQ, smartlist_len(fake_cpuworker_queue));
  348. tt_int_op(0, OP_EQ, mock_cpuworker_run_work());
  349. mock_cpuworker_handle_replies();
  350. tt_ptr_op(NULL, OP_EQ, fake_cpuworker_queue);
  351. /* For the NS consensuses: add 3, generate, and add one older one and
  352. * make sure that older one is the only one whose diff is generated */
  353. tt_int_op(0, OP_EQ, consdiffmgr_add_consensus(ns_body[0], ns_ns[0]));
  354. tt_int_op(0, OP_EQ, consdiffmgr_add_consensus(ns_body[1], ns_ns[1]));
  355. tt_int_op(0, OP_EQ, consdiffmgr_add_consensus(ns_body[5], ns_ns[5]));
  356. consdiffmgr_rescan();
  357. tt_ptr_op(NULL, OP_NE, fake_cpuworker_queue);
  358. tt_int_op(2, OP_EQ, smartlist_len(fake_cpuworker_queue));
  359. tt_int_op(0, OP_EQ, mock_cpuworker_run_work());
  360. mock_cpuworker_handle_replies();
  361. /* At this point, we should actually have working diffs! */
  362. tt_int_op(0, OP_EQ,
  363. lookup_apply_and_verify_diff(FLAV_NS, ns_body[0], ns_body[5]));
  364. tt_int_op(0, OP_EQ,
  365. lookup_apply_and_verify_diff(FLAV_NS, ns_body[1], ns_body[5]));
  366. tt_int_op(0, OP_EQ,
  367. lookup_apply_and_verify_diff(FLAV_MICRODESC, md_body[1], md_body[4]));
  368. tt_int_op(0, OP_EQ,
  369. lookup_apply_and_verify_diff(FLAV_MICRODESC, md_body[2], md_body[4]));
  370. tt_int_op(0, OP_EQ,
  371. lookup_apply_and_verify_diff(FLAV_MICRODESC, md_body[3], md_body[4]));
  372. /* Self-to-self diff won't be present */
  373. consensus_cache_entry_t *ent;
  374. tt_int_op(CONSDIFF_NOT_FOUND, OP_EQ,
  375. lookup_diff_from(&ent, FLAV_NS, ns_body[5]));
  376. /* No diff from 2 has been added yet */
  377. tt_int_op(CONSDIFF_NOT_FOUND, OP_EQ,
  378. lookup_diff_from(&ent, FLAV_NS, ns_body[2]));
  379. /* No diff arriving at old things. */
  380. tt_int_op(-1, OP_EQ,
  381. lookup_apply_and_verify_diff(FLAV_MICRODESC, md_body[1], md_body[2]));
  382. /* No backwards diff */
  383. tt_int_op(-1, OP_EQ,
  384. lookup_apply_and_verify_diff(FLAV_MICRODESC, md_body[4], md_body[3]));
  385. /* Now, an update: add number 2 and make sure it's the only one whose diff
  386. * is regenerated. */
  387. tt_int_op(0, OP_EQ, consdiffmgr_add_consensus(ns_body[2], ns_ns[2]));
  388. consdiffmgr_rescan();
  389. tt_ptr_op(NULL, OP_NE, fake_cpuworker_queue);
  390. tt_int_op(1, OP_EQ, smartlist_len(fake_cpuworker_queue));
  391. tt_int_op(0, OP_EQ, mock_cpuworker_run_work());
  392. mock_cpuworker_handle_replies();
  393. tt_int_op(0, OP_EQ,
  394. lookup_apply_and_verify_diff(FLAV_NS, ns_body[2], ns_body[5]));
  395. done:
  396. for (i = 0; i < N; ++i) {
  397. tor_free(md_body[i]);
  398. tor_free(ns_body[i]);
  399. networkstatus_vote_free(md_ns[i]);
  400. networkstatus_vote_free(ns_ns[i]);
  401. }
  402. UNMOCK(cpuworker_queue_work);
  403. #undef N
  404. }
  405. static void
  406. test_consdiffmgr_diff_failure(void *arg)
  407. {
  408. (void)arg;
  409. MOCK(cpuworker_queue_work, mock_cpuworker_queue_work);
  410. /* We're going to make sure that if we have a bogus request where
  411. * we can't actually compute a diff, the world must not end. */
  412. networkstatus_t *ns1 = NULL;
  413. networkstatus_t *ns2 = NULL;
  414. int r;
  415. ns1 = fake_ns_new(FLAV_NS, approx_time()-100);
  416. ns2 = fake_ns_new(FLAV_NS, approx_time()-50);
  417. r = consdiffmgr_add_consensus("foo bar baz\n", ns1);
  418. tt_int_op(r, OP_EQ, 0);
  419. // We refuse to compute a diff to or from a line holding only a single dot.
  420. // We can add it here, though.
  421. r = consdiffmgr_add_consensus("foo bar baz\n.\n.\n", ns2);
  422. tt_int_op(r, OP_EQ, 0);
  423. consdiffmgr_rescan();
  424. tt_ptr_op(NULL, OP_NE, fake_cpuworker_queue);
  425. setup_capture_of_logs(LOG_WARN);
  426. tt_int_op(1, OP_EQ, smartlist_len(fake_cpuworker_queue));
  427. tt_int_op(0, OP_EQ, mock_cpuworker_run_work());
  428. expect_single_log_msg_containing("one of the lines to be added is \".\".");
  429. mock_clean_saved_logs();
  430. mock_cpuworker_handle_replies();
  431. expect_single_log_msg_containing("Worker was unable to compute consensus "
  432. "diff from ");
  433. /* Make sure the diff is not present */
  434. consensus_cache_entry_t *ent;
  435. tt_int_op(CONSDIFF_NOT_FOUND, OP_EQ,
  436. lookup_diff_from(&ent, FLAV_NS, "foo bar baz\n"));
  437. done:
  438. teardown_capture_of_logs();
  439. UNMOCK(cpuworker_queue_work);
  440. networkstatus_vote_free(ns1);
  441. networkstatus_vote_free(ns2);
  442. }
  443. static void
  444. test_consdiffmgr_cleanup_old(void *arg)
  445. {
  446. (void)arg;
  447. config_line_t *labels = NULL;
  448. consensus_cache_entry_t *ent = NULL;
  449. consensus_cache_t *cache = cdm_cache_get(); // violate abstraction barrier
  450. /* This item will be will be cleanable because it has a valid-after
  451. * time far in the past. */
  452. config_line_prepend(&labels, "document-type", "confribble-blarg");
  453. config_line_prepend(&labels, "consensus-valid-after",
  454. "1980-10-10T10:10:10");
  455. ent = consensus_cache_add(cache, labels, (const uint8_t*)"Foo", 3);
  456. tt_assert(ent);
  457. consensus_cache_entry_decref(ent);
  458. setup_capture_of_logs(LOG_DEBUG);
  459. tt_int_op(1, OP_EQ, consdiffmgr_cleanup());
  460. expect_log_msg_containing("Deleting entry because its consensus-valid-"
  461. "after value (1980-10-10T10:10:10) was too old");
  462. done:
  463. teardown_capture_of_logs();
  464. config_free_lines(labels);
  465. }
  466. static void
  467. test_consdiffmgr_cleanup_bad_valid_after(void *arg)
  468. {
  469. /* This will seem cleanable, but isn't, because its valid-after time is
  470. * misformed. */
  471. (void)arg;
  472. config_line_t *labels = NULL;
  473. consensus_cache_entry_t *ent = NULL;
  474. consensus_cache_t *cache = cdm_cache_get(); // violate abstraction barrier
  475. config_line_prepend(&labels, "document-type", "consensus");
  476. config_line_prepend(&labels, "consensus-valid-after",
  477. "whan that aprille with his shoures soote"); // (~1385?)
  478. ent = consensus_cache_add(cache, labels, (const uint8_t*)"Foo", 3);
  479. tt_assert(ent);
  480. consensus_cache_entry_decref(ent);
  481. setup_capture_of_logs(LOG_DEBUG);
  482. tt_int_op(0, OP_EQ, consdiffmgr_cleanup());
  483. expect_log_msg_containing("Ignoring entry because its consensus-valid-"
  484. "after value (\"whan that aprille with his "
  485. "shoures soote\") was unparseable");
  486. done:
  487. teardown_capture_of_logs();
  488. config_free_lines(labels);
  489. }
  490. static void
  491. test_consdiffmgr_cleanup_no_valid_after(void *arg)
  492. {
  493. (void)arg;
  494. config_line_t *labels = NULL;
  495. consensus_cache_entry_t *ent = NULL;
  496. consensus_cache_t *cache = cdm_cache_get(); // violate abstraction barrier
  497. /* This item will be will be uncleanable because it has no recognized
  498. * valid-after. */
  499. config_line_prepend(&labels, "document-type", "consensus");
  500. config_line_prepend(&labels, "confrooble-voolid-oofter",
  501. "2010-10-10T09:08:07");
  502. ent = consensus_cache_add(cache, labels, (const uint8_t*)"Foo", 3);
  503. tt_assert(ent);
  504. consensus_cache_entry_decref(ent);
  505. setup_capture_of_logs(LOG_DEBUG);
  506. tt_int_op(0, OP_EQ, consdiffmgr_cleanup());
  507. expect_log_msg_containing("Ignoring entry because it had no consensus-"
  508. "valid-after label");
  509. done:
  510. teardown_capture_of_logs();
  511. config_free_lines(labels);
  512. }
  513. static void
  514. test_consdiffmgr_cleanup_old_diffs(void *arg)
  515. {
  516. (void)arg;
  517. #define N 4
  518. char *md_body[N];
  519. networkstatus_t *md_ns[N];
  520. uint8_t md_ns_sha3[N][DIGEST256_LEN];
  521. int i;
  522. /* Make sure that the cleanup function removes diffs to the not-most-recent
  523. * consensus. */
  524. MOCK(cpuworker_queue_work, mock_cpuworker_queue_work);
  525. /* Create a bunch of consensus things at 15-second intervals. */
  526. time_t start = approx_time() - 120;
  527. for (i = 0; i < N; ++i) {
  528. time_t when = start + i * 15;
  529. md_body[i] = fake_ns_body_new(FLAV_MICRODESC, when);
  530. md_ns[i] = fake_ns_new(FLAV_MICRODESC, when);
  531. crypto_digest256((char *)md_ns_sha3[i], md_body[i], strlen(md_body[i]),
  532. DIGEST_SHA3_256);
  533. }
  534. /* add the first 3. */
  535. tt_int_op(0, OP_EQ, consdiffmgr_add_consensus(md_body[0], md_ns[0]));
  536. tt_int_op(0, OP_EQ, consdiffmgr_add_consensus(md_body[1], md_ns[1]));
  537. tt_int_op(0, OP_EQ, consdiffmgr_add_consensus(md_body[2], md_ns[2]));
  538. /* Make diffs. */
  539. consdiffmgr_rescan();
  540. tt_ptr_op(NULL, OP_NE, fake_cpuworker_queue);
  541. tt_int_op(2, OP_EQ, smartlist_len(fake_cpuworker_queue));
  542. tt_int_op(0, OP_EQ, mock_cpuworker_run_work());
  543. mock_cpuworker_handle_replies();
  544. tt_ptr_op(NULL, OP_EQ, fake_cpuworker_queue);
  545. /* Nothing is deletable now */
  546. tt_int_op(0, OP_EQ, consdiffmgr_cleanup());
  547. tt_int_op(0, OP_EQ,
  548. lookup_apply_and_verify_diff(FLAV_MICRODESC, md_body[0], md_body[2]));
  549. tt_int_op(0, OP_EQ,
  550. lookup_apply_and_verify_diff(FLAV_MICRODESC, md_body[1], md_body[2]));
  551. /* Now add an even-more-recent consensus; this should make all previous
  552. * diffs deletable */
  553. tt_int_op(0, OP_EQ, consdiffmgr_add_consensus(md_body[3], md_ns[3]));
  554. tt_int_op(2, OP_EQ, consdiffmgr_cleanup());
  555. consensus_cache_entry_t *ent;
  556. tt_int_op(CONSDIFF_NOT_FOUND, OP_EQ,
  557. lookup_diff_from(&ent, FLAV_MICRODESC, md_body[0]));
  558. tt_int_op(CONSDIFF_NOT_FOUND, OP_EQ,
  559. lookup_diff_from(&ent, FLAV_MICRODESC, md_body[1]));
  560. tt_int_op(CONSDIFF_NOT_FOUND, OP_EQ,
  561. lookup_diff_from(&ent, FLAV_MICRODESC, md_body[2]));
  562. /* Everything should be valid at this point */
  563. tt_int_op(0, OP_EQ, consdiffmgr_validate());
  564. done:
  565. for (i = 0; i < N; ++i) {
  566. tor_free(md_body[i]);
  567. networkstatus_vote_free(md_ns[i]);
  568. }
  569. UNMOCK(cpuworker_queue_work);
  570. #undef N
  571. }
  572. #define TEST(name) \
  573. { #name, test_consdiffmgr_ ## name , TT_FORK, &setup_diffmgr, NULL }
  574. struct testcase_t consdiffmgr_tests[] = {
  575. #if 0
  576. { "init_failure", test_consdiffmgr_init_failure, TT_FORK, NULL, NULL },
  577. #endif
  578. TEST(add),
  579. TEST(make_diffs),
  580. TEST(diff_rules),
  581. TEST(diff_failure),
  582. TEST(cleanup_old),
  583. TEST(cleanup_bad_valid_after),
  584. TEST(cleanup_no_valid_after),
  585. TEST(cleanup_old_diffs),
  586. // XXXX Test: no duplicate diff job is launched when a job is pending.
  587. // XXXX Test: register status when no pending entry existed?? (bug)
  588. // XXXX Test: clean up hashtable after most-recent-consensus changes
  589. // in cdm_diff_ht_purge().
  590. // XXXX Test: cdm_entry_get_sha3_value cases.
  591. // XXXX Test: sha3 mismatch on validation
  592. // XXXX Test: initial loading of diffs from disk.
  593. // XXXX Test: non-cacheing cases of replyfn().
  594. END_OF_TESTCASES
  595. };