test_routerlist.c 17 KB

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