test_entrynodes.c 24 KB

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