test_entrynodes.c 113 KB

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