test_entrynodes.c 92 KB

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