test_routerlist.c 25 KB

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