test_consdiffmgr.c 29 KB

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