test_entrynodes.c 103 KB

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