test_entrynodes.c 104 KB

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