test_consdiffmgr.c 30 KB

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