test_routerlist.c 26 KB

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