test_entrynodes.c 105 KB

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