test_entrynodes.c 27 KB

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