test_entrynodes.c 23 KB

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