test_entrynodes.c 92 KB

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