test_entrynodes.c 105 KB

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