test_entrynodes.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141
  1. /* Copyright (c) 2014-2016, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. #include "orconfig.h"
  4. #define STATEFILE_PRIVATE
  5. #define ENTRYNODES_PRIVATE
  6. #define ROUTERLIST_PRIVATE
  7. #include "or.h"
  8. #include "test.h"
  9. #include "config.h"
  10. #include "entrynodes.h"
  11. #include "nodelist.h"
  12. #include "policies.h"
  13. #include "routerlist.h"
  14. #include "routerparse.h"
  15. #include "routerset.h"
  16. #include "statefile.h"
  17. #include "util.h"
  18. #include "test_helpers.h"
  19. /* TODO:
  20. * choose_random_entry() test with state set.
  21. *
  22. * parse_state() tests with more than one guards.
  23. *
  24. * More tests for set_from_config(): Multiple nodes, use fingerprints,
  25. * use country codes.
  26. */
  27. /** Dummy Tor state used in unittests. */
  28. static or_state_t *dummy_state = NULL;
  29. static or_state_t *
  30. get_or_state_replacement(void)
  31. {
  32. return dummy_state;
  33. }
  34. /* Unittest cleanup function: Cleanup the fake network. */
  35. static int
  36. fake_network_cleanup(const struct testcase_t *testcase, void *ptr)
  37. {
  38. (void) testcase;
  39. (void) ptr;
  40. routerlist_free_all();
  41. nodelist_free_all();
  42. entry_guards_free_all();
  43. or_state_free(dummy_state);
  44. return 1; /* NOP */
  45. }
  46. /* Unittest setup function: Setup a fake network. */
  47. static void *
  48. fake_network_setup(const struct testcase_t *testcase)
  49. {
  50. (void) testcase;
  51. /* Setup fake state */
  52. dummy_state = tor_malloc_zero(sizeof(or_state_t));
  53. MOCK(get_or_state,
  54. get_or_state_replacement);
  55. /* Setup fake routerlist. */
  56. helper_setup_fake_routerlist();
  57. /* Return anything but NULL (it's interpreted as test fail) */
  58. return dummy_state;
  59. }
  60. static or_options_t mocked_options;
  61. static const or_options_t *
  62. mock_get_options(void)
  63. {
  64. return &mocked_options;
  65. }
  66. /** Test choose_random_entry() with none of our routers being guard nodes. */
  67. static void
  68. test_choose_random_entry_no_guards(void *arg)
  69. {
  70. const node_t *chosen_entry = NULL;
  71. (void) arg;
  72. MOCK(get_options, mock_get_options);
  73. /* Check that we get a guard if it passes preferred
  74. * address settings */
  75. memset(&mocked_options, 0, sizeof(mocked_options));
  76. mocked_options.ClientUseIPv4 = 1;
  77. mocked_options.ClientPreferIPv6ORPort = 0;
  78. /* Try to pick an entry even though none of our routers are guards. */
  79. chosen_entry = choose_random_entry(NULL);
  80. /* Unintuitively, we actually pick a random node as our entry,
  81. because router_choose_random_node() relaxes its constraints if it
  82. can't find a proper entry guard. */
  83. tt_assert(chosen_entry);
  84. /* And with the other IP version active */
  85. mocked_options.ClientUseIPv6 = 1;
  86. chosen_entry = choose_random_entry(NULL);
  87. tt_assert(chosen_entry);
  88. /* And with the preference on auto */
  89. mocked_options.ClientPreferIPv6ORPort = -1;
  90. chosen_entry = choose_random_entry(NULL);
  91. tt_assert(chosen_entry);
  92. /* Check that we don't get a guard if it doesn't pass mandatory address
  93. * settings */
  94. memset(&mocked_options, 0, sizeof(mocked_options));
  95. mocked_options.ClientUseIPv4 = 0;
  96. mocked_options.ClientPreferIPv6ORPort = 0;
  97. chosen_entry = choose_random_entry(NULL);
  98. /* If we don't allow IPv4 at all, we don't get a guard*/
  99. tt_assert(!chosen_entry);
  100. /* Check that we get a guard if it passes allowed but not preferred address
  101. * settings */
  102. memset(&mocked_options, 0, sizeof(mocked_options));
  103. mocked_options.ClientUseIPv4 = 1;
  104. mocked_options.ClientUseIPv6 = 1;
  105. mocked_options.ClientPreferIPv6ORPort = 1;
  106. chosen_entry = choose_random_entry(NULL);
  107. tt_assert(chosen_entry);
  108. /* Check that we get a guard if it passes preferred address settings when
  109. * they're auto */
  110. memset(&mocked_options, 0, sizeof(mocked_options));
  111. mocked_options.ClientUseIPv4 = 1;
  112. mocked_options.ClientPreferIPv6ORPort = -1;
  113. chosen_entry = choose_random_entry(NULL);
  114. tt_assert(chosen_entry);
  115. /* And with IPv6 active */
  116. mocked_options.ClientUseIPv6 = 1;
  117. chosen_entry = choose_random_entry(NULL);
  118. tt_assert(chosen_entry);
  119. done:
  120. memset(&mocked_options, 0, sizeof(mocked_options));
  121. UNMOCK(get_options);
  122. }
  123. /** Test choose_random_entry() with only one of our routers being a
  124. guard node. */
  125. static void
  126. test_choose_random_entry_one_possible_guard(void *arg)
  127. {
  128. const node_t *chosen_entry = NULL;
  129. node_t *the_guard = NULL;
  130. smartlist_t *our_nodelist = NULL;
  131. (void) arg;
  132. MOCK(get_options, mock_get_options);
  133. /* Set one of the nodes to be a guard. */
  134. our_nodelist = nodelist_get_list();
  135. the_guard = smartlist_get(our_nodelist, 4); /* chosen by fair dice roll */
  136. the_guard->is_possible_guard = 1;
  137. /* Check that we get the guard if it passes preferred
  138. * address settings */
  139. memset(&mocked_options, 0, sizeof(mocked_options));
  140. mocked_options.ClientUseIPv4 = 1;
  141. mocked_options.ClientPreferIPv6ORPort = 0;
  142. /* Pick an entry. Make sure we pick the node we marked as guard. */
  143. chosen_entry = choose_random_entry(NULL);
  144. tt_ptr_op(chosen_entry, OP_EQ, the_guard);
  145. /* And with the other IP version active */
  146. mocked_options.ClientUseIPv6 = 1;
  147. chosen_entry = choose_random_entry(NULL);
  148. tt_ptr_op(chosen_entry, OP_EQ, the_guard);
  149. /* And with the preference on auto */
  150. mocked_options.ClientPreferIPv6ORPort = -1;
  151. chosen_entry = choose_random_entry(NULL);
  152. tt_ptr_op(chosen_entry, OP_EQ, the_guard);
  153. /* Check that we don't get a guard if it doesn't pass mandatory address
  154. * settings */
  155. memset(&mocked_options, 0, sizeof(mocked_options));
  156. mocked_options.ClientUseIPv4 = 0;
  157. mocked_options.ClientPreferIPv6ORPort = 0;
  158. chosen_entry = choose_random_entry(NULL);
  159. /* If we don't allow IPv4 at all, we don't get a guard*/
  160. tt_assert(!chosen_entry);
  161. /* Check that we get a node if it passes allowed but not preferred
  162. * address settings */
  163. memset(&mocked_options, 0, sizeof(mocked_options));
  164. mocked_options.ClientUseIPv4 = 1;
  165. mocked_options.ClientUseIPv6 = 1;
  166. mocked_options.ClientPreferIPv6ORPort = 1;
  167. chosen_entry = choose_random_entry(NULL);
  168. /* We disable the guard check and the preferred address check at the same
  169. * time, so we can't be sure we get the guard */
  170. tt_assert(chosen_entry);
  171. /* Check that we get a node if it is allowed but not preferred when settings
  172. * are auto */
  173. memset(&mocked_options, 0, sizeof(mocked_options));
  174. mocked_options.ClientUseIPv4 = 1;
  175. mocked_options.ClientPreferIPv6ORPort = -1;
  176. chosen_entry = choose_random_entry(NULL);
  177. /* We disable the guard check and the preferred address check at the same
  178. * time, so we can't be sure we get the guard */
  179. tt_assert(chosen_entry);
  180. /* and with IPv6 active */
  181. mocked_options.ClientUseIPv6 = 1;
  182. chosen_entry = choose_random_entry(NULL);
  183. tt_assert(chosen_entry);
  184. done:
  185. memset(&mocked_options, 0, sizeof(mocked_options));
  186. UNMOCK(get_options);
  187. }
  188. /** Helper to conduct tests for populate_live_entry_guards().
  189. This test adds some entry guards to our list, and then tests
  190. populate_live_entry_guards() to mke sure it filters them correctly.
  191. <b>num_needed</b> is the number of guard nodes we support. It's
  192. configurable to make sure we function properly with 1 or 3 guard
  193. nodes configured.
  194. */
  195. static void
  196. populate_live_entry_guards_test_helper(int num_needed)
  197. {
  198. smartlist_t *our_nodelist = NULL;
  199. smartlist_t *live_entry_guards = smartlist_new();
  200. guard_selection_t *gs = get_guard_selection_info();
  201. const smartlist_t *all_entry_guards =
  202. get_entry_guards_for_guard_selection(gs);
  203. or_options_t *options = get_options_mutable();
  204. int retval;
  205. /* Set NumEntryGuards to the provided number. */
  206. options->NumEntryGuards = num_needed;
  207. tt_int_op(num_needed, OP_EQ, decide_num_guards(options, 0));
  208. /* The global entry guards smartlist should be empty now. */
  209. tt_int_op(smartlist_len(all_entry_guards), OP_EQ, 0);
  210. /* Walk the nodelist and add all nodes as entry guards. */
  211. our_nodelist = nodelist_get_list();
  212. tt_int_op(smartlist_len(our_nodelist), OP_EQ, HELPER_NUMBER_OF_DESCRIPTORS);
  213. SMARTLIST_FOREACH_BEGIN(our_nodelist, const node_t *, node) {
  214. const node_t *node_tmp;
  215. node_tmp = add_an_entry_guard(gs, node, 0, 1, 0, 0);
  216. tt_assert(node_tmp);
  217. } SMARTLIST_FOREACH_END(node);
  218. /* Make sure the nodes were added as entry guards. */
  219. tt_int_op(smartlist_len(all_entry_guards), OP_EQ,
  220. HELPER_NUMBER_OF_DESCRIPTORS);
  221. /* Ensure that all the possible entry guards are enough to satisfy us. */
  222. tt_int_op(smartlist_len(all_entry_guards), OP_GE, num_needed);
  223. /* Walk the entry guard list for some sanity checking */
  224. SMARTLIST_FOREACH_BEGIN(all_entry_guards, const entry_guard_t *, entry) {
  225. /* Since we called add_an_entry_guard() with 'for_discovery' being
  226. False, all guards should have made_contact enabled. */
  227. tt_int_op(entry->made_contact, OP_EQ, 1);
  228. } SMARTLIST_FOREACH_END(entry);
  229. /* First, try to get some fast guards. This should fail. */
  230. retval = populate_live_entry_guards(live_entry_guards,
  231. all_entry_guards,
  232. NULL,
  233. NO_DIRINFO, /* Don't care about DIRINFO*/
  234. 0, 0,
  235. 1); /* We want fast guard! */
  236. tt_int_op(retval, OP_EQ, 0);
  237. tt_int_op(smartlist_len(live_entry_guards), OP_EQ, 0);
  238. /* Now try to get some stable guards. This should fail too. */
  239. retval = populate_live_entry_guards(live_entry_guards,
  240. all_entry_guards,
  241. NULL,
  242. NO_DIRINFO,
  243. 0,
  244. 1, /* We want stable guard! */
  245. 0);
  246. tt_int_op(retval, OP_EQ, 0);
  247. tt_int_op(smartlist_len(live_entry_guards), OP_EQ, 0);
  248. /* Now try to get any guard we can find. This should succeed. */
  249. retval = populate_live_entry_guards(live_entry_guards,
  250. all_entry_guards,
  251. NULL,
  252. NO_DIRINFO,
  253. 0, 0, 0); /* No restrictions! */
  254. /* Since we had more than enough guards in 'all_entry_guards', we
  255. should have added 'num_needed' of them to live_entry_guards.
  256. 'retval' should be 1 since we now have enough live entry guards
  257. to pick one. */
  258. tt_int_op(retval, OP_EQ, 1);
  259. tt_int_op(smartlist_len(live_entry_guards), OP_EQ, num_needed);
  260. done:
  261. smartlist_free(live_entry_guards);
  262. }
  263. /* Test populate_live_entry_guards() for 1 guard node. */
  264. static void
  265. test_populate_live_entry_guards_1guard(void *arg)
  266. {
  267. (void) arg;
  268. populate_live_entry_guards_test_helper(1);
  269. }
  270. /* Test populate_live_entry_guards() for 3 guard nodes. */
  271. static void
  272. test_populate_live_entry_guards_3guards(void *arg)
  273. {
  274. (void) arg;
  275. populate_live_entry_guards_test_helper(3);
  276. }
  277. /** Append some EntryGuard lines to the Tor state at <b>state</b>.
  278. <b>entry_guard_lines</b> is a smartlist containing 2-tuple
  279. smartlists that carry the key and values of the statefile.
  280. As an example:
  281. entry_guard_lines =
  282. (("EntryGuard", "name 67E72FF33D7D41BF11C569646A0A7B4B188340DF DirCache"),
  283. ("EntryGuardDownSince", "2014-06-07 16:02:46 2014-06-07 16:02:46"))
  284. */
  285. static void
  286. state_insert_entry_guard_helper(or_state_t *state,
  287. smartlist_t *entry_guard_lines)
  288. {
  289. config_line_t **next, *line;
  290. next = &state->EntryGuards;
  291. *next = NULL;
  292. /* Loop over all the state lines in the smartlist */
  293. SMARTLIST_FOREACH_BEGIN(entry_guard_lines, const smartlist_t *,state_lines) {
  294. /* Get key and value for each line */
  295. const char *state_key = smartlist_get(state_lines, 0);
  296. const char *state_value = smartlist_get(state_lines, 1);
  297. *next = line = tor_malloc_zero(sizeof(config_line_t));
  298. line->key = tor_strdup(state_key);
  299. tor_asprintf(&line->value, "%s", state_value);
  300. next = &(line->next);
  301. } SMARTLIST_FOREACH_END(state_lines);
  302. }
  303. /** Free memory occupied by <b>entry_guard_lines</b>. */
  304. static void
  305. state_lines_free(smartlist_t *entry_guard_lines)
  306. {
  307. SMARTLIST_FOREACH_BEGIN(entry_guard_lines, smartlist_t *, state_lines) {
  308. char *state_key = smartlist_get(state_lines, 0);
  309. char *state_value = smartlist_get(state_lines, 1);
  310. tor_free(state_key);
  311. tor_free(state_value);
  312. smartlist_free(state_lines);
  313. } SMARTLIST_FOREACH_END(state_lines);
  314. smartlist_free(entry_guard_lines);
  315. }
  316. /* Tests entry_guards_parse_state(). It creates a fake Tor state with
  317. a saved entry guard and makes sure that Tor can parse it and
  318. creates the right entry node out of it.
  319. */
  320. static void
  321. test_entry_guards_parse_state_simple(void *arg)
  322. {
  323. or_state_t *state = or_state_new();
  324. const smartlist_t *all_entry_guards = get_entry_guards();
  325. smartlist_t *entry_state_lines = smartlist_new();
  326. char *msg = NULL;
  327. int retval;
  328. /* Details of our fake guard node */
  329. const char *nickname = "hagbard";
  330. const char *fpr = "B29D536DD1752D542E1FBB3C9CE4449D51298212";
  331. const char *tor_version = "0.2.5.3-alpha-dev";
  332. const char *added_at = get_yesterday_date_str();
  333. const char *unlisted_since = "2014-06-08 16:16:50";
  334. (void) arg;
  335. /* The global entry guards smartlist should be empty now. */
  336. tt_int_op(smartlist_len(all_entry_guards), OP_EQ, 0);
  337. { /* Prepare the state entry */
  338. /* Prepare the smartlist to hold the key/value of each line */
  339. smartlist_t *state_line = smartlist_new();
  340. smartlist_add_asprintf(state_line, "EntryGuard");
  341. smartlist_add_asprintf(state_line, "%s %s %s", nickname, fpr, "DirCache");
  342. smartlist_add(entry_state_lines, state_line);
  343. state_line = smartlist_new();
  344. smartlist_add_asprintf(state_line, "EntryGuardAddedBy");
  345. smartlist_add_asprintf(state_line, "%s %s %s", fpr, tor_version, added_at);
  346. smartlist_add(entry_state_lines, state_line);
  347. state_line = smartlist_new();
  348. smartlist_add_asprintf(state_line, "EntryGuardUnlistedSince");
  349. smartlist_add_asprintf(state_line, "%s", unlisted_since);
  350. smartlist_add(entry_state_lines, state_line);
  351. }
  352. /* Inject our lines in the state */
  353. state_insert_entry_guard_helper(state, entry_state_lines);
  354. /* Parse state */
  355. retval = entry_guards_parse_state(state, 1, &msg);
  356. tt_int_op(retval, OP_GE, 0);
  357. /* Test that the guard was registered.
  358. We need to re-get the entry guard list since its pointer was
  359. overwritten in entry_guards_parse_state(). */
  360. all_entry_guards = get_entry_guards();
  361. tt_int_op(smartlist_len(all_entry_guards), OP_EQ, 1);
  362. { /* Test the entry guard structure */
  363. char hex_digest[1024];
  364. char str_time[1024];
  365. const entry_guard_t *e = smartlist_get(all_entry_guards, 0);
  366. tt_str_op(e->nickname, OP_EQ, nickname); /* Verify nickname */
  367. base16_encode(hex_digest, sizeof(hex_digest),
  368. e->identity, DIGEST_LEN);
  369. tt_str_op(hex_digest, OP_EQ, fpr); /* Verify fingerprint */
  370. tt_assert(e->is_dir_cache); /* Verify dirness */
  371. tt_str_op(e->chosen_by_version, OP_EQ, tor_version); /* Verify version */
  372. tt_assert(e->made_contact); /* All saved guards have been contacted */
  373. tt_assert(e->bad_since); /* Verify bad_since timestamp */
  374. format_iso_time(str_time, e->bad_since);
  375. tt_str_op(str_time, OP_EQ, unlisted_since);
  376. /* The rest should be unset */
  377. tt_assert(!e->unreachable_since);
  378. tt_assert(!e->can_retry);
  379. tt_assert(!e->pb.path_bias_noticed);
  380. tt_assert(!e->pb.path_bias_warned);
  381. tt_assert(!e->pb.path_bias_extreme);
  382. tt_assert(!e->pb.path_bias_disabled);
  383. tt_assert(!e->pb.path_bias_use_noticed);
  384. tt_assert(!e->pb.path_bias_use_extreme);
  385. tt_assert(!e->last_attempted);
  386. }
  387. done:
  388. state_lines_free(entry_state_lines);
  389. or_state_free(state);
  390. tor_free(msg);
  391. }
  392. /** Similar to test_entry_guards_parse_state_simple() but aims to test
  393. the PathBias-related details of the entry guard. */
  394. static void
  395. test_entry_guards_parse_state_pathbias(void *arg)
  396. {
  397. or_state_t *state = or_state_new();
  398. const smartlist_t *all_entry_guards = get_entry_guards();
  399. char *msg = NULL;
  400. int retval;
  401. smartlist_t *entry_state_lines = smartlist_new();
  402. /* Path bias details of the fake guard */
  403. const double circ_attempts = 9;
  404. const double circ_successes = 8;
  405. const double successful_closed = 4;
  406. const double collapsed = 2;
  407. const double unusable = 0;
  408. const double timeouts = 1;
  409. (void) arg;
  410. /* The global entry guards smartlist should be empty now. */
  411. tt_int_op(smartlist_len(all_entry_guards), OP_EQ, 0);
  412. { /* Prepare the state entry */
  413. /* Prepare the smartlist to hold the key/value of each line */
  414. smartlist_t *state_line = smartlist_new();
  415. smartlist_add_asprintf(state_line, "EntryGuard");
  416. smartlist_add_asprintf(state_line,
  417. "givethanks B29D536DD1752D542E1FBB3C9CE4449D51298212 NoDirCache");
  418. smartlist_add(entry_state_lines, state_line);
  419. state_line = smartlist_new();
  420. smartlist_add_asprintf(state_line, "EntryGuardAddedBy");
  421. smartlist_add_asprintf(state_line,
  422. "B29D536DD1752D542E1FBB3C9CE4449D51298212 0.2.5.3-alpha-dev "
  423. "%s", get_yesterday_date_str());
  424. smartlist_add(entry_state_lines, state_line);
  425. state_line = smartlist_new();
  426. smartlist_add_asprintf(state_line, "EntryGuardUnlistedSince");
  427. smartlist_add_asprintf(state_line, "2014-06-08 16:16:50");
  428. smartlist_add(entry_state_lines, state_line);
  429. state_line = smartlist_new();
  430. smartlist_add_asprintf(state_line, "EntryGuardPathBias");
  431. smartlist_add_asprintf(state_line, "%f %f %f %f %f %f",
  432. circ_attempts, circ_successes, successful_closed,
  433. collapsed, unusable, timeouts);
  434. smartlist_add(entry_state_lines, state_line);
  435. }
  436. /* Inject our lines in the state */
  437. state_insert_entry_guard_helper(state, entry_state_lines);
  438. /* Parse state */
  439. retval = entry_guards_parse_state(state, 1, &msg);
  440. tt_int_op(retval, OP_GE, 0);
  441. /* Test that the guard was registered */
  442. all_entry_guards = get_entry_guards();
  443. tt_int_op(smartlist_len(all_entry_guards), OP_EQ, 1);
  444. { /* Test the path bias of this guard */
  445. const entry_guard_t *e = smartlist_get(all_entry_guards, 0);
  446. tt_assert(!e->is_dir_cache);
  447. tt_assert(!e->can_retry);
  448. /* XXX tt_double_op doesn't support equality. Cast to int for now. */
  449. tt_int_op((int)e->pb.circ_attempts, OP_EQ, (int)circ_attempts);
  450. tt_int_op((int)e->pb.circ_successes, OP_EQ, (int)circ_successes);
  451. tt_int_op((int)e->pb.successful_circuits_closed, OP_EQ,
  452. (int)successful_closed);
  453. tt_int_op((int)e->pb.timeouts, OP_EQ, (int)timeouts);
  454. tt_int_op((int)e->pb.collapsed_circuits, OP_EQ, (int)collapsed);
  455. tt_int_op((int)e->pb.unusable_circuits, OP_EQ, (int)unusable);
  456. }
  457. done:
  458. or_state_free(state);
  459. state_lines_free(entry_state_lines);
  460. tor_free(msg);
  461. }
  462. /* Simple test of entry_guards_set_from_config() by specifying a
  463. particular EntryNode and making sure it gets picked. */
  464. static void
  465. test_entry_guards_set_from_config(void *arg)
  466. {
  467. or_options_t *options = get_options_mutable();
  468. guard_selection_t *gs = get_guard_selection_info();
  469. const smartlist_t *all_entry_guards =
  470. get_entry_guards_for_guard_selection(gs);
  471. const char *entrynodes_str = "test003r";
  472. const node_t *chosen_entry = NULL;
  473. int retval;
  474. (void) arg;
  475. /* Prase EntryNodes as a routerset. */
  476. options->EntryNodes = routerset_new();
  477. retval = routerset_parse(options->EntryNodes,
  478. entrynodes_str,
  479. "test_entrynodes");
  480. tt_int_op(retval, OP_GE, 0);
  481. /* Read nodes from EntryNodes */
  482. entry_guards_set_from_config(gs, options);
  483. /* Test that only one guard was added. */
  484. tt_int_op(smartlist_len(all_entry_guards), OP_EQ, 1);
  485. /* Make sure it was the guard we specified. */
  486. chosen_entry = choose_random_entry(NULL);
  487. tt_str_op(chosen_entry->ri->nickname, OP_EQ, entrynodes_str);
  488. done:
  489. routerset_free(options->EntryNodes);
  490. }
  491. static void
  492. test_entry_is_time_to_retry(void *arg)
  493. {
  494. entry_guard_t *test_guard;
  495. time_t now;
  496. int retval;
  497. (void)arg;
  498. now = time(NULL);
  499. test_guard = tor_malloc_zero(sizeof(entry_guard_t));
  500. test_guard->last_attempted = now - 10;
  501. test_guard->unreachable_since = now - 1;
  502. retval = entry_is_time_to_retry(test_guard,now);
  503. tt_int_op(retval,OP_EQ,1);
  504. test_guard->unreachable_since = now - (6*60*60 - 1);
  505. test_guard->last_attempted = now - (60*60 + 1);
  506. retval = entry_is_time_to_retry(test_guard,now);
  507. tt_int_op(retval,OP_EQ,1);
  508. test_guard->last_attempted = now - (60*60 - 1);
  509. retval = entry_is_time_to_retry(test_guard,now);
  510. tt_int_op(retval,OP_EQ,0);
  511. test_guard->unreachable_since = now - (6*60*60 + 1);
  512. test_guard->last_attempted = now - (4*60*60 + 1);
  513. retval = entry_is_time_to_retry(test_guard,now);
  514. tt_int_op(retval,OP_EQ,1);
  515. test_guard->unreachable_since = now - (3*24*60*60 - 1);
  516. test_guard->last_attempted = now - (4*60*60 + 1);
  517. retval = entry_is_time_to_retry(test_guard,now);
  518. tt_int_op(retval,OP_EQ,1);
  519. test_guard->unreachable_since = now - (3*24*60*60 + 1);
  520. test_guard->last_attempted = now - (18*60*60 + 1);
  521. retval = entry_is_time_to_retry(test_guard,now);
  522. tt_int_op(retval,OP_EQ,1);
  523. test_guard->unreachable_since = now - (7*24*60*60 - 1);
  524. test_guard->last_attempted = now - (18*60*60 + 1);
  525. retval = entry_is_time_to_retry(test_guard,now);
  526. tt_int_op(retval,OP_EQ,1);
  527. test_guard->last_attempted = now - (18*60*60 - 1);
  528. retval = entry_is_time_to_retry(test_guard,now);
  529. tt_int_op(retval,OP_EQ,0);
  530. test_guard->unreachable_since = now - (7*24*60*60 + 1);
  531. test_guard->last_attempted = now - (36*60*60 + 1);
  532. retval = entry_is_time_to_retry(test_guard,now);
  533. tt_int_op(retval,OP_EQ,1);
  534. test_guard->unreachable_since = now - (7*24*60*60 + 1);
  535. test_guard->last_attempted = now - (36*60*60 + 1);
  536. retval = entry_is_time_to_retry(test_guard,now);
  537. tt_int_op(retval,OP_EQ,1);
  538. done:
  539. tor_free(test_guard);
  540. }
  541. /** XXX Do some tests that entry_is_live() */
  542. static void
  543. test_entry_is_live(void *arg)
  544. {
  545. smartlist_t *our_nodelist = NULL;
  546. guard_selection_t *gs = get_guard_selection_info();
  547. const smartlist_t *all_entry_guards =
  548. get_entry_guards_for_guard_selection(gs);
  549. const node_t *test_node = NULL;
  550. const entry_guard_t *test_entry = NULL;
  551. const char *msg;
  552. int which_node;
  553. (void) arg;
  554. /* The global entry guards smartlist should be empty now. */
  555. tt_int_op(smartlist_len(all_entry_guards), OP_EQ, 0);
  556. /* Walk the nodelist and add all nodes as entry guards. */
  557. our_nodelist = nodelist_get_list();
  558. tt_int_op(smartlist_len(our_nodelist), OP_EQ, HELPER_NUMBER_OF_DESCRIPTORS);
  559. SMARTLIST_FOREACH_BEGIN(our_nodelist, const node_t *, node) {
  560. const node_t *node_tmp;
  561. node_tmp = add_an_entry_guard(gs, node, 0, 1, 0, 0);
  562. tt_assert(node_tmp);
  563. tt_int_op(node->is_stable, OP_EQ, 0);
  564. tt_int_op(node->is_fast, OP_EQ, 0);
  565. } SMARTLIST_FOREACH_END(node);
  566. /* Make sure the nodes were added as entry guards. */
  567. tt_int_op(smartlist_len(all_entry_guards), OP_EQ,
  568. HELPER_NUMBER_OF_DESCRIPTORS);
  569. /* Now get a random test entry that we will use for this unit test. */
  570. which_node = 3; /* (chosen by fair dice roll) */
  571. test_entry = smartlist_get(all_entry_guards, which_node);
  572. /* Let's do some entry_is_live() tests! */
  573. /* Require the node to be stable, but it's not. Should fail.
  574. Also enable 'assume_reachable' because why not. */
  575. test_node = entry_is_live(test_entry,
  576. ENTRY_NEED_UPTIME | ENTRY_ASSUME_REACHABLE,
  577. &msg);
  578. tt_assert(!test_node);
  579. /* Require the node to be fast, but it's not. Should fail. */
  580. test_node = entry_is_live(test_entry,
  581. ENTRY_NEED_CAPACITY | ENTRY_ASSUME_REACHABLE,
  582. &msg);
  583. tt_assert(!test_node);
  584. /* Don't impose any restrictions on the node. Should succeed. */
  585. test_node = entry_is_live(test_entry, 0, &msg);
  586. tt_assert(test_node);
  587. tt_ptr_op(test_node, OP_EQ, node_get_by_id(test_entry->identity));
  588. /* Require descriptor for this node. It has one so it should succeed. */
  589. test_node = entry_is_live(test_entry, ENTRY_NEED_DESCRIPTOR, &msg);
  590. tt_assert(test_node);
  591. tt_ptr_op(test_node, OP_EQ, node_get_by_id(test_entry->identity));
  592. done:
  593. ; /* XXX */
  594. }
  595. #define TEST_IPV4_ADDR "123.45.67.89"
  596. #define TEST_IPV6_ADDR "[1234:5678:90ab:cdef::]"
  597. static void
  598. test_node_preferred_orport(void *arg)
  599. {
  600. (void)arg;
  601. tor_addr_t ipv4_addr;
  602. const uint16_t ipv4_port = 4444;
  603. tor_addr_t ipv6_addr;
  604. const uint16_t ipv6_port = 6666;
  605. routerinfo_t node_ri;
  606. node_t node;
  607. tor_addr_port_t ap;
  608. /* Setup options */
  609. memset(&mocked_options, 0, sizeof(mocked_options));
  610. /* We don't test ClientPreferIPv6ORPort here, because it's used in
  611. * nodelist_set_consensus to setup node.ipv6_preferred, which we set
  612. * directly. */
  613. MOCK(get_options, mock_get_options);
  614. /* Setup IP addresses */
  615. tor_addr_parse(&ipv4_addr, TEST_IPV4_ADDR);
  616. tor_addr_parse(&ipv6_addr, TEST_IPV6_ADDR);
  617. /* Setup node_ri */
  618. memset(&node_ri, 0, sizeof(node_ri));
  619. node_ri.addr = tor_addr_to_ipv4h(&ipv4_addr);
  620. node_ri.or_port = ipv4_port;
  621. tor_addr_copy(&node_ri.ipv6_addr, &ipv6_addr);
  622. node_ri.ipv6_orport = ipv6_port;
  623. /* Setup node */
  624. memset(&node, 0, sizeof(node));
  625. node.ri = &node_ri;
  626. /* Check the preferred address is IPv4 if we're only using IPv4, regardless
  627. * of whether we prefer it or not */
  628. mocked_options.ClientUseIPv4 = 1;
  629. mocked_options.ClientUseIPv6 = 0;
  630. node.ipv6_preferred = 0;
  631. node_get_pref_orport(&node, &ap);
  632. tt_assert(tor_addr_eq(&ap.addr, &ipv4_addr));
  633. tt_assert(ap.port == ipv4_port);
  634. node.ipv6_preferred = 1;
  635. node_get_pref_orport(&node, &ap);
  636. tt_assert(tor_addr_eq(&ap.addr, &ipv4_addr));
  637. tt_assert(ap.port == ipv4_port);
  638. /* Check the preferred address is IPv4 if we're using IPv4 and IPv6, but
  639. * don't prefer the IPv6 address */
  640. mocked_options.ClientUseIPv4 = 1;
  641. mocked_options.ClientUseIPv6 = 1;
  642. node.ipv6_preferred = 0;
  643. node_get_pref_orport(&node, &ap);
  644. tt_assert(tor_addr_eq(&ap.addr, &ipv4_addr));
  645. tt_assert(ap.port == ipv4_port);
  646. /* Check the preferred address is IPv6 if we prefer it and
  647. * ClientUseIPv6 is 1, regardless of ClientUseIPv4 */
  648. mocked_options.ClientUseIPv4 = 1;
  649. mocked_options.ClientUseIPv6 = 1;
  650. node.ipv6_preferred = 1;
  651. node_get_pref_orport(&node, &ap);
  652. tt_assert(tor_addr_eq(&ap.addr, &ipv6_addr));
  653. tt_assert(ap.port == ipv6_port);
  654. mocked_options.ClientUseIPv4 = 0;
  655. node_get_pref_orport(&node, &ap);
  656. tt_assert(tor_addr_eq(&ap.addr, &ipv6_addr));
  657. tt_assert(ap.port == ipv6_port);
  658. /* Check the preferred address is IPv6 if we don't prefer it, but
  659. * ClientUseIPv4 is 0 */
  660. mocked_options.ClientUseIPv4 = 0;
  661. mocked_options.ClientUseIPv6 = 1;
  662. node.ipv6_preferred = fascist_firewall_prefer_ipv6_orport(&mocked_options);
  663. node_get_pref_orport(&node, &ap);
  664. tt_assert(tor_addr_eq(&ap.addr, &ipv6_addr));
  665. tt_assert(ap.port == ipv6_port);
  666. done:
  667. UNMOCK(get_options);
  668. }
  669. static void
  670. test_entry_guard_describe(void *arg)
  671. {
  672. (void)arg;
  673. entry_guard_t g;
  674. memset(&g, 0, sizeof(g));
  675. strlcpy(g.nickname, "okefenokee", sizeof(g.nickname));
  676. memcpy(g.identity, "theforestprimeval---", DIGEST_LEN);
  677. tt_str_op(entry_guard_describe(&g), OP_EQ,
  678. "okefenokee ($746865666F726573747072696D6576616C2D2D2D)");
  679. done:
  680. ;
  681. }
  682. static void
  683. test_entry_guard_randomize_time(void *arg)
  684. {
  685. const time_t now = 1479153573;
  686. const int delay = 86400;
  687. const int N = 1000;
  688. (void)arg;
  689. time_t t;
  690. int i;
  691. for (i = 0; i < N; ++i) {
  692. t = randomize_time(now, delay);
  693. tt_int_op(t, OP_LE, now);
  694. tt_int_op(t, OP_GE, now-delay);
  695. }
  696. /* now try the corner cases */
  697. for (i = 0; i < N; ++i) {
  698. t = randomize_time(100, delay);
  699. tt_int_op(t, OP_GE, 1);
  700. tt_int_op(t, OP_LE, 100);
  701. t = randomize_time(0, delay);
  702. tt_int_op(t, OP_EQ, 1);
  703. }
  704. done:
  705. ;
  706. }
  707. static void
  708. test_entry_guard_encode_for_state_minimal(void *arg)
  709. {
  710. (void) arg;
  711. entry_guard_t *eg = tor_malloc_zero(sizeof(entry_guard_t));
  712. memcpy(eg->identity, "plurpyflurpyslurpydo", DIGEST_LEN);
  713. eg->sampled_on_date = 1479081600;
  714. eg->confirmed_idx = -1;
  715. char *s = NULL;
  716. s = entry_guard_encode_for_state(eg);
  717. tt_str_op(s, OP_EQ,
  718. "rsa_id=706C75727079666C75727079736C75727079646F "
  719. "sampled_on=2016-11-14T00:00:00 "
  720. "listed=0");
  721. done:
  722. entry_guard_free(eg);
  723. tor_free(s);
  724. }
  725. static void
  726. test_entry_guard_encode_for_state_maximal(void *arg)
  727. {
  728. (void) arg;
  729. entry_guard_t *eg = tor_malloc_zero(sizeof(entry_guard_t));
  730. strlcpy(eg->nickname, "Fred", sizeof(eg->nickname));
  731. memcpy(eg->identity, "plurpyflurpyslurpydo", DIGEST_LEN);
  732. eg->sampled_on_date = 1479081600;
  733. eg->sampled_by_version = tor_strdup("1.2.3");
  734. eg->unlisted_since_date = 1479081645;
  735. eg->currently_listed = 1;
  736. eg->confirmed_on_date = 1479081690;
  737. eg->confirmed_idx = 333;
  738. eg->extra_state_fields = tor_strdup("and the green grass grew all around");
  739. char *s = NULL;
  740. s = entry_guard_encode_for_state(eg);
  741. tt_str_op(s, OP_EQ,
  742. "rsa_id=706C75727079666C75727079736C75727079646F "
  743. "nickname=Fred "
  744. "sampled_on=2016-11-14T00:00:00 "
  745. "sampled_by=1.2.3 "
  746. "unlisted_since=2016-11-14T00:00:45 "
  747. "listed=1 "
  748. "confirmed_on=2016-11-14T00:01:30 "
  749. "confirmed_idx=333 "
  750. "and the green grass grew all around");
  751. done:
  752. entry_guard_free(eg);
  753. tor_free(s);
  754. }
  755. static void
  756. test_entry_guard_parse_from_state_minimal(void *arg)
  757. {
  758. (void)arg;
  759. char *mem_op_hex_tmp = NULL;
  760. entry_guard_t *eg = NULL;
  761. time_t t = approx_time();
  762. eg = entry_guard_parse_from_state(
  763. "rsa_id=596f75206d6179206e656564206120686f626279");
  764. tt_assert(eg);
  765. test_mem_op_hex(eg->identity, OP_EQ,
  766. "596f75206d6179206e656564206120686f626279");
  767. tt_str_op(eg->nickname, OP_EQ, "$596F75206D6179206E656564206120686F626279");
  768. tt_i64_op(eg->sampled_on_date, OP_GE, t);
  769. tt_i64_op(eg->sampled_on_date, OP_LE, t+86400);
  770. tt_i64_op(eg->unlisted_since_date, OP_EQ, 0);
  771. tt_ptr_op(eg->sampled_by_version, OP_EQ, NULL);
  772. tt_int_op(eg->currently_listed, OP_EQ, 0);
  773. tt_i64_op(eg->confirmed_on_date, OP_EQ, 0);
  774. tt_int_op(eg->confirmed_idx, OP_EQ, -1);
  775. tt_int_op(eg->last_tried_to_connect, OP_EQ, 0);
  776. tt_int_op(eg->is_reachable, OP_EQ, GUARD_REACHABLE_MAYBE);
  777. done:
  778. entry_guard_free(eg);
  779. tor_free(mem_op_hex_tmp);
  780. }
  781. static void
  782. test_entry_guard_parse_from_state_maximal(void *arg)
  783. {
  784. (void)arg;
  785. char *mem_op_hex_tmp = NULL;
  786. entry_guard_t *eg = NULL;
  787. eg = entry_guard_parse_from_state(
  788. "rsa_id=706C75727079666C75727079736C75727079646F "
  789. "nickname=Fred "
  790. "sampled_on=2016-11-14T00:00:00 "
  791. "sampled_by=1.2.3 "
  792. "unlisted_since=2016-11-14T00:00:45 "
  793. "listed=1 "
  794. "confirmed_on=2016-11-14T00:01:30 "
  795. "confirmed_idx=333 "
  796. "and the green grass grew all around "
  797. "rsa_id=all,around");
  798. tt_assert(eg);
  799. test_mem_op_hex(eg->identity, OP_EQ,
  800. "706C75727079666C75727079736C75727079646F");
  801. tt_str_op(eg->nickname, OP_EQ, "Fred");
  802. tt_i64_op(eg->sampled_on_date, OP_EQ, 1479081600);
  803. tt_i64_op(eg->unlisted_since_date, OP_EQ, 1479081645);
  804. tt_str_op(eg->sampled_by_version, OP_EQ, "1.2.3");
  805. tt_int_op(eg->currently_listed, OP_EQ, 1);
  806. tt_i64_op(eg->confirmed_on_date, OP_EQ, 1479081690);
  807. tt_int_op(eg->confirmed_idx, OP_EQ, 333);
  808. tt_str_op(eg->extra_state_fields, OP_EQ,
  809. "and the green grass grew all around rsa_id=all,around");
  810. tt_int_op(eg->last_tried_to_connect, OP_EQ, 0);
  811. tt_int_op(eg->is_reachable, OP_EQ, GUARD_REACHABLE_MAYBE);
  812. done:
  813. entry_guard_free(eg);
  814. tor_free(mem_op_hex_tmp);
  815. }
  816. static void
  817. test_entry_guard_parse_from_state_failure(void *arg)
  818. {
  819. (void)arg;
  820. entry_guard_t *eg = NULL;
  821. /* no RSA ID. */
  822. eg = entry_guard_parse_from_state("nickname=Fred");
  823. tt_assert(! eg);
  824. /* Bad RSA ID: bad character. */
  825. eg = entry_guard_parse_from_state(
  826. "rsa_id=596f75206d6179206e656564206120686f62627q");
  827. tt_assert(! eg);
  828. /* Bad RSA ID: too long.*/
  829. eg = entry_guard_parse_from_state(
  830. "rsa_id=596f75206d6179206e656564206120686f6262703");
  831. tt_assert(! eg);
  832. /* Bad RSA ID: too short.*/
  833. eg = entry_guard_parse_from_state(
  834. "rsa_id=596f75206d6179206e65656420612");
  835. tt_assert(! eg);
  836. done:
  837. entry_guard_free(eg);
  838. }
  839. static void
  840. test_entry_guard_parse_from_state_partial_failure(void *arg)
  841. {
  842. (void)arg;
  843. char *mem_op_hex_tmp = NULL;
  844. entry_guard_t *eg = NULL;
  845. time_t t = approx_time();
  846. eg = entry_guard_parse_from_state(
  847. "rsa_id=706C75727079666C75727079736C75727079646F "
  848. "nickname=FredIsANodeWithAStrangeNicknameThatIsTooLong "
  849. "sampled_on=2016-11-14T00:00:99 "
  850. "sampled_by=1.2.3 stuff in the middle "
  851. "unlisted_since=2016-xx-14T00:00:45 "
  852. "listed=0 "
  853. "confirmed_on=2016-11-14T00:01:30zz "
  854. "confirmed_idx=idx "
  855. "and the green grass grew all around "
  856. "rsa_id=all,around");
  857. tt_assert(eg);
  858. test_mem_op_hex(eg->identity, OP_EQ,
  859. "706C75727079666C75727079736C75727079646F");
  860. tt_str_op(eg->nickname, OP_EQ, "FredIsANodeWithAStrangeNicknameThatIsTooL");
  861. tt_i64_op(eg->sampled_on_date, OP_EQ, t);
  862. tt_i64_op(eg->unlisted_since_date, OP_EQ, 0);
  863. tt_str_op(eg->sampled_by_version, OP_EQ, "1.2.3");
  864. tt_int_op(eg->currently_listed, OP_EQ, 0);
  865. tt_i64_op(eg->confirmed_on_date, OP_EQ, 0);
  866. tt_int_op(eg->confirmed_idx, OP_EQ, -1);
  867. tt_str_op(eg->extra_state_fields, OP_EQ,
  868. "stuff in the middle and the green grass grew all around "
  869. "rsa_id=all,around");
  870. tt_int_op(eg->last_tried_to_connect, OP_EQ, 0);
  871. tt_int_op(eg->is_reachable, OP_EQ, GUARD_REACHABLE_MAYBE);
  872. done:
  873. entry_guard_free(eg);
  874. tor_free(mem_op_hex_tmp);
  875. }
  876. static const struct testcase_setup_t fake_network = {
  877. fake_network_setup, fake_network_cleanup
  878. };
  879. struct testcase_t entrynodes_tests[] = {
  880. { "entry_is_time_to_retry", test_entry_is_time_to_retry,
  881. TT_FORK, NULL, NULL },
  882. { "choose_random_entry_no_guards", test_choose_random_entry_no_guards,
  883. TT_FORK, &fake_network, NULL },
  884. { "choose_random_entry_one_possibleguard",
  885. test_choose_random_entry_one_possible_guard,
  886. TT_FORK, &fake_network, NULL },
  887. { "populate_live_entry_guards_1guard",
  888. test_populate_live_entry_guards_1guard,
  889. TT_FORK, &fake_network, NULL },
  890. { "populate_live_entry_guards_3guards",
  891. test_populate_live_entry_guards_3guards,
  892. TT_FORK, &fake_network, NULL },
  893. { "entry_guards_parse_state_simple",
  894. test_entry_guards_parse_state_simple,
  895. TT_FORK, &fake_network, NULL },
  896. { "entry_guards_parse_state_pathbias",
  897. test_entry_guards_parse_state_pathbias,
  898. TT_FORK, &fake_network, NULL },
  899. { "entry_guards_set_from_config",
  900. test_entry_guards_set_from_config,
  901. TT_FORK, &fake_network, NULL },
  902. { "entry_is_live",
  903. test_entry_is_live,
  904. TT_FORK, &fake_network, NULL },
  905. { "node_preferred_orport",
  906. test_node_preferred_orport,
  907. 0, NULL, NULL },
  908. { "entry_guard_describe", test_entry_guard_describe, 0, NULL, NULL },
  909. { "randomize_time", test_entry_guard_randomize_time, 0, NULL, NULL },
  910. { "encode_for_state_minimal",
  911. test_entry_guard_encode_for_state_minimal, 0, NULL, NULL },
  912. { "encode_for_state_maximal",
  913. test_entry_guard_encode_for_state_maximal, 0, NULL, NULL },
  914. { "parse_from_state_minimal",
  915. test_entry_guard_parse_from_state_minimal, 0, NULL, NULL },
  916. { "parse_from_state_maximal",
  917. test_entry_guard_parse_from_state_maximal, 0, NULL, NULL },
  918. { "parse_from_state_failure",
  919. test_entry_guard_parse_from_state_failure, 0, NULL, NULL },
  920. { "parse_from_state_partial_failure",
  921. test_entry_guard_parse_from_state_partial_failure, 0, NULL, NULL },
  922. END_OF_TESTCASES
  923. };