test_entrynodes.c 103 KB

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