test_consdiffmgr.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. /* Copyright (c) 2017, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. #define CONSDIFFMGR_PRIVATE
  4. #include "or.h"
  5. #include "config.h"
  6. #include "conscache.h"
  7. #include "consdiff.h"
  8. #include "consdiffmgr.h"
  9. #include "cpuworker.h"
  10. #include "networkstatus.h"
  11. #include "workqueue.h"
  12. #include "test.h"
  13. #include "log_test_helpers.h"
  14. // ============================== Setup/teardown the consdiffmgr
  15. // These functions get run before/after each test in this module
  16. static void *
  17. consdiffmgr_test_setup(const struct testcase_t *arg)
  18. {
  19. (void)arg;
  20. char *ddir_fname = tor_strdup(get_fname_rnd("datadir_cdm"));
  21. tor_free(get_options_mutable()->DataDirectory);
  22. get_options_mutable()->DataDirectory = ddir_fname; // now owns the pointer.
  23. check_private_dir(ddir_fname, CPD_CREATE, NULL);
  24. consdiff_cfg_t consdiff_cfg = { 7200, 300 };
  25. consdiffmgr_configure(&consdiff_cfg);
  26. return (void *)1; // must return something non-null.
  27. }
  28. static int
  29. consdiffmgr_test_teardown(const struct testcase_t *arg, void *ignore)
  30. {
  31. (void)arg;
  32. (void)ignore;
  33. consdiffmgr_free_all();
  34. return 1;
  35. }
  36. static struct testcase_setup_t setup_diffmgr = {
  37. consdiffmgr_test_setup,
  38. consdiffmgr_test_teardown
  39. };
  40. // ============================== NS faking functions
  41. // These functions are for making quick fake consensus objects and
  42. // strings that are just good enough for consdiff and consdiffmgr.
  43. static networkstatus_t *
  44. fake_ns_new(consensus_flavor_t flav, time_t valid_after)
  45. {
  46. networkstatus_t *ns = tor_malloc_zero(sizeof(networkstatus_t));
  47. ns->type = NS_TYPE_CONSENSUS;
  48. ns->flavor = flav;
  49. ns->valid_after = valid_after;
  50. return ns;
  51. }
  52. static char *
  53. fake_ns_body_new(consensus_flavor_t flav, time_t valid_after)
  54. {
  55. const char *flavor_string = flav == FLAV_NS ? "" : " microdesc";
  56. char valid_after_string[ISO_TIME_LEN+1];
  57. format_iso_time(valid_after_string, valid_after);
  58. char *random_stuff = crypto_random_hostname(3, 25, "junk ", "");
  59. char *consensus;
  60. tor_asprintf(&consensus,
  61. "network-status-version 3%s\n"
  62. "vote-status consensus\n"
  63. "valid-after %s\n"
  64. "r name ccccccccccccccccc etc\nsample\n"
  65. "r name eeeeeeeeeeeeeeeee etc\nbar\n"
  66. "%s\n",
  67. flavor_string,
  68. valid_after_string,
  69. random_stuff);
  70. tor_free(random_stuff);
  71. return consensus;
  72. }
  73. // ============================== Cpuworker mocking code
  74. // These mocking functions and types capture the cpuworker calls
  75. // so we can inspect them and run them in the main thread.
  76. static smartlist_t *fake_cpuworker_queue = NULL;
  77. typedef struct fake_work_queue_ent_t {
  78. enum workqueue_reply_t (*fn)(void *, void *);
  79. void (*reply_fn)(void *);
  80. void *arg;
  81. } fake_work_queue_ent_t;
  82. static struct workqueue_entry_s *
  83. mock_cpuworker_queue_work(enum workqueue_reply_t (*fn)(void *, void *),
  84. void (*reply_fn)(void *),
  85. void *arg)
  86. {
  87. if (! fake_cpuworker_queue)
  88. fake_cpuworker_queue = smartlist_new();
  89. fake_work_queue_ent_t *ent = tor_malloc_zero(sizeof(*ent));
  90. ent->fn = fn;
  91. ent->reply_fn = reply_fn;
  92. ent->arg = arg;
  93. smartlist_add(fake_cpuworker_queue, ent);
  94. return (struct workqueue_entry_s *)ent;
  95. }
  96. static int
  97. mock_cpuworker_run_work(void)
  98. {
  99. if (! fake_cpuworker_queue)
  100. return 0;
  101. SMARTLIST_FOREACH(fake_cpuworker_queue, fake_work_queue_ent_t *, ent, {
  102. enum workqueue_reply_t r = ent->fn(NULL, ent->arg);
  103. if (r != WQ_RPL_REPLY)
  104. return -1;
  105. });
  106. return 0;
  107. }
  108. static void
  109. mock_cpuworker_handle_replies(void)
  110. {
  111. if (! fake_cpuworker_queue)
  112. return;
  113. SMARTLIST_FOREACH(fake_cpuworker_queue, fake_work_queue_ent_t *, ent, {
  114. ent->reply_fn(ent->arg);
  115. });
  116. smartlist_free(fake_cpuworker_queue);
  117. fake_cpuworker_queue = NULL;
  118. }
  119. // ============================== Beginning of tests
  120. static void
  121. test_consdiffmgr_add(void *arg)
  122. {
  123. (void) arg;
  124. time_t now = approx_time();
  125. consensus_cache_entry_t *ent = NULL;
  126. networkstatus_t *ns_tmp = fake_ns_new(FLAV_NS, now);
  127. const char *dummy = "foo";
  128. int r = consdiffmgr_add_consensus(dummy, ns_tmp);
  129. tt_int_op(r, OP_EQ, 0);
  130. /* If we add it again, it won't work */
  131. setup_capture_of_logs(LOG_INFO);
  132. dummy = "bar";
  133. r = consdiffmgr_add_consensus(dummy, ns_tmp);
  134. tt_int_op(r, OP_EQ, -1);
  135. expect_single_log_msg_containing("We already have a copy of that "
  136. "consensus");
  137. mock_clean_saved_logs();
  138. /* But it will work fine if the flavor is different */
  139. dummy = "baz";
  140. ns_tmp->flavor = FLAV_MICRODESC;
  141. r = consdiffmgr_add_consensus(dummy, ns_tmp);
  142. tt_int_op(r, OP_EQ, 0);
  143. /* And it will work fine if the time is different */
  144. dummy = "quux";
  145. ns_tmp->flavor = FLAV_NS;
  146. ns_tmp->valid_after = now - 60;
  147. r = consdiffmgr_add_consensus(dummy, ns_tmp);
  148. tt_int_op(r, OP_EQ, 0);
  149. /* If we add one a long long time ago, it will fail. */
  150. dummy = "xyzzy";
  151. ns_tmp->valid_after = 86400 * 100; /* A few months into 1970 */
  152. r = consdiffmgr_add_consensus(dummy, ns_tmp);
  153. tt_int_op(r, OP_EQ, -1);
  154. expect_single_log_msg_containing("it's too old.");
  155. /* Try looking up a consensuses. */
  156. ent = cdm_cache_lookup_consensus(FLAV_NS, now-60);
  157. tt_assert(ent);
  158. consensus_cache_entry_incref(ent);
  159. size_t s;
  160. const uint8_t *body;
  161. r = consensus_cache_entry_get_body(ent, &body, &s);
  162. tt_int_op(r, OP_EQ, 0);
  163. tt_int_op(s, OP_EQ, 4);
  164. tt_mem_op(body, OP_EQ, "quux", 4);
  165. /* Try looking up another entry, but fail */
  166. tt_assert(NULL == cdm_cache_lookup_consensus(FLAV_MICRODESC, now-60));
  167. tt_assert(NULL == cdm_cache_lookup_consensus(FLAV_NS, now-61));
  168. done:
  169. networkstatus_vote_free(ns_tmp);
  170. teardown_capture_of_logs();
  171. consensus_cache_entry_decref(ent);
  172. }
  173. static void
  174. test_consdiffmgr_make_diffs(void *arg)
  175. {
  176. (void)arg;
  177. networkstatus_t *ns = NULL;
  178. char *ns_body = NULL, *md_ns_body = NULL, *md_ns_body_2 = NULL;
  179. char *applied = NULL, *diff_text = NULL;
  180. time_t now = approx_time();
  181. int r;
  182. consensus_cache_entry_t *diff = NULL;
  183. uint8_t md_ns_sha3[DIGEST256_LEN];
  184. consdiff_status_t diff_status;
  185. MOCK(cpuworker_queue_work, mock_cpuworker_queue_work);
  186. // Try rescan with no consensuses: shouldn't crash or queue work.
  187. consdiffmgr_rescan();
  188. tt_ptr_op(NULL, OP_EQ, fake_cpuworker_queue);
  189. // Make two consensuses, 1 hour sec ago.
  190. ns = fake_ns_new(FLAV_NS, now-3600);
  191. ns_body = fake_ns_body_new(FLAV_NS, now-3600);
  192. r = consdiffmgr_add_consensus(ns_body, ns);
  193. networkstatus_vote_free(ns);
  194. tor_free(ns_body);
  195. tt_int_op(r, OP_EQ, 0);
  196. ns = fake_ns_new(FLAV_MICRODESC, now-3600);
  197. md_ns_body = fake_ns_body_new(FLAV_MICRODESC, now-3600);
  198. r = consdiffmgr_add_consensus(md_ns_body, ns);
  199. crypto_digest256((char*)md_ns_sha3, md_ns_body, strlen(md_ns_body),
  200. DIGEST_SHA3_256);
  201. networkstatus_vote_free(ns);
  202. tt_int_op(r, OP_EQ, 0);
  203. // No diffs will be generated.
  204. consdiffmgr_rescan();
  205. tt_ptr_op(NULL, OP_EQ, fake_cpuworker_queue);
  206. // Add a MD consensus from 45 minutes ago. This should cause one diff
  207. // worth of work to get queued.
  208. ns = fake_ns_new(FLAV_MICRODESC, now-45*60);
  209. md_ns_body_2 = fake_ns_body_new(FLAV_MICRODESC, now-45*60);
  210. r = consdiffmgr_add_consensus(md_ns_body_2, ns);
  211. networkstatus_vote_free(ns);
  212. tt_int_op(r, OP_EQ, 0);
  213. consdiffmgr_rescan();
  214. tt_ptr_op(NULL, OP_NE, fake_cpuworker_queue);
  215. tt_int_op(1, OP_EQ, smartlist_len(fake_cpuworker_queue));
  216. // XXXX not working yet
  217. /*
  218. diff_status = consdiffmgr_find_diff_from(&diff, FLAV_MICRODESC,
  219. DIGEST_SHA3_256,
  220. md_ns_sha3, DIGEST256_LEN);
  221. tt_int_op(CONSDIFF_IN_PROGRESS, OP_EQ, diff_status);
  222. */
  223. // Now run that process and get the diff.
  224. r = mock_cpuworker_run_work();
  225. tt_int_op(r, OP_EQ, 0);
  226. mock_cpuworker_handle_replies();
  227. // At this point we should be able to get that diff.
  228. diff_status = consdiffmgr_find_diff_from(&diff, FLAV_MICRODESC,
  229. DIGEST_SHA3_256,
  230. md_ns_sha3, DIGEST256_LEN);
  231. tt_int_op(CONSDIFF_AVAILABLE, OP_EQ, diff_status);
  232. tt_assert(diff);
  233. /* Make sure applying the diff actually works */
  234. const uint8_t *diff_body;
  235. size_t diff_size;
  236. r = consensus_cache_entry_get_body(diff, &diff_body, &diff_size);
  237. tt_int_op(r, OP_EQ, 0);
  238. diff_text = tor_memdup_nulterm(diff_body, diff_size);
  239. applied = consensus_diff_apply(md_ns_body, diff_text);
  240. tt_assert(applied);
  241. tt_str_op(applied, OP_EQ, md_ns_body_2);
  242. /* Rescan again: no more work to do. */
  243. consdiffmgr_rescan();
  244. tt_ptr_op(NULL, OP_EQ, fake_cpuworker_queue);
  245. done:
  246. tor_free(md_ns_body);
  247. tor_free(md_ns_body_2);
  248. tor_free(diff_text);
  249. tor_free(applied);
  250. }
  251. #define TEST(name) \
  252. { #name, test_consdiffmgr_ ## name , TT_FORK, &setup_diffmgr, NULL }
  253. struct testcase_t consdiffmgr_tests[] = {
  254. TEST(add),
  255. TEST(make_diffs),
  256. // XXXX Test: deleting consensuses for being too old
  257. // XXXX Test: deleting diffs for not being to most recent consensus
  258. // XXXX Test: Objects of unrecognized doctype are not cleaned.
  259. // XXXX Test: Objects with bad iso time are not cleaned.
  260. // XXXX Test: only generate diffs to most recent consensus
  261. // XXXX Test: making diffs from some old consensuses, but having diffs
  262. // for others.
  263. // XXXX Test: Failure to open cache???
  264. // XXXX Test: failure to create consensus diff.
  265. END_OF_TESTCASES
  266. };