test_routerlist.c 25 KB

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