test_entrynodes.c 27 KB

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