test_entrynodes.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875
  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. const smartlist_t *all_entry_guards = get_entry_guards();
  201. or_options_t *options = get_options_mutable();
  202. int retval;
  203. /* Set NumEntryGuards to the provided number. */
  204. options->NumEntryGuards = num_needed;
  205. tt_int_op(num_needed, OP_EQ, decide_num_guards(options, 0));
  206. /* The global entry guards smartlist should be empty now. */
  207. tt_int_op(smartlist_len(all_entry_guards), OP_EQ, 0);
  208. /* Walk the nodelist and add all nodes as entry guards. */
  209. our_nodelist = nodelist_get_list();
  210. tt_int_op(smartlist_len(our_nodelist), OP_EQ, HELPER_NUMBER_OF_DESCRIPTORS);
  211. SMARTLIST_FOREACH_BEGIN(our_nodelist, const node_t *, node) {
  212. const node_t *node_tmp;
  213. node_tmp = add_an_entry_guard(node, 0, 1, 0, 0);
  214. tt_assert(node_tmp);
  215. } SMARTLIST_FOREACH_END(node);
  216. /* Make sure the nodes were added as entry guards. */
  217. tt_int_op(smartlist_len(all_entry_guards), OP_EQ,
  218. HELPER_NUMBER_OF_DESCRIPTORS);
  219. /* Ensure that all the possible entry guards are enough to satisfy us. */
  220. tt_int_op(smartlist_len(all_entry_guards), OP_GE, num_needed);
  221. /* Walk the entry guard list for some sanity checking */
  222. SMARTLIST_FOREACH_BEGIN(all_entry_guards, const entry_guard_t *, entry) {
  223. /* Since we called add_an_entry_guard() with 'for_discovery' being
  224. False, all guards should have made_contact enabled. */
  225. tt_int_op(entry->made_contact, OP_EQ, 1);
  226. } SMARTLIST_FOREACH_END(entry);
  227. /* First, try to get some fast guards. This should fail. */
  228. retval = populate_live_entry_guards(live_entry_guards,
  229. all_entry_guards,
  230. NULL,
  231. NO_DIRINFO, /* Don't care about DIRINFO*/
  232. 0, 0,
  233. 1); /* We want fast guard! */
  234. tt_int_op(retval, OP_EQ, 0);
  235. tt_int_op(smartlist_len(live_entry_guards), OP_EQ, 0);
  236. /* Now try to get some stable guards. This should fail too. */
  237. retval = populate_live_entry_guards(live_entry_guards,
  238. all_entry_guards,
  239. NULL,
  240. NO_DIRINFO,
  241. 0,
  242. 1, /* We want stable guard! */
  243. 0);
  244. tt_int_op(retval, OP_EQ, 0);
  245. tt_int_op(smartlist_len(live_entry_guards), OP_EQ, 0);
  246. /* Now try to get any guard we can find. This should succeed. */
  247. retval = populate_live_entry_guards(live_entry_guards,
  248. all_entry_guards,
  249. NULL,
  250. NO_DIRINFO,
  251. 0, 0, 0); /* No restrictions! */
  252. /* Since we had more than enough guards in 'all_entry_guards', we
  253. should have added 'num_needed' of them to live_entry_guards.
  254. 'retval' should be 1 since we now have enough live entry guards
  255. to pick one. */
  256. tt_int_op(retval, OP_EQ, 1);
  257. tt_int_op(smartlist_len(live_entry_guards), OP_EQ, num_needed);
  258. done:
  259. smartlist_free(live_entry_guards);
  260. }
  261. /* Test populate_live_entry_guards() for 1 guard node. */
  262. static void
  263. test_populate_live_entry_guards_1guard(void *arg)
  264. {
  265. (void) arg;
  266. populate_live_entry_guards_test_helper(1);
  267. }
  268. /* Test populate_live_entry_guards() for 3 guard nodes. */
  269. static void
  270. test_populate_live_entry_guards_3guards(void *arg)
  271. {
  272. (void) arg;
  273. populate_live_entry_guards_test_helper(3);
  274. }
  275. /** Append some EntryGuard lines to the Tor state at <b>state</b>.
  276. <b>entry_guard_lines</b> is a smartlist containing 2-tuple
  277. smartlists that carry the key and values of the statefile.
  278. As an example:
  279. entry_guard_lines =
  280. (("EntryGuard", "name 67E72FF33D7D41BF11C569646A0A7B4B188340DF DirCache"),
  281. ("EntryGuardDownSince", "2014-06-07 16:02:46 2014-06-07 16:02:46"))
  282. */
  283. static void
  284. state_insert_entry_guard_helper(or_state_t *state,
  285. smartlist_t *entry_guard_lines)
  286. {
  287. config_line_t **next, *line;
  288. next = &state->EntryGuards;
  289. *next = NULL;
  290. /* Loop over all the state lines in the smartlist */
  291. SMARTLIST_FOREACH_BEGIN(entry_guard_lines, const smartlist_t *,state_lines) {
  292. /* Get key and value for each line */
  293. const char *state_key = smartlist_get(state_lines, 0);
  294. const char *state_value = smartlist_get(state_lines, 1);
  295. *next = line = tor_malloc_zero(sizeof(config_line_t));
  296. line->key = tor_strdup(state_key);
  297. tor_asprintf(&line->value, "%s", state_value);
  298. next = &(line->next);
  299. } SMARTLIST_FOREACH_END(state_lines);
  300. }
  301. /** Free memory occupied by <b>entry_guard_lines</b>. */
  302. static void
  303. state_lines_free(smartlist_t *entry_guard_lines)
  304. {
  305. SMARTLIST_FOREACH_BEGIN(entry_guard_lines, smartlist_t *, state_lines) {
  306. char *state_key = smartlist_get(state_lines, 0);
  307. char *state_value = smartlist_get(state_lines, 1);
  308. tor_free(state_key);
  309. tor_free(state_value);
  310. smartlist_free(state_lines);
  311. } SMARTLIST_FOREACH_END(state_lines);
  312. smartlist_free(entry_guard_lines);
  313. }
  314. /* Tests entry_guards_parse_state(). It creates a fake Tor state with
  315. a saved entry guard and makes sure that Tor can parse it and
  316. creates the right entry node out of it.
  317. */
  318. static void
  319. test_entry_guards_parse_state_simple(void *arg)
  320. {
  321. or_state_t *state = or_state_new();
  322. const smartlist_t *all_entry_guards = get_entry_guards();
  323. smartlist_t *entry_state_lines = smartlist_new();
  324. char *msg = NULL;
  325. int retval;
  326. /* Details of our fake guard node */
  327. const char *nickname = "hagbard";
  328. const char *fpr = "B29D536DD1752D542E1FBB3C9CE4449D51298212";
  329. const char *tor_version = "0.2.5.3-alpha-dev";
  330. const char *added_at = get_yesterday_date_str();
  331. const char *unlisted_since = "2014-06-08 16:16:50";
  332. (void) arg;
  333. /* The global entry guards smartlist should be empty now. */
  334. tt_int_op(smartlist_len(all_entry_guards), OP_EQ, 0);
  335. { /* Prepare the state entry */
  336. /* Prepare the smartlist to hold the key/value of each line */
  337. smartlist_t *state_line = smartlist_new();
  338. smartlist_add_asprintf(state_line, "EntryGuard");
  339. smartlist_add_asprintf(state_line, "%s %s %s", nickname, fpr, "DirCache");
  340. smartlist_add(entry_state_lines, state_line);
  341. state_line = smartlist_new();
  342. smartlist_add_asprintf(state_line, "EntryGuardAddedBy");
  343. smartlist_add_asprintf(state_line, "%s %s %s", fpr, tor_version, added_at);
  344. smartlist_add(entry_state_lines, state_line);
  345. state_line = smartlist_new();
  346. smartlist_add_asprintf(state_line, "EntryGuardUnlistedSince");
  347. smartlist_add_asprintf(state_line, "%s", unlisted_since);
  348. smartlist_add(entry_state_lines, state_line);
  349. }
  350. /* Inject our lines in the state */
  351. state_insert_entry_guard_helper(state, entry_state_lines);
  352. /* Parse state */
  353. retval = entry_guards_parse_state(state, 1, &msg);
  354. tt_int_op(retval, OP_GE, 0);
  355. /* Test that the guard was registered.
  356. We need to re-get the entry guard list since its pointer was
  357. overwritten in entry_guards_parse_state(). */
  358. all_entry_guards = get_entry_guards();
  359. tt_int_op(smartlist_len(all_entry_guards), OP_EQ, 1);
  360. { /* Test the entry guard structure */
  361. char hex_digest[1024];
  362. char str_time[1024];
  363. const entry_guard_t *e = smartlist_get(all_entry_guards, 0);
  364. tt_str_op(e->nickname, OP_EQ, nickname); /* Verify nickname */
  365. base16_encode(hex_digest, sizeof(hex_digest),
  366. e->identity, DIGEST_LEN);
  367. tt_str_op(hex_digest, OP_EQ, fpr); /* Verify fingerprint */
  368. tt_assert(e->is_dir_cache); /* Verify dirness */
  369. tt_str_op(e->chosen_by_version, OP_EQ, tor_version); /* Verify version */
  370. tt_assert(e->made_contact); /* All saved guards have been contacted */
  371. tt_assert(e->bad_since); /* Verify bad_since timestamp */
  372. format_iso_time(str_time, e->bad_since);
  373. tt_str_op(str_time, OP_EQ, unlisted_since);
  374. /* The rest should be unset */
  375. tt_assert(!e->unreachable_since);
  376. tt_assert(!e->can_retry);
  377. tt_assert(!e->path_bias_noticed);
  378. tt_assert(!e->path_bias_warned);
  379. tt_assert(!e->path_bias_extreme);
  380. tt_assert(!e->path_bias_disabled);
  381. tt_assert(!e->path_bias_use_noticed);
  382. tt_assert(!e->path_bias_use_extreme);
  383. tt_assert(!e->last_attempted);
  384. }
  385. done:
  386. state_lines_free(entry_state_lines);
  387. or_state_free(state);
  388. tor_free(msg);
  389. }
  390. /** Similar to test_entry_guards_parse_state_simple() but aims to test
  391. the PathBias-related details of the entry guard. */
  392. static void
  393. test_entry_guards_parse_state_pathbias(void *arg)
  394. {
  395. or_state_t *state = or_state_new();
  396. const smartlist_t *all_entry_guards = get_entry_guards();
  397. char *msg = NULL;
  398. int retval;
  399. smartlist_t *entry_state_lines = smartlist_new();
  400. /* Path bias details of the fake guard */
  401. const double circ_attempts = 9;
  402. const double circ_successes = 8;
  403. const double successful_closed = 4;
  404. const double collapsed = 2;
  405. const double unusable = 0;
  406. const double timeouts = 1;
  407. (void) arg;
  408. /* The global entry guards smartlist should be empty now. */
  409. tt_int_op(smartlist_len(all_entry_guards), OP_EQ, 0);
  410. { /* Prepare the state entry */
  411. /* Prepare the smartlist to hold the key/value of each line */
  412. smartlist_t *state_line = smartlist_new();
  413. smartlist_add_asprintf(state_line, "EntryGuard");
  414. smartlist_add_asprintf(state_line,
  415. "givethanks B29D536DD1752D542E1FBB3C9CE4449D51298212 NoDirCache");
  416. smartlist_add(entry_state_lines, state_line);
  417. state_line = smartlist_new();
  418. smartlist_add_asprintf(state_line, "EntryGuardAddedBy");
  419. smartlist_add_asprintf(state_line,
  420. "B29D536DD1752D542E1FBB3C9CE4449D51298212 0.2.5.3-alpha-dev "
  421. "%s", get_yesterday_date_str());
  422. smartlist_add(entry_state_lines, state_line);
  423. state_line = smartlist_new();
  424. smartlist_add_asprintf(state_line, "EntryGuardUnlistedSince");
  425. smartlist_add_asprintf(state_line, "2014-06-08 16:16:50");
  426. smartlist_add(entry_state_lines, state_line);
  427. state_line = smartlist_new();
  428. smartlist_add_asprintf(state_line, "EntryGuardPathBias");
  429. smartlist_add_asprintf(state_line, "%f %f %f %f %f %f",
  430. circ_attempts, circ_successes, successful_closed,
  431. collapsed, unusable, timeouts);
  432. smartlist_add(entry_state_lines, state_line);
  433. }
  434. /* Inject our lines in the state */
  435. state_insert_entry_guard_helper(state, entry_state_lines);
  436. /* Parse state */
  437. retval = entry_guards_parse_state(state, 1, &msg);
  438. tt_int_op(retval, OP_GE, 0);
  439. /* Test that the guard was registered */
  440. all_entry_guards = get_entry_guards();
  441. tt_int_op(smartlist_len(all_entry_guards), OP_EQ, 1);
  442. { /* Test the path bias of this guard */
  443. const entry_guard_t *e = smartlist_get(all_entry_guards, 0);
  444. tt_assert(!e->is_dir_cache);
  445. tt_assert(!e->can_retry);
  446. /* XXX tt_double_op doesn't support equality. Cast to int for now. */
  447. tt_int_op((int)e->circ_attempts, OP_EQ, (int)circ_attempts);
  448. tt_int_op((int)e->circ_successes, OP_EQ, (int)circ_successes);
  449. tt_int_op((int)e->successful_circuits_closed, OP_EQ,
  450. (int)successful_closed);
  451. tt_int_op((int)e->timeouts, OP_EQ, (int)timeouts);
  452. tt_int_op((int)e->collapsed_circuits, OP_EQ, (int)collapsed);
  453. tt_int_op((int)e->unusable_circuits, OP_EQ, (int)unusable);
  454. }
  455. done:
  456. or_state_free(state);
  457. state_lines_free(entry_state_lines);
  458. tor_free(msg);
  459. }
  460. /* Simple test of entry_guards_set_from_config() by specifying a
  461. particular EntryNode and making sure it gets picked. */
  462. static void
  463. test_entry_guards_set_from_config(void *arg)
  464. {
  465. or_options_t *options = get_options_mutable();
  466. const smartlist_t *all_entry_guards = get_entry_guards();
  467. const char *entrynodes_str = "test003r";
  468. const node_t *chosen_entry = NULL;
  469. int retval;
  470. (void) arg;
  471. /* Prase EntryNodes as a routerset. */
  472. options->EntryNodes = routerset_new();
  473. retval = routerset_parse(options->EntryNodes,
  474. entrynodes_str,
  475. "test_entrynodes");
  476. tt_int_op(retval, OP_GE, 0);
  477. /* Read nodes from EntryNodes */
  478. entry_guards_set_from_config(options);
  479. /* Test that only one guard was added. */
  480. tt_int_op(smartlist_len(all_entry_guards), OP_EQ, 1);
  481. /* Make sure it was the guard we specified. */
  482. chosen_entry = choose_random_entry(NULL);
  483. tt_str_op(chosen_entry->ri->nickname, OP_EQ, entrynodes_str);
  484. done:
  485. routerset_free(options->EntryNodes);
  486. }
  487. static void
  488. test_entry_is_time_to_retry(void *arg)
  489. {
  490. entry_guard_t *test_guard;
  491. time_t now;
  492. int retval;
  493. (void)arg;
  494. now = time(NULL);
  495. test_guard = tor_malloc_zero(sizeof(entry_guard_t));
  496. test_guard->last_attempted = now - 10;
  497. test_guard->unreachable_since = now - 1;
  498. retval = entry_is_time_to_retry(test_guard,now);
  499. tt_int_op(retval,OP_EQ,1);
  500. test_guard->unreachable_since = now - (6*60*60 - 1);
  501. test_guard->last_attempted = now - (60*60 + 1);
  502. retval = entry_is_time_to_retry(test_guard,now);
  503. tt_int_op(retval,OP_EQ,1);
  504. test_guard->last_attempted = now - (60*60 - 1);
  505. retval = entry_is_time_to_retry(test_guard,now);
  506. tt_int_op(retval,OP_EQ,0);
  507. test_guard->unreachable_since = now - (6*60*60 + 1);
  508. test_guard->last_attempted = now - (4*60*60 + 1);
  509. retval = entry_is_time_to_retry(test_guard,now);
  510. tt_int_op(retval,OP_EQ,1);
  511. test_guard->unreachable_since = now - (3*24*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 - (18*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 - (7*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->last_attempted = now - (18*60*60 - 1);
  524. retval = entry_is_time_to_retry(test_guard,now);
  525. tt_int_op(retval,OP_EQ,0);
  526. test_guard->unreachable_since = now - (7*24*60*60 + 1);
  527. test_guard->last_attempted = now - (36*60*60 + 1);
  528. retval = entry_is_time_to_retry(test_guard,now);
  529. tt_int_op(retval,OP_EQ,1);
  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. done:
  535. tor_free(test_guard);
  536. }
  537. /** XXX Do some tests that entry_is_live() */
  538. static void
  539. test_entry_is_live(void *arg)
  540. {
  541. smartlist_t *our_nodelist = NULL;
  542. const smartlist_t *all_entry_guards = get_entry_guards();
  543. const node_t *test_node = NULL;
  544. const entry_guard_t *test_entry = NULL;
  545. const char *msg;
  546. int which_node;
  547. (void) arg;
  548. /* The global entry guards smartlist should be empty now. */
  549. tt_int_op(smartlist_len(all_entry_guards), OP_EQ, 0);
  550. /* Walk the nodelist and add all nodes as entry guards. */
  551. our_nodelist = nodelist_get_list();
  552. tt_int_op(smartlist_len(our_nodelist), OP_EQ, HELPER_NUMBER_OF_DESCRIPTORS);
  553. SMARTLIST_FOREACH_BEGIN(our_nodelist, const node_t *, node) {
  554. const node_t *node_tmp;
  555. node_tmp = add_an_entry_guard(node, 0, 1, 0, 0);
  556. tt_assert(node_tmp);
  557. tt_int_op(node->is_stable, OP_EQ, 0);
  558. tt_int_op(node->is_fast, OP_EQ, 0);
  559. } SMARTLIST_FOREACH_END(node);
  560. /* Make sure the nodes were added as entry guards. */
  561. tt_int_op(smartlist_len(all_entry_guards), OP_EQ,
  562. HELPER_NUMBER_OF_DESCRIPTORS);
  563. /* Now get a random test entry that we will use for this unit test. */
  564. which_node = 3; /* (chosen by fair dice roll) */
  565. test_entry = smartlist_get(all_entry_guards, which_node);
  566. /* Let's do some entry_is_live() tests! */
  567. /* Require the node to be stable, but it's not. Should fail.
  568. Also enable 'assume_reachable' because why not. */
  569. test_node = entry_is_live(test_entry,
  570. ENTRY_NEED_UPTIME | ENTRY_ASSUME_REACHABLE,
  571. &msg);
  572. tt_assert(!test_node);
  573. /* Require the node to be fast, but it's not. Should fail. */
  574. test_node = entry_is_live(test_entry,
  575. ENTRY_NEED_CAPACITY | ENTRY_ASSUME_REACHABLE,
  576. &msg);
  577. tt_assert(!test_node);
  578. /* Don't impose any restrictions on the node. Should succeed. */
  579. test_node = entry_is_live(test_entry, 0, &msg);
  580. tt_assert(test_node);
  581. tt_ptr_op(test_node, OP_EQ, node_get_by_id(test_entry->identity));
  582. /* Require descriptor for this node. It has one so it should succeed. */
  583. test_node = entry_is_live(test_entry, ENTRY_NEED_DESCRIPTOR, &msg);
  584. tt_assert(test_node);
  585. tt_ptr_op(test_node, OP_EQ, node_get_by_id(test_entry->identity));
  586. done:
  587. ; /* XXX */
  588. }
  589. #define TEST_IPV4_ADDR "123.45.67.89"
  590. #define TEST_IPV6_ADDR "[1234:5678:90ab:cdef::]"
  591. static void
  592. test_node_preferred_orport(void *arg)
  593. {
  594. (void)arg;
  595. tor_addr_t ipv4_addr;
  596. const uint16_t ipv4_port = 4444;
  597. tor_addr_t ipv6_addr;
  598. const uint16_t ipv6_port = 6666;
  599. routerinfo_t node_ri;
  600. node_t node;
  601. tor_addr_port_t ap;
  602. /* Setup options */
  603. memset(&mocked_options, 0, sizeof(mocked_options));
  604. /* We don't test ClientPreferIPv6ORPort here, because it's used in
  605. * nodelist_set_consensus to setup node.ipv6_preferred, which we set
  606. * directly. */
  607. MOCK(get_options, mock_get_options);
  608. /* Setup IP addresses */
  609. tor_addr_parse(&ipv4_addr, TEST_IPV4_ADDR);
  610. tor_addr_parse(&ipv6_addr, TEST_IPV6_ADDR);
  611. /* Setup node_ri */
  612. memset(&node_ri, 0, sizeof(node_ri));
  613. node_ri.addr = tor_addr_to_ipv4h(&ipv4_addr);
  614. node_ri.or_port = ipv4_port;
  615. tor_addr_copy(&node_ri.ipv6_addr, &ipv6_addr);
  616. node_ri.ipv6_orport = ipv6_port;
  617. /* Setup node */
  618. memset(&node, 0, sizeof(node));
  619. node.ri = &node_ri;
  620. /* Check the preferred address is IPv4 if we're only using IPv4, regardless
  621. * of whether we prefer it or not */
  622. mocked_options.ClientUseIPv4 = 1;
  623. mocked_options.ClientUseIPv6 = 0;
  624. node.ipv6_preferred = 0;
  625. node_get_pref_orport(&node, &ap);
  626. tt_assert(tor_addr_eq(&ap.addr, &ipv4_addr));
  627. tt_assert(ap.port == ipv4_port);
  628. node.ipv6_preferred = 1;
  629. node_get_pref_orport(&node, &ap);
  630. tt_assert(tor_addr_eq(&ap.addr, &ipv4_addr));
  631. tt_assert(ap.port == ipv4_port);
  632. /* Check the preferred address is IPv4 if we're using IPv4 and IPv6, but
  633. * don't prefer the IPv6 address */
  634. mocked_options.ClientUseIPv4 = 1;
  635. mocked_options.ClientUseIPv6 = 1;
  636. node.ipv6_preferred = 0;
  637. node_get_pref_orport(&node, &ap);
  638. tt_assert(tor_addr_eq(&ap.addr, &ipv4_addr));
  639. tt_assert(ap.port == ipv4_port);
  640. /* Check the preferred address is IPv6 if we prefer it and
  641. * ClientUseIPv6 is 1, regardless of ClientUseIPv4 */
  642. mocked_options.ClientUseIPv4 = 1;
  643. mocked_options.ClientUseIPv6 = 1;
  644. node.ipv6_preferred = 1;
  645. node_get_pref_orport(&node, &ap);
  646. tt_assert(tor_addr_eq(&ap.addr, &ipv6_addr));
  647. tt_assert(ap.port == ipv6_port);
  648. mocked_options.ClientUseIPv4 = 0;
  649. node_get_pref_orport(&node, &ap);
  650. tt_assert(tor_addr_eq(&ap.addr, &ipv6_addr));
  651. tt_assert(ap.port == ipv6_port);
  652. /* Check the preferred address is IPv6 if we don't prefer it, but
  653. * ClientUseIPv4 is 0 */
  654. mocked_options.ClientUseIPv4 = 0;
  655. mocked_options.ClientUseIPv6 = 1;
  656. node.ipv6_preferred = fascist_firewall_prefer_ipv6_orport(&mocked_options);
  657. node_get_pref_orport(&node, &ap);
  658. tt_assert(tor_addr_eq(&ap.addr, &ipv6_addr));
  659. tt_assert(ap.port == ipv6_port);
  660. done:
  661. UNMOCK(get_options);
  662. }
  663. static const struct testcase_setup_t fake_network = {
  664. fake_network_setup, fake_network_cleanup
  665. };
  666. struct testcase_t entrynodes_tests[] = {
  667. { "entry_is_time_to_retry", test_entry_is_time_to_retry,
  668. TT_FORK, NULL, NULL },
  669. { "choose_random_entry_no_guards", test_choose_random_entry_no_guards,
  670. TT_FORK, &fake_network, NULL },
  671. { "choose_random_entry_one_possibleguard",
  672. test_choose_random_entry_one_possible_guard,
  673. TT_FORK, &fake_network, NULL },
  674. { "populate_live_entry_guards_1guard",
  675. test_populate_live_entry_guards_1guard,
  676. TT_FORK, &fake_network, NULL },
  677. { "populate_live_entry_guards_3guards",
  678. test_populate_live_entry_guards_3guards,
  679. TT_FORK, &fake_network, NULL },
  680. { "entry_guards_parse_state_simple",
  681. test_entry_guards_parse_state_simple,
  682. TT_FORK, &fake_network, NULL },
  683. { "entry_guards_parse_state_pathbias",
  684. test_entry_guards_parse_state_pathbias,
  685. TT_FORK, &fake_network, NULL },
  686. { "entry_guards_set_from_config",
  687. test_entry_guards_set_from_config,
  688. TT_FORK, &fake_network, NULL },
  689. { "entry_is_live",
  690. test_entry_is_live,
  691. TT_FORK, &fake_network, NULL },
  692. { "node_preferred_orport",
  693. test_node_preferred_orport,
  694. 0, NULL, NULL },
  695. END_OF_TESTCASES
  696. };