test_entrynodes.c 103 KB

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