test_routerlist.c 18 KB

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