test_entrynodes.c 103 KB

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