test_entrynodes.c 23 KB

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