test_entrynodes.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881
  1. /* Copyright (c) 2014-2016, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. #include "orconfig.h"
  4. #define STATEFILE_PRIVATE
  5. #define ENTRYNODES_PRIVATE
  6. #define ROUTERLIST_PRIVATE
  7. #include "or.h"
  8. #include "test.h"
  9. #include "config.h"
  10. #include "entrynodes.h"
  11. #include "nodelist.h"
  12. #include "policies.h"
  13. #include "routerlist.h"
  14. #include "routerparse.h"
  15. #include "routerset.h"
  16. #include "statefile.h"
  17. #include "util.h"
  18. #include "test_helpers.h"
  19. /* TODO:
  20. * choose_random_entry() test with state set.
  21. *
  22. * parse_state() tests with more than one guards.
  23. *
  24. * More tests for set_from_config(): Multiple nodes, use fingerprints,
  25. * use country codes.
  26. */
  27. /** Dummy Tor state used in unittests. */
  28. static or_state_t *dummy_state = NULL;
  29. static or_state_t *
  30. get_or_state_replacement(void)
  31. {
  32. return dummy_state;
  33. }
  34. /* Unittest cleanup function: Cleanup the fake network. */
  35. static int
  36. fake_network_cleanup(const struct testcase_t *testcase, void *ptr)
  37. {
  38. (void) testcase;
  39. (void) ptr;
  40. routerlist_free_all();
  41. nodelist_free_all();
  42. entry_guards_free_all();
  43. or_state_free(dummy_state);
  44. return 1; /* NOP */
  45. }
  46. /* Unittest setup function: Setup a fake network. */
  47. static void *
  48. fake_network_setup(const struct testcase_t *testcase)
  49. {
  50. (void) testcase;
  51. /* Setup fake state */
  52. dummy_state = tor_malloc_zero(sizeof(or_state_t));
  53. MOCK(get_or_state,
  54. get_or_state_replacement);
  55. /* Setup fake routerlist. */
  56. helper_setup_fake_routerlist();
  57. /* Return anything but NULL (it's interpreted as test fail) */
  58. return dummy_state;
  59. }
  60. static or_options_t mocked_options;
  61. static const or_options_t *
  62. mock_get_options(void)
  63. {
  64. return &mocked_options;
  65. }
  66. /** Test choose_random_entry() with none of our routers being guard nodes. */
  67. static void
  68. test_choose_random_entry_no_guards(void *arg)
  69. {
  70. const node_t *chosen_entry = NULL;
  71. (void) arg;
  72. MOCK(get_options, mock_get_options);
  73. /* Check that we get a guard if it passes preferred
  74. * address settings */
  75. memset(&mocked_options, 0, sizeof(mocked_options));
  76. mocked_options.ClientUseIPv4 = 1;
  77. mocked_options.ClientPreferIPv6ORPort = 0;
  78. /* Try to pick an entry even though none of our routers are guards. */
  79. chosen_entry = choose_random_entry(NULL);
  80. /* Unintuitively, we actually pick a random node as our entry,
  81. because router_choose_random_node() relaxes its constraints if it
  82. can't find a proper entry guard. */
  83. tt_assert(chosen_entry);
  84. /* And with the other IP version active */
  85. mocked_options.ClientUseIPv6 = 1;
  86. chosen_entry = choose_random_entry(NULL);
  87. tt_assert(chosen_entry);
  88. /* And with the preference on auto */
  89. mocked_options.ClientPreferIPv6ORPort = -1;
  90. chosen_entry = choose_random_entry(NULL);
  91. tt_assert(chosen_entry);
  92. /* Check that we don't get a guard if it doesn't pass mandatory address
  93. * settings */
  94. memset(&mocked_options, 0, sizeof(mocked_options));
  95. mocked_options.ClientUseIPv4 = 0;
  96. mocked_options.ClientPreferIPv6ORPort = 0;
  97. chosen_entry = choose_random_entry(NULL);
  98. /* If we don't allow IPv4 at all, we don't get a guard*/
  99. tt_assert(!chosen_entry);
  100. /* Check that we get a guard if it passes allowed but not preferred address
  101. * settings */
  102. memset(&mocked_options, 0, sizeof(mocked_options));
  103. mocked_options.ClientUseIPv4 = 1;
  104. mocked_options.ClientUseIPv6 = 1;
  105. mocked_options.ClientPreferIPv6ORPort = 1;
  106. chosen_entry = choose_random_entry(NULL);
  107. tt_assert(chosen_entry);
  108. /* Check that we get a guard if it passes preferred address settings when
  109. * they're auto */
  110. memset(&mocked_options, 0, sizeof(mocked_options));
  111. mocked_options.ClientUseIPv4 = 1;
  112. mocked_options.ClientPreferIPv6ORPort = -1;
  113. chosen_entry = choose_random_entry(NULL);
  114. tt_assert(chosen_entry);
  115. /* And with IPv6 active */
  116. mocked_options.ClientUseIPv6 = 1;
  117. chosen_entry = choose_random_entry(NULL);
  118. tt_assert(chosen_entry);
  119. done:
  120. memset(&mocked_options, 0, sizeof(mocked_options));
  121. UNMOCK(get_options);
  122. }
  123. /** Test choose_random_entry() with only one of our routers being a
  124. guard node. */
  125. static void
  126. test_choose_random_entry_one_possible_guard(void *arg)
  127. {
  128. const node_t *chosen_entry = NULL;
  129. node_t *the_guard = NULL;
  130. smartlist_t *our_nodelist = NULL;
  131. (void) arg;
  132. MOCK(get_options, mock_get_options);
  133. /* Set one of the nodes to be a guard. */
  134. our_nodelist = nodelist_get_list();
  135. the_guard = smartlist_get(our_nodelist, 4); /* chosen by fair dice roll */
  136. the_guard->is_possible_guard = 1;
  137. /* Check that we get the guard if it passes preferred
  138. * address settings */
  139. memset(&mocked_options, 0, sizeof(mocked_options));
  140. mocked_options.ClientUseIPv4 = 1;
  141. mocked_options.ClientPreferIPv6ORPort = 0;
  142. /* Pick an entry. Make sure we pick the node we marked as guard. */
  143. chosen_entry = choose_random_entry(NULL);
  144. tt_ptr_op(chosen_entry, OP_EQ, the_guard);
  145. /* And with the other IP version active */
  146. mocked_options.ClientUseIPv6 = 1;
  147. chosen_entry = choose_random_entry(NULL);
  148. tt_ptr_op(chosen_entry, OP_EQ, the_guard);
  149. /* And with the preference on auto */
  150. mocked_options.ClientPreferIPv6ORPort = -1;
  151. chosen_entry = choose_random_entry(NULL);
  152. tt_ptr_op(chosen_entry, OP_EQ, the_guard);
  153. /* Check that we don't get a guard if it doesn't pass mandatory address
  154. * settings */
  155. memset(&mocked_options, 0, sizeof(mocked_options));
  156. mocked_options.ClientUseIPv4 = 0;
  157. mocked_options.ClientPreferIPv6ORPort = 0;
  158. chosen_entry = choose_random_entry(NULL);
  159. /* If we don't allow IPv4 at all, we don't get a guard*/
  160. tt_assert(!chosen_entry);
  161. /* Check that we get a node if it passes allowed but not preferred
  162. * address settings */
  163. memset(&mocked_options, 0, sizeof(mocked_options));
  164. mocked_options.ClientUseIPv4 = 1;
  165. mocked_options.ClientUseIPv6 = 1;
  166. mocked_options.ClientPreferIPv6ORPort = 1;
  167. chosen_entry = choose_random_entry(NULL);
  168. /* We disable the guard check and the preferred address check at the same
  169. * time, so we can't be sure we get the guard */
  170. tt_assert(chosen_entry);
  171. /* Check that we get a node if it is allowed but not preferred when settings
  172. * are auto */
  173. memset(&mocked_options, 0, sizeof(mocked_options));
  174. mocked_options.ClientUseIPv4 = 1;
  175. mocked_options.ClientPreferIPv6ORPort = -1;
  176. chosen_entry = choose_random_entry(NULL);
  177. /* We disable the guard check and the preferred address check at the same
  178. * time, so we can't be sure we get the guard */
  179. tt_assert(chosen_entry);
  180. /* and with IPv6 active */
  181. mocked_options.ClientUseIPv6 = 1;
  182. chosen_entry = choose_random_entry(NULL);
  183. tt_assert(chosen_entry);
  184. done:
  185. memset(&mocked_options, 0, sizeof(mocked_options));
  186. UNMOCK(get_options);
  187. }
  188. /** Helper to conduct tests for populate_live_entry_guards().
  189. This test adds some entry guards to our list, and then tests
  190. populate_live_entry_guards() to mke sure it filters them correctly.
  191. <b>num_needed</b> is the number of guard nodes we support. It's
  192. configurable to make sure we function properly with 1 or 3 guard
  193. nodes configured.
  194. */
  195. static void
  196. populate_live_entry_guards_test_helper(int num_needed)
  197. {
  198. smartlist_t *our_nodelist = NULL;
  199. smartlist_t *live_entry_guards = smartlist_new();
  200. guard_selection_t *gs = get_guard_selection_info();
  201. const smartlist_t *all_entry_guards =
  202. get_entry_guards_for_guard_selection(gs);
  203. or_options_t *options = get_options_mutable();
  204. int retval;
  205. /* Set NumEntryGuards to the provided number. */
  206. options->NumEntryGuards = num_needed;
  207. tt_int_op(num_needed, OP_EQ, decide_num_guards(options, 0));
  208. /* The global entry guards smartlist should be empty now. */
  209. tt_int_op(smartlist_len(all_entry_guards), OP_EQ, 0);
  210. /* Walk the nodelist and add all nodes as entry guards. */
  211. our_nodelist = nodelist_get_list();
  212. tt_int_op(smartlist_len(our_nodelist), OP_EQ, HELPER_NUMBER_OF_DESCRIPTORS);
  213. SMARTLIST_FOREACH_BEGIN(our_nodelist, const node_t *, node) {
  214. const node_t *node_tmp;
  215. node_tmp = add_an_entry_guard(gs, node, 0, 1, 0, 0);
  216. tt_assert(node_tmp);
  217. } SMARTLIST_FOREACH_END(node);
  218. /* Make sure the nodes were added as entry guards. */
  219. tt_int_op(smartlist_len(all_entry_guards), OP_EQ,
  220. HELPER_NUMBER_OF_DESCRIPTORS);
  221. /* Ensure that all the possible entry guards are enough to satisfy us. */
  222. tt_int_op(smartlist_len(all_entry_guards), OP_GE, num_needed);
  223. /* Walk the entry guard list for some sanity checking */
  224. SMARTLIST_FOREACH_BEGIN(all_entry_guards, const entry_guard_t *, entry) {
  225. /* Since we called add_an_entry_guard() with 'for_discovery' being
  226. False, all guards should have made_contact enabled. */
  227. tt_int_op(entry->made_contact, OP_EQ, 1);
  228. } SMARTLIST_FOREACH_END(entry);
  229. /* First, try to get some fast guards. This should fail. */
  230. retval = populate_live_entry_guards(live_entry_guards,
  231. all_entry_guards,
  232. NULL,
  233. NO_DIRINFO, /* Don't care about DIRINFO*/
  234. 0, 0,
  235. 1); /* We want fast guard! */
  236. tt_int_op(retval, OP_EQ, 0);
  237. tt_int_op(smartlist_len(live_entry_guards), OP_EQ, 0);
  238. /* Now try to get some stable guards. This should fail too. */
  239. retval = populate_live_entry_guards(live_entry_guards,
  240. all_entry_guards,
  241. NULL,
  242. NO_DIRINFO,
  243. 0,
  244. 1, /* We want stable guard! */
  245. 0);
  246. tt_int_op(retval, OP_EQ, 0);
  247. tt_int_op(smartlist_len(live_entry_guards), OP_EQ, 0);
  248. /* Now try to get any guard we can find. This should succeed. */
  249. retval = populate_live_entry_guards(live_entry_guards,
  250. all_entry_guards,
  251. NULL,
  252. NO_DIRINFO,
  253. 0, 0, 0); /* No restrictions! */
  254. /* Since we had more than enough guards in 'all_entry_guards', we
  255. should have added 'num_needed' of them to live_entry_guards.
  256. 'retval' should be 1 since we now have enough live entry guards
  257. to pick one. */
  258. tt_int_op(retval, OP_EQ, 1);
  259. tt_int_op(smartlist_len(live_entry_guards), OP_EQ, num_needed);
  260. done:
  261. smartlist_free(live_entry_guards);
  262. }
  263. /* Test populate_live_entry_guards() for 1 guard node. */
  264. static void
  265. test_populate_live_entry_guards_1guard(void *arg)
  266. {
  267. (void) arg;
  268. populate_live_entry_guards_test_helper(1);
  269. }
  270. /* Test populate_live_entry_guards() for 3 guard nodes. */
  271. static void
  272. test_populate_live_entry_guards_3guards(void *arg)
  273. {
  274. (void) arg;
  275. populate_live_entry_guards_test_helper(3);
  276. }
  277. /** Append some EntryGuard lines to the Tor state at <b>state</b>.
  278. <b>entry_guard_lines</b> is a smartlist containing 2-tuple
  279. smartlists that carry the key and values of the statefile.
  280. As an example:
  281. entry_guard_lines =
  282. (("EntryGuard", "name 67E72FF33D7D41BF11C569646A0A7B4B188340DF DirCache"),
  283. ("EntryGuardDownSince", "2014-06-07 16:02:46 2014-06-07 16:02:46"))
  284. */
  285. static void
  286. state_insert_entry_guard_helper(or_state_t *state,
  287. smartlist_t *entry_guard_lines)
  288. {
  289. config_line_t **next, *line;
  290. next = &state->EntryGuards;
  291. *next = NULL;
  292. /* Loop over all the state lines in the smartlist */
  293. SMARTLIST_FOREACH_BEGIN(entry_guard_lines, const smartlist_t *,state_lines) {
  294. /* Get key and value for each line */
  295. const char *state_key = smartlist_get(state_lines, 0);
  296. const char *state_value = smartlist_get(state_lines, 1);
  297. *next = line = tor_malloc_zero(sizeof(config_line_t));
  298. line->key = tor_strdup(state_key);
  299. tor_asprintf(&line->value, "%s", state_value);
  300. next = &(line->next);
  301. } SMARTLIST_FOREACH_END(state_lines);
  302. }
  303. /** Free memory occupied by <b>entry_guard_lines</b>. */
  304. static void
  305. state_lines_free(smartlist_t *entry_guard_lines)
  306. {
  307. SMARTLIST_FOREACH_BEGIN(entry_guard_lines, smartlist_t *, state_lines) {
  308. char *state_key = smartlist_get(state_lines, 0);
  309. char *state_value = smartlist_get(state_lines, 1);
  310. tor_free(state_key);
  311. tor_free(state_value);
  312. smartlist_free(state_lines);
  313. } SMARTLIST_FOREACH_END(state_lines);
  314. smartlist_free(entry_guard_lines);
  315. }
  316. /* Tests entry_guards_parse_state(). It creates a fake Tor state with
  317. a saved entry guard and makes sure that Tor can parse it and
  318. creates the right entry node out of it.
  319. */
  320. static void
  321. test_entry_guards_parse_state_simple(void *arg)
  322. {
  323. or_state_t *state = or_state_new();
  324. const smartlist_t *all_entry_guards = get_entry_guards();
  325. smartlist_t *entry_state_lines = smartlist_new();
  326. char *msg = NULL;
  327. int retval;
  328. /* Details of our fake guard node */
  329. const char *nickname = "hagbard";
  330. const char *fpr = "B29D536DD1752D542E1FBB3C9CE4449D51298212";
  331. const char *tor_version = "0.2.5.3-alpha-dev";
  332. const char *added_at = get_yesterday_date_str();
  333. const char *unlisted_since = "2014-06-08 16:16:50";
  334. (void) arg;
  335. /* The global entry guards smartlist should be empty now. */
  336. tt_int_op(smartlist_len(all_entry_guards), OP_EQ, 0);
  337. { /* Prepare the state entry */
  338. /* Prepare the smartlist to hold the key/value of each line */
  339. smartlist_t *state_line = smartlist_new();
  340. smartlist_add_asprintf(state_line, "EntryGuard");
  341. smartlist_add_asprintf(state_line, "%s %s %s", nickname, fpr, "DirCache");
  342. smartlist_add(entry_state_lines, state_line);
  343. state_line = smartlist_new();
  344. smartlist_add_asprintf(state_line, "EntryGuardAddedBy");
  345. smartlist_add_asprintf(state_line, "%s %s %s", fpr, tor_version, added_at);
  346. smartlist_add(entry_state_lines, state_line);
  347. state_line = smartlist_new();
  348. smartlist_add_asprintf(state_line, "EntryGuardUnlistedSince");
  349. smartlist_add_asprintf(state_line, "%s", unlisted_since);
  350. smartlist_add(entry_state_lines, state_line);
  351. }
  352. /* Inject our lines in the state */
  353. state_insert_entry_guard_helper(state, entry_state_lines);
  354. /* Parse state */
  355. retval = entry_guards_parse_state(state, 1, &msg);
  356. tt_int_op(retval, OP_GE, 0);
  357. /* Test that the guard was registered.
  358. We need to re-get the entry guard list since its pointer was
  359. overwritten in entry_guards_parse_state(). */
  360. all_entry_guards = get_entry_guards();
  361. tt_int_op(smartlist_len(all_entry_guards), OP_EQ, 1);
  362. { /* Test the entry guard structure */
  363. char hex_digest[1024];
  364. char str_time[1024];
  365. const entry_guard_t *e = smartlist_get(all_entry_guards, 0);
  366. tt_str_op(e->nickname, OP_EQ, nickname); /* Verify nickname */
  367. base16_encode(hex_digest, sizeof(hex_digest),
  368. e->identity, DIGEST_LEN);
  369. tt_str_op(hex_digest, OP_EQ, fpr); /* Verify fingerprint */
  370. tt_assert(e->is_dir_cache); /* Verify dirness */
  371. tt_str_op(e->chosen_by_version, OP_EQ, tor_version); /* Verify version */
  372. tt_assert(e->made_contact); /* All saved guards have been contacted */
  373. tt_assert(e->bad_since); /* Verify bad_since timestamp */
  374. format_iso_time(str_time, e->bad_since);
  375. tt_str_op(str_time, OP_EQ, unlisted_since);
  376. /* The rest should be unset */
  377. tt_assert(!e->unreachable_since);
  378. tt_assert(!e->can_retry);
  379. tt_assert(!e->path_bias_noticed);
  380. tt_assert(!e->path_bias_warned);
  381. tt_assert(!e->path_bias_extreme);
  382. tt_assert(!e->path_bias_disabled);
  383. tt_assert(!e->path_bias_use_noticed);
  384. tt_assert(!e->path_bias_use_extreme);
  385. tt_assert(!e->last_attempted);
  386. }
  387. done:
  388. state_lines_free(entry_state_lines);
  389. or_state_free(state);
  390. tor_free(msg);
  391. }
  392. /** Similar to test_entry_guards_parse_state_simple() but aims to test
  393. the PathBias-related details of the entry guard. */
  394. static void
  395. test_entry_guards_parse_state_pathbias(void *arg)
  396. {
  397. or_state_t *state = or_state_new();
  398. const smartlist_t *all_entry_guards = get_entry_guards();
  399. char *msg = NULL;
  400. int retval;
  401. smartlist_t *entry_state_lines = smartlist_new();
  402. /* Path bias details of the fake guard */
  403. const double circ_attempts = 9;
  404. const double circ_successes = 8;
  405. const double successful_closed = 4;
  406. const double collapsed = 2;
  407. const double unusable = 0;
  408. const double timeouts = 1;
  409. (void) arg;
  410. /* The global entry guards smartlist should be empty now. */
  411. tt_int_op(smartlist_len(all_entry_guards), OP_EQ, 0);
  412. { /* Prepare the state entry */
  413. /* Prepare the smartlist to hold the key/value of each line */
  414. smartlist_t *state_line = smartlist_new();
  415. smartlist_add_asprintf(state_line, "EntryGuard");
  416. smartlist_add_asprintf(state_line,
  417. "givethanks B29D536DD1752D542E1FBB3C9CE4449D51298212 NoDirCache");
  418. smartlist_add(entry_state_lines, state_line);
  419. state_line = smartlist_new();
  420. smartlist_add_asprintf(state_line, "EntryGuardAddedBy");
  421. smartlist_add_asprintf(state_line,
  422. "B29D536DD1752D542E1FBB3C9CE4449D51298212 0.2.5.3-alpha-dev "
  423. "%s", get_yesterday_date_str());
  424. smartlist_add(entry_state_lines, state_line);
  425. state_line = smartlist_new();
  426. smartlist_add_asprintf(state_line, "EntryGuardUnlistedSince");
  427. smartlist_add_asprintf(state_line, "2014-06-08 16:16:50");
  428. smartlist_add(entry_state_lines, state_line);
  429. state_line = smartlist_new();
  430. smartlist_add_asprintf(state_line, "EntryGuardPathBias");
  431. smartlist_add_asprintf(state_line, "%f %f %f %f %f %f",
  432. circ_attempts, circ_successes, successful_closed,
  433. collapsed, unusable, timeouts);
  434. smartlist_add(entry_state_lines, state_line);
  435. }
  436. /* Inject our lines in the state */
  437. state_insert_entry_guard_helper(state, entry_state_lines);
  438. /* Parse state */
  439. retval = entry_guards_parse_state(state, 1, &msg);
  440. tt_int_op(retval, OP_GE, 0);
  441. /* Test that the guard was registered */
  442. all_entry_guards = get_entry_guards();
  443. tt_int_op(smartlist_len(all_entry_guards), OP_EQ, 1);
  444. { /* Test the path bias of this guard */
  445. const entry_guard_t *e = smartlist_get(all_entry_guards, 0);
  446. tt_assert(!e->is_dir_cache);
  447. tt_assert(!e->can_retry);
  448. /* XXX tt_double_op doesn't support equality. Cast to int for now. */
  449. tt_int_op((int)e->circ_attempts, OP_EQ, (int)circ_attempts);
  450. tt_int_op((int)e->circ_successes, OP_EQ, (int)circ_successes);
  451. tt_int_op((int)e->successful_circuits_closed, OP_EQ,
  452. (int)successful_closed);
  453. tt_int_op((int)e->timeouts, OP_EQ, (int)timeouts);
  454. tt_int_op((int)e->collapsed_circuits, OP_EQ, (int)collapsed);
  455. tt_int_op((int)e->unusable_circuits, OP_EQ, (int)unusable);
  456. }
  457. done:
  458. or_state_free(state);
  459. state_lines_free(entry_state_lines);
  460. tor_free(msg);
  461. }
  462. /* Simple test of entry_guards_set_from_config() by specifying a
  463. particular EntryNode and making sure it gets picked. */
  464. static void
  465. test_entry_guards_set_from_config(void *arg)
  466. {
  467. or_options_t *options = get_options_mutable();
  468. guard_selection_t *gs = get_guard_selection_info();
  469. const smartlist_t *all_entry_guards =
  470. get_entry_guards_for_guard_selection(gs);
  471. const char *entrynodes_str = "test003r";
  472. const node_t *chosen_entry = NULL;
  473. int retval;
  474. (void) arg;
  475. /* Prase EntryNodes as a routerset. */
  476. options->EntryNodes = routerset_new();
  477. retval = routerset_parse(options->EntryNodes,
  478. entrynodes_str,
  479. "test_entrynodes");
  480. tt_int_op(retval, OP_GE, 0);
  481. /* Read nodes from EntryNodes */
  482. entry_guards_set_from_config(gs, options);
  483. /* Test that only one guard was added. */
  484. tt_int_op(smartlist_len(all_entry_guards), OP_EQ, 1);
  485. /* Make sure it was the guard we specified. */
  486. chosen_entry = choose_random_entry(NULL);
  487. tt_str_op(chosen_entry->ri->nickname, OP_EQ, entrynodes_str);
  488. done:
  489. routerset_free(options->EntryNodes);
  490. }
  491. static void
  492. test_entry_is_time_to_retry(void *arg)
  493. {
  494. entry_guard_t *test_guard;
  495. time_t now;
  496. int retval;
  497. (void)arg;
  498. now = time(NULL);
  499. test_guard = tor_malloc_zero(sizeof(entry_guard_t));
  500. test_guard->last_attempted = now - 10;
  501. test_guard->unreachable_since = now - 1;
  502. retval = entry_is_time_to_retry(test_guard,now);
  503. tt_int_op(retval,OP_EQ,1);
  504. test_guard->unreachable_since = now - (6*60*60 - 1);
  505. test_guard->last_attempted = now - (60*60 + 1);
  506. retval = entry_is_time_to_retry(test_guard,now);
  507. tt_int_op(retval,OP_EQ,1);
  508. test_guard->last_attempted = now - (60*60 - 1);
  509. retval = entry_is_time_to_retry(test_guard,now);
  510. tt_int_op(retval,OP_EQ,0);
  511. test_guard->unreachable_since = now - (6*60*60 + 1);
  512. test_guard->last_attempted = now - (4*60*60 + 1);
  513. retval = entry_is_time_to_retry(test_guard,now);
  514. tt_int_op(retval,OP_EQ,1);
  515. test_guard->unreachable_since = now - (3*24*60*60 - 1);
  516. test_guard->last_attempted = now - (4*60*60 + 1);
  517. retval = entry_is_time_to_retry(test_guard,now);
  518. tt_int_op(retval,OP_EQ,1);
  519. test_guard->unreachable_since = now - (3*24*60*60 + 1);
  520. test_guard->last_attempted = now - (18*60*60 + 1);
  521. retval = entry_is_time_to_retry(test_guard,now);
  522. tt_int_op(retval,OP_EQ,1);
  523. test_guard->unreachable_since = now - (7*24*60*60 - 1);
  524. test_guard->last_attempted = now - (18*60*60 + 1);
  525. retval = entry_is_time_to_retry(test_guard,now);
  526. tt_int_op(retval,OP_EQ,1);
  527. test_guard->last_attempted = now - (18*60*60 - 1);
  528. retval = entry_is_time_to_retry(test_guard,now);
  529. tt_int_op(retval,OP_EQ,0);
  530. test_guard->unreachable_since = now - (7*24*60*60 + 1);
  531. test_guard->last_attempted = now - (36*60*60 + 1);
  532. retval = entry_is_time_to_retry(test_guard,now);
  533. tt_int_op(retval,OP_EQ,1);
  534. test_guard->unreachable_since = now - (7*24*60*60 + 1);
  535. test_guard->last_attempted = now - (36*60*60 + 1);
  536. retval = entry_is_time_to_retry(test_guard,now);
  537. tt_int_op(retval,OP_EQ,1);
  538. done:
  539. tor_free(test_guard);
  540. }
  541. /** XXX Do some tests that entry_is_live() */
  542. static void
  543. test_entry_is_live(void *arg)
  544. {
  545. smartlist_t *our_nodelist = NULL;
  546. guard_selection_t *gs = get_guard_selection_info();
  547. const smartlist_t *all_entry_guards =
  548. get_entry_guards_for_guard_selection(gs);
  549. const node_t *test_node = NULL;
  550. const entry_guard_t *test_entry = NULL;
  551. const char *msg;
  552. int which_node;
  553. (void) arg;
  554. /* The global entry guards smartlist should be empty now. */
  555. tt_int_op(smartlist_len(all_entry_guards), OP_EQ, 0);
  556. /* Walk the nodelist and add all nodes as entry guards. */
  557. our_nodelist = nodelist_get_list();
  558. tt_int_op(smartlist_len(our_nodelist), OP_EQ, HELPER_NUMBER_OF_DESCRIPTORS);
  559. SMARTLIST_FOREACH_BEGIN(our_nodelist, const node_t *, node) {
  560. const node_t *node_tmp;
  561. node_tmp = add_an_entry_guard(gs, node, 0, 1, 0, 0);
  562. tt_assert(node_tmp);
  563. tt_int_op(node->is_stable, OP_EQ, 0);
  564. tt_int_op(node->is_fast, OP_EQ, 0);
  565. } SMARTLIST_FOREACH_END(node);
  566. /* Make sure the nodes were added as entry guards. */
  567. tt_int_op(smartlist_len(all_entry_guards), OP_EQ,
  568. HELPER_NUMBER_OF_DESCRIPTORS);
  569. /* Now get a random test entry that we will use for this unit test. */
  570. which_node = 3; /* (chosen by fair dice roll) */
  571. test_entry = smartlist_get(all_entry_guards, which_node);
  572. /* Let's do some entry_is_live() tests! */
  573. /* Require the node to be stable, but it's not. Should fail.
  574. Also enable 'assume_reachable' because why not. */
  575. test_node = entry_is_live(test_entry,
  576. ENTRY_NEED_UPTIME | ENTRY_ASSUME_REACHABLE,
  577. &msg);
  578. tt_assert(!test_node);
  579. /* Require the node to be fast, but it's not. Should fail. */
  580. test_node = entry_is_live(test_entry,
  581. ENTRY_NEED_CAPACITY | ENTRY_ASSUME_REACHABLE,
  582. &msg);
  583. tt_assert(!test_node);
  584. /* Don't impose any restrictions on the node. Should succeed. */
  585. test_node = entry_is_live(test_entry, 0, &msg);
  586. tt_assert(test_node);
  587. tt_ptr_op(test_node, OP_EQ, node_get_by_id(test_entry->identity));
  588. /* Require descriptor for this node. It has one so it should succeed. */
  589. test_node = entry_is_live(test_entry, ENTRY_NEED_DESCRIPTOR, &msg);
  590. tt_assert(test_node);
  591. tt_ptr_op(test_node, OP_EQ, node_get_by_id(test_entry->identity));
  592. done:
  593. ; /* XXX */
  594. }
  595. #define TEST_IPV4_ADDR "123.45.67.89"
  596. #define TEST_IPV6_ADDR "[1234:5678:90ab:cdef::]"
  597. static void
  598. test_node_preferred_orport(void *arg)
  599. {
  600. (void)arg;
  601. tor_addr_t ipv4_addr;
  602. const uint16_t ipv4_port = 4444;
  603. tor_addr_t ipv6_addr;
  604. const uint16_t ipv6_port = 6666;
  605. routerinfo_t node_ri;
  606. node_t node;
  607. tor_addr_port_t ap;
  608. /* Setup options */
  609. memset(&mocked_options, 0, sizeof(mocked_options));
  610. /* We don't test ClientPreferIPv6ORPort here, because it's used in
  611. * nodelist_set_consensus to setup node.ipv6_preferred, which we set
  612. * directly. */
  613. MOCK(get_options, mock_get_options);
  614. /* Setup IP addresses */
  615. tor_addr_parse(&ipv4_addr, TEST_IPV4_ADDR);
  616. tor_addr_parse(&ipv6_addr, TEST_IPV6_ADDR);
  617. /* Setup node_ri */
  618. memset(&node_ri, 0, sizeof(node_ri));
  619. node_ri.addr = tor_addr_to_ipv4h(&ipv4_addr);
  620. node_ri.or_port = ipv4_port;
  621. tor_addr_copy(&node_ri.ipv6_addr, &ipv6_addr);
  622. node_ri.ipv6_orport = ipv6_port;
  623. /* Setup node */
  624. memset(&node, 0, sizeof(node));
  625. node.ri = &node_ri;
  626. /* Check the preferred address is IPv4 if we're only using IPv4, regardless
  627. * of whether we prefer it or not */
  628. mocked_options.ClientUseIPv4 = 1;
  629. mocked_options.ClientUseIPv6 = 0;
  630. node.ipv6_preferred = 0;
  631. node_get_pref_orport(&node, &ap);
  632. tt_assert(tor_addr_eq(&ap.addr, &ipv4_addr));
  633. tt_assert(ap.port == ipv4_port);
  634. node.ipv6_preferred = 1;
  635. node_get_pref_orport(&node, &ap);
  636. tt_assert(tor_addr_eq(&ap.addr, &ipv4_addr));
  637. tt_assert(ap.port == ipv4_port);
  638. /* Check the preferred address is IPv4 if we're using IPv4 and IPv6, but
  639. * don't prefer the IPv6 address */
  640. mocked_options.ClientUseIPv4 = 1;
  641. mocked_options.ClientUseIPv6 = 1;
  642. node.ipv6_preferred = 0;
  643. node_get_pref_orport(&node, &ap);
  644. tt_assert(tor_addr_eq(&ap.addr, &ipv4_addr));
  645. tt_assert(ap.port == ipv4_port);
  646. /* Check the preferred address is IPv6 if we prefer it and
  647. * ClientUseIPv6 is 1, regardless of ClientUseIPv4 */
  648. mocked_options.ClientUseIPv4 = 1;
  649. mocked_options.ClientUseIPv6 = 1;
  650. node.ipv6_preferred = 1;
  651. node_get_pref_orport(&node, &ap);
  652. tt_assert(tor_addr_eq(&ap.addr, &ipv6_addr));
  653. tt_assert(ap.port == ipv6_port);
  654. mocked_options.ClientUseIPv4 = 0;
  655. node_get_pref_orport(&node, &ap);
  656. tt_assert(tor_addr_eq(&ap.addr, &ipv6_addr));
  657. tt_assert(ap.port == ipv6_port);
  658. /* Check the preferred address is IPv6 if we don't prefer it, but
  659. * ClientUseIPv4 is 0 */
  660. mocked_options.ClientUseIPv4 = 0;
  661. mocked_options.ClientUseIPv6 = 1;
  662. node.ipv6_preferred = fascist_firewall_prefer_ipv6_orport(&mocked_options);
  663. node_get_pref_orport(&node, &ap);
  664. tt_assert(tor_addr_eq(&ap.addr, &ipv6_addr));
  665. tt_assert(ap.port == ipv6_port);
  666. done:
  667. UNMOCK(get_options);
  668. }
  669. static const struct testcase_setup_t fake_network = {
  670. fake_network_setup, fake_network_cleanup
  671. };
  672. struct testcase_t entrynodes_tests[] = {
  673. { "entry_is_time_to_retry", test_entry_is_time_to_retry,
  674. TT_FORK, NULL, NULL },
  675. { "choose_random_entry_no_guards", test_choose_random_entry_no_guards,
  676. TT_FORK, &fake_network, NULL },
  677. { "choose_random_entry_one_possibleguard",
  678. test_choose_random_entry_one_possible_guard,
  679. TT_FORK, &fake_network, NULL },
  680. { "populate_live_entry_guards_1guard",
  681. test_populate_live_entry_guards_1guard,
  682. TT_FORK, &fake_network, NULL },
  683. { "populate_live_entry_guards_3guards",
  684. test_populate_live_entry_guards_3guards,
  685. TT_FORK, &fake_network, NULL },
  686. { "entry_guards_parse_state_simple",
  687. test_entry_guards_parse_state_simple,
  688. TT_FORK, &fake_network, NULL },
  689. { "entry_guards_parse_state_pathbias",
  690. test_entry_guards_parse_state_pathbias,
  691. TT_FORK, &fake_network, NULL },
  692. { "entry_guards_set_from_config",
  693. test_entry_guards_set_from_config,
  694. TT_FORK, &fake_network, NULL },
  695. { "entry_is_live",
  696. test_entry_is_live,
  697. TT_FORK, &fake_network, NULL },
  698. { "node_preferred_orport",
  699. test_node_preferred_orport,
  700. 0, NULL, NULL },
  701. END_OF_TESTCASES
  702. };