test_consdiffmgr.c 29 KB

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