test_routerlist.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609
  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 DIRVOTE_PRIVATE
  7. #define ENTRYNODES_PRIVATE
  8. #define DIRECTORY_PRIVATE
  9. #define NETWORKSTATUS_PRIVATE
  10. #define CONNECTION_PRIVATE
  11. #define ROUTERLIST_PRIVATE
  12. #define TOR_UNIT_TESTING
  13. #include "or.h"
  14. #include "config.h"
  15. #include "connection.h"
  16. #include "container.h"
  17. #include "control.h"
  18. #include "directory.h"
  19. #include "dirvote.h"
  20. #include "entrynodes.h"
  21. #include "microdesc.h"
  22. #include "networkstatus.h"
  23. #include "nodelist.h"
  24. #include "policies.h"
  25. #include "router.h"
  26. #include "routerlist.h"
  27. #include "routerset.h"
  28. #include "routerparse.h"
  29. #include "shared_random.h"
  30. #include "statefile.h"
  31. #include "test.h"
  32. #include "test_dir_common.h"
  33. #include "log_test_helpers.h"
  34. void construct_consensus(char **consensus_text_md);
  35. static authority_cert_t *mock_cert;
  36. static authority_cert_t *
  37. get_my_v3_authority_cert_m(void)
  38. {
  39. tor_assert(mock_cert);
  40. return mock_cert;
  41. }
  42. /* 4 digests + 3 sep + pre + post + NULL */
  43. static char output[4*BASE64_DIGEST256_LEN+3+2+2+1];
  44. static void
  45. mock_get_from_dirserver(uint8_t dir_purpose, uint8_t router_purpose,
  46. const char *resource, int pds_flags,
  47. download_want_authority_t want_authority)
  48. {
  49. (void)dir_purpose;
  50. (void)router_purpose;
  51. (void)pds_flags;
  52. (void)want_authority;
  53. tt_assert(resource);
  54. strlcpy(output, resource, sizeof(output));
  55. done:
  56. ;
  57. }
  58. static void
  59. test_routerlist_initiate_descriptor_downloads(void *arg)
  60. {
  61. const char *prose = "unhurried and wise, we perceive.";
  62. smartlist_t *digests = smartlist_new();
  63. (void)arg;
  64. for (int i = 0; i < 20; i++) {
  65. smartlist_add(digests, (char*)prose);
  66. }
  67. MOCK(directory_get_from_dirserver, mock_get_from_dirserver);
  68. initiate_descriptor_downloads(NULL, DIR_PURPOSE_FETCH_MICRODESC,
  69. digests, 3, 7, 0);
  70. UNMOCK(directory_get_from_dirserver);
  71. tt_str_op(output, OP_EQ, "d/"
  72. "dW5odXJyaWVkIGFuZCB3aXNlLCB3ZSBwZXJjZWl2ZS4-"
  73. "dW5odXJyaWVkIGFuZCB3aXNlLCB3ZSBwZXJjZWl2ZS4-"
  74. "dW5odXJyaWVkIGFuZCB3aXNlLCB3ZSBwZXJjZWl2ZS4-"
  75. "dW5odXJyaWVkIGFuZCB3aXNlLCB3ZSBwZXJjZWl2ZS4"
  76. ".z");
  77. done:
  78. smartlist_free(digests);
  79. }
  80. static int count = 0;
  81. static void
  82. mock_initiate_descriptor_downloads(const routerstatus_t *source,
  83. int purpose, smartlist_t *digests,
  84. int lo, int hi, int pds_flags)
  85. {
  86. (void)source;
  87. (void)purpose;
  88. (void)digests;
  89. (void)pds_flags;
  90. (void)hi;
  91. (void)lo;
  92. count += 1;
  93. }
  94. static void
  95. test_routerlist_launch_descriptor_downloads(void *arg)
  96. {
  97. smartlist_t *downloadable = smartlist_new();
  98. time_t now = time(NULL);
  99. char *cp;
  100. (void)arg;
  101. for (int i = 0; i < 100; i++) {
  102. cp = tor_malloc(DIGEST256_LEN);
  103. tt_assert(cp);
  104. crypto_rand(cp, DIGEST256_LEN);
  105. smartlist_add(downloadable, cp);
  106. }
  107. MOCK(initiate_descriptor_downloads, mock_initiate_descriptor_downloads);
  108. launch_descriptor_downloads(DIR_PURPOSE_FETCH_MICRODESC, downloadable,
  109. NULL, now);
  110. tt_int_op(3, OP_EQ, count);
  111. UNMOCK(initiate_descriptor_downloads);
  112. done:
  113. SMARTLIST_FOREACH(downloadable, char *, cp1, tor_free(cp1));
  114. smartlist_free(downloadable);
  115. }
  116. void
  117. construct_consensus(char **consensus_text_md)
  118. {
  119. networkstatus_t *vote = NULL;
  120. networkstatus_t *v1 = NULL, *v2 = NULL, *v3 = NULL;
  121. networkstatus_voter_info_t *voter = NULL;
  122. authority_cert_t *cert1=NULL, *cert2=NULL, *cert3=NULL;
  123. crypto_pk_t *sign_skey_1=NULL, *sign_skey_2=NULL, *sign_skey_3=NULL;
  124. crypto_pk_t *sign_skey_leg=NULL;
  125. time_t now = time(NULL);
  126. smartlist_t *votes = NULL;
  127. int n_vrs;
  128. tt_assert(!dir_common_authority_pk_init(&cert1, &cert2, &cert3,
  129. &sign_skey_1, &sign_skey_2,
  130. &sign_skey_3));
  131. sign_skey_leg = pk_generate(4);
  132. dir_common_construct_vote_1(&vote, cert1, sign_skey_1,
  133. &dir_common_gen_routerstatus_for_v3ns,
  134. &v1, &n_vrs, now, 1);
  135. networkstatus_vote_free(vote);
  136. tt_assert(v1);
  137. tt_int_op(n_vrs, OP_EQ, 4);
  138. tt_int_op(smartlist_len(v1->routerstatus_list), OP_EQ, 4);
  139. dir_common_construct_vote_2(&vote, cert2, sign_skey_2,
  140. &dir_common_gen_routerstatus_for_v3ns,
  141. &v2, &n_vrs, now, 1);
  142. networkstatus_vote_free(vote);
  143. tt_assert(v2);
  144. tt_int_op(n_vrs, OP_EQ, 4);
  145. tt_int_op(smartlist_len(v2->routerstatus_list), OP_EQ, 4);
  146. dir_common_construct_vote_3(&vote, cert3, sign_skey_3,
  147. &dir_common_gen_routerstatus_for_v3ns,
  148. &v3, &n_vrs, now, 1);
  149. tt_assert(v3);
  150. tt_int_op(n_vrs, OP_EQ, 4);
  151. tt_int_op(smartlist_len(v3->routerstatus_list), OP_EQ, 4);
  152. networkstatus_vote_free(vote);
  153. votes = smartlist_new();
  154. smartlist_add(votes, v1);
  155. smartlist_add(votes, v2);
  156. smartlist_add(votes, v3);
  157. *consensus_text_md = networkstatus_compute_consensus(votes, 3,
  158. cert1->identity_key,
  159. sign_skey_1,
  160. "AAAAAAAAAAAAAAAAAAAA",
  161. sign_skey_leg,
  162. FLAV_MICRODESC);
  163. tt_assert(*consensus_text_md);
  164. done:
  165. tor_free(voter);
  166. networkstatus_vote_free(v1);
  167. networkstatus_vote_free(v2);
  168. networkstatus_vote_free(v3);
  169. smartlist_free(votes);
  170. authority_cert_free(cert1);
  171. authority_cert_free(cert2);
  172. authority_cert_free(cert3);
  173. crypto_pk_free(sign_skey_1);
  174. crypto_pk_free(sign_skey_2);
  175. crypto_pk_free(sign_skey_3);
  176. crypto_pk_free(sign_skey_leg);
  177. }
  178. static int mock_usable_consensus_flavor_value = FLAV_NS;
  179. static int
  180. mock_usable_consensus_flavor(void)
  181. {
  182. return mock_usable_consensus_flavor_value;
  183. }
  184. static void
  185. test_router_pick_directory_server_impl(void *arg)
  186. {
  187. (void)arg;
  188. networkstatus_t *con_md = NULL;
  189. char *consensus_text_md = NULL;
  190. int flags = PDS_IGNORE_FASCISTFIREWALL|PDS_RETRY_IF_NO_SERVERS;
  191. or_options_t *options = get_options_mutable();
  192. const routerstatus_t *rs = NULL;
  193. options->UseMicrodescriptors = 1;
  194. char *router1_id = NULL, *router2_id = NULL, *router3_id = NULL;
  195. node_t *node_router1 = NULL, *node_router2 = NULL, *node_router3 = NULL;
  196. config_line_t *policy_line = NULL;
  197. time_t now = time(NULL);
  198. int tmp_dirport1, tmp_dirport3;
  199. (void)arg;
  200. MOCK(usable_consensus_flavor, mock_usable_consensus_flavor);
  201. /* With no consensus, we must be bootstrapping, regardless of time or flavor
  202. */
  203. mock_usable_consensus_flavor_value = FLAV_NS;
  204. tt_assert(networkstatus_consensus_is_bootstrapping(now));
  205. tt_assert(networkstatus_consensus_is_bootstrapping(now + 2000));
  206. tt_assert(networkstatus_consensus_is_bootstrapping(now + 2*24*60*60));
  207. tt_assert(networkstatus_consensus_is_bootstrapping(now - 2*24*60*60));
  208. mock_usable_consensus_flavor_value = FLAV_MICRODESC;
  209. tt_assert(networkstatus_consensus_is_bootstrapping(now));
  210. tt_assert(networkstatus_consensus_is_bootstrapping(now + 2000));
  211. tt_assert(networkstatus_consensus_is_bootstrapping(now + 2*24*60*60));
  212. tt_assert(networkstatus_consensus_is_bootstrapping(now - 2*24*60*60));
  213. /* Init SR subsystem. */
  214. MOCK(get_my_v3_authority_cert, get_my_v3_authority_cert_m);
  215. mock_cert = authority_cert_parse_from_string(AUTHORITY_CERT_1, NULL);
  216. sr_init(0);
  217. UNMOCK(get_my_v3_authority_cert);
  218. /* No consensus available, fail early */
  219. rs = router_pick_directory_server_impl(V3_DIRINFO, (const int) 0, NULL);
  220. tt_ptr_op(rs, OP_EQ, NULL);
  221. construct_consensus(&consensus_text_md);
  222. tt_assert(consensus_text_md);
  223. con_md = networkstatus_parse_vote_from_string(consensus_text_md, NULL,
  224. NS_TYPE_CONSENSUS);
  225. tt_assert(con_md);
  226. tt_int_op(con_md->flavor,OP_EQ, FLAV_MICRODESC);
  227. tt_assert(con_md->routerstatus_list);
  228. tt_int_op(smartlist_len(con_md->routerstatus_list), OP_EQ, 3);
  229. tt_assert(!networkstatus_set_current_consensus_from_ns(con_md,
  230. "microdesc"));
  231. /* If the consensus time or flavor doesn't match, we are still
  232. * bootstrapping */
  233. mock_usable_consensus_flavor_value = FLAV_NS;
  234. tt_assert(networkstatus_consensus_is_bootstrapping(now));
  235. tt_assert(networkstatus_consensus_is_bootstrapping(now + 2000));
  236. tt_assert(networkstatus_consensus_is_bootstrapping(now + 2*24*60*60));
  237. tt_assert(networkstatus_consensus_is_bootstrapping(now - 2*24*60*60));
  238. /* With a valid consensus for the current time and flavor, we stop
  239. * bootstrapping, even if we have no certificates */
  240. mock_usable_consensus_flavor_value = FLAV_MICRODESC;
  241. tt_assert(!networkstatus_consensus_is_bootstrapping(now + 2000));
  242. tt_assert(!networkstatus_consensus_is_bootstrapping(con_md->valid_after));
  243. tt_assert(!networkstatus_consensus_is_bootstrapping(con_md->valid_until));
  244. tt_assert(!networkstatus_consensus_is_bootstrapping(con_md->valid_until
  245. + 24*60*60));
  246. /* These times are outside the test validity period */
  247. tt_assert(networkstatus_consensus_is_bootstrapping(now));
  248. tt_assert(networkstatus_consensus_is_bootstrapping(now + 2*24*60*60));
  249. tt_assert(networkstatus_consensus_is_bootstrapping(now - 2*24*60*60));
  250. nodelist_set_consensus(con_md);
  251. nodelist_assert_ok();
  252. rs = router_pick_directory_server_impl(V3_DIRINFO, flags, NULL);
  253. /* We should not fail now we have a consensus and routerstatus_list
  254. * and nodelist are populated. */
  255. tt_ptr_op(rs, OP_NE, NULL);
  256. /* Manipulate the nodes so we get the dir server we expect */
  257. router1_id = tor_malloc(DIGEST_LEN);
  258. memset(router1_id, TEST_DIR_ROUTER_ID_1, DIGEST_LEN);
  259. router2_id = tor_malloc(DIGEST_LEN);
  260. memset(router2_id, TEST_DIR_ROUTER_ID_2, DIGEST_LEN);
  261. router3_id = tor_malloc(DIGEST_LEN);
  262. memset(router3_id, TEST_DIR_ROUTER_ID_3, DIGEST_LEN);
  263. node_router1 = node_get_mutable_by_id(router1_id);
  264. node_router2 = node_get_mutable_by_id(router2_id);
  265. node_router3 = node_get_mutable_by_id(router3_id);
  266. node_router1->is_possible_guard = 1;
  267. node_router1->is_running = 0;
  268. node_router3->is_running = 0;
  269. rs = router_pick_directory_server_impl(V3_DIRINFO, flags, NULL);
  270. tt_ptr_op(rs, OP_NE, NULL);
  271. tt_assert(tor_memeq(rs->identity_digest, router2_id, DIGEST_LEN));
  272. rs = NULL;
  273. node_router1->is_running = 1;
  274. node_router3->is_running = 1;
  275. node_router1->rs->is_v2_dir = 0;
  276. node_router3->rs->is_v2_dir = 0;
  277. tmp_dirport1 = node_router1->rs->dir_port;
  278. tmp_dirport3 = node_router3->rs->dir_port;
  279. node_router1->rs->dir_port = 0;
  280. node_router3->rs->dir_port = 0;
  281. rs = router_pick_directory_server_impl(V3_DIRINFO, flags, NULL);
  282. tt_ptr_op(rs, OP_NE, NULL);
  283. tt_assert(tor_memeq(rs->identity_digest, router2_id, DIGEST_LEN));
  284. rs = NULL;
  285. node_router1->rs->is_v2_dir = 1;
  286. node_router3->rs->is_v2_dir = 1;
  287. node_router1->rs->dir_port = tmp_dirport1;
  288. node_router3->rs->dir_port = tmp_dirport3;
  289. node_router1->is_valid = 0;
  290. node_router3->is_valid = 0;
  291. rs = router_pick_directory_server_impl(V3_DIRINFO, flags, NULL);
  292. tt_ptr_op(rs, OP_NE, NULL);
  293. tt_assert(tor_memeq(rs->identity_digest, router2_id, DIGEST_LEN));
  294. rs = NULL;
  295. node_router1->is_valid = 1;
  296. node_router3->is_valid = 1;
  297. /* Manipulate overloaded */
  298. node_router2->rs->last_dir_503_at = now;
  299. node_router3->rs->last_dir_503_at = now;
  300. rs = router_pick_directory_server_impl(V3_DIRINFO, flags, NULL);
  301. tt_ptr_op(rs, OP_NE, NULL);
  302. tt_assert(tor_memeq(rs->identity_digest, router1_id, DIGEST_LEN));
  303. node_router2->rs->last_dir_503_at = 0;
  304. node_router3->rs->last_dir_503_at = 0;
  305. /* Set a Fascist firewall */
  306. flags &= ~ PDS_IGNORE_FASCISTFIREWALL;
  307. policy_line = tor_malloc_zero(sizeof(config_line_t));
  308. policy_line->key = tor_strdup("ReachableORAddresses");
  309. policy_line->value = tor_strdup("accept *:442, reject *:*");
  310. options->ReachableORAddresses = policy_line;
  311. policies_parse_from_options(options);
  312. node_router1->rs->or_port = 444;
  313. node_router2->rs->or_port = 443;
  314. node_router3->rs->or_port = 442;
  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, router3_id, DIGEST_LEN));
  318. node_router1->rs->or_port = 442;
  319. node_router2->rs->or_port = 443;
  320. node_router3->rs->or_port = 444;
  321. rs = router_pick_directory_server_impl(V3_DIRINFO, flags, NULL);
  322. tt_ptr_op(rs, OP_NE, NULL);
  323. tt_assert(tor_memeq(rs->identity_digest, router1_id, DIGEST_LEN));
  324. /* Fascist firewall and overloaded */
  325. node_router1->rs->or_port = 442;
  326. node_router2->rs->or_port = 443;
  327. node_router3->rs->or_port = 442;
  328. node_router3->rs->last_dir_503_at = now;
  329. rs = router_pick_directory_server_impl(V3_DIRINFO, flags, NULL);
  330. tt_ptr_op(rs, OP_NE, NULL);
  331. tt_assert(tor_memeq(rs->identity_digest, router1_id, DIGEST_LEN));
  332. node_router3->rs->last_dir_503_at = 0;
  333. /* Fascists against OR and Dir */
  334. policy_line = tor_malloc_zero(sizeof(config_line_t));
  335. policy_line->key = tor_strdup("ReachableAddresses");
  336. policy_line->value = tor_strdup("accept *:80, reject *:*");
  337. options->ReachableDirAddresses = policy_line;
  338. policies_parse_from_options(options);
  339. node_router1->rs->or_port = 442;
  340. node_router2->rs->or_port = 441;
  341. node_router3->rs->or_port = 443;
  342. node_router1->rs->dir_port = 80;
  343. node_router2->rs->dir_port = 80;
  344. node_router3->rs->dir_port = 81;
  345. node_router1->rs->last_dir_503_at = now;
  346. rs = router_pick_directory_server_impl(V3_DIRINFO, flags, NULL);
  347. tt_ptr_op(rs, OP_NE, NULL);
  348. tt_assert(tor_memeq(rs->identity_digest, router1_id, DIGEST_LEN));
  349. node_router1->rs->last_dir_503_at = 0;
  350. done:
  351. UNMOCK(usable_consensus_flavor);
  352. if (router1_id)
  353. tor_free(router1_id);
  354. if (router2_id)
  355. tor_free(router2_id);
  356. if (router3_id)
  357. tor_free(router3_id);
  358. if (options->ReachableORAddresses ||
  359. options->ReachableDirAddresses)
  360. policies_free_all();
  361. tor_free(consensus_text_md);
  362. networkstatus_vote_free(con_md);
  363. }
  364. static or_state_t *dummy_state = NULL;
  365. static or_state_t *
  366. get_or_state_replacement(void)
  367. {
  368. return dummy_state;
  369. }
  370. static void
  371. mock_directory_initiate_request(directory_request_t *req)
  372. {
  373. (void)req;
  374. return;
  375. }
  376. static circuit_guard_state_t *
  377. mock_circuit_guard_state_new(entry_guard_t *guard, unsigned state,
  378. entry_guard_restriction_t *rst)
  379. {
  380. (void) guard;
  381. (void) state;
  382. (void) rst;
  383. return NULL;
  384. }
  385. /** Test that we will use our directory guards to fetch mds even if we don't
  386. * have any dirinfo (tests bug #23862). */
  387. static void
  388. test_directory_guard_fetch_with_no_dirinfo(void *arg)
  389. {
  390. int retval;
  391. char *consensus_text_md = NULL;
  392. or_options_t *options = get_options_mutable();
  393. (void) arg;
  394. /* Initialize the SRV subsystem */
  395. sr_init(0);
  396. /* Initialize the entry node configuration from the ticket */
  397. options->UseEntryGuards = 1;
  398. options->StrictNodes = 1;
  399. get_options_mutable()->EntryNodes = routerset_new();
  400. routerset_parse(get_options_mutable()->EntryNodes,
  401. "2121212121212121212121212121212121212121", "foo");
  402. /* Mock some functions */
  403. dummy_state = tor_malloc_zero(sizeof(or_state_t));
  404. MOCK(get_or_state, get_or_state_replacement);
  405. MOCK(directory_initiate_request, mock_directory_initiate_request);
  406. /* we need to mock this one to avoid memleaks */
  407. MOCK(circuit_guard_state_new, mock_circuit_guard_state_new);
  408. /* Call guards_update_all() to simulate loading our state file (see
  409. * entry_guards_load_guards_from_state() and ticket #23989). */
  410. guards_update_all();
  411. /* Test logic: Simulate the arrival of a new consensus when we have no
  412. * dirinfo at all. Tor will need to fetch the mds from the consensus. Make
  413. * sure that Tor will use the specified entry guard instead of relying on the
  414. * fallback directories. */
  415. /* Fixup the dirconn that will deliver the consensus */
  416. dir_connection_t *conn = dir_connection_new(AF_INET);
  417. tor_addr_from_ipv4h(&conn->base_.addr, 0x7f000001);
  418. conn->base_.port = 8800;
  419. TO_CONN(conn)->address = tor_strdup("127.0.0.1");
  420. conn->base_.purpose = DIR_PURPOSE_FETCH_CONSENSUS;
  421. conn->requested_resource = tor_strdup("ns");
  422. /* Construct a consensus */
  423. construct_consensus(&consensus_text_md);
  424. tt_assert(consensus_text_md);
  425. /* Place the consensus in the dirconn */
  426. response_handler_args_t args;
  427. memset(&args, 0, sizeof(response_handler_args_t));
  428. args.status_code = 200;
  429. args.body = consensus_text_md;
  430. args.body_len = strlen(consensus_text_md);
  431. /* Update approx time so that the consensus is considered live */
  432. update_approx_time(time(NULL)+1010);
  433. setup_capture_of_logs(LOG_DEBUG);
  434. /* Now handle the consensus */
  435. retval = handle_response_fetch_consensus(conn, &args);
  436. tt_int_op(retval, OP_EQ, 0);
  437. /* Make sure that our primary guard was chosen */
  438. /* BUG #23862 falls back to dirserver list!! */
  439. expect_log_msg_containing("falling back to dirserver list");
  440. done:
  441. tor_free(consensus_text_md);
  442. tor_free(dummy_state);
  443. connection_free_(TO_CONN(conn));
  444. entry_guards_free_all();
  445. teardown_capture_of_logs();
  446. }
  447. static connection_t *mocked_connection = NULL;
  448. /* Mock connection_get_by_type_addr_port_purpose by returning
  449. * mocked_connection. */
  450. static connection_t *
  451. mock_connection_get_by_type_addr_port_purpose(int type,
  452. const tor_addr_t *addr,
  453. uint16_t port, int purpose)
  454. {
  455. (void)type;
  456. (void)addr;
  457. (void)port;
  458. (void)purpose;
  459. return mocked_connection;
  460. }
  461. #define TEST_ADDR_STR "127.0.0.1"
  462. #define TEST_DIR_PORT 12345
  463. static void
  464. test_routerlist_router_is_already_dir_fetching(void *arg)
  465. {
  466. (void)arg;
  467. tor_addr_port_t test_ap, null_addr_ap, zero_port_ap;
  468. /* Setup */
  469. tor_addr_parse(&test_ap.addr, TEST_ADDR_STR);
  470. test_ap.port = TEST_DIR_PORT;
  471. tor_addr_make_null(&null_addr_ap.addr, AF_INET6);
  472. null_addr_ap.port = TEST_DIR_PORT;
  473. tor_addr_parse(&zero_port_ap.addr, TEST_ADDR_STR);
  474. zero_port_ap.port = 0;
  475. MOCK(connection_get_by_type_addr_port_purpose,
  476. mock_connection_get_by_type_addr_port_purpose);
  477. /* Test that we never get 1 from a NULL connection */
  478. mocked_connection = NULL;
  479. tt_int_op(router_is_already_dir_fetching(&test_ap, 1, 1), OP_EQ, 0);
  480. tt_int_op(router_is_already_dir_fetching(&test_ap, 1, 0), OP_EQ, 0);
  481. tt_int_op(router_is_already_dir_fetching(&test_ap, 0, 1), OP_EQ, 0);
  482. /* We always expect 0 in these cases */
  483. tt_int_op(router_is_already_dir_fetching(&test_ap, 0, 0), OP_EQ, 0);
  484. tt_int_op(router_is_already_dir_fetching(NULL, 1, 1), OP_EQ, 0);
  485. tt_int_op(router_is_already_dir_fetching(&null_addr_ap, 1, 1), OP_EQ, 0);
  486. tt_int_op(router_is_already_dir_fetching(&zero_port_ap, 1, 1), OP_EQ, 0);
  487. /* Test that we get 1 with a connection in the appropriate circumstances */
  488. mocked_connection = connection_new(CONN_TYPE_DIR, AF_INET);
  489. tt_int_op(router_is_already_dir_fetching(&test_ap, 1, 1), OP_EQ, 1);
  490. tt_int_op(router_is_already_dir_fetching(&test_ap, 1, 0), OP_EQ, 1);
  491. tt_int_op(router_is_already_dir_fetching(&test_ap, 0, 1), OP_EQ, 1);
  492. /* Test that we get 0 even with a connection in the appropriate
  493. * circumstances */
  494. tt_int_op(router_is_already_dir_fetching(&test_ap, 0, 0), OP_EQ, 0);
  495. tt_int_op(router_is_already_dir_fetching(NULL, 1, 1), OP_EQ, 0);
  496. tt_int_op(router_is_already_dir_fetching(&null_addr_ap, 1, 1), OP_EQ, 0);
  497. tt_int_op(router_is_already_dir_fetching(&zero_port_ap, 1, 1), OP_EQ, 0);
  498. done:
  499. /* If a connection is never set up, connection_free chokes on it. */
  500. if (mocked_connection) {
  501. buf_free(mocked_connection->inbuf);
  502. buf_free(mocked_connection->outbuf);
  503. }
  504. tor_free(mocked_connection);
  505. UNMOCK(connection_get_by_type_addr_port_purpose);
  506. }
  507. #undef TEST_ADDR_STR
  508. #undef TEST_DIR_PORT
  509. #define NODE(name, flags) \
  510. { #name, test_routerlist_##name, (flags), NULL, NULL }
  511. #define ROUTER(name,flags) \
  512. { #name, test_router_##name, (flags), NULL, NULL }
  513. struct testcase_t routerlist_tests[] = {
  514. NODE(initiate_descriptor_downloads, 0),
  515. NODE(launch_descriptor_downloads, 0),
  516. NODE(router_is_already_dir_fetching, TT_FORK),
  517. ROUTER(pick_directory_server_impl, TT_FORK),
  518. { "directory_guard_fetch_with_no_dirinfo",
  519. test_directory_guard_fetch_with_no_dirinfo, TT_FORK, NULL, NULL },
  520. END_OF_TESTCASES
  521. };