test_entrynodes.c 91 KB

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