test_entrynodes.c 103 KB

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