test_entrynodes.c 99 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019
  1. /* Copyright (c) 2014-2016, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. #include "orconfig.h"
  4. #define CIRCUITLIST_PRIVATE
  5. #define STATEFILE_PRIVATE
  6. #define ENTRYNODES_PRIVATE
  7. #define ROUTERLIST_PRIVATE
  8. #include "or.h"
  9. #include "test.h"
  10. #include "bridges.h"
  11. #include "circuitlist.h"
  12. #include "config.h"
  13. #include "entrynodes.h"
  14. #include "nodelist.h"
  15. #include "networkstatus.h"
  16. #include "policies.h"
  17. #include "routerlist.h"
  18. #include "routerparse.h"
  19. #include "routerset.h"
  20. #include "statefile.h"
  21. #include "util.h"
  22. #include "test_helpers.h"
  23. #include "log_test_helpers.h"
  24. /* TODO:
  25. * choose_random_entry() test with state set.
  26. *
  27. * parse_state() tests with more than one guards.
  28. *
  29. * More tests for set_from_config(): Multiple nodes, use fingerprints,
  30. * use country codes.
  31. */
  32. /** Dummy Tor state used in unittests. */
  33. static or_state_t *dummy_state = NULL;
  34. static or_state_t *
  35. get_or_state_replacement(void)
  36. {
  37. return dummy_state;
  38. }
  39. /* Unittest cleanup function: Cleanup the fake network. */
  40. static int
  41. fake_network_cleanup(const struct testcase_t *testcase, void *ptr)
  42. {
  43. (void) testcase;
  44. (void) ptr;
  45. routerlist_free_all();
  46. nodelist_free_all();
  47. entry_guards_free_all();
  48. or_state_free(dummy_state);
  49. return 1; /* NOP */
  50. }
  51. /* Unittest setup function: Setup a fake network. */
  52. static void *
  53. fake_network_setup(const struct testcase_t *testcase)
  54. {
  55. (void) testcase;
  56. /* Setup fake state */
  57. dummy_state = tor_malloc_zero(sizeof(or_state_t));
  58. MOCK(get_or_state,
  59. get_or_state_replacement);
  60. /* Setup fake routerlist. */
  61. helper_setup_fake_routerlist();
  62. /* Return anything but NULL (it's interpreted as test fail) */
  63. return dummy_state;
  64. }
  65. static networkstatus_t *dummy_consensus = NULL;
  66. static smartlist_t *big_fake_net_nodes = NULL;
  67. static smartlist_t *
  68. bfn_mock_nodelist_get_list(void)
  69. {
  70. return big_fake_net_nodes;
  71. }
  72. static networkstatus_t *
  73. bfn_mock_networkstatus_get_live_consensus(time_t now)
  74. {
  75. (void)now;
  76. return dummy_consensus;
  77. }
  78. static const node_t *
  79. bfn_mock_node_get_by_id(const char *id)
  80. {
  81. SMARTLIST_FOREACH(big_fake_net_nodes, node_t *, n,
  82. if (fast_memeq(n->identity, id, 20))
  83. return n);
  84. return NULL;
  85. }
  86. /* Unittest cleanup function: Cleanup the fake network. */
  87. static int
  88. big_fake_network_cleanup(const struct testcase_t *testcase, void *ptr)
  89. {
  90. (void) testcase;
  91. (void) ptr;
  92. if (big_fake_net_nodes) {
  93. SMARTLIST_FOREACH(big_fake_net_nodes, node_t *, n, {
  94. tor_free(n->rs);
  95. tor_free(n->md);
  96. tor_free(n);
  97. });
  98. smartlist_free(big_fake_net_nodes);
  99. }
  100. UNMOCK(nodelist_get_list);
  101. UNMOCK(node_get_by_id);
  102. UNMOCK(get_or_state);
  103. UNMOCK(networkstatus_get_live_consensus);
  104. or_state_free(dummy_state);
  105. dummy_state = NULL;
  106. tor_free(dummy_consensus);
  107. return 1; /* NOP */
  108. }
  109. /* Unittest setup function: Setup a fake network. */
  110. static void *
  111. big_fake_network_setup(const struct testcase_t *testcase)
  112. {
  113. int i;
  114. /* These are minimal node_t objects that only contain the aspects of node_t
  115. * that we need for entrynodes.c. */
  116. const int N_NODES = 271;
  117. big_fake_net_nodes = smartlist_new();
  118. for (i = 0; i < N_NODES; ++i) {
  119. node_t *n = tor_malloc_zero(sizeof(node_t));
  120. n->md = tor_malloc_zero(sizeof(microdesc_t));
  121. crypto_rand(n->identity, sizeof(n->identity));
  122. n->rs = tor_malloc_zero(sizeof(routerstatus_t));
  123. memcpy(n->rs->identity_digest, n->identity, DIGEST_LEN);
  124. n->is_running = n->is_valid = n->is_fast = n->is_stable = 1;
  125. n->rs->addr = 0x04020202;
  126. n->rs->or_port = 1234;
  127. n->rs->is_v2_dir = 1;
  128. n->rs->has_bandwidth = 1;
  129. n->rs->bandwidth_kb = 30;
  130. /* Call half of the nodes a possible guard. */
  131. if (i % 2 == 0) {
  132. n->is_possible_guard = 1;
  133. n->rs->guardfraction_percentage = 100;
  134. n->rs->has_guardfraction = 1;
  135. }
  136. smartlist_add(big_fake_net_nodes, n);
  137. }
  138. dummy_state = tor_malloc_zero(sizeof(or_state_t));
  139. dummy_consensus = tor_malloc_zero(sizeof(networkstatus_t));
  140. dummy_consensus->valid_after = approx_time() - 3600;
  141. dummy_consensus->valid_until = approx_time() + 3600;
  142. MOCK(nodelist_get_list, bfn_mock_nodelist_get_list);
  143. MOCK(node_get_by_id, bfn_mock_node_get_by_id);
  144. MOCK(get_or_state,
  145. get_or_state_replacement);
  146. MOCK(networkstatus_get_live_consensus,
  147. bfn_mock_networkstatus_get_live_consensus);
  148. /* Return anything but NULL (it's interpreted as test fail) */
  149. return (void*)testcase;
  150. }
  151. static time_t
  152. mock_randomize_time_no_randomization(time_t a, time_t b)
  153. {
  154. (void) b;
  155. return a;
  156. }
  157. static or_options_t mocked_options;
  158. static const or_options_t *
  159. mock_get_options(void)
  160. {
  161. return &mocked_options;
  162. }
  163. /** Test choose_random_entry() with none of our routers being guard nodes. */
  164. static void
  165. test_choose_random_entry_no_guards(void *arg)
  166. {
  167. const node_t *chosen_entry = NULL;
  168. (void) arg;
  169. MOCK(get_options, mock_get_options);
  170. /* Check that we get a guard if it passes preferred
  171. * address settings */
  172. memset(&mocked_options, 0, sizeof(mocked_options));
  173. mocked_options.ClientUseIPv4 = 1;
  174. mocked_options.ClientPreferIPv6ORPort = 0;
  175. mocked_options.UseDeprecatedGuardAlgorithm = 1;
  176. /* Try to pick an entry even though none of our routers are guards. */
  177. chosen_entry = choose_random_entry(NULL);
  178. /* Unintuitively, we actually pick a random node as our entry,
  179. because router_choose_random_node() relaxes its constraints if it
  180. can't find a proper entry guard. */
  181. tt_assert(chosen_entry);
  182. /* And with the other IP version active */
  183. mocked_options.ClientUseIPv6 = 1;
  184. chosen_entry = choose_random_entry(NULL);
  185. tt_assert(chosen_entry);
  186. /* And with the preference on auto */
  187. mocked_options.ClientPreferIPv6ORPort = -1;
  188. chosen_entry = choose_random_entry(NULL);
  189. tt_assert(chosen_entry);
  190. /* Check that we don't get a guard if it doesn't pass mandatory address
  191. * settings */
  192. memset(&mocked_options, 0, sizeof(mocked_options));
  193. mocked_options.ClientUseIPv4 = 0;
  194. mocked_options.ClientPreferIPv6ORPort = 0;
  195. mocked_options.UseDeprecatedGuardAlgorithm = 1;
  196. chosen_entry = choose_random_entry(NULL);
  197. /* If we don't allow IPv4 at all, we don't get a guard*/
  198. tt_assert(!chosen_entry);
  199. /* Check that we get a guard if it passes allowed but not preferred address
  200. * settings */
  201. memset(&mocked_options, 0, sizeof(mocked_options));
  202. mocked_options.ClientUseIPv4 = 1;
  203. mocked_options.ClientUseIPv6 = 1;
  204. mocked_options.ClientPreferIPv6ORPort = 1;
  205. mocked_options.UseDeprecatedGuardAlgorithm = 1;
  206. chosen_entry = choose_random_entry(NULL);
  207. tt_assert(chosen_entry);
  208. /* Check that we get a guard if it passes preferred address settings when
  209. * they're auto */
  210. memset(&mocked_options, 0, sizeof(mocked_options));
  211. mocked_options.ClientUseIPv4 = 1;
  212. mocked_options.ClientPreferIPv6ORPort = -1;
  213. mocked_options.UseDeprecatedGuardAlgorithm = 1;
  214. chosen_entry = choose_random_entry(NULL);
  215. tt_assert(chosen_entry);
  216. /* And with IPv6 active */
  217. mocked_options.ClientUseIPv6 = 1;
  218. chosen_entry = choose_random_entry(NULL);
  219. tt_assert(chosen_entry);
  220. done:
  221. memset(&mocked_options, 0, sizeof(mocked_options));
  222. UNMOCK(get_options);
  223. }
  224. /** Test choose_random_entry() with only one of our routers being a
  225. guard node. */
  226. static void
  227. test_choose_random_entry_one_possible_guard(void *arg)
  228. {
  229. const node_t *chosen_entry = NULL;
  230. node_t *the_guard = NULL;
  231. smartlist_t *our_nodelist = NULL;
  232. (void) arg;
  233. MOCK(get_options, mock_get_options);
  234. /* Set one of the nodes to be a guard. */
  235. our_nodelist = nodelist_get_list();
  236. the_guard = smartlist_get(our_nodelist, 4); /* chosen by fair dice roll */
  237. the_guard->is_possible_guard = 1;
  238. /* Check that we get the guard if it passes preferred
  239. * address settings */
  240. memset(&mocked_options, 0, sizeof(mocked_options));
  241. mocked_options.ClientUseIPv4 = 1;
  242. mocked_options.ClientPreferIPv6ORPort = 0;
  243. mocked_options.UseDeprecatedGuardAlgorithm = 1;
  244. /* Pick an entry. Make sure we pick the node we marked as guard. */
  245. chosen_entry = choose_random_entry(NULL);
  246. tt_ptr_op(chosen_entry, OP_EQ, the_guard);
  247. /* And with the other IP version active */
  248. mocked_options.ClientUseIPv6 = 1;
  249. chosen_entry = choose_random_entry(NULL);
  250. tt_ptr_op(chosen_entry, OP_EQ, the_guard);
  251. /* And with the preference on auto */
  252. mocked_options.ClientPreferIPv6ORPort = -1;
  253. chosen_entry = choose_random_entry(NULL);
  254. tt_ptr_op(chosen_entry, OP_EQ, the_guard);
  255. /* Check that we don't get a guard if it doesn't pass mandatory address
  256. * settings */
  257. memset(&mocked_options, 0, sizeof(mocked_options));
  258. mocked_options.ClientUseIPv4 = 0;
  259. mocked_options.ClientPreferIPv6ORPort = 0;
  260. mocked_options.UseDeprecatedGuardAlgorithm = 1;
  261. chosen_entry = choose_random_entry(NULL);
  262. /* If we don't allow IPv4 at all, we don't get a guard*/
  263. tt_assert(!chosen_entry);
  264. /* Check that we get a node if it passes allowed but not preferred
  265. * address settings */
  266. memset(&mocked_options, 0, sizeof(mocked_options));
  267. mocked_options.ClientUseIPv4 = 1;
  268. mocked_options.ClientUseIPv6 = 1;
  269. mocked_options.ClientPreferIPv6ORPort = 1;
  270. mocked_options.UseDeprecatedGuardAlgorithm = 1;
  271. chosen_entry = choose_random_entry(NULL);
  272. /* We disable the guard check and the preferred address check at the same
  273. * time, so we can't be sure we get the guard */
  274. tt_assert(chosen_entry);
  275. /* Check that we get a node if it is allowed but not preferred when settings
  276. * are auto */
  277. memset(&mocked_options, 0, sizeof(mocked_options));
  278. mocked_options.ClientUseIPv4 = 1;
  279. mocked_options.ClientPreferIPv6ORPort = -1;
  280. mocked_options.UseDeprecatedGuardAlgorithm = 1;
  281. chosen_entry = choose_random_entry(NULL);
  282. /* We disable the guard check and the preferred address check at the same
  283. * time, so we can't be sure we get the guard */
  284. tt_assert(chosen_entry);
  285. /* and with IPv6 active */
  286. mocked_options.ClientUseIPv6 = 1;
  287. chosen_entry = choose_random_entry(NULL);
  288. tt_assert(chosen_entry);
  289. done:
  290. memset(&mocked_options, 0, sizeof(mocked_options));
  291. UNMOCK(get_options);
  292. }
  293. /** Helper to conduct tests for populate_live_entry_guards().
  294. This test adds some entry guards to our list, and then tests
  295. populate_live_entry_guards() to mke sure it filters them correctly.
  296. <b>num_needed</b> is the number of guard nodes we support. It's
  297. configurable to make sure we function properly with 1 or 3 guard
  298. nodes configured.
  299. */
  300. static void
  301. populate_live_entry_guards_test_helper(int num_needed)
  302. {
  303. smartlist_t *our_nodelist = NULL;
  304. smartlist_t *live_entry_guards = smartlist_new();
  305. guard_selection_t *gs = get_guard_selection_info();
  306. const smartlist_t *all_entry_guards =
  307. get_entry_guards_for_guard_selection(gs);
  308. or_options_t *options = get_options_mutable();
  309. int retval;
  310. /* Set NumEntryGuards to the provided number. */
  311. options->NumEntryGuards = num_needed;
  312. tt_int_op(num_needed, OP_EQ, decide_num_guards(options, 0));
  313. /* The global entry guards smartlist should be empty now. */
  314. tt_int_op(smartlist_len(all_entry_guards), OP_EQ, 0);
  315. /* Walk the nodelist and add all nodes as entry guards. */
  316. our_nodelist = nodelist_get_list();
  317. tt_int_op(smartlist_len(our_nodelist), OP_EQ, HELPER_NUMBER_OF_DESCRIPTORS);
  318. SMARTLIST_FOREACH_BEGIN(our_nodelist, const node_t *, node) {
  319. const node_t *node_tmp;
  320. node_tmp = add_an_entry_guard(gs, node, 0, 1, 0, 0);
  321. tt_assert(node_tmp);
  322. } SMARTLIST_FOREACH_END(node);
  323. /* Make sure the nodes were added as entry guards. */
  324. tt_int_op(smartlist_len(all_entry_guards), OP_EQ,
  325. HELPER_NUMBER_OF_DESCRIPTORS);
  326. /* Ensure that all the possible entry guards are enough to satisfy us. */
  327. tt_int_op(smartlist_len(all_entry_guards), OP_GE, num_needed);
  328. /* Walk the entry guard list for some sanity checking */
  329. SMARTLIST_FOREACH_BEGIN(all_entry_guards, const entry_guard_t *, entry) {
  330. /* Since we called add_an_entry_guard() with 'for_discovery' being
  331. False, all guards should have made_contact enabled. */
  332. tt_int_op(entry->made_contact, OP_EQ, 1);
  333. } SMARTLIST_FOREACH_END(entry);
  334. /* First, try to get some fast guards. This should fail. */
  335. retval = populate_live_entry_guards(live_entry_guards,
  336. all_entry_guards,
  337. NULL,
  338. NO_DIRINFO, /* Don't care about DIRINFO*/
  339. 0, 0,
  340. 1); /* We want fast guard! */
  341. tt_int_op(retval, OP_EQ, 0);
  342. tt_int_op(smartlist_len(live_entry_guards), OP_EQ, 0);
  343. /* Now try to get some stable guards. This should fail too. */
  344. retval = populate_live_entry_guards(live_entry_guards,
  345. all_entry_guards,
  346. NULL,
  347. NO_DIRINFO,
  348. 0,
  349. 1, /* We want stable guard! */
  350. 0);
  351. tt_int_op(retval, OP_EQ, 0);
  352. tt_int_op(smartlist_len(live_entry_guards), OP_EQ, 0);
  353. /* Now try to get any guard we can find. This should succeed. */
  354. retval = populate_live_entry_guards(live_entry_guards,
  355. all_entry_guards,
  356. NULL,
  357. NO_DIRINFO,
  358. 0, 0, 0); /* No restrictions! */
  359. /* Since we had more than enough guards in 'all_entry_guards', we
  360. should have added 'num_needed' of them to live_entry_guards.
  361. 'retval' should be 1 since we now have enough live entry guards
  362. to pick one. */
  363. tt_int_op(retval, OP_EQ, 1);
  364. tt_int_op(smartlist_len(live_entry_guards), OP_EQ, num_needed);
  365. done:
  366. smartlist_free(live_entry_guards);
  367. }
  368. /* Test populate_live_entry_guards() for 1 guard node. */
  369. static void
  370. test_populate_live_entry_guards_1guard(void *arg)
  371. {
  372. (void) arg;
  373. populate_live_entry_guards_test_helper(1);
  374. }
  375. /* Test populate_live_entry_guards() for 3 guard nodes. */
  376. static void
  377. test_populate_live_entry_guards_3guards(void *arg)
  378. {
  379. (void) arg;
  380. populate_live_entry_guards_test_helper(3);
  381. }
  382. /** Append some EntryGuard lines to the Tor state at <b>state</b>.
  383. <b>entry_guard_lines</b> is a smartlist containing 2-tuple
  384. smartlists that carry the key and values of the statefile.
  385. As an example:
  386. entry_guard_lines =
  387. (("EntryGuard", "name 67E72FF33D7D41BF11C569646A0A7B4B188340DF DirCache"),
  388. ("EntryGuardDownSince", "2014-06-07 16:02:46 2014-06-07 16:02:46"))
  389. */
  390. static void
  391. state_insert_entry_guard_helper(or_state_t *state,
  392. smartlist_t *entry_guard_lines)
  393. {
  394. config_line_t **next, *line;
  395. next = &state->EntryGuards;
  396. *next = NULL;
  397. /* Loop over all the state lines in the smartlist */
  398. SMARTLIST_FOREACH_BEGIN(entry_guard_lines, const smartlist_t *,state_lines) {
  399. /* Get key and value for each line */
  400. const char *state_key = smartlist_get(state_lines, 0);
  401. const char *state_value = smartlist_get(state_lines, 1);
  402. *next = line = tor_malloc_zero(sizeof(config_line_t));
  403. line->key = tor_strdup(state_key);
  404. tor_asprintf(&line->value, "%s", state_value);
  405. next = &(line->next);
  406. } SMARTLIST_FOREACH_END(state_lines);
  407. }
  408. /** Free memory occupied by <b>entry_guard_lines</b>. */
  409. static void
  410. state_lines_free(smartlist_t *entry_guard_lines)
  411. {
  412. SMARTLIST_FOREACH_BEGIN(entry_guard_lines, smartlist_t *, state_lines) {
  413. char *state_key = smartlist_get(state_lines, 0);
  414. char *state_value = smartlist_get(state_lines, 1);
  415. tor_free(state_key);
  416. tor_free(state_value);
  417. smartlist_free(state_lines);
  418. } SMARTLIST_FOREACH_END(state_lines);
  419. smartlist_free(entry_guard_lines);
  420. }
  421. /* Tests entry_guards_parse_state(). It creates a fake Tor state with
  422. a saved entry guard and makes sure that Tor can parse it and
  423. creates the right entry node out of it.
  424. */
  425. static void
  426. test_entry_guards_parse_state_simple(void *arg)
  427. {
  428. or_options_t *options = get_options_mutable();
  429. options->UseDeprecatedGuardAlgorithm = 1;
  430. or_state_t *state = or_state_new();
  431. const smartlist_t *all_entry_guards = get_entry_guards();
  432. smartlist_t *entry_state_lines = smartlist_new();
  433. char *msg = NULL;
  434. int retval;
  435. /* Details of our fake guard node */
  436. const char *nickname = "hagbard";
  437. const char *fpr = "B29D536DD1752D542E1FBB3C9CE4449D51298212";
  438. const char *tor_version = "0.2.5.3-alpha-dev";
  439. const char *added_at = get_yesterday_date_str();
  440. const char *unlisted_since = "2014-06-08 16:16:50";
  441. (void) arg;
  442. /* The global entry guards smartlist should be empty now. */
  443. tt_int_op(smartlist_len(all_entry_guards), OP_EQ, 0);
  444. { /* Prepare the state entry */
  445. /* Prepare the smartlist to hold the key/value of each line */
  446. smartlist_t *state_line = smartlist_new();
  447. smartlist_add_asprintf(state_line, "EntryGuard");
  448. smartlist_add_asprintf(state_line, "%s %s %s", nickname, fpr, "DirCache");
  449. smartlist_add(entry_state_lines, state_line);
  450. state_line = smartlist_new();
  451. smartlist_add_asprintf(state_line, "EntryGuardAddedBy");
  452. smartlist_add_asprintf(state_line, "%s %s %s", fpr, tor_version, added_at);
  453. smartlist_add(entry_state_lines, state_line);
  454. state_line = smartlist_new();
  455. smartlist_add_asprintf(state_line, "EntryGuardUnlistedSince");
  456. smartlist_add_asprintf(state_line, "%s", unlisted_since);
  457. smartlist_add(entry_state_lines, state_line);
  458. }
  459. /* Inject our lines in the state */
  460. state_insert_entry_guard_helper(state, entry_state_lines);
  461. /* Parse state */
  462. retval = entry_guards_parse_state(state, 1, &msg);
  463. tt_int_op(retval, OP_GE, 0);
  464. /* Test that the guard was registered.
  465. We need to re-get the entry guard list since its pointer was
  466. overwritten in entry_guards_parse_state(). */
  467. all_entry_guards = get_entry_guards();
  468. tt_int_op(smartlist_len(all_entry_guards), OP_EQ, 1);
  469. { /* Test the entry guard structure */
  470. char hex_digest[1024];
  471. char str_time[1024];
  472. const entry_guard_t *e = smartlist_get(all_entry_guards, 0);
  473. tt_str_op(e->nickname, OP_EQ, nickname); /* Verify nickname */
  474. base16_encode(hex_digest, sizeof(hex_digest),
  475. e->identity, DIGEST_LEN);
  476. tt_str_op(hex_digest, OP_EQ, fpr); /* Verify fingerprint */
  477. tt_assert(e->is_dir_cache); /* Verify dirness */
  478. tt_str_op(e->chosen_by_version, OP_EQ, tor_version); /* Verify version */
  479. tt_assert(e->made_contact); /* All saved guards have been contacted */
  480. tt_assert(e->bad_since); /* Verify bad_since timestamp */
  481. format_iso_time(str_time, e->bad_since);
  482. tt_str_op(str_time, OP_EQ, unlisted_since);
  483. /* The rest should be unset */
  484. tt_assert(!e->unreachable_since);
  485. tt_assert(!e->can_retry);
  486. tt_assert(!e->pb.path_bias_noticed);
  487. tt_assert(!e->pb.path_bias_warned);
  488. tt_assert(!e->pb.path_bias_extreme);
  489. tt_assert(!e->pb.path_bias_disabled);
  490. tt_assert(!e->pb.path_bias_use_noticed);
  491. tt_assert(!e->pb.path_bias_use_extreme);
  492. tt_assert(!e->last_attempted);
  493. }
  494. done:
  495. state_lines_free(entry_state_lines);
  496. or_state_free(state);
  497. tor_free(msg);
  498. }
  499. /** Similar to test_entry_guards_parse_state_simple() but aims to test
  500. the PathBias-related details of the entry guard. */
  501. static void
  502. test_entry_guards_parse_state_pathbias(void *arg)
  503. {
  504. or_options_t *options = get_options_mutable();
  505. options->UseDeprecatedGuardAlgorithm = 1;
  506. or_state_t *state = or_state_new();
  507. const smartlist_t *all_entry_guards = get_entry_guards();
  508. char *msg = NULL;
  509. int retval;
  510. smartlist_t *entry_state_lines = smartlist_new();
  511. /* Path bias details of the fake guard */
  512. const double circ_attempts = 9;
  513. const double circ_successes = 8;
  514. const double successful_closed = 4;
  515. const double collapsed = 2;
  516. const double unusable = 0;
  517. const double timeouts = 1;
  518. (void) arg;
  519. /* The global entry guards smartlist should be empty now. */
  520. tt_int_op(smartlist_len(all_entry_guards), OP_EQ, 0);
  521. { /* Prepare the state entry */
  522. /* Prepare the smartlist to hold the key/value of each line */
  523. smartlist_t *state_line = smartlist_new();
  524. smartlist_add_asprintf(state_line, "EntryGuard");
  525. smartlist_add_asprintf(state_line,
  526. "givethanks B29D536DD1752D542E1FBB3C9CE4449D51298212 NoDirCache");
  527. smartlist_add(entry_state_lines, state_line);
  528. state_line = smartlist_new();
  529. smartlist_add_asprintf(state_line, "EntryGuardAddedBy");
  530. smartlist_add_asprintf(state_line,
  531. "B29D536DD1752D542E1FBB3C9CE4449D51298212 0.2.5.3-alpha-dev "
  532. "%s", get_yesterday_date_str());
  533. smartlist_add(entry_state_lines, state_line);
  534. state_line = smartlist_new();
  535. smartlist_add_asprintf(state_line, "EntryGuardUnlistedSince");
  536. smartlist_add_asprintf(state_line, "2014-06-08 16:16:50");
  537. smartlist_add(entry_state_lines, state_line);
  538. state_line = smartlist_new();
  539. smartlist_add_asprintf(state_line, "EntryGuardPathBias");
  540. smartlist_add_asprintf(state_line, "%f %f %f %f %f %f",
  541. circ_attempts, circ_successes, successful_closed,
  542. collapsed, unusable, timeouts);
  543. smartlist_add(entry_state_lines, state_line);
  544. }
  545. /* Inject our lines in the state */
  546. state_insert_entry_guard_helper(state, entry_state_lines);
  547. /* Parse state */
  548. retval = entry_guards_parse_state(state, 1, &msg);
  549. tt_int_op(retval, OP_GE, 0);
  550. /* Test that the guard was registered */
  551. all_entry_guards = get_entry_guards();
  552. tt_int_op(smartlist_len(all_entry_guards), OP_EQ, 1);
  553. { /* Test the path bias of this guard */
  554. const entry_guard_t *e = smartlist_get(all_entry_guards, 0);
  555. tt_assert(!e->is_dir_cache);
  556. tt_assert(!e->can_retry);
  557. /* XXX tt_double_op doesn't support equality. Cast to int for now. */
  558. tt_int_op((int)e->pb.circ_attempts, OP_EQ, (int)circ_attempts);
  559. tt_int_op((int)e->pb.circ_successes, OP_EQ, (int)circ_successes);
  560. tt_int_op((int)e->pb.successful_circuits_closed, OP_EQ,
  561. (int)successful_closed);
  562. tt_int_op((int)e->pb.timeouts, OP_EQ, (int)timeouts);
  563. tt_int_op((int)e->pb.collapsed_circuits, OP_EQ, (int)collapsed);
  564. tt_int_op((int)e->pb.unusable_circuits, OP_EQ, (int)unusable);
  565. }
  566. done:
  567. or_state_free(state);
  568. state_lines_free(entry_state_lines);
  569. tor_free(msg);
  570. }
  571. /* Simple test of entry_guards_set_from_config() by specifying a
  572. particular EntryNode and making sure it gets picked. */
  573. static void
  574. test_entry_guards_set_from_config(void *arg)
  575. {
  576. or_options_t *options = get_options_mutable();
  577. options->UseDeprecatedGuardAlgorithm = 1;
  578. guard_selection_t *gs = get_guard_selection_info();
  579. const smartlist_t *all_entry_guards =
  580. get_entry_guards_for_guard_selection(gs);
  581. const char *entrynodes_str = "test003r";
  582. const node_t *chosen_entry = NULL;
  583. int retval;
  584. (void) arg;
  585. /* Prase EntryNodes as a routerset. */
  586. options->EntryNodes = routerset_new();
  587. retval = routerset_parse(options->EntryNodes,
  588. entrynodes_str,
  589. "test_entrynodes");
  590. tt_int_op(retval, OP_GE, 0);
  591. /* Read nodes from EntryNodes */
  592. entry_guards_set_from_config(gs, options);
  593. /* Test that only one guard was added. */
  594. tt_int_op(smartlist_len(all_entry_guards), OP_EQ, 1);
  595. /* Make sure it was the guard we specified. */
  596. chosen_entry = choose_random_entry(NULL);
  597. tt_str_op(chosen_entry->ri->nickname, OP_EQ, entrynodes_str);
  598. done:
  599. routerset_free(options->EntryNodes);
  600. }
  601. static void
  602. test_entry_is_time_to_retry(void *arg)
  603. {
  604. entry_guard_t *test_guard;
  605. time_t now;
  606. int retval;
  607. (void)arg;
  608. now = time(NULL);
  609. test_guard = tor_malloc_zero(sizeof(entry_guard_t));
  610. test_guard->last_attempted = now - 10;
  611. test_guard->unreachable_since = now - 1;
  612. retval = entry_is_time_to_retry(test_guard,now);
  613. tt_int_op(retval,OP_EQ,1);
  614. test_guard->unreachable_since = now - (6*60*60 - 1);
  615. test_guard->last_attempted = now - (60*60 + 1);
  616. retval = entry_is_time_to_retry(test_guard,now);
  617. tt_int_op(retval,OP_EQ,1);
  618. test_guard->last_attempted = now - (60*60 - 1);
  619. retval = entry_is_time_to_retry(test_guard,now);
  620. tt_int_op(retval,OP_EQ,0);
  621. test_guard->unreachable_since = now - (6*60*60 + 1);
  622. test_guard->last_attempted = now - (4*60*60 + 1);
  623. retval = entry_is_time_to_retry(test_guard,now);
  624. tt_int_op(retval,OP_EQ,1);
  625. test_guard->unreachable_since = now - (3*24*60*60 - 1);
  626. test_guard->last_attempted = now - (4*60*60 + 1);
  627. retval = entry_is_time_to_retry(test_guard,now);
  628. tt_int_op(retval,OP_EQ,1);
  629. test_guard->unreachable_since = now - (3*24*60*60 + 1);
  630. test_guard->last_attempted = now - (18*60*60 + 1);
  631. retval = entry_is_time_to_retry(test_guard,now);
  632. tt_int_op(retval,OP_EQ,1);
  633. test_guard->unreachable_since = now - (7*24*60*60 - 1);
  634. test_guard->last_attempted = now - (18*60*60 + 1);
  635. retval = entry_is_time_to_retry(test_guard,now);
  636. tt_int_op(retval,OP_EQ,1);
  637. test_guard->last_attempted = now - (18*60*60 - 1);
  638. retval = entry_is_time_to_retry(test_guard,now);
  639. tt_int_op(retval,OP_EQ,0);
  640. test_guard->unreachable_since = now - (7*24*60*60 + 1);
  641. test_guard->last_attempted = now - (36*60*60 + 1);
  642. retval = entry_is_time_to_retry(test_guard,now);
  643. tt_int_op(retval,OP_EQ,1);
  644. test_guard->unreachable_since = now - (7*24*60*60 + 1);
  645. test_guard->last_attempted = now - (36*60*60 + 1);
  646. retval = entry_is_time_to_retry(test_guard,now);
  647. tt_int_op(retval,OP_EQ,1);
  648. done:
  649. tor_free(test_guard);
  650. }
  651. /** XXX Do some tests that entry_is_live() */
  652. static void
  653. test_entry_is_live(void *arg)
  654. {
  655. smartlist_t *our_nodelist = NULL;
  656. guard_selection_t *gs = get_guard_selection_info();
  657. const smartlist_t *all_entry_guards =
  658. get_entry_guards_for_guard_selection(gs);
  659. const node_t *test_node = NULL;
  660. const entry_guard_t *test_entry = NULL;
  661. const char *msg;
  662. int which_node;
  663. (void) arg;
  664. /* The global entry guards smartlist should be empty now. */
  665. tt_int_op(smartlist_len(all_entry_guards), OP_EQ, 0);
  666. /* Walk the nodelist and add all nodes as entry guards. */
  667. our_nodelist = nodelist_get_list();
  668. tt_int_op(smartlist_len(our_nodelist), OP_EQ, HELPER_NUMBER_OF_DESCRIPTORS);
  669. SMARTLIST_FOREACH_BEGIN(our_nodelist, const node_t *, node) {
  670. const node_t *node_tmp;
  671. node_tmp = add_an_entry_guard(gs, node, 0, 1, 0, 0);
  672. tt_assert(node_tmp);
  673. tt_int_op(node->is_stable, OP_EQ, 0);
  674. tt_int_op(node->is_fast, OP_EQ, 0);
  675. } SMARTLIST_FOREACH_END(node);
  676. /* Make sure the nodes were added as entry guards. */
  677. tt_int_op(smartlist_len(all_entry_guards), OP_EQ,
  678. HELPER_NUMBER_OF_DESCRIPTORS);
  679. /* Now get a random test entry that we will use for this unit test. */
  680. which_node = 3; /* (chosen by fair dice roll) */
  681. test_entry = smartlist_get(all_entry_guards, which_node);
  682. /* Let's do some entry_is_live() tests! */
  683. /* Require the node to be stable, but it's not. Should fail.
  684. Also enable 'assume_reachable' because why not. */
  685. test_node = entry_is_live(test_entry,
  686. ENTRY_NEED_UPTIME | ENTRY_ASSUME_REACHABLE,
  687. &msg);
  688. tt_assert(!test_node);
  689. /* Require the node to be fast, but it's not. Should fail. */
  690. test_node = entry_is_live(test_entry,
  691. ENTRY_NEED_CAPACITY | ENTRY_ASSUME_REACHABLE,
  692. &msg);
  693. tt_assert(!test_node);
  694. /* Don't impose any restrictions on the node. Should succeed. */
  695. test_node = entry_is_live(test_entry, 0, &msg);
  696. tt_assert(test_node);
  697. tt_ptr_op(test_node, OP_EQ, node_get_by_id(test_entry->identity));
  698. /* Require descriptor for this node. It has one so it should succeed. */
  699. test_node = entry_is_live(test_entry, ENTRY_NEED_DESCRIPTOR, &msg);
  700. tt_assert(test_node);
  701. tt_ptr_op(test_node, OP_EQ, node_get_by_id(test_entry->identity));
  702. done:
  703. ; /* XXX */
  704. }
  705. #define TEST_IPV4_ADDR "123.45.67.89"
  706. #define TEST_IPV6_ADDR "[1234:5678:90ab:cdef::]"
  707. static void
  708. test_node_preferred_orport(void *arg)
  709. {
  710. (void)arg;
  711. tor_addr_t ipv4_addr;
  712. const uint16_t ipv4_port = 4444;
  713. tor_addr_t ipv6_addr;
  714. const uint16_t ipv6_port = 6666;
  715. routerinfo_t node_ri;
  716. node_t node;
  717. tor_addr_port_t ap;
  718. /* Setup options */
  719. memset(&mocked_options, 0, sizeof(mocked_options));
  720. /* We don't test ClientPreferIPv6ORPort here, because it's used in
  721. * nodelist_set_consensus to setup node.ipv6_preferred, which we set
  722. * directly. */
  723. MOCK(get_options, mock_get_options);
  724. /* Setup IP addresses */
  725. tor_addr_parse(&ipv4_addr, TEST_IPV4_ADDR);
  726. tor_addr_parse(&ipv6_addr, TEST_IPV6_ADDR);
  727. /* Setup node_ri */
  728. memset(&node_ri, 0, sizeof(node_ri));
  729. node_ri.addr = tor_addr_to_ipv4h(&ipv4_addr);
  730. node_ri.or_port = ipv4_port;
  731. tor_addr_copy(&node_ri.ipv6_addr, &ipv6_addr);
  732. node_ri.ipv6_orport = ipv6_port;
  733. /* Setup node */
  734. memset(&node, 0, sizeof(node));
  735. node.ri = &node_ri;
  736. /* Check the preferred address is IPv4 if we're only using IPv4, regardless
  737. * of whether we prefer it or not */
  738. mocked_options.ClientUseIPv4 = 1;
  739. mocked_options.ClientUseIPv6 = 0;
  740. node.ipv6_preferred = 0;
  741. node_get_pref_orport(&node, &ap);
  742. tt_assert(tor_addr_eq(&ap.addr, &ipv4_addr));
  743. tt_assert(ap.port == ipv4_port);
  744. node.ipv6_preferred = 1;
  745. node_get_pref_orport(&node, &ap);
  746. tt_assert(tor_addr_eq(&ap.addr, &ipv4_addr));
  747. tt_assert(ap.port == ipv4_port);
  748. /* Check the preferred address is IPv4 if we're using IPv4 and IPv6, but
  749. * don't prefer the IPv6 address */
  750. mocked_options.ClientUseIPv4 = 1;
  751. mocked_options.ClientUseIPv6 = 1;
  752. node.ipv6_preferred = 0;
  753. node_get_pref_orport(&node, &ap);
  754. tt_assert(tor_addr_eq(&ap.addr, &ipv4_addr));
  755. tt_assert(ap.port == ipv4_port);
  756. /* Check the preferred address is IPv6 if we prefer it and
  757. * ClientUseIPv6 is 1, regardless of ClientUseIPv4 */
  758. mocked_options.ClientUseIPv4 = 1;
  759. mocked_options.ClientUseIPv6 = 1;
  760. node.ipv6_preferred = 1;
  761. node_get_pref_orport(&node, &ap);
  762. tt_assert(tor_addr_eq(&ap.addr, &ipv6_addr));
  763. tt_assert(ap.port == ipv6_port);
  764. mocked_options.ClientUseIPv4 = 0;
  765. node_get_pref_orport(&node, &ap);
  766. tt_assert(tor_addr_eq(&ap.addr, &ipv6_addr));
  767. tt_assert(ap.port == ipv6_port);
  768. /* Check the preferred address is IPv6 if we don't prefer it, but
  769. * ClientUseIPv4 is 0 */
  770. mocked_options.ClientUseIPv4 = 0;
  771. mocked_options.ClientUseIPv6 = 1;
  772. node.ipv6_preferred = fascist_firewall_prefer_ipv6_orport(&mocked_options);
  773. node_get_pref_orport(&node, &ap);
  774. tt_assert(tor_addr_eq(&ap.addr, &ipv6_addr));
  775. tt_assert(ap.port == ipv6_port);
  776. done:
  777. UNMOCK(get_options);
  778. }
  779. static void
  780. test_entry_guard_describe(void *arg)
  781. {
  782. (void)arg;
  783. entry_guard_t g;
  784. memset(&g, 0, sizeof(g));
  785. strlcpy(g.nickname, "okefenokee", sizeof(g.nickname));
  786. memcpy(g.identity, "theforestprimeval---", DIGEST_LEN);
  787. tt_str_op(entry_guard_describe(&g), OP_EQ,
  788. "okefenokee ($746865666F726573747072696D6576616C2D2D2D)");
  789. done:
  790. ;
  791. }
  792. static void
  793. test_entry_guard_randomize_time(void *arg)
  794. {
  795. const time_t now = 1479153573;
  796. const int delay = 86400;
  797. const int N = 1000;
  798. (void)arg;
  799. time_t t;
  800. int i;
  801. for (i = 0; i < N; ++i) {
  802. t = randomize_time(now, delay);
  803. tt_int_op(t, OP_LE, now);
  804. tt_int_op(t, OP_GE, now-delay);
  805. }
  806. /* now try the corner cases */
  807. for (i = 0; i < N; ++i) {
  808. t = randomize_time(100, delay);
  809. tt_int_op(t, OP_GE, 1);
  810. tt_int_op(t, OP_LE, 100);
  811. t = randomize_time(0, delay);
  812. tt_int_op(t, OP_EQ, 1);
  813. }
  814. done:
  815. ;
  816. }
  817. static void
  818. test_entry_guard_encode_for_state_minimal(void *arg)
  819. {
  820. (void) arg;
  821. entry_guard_t *eg = tor_malloc_zero(sizeof(entry_guard_t));
  822. eg->selection_name = tor_strdup("wubwub");
  823. memcpy(eg->identity, "plurpyflurpyslurpydo", DIGEST_LEN);
  824. eg->sampled_on_date = 1479081600;
  825. eg->confirmed_idx = -1;
  826. char *s = NULL;
  827. s = entry_guard_encode_for_state(eg);
  828. tt_str_op(s, OP_EQ,
  829. "in=wubwub "
  830. "rsa_id=706C75727079666C75727079736C75727079646F "
  831. "sampled_on=2016-11-14T00:00:00 "
  832. "listed=0");
  833. done:
  834. entry_guard_free(eg);
  835. tor_free(s);
  836. }
  837. static void
  838. test_entry_guard_encode_for_state_maximal(void *arg)
  839. {
  840. (void) arg;
  841. entry_guard_t *eg = tor_malloc_zero(sizeof(entry_guard_t));
  842. strlcpy(eg->nickname, "Fred", sizeof(eg->nickname));
  843. eg->selection_name = tor_strdup("default");
  844. memcpy(eg->identity, "plurpyflurpyslurpydo", DIGEST_LEN);
  845. eg->bridge_addr = tor_malloc_zero(sizeof(tor_addr_port_t));
  846. tor_addr_from_ipv4h(&eg->bridge_addr->addr, 0x08080404);
  847. eg->bridge_addr->port = 9999;
  848. eg->sampled_on_date = 1479081600;
  849. eg->sampled_by_version = tor_strdup("1.2.3");
  850. eg->unlisted_since_date = 1479081645;
  851. eg->currently_listed = 1;
  852. eg->confirmed_on_date = 1479081690;
  853. eg->confirmed_idx = 333;
  854. eg->extra_state_fields = tor_strdup("and the green grass grew all around");
  855. char *s = NULL;
  856. s = entry_guard_encode_for_state(eg);
  857. tt_str_op(s, OP_EQ,
  858. "in=default "
  859. "rsa_id=706C75727079666C75727079736C75727079646F "
  860. "bridge_addr=8.8.4.4:9999 "
  861. "nickname=Fred "
  862. "sampled_on=2016-11-14T00:00:00 "
  863. "sampled_by=1.2.3 "
  864. "unlisted_since=2016-11-14T00:00:45 "
  865. "listed=1 "
  866. "confirmed_on=2016-11-14T00:01:30 "
  867. "confirmed_idx=333 "
  868. "and the green grass grew all around");
  869. done:
  870. entry_guard_free(eg);
  871. tor_free(s);
  872. }
  873. static void
  874. test_entry_guard_parse_from_state_minimal(void *arg)
  875. {
  876. (void)arg;
  877. char *mem_op_hex_tmp = NULL;
  878. entry_guard_t *eg = NULL;
  879. time_t t = approx_time();
  880. eg = entry_guard_parse_from_state(
  881. "in=default_plus "
  882. "rsa_id=596f75206d6179206e656564206120686f626279");
  883. tt_assert(eg);
  884. tt_str_op(eg->selection_name, OP_EQ, "default_plus");
  885. test_mem_op_hex(eg->identity, OP_EQ,
  886. "596f75206d6179206e656564206120686f626279");
  887. tt_str_op(eg->nickname, OP_EQ, "$596F75206D6179206E656564206120686F626279");
  888. tt_ptr_op(eg->bridge_addr, OP_EQ, NULL);
  889. tt_i64_op(eg->sampled_on_date, OP_GE, t);
  890. tt_i64_op(eg->sampled_on_date, OP_LE, t+86400);
  891. tt_i64_op(eg->unlisted_since_date, OP_EQ, 0);
  892. tt_ptr_op(eg->sampled_by_version, OP_EQ, NULL);
  893. tt_int_op(eg->currently_listed, OP_EQ, 0);
  894. tt_i64_op(eg->confirmed_on_date, OP_EQ, 0);
  895. tt_int_op(eg->confirmed_idx, OP_EQ, -1);
  896. tt_int_op(eg->last_tried_to_connect, OP_EQ, 0);
  897. tt_int_op(eg->is_reachable, OP_EQ, GUARD_REACHABLE_MAYBE);
  898. done:
  899. entry_guard_free(eg);
  900. tor_free(mem_op_hex_tmp);
  901. }
  902. static void
  903. test_entry_guard_parse_from_state_maximal(void *arg)
  904. {
  905. (void)arg;
  906. char *mem_op_hex_tmp = NULL;
  907. entry_guard_t *eg = NULL;
  908. eg = entry_guard_parse_from_state(
  909. "in=fred "
  910. "rsa_id=706C75727079666C75727079736C75727079646F "
  911. "bridge_addr=[1::3]:9999 "
  912. "nickname=Fred "
  913. "sampled_on=2016-11-14T00:00:00 "
  914. "sampled_by=1.2.3 "
  915. "unlisted_since=2016-11-14T00:00:45 "
  916. "listed=1 "
  917. "confirmed_on=2016-11-14T00:01:30 "
  918. "confirmed_idx=333 "
  919. "and the green grass grew all around "
  920. "rsa_id=all,around");
  921. tt_assert(eg);
  922. test_mem_op_hex(eg->identity, OP_EQ,
  923. "706C75727079666C75727079736C75727079646F");
  924. tt_str_op(fmt_addr(&eg->bridge_addr->addr), OP_EQ, "1::3");
  925. tt_int_op(eg->bridge_addr->port, OP_EQ, 9999);
  926. tt_str_op(eg->nickname, OP_EQ, "Fred");
  927. tt_i64_op(eg->sampled_on_date, OP_EQ, 1479081600);
  928. tt_i64_op(eg->unlisted_since_date, OP_EQ, 1479081645);
  929. tt_str_op(eg->sampled_by_version, OP_EQ, "1.2.3");
  930. tt_int_op(eg->currently_listed, OP_EQ, 1);
  931. tt_i64_op(eg->confirmed_on_date, OP_EQ, 1479081690);
  932. tt_int_op(eg->confirmed_idx, OP_EQ, 333);
  933. tt_str_op(eg->extra_state_fields, OP_EQ,
  934. "and the green grass grew all around rsa_id=all,around");
  935. tt_int_op(eg->last_tried_to_connect, OP_EQ, 0);
  936. tt_int_op(eg->is_reachable, OP_EQ, GUARD_REACHABLE_MAYBE);
  937. done:
  938. entry_guard_free(eg);
  939. tor_free(mem_op_hex_tmp);
  940. }
  941. static void
  942. test_entry_guard_parse_from_state_failure(void *arg)
  943. {
  944. (void)arg;
  945. entry_guard_t *eg = NULL;
  946. /* no selection */
  947. eg = entry_guard_parse_from_state(
  948. "rsa_id=596f75206d6179206e656564206120686f626270");
  949. tt_assert(! eg);
  950. /* no RSA ID. */
  951. eg = entry_guard_parse_from_state("in=default nickname=Fred");
  952. tt_assert(! eg);
  953. /* Bad RSA ID: bad character. */
  954. eg = entry_guard_parse_from_state(
  955. "in=default "
  956. "rsa_id=596f75206d6179206e656564206120686f62627q");
  957. tt_assert(! eg);
  958. /* Bad RSA ID: too long.*/
  959. eg = entry_guard_parse_from_state(
  960. "in=default "
  961. "rsa_id=596f75206d6179206e656564206120686f6262703");
  962. tt_assert(! eg);
  963. /* Bad RSA ID: too short.*/
  964. eg = entry_guard_parse_from_state(
  965. "in=default "
  966. "rsa_id=596f75206d6179206e65656420612");
  967. tt_assert(! eg);
  968. done:
  969. entry_guard_free(eg);
  970. }
  971. static void
  972. test_entry_guard_parse_from_state_partial_failure(void *arg)
  973. {
  974. (void)arg;
  975. char *mem_op_hex_tmp = NULL;
  976. entry_guard_t *eg = NULL;
  977. time_t t = approx_time();
  978. eg = entry_guard_parse_from_state(
  979. "in=default "
  980. "rsa_id=706C75727079666C75727079736C75727079646F "
  981. "bridge_addr=1.2.3.3.4:5 "
  982. "nickname=FredIsANodeWithAStrangeNicknameThatIsTooLong "
  983. "sampled_on=2016-11-14T00:00:99 "
  984. "sampled_by=1.2.3 stuff in the middle "
  985. "unlisted_since=2016-xx-14T00:00:45 "
  986. "listed=0 "
  987. "confirmed_on=2016-11-14T00:01:30zz "
  988. "confirmed_idx=idx "
  989. "and the green grass grew all around "
  990. "rsa_id=all,around");
  991. tt_assert(eg);
  992. test_mem_op_hex(eg->identity, OP_EQ,
  993. "706C75727079666C75727079736C75727079646F");
  994. tt_str_op(eg->nickname, OP_EQ, "FredIsANodeWithAStrangeNicknameThatIsTooL");
  995. tt_ptr_op(eg->bridge_addr, OP_EQ, NULL);
  996. tt_i64_op(eg->sampled_on_date, OP_EQ, t);
  997. tt_i64_op(eg->unlisted_since_date, OP_EQ, 0);
  998. tt_str_op(eg->sampled_by_version, OP_EQ, "1.2.3");
  999. tt_int_op(eg->currently_listed, OP_EQ, 0);
  1000. tt_i64_op(eg->confirmed_on_date, OP_EQ, 0);
  1001. tt_int_op(eg->confirmed_idx, OP_EQ, -1);
  1002. tt_str_op(eg->extra_state_fields, OP_EQ,
  1003. "stuff in the middle and the green grass grew all around "
  1004. "rsa_id=all,around");
  1005. tt_int_op(eg->last_tried_to_connect, OP_EQ, 0);
  1006. tt_int_op(eg->is_reachable, OP_EQ, GUARD_REACHABLE_MAYBE);
  1007. done:
  1008. entry_guard_free(eg);
  1009. tor_free(mem_op_hex_tmp);
  1010. }
  1011. static void
  1012. test_entry_guard_get_guard_selection_by_name(void *arg)
  1013. {
  1014. (void)arg;
  1015. guard_selection_t *gs1, *gs2, *gs3;
  1016. gs1 = get_guard_selection_by_name("unlikely", GS_TYPE_NORMAL, 0);
  1017. tt_assert(gs1 == NULL);
  1018. gs1 = get_guard_selection_by_name("unlikely", GS_TYPE_NORMAL, 1);
  1019. tt_assert(gs1 != NULL);
  1020. gs2 = get_guard_selection_by_name("unlikely", GS_TYPE_NORMAL, 1);
  1021. tt_assert(gs2 == gs1);
  1022. gs2 = get_guard_selection_by_name("unlikely", GS_TYPE_NORMAL, 0);
  1023. tt_assert(gs2 == gs1);
  1024. gs2 = get_guard_selection_by_name("implausible", GS_TYPE_NORMAL, 0);
  1025. tt_assert(gs2 == NULL);
  1026. gs2 = get_guard_selection_by_name("implausible", GS_TYPE_NORMAL, 1);
  1027. tt_assert(gs2 != NULL);
  1028. tt_assert(gs2 != gs1);
  1029. gs3 = get_guard_selection_by_name("implausible", GS_TYPE_NORMAL, 0);
  1030. tt_assert(gs3 == gs2);
  1031. gs3 = get_guard_selection_by_name("default", GS_TYPE_NORMAL, 0);
  1032. tt_assert(gs3 == NULL);
  1033. gs3 = get_guard_selection_by_name("default", GS_TYPE_NORMAL, 1);
  1034. tt_assert(gs3 != NULL);
  1035. tt_assert(gs3 != gs2);
  1036. tt_assert(gs3 != gs1);
  1037. // XXXX prop271 re-enable this. tt_assert(gs3 == get_guard_selection_info());
  1038. #if 0
  1039. or_options_t *options = get_options_mutable();
  1040. options->UseDeprecatedGuardAlgorithm = 1;
  1041. gs4 = get_guard_selection_info();
  1042. tt_assert(gs4 != gs3);
  1043. tt_assert(gs4 == get_guard_selection_by_name("legacy", 1));
  1044. options->UseDeprecatedGuardAlgorithm = 0;
  1045. tt_assert(gs3 == get_guard_selection_info());
  1046. #endif
  1047. done:
  1048. entry_guards_free_all();
  1049. }
  1050. static void
  1051. test_entry_guard_add_single_guard(void *arg)
  1052. {
  1053. (void)arg;
  1054. guard_selection_t *gs = guard_selection_new("default", GS_TYPE_NORMAL);
  1055. /* 1: Add a single guard to the sample. */
  1056. node_t *n1 = smartlist_get(big_fake_net_nodes, 0);
  1057. time_t now = approx_time();
  1058. tt_assert(n1->is_possible_guard == 1);
  1059. entry_guard_t *g1 = entry_guard_add_to_sample(gs, n1);
  1060. tt_assert(g1);
  1061. /* Make sure its fields look right. */
  1062. tt_mem_op(n1->identity, OP_EQ, g1->identity, DIGEST_LEN);
  1063. tt_i64_op(g1->sampled_on_date, OP_GE, now - 12*86400);
  1064. tt_i64_op(g1->sampled_on_date, OP_LE, now);
  1065. tt_str_op(g1->sampled_by_version, OP_EQ, VERSION);
  1066. tt_assert(g1->currently_listed == 1);
  1067. tt_i64_op(g1->confirmed_on_date, OP_EQ, 0);
  1068. tt_int_op(g1->confirmed_idx, OP_EQ, -1);
  1069. tt_int_op(g1->last_tried_to_connect, OP_EQ, 0);
  1070. tt_uint_op(g1->is_reachable, OP_EQ, GUARD_REACHABLE_MAYBE);
  1071. tt_i64_op(g1->failing_since, OP_EQ, 0);
  1072. tt_assert(g1->is_filtered_guard == 1);
  1073. tt_assert(g1->is_usable_filtered_guard == 1);
  1074. tt_assert(g1->is_primary == 0);
  1075. tt_assert(g1->extra_state_fields == NULL);
  1076. /* Make sure it got added. */
  1077. tt_int_op(1, OP_EQ, smartlist_len(gs->sampled_entry_guards));
  1078. tt_ptr_op(g1, OP_EQ, smartlist_get(gs->sampled_entry_guards, 0));
  1079. tt_ptr_op(g1, OP_EQ, get_sampled_guard_with_id(gs, (uint8_t*)n1->identity));
  1080. const uint8_t bad_id[20] = {0};
  1081. tt_ptr_op(NULL, OP_EQ, get_sampled_guard_with_id(gs, bad_id));
  1082. done:
  1083. guard_selection_free(gs);
  1084. }
  1085. static void
  1086. test_entry_guard_node_filter(void *arg)
  1087. {
  1088. (void)arg;
  1089. guard_selection_t *gs = guard_selection_new("default", GS_TYPE_NORMAL);
  1090. bridge_line_t *bl = NULL;
  1091. /* Initialize a bunch of node objects that are all guards. */
  1092. const int NUM = 7;
  1093. node_t *n[NUM];
  1094. entry_guard_t *g[NUM];
  1095. int i;
  1096. for (i=0; i < NUM; ++i) {
  1097. n[i] = smartlist_get(big_fake_net_nodes, i*2); // even ones are guards.
  1098. g[i] = entry_guard_add_to_sample(gs, n[i]);
  1099. // everything starts out filtered-in
  1100. tt_assert(g[i]->is_filtered_guard == 1);
  1101. tt_assert(g[i]->is_usable_filtered_guard == 1);
  1102. }
  1103. tt_int_op(num_reachable_filtered_guards(gs), OP_EQ, NUM);
  1104. /* Make sure refiltering doesn't hurt */
  1105. entry_guards_update_filtered_sets(gs);
  1106. for (i = 0; i < NUM; ++i) {
  1107. tt_assert(g[i]->is_filtered_guard == 1);
  1108. tt_assert(g[i]->is_usable_filtered_guard == 1);
  1109. }
  1110. tt_int_op(num_reachable_filtered_guards(gs), OP_EQ, NUM);
  1111. /* Now start doing things to make the guards get filtered out, 1 by 1. */
  1112. /* 0: Not listed. */
  1113. g[0]->currently_listed = 0;
  1114. /* 1: path bias says this guard is maybe eeeevil. */
  1115. g[1]->pb.path_bias_disabled = 1;
  1116. /* 2: Unreachable address. */
  1117. n[2]->rs->addr = 0;
  1118. /* 3: ExcludeNodes */
  1119. n[3]->rs->addr = 0x90902020;
  1120. routerset_free(get_options_mutable()->ExcludeNodes);
  1121. get_options_mutable()->ExcludeNodes = routerset_new();
  1122. routerset_parse(get_options_mutable()->ExcludeNodes, "144.144.0.0/16", "");
  1123. /* 4: Bridge. */
  1124. sweep_bridge_list();
  1125. bl = tor_malloc_zero(sizeof(bridge_line_t));
  1126. tor_addr_from_ipv4h(&bl->addr, n[4]->rs->addr);
  1127. bl->port = n[4]->rs->or_port;
  1128. memcpy(bl->digest, n[4]->identity, 20);
  1129. bridge_add_from_config(bl);
  1130. bl = NULL; // prevent free.
  1131. /* 5: Unreachable. This stays in the filter, but isn't in usable-filtered */
  1132. g[5]->last_tried_to_connect = approx_time(); // prevent retry.
  1133. g[5]->is_reachable = GUARD_REACHABLE_NO;
  1134. /* 6: no change. */
  1135. /* Now refilter and inspect. */
  1136. entry_guards_update_filtered_sets(gs);
  1137. for (i = 0; i < NUM; ++i) {
  1138. tt_assert(g[i]->is_filtered_guard == (i == 5 || i == 6));
  1139. tt_assert(g[i]->is_usable_filtered_guard == (i == 6));
  1140. }
  1141. tt_int_op(num_reachable_filtered_guards(gs), OP_EQ, 1);
  1142. /* Now make sure we have no live consensus, and no nodes. Nothing should
  1143. * pass the filter any more. */
  1144. tor_free(dummy_consensus);
  1145. dummy_consensus = NULL;
  1146. SMARTLIST_FOREACH(big_fake_net_nodes, node_t *, node, {
  1147. memset(node->identity, 0xff, 20);
  1148. });
  1149. entry_guards_update_filtered_sets(gs);
  1150. for (i = 0; i < NUM; ++i) {
  1151. tt_assert(g[i]->is_filtered_guard == 0);
  1152. tt_assert(g[i]->is_usable_filtered_guard == 0);
  1153. }
  1154. tt_int_op(num_reachable_filtered_guards(gs), OP_EQ, 0);
  1155. done:
  1156. guard_selection_free(gs);
  1157. tor_free(bl);
  1158. }
  1159. static void
  1160. test_entry_guard_expand_sample(void *arg)
  1161. {
  1162. (void)arg;
  1163. guard_selection_t *gs = guard_selection_new("default", GS_TYPE_NORMAL);
  1164. digestmap_t *node_by_id = digestmap_new();
  1165. entry_guard_t *guard = entry_guards_expand_sample(gs);
  1166. tt_assert(guard); // the last guard returned.
  1167. // Every sampled guard here should be filtered and reachable for now.
  1168. tt_int_op(smartlist_len(gs->sampled_entry_guards), OP_EQ,
  1169. num_reachable_filtered_guards(gs));
  1170. /* Make sure we got the right number. */
  1171. tt_int_op(DFLT_MIN_FILTERED_SAMPLE_SIZE, OP_EQ,
  1172. num_reachable_filtered_guards(gs));
  1173. // Make sure everything we got was from our fake node list, and everything
  1174. // was unique.
  1175. SMARTLIST_FOREACH_BEGIN(gs->sampled_entry_guards, entry_guard_t *, g) {
  1176. const node_t *n = bfn_mock_node_get_by_id(g->identity);
  1177. tt_assert(n);
  1178. tt_ptr_op(NULL, OP_EQ, digestmap_get(node_by_id, g->identity));
  1179. digestmap_set(node_by_id, g->identity, (void*) n);
  1180. int idx = smartlist_pos(big_fake_net_nodes, n);
  1181. // The even ones are the guards; make sure we got guards.
  1182. tt_int_op(idx & 1, OP_EQ, 0);
  1183. } SMARTLIST_FOREACH_END(g);
  1184. // Nothing became unusable/unfiltered, so a subsequent expand should
  1185. // make no changes.
  1186. guard = entry_guards_expand_sample(gs);
  1187. tt_assert(! guard); // no guard was added.
  1188. tt_int_op(DFLT_MIN_FILTERED_SAMPLE_SIZE, OP_EQ,
  1189. num_reachable_filtered_guards(gs));
  1190. // Make a few guards unreachable.
  1191. guard = smartlist_get(gs->sampled_entry_guards, 0);
  1192. guard->is_usable_filtered_guard = 0;
  1193. guard = smartlist_get(gs->sampled_entry_guards, 1);
  1194. guard->is_usable_filtered_guard = 0;
  1195. guard = smartlist_get(gs->sampled_entry_guards, 2);
  1196. guard->is_usable_filtered_guard = 0;
  1197. tt_int_op(DFLT_MIN_FILTERED_SAMPLE_SIZE - 3, OP_EQ,
  1198. num_reachable_filtered_guards(gs));
  1199. // This time, expanding the sample will add some more guards.
  1200. guard = entry_guards_expand_sample(gs);
  1201. tt_assert(guard); // no guard was added.
  1202. tt_int_op(DFLT_MIN_FILTERED_SAMPLE_SIZE, OP_EQ,
  1203. num_reachable_filtered_guards(gs));
  1204. tt_int_op(smartlist_len(gs->sampled_entry_guards), OP_EQ,
  1205. num_reachable_filtered_guards(gs)+3);
  1206. // Still idempotent.
  1207. guard = entry_guards_expand_sample(gs);
  1208. tt_assert(! guard); // no guard was added.
  1209. tt_int_op(DFLT_MIN_FILTERED_SAMPLE_SIZE, OP_EQ,
  1210. num_reachable_filtered_guards(gs));
  1211. // Now, do a nasty trick: tell the filter to exclude 31/32 of the guards.
  1212. // This will cause the sample size to get reeeeally huge, while the
  1213. // filtered sample size grows only slowly.
  1214. routerset_free(get_options_mutable()->ExcludeNodes);
  1215. get_options_mutable()->ExcludeNodes = routerset_new();
  1216. routerset_parse(get_options_mutable()->ExcludeNodes, "144.144.0.0/16", "");
  1217. SMARTLIST_FOREACH(big_fake_net_nodes, node_t *, n, {
  1218. if (n_sl_idx % 64 != 0) {
  1219. n->rs->addr = 0x90903030;
  1220. }
  1221. });
  1222. entry_guards_update_filtered_sets(gs);
  1223. // Surely (p ~ 1-2**-60), one of our guards has been excluded.
  1224. tt_int_op(num_reachable_filtered_guards(gs), OP_LT,
  1225. DFLT_MIN_FILTERED_SAMPLE_SIZE);
  1226. // Try to regenerate the guards.
  1227. guard = entry_guards_expand_sample(gs);
  1228. tt_assert(guard); // no guard was added.
  1229. /* this time, it's possible that we didn't add enough sampled guards. */
  1230. tt_int_op(num_reachable_filtered_guards(gs), OP_LE,
  1231. DFLT_MIN_FILTERED_SAMPLE_SIZE);
  1232. /* but we definitely didn't exceed the sample maximum. */
  1233. tt_int_op(smartlist_len(gs->sampled_entry_guards), OP_LE,
  1234. (int)((271 / 2) * .3));
  1235. done:
  1236. guard_selection_free(gs);
  1237. digestmap_free(node_by_id, NULL);
  1238. }
  1239. static void
  1240. test_entry_guard_expand_sample_small_net(void *arg)
  1241. {
  1242. (void)arg;
  1243. guard_selection_t *gs = guard_selection_new("default", GS_TYPE_NORMAL);
  1244. /* Fun corner case: not enough guards to make up our whole sample size. */
  1245. SMARTLIST_FOREACH(big_fake_net_nodes, node_t *, n, {
  1246. if (n_sl_idx >= 40) {
  1247. tor_free(n->rs);
  1248. tor_free(n->md);
  1249. tor_free(n);
  1250. SMARTLIST_DEL_CURRENT(big_fake_net_nodes, n);
  1251. } else {
  1252. n->rs->addr = 0; // make the filter reject this.
  1253. }
  1254. });
  1255. entry_guard_t *guard = entry_guards_expand_sample(gs);
  1256. tt_assert(guard); // the last guard returned -- some guard was added.
  1257. tt_int_op(smartlist_len(gs->sampled_entry_guards), OP_GT, 0);
  1258. tt_int_op(smartlist_len(gs->sampled_entry_guards), OP_LT, 10);
  1259. tt_int_op(num_reachable_filtered_guards(gs), OP_EQ, 0);
  1260. done:
  1261. guard_selection_free(gs);
  1262. }
  1263. static void
  1264. test_entry_guard_update_from_consensus_status(void *arg)
  1265. {
  1266. /* Here we're going to have some nodes become un-guardy, and say we got a
  1267. * new consensus. This should cause those nodes to get detected as
  1268. * unreachable. */
  1269. (void)arg;
  1270. int i;
  1271. time_t start = approx_time();
  1272. guard_selection_t *gs = guard_selection_new("default", GS_TYPE_NORMAL);
  1273. networkstatus_t *ns_tmp = NULL;
  1274. /* Don't randomly backdate stuff; it will make correctness harder to check.*/
  1275. MOCK(randomize_time, mock_randomize_time_no_randomization);
  1276. /* First, sample some guards. */
  1277. entry_guards_expand_sample(gs);
  1278. int n_sampled_pre = smartlist_len(gs->sampled_entry_guards);
  1279. int n_filtered_pre = num_reachable_filtered_guards(gs);
  1280. tt_i64_op(n_sampled_pre, OP_EQ, n_filtered_pre);
  1281. tt_i64_op(n_sampled_pre, OP_GT, 10);
  1282. /* At this point, it should be a no-op to do this: */
  1283. sampled_guards_update_from_consensus(gs);
  1284. /* Now let's make some of our guards become unlisted. The easiest way to
  1285. * do that would be to take away their guard flag. */
  1286. for (i = 0; i < 5; ++i) {
  1287. entry_guard_t *g = smartlist_get(gs->sampled_entry_guards, i);
  1288. node_t *n = (node_t*) bfn_mock_node_get_by_id(g->identity);
  1289. n->is_possible_guard = 0;
  1290. }
  1291. update_approx_time(start + 30);
  1292. {
  1293. /* try this with no live networkstatus. Nothing should happen! */
  1294. ns_tmp = dummy_consensus;
  1295. dummy_consensus = NULL;
  1296. sampled_guards_update_from_consensus(gs);
  1297. tt_i64_op(smartlist_len(gs->sampled_entry_guards), OP_EQ, n_sampled_pre);
  1298. tt_i64_op(num_reachable_filtered_guards(gs), OP_EQ, n_filtered_pre);
  1299. /* put the networkstatus back. */
  1300. dummy_consensus = ns_tmp;
  1301. ns_tmp = NULL;
  1302. }
  1303. /* Now those guards should become unlisted, and drop off the filter, but
  1304. * stay in the sample. */
  1305. update_approx_time(start + 60);
  1306. sampled_guards_update_from_consensus(gs);
  1307. tt_i64_op(smartlist_len(gs->sampled_entry_guards), OP_EQ, n_sampled_pre);
  1308. tt_i64_op(num_reachable_filtered_guards(gs), OP_EQ, n_filtered_pre - 5);
  1309. for (i = 0; i < 5; ++i) {
  1310. entry_guard_t *g = smartlist_get(gs->sampled_entry_guards, i);
  1311. tt_assert(! g->currently_listed);
  1312. tt_i64_op(g->unlisted_since_date, OP_EQ, start+60);
  1313. }
  1314. for (i = 5; i < n_sampled_pre; ++i) {
  1315. entry_guard_t *g = smartlist_get(gs->sampled_entry_guards, i);
  1316. tt_assert(g->currently_listed);
  1317. tt_i64_op(g->unlisted_since_date, OP_EQ, 0);
  1318. }
  1319. /* Now re-list one, and remove one completely. */
  1320. {
  1321. entry_guard_t *g = smartlist_get(gs->sampled_entry_guards, 0);
  1322. node_t *n = (node_t*) bfn_mock_node_get_by_id(g->identity);
  1323. n->is_possible_guard = 1;
  1324. }
  1325. {
  1326. /* try removing the node, to make sure we don't crash on an absent node
  1327. */
  1328. entry_guard_t *g = smartlist_get(gs->sampled_entry_guards, 5);
  1329. node_t *n = (node_t*) bfn_mock_node_get_by_id(g->identity);
  1330. smartlist_remove(big_fake_net_nodes, n);
  1331. tor_free(n->rs);
  1332. tor_free(n->md);
  1333. tor_free(n);
  1334. }
  1335. update_approx_time(start + 300);
  1336. sampled_guards_update_from_consensus(gs);
  1337. /* guards 1..5 are now unlisted; 0,6,7.. are listed. */
  1338. tt_i64_op(smartlist_len(gs->sampled_entry_guards), OP_EQ, n_sampled_pre);
  1339. for (i = 1; i < 6; ++i) {
  1340. entry_guard_t *g = smartlist_get(gs->sampled_entry_guards, i);
  1341. tt_assert(! g->currently_listed);
  1342. if (i == 5)
  1343. tt_i64_op(g->unlisted_since_date, OP_EQ, start+300);
  1344. else
  1345. tt_i64_op(g->unlisted_since_date, OP_EQ, start+60);
  1346. }
  1347. for (i = 0; i < n_sampled_pre; i = (!i) ? 6 : i+1) { /* 0,6,7,8, ... */
  1348. entry_guard_t *g = smartlist_get(gs->sampled_entry_guards, i);
  1349. tt_assert(g->currently_listed);
  1350. tt_i64_op(g->unlisted_since_date, OP_EQ, 0);
  1351. }
  1352. done:
  1353. tor_free(ns_tmp); /* in case we couldn't put it back */
  1354. guard_selection_free(gs);
  1355. UNMOCK(randomize_time);
  1356. }
  1357. static void
  1358. test_entry_guard_update_from_consensus_repair(void *arg)
  1359. {
  1360. /* Here we'll make sure that our code to repair the unlisted-since
  1361. * times is correct. */
  1362. (void)arg;
  1363. int i;
  1364. time_t start = approx_time();
  1365. guard_selection_t *gs = guard_selection_new("default", GS_TYPE_NORMAL);
  1366. /* Don't randomly backdate stuff; it will make correctness harder to check.*/
  1367. MOCK(randomize_time, mock_randomize_time_no_randomization);
  1368. /* First, sample some guards. */
  1369. entry_guards_expand_sample(gs);
  1370. int n_sampled_pre = smartlist_len(gs->sampled_entry_guards);
  1371. int n_filtered_pre = num_reachable_filtered_guards(gs);
  1372. tt_i64_op(n_sampled_pre, OP_EQ, n_filtered_pre);
  1373. tt_i64_op(n_sampled_pre, OP_GT, 10);
  1374. /* Now corrupt the list a bit. Call some unlisted-since-never, and some
  1375. * listed-and-unlisted-since-a-time. */
  1376. update_approx_time(start + 300);
  1377. for (i = 0; i < 3; ++i) {
  1378. /* these will get a date. */
  1379. entry_guard_t *g = smartlist_get(gs->sampled_entry_guards, i);
  1380. node_t *n = (node_t*) bfn_mock_node_get_by_id(g->identity);
  1381. n->is_possible_guard = 0;
  1382. g->currently_listed = 0;
  1383. }
  1384. for (i = 3; i < 6; ++i) {
  1385. /* these will become listed. */
  1386. entry_guard_t *g = smartlist_get(gs->sampled_entry_guards, i);
  1387. g->unlisted_since_date = start+100;
  1388. }
  1389. setup_full_capture_of_logs(LOG_WARN);
  1390. sampled_guards_update_from_consensus(gs);
  1391. expect_log_msg_containing(
  1392. "was listed, but with unlisted_since_date set");
  1393. expect_log_msg_containing(
  1394. "was unlisted, but with unlisted_since_date unset");
  1395. teardown_capture_of_logs();
  1396. tt_int_op(smartlist_len(gs->sampled_entry_guards), OP_EQ, n_sampled_pre);
  1397. tt_int_op(num_reachable_filtered_guards(gs), OP_EQ, n_filtered_pre - 3);
  1398. for (i = 3; i < n_sampled_pre; ++i) {
  1399. /* these will become listed. */
  1400. entry_guard_t *g = smartlist_get(gs->sampled_entry_guards, i);
  1401. if (i < 3) {
  1402. tt_assert(! g->currently_listed);
  1403. tt_i64_op(g->unlisted_since_date, OP_EQ, start+300);
  1404. } else {
  1405. tt_assert(g->currently_listed);
  1406. tt_i64_op(g->unlisted_since_date, OP_EQ, 0);
  1407. }
  1408. }
  1409. done:
  1410. teardown_capture_of_logs();
  1411. guard_selection_free(gs);
  1412. UNMOCK(randomize_time);
  1413. }
  1414. static void
  1415. test_entry_guard_update_from_consensus_remove(void *arg)
  1416. {
  1417. /* Now let's check the logic responsible for removing guards from the
  1418. * sample entirely. */
  1419. (void)arg;
  1420. //int i;
  1421. guard_selection_t *gs = guard_selection_new("default", GS_TYPE_NORMAL);
  1422. smartlist_t *keep_ids = smartlist_new();
  1423. smartlist_t *remove_ids = smartlist_new();
  1424. /* Don't randomly backdate stuff; it will make correctness harder to check.*/
  1425. MOCK(randomize_time, mock_randomize_time_no_randomization);
  1426. /* First, sample some guards. */
  1427. entry_guards_expand_sample(gs);
  1428. int n_sampled_pre = smartlist_len(gs->sampled_entry_guards);
  1429. int n_filtered_pre = num_reachable_filtered_guards(gs);
  1430. tt_i64_op(n_sampled_pre, OP_EQ, n_filtered_pre);
  1431. tt_i64_op(n_sampled_pre, OP_GT, 10);
  1432. const time_t one_day_ago = approx_time() - 1*24*60*60;
  1433. const time_t one_year_ago = approx_time() - 365*24*60*60;
  1434. const time_t two_years_ago = approx_time() - 2*365*24*60*60;
  1435. /* 0: unlisted for a day. (keep this) */
  1436. {
  1437. entry_guard_t *g = smartlist_get(gs->sampled_entry_guards, 0);
  1438. node_t *n = (node_t*) bfn_mock_node_get_by_id(g->identity);
  1439. n->is_possible_guard = 0;
  1440. g->currently_listed = 0;
  1441. g->unlisted_since_date = one_day_ago;
  1442. smartlist_add(keep_ids, tor_memdup(g->identity, 20));
  1443. }
  1444. /* 1: unlisted for a year. (remove this) */
  1445. {
  1446. entry_guard_t *g = smartlist_get(gs->sampled_entry_guards, 1);
  1447. node_t *n = (node_t*) bfn_mock_node_get_by_id(g->identity);
  1448. n->is_possible_guard = 0;
  1449. g->currently_listed = 0;
  1450. g->unlisted_since_date = one_year_ago;
  1451. smartlist_add(remove_ids, tor_memdup(g->identity, 20));
  1452. }
  1453. /* 2: added a day ago, never confirmed. (keep this) */
  1454. {
  1455. entry_guard_t *g = smartlist_get(gs->sampled_entry_guards, 2);
  1456. g->sampled_on_date = one_day_ago;
  1457. smartlist_add(keep_ids, tor_memdup(g->identity, 20));
  1458. }
  1459. /* 3: added a year ago, never confirmed. (remove this) */
  1460. {
  1461. entry_guard_t *g = smartlist_get(gs->sampled_entry_guards, 3);
  1462. g->sampled_on_date = one_year_ago;
  1463. smartlist_add(remove_ids, tor_memdup(g->identity, 20));
  1464. }
  1465. /* 4: added two year ago, confirmed yesterday, primary. (keep this.) */
  1466. {
  1467. entry_guard_t *g = smartlist_get(gs->sampled_entry_guards, 4);
  1468. g->sampled_on_date = one_year_ago;
  1469. g->confirmed_on_date = one_day_ago;
  1470. g->confirmed_idx = 0;
  1471. g->is_primary = 1;
  1472. smartlist_add(gs->confirmed_entry_guards, g);
  1473. smartlist_add(gs->primary_entry_guards, g);
  1474. smartlist_add(keep_ids, tor_memdup(g->identity, 20));
  1475. }
  1476. /* 5: added two years ago, confirmed a year ago, primary. (remove this) */
  1477. {
  1478. entry_guard_t *g = smartlist_get(gs->sampled_entry_guards, 5);
  1479. g->sampled_on_date = two_years_ago;
  1480. g->confirmed_on_date = one_year_ago;
  1481. g->confirmed_idx = 1;
  1482. g->is_primary = 1;
  1483. smartlist_add(gs->confirmed_entry_guards, g);
  1484. smartlist_add(gs->primary_entry_guards, g);
  1485. smartlist_add(remove_ids, tor_memdup(g->identity, 20));
  1486. }
  1487. sampled_guards_update_from_consensus(gs);
  1488. /* Did we remove the right ones? */
  1489. SMARTLIST_FOREACH(keep_ids, uint8_t *, id, {
  1490. tt_assert(get_sampled_guard_with_id(gs, id) != NULL);
  1491. });
  1492. SMARTLIST_FOREACH(remove_ids, uint8_t *, id, {
  1493. tt_want(get_sampled_guard_with_id(gs, id) == NULL);
  1494. });
  1495. /* Did we remove the right number? */
  1496. tt_int_op(smartlist_len(gs->sampled_entry_guards), OP_EQ, n_sampled_pre - 3);
  1497. done:
  1498. guard_selection_free(gs);
  1499. UNMOCK(randomize_time);
  1500. SMARTLIST_FOREACH(keep_ids, char *, cp, tor_free(cp));
  1501. SMARTLIST_FOREACH(remove_ids, char *, cp, tor_free(cp));
  1502. smartlist_free(keep_ids);
  1503. smartlist_free(remove_ids);
  1504. }
  1505. static void
  1506. test_entry_guard_confirming_guards(void *arg)
  1507. {
  1508. (void)arg;
  1509. /* Now let's check the logic responsible for manipulating the list
  1510. * of confirmed guards */
  1511. guard_selection_t *gs = guard_selection_new("default", GS_TYPE_NORMAL);
  1512. MOCK(randomize_time, mock_randomize_time_no_randomization);
  1513. /* Create the sample. */
  1514. entry_guards_expand_sample(gs);
  1515. /* Confirm a few guards. */
  1516. time_t start = approx_time();
  1517. entry_guard_t *g1 = smartlist_get(gs->sampled_entry_guards, 0);
  1518. entry_guard_t *g2 = smartlist_get(gs->sampled_entry_guards, 1);
  1519. entry_guard_t *g3 = smartlist_get(gs->sampled_entry_guards, 8);
  1520. make_guard_confirmed(gs, g2);
  1521. update_approx_time(start + 10);
  1522. make_guard_confirmed(gs, g1);
  1523. make_guard_confirmed(gs, g3);
  1524. /* Were the correct dates and indices fed in? */
  1525. tt_int_op(g1->confirmed_idx, OP_EQ, 1);
  1526. tt_int_op(g2->confirmed_idx, OP_EQ, 0);
  1527. tt_int_op(g3->confirmed_idx, OP_EQ, 2);
  1528. tt_i64_op(g1->confirmed_on_date, OP_EQ, start+10);
  1529. tt_i64_op(g2->confirmed_on_date, OP_EQ, start);
  1530. tt_i64_op(g3->confirmed_on_date, OP_EQ, start+10);
  1531. tt_ptr_op(smartlist_get(gs->confirmed_entry_guards, 0), OP_EQ, g2);
  1532. tt_ptr_op(smartlist_get(gs->confirmed_entry_guards, 1), OP_EQ, g1);
  1533. tt_ptr_op(smartlist_get(gs->confirmed_entry_guards, 2), OP_EQ, g3);
  1534. /* Now make sure we can regenerate the confirmed_entry_guards list. */
  1535. smartlist_clear(gs->confirmed_entry_guards);
  1536. g2->confirmed_idx = 0;
  1537. g1->confirmed_idx = 10;
  1538. g3->confirmed_idx = 100;
  1539. entry_guards_update_confirmed(gs);
  1540. tt_int_op(g1->confirmed_idx, OP_EQ, 1);
  1541. tt_int_op(g2->confirmed_idx, OP_EQ, 0);
  1542. tt_int_op(g3->confirmed_idx, OP_EQ, 2);
  1543. tt_ptr_op(smartlist_get(gs->confirmed_entry_guards, 0), OP_EQ, g2);
  1544. tt_ptr_op(smartlist_get(gs->confirmed_entry_guards, 1), OP_EQ, g1);
  1545. tt_ptr_op(smartlist_get(gs->confirmed_entry_guards, 2), OP_EQ, g3);
  1546. /* Now make sure we can regenerate the confirmed_entry_guards list if
  1547. * the indices are messed up. */
  1548. g1->confirmed_idx = g2->confirmed_idx = g3->confirmed_idx = 999;
  1549. smartlist_clear(gs->confirmed_entry_guards);
  1550. entry_guards_update_confirmed(gs);
  1551. tt_int_op(g1->confirmed_idx, OP_GE, 0);
  1552. tt_int_op(g2->confirmed_idx, OP_GE, 0);
  1553. tt_int_op(g3->confirmed_idx, OP_GE, 0);
  1554. tt_int_op(g1->confirmed_idx, OP_LE, 2);
  1555. tt_int_op(g2->confirmed_idx, OP_LE, 2);
  1556. tt_int_op(g3->confirmed_idx, OP_LE, 2);
  1557. g1 = smartlist_get(gs->confirmed_entry_guards, 0);
  1558. g2 = smartlist_get(gs->confirmed_entry_guards, 1);
  1559. g3 = smartlist_get(gs->confirmed_entry_guards, 2);
  1560. tt_int_op(g1->confirmed_idx, OP_EQ, 0);
  1561. tt_int_op(g2->confirmed_idx, OP_EQ, 1);
  1562. tt_int_op(g3->confirmed_idx, OP_EQ, 2);
  1563. tt_assert(g1 != g2);
  1564. tt_assert(g1 != g3);
  1565. tt_assert(g2 != g3);
  1566. done:
  1567. UNMOCK(randomize_time);
  1568. guard_selection_free(gs);
  1569. }
  1570. static void
  1571. test_entry_guard_sample_reachable_filtered(void *arg)
  1572. {
  1573. (void)arg;
  1574. guard_selection_t *gs = guard_selection_new("default", GS_TYPE_NORMAL);
  1575. entry_guards_expand_sample(gs);
  1576. const int N = 10000;
  1577. bitarray_t *selected = NULL;
  1578. int i, j;
  1579. /* We've got a sampled list now; let's make one non-usable-filtered; some
  1580. * confirmed, some primary, some pending.
  1581. */
  1582. int n_guards = smartlist_len(gs->sampled_entry_guards);
  1583. tt_int_op(n_guards, OP_GT, 10);
  1584. entry_guard_t *g;
  1585. g = smartlist_get(gs->sampled_entry_guards, 0);
  1586. g->is_pending = 1;
  1587. g = smartlist_get(gs->sampled_entry_guards, 1);
  1588. make_guard_confirmed(gs, g);
  1589. g = smartlist_get(gs->sampled_entry_guards, 2);
  1590. g->is_primary = 1;
  1591. g = smartlist_get(gs->sampled_entry_guards, 3);
  1592. g->pb.path_bias_disabled = 1;
  1593. entry_guards_update_filtered_sets(gs);
  1594. gs->primary_guards_up_to_date = 1;
  1595. tt_int_op(num_reachable_filtered_guards(gs), OP_EQ, n_guards - 1);
  1596. tt_int_op(smartlist_len(gs->sampled_entry_guards), OP_EQ, n_guards);
  1597. // +1 since the one we made disabled will make another one get added.
  1598. ++n_guards;
  1599. /* Try a bunch of selections. */
  1600. const struct {
  1601. int flag; int idx;
  1602. } tests[] = {
  1603. { 0, -1 },
  1604. { SAMPLE_EXCLUDE_CONFIRMED, 1 },
  1605. { SAMPLE_EXCLUDE_PRIMARY|SAMPLE_NO_UPDATE_PRIMARY, 2 },
  1606. { SAMPLE_EXCLUDE_PENDING, 0 },
  1607. { -1, -1},
  1608. };
  1609. for (j = 0; tests[j].flag >= 0; ++j) {
  1610. selected = bitarray_init_zero(n_guards);
  1611. const int excluded_flags = tests[j].flag;
  1612. const int excluded_idx = tests[j].idx;
  1613. for (i = 0; i < N; ++i) {
  1614. g = sample_reachable_filtered_entry_guards(gs, excluded_flags);
  1615. tor_assert(g);
  1616. int pos = smartlist_pos(gs->sampled_entry_guards, g);
  1617. tt_int_op(smartlist_len(gs->sampled_entry_guards), OP_EQ, n_guards);
  1618. tt_int_op(pos, OP_GE, 0);
  1619. tt_int_op(pos, OP_LT, n_guards);
  1620. bitarray_set(selected, pos);
  1621. }
  1622. for (i = 0; i < n_guards; ++i) {
  1623. const int should_be_set = (i != excluded_idx &&
  1624. i != 3); // filtered out.
  1625. tt_int_op(!!bitarray_is_set(selected, i), OP_EQ, should_be_set);
  1626. }
  1627. bitarray_free(selected);
  1628. selected = NULL;
  1629. }
  1630. done:
  1631. guard_selection_free(gs);
  1632. bitarray_free(selected);
  1633. }
  1634. static void
  1635. test_entry_guard_sample_reachable_filtered_empty(void *arg)
  1636. {
  1637. (void)arg;
  1638. guard_selection_t *gs = guard_selection_new("default", GS_TYPE_NORMAL);
  1639. /* What if we try to sample from a set of 0? */
  1640. SMARTLIST_FOREACH(big_fake_net_nodes, node_t *, n,
  1641. n->is_possible_guard = 0);
  1642. entry_guard_t *g = sample_reachable_filtered_entry_guards(gs, 0);
  1643. tt_ptr_op(g, OP_EQ, NULL);
  1644. done:
  1645. guard_selection_free(gs);
  1646. }
  1647. static void
  1648. test_entry_guard_retry_unreachable(void *arg)
  1649. {
  1650. (void)arg;
  1651. guard_selection_t *gs = guard_selection_new("default", GS_TYPE_NORMAL);
  1652. entry_guards_expand_sample(gs);
  1653. /* Let's say that we have two guards, and they're down.
  1654. */
  1655. time_t start = approx_time();;
  1656. entry_guard_t *g1 = smartlist_get(gs->sampled_entry_guards, 0);
  1657. entry_guard_t *g2 = smartlist_get(gs->sampled_entry_guards, 1);
  1658. entry_guard_t *g3 = smartlist_get(gs->sampled_entry_guards, 2);
  1659. g1->is_reachable = GUARD_REACHABLE_NO;
  1660. g2->is_reachable = GUARD_REACHABLE_NO;
  1661. g1->is_primary = 1;
  1662. g1->failing_since = g2->failing_since = start;
  1663. g1->last_tried_to_connect = g2->last_tried_to_connect = start;
  1664. /* Wait 5 minutes. Nothing will get retried. */
  1665. update_approx_time(start + 5 * 60);
  1666. entry_guard_consider_retry(g1);
  1667. entry_guard_consider_retry(g2);
  1668. entry_guard_consider_retry(g3); // just to make sure this doesn't crash.
  1669. tt_int_op(g1->is_reachable, OP_EQ, GUARD_REACHABLE_NO);
  1670. tt_int_op(g2->is_reachable, OP_EQ, GUARD_REACHABLE_NO);
  1671. tt_int_op(g3->is_reachable, OP_EQ, GUARD_REACHABLE_MAYBE);
  1672. /* After 30 min, the primary one gets retried */
  1673. update_approx_time(start + 35 * 60);
  1674. entry_guard_consider_retry(g1);
  1675. entry_guard_consider_retry(g2);
  1676. tt_int_op(g1->is_reachable, OP_EQ, GUARD_REACHABLE_MAYBE);
  1677. tt_int_op(g2->is_reachable, OP_EQ, GUARD_REACHABLE_NO);
  1678. g1->is_reachable = GUARD_REACHABLE_NO;
  1679. g1->last_tried_to_connect = start + 35*60;
  1680. /* After 1 hour, we'll retry the nonprimary one. */
  1681. update_approx_time(start + 61 * 60);
  1682. entry_guard_consider_retry(g1);
  1683. entry_guard_consider_retry(g2);
  1684. tt_int_op(g1->is_reachable, OP_EQ, GUARD_REACHABLE_NO);
  1685. tt_int_op(g2->is_reachable, OP_EQ, GUARD_REACHABLE_MAYBE);
  1686. g2->is_reachable = GUARD_REACHABLE_NO;
  1687. g2->last_tried_to_connect = start + 61*60;
  1688. /* And then the primary one again. */
  1689. update_approx_time(start + 66 * 60);
  1690. entry_guard_consider_retry(g1);
  1691. entry_guard_consider_retry(g2);
  1692. tt_int_op(g1->is_reachable, OP_EQ, GUARD_REACHABLE_MAYBE);
  1693. tt_int_op(g2->is_reachable, OP_EQ, GUARD_REACHABLE_NO);
  1694. done:
  1695. guard_selection_free(gs);
  1696. }
  1697. static void
  1698. test_entry_guard_manage_primary(void *arg)
  1699. {
  1700. (void)arg;
  1701. guard_selection_t *gs = guard_selection_new("default", GS_TYPE_NORMAL);
  1702. smartlist_t *prev_guards = smartlist_new();
  1703. /* If no guards are confirmed, we should pick a few reachable guards and
  1704. * call them all primary. But not confirmed.*/
  1705. entry_guards_update_primary(gs);
  1706. int n_primary = smartlist_len(gs->primary_entry_guards);
  1707. tt_int_op(n_primary, OP_GE, 1);
  1708. SMARTLIST_FOREACH(gs->primary_entry_guards, entry_guard_t *, g, {
  1709. tt_assert(g->is_primary);
  1710. tt_assert(g->confirmed_idx == -1);
  1711. });
  1712. /* Calling it a second time should leave the guards unchanged. */
  1713. smartlist_add_all(prev_guards, gs->primary_entry_guards);
  1714. entry_guards_update_primary(gs);
  1715. tt_int_op(smartlist_len(gs->primary_entry_guards), OP_EQ, n_primary);
  1716. SMARTLIST_FOREACH(gs->primary_entry_guards, entry_guard_t *, g, {
  1717. tt_ptr_op(g, OP_EQ, smartlist_get(prev_guards, g_sl_idx));
  1718. });
  1719. /* If we have one confirmed guard, that guards becomes the first primary
  1720. * guard, and the other primary guards get kept. */
  1721. /* find a non-primary guard... */
  1722. entry_guard_t *confirmed = NULL;
  1723. SMARTLIST_FOREACH(gs->sampled_entry_guards, entry_guard_t *, g, {
  1724. if (! g->is_primary) {
  1725. confirmed = g;
  1726. break;
  1727. }
  1728. });
  1729. tt_assert(confirmed);
  1730. /* make it confirmed. */
  1731. make_guard_confirmed(gs, confirmed);
  1732. /* update the list... */
  1733. smartlist_clear(prev_guards);
  1734. smartlist_add_all(prev_guards, gs->primary_entry_guards);
  1735. entry_guards_update_primary(gs);
  1736. /* and see what's primary now! */
  1737. tt_int_op(smartlist_len(gs->primary_entry_guards), OP_EQ, n_primary);
  1738. tt_ptr_op(smartlist_get(gs->primary_entry_guards, 0), OP_EQ, confirmed);
  1739. SMARTLIST_FOREACH(gs->primary_entry_guards, entry_guard_t *, g, {
  1740. tt_assert(g->is_primary);
  1741. if (g_sl_idx == 0)
  1742. continue;
  1743. tt_ptr_op(g, OP_EQ, smartlist_get(prev_guards, g_sl_idx - 1));
  1744. });
  1745. {
  1746. entry_guard_t *prev_last_guard = smartlist_get(prev_guards, n_primary-1);
  1747. tt_assert(! prev_last_guard->is_primary);
  1748. }
  1749. /* Calling it a fourth time should leave the guards unchanged. */
  1750. smartlist_clear(prev_guards);
  1751. smartlist_add_all(prev_guards, gs->primary_entry_guards);
  1752. entry_guards_update_primary(gs);
  1753. tt_int_op(smartlist_len(gs->primary_entry_guards), OP_EQ, n_primary);
  1754. SMARTLIST_FOREACH(gs->primary_entry_guards, entry_guard_t *, g, {
  1755. tt_ptr_op(g, OP_EQ, smartlist_get(prev_guards, g_sl_idx));
  1756. });
  1757. done:
  1758. guard_selection_free(gs);
  1759. smartlist_free(prev_guards);
  1760. }
  1761. static void
  1762. test_entry_guard_guard_preferred(void *arg)
  1763. {
  1764. (void) arg;
  1765. entry_guard_t *g1 = tor_malloc_zero(sizeof(entry_guard_t));
  1766. entry_guard_t *g2 = tor_malloc_zero(sizeof(entry_guard_t));
  1767. g1->confirmed_idx = g2->confirmed_idx = -1;
  1768. g1->last_tried_to_connect = approx_time();
  1769. g2->last_tried_to_connect = approx_time();
  1770. tt_int_op(0, OP_EQ, entry_guard_has_higher_priority(g1, g1));
  1771. /* Neither is pending; priorities equal. */
  1772. tt_int_op(0, OP_EQ, entry_guard_has_higher_priority(g2, g1));
  1773. tt_int_op(0, OP_EQ, entry_guard_has_higher_priority(g1, g2));
  1774. /* If one is pending, the pending one has higher priority */
  1775. g1->is_pending = 1;
  1776. tt_int_op(1, OP_EQ, entry_guard_has_higher_priority(g1, g2));
  1777. tt_int_op(0, OP_EQ, entry_guard_has_higher_priority(g2, g1));
  1778. /* If both are pending, and last_tried_to_connect is equal:
  1779. priorities equal */
  1780. g2->is_pending = 1;
  1781. tt_int_op(0, OP_EQ, entry_guard_has_higher_priority(g2, g1));
  1782. tt_int_op(0, OP_EQ, entry_guard_has_higher_priority(g1, g2));
  1783. /* One had a connection that startied earlier: it has higher priority. */
  1784. g2->last_tried_to_connect -= 10;
  1785. tt_int_op(1, OP_EQ, entry_guard_has_higher_priority(g2, g1));
  1786. tt_int_op(0, OP_EQ, entry_guard_has_higher_priority(g1, g2));
  1787. /* Now, say that g1 is confirmed. It will get higher priority. */
  1788. g1->confirmed_idx = 5;
  1789. tt_int_op(0, OP_EQ, entry_guard_has_higher_priority(g2, g1));
  1790. tt_int_op(1, OP_EQ, entry_guard_has_higher_priority(g1, g2));
  1791. /* But if g2 was confirmed first, it will get priority */
  1792. g2->confirmed_idx = 2;
  1793. tt_int_op(1, OP_EQ, entry_guard_has_higher_priority(g2, g1));
  1794. tt_int_op(0, OP_EQ, entry_guard_has_higher_priority(g1, g2));
  1795. done:
  1796. tor_free(g1);
  1797. tor_free(g2);
  1798. }
  1799. static void
  1800. test_entry_guard_select_for_circuit_no_confirmed(void *arg)
  1801. {
  1802. /* Simpler cases: no gaurds are confirmed yet. */
  1803. (void)arg;
  1804. guard_selection_t *gs = guard_selection_new("default", GS_TYPE_NORMAL);
  1805. /* simple starting configuration */
  1806. entry_guards_update_primary(gs);
  1807. unsigned state = 9999;
  1808. entry_guard_t *g = select_entry_guard_for_circuit(gs, &state);
  1809. tt_assert(g);
  1810. tt_assert(g->is_primary);
  1811. tt_int_op(g->confirmed_idx, OP_EQ, -1);
  1812. tt_assert(g->is_pending == 0); // primary implies non-pending.
  1813. tt_uint_op(state, OP_EQ, GUARD_CIRC_STATE_USABLE_ON_COMPLETION);
  1814. tt_i64_op(g->last_tried_to_connect, OP_EQ, approx_time());
  1815. // If we do that again, we should get the same guard.
  1816. entry_guard_t *g2 = select_entry_guard_for_circuit(gs, &state);
  1817. tt_ptr_op(g2, OP_EQ, g);
  1818. // if we mark that guard down, we should get a different primary guard.
  1819. // auto-retry it.
  1820. g->is_reachable = GUARD_REACHABLE_NO;
  1821. g->unreachable_since = approx_time() - 10;
  1822. g->last_tried_to_connect = approx_time() - 10;
  1823. state = 9999;
  1824. g2 = select_entry_guard_for_circuit(gs, &state);
  1825. tt_ptr_op(g2, OP_NE, g);
  1826. tt_assert(g2);
  1827. tt_assert(g2->is_primary);
  1828. tt_int_op(g2->confirmed_idx, OP_EQ, -1);
  1829. tt_assert(g2->is_pending == 0); // primary implies non-pending.
  1830. tt_uint_op(state, OP_EQ, GUARD_CIRC_STATE_USABLE_ON_COMPLETION);
  1831. tt_i64_op(g2->last_tried_to_connect, OP_EQ, approx_time());
  1832. // If we say that the first primary guard was last tried a long time ago, we
  1833. // should get an automatic retry on it.
  1834. g->unreachable_since = approx_time() - 72*60*60;
  1835. g->last_tried_to_connect = approx_time() - 72*60*60;
  1836. state = 9999;
  1837. g2 = select_entry_guard_for_circuit(gs, &state);
  1838. tt_ptr_op(g2, OP_EQ, g);
  1839. tt_assert(g2);
  1840. tt_uint_op(state, OP_EQ, GUARD_CIRC_STATE_USABLE_ON_COMPLETION);
  1841. tt_i64_op(g2->last_tried_to_connect, OP_EQ, approx_time());
  1842. tt_int_op(g2->is_reachable, OP_EQ, GUARD_REACHABLE_MAYBE);
  1843. // And if we mark ALL the primary guards down, we should get another guard
  1844. // at random.
  1845. SMARTLIST_FOREACH(gs->primary_entry_guards, entry_guard_t *, guard, {
  1846. guard->is_reachable = GUARD_REACHABLE_NO;
  1847. guard->last_tried_to_connect = approx_time() - 5;
  1848. guard->unreachable_since = approx_time() - 30;
  1849. });
  1850. state = 9999;
  1851. g2 = select_entry_guard_for_circuit(gs, &state);
  1852. tt_assert(g2);
  1853. tt_assert(!g2->is_primary);
  1854. tt_int_op(g2->confirmed_idx, OP_EQ, -1);
  1855. tt_assert(g2->is_pending == 1);
  1856. tt_uint_op(state, OP_EQ, GUARD_CIRC_STATE_USABLE_IF_NO_BETTER_GUARD);
  1857. tt_i64_op(g2->last_tried_to_connect, OP_EQ, approx_time());
  1858. tt_int_op(g2->is_reachable, OP_EQ, GUARD_REACHABLE_MAYBE);
  1859. // As a bonus, maybe we should be retrying the primary guards. Let's say so.
  1860. mark_primary_guards_maybe_reachable(gs);
  1861. SMARTLIST_FOREACH(gs->primary_entry_guards, entry_guard_t *, guard, {
  1862. tt_int_op(guard->is_reachable, OP_EQ, GUARD_REACHABLE_MAYBE);
  1863. tt_assert(guard->is_usable_filtered_guard == 1);
  1864. // no change to these fields.
  1865. tt_i64_op(guard->last_tried_to_connect, OP_EQ, approx_time() - 5);
  1866. tt_i64_op(guard->unreachable_since, OP_EQ, approx_time() - 30);
  1867. });
  1868. done:
  1869. guard_selection_free(gs);
  1870. }
  1871. static void
  1872. test_entry_guard_select_for_circuit_confirmed(void *arg)
  1873. {
  1874. /* Case 2: if all the primary guards are down, and there are more confirmed
  1875. guards, we use a confirmed guard. */
  1876. (void)arg;
  1877. int i;
  1878. guard_selection_t *gs = guard_selection_new("default", GS_TYPE_NORMAL);
  1879. const int N_CONFIRMED = 10;
  1880. /* slightly more complicated simple starting configuration */
  1881. entry_guards_update_primary(gs);
  1882. for (i = 0; i < N_CONFIRMED; ++i) {
  1883. entry_guard_t *guard = smartlist_get(gs->sampled_entry_guards, i);
  1884. make_guard_confirmed(gs, guard);
  1885. }
  1886. entry_guards_update_primary(gs); // rebuild the primary list.
  1887. unsigned state = 9999;
  1888. // As above, this gives us a primary guard.
  1889. entry_guard_t *g = select_entry_guard_for_circuit(gs, &state);
  1890. tt_assert(g);
  1891. tt_assert(g->is_primary);
  1892. tt_int_op(g->confirmed_idx, OP_EQ, 0);
  1893. tt_assert(g->is_pending == 0); // primary implies non-pending.
  1894. tt_uint_op(state, OP_EQ, GUARD_CIRC_STATE_USABLE_ON_COMPLETION);
  1895. tt_i64_op(g->last_tried_to_connect, OP_EQ, approx_time());
  1896. tt_ptr_op(g, OP_EQ, smartlist_get(gs->primary_entry_guards, 0));
  1897. // But if we mark all the primary guards down...
  1898. SMARTLIST_FOREACH(gs->primary_entry_guards, entry_guard_t *, guard, {
  1899. guard->last_tried_to_connect = approx_time();
  1900. entry_guards_note_guard_failure(gs, guard);
  1901. });
  1902. // ... we should get a confirmed guard.
  1903. state = 9999;
  1904. g = select_entry_guard_for_circuit(gs, &state);
  1905. tt_assert(g);
  1906. tt_assert(! g->is_primary);
  1907. tt_int_op(g->confirmed_idx, OP_EQ, smartlist_len(gs->primary_entry_guards));
  1908. tt_assert(g->is_pending);
  1909. tt_uint_op(state, OP_EQ, GUARD_CIRC_STATE_USABLE_IF_NO_BETTER_GUARD);
  1910. tt_i64_op(g->last_tried_to_connect, OP_EQ, approx_time());
  1911. // And if we try again, we should get a different confirmed guard, since
  1912. // that one is pending.
  1913. state = 9999;
  1914. entry_guard_t *g2 = select_entry_guard_for_circuit(gs, &state);
  1915. tt_assert(g2);
  1916. tt_assert(! g2->is_primary);
  1917. tt_ptr_op(g2, OP_NE, g);
  1918. tt_int_op(g2->confirmed_idx, OP_EQ,
  1919. smartlist_len(gs->primary_entry_guards)+1);
  1920. tt_assert(g2->is_pending);
  1921. tt_uint_op(state, OP_EQ, GUARD_CIRC_STATE_USABLE_IF_NO_BETTER_GUARD);
  1922. tt_i64_op(g2->last_tried_to_connect, OP_EQ, approx_time());
  1923. // If we make every confirmed guard become pending then we start poking
  1924. // other guards.
  1925. const int n_remaining_confirmed =
  1926. N_CONFIRMED - 2 - smartlist_len(gs->primary_entry_guards);
  1927. for (i = 0; i < n_remaining_confirmed; ++i) {
  1928. g = select_entry_guard_for_circuit(gs, &state);
  1929. tt_int_op(g->confirmed_idx, OP_GE, 0);
  1930. tt_assert(g);
  1931. }
  1932. state = 9999;
  1933. g = select_entry_guard_for_circuit(gs, &state);
  1934. tt_assert(g);
  1935. tt_assert(g->is_pending);
  1936. tt_int_op(g->confirmed_idx, OP_EQ, -1);
  1937. done:
  1938. guard_selection_free(gs);
  1939. }
  1940. static void
  1941. test_entry_guard_select_for_circuit_highlevel_primary(void *arg)
  1942. {
  1943. /* Play around with selecting primary guards for circuits and markign
  1944. * them up and down */
  1945. (void)arg;
  1946. guard_selection_t *gs = guard_selection_new("default", GS_TYPE_NORMAL);
  1947. time_t start = approx_time();
  1948. const node_t *node = NULL;
  1949. circuit_guard_state_t *guard = NULL;
  1950. entry_guard_t *g;
  1951. /*
  1952. * Make sure that the pick-for-circuit API basically works. We'll get
  1953. * a primary guard, so it'll be usable on completion.
  1954. */
  1955. int r = entry_guard_pick_for_circuit(gs, &node, &guard);
  1956. tt_assert(r == 0);
  1957. tt_assert(node);
  1958. tt_assert(guard);
  1959. tt_int_op(guard->state, OP_EQ, GUARD_CIRC_STATE_USABLE_ON_COMPLETION);
  1960. g = entry_guard_handle_get(guard->guard);
  1961. tt_assert(g);
  1962. tt_mem_op(g->identity, OP_EQ, node->identity, DIGEST_LEN);
  1963. tt_int_op(g->is_primary, OP_EQ, 1);
  1964. tt_i64_op(g->last_tried_to_connect, OP_EQ, start);
  1965. tt_int_op(g->confirmed_idx, OP_EQ, -1);
  1966. /* Call that circuit successful. */
  1967. update_approx_time(start+15);
  1968. r = entry_guard_succeeded(&guard);
  1969. tt_int_op(r, OP_EQ, 1); /* We can use it now. */
  1970. tt_assert(guard);
  1971. tt_int_op(guard->state, OP_EQ, GUARD_CIRC_STATE_COMPLETE);
  1972. g = entry_guard_handle_get(guard->guard);
  1973. tt_assert(g);
  1974. tt_int_op(g->is_reachable, OP_EQ, GUARD_REACHABLE_YES);
  1975. tt_int_op(g->confirmed_idx, OP_EQ, 0);
  1976. circuit_guard_state_free(guard);
  1977. guard = NULL;
  1978. node = NULL;
  1979. g = NULL;
  1980. /* Try again. We'll also get a primary guard this time. (The same one,
  1981. in fact.) But this time, we'll say the connection has failed. */
  1982. update_approx_time(start+35);
  1983. r = entry_guard_pick_for_circuit(gs, &node, &guard);
  1984. tt_assert(r == 0);
  1985. tt_assert(node);
  1986. tt_assert(guard);
  1987. tt_int_op(guard->state, OP_EQ, GUARD_CIRC_STATE_USABLE_ON_COMPLETION);
  1988. tt_i64_op(guard->state_set_at, OP_EQ, start+35);
  1989. g = entry_guard_handle_get(guard->guard);
  1990. tt_assert(g);
  1991. tt_mem_op(g->identity, OP_EQ, node->identity, DIGEST_LEN);
  1992. tt_int_op(g->is_primary, OP_EQ, 1);
  1993. tt_i64_op(g->last_tried_to_connect, OP_EQ, start+35);
  1994. tt_int_op(g->confirmed_idx, OP_EQ, 0); // same one.
  1995. /* It's failed! What will happen to our poor guard? */
  1996. update_approx_time(start+45);
  1997. entry_guard_failed(&guard);
  1998. tt_assert(guard);
  1999. tt_int_op(guard->state, OP_EQ, GUARD_CIRC_STATE_DEAD);
  2000. tt_i64_op(guard->state_set_at, OP_EQ, start+45);
  2001. g = entry_guard_handle_get(guard->guard);
  2002. tt_assert(g);
  2003. tt_int_op(g->is_reachable, OP_EQ, GUARD_REACHABLE_NO);
  2004. tt_i64_op(g->failing_since, OP_EQ, start+45);
  2005. tt_int_op(g->confirmed_idx, OP_EQ, 0); // still confirmed.
  2006. circuit_guard_state_free(guard);
  2007. guard = NULL;
  2008. node = NULL;
  2009. entry_guard_t *g_prev = g;
  2010. g = NULL;
  2011. /* Now try a third time. Since the other one is down, we'll get a different
  2012. * (still primary) guard.
  2013. */
  2014. update_approx_time(start+60);
  2015. r = entry_guard_pick_for_circuit(gs, &node, &guard);
  2016. tt_assert(r == 0);
  2017. tt_assert(node);
  2018. tt_assert(guard);
  2019. tt_int_op(guard->state, OP_EQ, GUARD_CIRC_STATE_USABLE_ON_COMPLETION);
  2020. g = entry_guard_handle_get(guard->guard);
  2021. tt_assert(g);
  2022. tt_ptr_op(g, OP_NE, g_prev);
  2023. tt_mem_op(g->identity, OP_EQ, node->identity, DIGEST_LEN);
  2024. tt_mem_op(g->identity, OP_NE, g_prev->identity, DIGEST_LEN);
  2025. tt_int_op(g->is_primary, OP_EQ, 1);
  2026. tt_i64_op(g->last_tried_to_connect, OP_EQ, start+60);
  2027. tt_int_op(g->confirmed_idx, OP_EQ, -1); // not confirmd now.
  2028. /* Call this one up; watch it get confirmed. */
  2029. update_approx_time(start+90);
  2030. r = entry_guard_succeeded(&guard);
  2031. tt_int_op(r, OP_EQ, 1); /* We can use it now. */
  2032. tt_assert(guard);
  2033. tt_int_op(guard->state, OP_EQ, GUARD_CIRC_STATE_COMPLETE);
  2034. g = entry_guard_handle_get(guard->guard);
  2035. tt_assert(g);
  2036. tt_int_op(g->is_reachable, OP_EQ, GUARD_REACHABLE_YES);
  2037. tt_int_op(g->confirmed_idx, OP_EQ, 1);
  2038. done:
  2039. guard_selection_free(gs);
  2040. circuit_guard_state_free(guard);
  2041. }
  2042. static void
  2043. test_entry_guard_select_for_circuit_highlevel_confirm_other(void *arg)
  2044. {
  2045. (void) arg;
  2046. const int N_PRIMARY = DFLT_N_PRIMARY_GUARDS;
  2047. /* At the start, we have no confirmed guards. We'll mark the primary guards
  2048. * down, then confirm something else. As soon as we do, it should become
  2049. * primary, and we should get it next time. */
  2050. time_t start = approx_time();
  2051. guard_selection_t *gs = guard_selection_new("default", GS_TYPE_NORMAL);
  2052. circuit_guard_state_t *guard = NULL;
  2053. int i, r;
  2054. const node_t *node = NULL;
  2055. /* Declare that we're on the internet. */
  2056. entry_guards_note_internet_connectivity(gs);
  2057. /* Primary guards are down! */
  2058. for (i = 0; i < N_PRIMARY; ++i) {
  2059. r = entry_guard_pick_for_circuit(gs, &node, &guard);
  2060. tt_assert(node);
  2061. tt_assert(guard);
  2062. tt_assert(r == 0);
  2063. tt_int_op(guard->state, OP_EQ, GUARD_CIRC_STATE_USABLE_ON_COMPLETION);
  2064. entry_guard_failed(&guard);
  2065. circuit_guard_state_free(guard);
  2066. guard = NULL;
  2067. node = NULL;
  2068. }
  2069. /* Next guard should be non-primary. */
  2070. node = NULL;
  2071. r = entry_guard_pick_for_circuit(gs, &node, &guard);
  2072. tt_assert(node);
  2073. tt_assert(guard);
  2074. tt_assert(r == 0);
  2075. entry_guard_t *g = entry_guard_handle_get(guard->guard);
  2076. tt_assert(g);
  2077. tt_int_op(guard->state, OP_EQ, GUARD_CIRC_STATE_USABLE_IF_NO_BETTER_GUARD);
  2078. tt_int_op(g->confirmed_idx, OP_EQ, -1);
  2079. tt_int_op(g->is_primary, OP_EQ, 0);
  2080. tt_int_op(g->is_pending, OP_EQ, 1);
  2081. (void)start;
  2082. r = entry_guard_succeeded(&guard);
  2083. /* We're on the internet (by fiat), so this guard will get called "confirmed"
  2084. * and should immediately become primary.
  2085. * XXXX prop271 -- I don't like that behavior, but it's what is specified
  2086. */
  2087. tt_int_op(guard->state, OP_EQ, GUARD_CIRC_STATE_COMPLETE);
  2088. tt_assert(r == 1);
  2089. tt_int_op(g->confirmed_idx, OP_EQ, 0);
  2090. tt_int_op(g->is_primary, OP_EQ, 1);
  2091. tt_int_op(g->is_pending, OP_EQ, 0);
  2092. done:
  2093. guard_selection_free(gs);
  2094. circuit_guard_state_free(guard);
  2095. }
  2096. static void
  2097. test_entry_guard_select_for_circuit_highlevel_primary_retry(void *arg)
  2098. {
  2099. (void) arg;
  2100. const int N_PRIMARY = DFLT_N_PRIMARY_GUARDS;
  2101. /* At the start, we have no confirmed guards. We'll mark the primary guards
  2102. * down, then confirm something else. As soon as we do, it should become
  2103. * primary, and we should get it next time. */
  2104. time_t start = approx_time();
  2105. guard_selection_t *gs = guard_selection_new("default", GS_TYPE_NORMAL);
  2106. circuit_guard_state_t *guard = NULL, *guard2 = NULL;
  2107. int i, r;
  2108. const node_t *node = NULL;
  2109. entry_guard_t *g;
  2110. /* Declare that we're on the internet. */
  2111. entry_guards_note_internet_connectivity(gs);
  2112. /* Make primary guards confirmed (so they won't be superseded by a later
  2113. * guard), then mark them down. */
  2114. for (i = 0; i < N_PRIMARY; ++i) {
  2115. r = entry_guard_pick_for_circuit(gs, &node, &guard);
  2116. tt_assert(node);
  2117. tt_assert(guard);
  2118. tt_assert(r == 0);
  2119. tt_int_op(guard->state, OP_EQ, GUARD_CIRC_STATE_USABLE_ON_COMPLETION);
  2120. g = entry_guard_handle_get(guard->guard);
  2121. make_guard_confirmed(gs, g);
  2122. tt_int_op(g->is_primary, OP_EQ, 1);
  2123. entry_guard_failed(&guard);
  2124. circuit_guard_state_free(guard);
  2125. tt_int_op(g->is_reachable, OP_EQ, GUARD_REACHABLE_NO);
  2126. guard = NULL;
  2127. node = NULL;
  2128. }
  2129. /* Get another guard that we might try. */
  2130. r = entry_guard_pick_for_circuit(gs, &node, &guard);
  2131. tt_assert(node);
  2132. tt_assert(guard);
  2133. tt_assert(r == 0);
  2134. tt_int_op(guard->state, OP_EQ, GUARD_CIRC_STATE_USABLE_IF_NO_BETTER_GUARD);
  2135. g = entry_guard_handle_get(guard->guard);
  2136. tt_int_op(g->is_primary, OP_EQ, 0);
  2137. tt_assert(entry_guards_all_primary_guards_are_down(gs));
  2138. /* And an hour has passed ... */
  2139. update_approx_time(start + 3600);
  2140. /* Say that guard has succeeded! */
  2141. r = entry_guard_succeeded(&guard);
  2142. tt_int_op(r, OP_EQ, 0); // can't use it yet.
  2143. tt_int_op(guard->state, OP_EQ, GUARD_CIRC_STATE_WAITING_FOR_BETTER_GUARD);
  2144. g = entry_guard_handle_get(guard->guard);
  2145. /* The primary guards should have been marked up! */
  2146. SMARTLIST_FOREACH(gs->primary_entry_guards, entry_guard_t *, pg, {
  2147. tt_int_op(pg->is_primary, OP_EQ, 1);
  2148. tt_ptr_op(g, OP_NE, pg);
  2149. tt_int_op(pg->is_reachable, OP_EQ, GUARD_REACHABLE_MAYBE);
  2150. });
  2151. /* Have a circuit to a primary guard succeed. */
  2152. r = entry_guard_pick_for_circuit(gs, &node, &guard2);
  2153. tt_assert(r == 0);
  2154. tt_int_op(guard2->state, OP_EQ, GUARD_CIRC_STATE_USABLE_ON_COMPLETION);
  2155. r = entry_guard_succeeded(&guard2);
  2156. tt_assert(r == 1);
  2157. tt_int_op(guard2->state, OP_EQ, GUARD_CIRC_STATE_COMPLETE);
  2158. tt_assert(! entry_guards_all_primary_guards_are_down(gs));
  2159. done:
  2160. guard_selection_free(gs);
  2161. circuit_guard_state_free(guard);
  2162. circuit_guard_state_free(guard2);
  2163. }
  2164. static void
  2165. test_entry_guard_select_and_cancel(void *arg)
  2166. {
  2167. (void) arg;
  2168. const int N_PRIMARY = DFLT_N_PRIMARY_GUARDS;
  2169. int i,r;
  2170. const node_t *node = NULL;
  2171. circuit_guard_state_t *guard;
  2172. guard_selection_t *gs = guard_selection_new("default", GS_TYPE_NORMAL);
  2173. entry_guard_t *g;
  2174. /* Once more, we mark all the primary guards down. */
  2175. entry_guards_note_internet_connectivity(gs);
  2176. for (i = 0; i < N_PRIMARY; ++i) {
  2177. r = entry_guard_pick_for_circuit(gs, &node, &guard);
  2178. tt_int_op(guard->state, OP_EQ, GUARD_CIRC_STATE_USABLE_ON_COMPLETION);
  2179. g = entry_guard_handle_get(guard->guard);
  2180. tt_int_op(g->is_primary, OP_EQ, 1);
  2181. tt_int_op(g->is_pending, OP_EQ, 0);
  2182. make_guard_confirmed(gs, g);
  2183. entry_guard_failed(&guard);
  2184. circuit_guard_state_free(guard);
  2185. guard = NULL;
  2186. node = NULL;
  2187. }
  2188. tt_assert(entry_guards_all_primary_guards_are_down(gs));
  2189. /* Now get another guard we could try... */
  2190. r = entry_guard_pick_for_circuit(gs, &node, &guard);
  2191. tt_assert(node);
  2192. tt_assert(guard);
  2193. tt_assert(r == 0);
  2194. tt_int_op(guard->state, OP_EQ, GUARD_CIRC_STATE_USABLE_IF_NO_BETTER_GUARD);
  2195. g = entry_guard_handle_get(guard->guard);
  2196. tt_int_op(g->is_primary, OP_EQ, 0);
  2197. tt_int_op(g->is_pending, OP_EQ, 1);
  2198. /* Whoops! We should never have asked for this guard. Cancel the request! */
  2199. entry_guard_cancel(&guard);
  2200. tt_assert(guard == NULL);
  2201. tt_int_op(g->is_primary, OP_EQ, 0);
  2202. tt_int_op(g->is_pending, OP_EQ, 0);
  2203. done:
  2204. guard_selection_free(gs);
  2205. circuit_guard_state_free(guard);
  2206. }
  2207. /* Unit test setup function: Create a fake network, and set everything up
  2208. * for testing the upgrade-a-waiting-circuit code. */
  2209. typedef struct {
  2210. guard_selection_t *gs;
  2211. time_t start;
  2212. circuit_guard_state_t *guard1_state;
  2213. circuit_guard_state_t *guard2_state;
  2214. entry_guard_t *guard1;
  2215. entry_guard_t *guard2;
  2216. origin_circuit_t *circ1;
  2217. origin_circuit_t *circ2;
  2218. smartlist_t *all_origin_circuits;
  2219. } upgrade_circuits_data_t;
  2220. static void *
  2221. upgrade_circuits_setup(const struct testcase_t *testcase)
  2222. {
  2223. upgrade_circuits_data_t *data = tor_malloc_zero(sizeof(*data));
  2224. guard_selection_t *gs = data->gs =
  2225. guard_selection_new("default", GS_TYPE_NORMAL);
  2226. circuit_guard_state_t *guard;
  2227. const node_t *node;
  2228. entry_guard_t *g;
  2229. int i;
  2230. const int N_PRIMARY = DFLT_N_PRIMARY_GUARDS;
  2231. const char *argument = testcase->setup_data;
  2232. const int make_circ1_succeed = strstr(argument, "c1-done") != NULL;
  2233. const int make_circ2_succeed = strstr(argument, "c2-done") != NULL;
  2234. big_fake_network_setup(testcase);
  2235. /* We're going to set things up in a state where a circuit will be ready to
  2236. * be upgraded. Each test can make a single change (or not) that should
  2237. * block the upgrade.
  2238. */
  2239. /* First, make all the primary guards confirmed, and down. */
  2240. data->start = approx_time();
  2241. entry_guards_note_internet_connectivity(gs);
  2242. for (i = 0; i < N_PRIMARY; ++i) {
  2243. entry_guard_pick_for_circuit(gs, &node, &guard);
  2244. g = entry_guard_handle_get(guard->guard);
  2245. make_guard_confirmed(gs, g);
  2246. entry_guard_failed(&guard);
  2247. circuit_guard_state_free(guard);
  2248. }
  2249. /* Grab another couple of guards */
  2250. data->all_origin_circuits = smartlist_new();
  2251. update_approx_time(data->start + 27);
  2252. entry_guard_pick_for_circuit(gs, &node, &data->guard1_state);
  2253. origin_circuit_t *circ;
  2254. data->circ1 = circ = origin_circuit_new();
  2255. circ->base_.purpose = CIRCUIT_PURPOSE_C_GENERAL;
  2256. circ->guard_state = data->guard1_state;
  2257. smartlist_add(data->all_origin_circuits, circ);
  2258. update_approx_time(data->start + 30);
  2259. entry_guard_pick_for_circuit(gs, &node, &data->guard2_state);
  2260. data->circ2 = circ = origin_circuit_new();
  2261. circ->base_.purpose = CIRCUIT_PURPOSE_C_GENERAL;
  2262. circ->guard_state = data->guard2_state;
  2263. smartlist_add(data->all_origin_circuits, circ);
  2264. data->guard1 = entry_guard_handle_get(data->guard1_state->guard);
  2265. data->guard2 = entry_guard_handle_get(data->guard2_state->guard);
  2266. tor_assert(data->guard1 != data->guard2);
  2267. tor_assert(data->guard1_state->state ==
  2268. GUARD_CIRC_STATE_USABLE_IF_NO_BETTER_GUARD);
  2269. tor_assert(data->guard2_state->state ==
  2270. GUARD_CIRC_STATE_USABLE_IF_NO_BETTER_GUARD);
  2271. int r;
  2272. update_approx_time(data->start + 32);
  2273. if (make_circ1_succeed) {
  2274. r = entry_guard_succeeded(&data->guard1_state);
  2275. tor_assert(r == 0);
  2276. tor_assert(data->guard1_state->state ==
  2277. GUARD_CIRC_STATE_WAITING_FOR_BETTER_GUARD);
  2278. }
  2279. update_approx_time(data->start + 33);
  2280. if (make_circ2_succeed) {
  2281. r = entry_guard_succeeded(&data->guard2_state);
  2282. tor_assert(r == 0);
  2283. tor_assert(data->guard2_state->state ==
  2284. GUARD_CIRC_STATE_WAITING_FOR_BETTER_GUARD);
  2285. }
  2286. return data;
  2287. }
  2288. static int
  2289. upgrade_circuits_cleanup(const struct testcase_t *testcase, void *ptr)
  2290. {
  2291. upgrade_circuits_data_t *data = ptr;
  2292. // circuit_guard_state_free(data->guard1_state); // held in circ1
  2293. // circuit_guard_state_free(data->guard2_state); // held in circ2
  2294. guard_selection_free(data->gs);
  2295. smartlist_free(data->all_origin_circuits);
  2296. circuit_free(TO_CIRCUIT(data->circ1));
  2297. circuit_free(TO_CIRCUIT(data->circ2));
  2298. tor_free(data);
  2299. return big_fake_network_cleanup(testcase, ptr);
  2300. }
  2301. static void
  2302. test_entry_guard_upgrade_a_circuit(void *arg)
  2303. {
  2304. upgrade_circuits_data_t *data = arg;
  2305. /* This is the easy case: we have no COMPLETED circuits, all the
  2306. * primary guards are down, we have two WAITING circuits: one will
  2307. * get upgraded to COMPLETED! (The one that started first.)
  2308. */
  2309. /* XXXX prop271 -- perhaps the one that started first should
  2310. * also wind up in confirmed_entry_guards earlier?
  2311. */
  2312. smartlist_t *result = smartlist_new();
  2313. int r;
  2314. r = entry_guards_upgrade_waiting_circuits(data->gs,
  2315. data->all_origin_circuits,
  2316. result);
  2317. tt_int_op(r, OP_EQ, 1);
  2318. tt_int_op(smartlist_len(result), OP_EQ, 1);
  2319. origin_circuit_t *oc = smartlist_get(result, 0);
  2320. /* circ1 was started first, so we'll get told to ugrade it... */
  2321. tt_ptr_op(oc, OP_EQ, data->circ1);
  2322. /* And the guard state should be complete */
  2323. tt_ptr_op(data->guard1_state, OP_NE, NULL);
  2324. tt_int_op(data->guard1_state->state, OP_EQ, GUARD_CIRC_STATE_COMPLETE);
  2325. done:
  2326. smartlist_free(result);
  2327. }
  2328. static void
  2329. test_entry_guard_upgrade_blocked_by_live_primary_guards(void *arg)
  2330. {
  2331. upgrade_circuits_data_t *data = arg;
  2332. /* If any primary guards might be up, we can't upgrade any waiting
  2333. * circuits.
  2334. */
  2335. mark_primary_guards_maybe_reachable(data->gs);
  2336. smartlist_t *result = smartlist_new();
  2337. int r;
  2338. setup_capture_of_logs(LOG_DEBUG);
  2339. r = entry_guards_upgrade_waiting_circuits(data->gs,
  2340. data->all_origin_circuits,
  2341. result);
  2342. tt_int_op(r, OP_EQ, 0);
  2343. tt_int_op(smartlist_len(result), OP_EQ, 0);
  2344. expect_log_msg_containing("not all primary guards were definitely down.");
  2345. done:
  2346. teardown_capture_of_logs();
  2347. smartlist_free(result);
  2348. }
  2349. static void
  2350. test_entry_guard_upgrade_blocked_by_lack_of_waiting_circuits(void *arg)
  2351. {
  2352. upgrade_circuits_data_t *data = arg;
  2353. /* If no circuits are waiting, we can't upgrade anything. (The test
  2354. * setup in this case was told not to make any of the circuits "waiting".)
  2355. */
  2356. smartlist_t *result = smartlist_new();
  2357. int r;
  2358. setup_capture_of_logs(LOG_DEBUG);
  2359. r = entry_guards_upgrade_waiting_circuits(data->gs,
  2360. data->all_origin_circuits,
  2361. result);
  2362. tt_int_op(r, OP_EQ, 0);
  2363. tt_int_op(smartlist_len(result), OP_EQ, 0);
  2364. expect_log_msg_containing("Considered upgrading guard-stalled circuits, "
  2365. "but didn't find any.");
  2366. done:
  2367. teardown_capture_of_logs();
  2368. smartlist_free(result);
  2369. }
  2370. static void
  2371. test_entry_guard_upgrade_blocked_by_better_circ_complete(void *arg)
  2372. {
  2373. upgrade_circuits_data_t *data = arg;
  2374. /* We'll run through the logic of upgrade_a_circuit below...
  2375. * and then try again to make sure that circ2 isn't also upgraded.
  2376. */
  2377. smartlist_t *result = smartlist_new();
  2378. int r;
  2379. r = entry_guards_upgrade_waiting_circuits(data->gs,
  2380. data->all_origin_circuits,
  2381. result);
  2382. tt_int_op(r, OP_EQ, 1);
  2383. tt_int_op(smartlist_len(result), OP_EQ, 1);
  2384. origin_circuit_t *oc = smartlist_get(result, 0);
  2385. tt_ptr_op(oc, OP_EQ, data->circ1);
  2386. tt_ptr_op(data->guard1_state, OP_NE, NULL);
  2387. tt_int_op(data->guard1_state->state, OP_EQ, GUARD_CIRC_STATE_COMPLETE);
  2388. /* Now, try again. Make sure that circ2 isn't upgraded. */
  2389. smartlist_clear(result);
  2390. setup_capture_of_logs(LOG_DEBUG);
  2391. r = entry_guards_upgrade_waiting_circuits(data->gs,
  2392. data->all_origin_circuits,
  2393. result);
  2394. tt_int_op(r, OP_EQ, 0);
  2395. tt_int_op(smartlist_len(result), OP_EQ, 0);
  2396. expect_log_msg_containing("At least one complete circuit had higher "
  2397. "priority, so not upgrading.");
  2398. done:
  2399. teardown_capture_of_logs();
  2400. smartlist_free(result);
  2401. }
  2402. static void
  2403. test_entry_guard_upgrade_not_blocked_by_worse_circ_complete(void *arg)
  2404. {
  2405. upgrade_circuits_data_t *data = arg;
  2406. smartlist_t *result = smartlist_new();
  2407. /* here we manually make circ2 COMPLETE, and make sure that circ1
  2408. * gets made complete anyway, since guard1 has higher priority
  2409. */
  2410. update_approx_time(data->start + 300);
  2411. data->guard2_state->state = GUARD_CIRC_STATE_COMPLETE;
  2412. data->guard2_state->state_set_at = approx_time();
  2413. update_approx_time(data->start + 301);
  2414. /* Now, try again. Make sure that circ1 is approved. */
  2415. int r;
  2416. r = entry_guards_upgrade_waiting_circuits(data->gs,
  2417. data->all_origin_circuits,
  2418. result);
  2419. tt_int_op(r, OP_EQ, 1);
  2420. tt_int_op(smartlist_len(result), OP_EQ, 1);
  2421. origin_circuit_t *oc = smartlist_get(result, 0);
  2422. tt_ptr_op(oc, OP_EQ, data->circ1);
  2423. done:
  2424. smartlist_free(result);
  2425. }
  2426. static void
  2427. test_entry_guard_upgrade_blocked_by_better_circ_pending(void *arg)
  2428. {
  2429. upgrade_circuits_data_t *data = arg;
  2430. /* circ2 is done, but circ1 is still pending. Since circ1 is better,
  2431. * we won't upgrade circ2. */
  2432. /* XXXX Prop271 -- this is a kludge. I'm making sure circ1 _is_ better,
  2433. * by messing with the guards' confirmed_idx */
  2434. make_guard_confirmed(data->gs, data->guard1);
  2435. {
  2436. int tmp;
  2437. tmp = data->guard1->confirmed_idx;
  2438. data->guard1->confirmed_idx = data->guard2->confirmed_idx;
  2439. data->guard2->confirmed_idx = tmp;
  2440. }
  2441. smartlist_t *result = smartlist_new();
  2442. setup_capture_of_logs(LOG_DEBUG);
  2443. int r;
  2444. r = entry_guards_upgrade_waiting_circuits(data->gs,
  2445. data->all_origin_circuits,
  2446. result);
  2447. tt_int_op(r, OP_EQ, 0);
  2448. tt_int_op(smartlist_len(result), OP_EQ, 0);
  2449. expect_log_msg_containing("but 1 pending circuit(s) had higher guard "
  2450. "priority, so not upgrading.");
  2451. done:
  2452. teardown_capture_of_logs();
  2453. smartlist_free(result);
  2454. }
  2455. static void
  2456. test_entry_guard_upgrade_not_blocked_by_worse_circ_pending(void *arg)
  2457. {
  2458. upgrade_circuits_data_t *data = arg;
  2459. /* circ1 is done, but circ2 is still pending. Since circ1 is better,
  2460. * we will upgrade it. */
  2461. smartlist_t *result = smartlist_new();
  2462. int r;
  2463. r = entry_guards_upgrade_waiting_circuits(data->gs,
  2464. data->all_origin_circuits,
  2465. result);
  2466. tt_int_op(r, OP_EQ, 1);
  2467. tt_int_op(smartlist_len(result), OP_EQ, 1);
  2468. origin_circuit_t *oc = smartlist_get(result, 0);
  2469. tt_ptr_op(oc, OP_EQ, data->circ1);
  2470. done:
  2471. smartlist_free(result);
  2472. }
  2473. static const struct testcase_setup_t fake_network = {
  2474. fake_network_setup, fake_network_cleanup
  2475. };
  2476. static const struct testcase_setup_t big_fake_network = {
  2477. big_fake_network_setup, big_fake_network_cleanup
  2478. };
  2479. static const struct testcase_setup_t upgrade_circuits = {
  2480. upgrade_circuits_setup, upgrade_circuits_cleanup
  2481. };
  2482. #define BFN_TEST(name) \
  2483. { #name, test_entry_guard_ ## name, TT_FORK, &big_fake_network, NULL }
  2484. #define UPGRADE_TEST(name, arg) \
  2485. { #name, test_entry_guard_ ## name, TT_FORK, &upgrade_circuits, \
  2486. (void*)(arg) }
  2487. struct testcase_t entrynodes_tests[] = {
  2488. { "entry_is_time_to_retry", test_entry_is_time_to_retry,
  2489. TT_FORK, NULL, NULL },
  2490. { "choose_random_entry_no_guards", test_choose_random_entry_no_guards,
  2491. TT_FORK, &fake_network, NULL },
  2492. { "choose_random_entry_one_possible_guard",
  2493. test_choose_random_entry_one_possible_guard,
  2494. TT_FORK, &fake_network, NULL },
  2495. { "populate_live_entry_guards_1guard",
  2496. test_populate_live_entry_guards_1guard,
  2497. TT_FORK, &fake_network, NULL },
  2498. { "populate_live_entry_guards_3guards",
  2499. test_populate_live_entry_guards_3guards,
  2500. TT_FORK, &fake_network, NULL },
  2501. { "entry_guards_parse_state_simple",
  2502. test_entry_guards_parse_state_simple,
  2503. TT_FORK, &fake_network, NULL },
  2504. { "entry_guards_parse_state_pathbias",
  2505. test_entry_guards_parse_state_pathbias,
  2506. TT_FORK, &fake_network, NULL },
  2507. { "entry_guards_set_from_config",
  2508. test_entry_guards_set_from_config,
  2509. TT_FORK, &fake_network, NULL },
  2510. { "entry_is_live",
  2511. test_entry_is_live,
  2512. TT_FORK, &fake_network, NULL },
  2513. { "node_preferred_orport",
  2514. test_node_preferred_orport,
  2515. 0, NULL, NULL },
  2516. { "entry_guard_describe", test_entry_guard_describe, 0, NULL, NULL },
  2517. { "randomize_time", test_entry_guard_randomize_time, 0, NULL, NULL },
  2518. { "encode_for_state_minimal",
  2519. test_entry_guard_encode_for_state_minimal, 0, NULL, NULL },
  2520. { "encode_for_state_maximal",
  2521. test_entry_guard_encode_for_state_maximal, 0, NULL, NULL },
  2522. { "parse_from_state_minimal",
  2523. test_entry_guard_parse_from_state_minimal, 0, NULL, NULL },
  2524. { "parse_from_state_maximal",
  2525. test_entry_guard_parse_from_state_maximal, 0, NULL, NULL },
  2526. { "parse_from_state_failure",
  2527. test_entry_guard_parse_from_state_failure, 0, NULL, NULL },
  2528. { "parse_from_state_partial_failure",
  2529. test_entry_guard_parse_from_state_partial_failure, 0, NULL, NULL },
  2530. { "get_guard_selection_by_name",
  2531. test_entry_guard_get_guard_selection_by_name, TT_FORK, NULL, NULL },
  2532. BFN_TEST(add_single_guard),
  2533. BFN_TEST(node_filter),
  2534. BFN_TEST(expand_sample),
  2535. BFN_TEST(expand_sample_small_net),
  2536. BFN_TEST(update_from_consensus_status),
  2537. BFN_TEST(update_from_consensus_repair),
  2538. BFN_TEST(update_from_consensus_remove),
  2539. BFN_TEST(confirming_guards),
  2540. BFN_TEST(sample_reachable_filtered),
  2541. BFN_TEST(sample_reachable_filtered_empty),
  2542. BFN_TEST(retry_unreachable),
  2543. BFN_TEST(manage_primary),
  2544. { "guard_preferred", test_entry_guard_guard_preferred, TT_FORK, NULL, NULL },
  2545. BFN_TEST(select_for_circuit_no_confirmed),
  2546. BFN_TEST(select_for_circuit_confirmed),
  2547. BFN_TEST(select_for_circuit_highlevel_primary),
  2548. BFN_TEST(select_for_circuit_highlevel_confirm_other),
  2549. BFN_TEST(select_for_circuit_highlevel_primary_retry),
  2550. BFN_TEST(select_and_cancel),
  2551. UPGRADE_TEST(upgrade_a_circuit, "c1-done c2-done"),
  2552. UPGRADE_TEST(upgrade_blocked_by_live_primary_guards, "c1-done c2-done"),
  2553. UPGRADE_TEST(upgrade_blocked_by_lack_of_waiting_circuits, ""),
  2554. UPGRADE_TEST(upgrade_blocked_by_better_circ_complete, "c1-done c2-done"),
  2555. UPGRADE_TEST(upgrade_not_blocked_by_worse_circ_complete, "c1-done c2-done"),
  2556. UPGRADE_TEST(upgrade_blocked_by_better_circ_pending, "c2-done"),
  2557. UPGRADE_TEST(upgrade_not_blocked_by_worse_circ_pending, "c1-done"),
  2558. END_OF_TESTCASES
  2559. };