test_routerlist.c 26 KB

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