test_entrynodes.c 97 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877
  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. tt_assert(n);
  968. n->is_possible_guard = 0;
  969. }
  970. update_approx_time(start + 30);
  971. {
  972. /* try this with no live networkstatus. Nothing should happen! */
  973. ns_tmp = dummy_consensus;
  974. dummy_consensus = NULL;
  975. sampled_guards_update_from_consensus(gs);
  976. tt_i64_op(smartlist_len(gs->sampled_entry_guards), OP_EQ, n_sampled_pre);
  977. tt_i64_op(num_reachable_filtered_guards(gs, NULL), OP_EQ, n_filtered_pre);
  978. /* put the networkstatus back. */
  979. dummy_consensus = ns_tmp;
  980. ns_tmp = NULL;
  981. }
  982. /* Now those guards should become unlisted, and drop off the filter, but
  983. * stay in the sample. */
  984. update_approx_time(start + 60);
  985. sampled_guards_update_from_consensus(gs);
  986. tt_i64_op(smartlist_len(gs->sampled_entry_guards), OP_EQ, n_sampled_pre);
  987. tt_i64_op(num_reachable_filtered_guards(gs, NULL), OP_EQ, n_filtered_pre-5);
  988. for (i = 0; i < 5; ++i) {
  989. entry_guard_t *g = smartlist_get(gs->sampled_entry_guards, i);
  990. tt_assert(! g->currently_listed);
  991. tt_i64_op(g->unlisted_since_date, OP_EQ, start+60);
  992. }
  993. for (i = 5; i < n_sampled_pre; ++i) {
  994. entry_guard_t *g = smartlist_get(gs->sampled_entry_guards, i);
  995. tt_assert(g->currently_listed);
  996. tt_i64_op(g->unlisted_since_date, OP_EQ, 0);
  997. }
  998. /* Now re-list one, and remove one completely. */
  999. {
  1000. entry_guard_t *g = smartlist_get(gs->sampled_entry_guards, 0);
  1001. node_t *n = (node_t*) bfn_mock_node_get_by_id(g->identity);
  1002. tt_assert(n);
  1003. n->is_possible_guard = 1;
  1004. }
  1005. {
  1006. /* try removing the node, to make sure we don't crash on an absent node
  1007. */
  1008. entry_guard_t *g = smartlist_get(gs->sampled_entry_guards, 5);
  1009. node_t *n = (node_t*) bfn_mock_node_get_by_id(g->identity);
  1010. tt_assert(n);
  1011. smartlist_remove(big_fake_net_nodes, n);
  1012. tor_free(n->rs);
  1013. tor_free(n->md);
  1014. tor_free(n);
  1015. }
  1016. update_approx_time(start + 300);
  1017. sampled_guards_update_from_consensus(gs);
  1018. /* guards 1..5 are now unlisted; 0,6,7.. are listed. */
  1019. tt_i64_op(smartlist_len(gs->sampled_entry_guards), OP_EQ, n_sampled_pre);
  1020. for (i = 1; i < 6; ++i) {
  1021. entry_guard_t *g = smartlist_get(gs->sampled_entry_guards, i);
  1022. tt_assert(! g->currently_listed);
  1023. if (i == 5)
  1024. tt_i64_op(g->unlisted_since_date, OP_EQ, start+300);
  1025. else
  1026. tt_i64_op(g->unlisted_since_date, OP_EQ, start+60);
  1027. }
  1028. for (i = 0; i < n_sampled_pre; i = (!i) ? 6 : i+1) { /* 0,6,7,8, ... */
  1029. entry_guard_t *g = smartlist_get(gs->sampled_entry_guards, i);
  1030. tt_assert(g->currently_listed);
  1031. tt_i64_op(g->unlisted_since_date, OP_EQ, 0);
  1032. }
  1033. done:
  1034. tor_free(ns_tmp); /* in case we couldn't put it back */
  1035. guard_selection_free(gs);
  1036. UNMOCK(randomize_time);
  1037. }
  1038. static void
  1039. test_entry_guard_update_from_consensus_repair(void *arg)
  1040. {
  1041. /* Here we'll make sure that our code to repair the unlisted-since
  1042. * times is correct. */
  1043. (void)arg;
  1044. int i;
  1045. time_t start = approx_time();
  1046. guard_selection_t *gs = guard_selection_new("default", GS_TYPE_NORMAL);
  1047. /* Don't randomly backdate stuff; it will make correctness harder to check.*/
  1048. MOCK(randomize_time, mock_randomize_time_no_randomization);
  1049. /* First, sample some guards. */
  1050. entry_guards_expand_sample(gs);
  1051. int n_sampled_pre = smartlist_len(gs->sampled_entry_guards);
  1052. int n_filtered_pre = num_reachable_filtered_guards(gs, NULL);
  1053. tt_i64_op(n_sampled_pre, OP_EQ, n_filtered_pre);
  1054. tt_i64_op(n_sampled_pre, OP_GT, 10);
  1055. /* Now corrupt the list a bit. Call some unlisted-since-never, and some
  1056. * listed-and-unlisted-since-a-time. */
  1057. update_approx_time(start + 300);
  1058. for (i = 0; i < 3; ++i) {
  1059. /* these will get a date. */
  1060. entry_guard_t *g = smartlist_get(gs->sampled_entry_guards, i);
  1061. node_t *n = (node_t*) bfn_mock_node_get_by_id(g->identity);
  1062. tt_assert(n);
  1063. n->is_possible_guard = 0;
  1064. g->currently_listed = 0;
  1065. }
  1066. for (i = 3; i < 6; ++i) {
  1067. /* these will become listed. */
  1068. entry_guard_t *g = smartlist_get(gs->sampled_entry_guards, i);
  1069. g->unlisted_since_date = start+100;
  1070. }
  1071. setup_full_capture_of_logs(LOG_WARN);
  1072. sampled_guards_update_from_consensus(gs);
  1073. expect_log_msg_containing(
  1074. "was listed, but with unlisted_since_date set");
  1075. expect_log_msg_containing(
  1076. "was unlisted, but with unlisted_since_date unset");
  1077. teardown_capture_of_logs();
  1078. tt_int_op(smartlist_len(gs->sampled_entry_guards), OP_EQ, n_sampled_pre);
  1079. tt_int_op(num_reachable_filtered_guards(gs, NULL), OP_EQ, n_filtered_pre-3);
  1080. for (i = 3; i < n_sampled_pre; ++i) {
  1081. /* these will become listed. */
  1082. entry_guard_t *g = smartlist_get(gs->sampled_entry_guards, i);
  1083. if (i < 3) {
  1084. tt_assert(! g->currently_listed);
  1085. tt_i64_op(g->unlisted_since_date, OP_EQ, start+300);
  1086. } else {
  1087. tt_assert(g->currently_listed);
  1088. tt_i64_op(g->unlisted_since_date, OP_EQ, 0);
  1089. }
  1090. }
  1091. done:
  1092. teardown_capture_of_logs();
  1093. guard_selection_free(gs);
  1094. UNMOCK(randomize_time);
  1095. }
  1096. static void
  1097. test_entry_guard_update_from_consensus_remove(void *arg)
  1098. {
  1099. /* Now let's check the logic responsible for removing guards from the
  1100. * sample entirely. */
  1101. (void)arg;
  1102. //int i;
  1103. guard_selection_t *gs = guard_selection_new("default", GS_TYPE_NORMAL);
  1104. smartlist_t *keep_ids = smartlist_new();
  1105. smartlist_t *remove_ids = smartlist_new();
  1106. /* Don't randomly backdate stuff; it will make correctness harder to check.*/
  1107. MOCK(randomize_time, mock_randomize_time_no_randomization);
  1108. /* First, sample some guards. */
  1109. entry_guards_expand_sample(gs);
  1110. int n_sampled_pre = smartlist_len(gs->sampled_entry_guards);
  1111. int n_filtered_pre = num_reachable_filtered_guards(gs, NULL);
  1112. tt_i64_op(n_sampled_pre, OP_EQ, n_filtered_pre);
  1113. tt_i64_op(n_sampled_pre, OP_GT, 10);
  1114. const time_t one_day_ago = approx_time() - 1*24*60*60;
  1115. const time_t one_year_ago = approx_time() - 365*24*60*60;
  1116. const time_t two_years_ago = approx_time() - 2*365*24*60*60;
  1117. /* 0: unlisted for a day. (keep this) */
  1118. {
  1119. entry_guard_t *g = smartlist_get(gs->sampled_entry_guards, 0);
  1120. node_t *n = (node_t*) bfn_mock_node_get_by_id(g->identity);
  1121. tt_assert(n);
  1122. n->is_possible_guard = 0;
  1123. g->currently_listed = 0;
  1124. g->unlisted_since_date = one_day_ago;
  1125. smartlist_add(keep_ids, tor_memdup(g->identity, 20));
  1126. }
  1127. /* 1: unlisted for a year. (remove this) */
  1128. {
  1129. entry_guard_t *g = smartlist_get(gs->sampled_entry_guards, 1);
  1130. node_t *n = (node_t*) bfn_mock_node_get_by_id(g->identity);
  1131. tt_assert(n);
  1132. n->is_possible_guard = 0;
  1133. g->currently_listed = 0;
  1134. g->unlisted_since_date = one_year_ago;
  1135. smartlist_add(remove_ids, tor_memdup(g->identity, 20));
  1136. }
  1137. /* 2: added a day ago, never confirmed. (keep this) */
  1138. {
  1139. entry_guard_t *g = smartlist_get(gs->sampled_entry_guards, 2);
  1140. g->sampled_on_date = one_day_ago;
  1141. smartlist_add(keep_ids, tor_memdup(g->identity, 20));
  1142. }
  1143. /* 3: added a year ago, never confirmed. (remove this) */
  1144. {
  1145. entry_guard_t *g = smartlist_get(gs->sampled_entry_guards, 3);
  1146. g->sampled_on_date = one_year_ago;
  1147. smartlist_add(remove_ids, tor_memdup(g->identity, 20));
  1148. }
  1149. /* 4: added two year ago, confirmed yesterday, primary. (keep this.) */
  1150. {
  1151. entry_guard_t *g = smartlist_get(gs->sampled_entry_guards, 4);
  1152. g->sampled_on_date = one_year_ago;
  1153. g->confirmed_on_date = one_day_ago;
  1154. g->confirmed_idx = 0;
  1155. g->is_primary = 1;
  1156. smartlist_add(gs->confirmed_entry_guards, g);
  1157. smartlist_add(gs->primary_entry_guards, g);
  1158. smartlist_add(keep_ids, tor_memdup(g->identity, 20));
  1159. }
  1160. /* 5: added two years ago, confirmed a year ago, primary. (remove this) */
  1161. {
  1162. entry_guard_t *g = smartlist_get(gs->sampled_entry_guards, 5);
  1163. g->sampled_on_date = two_years_ago;
  1164. g->confirmed_on_date = one_year_ago;
  1165. g->confirmed_idx = 1;
  1166. g->is_primary = 1;
  1167. smartlist_add(gs->confirmed_entry_guards, g);
  1168. smartlist_add(gs->primary_entry_guards, g);
  1169. smartlist_add(remove_ids, tor_memdup(g->identity, 20));
  1170. }
  1171. sampled_guards_update_from_consensus(gs);
  1172. /* Did we remove the right ones? */
  1173. SMARTLIST_FOREACH(keep_ids, uint8_t *, id, {
  1174. tt_assert(get_sampled_guard_with_id(gs, id) != NULL);
  1175. });
  1176. SMARTLIST_FOREACH(remove_ids, uint8_t *, id, {
  1177. tt_want(get_sampled_guard_with_id(gs, id) == NULL);
  1178. });
  1179. /* Did we remove the right number? */
  1180. tt_int_op(smartlist_len(gs->sampled_entry_guards), OP_EQ, n_sampled_pre - 3);
  1181. done:
  1182. guard_selection_free(gs);
  1183. UNMOCK(randomize_time);
  1184. SMARTLIST_FOREACH(keep_ids, char *, cp, tor_free(cp));
  1185. SMARTLIST_FOREACH(remove_ids, char *, cp, tor_free(cp));
  1186. smartlist_free(keep_ids);
  1187. smartlist_free(remove_ids);
  1188. }
  1189. static void
  1190. test_entry_guard_confirming_guards(void *arg)
  1191. {
  1192. (void)arg;
  1193. /* Now let's check the logic responsible for manipulating the list
  1194. * of confirmed guards */
  1195. guard_selection_t *gs = guard_selection_new("default", GS_TYPE_NORMAL);
  1196. MOCK(randomize_time, mock_randomize_time_no_randomization);
  1197. /* Create the sample. */
  1198. entry_guards_expand_sample(gs);
  1199. /* Confirm a few guards. */
  1200. time_t start = approx_time();
  1201. entry_guard_t *g1 = smartlist_get(gs->sampled_entry_guards, 0);
  1202. entry_guard_t *g2 = smartlist_get(gs->sampled_entry_guards, 1);
  1203. entry_guard_t *g3 = smartlist_get(gs->sampled_entry_guards, 8);
  1204. make_guard_confirmed(gs, g2);
  1205. update_approx_time(start + 10);
  1206. make_guard_confirmed(gs, g1);
  1207. make_guard_confirmed(gs, g3);
  1208. /* Were the correct dates and indices fed in? */
  1209. tt_int_op(g1->confirmed_idx, OP_EQ, 1);
  1210. tt_int_op(g2->confirmed_idx, OP_EQ, 0);
  1211. tt_int_op(g3->confirmed_idx, OP_EQ, 2);
  1212. tt_i64_op(g1->confirmed_on_date, OP_EQ, start+10);
  1213. tt_i64_op(g2->confirmed_on_date, OP_EQ, start);
  1214. tt_i64_op(g3->confirmed_on_date, OP_EQ, start+10);
  1215. tt_ptr_op(smartlist_get(gs->confirmed_entry_guards, 0), OP_EQ, g2);
  1216. tt_ptr_op(smartlist_get(gs->confirmed_entry_guards, 1), OP_EQ, g1);
  1217. tt_ptr_op(smartlist_get(gs->confirmed_entry_guards, 2), OP_EQ, g3);
  1218. /* Now make sure we can regenerate the confirmed_entry_guards list. */
  1219. smartlist_clear(gs->confirmed_entry_guards);
  1220. g2->confirmed_idx = 0;
  1221. g1->confirmed_idx = 10;
  1222. g3->confirmed_idx = 100;
  1223. entry_guards_update_confirmed(gs);
  1224. tt_int_op(g1->confirmed_idx, OP_EQ, 1);
  1225. tt_int_op(g2->confirmed_idx, OP_EQ, 0);
  1226. tt_int_op(g3->confirmed_idx, OP_EQ, 2);
  1227. tt_ptr_op(smartlist_get(gs->confirmed_entry_guards, 0), OP_EQ, g2);
  1228. tt_ptr_op(smartlist_get(gs->confirmed_entry_guards, 1), OP_EQ, g1);
  1229. tt_ptr_op(smartlist_get(gs->confirmed_entry_guards, 2), OP_EQ, g3);
  1230. /* Now make sure we can regenerate the confirmed_entry_guards list if
  1231. * the indices are messed up. */
  1232. g1->confirmed_idx = g2->confirmed_idx = g3->confirmed_idx = 999;
  1233. smartlist_clear(gs->confirmed_entry_guards);
  1234. entry_guards_update_confirmed(gs);
  1235. tt_int_op(g1->confirmed_idx, OP_GE, 0);
  1236. tt_int_op(g2->confirmed_idx, OP_GE, 0);
  1237. tt_int_op(g3->confirmed_idx, OP_GE, 0);
  1238. tt_int_op(g1->confirmed_idx, OP_LE, 2);
  1239. tt_int_op(g2->confirmed_idx, OP_LE, 2);
  1240. tt_int_op(g3->confirmed_idx, OP_LE, 2);
  1241. g1 = smartlist_get(gs->confirmed_entry_guards, 0);
  1242. g2 = smartlist_get(gs->confirmed_entry_guards, 1);
  1243. g3 = smartlist_get(gs->confirmed_entry_guards, 2);
  1244. tt_int_op(g1->confirmed_idx, OP_EQ, 0);
  1245. tt_int_op(g2->confirmed_idx, OP_EQ, 1);
  1246. tt_int_op(g3->confirmed_idx, OP_EQ, 2);
  1247. tt_assert(g1 != g2);
  1248. tt_assert(g1 != g3);
  1249. tt_assert(g2 != g3);
  1250. done:
  1251. UNMOCK(randomize_time);
  1252. guard_selection_free(gs);
  1253. }
  1254. static void
  1255. test_entry_guard_sample_reachable_filtered(void *arg)
  1256. {
  1257. (void)arg;
  1258. guard_selection_t *gs = guard_selection_new("default", GS_TYPE_NORMAL);
  1259. entry_guards_expand_sample(gs);
  1260. const int N = 10000;
  1261. bitarray_t *selected = NULL;
  1262. int i, j;
  1263. /* We've got a sampled list now; let's make one non-usable-filtered; some
  1264. * confirmed, some primary, some pending.
  1265. */
  1266. int n_guards = smartlist_len(gs->sampled_entry_guards);
  1267. tt_int_op(n_guards, OP_GT, 10);
  1268. entry_guard_t *g;
  1269. g = smartlist_get(gs->sampled_entry_guards, 0);
  1270. g->is_pending = 1;
  1271. g = smartlist_get(gs->sampled_entry_guards, 1);
  1272. make_guard_confirmed(gs, g);
  1273. g = smartlist_get(gs->sampled_entry_guards, 2);
  1274. g->is_primary = 1;
  1275. g = smartlist_get(gs->sampled_entry_guards, 3);
  1276. g->pb.path_bias_disabled = 1;
  1277. entry_guards_update_filtered_sets(gs);
  1278. gs->primary_guards_up_to_date = 1;
  1279. tt_int_op(num_reachable_filtered_guards(gs, NULL), OP_EQ, n_guards - 1);
  1280. tt_int_op(smartlist_len(gs->sampled_entry_guards), OP_EQ, n_guards);
  1281. // +1 since the one we made disabled will make another one get added.
  1282. ++n_guards;
  1283. /* Try a bunch of selections. */
  1284. const struct {
  1285. int flag; int idx;
  1286. } tests[] = {
  1287. { 0, -1 },
  1288. { SAMPLE_EXCLUDE_CONFIRMED, 1 },
  1289. { SAMPLE_EXCLUDE_PRIMARY|SAMPLE_NO_UPDATE_PRIMARY, 2 },
  1290. { SAMPLE_EXCLUDE_PENDING, 0 },
  1291. { -1, -1},
  1292. };
  1293. for (j = 0; tests[j].flag >= 0; ++j) {
  1294. selected = bitarray_init_zero(n_guards);
  1295. const int excluded_flags = tests[j].flag;
  1296. const int excluded_idx = tests[j].idx;
  1297. for (i = 0; i < N; ++i) {
  1298. g = sample_reachable_filtered_entry_guards(gs, NULL, excluded_flags);
  1299. tor_assert(g);
  1300. int pos = smartlist_pos(gs->sampled_entry_guards, g);
  1301. tt_int_op(smartlist_len(gs->sampled_entry_guards), OP_EQ, n_guards);
  1302. tt_int_op(pos, OP_GE, 0);
  1303. tt_int_op(pos, OP_LT, n_guards);
  1304. bitarray_set(selected, pos);
  1305. }
  1306. for (i = 0; i < n_guards; ++i) {
  1307. const int should_be_set = (i != excluded_idx &&
  1308. i != 3); // filtered out.
  1309. tt_int_op(!!bitarray_is_set(selected, i), OP_EQ, should_be_set);
  1310. }
  1311. bitarray_free(selected);
  1312. selected = NULL;
  1313. }
  1314. done:
  1315. guard_selection_free(gs);
  1316. bitarray_free(selected);
  1317. }
  1318. static void
  1319. test_entry_guard_sample_reachable_filtered_empty(void *arg)
  1320. {
  1321. (void)arg;
  1322. guard_selection_t *gs = guard_selection_new("default", GS_TYPE_NORMAL);
  1323. /* What if we try to sample from a set of 0? */
  1324. SMARTLIST_FOREACH(big_fake_net_nodes, node_t *, n,
  1325. n->is_possible_guard = 0);
  1326. entry_guard_t *g = sample_reachable_filtered_entry_guards(gs, NULL, 0);
  1327. tt_ptr_op(g, OP_EQ, NULL);
  1328. done:
  1329. guard_selection_free(gs);
  1330. }
  1331. static void
  1332. test_entry_guard_retry_unreachable(void *arg)
  1333. {
  1334. (void)arg;
  1335. guard_selection_t *gs = guard_selection_new("default", GS_TYPE_NORMAL);
  1336. entry_guards_expand_sample(gs);
  1337. /* Let's say that we have two guards, and they're down.
  1338. */
  1339. time_t start = approx_time();
  1340. entry_guard_t *g1 = smartlist_get(gs->sampled_entry_guards, 0);
  1341. entry_guard_t *g2 = smartlist_get(gs->sampled_entry_guards, 1);
  1342. entry_guard_t *g3 = smartlist_get(gs->sampled_entry_guards, 2);
  1343. g1->is_reachable = GUARD_REACHABLE_NO;
  1344. g2->is_reachable = GUARD_REACHABLE_NO;
  1345. g1->is_primary = 1;
  1346. g1->failing_since = g2->failing_since = start;
  1347. g1->last_tried_to_connect = g2->last_tried_to_connect = start;
  1348. /* Wait 5 minutes. Nothing will get retried. */
  1349. update_approx_time(start + 5 * 60);
  1350. entry_guard_consider_retry(g1);
  1351. entry_guard_consider_retry(g2);
  1352. entry_guard_consider_retry(g3); // just to make sure this doesn't crash.
  1353. tt_int_op(g1->is_reachable, OP_EQ, GUARD_REACHABLE_NO);
  1354. tt_int_op(g2->is_reachable, OP_EQ, GUARD_REACHABLE_NO);
  1355. tt_int_op(g3->is_reachable, OP_EQ, GUARD_REACHABLE_MAYBE);
  1356. /* After 30 min, the primary one gets retried */
  1357. update_approx_time(start + 35 * 60);
  1358. entry_guard_consider_retry(g1);
  1359. entry_guard_consider_retry(g2);
  1360. tt_int_op(g1->is_reachable, OP_EQ, GUARD_REACHABLE_MAYBE);
  1361. tt_int_op(g2->is_reachable, OP_EQ, GUARD_REACHABLE_NO);
  1362. g1->is_reachable = GUARD_REACHABLE_NO;
  1363. g1->last_tried_to_connect = start + 55*60;
  1364. /* After 1 hour, we'll retry the nonprimary one. */
  1365. update_approx_time(start + 61 * 60);
  1366. entry_guard_consider_retry(g1);
  1367. entry_guard_consider_retry(g2);
  1368. tt_int_op(g1->is_reachable, OP_EQ, GUARD_REACHABLE_NO);
  1369. tt_int_op(g2->is_reachable, OP_EQ, GUARD_REACHABLE_MAYBE);
  1370. g2->is_reachable = GUARD_REACHABLE_NO;
  1371. g2->last_tried_to_connect = start + 61*60;
  1372. /* And then the primary one again. */
  1373. update_approx_time(start + 66 * 60);
  1374. entry_guard_consider_retry(g1);
  1375. entry_guard_consider_retry(g2);
  1376. tt_int_op(g1->is_reachable, OP_EQ, GUARD_REACHABLE_MAYBE);
  1377. tt_int_op(g2->is_reachable, OP_EQ, GUARD_REACHABLE_NO);
  1378. done:
  1379. guard_selection_free(gs);
  1380. }
  1381. static void
  1382. test_entry_guard_manage_primary(void *arg)
  1383. {
  1384. (void)arg;
  1385. guard_selection_t *gs = guard_selection_new("default", GS_TYPE_NORMAL);
  1386. smartlist_t *prev_guards = smartlist_new();
  1387. /* If no guards are confirmed, we should pick a few reachable guards and
  1388. * call them all primary. But not confirmed.*/
  1389. entry_guards_update_primary(gs);
  1390. int n_primary = smartlist_len(gs->primary_entry_guards);
  1391. tt_int_op(n_primary, OP_GE, 1);
  1392. SMARTLIST_FOREACH(gs->primary_entry_guards, entry_guard_t *, g, {
  1393. tt_assert(g->is_primary);
  1394. tt_assert(g->confirmed_idx == -1);
  1395. });
  1396. /* Calling it a second time should leave the guards unchanged. */
  1397. smartlist_add_all(prev_guards, gs->primary_entry_guards);
  1398. entry_guards_update_primary(gs);
  1399. tt_int_op(smartlist_len(gs->primary_entry_guards), OP_EQ, n_primary);
  1400. SMARTLIST_FOREACH(gs->primary_entry_guards, entry_guard_t *, g, {
  1401. tt_ptr_op(g, OP_EQ, smartlist_get(prev_guards, g_sl_idx));
  1402. });
  1403. /* If we have one confirmed guard, that guards becomes the first primary
  1404. * guard, and the other primary guards get kept. */
  1405. /* find a non-primary guard... */
  1406. entry_guard_t *confirmed = NULL;
  1407. SMARTLIST_FOREACH(gs->sampled_entry_guards, entry_guard_t *, g, {
  1408. if (! g->is_primary) {
  1409. confirmed = g;
  1410. break;
  1411. }
  1412. });
  1413. tt_assert(confirmed);
  1414. /* make it confirmed. */
  1415. make_guard_confirmed(gs, confirmed);
  1416. /* update the list... */
  1417. smartlist_clear(prev_guards);
  1418. smartlist_add_all(prev_guards, gs->primary_entry_guards);
  1419. entry_guards_update_primary(gs);
  1420. /* and see what's primary now! */
  1421. tt_int_op(smartlist_len(gs->primary_entry_guards), OP_EQ, n_primary);
  1422. tt_ptr_op(smartlist_get(gs->primary_entry_guards, 0), OP_EQ, confirmed);
  1423. SMARTLIST_FOREACH(gs->primary_entry_guards, entry_guard_t *, g, {
  1424. tt_assert(g->is_primary);
  1425. if (g_sl_idx == 0)
  1426. continue;
  1427. tt_ptr_op(g, OP_EQ, smartlist_get(prev_guards, g_sl_idx - 1));
  1428. });
  1429. {
  1430. entry_guard_t *prev_last_guard = smartlist_get(prev_guards, n_primary-1);
  1431. tt_assert(! prev_last_guard->is_primary);
  1432. }
  1433. /* Calling it a fourth time should leave the guards unchanged. */
  1434. smartlist_clear(prev_guards);
  1435. smartlist_add_all(prev_guards, gs->primary_entry_guards);
  1436. entry_guards_update_primary(gs);
  1437. tt_int_op(smartlist_len(gs->primary_entry_guards), OP_EQ, n_primary);
  1438. SMARTLIST_FOREACH(gs->primary_entry_guards, entry_guard_t *, g, {
  1439. tt_ptr_op(g, OP_EQ, smartlist_get(prev_guards, g_sl_idx));
  1440. });
  1441. /* Do some dirinfo checks */
  1442. {
  1443. /* Check that we have all required dirinfo for the primaries (that's done
  1444. * in big_fake_network_setup()) */
  1445. char *dir_info_str =
  1446. guard_selection_get_err_str_if_dir_info_missing(gs, 0, 0, 0);
  1447. tt_assert(!dir_info_str);
  1448. /* Now artificially remove the first primary's descriptor and re-check */
  1449. entry_guard_t *first_primary;
  1450. first_primary = smartlist_get(gs->primary_entry_guards, 0);
  1451. /* Change the first primary's identity digest so that the mocked functions
  1452. * can't find its descriptor */
  1453. memset(first_primary->identity, 9, sizeof(first_primary->identity));
  1454. dir_info_str =guard_selection_get_err_str_if_dir_info_missing(gs, 1, 2, 3);
  1455. tt_str_op(dir_info_str, OP_EQ,
  1456. "We're missing descriptors for 1/2 of our primary entry guards "
  1457. "(total microdescriptors: 2/3).");
  1458. tor_free(dir_info_str);
  1459. }
  1460. done:
  1461. guard_selection_free(gs);
  1462. smartlist_free(prev_guards);
  1463. }
  1464. static void
  1465. test_entry_guard_guard_preferred(void *arg)
  1466. {
  1467. (void) arg;
  1468. entry_guard_t *g1 = tor_malloc_zero(sizeof(entry_guard_t));
  1469. entry_guard_t *g2 = tor_malloc_zero(sizeof(entry_guard_t));
  1470. g1->confirmed_idx = g2->confirmed_idx = -1;
  1471. g1->last_tried_to_connect = approx_time();
  1472. g2->last_tried_to_connect = approx_time();
  1473. tt_int_op(0, OP_EQ, entry_guard_has_higher_priority(g1, g1));
  1474. /* Neither is pending; priorities equal. */
  1475. tt_int_op(0, OP_EQ, entry_guard_has_higher_priority(g2, g1));
  1476. tt_int_op(0, OP_EQ, entry_guard_has_higher_priority(g1, g2));
  1477. /* If one is pending, the pending one has higher priority */
  1478. g1->is_pending = 1;
  1479. tt_int_op(1, OP_EQ, entry_guard_has_higher_priority(g1, g2));
  1480. tt_int_op(0, OP_EQ, entry_guard_has_higher_priority(g2, g1));
  1481. /* If both are pending, and last_tried_to_connect is equal:
  1482. priorities equal */
  1483. g2->is_pending = 1;
  1484. tt_int_op(0, OP_EQ, entry_guard_has_higher_priority(g2, g1));
  1485. tt_int_op(0, OP_EQ, entry_guard_has_higher_priority(g1, g2));
  1486. /* One had a connection that startied earlier: it has higher priority. */
  1487. g2->last_tried_to_connect -= 10;
  1488. tt_int_op(1, OP_EQ, entry_guard_has_higher_priority(g2, g1));
  1489. tt_int_op(0, OP_EQ, entry_guard_has_higher_priority(g1, g2));
  1490. /* Now, say that g1 is confirmed. It will get higher priority. */
  1491. g1->confirmed_idx = 5;
  1492. tt_int_op(0, OP_EQ, entry_guard_has_higher_priority(g2, g1));
  1493. tt_int_op(1, OP_EQ, entry_guard_has_higher_priority(g1, g2));
  1494. /* But if g2 was confirmed first, it will get priority */
  1495. g2->confirmed_idx = 2;
  1496. tt_int_op(1, OP_EQ, entry_guard_has_higher_priority(g2, g1));
  1497. tt_int_op(0, OP_EQ, entry_guard_has_higher_priority(g1, g2));
  1498. done:
  1499. tor_free(g1);
  1500. tor_free(g2);
  1501. }
  1502. static void
  1503. test_entry_guard_select_for_circuit_no_confirmed(void *arg)
  1504. {
  1505. /* Simpler cases: no gaurds are confirmed yet. */
  1506. (void)arg;
  1507. guard_selection_t *gs = guard_selection_new("default", GS_TYPE_NORMAL);
  1508. entry_guard_restriction_t *rst = NULL;
  1509. /* simple starting configuration */
  1510. entry_guards_update_primary(gs);
  1511. unsigned state = 9999;
  1512. entry_guard_t *g = select_entry_guard_for_circuit(gs, GUARD_USAGE_TRAFFIC,
  1513. NULL, &state);
  1514. tt_assert(g);
  1515. tt_assert(g->is_primary);
  1516. tt_int_op(g->confirmed_idx, OP_EQ, -1);
  1517. tt_uint_op(g->is_pending, OP_EQ, 0); // primary implies non-pending.
  1518. tt_uint_op(state, OP_EQ, GUARD_CIRC_STATE_USABLE_ON_COMPLETION);
  1519. tt_i64_op(g->last_tried_to_connect, OP_EQ, approx_time());
  1520. // If we do that again, we should get the same guard.
  1521. entry_guard_t *g2 = select_entry_guard_for_circuit(gs, GUARD_USAGE_TRAFFIC,
  1522. NULL, &state);
  1523. tt_ptr_op(g2, OP_EQ, g);
  1524. // if we mark that guard down, we should get a different primary guard.
  1525. // auto-retry it.
  1526. g->is_reachable = GUARD_REACHABLE_NO;
  1527. g->failing_since = approx_time() - 10;
  1528. g->last_tried_to_connect = approx_time() - 10;
  1529. state = 9999;
  1530. g2 = select_entry_guard_for_circuit(gs, GUARD_USAGE_TRAFFIC, NULL, &state);
  1531. tt_ptr_op(g2, OP_NE, g);
  1532. tt_assert(g2);
  1533. tt_assert(g2->is_primary);
  1534. tt_int_op(g2->confirmed_idx, OP_EQ, -1);
  1535. tt_uint_op(g2->is_pending, OP_EQ, 0); // primary implies non-pending.
  1536. tt_uint_op(state, OP_EQ, GUARD_CIRC_STATE_USABLE_ON_COMPLETION);
  1537. tt_i64_op(g2->last_tried_to_connect, OP_EQ, approx_time());
  1538. // If we say that the first primary guard was last tried a long time ago, we
  1539. // should get an automatic retry on it.
  1540. g->failing_since = approx_time() - 72*60*60;
  1541. g->last_tried_to_connect = approx_time() - 72*60*60;
  1542. state = 9999;
  1543. g2 = select_entry_guard_for_circuit(gs, GUARD_USAGE_TRAFFIC, NULL, &state);
  1544. tt_ptr_op(g2, OP_EQ, g);
  1545. tt_assert(g2);
  1546. tt_uint_op(state, OP_EQ, GUARD_CIRC_STATE_USABLE_ON_COMPLETION);
  1547. tt_i64_op(g2->last_tried_to_connect, OP_EQ, approx_time());
  1548. tt_int_op(g2->is_reachable, OP_EQ, GUARD_REACHABLE_MAYBE);
  1549. // And if we mark ALL the primary guards down, we should get another guard
  1550. // at random.
  1551. SMARTLIST_FOREACH(gs->primary_entry_guards, entry_guard_t *, guard, {
  1552. guard->is_reachable = GUARD_REACHABLE_NO;
  1553. guard->last_tried_to_connect = approx_time() - 5;
  1554. guard->failing_since = approx_time() - 30;
  1555. });
  1556. state = 9999;
  1557. g2 = select_entry_guard_for_circuit(gs, GUARD_USAGE_TRAFFIC, NULL, &state);
  1558. tt_assert(g2);
  1559. tt_assert(!g2->is_primary);
  1560. tt_int_op(g2->confirmed_idx, OP_EQ, -1);
  1561. tt_uint_op(g2->is_pending, OP_EQ, 1);
  1562. tt_uint_op(state, OP_EQ, GUARD_CIRC_STATE_USABLE_IF_NO_BETTER_GUARD);
  1563. tt_i64_op(g2->last_tried_to_connect, OP_EQ, approx_time());
  1564. tt_int_op(g2->is_reachable, OP_EQ, GUARD_REACHABLE_MAYBE);
  1565. // As a bonus, maybe we should be retrying the primary guards. Let's say so.
  1566. mark_primary_guards_maybe_reachable(gs);
  1567. SMARTLIST_FOREACH(gs->primary_entry_guards, entry_guard_t *, guard, {
  1568. tt_int_op(guard->is_reachable, OP_EQ, GUARD_REACHABLE_MAYBE);
  1569. tt_assert(guard->is_usable_filtered_guard == 1);
  1570. // no change to these fields.
  1571. tt_i64_op(guard->last_tried_to_connect, OP_EQ, approx_time() - 5);
  1572. tt_i64_op(guard->failing_since, OP_EQ, approx_time() - 30);
  1573. });
  1574. /* Let's try again and we should get the first primary guard again */
  1575. g = select_entry_guard_for_circuit(gs, GUARD_USAGE_TRAFFIC, NULL, &state);
  1576. tt_ptr_op(g, OP_EQ, smartlist_get(gs->primary_entry_guards, 0));
  1577. g2 = select_entry_guard_for_circuit(gs, GUARD_USAGE_TRAFFIC, NULL, &state);
  1578. tt_ptr_op(g2, OP_EQ, g);
  1579. /* But if we impose a restriction, we don't get the same guard */
  1580. rst = guard_create_exit_restriction((uint8_t*)g->identity);
  1581. g2 = select_entry_guard_for_circuit(gs, GUARD_USAGE_TRAFFIC, rst, &state);
  1582. tt_ptr_op(g2, OP_NE, g);
  1583. done:
  1584. guard_selection_free(gs);
  1585. entry_guard_restriction_free(rst);
  1586. }
  1587. static void
  1588. test_entry_guard_select_for_circuit_confirmed(void *arg)
  1589. {
  1590. /* Case 2: if all the primary guards are down, and there are more confirmed
  1591. guards, we use a confirmed guard. */
  1592. (void)arg;
  1593. int i;
  1594. entry_guard_restriction_t *rst = NULL;
  1595. guard_selection_t *gs = guard_selection_new("default", GS_TYPE_NORMAL);
  1596. const int N_CONFIRMED = 10;
  1597. /* slightly more complicated simple starting configuration */
  1598. entry_guards_update_primary(gs);
  1599. for (i = 0; i < N_CONFIRMED; ++i) {
  1600. entry_guard_t *guard = smartlist_get(gs->sampled_entry_guards, i);
  1601. make_guard_confirmed(gs, guard);
  1602. }
  1603. entry_guards_update_primary(gs); // rebuild the primary list.
  1604. unsigned state = 9999;
  1605. // As above, this gives us a primary guard.
  1606. entry_guard_t *g = select_entry_guard_for_circuit(gs, GUARD_USAGE_TRAFFIC,
  1607. NULL, &state);
  1608. tt_assert(g);
  1609. tt_assert(g->is_primary);
  1610. tt_int_op(g->confirmed_idx, OP_EQ, 0);
  1611. tt_uint_op(g->is_pending, OP_EQ, 0); // primary implies non-pending.
  1612. tt_uint_op(state, OP_EQ, GUARD_CIRC_STATE_USABLE_ON_COMPLETION);
  1613. tt_i64_op(g->last_tried_to_connect, OP_EQ, approx_time());
  1614. tt_ptr_op(g, OP_EQ, smartlist_get(gs->primary_entry_guards, 0));
  1615. // But if we mark all the primary guards down...
  1616. SMARTLIST_FOREACH(gs->primary_entry_guards, entry_guard_t *, guard, {
  1617. guard->last_tried_to_connect = approx_time();
  1618. entry_guards_note_guard_failure(gs, guard);
  1619. });
  1620. // ... we should get a confirmed guard.
  1621. state = 9999;
  1622. g = select_entry_guard_for_circuit(gs, GUARD_USAGE_TRAFFIC, NULL, &state);
  1623. tt_assert(g);
  1624. tt_assert(! g->is_primary);
  1625. tt_int_op(g->confirmed_idx, OP_EQ, smartlist_len(gs->primary_entry_guards));
  1626. tt_assert(g->is_pending);
  1627. tt_uint_op(state, OP_EQ, GUARD_CIRC_STATE_USABLE_IF_NO_BETTER_GUARD);
  1628. tt_i64_op(g->last_tried_to_connect, OP_EQ, approx_time());
  1629. // And if we try again, we should get a different confirmed guard, since
  1630. // that one is pending.
  1631. state = 9999;
  1632. entry_guard_t *g2 = select_entry_guard_for_circuit(gs, GUARD_USAGE_TRAFFIC,
  1633. NULL, &state);
  1634. tt_assert(g2);
  1635. tt_assert(! g2->is_primary);
  1636. tt_ptr_op(g2, OP_NE, g);
  1637. tt_int_op(g2->confirmed_idx, OP_EQ,
  1638. smartlist_len(gs->primary_entry_guards)+1);
  1639. tt_assert(g2->is_pending);
  1640. tt_uint_op(state, OP_EQ, GUARD_CIRC_STATE_USABLE_IF_NO_BETTER_GUARD);
  1641. tt_i64_op(g2->last_tried_to_connect, OP_EQ, approx_time());
  1642. // If we say that the next confirmed guard in order is excluded, and
  1643. // we disable EnforceDistinctSubnets, we get the guard AFTER the
  1644. // one we excluded.
  1645. get_options_mutable()->EnforceDistinctSubnets = 0;
  1646. g = smartlist_get(gs->confirmed_entry_guards,
  1647. smartlist_len(gs->primary_entry_guards)+2);
  1648. rst = guard_create_exit_restriction((uint8_t*)g->identity);
  1649. g2 = select_entry_guard_for_circuit(gs, GUARD_USAGE_TRAFFIC, rst, &state);
  1650. tt_ptr_op(g2, OP_NE, NULL);
  1651. tt_ptr_op(g2, OP_NE, g);
  1652. tt_int_op(g2->confirmed_idx, OP_EQ,
  1653. smartlist_len(gs->primary_entry_guards)+3);
  1654. // If we make every confirmed guard become pending then we start poking
  1655. // other guards.
  1656. const int n_remaining_confirmed =
  1657. N_CONFIRMED - 3 - smartlist_len(gs->primary_entry_guards);
  1658. for (i = 0; i < n_remaining_confirmed; ++i) {
  1659. g = select_entry_guard_for_circuit(gs, GUARD_USAGE_TRAFFIC, NULL, &state);
  1660. tt_int_op(g->confirmed_idx, OP_GE, 0);
  1661. tt_assert(g);
  1662. }
  1663. state = 9999;
  1664. g = select_entry_guard_for_circuit(gs, GUARD_USAGE_TRAFFIC, NULL, &state);
  1665. tt_assert(g);
  1666. tt_assert(g->is_pending);
  1667. tt_int_op(g->confirmed_idx, OP_EQ, -1);
  1668. // If we EnforceDistinctSubnets and apply a restriction, we get
  1669. // nothing, since we put all of the nodes in the same /16.
  1670. // Regression test for bug 22753/TROVE-2017-006.
  1671. get_options_mutable()->EnforceDistinctSubnets = 1;
  1672. g = smartlist_get(gs->confirmed_entry_guards, 0);
  1673. memcpy(rst->exclude_id, g->identity, DIGEST_LEN);
  1674. g2 = select_entry_guard_for_circuit(gs, GUARD_USAGE_TRAFFIC, rst, &state);
  1675. tt_ptr_op(g2, OP_EQ, NULL);
  1676. done:
  1677. guard_selection_free(gs);
  1678. entry_guard_restriction_free(rst);
  1679. }
  1680. static void
  1681. test_entry_guard_select_for_circuit_highlevel_primary(void *arg)
  1682. {
  1683. /* Play around with selecting primary guards for circuits and markign
  1684. * them up and down */
  1685. (void)arg;
  1686. guard_selection_t *gs = guard_selection_new("default", GS_TYPE_NORMAL);
  1687. time_t start = approx_time();
  1688. const node_t *node = NULL;
  1689. circuit_guard_state_t *guard = NULL;
  1690. entry_guard_t *g;
  1691. guard_usable_t u;
  1692. /*
  1693. * Make sure that the pick-for-circuit API basically works. We'll get
  1694. * a primary guard, so it'll be usable on completion.
  1695. */
  1696. int r = entry_guard_pick_for_circuit(gs, GUARD_USAGE_TRAFFIC, NULL,
  1697. &node, &guard);
  1698. tt_int_op(r, OP_EQ, 0);
  1699. tt_assert(node);
  1700. tt_assert(guard);
  1701. tt_int_op(guard->state, OP_EQ, GUARD_CIRC_STATE_USABLE_ON_COMPLETION);
  1702. g = entry_guard_handle_get(guard->guard);
  1703. tt_assert(g);
  1704. tt_mem_op(g->identity, OP_EQ, node->identity, DIGEST_LEN);
  1705. tt_int_op(g->is_primary, OP_EQ, 1);
  1706. tt_i64_op(g->last_tried_to_connect, OP_EQ, start);
  1707. tt_int_op(g->confirmed_idx, OP_EQ, -1);
  1708. /* Call that circuit successful. */
  1709. update_approx_time(start+15);
  1710. u = entry_guard_succeeded(&guard);
  1711. tt_int_op(u, OP_EQ, GUARD_USABLE_NOW); /* We can use it now. */
  1712. tt_assert(guard);
  1713. tt_int_op(guard->state, OP_EQ, GUARD_CIRC_STATE_COMPLETE);
  1714. g = entry_guard_handle_get(guard->guard);
  1715. tt_assert(g);
  1716. tt_int_op(g->is_reachable, OP_EQ, GUARD_REACHABLE_YES);
  1717. tt_int_op(g->confirmed_idx, OP_EQ, 0);
  1718. circuit_guard_state_free(guard);
  1719. guard = NULL;
  1720. node = NULL;
  1721. g = NULL;
  1722. /* Try again. We'll also get a primary guard this time. (The same one,
  1723. in fact.) But this time, we'll say the connection has failed. */
  1724. update_approx_time(start+35);
  1725. r = entry_guard_pick_for_circuit(gs, GUARD_USAGE_TRAFFIC, NULL,
  1726. &node, &guard);
  1727. tt_int_op(r, OP_EQ, 0);
  1728. tt_assert(node);
  1729. tt_assert(guard);
  1730. tt_int_op(guard->state, OP_EQ, GUARD_CIRC_STATE_USABLE_ON_COMPLETION);
  1731. tt_i64_op(guard->state_set_at, OP_EQ, start+35);
  1732. g = entry_guard_handle_get(guard->guard);
  1733. tt_assert(g);
  1734. tt_mem_op(g->identity, OP_EQ, node->identity, DIGEST_LEN);
  1735. tt_int_op(g->is_primary, OP_EQ, 1);
  1736. tt_i64_op(g->last_tried_to_connect, OP_EQ, start+35);
  1737. tt_int_op(g->confirmed_idx, OP_EQ, 0); // same one.
  1738. /* It's failed! What will happen to our poor guard? */
  1739. update_approx_time(start+45);
  1740. entry_guard_failed(&guard);
  1741. tt_assert(guard);
  1742. tt_int_op(guard->state, OP_EQ, GUARD_CIRC_STATE_DEAD);
  1743. tt_i64_op(guard->state_set_at, OP_EQ, start+45);
  1744. g = entry_guard_handle_get(guard->guard);
  1745. tt_assert(g);
  1746. tt_int_op(g->is_reachable, OP_EQ, GUARD_REACHABLE_NO);
  1747. tt_i64_op(g->failing_since, OP_EQ, start+45);
  1748. tt_int_op(g->confirmed_idx, OP_EQ, 0); // still confirmed.
  1749. circuit_guard_state_free(guard);
  1750. guard = NULL;
  1751. node = NULL;
  1752. entry_guard_t *g_prev = g;
  1753. g = NULL;
  1754. /* Now try a third time. Since the other one is down, we'll get a different
  1755. * (still primary) guard.
  1756. */
  1757. update_approx_time(start+60);
  1758. r = entry_guard_pick_for_circuit(gs, GUARD_USAGE_TRAFFIC, NULL,
  1759. &node, &guard);
  1760. tt_int_op(r, OP_EQ, 0);
  1761. tt_assert(node);
  1762. tt_assert(guard);
  1763. tt_int_op(guard->state, OP_EQ, GUARD_CIRC_STATE_USABLE_ON_COMPLETION);
  1764. g = entry_guard_handle_get(guard->guard);
  1765. tt_assert(g);
  1766. tt_ptr_op(g, OP_NE, g_prev);
  1767. tt_mem_op(g->identity, OP_EQ, node->identity, DIGEST_LEN);
  1768. tt_mem_op(g->identity, OP_NE, g_prev->identity, DIGEST_LEN);
  1769. tt_int_op(g->is_primary, OP_EQ, 1);
  1770. tt_i64_op(g->last_tried_to_connect, OP_EQ, start+60);
  1771. tt_int_op(g->confirmed_idx, OP_EQ, -1); // not confirmed now.
  1772. /* Call this one up; watch it get confirmed. */
  1773. update_approx_time(start+90);
  1774. u = entry_guard_succeeded(&guard);
  1775. tt_int_op(u, OP_EQ, GUARD_USABLE_NOW);
  1776. tt_assert(guard);
  1777. tt_int_op(guard->state, OP_EQ, GUARD_CIRC_STATE_COMPLETE);
  1778. g = entry_guard_handle_get(guard->guard);
  1779. tt_assert(g);
  1780. tt_int_op(g->is_reachable, OP_EQ, GUARD_REACHABLE_YES);
  1781. tt_int_op(g->confirmed_idx, OP_EQ, 1);
  1782. done:
  1783. guard_selection_free(gs);
  1784. circuit_guard_state_free(guard);
  1785. }
  1786. static void
  1787. test_entry_guard_select_for_circuit_highlevel_confirm_other(void *arg)
  1788. {
  1789. (void) arg;
  1790. const int N_PRIMARY = DFLT_N_PRIMARY_GUARDS;
  1791. /* At the start, we have no confirmed guards. We'll mark the primary guards
  1792. * down, then confirm something else. As soon as we do, it should become
  1793. * primary, and we should get it next time. */
  1794. time_t start = approx_time();
  1795. guard_selection_t *gs = guard_selection_new("default", GS_TYPE_NORMAL);
  1796. circuit_guard_state_t *guard = NULL;
  1797. int i, r;
  1798. const node_t *node = NULL;
  1799. guard_usable_t u;
  1800. /* Declare that we're on the internet. */
  1801. entry_guards_note_internet_connectivity(gs);
  1802. /* Primary guards are down! */
  1803. for (i = 0; i < N_PRIMARY; ++i) {
  1804. r = entry_guard_pick_for_circuit(gs, GUARD_USAGE_TRAFFIC, NULL,
  1805. &node, &guard);
  1806. tt_assert(node);
  1807. tt_assert(guard);
  1808. tt_int_op(r, OP_EQ, 0);
  1809. tt_int_op(guard->state, OP_EQ, GUARD_CIRC_STATE_USABLE_ON_COMPLETION);
  1810. entry_guard_failed(&guard);
  1811. circuit_guard_state_free(guard);
  1812. guard = NULL;
  1813. node = NULL;
  1814. }
  1815. /* Next guard should be non-primary. */
  1816. node = NULL;
  1817. r = entry_guard_pick_for_circuit(gs, GUARD_USAGE_TRAFFIC, NULL,
  1818. &node, &guard);
  1819. tt_assert(node);
  1820. tt_assert(guard);
  1821. tt_int_op(r, OP_EQ, 0);
  1822. entry_guard_t *g = entry_guard_handle_get(guard->guard);
  1823. tt_assert(g);
  1824. tt_int_op(guard->state, OP_EQ, GUARD_CIRC_STATE_USABLE_IF_NO_BETTER_GUARD);
  1825. tt_int_op(g->confirmed_idx, OP_EQ, -1);
  1826. tt_int_op(g->is_primary, OP_EQ, 0);
  1827. tt_int_op(g->is_pending, OP_EQ, 1);
  1828. (void)start;
  1829. u = entry_guard_succeeded(&guard);
  1830. /* We're on the internet (by fiat), so this guard will get called "confirmed"
  1831. * and should immediately become primary.
  1832. */
  1833. tt_int_op(guard->state, OP_EQ, GUARD_CIRC_STATE_COMPLETE);
  1834. tt_assert(u == GUARD_USABLE_NOW);
  1835. tt_int_op(g->confirmed_idx, OP_EQ, 0);
  1836. tt_int_op(g->is_primary, OP_EQ, 1);
  1837. tt_int_op(g->is_pending, OP_EQ, 0);
  1838. done:
  1839. guard_selection_free(gs);
  1840. circuit_guard_state_free(guard);
  1841. }
  1842. static void
  1843. test_entry_guard_select_for_circuit_highlevel_primary_retry(void *arg)
  1844. {
  1845. (void) arg;
  1846. const int N_PRIMARY = DFLT_N_PRIMARY_GUARDS;
  1847. /* At the start, we have no confirmed guards. We'll mark the primary guards
  1848. * down, then confirm something else. As soon as we do, it should become
  1849. * primary, and we should get it next time. */
  1850. time_t start = approx_time();
  1851. guard_selection_t *gs = guard_selection_new("default", GS_TYPE_NORMAL);
  1852. circuit_guard_state_t *guard = NULL, *guard2 = NULL;
  1853. int i, r;
  1854. const node_t *node = NULL;
  1855. entry_guard_t *g;
  1856. guard_usable_t u;
  1857. /* Declare that we're on the internet. */
  1858. entry_guards_note_internet_connectivity(gs);
  1859. /* Make primary guards confirmed (so they won't be superseded by a later
  1860. * guard), then mark them down. */
  1861. for (i = 0; i < N_PRIMARY; ++i) {
  1862. r = entry_guard_pick_for_circuit(gs, GUARD_USAGE_TRAFFIC, NULL,
  1863. &node, &guard);
  1864. tt_assert(node);
  1865. tt_assert(guard);
  1866. tt_int_op(r, OP_EQ, 0);
  1867. tt_int_op(guard->state, OP_EQ, GUARD_CIRC_STATE_USABLE_ON_COMPLETION);
  1868. g = entry_guard_handle_get(guard->guard);
  1869. make_guard_confirmed(gs, g);
  1870. tt_int_op(g->is_primary, OP_EQ, 1);
  1871. entry_guard_failed(&guard);
  1872. circuit_guard_state_free(guard);
  1873. tt_int_op(g->is_reachable, OP_EQ, GUARD_REACHABLE_NO);
  1874. guard = NULL;
  1875. node = NULL;
  1876. }
  1877. /* Get another guard that we might try. */
  1878. r = entry_guard_pick_for_circuit(gs, GUARD_USAGE_TRAFFIC, NULL,
  1879. &node, &guard);
  1880. tt_assert(node);
  1881. tt_assert(guard);
  1882. tt_int_op(r, OP_EQ, 0);
  1883. tt_int_op(guard->state, OP_EQ, GUARD_CIRC_STATE_USABLE_IF_NO_BETTER_GUARD);
  1884. g = entry_guard_handle_get(guard->guard);
  1885. tt_int_op(g->is_primary, OP_EQ, 0);
  1886. tt_assert(entry_guards_all_primary_guards_are_down(gs));
  1887. /* And an hour has passed ... */
  1888. update_approx_time(start + 3600);
  1889. /* Say that guard has succeeded! */
  1890. u = entry_guard_succeeded(&guard);
  1891. tt_int_op(u, OP_EQ, GUARD_MAYBE_USABLE_LATER);
  1892. tt_int_op(guard->state, OP_EQ, GUARD_CIRC_STATE_WAITING_FOR_BETTER_GUARD);
  1893. g = entry_guard_handle_get(guard->guard);
  1894. /* The primary guards should have been marked up! */
  1895. SMARTLIST_FOREACH(gs->primary_entry_guards, entry_guard_t *, pg, {
  1896. tt_int_op(pg->is_primary, OP_EQ, 1);
  1897. tt_ptr_op(g, OP_NE, pg);
  1898. tt_int_op(pg->is_reachable, OP_EQ, GUARD_REACHABLE_MAYBE);
  1899. });
  1900. /* Have a circuit to a primary guard succeed. */
  1901. r = entry_guard_pick_for_circuit(gs, GUARD_USAGE_TRAFFIC, NULL,
  1902. &node, &guard2);
  1903. tt_int_op(r, OP_EQ, 0);
  1904. tt_int_op(guard2->state, OP_EQ, GUARD_CIRC_STATE_USABLE_ON_COMPLETION);
  1905. u = entry_guard_succeeded(&guard2);
  1906. tt_assert(u == GUARD_USABLE_NOW);
  1907. tt_int_op(guard2->state, OP_EQ, GUARD_CIRC_STATE_COMPLETE);
  1908. tt_assert(! entry_guards_all_primary_guards_are_down(gs));
  1909. done:
  1910. guard_selection_free(gs);
  1911. circuit_guard_state_free(guard);
  1912. circuit_guard_state_free(guard2);
  1913. }
  1914. static void
  1915. test_entry_guard_select_and_cancel(void *arg)
  1916. {
  1917. (void) arg;
  1918. const int N_PRIMARY = DFLT_N_PRIMARY_GUARDS;
  1919. int i,r;
  1920. const node_t *node = NULL;
  1921. circuit_guard_state_t *guard;
  1922. guard_selection_t *gs = guard_selection_new("default", GS_TYPE_NORMAL);
  1923. entry_guard_t *g;
  1924. /* Once more, we mark all the primary guards down. */
  1925. entry_guards_note_internet_connectivity(gs);
  1926. for (i = 0; i < N_PRIMARY; ++i) {
  1927. r = entry_guard_pick_for_circuit(gs, GUARD_USAGE_TRAFFIC, NULL,
  1928. &node, &guard);
  1929. tt_int_op(r, OP_EQ, 0);
  1930. tt_int_op(guard->state, OP_EQ, GUARD_CIRC_STATE_USABLE_ON_COMPLETION);
  1931. g = entry_guard_handle_get(guard->guard);
  1932. tt_int_op(g->is_primary, OP_EQ, 1);
  1933. tt_int_op(g->is_pending, OP_EQ, 0);
  1934. make_guard_confirmed(gs, g);
  1935. entry_guard_failed(&guard);
  1936. circuit_guard_state_free(guard);
  1937. guard = NULL;
  1938. node = NULL;
  1939. }
  1940. tt_assert(entry_guards_all_primary_guards_are_down(gs));
  1941. /* Now get another guard we could try... */
  1942. r = entry_guard_pick_for_circuit(gs, GUARD_USAGE_TRAFFIC, NULL,
  1943. &node, &guard);
  1944. tt_assert(node);
  1945. tt_assert(guard);
  1946. tt_int_op(r, OP_EQ, 0);
  1947. tt_int_op(guard->state, OP_EQ, GUARD_CIRC_STATE_USABLE_IF_NO_BETTER_GUARD);
  1948. g = entry_guard_handle_get(guard->guard);
  1949. tt_int_op(g->is_primary, OP_EQ, 0);
  1950. tt_int_op(g->is_pending, OP_EQ, 1);
  1951. /* Whoops! We should never have asked for this guard. Cancel the request! */
  1952. entry_guard_cancel(&guard);
  1953. tt_ptr_op(guard, OP_EQ, NULL);
  1954. tt_int_op(g->is_primary, OP_EQ, 0);
  1955. tt_int_op(g->is_pending, OP_EQ, 0);
  1956. done:
  1957. guard_selection_free(gs);
  1958. circuit_guard_state_free(guard);
  1959. }
  1960. static void
  1961. test_entry_guard_drop_guards(void *arg)
  1962. {
  1963. (void) arg;
  1964. int r;
  1965. const node_t *node = NULL;
  1966. circuit_guard_state_t *guard;
  1967. guard_selection_t *gs = get_guard_selection_info();
  1968. // Pick a guard, to get things set up.
  1969. r = entry_guard_pick_for_circuit(gs, GUARD_USAGE_TRAFFIC, NULL,
  1970. &node, &guard);
  1971. tt_int_op(r, OP_EQ, 0);
  1972. tt_int_op(smartlist_len(gs->sampled_entry_guards), OP_GE,
  1973. DFLT_MIN_FILTERED_SAMPLE_SIZE);
  1974. tt_ptr_op(gs, OP_EQ, get_guard_selection_info());
  1975. // Drop all the guards! (This is a bad idea....)
  1976. remove_all_entry_guards_for_guard_selection(gs);
  1977. gs = get_guard_selection_info();
  1978. tt_int_op(smartlist_len(gs->sampled_entry_guards), OP_EQ, 0);
  1979. tt_int_op(smartlist_len(gs->primary_entry_guards), OP_EQ, 0);
  1980. tt_int_op(smartlist_len(gs->confirmed_entry_guards), OP_EQ, 0);
  1981. done:
  1982. circuit_guard_state_free(guard);
  1983. guard_selection_free(gs);
  1984. }
  1985. /* Unit test setup function: Create a fake network, and set everything up
  1986. * for testing the upgrade-a-waiting-circuit code. */
  1987. typedef struct {
  1988. guard_selection_t *gs;
  1989. time_t start;
  1990. circuit_guard_state_t *guard1_state;
  1991. circuit_guard_state_t *guard2_state;
  1992. entry_guard_t *guard1;
  1993. entry_guard_t *guard2;
  1994. origin_circuit_t *circ1;
  1995. origin_circuit_t *circ2;
  1996. smartlist_t *all_origin_circuits;
  1997. } upgrade_circuits_data_t;
  1998. static void *
  1999. upgrade_circuits_setup(const struct testcase_t *testcase)
  2000. {
  2001. upgrade_circuits_data_t *data = tor_malloc_zero(sizeof(*data));
  2002. guard_selection_t *gs = data->gs =
  2003. guard_selection_new("default", GS_TYPE_NORMAL);
  2004. circuit_guard_state_t *guard;
  2005. const node_t *node;
  2006. entry_guard_t *g;
  2007. int i;
  2008. const int N_PRIMARY = DFLT_N_PRIMARY_GUARDS;
  2009. const char *argument = testcase->setup_data;
  2010. const int make_circ1_succeed = strstr(argument, "c1-done") != NULL;
  2011. const int make_circ2_succeed = strstr(argument, "c2-done") != NULL;
  2012. big_fake_network_setup(testcase);
  2013. /* We're going to set things up in a state where a circuit will be ready to
  2014. * be upgraded. Each test can make a single change (or not) that should
  2015. * block the upgrade.
  2016. */
  2017. /* First, make all the primary guards confirmed, and down. */
  2018. data->start = approx_time();
  2019. entry_guards_note_internet_connectivity(gs);
  2020. for (i = 0; i < N_PRIMARY; ++i) {
  2021. entry_guard_pick_for_circuit(gs, GUARD_USAGE_TRAFFIC, NULL, &node, &guard);
  2022. g = entry_guard_handle_get(guard->guard);
  2023. make_guard_confirmed(gs, g);
  2024. entry_guard_failed(&guard);
  2025. circuit_guard_state_free(guard);
  2026. }
  2027. /* Grab another couple of guards */
  2028. data->all_origin_circuits = smartlist_new();
  2029. update_approx_time(data->start + 27);
  2030. entry_guard_pick_for_circuit(gs, GUARD_USAGE_TRAFFIC, NULL,
  2031. &node, &data->guard1_state);
  2032. origin_circuit_t *circ;
  2033. data->circ1 = circ = origin_circuit_new();
  2034. circ->base_.purpose = CIRCUIT_PURPOSE_C_GENERAL;
  2035. circ->guard_state = data->guard1_state;
  2036. smartlist_add(data->all_origin_circuits, circ);
  2037. update_approx_time(data->start + 30);
  2038. entry_guard_pick_for_circuit(gs, GUARD_USAGE_TRAFFIC, NULL,
  2039. &node, &data->guard2_state);
  2040. data->circ2 = circ = origin_circuit_new();
  2041. circ->base_.purpose = CIRCUIT_PURPOSE_C_GENERAL;
  2042. circ->guard_state = data->guard2_state;
  2043. smartlist_add(data->all_origin_circuits, circ);
  2044. data->guard1 = entry_guard_handle_get(data->guard1_state->guard);
  2045. data->guard2 = entry_guard_handle_get(data->guard2_state->guard);
  2046. tor_assert(data->guard1 != data->guard2);
  2047. tor_assert(data->guard1_state->state ==
  2048. GUARD_CIRC_STATE_USABLE_IF_NO_BETTER_GUARD);
  2049. tor_assert(data->guard2_state->state ==
  2050. GUARD_CIRC_STATE_USABLE_IF_NO_BETTER_GUARD);
  2051. guard_usable_t r;
  2052. update_approx_time(data->start + 32);
  2053. if (make_circ1_succeed) {
  2054. r = entry_guard_succeeded(&data->guard1_state);
  2055. tor_assert(r == GUARD_MAYBE_USABLE_LATER);
  2056. tor_assert(data->guard1_state->state ==
  2057. GUARD_CIRC_STATE_WAITING_FOR_BETTER_GUARD);
  2058. }
  2059. update_approx_time(data->start + 33);
  2060. if (make_circ2_succeed) {
  2061. r = entry_guard_succeeded(&data->guard2_state);
  2062. tor_assert(r == GUARD_MAYBE_USABLE_LATER);
  2063. tor_assert(data->guard2_state->state ==
  2064. GUARD_CIRC_STATE_WAITING_FOR_BETTER_GUARD);
  2065. }
  2066. return data;
  2067. }
  2068. static int
  2069. upgrade_circuits_cleanup(const struct testcase_t *testcase, void *ptr)
  2070. {
  2071. upgrade_circuits_data_t *data = ptr;
  2072. // circuit_guard_state_free(data->guard1_state); // held in circ1
  2073. // circuit_guard_state_free(data->guard2_state); // held in circ2
  2074. guard_selection_free(data->gs);
  2075. smartlist_free(data->all_origin_circuits);
  2076. circuit_free_(TO_CIRCUIT(data->circ1));
  2077. circuit_free_(TO_CIRCUIT(data->circ2));
  2078. tor_free(data);
  2079. return big_fake_network_cleanup(testcase, NULL);
  2080. }
  2081. static void
  2082. test_entry_guard_upgrade_a_circuit(void *arg)
  2083. {
  2084. upgrade_circuits_data_t *data = arg;
  2085. /* This is the easy case: we have no COMPLETED circuits, all the
  2086. * primary guards are down, we have two WAITING circuits: one will
  2087. * get upgraded to COMPLETED! (The one that started first.)
  2088. */
  2089. smartlist_t *result = smartlist_new();
  2090. int r;
  2091. r = entry_guards_upgrade_waiting_circuits(data->gs,
  2092. data->all_origin_circuits,
  2093. result);
  2094. tt_int_op(r, OP_EQ, 1);
  2095. tt_int_op(smartlist_len(result), OP_EQ, 1);
  2096. origin_circuit_t *oc = smartlist_get(result, 0);
  2097. /* circ1 was started first, so we'll get told to ugrade it... */
  2098. tt_ptr_op(oc, OP_EQ, data->circ1);
  2099. /* And the guard state should be complete */
  2100. tt_ptr_op(data->guard1_state, OP_NE, NULL);
  2101. tt_int_op(data->guard1_state->state, OP_EQ, GUARD_CIRC_STATE_COMPLETE);
  2102. done:
  2103. smartlist_free(result);
  2104. }
  2105. static void
  2106. test_entry_guard_upgrade_blocked_by_live_primary_guards(void *arg)
  2107. {
  2108. upgrade_circuits_data_t *data = arg;
  2109. /* If any primary guards might be up, we can't upgrade any waiting
  2110. * circuits.
  2111. */
  2112. mark_primary_guards_maybe_reachable(data->gs);
  2113. smartlist_t *result = smartlist_new();
  2114. int r;
  2115. setup_capture_of_logs(LOG_DEBUG);
  2116. r = entry_guards_upgrade_waiting_circuits(data->gs,
  2117. data->all_origin_circuits,
  2118. result);
  2119. tt_int_op(r, OP_EQ, 0);
  2120. tt_int_op(smartlist_len(result), OP_EQ, 0);
  2121. expect_log_msg_containing("not all primary guards were definitely down.");
  2122. done:
  2123. teardown_capture_of_logs();
  2124. smartlist_free(result);
  2125. }
  2126. static void
  2127. test_entry_guard_upgrade_blocked_by_lack_of_waiting_circuits(void *arg)
  2128. {
  2129. upgrade_circuits_data_t *data = arg;
  2130. /* If no circuits are waiting, we can't upgrade anything. (The test
  2131. * setup in this case was told not to make any of the circuits "waiting".)
  2132. */
  2133. smartlist_t *result = smartlist_new();
  2134. int r;
  2135. setup_capture_of_logs(LOG_DEBUG);
  2136. r = entry_guards_upgrade_waiting_circuits(data->gs,
  2137. data->all_origin_circuits,
  2138. result);
  2139. tt_int_op(r, OP_EQ, 0);
  2140. tt_int_op(smartlist_len(result), OP_EQ, 0);
  2141. expect_log_msg_containing("Considered upgrading guard-stalled circuits, "
  2142. "but didn't find any.");
  2143. done:
  2144. teardown_capture_of_logs();
  2145. smartlist_free(result);
  2146. }
  2147. static void
  2148. test_entry_guard_upgrade_blocked_by_better_circ_complete(void *arg)
  2149. {
  2150. upgrade_circuits_data_t *data = arg;
  2151. /* We'll run through the logic of upgrade_a_circuit below...
  2152. * and then try again to make sure that circ2 isn't also upgraded.
  2153. */
  2154. smartlist_t *result = smartlist_new();
  2155. int r;
  2156. r = entry_guards_upgrade_waiting_circuits(data->gs,
  2157. data->all_origin_circuits,
  2158. result);
  2159. tt_int_op(r, OP_EQ, 1);
  2160. tt_int_op(smartlist_len(result), OP_EQ, 1);
  2161. origin_circuit_t *oc = smartlist_get(result, 0);
  2162. tt_ptr_op(oc, OP_EQ, data->circ1);
  2163. tt_ptr_op(data->guard1_state, OP_NE, NULL);
  2164. tt_int_op(data->guard1_state->state, OP_EQ, GUARD_CIRC_STATE_COMPLETE);
  2165. /* Now, try again. Make sure that circ2 isn't upgraded. */
  2166. smartlist_clear(result);
  2167. setup_capture_of_logs(LOG_DEBUG);
  2168. r = entry_guards_upgrade_waiting_circuits(data->gs,
  2169. data->all_origin_circuits,
  2170. result);
  2171. tt_int_op(r, OP_EQ, 0);
  2172. tt_int_op(smartlist_len(result), OP_EQ, 0);
  2173. expect_log_msg_containing("At least one complete circuit had higher "
  2174. "priority, so not upgrading.");
  2175. done:
  2176. teardown_capture_of_logs();
  2177. smartlist_free(result);
  2178. }
  2179. static void
  2180. test_entry_guard_upgrade_not_blocked_by_restricted_circ_complete(void *arg)
  2181. {
  2182. upgrade_circuits_data_t *data = arg;
  2183. /* Once more, let circ1 become complete. But this time, we'll claim
  2184. * that circ2 was restricted to not use the same guard as circ1. */
  2185. data->guard2_state->restrictions =
  2186. guard_create_exit_restriction((uint8_t*)data->guard1->identity);
  2187. smartlist_t *result = smartlist_new();
  2188. int r;
  2189. r = entry_guards_upgrade_waiting_circuits(data->gs,
  2190. data->all_origin_circuits,
  2191. result);
  2192. tt_int_op(r, OP_EQ, 1);
  2193. tt_int_op(smartlist_len(result), OP_EQ, 1);
  2194. origin_circuit_t *oc = smartlist_get(result, 0);
  2195. tt_ptr_op(oc, OP_EQ, data->circ1);
  2196. tt_ptr_op(data->guard1_state, OP_NE, NULL);
  2197. tt_int_op(data->guard1_state->state, OP_EQ, GUARD_CIRC_STATE_COMPLETE);
  2198. /* Now, we try again. Since circ2 has a restriction that circ1 doesn't obey,
  2199. * circ2 _is_ eligible for upgrade. */
  2200. smartlist_clear(result);
  2201. r = entry_guards_upgrade_waiting_circuits(data->gs,
  2202. data->all_origin_circuits,
  2203. result);
  2204. tt_int_op(r, OP_EQ, 1);
  2205. tt_int_op(smartlist_len(result), OP_EQ, 1);
  2206. origin_circuit_t *oc2 = smartlist_get(result, 0);
  2207. tt_ptr_op(oc2, OP_EQ, data->circ2);
  2208. done:
  2209. smartlist_free(result);
  2210. }
  2211. static void
  2212. test_entry_guard_upgrade_not_blocked_by_worse_circ_complete(void *arg)
  2213. {
  2214. upgrade_circuits_data_t *data = arg;
  2215. smartlist_t *result = smartlist_new();
  2216. /* here we manually make circ2 COMPLETE, and make sure that circ1
  2217. * gets made complete anyway, since guard1 has higher priority
  2218. */
  2219. update_approx_time(data->start + 300);
  2220. data->guard2_state->state = GUARD_CIRC_STATE_COMPLETE;
  2221. data->guard2_state->state_set_at = approx_time();
  2222. update_approx_time(data->start + 301);
  2223. /* Now, try again. Make sure that circ1 is approved. */
  2224. int r;
  2225. r = entry_guards_upgrade_waiting_circuits(data->gs,
  2226. data->all_origin_circuits,
  2227. result);
  2228. tt_int_op(r, OP_EQ, 1);
  2229. tt_int_op(smartlist_len(result), OP_EQ, 1);
  2230. origin_circuit_t *oc = smartlist_get(result, 0);
  2231. tt_ptr_op(oc, OP_EQ, data->circ1);
  2232. done:
  2233. smartlist_free(result);
  2234. }
  2235. static void
  2236. test_entry_guard_upgrade_blocked_by_better_circ_pending(void *arg)
  2237. {
  2238. upgrade_circuits_data_t *data = arg;
  2239. /* circ2 is done, but circ1 is still pending. Since circ1 is better,
  2240. * we won't upgrade circ2. */
  2241. /* XXXX Prop271 -- this is a kludge. I'm making sure circ1 _is_ better,
  2242. * by messing with the guards' confirmed_idx */
  2243. make_guard_confirmed(data->gs, data->guard1);
  2244. {
  2245. int tmp;
  2246. tmp = data->guard1->confirmed_idx;
  2247. data->guard1->confirmed_idx = data->guard2->confirmed_idx;
  2248. data->guard2->confirmed_idx = tmp;
  2249. }
  2250. smartlist_t *result = smartlist_new();
  2251. setup_capture_of_logs(LOG_DEBUG);
  2252. int r;
  2253. r = entry_guards_upgrade_waiting_circuits(data->gs,
  2254. data->all_origin_circuits,
  2255. result);
  2256. tt_int_op(r, OP_EQ, 0);
  2257. tt_int_op(smartlist_len(result), OP_EQ, 0);
  2258. expect_log_msg_containing("but 1 pending circuit(s) had higher guard "
  2259. "priority, so not upgrading.");
  2260. done:
  2261. teardown_capture_of_logs();
  2262. smartlist_free(result);
  2263. }
  2264. static void
  2265. test_entry_guard_upgrade_not_blocked_by_restricted_circ_pending(void *arg)
  2266. {
  2267. upgrade_circuits_data_t *data = arg;
  2268. /* circ2 is done, but circ1 is still pending. But when there is a
  2269. restriction on circ2 that circ1 can't satisfy, circ1 can't block
  2270. circ2. */
  2271. /* XXXX Prop271 -- this is a kludge. I'm making sure circ1 _is_ better,
  2272. * by messing with the guards' confirmed_idx */
  2273. make_guard_confirmed(data->gs, data->guard1);
  2274. {
  2275. int tmp;
  2276. tmp = data->guard1->confirmed_idx;
  2277. data->guard1->confirmed_idx = data->guard2->confirmed_idx;
  2278. data->guard2->confirmed_idx = tmp;
  2279. }
  2280. data->guard2_state->restrictions =
  2281. guard_create_exit_restriction((uint8_t*)data->guard1->identity);
  2282. smartlist_t *result = smartlist_new();
  2283. int r;
  2284. r = entry_guards_upgrade_waiting_circuits(data->gs,
  2285. data->all_origin_circuits,
  2286. result);
  2287. tt_int_op(r, OP_EQ, 1);
  2288. tt_int_op(smartlist_len(result), OP_EQ, 1);
  2289. origin_circuit_t *oc = smartlist_get(result, 0);
  2290. tt_ptr_op(oc, OP_EQ, data->circ2);
  2291. done:
  2292. smartlist_free(result);
  2293. }
  2294. static void
  2295. test_entry_guard_upgrade_not_blocked_by_worse_circ_pending(void *arg)
  2296. {
  2297. upgrade_circuits_data_t *data = arg;
  2298. /* circ1 is done, but circ2 is still pending. Since circ1 is better,
  2299. * we will upgrade it. */
  2300. smartlist_t *result = smartlist_new();
  2301. int r;
  2302. r = entry_guards_upgrade_waiting_circuits(data->gs,
  2303. data->all_origin_circuits,
  2304. result);
  2305. tt_int_op(r, OP_EQ, 1);
  2306. tt_int_op(smartlist_len(result), OP_EQ, 1);
  2307. origin_circuit_t *oc = smartlist_get(result, 0);
  2308. tt_ptr_op(oc, OP_EQ, data->circ1);
  2309. done:
  2310. smartlist_free(result);
  2311. }
  2312. static void
  2313. test_enty_guard_should_expire_waiting(void *arg)
  2314. {
  2315. (void)arg;
  2316. circuit_guard_state_t *fake_state = tor_malloc_zero(sizeof(*fake_state));
  2317. /* We'll leave "guard" unset -- it won't matter here. */
  2318. /* No state? Can't expire. */
  2319. tt_assert(! entry_guard_state_should_expire(NULL));
  2320. /* Let's try one that expires. */
  2321. fake_state->state = GUARD_CIRC_STATE_WAITING_FOR_BETTER_GUARD;
  2322. fake_state->state_set_at =
  2323. approx_time() - DFLT_NONPRIMARY_GUARD_IDLE_TIMEOUT - 1;
  2324. tt_assert(entry_guard_state_should_expire(fake_state));
  2325. /* But it wouldn't expire if we changed the state. */
  2326. fake_state->state = GUARD_CIRC_STATE_USABLE_IF_NO_BETTER_GUARD;
  2327. tt_assert(! entry_guard_state_should_expire(fake_state));
  2328. /* And it wouldn't have expired a few seconds ago. */
  2329. fake_state->state = GUARD_CIRC_STATE_WAITING_FOR_BETTER_GUARD;
  2330. fake_state->state_set_at =
  2331. approx_time() - DFLT_NONPRIMARY_GUARD_IDLE_TIMEOUT + 5;
  2332. tt_assert(! entry_guard_state_should_expire(fake_state));
  2333. done:
  2334. tor_free(fake_state);
  2335. }
  2336. static void
  2337. mock_directory_initiate_request(directory_request_t *req)
  2338. {
  2339. if (req->guard_state) {
  2340. circuit_guard_state_free(req->guard_state);
  2341. }
  2342. }
  2343. static networkstatus_t *mock_ns_val = NULL;
  2344. static networkstatus_t *
  2345. mock_ns_get_by_flavor(consensus_flavor_t f)
  2346. {
  2347. (void)f;
  2348. return mock_ns_val;
  2349. }
  2350. /** Test that when we fetch microdescriptors we skip guards that have
  2351. * previously failed to serve us needed microdescriptors. */
  2352. static void
  2353. test_entry_guard_outdated_dirserver_exclusion(void *arg)
  2354. {
  2355. int retval;
  2356. response_handler_args_t *args = NULL;
  2357. dir_connection_t *conn = NULL;
  2358. (void) arg;
  2359. /* Test prep: Make a new guard selection */
  2360. guard_selection_t *gs = get_guard_selection_by_name("default",
  2361. GS_TYPE_NORMAL, 1);
  2362. /* ... we want to use entry guards */
  2363. or_options_t *options = get_options_mutable();
  2364. options->UseEntryGuards = 1;
  2365. options->UseBridges = 0;
  2366. /* ... prepare some md digests we want to download in the future */
  2367. smartlist_t *digests = smartlist_new();
  2368. const char *prose = "unhurried and wise, we perceive.";
  2369. for (int i = 0; i < 20; i++) {
  2370. smartlist_add(digests, (char*)prose);
  2371. }
  2372. tt_int_op(smartlist_len(digests), OP_EQ, 20);
  2373. /* ... now mock some functions */
  2374. mock_ns_val = tor_malloc_zero(sizeof(networkstatus_t));
  2375. MOCK(networkstatus_get_latest_consensus_by_flavor, mock_ns_get_by_flavor);
  2376. MOCK(directory_initiate_request, mock_directory_initiate_request);
  2377. /* Test logic:
  2378. * 0. Create a proper guard set and primary guard list.
  2379. * 1. Pretend to fail microdescriptor fetches from all the primary guards.
  2380. * 2. Order another microdescriptor fetch and make sure that primary guards
  2381. * get skipped since they failed previous fetches.
  2382. */
  2383. { /* Setup primary guard list */
  2384. int i;
  2385. entry_guards_update_primary(gs);
  2386. for (i = 0; i < DFLT_N_PRIMARY_GUARDS; ++i) {
  2387. entry_guard_t *guard = smartlist_get(gs->sampled_entry_guards, i);
  2388. make_guard_confirmed(gs, guard);
  2389. }
  2390. entry_guards_update_primary(gs);
  2391. }
  2392. {
  2393. /* Fail microdesc fetches with all the primary guards */
  2394. args = tor_malloc_zero(sizeof(response_handler_args_t));
  2395. args->status_code = 404;
  2396. args->reason = NULL;
  2397. args->body = NULL;
  2398. args->body_len = 0;
  2399. conn = tor_malloc_zero(sizeof(dir_connection_t));
  2400. conn->requested_resource = tor_strdup("d/jlinblackorigami");
  2401. conn->base_.purpose = DIR_PURPOSE_FETCH_MICRODESC;
  2402. /* Pretend to fail fetches with all primary guards */
  2403. SMARTLIST_FOREACH_BEGIN(gs->primary_entry_guards,const entry_guard_t *,g) {
  2404. memcpy(conn->identity_digest, g->identity, DIGEST_LEN);
  2405. retval = handle_response_fetch_microdesc(conn, args);
  2406. tt_int_op(retval, OP_EQ, 0);
  2407. } SMARTLIST_FOREACH_END(g);
  2408. }
  2409. {
  2410. /* Now order the final md download */
  2411. setup_full_capture_of_logs(LOG_INFO);
  2412. initiate_descriptor_downloads(NULL, DIR_PURPOSE_FETCH_MICRODESC,
  2413. digests, 3, 7, 0);
  2414. /* ... and check that because we failed to fetch microdescs from all our
  2415. * primaries, we didn't end up selecting a primary for fetching dir info */
  2416. expect_log_msg_containing("No primary or confirmed guards available.");
  2417. teardown_capture_of_logs();
  2418. }
  2419. done:
  2420. UNMOCK(networkstatus_get_latest_consensus_by_flavor);
  2421. UNMOCK(directory_initiate_request);
  2422. smartlist_free(digests);
  2423. tor_free(mock_ns_val);
  2424. tor_free(args);
  2425. if (conn) {
  2426. tor_free(conn->requested_resource);
  2427. tor_free(conn);
  2428. }
  2429. }
  2430. static const struct testcase_setup_t big_fake_network = {
  2431. big_fake_network_setup, big_fake_network_cleanup
  2432. };
  2433. static const struct testcase_setup_t upgrade_circuits = {
  2434. upgrade_circuits_setup, upgrade_circuits_cleanup
  2435. };
  2436. #define BFN_TEST(name) \
  2437. { #name, test_entry_guard_ ## name, TT_FORK, &big_fake_network, NULL }
  2438. #define UPGRADE_TEST(name, arg) \
  2439. { #name, test_entry_guard_ ## name, TT_FORK, &upgrade_circuits, \
  2440. (void*)(arg) }
  2441. struct testcase_t entrynodes_tests[] = {
  2442. { "node_preferred_orport",
  2443. test_node_preferred_orport,
  2444. 0, NULL, NULL },
  2445. { "entry_guard_describe", test_entry_guard_describe, 0, NULL, NULL },
  2446. { "randomize_time", test_entry_guard_randomize_time, 0, NULL, NULL },
  2447. { "encode_for_state_minimal",
  2448. test_entry_guard_encode_for_state_minimal, 0, NULL, NULL },
  2449. { "encode_for_state_maximal",
  2450. test_entry_guard_encode_for_state_maximal, 0, NULL, NULL },
  2451. { "parse_from_state_minimal",
  2452. test_entry_guard_parse_from_state_minimal, 0, NULL, NULL },
  2453. { "parse_from_state_maximal",
  2454. test_entry_guard_parse_from_state_maximal, 0, NULL, NULL },
  2455. { "parse_from_state_failure",
  2456. test_entry_guard_parse_from_state_failure, 0, NULL, NULL },
  2457. { "parse_from_state_partial_failure",
  2458. test_entry_guard_parse_from_state_partial_failure, 0, NULL, NULL },
  2459. { "parse_from_state_full",
  2460. test_entry_guard_parse_from_state_full, TT_FORK, NULL, NULL },
  2461. { "parse_from_state_broken",
  2462. test_entry_guard_parse_from_state_broken, TT_FORK, NULL, NULL },
  2463. { "get_guard_selection_by_name",
  2464. test_entry_guard_get_guard_selection_by_name, TT_FORK, NULL, NULL },
  2465. BFN_TEST(choose_selection_initial),
  2466. BFN_TEST(add_single_guard),
  2467. BFN_TEST(node_filter),
  2468. BFN_TEST(expand_sample),
  2469. BFN_TEST(expand_sample_small_net),
  2470. BFN_TEST(update_from_consensus_status),
  2471. BFN_TEST(update_from_consensus_repair),
  2472. BFN_TEST(update_from_consensus_remove),
  2473. BFN_TEST(confirming_guards),
  2474. BFN_TEST(sample_reachable_filtered),
  2475. BFN_TEST(sample_reachable_filtered_empty),
  2476. BFN_TEST(retry_unreachable),
  2477. BFN_TEST(manage_primary),
  2478. { "guard_preferred", test_entry_guard_guard_preferred, TT_FORK, NULL, NULL },
  2479. BFN_TEST(select_for_circuit_no_confirmed),
  2480. BFN_TEST(select_for_circuit_confirmed),
  2481. BFN_TEST(select_for_circuit_highlevel_primary),
  2482. BFN_TEST(select_for_circuit_highlevel_confirm_other),
  2483. BFN_TEST(select_for_circuit_highlevel_primary_retry),
  2484. BFN_TEST(select_and_cancel),
  2485. BFN_TEST(drop_guards),
  2486. BFN_TEST(outdated_dirserver_exclusion),
  2487. UPGRADE_TEST(upgrade_a_circuit, "c1-done c2-done"),
  2488. UPGRADE_TEST(upgrade_blocked_by_live_primary_guards, "c1-done c2-done"),
  2489. UPGRADE_TEST(upgrade_blocked_by_lack_of_waiting_circuits, ""),
  2490. UPGRADE_TEST(upgrade_blocked_by_better_circ_complete, "c1-done c2-done"),
  2491. UPGRADE_TEST(upgrade_not_blocked_by_restricted_circ_complete,
  2492. "c1-done c2-done"),
  2493. UPGRADE_TEST(upgrade_not_blocked_by_worse_circ_complete, "c1-done c2-done"),
  2494. UPGRADE_TEST(upgrade_blocked_by_better_circ_pending, "c2-done"),
  2495. UPGRADE_TEST(upgrade_not_blocked_by_restricted_circ_pending,
  2496. "c2-done"),
  2497. UPGRADE_TEST(upgrade_not_blocked_by_worse_circ_pending, "c1-done"),
  2498. { "should_expire_waiting", test_enty_guard_should_expire_waiting, TT_FORK,
  2499. NULL, NULL },
  2500. END_OF_TESTCASES
  2501. };