test_consdiffmgr.c 30 KB

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