test_consdiffmgr.c 29 KB

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