test_entrynodes.c 21 KB

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