test_entrynodes.c 21 KB

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