test_entrynodes.c 92 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731
  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. n->is_possible_guard = 0;
  957. }
  958. update_approx_time(start + 30);
  959. {
  960. /* try this with no live networkstatus. Nothing should happen! */
  961. ns_tmp = dummy_consensus;
  962. dummy_consensus = NULL;
  963. sampled_guards_update_from_consensus(gs);
  964. tt_i64_op(smartlist_len(gs->sampled_entry_guards), OP_EQ, n_sampled_pre);
  965. tt_i64_op(num_reachable_filtered_guards(gs, NULL), OP_EQ, n_filtered_pre);
  966. /* put the networkstatus back. */
  967. dummy_consensus = ns_tmp;
  968. ns_tmp = NULL;
  969. }
  970. /* Now those guards should become unlisted, and drop off the filter, but
  971. * stay in the sample. */
  972. update_approx_time(start + 60);
  973. sampled_guards_update_from_consensus(gs);
  974. tt_i64_op(smartlist_len(gs->sampled_entry_guards), OP_EQ, n_sampled_pre);
  975. tt_i64_op(num_reachable_filtered_guards(gs, NULL), OP_EQ, n_filtered_pre-5);
  976. for (i = 0; i < 5; ++i) {
  977. entry_guard_t *g = smartlist_get(gs->sampled_entry_guards, i);
  978. tt_assert(! g->currently_listed);
  979. tt_i64_op(g->unlisted_since_date, OP_EQ, start+60);
  980. }
  981. for (i = 5; i < n_sampled_pre; ++i) {
  982. entry_guard_t *g = smartlist_get(gs->sampled_entry_guards, i);
  983. tt_assert(g->currently_listed);
  984. tt_i64_op(g->unlisted_since_date, OP_EQ, 0);
  985. }
  986. /* Now re-list one, and remove one completely. */
  987. {
  988. entry_guard_t *g = smartlist_get(gs->sampled_entry_guards, 0);
  989. node_t *n = (node_t*) bfn_mock_node_get_by_id(g->identity);
  990. n->is_possible_guard = 1;
  991. }
  992. {
  993. /* try removing the node, to make sure we don't crash on an absent node
  994. */
  995. entry_guard_t *g = smartlist_get(gs->sampled_entry_guards, 5);
  996. node_t *n = (node_t*) bfn_mock_node_get_by_id(g->identity);
  997. smartlist_remove(big_fake_net_nodes, n);
  998. tor_free(n->rs);
  999. tor_free(n->md);
  1000. tor_free(n);
  1001. }
  1002. update_approx_time(start + 300);
  1003. sampled_guards_update_from_consensus(gs);
  1004. /* guards 1..5 are now unlisted; 0,6,7.. are listed. */
  1005. tt_i64_op(smartlist_len(gs->sampled_entry_guards), OP_EQ, n_sampled_pre);
  1006. for (i = 1; i < 6; ++i) {
  1007. entry_guard_t *g = smartlist_get(gs->sampled_entry_guards, i);
  1008. tt_assert(! g->currently_listed);
  1009. if (i == 5)
  1010. tt_i64_op(g->unlisted_since_date, OP_EQ, start+300);
  1011. else
  1012. tt_i64_op(g->unlisted_since_date, OP_EQ, start+60);
  1013. }
  1014. for (i = 0; i < n_sampled_pre; i = (!i) ? 6 : i+1) { /* 0,6,7,8, ... */
  1015. entry_guard_t *g = smartlist_get(gs->sampled_entry_guards, i);
  1016. tt_assert(g->currently_listed);
  1017. tt_i64_op(g->unlisted_since_date, OP_EQ, 0);
  1018. }
  1019. done:
  1020. tor_free(ns_tmp); /* in case we couldn't put it back */
  1021. guard_selection_free(gs);
  1022. UNMOCK(randomize_time);
  1023. }
  1024. static void
  1025. test_entry_guard_update_from_consensus_repair(void *arg)
  1026. {
  1027. /* Here we'll make sure that our code to repair the unlisted-since
  1028. * times is correct. */
  1029. (void)arg;
  1030. int i;
  1031. time_t start = approx_time();
  1032. guard_selection_t *gs = guard_selection_new("default", GS_TYPE_NORMAL);
  1033. /* Don't randomly backdate stuff; it will make correctness harder to check.*/
  1034. MOCK(randomize_time, mock_randomize_time_no_randomization);
  1035. /* First, sample some guards. */
  1036. entry_guards_expand_sample(gs);
  1037. int n_sampled_pre = smartlist_len(gs->sampled_entry_guards);
  1038. int n_filtered_pre = num_reachable_filtered_guards(gs, NULL);
  1039. tt_i64_op(n_sampled_pre, OP_EQ, n_filtered_pre);
  1040. tt_i64_op(n_sampled_pre, OP_GT, 10);
  1041. /* Now corrupt the list a bit. Call some unlisted-since-never, and some
  1042. * listed-and-unlisted-since-a-time. */
  1043. update_approx_time(start + 300);
  1044. for (i = 0; i < 3; ++i) {
  1045. /* these will get a date. */
  1046. entry_guard_t *g = smartlist_get(gs->sampled_entry_guards, i);
  1047. node_t *n = (node_t*) bfn_mock_node_get_by_id(g->identity);
  1048. n->is_possible_guard = 0;
  1049. g->currently_listed = 0;
  1050. }
  1051. for (i = 3; i < 6; ++i) {
  1052. /* these will become listed. */
  1053. entry_guard_t *g = smartlist_get(gs->sampled_entry_guards, i);
  1054. g->unlisted_since_date = start+100;
  1055. }
  1056. setup_full_capture_of_logs(LOG_WARN);
  1057. sampled_guards_update_from_consensus(gs);
  1058. expect_log_msg_containing(
  1059. "was listed, but with unlisted_since_date set");
  1060. expect_log_msg_containing(
  1061. "was unlisted, but with unlisted_since_date unset");
  1062. teardown_capture_of_logs();
  1063. tt_int_op(smartlist_len(gs->sampled_entry_guards), OP_EQ, n_sampled_pre);
  1064. tt_int_op(num_reachable_filtered_guards(gs, NULL), OP_EQ, n_filtered_pre-3);
  1065. for (i = 3; i < n_sampled_pre; ++i) {
  1066. /* these will become listed. */
  1067. entry_guard_t *g = smartlist_get(gs->sampled_entry_guards, i);
  1068. if (i < 3) {
  1069. tt_assert(! g->currently_listed);
  1070. tt_i64_op(g->unlisted_since_date, OP_EQ, start+300);
  1071. } else {
  1072. tt_assert(g->currently_listed);
  1073. tt_i64_op(g->unlisted_since_date, OP_EQ, 0);
  1074. }
  1075. }
  1076. done:
  1077. teardown_capture_of_logs();
  1078. guard_selection_free(gs);
  1079. UNMOCK(randomize_time);
  1080. }
  1081. static void
  1082. test_entry_guard_update_from_consensus_remove(void *arg)
  1083. {
  1084. /* Now let's check the logic responsible for removing guards from the
  1085. * sample entirely. */
  1086. (void)arg;
  1087. //int i;
  1088. guard_selection_t *gs = guard_selection_new("default", GS_TYPE_NORMAL);
  1089. smartlist_t *keep_ids = smartlist_new();
  1090. smartlist_t *remove_ids = smartlist_new();
  1091. /* Don't randomly backdate stuff; it will make correctness harder to check.*/
  1092. MOCK(randomize_time, mock_randomize_time_no_randomization);
  1093. /* First, sample some guards. */
  1094. entry_guards_expand_sample(gs);
  1095. int n_sampled_pre = smartlist_len(gs->sampled_entry_guards);
  1096. int n_filtered_pre = num_reachable_filtered_guards(gs, NULL);
  1097. tt_i64_op(n_sampled_pre, OP_EQ, n_filtered_pre);
  1098. tt_i64_op(n_sampled_pre, OP_GT, 10);
  1099. const time_t one_day_ago = approx_time() - 1*24*60*60;
  1100. const time_t one_year_ago = approx_time() - 365*24*60*60;
  1101. const time_t two_years_ago = approx_time() - 2*365*24*60*60;
  1102. /* 0: unlisted for a day. (keep this) */
  1103. {
  1104. entry_guard_t *g = smartlist_get(gs->sampled_entry_guards, 0);
  1105. node_t *n = (node_t*) bfn_mock_node_get_by_id(g->identity);
  1106. n->is_possible_guard = 0;
  1107. g->currently_listed = 0;
  1108. g->unlisted_since_date = one_day_ago;
  1109. smartlist_add(keep_ids, tor_memdup(g->identity, 20));
  1110. }
  1111. /* 1: unlisted for a year. (remove this) */
  1112. {
  1113. entry_guard_t *g = smartlist_get(gs->sampled_entry_guards, 1);
  1114. node_t *n = (node_t*) bfn_mock_node_get_by_id(g->identity);
  1115. n->is_possible_guard = 0;
  1116. g->currently_listed = 0;
  1117. g->unlisted_since_date = one_year_ago;
  1118. smartlist_add(remove_ids, tor_memdup(g->identity, 20));
  1119. }
  1120. /* 2: added a day ago, never confirmed. (keep this) */
  1121. {
  1122. entry_guard_t *g = smartlist_get(gs->sampled_entry_guards, 2);
  1123. g->sampled_on_date = one_day_ago;
  1124. smartlist_add(keep_ids, tor_memdup(g->identity, 20));
  1125. }
  1126. /* 3: added a year ago, never confirmed. (remove this) */
  1127. {
  1128. entry_guard_t *g = smartlist_get(gs->sampled_entry_guards, 3);
  1129. g->sampled_on_date = one_year_ago;
  1130. smartlist_add(remove_ids, tor_memdup(g->identity, 20));
  1131. }
  1132. /* 4: added two year ago, confirmed yesterday, primary. (keep this.) */
  1133. {
  1134. entry_guard_t *g = smartlist_get(gs->sampled_entry_guards, 4);
  1135. g->sampled_on_date = one_year_ago;
  1136. g->confirmed_on_date = one_day_ago;
  1137. g->confirmed_idx = 0;
  1138. g->is_primary = 1;
  1139. smartlist_add(gs->confirmed_entry_guards, g);
  1140. smartlist_add(gs->primary_entry_guards, g);
  1141. smartlist_add(keep_ids, tor_memdup(g->identity, 20));
  1142. }
  1143. /* 5: added two years ago, confirmed a year ago, primary. (remove this) */
  1144. {
  1145. entry_guard_t *g = smartlist_get(gs->sampled_entry_guards, 5);
  1146. g->sampled_on_date = two_years_ago;
  1147. g->confirmed_on_date = one_year_ago;
  1148. g->confirmed_idx = 1;
  1149. g->is_primary = 1;
  1150. smartlist_add(gs->confirmed_entry_guards, g);
  1151. smartlist_add(gs->primary_entry_guards, g);
  1152. smartlist_add(remove_ids, tor_memdup(g->identity, 20));
  1153. }
  1154. sampled_guards_update_from_consensus(gs);
  1155. /* Did we remove the right ones? */
  1156. SMARTLIST_FOREACH(keep_ids, uint8_t *, id, {
  1157. tt_assert(get_sampled_guard_with_id(gs, id) != NULL);
  1158. });
  1159. SMARTLIST_FOREACH(remove_ids, uint8_t *, id, {
  1160. tt_want(get_sampled_guard_with_id(gs, id) == NULL);
  1161. });
  1162. /* Did we remove the right number? */
  1163. tt_int_op(smartlist_len(gs->sampled_entry_guards), OP_EQ, n_sampled_pre - 3);
  1164. done:
  1165. guard_selection_free(gs);
  1166. UNMOCK(randomize_time);
  1167. SMARTLIST_FOREACH(keep_ids, char *, cp, tor_free(cp));
  1168. SMARTLIST_FOREACH(remove_ids, char *, cp, tor_free(cp));
  1169. smartlist_free(keep_ids);
  1170. smartlist_free(remove_ids);
  1171. }
  1172. static void
  1173. test_entry_guard_confirming_guards(void *arg)
  1174. {
  1175. (void)arg;
  1176. /* Now let's check the logic responsible for manipulating the list
  1177. * of confirmed guards */
  1178. guard_selection_t *gs = guard_selection_new("default", GS_TYPE_NORMAL);
  1179. MOCK(randomize_time, mock_randomize_time_no_randomization);
  1180. /* Create the sample. */
  1181. entry_guards_expand_sample(gs);
  1182. /* Confirm a few guards. */
  1183. time_t start = approx_time();
  1184. entry_guard_t *g1 = smartlist_get(gs->sampled_entry_guards, 0);
  1185. entry_guard_t *g2 = smartlist_get(gs->sampled_entry_guards, 1);
  1186. entry_guard_t *g3 = smartlist_get(gs->sampled_entry_guards, 8);
  1187. make_guard_confirmed(gs, g2);
  1188. update_approx_time(start + 10);
  1189. make_guard_confirmed(gs, g1);
  1190. make_guard_confirmed(gs, g3);
  1191. /* Were the correct dates and indices fed in? */
  1192. tt_int_op(g1->confirmed_idx, OP_EQ, 1);
  1193. tt_int_op(g2->confirmed_idx, OP_EQ, 0);
  1194. tt_int_op(g3->confirmed_idx, OP_EQ, 2);
  1195. tt_i64_op(g1->confirmed_on_date, OP_EQ, start+10);
  1196. tt_i64_op(g2->confirmed_on_date, OP_EQ, start);
  1197. tt_i64_op(g3->confirmed_on_date, OP_EQ, start+10);
  1198. tt_ptr_op(smartlist_get(gs->confirmed_entry_guards, 0), OP_EQ, g2);
  1199. tt_ptr_op(smartlist_get(gs->confirmed_entry_guards, 1), OP_EQ, g1);
  1200. tt_ptr_op(smartlist_get(gs->confirmed_entry_guards, 2), OP_EQ, g3);
  1201. /* Now make sure we can regenerate the confirmed_entry_guards list. */
  1202. smartlist_clear(gs->confirmed_entry_guards);
  1203. g2->confirmed_idx = 0;
  1204. g1->confirmed_idx = 10;
  1205. g3->confirmed_idx = 100;
  1206. entry_guards_update_confirmed(gs);
  1207. tt_int_op(g1->confirmed_idx, OP_EQ, 1);
  1208. tt_int_op(g2->confirmed_idx, OP_EQ, 0);
  1209. tt_int_op(g3->confirmed_idx, OP_EQ, 2);
  1210. tt_ptr_op(smartlist_get(gs->confirmed_entry_guards, 0), OP_EQ, g2);
  1211. tt_ptr_op(smartlist_get(gs->confirmed_entry_guards, 1), OP_EQ, g1);
  1212. tt_ptr_op(smartlist_get(gs->confirmed_entry_guards, 2), OP_EQ, g3);
  1213. /* Now make sure we can regenerate the confirmed_entry_guards list if
  1214. * the indices are messed up. */
  1215. g1->confirmed_idx = g2->confirmed_idx = g3->confirmed_idx = 999;
  1216. smartlist_clear(gs->confirmed_entry_guards);
  1217. entry_guards_update_confirmed(gs);
  1218. tt_int_op(g1->confirmed_idx, OP_GE, 0);
  1219. tt_int_op(g2->confirmed_idx, OP_GE, 0);
  1220. tt_int_op(g3->confirmed_idx, OP_GE, 0);
  1221. tt_int_op(g1->confirmed_idx, OP_LE, 2);
  1222. tt_int_op(g2->confirmed_idx, OP_LE, 2);
  1223. tt_int_op(g3->confirmed_idx, OP_LE, 2);
  1224. g1 = smartlist_get(gs->confirmed_entry_guards, 0);
  1225. g2 = smartlist_get(gs->confirmed_entry_guards, 1);
  1226. g3 = smartlist_get(gs->confirmed_entry_guards, 2);
  1227. tt_int_op(g1->confirmed_idx, OP_EQ, 0);
  1228. tt_int_op(g2->confirmed_idx, OP_EQ, 1);
  1229. tt_int_op(g3->confirmed_idx, OP_EQ, 2);
  1230. tt_assert(g1 != g2);
  1231. tt_assert(g1 != g3);
  1232. tt_assert(g2 != g3);
  1233. done:
  1234. UNMOCK(randomize_time);
  1235. guard_selection_free(gs);
  1236. }
  1237. static void
  1238. test_entry_guard_sample_reachable_filtered(void *arg)
  1239. {
  1240. (void)arg;
  1241. guard_selection_t *gs = guard_selection_new("default", GS_TYPE_NORMAL);
  1242. entry_guards_expand_sample(gs);
  1243. const int N = 10000;
  1244. bitarray_t *selected = NULL;
  1245. int i, j;
  1246. /* We've got a sampled list now; let's make one non-usable-filtered; some
  1247. * confirmed, some primary, some pending.
  1248. */
  1249. int n_guards = smartlist_len(gs->sampled_entry_guards);
  1250. tt_int_op(n_guards, OP_GT, 10);
  1251. entry_guard_t *g;
  1252. g = smartlist_get(gs->sampled_entry_guards, 0);
  1253. g->is_pending = 1;
  1254. g = smartlist_get(gs->sampled_entry_guards, 1);
  1255. make_guard_confirmed(gs, g);
  1256. g = smartlist_get(gs->sampled_entry_guards, 2);
  1257. g->is_primary = 1;
  1258. g = smartlist_get(gs->sampled_entry_guards, 3);
  1259. g->pb.path_bias_disabled = 1;
  1260. entry_guards_update_filtered_sets(gs);
  1261. gs->primary_guards_up_to_date = 1;
  1262. tt_int_op(num_reachable_filtered_guards(gs, NULL), OP_EQ, n_guards - 1);
  1263. tt_int_op(smartlist_len(gs->sampled_entry_guards), OP_EQ, n_guards);
  1264. // +1 since the one we made disabled will make another one get added.
  1265. ++n_guards;
  1266. /* Try a bunch of selections. */
  1267. const struct {
  1268. int flag; int idx;
  1269. } tests[] = {
  1270. { 0, -1 },
  1271. { SAMPLE_EXCLUDE_CONFIRMED, 1 },
  1272. { SAMPLE_EXCLUDE_PRIMARY|SAMPLE_NO_UPDATE_PRIMARY, 2 },
  1273. { SAMPLE_EXCLUDE_PENDING, 0 },
  1274. { -1, -1},
  1275. };
  1276. for (j = 0; tests[j].flag >= 0; ++j) {
  1277. selected = bitarray_init_zero(n_guards);
  1278. const int excluded_flags = tests[j].flag;
  1279. const int excluded_idx = tests[j].idx;
  1280. for (i = 0; i < N; ++i) {
  1281. g = sample_reachable_filtered_entry_guards(gs, NULL, excluded_flags);
  1282. tor_assert(g);
  1283. int pos = smartlist_pos(gs->sampled_entry_guards, g);
  1284. tt_int_op(smartlist_len(gs->sampled_entry_guards), OP_EQ, n_guards);
  1285. tt_int_op(pos, OP_GE, 0);
  1286. tt_int_op(pos, OP_LT, n_guards);
  1287. bitarray_set(selected, pos);
  1288. }
  1289. for (i = 0; i < n_guards; ++i) {
  1290. const int should_be_set = (i != excluded_idx &&
  1291. i != 3); // filtered out.
  1292. tt_int_op(!!bitarray_is_set(selected, i), OP_EQ, should_be_set);
  1293. }
  1294. bitarray_free(selected);
  1295. selected = NULL;
  1296. }
  1297. done:
  1298. guard_selection_free(gs);
  1299. bitarray_free(selected);
  1300. }
  1301. static void
  1302. test_entry_guard_sample_reachable_filtered_empty(void *arg)
  1303. {
  1304. (void)arg;
  1305. guard_selection_t *gs = guard_selection_new("default", GS_TYPE_NORMAL);
  1306. /* What if we try to sample from a set of 0? */
  1307. SMARTLIST_FOREACH(big_fake_net_nodes, node_t *, n,
  1308. n->is_possible_guard = 0);
  1309. entry_guard_t *g = sample_reachable_filtered_entry_guards(gs, NULL, 0);
  1310. tt_ptr_op(g, OP_EQ, NULL);
  1311. done:
  1312. guard_selection_free(gs);
  1313. }
  1314. static void
  1315. test_entry_guard_retry_unreachable(void *arg)
  1316. {
  1317. (void)arg;
  1318. guard_selection_t *gs = guard_selection_new("default", GS_TYPE_NORMAL);
  1319. entry_guards_expand_sample(gs);
  1320. /* Let's say that we have two guards, and they're down.
  1321. */
  1322. time_t start = approx_time();;
  1323. entry_guard_t *g1 = smartlist_get(gs->sampled_entry_guards, 0);
  1324. entry_guard_t *g2 = smartlist_get(gs->sampled_entry_guards, 1);
  1325. entry_guard_t *g3 = smartlist_get(gs->sampled_entry_guards, 2);
  1326. g1->is_reachable = GUARD_REACHABLE_NO;
  1327. g2->is_reachable = GUARD_REACHABLE_NO;
  1328. g1->is_primary = 1;
  1329. g1->failing_since = g2->failing_since = start;
  1330. g1->last_tried_to_connect = g2->last_tried_to_connect = start;
  1331. /* Wait 5 minutes. Nothing will get retried. */
  1332. update_approx_time(start + 5 * 60);
  1333. entry_guard_consider_retry(g1);
  1334. entry_guard_consider_retry(g2);
  1335. entry_guard_consider_retry(g3); // just to make sure this doesn't crash.
  1336. tt_int_op(g1->is_reachable, OP_EQ, GUARD_REACHABLE_NO);
  1337. tt_int_op(g2->is_reachable, OP_EQ, GUARD_REACHABLE_NO);
  1338. tt_int_op(g3->is_reachable, OP_EQ, GUARD_REACHABLE_MAYBE);
  1339. /* After 30 min, the primary one gets retried */
  1340. update_approx_time(start + 35 * 60);
  1341. entry_guard_consider_retry(g1);
  1342. entry_guard_consider_retry(g2);
  1343. tt_int_op(g1->is_reachable, OP_EQ, GUARD_REACHABLE_MAYBE);
  1344. tt_int_op(g2->is_reachable, OP_EQ, GUARD_REACHABLE_NO);
  1345. g1->is_reachable = GUARD_REACHABLE_NO;
  1346. g1->last_tried_to_connect = start + 55*60;
  1347. /* After 1 hour, we'll retry the nonprimary one. */
  1348. update_approx_time(start + 61 * 60);
  1349. entry_guard_consider_retry(g1);
  1350. entry_guard_consider_retry(g2);
  1351. tt_int_op(g1->is_reachable, OP_EQ, GUARD_REACHABLE_NO);
  1352. tt_int_op(g2->is_reachable, OP_EQ, GUARD_REACHABLE_MAYBE);
  1353. g2->is_reachable = GUARD_REACHABLE_NO;
  1354. g2->last_tried_to_connect = start + 61*60;
  1355. /* And then the primary one again. */
  1356. update_approx_time(start + 66 * 60);
  1357. entry_guard_consider_retry(g1);
  1358. entry_guard_consider_retry(g2);
  1359. tt_int_op(g1->is_reachable, OP_EQ, GUARD_REACHABLE_MAYBE);
  1360. tt_int_op(g2->is_reachable, OP_EQ, GUARD_REACHABLE_NO);
  1361. done:
  1362. guard_selection_free(gs);
  1363. }
  1364. static void
  1365. test_entry_guard_manage_primary(void *arg)
  1366. {
  1367. (void)arg;
  1368. guard_selection_t *gs = guard_selection_new("default", GS_TYPE_NORMAL);
  1369. smartlist_t *prev_guards = smartlist_new();
  1370. /* If no guards are confirmed, we should pick a few reachable guards and
  1371. * call them all primary. But not confirmed.*/
  1372. entry_guards_update_primary(gs);
  1373. int n_primary = smartlist_len(gs->primary_entry_guards);
  1374. tt_int_op(n_primary, OP_GE, 1);
  1375. SMARTLIST_FOREACH(gs->primary_entry_guards, entry_guard_t *, g, {
  1376. tt_assert(g->is_primary);
  1377. tt_assert(g->confirmed_idx == -1);
  1378. });
  1379. /* Calling it a second time should leave the guards unchanged. */
  1380. smartlist_add_all(prev_guards, gs->primary_entry_guards);
  1381. entry_guards_update_primary(gs);
  1382. tt_int_op(smartlist_len(gs->primary_entry_guards), OP_EQ, n_primary);
  1383. SMARTLIST_FOREACH(gs->primary_entry_guards, entry_guard_t *, g, {
  1384. tt_ptr_op(g, OP_EQ, smartlist_get(prev_guards, g_sl_idx));
  1385. });
  1386. /* If we have one confirmed guard, that guards becomes the first primary
  1387. * guard, and the other primary guards get kept. */
  1388. /* find a non-primary guard... */
  1389. entry_guard_t *confirmed = NULL;
  1390. SMARTLIST_FOREACH(gs->sampled_entry_guards, entry_guard_t *, g, {
  1391. if (! g->is_primary) {
  1392. confirmed = g;
  1393. break;
  1394. }
  1395. });
  1396. tt_assert(confirmed);
  1397. /* make it confirmed. */
  1398. make_guard_confirmed(gs, confirmed);
  1399. /* update the list... */
  1400. smartlist_clear(prev_guards);
  1401. smartlist_add_all(prev_guards, gs->primary_entry_guards);
  1402. entry_guards_update_primary(gs);
  1403. /* and see what's primary now! */
  1404. tt_int_op(smartlist_len(gs->primary_entry_guards), OP_EQ, n_primary);
  1405. tt_ptr_op(smartlist_get(gs->primary_entry_guards, 0), OP_EQ, confirmed);
  1406. SMARTLIST_FOREACH(gs->primary_entry_guards, entry_guard_t *, g, {
  1407. tt_assert(g->is_primary);
  1408. if (g_sl_idx == 0)
  1409. continue;
  1410. tt_ptr_op(g, OP_EQ, smartlist_get(prev_guards, g_sl_idx - 1));
  1411. });
  1412. {
  1413. entry_guard_t *prev_last_guard = smartlist_get(prev_guards, n_primary-1);
  1414. tt_assert(! prev_last_guard->is_primary);
  1415. }
  1416. /* Calling it a fourth time should leave the guards unchanged. */
  1417. smartlist_clear(prev_guards);
  1418. smartlist_add_all(prev_guards, gs->primary_entry_guards);
  1419. entry_guards_update_primary(gs);
  1420. tt_int_op(smartlist_len(gs->primary_entry_guards), OP_EQ, n_primary);
  1421. SMARTLIST_FOREACH(gs->primary_entry_guards, entry_guard_t *, g, {
  1422. tt_ptr_op(g, OP_EQ, smartlist_get(prev_guards, g_sl_idx));
  1423. });
  1424. done:
  1425. guard_selection_free(gs);
  1426. smartlist_free(prev_guards);
  1427. }
  1428. static void
  1429. test_entry_guard_guard_preferred(void *arg)
  1430. {
  1431. (void) arg;
  1432. entry_guard_t *g1 = tor_malloc_zero(sizeof(entry_guard_t));
  1433. entry_guard_t *g2 = tor_malloc_zero(sizeof(entry_guard_t));
  1434. g1->confirmed_idx = g2->confirmed_idx = -1;
  1435. g1->last_tried_to_connect = approx_time();
  1436. g2->last_tried_to_connect = approx_time();
  1437. tt_int_op(0, OP_EQ, entry_guard_has_higher_priority(g1, g1));
  1438. /* Neither is pending; priorities equal. */
  1439. tt_int_op(0, OP_EQ, entry_guard_has_higher_priority(g2, g1));
  1440. tt_int_op(0, OP_EQ, entry_guard_has_higher_priority(g1, g2));
  1441. /* If one is pending, the pending one has higher priority */
  1442. g1->is_pending = 1;
  1443. tt_int_op(1, OP_EQ, entry_guard_has_higher_priority(g1, g2));
  1444. tt_int_op(0, OP_EQ, entry_guard_has_higher_priority(g2, g1));
  1445. /* If both are pending, and last_tried_to_connect is equal:
  1446. priorities equal */
  1447. g2->is_pending = 1;
  1448. tt_int_op(0, OP_EQ, entry_guard_has_higher_priority(g2, g1));
  1449. tt_int_op(0, OP_EQ, entry_guard_has_higher_priority(g1, g2));
  1450. /* One had a connection that startied earlier: it has higher priority. */
  1451. g2->last_tried_to_connect -= 10;
  1452. tt_int_op(1, OP_EQ, entry_guard_has_higher_priority(g2, g1));
  1453. tt_int_op(0, OP_EQ, entry_guard_has_higher_priority(g1, g2));
  1454. /* Now, say that g1 is confirmed. It will get higher priority. */
  1455. g1->confirmed_idx = 5;
  1456. tt_int_op(0, OP_EQ, entry_guard_has_higher_priority(g2, g1));
  1457. tt_int_op(1, OP_EQ, entry_guard_has_higher_priority(g1, g2));
  1458. /* But if g2 was confirmed first, it will get priority */
  1459. g2->confirmed_idx = 2;
  1460. tt_int_op(1, OP_EQ, entry_guard_has_higher_priority(g2, g1));
  1461. tt_int_op(0, OP_EQ, entry_guard_has_higher_priority(g1, g2));
  1462. done:
  1463. tor_free(g1);
  1464. tor_free(g2);
  1465. }
  1466. static void
  1467. test_entry_guard_select_for_circuit_no_confirmed(void *arg)
  1468. {
  1469. /* Simpler cases: no gaurds are confirmed yet. */
  1470. (void)arg;
  1471. guard_selection_t *gs = guard_selection_new("default", GS_TYPE_NORMAL);
  1472. /* simple starting configuration */
  1473. entry_guards_update_primary(gs);
  1474. unsigned state = 9999;
  1475. entry_guard_t *g = select_entry_guard_for_circuit(gs, GUARD_USAGE_TRAFFIC,
  1476. NULL, &state);
  1477. tt_assert(g);
  1478. tt_assert(g->is_primary);
  1479. tt_int_op(g->confirmed_idx, OP_EQ, -1);
  1480. tt_assert(g->is_pending == 0); // primary implies non-pending.
  1481. tt_uint_op(state, OP_EQ, GUARD_CIRC_STATE_USABLE_ON_COMPLETION);
  1482. tt_i64_op(g->last_tried_to_connect, OP_EQ, approx_time());
  1483. // If we do that again, we should get the same guard.
  1484. entry_guard_t *g2 = select_entry_guard_for_circuit(gs, GUARD_USAGE_TRAFFIC,
  1485. NULL, &state);
  1486. tt_ptr_op(g2, OP_EQ, g);
  1487. // if we mark that guard down, we should get a different primary guard.
  1488. // auto-retry it.
  1489. g->is_reachable = GUARD_REACHABLE_NO;
  1490. g->failing_since = approx_time() - 10;
  1491. g->last_tried_to_connect = approx_time() - 10;
  1492. state = 9999;
  1493. g2 = select_entry_guard_for_circuit(gs, GUARD_USAGE_TRAFFIC, NULL, &state);
  1494. tt_ptr_op(g2, OP_NE, g);
  1495. tt_assert(g2);
  1496. tt_assert(g2->is_primary);
  1497. tt_int_op(g2->confirmed_idx, OP_EQ, -1);
  1498. tt_assert(g2->is_pending == 0); // primary implies non-pending.
  1499. tt_uint_op(state, OP_EQ, GUARD_CIRC_STATE_USABLE_ON_COMPLETION);
  1500. tt_i64_op(g2->last_tried_to_connect, OP_EQ, approx_time());
  1501. // If we say that the first primary guard was last tried a long time ago, we
  1502. // should get an automatic retry on it.
  1503. g->failing_since = approx_time() - 72*60*60;
  1504. g->last_tried_to_connect = approx_time() - 72*60*60;
  1505. state = 9999;
  1506. g2 = select_entry_guard_for_circuit(gs, GUARD_USAGE_TRAFFIC, NULL, &state);
  1507. tt_ptr_op(g2, OP_EQ, g);
  1508. tt_assert(g2);
  1509. tt_uint_op(state, OP_EQ, GUARD_CIRC_STATE_USABLE_ON_COMPLETION);
  1510. tt_i64_op(g2->last_tried_to_connect, OP_EQ, approx_time());
  1511. tt_int_op(g2->is_reachable, OP_EQ, GUARD_REACHABLE_MAYBE);
  1512. // And if we mark ALL the primary guards down, we should get another guard
  1513. // at random.
  1514. SMARTLIST_FOREACH(gs->primary_entry_guards, entry_guard_t *, guard, {
  1515. guard->is_reachable = GUARD_REACHABLE_NO;
  1516. guard->last_tried_to_connect = approx_time() - 5;
  1517. guard->failing_since = approx_time() - 30;
  1518. });
  1519. state = 9999;
  1520. g2 = select_entry_guard_for_circuit(gs, GUARD_USAGE_TRAFFIC, NULL, &state);
  1521. tt_assert(g2);
  1522. tt_assert(!g2->is_primary);
  1523. tt_int_op(g2->confirmed_idx, OP_EQ, -1);
  1524. tt_assert(g2->is_pending == 1);
  1525. tt_uint_op(state, OP_EQ, GUARD_CIRC_STATE_USABLE_IF_NO_BETTER_GUARD);
  1526. tt_i64_op(g2->last_tried_to_connect, OP_EQ, approx_time());
  1527. tt_int_op(g2->is_reachable, OP_EQ, GUARD_REACHABLE_MAYBE);
  1528. // As a bonus, maybe we should be retrying the primary guards. Let's say so.
  1529. mark_primary_guards_maybe_reachable(gs);
  1530. SMARTLIST_FOREACH(gs->primary_entry_guards, entry_guard_t *, guard, {
  1531. tt_int_op(guard->is_reachable, OP_EQ, GUARD_REACHABLE_MAYBE);
  1532. tt_assert(guard->is_usable_filtered_guard == 1);
  1533. // no change to these fields.
  1534. tt_i64_op(guard->last_tried_to_connect, OP_EQ, approx_time() - 5);
  1535. tt_i64_op(guard->failing_since, OP_EQ, approx_time() - 30);
  1536. });
  1537. /* Let's try again and we should get the first primary guard again */
  1538. g = select_entry_guard_for_circuit(gs, GUARD_USAGE_TRAFFIC, NULL, &state);
  1539. tt_ptr_op(g, OP_EQ, smartlist_get(gs->primary_entry_guards, 0));
  1540. g2 = select_entry_guard_for_circuit(gs, GUARD_USAGE_TRAFFIC, NULL, &state);
  1541. tt_ptr_op(g2, OP_EQ, g);
  1542. /* But if we impose a restriction, we don't get the same guard */
  1543. entry_guard_restriction_t rst;
  1544. memset(&rst, 0, sizeof(rst));
  1545. memcpy(rst.exclude_id, g->identity, DIGEST_LEN);
  1546. g2 = select_entry_guard_for_circuit(gs, GUARD_USAGE_TRAFFIC, &rst, &state);
  1547. tt_ptr_op(g2, OP_NE, g);
  1548. done:
  1549. guard_selection_free(gs);
  1550. }
  1551. static void
  1552. test_entry_guard_select_for_circuit_confirmed(void *arg)
  1553. {
  1554. /* Case 2: if all the primary guards are down, and there are more confirmed
  1555. guards, we use a confirmed guard. */
  1556. (void)arg;
  1557. int i;
  1558. guard_selection_t *gs = guard_selection_new("default", GS_TYPE_NORMAL);
  1559. const int N_CONFIRMED = 10;
  1560. /* slightly more complicated simple starting configuration */
  1561. entry_guards_update_primary(gs);
  1562. for (i = 0; i < N_CONFIRMED; ++i) {
  1563. entry_guard_t *guard = smartlist_get(gs->sampled_entry_guards, i);
  1564. make_guard_confirmed(gs, guard);
  1565. }
  1566. entry_guards_update_primary(gs); // rebuild the primary list.
  1567. unsigned state = 9999;
  1568. // As above, this gives us a primary guard.
  1569. entry_guard_t *g = select_entry_guard_for_circuit(gs, GUARD_USAGE_TRAFFIC,
  1570. NULL, &state);
  1571. tt_assert(g);
  1572. tt_assert(g->is_primary);
  1573. tt_int_op(g->confirmed_idx, OP_EQ, 0);
  1574. tt_assert(g->is_pending == 0); // primary implies non-pending.
  1575. tt_uint_op(state, OP_EQ, GUARD_CIRC_STATE_USABLE_ON_COMPLETION);
  1576. tt_i64_op(g->last_tried_to_connect, OP_EQ, approx_time());
  1577. tt_ptr_op(g, OP_EQ, smartlist_get(gs->primary_entry_guards, 0));
  1578. // But if we mark all the primary guards down...
  1579. SMARTLIST_FOREACH(gs->primary_entry_guards, entry_guard_t *, guard, {
  1580. guard->last_tried_to_connect = approx_time();
  1581. entry_guards_note_guard_failure(gs, guard);
  1582. });
  1583. // ... we should get a confirmed guard.
  1584. state = 9999;
  1585. g = select_entry_guard_for_circuit(gs, GUARD_USAGE_TRAFFIC, NULL, &state);
  1586. tt_assert(g);
  1587. tt_assert(! g->is_primary);
  1588. tt_int_op(g->confirmed_idx, OP_EQ, smartlist_len(gs->primary_entry_guards));
  1589. tt_assert(g->is_pending);
  1590. tt_uint_op(state, OP_EQ, GUARD_CIRC_STATE_USABLE_IF_NO_BETTER_GUARD);
  1591. tt_i64_op(g->last_tried_to_connect, OP_EQ, approx_time());
  1592. // And if we try again, we should get a different confirmed guard, since
  1593. // that one is pending.
  1594. state = 9999;
  1595. entry_guard_t *g2 = select_entry_guard_for_circuit(gs, GUARD_USAGE_TRAFFIC,
  1596. NULL, &state);
  1597. tt_assert(g2);
  1598. tt_assert(! g2->is_primary);
  1599. tt_ptr_op(g2, OP_NE, g);
  1600. tt_int_op(g2->confirmed_idx, OP_EQ,
  1601. smartlist_len(gs->primary_entry_guards)+1);
  1602. tt_assert(g2->is_pending);
  1603. tt_uint_op(state, OP_EQ, GUARD_CIRC_STATE_USABLE_IF_NO_BETTER_GUARD);
  1604. tt_i64_op(g2->last_tried_to_connect, OP_EQ, approx_time());
  1605. // If we say that the next confirmed guard in order is excluded, and
  1606. // we disable EnforceDistinctSubnets, we get the guard AFTER the
  1607. // one we excluded.
  1608. get_options_mutable()->EnforceDistinctSubnets = 0;
  1609. g = smartlist_get(gs->confirmed_entry_guards,
  1610. smartlist_len(gs->primary_entry_guards)+2);
  1611. entry_guard_restriction_t rst;
  1612. memset(&rst, 0, sizeof(rst));
  1613. memcpy(rst.exclude_id, g->identity, DIGEST_LEN);
  1614. g2 = select_entry_guard_for_circuit(gs, GUARD_USAGE_TRAFFIC, &rst, &state);
  1615. tt_ptr_op(g2, OP_NE, NULL);
  1616. tt_ptr_op(g2, OP_NE, g);
  1617. tt_int_op(g2->confirmed_idx, OP_EQ,
  1618. smartlist_len(gs->primary_entry_guards)+3);
  1619. // If we make every confirmed guard become pending then we start poking
  1620. // other guards.
  1621. const int n_remaining_confirmed =
  1622. N_CONFIRMED - 3 - smartlist_len(gs->primary_entry_guards);
  1623. for (i = 0; i < n_remaining_confirmed; ++i) {
  1624. g = select_entry_guard_for_circuit(gs, GUARD_USAGE_TRAFFIC, NULL, &state);
  1625. tt_int_op(g->confirmed_idx, OP_GE, 0);
  1626. tt_assert(g);
  1627. }
  1628. state = 9999;
  1629. g = select_entry_guard_for_circuit(gs, GUARD_USAGE_TRAFFIC, NULL, &state);
  1630. tt_assert(g);
  1631. tt_assert(g->is_pending);
  1632. tt_int_op(g->confirmed_idx, OP_EQ, -1);
  1633. // If we EnforceDistinctSubnets and apply a restriction, we get
  1634. // nothing, since we put all of the nodes in the same /16.
  1635. // Regression test for bug 22753/TROVE-2017-006.
  1636. get_options_mutable()->EnforceDistinctSubnets = 1;
  1637. g = smartlist_get(gs->confirmed_entry_guards, 0);
  1638. memset(&rst, 0, sizeof(rst));
  1639. memcpy(rst.exclude_id, g->identity, DIGEST_LEN);
  1640. g2 = select_entry_guard_for_circuit(gs, GUARD_USAGE_TRAFFIC, &rst, &state);
  1641. tt_ptr_op(g2, OP_EQ, NULL);
  1642. done:
  1643. guard_selection_free(gs);
  1644. }
  1645. static void
  1646. test_entry_guard_select_for_circuit_highlevel_primary(void *arg)
  1647. {
  1648. /* Play around with selecting primary guards for circuits and markign
  1649. * them up and down */
  1650. (void)arg;
  1651. guard_selection_t *gs = guard_selection_new("default", GS_TYPE_NORMAL);
  1652. time_t start = approx_time();
  1653. const node_t *node = NULL;
  1654. circuit_guard_state_t *guard = NULL;
  1655. entry_guard_t *g;
  1656. guard_usable_t u;
  1657. /*
  1658. * Make sure that the pick-for-circuit API basically works. We'll get
  1659. * a primary guard, so it'll be usable on completion.
  1660. */
  1661. int r = entry_guard_pick_for_circuit(gs, GUARD_USAGE_TRAFFIC, NULL,
  1662. &node, &guard);
  1663. tt_assert(r == 0);
  1664. tt_assert(node);
  1665. tt_assert(guard);
  1666. tt_int_op(guard->state, OP_EQ, GUARD_CIRC_STATE_USABLE_ON_COMPLETION);
  1667. g = entry_guard_handle_get(guard->guard);
  1668. tt_assert(g);
  1669. tt_mem_op(g->identity, OP_EQ, node->identity, DIGEST_LEN);
  1670. tt_int_op(g->is_primary, OP_EQ, 1);
  1671. tt_i64_op(g->last_tried_to_connect, OP_EQ, start);
  1672. tt_int_op(g->confirmed_idx, OP_EQ, -1);
  1673. /* Call that circuit successful. */
  1674. update_approx_time(start+15);
  1675. u = entry_guard_succeeded(&guard);
  1676. tt_int_op(u, OP_EQ, GUARD_USABLE_NOW); /* We can use it now. */
  1677. tt_assert(guard);
  1678. tt_int_op(guard->state, OP_EQ, GUARD_CIRC_STATE_COMPLETE);
  1679. g = entry_guard_handle_get(guard->guard);
  1680. tt_assert(g);
  1681. tt_int_op(g->is_reachable, OP_EQ, GUARD_REACHABLE_YES);
  1682. tt_int_op(g->confirmed_idx, OP_EQ, 0);
  1683. circuit_guard_state_free(guard);
  1684. guard = NULL;
  1685. node = NULL;
  1686. g = NULL;
  1687. /* Try again. We'll also get a primary guard this time. (The same one,
  1688. in fact.) But this time, we'll say the connection has failed. */
  1689. update_approx_time(start+35);
  1690. r = entry_guard_pick_for_circuit(gs, GUARD_USAGE_TRAFFIC, NULL,
  1691. &node, &guard);
  1692. tt_assert(r == 0);
  1693. tt_assert(node);
  1694. tt_assert(guard);
  1695. tt_int_op(guard->state, OP_EQ, GUARD_CIRC_STATE_USABLE_ON_COMPLETION);
  1696. tt_i64_op(guard->state_set_at, OP_EQ, start+35);
  1697. g = entry_guard_handle_get(guard->guard);
  1698. tt_assert(g);
  1699. tt_mem_op(g->identity, OP_EQ, node->identity, DIGEST_LEN);
  1700. tt_int_op(g->is_primary, OP_EQ, 1);
  1701. tt_i64_op(g->last_tried_to_connect, OP_EQ, start+35);
  1702. tt_int_op(g->confirmed_idx, OP_EQ, 0); // same one.
  1703. /* It's failed! What will happen to our poor guard? */
  1704. update_approx_time(start+45);
  1705. entry_guard_failed(&guard);
  1706. tt_assert(guard);
  1707. tt_int_op(guard->state, OP_EQ, GUARD_CIRC_STATE_DEAD);
  1708. tt_i64_op(guard->state_set_at, OP_EQ, start+45);
  1709. g = entry_guard_handle_get(guard->guard);
  1710. tt_assert(g);
  1711. tt_int_op(g->is_reachable, OP_EQ, GUARD_REACHABLE_NO);
  1712. tt_i64_op(g->failing_since, OP_EQ, start+45);
  1713. tt_int_op(g->confirmed_idx, OP_EQ, 0); // still confirmed.
  1714. circuit_guard_state_free(guard);
  1715. guard = NULL;
  1716. node = NULL;
  1717. entry_guard_t *g_prev = g;
  1718. g = NULL;
  1719. /* Now try a third time. Since the other one is down, we'll get a different
  1720. * (still primary) guard.
  1721. */
  1722. update_approx_time(start+60);
  1723. r = entry_guard_pick_for_circuit(gs, GUARD_USAGE_TRAFFIC, NULL,
  1724. &node, &guard);
  1725. tt_assert(r == 0);
  1726. tt_assert(node);
  1727. tt_assert(guard);
  1728. tt_int_op(guard->state, OP_EQ, GUARD_CIRC_STATE_USABLE_ON_COMPLETION);
  1729. g = entry_guard_handle_get(guard->guard);
  1730. tt_assert(g);
  1731. tt_ptr_op(g, OP_NE, g_prev);
  1732. tt_mem_op(g->identity, OP_EQ, node->identity, DIGEST_LEN);
  1733. tt_mem_op(g->identity, OP_NE, g_prev->identity, DIGEST_LEN);
  1734. tt_int_op(g->is_primary, OP_EQ, 1);
  1735. tt_i64_op(g->last_tried_to_connect, OP_EQ, start+60);
  1736. tt_int_op(g->confirmed_idx, OP_EQ, -1); // not confirmd now.
  1737. /* Call this one up; watch it get confirmed. */
  1738. update_approx_time(start+90);
  1739. u = entry_guard_succeeded(&guard);
  1740. tt_int_op(u, OP_EQ, GUARD_USABLE_NOW);
  1741. tt_assert(guard);
  1742. tt_int_op(guard->state, OP_EQ, GUARD_CIRC_STATE_COMPLETE);
  1743. g = entry_guard_handle_get(guard->guard);
  1744. tt_assert(g);
  1745. tt_int_op(g->is_reachable, OP_EQ, GUARD_REACHABLE_YES);
  1746. tt_int_op(g->confirmed_idx, OP_EQ, 1);
  1747. done:
  1748. guard_selection_free(gs);
  1749. circuit_guard_state_free(guard);
  1750. }
  1751. static void
  1752. test_entry_guard_select_for_circuit_highlevel_confirm_other(void *arg)
  1753. {
  1754. (void) arg;
  1755. const int N_PRIMARY = DFLT_N_PRIMARY_GUARDS;
  1756. /* At the start, we have no confirmed guards. We'll mark the primary guards
  1757. * down, then confirm something else. As soon as we do, it should become
  1758. * primary, and we should get it next time. */
  1759. time_t start = approx_time();
  1760. guard_selection_t *gs = guard_selection_new("default", GS_TYPE_NORMAL);
  1761. circuit_guard_state_t *guard = NULL;
  1762. int i, r;
  1763. const node_t *node = NULL;
  1764. guard_usable_t u;
  1765. /* Declare that we're on the internet. */
  1766. entry_guards_note_internet_connectivity(gs);
  1767. /* Primary guards are down! */
  1768. for (i = 0; i < N_PRIMARY; ++i) {
  1769. r = entry_guard_pick_for_circuit(gs, GUARD_USAGE_TRAFFIC, NULL,
  1770. &node, &guard);
  1771. tt_assert(node);
  1772. tt_assert(guard);
  1773. tt_assert(r == 0);
  1774. tt_int_op(guard->state, OP_EQ, GUARD_CIRC_STATE_USABLE_ON_COMPLETION);
  1775. entry_guard_failed(&guard);
  1776. circuit_guard_state_free(guard);
  1777. guard = NULL;
  1778. node = NULL;
  1779. }
  1780. /* Next guard should be non-primary. */
  1781. node = NULL;
  1782. r = entry_guard_pick_for_circuit(gs, GUARD_USAGE_TRAFFIC, NULL,
  1783. &node, &guard);
  1784. tt_assert(node);
  1785. tt_assert(guard);
  1786. tt_assert(r == 0);
  1787. entry_guard_t *g = entry_guard_handle_get(guard->guard);
  1788. tt_assert(g);
  1789. tt_int_op(guard->state, OP_EQ, GUARD_CIRC_STATE_USABLE_IF_NO_BETTER_GUARD);
  1790. tt_int_op(g->confirmed_idx, OP_EQ, -1);
  1791. tt_int_op(g->is_primary, OP_EQ, 0);
  1792. tt_int_op(g->is_pending, OP_EQ, 1);
  1793. (void)start;
  1794. u = entry_guard_succeeded(&guard);
  1795. /* We're on the internet (by fiat), so this guard will get called "confirmed"
  1796. * and should immediately become primary.
  1797. */
  1798. tt_int_op(guard->state, OP_EQ, GUARD_CIRC_STATE_COMPLETE);
  1799. tt_assert(u == GUARD_USABLE_NOW);
  1800. tt_int_op(g->confirmed_idx, OP_EQ, 0);
  1801. tt_int_op(g->is_primary, OP_EQ, 1);
  1802. tt_int_op(g->is_pending, OP_EQ, 0);
  1803. done:
  1804. guard_selection_free(gs);
  1805. circuit_guard_state_free(guard);
  1806. }
  1807. static void
  1808. test_entry_guard_select_for_circuit_highlevel_primary_retry(void *arg)
  1809. {
  1810. (void) arg;
  1811. const int N_PRIMARY = DFLT_N_PRIMARY_GUARDS;
  1812. /* At the start, we have no confirmed guards. We'll mark the primary guards
  1813. * down, then confirm something else. As soon as we do, it should become
  1814. * primary, and we should get it next time. */
  1815. time_t start = approx_time();
  1816. guard_selection_t *gs = guard_selection_new("default", GS_TYPE_NORMAL);
  1817. circuit_guard_state_t *guard = NULL, *guard2 = NULL;
  1818. int i, r;
  1819. const node_t *node = NULL;
  1820. entry_guard_t *g;
  1821. guard_usable_t u;
  1822. /* Declare that we're on the internet. */
  1823. entry_guards_note_internet_connectivity(gs);
  1824. /* Make primary guards confirmed (so they won't be superseded by a later
  1825. * guard), then mark them down. */
  1826. for (i = 0; i < N_PRIMARY; ++i) {
  1827. r = entry_guard_pick_for_circuit(gs, GUARD_USAGE_TRAFFIC, NULL,
  1828. &node, &guard);
  1829. tt_assert(node);
  1830. tt_assert(guard);
  1831. tt_assert(r == 0);
  1832. tt_int_op(guard->state, OP_EQ, GUARD_CIRC_STATE_USABLE_ON_COMPLETION);
  1833. g = entry_guard_handle_get(guard->guard);
  1834. make_guard_confirmed(gs, g);
  1835. tt_int_op(g->is_primary, OP_EQ, 1);
  1836. entry_guard_failed(&guard);
  1837. circuit_guard_state_free(guard);
  1838. tt_int_op(g->is_reachable, OP_EQ, GUARD_REACHABLE_NO);
  1839. guard = NULL;
  1840. node = NULL;
  1841. }
  1842. /* Get another guard that we might try. */
  1843. r = entry_guard_pick_for_circuit(gs, GUARD_USAGE_TRAFFIC, NULL,
  1844. &node, &guard);
  1845. tt_assert(node);
  1846. tt_assert(guard);
  1847. tt_assert(r == 0);
  1848. tt_int_op(guard->state, OP_EQ, GUARD_CIRC_STATE_USABLE_IF_NO_BETTER_GUARD);
  1849. g = entry_guard_handle_get(guard->guard);
  1850. tt_int_op(g->is_primary, OP_EQ, 0);
  1851. tt_assert(entry_guards_all_primary_guards_are_down(gs));
  1852. /* And an hour has passed ... */
  1853. update_approx_time(start + 3600);
  1854. /* Say that guard has succeeded! */
  1855. u = entry_guard_succeeded(&guard);
  1856. tt_int_op(u, OP_EQ, GUARD_MAYBE_USABLE_LATER);
  1857. tt_int_op(guard->state, OP_EQ, GUARD_CIRC_STATE_WAITING_FOR_BETTER_GUARD);
  1858. g = entry_guard_handle_get(guard->guard);
  1859. /* The primary guards should have been marked up! */
  1860. SMARTLIST_FOREACH(gs->primary_entry_guards, entry_guard_t *, pg, {
  1861. tt_int_op(pg->is_primary, OP_EQ, 1);
  1862. tt_ptr_op(g, OP_NE, pg);
  1863. tt_int_op(pg->is_reachable, OP_EQ, GUARD_REACHABLE_MAYBE);
  1864. });
  1865. /* Have a circuit to a primary guard succeed. */
  1866. r = entry_guard_pick_for_circuit(gs, GUARD_USAGE_TRAFFIC, NULL,
  1867. &node, &guard2);
  1868. tt_assert(r == 0);
  1869. tt_int_op(guard2->state, OP_EQ, GUARD_CIRC_STATE_USABLE_ON_COMPLETION);
  1870. u = entry_guard_succeeded(&guard2);
  1871. tt_assert(u == GUARD_USABLE_NOW);
  1872. tt_int_op(guard2->state, OP_EQ, GUARD_CIRC_STATE_COMPLETE);
  1873. tt_assert(! entry_guards_all_primary_guards_are_down(gs));
  1874. done:
  1875. guard_selection_free(gs);
  1876. circuit_guard_state_free(guard);
  1877. circuit_guard_state_free(guard2);
  1878. }
  1879. static void
  1880. test_entry_guard_select_and_cancel(void *arg)
  1881. {
  1882. (void) arg;
  1883. const int N_PRIMARY = DFLT_N_PRIMARY_GUARDS;
  1884. int i,r;
  1885. const node_t *node = NULL;
  1886. circuit_guard_state_t *guard;
  1887. guard_selection_t *gs = guard_selection_new("default", GS_TYPE_NORMAL);
  1888. entry_guard_t *g;
  1889. /* Once more, we mark all the primary guards down. */
  1890. entry_guards_note_internet_connectivity(gs);
  1891. for (i = 0; i < N_PRIMARY; ++i) {
  1892. r = entry_guard_pick_for_circuit(gs, GUARD_USAGE_TRAFFIC, NULL,
  1893. &node, &guard);
  1894. tt_int_op(r, OP_EQ, 0);
  1895. tt_int_op(guard->state, OP_EQ, GUARD_CIRC_STATE_USABLE_ON_COMPLETION);
  1896. g = entry_guard_handle_get(guard->guard);
  1897. tt_int_op(g->is_primary, OP_EQ, 1);
  1898. tt_int_op(g->is_pending, OP_EQ, 0);
  1899. make_guard_confirmed(gs, g);
  1900. entry_guard_failed(&guard);
  1901. circuit_guard_state_free(guard);
  1902. guard = NULL;
  1903. node = NULL;
  1904. }
  1905. tt_assert(entry_guards_all_primary_guards_are_down(gs));
  1906. /* Now get another guard we could try... */
  1907. r = entry_guard_pick_for_circuit(gs, GUARD_USAGE_TRAFFIC, NULL,
  1908. &node, &guard);
  1909. tt_assert(node);
  1910. tt_assert(guard);
  1911. tt_assert(r == 0);
  1912. tt_int_op(guard->state, OP_EQ, GUARD_CIRC_STATE_USABLE_IF_NO_BETTER_GUARD);
  1913. g = entry_guard_handle_get(guard->guard);
  1914. tt_int_op(g->is_primary, OP_EQ, 0);
  1915. tt_int_op(g->is_pending, OP_EQ, 1);
  1916. /* Whoops! We should never have asked for this guard. Cancel the request! */
  1917. entry_guard_cancel(&guard);
  1918. tt_assert(guard == NULL);
  1919. tt_int_op(g->is_primary, OP_EQ, 0);
  1920. tt_int_op(g->is_pending, OP_EQ, 0);
  1921. done:
  1922. guard_selection_free(gs);
  1923. circuit_guard_state_free(guard);
  1924. }
  1925. static void
  1926. test_entry_guard_drop_guards(void *arg)
  1927. {
  1928. (void) arg;
  1929. int r;
  1930. const node_t *node = NULL;
  1931. circuit_guard_state_t *guard;
  1932. guard_selection_t *gs = get_guard_selection_info();
  1933. // Pick a guard, to get things set up.
  1934. r = entry_guard_pick_for_circuit(gs, GUARD_USAGE_TRAFFIC, NULL,
  1935. &node, &guard);
  1936. tt_int_op(r, OP_EQ, 0);
  1937. tt_int_op(smartlist_len(gs->sampled_entry_guards), OP_GE,
  1938. DFLT_MIN_FILTERED_SAMPLE_SIZE);
  1939. tt_ptr_op(gs, OP_EQ, get_guard_selection_info());
  1940. // Drop all the guards! (This is a bad idea....)
  1941. remove_all_entry_guards_for_guard_selection(gs);
  1942. gs = get_guard_selection_info();
  1943. tt_int_op(smartlist_len(gs->sampled_entry_guards), OP_EQ, 0);
  1944. tt_int_op(smartlist_len(gs->primary_entry_guards), OP_EQ, 0);
  1945. tt_int_op(smartlist_len(gs->confirmed_entry_guards), OP_EQ, 0);
  1946. done:
  1947. circuit_guard_state_free(guard);
  1948. guard_selection_free(gs);
  1949. }
  1950. /* Unit test setup function: Create a fake network, and set everything up
  1951. * for testing the upgrade-a-waiting-circuit code. */
  1952. typedef struct {
  1953. guard_selection_t *gs;
  1954. time_t start;
  1955. circuit_guard_state_t *guard1_state;
  1956. circuit_guard_state_t *guard2_state;
  1957. entry_guard_t *guard1;
  1958. entry_guard_t *guard2;
  1959. origin_circuit_t *circ1;
  1960. origin_circuit_t *circ2;
  1961. smartlist_t *all_origin_circuits;
  1962. } upgrade_circuits_data_t;
  1963. static void *
  1964. upgrade_circuits_setup(const struct testcase_t *testcase)
  1965. {
  1966. upgrade_circuits_data_t *data = tor_malloc_zero(sizeof(*data));
  1967. guard_selection_t *gs = data->gs =
  1968. guard_selection_new("default", GS_TYPE_NORMAL);
  1969. circuit_guard_state_t *guard;
  1970. const node_t *node;
  1971. entry_guard_t *g;
  1972. int i;
  1973. const int N_PRIMARY = DFLT_N_PRIMARY_GUARDS;
  1974. const char *argument = testcase->setup_data;
  1975. const int make_circ1_succeed = strstr(argument, "c1-done") != NULL;
  1976. const int make_circ2_succeed = strstr(argument, "c2-done") != NULL;
  1977. big_fake_network_setup(testcase);
  1978. /* We're going to set things up in a state where a circuit will be ready to
  1979. * be upgraded. Each test can make a single change (or not) that should
  1980. * block the upgrade.
  1981. */
  1982. /* First, make all the primary guards confirmed, and down. */
  1983. data->start = approx_time();
  1984. entry_guards_note_internet_connectivity(gs);
  1985. for (i = 0; i < N_PRIMARY; ++i) {
  1986. entry_guard_pick_for_circuit(gs, GUARD_USAGE_TRAFFIC, NULL, &node, &guard);
  1987. g = entry_guard_handle_get(guard->guard);
  1988. make_guard_confirmed(gs, g);
  1989. entry_guard_failed(&guard);
  1990. circuit_guard_state_free(guard);
  1991. }
  1992. /* Grab another couple of guards */
  1993. data->all_origin_circuits = smartlist_new();
  1994. update_approx_time(data->start + 27);
  1995. entry_guard_pick_for_circuit(gs, GUARD_USAGE_TRAFFIC, NULL,
  1996. &node, &data->guard1_state);
  1997. origin_circuit_t *circ;
  1998. data->circ1 = circ = origin_circuit_new();
  1999. circ->base_.purpose = CIRCUIT_PURPOSE_C_GENERAL;
  2000. circ->guard_state = data->guard1_state;
  2001. smartlist_add(data->all_origin_circuits, circ);
  2002. update_approx_time(data->start + 30);
  2003. entry_guard_pick_for_circuit(gs, GUARD_USAGE_TRAFFIC, NULL,
  2004. &node, &data->guard2_state);
  2005. data->circ2 = circ = origin_circuit_new();
  2006. circ->base_.purpose = CIRCUIT_PURPOSE_C_GENERAL;
  2007. circ->guard_state = data->guard2_state;
  2008. smartlist_add(data->all_origin_circuits, circ);
  2009. data->guard1 = entry_guard_handle_get(data->guard1_state->guard);
  2010. data->guard2 = entry_guard_handle_get(data->guard2_state->guard);
  2011. tor_assert(data->guard1 != data->guard2);
  2012. tor_assert(data->guard1_state->state ==
  2013. GUARD_CIRC_STATE_USABLE_IF_NO_BETTER_GUARD);
  2014. tor_assert(data->guard2_state->state ==
  2015. GUARD_CIRC_STATE_USABLE_IF_NO_BETTER_GUARD);
  2016. guard_usable_t r;
  2017. update_approx_time(data->start + 32);
  2018. if (make_circ1_succeed) {
  2019. r = entry_guard_succeeded(&data->guard1_state);
  2020. tor_assert(r == GUARD_MAYBE_USABLE_LATER);
  2021. tor_assert(data->guard1_state->state ==
  2022. GUARD_CIRC_STATE_WAITING_FOR_BETTER_GUARD);
  2023. }
  2024. update_approx_time(data->start + 33);
  2025. if (make_circ2_succeed) {
  2026. r = entry_guard_succeeded(&data->guard2_state);
  2027. tor_assert(r == GUARD_MAYBE_USABLE_LATER);
  2028. tor_assert(data->guard2_state->state ==
  2029. GUARD_CIRC_STATE_WAITING_FOR_BETTER_GUARD);
  2030. }
  2031. return data;
  2032. }
  2033. static int
  2034. upgrade_circuits_cleanup(const struct testcase_t *testcase, void *ptr)
  2035. {
  2036. upgrade_circuits_data_t *data = ptr;
  2037. // circuit_guard_state_free(data->guard1_state); // held in circ1
  2038. // circuit_guard_state_free(data->guard2_state); // held in circ2
  2039. guard_selection_free(data->gs);
  2040. smartlist_free(data->all_origin_circuits);
  2041. circuit_free(TO_CIRCUIT(data->circ1));
  2042. circuit_free(TO_CIRCUIT(data->circ2));
  2043. tor_free(data);
  2044. return big_fake_network_cleanup(testcase, NULL);
  2045. }
  2046. static void
  2047. test_entry_guard_upgrade_a_circuit(void *arg)
  2048. {
  2049. upgrade_circuits_data_t *data = arg;
  2050. /* This is the easy case: we have no COMPLETED circuits, all the
  2051. * primary guards are down, we have two WAITING circuits: one will
  2052. * get upgraded to COMPLETED! (The one that started first.)
  2053. */
  2054. smartlist_t *result = smartlist_new();
  2055. int r;
  2056. r = entry_guards_upgrade_waiting_circuits(data->gs,
  2057. data->all_origin_circuits,
  2058. result);
  2059. tt_int_op(r, OP_EQ, 1);
  2060. tt_int_op(smartlist_len(result), OP_EQ, 1);
  2061. origin_circuit_t *oc = smartlist_get(result, 0);
  2062. /* circ1 was started first, so we'll get told to ugrade it... */
  2063. tt_ptr_op(oc, OP_EQ, data->circ1);
  2064. /* And the guard state should be complete */
  2065. tt_ptr_op(data->guard1_state, OP_NE, NULL);
  2066. tt_int_op(data->guard1_state->state, OP_EQ, GUARD_CIRC_STATE_COMPLETE);
  2067. done:
  2068. smartlist_free(result);
  2069. }
  2070. static void
  2071. test_entry_guard_upgrade_blocked_by_live_primary_guards(void *arg)
  2072. {
  2073. upgrade_circuits_data_t *data = arg;
  2074. /* If any primary guards might be up, we can't upgrade any waiting
  2075. * circuits.
  2076. */
  2077. mark_primary_guards_maybe_reachable(data->gs);
  2078. smartlist_t *result = smartlist_new();
  2079. int r;
  2080. setup_capture_of_logs(LOG_DEBUG);
  2081. r = entry_guards_upgrade_waiting_circuits(data->gs,
  2082. data->all_origin_circuits,
  2083. result);
  2084. tt_int_op(r, OP_EQ, 0);
  2085. tt_int_op(smartlist_len(result), OP_EQ, 0);
  2086. expect_log_msg_containing("not all primary guards were definitely down.");
  2087. done:
  2088. teardown_capture_of_logs();
  2089. smartlist_free(result);
  2090. }
  2091. static void
  2092. test_entry_guard_upgrade_blocked_by_lack_of_waiting_circuits(void *arg)
  2093. {
  2094. upgrade_circuits_data_t *data = arg;
  2095. /* If no circuits are waiting, we can't upgrade anything. (The test
  2096. * setup in this case was told not to make any of the circuits "waiting".)
  2097. */
  2098. smartlist_t *result = smartlist_new();
  2099. int r;
  2100. setup_capture_of_logs(LOG_DEBUG);
  2101. r = entry_guards_upgrade_waiting_circuits(data->gs,
  2102. data->all_origin_circuits,
  2103. result);
  2104. tt_int_op(r, OP_EQ, 0);
  2105. tt_int_op(smartlist_len(result), OP_EQ, 0);
  2106. expect_log_msg_containing("Considered upgrading guard-stalled circuits, "
  2107. "but didn't find any.");
  2108. done:
  2109. teardown_capture_of_logs();
  2110. smartlist_free(result);
  2111. }
  2112. static void
  2113. test_entry_guard_upgrade_blocked_by_better_circ_complete(void *arg)
  2114. {
  2115. upgrade_circuits_data_t *data = arg;
  2116. /* We'll run through the logic of upgrade_a_circuit below...
  2117. * and then try again to make sure that circ2 isn't also upgraded.
  2118. */
  2119. smartlist_t *result = smartlist_new();
  2120. int r;
  2121. r = entry_guards_upgrade_waiting_circuits(data->gs,
  2122. data->all_origin_circuits,
  2123. result);
  2124. tt_int_op(r, OP_EQ, 1);
  2125. tt_int_op(smartlist_len(result), OP_EQ, 1);
  2126. origin_circuit_t *oc = smartlist_get(result, 0);
  2127. tt_ptr_op(oc, OP_EQ, data->circ1);
  2128. tt_ptr_op(data->guard1_state, OP_NE, NULL);
  2129. tt_int_op(data->guard1_state->state, OP_EQ, GUARD_CIRC_STATE_COMPLETE);
  2130. /* Now, try again. Make sure that circ2 isn't upgraded. */
  2131. smartlist_clear(result);
  2132. setup_capture_of_logs(LOG_DEBUG);
  2133. r = entry_guards_upgrade_waiting_circuits(data->gs,
  2134. data->all_origin_circuits,
  2135. result);
  2136. tt_int_op(r, OP_EQ, 0);
  2137. tt_int_op(smartlist_len(result), OP_EQ, 0);
  2138. expect_log_msg_containing("At least one complete circuit had higher "
  2139. "priority, so not upgrading.");
  2140. done:
  2141. teardown_capture_of_logs();
  2142. smartlist_free(result);
  2143. }
  2144. static void
  2145. test_entry_guard_upgrade_not_blocked_by_restricted_circ_complete(void *arg)
  2146. {
  2147. upgrade_circuits_data_t *data = arg;
  2148. /* Once more, let circ1 become complete. But this time, we'll claim
  2149. * that circ2 was restricted to not use the same guard as circ1. */
  2150. data->guard2_state->restrictions =
  2151. tor_malloc_zero(sizeof(entry_guard_restriction_t));
  2152. memcpy(data->guard2_state->restrictions->exclude_id,
  2153. data->guard1->identity, DIGEST_LEN);
  2154. smartlist_t *result = smartlist_new();
  2155. int r;
  2156. r = entry_guards_upgrade_waiting_circuits(data->gs,
  2157. data->all_origin_circuits,
  2158. result);
  2159. tt_int_op(r, OP_EQ, 1);
  2160. tt_int_op(smartlist_len(result), OP_EQ, 1);
  2161. origin_circuit_t *oc = smartlist_get(result, 0);
  2162. tt_ptr_op(oc, OP_EQ, data->circ1);
  2163. tt_ptr_op(data->guard1_state, OP_NE, NULL);
  2164. tt_int_op(data->guard1_state->state, OP_EQ, GUARD_CIRC_STATE_COMPLETE);
  2165. /* Now, we try again. Since circ2 has a restriction that circ1 doesn't obey,
  2166. * circ2 _is_ eligible for upgrade. */
  2167. smartlist_clear(result);
  2168. r = entry_guards_upgrade_waiting_circuits(data->gs,
  2169. data->all_origin_circuits,
  2170. result);
  2171. tt_int_op(r, OP_EQ, 1);
  2172. tt_int_op(smartlist_len(result), OP_EQ, 1);
  2173. origin_circuit_t *oc2 = smartlist_get(result, 0);
  2174. tt_ptr_op(oc2, OP_EQ, data->circ2);
  2175. done:
  2176. smartlist_free(result);
  2177. }
  2178. static void
  2179. test_entry_guard_upgrade_not_blocked_by_worse_circ_complete(void *arg)
  2180. {
  2181. upgrade_circuits_data_t *data = arg;
  2182. smartlist_t *result = smartlist_new();
  2183. /* here we manually make circ2 COMPLETE, and make sure that circ1
  2184. * gets made complete anyway, since guard1 has higher priority
  2185. */
  2186. update_approx_time(data->start + 300);
  2187. data->guard2_state->state = GUARD_CIRC_STATE_COMPLETE;
  2188. data->guard2_state->state_set_at = approx_time();
  2189. update_approx_time(data->start + 301);
  2190. /* Now, try again. Make sure that circ1 is approved. */
  2191. int r;
  2192. r = entry_guards_upgrade_waiting_circuits(data->gs,
  2193. data->all_origin_circuits,
  2194. result);
  2195. tt_int_op(r, OP_EQ, 1);
  2196. tt_int_op(smartlist_len(result), OP_EQ, 1);
  2197. origin_circuit_t *oc = smartlist_get(result, 0);
  2198. tt_ptr_op(oc, OP_EQ, data->circ1);
  2199. done:
  2200. smartlist_free(result);
  2201. }
  2202. static void
  2203. test_entry_guard_upgrade_blocked_by_better_circ_pending(void *arg)
  2204. {
  2205. upgrade_circuits_data_t *data = arg;
  2206. /* circ2 is done, but circ1 is still pending. Since circ1 is better,
  2207. * we won't upgrade circ2. */
  2208. /* XXXX Prop271 -- this is a kludge. I'm making sure circ1 _is_ better,
  2209. * by messing with the guards' confirmed_idx */
  2210. make_guard_confirmed(data->gs, data->guard1);
  2211. {
  2212. int tmp;
  2213. tmp = data->guard1->confirmed_idx;
  2214. data->guard1->confirmed_idx = data->guard2->confirmed_idx;
  2215. data->guard2->confirmed_idx = tmp;
  2216. }
  2217. smartlist_t *result = smartlist_new();
  2218. setup_capture_of_logs(LOG_DEBUG);
  2219. int r;
  2220. r = entry_guards_upgrade_waiting_circuits(data->gs,
  2221. data->all_origin_circuits,
  2222. result);
  2223. tt_int_op(r, OP_EQ, 0);
  2224. tt_int_op(smartlist_len(result), OP_EQ, 0);
  2225. expect_log_msg_containing("but 1 pending circuit(s) had higher guard "
  2226. "priority, so not upgrading.");
  2227. done:
  2228. teardown_capture_of_logs();
  2229. smartlist_free(result);
  2230. }
  2231. static void
  2232. test_entry_guard_upgrade_not_blocked_by_restricted_circ_pending(void *arg)
  2233. {
  2234. upgrade_circuits_data_t *data = arg;
  2235. /* circ2 is done, but circ1 is still pending. But when there is a
  2236. restriction on circ2 that circ1 can't satisfy, circ1 can't block
  2237. circ2. */
  2238. /* XXXX Prop271 -- this is a kludge. I'm making sure circ1 _is_ better,
  2239. * by messing with the guards' confirmed_idx */
  2240. make_guard_confirmed(data->gs, data->guard1);
  2241. {
  2242. int tmp;
  2243. tmp = data->guard1->confirmed_idx;
  2244. data->guard1->confirmed_idx = data->guard2->confirmed_idx;
  2245. data->guard2->confirmed_idx = tmp;
  2246. }
  2247. data->guard2_state->restrictions =
  2248. tor_malloc_zero(sizeof(entry_guard_restriction_t));
  2249. memcpy(data->guard2_state->restrictions->exclude_id,
  2250. data->guard1->identity, DIGEST_LEN);
  2251. smartlist_t *result = smartlist_new();
  2252. int r;
  2253. r = entry_guards_upgrade_waiting_circuits(data->gs,
  2254. data->all_origin_circuits,
  2255. result);
  2256. tt_int_op(r, OP_EQ, 1);
  2257. tt_int_op(smartlist_len(result), OP_EQ, 1);
  2258. origin_circuit_t *oc = smartlist_get(result, 0);
  2259. tt_ptr_op(oc, OP_EQ, data->circ2);
  2260. done:
  2261. smartlist_free(result);
  2262. }
  2263. static void
  2264. test_entry_guard_upgrade_not_blocked_by_worse_circ_pending(void *arg)
  2265. {
  2266. upgrade_circuits_data_t *data = arg;
  2267. /* circ1 is done, but circ2 is still pending. Since circ1 is better,
  2268. * we will upgrade it. */
  2269. smartlist_t *result = smartlist_new();
  2270. int r;
  2271. r = entry_guards_upgrade_waiting_circuits(data->gs,
  2272. data->all_origin_circuits,
  2273. result);
  2274. tt_int_op(r, OP_EQ, 1);
  2275. tt_int_op(smartlist_len(result), OP_EQ, 1);
  2276. origin_circuit_t *oc = smartlist_get(result, 0);
  2277. tt_ptr_op(oc, OP_EQ, data->circ1);
  2278. done:
  2279. smartlist_free(result);
  2280. }
  2281. static void
  2282. test_enty_guard_should_expire_waiting(void *arg)
  2283. {
  2284. (void)arg;
  2285. circuit_guard_state_t *fake_state = tor_malloc_zero(sizeof(*fake_state));
  2286. /* We'll leave "guard" unset -- it won't matter here. */
  2287. /* No state? Can't expire. */
  2288. tt_assert(! entry_guard_state_should_expire(NULL));
  2289. /* Let's try one that expires. */
  2290. fake_state->state = GUARD_CIRC_STATE_WAITING_FOR_BETTER_GUARD;
  2291. fake_state->state_set_at =
  2292. approx_time() - DFLT_NONPRIMARY_GUARD_IDLE_TIMEOUT - 1;
  2293. tt_assert(entry_guard_state_should_expire(fake_state));
  2294. /* But it wouldn't expire if we changed the state. */
  2295. fake_state->state = GUARD_CIRC_STATE_USABLE_IF_NO_BETTER_GUARD;
  2296. tt_assert(! entry_guard_state_should_expire(fake_state));
  2297. /* And it wouldn't have expired a few seconds ago. */
  2298. fake_state->state = GUARD_CIRC_STATE_WAITING_FOR_BETTER_GUARD;
  2299. fake_state->state_set_at =
  2300. approx_time() - DFLT_NONPRIMARY_GUARD_IDLE_TIMEOUT + 5;
  2301. tt_assert(! entry_guard_state_should_expire(fake_state));
  2302. done:
  2303. tor_free(fake_state);
  2304. }
  2305. static const struct testcase_setup_t big_fake_network = {
  2306. big_fake_network_setup, big_fake_network_cleanup
  2307. };
  2308. static const struct testcase_setup_t upgrade_circuits = {
  2309. upgrade_circuits_setup, upgrade_circuits_cleanup
  2310. };
  2311. #define BFN_TEST(name) \
  2312. { #name, test_entry_guard_ ## name, TT_FORK, &big_fake_network, NULL }
  2313. #define UPGRADE_TEST(name, arg) \
  2314. { #name, test_entry_guard_ ## name, TT_FORK, &upgrade_circuits, \
  2315. (void*)(arg) }
  2316. struct testcase_t entrynodes_tests[] = {
  2317. { "node_preferred_orport",
  2318. test_node_preferred_orport,
  2319. 0, NULL, NULL },
  2320. { "entry_guard_describe", test_entry_guard_describe, 0, NULL, NULL },
  2321. { "randomize_time", test_entry_guard_randomize_time, 0, NULL, NULL },
  2322. { "encode_for_state_minimal",
  2323. test_entry_guard_encode_for_state_minimal, 0, NULL, NULL },
  2324. { "encode_for_state_maximal",
  2325. test_entry_guard_encode_for_state_maximal, 0, NULL, NULL },
  2326. { "parse_from_state_minimal",
  2327. test_entry_guard_parse_from_state_minimal, 0, NULL, NULL },
  2328. { "parse_from_state_maximal",
  2329. test_entry_guard_parse_from_state_maximal, 0, NULL, NULL },
  2330. { "parse_from_state_failure",
  2331. test_entry_guard_parse_from_state_failure, 0, NULL, NULL },
  2332. { "parse_from_state_partial_failure",
  2333. test_entry_guard_parse_from_state_partial_failure, 0, NULL, NULL },
  2334. { "parse_from_state_full",
  2335. test_entry_guard_parse_from_state_full, TT_FORK, NULL, NULL },
  2336. { "parse_from_state_broken",
  2337. test_entry_guard_parse_from_state_broken, TT_FORK, NULL, NULL },
  2338. { "get_guard_selection_by_name",
  2339. test_entry_guard_get_guard_selection_by_name, TT_FORK, NULL, NULL },
  2340. BFN_TEST(choose_selection_initial),
  2341. BFN_TEST(add_single_guard),
  2342. BFN_TEST(node_filter),
  2343. BFN_TEST(expand_sample),
  2344. BFN_TEST(expand_sample_small_net),
  2345. BFN_TEST(update_from_consensus_status),
  2346. BFN_TEST(update_from_consensus_repair),
  2347. BFN_TEST(update_from_consensus_remove),
  2348. BFN_TEST(confirming_guards),
  2349. BFN_TEST(sample_reachable_filtered),
  2350. BFN_TEST(sample_reachable_filtered_empty),
  2351. BFN_TEST(retry_unreachable),
  2352. BFN_TEST(manage_primary),
  2353. { "guard_preferred", test_entry_guard_guard_preferred, TT_FORK, NULL, NULL },
  2354. BFN_TEST(select_for_circuit_no_confirmed),
  2355. BFN_TEST(select_for_circuit_confirmed),
  2356. BFN_TEST(select_for_circuit_highlevel_primary),
  2357. BFN_TEST(select_for_circuit_highlevel_confirm_other),
  2358. BFN_TEST(select_for_circuit_highlevel_primary_retry),
  2359. BFN_TEST(select_and_cancel),
  2360. BFN_TEST(drop_guards),
  2361. UPGRADE_TEST(upgrade_a_circuit, "c1-done c2-done"),
  2362. UPGRADE_TEST(upgrade_blocked_by_live_primary_guards, "c1-done c2-done"),
  2363. UPGRADE_TEST(upgrade_blocked_by_lack_of_waiting_circuits, ""),
  2364. UPGRADE_TEST(upgrade_blocked_by_better_circ_complete, "c1-done c2-done"),
  2365. UPGRADE_TEST(upgrade_not_blocked_by_restricted_circ_complete,
  2366. "c1-done c2-done"),
  2367. UPGRADE_TEST(upgrade_not_blocked_by_worse_circ_complete, "c1-done c2-done"),
  2368. UPGRADE_TEST(upgrade_blocked_by_better_circ_pending, "c2-done"),
  2369. UPGRADE_TEST(upgrade_not_blocked_by_restricted_circ_pending,
  2370. "c2-done"),
  2371. UPGRADE_TEST(upgrade_not_blocked_by_worse_circ_pending, "c1-done"),
  2372. { "should_expire_waiting", test_enty_guard_should_expire_waiting, TT_FORK,
  2373. NULL, NULL },
  2374. END_OF_TESTCASES
  2375. };