test_entrynodes.c 103 KB

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