test_entrynodes.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673
  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 "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. 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, 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), OP_GE, 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, OP_EQ, 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, OP_EQ, 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, OP_EQ, 0);
  142. tt_int_op(smartlist_len(live_entry_guards), OP_EQ, 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, OP_EQ, 0);
  152. tt_int_op(smartlist_len(live_entry_guards), OP_EQ, 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, OP_EQ, 1);
  164. tt_int_op(smartlist_len(live_entry_guards), OP_EQ, 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), OP_EQ, 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, OP_GE, 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), OP_EQ, 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, OP_EQ, nickname); /* Verify nickname */
  283. base16_encode(hex_digest, sizeof(hex_digest),
  284. e->identity, DIGEST_LEN);
  285. tt_str_op(hex_digest, OP_EQ, fpr); /* Verify fingerprint */
  286. tt_assert(e->is_dir_cache); /* Verify dirness */
  287. tt_str_op(e->chosen_by_version, OP_EQ, tor_version); /* Verify 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, OP_EQ, 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), OP_EQ, 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, OP_GE, 0);
  357. /* Test that the guard was registered */
  358. all_entry_guards = get_entry_guards();
  359. tt_int_op(smartlist_len(all_entry_guards), OP_EQ, 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, OP_EQ, (int)circ_attempts);
  366. tt_int_op((int)e->circ_successes, OP_EQ, (int)circ_successes);
  367. tt_int_op((int)e->successful_circuits_closed, OP_EQ,
  368. (int)successful_closed);
  369. tt_int_op((int)e->timeouts, OP_EQ, (int)timeouts);
  370. tt_int_op((int)e->collapsed_circuits, OP_EQ, (int)collapsed);
  371. tt_int_op((int)e->unusable_circuits, OP_EQ, (int)unusable);
  372. }
  373. done:
  374. or_state_free(state);
  375. state_lines_free(entry_state_lines);
  376. tor_free(msg);
  377. }
  378. /* Simple test of entry_guards_set_from_config() by specifying a
  379. particular EntryNode and making sure it gets picked. */
  380. static void
  381. test_entry_guards_set_from_config(void *arg)
  382. {
  383. or_options_t *options = get_options_mutable();
  384. const smartlist_t *all_entry_guards = get_entry_guards();
  385. const char *entrynodes_str = "test003r";
  386. const node_t *chosen_entry = NULL;
  387. int retval;
  388. (void) arg;
  389. /* Prase EntryNodes as a routerset. */
  390. options->EntryNodes = routerset_new();
  391. retval = routerset_parse(options->EntryNodes,
  392. entrynodes_str,
  393. "test_entrynodes");
  394. tt_int_op(retval, OP_GE, 0);
  395. /* Read nodes from EntryNodes */
  396. entry_guards_set_from_config(options);
  397. /* Test that only one guard was added. */
  398. tt_int_op(smartlist_len(all_entry_guards), OP_EQ, 1);
  399. /* Make sure it was the guard we specified. */
  400. chosen_entry = choose_random_entry(NULL);
  401. tt_str_op(chosen_entry->ri->nickname, OP_EQ, entrynodes_str);
  402. done:
  403. routerset_free(options->EntryNodes);
  404. }
  405. static void
  406. test_entry_is_time_to_retry(void *arg)
  407. {
  408. entry_guard_t *test_guard;
  409. time_t now;
  410. int retval;
  411. (void)arg;
  412. now = time(NULL);
  413. test_guard = tor_malloc_zero(sizeof(entry_guard_t));
  414. test_guard->last_attempted = now - 10;
  415. test_guard->unreachable_since = now - 1;
  416. retval = entry_is_time_to_retry(test_guard,now);
  417. tt_int_op(retval,OP_EQ,1);
  418. test_guard->unreachable_since = now - (6*60*60 - 1);
  419. test_guard->last_attempted = now - (60*60 + 1);
  420. retval = entry_is_time_to_retry(test_guard,now);
  421. tt_int_op(retval,OP_EQ,1);
  422. test_guard->last_attempted = now - (60*60 - 1);
  423. retval = entry_is_time_to_retry(test_guard,now);
  424. tt_int_op(retval,OP_EQ,0);
  425. test_guard->unreachable_since = now - (6*60*60 + 1);
  426. test_guard->last_attempted = now - (4*60*60 + 1);
  427. retval = entry_is_time_to_retry(test_guard,now);
  428. tt_int_op(retval,OP_EQ,1);
  429. test_guard->unreachable_since = now - (3*24*60*60 - 1);
  430. test_guard->last_attempted = now - (4*60*60 + 1);
  431. retval = entry_is_time_to_retry(test_guard,now);
  432. tt_int_op(retval,OP_EQ,1);
  433. test_guard->unreachable_since = now - (3*24*60*60 + 1);
  434. test_guard->last_attempted = now - (18*60*60 + 1);
  435. retval = entry_is_time_to_retry(test_guard,now);
  436. tt_int_op(retval,OP_EQ,1);
  437. test_guard->unreachable_since = now - (7*24*60*60 - 1);
  438. test_guard->last_attempted = now - (18*60*60 + 1);
  439. retval = entry_is_time_to_retry(test_guard,now);
  440. tt_int_op(retval,OP_EQ,1);
  441. test_guard->last_attempted = now - (18*60*60 - 1);
  442. retval = entry_is_time_to_retry(test_guard,now);
  443. tt_int_op(retval,OP_EQ,0);
  444. test_guard->unreachable_since = now - (7*24*60*60 + 1);
  445. test_guard->last_attempted = now - (36*60*60 + 1);
  446. retval = entry_is_time_to_retry(test_guard,now);
  447. tt_int_op(retval,OP_EQ,1);
  448. test_guard->unreachable_since = now - (7*24*60*60 + 1);
  449. test_guard->last_attempted = now - (36*60*60 + 1);
  450. retval = entry_is_time_to_retry(test_guard,now);
  451. tt_int_op(retval,OP_EQ,1);
  452. done:
  453. tor_free(test_guard);
  454. }
  455. /** XXX Do some tests that entry_is_live() */
  456. static void
  457. test_entry_is_live(void *arg)
  458. {
  459. smartlist_t *our_nodelist = NULL;
  460. const smartlist_t *all_entry_guards = get_entry_guards();
  461. const node_t *test_node = NULL;
  462. const entry_guard_t *test_entry = NULL;
  463. const char *msg;
  464. int which_node;
  465. (void) arg;
  466. /* The global entry guards smartlist should be empty now. */
  467. tt_int_op(smartlist_len(all_entry_guards), OP_EQ, 0);
  468. /* Walk the nodelist and add all nodes as entry guards. */
  469. our_nodelist = nodelist_get_list();
  470. tt_int_op(smartlist_len(our_nodelist), OP_EQ, HELPER_NUMBER_OF_DESCRIPTORS);
  471. SMARTLIST_FOREACH_BEGIN(our_nodelist, const node_t *, node) {
  472. const node_t *node_tmp;
  473. node_tmp = add_an_entry_guard(node, 0, 1, 0, 0);
  474. tt_assert(node_tmp);
  475. tt_int_op(node->is_stable, OP_EQ, 0);
  476. tt_int_op(node->is_fast, OP_EQ, 0);
  477. } SMARTLIST_FOREACH_END(node);
  478. /* Make sure the nodes were added as entry guards. */
  479. tt_int_op(smartlist_len(all_entry_guards), OP_EQ, HELPER_NUMBER_OF_DESCRIPTORS);
  480. /* Now get a random test entry that we will use for this unit test. */
  481. which_node = 3; /* (chosen by fair dice roll) */
  482. test_entry = smartlist_get(all_entry_guards, which_node);
  483. /* Let's do some entry_is_live() tests! */
  484. /* Require the node to be stable, but it's not. Should fail.
  485. Also enable 'assume_reachable' because why not. */
  486. test_node = entry_is_live(test_entry,
  487. ENTRY_NEED_UPTIME | ENTRY_ASSUME_REACHABLE,
  488. &msg);
  489. tt_assert(!test_node);
  490. /* Require the node to be fast, but it's not. Should fail. */
  491. test_node = entry_is_live(test_entry,
  492. ENTRY_NEED_CAPACITY | ENTRY_ASSUME_REACHABLE,
  493. &msg);
  494. tt_assert(!test_node);
  495. /* Don't impose any restrictions on the node. Should succeed. */
  496. test_node = entry_is_live(test_entry, 0, &msg);
  497. tt_assert(test_node);
  498. tt_ptr_op(test_node, OP_EQ, node_get_by_id(test_entry->identity));
  499. /* Require descriptor for this node. It has one so it should succeed. */
  500. test_node = entry_is_live(test_entry, ENTRY_NEED_DESCRIPTOR, &msg);
  501. tt_assert(test_node);
  502. tt_ptr_op(test_node, OP_EQ, node_get_by_id(test_entry->identity));
  503. done:
  504. ; /* XXX */
  505. }
  506. static const struct testcase_setup_t fake_network = {
  507. fake_network_setup, fake_network_cleanup
  508. };
  509. struct testcase_t entrynodes_tests[] = {
  510. { "entry_is_time_to_retry", test_entry_is_time_to_retry,
  511. TT_FORK, NULL, NULL },
  512. { "choose_random_entry_no_guards", test_choose_random_entry_no_guards,
  513. TT_FORK, &fake_network, NULL },
  514. { "choose_random_entry_one_possibleguard",
  515. test_choose_random_entry_one_possible_guard,
  516. TT_FORK, &fake_network, NULL },
  517. { "populate_live_entry_guards_1guard",
  518. test_populate_live_entry_guards_1guard,
  519. TT_FORK, &fake_network, NULL },
  520. { "populate_live_entry_guards_3guards",
  521. test_populate_live_entry_guards_3guards,
  522. TT_FORK, &fake_network, NULL },
  523. { "entry_guards_parse_state_simple",
  524. test_entry_guards_parse_state_simple,
  525. TT_FORK, &fake_network, NULL },
  526. { "entry_guards_parse_state_pathbias",
  527. test_entry_guards_parse_state_pathbias,
  528. TT_FORK, &fake_network, NULL },
  529. { "entry_guards_set_from_config",
  530. test_entry_guards_set_from_config,
  531. TT_FORK, &fake_network, NULL },
  532. { "entry_is_live",
  533. test_entry_is_live,
  534. TT_FORK, &fake_network, NULL },
  535. END_OF_TESTCASES
  536. };