test_routerlist.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787
  1. /* Copyright (c) 2014-2018, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. #include "orconfig.h"
  4. #include <math.h>
  5. #include <time.h>
  6. #define CONNECTION_PRIVATE
  7. #define DIRECTORY_PRIVATE
  8. #define DIRVOTE_PRIVATE
  9. #define ENTRYNODES_PRIVATE
  10. #define HIBERNATE_PRIVATE
  11. #define NETWORKSTATUS_PRIVATE
  12. #define ROUTERLIST_PRIVATE
  13. #define TOR_UNIT_TESTING
  14. #include "or/or.h"
  15. #include "or/config.h"
  16. #include "or/connection.h"
  17. #include "or/control.h"
  18. #include "lib/crypt_ops/crypto_rand.h"
  19. #include "or/directory.h"
  20. #include "or/dirauth/dirvote.h"
  21. #include "or/entrynodes.h"
  22. #include "or/hibernate.h"
  23. #include "or/microdesc.h"
  24. #include "or/networkstatus.h"
  25. #include "or/nodelist.h"
  26. #include "or/policies.h"
  27. #include "or/router.h"
  28. #include "or/routerlist.h"
  29. #include "or/routerset.h"
  30. #include "or/routerparse.h"
  31. #include "or/dirauth/shared_random.h"
  32. #include "or/statefile.h"
  33. #include "or/authority_cert_st.h"
  34. #include "or/dir_connection_st.h"
  35. #include "or/networkstatus_st.h"
  36. #include "or/node_st.h"
  37. #include "or/routerstatus_st.h"
  38. #include "lib/encoding/confline.h"
  39. #include "lib/container/buffers.h"
  40. #include "test/test.h"
  41. #include "test/test_dir_common.h"
  42. #include "test/log_test_helpers.h"
  43. void construct_consensus(char **consensus_text_md, time_t now);
  44. static authority_cert_t *mock_cert;
  45. static authority_cert_t *
  46. get_my_v3_authority_cert_m(void)
  47. {
  48. tor_assert(mock_cert);
  49. return mock_cert;
  50. }
  51. /* 4 digests + 3 sep + pre + post + NULL */
  52. static char output[4*BASE64_DIGEST256_LEN+3+2+2+1];
  53. static void
  54. mock_get_from_dirserver(uint8_t dir_purpose, uint8_t router_purpose,
  55. const char *resource, int pds_flags,
  56. download_want_authority_t want_authority)
  57. {
  58. (void)dir_purpose;
  59. (void)router_purpose;
  60. (void)pds_flags;
  61. (void)want_authority;
  62. tt_assert(resource);
  63. strlcpy(output, resource, sizeof(output));
  64. done:
  65. ;
  66. }
  67. static void
  68. test_routerlist_initiate_descriptor_downloads(void *arg)
  69. {
  70. const char *prose = "unhurried and wise, we perceive.";
  71. smartlist_t *digests = smartlist_new();
  72. (void)arg;
  73. for (int i = 0; i < 20; i++) {
  74. smartlist_add(digests, (char*)prose);
  75. }
  76. MOCK(directory_get_from_dirserver, mock_get_from_dirserver);
  77. initiate_descriptor_downloads(NULL, DIR_PURPOSE_FETCH_MICRODESC,
  78. digests, 3, 7, 0);
  79. UNMOCK(directory_get_from_dirserver);
  80. tt_str_op(output, OP_EQ, "d/"
  81. "dW5odXJyaWVkIGFuZCB3aXNlLCB3ZSBwZXJjZWl2ZS4-"
  82. "dW5odXJyaWVkIGFuZCB3aXNlLCB3ZSBwZXJjZWl2ZS4-"
  83. "dW5odXJyaWVkIGFuZCB3aXNlLCB3ZSBwZXJjZWl2ZS4-"
  84. "dW5odXJyaWVkIGFuZCB3aXNlLCB3ZSBwZXJjZWl2ZS4"
  85. ".z");
  86. done:
  87. smartlist_free(digests);
  88. }
  89. static int count = 0;
  90. static void
  91. mock_initiate_descriptor_downloads(const routerstatus_t *source,
  92. int purpose, smartlist_t *digests,
  93. int lo, int hi, int pds_flags)
  94. {
  95. (void)source;
  96. (void)purpose;
  97. (void)digests;
  98. (void)pds_flags;
  99. (void)hi;
  100. (void)lo;
  101. count += 1;
  102. }
  103. static void
  104. test_routerlist_launch_descriptor_downloads(void *arg)
  105. {
  106. smartlist_t *downloadable = smartlist_new();
  107. time_t now = time(NULL);
  108. char *cp;
  109. (void)arg;
  110. for (int i = 0; i < 100; i++) {
  111. cp = tor_malloc(DIGEST256_LEN);
  112. tt_assert(cp);
  113. crypto_rand(cp, DIGEST256_LEN);
  114. smartlist_add(downloadable, cp);
  115. }
  116. MOCK(initiate_descriptor_downloads, mock_initiate_descriptor_downloads);
  117. launch_descriptor_downloads(DIR_PURPOSE_FETCH_MICRODESC, downloadable,
  118. NULL, now);
  119. tt_int_op(3, OP_EQ, count);
  120. UNMOCK(initiate_descriptor_downloads);
  121. done:
  122. SMARTLIST_FOREACH(downloadable, char *, cp1, tor_free(cp1));
  123. smartlist_free(downloadable);
  124. }
  125. void
  126. construct_consensus(char **consensus_text_md, time_t now)
  127. {
  128. networkstatus_t *vote = NULL;
  129. networkstatus_t *v1 = NULL, *v2 = NULL, *v3 = NULL;
  130. networkstatus_voter_info_t *voter = NULL;
  131. authority_cert_t *cert1=NULL, *cert2=NULL, *cert3=NULL;
  132. crypto_pk_t *sign_skey_1=NULL, *sign_skey_2=NULL, *sign_skey_3=NULL;
  133. crypto_pk_t *sign_skey_leg=NULL;
  134. smartlist_t *votes = NULL;
  135. int n_vrs;
  136. tt_assert(!dir_common_authority_pk_init(&cert1, &cert2, &cert3,
  137. &sign_skey_1, &sign_skey_2,
  138. &sign_skey_3));
  139. sign_skey_leg = pk_generate(4);
  140. dir_common_construct_vote_1(&vote, cert1, sign_skey_1,
  141. &dir_common_gen_routerstatus_for_v3ns,
  142. &v1, &n_vrs, now, 1);
  143. networkstatus_vote_free(vote);
  144. tt_assert(v1);
  145. tt_int_op(n_vrs, OP_EQ, 4);
  146. tt_int_op(smartlist_len(v1->routerstatus_list), OP_EQ, 4);
  147. dir_common_construct_vote_2(&vote, cert2, sign_skey_2,
  148. &dir_common_gen_routerstatus_for_v3ns,
  149. &v2, &n_vrs, now, 1);
  150. networkstatus_vote_free(vote);
  151. tt_assert(v2);
  152. tt_int_op(n_vrs, OP_EQ, 4);
  153. tt_int_op(smartlist_len(v2->routerstatus_list), OP_EQ, 4);
  154. dir_common_construct_vote_3(&vote, cert3, sign_skey_3,
  155. &dir_common_gen_routerstatus_for_v3ns,
  156. &v3, &n_vrs, now, 1);
  157. tt_assert(v3);
  158. tt_int_op(n_vrs, OP_EQ, 4);
  159. tt_int_op(smartlist_len(v3->routerstatus_list), OP_EQ, 4);
  160. networkstatus_vote_free(vote);
  161. votes = smartlist_new();
  162. smartlist_add(votes, v1);
  163. smartlist_add(votes, v2);
  164. smartlist_add(votes, v3);
  165. *consensus_text_md = networkstatus_compute_consensus(votes, 3,
  166. cert1->identity_key,
  167. sign_skey_1,
  168. "AAAAAAAAAAAAAAAAAAAA",
  169. sign_skey_leg,
  170. FLAV_MICRODESC);
  171. tt_assert(*consensus_text_md);
  172. done:
  173. tor_free(voter);
  174. networkstatus_vote_free(v1);
  175. networkstatus_vote_free(v2);
  176. networkstatus_vote_free(v3);
  177. smartlist_free(votes);
  178. authority_cert_free(cert1);
  179. authority_cert_free(cert2);
  180. authority_cert_free(cert3);
  181. crypto_pk_free(sign_skey_1);
  182. crypto_pk_free(sign_skey_2);
  183. crypto_pk_free(sign_skey_3);
  184. crypto_pk_free(sign_skey_leg);
  185. }
  186. static int mock_usable_consensus_flavor_value = FLAV_NS;
  187. static int
  188. mock_usable_consensus_flavor(void)
  189. {
  190. return mock_usable_consensus_flavor_value;
  191. }
  192. static void
  193. test_router_pick_directory_server_impl(void *arg)
  194. {
  195. (void)arg;
  196. networkstatus_t *con_md = NULL;
  197. char *consensus_text_md = NULL;
  198. int flags = PDS_IGNORE_FASCISTFIREWALL|PDS_RETRY_IF_NO_SERVERS;
  199. or_options_t *options = get_options_mutable();
  200. const routerstatus_t *rs = NULL;
  201. options->UseMicrodescriptors = 1;
  202. char *router1_id = NULL, *router2_id = NULL, *router3_id = NULL;
  203. node_t *node_router1 = NULL, *node_router2 = NULL, *node_router3 = NULL;
  204. config_line_t *policy_line = NULL;
  205. time_t now = time(NULL);
  206. int tmp_dirport1, tmp_dirport3;
  207. (void)arg;
  208. MOCK(usable_consensus_flavor, mock_usable_consensus_flavor);
  209. /* With no consensus, we must be bootstrapping, regardless of time or flavor
  210. */
  211. mock_usable_consensus_flavor_value = FLAV_NS;
  212. tt_assert(networkstatus_consensus_is_bootstrapping(now));
  213. tt_assert(networkstatus_consensus_is_bootstrapping(now + 2000));
  214. tt_assert(networkstatus_consensus_is_bootstrapping(now + 2*24*60*60));
  215. tt_assert(networkstatus_consensus_is_bootstrapping(now - 2*24*60*60));
  216. mock_usable_consensus_flavor_value = FLAV_MICRODESC;
  217. tt_assert(networkstatus_consensus_is_bootstrapping(now));
  218. tt_assert(networkstatus_consensus_is_bootstrapping(now + 2000));
  219. tt_assert(networkstatus_consensus_is_bootstrapping(now + 2*24*60*60));
  220. tt_assert(networkstatus_consensus_is_bootstrapping(now - 2*24*60*60));
  221. /* Init SR subsystem. */
  222. MOCK(get_my_v3_authority_cert, get_my_v3_authority_cert_m);
  223. mock_cert = authority_cert_parse_from_string(AUTHORITY_CERT_1, NULL);
  224. sr_init(0);
  225. UNMOCK(get_my_v3_authority_cert);
  226. /* No consensus available, fail early */
  227. rs = router_pick_directory_server_impl(V3_DIRINFO, (const int) 0, NULL);
  228. tt_ptr_op(rs, OP_EQ, NULL);
  229. construct_consensus(&consensus_text_md, now);
  230. tt_assert(consensus_text_md);
  231. con_md = networkstatus_parse_vote_from_string(consensus_text_md, NULL,
  232. NS_TYPE_CONSENSUS);
  233. tt_assert(con_md);
  234. tt_int_op(con_md->flavor,OP_EQ, FLAV_MICRODESC);
  235. tt_assert(con_md->routerstatus_list);
  236. tt_int_op(smartlist_len(con_md->routerstatus_list), OP_EQ, 3);
  237. tt_assert(!networkstatus_set_current_consensus_from_ns(con_md,
  238. "microdesc"));
  239. /* If the consensus time or flavor doesn't match, we are still
  240. * bootstrapping */
  241. mock_usable_consensus_flavor_value = FLAV_NS;
  242. tt_assert(networkstatus_consensus_is_bootstrapping(now));
  243. tt_assert(networkstatus_consensus_is_bootstrapping(now + 2000));
  244. tt_assert(networkstatus_consensus_is_bootstrapping(now + 2*24*60*60));
  245. tt_assert(networkstatus_consensus_is_bootstrapping(now - 2*24*60*60));
  246. /* With a valid consensus for the current time and flavor, we stop
  247. * bootstrapping, even if we have no certificates */
  248. mock_usable_consensus_flavor_value = FLAV_MICRODESC;
  249. tt_assert(!networkstatus_consensus_is_bootstrapping(now + 2000));
  250. tt_assert(!networkstatus_consensus_is_bootstrapping(con_md->valid_after));
  251. tt_assert(!networkstatus_consensus_is_bootstrapping(con_md->valid_until));
  252. tt_assert(!networkstatus_consensus_is_bootstrapping(con_md->valid_until
  253. + 24*60*60));
  254. /* These times are outside the test validity period */
  255. tt_assert(networkstatus_consensus_is_bootstrapping(now));
  256. tt_assert(networkstatus_consensus_is_bootstrapping(now + 2*24*60*60));
  257. tt_assert(networkstatus_consensus_is_bootstrapping(now - 2*24*60*60));
  258. nodelist_set_consensus(con_md);
  259. nodelist_assert_ok();
  260. rs = router_pick_directory_server_impl(V3_DIRINFO, flags, NULL);
  261. /* We should not fail now we have a consensus and routerstatus_list
  262. * and nodelist are populated. */
  263. tt_ptr_op(rs, OP_NE, NULL);
  264. /* Manipulate the nodes so we get the dir server we expect */
  265. router1_id = tor_malloc(DIGEST_LEN);
  266. memset(router1_id, TEST_DIR_ROUTER_ID_1, DIGEST_LEN);
  267. router2_id = tor_malloc(DIGEST_LEN);
  268. memset(router2_id, TEST_DIR_ROUTER_ID_2, DIGEST_LEN);
  269. router3_id = tor_malloc(DIGEST_LEN);
  270. memset(router3_id, TEST_DIR_ROUTER_ID_3, DIGEST_LEN);
  271. node_router1 = node_get_mutable_by_id(router1_id);
  272. node_router2 = node_get_mutable_by_id(router2_id);
  273. node_router3 = node_get_mutable_by_id(router3_id);
  274. node_router1->is_possible_guard = 1;
  275. node_router1->is_running = 0;
  276. node_router3->is_running = 0;
  277. rs = router_pick_directory_server_impl(V3_DIRINFO, flags, NULL);
  278. tt_ptr_op(rs, OP_NE, NULL);
  279. tt_assert(tor_memeq(rs->identity_digest, router2_id, DIGEST_LEN));
  280. rs = NULL;
  281. node_router1->is_running = 1;
  282. node_router3->is_running = 1;
  283. node_router1->rs->is_v2_dir = 0;
  284. node_router3->rs->is_v2_dir = 0;
  285. tmp_dirport1 = node_router1->rs->dir_port;
  286. tmp_dirport3 = node_router3->rs->dir_port;
  287. node_router1->rs->dir_port = 0;
  288. node_router3->rs->dir_port = 0;
  289. rs = router_pick_directory_server_impl(V3_DIRINFO, flags, NULL);
  290. tt_ptr_op(rs, OP_NE, NULL);
  291. tt_assert(tor_memeq(rs->identity_digest, router2_id, DIGEST_LEN));
  292. rs = NULL;
  293. node_router1->rs->is_v2_dir = 1;
  294. node_router3->rs->is_v2_dir = 1;
  295. node_router1->rs->dir_port = tmp_dirport1;
  296. node_router3->rs->dir_port = tmp_dirport3;
  297. node_router1->is_valid = 0;
  298. node_router3->is_valid = 0;
  299. rs = router_pick_directory_server_impl(V3_DIRINFO, flags, NULL);
  300. tt_ptr_op(rs, OP_NE, NULL);
  301. tt_assert(tor_memeq(rs->identity_digest, router2_id, DIGEST_LEN));
  302. rs = NULL;
  303. node_router1->is_valid = 1;
  304. node_router3->is_valid = 1;
  305. /* Manipulate overloaded */
  306. node_router2->rs->last_dir_503_at = now;
  307. node_router3->rs->last_dir_503_at = now;
  308. rs = router_pick_directory_server_impl(V3_DIRINFO, flags, NULL);
  309. tt_ptr_op(rs, OP_NE, NULL);
  310. tt_assert(tor_memeq(rs->identity_digest, router1_id, DIGEST_LEN));
  311. node_router2->rs->last_dir_503_at = 0;
  312. node_router3->rs->last_dir_503_at = 0;
  313. /* Set a Fascist firewall */
  314. flags &= ~ PDS_IGNORE_FASCISTFIREWALL;
  315. policy_line = tor_malloc_zero(sizeof(config_line_t));
  316. policy_line->key = tor_strdup("ReachableORAddresses");
  317. policy_line->value = tor_strdup("accept *:442, reject *:*");
  318. options->ReachableORAddresses = policy_line;
  319. policies_parse_from_options(options);
  320. node_router1->rs->or_port = 444;
  321. node_router2->rs->or_port = 443;
  322. node_router3->rs->or_port = 442;
  323. rs = router_pick_directory_server_impl(V3_DIRINFO, flags, NULL);
  324. tt_ptr_op(rs, OP_NE, NULL);
  325. tt_assert(tor_memeq(rs->identity_digest, router3_id, DIGEST_LEN));
  326. node_router1->rs->or_port = 442;
  327. node_router2->rs->or_port = 443;
  328. node_router3->rs->or_port = 444;
  329. rs = router_pick_directory_server_impl(V3_DIRINFO, flags, NULL);
  330. tt_ptr_op(rs, OP_NE, NULL);
  331. tt_assert(tor_memeq(rs->identity_digest, router1_id, DIGEST_LEN));
  332. /* Fascist firewall and overloaded */
  333. node_router1->rs->or_port = 442;
  334. node_router2->rs->or_port = 443;
  335. node_router3->rs->or_port = 442;
  336. node_router3->rs->last_dir_503_at = now;
  337. rs = router_pick_directory_server_impl(V3_DIRINFO, flags, NULL);
  338. tt_ptr_op(rs, OP_NE, NULL);
  339. tt_assert(tor_memeq(rs->identity_digest, router1_id, DIGEST_LEN));
  340. node_router3->rs->last_dir_503_at = 0;
  341. /* Fascists against OR and Dir */
  342. policy_line = tor_malloc_zero(sizeof(config_line_t));
  343. policy_line->key = tor_strdup("ReachableAddresses");
  344. policy_line->value = tor_strdup("accept *:80, reject *:*");
  345. options->ReachableDirAddresses = policy_line;
  346. policies_parse_from_options(options);
  347. node_router1->rs->or_port = 442;
  348. node_router2->rs->or_port = 441;
  349. node_router3->rs->or_port = 443;
  350. node_router1->rs->dir_port = 80;
  351. node_router2->rs->dir_port = 80;
  352. node_router3->rs->dir_port = 81;
  353. node_router1->rs->last_dir_503_at = now;
  354. rs = router_pick_directory_server_impl(V3_DIRINFO, flags, NULL);
  355. tt_ptr_op(rs, OP_NE, NULL);
  356. tt_assert(tor_memeq(rs->identity_digest, router1_id, DIGEST_LEN));
  357. node_router1->rs->last_dir_503_at = 0;
  358. done:
  359. UNMOCK(usable_consensus_flavor);
  360. if (router1_id)
  361. tor_free(router1_id);
  362. if (router2_id)
  363. tor_free(router2_id);
  364. if (router3_id)
  365. tor_free(router3_id);
  366. if (options->ReachableORAddresses ||
  367. options->ReachableDirAddresses)
  368. policies_free_all();
  369. tor_free(consensus_text_md);
  370. networkstatus_vote_free(con_md);
  371. }
  372. static or_state_t *dummy_state = NULL;
  373. static or_state_t *
  374. get_or_state_replacement(void)
  375. {
  376. return dummy_state;
  377. }
  378. static void
  379. mock_directory_initiate_request(directory_request_t *req)
  380. {
  381. (void)req;
  382. return;
  383. }
  384. static circuit_guard_state_t *
  385. mock_circuit_guard_state_new(entry_guard_t *guard, unsigned state,
  386. entry_guard_restriction_t *rst)
  387. {
  388. (void) guard;
  389. (void) state;
  390. (void) rst;
  391. return NULL;
  392. }
  393. /** Test that we will use our directory guards to fetch mds even if we don't
  394. * have any dirinfo (tests bug #23862). */
  395. static void
  396. test_directory_guard_fetch_with_no_dirinfo(void *arg)
  397. {
  398. int retval;
  399. char *consensus_text_md = NULL;
  400. or_options_t *options = get_options_mutable();
  401. time_t now = time(NULL);
  402. (void) arg;
  403. hibernate_set_state_for_testing_(HIBERNATE_STATE_LIVE);
  404. /* Initialize the SRV subsystem */
  405. MOCK(get_my_v3_authority_cert, get_my_v3_authority_cert_m);
  406. mock_cert = authority_cert_parse_from_string(AUTHORITY_CERT_1, NULL);
  407. sr_init(0);
  408. UNMOCK(get_my_v3_authority_cert);
  409. /* Initialize the entry node configuration from the ticket */
  410. options->UseEntryGuards = 1;
  411. options->StrictNodes = 1;
  412. get_options_mutable()->EntryNodes = routerset_new();
  413. routerset_parse(get_options_mutable()->EntryNodes,
  414. "2121212121212121212121212121212121212121", "foo");
  415. /* Mock some functions */
  416. dummy_state = tor_malloc_zero(sizeof(or_state_t));
  417. MOCK(get_or_state, get_or_state_replacement);
  418. MOCK(directory_initiate_request, mock_directory_initiate_request);
  419. /* we need to mock this one to avoid memleaks */
  420. MOCK(circuit_guard_state_new, mock_circuit_guard_state_new);
  421. /* Call guards_update_all() to simulate loading our state file (see
  422. * entry_guards_load_guards_from_state() and ticket #23989). */
  423. guards_update_all();
  424. /* Test logic: Simulate the arrival of a new consensus when we have no
  425. * dirinfo at all. Tor will need to fetch the mds from the consensus. Make
  426. * sure that Tor will use the specified entry guard instead of relying on the
  427. * fallback directories. */
  428. /* Fixup the dirconn that will deliver the consensus */
  429. dir_connection_t *conn = dir_connection_new(AF_INET);
  430. tor_addr_from_ipv4h(&conn->base_.addr, 0x7f000001);
  431. conn->base_.port = 8800;
  432. TO_CONN(conn)->address = tor_strdup("127.0.0.1");
  433. conn->base_.purpose = DIR_PURPOSE_FETCH_CONSENSUS;
  434. conn->requested_resource = tor_strdup("ns");
  435. /* Construct a consensus */
  436. construct_consensus(&consensus_text_md, now);
  437. tt_assert(consensus_text_md);
  438. /* Place the consensus in the dirconn */
  439. response_handler_args_t args;
  440. memset(&args, 0, sizeof(response_handler_args_t));
  441. args.status_code = 200;
  442. args.body = consensus_text_md;
  443. args.body_len = strlen(consensus_text_md);
  444. /* Update approx time so that the consensus is considered live */
  445. update_approx_time(now+1010);
  446. setup_capture_of_logs(LOG_DEBUG);
  447. /* Now handle the consensus */
  448. retval = handle_response_fetch_consensus(conn, &args);
  449. tt_int_op(retval, OP_EQ, 0);
  450. /* Make sure that our primary guard was chosen */
  451. expect_log_msg_containing("Selected primary guard router3");
  452. done:
  453. tor_free(consensus_text_md);
  454. tor_free(dummy_state);
  455. connection_free_minimal(TO_CONN(conn));
  456. entry_guards_free_all();
  457. teardown_capture_of_logs();
  458. }
  459. static connection_t *mocked_connection = NULL;
  460. /* Mock connection_get_by_type_addr_port_purpose by returning
  461. * mocked_connection. */
  462. static connection_t *
  463. mock_connection_get_by_type_addr_port_purpose(int type,
  464. const tor_addr_t *addr,
  465. uint16_t port, int purpose)
  466. {
  467. (void)type;
  468. (void)addr;
  469. (void)port;
  470. (void)purpose;
  471. return mocked_connection;
  472. }
  473. #define TEST_ADDR_STR "127.0.0.1"
  474. #define TEST_DIR_PORT 12345
  475. static void
  476. test_routerlist_router_is_already_dir_fetching(void *arg)
  477. {
  478. (void)arg;
  479. tor_addr_port_t test_ap, null_addr_ap, zero_port_ap;
  480. /* Setup */
  481. tor_addr_parse(&test_ap.addr, TEST_ADDR_STR);
  482. test_ap.port = TEST_DIR_PORT;
  483. tor_addr_make_null(&null_addr_ap.addr, AF_INET6);
  484. null_addr_ap.port = TEST_DIR_PORT;
  485. tor_addr_parse(&zero_port_ap.addr, TEST_ADDR_STR);
  486. zero_port_ap.port = 0;
  487. MOCK(connection_get_by_type_addr_port_purpose,
  488. mock_connection_get_by_type_addr_port_purpose);
  489. /* Test that we never get 1 from a NULL connection */
  490. mocked_connection = NULL;
  491. tt_int_op(router_is_already_dir_fetching(&test_ap, 1, 1), OP_EQ, 0);
  492. tt_int_op(router_is_already_dir_fetching(&test_ap, 1, 0), OP_EQ, 0);
  493. tt_int_op(router_is_already_dir_fetching(&test_ap, 0, 1), OP_EQ, 0);
  494. /* We always expect 0 in these cases */
  495. tt_int_op(router_is_already_dir_fetching(&test_ap, 0, 0), OP_EQ, 0);
  496. tt_int_op(router_is_already_dir_fetching(NULL, 1, 1), OP_EQ, 0);
  497. tt_int_op(router_is_already_dir_fetching(&null_addr_ap, 1, 1), OP_EQ, 0);
  498. tt_int_op(router_is_already_dir_fetching(&zero_port_ap, 1, 1), OP_EQ, 0);
  499. /* Test that we get 1 with a connection in the appropriate circumstances */
  500. mocked_connection = connection_new(CONN_TYPE_DIR, AF_INET);
  501. tt_int_op(router_is_already_dir_fetching(&test_ap, 1, 1), OP_EQ, 1);
  502. tt_int_op(router_is_already_dir_fetching(&test_ap, 1, 0), OP_EQ, 1);
  503. tt_int_op(router_is_already_dir_fetching(&test_ap, 0, 1), OP_EQ, 1);
  504. /* Test that we get 0 even with a connection in the appropriate
  505. * circumstances */
  506. tt_int_op(router_is_already_dir_fetching(&test_ap, 0, 0), OP_EQ, 0);
  507. tt_int_op(router_is_already_dir_fetching(NULL, 1, 1), OP_EQ, 0);
  508. tt_int_op(router_is_already_dir_fetching(&null_addr_ap, 1, 1), OP_EQ, 0);
  509. tt_int_op(router_is_already_dir_fetching(&zero_port_ap, 1, 1), OP_EQ, 0);
  510. done:
  511. /* If a connection is never set up, connection_free chokes on it. */
  512. if (mocked_connection) {
  513. buf_free(mocked_connection->inbuf);
  514. buf_free(mocked_connection->outbuf);
  515. }
  516. tor_free(mocked_connection);
  517. UNMOCK(connection_get_by_type_addr_port_purpose);
  518. }
  519. #undef TEST_ADDR_STR
  520. #undef TEST_DIR_PORT
  521. static long mock_apparent_skew = 0;
  522. /** Store apparent_skew and assert that the other arguments are as
  523. * expected. */
  524. static void
  525. mock_clock_skew_warning(const connection_t *conn, long apparent_skew,
  526. int trusted, log_domain_mask_t domain,
  527. const char *received, const char *source)
  528. {
  529. (void)conn;
  530. mock_apparent_skew = apparent_skew;
  531. tt_int_op(trusted, OP_EQ, 1);
  532. tt_int_op(domain, OP_EQ, LD_GENERAL);
  533. tt_str_op(received, OP_EQ, "microdesc flavor consensus");
  534. tt_str_op(source, OP_EQ, "CONSENSUS");
  535. done:
  536. ;
  537. }
  538. /** Do common setup for test_timely_consensus() and
  539. * test_early_consensus(). Call networkstatus_set_current_consensus()
  540. * on a constructed consensus and with an appropriately-modified
  541. * approx_time. Callers expect presence or absence of appropriate log
  542. * messages and control events. */
  543. static int
  544. test_skew_common(void *arg, time_t now, unsigned long *offset)
  545. {
  546. char *consensus = NULL;
  547. int retval = 0;
  548. *offset = strtoul(arg, NULL, 10);
  549. /* Initialize the SRV subsystem */
  550. MOCK(get_my_v3_authority_cert, get_my_v3_authority_cert_m);
  551. mock_cert = authority_cert_parse_from_string(AUTHORITY_CERT_1, NULL);
  552. sr_init(0);
  553. UNMOCK(get_my_v3_authority_cert);
  554. construct_consensus(&consensus, now);
  555. tt_assert(consensus);
  556. update_approx_time(now + *offset);
  557. mock_apparent_skew = 0;
  558. /* Caller will call UNMOCK() */
  559. MOCK(clock_skew_warning, mock_clock_skew_warning);
  560. /* Caller will call teardown_capture_of_logs() */
  561. setup_capture_of_logs(LOG_WARN);
  562. retval = networkstatus_set_current_consensus(consensus, "microdesc", 0,
  563. NULL);
  564. done:
  565. tor_free(consensus);
  566. return retval;
  567. }
  568. /** Test non-early consensus */
  569. static void
  570. test_timely_consensus(void *arg)
  571. {
  572. time_t now = time(NULL);
  573. unsigned long offset = 0;
  574. int retval = 0;
  575. retval = test_skew_common(arg, now, &offset);
  576. (void)offset;
  577. expect_no_log_msg_containing("behind the time published in the consensus");
  578. tt_int_op(retval, OP_EQ, 0);
  579. tt_int_op(mock_apparent_skew, OP_EQ, 0);
  580. done:
  581. teardown_capture_of_logs();
  582. UNMOCK(clock_skew_warning);
  583. }
  584. /** Test early consensus */
  585. static void
  586. test_early_consensus(void *arg)
  587. {
  588. time_t now = time(NULL);
  589. unsigned long offset = 0;
  590. int retval = 0;
  591. retval = test_skew_common(arg, now, &offset);
  592. /* Can't use expect_single_log_msg() because of unrecognized authorities */
  593. expect_log_msg_containing("behind the time published in the consensus");
  594. tt_int_op(retval, OP_EQ, 0);
  595. /* This depends on construct_consensus() setting valid_after=now+1000 */
  596. tt_int_op(mock_apparent_skew, OP_EQ, offset - 1000);
  597. done:
  598. teardown_capture_of_logs();
  599. UNMOCK(clock_skew_warning);
  600. }
  601. /** Test warn_early_consensus(), expecting no warning */
  602. static void
  603. test_warn_early_consensus_no(const networkstatus_t *c, time_t now,
  604. long offset)
  605. {
  606. mock_apparent_skew = 0;
  607. setup_capture_of_logs(LOG_WARN);
  608. warn_early_consensus(c, "microdesc", now + offset);
  609. expect_no_log_msg_containing("behind the time published in the consensus");
  610. tt_int_op(mock_apparent_skew, OP_EQ, 0);
  611. done:
  612. teardown_capture_of_logs();
  613. }
  614. /** Test warn_early_consensus(), expecting a warning */
  615. static void
  616. test_warn_early_consensus_yes(const networkstatus_t *c, time_t now,
  617. long offset)
  618. {
  619. mock_apparent_skew = 0;
  620. setup_capture_of_logs(LOG_WARN);
  621. warn_early_consensus(c, "microdesc", now + offset);
  622. /* Can't use expect_single_log_msg() because of unrecognized authorities */
  623. expect_log_msg_containing("behind the time published in the consensus");
  624. tt_int_op(mock_apparent_skew, OP_EQ, offset);
  625. done:
  626. teardown_capture_of_logs();
  627. }
  628. /**
  629. * Test warn_early_consensus() directly, checking both the non-warning
  630. * case (consensus is not early) and the warning case (consensus is
  631. * early). Depends on EARLY_CONSENSUS_NOTICE_SKEW=60.
  632. */
  633. static void
  634. test_warn_early_consensus(void *arg)
  635. {
  636. networkstatus_t *c = NULL;
  637. time_t now = time(NULL);
  638. (void)arg;
  639. c = tor_malloc_zero(sizeof *c);
  640. c->valid_after = now;
  641. c->dist_seconds = 300;
  642. mock_apparent_skew = 0;
  643. MOCK(clock_skew_warning, mock_clock_skew_warning);
  644. test_warn_early_consensus_no(c, now, 60);
  645. test_warn_early_consensus_no(c, now, 0);
  646. test_warn_early_consensus_no(c, now, -60);
  647. test_warn_early_consensus_no(c, now, -360);
  648. test_warn_early_consensus_yes(c, now, -361);
  649. test_warn_early_consensus_yes(c, now, -600);
  650. UNMOCK(clock_skew_warning);
  651. tor_free(c);
  652. }
  653. #define NODE(name, flags) \
  654. { #name, test_routerlist_##name, (flags), NULL, NULL }
  655. #define ROUTER(name,flags) \
  656. { #name, test_router_##name, (flags), NULL, NULL }
  657. #define TIMELY(name, arg) \
  658. { name, test_timely_consensus, TT_FORK, &passthrough_setup, \
  659. (char *)(arg) }
  660. #define EARLY(name, arg) \
  661. { name, test_early_consensus, TT_FORK, &passthrough_setup, \
  662. (char *)(arg) }
  663. struct testcase_t routerlist_tests[] = {
  664. NODE(initiate_descriptor_downloads, 0),
  665. NODE(launch_descriptor_downloads, 0),
  666. NODE(router_is_already_dir_fetching, TT_FORK),
  667. ROUTER(pick_directory_server_impl, TT_FORK),
  668. { "directory_guard_fetch_with_no_dirinfo",
  669. test_directory_guard_fetch_with_no_dirinfo, TT_FORK, NULL, NULL },
  670. /* These depend on construct_consensus() setting
  671. * valid_after=now+1000 and dist_seconds=250 */
  672. TIMELY("timely_consensus1", "1010"),
  673. TIMELY("timely_consensus2", "1000"),
  674. TIMELY("timely_consensus3", "690"),
  675. EARLY("early_consensus1", "689"),
  676. { "warn_early_consensus", test_warn_early_consensus, 0, NULL, NULL },
  677. END_OF_TESTCASES
  678. };