test_entrynodes.c 23 KB

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