test_consdiffmgr.c 23 KB

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