test_entrynodes.c 105 KB

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