test_entrynodes.c 97 KB

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