test_entrynodes.c 73 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263
  1. /* Copyright (c) 2014-2016, 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 "bridges.h"
  10. #include "config.h"
  11. #include "entrynodes.h"
  12. #include "nodelist.h"
  13. #include "networkstatus.h"
  14. #include "policies.h"
  15. #include "routerlist.h"
  16. #include "routerparse.h"
  17. #include "routerset.h"
  18. #include "statefile.h"
  19. #include "util.h"
  20. #include "test_helpers.h"
  21. #include "log_test_helpers.h"
  22. /* TODO:
  23. * choose_random_entry() test with state set.
  24. *
  25. * parse_state() tests with more than one guards.
  26. *
  27. * More tests for set_from_config(): Multiple nodes, use fingerprints,
  28. * use country codes.
  29. */
  30. /** Dummy Tor state used in unittests. */
  31. static or_state_t *dummy_state = NULL;
  32. static or_state_t *
  33. get_or_state_replacement(void)
  34. {
  35. return dummy_state;
  36. }
  37. /* Unittest cleanup function: Cleanup the fake network. */
  38. static int
  39. fake_network_cleanup(const struct testcase_t *testcase, void *ptr)
  40. {
  41. (void) testcase;
  42. (void) ptr;
  43. routerlist_free_all();
  44. nodelist_free_all();
  45. entry_guards_free_all();
  46. or_state_free(dummy_state);
  47. return 1; /* NOP */
  48. }
  49. /* Unittest setup function: Setup a fake network. */
  50. static void *
  51. fake_network_setup(const struct testcase_t *testcase)
  52. {
  53. (void) testcase;
  54. /* Setup fake state */
  55. dummy_state = tor_malloc_zero(sizeof(or_state_t));
  56. MOCK(get_or_state,
  57. get_or_state_replacement);
  58. /* Setup fake routerlist. */
  59. helper_setup_fake_routerlist();
  60. /* Return anything but NULL (it's interpreted as test fail) */
  61. return dummy_state;
  62. }
  63. static networkstatus_t *dummy_consensus = NULL;
  64. static smartlist_t *big_fake_net_nodes = NULL;
  65. static smartlist_t *
  66. bfn_mock_nodelist_get_list(void)
  67. {
  68. return big_fake_net_nodes;
  69. }
  70. static networkstatus_t *
  71. bfn_mock_networkstatus_get_live_consensus(time_t now)
  72. {
  73. (void)now;
  74. return dummy_consensus;
  75. }
  76. static const node_t *
  77. bfn_mock_node_get_by_id(const char *id)
  78. {
  79. SMARTLIST_FOREACH(big_fake_net_nodes, node_t *, n,
  80. if (fast_memeq(n->identity, id, 20))
  81. return n);
  82. return NULL;
  83. }
  84. /* Unittest cleanup function: Cleanup the fake network. */
  85. static int
  86. big_fake_network_cleanup(const struct testcase_t *testcase, void *ptr)
  87. {
  88. (void) testcase;
  89. (void) ptr;
  90. if (big_fake_net_nodes) {
  91. SMARTLIST_FOREACH(big_fake_net_nodes, node_t *, n, {
  92. tor_free(n->rs);
  93. tor_free(n->md);
  94. tor_free(n);
  95. });
  96. smartlist_free(big_fake_net_nodes);
  97. }
  98. UNMOCK(nodelist_get_list);
  99. UNMOCK(node_get_by_id);
  100. UNMOCK(get_or_state);
  101. UNMOCK(networkstatus_get_live_consensus);
  102. or_state_free(dummy_state);
  103. dummy_state = NULL;
  104. tor_free(dummy_consensus);
  105. return 1; /* NOP */
  106. }
  107. /* Unittest setup function: Setup a fake network. */
  108. static void *
  109. big_fake_network_setup(const struct testcase_t *testcase)
  110. {
  111. int i;
  112. /* These are minimal node_t objects that only contain the aspects of node_t
  113. * that we need for entrynodes.c. */
  114. const int N_NODES = 271;
  115. big_fake_net_nodes = smartlist_new();
  116. for (i = 0; i < N_NODES; ++i) {
  117. node_t *n = tor_malloc_zero(sizeof(node_t));
  118. n->md = tor_malloc_zero(sizeof(microdesc_t));
  119. crypto_rand(n->identity, sizeof(n->identity));
  120. n->rs = tor_malloc_zero(sizeof(routerstatus_t));
  121. memcpy(n->rs->identity_digest, n->identity, DIGEST_LEN);
  122. n->is_running = n->is_valid = n->is_fast = n->is_stable = 1;
  123. n->rs->addr = 0x04020202;
  124. n->rs->or_port = 1234;
  125. n->rs->is_v2_dir = 1;
  126. n->rs->has_bandwidth = 1;
  127. n->rs->bandwidth_kb = 30;
  128. /* Call half of the nodes a possible guard. */
  129. if (i % 2 == 0) {
  130. n->is_possible_guard = 1;
  131. n->rs->guardfraction_percentage = 100;
  132. n->rs->has_guardfraction = 1;
  133. }
  134. smartlist_add(big_fake_net_nodes, n);
  135. }
  136. dummy_state = tor_malloc_zero(sizeof(or_state_t));
  137. dummy_consensus = tor_malloc_zero(sizeof(networkstatus_t));
  138. dummy_consensus->valid_after = approx_time() - 3600;
  139. dummy_consensus->valid_until = approx_time() + 3600;
  140. MOCK(nodelist_get_list, bfn_mock_nodelist_get_list);
  141. MOCK(node_get_by_id, bfn_mock_node_get_by_id);
  142. MOCK(get_or_state,
  143. get_or_state_replacement);
  144. MOCK(networkstatus_get_live_consensus,
  145. bfn_mock_networkstatus_get_live_consensus);
  146. /* Return anything but NULL (it's interpreted as test fail) */
  147. return (void*)testcase;
  148. }
  149. static time_t
  150. mock_randomize_time_no_randomization(time_t a, time_t b)
  151. {
  152. (void) b;
  153. return a;
  154. }
  155. static or_options_t mocked_options;
  156. static const or_options_t *
  157. mock_get_options(void)
  158. {
  159. return &mocked_options;
  160. }
  161. /** Test choose_random_entry() with none of our routers being guard nodes. */
  162. static void
  163. test_choose_random_entry_no_guards(void *arg)
  164. {
  165. const node_t *chosen_entry = NULL;
  166. (void) arg;
  167. MOCK(get_options, mock_get_options);
  168. /* Check that we get a guard if it passes preferred
  169. * address settings */
  170. memset(&mocked_options, 0, sizeof(mocked_options));
  171. mocked_options.ClientUseIPv4 = 1;
  172. mocked_options.ClientPreferIPv6ORPort = 0;
  173. mocked_options.UseDeprecatedGuardAlgorithm = 1;
  174. /* Try to pick an entry even though none of our routers are guards. */
  175. chosen_entry = choose_random_entry(NULL);
  176. /* Unintuitively, we actually pick a random node as our entry,
  177. because router_choose_random_node() relaxes its constraints if it
  178. can't find a proper entry guard. */
  179. tt_assert(chosen_entry);
  180. /* And with the other IP version active */
  181. mocked_options.ClientUseIPv6 = 1;
  182. chosen_entry = choose_random_entry(NULL);
  183. tt_assert(chosen_entry);
  184. /* And with the preference on auto */
  185. mocked_options.ClientPreferIPv6ORPort = -1;
  186. chosen_entry = choose_random_entry(NULL);
  187. tt_assert(chosen_entry);
  188. /* Check that we don't get a guard if it doesn't pass mandatory address
  189. * settings */
  190. memset(&mocked_options, 0, sizeof(mocked_options));
  191. mocked_options.ClientUseIPv4 = 0;
  192. mocked_options.ClientPreferIPv6ORPort = 0;
  193. mocked_options.UseDeprecatedGuardAlgorithm = 1;
  194. chosen_entry = choose_random_entry(NULL);
  195. /* If we don't allow IPv4 at all, we don't get a guard*/
  196. tt_assert(!chosen_entry);
  197. /* Check that we get a guard if it passes allowed but not preferred address
  198. * settings */
  199. memset(&mocked_options, 0, sizeof(mocked_options));
  200. mocked_options.ClientUseIPv4 = 1;
  201. mocked_options.ClientUseIPv6 = 1;
  202. mocked_options.ClientPreferIPv6ORPort = 1;
  203. mocked_options.UseDeprecatedGuardAlgorithm = 1;
  204. chosen_entry = choose_random_entry(NULL);
  205. tt_assert(chosen_entry);
  206. /* Check that we get a guard if it passes preferred address settings when
  207. * they're auto */
  208. memset(&mocked_options, 0, sizeof(mocked_options));
  209. mocked_options.ClientUseIPv4 = 1;
  210. mocked_options.ClientPreferIPv6ORPort = -1;
  211. mocked_options.UseDeprecatedGuardAlgorithm = 1;
  212. chosen_entry = choose_random_entry(NULL);
  213. tt_assert(chosen_entry);
  214. /* And with IPv6 active */
  215. mocked_options.ClientUseIPv6 = 1;
  216. chosen_entry = choose_random_entry(NULL);
  217. tt_assert(chosen_entry);
  218. done:
  219. memset(&mocked_options, 0, sizeof(mocked_options));
  220. UNMOCK(get_options);
  221. }
  222. /** Test choose_random_entry() with only one of our routers being a
  223. guard node. */
  224. static void
  225. test_choose_random_entry_one_possible_guard(void *arg)
  226. {
  227. const node_t *chosen_entry = NULL;
  228. node_t *the_guard = NULL;
  229. smartlist_t *our_nodelist = NULL;
  230. (void) arg;
  231. MOCK(get_options, mock_get_options);
  232. /* Set one of the nodes to be a guard. */
  233. our_nodelist = nodelist_get_list();
  234. the_guard = smartlist_get(our_nodelist, 4); /* chosen by fair dice roll */
  235. the_guard->is_possible_guard = 1;
  236. /* Check that we get the guard if it passes preferred
  237. * address settings */
  238. memset(&mocked_options, 0, sizeof(mocked_options));
  239. mocked_options.ClientUseIPv4 = 1;
  240. mocked_options.ClientPreferIPv6ORPort = 0;
  241. mocked_options.UseDeprecatedGuardAlgorithm = 1;
  242. /* Pick an entry. Make sure we pick the node we marked as guard. */
  243. chosen_entry = choose_random_entry(NULL);
  244. tt_ptr_op(chosen_entry, OP_EQ, the_guard);
  245. /* And with the other IP version active */
  246. mocked_options.ClientUseIPv6 = 1;
  247. chosen_entry = choose_random_entry(NULL);
  248. tt_ptr_op(chosen_entry, OP_EQ, the_guard);
  249. /* And with the preference on auto */
  250. mocked_options.ClientPreferIPv6ORPort = -1;
  251. chosen_entry = choose_random_entry(NULL);
  252. tt_ptr_op(chosen_entry, OP_EQ, the_guard);
  253. /* Check that we don't get a guard if it doesn't pass mandatory address
  254. * settings */
  255. memset(&mocked_options, 0, sizeof(mocked_options));
  256. mocked_options.ClientUseIPv4 = 0;
  257. mocked_options.ClientPreferIPv6ORPort = 0;
  258. mocked_options.UseDeprecatedGuardAlgorithm = 1;
  259. chosen_entry = choose_random_entry(NULL);
  260. /* If we don't allow IPv4 at all, we don't get a guard*/
  261. tt_assert(!chosen_entry);
  262. /* Check that we get a node if it passes allowed but not preferred
  263. * address settings */
  264. memset(&mocked_options, 0, sizeof(mocked_options));
  265. mocked_options.ClientUseIPv4 = 1;
  266. mocked_options.ClientUseIPv6 = 1;
  267. mocked_options.ClientPreferIPv6ORPort = 1;
  268. mocked_options.UseDeprecatedGuardAlgorithm = 1;
  269. chosen_entry = choose_random_entry(NULL);
  270. /* We disable the guard check and the preferred address check at the same
  271. * time, so we can't be sure we get the guard */
  272. tt_assert(chosen_entry);
  273. /* Check that we get a node if it is allowed but not preferred when settings
  274. * are auto */
  275. memset(&mocked_options, 0, sizeof(mocked_options));
  276. mocked_options.ClientUseIPv4 = 1;
  277. mocked_options.ClientPreferIPv6ORPort = -1;
  278. mocked_options.UseDeprecatedGuardAlgorithm = 1;
  279. chosen_entry = choose_random_entry(NULL);
  280. /* We disable the guard check and the preferred address check at the same
  281. * time, so we can't be sure we get the guard */
  282. tt_assert(chosen_entry);
  283. /* and with IPv6 active */
  284. mocked_options.ClientUseIPv6 = 1;
  285. chosen_entry = choose_random_entry(NULL);
  286. tt_assert(chosen_entry);
  287. done:
  288. memset(&mocked_options, 0, sizeof(mocked_options));
  289. UNMOCK(get_options);
  290. }
  291. /** Helper to conduct tests for populate_live_entry_guards().
  292. This test adds some entry guards to our list, and then tests
  293. populate_live_entry_guards() to mke sure it filters them correctly.
  294. <b>num_needed</b> is the number of guard nodes we support. It's
  295. configurable to make sure we function properly with 1 or 3 guard
  296. nodes configured.
  297. */
  298. static void
  299. populate_live_entry_guards_test_helper(int num_needed)
  300. {
  301. smartlist_t *our_nodelist = NULL;
  302. smartlist_t *live_entry_guards = smartlist_new();
  303. guard_selection_t *gs = get_guard_selection_info();
  304. const smartlist_t *all_entry_guards =
  305. get_entry_guards_for_guard_selection(gs);
  306. or_options_t *options = get_options_mutable();
  307. int retval;
  308. /* Set NumEntryGuards to the provided number. */
  309. options->NumEntryGuards = num_needed;
  310. tt_int_op(num_needed, OP_EQ, decide_num_guards(options, 0));
  311. /* The global entry guards smartlist should be empty now. */
  312. tt_int_op(smartlist_len(all_entry_guards), OP_EQ, 0);
  313. /* Walk the nodelist and add all nodes as entry guards. */
  314. our_nodelist = nodelist_get_list();
  315. tt_int_op(smartlist_len(our_nodelist), OP_EQ, HELPER_NUMBER_OF_DESCRIPTORS);
  316. SMARTLIST_FOREACH_BEGIN(our_nodelist, const node_t *, node) {
  317. const node_t *node_tmp;
  318. node_tmp = add_an_entry_guard(gs, node, 0, 1, 0, 0);
  319. tt_assert(node_tmp);
  320. } SMARTLIST_FOREACH_END(node);
  321. /* Make sure the nodes were added as entry guards. */
  322. tt_int_op(smartlist_len(all_entry_guards), OP_EQ,
  323. HELPER_NUMBER_OF_DESCRIPTORS);
  324. /* Ensure that all the possible entry guards are enough to satisfy us. */
  325. tt_int_op(smartlist_len(all_entry_guards), OP_GE, num_needed);
  326. /* Walk the entry guard list for some sanity checking */
  327. SMARTLIST_FOREACH_BEGIN(all_entry_guards, const entry_guard_t *, entry) {
  328. /* Since we called add_an_entry_guard() with 'for_discovery' being
  329. False, all guards should have made_contact enabled. */
  330. tt_int_op(entry->made_contact, OP_EQ, 1);
  331. } SMARTLIST_FOREACH_END(entry);
  332. /* First, try to get some fast guards. This should fail. */
  333. retval = populate_live_entry_guards(live_entry_guards,
  334. all_entry_guards,
  335. NULL,
  336. NO_DIRINFO, /* Don't care about DIRINFO*/
  337. 0, 0,
  338. 1); /* We want fast guard! */
  339. tt_int_op(retval, OP_EQ, 0);
  340. tt_int_op(smartlist_len(live_entry_guards), OP_EQ, 0);
  341. /* Now try to get some stable guards. This should fail too. */
  342. retval = populate_live_entry_guards(live_entry_guards,
  343. all_entry_guards,
  344. NULL,
  345. NO_DIRINFO,
  346. 0,
  347. 1, /* We want stable guard! */
  348. 0);
  349. tt_int_op(retval, OP_EQ, 0);
  350. tt_int_op(smartlist_len(live_entry_guards), OP_EQ, 0);
  351. /* Now try to get any guard we can find. This should succeed. */
  352. retval = populate_live_entry_guards(live_entry_guards,
  353. all_entry_guards,
  354. NULL,
  355. NO_DIRINFO,
  356. 0, 0, 0); /* No restrictions! */
  357. /* Since we had more than enough guards in 'all_entry_guards', we
  358. should have added 'num_needed' of them to live_entry_guards.
  359. 'retval' should be 1 since we now have enough live entry guards
  360. to pick one. */
  361. tt_int_op(retval, OP_EQ, 1);
  362. tt_int_op(smartlist_len(live_entry_guards), OP_EQ, num_needed);
  363. done:
  364. smartlist_free(live_entry_guards);
  365. }
  366. /* Test populate_live_entry_guards() for 1 guard node. */
  367. static void
  368. test_populate_live_entry_guards_1guard(void *arg)
  369. {
  370. (void) arg;
  371. populate_live_entry_guards_test_helper(1);
  372. }
  373. /* Test populate_live_entry_guards() for 3 guard nodes. */
  374. static void
  375. test_populate_live_entry_guards_3guards(void *arg)
  376. {
  377. (void) arg;
  378. populate_live_entry_guards_test_helper(3);
  379. }
  380. /** Append some EntryGuard lines to the Tor state at <b>state</b>.
  381. <b>entry_guard_lines</b> is a smartlist containing 2-tuple
  382. smartlists that carry the key and values of the statefile.
  383. As an example:
  384. entry_guard_lines =
  385. (("EntryGuard", "name 67E72FF33D7D41BF11C569646A0A7B4B188340DF DirCache"),
  386. ("EntryGuardDownSince", "2014-06-07 16:02:46 2014-06-07 16:02:46"))
  387. */
  388. static void
  389. state_insert_entry_guard_helper(or_state_t *state,
  390. smartlist_t *entry_guard_lines)
  391. {
  392. config_line_t **next, *line;
  393. next = &state->EntryGuards;
  394. *next = NULL;
  395. /* Loop over all the state lines in the smartlist */
  396. SMARTLIST_FOREACH_BEGIN(entry_guard_lines, const smartlist_t *,state_lines) {
  397. /* Get key and value for each line */
  398. const char *state_key = smartlist_get(state_lines, 0);
  399. const char *state_value = smartlist_get(state_lines, 1);
  400. *next = line = tor_malloc_zero(sizeof(config_line_t));
  401. line->key = tor_strdup(state_key);
  402. tor_asprintf(&line->value, "%s", state_value);
  403. next = &(line->next);
  404. } SMARTLIST_FOREACH_END(state_lines);
  405. }
  406. /** Free memory occupied by <b>entry_guard_lines</b>. */
  407. static void
  408. state_lines_free(smartlist_t *entry_guard_lines)
  409. {
  410. SMARTLIST_FOREACH_BEGIN(entry_guard_lines, smartlist_t *, state_lines) {
  411. char *state_key = smartlist_get(state_lines, 0);
  412. char *state_value = smartlist_get(state_lines, 1);
  413. tor_free(state_key);
  414. tor_free(state_value);
  415. smartlist_free(state_lines);
  416. } SMARTLIST_FOREACH_END(state_lines);
  417. smartlist_free(entry_guard_lines);
  418. }
  419. /* Tests entry_guards_parse_state(). It creates a fake Tor state with
  420. a saved entry guard and makes sure that Tor can parse it and
  421. creates the right entry node out of it.
  422. */
  423. static void
  424. test_entry_guards_parse_state_simple(void *arg)
  425. {
  426. or_options_t *options = get_options_mutable();
  427. options->UseDeprecatedGuardAlgorithm = 1;
  428. or_state_t *state = or_state_new();
  429. const smartlist_t *all_entry_guards = get_entry_guards();
  430. smartlist_t *entry_state_lines = smartlist_new();
  431. char *msg = NULL;
  432. int retval;
  433. /* Details of our fake guard node */
  434. const char *nickname = "hagbard";
  435. const char *fpr = "B29D536DD1752D542E1FBB3C9CE4449D51298212";
  436. const char *tor_version = "0.2.5.3-alpha-dev";
  437. const char *added_at = get_yesterday_date_str();
  438. const char *unlisted_since = "2014-06-08 16:16:50";
  439. (void) arg;
  440. /* The global entry guards smartlist should be empty now. */
  441. tt_int_op(smartlist_len(all_entry_guards), OP_EQ, 0);
  442. { /* Prepare the state entry */
  443. /* Prepare the smartlist to hold the key/value of each line */
  444. smartlist_t *state_line = smartlist_new();
  445. smartlist_add_asprintf(state_line, "EntryGuard");
  446. smartlist_add_asprintf(state_line, "%s %s %s", nickname, fpr, "DirCache");
  447. smartlist_add(entry_state_lines, state_line);
  448. state_line = smartlist_new();
  449. smartlist_add_asprintf(state_line, "EntryGuardAddedBy");
  450. smartlist_add_asprintf(state_line, "%s %s %s", fpr, tor_version, added_at);
  451. smartlist_add(entry_state_lines, state_line);
  452. state_line = smartlist_new();
  453. smartlist_add_asprintf(state_line, "EntryGuardUnlistedSince");
  454. smartlist_add_asprintf(state_line, "%s", unlisted_since);
  455. smartlist_add(entry_state_lines, state_line);
  456. }
  457. /* Inject our lines in the state */
  458. state_insert_entry_guard_helper(state, entry_state_lines);
  459. /* Parse state */
  460. retval = entry_guards_parse_state(state, 1, &msg);
  461. tt_int_op(retval, OP_GE, 0);
  462. /* Test that the guard was registered.
  463. We need to re-get the entry guard list since its pointer was
  464. overwritten in entry_guards_parse_state(). */
  465. all_entry_guards = get_entry_guards();
  466. tt_int_op(smartlist_len(all_entry_guards), OP_EQ, 1);
  467. { /* Test the entry guard structure */
  468. char hex_digest[1024];
  469. char str_time[1024];
  470. const entry_guard_t *e = smartlist_get(all_entry_guards, 0);
  471. tt_str_op(e->nickname, OP_EQ, nickname); /* Verify nickname */
  472. base16_encode(hex_digest, sizeof(hex_digest),
  473. e->identity, DIGEST_LEN);
  474. tt_str_op(hex_digest, OP_EQ, fpr); /* Verify fingerprint */
  475. tt_assert(e->is_dir_cache); /* Verify dirness */
  476. tt_str_op(e->chosen_by_version, OP_EQ, tor_version); /* Verify version */
  477. tt_assert(e->made_contact); /* All saved guards have been contacted */
  478. tt_assert(e->bad_since); /* Verify bad_since timestamp */
  479. format_iso_time(str_time, e->bad_since);
  480. tt_str_op(str_time, OP_EQ, unlisted_since);
  481. /* The rest should be unset */
  482. tt_assert(!e->unreachable_since);
  483. tt_assert(!e->can_retry);
  484. tt_assert(!e->pb.path_bias_noticed);
  485. tt_assert(!e->pb.path_bias_warned);
  486. tt_assert(!e->pb.path_bias_extreme);
  487. tt_assert(!e->pb.path_bias_disabled);
  488. tt_assert(!e->pb.path_bias_use_noticed);
  489. tt_assert(!e->pb.path_bias_use_extreme);
  490. tt_assert(!e->last_attempted);
  491. }
  492. done:
  493. state_lines_free(entry_state_lines);
  494. or_state_free(state);
  495. tor_free(msg);
  496. }
  497. /** Similar to test_entry_guards_parse_state_simple() but aims to test
  498. the PathBias-related details of the entry guard. */
  499. static void
  500. test_entry_guards_parse_state_pathbias(void *arg)
  501. {
  502. or_options_t *options = get_options_mutable();
  503. options->UseDeprecatedGuardAlgorithm = 1;
  504. or_state_t *state = or_state_new();
  505. const smartlist_t *all_entry_guards = get_entry_guards();
  506. char *msg = NULL;
  507. int retval;
  508. smartlist_t *entry_state_lines = smartlist_new();
  509. /* Path bias details of the fake guard */
  510. const double circ_attempts = 9;
  511. const double circ_successes = 8;
  512. const double successful_closed = 4;
  513. const double collapsed = 2;
  514. const double unusable = 0;
  515. const double timeouts = 1;
  516. (void) arg;
  517. /* The global entry guards smartlist should be empty now. */
  518. tt_int_op(smartlist_len(all_entry_guards), OP_EQ, 0);
  519. { /* Prepare the state entry */
  520. /* Prepare the smartlist to hold the key/value of each line */
  521. smartlist_t *state_line = smartlist_new();
  522. smartlist_add_asprintf(state_line, "EntryGuard");
  523. smartlist_add_asprintf(state_line,
  524. "givethanks B29D536DD1752D542E1FBB3C9CE4449D51298212 NoDirCache");
  525. smartlist_add(entry_state_lines, state_line);
  526. state_line = smartlist_new();
  527. smartlist_add_asprintf(state_line, "EntryGuardAddedBy");
  528. smartlist_add_asprintf(state_line,
  529. "B29D536DD1752D542E1FBB3C9CE4449D51298212 0.2.5.3-alpha-dev "
  530. "%s", get_yesterday_date_str());
  531. smartlist_add(entry_state_lines, state_line);
  532. state_line = smartlist_new();
  533. smartlist_add_asprintf(state_line, "EntryGuardUnlistedSince");
  534. smartlist_add_asprintf(state_line, "2014-06-08 16:16:50");
  535. smartlist_add(entry_state_lines, state_line);
  536. state_line = smartlist_new();
  537. smartlist_add_asprintf(state_line, "EntryGuardPathBias");
  538. smartlist_add_asprintf(state_line, "%f %f %f %f %f %f",
  539. circ_attempts, circ_successes, successful_closed,
  540. collapsed, unusable, timeouts);
  541. smartlist_add(entry_state_lines, state_line);
  542. }
  543. /* Inject our lines in the state */
  544. state_insert_entry_guard_helper(state, entry_state_lines);
  545. /* Parse state */
  546. retval = entry_guards_parse_state(state, 1, &msg);
  547. tt_int_op(retval, OP_GE, 0);
  548. /* Test that the guard was registered */
  549. all_entry_guards = get_entry_guards();
  550. tt_int_op(smartlist_len(all_entry_guards), OP_EQ, 1);
  551. { /* Test the path bias of this guard */
  552. const entry_guard_t *e = smartlist_get(all_entry_guards, 0);
  553. tt_assert(!e->is_dir_cache);
  554. tt_assert(!e->can_retry);
  555. /* XXX tt_double_op doesn't support equality. Cast to int for now. */
  556. tt_int_op((int)e->pb.circ_attempts, OP_EQ, (int)circ_attempts);
  557. tt_int_op((int)e->pb.circ_successes, OP_EQ, (int)circ_successes);
  558. tt_int_op((int)e->pb.successful_circuits_closed, OP_EQ,
  559. (int)successful_closed);
  560. tt_int_op((int)e->pb.timeouts, OP_EQ, (int)timeouts);
  561. tt_int_op((int)e->pb.collapsed_circuits, OP_EQ, (int)collapsed);
  562. tt_int_op((int)e->pb.unusable_circuits, OP_EQ, (int)unusable);
  563. }
  564. done:
  565. or_state_free(state);
  566. state_lines_free(entry_state_lines);
  567. tor_free(msg);
  568. }
  569. /* Simple test of entry_guards_set_from_config() by specifying a
  570. particular EntryNode and making sure it gets picked. */
  571. static void
  572. test_entry_guards_set_from_config(void *arg)
  573. {
  574. or_options_t *options = get_options_mutable();
  575. options->UseDeprecatedGuardAlgorithm = 1;
  576. guard_selection_t *gs = get_guard_selection_info();
  577. const smartlist_t *all_entry_guards =
  578. get_entry_guards_for_guard_selection(gs);
  579. const char *entrynodes_str = "test003r";
  580. const node_t *chosen_entry = NULL;
  581. int retval;
  582. (void) arg;
  583. /* Prase EntryNodes as a routerset. */
  584. options->EntryNodes = routerset_new();
  585. retval = routerset_parse(options->EntryNodes,
  586. entrynodes_str,
  587. "test_entrynodes");
  588. tt_int_op(retval, OP_GE, 0);
  589. /* Read nodes from EntryNodes */
  590. entry_guards_set_from_config(gs, options);
  591. /* Test that only one guard was added. */
  592. tt_int_op(smartlist_len(all_entry_guards), OP_EQ, 1);
  593. /* Make sure it was the guard we specified. */
  594. chosen_entry = choose_random_entry(NULL);
  595. tt_str_op(chosen_entry->ri->nickname, OP_EQ, entrynodes_str);
  596. done:
  597. routerset_free(options->EntryNodes);
  598. }
  599. static void
  600. test_entry_is_time_to_retry(void *arg)
  601. {
  602. entry_guard_t *test_guard;
  603. time_t now;
  604. int retval;
  605. (void)arg;
  606. now = time(NULL);
  607. test_guard = tor_malloc_zero(sizeof(entry_guard_t));
  608. test_guard->last_attempted = now - 10;
  609. test_guard->unreachable_since = now - 1;
  610. retval = entry_is_time_to_retry(test_guard,now);
  611. tt_int_op(retval,OP_EQ,1);
  612. test_guard->unreachable_since = now - (6*60*60 - 1);
  613. test_guard->last_attempted = now - (60*60 + 1);
  614. retval = entry_is_time_to_retry(test_guard,now);
  615. tt_int_op(retval,OP_EQ,1);
  616. test_guard->last_attempted = now - (60*60 - 1);
  617. retval = entry_is_time_to_retry(test_guard,now);
  618. tt_int_op(retval,OP_EQ,0);
  619. test_guard->unreachable_since = now - (6*60*60 + 1);
  620. test_guard->last_attempted = now - (4*60*60 + 1);
  621. retval = entry_is_time_to_retry(test_guard,now);
  622. tt_int_op(retval,OP_EQ,1);
  623. test_guard->unreachable_since = now - (3*24*60*60 - 1);
  624. test_guard->last_attempted = now - (4*60*60 + 1);
  625. retval = entry_is_time_to_retry(test_guard,now);
  626. tt_int_op(retval,OP_EQ,1);
  627. test_guard->unreachable_since = now - (3*24*60*60 + 1);
  628. test_guard->last_attempted = now - (18*60*60 + 1);
  629. retval = entry_is_time_to_retry(test_guard,now);
  630. tt_int_op(retval,OP_EQ,1);
  631. test_guard->unreachable_since = now - (7*24*60*60 - 1);
  632. test_guard->last_attempted = now - (18*60*60 + 1);
  633. retval = entry_is_time_to_retry(test_guard,now);
  634. tt_int_op(retval,OP_EQ,1);
  635. test_guard->last_attempted = now - (18*60*60 - 1);
  636. retval = entry_is_time_to_retry(test_guard,now);
  637. tt_int_op(retval,OP_EQ,0);
  638. test_guard->unreachable_since = now - (7*24*60*60 + 1);
  639. test_guard->last_attempted = now - (36*60*60 + 1);
  640. retval = entry_is_time_to_retry(test_guard,now);
  641. tt_int_op(retval,OP_EQ,1);
  642. test_guard->unreachable_since = now - (7*24*60*60 + 1);
  643. test_guard->last_attempted = now - (36*60*60 + 1);
  644. retval = entry_is_time_to_retry(test_guard,now);
  645. tt_int_op(retval,OP_EQ,1);
  646. done:
  647. tor_free(test_guard);
  648. }
  649. /** XXX Do some tests that entry_is_live() */
  650. static void
  651. test_entry_is_live(void *arg)
  652. {
  653. smartlist_t *our_nodelist = NULL;
  654. guard_selection_t *gs = get_guard_selection_info();
  655. const smartlist_t *all_entry_guards =
  656. get_entry_guards_for_guard_selection(gs);
  657. const node_t *test_node = NULL;
  658. const entry_guard_t *test_entry = NULL;
  659. const char *msg;
  660. int which_node;
  661. (void) arg;
  662. /* The global entry guards smartlist should be empty now. */
  663. tt_int_op(smartlist_len(all_entry_guards), OP_EQ, 0);
  664. /* Walk the nodelist and add all nodes as entry guards. */
  665. our_nodelist = nodelist_get_list();
  666. tt_int_op(smartlist_len(our_nodelist), OP_EQ, HELPER_NUMBER_OF_DESCRIPTORS);
  667. SMARTLIST_FOREACH_BEGIN(our_nodelist, const node_t *, node) {
  668. const node_t *node_tmp;
  669. node_tmp = add_an_entry_guard(gs, node, 0, 1, 0, 0);
  670. tt_assert(node_tmp);
  671. tt_int_op(node->is_stable, OP_EQ, 0);
  672. tt_int_op(node->is_fast, OP_EQ, 0);
  673. } SMARTLIST_FOREACH_END(node);
  674. /* Make sure the nodes were added as entry guards. */
  675. tt_int_op(smartlist_len(all_entry_guards), OP_EQ,
  676. HELPER_NUMBER_OF_DESCRIPTORS);
  677. /* Now get a random test entry that we will use for this unit test. */
  678. which_node = 3; /* (chosen by fair dice roll) */
  679. test_entry = smartlist_get(all_entry_guards, which_node);
  680. /* Let's do some entry_is_live() tests! */
  681. /* Require the node to be stable, but it's not. Should fail.
  682. Also enable 'assume_reachable' because why not. */
  683. test_node = entry_is_live(test_entry,
  684. ENTRY_NEED_UPTIME | ENTRY_ASSUME_REACHABLE,
  685. &msg);
  686. tt_assert(!test_node);
  687. /* Require the node to be fast, but it's not. Should fail. */
  688. test_node = entry_is_live(test_entry,
  689. ENTRY_NEED_CAPACITY | ENTRY_ASSUME_REACHABLE,
  690. &msg);
  691. tt_assert(!test_node);
  692. /* Don't impose any restrictions on the node. Should succeed. */
  693. test_node = entry_is_live(test_entry, 0, &msg);
  694. tt_assert(test_node);
  695. tt_ptr_op(test_node, OP_EQ, node_get_by_id(test_entry->identity));
  696. /* Require descriptor for this node. It has one so it should succeed. */
  697. test_node = entry_is_live(test_entry, ENTRY_NEED_DESCRIPTOR, &msg);
  698. tt_assert(test_node);
  699. tt_ptr_op(test_node, OP_EQ, node_get_by_id(test_entry->identity));
  700. done:
  701. ; /* XXX */
  702. }
  703. #define TEST_IPV4_ADDR "123.45.67.89"
  704. #define TEST_IPV6_ADDR "[1234:5678:90ab:cdef::]"
  705. static void
  706. test_node_preferred_orport(void *arg)
  707. {
  708. (void)arg;
  709. tor_addr_t ipv4_addr;
  710. const uint16_t ipv4_port = 4444;
  711. tor_addr_t ipv6_addr;
  712. const uint16_t ipv6_port = 6666;
  713. routerinfo_t node_ri;
  714. node_t node;
  715. tor_addr_port_t ap;
  716. /* Setup options */
  717. memset(&mocked_options, 0, sizeof(mocked_options));
  718. /* We don't test ClientPreferIPv6ORPort here, because it's used in
  719. * nodelist_set_consensus to setup node.ipv6_preferred, which we set
  720. * directly. */
  721. MOCK(get_options, mock_get_options);
  722. /* Setup IP addresses */
  723. tor_addr_parse(&ipv4_addr, TEST_IPV4_ADDR);
  724. tor_addr_parse(&ipv6_addr, TEST_IPV6_ADDR);
  725. /* Setup node_ri */
  726. memset(&node_ri, 0, sizeof(node_ri));
  727. node_ri.addr = tor_addr_to_ipv4h(&ipv4_addr);
  728. node_ri.or_port = ipv4_port;
  729. tor_addr_copy(&node_ri.ipv6_addr, &ipv6_addr);
  730. node_ri.ipv6_orport = ipv6_port;
  731. /* Setup node */
  732. memset(&node, 0, sizeof(node));
  733. node.ri = &node_ri;
  734. /* Check the preferred address is IPv4 if we're only using IPv4, regardless
  735. * of whether we prefer it or not */
  736. mocked_options.ClientUseIPv4 = 1;
  737. mocked_options.ClientUseIPv6 = 0;
  738. node.ipv6_preferred = 0;
  739. node_get_pref_orport(&node, &ap);
  740. tt_assert(tor_addr_eq(&ap.addr, &ipv4_addr));
  741. tt_assert(ap.port == ipv4_port);
  742. node.ipv6_preferred = 1;
  743. node_get_pref_orport(&node, &ap);
  744. tt_assert(tor_addr_eq(&ap.addr, &ipv4_addr));
  745. tt_assert(ap.port == ipv4_port);
  746. /* Check the preferred address is IPv4 if we're using IPv4 and IPv6, but
  747. * don't prefer the IPv6 address */
  748. mocked_options.ClientUseIPv4 = 1;
  749. mocked_options.ClientUseIPv6 = 1;
  750. node.ipv6_preferred = 0;
  751. node_get_pref_orport(&node, &ap);
  752. tt_assert(tor_addr_eq(&ap.addr, &ipv4_addr));
  753. tt_assert(ap.port == ipv4_port);
  754. /* Check the preferred address is IPv6 if we prefer it and
  755. * ClientUseIPv6 is 1, regardless of ClientUseIPv4 */
  756. mocked_options.ClientUseIPv4 = 1;
  757. mocked_options.ClientUseIPv6 = 1;
  758. node.ipv6_preferred = 1;
  759. node_get_pref_orport(&node, &ap);
  760. tt_assert(tor_addr_eq(&ap.addr, &ipv6_addr));
  761. tt_assert(ap.port == ipv6_port);
  762. mocked_options.ClientUseIPv4 = 0;
  763. node_get_pref_orport(&node, &ap);
  764. tt_assert(tor_addr_eq(&ap.addr, &ipv6_addr));
  765. tt_assert(ap.port == ipv6_port);
  766. /* Check the preferred address is IPv6 if we don't prefer it, but
  767. * ClientUseIPv4 is 0 */
  768. mocked_options.ClientUseIPv4 = 0;
  769. mocked_options.ClientUseIPv6 = 1;
  770. node.ipv6_preferred = fascist_firewall_prefer_ipv6_orport(&mocked_options);
  771. node_get_pref_orport(&node, &ap);
  772. tt_assert(tor_addr_eq(&ap.addr, &ipv6_addr));
  773. tt_assert(ap.port == ipv6_port);
  774. done:
  775. UNMOCK(get_options);
  776. }
  777. static void
  778. test_entry_guard_describe(void *arg)
  779. {
  780. (void)arg;
  781. entry_guard_t g;
  782. memset(&g, 0, sizeof(g));
  783. strlcpy(g.nickname, "okefenokee", sizeof(g.nickname));
  784. memcpy(g.identity, "theforestprimeval---", DIGEST_LEN);
  785. tt_str_op(entry_guard_describe(&g), OP_EQ,
  786. "okefenokee ($746865666F726573747072696D6576616C2D2D2D)");
  787. done:
  788. ;
  789. }
  790. static void
  791. test_entry_guard_randomize_time(void *arg)
  792. {
  793. const time_t now = 1479153573;
  794. const int delay = 86400;
  795. const int N = 1000;
  796. (void)arg;
  797. time_t t;
  798. int i;
  799. for (i = 0; i < N; ++i) {
  800. t = randomize_time(now, delay);
  801. tt_int_op(t, OP_LE, now);
  802. tt_int_op(t, OP_GE, now-delay);
  803. }
  804. /* now try the corner cases */
  805. for (i = 0; i < N; ++i) {
  806. t = randomize_time(100, delay);
  807. tt_int_op(t, OP_GE, 1);
  808. tt_int_op(t, OP_LE, 100);
  809. t = randomize_time(0, delay);
  810. tt_int_op(t, OP_EQ, 1);
  811. }
  812. done:
  813. ;
  814. }
  815. static void
  816. test_entry_guard_encode_for_state_minimal(void *arg)
  817. {
  818. (void) arg;
  819. entry_guard_t *eg = tor_malloc_zero(sizeof(entry_guard_t));
  820. eg->selection_name = tor_strdup("wubwub");
  821. memcpy(eg->identity, "plurpyflurpyslurpydo", DIGEST_LEN);
  822. eg->sampled_on_date = 1479081600;
  823. eg->confirmed_idx = -1;
  824. char *s = NULL;
  825. s = entry_guard_encode_for_state(eg);
  826. tt_str_op(s, OP_EQ,
  827. "in=wubwub "
  828. "rsa_id=706C75727079666C75727079736C75727079646F "
  829. "sampled_on=2016-11-14T00:00:00 "
  830. "listed=0");
  831. done:
  832. entry_guard_free(eg);
  833. tor_free(s);
  834. }
  835. static void
  836. test_entry_guard_encode_for_state_maximal(void *arg)
  837. {
  838. (void) arg;
  839. entry_guard_t *eg = tor_malloc_zero(sizeof(entry_guard_t));
  840. strlcpy(eg->nickname, "Fred", sizeof(eg->nickname));
  841. eg->selection_name = tor_strdup("default");
  842. memcpy(eg->identity, "plurpyflurpyslurpydo", DIGEST_LEN);
  843. eg->sampled_on_date = 1479081600;
  844. eg->sampled_by_version = tor_strdup("1.2.3");
  845. eg->unlisted_since_date = 1479081645;
  846. eg->currently_listed = 1;
  847. eg->confirmed_on_date = 1479081690;
  848. eg->confirmed_idx = 333;
  849. eg->extra_state_fields = tor_strdup("and the green grass grew all around");
  850. char *s = NULL;
  851. s = entry_guard_encode_for_state(eg);
  852. tt_str_op(s, OP_EQ,
  853. "in=default "
  854. "rsa_id=706C75727079666C75727079736C75727079646F "
  855. "nickname=Fred "
  856. "sampled_on=2016-11-14T00:00:00 "
  857. "sampled_by=1.2.3 "
  858. "unlisted_since=2016-11-14T00:00:45 "
  859. "listed=1 "
  860. "confirmed_on=2016-11-14T00:01:30 "
  861. "confirmed_idx=333 "
  862. "and the green grass grew all around");
  863. done:
  864. entry_guard_free(eg);
  865. tor_free(s);
  866. }
  867. static void
  868. test_entry_guard_parse_from_state_minimal(void *arg)
  869. {
  870. (void)arg;
  871. char *mem_op_hex_tmp = NULL;
  872. entry_guard_t *eg = NULL;
  873. time_t t = approx_time();
  874. eg = entry_guard_parse_from_state(
  875. "in=default_plus "
  876. "rsa_id=596f75206d6179206e656564206120686f626279");
  877. tt_assert(eg);
  878. tt_str_op(eg->selection_name, OP_EQ, "default_plus");
  879. test_mem_op_hex(eg->identity, OP_EQ,
  880. "596f75206d6179206e656564206120686f626279");
  881. tt_str_op(eg->nickname, OP_EQ, "$596F75206D6179206E656564206120686F626279");
  882. tt_i64_op(eg->sampled_on_date, OP_GE, t);
  883. tt_i64_op(eg->sampled_on_date, OP_LE, t+86400);
  884. tt_i64_op(eg->unlisted_since_date, OP_EQ, 0);
  885. tt_ptr_op(eg->sampled_by_version, OP_EQ, NULL);
  886. tt_int_op(eg->currently_listed, OP_EQ, 0);
  887. tt_i64_op(eg->confirmed_on_date, OP_EQ, 0);
  888. tt_int_op(eg->confirmed_idx, OP_EQ, -1);
  889. tt_int_op(eg->last_tried_to_connect, OP_EQ, 0);
  890. tt_int_op(eg->is_reachable, OP_EQ, GUARD_REACHABLE_MAYBE);
  891. done:
  892. entry_guard_free(eg);
  893. tor_free(mem_op_hex_tmp);
  894. }
  895. static void
  896. test_entry_guard_parse_from_state_maximal(void *arg)
  897. {
  898. (void)arg;
  899. char *mem_op_hex_tmp = NULL;
  900. entry_guard_t *eg = NULL;
  901. eg = entry_guard_parse_from_state(
  902. "in=fred "
  903. "rsa_id=706C75727079666C75727079736C75727079646F "
  904. "nickname=Fred "
  905. "sampled_on=2016-11-14T00:00:00 "
  906. "sampled_by=1.2.3 "
  907. "unlisted_since=2016-11-14T00:00:45 "
  908. "listed=1 "
  909. "confirmed_on=2016-11-14T00:01:30 "
  910. "confirmed_idx=333 "
  911. "and the green grass grew all around "
  912. "rsa_id=all,around");
  913. tt_assert(eg);
  914. test_mem_op_hex(eg->identity, OP_EQ,
  915. "706C75727079666C75727079736C75727079646F");
  916. tt_str_op(eg->nickname, OP_EQ, "Fred");
  917. tt_i64_op(eg->sampled_on_date, OP_EQ, 1479081600);
  918. tt_i64_op(eg->unlisted_since_date, OP_EQ, 1479081645);
  919. tt_str_op(eg->sampled_by_version, OP_EQ, "1.2.3");
  920. tt_int_op(eg->currently_listed, OP_EQ, 1);
  921. tt_i64_op(eg->confirmed_on_date, OP_EQ, 1479081690);
  922. tt_int_op(eg->confirmed_idx, OP_EQ, 333);
  923. tt_str_op(eg->extra_state_fields, OP_EQ,
  924. "and the green grass grew all around rsa_id=all,around");
  925. tt_int_op(eg->last_tried_to_connect, OP_EQ, 0);
  926. tt_int_op(eg->is_reachable, OP_EQ, GUARD_REACHABLE_MAYBE);
  927. done:
  928. entry_guard_free(eg);
  929. tor_free(mem_op_hex_tmp);
  930. }
  931. static void
  932. test_entry_guard_parse_from_state_failure(void *arg)
  933. {
  934. (void)arg;
  935. entry_guard_t *eg = NULL;
  936. /* no selection */
  937. eg = entry_guard_parse_from_state(
  938. "rsa_id=596f75206d6179206e656564206120686f626270");
  939. tt_assert(! eg);
  940. /* no RSA ID. */
  941. eg = entry_guard_parse_from_state("in=default nickname=Fred");
  942. tt_assert(! eg);
  943. /* Bad RSA ID: bad character. */
  944. eg = entry_guard_parse_from_state(
  945. "in=default "
  946. "rsa_id=596f75206d6179206e656564206120686f62627q");
  947. tt_assert(! eg);
  948. /* Bad RSA ID: too long.*/
  949. eg = entry_guard_parse_from_state(
  950. "in=default "
  951. "rsa_id=596f75206d6179206e656564206120686f6262703");
  952. tt_assert(! eg);
  953. /* Bad RSA ID: too short.*/
  954. eg = entry_guard_parse_from_state(
  955. "in=default "
  956. "rsa_id=596f75206d6179206e65656420612");
  957. tt_assert(! eg);
  958. done:
  959. entry_guard_free(eg);
  960. }
  961. static void
  962. test_entry_guard_parse_from_state_partial_failure(void *arg)
  963. {
  964. (void)arg;
  965. char *mem_op_hex_tmp = NULL;
  966. entry_guard_t *eg = NULL;
  967. time_t t = approx_time();
  968. eg = entry_guard_parse_from_state(
  969. "in=default "
  970. "rsa_id=706C75727079666C75727079736C75727079646F "
  971. "nickname=FredIsANodeWithAStrangeNicknameThatIsTooLong "
  972. "sampled_on=2016-11-14T00:00:99 "
  973. "sampled_by=1.2.3 stuff in the middle "
  974. "unlisted_since=2016-xx-14T00:00:45 "
  975. "listed=0 "
  976. "confirmed_on=2016-11-14T00:01:30zz "
  977. "confirmed_idx=idx "
  978. "and the green grass grew all around "
  979. "rsa_id=all,around");
  980. tt_assert(eg);
  981. test_mem_op_hex(eg->identity, OP_EQ,
  982. "706C75727079666C75727079736C75727079646F");
  983. tt_str_op(eg->nickname, OP_EQ, "FredIsANodeWithAStrangeNicknameThatIsTooL");
  984. tt_i64_op(eg->sampled_on_date, OP_EQ, t);
  985. tt_i64_op(eg->unlisted_since_date, OP_EQ, 0);
  986. tt_str_op(eg->sampled_by_version, OP_EQ, "1.2.3");
  987. tt_int_op(eg->currently_listed, OP_EQ, 0);
  988. tt_i64_op(eg->confirmed_on_date, OP_EQ, 0);
  989. tt_int_op(eg->confirmed_idx, OP_EQ, -1);
  990. tt_str_op(eg->extra_state_fields, OP_EQ,
  991. "stuff in the middle and the green grass grew all around "
  992. "rsa_id=all,around");
  993. tt_int_op(eg->last_tried_to_connect, OP_EQ, 0);
  994. tt_int_op(eg->is_reachable, OP_EQ, GUARD_REACHABLE_MAYBE);
  995. done:
  996. entry_guard_free(eg);
  997. tor_free(mem_op_hex_tmp);
  998. }
  999. static void
  1000. test_entry_guard_add_single_guard(void *arg)
  1001. {
  1002. (void)arg;
  1003. guard_selection_t *gs = guard_selection_new("default");
  1004. /* 1: Add a single guard to the sample. */
  1005. node_t *n1 = smartlist_get(big_fake_net_nodes, 0);
  1006. time_t now = approx_time();
  1007. tt_assert(n1->is_possible_guard == 1);
  1008. entry_guard_t *g1 = entry_guard_add_to_sample(gs, n1);
  1009. tt_assert(g1);
  1010. /* Make sure its fields look right. */
  1011. tt_mem_op(n1->identity, OP_EQ, g1->identity, DIGEST_LEN);
  1012. tt_i64_op(g1->sampled_on_date, OP_GE, now - 12*86400);
  1013. tt_i64_op(g1->sampled_on_date, OP_LE, now);
  1014. tt_str_op(g1->sampled_by_version, OP_EQ, VERSION);
  1015. tt_assert(g1->currently_listed == 1);
  1016. tt_i64_op(g1->confirmed_on_date, OP_EQ, 0);
  1017. tt_int_op(g1->confirmed_idx, OP_EQ, -1);
  1018. tt_int_op(g1->last_tried_to_connect, OP_EQ, 0);
  1019. tt_uint_op(g1->is_reachable, OP_EQ, GUARD_REACHABLE_MAYBE);
  1020. tt_i64_op(g1->failing_since, OP_EQ, 0);
  1021. tt_assert(g1->is_filtered_guard == 1);
  1022. tt_assert(g1->is_usable_filtered_guard == 1);
  1023. tt_assert(g1->is_primary == 0);
  1024. tt_assert(g1->extra_state_fields == NULL);
  1025. /* Make sure it got added. */
  1026. tt_int_op(1, OP_EQ, smartlist_len(gs->sampled_entry_guards));
  1027. tt_ptr_op(g1, OP_EQ, smartlist_get(gs->sampled_entry_guards, 0));
  1028. tt_ptr_op(g1, OP_EQ, get_sampled_guard_with_id(gs, (uint8_t*)n1->identity));
  1029. const uint8_t bad_id[20] = {0};
  1030. tt_ptr_op(NULL, OP_EQ, get_sampled_guard_with_id(gs, bad_id));
  1031. done:
  1032. guard_selection_free(gs);
  1033. }
  1034. static void
  1035. test_entry_guard_node_filter(void *arg)
  1036. {
  1037. (void)arg;
  1038. guard_selection_t *gs = guard_selection_new("default");
  1039. bridge_line_t *bl = NULL;
  1040. /* Initialize a bunch of node objects that are all guards. */
  1041. const int NUM = 7;
  1042. node_t *n[NUM];
  1043. entry_guard_t *g[NUM];
  1044. int i;
  1045. for (i=0; i < NUM; ++i) {
  1046. n[i] = smartlist_get(big_fake_net_nodes, i*2); // even ones are guards.
  1047. g[i] = entry_guard_add_to_sample(gs, n[i]);
  1048. // everything starts out filtered-in
  1049. tt_assert(g[i]->is_filtered_guard == 1);
  1050. tt_assert(g[i]->is_usable_filtered_guard == 1);
  1051. }
  1052. tt_int_op(num_reachable_filtered_guards(gs), OP_EQ, NUM);
  1053. /* Make sure refiltering doesn't hurt */
  1054. entry_guards_update_filtered_sets(gs);
  1055. for (i = 0; i < NUM; ++i) {
  1056. tt_assert(g[i]->is_filtered_guard == 1);
  1057. tt_assert(g[i]->is_usable_filtered_guard == 1);
  1058. }
  1059. tt_int_op(num_reachable_filtered_guards(gs), OP_EQ, NUM);
  1060. /* Now start doing things to make the guards get filtered out, 1 by 1. */
  1061. /* 0: Not listed. */
  1062. g[0]->currently_listed = 0;
  1063. /* 1: path bias says this guard is maybe eeeevil. */
  1064. g[1]->pb.path_bias_disabled = 1;
  1065. /* 2: Unreachable address. */
  1066. n[2]->rs->addr = 0;
  1067. /* 3: ExcludeNodes */
  1068. n[3]->rs->addr = 0x90902020;
  1069. routerset_free(get_options_mutable()->ExcludeNodes);
  1070. get_options_mutable()->ExcludeNodes = routerset_new();
  1071. routerset_parse(get_options_mutable()->ExcludeNodes, "144.144.0.0/16", "");
  1072. /* 4: Bridge. */
  1073. sweep_bridge_list();
  1074. bl = tor_malloc_zero(sizeof(bridge_line_t));
  1075. tor_addr_from_ipv4h(&bl->addr, n[4]->rs->addr);
  1076. bl->port = n[4]->rs->or_port;
  1077. memcpy(bl->digest, n[4]->identity, 20);
  1078. bridge_add_from_config(bl);
  1079. bl = NULL; // prevent free.
  1080. /* 5: Unreachable. This stays in the filter, but isn't in usable-filtered */
  1081. g[5]->last_tried_to_connect = approx_time(); // prevent retry.
  1082. g[5]->is_reachable = GUARD_REACHABLE_NO;
  1083. /* 6: no change. */
  1084. /* Now refilter and inspect. */
  1085. entry_guards_update_filtered_sets(gs);
  1086. for (i = 0; i < NUM; ++i) {
  1087. tt_assert(g[i]->is_filtered_guard == (i == 5 || i == 6));
  1088. tt_assert(g[i]->is_usable_filtered_guard == (i == 6));
  1089. }
  1090. tt_int_op(num_reachable_filtered_guards(gs), OP_EQ, 1);
  1091. done:
  1092. guard_selection_free(gs);
  1093. tor_free(bl);
  1094. }
  1095. static void
  1096. test_entry_guard_expand_sample(void *arg)
  1097. {
  1098. (void)arg;
  1099. guard_selection_t *gs = guard_selection_new("default");
  1100. digestmap_t *node_by_id = digestmap_new();
  1101. entry_guard_t *guard = entry_guards_expand_sample(gs);
  1102. tt_assert(guard); // the last guard returned.
  1103. // Every sampled guard here should be filtered and reachable for now.
  1104. tt_int_op(smartlist_len(gs->sampled_entry_guards), OP_EQ,
  1105. num_reachable_filtered_guards(gs));
  1106. /* Make sure we got the right number. */
  1107. tt_int_op(MIN_FILTERED_SAMPLE_SIZE, OP_EQ,
  1108. num_reachable_filtered_guards(gs));
  1109. // Make sure everything we got was from our fake node list, and everything
  1110. // was unique.
  1111. SMARTLIST_FOREACH_BEGIN(gs->sampled_entry_guards, entry_guard_t *, g) {
  1112. const node_t *n = bfn_mock_node_get_by_id(g->identity);
  1113. tt_assert(n);
  1114. tt_ptr_op(NULL, OP_EQ, digestmap_get(node_by_id, g->identity));
  1115. digestmap_set(node_by_id, g->identity, (void*) n);
  1116. int idx = smartlist_pos(big_fake_net_nodes, n);
  1117. // The even ones are the guards; make sure we got guards.
  1118. tt_int_op(idx & 1, OP_EQ, 0);
  1119. } SMARTLIST_FOREACH_END(g);
  1120. // Nothing became unusable/unfiltered, so a subsequent expand should
  1121. // make no changes.
  1122. guard = entry_guards_expand_sample(gs);
  1123. tt_assert(! guard); // no guard was added.
  1124. tt_int_op(MIN_FILTERED_SAMPLE_SIZE, OP_EQ,
  1125. num_reachable_filtered_guards(gs));
  1126. // Make a few guards unreachable.
  1127. guard = smartlist_get(gs->sampled_entry_guards, 0);
  1128. guard->is_usable_filtered_guard = 0;
  1129. guard = smartlist_get(gs->sampled_entry_guards, 1);
  1130. guard->is_usable_filtered_guard = 0;
  1131. guard = smartlist_get(gs->sampled_entry_guards, 2);
  1132. guard->is_usable_filtered_guard = 0;
  1133. tt_int_op(MIN_FILTERED_SAMPLE_SIZE - 3, OP_EQ,
  1134. num_reachable_filtered_guards(gs));
  1135. // This time, expanding the sample will add some more guards.
  1136. guard = entry_guards_expand_sample(gs);
  1137. tt_assert(guard); // no guard was added.
  1138. tt_int_op(MIN_FILTERED_SAMPLE_SIZE, OP_EQ,
  1139. num_reachable_filtered_guards(gs));
  1140. tt_int_op(smartlist_len(gs->sampled_entry_guards), OP_EQ,
  1141. num_reachable_filtered_guards(gs)+3);
  1142. // Still idempotent.
  1143. guard = entry_guards_expand_sample(gs);
  1144. tt_assert(! guard); // no guard was added.
  1145. tt_int_op(MIN_FILTERED_SAMPLE_SIZE, OP_EQ,
  1146. num_reachable_filtered_guards(gs));
  1147. // Now, do a nasty trick: tell the filter to exclude 31/32 of the guards.
  1148. // This will cause the sample size to get reeeeally huge, while the
  1149. // filtered sample size grows only slowly.
  1150. routerset_free(get_options_mutable()->ExcludeNodes);
  1151. get_options_mutable()->ExcludeNodes = routerset_new();
  1152. routerset_parse(get_options_mutable()->ExcludeNodes, "144.144.0.0/16", "");
  1153. SMARTLIST_FOREACH(big_fake_net_nodes, node_t *, n, {
  1154. if (n_sl_idx % 64 != 0) {
  1155. n->rs->addr = 0x90903030;
  1156. }
  1157. });
  1158. entry_guards_update_filtered_sets(gs);
  1159. // Surely (p ~ 1-2**-60), one of our guards has been excluded.
  1160. tt_int_op(num_reachable_filtered_guards(gs), OP_LT,
  1161. MIN_FILTERED_SAMPLE_SIZE);
  1162. // Try to regenerate the guards.
  1163. guard = entry_guards_expand_sample(gs);
  1164. tt_assert(guard); // no guard was added.
  1165. /* this time, it's possible that we didn't add enough sampled guards. */
  1166. tt_int_op(num_reachable_filtered_guards(gs), OP_LE,
  1167. MIN_FILTERED_SAMPLE_SIZE);
  1168. /* but we definitely didn't exceed the sample maximum. */
  1169. tt_int_op(smartlist_len(gs->sampled_entry_guards), OP_LE,
  1170. (int)((271 / 2) * .3));
  1171. done:
  1172. guard_selection_free(gs);
  1173. digestmap_free(node_by_id, NULL);
  1174. }
  1175. static void
  1176. test_entry_guard_expand_sample_small_net(void *arg)
  1177. {
  1178. (void)arg;
  1179. guard_selection_t *gs = guard_selection_new("default");
  1180. /* Fun corner case: not enough guards to make up our whole sample size. */
  1181. SMARTLIST_FOREACH(big_fake_net_nodes, node_t *, n, {
  1182. if (n_sl_idx >= 40) {
  1183. tor_free(n->rs);
  1184. tor_free(n->md);
  1185. tor_free(n);
  1186. SMARTLIST_DEL_CURRENT(big_fake_net_nodes, n);
  1187. } else {
  1188. n->rs->addr = 0; // make the filter reject this.
  1189. }
  1190. });
  1191. entry_guard_t *guard = entry_guards_expand_sample(gs);
  1192. tt_assert(guard); // the last guard returned -- some guard was added.
  1193. tt_int_op(smartlist_len(gs->sampled_entry_guards), OP_GT, 0);
  1194. tt_int_op(smartlist_len(gs->sampled_entry_guards), OP_LT, 10);
  1195. tt_int_op(num_reachable_filtered_guards(gs), OP_EQ, 0);
  1196. done:
  1197. guard_selection_free(gs);
  1198. }
  1199. static void
  1200. test_entry_guard_update_from_consensus_status(void *arg)
  1201. {
  1202. /* Here we're going to have some nodes become un-guardy, and say we got a
  1203. * new consensus. This should cause those nodes to get detected as
  1204. * unreachable. */
  1205. (void)arg;
  1206. int i;
  1207. time_t start = approx_time();
  1208. guard_selection_t *gs = guard_selection_new("default");
  1209. networkstatus_t *ns_tmp = NULL;
  1210. /* Don't randomly backdate stuff; it will make correctness harder to check.*/
  1211. MOCK(randomize_time, mock_randomize_time_no_randomization);
  1212. /* First, sample some guards. */
  1213. entry_guards_expand_sample(gs);
  1214. int n_sampled_pre = smartlist_len(gs->sampled_entry_guards);
  1215. int n_filtered_pre = num_reachable_filtered_guards(gs);
  1216. tt_i64_op(n_sampled_pre, OP_EQ, n_filtered_pre);
  1217. tt_i64_op(n_sampled_pre, OP_GT, 10);
  1218. /* At this point, it should be a no-op to do this: */
  1219. sampled_guards_update_from_consensus(gs);
  1220. /* Now let's make some of our guards become unlisted. The easiest way to
  1221. * do that would be to take away their guard flag. */
  1222. for (i = 0; i < 5; ++i) {
  1223. entry_guard_t *g = smartlist_get(gs->sampled_entry_guards, i);
  1224. node_t *n = (node_t*) bfn_mock_node_get_by_id(g->identity);
  1225. n->is_possible_guard = 0;
  1226. }
  1227. update_approx_time(start + 30);
  1228. {
  1229. /* try this with no live networkstatus. Nothing should happen! */
  1230. ns_tmp = dummy_consensus;
  1231. dummy_consensus = NULL;
  1232. sampled_guards_update_from_consensus(gs);
  1233. tt_i64_op(smartlist_len(gs->sampled_entry_guards), OP_EQ, n_sampled_pre);
  1234. tt_i64_op(num_reachable_filtered_guards(gs), OP_EQ, n_filtered_pre);
  1235. /* put the networkstatus back. */
  1236. dummy_consensus = ns_tmp;
  1237. ns_tmp = NULL;
  1238. }
  1239. /* Now those guards should become unlisted, and drop off the filter, but
  1240. * stay in the sample. */
  1241. update_approx_time(start + 60);
  1242. sampled_guards_update_from_consensus(gs);
  1243. tt_i64_op(smartlist_len(gs->sampled_entry_guards), OP_EQ, n_sampled_pre);
  1244. tt_i64_op(num_reachable_filtered_guards(gs), OP_EQ, n_filtered_pre - 5);
  1245. for (i = 0; i < 5; ++i) {
  1246. entry_guard_t *g = smartlist_get(gs->sampled_entry_guards, i);
  1247. tt_assert(! g->currently_listed);
  1248. tt_i64_op(g->unlisted_since_date, OP_EQ, start+60);
  1249. }
  1250. for (i = 5; i < n_sampled_pre; ++i) {
  1251. entry_guard_t *g = smartlist_get(gs->sampled_entry_guards, i);
  1252. tt_assert(g->currently_listed);
  1253. tt_i64_op(g->unlisted_since_date, OP_EQ, 0);
  1254. }
  1255. /* Now re-list one, and remove one completely. */
  1256. {
  1257. entry_guard_t *g = smartlist_get(gs->sampled_entry_guards, 0);
  1258. node_t *n = (node_t*) bfn_mock_node_get_by_id(g->identity);
  1259. n->is_possible_guard = 1;
  1260. }
  1261. {
  1262. /* try removing the node, to make sure we don't crash on an absent node
  1263. */
  1264. entry_guard_t *g = smartlist_get(gs->sampled_entry_guards, 5);
  1265. node_t *n = (node_t*) bfn_mock_node_get_by_id(g->identity);
  1266. smartlist_remove(big_fake_net_nodes, n);
  1267. tor_free(n->rs);
  1268. tor_free(n->md);
  1269. tor_free(n);
  1270. }
  1271. update_approx_time(start + 300);
  1272. sampled_guards_update_from_consensus(gs);
  1273. /* guards 1..5 are now unlisted; 0,6,7.. are listed. */
  1274. tt_i64_op(smartlist_len(gs->sampled_entry_guards), OP_EQ, n_sampled_pre);
  1275. for (i = 1; i < 6; ++i) {
  1276. entry_guard_t *g = smartlist_get(gs->sampled_entry_guards, i);
  1277. tt_assert(! g->currently_listed);
  1278. if (i == 5)
  1279. tt_i64_op(g->unlisted_since_date, OP_EQ, start+300);
  1280. else
  1281. tt_i64_op(g->unlisted_since_date, OP_EQ, start+60);
  1282. }
  1283. for (i = 0; i < n_sampled_pre; i = (!i) ? 6 : i+1) { /* 0,6,7,8, ... */
  1284. entry_guard_t *g = smartlist_get(gs->sampled_entry_guards, i);
  1285. tt_assert(g->currently_listed);
  1286. tt_i64_op(g->unlisted_since_date, OP_EQ, 0);
  1287. }
  1288. done:
  1289. tor_free(ns_tmp); /* in case we couldn't put it back */
  1290. guard_selection_free(gs);
  1291. UNMOCK(randomize_time);
  1292. }
  1293. static void
  1294. test_entry_guard_update_from_consensus_repair(void *arg)
  1295. {
  1296. /* Here we'll make sure that our code to repair the unlisted-since
  1297. * times is correct. */
  1298. (void)arg;
  1299. int i;
  1300. time_t start = approx_time();
  1301. guard_selection_t *gs = guard_selection_new("default");
  1302. /* Don't randomly backdate stuff; it will make correctness harder to check.*/
  1303. MOCK(randomize_time, mock_randomize_time_no_randomization);
  1304. /* First, sample some guards. */
  1305. entry_guards_expand_sample(gs);
  1306. int n_sampled_pre = smartlist_len(gs->sampled_entry_guards);
  1307. int n_filtered_pre = num_reachable_filtered_guards(gs);
  1308. tt_i64_op(n_sampled_pre, OP_EQ, n_filtered_pre);
  1309. tt_i64_op(n_sampled_pre, OP_GT, 10);
  1310. /* Now corrupt the list a bit. Call some unlisted-since-never, and some
  1311. * listed-and-unlisted-since-a-time. */
  1312. update_approx_time(start + 300);
  1313. for (i = 0; i < 3; ++i) {
  1314. /* these will get a date. */
  1315. entry_guard_t *g = smartlist_get(gs->sampled_entry_guards, i);
  1316. node_t *n = (node_t*) bfn_mock_node_get_by_id(g->identity);
  1317. n->is_possible_guard = 0;
  1318. g->currently_listed = 0;
  1319. }
  1320. for (i = 3; i < 6; ++i) {
  1321. /* these will become listed. */
  1322. entry_guard_t *g = smartlist_get(gs->sampled_entry_guards, i);
  1323. g->unlisted_since_date = start+100;
  1324. }
  1325. setup_full_capture_of_logs(LOG_WARN);
  1326. sampled_guards_update_from_consensus(gs);
  1327. expect_log_msg_containing(
  1328. "was listed, but with unlisted_since_date set");
  1329. expect_log_msg_containing(
  1330. "was unlisted, but with unlisted_since_date unset");
  1331. teardown_capture_of_logs();
  1332. tt_int_op(smartlist_len(gs->sampled_entry_guards), OP_EQ, n_sampled_pre);
  1333. tt_int_op(num_reachable_filtered_guards(gs), OP_EQ, n_filtered_pre - 3);
  1334. for (i = 3; i < n_sampled_pre; ++i) {
  1335. /* these will become listed. */
  1336. entry_guard_t *g = smartlist_get(gs->sampled_entry_guards, i);
  1337. if (i < 3) {
  1338. tt_assert(! g->currently_listed);
  1339. tt_i64_op(g->unlisted_since_date, OP_EQ, start+300);
  1340. } else {
  1341. tt_assert(g->currently_listed);
  1342. tt_i64_op(g->unlisted_since_date, OP_EQ, 0);
  1343. }
  1344. }
  1345. done:
  1346. teardown_capture_of_logs();
  1347. guard_selection_free(gs);
  1348. UNMOCK(randomize_time);
  1349. }
  1350. static void
  1351. test_entry_guard_update_from_consensus_remove(void *arg)
  1352. {
  1353. /* Now let's check the logic responsible for removing guards from the
  1354. * sample entirely. */
  1355. (void)arg;
  1356. //int i;
  1357. guard_selection_t *gs = guard_selection_new("default");
  1358. smartlist_t *keep_ids = smartlist_new();
  1359. smartlist_t *remove_ids = smartlist_new();
  1360. /* Don't randomly backdate stuff; it will make correctness harder to check.*/
  1361. MOCK(randomize_time, mock_randomize_time_no_randomization);
  1362. /* First, sample some guards. */
  1363. entry_guards_expand_sample(gs);
  1364. int n_sampled_pre = smartlist_len(gs->sampled_entry_guards);
  1365. int n_filtered_pre = num_reachable_filtered_guards(gs);
  1366. tt_i64_op(n_sampled_pre, OP_EQ, n_filtered_pre);
  1367. tt_i64_op(n_sampled_pre, OP_GT, 10);
  1368. const time_t one_day_ago = approx_time() - 1*24*60*60;
  1369. const time_t one_year_ago = approx_time() - 365*24*60*60;
  1370. const time_t two_years_ago = approx_time() - 2*365*24*60*60;
  1371. /* 0: unlisted for a day. (keep this) */
  1372. {
  1373. entry_guard_t *g = smartlist_get(gs->sampled_entry_guards, 0);
  1374. node_t *n = (node_t*) bfn_mock_node_get_by_id(g->identity);
  1375. n->is_possible_guard = 0;
  1376. g->currently_listed = 0;
  1377. g->unlisted_since_date = one_day_ago;
  1378. smartlist_add(keep_ids, tor_memdup(g->identity, 20));
  1379. }
  1380. /* 1: unlisted for a year. (remove this) */
  1381. {
  1382. entry_guard_t *g = smartlist_get(gs->sampled_entry_guards, 1);
  1383. node_t *n = (node_t*) bfn_mock_node_get_by_id(g->identity);
  1384. n->is_possible_guard = 0;
  1385. g->currently_listed = 0;
  1386. g->unlisted_since_date = one_year_ago;
  1387. smartlist_add(remove_ids, tor_memdup(g->identity, 20));
  1388. }
  1389. /* 2: added a day ago, never confirmed. (keep this) */
  1390. {
  1391. entry_guard_t *g = smartlist_get(gs->sampled_entry_guards, 2);
  1392. g->sampled_on_date = one_day_ago;
  1393. smartlist_add(keep_ids, tor_memdup(g->identity, 20));
  1394. }
  1395. /* 3: added a year ago, never confirmed. (remove this) */
  1396. {
  1397. entry_guard_t *g = smartlist_get(gs->sampled_entry_guards, 3);
  1398. g->sampled_on_date = one_year_ago;
  1399. smartlist_add(remove_ids, tor_memdup(g->identity, 20));
  1400. }
  1401. /* 4: added two year ago, confirmed yesterday, primary. (keep this.) */
  1402. {
  1403. entry_guard_t *g = smartlist_get(gs->sampled_entry_guards, 4);
  1404. g->sampled_on_date = one_year_ago;
  1405. g->confirmed_on_date = one_day_ago;
  1406. g->confirmed_idx = 0;
  1407. g->is_primary = 1;
  1408. smartlist_add(gs->confirmed_entry_guards, g);
  1409. smartlist_add(gs->primary_entry_guards, g);
  1410. smartlist_add(keep_ids, tor_memdup(g->identity, 20));
  1411. }
  1412. /* 5: added two years ago, confirmed a year ago, primary. (remove this) */
  1413. {
  1414. entry_guard_t *g = smartlist_get(gs->sampled_entry_guards, 5);
  1415. g->sampled_on_date = two_years_ago;
  1416. g->confirmed_on_date = one_year_ago;
  1417. g->confirmed_idx = 1;
  1418. g->is_primary = 1;
  1419. smartlist_add(gs->confirmed_entry_guards, g);
  1420. smartlist_add(gs->primary_entry_guards, g);
  1421. smartlist_add(remove_ids, tor_memdup(g->identity, 20));
  1422. }
  1423. sampled_guards_update_from_consensus(gs);
  1424. /* Did we remove the right ones? */
  1425. SMARTLIST_FOREACH(keep_ids, uint8_t *, id, {
  1426. tt_assert(get_sampled_guard_with_id(gs, id) != NULL);
  1427. });
  1428. SMARTLIST_FOREACH(remove_ids, uint8_t *, id, {
  1429. tt_want(get_sampled_guard_with_id(gs, id) == NULL);
  1430. });
  1431. /* Did we remove the right number? */
  1432. tt_int_op(smartlist_len(gs->sampled_entry_guards), OP_EQ, n_sampled_pre - 3);
  1433. done:
  1434. guard_selection_free(gs);
  1435. UNMOCK(randomize_time);
  1436. SMARTLIST_FOREACH(keep_ids, char *, cp, tor_free(cp));
  1437. SMARTLIST_FOREACH(remove_ids, char *, cp, tor_free(cp));
  1438. smartlist_free(keep_ids);
  1439. smartlist_free(remove_ids);
  1440. }
  1441. static void
  1442. test_entry_guard_confirming_guards(void *arg)
  1443. {
  1444. (void)arg;
  1445. /* Now let's check the logic responsible for manipulating the list
  1446. * of confirmed guards */
  1447. guard_selection_t *gs = guard_selection_new("default");
  1448. MOCK(randomize_time, mock_randomize_time_no_randomization);
  1449. /* Create the sample. */
  1450. entry_guards_expand_sample(gs);
  1451. /* Confirm a few guards. */
  1452. time_t start = approx_time();
  1453. entry_guard_t *g1 = smartlist_get(gs->sampled_entry_guards, 0);
  1454. entry_guard_t *g2 = smartlist_get(gs->sampled_entry_guards, 1);
  1455. entry_guard_t *g3 = smartlist_get(gs->sampled_entry_guards, 8);
  1456. make_guard_confirmed(gs, g2);
  1457. update_approx_time(start + 10);
  1458. make_guard_confirmed(gs, g1);
  1459. make_guard_confirmed(gs, g3);
  1460. /* Were the correct dates and indices fed in? */
  1461. tt_int_op(g1->confirmed_idx, OP_EQ, 1);
  1462. tt_int_op(g2->confirmed_idx, OP_EQ, 0);
  1463. tt_int_op(g3->confirmed_idx, OP_EQ, 2);
  1464. tt_i64_op(g1->confirmed_on_date, OP_EQ, start+10);
  1465. tt_i64_op(g2->confirmed_on_date, OP_EQ, start);
  1466. tt_i64_op(g3->confirmed_on_date, OP_EQ, start+10);
  1467. tt_ptr_op(smartlist_get(gs->confirmed_entry_guards, 0), OP_EQ, g2);
  1468. tt_ptr_op(smartlist_get(gs->confirmed_entry_guards, 1), OP_EQ, g1);
  1469. tt_ptr_op(smartlist_get(gs->confirmed_entry_guards, 2), OP_EQ, g3);
  1470. /* Now make sure we can regenerate the confirmed_entry_guards list. */
  1471. smartlist_clear(gs->confirmed_entry_guards);
  1472. g2->confirmed_idx = 0;
  1473. g1->confirmed_idx = 10;
  1474. g3->confirmed_idx = 100;
  1475. entry_guards_update_confirmed(gs);
  1476. tt_int_op(g1->confirmed_idx, OP_EQ, 1);
  1477. tt_int_op(g2->confirmed_idx, OP_EQ, 0);
  1478. tt_int_op(g3->confirmed_idx, OP_EQ, 2);
  1479. tt_ptr_op(smartlist_get(gs->confirmed_entry_guards, 0), OP_EQ, g2);
  1480. tt_ptr_op(smartlist_get(gs->confirmed_entry_guards, 1), OP_EQ, g1);
  1481. tt_ptr_op(smartlist_get(gs->confirmed_entry_guards, 2), OP_EQ, g3);
  1482. /* Now make sure we can regenerate the confirmed_entry_guards list if
  1483. * the indices are messed up. */
  1484. g1->confirmed_idx = g2->confirmed_idx = g3->confirmed_idx = 999;
  1485. smartlist_clear(gs->confirmed_entry_guards);
  1486. entry_guards_update_confirmed(gs);
  1487. tt_int_op(g1->confirmed_idx, OP_GE, 0);
  1488. tt_int_op(g2->confirmed_idx, OP_GE, 0);
  1489. tt_int_op(g3->confirmed_idx, OP_GE, 0);
  1490. tt_int_op(g1->confirmed_idx, OP_LE, 2);
  1491. tt_int_op(g2->confirmed_idx, OP_LE, 2);
  1492. tt_int_op(g3->confirmed_idx, OP_LE, 2);
  1493. g1 = smartlist_get(gs->confirmed_entry_guards, 0);
  1494. g2 = smartlist_get(gs->confirmed_entry_guards, 1);
  1495. g3 = smartlist_get(gs->confirmed_entry_guards, 2);
  1496. tt_int_op(g1->confirmed_idx, OP_EQ, 0);
  1497. tt_int_op(g2->confirmed_idx, OP_EQ, 1);
  1498. tt_int_op(g3->confirmed_idx, OP_EQ, 2);
  1499. tt_assert(g1 != g2);
  1500. tt_assert(g1 != g3);
  1501. tt_assert(g2 != g3);
  1502. done:
  1503. UNMOCK(randomize_time);
  1504. guard_selection_free(gs);
  1505. }
  1506. static void
  1507. test_entry_guard_sample_reachable_filtered(void *arg)
  1508. {
  1509. (void)arg;
  1510. guard_selection_t *gs = guard_selection_new("default");
  1511. entry_guards_expand_sample(gs);
  1512. const int N = 10000;
  1513. bitarray_t *selected = NULL;
  1514. int i, j;
  1515. /* We've got a sampled list now; let's make one non-usable-filtered; some
  1516. * confirmed, some primary, some pending.
  1517. */
  1518. int n_guards = smartlist_len(gs->sampled_entry_guards);
  1519. tt_int_op(n_guards, OP_GT, 10);
  1520. entry_guard_t *g;
  1521. g = smartlist_get(gs->sampled_entry_guards, 0);
  1522. g->is_pending = 1;
  1523. g = smartlist_get(gs->sampled_entry_guards, 1);
  1524. make_guard_confirmed(gs, g);
  1525. g = smartlist_get(gs->sampled_entry_guards, 2);
  1526. g->is_primary = 1;
  1527. g = smartlist_get(gs->sampled_entry_guards, 3);
  1528. g->pb.path_bias_disabled = 1;
  1529. entry_guards_update_filtered_sets(gs);
  1530. tt_int_op(num_reachable_filtered_guards(gs), OP_EQ, n_guards - 1);
  1531. tt_int_op(smartlist_len(gs->sampled_entry_guards), OP_EQ, n_guards);
  1532. // +1 since the one we made disabled will make another one get added.
  1533. ++n_guards;
  1534. /* Try a bunch of selections. */
  1535. const struct {
  1536. int flag; int idx;
  1537. } tests[] = {
  1538. { 0, -1 },
  1539. { SAMPLE_EXCLUDE_CONFIRMED, 1 },
  1540. { SAMPLE_EXCLUDE_PRIMARY, 2 },
  1541. { SAMPLE_EXCLUDE_PENDING, 0 },
  1542. { -1, -1},
  1543. };
  1544. for (j = 0; tests[j].flag >= 0; ++j) {
  1545. selected = bitarray_init_zero(n_guards);
  1546. const int excluded_flags = tests[j].flag;
  1547. const int excluded_idx = tests[j].idx;
  1548. for (i = 0; i < N; ++i) {
  1549. g = sample_reachable_filtered_entry_guards(gs, excluded_flags);
  1550. tor_assert(g);
  1551. int pos = smartlist_pos(gs->sampled_entry_guards, g);
  1552. tt_int_op(smartlist_len(gs->sampled_entry_guards), OP_EQ, n_guards);
  1553. tt_int_op(pos, OP_GE, 0);
  1554. tt_int_op(pos, OP_LT, n_guards);
  1555. bitarray_set(selected, pos);
  1556. }
  1557. for (i = 0; i < n_guards; ++i) {
  1558. const int should_be_set = (i != excluded_idx &&
  1559. i != 3); // filtered out.
  1560. tt_int_op(!!bitarray_is_set(selected, i), OP_EQ, should_be_set);
  1561. }
  1562. bitarray_free(selected);
  1563. selected = NULL;
  1564. }
  1565. done:
  1566. guard_selection_free(gs);
  1567. bitarray_free(selected);
  1568. }
  1569. static void
  1570. test_entry_guard_sample_reachable_filtered_empty(void *arg)
  1571. {
  1572. (void)arg;
  1573. guard_selection_t *gs = guard_selection_new("default");
  1574. /* What if we try to sample from a set of 0? */
  1575. SMARTLIST_FOREACH(big_fake_net_nodes, node_t *, n,
  1576. n->is_possible_guard = 0);
  1577. entry_guard_t *g = sample_reachable_filtered_entry_guards(gs, 0);
  1578. tt_ptr_op(g, OP_EQ, NULL);
  1579. done:
  1580. guard_selection_free(gs);
  1581. }
  1582. static void
  1583. test_entry_guard_retry_unreachable(void *arg)
  1584. {
  1585. (void)arg;
  1586. guard_selection_t *gs = guard_selection_new("default");
  1587. entry_guards_expand_sample(gs);
  1588. /* Let's say that we have two guards, and they're down.
  1589. */
  1590. time_t start = approx_time();;
  1591. entry_guard_t *g1 = smartlist_get(gs->sampled_entry_guards, 0);
  1592. entry_guard_t *g2 = smartlist_get(gs->sampled_entry_guards, 1);
  1593. entry_guard_t *g3 = smartlist_get(gs->sampled_entry_guards, 2);
  1594. g1->is_reachable = GUARD_REACHABLE_NO;
  1595. g2->is_reachable = GUARD_REACHABLE_NO;
  1596. g1->is_primary = 1;
  1597. g1->failing_since = g2->failing_since = start;
  1598. g1->last_tried_to_connect = g2->last_tried_to_connect = start;
  1599. /* Wait 5 minutes. Nothing will get retried. */
  1600. update_approx_time(start + 5 * 60);
  1601. entry_guard_consider_retry(g1);
  1602. entry_guard_consider_retry(g2);
  1603. entry_guard_consider_retry(g3); // just to make sure this doesn't crash.
  1604. tt_int_op(g1->is_reachable, OP_EQ, GUARD_REACHABLE_NO);
  1605. tt_int_op(g2->is_reachable, OP_EQ, GUARD_REACHABLE_NO);
  1606. tt_int_op(g3->is_reachable, OP_EQ, GUARD_REACHABLE_MAYBE);
  1607. /* After 30 min, the primary one gets retried */
  1608. update_approx_time(start + 35 * 60);
  1609. entry_guard_consider_retry(g1);
  1610. entry_guard_consider_retry(g2);
  1611. tt_int_op(g1->is_reachable, OP_EQ, GUARD_REACHABLE_MAYBE);
  1612. tt_int_op(g2->is_reachable, OP_EQ, GUARD_REACHABLE_NO);
  1613. g1->is_reachable = GUARD_REACHABLE_NO;
  1614. g1->last_tried_to_connect = start + 35*60;
  1615. /* After 1 hour, we'll retry the nonprimary one. */
  1616. update_approx_time(start + 61 * 60);
  1617. entry_guard_consider_retry(g1);
  1618. entry_guard_consider_retry(g2);
  1619. tt_int_op(g1->is_reachable, OP_EQ, GUARD_REACHABLE_NO);
  1620. tt_int_op(g2->is_reachable, OP_EQ, GUARD_REACHABLE_MAYBE);
  1621. g2->is_reachable = GUARD_REACHABLE_NO;
  1622. g2->last_tried_to_connect = start + 61*60;
  1623. /* And then the primary one again. */
  1624. update_approx_time(start + 66 * 60);
  1625. entry_guard_consider_retry(g1);
  1626. entry_guard_consider_retry(g2);
  1627. tt_int_op(g1->is_reachable, OP_EQ, GUARD_REACHABLE_MAYBE);
  1628. tt_int_op(g2->is_reachable, OP_EQ, GUARD_REACHABLE_NO);
  1629. done:
  1630. guard_selection_free(gs);
  1631. }
  1632. static void
  1633. test_entry_guard_manage_primary(void *arg)
  1634. {
  1635. (void)arg;
  1636. guard_selection_t *gs = guard_selection_new("default");
  1637. smartlist_t *prev_guards = smartlist_new();
  1638. /* If no guards are confirmed, we should pick a few reachable guards and
  1639. * call them all primary. But not confirmed.*/
  1640. entry_guards_update_primary(gs);
  1641. int n_primary = smartlist_len(gs->primary_entry_guards);
  1642. tt_int_op(n_primary, OP_GE, 1);
  1643. SMARTLIST_FOREACH(gs->primary_entry_guards, entry_guard_t *, g, {
  1644. tt_assert(g->is_primary);
  1645. tt_assert(g->confirmed_idx == -1);
  1646. });
  1647. /* Calling it a second time should leave the guards unchanged. */
  1648. smartlist_add_all(prev_guards, gs->primary_entry_guards);
  1649. entry_guards_update_primary(gs);
  1650. tt_int_op(smartlist_len(gs->primary_entry_guards), OP_EQ, n_primary);
  1651. SMARTLIST_FOREACH(gs->primary_entry_guards, entry_guard_t *, g, {
  1652. tt_ptr_op(g, OP_EQ, smartlist_get(prev_guards, g_sl_idx));
  1653. });
  1654. /* If we have one confirmed guard, that guards becomes the first primary
  1655. * guard, and the other primary guards get kept. */
  1656. /* find a non-primary guard... */
  1657. entry_guard_t *confirmed = NULL;
  1658. SMARTLIST_FOREACH(gs->sampled_entry_guards, entry_guard_t *, g, {
  1659. if (! g->is_primary) {
  1660. confirmed = g;
  1661. break;
  1662. }
  1663. });
  1664. tt_assert(confirmed);
  1665. /* make it confirmed. */
  1666. make_guard_confirmed(gs, confirmed);
  1667. /* update the list... */
  1668. smartlist_clear(prev_guards);
  1669. smartlist_add_all(prev_guards, gs->primary_entry_guards);
  1670. entry_guards_update_primary(gs);
  1671. /* and see what's primary now! */
  1672. tt_int_op(smartlist_len(gs->primary_entry_guards), OP_EQ, n_primary);
  1673. tt_ptr_op(smartlist_get(gs->primary_entry_guards, 0), OP_EQ, confirmed);
  1674. SMARTLIST_FOREACH(gs->primary_entry_guards, entry_guard_t *, g, {
  1675. tt_assert(g->is_primary);
  1676. if (g_sl_idx == 0)
  1677. continue;
  1678. tt_ptr_op(g, OP_EQ, smartlist_get(prev_guards, g_sl_idx - 1));
  1679. });
  1680. {
  1681. entry_guard_t *prev_last_guard = smartlist_get(prev_guards, n_primary-1);
  1682. tt_assert(! prev_last_guard->is_primary);
  1683. }
  1684. /* Calling it a fourth time should leave the guards unchanged. */
  1685. smartlist_clear(prev_guards);
  1686. smartlist_add_all(prev_guards, gs->primary_entry_guards);
  1687. entry_guards_update_primary(gs);
  1688. tt_int_op(smartlist_len(gs->primary_entry_guards), OP_EQ, n_primary);
  1689. SMARTLIST_FOREACH(gs->primary_entry_guards, entry_guard_t *, g, {
  1690. tt_ptr_op(g, OP_EQ, smartlist_get(prev_guards, g_sl_idx));
  1691. });
  1692. done:
  1693. guard_selection_free(gs);
  1694. smartlist_free(prev_guards);
  1695. }
  1696. static void
  1697. test_entry_guard_select_for_circuit_no_confirmed(void *arg)
  1698. {
  1699. /* Simpler cases: no gaurds are confirmed yet. */
  1700. (void)arg;
  1701. guard_selection_t *gs = guard_selection_new("default");
  1702. /* simple starting configuration */
  1703. entry_guards_update_primary(gs);
  1704. unsigned state = 9999;
  1705. entry_guard_t *g = select_entry_guard_for_circuit(gs, &state);
  1706. tt_assert(g);
  1707. tt_assert(g->is_primary);
  1708. tt_int_op(g->confirmed_idx, OP_EQ, -1);
  1709. tt_assert(g->is_pending == 0); // primary implies non-pending.
  1710. tt_uint_op(state, OP_EQ, GUARD_CIRC_STATE_USABLE_ON_COMPLETION);
  1711. tt_i64_op(g->last_tried_to_connect, OP_EQ, approx_time());
  1712. // If we do that again, we should get the same guard.
  1713. entry_guard_t *g2 = select_entry_guard_for_circuit(gs, &state);
  1714. tt_ptr_op(g2, OP_EQ, g);
  1715. // if we mark that guard down, we should get a different primary guard.
  1716. // auto-retry it.
  1717. g->is_reachable = GUARD_REACHABLE_NO;
  1718. g->unreachable_since = approx_time() - 10;
  1719. g->last_tried_to_connect = approx_time() - 10;
  1720. state = 9999;
  1721. g2 = select_entry_guard_for_circuit(gs, &state);
  1722. tt_ptr_op(g2, OP_NE, g);
  1723. tt_assert(g2);
  1724. tt_assert(g2->is_primary);
  1725. tt_int_op(g2->confirmed_idx, OP_EQ, -1);
  1726. tt_assert(g2->is_pending == 0); // primary implies non-pending.
  1727. tt_uint_op(state, OP_EQ, GUARD_CIRC_STATE_USABLE_ON_COMPLETION);
  1728. tt_i64_op(g2->last_tried_to_connect, OP_EQ, approx_time());
  1729. // If we say that the first primary guard was last tried a long time ago, we
  1730. // should get an automatic retry on it.
  1731. g->unreachable_since = approx_time() - 72*60*60;
  1732. g->last_tried_to_connect = approx_time() - 72*60*60;
  1733. state = 9999;
  1734. g2 = select_entry_guard_for_circuit(gs, &state);
  1735. tt_ptr_op(g2, OP_EQ, g);
  1736. tt_assert(g2);
  1737. tt_uint_op(state, OP_EQ, GUARD_CIRC_STATE_USABLE_ON_COMPLETION);
  1738. tt_i64_op(g2->last_tried_to_connect, OP_EQ, approx_time());
  1739. tt_int_op(g2->is_reachable, OP_EQ, GUARD_REACHABLE_MAYBE);
  1740. // And if we mark ALL the primary guards down, we should get another guard
  1741. // at random.
  1742. SMARTLIST_FOREACH(gs->primary_entry_guards, entry_guard_t *, guard, {
  1743. guard->is_reachable = GUARD_REACHABLE_NO;
  1744. guard->last_tried_to_connect = approx_time() - 5;
  1745. guard->unreachable_since = approx_time() - 30;
  1746. });
  1747. state = 9999;
  1748. g2 = select_entry_guard_for_circuit(gs, &state);
  1749. tt_assert(g2);
  1750. tt_assert(!g2->is_primary);
  1751. tt_int_op(g2->confirmed_idx, OP_EQ, -1);
  1752. tt_assert(g2->is_pending == 1);
  1753. tt_uint_op(state, OP_EQ, GUARD_CIRC_STATE_USABLE_IF_NO_BETTER_GUARD);
  1754. tt_i64_op(g2->last_tried_to_connect, OP_EQ, approx_time());
  1755. tt_int_op(g2->is_reachable, OP_EQ, GUARD_REACHABLE_MAYBE);
  1756. // As a bonus, maybe we should be retrying the primary guards. Let's say so.
  1757. mark_primary_guards_maybe_reachable(gs);
  1758. SMARTLIST_FOREACH(gs->primary_entry_guards, entry_guard_t *, guard, {
  1759. tt_int_op(guard->is_reachable, OP_EQ, GUARD_REACHABLE_MAYBE);
  1760. tt_assert(guard->is_usable_filtered_guard == 1);
  1761. // no change to these fields.
  1762. tt_i64_op(guard->last_tried_to_connect, OP_EQ, approx_time() - 5);
  1763. tt_i64_op(guard->unreachable_since, OP_EQ, approx_time() - 30);
  1764. });
  1765. done:
  1766. guard_selection_free(gs);
  1767. }
  1768. static void
  1769. test_entry_guard_select_for_circuit_confirmed(void *arg)
  1770. {
  1771. /* Case 2: if all the primary guards are down, and there are more confirmed
  1772. guards, we use a confirmed guard. */
  1773. (void)arg;
  1774. int i;
  1775. guard_selection_t *gs = guard_selection_new("default");
  1776. const int N_CONFIRMED = 10;
  1777. /* slightly more complicated simple starting configuration */
  1778. entry_guards_update_primary(gs);
  1779. for (i = 0; i < N_CONFIRMED; ++i) {
  1780. entry_guard_t *guard = smartlist_get(gs->sampled_entry_guards, i);
  1781. make_guard_confirmed(gs, guard);
  1782. }
  1783. entry_guards_update_primary(gs); // rebuild the primary list.
  1784. unsigned state = 9999;
  1785. // As above, this gives us a primary guard.
  1786. entry_guard_t *g = select_entry_guard_for_circuit(gs, &state);
  1787. tt_assert(g);
  1788. tt_assert(g->is_primary);
  1789. tt_int_op(g->confirmed_idx, OP_EQ, 0);
  1790. tt_assert(g->is_pending == 0); // primary implies non-pending.
  1791. tt_uint_op(state, OP_EQ, GUARD_CIRC_STATE_USABLE_ON_COMPLETION);
  1792. tt_i64_op(g->last_tried_to_connect, OP_EQ, approx_time());
  1793. tt_ptr_op(g, OP_EQ, smartlist_get(gs->primary_entry_guards, 0));
  1794. // But if we mark all the primary guards down...
  1795. SMARTLIST_FOREACH(gs->primary_entry_guards, entry_guard_t *, guard, {
  1796. guard->last_tried_to_connect = approx_time();
  1797. entry_guards_note_guard_failure(gs, guard);
  1798. });
  1799. // ... we should get a confirmed guard.
  1800. state = 9999;
  1801. g = select_entry_guard_for_circuit(gs, &state);
  1802. tt_assert(g);
  1803. tt_assert(! g->is_primary);
  1804. tt_int_op(g->confirmed_idx, OP_EQ, smartlist_len(gs->primary_entry_guards));
  1805. tt_assert(g->is_pending);
  1806. tt_uint_op(state, OP_EQ, GUARD_CIRC_STATE_USABLE_IF_NO_BETTER_GUARD);
  1807. tt_i64_op(g->last_tried_to_connect, OP_EQ, approx_time());
  1808. // And if we try again, we should get a different confirmed guard, since
  1809. // that one is pending.
  1810. state = 9999;
  1811. entry_guard_t *g2 = select_entry_guard_for_circuit(gs, &state);
  1812. tt_assert(g2);
  1813. tt_assert(! g2->is_primary);
  1814. tt_ptr_op(g2, OP_NE, g);
  1815. tt_int_op(g2->confirmed_idx, OP_EQ,
  1816. smartlist_len(gs->primary_entry_guards)+1);
  1817. tt_assert(g2->is_pending);
  1818. tt_uint_op(state, OP_EQ, GUARD_CIRC_STATE_USABLE_IF_NO_BETTER_GUARD);
  1819. tt_i64_op(g2->last_tried_to_connect, OP_EQ, approx_time());
  1820. // If we make every confirmed guard become pending then we start poking
  1821. // other guards.
  1822. const int n_remaining_confirmed =
  1823. N_CONFIRMED - 2 - smartlist_len(gs->primary_entry_guards);
  1824. for (i = 0; i < n_remaining_confirmed; ++i) {
  1825. g = select_entry_guard_for_circuit(gs, &state);
  1826. tt_int_op(g->confirmed_idx, OP_GE, 0);
  1827. tt_assert(g);
  1828. }
  1829. state = 9999;
  1830. g = select_entry_guard_for_circuit(gs, &state);
  1831. tt_assert(g);
  1832. tt_assert(g->is_pending);
  1833. tt_int_op(g->confirmed_idx, OP_EQ, -1);
  1834. done:
  1835. guard_selection_free(gs);
  1836. }
  1837. static const struct testcase_setup_t fake_network = {
  1838. fake_network_setup, fake_network_cleanup
  1839. };
  1840. static const struct testcase_setup_t big_fake_network = {
  1841. big_fake_network_setup, big_fake_network_cleanup
  1842. };
  1843. #define BFN_TEST(name) \
  1844. { #name, test_entry_guard_ ## name, TT_FORK, &big_fake_network, NULL }
  1845. struct testcase_t entrynodes_tests[] = {
  1846. { "entry_is_time_to_retry", test_entry_is_time_to_retry,
  1847. TT_FORK, NULL, NULL },
  1848. { "choose_random_entry_no_guards", test_choose_random_entry_no_guards,
  1849. TT_FORK, &fake_network, NULL },
  1850. { "choose_random_entry_one_possible_guard",
  1851. test_choose_random_entry_one_possible_guard,
  1852. TT_FORK, &fake_network, NULL },
  1853. { "populate_live_entry_guards_1guard",
  1854. test_populate_live_entry_guards_1guard,
  1855. TT_FORK, &fake_network, NULL },
  1856. { "populate_live_entry_guards_3guards",
  1857. test_populate_live_entry_guards_3guards,
  1858. TT_FORK, &fake_network, NULL },
  1859. { "entry_guards_parse_state_simple",
  1860. test_entry_guards_parse_state_simple,
  1861. TT_FORK, &fake_network, NULL },
  1862. { "entry_guards_parse_state_pathbias",
  1863. test_entry_guards_parse_state_pathbias,
  1864. TT_FORK, &fake_network, NULL },
  1865. { "entry_guards_set_from_config",
  1866. test_entry_guards_set_from_config,
  1867. TT_FORK, &fake_network, NULL },
  1868. { "entry_is_live",
  1869. test_entry_is_live,
  1870. TT_FORK, &fake_network, NULL },
  1871. { "node_preferred_orport",
  1872. test_node_preferred_orport,
  1873. 0, NULL, NULL },
  1874. { "entry_guard_describe", test_entry_guard_describe, 0, NULL, NULL },
  1875. { "randomize_time", test_entry_guard_randomize_time, 0, NULL, NULL },
  1876. { "encode_for_state_minimal",
  1877. test_entry_guard_encode_for_state_minimal, 0, NULL, NULL },
  1878. { "encode_for_state_maximal",
  1879. test_entry_guard_encode_for_state_maximal, 0, NULL, NULL },
  1880. { "parse_from_state_minimal",
  1881. test_entry_guard_parse_from_state_minimal, 0, NULL, NULL },
  1882. { "parse_from_state_maximal",
  1883. test_entry_guard_parse_from_state_maximal, 0, NULL, NULL },
  1884. { "parse_from_state_failure",
  1885. test_entry_guard_parse_from_state_failure, 0, NULL, NULL },
  1886. { "parse_from_state_partial_failure",
  1887. test_entry_guard_parse_from_state_partial_failure, 0, NULL, NULL },
  1888. BFN_TEST(add_single_guard),
  1889. BFN_TEST(node_filter),
  1890. BFN_TEST(expand_sample),
  1891. BFN_TEST(expand_sample_small_net),
  1892. BFN_TEST(update_from_consensus_status),
  1893. BFN_TEST(update_from_consensus_repair),
  1894. BFN_TEST(update_from_consensus_remove),
  1895. BFN_TEST(confirming_guards),
  1896. BFN_TEST(sample_reachable_filtered),
  1897. BFN_TEST(sample_reachable_filtered_empty),
  1898. BFN_TEST(retry_unreachable),
  1899. BFN_TEST(manage_primary),
  1900. BFN_TEST(select_for_circuit_no_confirmed),
  1901. BFN_TEST(select_for_circuit_confirmed),
  1902. END_OF_TESTCASES
  1903. };