test_entrynodes.c 115 KB

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