test_entrynodes.c 116 KB

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