test_consdiffmgr.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884
  1. /* Copyright (c) 2017, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. #define CONSDIFFMGR_PRIVATE
  4. #include "or.h"
  5. #include "config.h"
  6. #include "conscache.h"
  7. #include "consdiff.h"
  8. #include "consdiffmgr.h"
  9. #include "cpuworker.h"
  10. #include "networkstatus.h"
  11. #include "workqueue.h"
  12. #include "test.h"
  13. #include "log_test_helpers.h"
  14. // ============================== Setup/teardown the consdiffmgr
  15. // These functions get run before/after each test in this module
  16. static void *
  17. consdiffmgr_test_setup(const struct testcase_t *arg)
  18. {
  19. (void)arg;
  20. char *ddir_fname = tor_strdup(get_fname_rnd("datadir_cdm"));
  21. tor_free(get_options_mutable()->DataDirectory);
  22. get_options_mutable()->DataDirectory = ddir_fname; // now owns the pointer.
  23. check_private_dir(ddir_fname, CPD_CREATE, NULL);
  24. consdiff_cfg_t consdiff_cfg = { 7200, 300 };
  25. consdiffmgr_configure(&consdiff_cfg);
  26. return (void *)1; // must return something non-null.
  27. }
  28. static int
  29. consdiffmgr_test_teardown(const struct testcase_t *arg, void *ignore)
  30. {
  31. (void)arg;
  32. (void)ignore;
  33. consdiffmgr_free_all();
  34. return 1;
  35. }
  36. static struct testcase_setup_t setup_diffmgr = {
  37. consdiffmgr_test_setup,
  38. consdiffmgr_test_teardown
  39. };
  40. // ============================== NS faking functions
  41. // These functions are for making quick fake consensus objects and
  42. // strings that are just good enough for consdiff and consdiffmgr.
  43. static networkstatus_t *
  44. fake_ns_new(consensus_flavor_t flav, time_t valid_after)
  45. {
  46. networkstatus_t *ns = tor_malloc_zero(sizeof(networkstatus_t));
  47. ns->type = NS_TYPE_CONSENSUS;
  48. ns->flavor = flav;
  49. ns->valid_after = valid_after;
  50. return ns;
  51. }
  52. static char *
  53. fake_ns_body_new(consensus_flavor_t flav, time_t valid_after)
  54. {
  55. const char *flavor_string = flav == FLAV_NS ? "" : " microdesc";
  56. char valid_after_string[ISO_TIME_LEN+1];
  57. format_iso_time(valid_after_string, valid_after);
  58. char *random_stuff = crypto_random_hostname(3, 25, "junk ", "");
  59. char *consensus;
  60. tor_asprintf(&consensus,
  61. "network-status-version 3%s\n"
  62. "vote-status consensus\n"
  63. "valid-after %s\n"
  64. "r name ccccccccccccccccc etc\nsample\n"
  65. "r name eeeeeeeeeeeeeeeee etc\nbar\n"
  66. "%s\n",
  67. flavor_string,
  68. valid_after_string,
  69. random_stuff);
  70. tor_free(random_stuff);
  71. return consensus;
  72. }
  73. // ============================== Cpuworker mocking code
  74. // These mocking functions and types capture the cpuworker calls
  75. // so we can inspect them and run them in the main thread.
  76. static smartlist_t *fake_cpuworker_queue = NULL;
  77. typedef struct fake_work_queue_ent_t {
  78. enum workqueue_reply_t (*fn)(void *, void *);
  79. void (*reply_fn)(void *);
  80. void *arg;
  81. } fake_work_queue_ent_t;
  82. static struct workqueue_entry_s *
  83. mock_cpuworker_queue_work(enum workqueue_reply_t (*fn)(void *, void *),
  84. void (*reply_fn)(void *),
  85. void *arg)
  86. {
  87. if (! fake_cpuworker_queue)
  88. fake_cpuworker_queue = smartlist_new();
  89. fake_work_queue_ent_t *ent = tor_malloc_zero(sizeof(*ent));
  90. ent->fn = fn;
  91. ent->reply_fn = reply_fn;
  92. ent->arg = arg;
  93. smartlist_add(fake_cpuworker_queue, ent);
  94. return (struct workqueue_entry_s *)ent;
  95. }
  96. static int
  97. mock_cpuworker_run_work(void)
  98. {
  99. if (! fake_cpuworker_queue)
  100. return 0;
  101. SMARTLIST_FOREACH(fake_cpuworker_queue, fake_work_queue_ent_t *, ent, {
  102. enum workqueue_reply_t r = ent->fn(NULL, ent->arg);
  103. if (r != WQ_RPL_REPLY)
  104. return -1;
  105. });
  106. return 0;
  107. }
  108. static void
  109. mock_cpuworker_handle_replies(void)
  110. {
  111. if (! fake_cpuworker_queue)
  112. return;
  113. SMARTLIST_FOREACH(fake_cpuworker_queue, fake_work_queue_ent_t *, ent, {
  114. ent->reply_fn(ent->arg);
  115. });
  116. smartlist_free(fake_cpuworker_queue);
  117. fake_cpuworker_queue = NULL;
  118. }
  119. // ============================== Other helpers
  120. static consdiff_status_t
  121. lookup_diff_from(consensus_cache_entry_t **out,
  122. consensus_flavor_t flav,
  123. const char *str1)
  124. {
  125. uint8_t digest[DIGEST256_LEN];
  126. crypto_digest256((char*)digest, str1, strlen(str1), DIGEST_SHA3_256);
  127. return consdiffmgr_find_diff_from(out, flav,
  128. DIGEST_SHA3_256, digest, sizeof(digest));
  129. }
  130. static int
  131. lookup_apply_and_verify_diff(consensus_flavor_t flav,
  132. const char *str1,
  133. const char *str2)
  134. {
  135. char *diff_string = NULL;
  136. consensus_cache_entry_t *ent = NULL;
  137. consdiff_status_t status = lookup_diff_from(&ent, flav, str1);
  138. if (ent == NULL || status != CONSDIFF_AVAILABLE)
  139. return -1;
  140. consensus_cache_entry_incref(ent);
  141. size_t size;
  142. const uint8_t *body;
  143. int r = consensus_cache_entry_get_body(ent, &body, &size);
  144. if (r == 0)
  145. diff_string = tor_memdup_nulterm(body, size);
  146. consensus_cache_entry_decref(ent);
  147. if (diff_string == NULL)
  148. return -1;
  149. char *applied = consensus_diff_apply(str1, diff_string);
  150. tor_free(diff_string);
  151. if (applied == NULL)
  152. return -1;
  153. int match = !strcmp(applied, str2);
  154. tor_free(applied);
  155. return match ? 0 : -1;
  156. }
  157. static void
  158. cdm_reload(void)
  159. {
  160. consdiffmgr_free_all();
  161. cdm_cache_get();
  162. consdiffmgr_rescan();
  163. }
  164. // ============================== Beginning of tests
  165. #if 0
  166. static int got_failure = 0;
  167. static void
  168. got_assertion_failure(void)
  169. {
  170. ++got_failure;
  171. }
  172. /* XXXX This test won't work, because there is currently no way to actually
  173. * XXXX capture a real assertion failure. */
  174. static void
  175. test_consdiffmgr_init_failure(void *arg)
  176. {
  177. (void)arg;
  178. // Capture assertions and bugs.
  179. /* As in ...test_setup, but do not create the datadir. The missing directory
  180. * will cause a failure. */
  181. char *ddir_fname = tor_strdup(get_fname_rnd("datadir_cdm"));
  182. tor_free(get_options_mutable()->DataDirectory);
  183. get_options_mutable()->DataDirectory = ddir_fname; // now owns the pointer.
  184. consdiff_cfg_t consdiff_cfg = { 7200, 300 };
  185. tor_set_failed_assertion_callback(got_assertion_failure);
  186. tor_capture_bugs_(1);
  187. consdiffmgr_configure(&consdiff_cfg); // This should fail.
  188. tt_int_op(got_failure, OP_EQ, 1);
  189. const smartlist_t *bugs = tor_get_captured_bug_log_();
  190. tt_int_op(smartlist_len(bugs), OP_EQ, 1);
  191. done:
  192. tor_end_capture_bugs_();
  193. }
  194. #endif
  195. static void
  196. test_consdiffmgr_sha3_helper(void *arg)
  197. {
  198. (void) arg;
  199. consensus_cache_t *cache = cdm_cache_get(); // violate abstraction barrier
  200. config_line_t *lines = NULL;
  201. char *mem_op_hex_tmp = NULL;
  202. config_line_prepend(&lines, "good-sha",
  203. "F00DF00DF00DF00DF00DF00DF00DF00D"
  204. "F00DF00DF00DF00DF00DF00DF00DF00D");
  205. config_line_prepend(&lines, "short-sha",
  206. "F00DF00DF00DF00DF00DF00DF00DF00D"
  207. "F00DF00DF00DF00DF00DF00DF00DF0");
  208. config_line_prepend(&lines, "long-sha",
  209. "F00DF00DF00DF00DF00DF00DF00DF00D"
  210. "F00DF00DF00DF00DF00DF00DF00DF00DF00D");
  211. config_line_prepend(&lines, "not-sha",
  212. "F00DF00DF00DF00DF00DF00DF00DF00D"
  213. "F00DF00DF00DF00DF00DF00DF00DXXXX");
  214. consensus_cache_entry_t *ent =
  215. consensus_cache_add(cache, lines, (const uint8_t *)"Hi there", 8);
  216. uint8_t buf[DIGEST256_LEN];
  217. tt_int_op(-1, OP_EQ, cdm_entry_get_sha3_value(buf, NULL, "good-sha"));
  218. tt_int_op(0, OP_EQ, cdm_entry_get_sha3_value(buf, ent, "good-sha"));
  219. test_memeq_hex(buf, "F00DF00DF00DF00DF00DF00DF00DF00D"
  220. "F00DF00DF00DF00DF00DF00DF00DF00D");
  221. tt_int_op(-1, OP_EQ, cdm_entry_get_sha3_value(buf, ent, "missing-sha"));
  222. tt_int_op(-2, OP_EQ, cdm_entry_get_sha3_value(buf, ent, "short-sha"));
  223. tt_int_op(-2, OP_EQ, cdm_entry_get_sha3_value(buf, ent, "long-sha"));
  224. tt_int_op(-2, OP_EQ, cdm_entry_get_sha3_value(buf, ent, "not-sha"));
  225. done:
  226. consensus_cache_entry_decref(ent);
  227. config_free_lines(lines);
  228. tor_free(mem_op_hex_tmp);
  229. }
  230. static void
  231. test_consdiffmgr_add(void *arg)
  232. {
  233. (void) arg;
  234. time_t now = approx_time();
  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. const uint8_t *body;
  271. r = consensus_cache_entry_get_body(ent, &body, &s);
  272. tt_int_op(r, OP_EQ, 0);
  273. tt_int_op(s, OP_EQ, 4);
  274. tt_mem_op(body, OP_EQ, "quux", 4);
  275. /* Try looking up another entry, but fail */
  276. tt_assert(NULL == cdm_cache_lookup_consensus(FLAV_MICRODESC, now-60));
  277. tt_assert(NULL == cdm_cache_lookup_consensus(FLAV_NS, now-61));
  278. done:
  279. networkstatus_vote_free(ns_tmp);
  280. teardown_capture_of_logs();
  281. consensus_cache_entry_decref(ent);
  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. tt_int_op(CONSDIFF_IN_PROGRESS, OP_EQ, diff_status);
  330. // Now run that process and get the diff.
  331. r = mock_cpuworker_run_work();
  332. tt_int_op(r, OP_EQ, 0);
  333. mock_cpuworker_handle_replies();
  334. // At this point we should be able to get that diff.
  335. diff_status = consdiffmgr_find_diff_from(&diff, FLAV_MICRODESC,
  336. DIGEST_SHA3_256,
  337. md_ns_sha3, DIGEST256_LEN);
  338. tt_int_op(CONSDIFF_AVAILABLE, OP_EQ, diff_status);
  339. tt_assert(diff);
  340. /* Make sure applying the diff actually works */
  341. const uint8_t *diff_body;
  342. size_t diff_size;
  343. r = consensus_cache_entry_get_body(diff, &diff_body, &diff_size);
  344. tt_int_op(r, OP_EQ, 0);
  345. diff_text = tor_memdup_nulterm(diff_body, diff_size);
  346. applied = consensus_diff_apply(md_ns_body, diff_text);
  347. tt_assert(applied);
  348. tt_str_op(applied, OP_EQ, md_ns_body_2);
  349. /* Rescan again: no more work to do. */
  350. consdiffmgr_rescan();
  351. tt_ptr_op(NULL, OP_EQ, fake_cpuworker_queue);
  352. done:
  353. tor_free(md_ns_body);
  354. tor_free(md_ns_body_2);
  355. tor_free(diff_text);
  356. tor_free(applied);
  357. }
  358. static void
  359. test_consdiffmgr_diff_rules(void *arg)
  360. {
  361. (void)arg;
  362. #define N 6
  363. char *md_body[N], *ns_body[N];
  364. networkstatus_t *md_ns[N], *ns_ns[N];
  365. uint8_t md_ns_sha3[N][DIGEST256_LEN], ns_ns_sha3[N][DIGEST256_LEN];
  366. int i;
  367. MOCK(cpuworker_queue_work, mock_cpuworker_queue_work);
  368. /* Create a bunch of consensus things at 15-second intervals. */
  369. time_t start = approx_time() - 120;
  370. for (i = 0; i < N; ++i) {
  371. time_t when = start + i * 15;
  372. md_body[i] = fake_ns_body_new(FLAV_MICRODESC, when);
  373. ns_body[i] = fake_ns_body_new(FLAV_NS, when);
  374. md_ns[i] = fake_ns_new(FLAV_MICRODESC, when);
  375. ns_ns[i] = fake_ns_new(FLAV_NS, when);
  376. crypto_digest256((char *)md_ns_sha3[i], md_body[i], strlen(md_body[i]),
  377. DIGEST_SHA3_256);
  378. crypto_digest256((char *)ns_ns_sha3[i], ns_body[i], strlen(ns_body[i]),
  379. DIGEST_SHA3_256);
  380. }
  381. /* For the MD consensuses: add 4 of them, and make sure that
  382. * diffs are created to one consensus (the most recent) only. */
  383. tt_int_op(0, OP_EQ, consdiffmgr_add_consensus(md_body[1], md_ns[1]));
  384. tt_int_op(0, OP_EQ, consdiffmgr_add_consensus(md_body[2], md_ns[2]));
  385. tt_int_op(0, OP_EQ, consdiffmgr_add_consensus(md_body[3], md_ns[3]));
  386. tt_int_op(0, OP_EQ, consdiffmgr_add_consensus(md_body[4], md_ns[4]));
  387. consdiffmgr_rescan();
  388. tt_ptr_op(NULL, OP_NE, fake_cpuworker_queue);
  389. tt_int_op(3, OP_EQ, smartlist_len(fake_cpuworker_queue));
  390. tt_int_op(0, OP_EQ, mock_cpuworker_run_work());
  391. mock_cpuworker_handle_replies();
  392. tt_ptr_op(NULL, OP_EQ, fake_cpuworker_queue);
  393. /* For the NS consensuses: add 3, generate, and add one older one and
  394. * make sure that older one is the only one whose diff is generated */
  395. tt_int_op(0, OP_EQ, consdiffmgr_add_consensus(ns_body[0], ns_ns[0]));
  396. tt_int_op(0, OP_EQ, consdiffmgr_add_consensus(ns_body[1], ns_ns[1]));
  397. tt_int_op(0, OP_EQ, consdiffmgr_add_consensus(ns_body[5], ns_ns[5]));
  398. consdiffmgr_rescan();
  399. tt_ptr_op(NULL, OP_NE, fake_cpuworker_queue);
  400. tt_int_op(2, OP_EQ, smartlist_len(fake_cpuworker_queue));
  401. tt_int_op(0, OP_EQ, mock_cpuworker_run_work());
  402. mock_cpuworker_handle_replies();
  403. /* At this point, we should actually have working diffs! */
  404. tt_int_op(0, OP_EQ,
  405. lookup_apply_and_verify_diff(FLAV_NS, ns_body[0], ns_body[5]));
  406. tt_int_op(0, OP_EQ,
  407. lookup_apply_and_verify_diff(FLAV_NS, ns_body[1], ns_body[5]));
  408. tt_int_op(0, OP_EQ,
  409. lookup_apply_and_verify_diff(FLAV_MICRODESC, md_body[1], md_body[4]));
  410. tt_int_op(0, OP_EQ,
  411. lookup_apply_and_verify_diff(FLAV_MICRODESC, md_body[2], md_body[4]));
  412. tt_int_op(0, OP_EQ,
  413. lookup_apply_and_verify_diff(FLAV_MICRODESC, md_body[3], md_body[4]));
  414. /* Self-to-self diff won't be present */
  415. consensus_cache_entry_t *ent;
  416. tt_int_op(CONSDIFF_NOT_FOUND, OP_EQ,
  417. lookup_diff_from(&ent, FLAV_NS, ns_body[5]));
  418. /* No diff from 2 has been added yet */
  419. tt_int_op(CONSDIFF_NOT_FOUND, OP_EQ,
  420. lookup_diff_from(&ent, FLAV_NS, ns_body[2]));
  421. /* No diff arriving at old things. */
  422. tt_int_op(-1, OP_EQ,
  423. lookup_apply_and_verify_diff(FLAV_MICRODESC, md_body[1], md_body[2]));
  424. /* No backwards diff */
  425. tt_int_op(-1, OP_EQ,
  426. lookup_apply_and_verify_diff(FLAV_MICRODESC, md_body[4], md_body[3]));
  427. /* Now, an update: add number 2 and make sure it's the only one whose diff
  428. * is regenerated. */
  429. tt_int_op(0, OP_EQ, consdiffmgr_add_consensus(ns_body[2], ns_ns[2]));
  430. consdiffmgr_rescan();
  431. tt_ptr_op(NULL, OP_NE, fake_cpuworker_queue);
  432. tt_int_op(1, OP_EQ, smartlist_len(fake_cpuworker_queue));
  433. tt_int_op(0, OP_EQ, mock_cpuworker_run_work());
  434. mock_cpuworker_handle_replies();
  435. tt_int_op(0, OP_EQ,
  436. lookup_apply_and_verify_diff(FLAV_NS, ns_body[2], ns_body[5]));
  437. /* Finally: reload, and make sure that the information is still indexed */
  438. cdm_reload();
  439. tt_int_op(0, OP_EQ,
  440. lookup_apply_and_verify_diff(FLAV_NS, ns_body[0], ns_body[5]));
  441. tt_int_op(0, OP_EQ,
  442. lookup_apply_and_verify_diff(FLAV_NS, ns_body[2], ns_body[5]));
  443. tt_int_op(0, OP_EQ,
  444. lookup_apply_and_verify_diff(FLAV_NS, ns_body[1], ns_body[5]));
  445. tt_int_op(0, OP_EQ,
  446. lookup_apply_and_verify_diff(FLAV_MICRODESC, md_body[1], md_body[4]));
  447. tt_int_op(0, OP_EQ,
  448. lookup_apply_and_verify_diff(FLAV_MICRODESC, md_body[2], md_body[4]));
  449. tt_int_op(0, OP_EQ,
  450. lookup_apply_and_verify_diff(FLAV_MICRODESC, md_body[3], md_body[4]));
  451. done:
  452. for (i = 0; i < N; ++i) {
  453. tor_free(md_body[i]);
  454. tor_free(ns_body[i]);
  455. networkstatus_vote_free(md_ns[i]);
  456. networkstatus_vote_free(ns_ns[i]);
  457. }
  458. UNMOCK(cpuworker_queue_work);
  459. #undef N
  460. }
  461. static void
  462. test_consdiffmgr_diff_failure(void *arg)
  463. {
  464. (void)arg;
  465. MOCK(cpuworker_queue_work, mock_cpuworker_queue_work);
  466. /* We're going to make sure that if we have a bogus request where
  467. * we can't actually compute a diff, the world must not end. */
  468. networkstatus_t *ns1 = NULL;
  469. networkstatus_t *ns2 = NULL;
  470. int r;
  471. ns1 = fake_ns_new(FLAV_NS, approx_time()-100);
  472. ns2 = fake_ns_new(FLAV_NS, approx_time()-50);
  473. r = consdiffmgr_add_consensus("foo bar baz\n", ns1);
  474. tt_int_op(r, OP_EQ, 0);
  475. // We refuse to compute a diff to or from a line holding only a single dot.
  476. // We can add it here, though.
  477. r = consdiffmgr_add_consensus("foo bar baz\n.\n.\n", ns2);
  478. tt_int_op(r, OP_EQ, 0);
  479. consdiffmgr_rescan();
  480. tt_ptr_op(NULL, OP_NE, fake_cpuworker_queue);
  481. setup_capture_of_logs(LOG_WARN);
  482. tt_int_op(1, OP_EQ, smartlist_len(fake_cpuworker_queue));
  483. tt_int_op(0, OP_EQ, mock_cpuworker_run_work());
  484. expect_single_log_msg_containing("one of the lines to be added is \".\".");
  485. mock_clean_saved_logs();
  486. mock_cpuworker_handle_replies();
  487. expect_single_log_msg_containing("Worker was unable to compute consensus "
  488. "diff from ");
  489. /* Make sure the diff is not present */
  490. consensus_cache_entry_t *ent;
  491. tt_int_op(CONSDIFF_NOT_FOUND, OP_EQ,
  492. lookup_diff_from(&ent, FLAV_NS, "foo bar baz\n"));
  493. done:
  494. teardown_capture_of_logs();
  495. UNMOCK(cpuworker_queue_work);
  496. networkstatus_vote_free(ns1);
  497. networkstatus_vote_free(ns2);
  498. }
  499. static void
  500. test_consdiffmgr_diff_pending(void *arg)
  501. {
  502. #define N 3
  503. (void)arg;
  504. char *md_body[N];
  505. networkstatus_t *md_ns[N];
  506. time_t start = approx_time() - 120;
  507. int i;
  508. for (i = 0; i < N; ++i) {
  509. time_t when = start + i * 30;
  510. md_body[i] = fake_ns_body_new(FLAV_MICRODESC, when);
  511. md_ns[i] = fake_ns_new(FLAV_MICRODESC, when);
  512. }
  513. MOCK(cpuworker_queue_work, mock_cpuworker_queue_work);
  514. tt_int_op(0, OP_EQ, consdiffmgr_add_consensus(md_body[1], md_ns[1]));
  515. tt_int_op(0, OP_EQ, consdiffmgr_add_consensus(md_body[2], md_ns[2]));
  516. /* Make a diff */
  517. consdiffmgr_rescan();
  518. tt_int_op(1, OP_EQ, smartlist_len(fake_cpuworker_queue));
  519. /* Look it up. Is it pending? */
  520. consensus_cache_entry_t *ent = NULL;
  521. consdiff_status_t diff_status;
  522. diff_status = lookup_diff_from(&ent, FLAV_MICRODESC, md_body[1]);
  523. tt_int_op(CONSDIFF_IN_PROGRESS, OP_EQ, diff_status);
  524. tt_ptr_op(ent, OP_EQ, NULL);
  525. /* Add another old consensus. only one new diff should launch! */
  526. tt_int_op(0, OP_EQ, consdiffmgr_add_consensus(md_body[0], md_ns[0]));
  527. consdiffmgr_rescan();
  528. tt_int_op(2, OP_EQ, smartlist_len(fake_cpuworker_queue));
  529. tt_int_op(0, OP_EQ, mock_cpuworker_run_work());
  530. mock_cpuworker_handle_replies();
  531. tt_int_op(0, OP_EQ,
  532. lookup_apply_and_verify_diff(FLAV_MICRODESC, md_body[0], md_body[2]));
  533. tt_int_op(0, OP_EQ,
  534. lookup_apply_and_verify_diff(FLAV_MICRODESC, md_body[1], md_body[2]));
  535. done:
  536. UNMOCK(cpuworker_queue_work);
  537. #undef N
  538. }
  539. static void
  540. test_consdiffmgr_cleanup_old(void *arg)
  541. {
  542. (void)arg;
  543. config_line_t *labels = NULL;
  544. consensus_cache_entry_t *ent = NULL;
  545. consensus_cache_t *cache = cdm_cache_get(); // violate abstraction barrier
  546. /* This item will be will be cleanable because it has a valid-after
  547. * time far in the past. */
  548. config_line_prepend(&labels, "document-type", "confribble-blarg");
  549. config_line_prepend(&labels, "consensus-valid-after",
  550. "1980-10-10T10:10:10");
  551. ent = consensus_cache_add(cache, labels, (const uint8_t*)"Foo", 3);
  552. tt_assert(ent);
  553. consensus_cache_entry_decref(ent);
  554. setup_capture_of_logs(LOG_DEBUG);
  555. tt_int_op(1, OP_EQ, consdiffmgr_cleanup());
  556. expect_log_msg_containing("Deleting entry because its consensus-valid-"
  557. "after value (1980-10-10T10:10:10) was too old");
  558. done:
  559. teardown_capture_of_logs();
  560. config_free_lines(labels);
  561. }
  562. static void
  563. test_consdiffmgr_cleanup_bad_valid_after(void *arg)
  564. {
  565. /* This will seem cleanable, but isn't, because its valid-after time is
  566. * misformed. */
  567. (void)arg;
  568. config_line_t *labels = NULL;
  569. consensus_cache_entry_t *ent = NULL;
  570. consensus_cache_t *cache = cdm_cache_get(); // violate abstraction barrier
  571. config_line_prepend(&labels, "document-type", "consensus");
  572. config_line_prepend(&labels, "consensus-valid-after",
  573. "whan that aprille with his shoures soote"); // (~1385?)
  574. ent = consensus_cache_add(cache, labels, (const uint8_t*)"Foo", 3);
  575. tt_assert(ent);
  576. consensus_cache_entry_decref(ent);
  577. setup_capture_of_logs(LOG_DEBUG);
  578. tt_int_op(0, OP_EQ, consdiffmgr_cleanup());
  579. expect_log_msg_containing("Ignoring entry because its consensus-valid-"
  580. "after value (\"whan that aprille with his "
  581. "shoures soote\") was unparseable");
  582. done:
  583. teardown_capture_of_logs();
  584. config_free_lines(labels);
  585. }
  586. static void
  587. test_consdiffmgr_cleanup_no_valid_after(void *arg)
  588. {
  589. (void)arg;
  590. config_line_t *labels = NULL;
  591. consensus_cache_entry_t *ent = NULL;
  592. consensus_cache_t *cache = cdm_cache_get(); // violate abstraction barrier
  593. /* This item will be will be uncleanable because it has no recognized
  594. * valid-after. */
  595. config_line_prepend(&labels, "document-type", "consensus");
  596. config_line_prepend(&labels, "confrooble-voolid-oofter",
  597. "2010-10-10T09:08:07");
  598. ent = consensus_cache_add(cache, labels, (const uint8_t*)"Foo", 3);
  599. tt_assert(ent);
  600. consensus_cache_entry_decref(ent);
  601. setup_capture_of_logs(LOG_DEBUG);
  602. tt_int_op(0, OP_EQ, consdiffmgr_cleanup());
  603. expect_log_msg_containing("Ignoring entry because it had no consensus-"
  604. "valid-after label");
  605. done:
  606. teardown_capture_of_logs();
  607. config_free_lines(labels);
  608. }
  609. static void
  610. test_consdiffmgr_cleanup_old_diffs(void *arg)
  611. {
  612. (void)arg;
  613. #define N 4
  614. char *md_body[N];
  615. networkstatus_t *md_ns[N];
  616. uint8_t md_ns_sha3[N][DIGEST256_LEN];
  617. int i;
  618. consensus_cache_entry_t *hold_ent = NULL, *ent;
  619. /* Make sure that the cleanup function removes diffs to the not-most-recent
  620. * consensus. */
  621. MOCK(cpuworker_queue_work, mock_cpuworker_queue_work);
  622. /* Create a bunch of consensus things at 15-second intervals. */
  623. time_t start = approx_time() - 120;
  624. for (i = 0; i < N; ++i) {
  625. time_t when = start + i * 15;
  626. md_body[i] = fake_ns_body_new(FLAV_MICRODESC, when);
  627. md_ns[i] = fake_ns_new(FLAV_MICRODESC, when);
  628. crypto_digest256((char *)md_ns_sha3[i], md_body[i], strlen(md_body[i]),
  629. DIGEST_SHA3_256);
  630. }
  631. /* add the first 3. */
  632. tt_int_op(0, OP_EQ, consdiffmgr_add_consensus(md_body[0], md_ns[0]));
  633. tt_int_op(0, OP_EQ, consdiffmgr_add_consensus(md_body[1], md_ns[1]));
  634. tt_int_op(0, OP_EQ, consdiffmgr_add_consensus(md_body[2], md_ns[2]));
  635. /* Make diffs. */
  636. consdiffmgr_rescan();
  637. tt_ptr_op(NULL, OP_NE, fake_cpuworker_queue);
  638. tt_int_op(2, OP_EQ, smartlist_len(fake_cpuworker_queue));
  639. tt_int_op(0, OP_EQ, mock_cpuworker_run_work());
  640. mock_cpuworker_handle_replies();
  641. tt_ptr_op(NULL, OP_EQ, fake_cpuworker_queue);
  642. /* Nothing is deletable now */
  643. tt_int_op(0, OP_EQ, consdiffmgr_cleanup());
  644. tt_int_op(0, OP_EQ,
  645. lookup_apply_and_verify_diff(FLAV_MICRODESC, md_body[0], md_body[2]));
  646. tt_int_op(0, OP_EQ,
  647. lookup_apply_and_verify_diff(FLAV_MICRODESC, md_body[1], md_body[2]));
  648. tt_int_op(CONSDIFF_AVAILABLE, OP_EQ,
  649. lookup_diff_from(&hold_ent, FLAV_MICRODESC, md_body[1]));
  650. consensus_cache_entry_incref(hold_ent); // incref, so it is preserved.
  651. /* Now add an even-more-recent consensus; this should make all previous
  652. * diffs deletable */
  653. tt_int_op(0, OP_EQ, consdiffmgr_add_consensus(md_body[3], md_ns[3]));
  654. tt_int_op(2, OP_EQ, consdiffmgr_cleanup());
  655. tt_int_op(CONSDIFF_NOT_FOUND, OP_EQ,
  656. lookup_diff_from(&ent, FLAV_MICRODESC, md_body[0]));
  657. /* This one is marked deletable but still in the hashtable */
  658. tt_int_op(CONSDIFF_AVAILABLE, OP_EQ,
  659. lookup_diff_from(&ent, FLAV_MICRODESC, md_body[1]));
  660. tt_int_op(CONSDIFF_NOT_FOUND, OP_EQ,
  661. lookup_diff_from(&ent, FLAV_MICRODESC, md_body[2]));
  662. /* Everything should be valid at this point */
  663. tt_int_op(0, OP_EQ, consdiffmgr_validate());
  664. /* And if we recan NOW, we'll purge the hashtable of the entries,
  665. * and launch attempts to generate new ones */
  666. consdiffmgr_rescan();
  667. tt_int_op(CONSDIFF_IN_PROGRESS, OP_EQ,
  668. lookup_diff_from(&ent, FLAV_MICRODESC, md_body[0]));
  669. tt_int_op(CONSDIFF_IN_PROGRESS, OP_EQ,
  670. lookup_diff_from(&ent, FLAV_MICRODESC, md_body[1]));
  671. tt_int_op(CONSDIFF_IN_PROGRESS, OP_EQ,
  672. lookup_diff_from(&ent, FLAV_MICRODESC, md_body[2]));
  673. /* We're still holding on to this, though, so we can still map it! */
  674. const uint8_t *t1 = NULL;
  675. size_t s;
  676. int r = consensus_cache_entry_get_body(hold_ent, &t1, &s);
  677. tt_int_op(r, OP_EQ, 0);
  678. tt_assert(t1);
  679. done:
  680. for (i = 0; i < N; ++i) {
  681. tor_free(md_body[i]);
  682. networkstatus_vote_free(md_ns[i]);
  683. }
  684. consensus_cache_entry_decref(hold_ent);
  685. UNMOCK(cpuworker_queue_work);
  686. #undef N
  687. }
  688. static void
  689. test_consdiffmgr_validate(void *arg)
  690. {
  691. (void)arg;
  692. config_line_t *lines = NULL;
  693. consensus_cache_entry_t *ent = NULL;
  694. consensus_cache_t *cache = cdm_cache_get(); // violate abstraction barrier
  695. smartlist_t *vals = smartlist_new();
  696. /* Put these: objects in the cache: one with a good sha3, one with bad sha3,
  697. * one with a wrong sha3, and one with no sha3. */
  698. config_line_prepend(&lines, "id", "wrong sha3");
  699. config_line_prepend(&lines, "sha3-digest",
  700. "F00DF00DF00DF00DF00DF00DF00DF00D"
  701. "F00DF00DF00DF00DF00DF00DF00DF00D");
  702. ent = consensus_cache_add(cache, lines, (const uint8_t *)"Hi there", 8);
  703. consensus_cache_entry_decref(ent);
  704. config_free_lines(lines);
  705. lines = NULL;
  706. config_line_prepend(&lines, "id", "bad sha3");
  707. config_line_prepend(&lines, "sha3-digest",
  708. "now is the winter of our dicotheque");
  709. ent = consensus_cache_add(cache, lines, (const uint8_t *)"Hi there", 8);
  710. consensus_cache_entry_decref(ent);
  711. config_free_lines(lines);
  712. lines = NULL;
  713. config_line_prepend(&lines, "id", "no sha3");
  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", "good sha3");
  719. config_line_prepend(&lines, "sha3-digest",
  720. "8d8b1998616cd6b4c4055da8d38728dc"
  721. "93c758d4131a53c7d81aa6337dee1c05");
  722. ent = consensus_cache_add(cache, lines, (const uint8_t *)"Hi there", 8);
  723. consensus_cache_entry_decref(ent);
  724. config_free_lines(lines);
  725. lines = NULL;
  726. cdm_reload();
  727. cache = cdm_cache_get();
  728. tt_int_op(1, OP_EQ, consdiffmgr_validate());
  729. consensus_cache_find_all(vals, cache, "id", "good sha3");
  730. tt_int_op(smartlist_len(vals), OP_EQ, 1);
  731. smartlist_clear(vals);
  732. consensus_cache_find_all(vals, cache, "id", "no sha3");
  733. tt_int_op(smartlist_len(vals), OP_EQ, 1);
  734. smartlist_clear(vals);
  735. consensus_cache_find_all(vals, cache, "id", "wrong sha3");
  736. tt_int_op(smartlist_len(vals), OP_EQ, 0);
  737. consensus_cache_find_all(vals, cache, "id", "bad sha3");
  738. tt_int_op(smartlist_len(vals), OP_EQ, 0);
  739. done:
  740. smartlist_free(vals);
  741. }
  742. #define TEST(name) \
  743. { #name, test_consdiffmgr_ ## name , TT_FORK, &setup_diffmgr, NULL }
  744. struct testcase_t consdiffmgr_tests[] = {
  745. #if 0
  746. { "init_failure", test_consdiffmgr_init_failure, TT_FORK, NULL, NULL },
  747. #endif
  748. TEST(sha3_helper),
  749. TEST(add),
  750. TEST(make_diffs),
  751. TEST(diff_rules),
  752. TEST(diff_failure),
  753. TEST(diff_pending),
  754. TEST(cleanup_old),
  755. TEST(cleanup_bad_valid_after),
  756. TEST(cleanup_no_valid_after),
  757. TEST(cleanup_old_diffs),
  758. TEST(validate),
  759. // XXXX Test: register status when no pending entry existed?? (bug)
  760. // XXXX Test: non-cacheing cases of replyfn().
  761. END_OF_TESTCASES
  762. };