test_routerlist.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. /* Copyright (c) 2014, 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 "container.h"
  13. #include "directory.h"
  14. #include "dirvote.h"
  15. #include "networkstatus.h"
  16. #include "nodelist.h"
  17. #include "policies.h"
  18. #include "routerlist.h"
  19. #include "routerparse.h"
  20. #include "test.h"
  21. #include "test_dir_common.h"
  22. extern const char AUTHORITY_CERT_1[];
  23. extern const char AUTHORITY_SIGNKEY_1[];
  24. extern const char AUTHORITY_CERT_2[];
  25. extern const char AUTHORITY_SIGNKEY_2[];
  26. extern const char AUTHORITY_CERT_3[];
  27. extern const char AUTHORITY_SIGNKEY_3[];
  28. void construct_consensus(const char **consensus_text_md);
  29. /* 4 digests + 3 sep + pre + post + NULL */
  30. static char output[4*BASE64_DIGEST256_LEN+3+2+2+1];
  31. static void
  32. mock_get_from_dirserver(uint8_t dir_purpose, uint8_t router_purpose,
  33. const char *resource, int pds_flags,
  34. download_want_authority_t want_authority)
  35. {
  36. (void)dir_purpose;
  37. (void)router_purpose;
  38. (void)pds_flags;
  39. (void)want_authority;
  40. tt_assert(resource);
  41. strlcpy(output, resource, sizeof(output));
  42. done:
  43. ;
  44. }
  45. static void
  46. test_routerlist_initiate_descriptor_downloads(void *arg)
  47. {
  48. const char *prose = "unhurried and wise, we perceive.";
  49. smartlist_t *digests = smartlist_new();
  50. (void)arg;
  51. for (int i = 0; i < 20; i++) {
  52. smartlist_add(digests, (char*)prose);
  53. }
  54. MOCK(directory_get_from_dirserver, mock_get_from_dirserver);
  55. initiate_descriptor_downloads(NULL, DIR_PURPOSE_FETCH_MICRODESC,
  56. digests, 3, 7, 0);
  57. UNMOCK(directory_get_from_dirserver);
  58. tt_str_op(output, OP_EQ, "d/"
  59. "dW5odXJyaWVkIGFuZCB3aXNlLCB3ZSBwZXJjZWl2ZS4-"
  60. "dW5odXJyaWVkIGFuZCB3aXNlLCB3ZSBwZXJjZWl2ZS4-"
  61. "dW5odXJyaWVkIGFuZCB3aXNlLCB3ZSBwZXJjZWl2ZS4-"
  62. "dW5odXJyaWVkIGFuZCB3aXNlLCB3ZSBwZXJjZWl2ZS4"
  63. ".z");
  64. done:
  65. smartlist_free(digests);
  66. }
  67. static int count = 0;
  68. static void
  69. mock_initiate_descriptor_downloads(const routerstatus_t *source,
  70. int purpose, smartlist_t *digests,
  71. int lo, int hi, int pds_flags)
  72. {
  73. (void)source;
  74. (void)purpose;
  75. (void)digests;
  76. (void)pds_flags;
  77. (void)hi;
  78. (void)lo;
  79. count += 1;
  80. }
  81. static void
  82. test_routerlist_launch_descriptor_downloads(void *arg)
  83. {
  84. smartlist_t *downloadable = smartlist_new();
  85. time_t now = time(NULL);
  86. char *cp;
  87. (void)arg;
  88. for (int i = 0; i < 100; i++) {
  89. cp = tor_malloc(DIGEST256_LEN);
  90. tt_assert(cp);
  91. crypto_rand(cp, DIGEST256_LEN);
  92. smartlist_add(downloadable, cp);
  93. }
  94. MOCK(initiate_descriptor_downloads, mock_initiate_descriptor_downloads);
  95. launch_descriptor_downloads(DIR_PURPOSE_FETCH_MICRODESC, downloadable,
  96. NULL, now);
  97. tt_int_op(3, ==, count);
  98. UNMOCK(initiate_descriptor_downloads);
  99. done:
  100. SMARTLIST_FOREACH(downloadable, char *, cp1, tor_free(cp1));
  101. smartlist_free(downloadable);
  102. }
  103. void
  104. construct_consensus(const char **consensus_text_md)
  105. {
  106. networkstatus_t *vote = NULL;
  107. networkstatus_t *v1 = NULL, *v2 = NULL, *v3 = NULL;
  108. networkstatus_voter_info_t *voter = NULL;
  109. authority_cert_t *cert1=NULL, *cert2=NULL, *cert3=NULL;
  110. crypto_pk_t *sign_skey_1=NULL, *sign_skey_2=NULL, *sign_skey_3=NULL;
  111. crypto_pk_t *sign_skey_leg=NULL;
  112. time_t now = time(NULL);
  113. smartlist_t *votes = NULL;
  114. addr_policy_t *pol1 = NULL, *pol2 = NULL, *pol3 = NULL;
  115. int n_vrs;
  116. tt_assert(!dir_common_authority_pk_init(&cert1, &cert2, &cert3,
  117. &sign_skey_1, &sign_skey_2,
  118. &sign_skey_3));
  119. sign_skey_leg = pk_generate(4);
  120. dir_common_construct_vote_1(&vote, cert1, sign_skey_1,
  121. &dir_common_gen_routerstatus_for_v3ns,
  122. &v1, &n_vrs, now, 1);
  123. tt_assert(v1);
  124. tt_int_op(n_vrs, ==, 4);
  125. tt_int_op(smartlist_len(v1->routerstatus_list), ==, 4);
  126. dir_common_construct_vote_2(&vote, cert2, sign_skey_2,
  127. &dir_common_gen_routerstatus_for_v3ns,
  128. &v2, &n_vrs, now, 1);
  129. tt_assert(v2);
  130. tt_int_op(n_vrs, ==, 4);
  131. tt_int_op(smartlist_len(v2->routerstatus_list), ==, 4);
  132. dir_common_construct_vote_3(&vote, cert3, sign_skey_3,
  133. &dir_common_gen_routerstatus_for_v3ns,
  134. &v3, &n_vrs, now, 1);
  135. tt_assert(v3);
  136. tt_int_op(n_vrs, ==, 4);
  137. tt_int_op(smartlist_len(v3->routerstatus_list), ==, 4);
  138. votes = smartlist_new();
  139. smartlist_add(votes, v1);
  140. smartlist_add(votes, v2);
  141. smartlist_add(votes, v3);
  142. *consensus_text_md = networkstatus_compute_consensus(votes, 3,
  143. cert1->identity_key,
  144. sign_skey_1,
  145. "AAAAAAAAAAAAAAAAAAAA",
  146. sign_skey_leg,
  147. FLAV_MICRODESC);
  148. tt_assert(*consensus_text_md);
  149. done:
  150. if (vote)
  151. tor_free(vote);
  152. if (voter)
  153. tor_free(voter);
  154. if (pol1)
  155. tor_free(pol1);
  156. if (pol2)
  157. tor_free(pol2);
  158. if (pol3)
  159. tor_free(pol3);
  160. }
  161. static void
  162. test_router_pick_directory_server_impl(void *arg)
  163. {
  164. (void)arg;
  165. networkstatus_t *con_md = NULL;
  166. const char *consensus_text_md = NULL;
  167. int flags = PDS_IGNORE_FASCISTFIREWALL|PDS_RETRY_IF_NO_SERVERS;
  168. or_options_t *options = get_options_mutable();
  169. const routerstatus_t *rs = NULL;
  170. options->UseMicrodescriptors = 1;
  171. char *router1_id = NULL, *router2_id = NULL, *router3_id = NULL;
  172. node_t *node_router1 = NULL, *node_router2 = NULL, *node_router3 = NULL;
  173. config_line_t *policy_line = NULL;
  174. time_t now = time(NULL);
  175. int tmp_dirport1, tmp_dirport3;
  176. (void)arg;
  177. /* No consensus available, fail early */
  178. rs = router_pick_directory_server_impl(V3_DIRINFO, (const int) 0, NULL);
  179. tt_assert(rs == NULL);
  180. construct_consensus(&consensus_text_md);
  181. tt_assert(consensus_text_md);
  182. con_md = networkstatus_parse_vote_from_string(consensus_text_md, NULL,
  183. NS_TYPE_CONSENSUS);
  184. tt_assert(con_md);
  185. tt_int_op(con_md->flavor,==, FLAV_MICRODESC);
  186. tt_assert(con_md->routerstatus_list);
  187. tt_int_op(smartlist_len(con_md->routerstatus_list), ==, 3);
  188. tt_assert(!networkstatus_set_current_consensus_from_ns(con_md,
  189. "microdesc"));
  190. nodelist_set_consensus(con_md);
  191. nodelist_assert_ok();
  192. rs = router_pick_directory_server_impl(V3_DIRINFO, flags, NULL);
  193. /* We should not fail now we have a consensus and routerstatus_list
  194. * and nodelist are populated. */
  195. tt_assert(rs != NULL);
  196. /* Manipulate the nodes so we get the dir server we expect */
  197. router1_id = tor_malloc(DIGEST_LEN);
  198. memset(router1_id, TEST_DIR_ROUTER_ID_1, DIGEST_LEN);
  199. router2_id = tor_malloc(DIGEST_LEN);
  200. memset(router2_id, TEST_DIR_ROUTER_ID_2, DIGEST_LEN);
  201. router3_id = tor_malloc(DIGEST_LEN);
  202. memset(router3_id, TEST_DIR_ROUTER_ID_3, DIGEST_LEN);
  203. node_router1 = node_get_mutable_by_id(router1_id);
  204. node_router2 = node_get_mutable_by_id(router2_id);
  205. node_router3 = node_get_mutable_by_id(router3_id);
  206. node_router1->is_possible_guard = 1;
  207. node_router1->is_running = 0;
  208. node_router3->is_running = 0;
  209. rs = router_pick_directory_server_impl(V3_DIRINFO, flags, NULL);
  210. tt_assert(rs != NULL);
  211. tt_assert(tor_memeq(rs->identity_digest, router2_id, DIGEST_LEN));
  212. rs = NULL;
  213. node_router1->is_running = 1;
  214. node_router3->is_running = 1;
  215. node_router1->rs->is_v2_dir = 0;
  216. node_router3->rs->is_v2_dir = 0;
  217. tmp_dirport1 = node_router1->rs->dir_port;
  218. tmp_dirport3 = node_router3->rs->dir_port;
  219. node_router1->rs->dir_port = 0;
  220. node_router3->rs->dir_port = 0;
  221. rs = router_pick_directory_server_impl(V3_DIRINFO, flags, NULL);
  222. tt_assert(rs != NULL);
  223. tt_assert(tor_memeq(rs->identity_digest, router2_id, DIGEST_LEN));
  224. rs = NULL;
  225. node_router1->rs->is_v2_dir = 1;
  226. node_router3->rs->is_v2_dir = 1;
  227. node_router1->rs->dir_port = tmp_dirport1;
  228. node_router3->rs->dir_port = tmp_dirport3;
  229. node_router1->is_valid = 0;
  230. node_router3->is_valid = 0;
  231. rs = router_pick_directory_server_impl(V3_DIRINFO, flags, NULL);
  232. tt_assert(rs != NULL);
  233. tt_assert(tor_memeq(rs->identity_digest, router2_id, DIGEST_LEN));
  234. rs = NULL;
  235. node_router1->is_valid = 1;
  236. node_router3->is_valid = 1;
  237. flags |= PDS_FOR_GUARD;
  238. node_router1->using_as_guard = 1;
  239. node_router2->using_as_guard = 1;
  240. node_router3->using_as_guard = 1;
  241. rs = router_pick_directory_server_impl(V3_DIRINFO, flags, NULL);
  242. tt_assert(rs == NULL);
  243. node_router1->using_as_guard = 0;
  244. rs = router_pick_directory_server_impl(V3_DIRINFO, flags, NULL);
  245. tt_assert(rs != NULL);
  246. tt_assert(tor_memeq(rs->identity_digest, router1_id, DIGEST_LEN));
  247. rs = NULL;
  248. node_router2->using_as_guard = 0;
  249. node_router3->using_as_guard = 0;
  250. /* One not valid, one guard. This should leave one remaining */
  251. node_router1->is_valid = 0;
  252. node_router2->using_as_guard = 1;
  253. rs = router_pick_directory_server_impl(V3_DIRINFO, flags, NULL);
  254. tt_assert(rs != NULL);
  255. tt_assert(tor_memeq(rs->identity_digest, router3_id, DIGEST_LEN));
  256. rs = NULL;
  257. node_router1->is_valid = 1;
  258. node_router2->using_as_guard = 0;
  259. /* Manipulate overloaded */
  260. node_router2->rs->last_dir_503_at = now;
  261. node_router3->rs->last_dir_503_at = now;
  262. rs = router_pick_directory_server_impl(V3_DIRINFO, flags, NULL);
  263. tt_assert(rs != NULL);
  264. tt_assert(tor_memeq(rs->identity_digest, router1_id, DIGEST_LEN));
  265. node_router2->rs->last_dir_503_at = 0;
  266. node_router3->rs->last_dir_503_at = 0;
  267. /* Set a Fascist firewall */
  268. flags &= ! PDS_IGNORE_FASCISTFIREWALL;
  269. policy_line = tor_malloc_zero(sizeof(config_line_t));
  270. policy_line->key = tor_strdup("ReachableORAddresses");
  271. policy_line->value = tor_strdup("accept *:442, reject *:*");
  272. options->ReachableORAddresses = policy_line;
  273. policies_parse_from_options(options);
  274. node_router1->rs->or_port = 444;
  275. node_router2->rs->or_port = 443;
  276. node_router3->rs->or_port = 442;
  277. rs = router_pick_directory_server_impl(V3_DIRINFO, flags, NULL);
  278. tt_assert(rs != NULL);
  279. tt_assert(tor_memeq(rs->identity_digest, router3_id, DIGEST_LEN));
  280. node_router1->rs->or_port = 442;
  281. node_router2->rs->or_port = 443;
  282. node_router3->rs->or_port = 444;
  283. rs = router_pick_directory_server_impl(V3_DIRINFO, flags, NULL);
  284. tt_assert(rs != NULL);
  285. tt_assert(tor_memeq(rs->identity_digest, router1_id, DIGEST_LEN));
  286. /* Fascist firewall and overloaded */
  287. node_router1->rs->or_port = 442;
  288. node_router2->rs->or_port = 443;
  289. node_router3->rs->or_port = 442;
  290. node_router3->rs->last_dir_503_at = now;
  291. rs = router_pick_directory_server_impl(V3_DIRINFO, flags, NULL);
  292. tt_assert(rs != NULL);
  293. tt_assert(tor_memeq(rs->identity_digest, router1_id, DIGEST_LEN));
  294. node_router3->rs->last_dir_503_at = 0;
  295. /* Fascists against OR and Dir */
  296. policy_line = tor_malloc_zero(sizeof(config_line_t));
  297. policy_line->key = tor_strdup("ReachableAddresses");
  298. policy_line->value = tor_strdup("accept *:80, reject *:*");
  299. options->ReachableDirAddresses = policy_line;
  300. policies_parse_from_options(options);
  301. node_router1->rs->or_port = 442;
  302. node_router2->rs->or_port = 441;
  303. node_router3->rs->or_port = 443;
  304. node_router1->rs->dir_port = 80;
  305. node_router2->rs->dir_port = 80;
  306. node_router3->rs->dir_port = 81;
  307. node_router1->rs->last_dir_503_at = now;
  308. rs = router_pick_directory_server_impl(V3_DIRINFO, flags, NULL);
  309. tt_assert(rs != NULL);
  310. tt_assert(tor_memeq(rs->identity_digest, router1_id, DIGEST_LEN));
  311. node_router1->rs->last_dir_503_at = 0;
  312. done:
  313. if (router1_id)
  314. tor_free(router1_id);
  315. if (router2_id)
  316. tor_free(router2_id);
  317. if (router3_id)
  318. tor_free(router3_id);
  319. if (options->ReachableORAddresses ||
  320. options->ReachableDirAddresses)
  321. policies_free_all();
  322. }
  323. #define NODE(name, flags) \
  324. { #name, test_routerlist_##name, (flags), NULL, NULL }
  325. #define ROUTER(name,flags) \
  326. { #name, test_router_##name, (flags), NULL, NULL }
  327. struct testcase_t routerlist_tests[] = {
  328. NODE(initiate_descriptor_downloads, 0),
  329. NODE(launch_descriptor_downloads, 0),
  330. ROUTER(pick_directory_server_impl, TT_FORK),
  331. END_OF_TESTCASES
  332. };