test_consdiffmgr.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887
  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. tor_free(ent);
  116. });
  117. smartlist_free(fake_cpuworker_queue);
  118. fake_cpuworker_queue = NULL;
  119. }
  120. // ============================== Other helpers
  121. static consdiff_status_t
  122. lookup_diff_from(consensus_cache_entry_t **out,
  123. consensus_flavor_t flav,
  124. const char *str1)
  125. {
  126. uint8_t digest[DIGEST256_LEN];
  127. crypto_digest256((char*)digest, str1, strlen(str1), DIGEST_SHA3_256);
  128. return consdiffmgr_find_diff_from(out, flav,
  129. DIGEST_SHA3_256, digest, sizeof(digest));
  130. }
  131. static int
  132. lookup_apply_and_verify_diff(consensus_flavor_t flav,
  133. const char *str1,
  134. const char *str2)
  135. {
  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. char *diff_string = NULL;
  143. int r = uncompress_or_copy(&diff_string, &size, ent);
  144. consensus_cache_entry_decref(ent);
  145. if (diff_string == NULL || r < 0)
  146. return -1;
  147. char *applied = consensus_diff_apply(str1, diff_string);
  148. tor_free(diff_string);
  149. if (applied == NULL)
  150. return -1;
  151. int match = !strcmp(applied, str2);
  152. tor_free(applied);
  153. return match ? 0 : -1;
  154. }
  155. static void
  156. cdm_reload(void)
  157. {
  158. consdiffmgr_free_all();
  159. cdm_cache_get();
  160. consdiffmgr_rescan();
  161. }
  162. // ============================== Beginning of tests
  163. #if 0
  164. static int got_failure = 0;
  165. static void
  166. got_assertion_failure(void)
  167. {
  168. ++got_failure;
  169. }
  170. /* XXXX This test won't work, because there is currently no way to actually
  171. * XXXX capture a real assertion failure. */
  172. static void
  173. test_consdiffmgr_init_failure(void *arg)
  174. {
  175. (void)arg;
  176. // Capture assertions and bugs.
  177. /* As in ...test_setup, but do not create the datadir. The missing directory
  178. * will cause a failure. */
  179. char *ddir_fname = tor_strdup(get_fname_rnd("datadir_cdm"));
  180. tor_free(get_options_mutable()->DataDirectory);
  181. get_options_mutable()->DataDirectory = ddir_fname; // now owns the pointer.
  182. consdiff_cfg_t consdiff_cfg = { 7200, 300 };
  183. tor_set_failed_assertion_callback(got_assertion_failure);
  184. tor_capture_bugs_(1);
  185. consdiffmgr_configure(&consdiff_cfg); // This should fail.
  186. tt_int_op(got_failure, OP_EQ, 1);
  187. const smartlist_t *bugs = tor_get_captured_bug_log_();
  188. tt_int_op(smartlist_len(bugs), OP_EQ, 1);
  189. done:
  190. tor_end_capture_bugs_();
  191. }
  192. #endif
  193. static void
  194. test_consdiffmgr_sha3_helper(void *arg)
  195. {
  196. (void) arg;
  197. consensus_cache_t *cache = cdm_cache_get(); // violate abstraction barrier
  198. config_line_t *lines = NULL;
  199. char *mem_op_hex_tmp = NULL;
  200. config_line_prepend(&lines, "good-sha",
  201. "F00DF00DF00DF00DF00DF00DF00DF00D"
  202. "F00DF00DF00DF00DF00DF00DF00DF00D");
  203. config_line_prepend(&lines, "short-sha",
  204. "F00DF00DF00DF00DF00DF00DF00DF00D"
  205. "F00DF00DF00DF00DF00DF00DF00DF0");
  206. config_line_prepend(&lines, "long-sha",
  207. "F00DF00DF00DF00DF00DF00DF00DF00D"
  208. "F00DF00DF00DF00DF00DF00DF00DF00DF00D");
  209. config_line_prepend(&lines, "not-sha",
  210. "F00DF00DF00DF00DF00DF00DF00DF00D"
  211. "F00DF00DF00DF00DF00DF00DF00DXXXX");
  212. consensus_cache_entry_t *ent =
  213. consensus_cache_add(cache, lines, (const uint8_t *)"Hi there", 8);
  214. uint8_t buf[DIGEST256_LEN];
  215. tt_int_op(-1, OP_EQ, cdm_entry_get_sha3_value(buf, NULL, "good-sha"));
  216. tt_int_op(0, OP_EQ, cdm_entry_get_sha3_value(buf, ent, "good-sha"));
  217. test_memeq_hex(buf, "F00DF00DF00DF00DF00DF00DF00DF00D"
  218. "F00DF00DF00DF00DF00DF00DF00DF00D");
  219. tt_int_op(-1, OP_EQ, cdm_entry_get_sha3_value(buf, ent, "missing-sha"));
  220. tt_int_op(-2, OP_EQ, cdm_entry_get_sha3_value(buf, ent, "short-sha"));
  221. tt_int_op(-2, OP_EQ, cdm_entry_get_sha3_value(buf, ent, "long-sha"));
  222. tt_int_op(-2, OP_EQ, cdm_entry_get_sha3_value(buf, ent, "not-sha"));
  223. done:
  224. consensus_cache_entry_decref(ent);
  225. config_free_lines(lines);
  226. tor_free(mem_op_hex_tmp);
  227. }
  228. static void
  229. test_consdiffmgr_add(void *arg)
  230. {
  231. (void) arg;
  232. time_t now = approx_time();
  233. char *body = NULL;
  234. consensus_cache_entry_t *ent = NULL;
  235. networkstatus_t *ns_tmp = fake_ns_new(FLAV_NS, now);
  236. const char *dummy = "foo";
  237. int r = consdiffmgr_add_consensus(dummy, ns_tmp);
  238. tt_int_op(r, OP_EQ, 0);
  239. /* If we add it again, it won't work */
  240. setup_capture_of_logs(LOG_INFO);
  241. dummy = "bar";
  242. r = consdiffmgr_add_consensus(dummy, ns_tmp);
  243. tt_int_op(r, OP_EQ, -1);
  244. expect_single_log_msg_containing("We already have a copy of that "
  245. "consensus");
  246. mock_clean_saved_logs();
  247. /* But it will work fine if the flavor is different */
  248. dummy = "baz";
  249. ns_tmp->flavor = FLAV_MICRODESC;
  250. r = consdiffmgr_add_consensus(dummy, ns_tmp);
  251. tt_int_op(r, OP_EQ, 0);
  252. /* And it will work fine if the time is different */
  253. dummy = "quux";
  254. ns_tmp->flavor = FLAV_NS;
  255. ns_tmp->valid_after = now - 60;
  256. r = consdiffmgr_add_consensus(dummy, ns_tmp);
  257. tt_int_op(r, OP_EQ, 0);
  258. /* If we add one a long long time ago, it will fail. */
  259. dummy = "xyzzy";
  260. ns_tmp->valid_after = 86400 * 100; /* A few months into 1970 */
  261. r = consdiffmgr_add_consensus(dummy, ns_tmp);
  262. tt_int_op(r, OP_EQ, -1);
  263. expect_single_log_msg_containing("it's too old.");
  264. /* Try looking up a consensuses. */
  265. ent = cdm_cache_lookup_consensus(FLAV_NS, now-60);
  266. tt_assert(ent);
  267. consensus_cache_entry_incref(ent);
  268. size_t s;
  269. r = uncompress_or_copy(&body, &s, ent);
  270. tt_int_op(r, OP_EQ, 0);
  271. tt_int_op(s, OP_EQ, 4);
  272. tt_mem_op(body, OP_EQ, "quux", 4);
  273. /* Try looking up another entry, but fail */
  274. tt_assert(NULL == cdm_cache_lookup_consensus(FLAV_MICRODESC, now-60));
  275. tt_assert(NULL == cdm_cache_lookup_consensus(FLAV_NS, now-61));
  276. done:
  277. networkstatus_vote_free(ns_tmp);
  278. teardown_capture_of_logs();
  279. consensus_cache_entry_decref(ent);
  280. tor_free(body);
  281. }
  282. static void
  283. test_consdiffmgr_make_diffs(void *arg)
  284. {
  285. (void)arg;
  286. networkstatus_t *ns = NULL;
  287. char *ns_body = NULL, *md_ns_body = NULL, *md_ns_body_2 = NULL;
  288. char *applied = NULL, *diff_text = NULL;
  289. time_t now = approx_time();
  290. int r;
  291. consensus_cache_entry_t *diff = NULL;
  292. uint8_t md_ns_sha3[DIGEST256_LEN];
  293. consdiff_status_t diff_status;
  294. MOCK(cpuworker_queue_work, mock_cpuworker_queue_work);
  295. // Try rescan with no consensuses: shouldn't crash or queue work.
  296. consdiffmgr_rescan();
  297. tt_ptr_op(NULL, OP_EQ, fake_cpuworker_queue);
  298. // Make two consensuses, 1 hour sec ago.
  299. ns = fake_ns_new(FLAV_NS, now-3600);
  300. ns_body = fake_ns_body_new(FLAV_NS, now-3600);
  301. r = consdiffmgr_add_consensus(ns_body, ns);
  302. networkstatus_vote_free(ns);
  303. tor_free(ns_body);
  304. tt_int_op(r, OP_EQ, 0);
  305. ns = fake_ns_new(FLAV_MICRODESC, now-3600);
  306. md_ns_body = fake_ns_body_new(FLAV_MICRODESC, now-3600);
  307. r = consdiffmgr_add_consensus(md_ns_body, ns);
  308. crypto_digest256((char*)md_ns_sha3, md_ns_body, strlen(md_ns_body),
  309. DIGEST_SHA3_256);
  310. networkstatus_vote_free(ns);
  311. tt_int_op(r, OP_EQ, 0);
  312. // No diffs will be generated.
  313. consdiffmgr_rescan();
  314. tt_ptr_op(NULL, OP_EQ, fake_cpuworker_queue);
  315. // Add a MD consensus from 45 minutes ago. This should cause one diff
  316. // worth of work to get queued.
  317. ns = fake_ns_new(FLAV_MICRODESC, now-45*60);
  318. md_ns_body_2 = fake_ns_body_new(FLAV_MICRODESC, now-45*60);
  319. r = consdiffmgr_add_consensus(md_ns_body_2, ns);
  320. networkstatus_vote_free(ns);
  321. tt_int_op(r, OP_EQ, 0);
  322. consdiffmgr_rescan();
  323. tt_ptr_op(NULL, OP_NE, fake_cpuworker_queue);
  324. tt_int_op(1, OP_EQ, smartlist_len(fake_cpuworker_queue));
  325. diff_status = consdiffmgr_find_diff_from(&diff, FLAV_MICRODESC,
  326. DIGEST_SHA3_256,
  327. md_ns_sha3, DIGEST256_LEN);
  328. tt_int_op(CONSDIFF_IN_PROGRESS, OP_EQ, diff_status);
  329. // Now run that process and get the diff.
  330. r = mock_cpuworker_run_work();
  331. tt_int_op(r, OP_EQ, 0);
  332. mock_cpuworker_handle_replies();
  333. // At this point we should be able to get that diff.
  334. diff_status = consdiffmgr_find_diff_from(&diff, FLAV_MICRODESC,
  335. DIGEST_SHA3_256,
  336. md_ns_sha3, DIGEST256_LEN);
  337. tt_int_op(CONSDIFF_AVAILABLE, OP_EQ, diff_status);
  338. tt_assert(diff);
  339. /* Make sure applying the diff actually works */
  340. const uint8_t *diff_body;
  341. size_t diff_size;
  342. r = consensus_cache_entry_get_body(diff, &diff_body, &diff_size);
  343. tt_int_op(r, OP_EQ, 0);
  344. diff_text = tor_memdup_nulterm(diff_body, diff_size);
  345. applied = consensus_diff_apply(md_ns_body, diff_text);
  346. tt_assert(applied);
  347. tt_str_op(applied, OP_EQ, md_ns_body_2);
  348. /* Rescan again: no more work to do. */
  349. consdiffmgr_rescan();
  350. tt_ptr_op(NULL, OP_EQ, fake_cpuworker_queue);
  351. done:
  352. tor_free(md_ns_body);
  353. tor_free(md_ns_body_2);
  354. tor_free(diff_text);
  355. tor_free(applied);
  356. }
  357. static void
  358. test_consdiffmgr_diff_rules(void *arg)
  359. {
  360. (void)arg;
  361. #define N 6
  362. char *md_body[N], *ns_body[N];
  363. networkstatus_t *md_ns[N], *ns_ns[N];
  364. uint8_t md_ns_sha3[N][DIGEST256_LEN], ns_ns_sha3[N][DIGEST256_LEN];
  365. int i;
  366. MOCK(cpuworker_queue_work, mock_cpuworker_queue_work);
  367. /* Create a bunch of consensus things at 15-second intervals. */
  368. time_t start = approx_time() - 120;
  369. for (i = 0; i < N; ++i) {
  370. time_t when = start + i * 15;
  371. md_body[i] = fake_ns_body_new(FLAV_MICRODESC, when);
  372. ns_body[i] = fake_ns_body_new(FLAV_NS, when);
  373. md_ns[i] = fake_ns_new(FLAV_MICRODESC, when);
  374. ns_ns[i] = fake_ns_new(FLAV_NS, when);
  375. crypto_digest256((char *)md_ns_sha3[i], md_body[i], strlen(md_body[i]),
  376. DIGEST_SHA3_256);
  377. crypto_digest256((char *)ns_ns_sha3[i], ns_body[i], strlen(ns_body[i]),
  378. DIGEST_SHA3_256);
  379. }
  380. /* For the MD consensuses: add 4 of them, and make sure that
  381. * diffs are created to one consensus (the most recent) only. */
  382. tt_int_op(0, OP_EQ, consdiffmgr_add_consensus(md_body[1], md_ns[1]));
  383. tt_int_op(0, OP_EQ, consdiffmgr_add_consensus(md_body[2], md_ns[2]));
  384. tt_int_op(0, OP_EQ, consdiffmgr_add_consensus(md_body[3], md_ns[3]));
  385. tt_int_op(0, OP_EQ, consdiffmgr_add_consensus(md_body[4], md_ns[4]));
  386. consdiffmgr_rescan();
  387. tt_ptr_op(NULL, OP_NE, fake_cpuworker_queue);
  388. tt_int_op(3, OP_EQ, smartlist_len(fake_cpuworker_queue));
  389. tt_int_op(0, OP_EQ, mock_cpuworker_run_work());
  390. mock_cpuworker_handle_replies();
  391. tt_ptr_op(NULL, OP_EQ, fake_cpuworker_queue);
  392. /* For the NS consensuses: add 3, generate, and add one older one and
  393. * make sure that older one is the only one whose diff is generated */
  394. tt_int_op(0, OP_EQ, consdiffmgr_add_consensus(ns_body[0], ns_ns[0]));
  395. tt_int_op(0, OP_EQ, consdiffmgr_add_consensus(ns_body[1], ns_ns[1]));
  396. tt_int_op(0, OP_EQ, consdiffmgr_add_consensus(ns_body[5], ns_ns[5]));
  397. consdiffmgr_rescan();
  398. tt_ptr_op(NULL, OP_NE, fake_cpuworker_queue);
  399. tt_int_op(2, OP_EQ, smartlist_len(fake_cpuworker_queue));
  400. tt_int_op(0, OP_EQ, mock_cpuworker_run_work());
  401. mock_cpuworker_handle_replies();
  402. /* At this point, we should actually have working diffs! */
  403. tt_int_op(0, OP_EQ,
  404. lookup_apply_and_verify_diff(FLAV_NS, ns_body[0], ns_body[5]));
  405. tt_int_op(0, OP_EQ,
  406. lookup_apply_and_verify_diff(FLAV_NS, ns_body[1], ns_body[5]));
  407. tt_int_op(0, OP_EQ,
  408. lookup_apply_and_verify_diff(FLAV_MICRODESC, md_body[1], md_body[4]));
  409. tt_int_op(0, OP_EQ,
  410. lookup_apply_and_verify_diff(FLAV_MICRODESC, md_body[2], md_body[4]));
  411. tt_int_op(0, OP_EQ,
  412. lookup_apply_and_verify_diff(FLAV_MICRODESC, md_body[3], md_body[4]));
  413. /* Self-to-self diff won't be present */
  414. consensus_cache_entry_t *ent;
  415. tt_int_op(CONSDIFF_NOT_FOUND, OP_EQ,
  416. lookup_diff_from(&ent, FLAV_NS, ns_body[5]));
  417. /* No diff from 2 has been added yet */
  418. tt_int_op(CONSDIFF_NOT_FOUND, OP_EQ,
  419. lookup_diff_from(&ent, FLAV_NS, ns_body[2]));
  420. /* No diff arriving at old things. */
  421. tt_int_op(-1, OP_EQ,
  422. lookup_apply_and_verify_diff(FLAV_MICRODESC, md_body[1], md_body[2]));
  423. /* No backwards diff */
  424. tt_int_op(-1, OP_EQ,
  425. lookup_apply_and_verify_diff(FLAV_MICRODESC, md_body[4], md_body[3]));
  426. /* Now, an update: add number 2 and make sure it's the only one whose diff
  427. * is regenerated. */
  428. tt_int_op(0, OP_EQ, consdiffmgr_add_consensus(ns_body[2], ns_ns[2]));
  429. consdiffmgr_rescan();
  430. tt_ptr_op(NULL, OP_NE, fake_cpuworker_queue);
  431. tt_int_op(1, OP_EQ, smartlist_len(fake_cpuworker_queue));
  432. tt_int_op(0, OP_EQ, mock_cpuworker_run_work());
  433. mock_cpuworker_handle_replies();
  434. tt_int_op(0, OP_EQ,
  435. lookup_apply_and_verify_diff(FLAV_NS, ns_body[2], ns_body[5]));
  436. /* Finally: reload, and make sure that the information is still indexed */
  437. cdm_reload();
  438. tt_int_op(0, OP_EQ,
  439. lookup_apply_and_verify_diff(FLAV_NS, ns_body[0], ns_body[5]));
  440. tt_int_op(0, OP_EQ,
  441. lookup_apply_and_verify_diff(FLAV_NS, ns_body[2], ns_body[5]));
  442. tt_int_op(0, OP_EQ,
  443. lookup_apply_and_verify_diff(FLAV_NS, ns_body[1], ns_body[5]));
  444. tt_int_op(0, OP_EQ,
  445. lookup_apply_and_verify_diff(FLAV_MICRODESC, md_body[1], md_body[4]));
  446. tt_int_op(0, OP_EQ,
  447. lookup_apply_and_verify_diff(FLAV_MICRODESC, md_body[2], md_body[4]));
  448. tt_int_op(0, OP_EQ,
  449. lookup_apply_and_verify_diff(FLAV_MICRODESC, md_body[3], md_body[4]));
  450. done:
  451. for (i = 0; i < N; ++i) {
  452. tor_free(md_body[i]);
  453. tor_free(ns_body[i]);
  454. networkstatus_vote_free(md_ns[i]);
  455. networkstatus_vote_free(ns_ns[i]);
  456. }
  457. UNMOCK(cpuworker_queue_work);
  458. #undef N
  459. }
  460. static void
  461. test_consdiffmgr_diff_failure(void *arg)
  462. {
  463. (void)arg;
  464. MOCK(cpuworker_queue_work, mock_cpuworker_queue_work);
  465. /* We're going to make sure that if we have a bogus request where
  466. * we can't actually compute a diff, the world must not end. */
  467. networkstatus_t *ns1 = NULL;
  468. networkstatus_t *ns2 = NULL;
  469. int r;
  470. ns1 = fake_ns_new(FLAV_NS, approx_time()-100);
  471. ns2 = fake_ns_new(FLAV_NS, approx_time()-50);
  472. r = consdiffmgr_add_consensus("foo bar baz\n", ns1);
  473. tt_int_op(r, OP_EQ, 0);
  474. // We refuse to compute a diff to or from a line holding only a single dot.
  475. // We can add it here, though.
  476. r = consdiffmgr_add_consensus("foo bar baz\n.\n.\n", ns2);
  477. tt_int_op(r, OP_EQ, 0);
  478. consdiffmgr_rescan();
  479. tt_ptr_op(NULL, OP_NE, fake_cpuworker_queue);
  480. setup_capture_of_logs(LOG_WARN);
  481. tt_int_op(1, OP_EQ, smartlist_len(fake_cpuworker_queue));
  482. tt_int_op(0, OP_EQ, mock_cpuworker_run_work());
  483. expect_single_log_msg_containing("one of the lines to be added is \".\".");
  484. mock_clean_saved_logs();
  485. mock_cpuworker_handle_replies();
  486. expect_single_log_msg_containing("Worker was unable to compute consensus "
  487. "diff from ");
  488. /* Make sure the diff is not present */
  489. consensus_cache_entry_t *ent;
  490. tt_int_op(CONSDIFF_NOT_FOUND, OP_EQ,
  491. lookup_diff_from(&ent, FLAV_NS, "foo bar baz\n"));
  492. done:
  493. teardown_capture_of_logs();
  494. UNMOCK(cpuworker_queue_work);
  495. networkstatus_vote_free(ns1);
  496. networkstatus_vote_free(ns2);
  497. }
  498. static void
  499. test_consdiffmgr_diff_pending(void *arg)
  500. {
  501. #define N 3
  502. (void)arg;
  503. char *md_body[N];
  504. networkstatus_t *md_ns[N];
  505. time_t start = approx_time() - 120;
  506. int i;
  507. for (i = 0; i < N; ++i) {
  508. time_t when = start + i * 30;
  509. md_body[i] = fake_ns_body_new(FLAV_MICRODESC, when);
  510. md_ns[i] = fake_ns_new(FLAV_MICRODESC, when);
  511. }
  512. MOCK(cpuworker_queue_work, mock_cpuworker_queue_work);
  513. tt_int_op(0, OP_EQ, consdiffmgr_add_consensus(md_body[1], md_ns[1]));
  514. tt_int_op(0, OP_EQ, consdiffmgr_add_consensus(md_body[2], md_ns[2]));
  515. /* Make a diff */
  516. consdiffmgr_rescan();
  517. tt_int_op(1, OP_EQ, smartlist_len(fake_cpuworker_queue));
  518. /* Look it up. Is it pending? */
  519. consensus_cache_entry_t *ent = NULL;
  520. consdiff_status_t diff_status;
  521. diff_status = lookup_diff_from(&ent, FLAV_MICRODESC, md_body[1]);
  522. tt_int_op(CONSDIFF_IN_PROGRESS, OP_EQ, diff_status);
  523. tt_ptr_op(ent, OP_EQ, NULL);
  524. /* Add another old consensus. only one new diff should launch! */
  525. tt_int_op(0, OP_EQ, consdiffmgr_add_consensus(md_body[0], md_ns[0]));
  526. consdiffmgr_rescan();
  527. tt_int_op(2, OP_EQ, smartlist_len(fake_cpuworker_queue));
  528. tt_int_op(0, OP_EQ, mock_cpuworker_run_work());
  529. mock_cpuworker_handle_replies();
  530. tt_int_op(0, OP_EQ,
  531. lookup_apply_and_verify_diff(FLAV_MICRODESC, md_body[0], md_body[2]));
  532. tt_int_op(0, OP_EQ,
  533. lookup_apply_and_verify_diff(FLAV_MICRODESC, md_body[1], md_body[2]));
  534. done:
  535. UNMOCK(cpuworker_queue_work);
  536. for (i = 0; i < N; ++i) {
  537. tor_free(md_body[i]);
  538. networkstatus_vote_free(md_ns[i]);
  539. }
  540. #undef N
  541. }
  542. static void
  543. test_consdiffmgr_cleanup_old(void *arg)
  544. {
  545. (void)arg;
  546. config_line_t *labels = NULL;
  547. consensus_cache_entry_t *ent = NULL;
  548. consensus_cache_t *cache = cdm_cache_get(); // violate abstraction barrier
  549. /* This item will be will be cleanable because it has a valid-after
  550. * time far in the past. */
  551. config_line_prepend(&labels, "document-type", "confribble-blarg");
  552. config_line_prepend(&labels, "consensus-valid-after",
  553. "1980-10-10T10:10:10");
  554. ent = consensus_cache_add(cache, labels, (const uint8_t*)"Foo", 3);
  555. tt_assert(ent);
  556. consensus_cache_entry_decref(ent);
  557. setup_capture_of_logs(LOG_DEBUG);
  558. tt_int_op(1, OP_EQ, consdiffmgr_cleanup());
  559. expect_log_msg_containing("Deleting entry because its consensus-valid-"
  560. "after value (1980-10-10T10:10:10) was too old");
  561. done:
  562. teardown_capture_of_logs();
  563. config_free_lines(labels);
  564. }
  565. static void
  566. test_consdiffmgr_cleanup_bad_valid_after(void *arg)
  567. {
  568. /* This will seem cleanable, but isn't, because its valid-after time is
  569. * misformed. */
  570. (void)arg;
  571. config_line_t *labels = NULL;
  572. consensus_cache_entry_t *ent = NULL;
  573. consensus_cache_t *cache = cdm_cache_get(); // violate abstraction barrier
  574. config_line_prepend(&labels, "document-type", "consensus");
  575. config_line_prepend(&labels, "consensus-valid-after",
  576. "whan that aprille with his shoures soote"); // (~1385?)
  577. ent = consensus_cache_add(cache, labels, (const uint8_t*)"Foo", 3);
  578. tt_assert(ent);
  579. consensus_cache_entry_decref(ent);
  580. setup_capture_of_logs(LOG_DEBUG);
  581. tt_int_op(0, OP_EQ, consdiffmgr_cleanup());
  582. expect_log_msg_containing("Ignoring entry because its consensus-valid-"
  583. "after value (\"whan that aprille with his "
  584. "shoures soote\") was unparseable");
  585. done:
  586. teardown_capture_of_logs();
  587. config_free_lines(labels);
  588. }
  589. static void
  590. test_consdiffmgr_cleanup_no_valid_after(void *arg)
  591. {
  592. (void)arg;
  593. config_line_t *labels = NULL;
  594. consensus_cache_entry_t *ent = NULL;
  595. consensus_cache_t *cache = cdm_cache_get(); // violate abstraction barrier
  596. /* This item will be will be uncleanable because it has no recognized
  597. * valid-after. */
  598. config_line_prepend(&labels, "document-type", "consensus");
  599. config_line_prepend(&labels, "confrooble-voolid-oofter",
  600. "2010-10-10T09:08:07");
  601. ent = consensus_cache_add(cache, labels, (const uint8_t*)"Foo", 3);
  602. tt_assert(ent);
  603. consensus_cache_entry_decref(ent);
  604. setup_capture_of_logs(LOG_DEBUG);
  605. tt_int_op(0, OP_EQ, consdiffmgr_cleanup());
  606. expect_log_msg_containing("Ignoring entry because it had no consensus-"
  607. "valid-after label");
  608. done:
  609. teardown_capture_of_logs();
  610. config_free_lines(labels);
  611. }
  612. static void
  613. test_consdiffmgr_cleanup_old_diffs(void *arg)
  614. {
  615. (void)arg;
  616. #define N 4
  617. char *md_body[N];
  618. networkstatus_t *md_ns[N];
  619. uint8_t md_ns_sha3[N][DIGEST256_LEN];
  620. int i;
  621. consensus_cache_entry_t *hold_ent = NULL, *ent;
  622. /* Make sure that the cleanup function removes diffs to the not-most-recent
  623. * consensus. */
  624. MOCK(cpuworker_queue_work, mock_cpuworker_queue_work);
  625. /* Create a bunch of consensus things at 15-second intervals. */
  626. time_t start = approx_time() - 120;
  627. for (i = 0; i < N; ++i) {
  628. time_t when = start + i * 15;
  629. md_body[i] = fake_ns_body_new(FLAV_MICRODESC, when);
  630. md_ns[i] = fake_ns_new(FLAV_MICRODESC, when);
  631. crypto_digest256((char *)md_ns_sha3[i], md_body[i], strlen(md_body[i]),
  632. DIGEST_SHA3_256);
  633. }
  634. /* add the first 3. */
  635. tt_int_op(0, OP_EQ, consdiffmgr_add_consensus(md_body[0], md_ns[0]));
  636. tt_int_op(0, OP_EQ, consdiffmgr_add_consensus(md_body[1], md_ns[1]));
  637. tt_int_op(0, OP_EQ, consdiffmgr_add_consensus(md_body[2], md_ns[2]));
  638. /* Make diffs. */
  639. consdiffmgr_rescan();
  640. tt_ptr_op(NULL, OP_NE, fake_cpuworker_queue);
  641. tt_int_op(2, OP_EQ, smartlist_len(fake_cpuworker_queue));
  642. tt_int_op(0, OP_EQ, mock_cpuworker_run_work());
  643. mock_cpuworker_handle_replies();
  644. tt_ptr_op(NULL, OP_EQ, fake_cpuworker_queue);
  645. /* Nothing is deletable now */
  646. tt_int_op(0, OP_EQ, consdiffmgr_cleanup());
  647. tt_int_op(0, OP_EQ,
  648. lookup_apply_and_verify_diff(FLAV_MICRODESC, md_body[0], md_body[2]));
  649. tt_int_op(0, OP_EQ,
  650. lookup_apply_and_verify_diff(FLAV_MICRODESC, md_body[1], md_body[2]));
  651. tt_int_op(CONSDIFF_AVAILABLE, OP_EQ,
  652. lookup_diff_from(&hold_ent, FLAV_MICRODESC, md_body[1]));
  653. consensus_cache_entry_incref(hold_ent); // incref, so it is preserved.
  654. /* Now add an even-more-recent consensus; this should make all previous
  655. * diffs deletable */
  656. tt_int_op(0, OP_EQ, consdiffmgr_add_consensus(md_body[3], md_ns[3]));
  657. tt_int_op(2, OP_EQ, consdiffmgr_cleanup());
  658. tt_int_op(CONSDIFF_NOT_FOUND, OP_EQ,
  659. lookup_diff_from(&ent, FLAV_MICRODESC, md_body[0]));
  660. /* This one is marked deletable but still in the hashtable */
  661. tt_int_op(CONSDIFF_AVAILABLE, OP_EQ,
  662. lookup_diff_from(&ent, FLAV_MICRODESC, md_body[1]));
  663. tt_int_op(CONSDIFF_NOT_FOUND, OP_EQ,
  664. lookup_diff_from(&ent, FLAV_MICRODESC, md_body[2]));
  665. /* Everything should be valid at this point */
  666. tt_int_op(0, OP_EQ, consdiffmgr_validate());
  667. /* And if we recan NOW, we'll purge the hashtable of the entries,
  668. * and launch attempts to generate new ones */
  669. consdiffmgr_rescan();
  670. tt_int_op(CONSDIFF_IN_PROGRESS, OP_EQ,
  671. lookup_diff_from(&ent, FLAV_MICRODESC, md_body[0]));
  672. tt_int_op(CONSDIFF_IN_PROGRESS, OP_EQ,
  673. lookup_diff_from(&ent, FLAV_MICRODESC, md_body[1]));
  674. tt_int_op(CONSDIFF_IN_PROGRESS, OP_EQ,
  675. lookup_diff_from(&ent, FLAV_MICRODESC, md_body[2]));
  676. /* We're still holding on to this, though, so we can still map it! */
  677. const uint8_t *t1 = NULL;
  678. size_t s;
  679. int r = consensus_cache_entry_get_body(hold_ent, &t1, &s);
  680. tt_int_op(r, OP_EQ, 0);
  681. tt_assert(t1);
  682. done:
  683. for (i = 0; i < N; ++i) {
  684. tor_free(md_body[i]);
  685. networkstatus_vote_free(md_ns[i]);
  686. }
  687. consensus_cache_entry_decref(hold_ent);
  688. UNMOCK(cpuworker_queue_work);
  689. #undef N
  690. }
  691. static void
  692. test_consdiffmgr_validate(void *arg)
  693. {
  694. (void)arg;
  695. config_line_t *lines = NULL;
  696. consensus_cache_entry_t *ent = NULL;
  697. consensus_cache_t *cache = cdm_cache_get(); // violate abstraction barrier
  698. smartlist_t *vals = smartlist_new();
  699. /* Put these: objects in the cache: one with a good sha3, one with bad sha3,
  700. * one with a wrong sha3, and one with no sha3. */
  701. config_line_prepend(&lines, "id", "wrong sha3");
  702. config_line_prepend(&lines, "sha3-digest",
  703. "F00DF00DF00DF00DF00DF00DF00DF00D"
  704. "F00DF00DF00DF00DF00DF00DF00DF00D");
  705. ent = consensus_cache_add(cache, lines, (const uint8_t *)"Hi there", 8);
  706. consensus_cache_entry_decref(ent);
  707. config_free_lines(lines);
  708. lines = NULL;
  709. config_line_prepend(&lines, "id", "bad sha3");
  710. config_line_prepend(&lines, "sha3-digest",
  711. "now is the winter of our dicotheque");
  712. ent = consensus_cache_add(cache, lines, (const uint8_t *)"Hi there", 8);
  713. consensus_cache_entry_decref(ent);
  714. config_free_lines(lines);
  715. lines = NULL;
  716. config_line_prepend(&lines, "id", "no sha3");
  717. ent = consensus_cache_add(cache, lines, (const uint8_t *)"Hi there", 8);
  718. consensus_cache_entry_decref(ent);
  719. config_free_lines(lines);
  720. lines = NULL;
  721. config_line_prepend(&lines, "id", "good sha3");
  722. config_line_prepend(&lines, "sha3-digest",
  723. "8d8b1998616cd6b4c4055da8d38728dc"
  724. "93c758d4131a53c7d81aa6337dee1c05");
  725. ent = consensus_cache_add(cache, lines, (const uint8_t *)"Hi there", 8);
  726. consensus_cache_entry_decref(ent);
  727. config_free_lines(lines);
  728. lines = NULL;
  729. cdm_reload();
  730. cache = cdm_cache_get();
  731. tt_int_op(1, OP_EQ, consdiffmgr_validate());
  732. consensus_cache_find_all(vals, cache, "id", "good sha3");
  733. tt_int_op(smartlist_len(vals), OP_EQ, 1);
  734. smartlist_clear(vals);
  735. consensus_cache_find_all(vals, cache, "id", "no sha3");
  736. tt_int_op(smartlist_len(vals), OP_EQ, 1);
  737. smartlist_clear(vals);
  738. consensus_cache_find_all(vals, cache, "id", "wrong sha3");
  739. tt_int_op(smartlist_len(vals), OP_EQ, 0);
  740. consensus_cache_find_all(vals, cache, "id", "bad sha3");
  741. tt_int_op(smartlist_len(vals), OP_EQ, 0);
  742. done:
  743. smartlist_free(vals);
  744. }
  745. #define TEST(name) \
  746. { #name, test_consdiffmgr_ ## name , TT_FORK, &setup_diffmgr, NULL }
  747. struct testcase_t consdiffmgr_tests[] = {
  748. #if 0
  749. { "init_failure", test_consdiffmgr_init_failure, TT_FORK, NULL, NULL },
  750. #endif
  751. TEST(sha3_helper),
  752. TEST(add),
  753. TEST(make_diffs),
  754. TEST(diff_rules),
  755. TEST(diff_failure),
  756. TEST(diff_pending),
  757. TEST(cleanup_old),
  758. TEST(cleanup_bad_valid_after),
  759. TEST(cleanup_no_valid_after),
  760. TEST(cleanup_old_diffs),
  761. TEST(validate),
  762. // XXXX Test: non-cacheing cases of replyfn().
  763. END_OF_TESTCASES
  764. };