test_entrynodes.c 103 KB

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