test_entrynodes.c 104 KB

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