test_consdiffmgr.c 29 KB

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