test_entrynodes.c 98 KB

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