test_routerlist.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583
  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. #ifdef ENABLE_LEGACY_GUARD_ALGORITHM
  178. static smartlist_t *mock_is_guard_list = NULL;
  179. static int
  180. mock_is_node_used_as_guard(const node_t *n)
  181. {
  182. if (mock_is_guard_list) {
  183. SMARTLIST_FOREACH_BEGIN(mock_is_guard_list, node_t *, e) {
  184. if (e == n) return 1;
  185. } SMARTLIST_FOREACH_END(e);
  186. }
  187. return 0;
  188. }
  189. static void
  190. mark_node_used_as_guard(node_t *n)
  191. {
  192. if (!n) return;
  193. if (!mock_is_guard_list) {
  194. mock_is_guard_list = smartlist_new();
  195. }
  196. if (!mock_is_node_used_as_guard(n)) {
  197. smartlist_add(mock_is_guard_list, n);
  198. }
  199. }
  200. static void
  201. mark_node_unused_as_guard(node_t *n)
  202. {
  203. if (!n) return;
  204. if (!mock_is_guard_list) return;
  205. smartlist_remove(mock_is_guard_list, n);
  206. }
  207. static void
  208. clear_mock_guard_list(void)
  209. {
  210. if (mock_is_guard_list) {
  211. smartlist_free(mock_is_guard_list);
  212. mock_is_guard_list = NULL;
  213. }
  214. }
  215. #endif
  216. static void
  217. test_router_pick_directory_server_impl(void *arg)
  218. {
  219. (void)arg;
  220. networkstatus_t *con_md = NULL;
  221. char *consensus_text_md = NULL;
  222. int flags = PDS_IGNORE_FASCISTFIREWALL|PDS_RETRY_IF_NO_SERVERS;
  223. or_options_t *options = get_options_mutable();
  224. const routerstatus_t *rs = NULL;
  225. options->UseMicrodescriptors = 1;
  226. char *router1_id = NULL, *router2_id = NULL, *router3_id = NULL;
  227. node_t *node_router1 = NULL, *node_router2 = NULL, *node_router3 = NULL;
  228. config_line_t *policy_line = NULL;
  229. time_t now = time(NULL);
  230. int tmp_dirport1, tmp_dirport3;
  231. (void)arg;
  232. MOCK(usable_consensus_flavor, mock_usable_consensus_flavor);
  233. #ifdef ENABLE_LEGACY_GUARD_ALGORITHM
  234. MOCK(is_node_used_as_guard, mock_is_node_used_as_guard);
  235. #endif
  236. /* With no consensus, we must be bootstrapping, regardless of time or flavor
  237. */
  238. mock_usable_consensus_flavor_value = FLAV_NS;
  239. tt_assert(networkstatus_consensus_is_bootstrapping(now));
  240. tt_assert(networkstatus_consensus_is_bootstrapping(now + 2000));
  241. tt_assert(networkstatus_consensus_is_bootstrapping(now + 2*24*60*60));
  242. tt_assert(networkstatus_consensus_is_bootstrapping(now - 2*24*60*60));
  243. mock_usable_consensus_flavor_value = FLAV_MICRODESC;
  244. tt_assert(networkstatus_consensus_is_bootstrapping(now));
  245. tt_assert(networkstatus_consensus_is_bootstrapping(now + 2000));
  246. tt_assert(networkstatus_consensus_is_bootstrapping(now + 2*24*60*60));
  247. tt_assert(networkstatus_consensus_is_bootstrapping(now - 2*24*60*60));
  248. /* Init SR subsystem. */
  249. MOCK(get_my_v3_authority_cert, get_my_v3_authority_cert_m);
  250. mock_cert = authority_cert_parse_from_string(AUTHORITY_CERT_1, NULL);
  251. sr_init(0);
  252. UNMOCK(get_my_v3_authority_cert);
  253. /* No consensus available, fail early */
  254. rs = router_pick_directory_server_impl(V3_DIRINFO, (const int) 0, NULL);
  255. tt_assert(rs == NULL);
  256. construct_consensus(&consensus_text_md);
  257. tt_assert(consensus_text_md);
  258. con_md = networkstatus_parse_vote_from_string(consensus_text_md, NULL,
  259. NS_TYPE_CONSENSUS);
  260. tt_assert(con_md);
  261. tt_int_op(con_md->flavor,==, FLAV_MICRODESC);
  262. tt_assert(con_md->routerstatus_list);
  263. tt_int_op(smartlist_len(con_md->routerstatus_list), ==, 3);
  264. tt_assert(!networkstatus_set_current_consensus_from_ns(con_md,
  265. "microdesc"));
  266. /* If the consensus time or flavor doesn't match, we are still
  267. * bootstrapping */
  268. mock_usable_consensus_flavor_value = FLAV_NS;
  269. tt_assert(networkstatus_consensus_is_bootstrapping(now));
  270. tt_assert(networkstatus_consensus_is_bootstrapping(now + 2000));
  271. tt_assert(networkstatus_consensus_is_bootstrapping(now + 2*24*60*60));
  272. tt_assert(networkstatus_consensus_is_bootstrapping(now - 2*24*60*60));
  273. /* With a valid consensus for the current time and flavor, we stop
  274. * bootstrapping, even if we have no certificates */
  275. mock_usable_consensus_flavor_value = FLAV_MICRODESC;
  276. tt_assert(!networkstatus_consensus_is_bootstrapping(now + 2000));
  277. tt_assert(!networkstatus_consensus_is_bootstrapping(con_md->valid_after));
  278. tt_assert(!networkstatus_consensus_is_bootstrapping(con_md->valid_until));
  279. tt_assert(!networkstatus_consensus_is_bootstrapping(con_md->valid_until
  280. + 24*60*60));
  281. /* These times are outside the test validity period */
  282. tt_assert(networkstatus_consensus_is_bootstrapping(now));
  283. tt_assert(networkstatus_consensus_is_bootstrapping(now + 2*24*60*60));
  284. tt_assert(networkstatus_consensus_is_bootstrapping(now - 2*24*60*60));
  285. nodelist_set_consensus(con_md);
  286. nodelist_assert_ok();
  287. rs = router_pick_directory_server_impl(V3_DIRINFO, flags, NULL);
  288. /* We should not fail now we have a consensus and routerstatus_list
  289. * and nodelist are populated. */
  290. tt_assert(rs != NULL);
  291. /* Manipulate the nodes so we get the dir server we expect */
  292. router1_id = tor_malloc(DIGEST_LEN);
  293. memset(router1_id, TEST_DIR_ROUTER_ID_1, DIGEST_LEN);
  294. router2_id = tor_malloc(DIGEST_LEN);
  295. memset(router2_id, TEST_DIR_ROUTER_ID_2, DIGEST_LEN);
  296. router3_id = tor_malloc(DIGEST_LEN);
  297. memset(router3_id, TEST_DIR_ROUTER_ID_3, DIGEST_LEN);
  298. node_router1 = node_get_mutable_by_id(router1_id);
  299. node_router2 = node_get_mutable_by_id(router2_id);
  300. node_router3 = node_get_mutable_by_id(router3_id);
  301. node_router1->is_possible_guard = 1;
  302. node_router1->is_running = 0;
  303. node_router3->is_running = 0;
  304. rs = router_pick_directory_server_impl(V3_DIRINFO, flags, NULL);
  305. tt_assert(rs != NULL);
  306. tt_assert(tor_memeq(rs->identity_digest, router2_id, DIGEST_LEN));
  307. rs = NULL;
  308. node_router1->is_running = 1;
  309. node_router3->is_running = 1;
  310. node_router1->rs->is_v2_dir = 0;
  311. node_router3->rs->is_v2_dir = 0;
  312. tmp_dirport1 = node_router1->rs->dir_port;
  313. tmp_dirport3 = node_router3->rs->dir_port;
  314. node_router1->rs->dir_port = 0;
  315. node_router3->rs->dir_port = 0;
  316. rs = router_pick_directory_server_impl(V3_DIRINFO, flags, NULL);
  317. tt_assert(rs != NULL);
  318. tt_assert(tor_memeq(rs->identity_digest, router2_id, DIGEST_LEN));
  319. rs = NULL;
  320. node_router1->rs->is_v2_dir = 1;
  321. node_router3->rs->is_v2_dir = 1;
  322. node_router1->rs->dir_port = tmp_dirport1;
  323. node_router3->rs->dir_port = tmp_dirport3;
  324. node_router1->is_valid = 0;
  325. node_router3->is_valid = 0;
  326. rs = router_pick_directory_server_impl(V3_DIRINFO, flags, NULL);
  327. tt_assert(rs != NULL);
  328. tt_assert(tor_memeq(rs->identity_digest, router2_id, DIGEST_LEN));
  329. rs = NULL;
  330. node_router1->is_valid = 1;
  331. node_router3->is_valid = 1;
  332. #ifdef ENABLE_LEGACY_GUARD_ALGORITHM
  333. flags |= PDS_FOR_GUARD;
  334. mark_node_used_as_guard(node_router1);
  335. mark_node_used_as_guard(node_router2);
  336. mark_node_used_as_guard(node_router3);
  337. rs = router_pick_directory_server_impl(V3_DIRINFO, flags, NULL);
  338. tt_assert(rs == NULL);
  339. mark_node_unused_as_guard(node_router1);
  340. rs = router_pick_directory_server_impl(V3_DIRINFO, flags, NULL);
  341. tt_assert(rs != NULL);
  342. tt_assert(tor_memeq(rs->identity_digest, router1_id, DIGEST_LEN));
  343. rs = NULL;
  344. mark_node_unused_as_guard(node_router2);
  345. mark_node_unused_as_guard(node_router3);
  346. #endif
  347. /* One not valid, one guard. This should leave one remaining */
  348. #ifdef ENABLE_LEGACY_GUARD_ALGORITHM
  349. node_router1->is_valid = 0;
  350. mark_node_used_as_guard(node_router2);
  351. rs = router_pick_directory_server_impl(V3_DIRINFO, flags, NULL);
  352. tt_assert(rs != NULL);
  353. tt_assert(tor_memeq(rs->identity_digest, router3_id, DIGEST_LEN));
  354. rs = NULL;
  355. node_router1->is_valid = 1;
  356. mark_node_unused_as_guard(node_router2);
  357. #endif
  358. /* Manipulate overloaded */
  359. node_router2->rs->last_dir_503_at = now;
  360. node_router3->rs->last_dir_503_at = now;
  361. rs = router_pick_directory_server_impl(V3_DIRINFO, flags, NULL);
  362. tt_assert(rs != NULL);
  363. tt_assert(tor_memeq(rs->identity_digest, router1_id, DIGEST_LEN));
  364. node_router2->rs->last_dir_503_at = 0;
  365. node_router3->rs->last_dir_503_at = 0;
  366. /* Set a Fascist firewall */
  367. flags &= ~ PDS_IGNORE_FASCISTFIREWALL;
  368. policy_line = tor_malloc_zero(sizeof(config_line_t));
  369. policy_line->key = tor_strdup("ReachableORAddresses");
  370. policy_line->value = tor_strdup("accept *:442, reject *:*");
  371. options->ReachableORAddresses = policy_line;
  372. policies_parse_from_options(options);
  373. node_router1->rs->or_port = 444;
  374. node_router2->rs->or_port = 443;
  375. node_router3->rs->or_port = 442;
  376. rs = router_pick_directory_server_impl(V3_DIRINFO, flags, NULL);
  377. tt_assert(rs != NULL);
  378. tt_assert(tor_memeq(rs->identity_digest, router3_id, DIGEST_LEN));
  379. node_router1->rs->or_port = 442;
  380. node_router2->rs->or_port = 443;
  381. node_router3->rs->or_port = 444;
  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. /* Fascist firewall and overloaded */
  386. node_router1->rs->or_port = 442;
  387. node_router2->rs->or_port = 443;
  388. node_router3->rs->or_port = 442;
  389. node_router3->rs->last_dir_503_at = now;
  390. rs = router_pick_directory_server_impl(V3_DIRINFO, flags, NULL);
  391. tt_assert(rs != NULL);
  392. tt_assert(tor_memeq(rs->identity_digest, router1_id, DIGEST_LEN));
  393. node_router3->rs->last_dir_503_at = 0;
  394. /* Fascists against OR and Dir */
  395. policy_line = tor_malloc_zero(sizeof(config_line_t));
  396. policy_line->key = tor_strdup("ReachableAddresses");
  397. policy_line->value = tor_strdup("accept *:80, reject *:*");
  398. options->ReachableDirAddresses = policy_line;
  399. policies_parse_from_options(options);
  400. node_router1->rs->or_port = 442;
  401. node_router2->rs->or_port = 441;
  402. node_router3->rs->or_port = 443;
  403. node_router1->rs->dir_port = 80;
  404. node_router2->rs->dir_port = 80;
  405. node_router3->rs->dir_port = 81;
  406. node_router1->rs->last_dir_503_at = now;
  407. rs = router_pick_directory_server_impl(V3_DIRINFO, flags, NULL);
  408. tt_assert(rs != NULL);
  409. tt_assert(tor_memeq(rs->identity_digest, router1_id, DIGEST_LEN));
  410. node_router1->rs->last_dir_503_at = 0;
  411. done:
  412. UNMOCK(usable_consensus_flavor);
  413. #ifdef ENABLE_LEGACY_GUARD_ALGORITHM
  414. UNMOCK(is_node_used_as_guard);
  415. clear_mock_guard_list();
  416. #endif
  417. if (router1_id)
  418. tor_free(router1_id);
  419. if (router2_id)
  420. tor_free(router2_id);
  421. if (router3_id)
  422. tor_free(router3_id);
  423. if (options->ReachableORAddresses ||
  424. options->ReachableDirAddresses)
  425. policies_free_all();
  426. tor_free(consensus_text_md);
  427. networkstatus_vote_free(con_md);
  428. }
  429. static connection_t *mocked_connection = NULL;
  430. /* Mock connection_get_by_type_addr_port_purpose by returning
  431. * mocked_connection. */
  432. static connection_t *
  433. mock_connection_get_by_type_addr_port_purpose(int type,
  434. const tor_addr_t *addr,
  435. uint16_t port, int purpose)
  436. {
  437. (void)type;
  438. (void)addr;
  439. (void)port;
  440. (void)purpose;
  441. return mocked_connection;
  442. }
  443. #define TEST_ADDR_STR "127.0.0.1"
  444. #define TEST_DIR_PORT 12345
  445. static void
  446. test_routerlist_router_is_already_dir_fetching(void *arg)
  447. {
  448. (void)arg;
  449. tor_addr_port_t test_ap, null_addr_ap, zero_port_ap;
  450. /* Setup */
  451. tor_addr_parse(&test_ap.addr, TEST_ADDR_STR);
  452. test_ap.port = TEST_DIR_PORT;
  453. tor_addr_make_null(&null_addr_ap.addr, AF_INET6);
  454. null_addr_ap.port = TEST_DIR_PORT;
  455. tor_addr_parse(&zero_port_ap.addr, TEST_ADDR_STR);
  456. zero_port_ap.port = 0;
  457. MOCK(connection_get_by_type_addr_port_purpose,
  458. mock_connection_get_by_type_addr_port_purpose);
  459. /* Test that we never get 1 from a NULL connection */
  460. mocked_connection = NULL;
  461. tt_assert(router_is_already_dir_fetching(&test_ap, 1, 1) == 0);
  462. tt_assert(router_is_already_dir_fetching(&test_ap, 1, 0) == 0);
  463. tt_assert(router_is_already_dir_fetching(&test_ap, 0, 1) == 0);
  464. /* We always expect 0 in these cases */
  465. tt_assert(router_is_already_dir_fetching(&test_ap, 0, 0) == 0);
  466. tt_assert(router_is_already_dir_fetching(NULL, 1, 1) == 0);
  467. tt_assert(router_is_already_dir_fetching(&null_addr_ap, 1, 1) == 0);
  468. tt_assert(router_is_already_dir_fetching(&zero_port_ap, 1, 1) == 0);
  469. /* Test that we get 1 with a connection in the appropriate circumstances */
  470. mocked_connection = connection_new(CONN_TYPE_DIR, AF_INET);
  471. tt_assert(router_is_already_dir_fetching(&test_ap, 1, 1) == 1);
  472. tt_assert(router_is_already_dir_fetching(&test_ap, 1, 0) == 1);
  473. tt_assert(router_is_already_dir_fetching(&test_ap, 0, 1) == 1);
  474. /* Test that we get 0 even with a connection in the appropriate
  475. * circumstances */
  476. tt_assert(router_is_already_dir_fetching(&test_ap, 0, 0) == 0);
  477. tt_assert(router_is_already_dir_fetching(NULL, 1, 1) == 0);
  478. tt_assert(router_is_already_dir_fetching(&null_addr_ap, 1, 1) == 0);
  479. tt_assert(router_is_already_dir_fetching(&zero_port_ap, 1, 1) == 0);
  480. done:
  481. /* If a connection is never set up, connection_free chokes on it. */
  482. if (mocked_connection) {
  483. buf_free(mocked_connection->inbuf);
  484. buf_free(mocked_connection->outbuf);
  485. }
  486. tor_free(mocked_connection);
  487. UNMOCK(connection_get_by_type_addr_port_purpose);
  488. }
  489. #undef TEST_ADDR_STR
  490. #undef TEST_DIR_PORT
  491. #define NODE(name, flags) \
  492. { #name, test_routerlist_##name, (flags), NULL, NULL }
  493. #define ROUTER(name,flags) \
  494. { #name, test_router_##name, (flags), NULL, NULL }
  495. struct testcase_t routerlist_tests[] = {
  496. NODE(initiate_descriptor_downloads, 0),
  497. NODE(launch_descriptor_downloads, 0),
  498. NODE(router_is_already_dir_fetching, TT_FORK),
  499. ROUTER(pick_directory_server_impl, TT_FORK),
  500. END_OF_TESTCASES
  501. };