test_entrynodes.c 104 KB

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