test_routerlist.c 20 KB

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