test_entrynodes.c 91 KB

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