test_entrynodes.c 97 KB

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