test_entrynodes.c 115 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454
  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. tt_int_op(smartlist_len(gs->sampled_entry_guards), OP_LE,
  1490. (int)((271 / 2) * .3));
  1491. done:
  1492. guard_selection_free(gs);
  1493. digestmap_free(node_by_id, NULL);
  1494. }
  1495. static void
  1496. test_entry_guard_expand_sample_small_net(void *arg)
  1497. {
  1498. (void)arg;
  1499. guard_selection_t *gs = guard_selection_new("default", GS_TYPE_NORMAL);
  1500. /* Fun corner case: not enough guards to make up our whole sample size. */
  1501. SMARTLIST_FOREACH(big_fake_net_nodes, node_t *, n, {
  1502. if (n_sl_idx >= 15) {
  1503. tor_free(n->rs);
  1504. tor_free(n->md);
  1505. tor_free(n);
  1506. SMARTLIST_DEL_CURRENT(big_fake_net_nodes, n);
  1507. } else {
  1508. n->rs->addr = 0; // make the filter reject this.
  1509. }
  1510. });
  1511. entry_guard_t *guard = entry_guards_expand_sample(gs);
  1512. tt_assert(guard); // the last guard returned -- some guard was added.
  1513. // half the nodes are guards, so we have 8 guards left. The set
  1514. // is small, so we sampled everything.
  1515. tt_int_op(smartlist_len(gs->sampled_entry_guards), OP_EQ, 8);
  1516. tt_int_op(num_reachable_filtered_guards(gs, NULL), OP_EQ, 0);
  1517. done:
  1518. guard_selection_free(gs);
  1519. }
  1520. static void
  1521. test_entry_guard_update_from_consensus_status(void *arg)
  1522. {
  1523. /* Here we're going to have some nodes become un-guardy, and say we got a
  1524. * new consensus. This should cause those nodes to get detected as
  1525. * unreachable. */
  1526. (void)arg;
  1527. int i;
  1528. time_t start = approx_time();
  1529. guard_selection_t *gs = guard_selection_new("default", GS_TYPE_NORMAL);
  1530. networkstatus_t *ns_tmp = NULL;
  1531. /* Don't randomly backdate stuff; it will make correctness harder to check.*/
  1532. MOCK(randomize_time, mock_randomize_time_no_randomization);
  1533. /* First, sample some guards. */
  1534. entry_guards_expand_sample(gs);
  1535. int n_sampled_pre = smartlist_len(gs->sampled_entry_guards);
  1536. int n_filtered_pre = num_reachable_filtered_guards(gs, NULL);
  1537. tt_i64_op(n_sampled_pre, OP_EQ, n_filtered_pre);
  1538. tt_i64_op(n_sampled_pre, OP_GT, 10);
  1539. /* At this point, it should be a no-op to do this: */
  1540. sampled_guards_update_from_consensus(gs);
  1541. /* Now let's make some of our guards become unlisted. The easiest way to
  1542. * do that would be to take away their guard flag. */
  1543. for (i = 0; i < 5; ++i) {
  1544. entry_guard_t *g = smartlist_get(gs->sampled_entry_guards, i);
  1545. node_t *n = (node_t*) bfn_mock_node_get_by_id(g->identity);
  1546. n->is_possible_guard = 0;
  1547. }
  1548. update_approx_time(start + 30);
  1549. {
  1550. /* try this with no live networkstatus. Nothing should happen! */
  1551. ns_tmp = dummy_consensus;
  1552. dummy_consensus = NULL;
  1553. sampled_guards_update_from_consensus(gs);
  1554. tt_i64_op(smartlist_len(gs->sampled_entry_guards), OP_EQ, n_sampled_pre);
  1555. tt_i64_op(num_reachable_filtered_guards(gs, NULL), OP_EQ, n_filtered_pre);
  1556. /* put the networkstatus back. */
  1557. dummy_consensus = ns_tmp;
  1558. ns_tmp = NULL;
  1559. }
  1560. /* Now those guards should become unlisted, and drop off the filter, but
  1561. * stay in the sample. */
  1562. update_approx_time(start + 60);
  1563. sampled_guards_update_from_consensus(gs);
  1564. tt_i64_op(smartlist_len(gs->sampled_entry_guards), OP_EQ, n_sampled_pre);
  1565. tt_i64_op(num_reachable_filtered_guards(gs, NULL), OP_EQ, n_filtered_pre-5);
  1566. for (i = 0; i < 5; ++i) {
  1567. entry_guard_t *g = smartlist_get(gs->sampled_entry_guards, i);
  1568. tt_assert(! g->currently_listed);
  1569. tt_i64_op(g->unlisted_since_date, OP_EQ, start+60);
  1570. }
  1571. for (i = 5; i < n_sampled_pre; ++i) {
  1572. entry_guard_t *g = smartlist_get(gs->sampled_entry_guards, i);
  1573. tt_assert(g->currently_listed);
  1574. tt_i64_op(g->unlisted_since_date, OP_EQ, 0);
  1575. }
  1576. /* Now re-list one, and remove one completely. */
  1577. {
  1578. entry_guard_t *g = smartlist_get(gs->sampled_entry_guards, 0);
  1579. node_t *n = (node_t*) bfn_mock_node_get_by_id(g->identity);
  1580. n->is_possible_guard = 1;
  1581. }
  1582. {
  1583. /* try removing the node, to make sure we don't crash on an absent node
  1584. */
  1585. entry_guard_t *g = smartlist_get(gs->sampled_entry_guards, 5);
  1586. node_t *n = (node_t*) bfn_mock_node_get_by_id(g->identity);
  1587. smartlist_remove(big_fake_net_nodes, n);
  1588. tor_free(n->rs);
  1589. tor_free(n->md);
  1590. tor_free(n);
  1591. }
  1592. update_approx_time(start + 300);
  1593. sampled_guards_update_from_consensus(gs);
  1594. /* guards 1..5 are now unlisted; 0,6,7.. are listed. */
  1595. tt_i64_op(smartlist_len(gs->sampled_entry_guards), OP_EQ, n_sampled_pre);
  1596. for (i = 1; i < 6; ++i) {
  1597. entry_guard_t *g = smartlist_get(gs->sampled_entry_guards, i);
  1598. tt_assert(! g->currently_listed);
  1599. if (i == 5)
  1600. tt_i64_op(g->unlisted_since_date, OP_EQ, start+300);
  1601. else
  1602. tt_i64_op(g->unlisted_since_date, OP_EQ, start+60);
  1603. }
  1604. for (i = 0; i < n_sampled_pre; i = (!i) ? 6 : i+1) { /* 0,6,7,8, ... */
  1605. entry_guard_t *g = smartlist_get(gs->sampled_entry_guards, i);
  1606. tt_assert(g->currently_listed);
  1607. tt_i64_op(g->unlisted_since_date, OP_EQ, 0);
  1608. }
  1609. done:
  1610. tor_free(ns_tmp); /* in case we couldn't put it back */
  1611. guard_selection_free(gs);
  1612. UNMOCK(randomize_time);
  1613. }
  1614. static void
  1615. test_entry_guard_update_from_consensus_repair(void *arg)
  1616. {
  1617. /* Here we'll make sure that our code to repair the unlisted-since
  1618. * times is correct. */
  1619. (void)arg;
  1620. int i;
  1621. time_t start = approx_time();
  1622. guard_selection_t *gs = guard_selection_new("default", GS_TYPE_NORMAL);
  1623. /* Don't randomly backdate stuff; it will make correctness harder to check.*/
  1624. MOCK(randomize_time, mock_randomize_time_no_randomization);
  1625. /* First, sample some guards. */
  1626. entry_guards_expand_sample(gs);
  1627. int n_sampled_pre = smartlist_len(gs->sampled_entry_guards);
  1628. int n_filtered_pre = num_reachable_filtered_guards(gs, NULL);
  1629. tt_i64_op(n_sampled_pre, OP_EQ, n_filtered_pre);
  1630. tt_i64_op(n_sampled_pre, OP_GT, 10);
  1631. /* Now corrupt the list a bit. Call some unlisted-since-never, and some
  1632. * listed-and-unlisted-since-a-time. */
  1633. update_approx_time(start + 300);
  1634. for (i = 0; i < 3; ++i) {
  1635. /* these will get a date. */
  1636. entry_guard_t *g = smartlist_get(gs->sampled_entry_guards, i);
  1637. node_t *n = (node_t*) bfn_mock_node_get_by_id(g->identity);
  1638. n->is_possible_guard = 0;
  1639. g->currently_listed = 0;
  1640. }
  1641. for (i = 3; i < 6; ++i) {
  1642. /* these will become listed. */
  1643. entry_guard_t *g = smartlist_get(gs->sampled_entry_guards, i);
  1644. g->unlisted_since_date = start+100;
  1645. }
  1646. setup_full_capture_of_logs(LOG_WARN);
  1647. sampled_guards_update_from_consensus(gs);
  1648. expect_log_msg_containing(
  1649. "was listed, but with unlisted_since_date set");
  1650. expect_log_msg_containing(
  1651. "was unlisted, but with unlisted_since_date unset");
  1652. teardown_capture_of_logs();
  1653. tt_int_op(smartlist_len(gs->sampled_entry_guards), OP_EQ, n_sampled_pre);
  1654. tt_int_op(num_reachable_filtered_guards(gs, NULL), OP_EQ, n_filtered_pre-3);
  1655. for (i = 3; i < n_sampled_pre; ++i) {
  1656. /* these will become listed. */
  1657. entry_guard_t *g = smartlist_get(gs->sampled_entry_guards, i);
  1658. if (i < 3) {
  1659. tt_assert(! g->currently_listed);
  1660. tt_i64_op(g->unlisted_since_date, OP_EQ, start+300);
  1661. } else {
  1662. tt_assert(g->currently_listed);
  1663. tt_i64_op(g->unlisted_since_date, OP_EQ, 0);
  1664. }
  1665. }
  1666. done:
  1667. teardown_capture_of_logs();
  1668. guard_selection_free(gs);
  1669. UNMOCK(randomize_time);
  1670. }
  1671. static void
  1672. test_entry_guard_update_from_consensus_remove(void *arg)
  1673. {
  1674. /* Now let's check the logic responsible for removing guards from the
  1675. * sample entirely. */
  1676. (void)arg;
  1677. //int i;
  1678. guard_selection_t *gs = guard_selection_new("default", GS_TYPE_NORMAL);
  1679. smartlist_t *keep_ids = smartlist_new();
  1680. smartlist_t *remove_ids = smartlist_new();
  1681. /* Don't randomly backdate stuff; it will make correctness harder to check.*/
  1682. MOCK(randomize_time, mock_randomize_time_no_randomization);
  1683. /* First, sample some guards. */
  1684. entry_guards_expand_sample(gs);
  1685. int n_sampled_pre = smartlist_len(gs->sampled_entry_guards);
  1686. int n_filtered_pre = num_reachable_filtered_guards(gs, NULL);
  1687. tt_i64_op(n_sampled_pre, OP_EQ, n_filtered_pre);
  1688. tt_i64_op(n_sampled_pre, OP_GT, 10);
  1689. const time_t one_day_ago = approx_time() - 1*24*60*60;
  1690. const time_t one_year_ago = approx_time() - 365*24*60*60;
  1691. const time_t two_years_ago = approx_time() - 2*365*24*60*60;
  1692. /* 0: unlisted for a day. (keep this) */
  1693. {
  1694. entry_guard_t *g = smartlist_get(gs->sampled_entry_guards, 0);
  1695. node_t *n = (node_t*) bfn_mock_node_get_by_id(g->identity);
  1696. n->is_possible_guard = 0;
  1697. g->currently_listed = 0;
  1698. g->unlisted_since_date = one_day_ago;
  1699. smartlist_add(keep_ids, tor_memdup(g->identity, 20));
  1700. }
  1701. /* 1: unlisted for a year. (remove this) */
  1702. {
  1703. entry_guard_t *g = smartlist_get(gs->sampled_entry_guards, 1);
  1704. node_t *n = (node_t*) bfn_mock_node_get_by_id(g->identity);
  1705. n->is_possible_guard = 0;
  1706. g->currently_listed = 0;
  1707. g->unlisted_since_date = one_year_ago;
  1708. smartlist_add(remove_ids, tor_memdup(g->identity, 20));
  1709. }
  1710. /* 2: added a day ago, never confirmed. (keep this) */
  1711. {
  1712. entry_guard_t *g = smartlist_get(gs->sampled_entry_guards, 2);
  1713. g->sampled_on_date = one_day_ago;
  1714. smartlist_add(keep_ids, tor_memdup(g->identity, 20));
  1715. }
  1716. /* 3: added a year ago, never confirmed. (remove this) */
  1717. {
  1718. entry_guard_t *g = smartlist_get(gs->sampled_entry_guards, 3);
  1719. g->sampled_on_date = one_year_ago;
  1720. smartlist_add(remove_ids, tor_memdup(g->identity, 20));
  1721. }
  1722. /* 4: added two year ago, confirmed yesterday, primary. (keep this.) */
  1723. {
  1724. entry_guard_t *g = smartlist_get(gs->sampled_entry_guards, 4);
  1725. g->sampled_on_date = one_year_ago;
  1726. g->confirmed_on_date = one_day_ago;
  1727. g->confirmed_idx = 0;
  1728. g->is_primary = 1;
  1729. smartlist_add(gs->confirmed_entry_guards, g);
  1730. smartlist_add(gs->primary_entry_guards, g);
  1731. smartlist_add(keep_ids, tor_memdup(g->identity, 20));
  1732. }
  1733. /* 5: added two years ago, confirmed a year ago, primary. (remove this) */
  1734. {
  1735. entry_guard_t *g = smartlist_get(gs->sampled_entry_guards, 5);
  1736. g->sampled_on_date = two_years_ago;
  1737. g->confirmed_on_date = one_year_ago;
  1738. g->confirmed_idx = 1;
  1739. g->is_primary = 1;
  1740. smartlist_add(gs->confirmed_entry_guards, g);
  1741. smartlist_add(gs->primary_entry_guards, g);
  1742. smartlist_add(remove_ids, tor_memdup(g->identity, 20));
  1743. }
  1744. sampled_guards_update_from_consensus(gs);
  1745. /* Did we remove the right ones? */
  1746. SMARTLIST_FOREACH(keep_ids, uint8_t *, id, {
  1747. tt_assert(get_sampled_guard_with_id(gs, id) != NULL);
  1748. });
  1749. SMARTLIST_FOREACH(remove_ids, uint8_t *, id, {
  1750. tt_want(get_sampled_guard_with_id(gs, id) == NULL);
  1751. });
  1752. /* Did we remove the right number? */
  1753. tt_int_op(smartlist_len(gs->sampled_entry_guards), OP_EQ, n_sampled_pre - 3);
  1754. done:
  1755. guard_selection_free(gs);
  1756. UNMOCK(randomize_time);
  1757. SMARTLIST_FOREACH(keep_ids, char *, cp, tor_free(cp));
  1758. SMARTLIST_FOREACH(remove_ids, char *, cp, tor_free(cp));
  1759. smartlist_free(keep_ids);
  1760. smartlist_free(remove_ids);
  1761. }
  1762. static void
  1763. test_entry_guard_confirming_guards(void *arg)
  1764. {
  1765. (void)arg;
  1766. /* Now let's check the logic responsible for manipulating the list
  1767. * of confirmed guards */
  1768. guard_selection_t *gs = guard_selection_new("default", GS_TYPE_NORMAL);
  1769. MOCK(randomize_time, mock_randomize_time_no_randomization);
  1770. /* Create the sample. */
  1771. entry_guards_expand_sample(gs);
  1772. /* Confirm a few guards. */
  1773. time_t start = approx_time();
  1774. entry_guard_t *g1 = smartlist_get(gs->sampled_entry_guards, 0);
  1775. entry_guard_t *g2 = smartlist_get(gs->sampled_entry_guards, 1);
  1776. entry_guard_t *g3 = smartlist_get(gs->sampled_entry_guards, 8);
  1777. make_guard_confirmed(gs, g2);
  1778. update_approx_time(start + 10);
  1779. make_guard_confirmed(gs, g1);
  1780. make_guard_confirmed(gs, g3);
  1781. /* Were the correct dates and indices fed in? */
  1782. tt_int_op(g1->confirmed_idx, OP_EQ, 1);
  1783. tt_int_op(g2->confirmed_idx, OP_EQ, 0);
  1784. tt_int_op(g3->confirmed_idx, OP_EQ, 2);
  1785. tt_i64_op(g1->confirmed_on_date, OP_EQ, start+10);
  1786. tt_i64_op(g2->confirmed_on_date, OP_EQ, start);
  1787. tt_i64_op(g3->confirmed_on_date, OP_EQ, start+10);
  1788. tt_ptr_op(smartlist_get(gs->confirmed_entry_guards, 0), OP_EQ, g2);
  1789. tt_ptr_op(smartlist_get(gs->confirmed_entry_guards, 1), OP_EQ, g1);
  1790. tt_ptr_op(smartlist_get(gs->confirmed_entry_guards, 2), OP_EQ, g3);
  1791. /* Now make sure we can regenerate the confirmed_entry_guards list. */
  1792. smartlist_clear(gs->confirmed_entry_guards);
  1793. g2->confirmed_idx = 0;
  1794. g1->confirmed_idx = 10;
  1795. g3->confirmed_idx = 100;
  1796. entry_guards_update_confirmed(gs);
  1797. tt_int_op(g1->confirmed_idx, OP_EQ, 1);
  1798. tt_int_op(g2->confirmed_idx, OP_EQ, 0);
  1799. tt_int_op(g3->confirmed_idx, OP_EQ, 2);
  1800. tt_ptr_op(smartlist_get(gs->confirmed_entry_guards, 0), OP_EQ, g2);
  1801. tt_ptr_op(smartlist_get(gs->confirmed_entry_guards, 1), OP_EQ, g1);
  1802. tt_ptr_op(smartlist_get(gs->confirmed_entry_guards, 2), OP_EQ, g3);
  1803. /* Now make sure we can regenerate the confirmed_entry_guards list if
  1804. * the indices are messed up. */
  1805. g1->confirmed_idx = g2->confirmed_idx = g3->confirmed_idx = 999;
  1806. smartlist_clear(gs->confirmed_entry_guards);
  1807. entry_guards_update_confirmed(gs);
  1808. tt_int_op(g1->confirmed_idx, OP_GE, 0);
  1809. tt_int_op(g2->confirmed_idx, OP_GE, 0);
  1810. tt_int_op(g3->confirmed_idx, OP_GE, 0);
  1811. tt_int_op(g1->confirmed_idx, OP_LE, 2);
  1812. tt_int_op(g2->confirmed_idx, OP_LE, 2);
  1813. tt_int_op(g3->confirmed_idx, OP_LE, 2);
  1814. g1 = smartlist_get(gs->confirmed_entry_guards, 0);
  1815. g2 = smartlist_get(gs->confirmed_entry_guards, 1);
  1816. g3 = smartlist_get(gs->confirmed_entry_guards, 2);
  1817. tt_int_op(g1->confirmed_idx, OP_EQ, 0);
  1818. tt_int_op(g2->confirmed_idx, OP_EQ, 1);
  1819. tt_int_op(g3->confirmed_idx, OP_EQ, 2);
  1820. tt_assert(g1 != g2);
  1821. tt_assert(g1 != g3);
  1822. tt_assert(g2 != g3);
  1823. done:
  1824. UNMOCK(randomize_time);
  1825. guard_selection_free(gs);
  1826. }
  1827. static void
  1828. test_entry_guard_sample_reachable_filtered(void *arg)
  1829. {
  1830. (void)arg;
  1831. guard_selection_t *gs = guard_selection_new("default", GS_TYPE_NORMAL);
  1832. entry_guards_expand_sample(gs);
  1833. const int N = 10000;
  1834. bitarray_t *selected = NULL;
  1835. int i, j;
  1836. /* We've got a sampled list now; let's make one non-usable-filtered; some
  1837. * confirmed, some primary, some pending.
  1838. */
  1839. int n_guards = smartlist_len(gs->sampled_entry_guards);
  1840. tt_int_op(n_guards, OP_GT, 10);
  1841. entry_guard_t *g;
  1842. g = smartlist_get(gs->sampled_entry_guards, 0);
  1843. g->is_pending = 1;
  1844. g = smartlist_get(gs->sampled_entry_guards, 1);
  1845. make_guard_confirmed(gs, g);
  1846. g = smartlist_get(gs->sampled_entry_guards, 2);
  1847. g->is_primary = 1;
  1848. g = smartlist_get(gs->sampled_entry_guards, 3);
  1849. g->pb.path_bias_disabled = 1;
  1850. entry_guards_update_filtered_sets(gs);
  1851. gs->primary_guards_up_to_date = 1;
  1852. tt_int_op(num_reachable_filtered_guards(gs, NULL), OP_EQ, n_guards - 1);
  1853. tt_int_op(smartlist_len(gs->sampled_entry_guards), OP_EQ, n_guards);
  1854. // +1 since the one we made disabled will make another one get added.
  1855. ++n_guards;
  1856. /* Try a bunch of selections. */
  1857. const struct {
  1858. int flag; int idx;
  1859. } tests[] = {
  1860. { 0, -1 },
  1861. { SAMPLE_EXCLUDE_CONFIRMED, 1 },
  1862. { SAMPLE_EXCLUDE_PRIMARY|SAMPLE_NO_UPDATE_PRIMARY, 2 },
  1863. { SAMPLE_EXCLUDE_PENDING, 0 },
  1864. { -1, -1},
  1865. };
  1866. for (j = 0; tests[j].flag >= 0; ++j) {
  1867. selected = bitarray_init_zero(n_guards);
  1868. const int excluded_flags = tests[j].flag;
  1869. const int excluded_idx = tests[j].idx;
  1870. for (i = 0; i < N; ++i) {
  1871. g = sample_reachable_filtered_entry_guards(gs, NULL, excluded_flags);
  1872. tor_assert(g);
  1873. int pos = smartlist_pos(gs->sampled_entry_guards, g);
  1874. tt_int_op(smartlist_len(gs->sampled_entry_guards), OP_EQ, n_guards);
  1875. tt_int_op(pos, OP_GE, 0);
  1876. tt_int_op(pos, OP_LT, n_guards);
  1877. bitarray_set(selected, pos);
  1878. }
  1879. for (i = 0; i < n_guards; ++i) {
  1880. const int should_be_set = (i != excluded_idx &&
  1881. i != 3); // filtered out.
  1882. tt_int_op(!!bitarray_is_set(selected, i), OP_EQ, should_be_set);
  1883. }
  1884. bitarray_free(selected);
  1885. selected = NULL;
  1886. }
  1887. done:
  1888. guard_selection_free(gs);
  1889. bitarray_free(selected);
  1890. }
  1891. static void
  1892. test_entry_guard_sample_reachable_filtered_empty(void *arg)
  1893. {
  1894. (void)arg;
  1895. guard_selection_t *gs = guard_selection_new("default", GS_TYPE_NORMAL);
  1896. /* What if we try to sample from a set of 0? */
  1897. SMARTLIST_FOREACH(big_fake_net_nodes, node_t *, n,
  1898. n->is_possible_guard = 0);
  1899. entry_guard_t *g = sample_reachable_filtered_entry_guards(gs, NULL, 0);
  1900. tt_ptr_op(g, OP_EQ, NULL);
  1901. done:
  1902. guard_selection_free(gs);
  1903. }
  1904. static void
  1905. test_entry_guard_retry_unreachable(void *arg)
  1906. {
  1907. (void)arg;
  1908. guard_selection_t *gs = guard_selection_new("default", GS_TYPE_NORMAL);
  1909. entry_guards_expand_sample(gs);
  1910. /* Let's say that we have two guards, and they're down.
  1911. */
  1912. time_t start = approx_time();;
  1913. entry_guard_t *g1 = smartlist_get(gs->sampled_entry_guards, 0);
  1914. entry_guard_t *g2 = smartlist_get(gs->sampled_entry_guards, 1);
  1915. entry_guard_t *g3 = smartlist_get(gs->sampled_entry_guards, 2);
  1916. g1->is_reachable = GUARD_REACHABLE_NO;
  1917. g2->is_reachable = GUARD_REACHABLE_NO;
  1918. g1->is_primary = 1;
  1919. g1->failing_since = g2->failing_since = start;
  1920. g1->last_tried_to_connect = g2->last_tried_to_connect = start;
  1921. /* Wait 5 minutes. Nothing will get retried. */
  1922. update_approx_time(start + 5 * 60);
  1923. entry_guard_consider_retry(g1);
  1924. entry_guard_consider_retry(g2);
  1925. entry_guard_consider_retry(g3); // just to make sure this doesn't crash.
  1926. tt_int_op(g1->is_reachable, OP_EQ, GUARD_REACHABLE_NO);
  1927. tt_int_op(g2->is_reachable, OP_EQ, GUARD_REACHABLE_NO);
  1928. tt_int_op(g3->is_reachable, OP_EQ, GUARD_REACHABLE_MAYBE);
  1929. /* After 30 min, the primary one gets retried */
  1930. update_approx_time(start + 35 * 60);
  1931. entry_guard_consider_retry(g1);
  1932. entry_guard_consider_retry(g2);
  1933. tt_int_op(g1->is_reachable, OP_EQ, GUARD_REACHABLE_MAYBE);
  1934. tt_int_op(g2->is_reachable, OP_EQ, GUARD_REACHABLE_NO);
  1935. g1->is_reachable = GUARD_REACHABLE_NO;
  1936. g1->last_tried_to_connect = start + 35*60;
  1937. /* After 1 hour, we'll retry the nonprimary one. */
  1938. update_approx_time(start + 61 * 60);
  1939. entry_guard_consider_retry(g1);
  1940. entry_guard_consider_retry(g2);
  1941. tt_int_op(g1->is_reachable, OP_EQ, GUARD_REACHABLE_NO);
  1942. tt_int_op(g2->is_reachable, OP_EQ, GUARD_REACHABLE_MAYBE);
  1943. g2->is_reachable = GUARD_REACHABLE_NO;
  1944. g2->last_tried_to_connect = start + 61*60;
  1945. /* And then the primary one again. */
  1946. update_approx_time(start + 66 * 60);
  1947. entry_guard_consider_retry(g1);
  1948. entry_guard_consider_retry(g2);
  1949. tt_int_op(g1->is_reachable, OP_EQ, GUARD_REACHABLE_MAYBE);
  1950. tt_int_op(g2->is_reachable, OP_EQ, GUARD_REACHABLE_NO);
  1951. done:
  1952. guard_selection_free(gs);
  1953. }
  1954. static void
  1955. test_entry_guard_manage_primary(void *arg)
  1956. {
  1957. (void)arg;
  1958. guard_selection_t *gs = guard_selection_new("default", GS_TYPE_NORMAL);
  1959. smartlist_t *prev_guards = smartlist_new();
  1960. /* If no guards are confirmed, we should pick a few reachable guards and
  1961. * call them all primary. But not confirmed.*/
  1962. entry_guards_update_primary(gs);
  1963. int n_primary = smartlist_len(gs->primary_entry_guards);
  1964. tt_int_op(n_primary, OP_GE, 1);
  1965. SMARTLIST_FOREACH(gs->primary_entry_guards, entry_guard_t *, g, {
  1966. tt_assert(g->is_primary);
  1967. tt_assert(g->confirmed_idx == -1);
  1968. });
  1969. /* Calling it a second time should leave the guards unchanged. */
  1970. smartlist_add_all(prev_guards, gs->primary_entry_guards);
  1971. entry_guards_update_primary(gs);
  1972. tt_int_op(smartlist_len(gs->primary_entry_guards), OP_EQ, n_primary);
  1973. SMARTLIST_FOREACH(gs->primary_entry_guards, entry_guard_t *, g, {
  1974. tt_ptr_op(g, OP_EQ, smartlist_get(prev_guards, g_sl_idx));
  1975. });
  1976. /* If we have one confirmed guard, that guards becomes the first primary
  1977. * guard, and the other primary guards get kept. */
  1978. /* find a non-primary guard... */
  1979. entry_guard_t *confirmed = NULL;
  1980. SMARTLIST_FOREACH(gs->sampled_entry_guards, entry_guard_t *, g, {
  1981. if (! g->is_primary) {
  1982. confirmed = g;
  1983. break;
  1984. }
  1985. });
  1986. tt_assert(confirmed);
  1987. /* make it confirmed. */
  1988. make_guard_confirmed(gs, confirmed);
  1989. /* update the list... */
  1990. smartlist_clear(prev_guards);
  1991. smartlist_add_all(prev_guards, gs->primary_entry_guards);
  1992. entry_guards_update_primary(gs);
  1993. /* and see what's primary now! */
  1994. tt_int_op(smartlist_len(gs->primary_entry_guards), OP_EQ, n_primary);
  1995. tt_ptr_op(smartlist_get(gs->primary_entry_guards, 0), OP_EQ, confirmed);
  1996. SMARTLIST_FOREACH(gs->primary_entry_guards, entry_guard_t *, g, {
  1997. tt_assert(g->is_primary);
  1998. if (g_sl_idx == 0)
  1999. continue;
  2000. tt_ptr_op(g, OP_EQ, smartlist_get(prev_guards, g_sl_idx - 1));
  2001. });
  2002. {
  2003. entry_guard_t *prev_last_guard = smartlist_get(prev_guards, n_primary-1);
  2004. tt_assert(! prev_last_guard->is_primary);
  2005. }
  2006. /* Calling it a fourth time should leave the guards unchanged. */
  2007. smartlist_clear(prev_guards);
  2008. smartlist_add_all(prev_guards, gs->primary_entry_guards);
  2009. entry_guards_update_primary(gs);
  2010. tt_int_op(smartlist_len(gs->primary_entry_guards), OP_EQ, n_primary);
  2011. SMARTLIST_FOREACH(gs->primary_entry_guards, entry_guard_t *, g, {
  2012. tt_ptr_op(g, OP_EQ, smartlist_get(prev_guards, g_sl_idx));
  2013. });
  2014. done:
  2015. guard_selection_free(gs);
  2016. smartlist_free(prev_guards);
  2017. }
  2018. static void
  2019. test_entry_guard_guard_preferred(void *arg)
  2020. {
  2021. (void) arg;
  2022. entry_guard_t *g1 = tor_malloc_zero(sizeof(entry_guard_t));
  2023. entry_guard_t *g2 = tor_malloc_zero(sizeof(entry_guard_t));
  2024. g1->confirmed_idx = g2->confirmed_idx = -1;
  2025. g1->last_tried_to_connect = approx_time();
  2026. g2->last_tried_to_connect = approx_time();
  2027. tt_int_op(0, OP_EQ, entry_guard_has_higher_priority(g1, g1));
  2028. /* Neither is pending; priorities equal. */
  2029. tt_int_op(0, OP_EQ, entry_guard_has_higher_priority(g2, g1));
  2030. tt_int_op(0, OP_EQ, entry_guard_has_higher_priority(g1, g2));
  2031. /* If one is pending, the pending one has higher priority */
  2032. g1->is_pending = 1;
  2033. tt_int_op(1, OP_EQ, entry_guard_has_higher_priority(g1, g2));
  2034. tt_int_op(0, OP_EQ, entry_guard_has_higher_priority(g2, g1));
  2035. /* If both are pending, and last_tried_to_connect is equal:
  2036. priorities equal */
  2037. g2->is_pending = 1;
  2038. tt_int_op(0, OP_EQ, entry_guard_has_higher_priority(g2, g1));
  2039. tt_int_op(0, OP_EQ, entry_guard_has_higher_priority(g1, g2));
  2040. /* One had a connection that startied earlier: it has higher priority. */
  2041. g2->last_tried_to_connect -= 10;
  2042. tt_int_op(1, OP_EQ, entry_guard_has_higher_priority(g2, g1));
  2043. tt_int_op(0, OP_EQ, entry_guard_has_higher_priority(g1, g2));
  2044. /* Now, say that g1 is confirmed. It will get higher priority. */
  2045. g1->confirmed_idx = 5;
  2046. tt_int_op(0, OP_EQ, entry_guard_has_higher_priority(g2, g1));
  2047. tt_int_op(1, OP_EQ, entry_guard_has_higher_priority(g1, g2));
  2048. /* But if g2 was confirmed first, it will get priority */
  2049. g2->confirmed_idx = 2;
  2050. tt_int_op(1, OP_EQ, entry_guard_has_higher_priority(g2, g1));
  2051. tt_int_op(0, OP_EQ, entry_guard_has_higher_priority(g1, g2));
  2052. done:
  2053. tor_free(g1);
  2054. tor_free(g2);
  2055. }
  2056. static void
  2057. test_entry_guard_select_for_circuit_no_confirmed(void *arg)
  2058. {
  2059. /* Simpler cases: no gaurds are confirmed yet. */
  2060. (void)arg;
  2061. guard_selection_t *gs = guard_selection_new("default", GS_TYPE_NORMAL);
  2062. /* simple starting configuration */
  2063. entry_guards_update_primary(gs);
  2064. unsigned state = 9999;
  2065. entry_guard_t *g = select_entry_guard_for_circuit(gs, NULL, &state);
  2066. tt_assert(g);
  2067. tt_assert(g->is_primary);
  2068. tt_int_op(g->confirmed_idx, OP_EQ, -1);
  2069. tt_assert(g->is_pending == 0); // primary implies non-pending.
  2070. tt_uint_op(state, OP_EQ, GUARD_CIRC_STATE_USABLE_ON_COMPLETION);
  2071. tt_i64_op(g->last_tried_to_connect, OP_EQ, approx_time());
  2072. // If we do that again, we should get the same guard.
  2073. entry_guard_t *g2 = select_entry_guard_for_circuit(gs, NULL, &state);
  2074. tt_ptr_op(g2, OP_EQ, g);
  2075. // if we mark that guard down, we should get a different primary guard.
  2076. // auto-retry it.
  2077. g->is_reachable = GUARD_REACHABLE_NO;
  2078. g->failing_since = approx_time() - 10;
  2079. g->last_tried_to_connect = approx_time() - 10;
  2080. state = 9999;
  2081. g2 = select_entry_guard_for_circuit(gs, NULL, &state);
  2082. tt_ptr_op(g2, OP_NE, g);
  2083. tt_assert(g2);
  2084. tt_assert(g2->is_primary);
  2085. tt_int_op(g2->confirmed_idx, OP_EQ, -1);
  2086. tt_assert(g2->is_pending == 0); // primary implies non-pending.
  2087. tt_uint_op(state, OP_EQ, GUARD_CIRC_STATE_USABLE_ON_COMPLETION);
  2088. tt_i64_op(g2->last_tried_to_connect, OP_EQ, approx_time());
  2089. // If we say that the first primary guard was last tried a long time ago, we
  2090. // should get an automatic retry on it.
  2091. g->failing_since = approx_time() - 72*60*60;
  2092. g->last_tried_to_connect = approx_time() - 72*60*60;
  2093. state = 9999;
  2094. g2 = select_entry_guard_for_circuit(gs, NULL, &state);
  2095. tt_ptr_op(g2, OP_EQ, g);
  2096. tt_assert(g2);
  2097. tt_uint_op(state, OP_EQ, GUARD_CIRC_STATE_USABLE_ON_COMPLETION);
  2098. tt_i64_op(g2->last_tried_to_connect, OP_EQ, approx_time());
  2099. tt_int_op(g2->is_reachable, OP_EQ, GUARD_REACHABLE_MAYBE);
  2100. // And if we mark ALL the primary guards down, we should get another guard
  2101. // at random.
  2102. SMARTLIST_FOREACH(gs->primary_entry_guards, entry_guard_t *, guard, {
  2103. guard->is_reachable = GUARD_REACHABLE_NO;
  2104. guard->last_tried_to_connect = approx_time() - 5;
  2105. guard->failing_since = approx_time() - 30;
  2106. });
  2107. state = 9999;
  2108. g2 = select_entry_guard_for_circuit(gs, NULL, &state);
  2109. tt_assert(g2);
  2110. tt_assert(!g2->is_primary);
  2111. tt_int_op(g2->confirmed_idx, OP_EQ, -1);
  2112. tt_assert(g2->is_pending == 1);
  2113. tt_uint_op(state, OP_EQ, GUARD_CIRC_STATE_USABLE_IF_NO_BETTER_GUARD);
  2114. tt_i64_op(g2->last_tried_to_connect, OP_EQ, approx_time());
  2115. tt_int_op(g2->is_reachable, OP_EQ, GUARD_REACHABLE_MAYBE);
  2116. // As a bonus, maybe we should be retrying the primary guards. Let's say so.
  2117. mark_primary_guards_maybe_reachable(gs);
  2118. SMARTLIST_FOREACH(gs->primary_entry_guards, entry_guard_t *, guard, {
  2119. tt_int_op(guard->is_reachable, OP_EQ, GUARD_REACHABLE_MAYBE);
  2120. tt_assert(guard->is_usable_filtered_guard == 1);
  2121. // no change to these fields.
  2122. tt_i64_op(guard->last_tried_to_connect, OP_EQ, approx_time() - 5);
  2123. tt_i64_op(guard->failing_since, OP_EQ, approx_time() - 30);
  2124. });
  2125. /* Let's try again and we should get the first primary guard again */
  2126. g = select_entry_guard_for_circuit(gs, NULL, &state);
  2127. tt_ptr_op(g, OP_EQ, smartlist_get(gs->primary_entry_guards, 0));
  2128. g2 = select_entry_guard_for_circuit(gs, NULL, &state);
  2129. tt_ptr_op(g2, OP_EQ, g);
  2130. /* But if we impose a restriction, we don't get the same guard */
  2131. entry_guard_restriction_t rst;
  2132. memset(&rst, 0, sizeof(rst));
  2133. memcpy(rst.exclude_id, g->identity, DIGEST_LEN);
  2134. g2 = select_entry_guard_for_circuit(gs, &rst, &state);
  2135. tt_ptr_op(g2, OP_NE, g);
  2136. done:
  2137. guard_selection_free(gs);
  2138. }
  2139. static void
  2140. test_entry_guard_select_for_circuit_confirmed(void *arg)
  2141. {
  2142. /* Case 2: if all the primary guards are down, and there are more confirmed
  2143. guards, we use a confirmed guard. */
  2144. (void)arg;
  2145. int i;
  2146. guard_selection_t *gs = guard_selection_new("default", GS_TYPE_NORMAL);
  2147. const int N_CONFIRMED = 10;
  2148. /* slightly more complicated simple starting configuration */
  2149. entry_guards_update_primary(gs);
  2150. for (i = 0; i < N_CONFIRMED; ++i) {
  2151. entry_guard_t *guard = smartlist_get(gs->sampled_entry_guards, i);
  2152. make_guard_confirmed(gs, guard);
  2153. }
  2154. entry_guards_update_primary(gs); // rebuild the primary list.
  2155. unsigned state = 9999;
  2156. // As above, this gives us a primary guard.
  2157. entry_guard_t *g = select_entry_guard_for_circuit(gs, NULL, &state);
  2158. tt_assert(g);
  2159. tt_assert(g->is_primary);
  2160. tt_int_op(g->confirmed_idx, OP_EQ, 0);
  2161. tt_assert(g->is_pending == 0); // primary implies non-pending.
  2162. tt_uint_op(state, OP_EQ, GUARD_CIRC_STATE_USABLE_ON_COMPLETION);
  2163. tt_i64_op(g->last_tried_to_connect, OP_EQ, approx_time());
  2164. tt_ptr_op(g, OP_EQ, smartlist_get(gs->primary_entry_guards, 0));
  2165. // But if we mark all the primary guards down...
  2166. SMARTLIST_FOREACH(gs->primary_entry_guards, entry_guard_t *, guard, {
  2167. guard->last_tried_to_connect = approx_time();
  2168. entry_guards_note_guard_failure(gs, guard);
  2169. });
  2170. // ... we should get a confirmed guard.
  2171. state = 9999;
  2172. g = select_entry_guard_for_circuit(gs, NULL, &state);
  2173. tt_assert(g);
  2174. tt_assert(! g->is_primary);
  2175. tt_int_op(g->confirmed_idx, OP_EQ, smartlist_len(gs->primary_entry_guards));
  2176. tt_assert(g->is_pending);
  2177. tt_uint_op(state, OP_EQ, GUARD_CIRC_STATE_USABLE_IF_NO_BETTER_GUARD);
  2178. tt_i64_op(g->last_tried_to_connect, OP_EQ, approx_time());
  2179. // And if we try again, we should get a different confirmed guard, since
  2180. // that one is pending.
  2181. state = 9999;
  2182. entry_guard_t *g2 = select_entry_guard_for_circuit(gs, NULL, &state);
  2183. tt_assert(g2);
  2184. tt_assert(! g2->is_primary);
  2185. tt_ptr_op(g2, OP_NE, g);
  2186. tt_int_op(g2->confirmed_idx, OP_EQ,
  2187. smartlist_len(gs->primary_entry_guards)+1);
  2188. tt_assert(g2->is_pending);
  2189. tt_uint_op(state, OP_EQ, GUARD_CIRC_STATE_USABLE_IF_NO_BETTER_GUARD);
  2190. tt_i64_op(g2->last_tried_to_connect, OP_EQ, approx_time());
  2191. // If we say that the next confirmed guard in order is excluded, we get
  2192. // The one AFTER that.
  2193. g = smartlist_get(gs->confirmed_entry_guards,
  2194. smartlist_len(gs->primary_entry_guards)+2);
  2195. entry_guard_restriction_t rst;
  2196. memset(&rst, 0, sizeof(rst));
  2197. memcpy(rst.exclude_id, g->identity, DIGEST_LEN);
  2198. g2 = select_entry_guard_for_circuit(gs, &rst, &state);
  2199. tt_ptr_op(g2, OP_NE, g);
  2200. tt_int_op(g2->confirmed_idx, OP_EQ,
  2201. smartlist_len(gs->primary_entry_guards)+3);
  2202. // If we make every confirmed guard become pending then we start poking
  2203. // other guards.
  2204. const int n_remaining_confirmed =
  2205. N_CONFIRMED - 3 - smartlist_len(gs->primary_entry_guards);
  2206. for (i = 0; i < n_remaining_confirmed; ++i) {
  2207. g = select_entry_guard_for_circuit(gs, NULL, &state);
  2208. tt_int_op(g->confirmed_idx, OP_GE, 0);
  2209. tt_assert(g);
  2210. }
  2211. state = 9999;
  2212. g = select_entry_guard_for_circuit(gs, NULL, &state);
  2213. tt_assert(g);
  2214. tt_assert(g->is_pending);
  2215. tt_int_op(g->confirmed_idx, OP_EQ, -1);
  2216. done:
  2217. guard_selection_free(gs);
  2218. }
  2219. static void
  2220. test_entry_guard_select_for_circuit_highlevel_primary(void *arg)
  2221. {
  2222. /* Play around with selecting primary guards for circuits and markign
  2223. * them up and down */
  2224. (void)arg;
  2225. guard_selection_t *gs = guard_selection_new("default", GS_TYPE_NORMAL);
  2226. time_t start = approx_time();
  2227. const node_t *node = NULL;
  2228. circuit_guard_state_t *guard = NULL;
  2229. entry_guard_t *g;
  2230. guard_usable_t u;
  2231. /*
  2232. * Make sure that the pick-for-circuit API basically works. We'll get
  2233. * a primary guard, so it'll be usable on completion.
  2234. */
  2235. int r = entry_guard_pick_for_circuit(gs, NULL, &node, &guard);
  2236. tt_assert(r == 0);
  2237. tt_assert(node);
  2238. tt_assert(guard);
  2239. tt_int_op(guard->state, OP_EQ, GUARD_CIRC_STATE_USABLE_ON_COMPLETION);
  2240. g = entry_guard_handle_get(guard->guard);
  2241. tt_assert(g);
  2242. tt_mem_op(g->identity, OP_EQ, node->identity, DIGEST_LEN);
  2243. tt_int_op(g->is_primary, OP_EQ, 1);
  2244. tt_i64_op(g->last_tried_to_connect, OP_EQ, start);
  2245. tt_int_op(g->confirmed_idx, OP_EQ, -1);
  2246. /* Call that circuit successful. */
  2247. update_approx_time(start+15);
  2248. u = entry_guard_succeeded(&guard);
  2249. tt_int_op(u, OP_EQ, GUARD_USABLE_NOW); /* We can use it now. */
  2250. tt_assert(guard);
  2251. tt_int_op(guard->state, OP_EQ, GUARD_CIRC_STATE_COMPLETE);
  2252. g = entry_guard_handle_get(guard->guard);
  2253. tt_assert(g);
  2254. tt_int_op(g->is_reachable, OP_EQ, GUARD_REACHABLE_YES);
  2255. tt_int_op(g->confirmed_idx, OP_EQ, 0);
  2256. circuit_guard_state_free(guard);
  2257. guard = NULL;
  2258. node = NULL;
  2259. g = NULL;
  2260. /* Try again. We'll also get a primary guard this time. (The same one,
  2261. in fact.) But this time, we'll say the connection has failed. */
  2262. update_approx_time(start+35);
  2263. r = entry_guard_pick_for_circuit(gs, NULL, &node, &guard);
  2264. tt_assert(r == 0);
  2265. tt_assert(node);
  2266. tt_assert(guard);
  2267. tt_int_op(guard->state, OP_EQ, GUARD_CIRC_STATE_USABLE_ON_COMPLETION);
  2268. tt_i64_op(guard->state_set_at, OP_EQ, start+35);
  2269. g = entry_guard_handle_get(guard->guard);
  2270. tt_assert(g);
  2271. tt_mem_op(g->identity, OP_EQ, node->identity, DIGEST_LEN);
  2272. tt_int_op(g->is_primary, OP_EQ, 1);
  2273. tt_i64_op(g->last_tried_to_connect, OP_EQ, start+35);
  2274. tt_int_op(g->confirmed_idx, OP_EQ, 0); // same one.
  2275. /* It's failed! What will happen to our poor guard? */
  2276. update_approx_time(start+45);
  2277. entry_guard_failed(&guard);
  2278. tt_assert(guard);
  2279. tt_int_op(guard->state, OP_EQ, GUARD_CIRC_STATE_DEAD);
  2280. tt_i64_op(guard->state_set_at, OP_EQ, start+45);
  2281. g = entry_guard_handle_get(guard->guard);
  2282. tt_assert(g);
  2283. tt_int_op(g->is_reachable, OP_EQ, GUARD_REACHABLE_NO);
  2284. tt_i64_op(g->failing_since, OP_EQ, start+45);
  2285. tt_int_op(g->confirmed_idx, OP_EQ, 0); // still confirmed.
  2286. circuit_guard_state_free(guard);
  2287. guard = NULL;
  2288. node = NULL;
  2289. entry_guard_t *g_prev = g;
  2290. g = NULL;
  2291. /* Now try a third time. Since the other one is down, we'll get a different
  2292. * (still primary) guard.
  2293. */
  2294. update_approx_time(start+60);
  2295. r = entry_guard_pick_for_circuit(gs, NULL, &node, &guard);
  2296. tt_assert(r == 0);
  2297. tt_assert(node);
  2298. tt_assert(guard);
  2299. tt_int_op(guard->state, OP_EQ, GUARD_CIRC_STATE_USABLE_ON_COMPLETION);
  2300. g = entry_guard_handle_get(guard->guard);
  2301. tt_assert(g);
  2302. tt_ptr_op(g, OP_NE, g_prev);
  2303. tt_mem_op(g->identity, OP_EQ, node->identity, DIGEST_LEN);
  2304. tt_mem_op(g->identity, OP_NE, g_prev->identity, DIGEST_LEN);
  2305. tt_int_op(g->is_primary, OP_EQ, 1);
  2306. tt_i64_op(g->last_tried_to_connect, OP_EQ, start+60);
  2307. tt_int_op(g->confirmed_idx, OP_EQ, -1); // not confirmd now.
  2308. /* Call this one up; watch it get confirmed. */
  2309. update_approx_time(start+90);
  2310. u = entry_guard_succeeded(&guard);
  2311. tt_int_op(u, OP_EQ, GUARD_USABLE_NOW);
  2312. tt_assert(guard);
  2313. tt_int_op(guard->state, OP_EQ, GUARD_CIRC_STATE_COMPLETE);
  2314. g = entry_guard_handle_get(guard->guard);
  2315. tt_assert(g);
  2316. tt_int_op(g->is_reachable, OP_EQ, GUARD_REACHABLE_YES);
  2317. tt_int_op(g->confirmed_idx, OP_EQ, 1);
  2318. done:
  2319. guard_selection_free(gs);
  2320. circuit_guard_state_free(guard);
  2321. }
  2322. static void
  2323. test_entry_guard_select_for_circuit_highlevel_confirm_other(void *arg)
  2324. {
  2325. (void) arg;
  2326. const int N_PRIMARY = DFLT_N_PRIMARY_GUARDS;
  2327. /* At the start, we have no confirmed guards. We'll mark the primary guards
  2328. * down, then confirm something else. As soon as we do, it should become
  2329. * primary, and we should get it next time. */
  2330. time_t start = approx_time();
  2331. guard_selection_t *gs = guard_selection_new("default", GS_TYPE_NORMAL);
  2332. circuit_guard_state_t *guard = NULL;
  2333. int i, r;
  2334. const node_t *node = NULL;
  2335. guard_usable_t u;
  2336. /* Declare that we're on the internet. */
  2337. entry_guards_note_internet_connectivity(gs);
  2338. /* Primary guards are down! */
  2339. for (i = 0; i < N_PRIMARY; ++i) {
  2340. r = entry_guard_pick_for_circuit(gs, NULL, &node, &guard);
  2341. tt_assert(node);
  2342. tt_assert(guard);
  2343. tt_assert(r == 0);
  2344. tt_int_op(guard->state, OP_EQ, GUARD_CIRC_STATE_USABLE_ON_COMPLETION);
  2345. entry_guard_failed(&guard);
  2346. circuit_guard_state_free(guard);
  2347. guard = NULL;
  2348. node = NULL;
  2349. }
  2350. /* Next guard should be non-primary. */
  2351. node = NULL;
  2352. r = entry_guard_pick_for_circuit(gs, NULL, &node, &guard);
  2353. tt_assert(node);
  2354. tt_assert(guard);
  2355. tt_assert(r == 0);
  2356. entry_guard_t *g = entry_guard_handle_get(guard->guard);
  2357. tt_assert(g);
  2358. tt_int_op(guard->state, OP_EQ, GUARD_CIRC_STATE_USABLE_IF_NO_BETTER_GUARD);
  2359. tt_int_op(g->confirmed_idx, OP_EQ, -1);
  2360. tt_int_op(g->is_primary, OP_EQ, 0);
  2361. tt_int_op(g->is_pending, OP_EQ, 1);
  2362. (void)start;
  2363. u = entry_guard_succeeded(&guard);
  2364. /* We're on the internet (by fiat), so this guard will get called "confirmed"
  2365. * and should immediately become primary.
  2366. * XXXX prop271 -- I don't like that behavior, but it's what is specified
  2367. */
  2368. tt_int_op(guard->state, OP_EQ, GUARD_CIRC_STATE_COMPLETE);
  2369. tt_assert(u == GUARD_USABLE_NOW);
  2370. tt_int_op(g->confirmed_idx, OP_EQ, 0);
  2371. tt_int_op(g->is_primary, OP_EQ, 1);
  2372. tt_int_op(g->is_pending, OP_EQ, 0);
  2373. done:
  2374. guard_selection_free(gs);
  2375. circuit_guard_state_free(guard);
  2376. }
  2377. static void
  2378. test_entry_guard_select_for_circuit_highlevel_primary_retry(void *arg)
  2379. {
  2380. (void) arg;
  2381. const int N_PRIMARY = DFLT_N_PRIMARY_GUARDS;
  2382. /* At the start, we have no confirmed guards. We'll mark the primary guards
  2383. * down, then confirm something else. As soon as we do, it should become
  2384. * primary, and we should get it next time. */
  2385. time_t start = approx_time();
  2386. guard_selection_t *gs = guard_selection_new("default", GS_TYPE_NORMAL);
  2387. circuit_guard_state_t *guard = NULL, *guard2 = NULL;
  2388. int i, r;
  2389. const node_t *node = NULL;
  2390. entry_guard_t *g;
  2391. guard_usable_t u;
  2392. /* Declare that we're on the internet. */
  2393. entry_guards_note_internet_connectivity(gs);
  2394. /* Make primary guards confirmed (so they won't be superseded by a later
  2395. * guard), then mark them down. */
  2396. for (i = 0; i < N_PRIMARY; ++i) {
  2397. r = entry_guard_pick_for_circuit(gs, NULL, &node, &guard);
  2398. tt_assert(node);
  2399. tt_assert(guard);
  2400. tt_assert(r == 0);
  2401. tt_int_op(guard->state, OP_EQ, GUARD_CIRC_STATE_USABLE_ON_COMPLETION);
  2402. g = entry_guard_handle_get(guard->guard);
  2403. make_guard_confirmed(gs, g);
  2404. tt_int_op(g->is_primary, OP_EQ, 1);
  2405. entry_guard_failed(&guard);
  2406. circuit_guard_state_free(guard);
  2407. tt_int_op(g->is_reachable, OP_EQ, GUARD_REACHABLE_NO);
  2408. guard = NULL;
  2409. node = NULL;
  2410. }
  2411. /* Get another guard that we might try. */
  2412. r = entry_guard_pick_for_circuit(gs, NULL, &node, &guard);
  2413. tt_assert(node);
  2414. tt_assert(guard);
  2415. tt_assert(r == 0);
  2416. tt_int_op(guard->state, OP_EQ, GUARD_CIRC_STATE_USABLE_IF_NO_BETTER_GUARD);
  2417. g = entry_guard_handle_get(guard->guard);
  2418. tt_int_op(g->is_primary, OP_EQ, 0);
  2419. tt_assert(entry_guards_all_primary_guards_are_down(gs));
  2420. /* And an hour has passed ... */
  2421. update_approx_time(start + 3600);
  2422. /* Say that guard has succeeded! */
  2423. u = entry_guard_succeeded(&guard);
  2424. tt_int_op(u, OP_EQ, GUARD_MAYBE_USABLE_LATER);
  2425. tt_int_op(guard->state, OP_EQ, GUARD_CIRC_STATE_WAITING_FOR_BETTER_GUARD);
  2426. g = entry_guard_handle_get(guard->guard);
  2427. /* The primary guards should have been marked up! */
  2428. SMARTLIST_FOREACH(gs->primary_entry_guards, entry_guard_t *, pg, {
  2429. tt_int_op(pg->is_primary, OP_EQ, 1);
  2430. tt_ptr_op(g, OP_NE, pg);
  2431. tt_int_op(pg->is_reachable, OP_EQ, GUARD_REACHABLE_MAYBE);
  2432. });
  2433. /* Have a circuit to a primary guard succeed. */
  2434. r = entry_guard_pick_for_circuit(gs, NULL, &node, &guard2);
  2435. tt_assert(r == 0);
  2436. tt_int_op(guard2->state, OP_EQ, GUARD_CIRC_STATE_USABLE_ON_COMPLETION);
  2437. u = entry_guard_succeeded(&guard2);
  2438. tt_assert(u == GUARD_USABLE_NOW);
  2439. tt_int_op(guard2->state, OP_EQ, GUARD_CIRC_STATE_COMPLETE);
  2440. tt_assert(! entry_guards_all_primary_guards_are_down(gs));
  2441. done:
  2442. guard_selection_free(gs);
  2443. circuit_guard_state_free(guard);
  2444. circuit_guard_state_free(guard2);
  2445. }
  2446. static void
  2447. test_entry_guard_select_and_cancel(void *arg)
  2448. {
  2449. (void) arg;
  2450. const int N_PRIMARY = DFLT_N_PRIMARY_GUARDS;
  2451. int i,r;
  2452. const node_t *node = NULL;
  2453. circuit_guard_state_t *guard;
  2454. guard_selection_t *gs = guard_selection_new("default", GS_TYPE_NORMAL);
  2455. entry_guard_t *g;
  2456. /* Once more, we mark all the primary guards down. */
  2457. entry_guards_note_internet_connectivity(gs);
  2458. for (i = 0; i < N_PRIMARY; ++i) {
  2459. r = entry_guard_pick_for_circuit(gs, NULL, &node, &guard);
  2460. tt_int_op(guard->state, OP_EQ, GUARD_CIRC_STATE_USABLE_ON_COMPLETION);
  2461. g = entry_guard_handle_get(guard->guard);
  2462. tt_int_op(g->is_primary, OP_EQ, 1);
  2463. tt_int_op(g->is_pending, OP_EQ, 0);
  2464. make_guard_confirmed(gs, g);
  2465. entry_guard_failed(&guard);
  2466. circuit_guard_state_free(guard);
  2467. guard = NULL;
  2468. node = NULL;
  2469. }
  2470. tt_assert(entry_guards_all_primary_guards_are_down(gs));
  2471. /* Now get another guard we could try... */
  2472. r = entry_guard_pick_for_circuit(gs, NULL, &node, &guard);
  2473. tt_assert(node);
  2474. tt_assert(guard);
  2475. tt_assert(r == 0);
  2476. tt_int_op(guard->state, OP_EQ, GUARD_CIRC_STATE_USABLE_IF_NO_BETTER_GUARD);
  2477. g = entry_guard_handle_get(guard->guard);
  2478. tt_int_op(g->is_primary, OP_EQ, 0);
  2479. tt_int_op(g->is_pending, OP_EQ, 1);
  2480. /* Whoops! We should never have asked for this guard. Cancel the request! */
  2481. entry_guard_cancel(&guard);
  2482. tt_assert(guard == NULL);
  2483. tt_int_op(g->is_primary, OP_EQ, 0);
  2484. tt_int_op(g->is_pending, OP_EQ, 0);
  2485. done:
  2486. guard_selection_free(gs);
  2487. circuit_guard_state_free(guard);
  2488. }
  2489. /* Unit test setup function: Create a fake network, and set everything up
  2490. * for testing the upgrade-a-waiting-circuit code. */
  2491. typedef struct {
  2492. guard_selection_t *gs;
  2493. time_t start;
  2494. circuit_guard_state_t *guard1_state;
  2495. circuit_guard_state_t *guard2_state;
  2496. entry_guard_t *guard1;
  2497. entry_guard_t *guard2;
  2498. origin_circuit_t *circ1;
  2499. origin_circuit_t *circ2;
  2500. smartlist_t *all_origin_circuits;
  2501. } upgrade_circuits_data_t;
  2502. static void *
  2503. upgrade_circuits_setup(const struct testcase_t *testcase)
  2504. {
  2505. upgrade_circuits_data_t *data = tor_malloc_zero(sizeof(*data));
  2506. guard_selection_t *gs = data->gs =
  2507. guard_selection_new("default", GS_TYPE_NORMAL);
  2508. circuit_guard_state_t *guard;
  2509. const node_t *node;
  2510. entry_guard_t *g;
  2511. int i;
  2512. const int N_PRIMARY = DFLT_N_PRIMARY_GUARDS;
  2513. const char *argument = testcase->setup_data;
  2514. const int make_circ1_succeed = strstr(argument, "c1-done") != NULL;
  2515. const int make_circ2_succeed = strstr(argument, "c2-done") != NULL;
  2516. big_fake_network_setup(testcase);
  2517. /* We're going to set things up in a state where a circuit will be ready to
  2518. * be upgraded. Each test can make a single change (or not) that should
  2519. * block the upgrade.
  2520. */
  2521. /* First, make all the primary guards confirmed, and down. */
  2522. data->start = approx_time();
  2523. entry_guards_note_internet_connectivity(gs);
  2524. for (i = 0; i < N_PRIMARY; ++i) {
  2525. entry_guard_pick_for_circuit(gs, NULL, &node, &guard);
  2526. g = entry_guard_handle_get(guard->guard);
  2527. make_guard_confirmed(gs, g);
  2528. entry_guard_failed(&guard);
  2529. circuit_guard_state_free(guard);
  2530. }
  2531. /* Grab another couple of guards */
  2532. data->all_origin_circuits = smartlist_new();
  2533. update_approx_time(data->start + 27);
  2534. entry_guard_pick_for_circuit(gs, NULL, &node, &data->guard1_state);
  2535. origin_circuit_t *circ;
  2536. data->circ1 = circ = origin_circuit_new();
  2537. circ->base_.purpose = CIRCUIT_PURPOSE_C_GENERAL;
  2538. circ->guard_state = data->guard1_state;
  2539. smartlist_add(data->all_origin_circuits, circ);
  2540. update_approx_time(data->start + 30);
  2541. entry_guard_pick_for_circuit(gs, NULL, &node, &data->guard2_state);
  2542. data->circ2 = circ = origin_circuit_new();
  2543. circ->base_.purpose = CIRCUIT_PURPOSE_C_GENERAL;
  2544. circ->guard_state = data->guard2_state;
  2545. smartlist_add(data->all_origin_circuits, circ);
  2546. data->guard1 = entry_guard_handle_get(data->guard1_state->guard);
  2547. data->guard2 = entry_guard_handle_get(data->guard2_state->guard);
  2548. tor_assert(data->guard1 != data->guard2);
  2549. tor_assert(data->guard1_state->state ==
  2550. GUARD_CIRC_STATE_USABLE_IF_NO_BETTER_GUARD);
  2551. tor_assert(data->guard2_state->state ==
  2552. GUARD_CIRC_STATE_USABLE_IF_NO_BETTER_GUARD);
  2553. guard_usable_t r;
  2554. update_approx_time(data->start + 32);
  2555. if (make_circ1_succeed) {
  2556. r = entry_guard_succeeded(&data->guard1_state);
  2557. tor_assert(r == GUARD_MAYBE_USABLE_LATER);
  2558. tor_assert(data->guard1_state->state ==
  2559. GUARD_CIRC_STATE_WAITING_FOR_BETTER_GUARD);
  2560. }
  2561. update_approx_time(data->start + 33);
  2562. if (make_circ2_succeed) {
  2563. r = entry_guard_succeeded(&data->guard2_state);
  2564. tor_assert(r == GUARD_MAYBE_USABLE_LATER);
  2565. tor_assert(data->guard2_state->state ==
  2566. GUARD_CIRC_STATE_WAITING_FOR_BETTER_GUARD);
  2567. }
  2568. return data;
  2569. }
  2570. static int
  2571. upgrade_circuits_cleanup(const struct testcase_t *testcase, void *ptr)
  2572. {
  2573. upgrade_circuits_data_t *data = ptr;
  2574. // circuit_guard_state_free(data->guard1_state); // held in circ1
  2575. // circuit_guard_state_free(data->guard2_state); // held in circ2
  2576. guard_selection_free(data->gs);
  2577. smartlist_free(data->all_origin_circuits);
  2578. circuit_free(TO_CIRCUIT(data->circ1));
  2579. circuit_free(TO_CIRCUIT(data->circ2));
  2580. tor_free(data);
  2581. return big_fake_network_cleanup(testcase, ptr);
  2582. }
  2583. static void
  2584. test_entry_guard_upgrade_a_circuit(void *arg)
  2585. {
  2586. upgrade_circuits_data_t *data = arg;
  2587. /* This is the easy case: we have no COMPLETED circuits, all the
  2588. * primary guards are down, we have two WAITING circuits: one will
  2589. * get upgraded to COMPLETED! (The one that started first.)
  2590. */
  2591. /* XXXX prop271 -- perhaps the one that started first should
  2592. * also wind up in confirmed_entry_guards earlier?
  2593. */
  2594. smartlist_t *result = smartlist_new();
  2595. int r;
  2596. r = entry_guards_upgrade_waiting_circuits(data->gs,
  2597. data->all_origin_circuits,
  2598. result);
  2599. tt_int_op(r, OP_EQ, 1);
  2600. tt_int_op(smartlist_len(result), OP_EQ, 1);
  2601. origin_circuit_t *oc = smartlist_get(result, 0);
  2602. /* circ1 was started first, so we'll get told to ugrade it... */
  2603. tt_ptr_op(oc, OP_EQ, data->circ1);
  2604. /* And the guard state should be complete */
  2605. tt_ptr_op(data->guard1_state, OP_NE, NULL);
  2606. tt_int_op(data->guard1_state->state, OP_EQ, GUARD_CIRC_STATE_COMPLETE);
  2607. done:
  2608. smartlist_free(result);
  2609. }
  2610. static void
  2611. test_entry_guard_upgrade_blocked_by_live_primary_guards(void *arg)
  2612. {
  2613. upgrade_circuits_data_t *data = arg;
  2614. /* If any primary guards might be up, we can't upgrade any waiting
  2615. * circuits.
  2616. */
  2617. mark_primary_guards_maybe_reachable(data->gs);
  2618. smartlist_t *result = smartlist_new();
  2619. int r;
  2620. setup_capture_of_logs(LOG_DEBUG);
  2621. r = entry_guards_upgrade_waiting_circuits(data->gs,
  2622. data->all_origin_circuits,
  2623. result);
  2624. tt_int_op(r, OP_EQ, 0);
  2625. tt_int_op(smartlist_len(result), OP_EQ, 0);
  2626. expect_log_msg_containing("not all primary guards were definitely down.");
  2627. done:
  2628. teardown_capture_of_logs();
  2629. smartlist_free(result);
  2630. }
  2631. static void
  2632. test_entry_guard_upgrade_blocked_by_lack_of_waiting_circuits(void *arg)
  2633. {
  2634. upgrade_circuits_data_t *data = arg;
  2635. /* If no circuits are waiting, we can't upgrade anything. (The test
  2636. * setup in this case was told not to make any of the circuits "waiting".)
  2637. */
  2638. smartlist_t *result = smartlist_new();
  2639. int r;
  2640. setup_capture_of_logs(LOG_DEBUG);
  2641. r = entry_guards_upgrade_waiting_circuits(data->gs,
  2642. data->all_origin_circuits,
  2643. result);
  2644. tt_int_op(r, OP_EQ, 0);
  2645. tt_int_op(smartlist_len(result), OP_EQ, 0);
  2646. expect_log_msg_containing("Considered upgrading guard-stalled circuits, "
  2647. "but didn't find any.");
  2648. done:
  2649. teardown_capture_of_logs();
  2650. smartlist_free(result);
  2651. }
  2652. static void
  2653. test_entry_guard_upgrade_blocked_by_better_circ_complete(void *arg)
  2654. {
  2655. upgrade_circuits_data_t *data = arg;
  2656. /* We'll run through the logic of upgrade_a_circuit below...
  2657. * and then try again to make sure that circ2 isn't also upgraded.
  2658. */
  2659. smartlist_t *result = smartlist_new();
  2660. int r;
  2661. r = entry_guards_upgrade_waiting_circuits(data->gs,
  2662. data->all_origin_circuits,
  2663. result);
  2664. tt_int_op(r, OP_EQ, 1);
  2665. tt_int_op(smartlist_len(result), OP_EQ, 1);
  2666. origin_circuit_t *oc = smartlist_get(result, 0);
  2667. tt_ptr_op(oc, OP_EQ, data->circ1);
  2668. tt_ptr_op(data->guard1_state, OP_NE, NULL);
  2669. tt_int_op(data->guard1_state->state, OP_EQ, GUARD_CIRC_STATE_COMPLETE);
  2670. /* Now, try again. Make sure that circ2 isn't upgraded. */
  2671. smartlist_clear(result);
  2672. setup_capture_of_logs(LOG_DEBUG);
  2673. r = entry_guards_upgrade_waiting_circuits(data->gs,
  2674. data->all_origin_circuits,
  2675. result);
  2676. tt_int_op(r, OP_EQ, 0);
  2677. tt_int_op(smartlist_len(result), OP_EQ, 0);
  2678. expect_log_msg_containing("At least one complete circuit had higher "
  2679. "priority, so not upgrading.");
  2680. done:
  2681. teardown_capture_of_logs();
  2682. smartlist_free(result);
  2683. }
  2684. static void
  2685. test_entry_guard_upgrade_not_blocked_by_restricted_circ_complete(void *arg)
  2686. {
  2687. upgrade_circuits_data_t *data = arg;
  2688. /* Once more, let circ1 become complete. But this time, we'll claim
  2689. * that circ2 was restricted to not use the same guard as circ1. */
  2690. data->guard2_state->restrictions =
  2691. tor_malloc_zero(sizeof(entry_guard_restriction_t));
  2692. memcpy(data->guard2_state->restrictions->exclude_id,
  2693. data->guard1->identity, DIGEST_LEN);
  2694. smartlist_t *result = smartlist_new();
  2695. int r;
  2696. r = entry_guards_upgrade_waiting_circuits(data->gs,
  2697. data->all_origin_circuits,
  2698. result);
  2699. tt_int_op(r, OP_EQ, 1);
  2700. tt_int_op(smartlist_len(result), OP_EQ, 1);
  2701. origin_circuit_t *oc = smartlist_get(result, 0);
  2702. tt_ptr_op(oc, OP_EQ, data->circ1);
  2703. tt_ptr_op(data->guard1_state, OP_NE, NULL);
  2704. tt_int_op(data->guard1_state->state, OP_EQ, GUARD_CIRC_STATE_COMPLETE);
  2705. /* Now, we try again. Since circ2 has a restriction that circ1 doesn't obey,
  2706. * circ2 _is_ eligible for upgrade. */
  2707. smartlist_clear(result);
  2708. r = entry_guards_upgrade_waiting_circuits(data->gs,
  2709. data->all_origin_circuits,
  2710. result);
  2711. tt_int_op(r, OP_EQ, 1);
  2712. tt_int_op(smartlist_len(result), OP_EQ, 1);
  2713. origin_circuit_t *oc2 = smartlist_get(result, 0);
  2714. tt_ptr_op(oc2, OP_EQ, data->circ2);
  2715. done:
  2716. smartlist_free(result);
  2717. }
  2718. static void
  2719. test_entry_guard_upgrade_not_blocked_by_worse_circ_complete(void *arg)
  2720. {
  2721. upgrade_circuits_data_t *data = arg;
  2722. smartlist_t *result = smartlist_new();
  2723. /* here we manually make circ2 COMPLETE, and make sure that circ1
  2724. * gets made complete anyway, since guard1 has higher priority
  2725. */
  2726. update_approx_time(data->start + 300);
  2727. data->guard2_state->state = GUARD_CIRC_STATE_COMPLETE;
  2728. data->guard2_state->state_set_at = approx_time();
  2729. update_approx_time(data->start + 301);
  2730. /* Now, try again. Make sure that circ1 is approved. */
  2731. int r;
  2732. r = entry_guards_upgrade_waiting_circuits(data->gs,
  2733. data->all_origin_circuits,
  2734. result);
  2735. tt_int_op(r, OP_EQ, 1);
  2736. tt_int_op(smartlist_len(result), OP_EQ, 1);
  2737. origin_circuit_t *oc = smartlist_get(result, 0);
  2738. tt_ptr_op(oc, OP_EQ, data->circ1);
  2739. done:
  2740. smartlist_free(result);
  2741. }
  2742. static void
  2743. test_entry_guard_upgrade_blocked_by_better_circ_pending(void *arg)
  2744. {
  2745. upgrade_circuits_data_t *data = arg;
  2746. /* circ2 is done, but circ1 is still pending. Since circ1 is better,
  2747. * we won't upgrade circ2. */
  2748. /* XXXX Prop271 -- this is a kludge. I'm making sure circ1 _is_ better,
  2749. * by messing with the guards' confirmed_idx */
  2750. make_guard_confirmed(data->gs, data->guard1);
  2751. {
  2752. int tmp;
  2753. tmp = data->guard1->confirmed_idx;
  2754. data->guard1->confirmed_idx = data->guard2->confirmed_idx;
  2755. data->guard2->confirmed_idx = tmp;
  2756. }
  2757. smartlist_t *result = smartlist_new();
  2758. setup_capture_of_logs(LOG_DEBUG);
  2759. int r;
  2760. r = entry_guards_upgrade_waiting_circuits(data->gs,
  2761. data->all_origin_circuits,
  2762. result);
  2763. tt_int_op(r, OP_EQ, 0);
  2764. tt_int_op(smartlist_len(result), OP_EQ, 0);
  2765. expect_log_msg_containing("but 1 pending circuit(s) had higher guard "
  2766. "priority, so not upgrading.");
  2767. done:
  2768. teardown_capture_of_logs();
  2769. smartlist_free(result);
  2770. }
  2771. static void
  2772. test_entry_guard_upgrade_not_blocked_by_restricted_circ_pending(void *arg)
  2773. {
  2774. upgrade_circuits_data_t *data = arg;
  2775. /* circ2 is done, but circ1 is still pending. But when there is a
  2776. restriction on circ2 that circ1 can't satisfy, circ1 can't block
  2777. circ2. */
  2778. /* XXXX Prop271 -- this is a kludge. I'm making sure circ1 _is_ better,
  2779. * by messing with the guards' confirmed_idx */
  2780. make_guard_confirmed(data->gs, data->guard1);
  2781. {
  2782. int tmp;
  2783. tmp = data->guard1->confirmed_idx;
  2784. data->guard1->confirmed_idx = data->guard2->confirmed_idx;
  2785. data->guard2->confirmed_idx = tmp;
  2786. }
  2787. data->guard2_state->restrictions =
  2788. tor_malloc_zero(sizeof(entry_guard_restriction_t));
  2789. memcpy(data->guard2_state->restrictions->exclude_id,
  2790. data->guard1->identity, DIGEST_LEN);
  2791. smartlist_t *result = smartlist_new();
  2792. int r;
  2793. r = entry_guards_upgrade_waiting_circuits(data->gs,
  2794. data->all_origin_circuits,
  2795. result);
  2796. tt_int_op(r, OP_EQ, 1);
  2797. tt_int_op(smartlist_len(result), OP_EQ, 1);
  2798. origin_circuit_t *oc = smartlist_get(result, 0);
  2799. tt_ptr_op(oc, OP_EQ, data->circ2);
  2800. done:
  2801. smartlist_free(result);
  2802. }
  2803. static void
  2804. test_entry_guard_upgrade_not_blocked_by_worse_circ_pending(void *arg)
  2805. {
  2806. upgrade_circuits_data_t *data = arg;
  2807. /* circ1 is done, but circ2 is still pending. Since circ1 is better,
  2808. * we will upgrade it. */
  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->circ1);
  2818. done:
  2819. smartlist_free(result);
  2820. }
  2821. static void
  2822. test_enty_guard_should_expire_waiting(void *arg)
  2823. {
  2824. (void)arg;
  2825. circuit_guard_state_t *fake_state = tor_malloc_zero(sizeof(*fake_state));
  2826. /* We'll leave "guard" unset -- it won't matter here. */
  2827. /* No state? Can't expire. */
  2828. tt_assert(! entry_guard_state_should_expire(NULL));
  2829. /* Let's try one that expires. */
  2830. fake_state->state = GUARD_CIRC_STATE_WAITING_FOR_BETTER_GUARD;
  2831. fake_state->state_set_at =
  2832. approx_time() - DFLT_NONPRIMARY_GUARD_IDLE_TIMEOUT - 1;
  2833. tt_assert(entry_guard_state_should_expire(fake_state));
  2834. /* But it wouldn't expire if we changed the state. */
  2835. fake_state->state = GUARD_CIRC_STATE_USABLE_IF_NO_BETTER_GUARD;
  2836. tt_assert(! entry_guard_state_should_expire(fake_state));
  2837. /* And it wouldn't have expired a few seconds ago. */
  2838. fake_state->state = GUARD_CIRC_STATE_WAITING_FOR_BETTER_GUARD;
  2839. fake_state->state_set_at =
  2840. approx_time() - DFLT_NONPRIMARY_GUARD_IDLE_TIMEOUT + 5;
  2841. tt_assert(! entry_guard_state_should_expire(fake_state));
  2842. done:
  2843. tor_free(fake_state);
  2844. }
  2845. #ifdef ENABLE_LEGACY_GUARD_ALGORITHM
  2846. static const struct testcase_setup_t fake_network = {
  2847. fake_network_setup, fake_network_cleanup
  2848. };
  2849. #endif
  2850. static const struct testcase_setup_t big_fake_network = {
  2851. big_fake_network_setup, big_fake_network_cleanup
  2852. };
  2853. static const struct testcase_setup_t upgrade_circuits = {
  2854. upgrade_circuits_setup, upgrade_circuits_cleanup
  2855. };
  2856. #define BFN_TEST(name) \
  2857. { #name, test_entry_guard_ ## name, TT_FORK, &big_fake_network, NULL }
  2858. #define UPGRADE_TEST(name, arg) \
  2859. { #name, test_entry_guard_ ## name, TT_FORK, &upgrade_circuits, \
  2860. (void*)(arg) }
  2861. struct testcase_t entrynodes_tests[] = {
  2862. #ifdef ENABLE_LEGACY_GUARD_ALGORITHM
  2863. { "entry_is_time_to_retry", test_entry_is_time_to_retry,
  2864. TT_FORK, NULL, NULL },
  2865. { "choose_random_entry_no_guards", test_choose_random_entry_no_guards,
  2866. TT_FORK, &fake_network, NULL },
  2867. { "choose_random_entry_one_possible_guard",
  2868. test_choose_random_entry_one_possible_guard,
  2869. TT_FORK, &fake_network, NULL },
  2870. { "populate_live_entry_guards_1guard",
  2871. test_populate_live_entry_guards_1guard,
  2872. TT_FORK, &fake_network, NULL },
  2873. { "populate_live_entry_guards_3guards",
  2874. test_populate_live_entry_guards_3guards,
  2875. TT_FORK, &fake_network, NULL },
  2876. { "entry_guards_parse_state_simple",
  2877. test_entry_guards_parse_state_simple,
  2878. TT_FORK, &fake_network, NULL },
  2879. { "entry_guards_parse_state_pathbias",
  2880. test_entry_guards_parse_state_pathbias,
  2881. TT_FORK, &fake_network, NULL },
  2882. { "entry_guards_set_from_config",
  2883. test_entry_guards_set_from_config,
  2884. TT_FORK, &fake_network, NULL },
  2885. { "entry_is_live",
  2886. test_entry_is_live,
  2887. TT_FORK, &fake_network, NULL },
  2888. #endif
  2889. { "node_preferred_orport",
  2890. test_node_preferred_orport,
  2891. 0, NULL, NULL },
  2892. { "entry_guard_describe", test_entry_guard_describe, 0, NULL, NULL },
  2893. { "randomize_time", test_entry_guard_randomize_time, 0, NULL, NULL },
  2894. { "encode_for_state_minimal",
  2895. test_entry_guard_encode_for_state_minimal, 0, NULL, NULL },
  2896. { "encode_for_state_maximal",
  2897. test_entry_guard_encode_for_state_maximal, 0, NULL, NULL },
  2898. { "parse_from_state_minimal",
  2899. test_entry_guard_parse_from_state_minimal, 0, NULL, NULL },
  2900. { "parse_from_state_maximal",
  2901. test_entry_guard_parse_from_state_maximal, 0, NULL, NULL },
  2902. { "parse_from_state_failure",
  2903. test_entry_guard_parse_from_state_failure, 0, NULL, NULL },
  2904. { "parse_from_state_partial_failure",
  2905. test_entry_guard_parse_from_state_partial_failure, 0, NULL, NULL },
  2906. { "parse_from_state_full",
  2907. test_entry_guard_parse_from_state_full, TT_FORK, NULL, NULL },
  2908. { "parse_from_state_broken",
  2909. test_entry_guard_parse_from_state_broken, TT_FORK, NULL, NULL },
  2910. { "get_guard_selection_by_name",
  2911. test_entry_guard_get_guard_selection_by_name, TT_FORK, NULL, NULL },
  2912. BFN_TEST(choose_selection_initial),
  2913. BFN_TEST(add_single_guard),
  2914. BFN_TEST(node_filter),
  2915. BFN_TEST(expand_sample),
  2916. BFN_TEST(expand_sample_small_net),
  2917. BFN_TEST(update_from_consensus_status),
  2918. BFN_TEST(update_from_consensus_repair),
  2919. BFN_TEST(update_from_consensus_remove),
  2920. BFN_TEST(confirming_guards),
  2921. BFN_TEST(sample_reachable_filtered),
  2922. BFN_TEST(sample_reachable_filtered_empty),
  2923. BFN_TEST(retry_unreachable),
  2924. BFN_TEST(manage_primary),
  2925. { "guard_preferred", test_entry_guard_guard_preferred, TT_FORK, NULL, NULL },
  2926. BFN_TEST(select_for_circuit_no_confirmed),
  2927. BFN_TEST(select_for_circuit_confirmed),
  2928. BFN_TEST(select_for_circuit_highlevel_primary),
  2929. BFN_TEST(select_for_circuit_highlevel_confirm_other),
  2930. BFN_TEST(select_for_circuit_highlevel_primary_retry),
  2931. BFN_TEST(select_and_cancel),
  2932. UPGRADE_TEST(upgrade_a_circuit, "c1-done c2-done"),
  2933. UPGRADE_TEST(upgrade_blocked_by_live_primary_guards, "c1-done c2-done"),
  2934. UPGRADE_TEST(upgrade_blocked_by_lack_of_waiting_circuits, ""),
  2935. UPGRADE_TEST(upgrade_blocked_by_better_circ_complete, "c1-done c2-done"),
  2936. UPGRADE_TEST(upgrade_not_blocked_by_restricted_circ_complete,
  2937. "c1-done c2-done"),
  2938. UPGRADE_TEST(upgrade_not_blocked_by_worse_circ_complete, "c1-done c2-done"),
  2939. UPGRADE_TEST(upgrade_blocked_by_better_circ_pending, "c2-done"),
  2940. UPGRADE_TEST(upgrade_not_blocked_by_restricted_circ_pending,
  2941. "c2-done"),
  2942. UPGRADE_TEST(upgrade_not_blocked_by_worse_circ_pending, "c1-done"),
  2943. { "should_expire_waiting", test_enty_guard_should_expire_waiting, TT_FORK,
  2944. NULL, NULL },
  2945. END_OF_TESTCASES
  2946. };