test_consdiffmgr.c 29 KB

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