test_entrynodes.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727
  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. tt_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. tt_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. tor_free(msg);
  352. }
  353. /** Similar to test_entry_guards_parse_state_simple() but aims to test
  354. the PathBias-related details of the entry guard. */
  355. static void
  356. test_entry_guards_parse_state_pathbias(void *arg)
  357. {
  358. or_state_t *state = or_state_new();
  359. const smartlist_t *all_entry_guards = get_entry_guards();
  360. char *msg = NULL;
  361. int retval;
  362. smartlist_t *entry_state_lines = smartlist_new();
  363. /* Path bias details of the fake guard */
  364. const double circ_attempts = 9;
  365. const double circ_successes = 8;
  366. const double successful_closed = 4;
  367. const double collapsed = 2;
  368. const double unusable = 0;
  369. const double timeouts = 1;
  370. (void) arg;
  371. /* The global entry guards smartlist should be empty now. */
  372. tt_int_op(smartlist_len(all_entry_guards), ==, 0);
  373. { /* Prepare the state entry */
  374. /* Prepare the smartlist to hold the key/value of each line */
  375. smartlist_t *state_line = smartlist_new();
  376. smartlist_add_asprintf(state_line, "EntryGuard");
  377. smartlist_add_asprintf(state_line,
  378. "givethanks B29D536DD1752D542E1FBB3C9CE4449D51298212 NoDirCache");
  379. smartlist_add(entry_state_lines, state_line);
  380. state_line = smartlist_new();
  381. smartlist_add_asprintf(state_line, "EntryGuardAddedBy");
  382. smartlist_add_asprintf(state_line,
  383. "B29D536DD1752D542E1FBB3C9CE4449D51298212 0.2.5.3-alpha-dev "
  384. "%s", get_yesterday_date_str());
  385. smartlist_add(entry_state_lines, state_line);
  386. state_line = smartlist_new();
  387. smartlist_add_asprintf(state_line, "EntryGuardUnlistedSince");
  388. smartlist_add_asprintf(state_line, "2014-06-08 16:16:50");
  389. smartlist_add(entry_state_lines, state_line);
  390. state_line = smartlist_new();
  391. smartlist_add_asprintf(state_line, "EntryGuardPathBias");
  392. smartlist_add_asprintf(state_line, "%f %f %f %f %f %f",
  393. circ_attempts, circ_successes, successful_closed,
  394. collapsed, unusable, timeouts);
  395. smartlist_add(entry_state_lines, state_line);
  396. }
  397. /* Inject our lines in the state */
  398. state_insert_entry_guard_helper(state, entry_state_lines);
  399. /* Parse state */
  400. retval = entry_guards_parse_state(state, 1, &msg);
  401. tt_int_op(retval, >=, 0);
  402. /* Test that the guard was registered */
  403. all_entry_guards = get_entry_guards();
  404. tt_int_op(smartlist_len(all_entry_guards), ==, 1);
  405. { /* Test the path bias of this guard */
  406. const entry_guard_t *e = smartlist_get(all_entry_guards, 0);
  407. tt_assert(!e->is_dir_cache);
  408. tt_assert(!e->can_retry);
  409. /* XXX tt_double_op doesn't support equality. Cast to int for now. */
  410. tt_int_op((int)e->circ_attempts, ==, (int)circ_attempts);
  411. tt_int_op((int)e->circ_successes, ==, (int)circ_successes);
  412. tt_int_op((int)e->successful_circuits_closed, ==, (int)successful_closed);
  413. tt_int_op((int)e->timeouts, ==, (int)timeouts);
  414. tt_int_op((int)e->collapsed_circuits, ==, (int)collapsed);
  415. tt_int_op((int)e->unusable_circuits, ==, (int)unusable);
  416. }
  417. done:
  418. or_state_free(state);
  419. state_lines_free(entry_state_lines);
  420. tor_free(msg);
  421. }
  422. /* Simple test of entry_guards_set_from_config() by specifying a
  423. particular EntryNode and making sure it gets picked. */
  424. static void
  425. test_entry_guards_set_from_config(void *arg)
  426. {
  427. or_options_t *options = get_options_mutable();
  428. const smartlist_t *all_entry_guards = get_entry_guards();
  429. const char *entrynodes_str = "test003r";
  430. const node_t *chosen_entry = NULL;
  431. int retval;
  432. (void) arg;
  433. /* Prase EntryNodes as a routerset. */
  434. options->EntryNodes = routerset_new();
  435. retval = routerset_parse(options->EntryNodes,
  436. entrynodes_str,
  437. "test_entrynodes");
  438. tt_int_op(retval, >=, 0);
  439. /* Read nodes from EntryNodes */
  440. entry_guards_set_from_config(options);
  441. /* Test that only one guard was added. */
  442. tt_int_op(smartlist_len(all_entry_guards), ==, 1);
  443. /* Make sure it was the guard we specified. */
  444. chosen_entry = choose_random_entry(NULL);
  445. tt_str_op(chosen_entry->ri->nickname, ==, entrynodes_str);
  446. done:
  447. routerset_free(options->EntryNodes);
  448. }
  449. static void
  450. test_entry_is_time_to_retry(void *arg)
  451. {
  452. entry_guard_t *test_guard;
  453. time_t now;
  454. int retval;
  455. (void)arg;
  456. now = time(NULL);
  457. test_guard = tor_malloc_zero(sizeof(entry_guard_t));
  458. test_guard->last_attempted = now - 10;
  459. test_guard->unreachable_since = now - 1;
  460. retval = entry_is_time_to_retry(test_guard,now);
  461. tt_int_op(retval,==,1);
  462. test_guard->unreachable_since = now - (6*60*60 - 1);
  463. test_guard->last_attempted = now - (60*60 + 1);
  464. retval = entry_is_time_to_retry(test_guard,now);
  465. tt_int_op(retval,==,1);
  466. test_guard->last_attempted = now - (60*60 - 1);
  467. retval = entry_is_time_to_retry(test_guard,now);
  468. tt_int_op(retval,==,0);
  469. test_guard->unreachable_since = now - (6*60*60 + 1);
  470. test_guard->last_attempted = now - (4*60*60 + 1);
  471. retval = entry_is_time_to_retry(test_guard,now);
  472. tt_int_op(retval,==,1);
  473. test_guard->unreachable_since = now - (3*24*60*60 - 1);
  474. test_guard->last_attempted = now - (4*60*60 + 1);
  475. retval = entry_is_time_to_retry(test_guard,now);
  476. tt_int_op(retval,==,1);
  477. test_guard->unreachable_since = now - (3*24*60*60 + 1);
  478. test_guard->last_attempted = now - (18*60*60 + 1);
  479. retval = entry_is_time_to_retry(test_guard,now);
  480. tt_int_op(retval,==,1);
  481. test_guard->unreachable_since = now - (7*24*60*60 - 1);
  482. test_guard->last_attempted = now - (18*60*60 + 1);
  483. retval = entry_is_time_to_retry(test_guard,now);
  484. tt_int_op(retval,==,1);
  485. test_guard->last_attempted = now - (18*60*60 - 1);
  486. retval = entry_is_time_to_retry(test_guard,now);
  487. tt_int_op(retval,==,0);
  488. test_guard->unreachable_since = now - (7*24*60*60 + 1);
  489. test_guard->last_attempted = now - (36*60*60 + 1);
  490. retval = entry_is_time_to_retry(test_guard,now);
  491. tt_int_op(retval,==,1);
  492. test_guard->unreachable_since = now - (7*24*60*60 + 1);
  493. test_guard->last_attempted = now - (36*60*60 + 1);
  494. retval = entry_is_time_to_retry(test_guard,now);
  495. tt_int_op(retval,==,1);
  496. done:
  497. tor_free(test_guard);
  498. }
  499. /** XXX Do some tests that entry_is_live() */
  500. static void
  501. test_entry_is_live(void *arg)
  502. {
  503. smartlist_t *our_nodelist = NULL;
  504. const smartlist_t *all_entry_guards = get_entry_guards();
  505. const node_t *test_node = NULL;
  506. const entry_guard_t *test_entry = NULL;
  507. const char *msg;
  508. int which_node;
  509. (void) arg;
  510. /* The global entry guards smartlist should be empty now. */
  511. tt_int_op(smartlist_len(all_entry_guards), ==, 0);
  512. /* Walk the nodelist and add all nodes as entry guards. */
  513. our_nodelist = nodelist_get_list();
  514. tt_int_op(smartlist_len(our_nodelist), ==, NUMBER_OF_DESCRIPTORS);
  515. SMARTLIST_FOREACH_BEGIN(our_nodelist, const node_t *, node) {
  516. const node_t *node_tmp;
  517. node_tmp = add_an_entry_guard(node, 0, 1, 0, 0);
  518. tt_assert(node_tmp);
  519. tt_int_op(node->is_stable, ==, 0);
  520. tt_int_op(node->is_fast, ==, 0);
  521. } SMARTLIST_FOREACH_END(node);
  522. /* Make sure the nodes were added as entry guards. */
  523. tt_int_op(smartlist_len(all_entry_guards), ==, NUMBER_OF_DESCRIPTORS);
  524. /* Now get a random test entry that we will use for this unit test. */
  525. which_node = 3; /* (chosen by fair dice roll) */
  526. test_entry = smartlist_get(all_entry_guards, which_node);
  527. /* Let's do some entry_is_live() tests! */
  528. /* Require the node to be stable, but it's not. Should fail.
  529. Also enable 'assume_reachable' because why not. */
  530. test_node = entry_is_live(test_entry,
  531. ENTRY_NEED_UPTIME | ENTRY_ASSUME_REACHABLE,
  532. &msg);
  533. tt_assert(!test_node);
  534. /* Require the node to be fast, but it's not. Should fail. */
  535. test_node = entry_is_live(test_entry,
  536. ENTRY_NEED_CAPACITY | ENTRY_ASSUME_REACHABLE,
  537. &msg);
  538. tt_assert(!test_node);
  539. /* Don't impose any restrictions on the node. Should succeed. */
  540. test_node = entry_is_live(test_entry, 0, &msg);
  541. tt_assert(test_node);
  542. tt_ptr_op(test_node, ==, node_get_by_id(test_entry->identity));
  543. /* Require descriptor for this node. It has one so it should succeed. */
  544. test_node = entry_is_live(test_entry, ENTRY_NEED_DESCRIPTOR, &msg);
  545. tt_assert(test_node);
  546. tt_ptr_op(test_node, ==, node_get_by_id(test_entry->identity));
  547. done:
  548. ; /* XXX */
  549. }
  550. static const struct testcase_setup_t fake_network = {
  551. fake_network_setup, fake_network_cleanup
  552. };
  553. struct testcase_t entrynodes_tests[] = {
  554. { "entry_is_time_to_retry", test_entry_is_time_to_retry,
  555. TT_FORK, NULL, NULL },
  556. { "choose_random_entry_no_guards", test_choose_random_entry_no_guards,
  557. TT_FORK, &fake_network, NULL },
  558. { "choose_random_entry_one_possibleguard",
  559. test_choose_random_entry_one_possible_guard,
  560. TT_FORK, &fake_network, NULL },
  561. { "populate_live_entry_guards_1guard",
  562. test_populate_live_entry_guards_1guard,
  563. TT_FORK, &fake_network, NULL },
  564. { "populate_live_entry_guards_3guards",
  565. test_populate_live_entry_guards_3guards,
  566. TT_FORK, &fake_network, NULL },
  567. { "entry_guards_parse_state_simple",
  568. test_entry_guards_parse_state_simple,
  569. TT_FORK, &fake_network, NULL },
  570. { "entry_guards_parse_state_pathbias",
  571. test_entry_guards_parse_state_pathbias,
  572. TT_FORK, &fake_network, NULL },
  573. { "entry_guards_set_from_config",
  574. test_entry_guards_set_from_config,
  575. TT_FORK, &fake_network, NULL },
  576. { "entry_is_live",
  577. test_entry_is_live,
  578. TT_FORK, &fake_network, NULL },
  579. END_OF_TESTCASES
  580. };