test_entrynodes.c 103 KB

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