test_entrynodes.c 104 KB

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