test_entrynodes.c 26 KB

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