test_entrynodes.c 104 KB

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