test_entrynodes.c 104 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080
  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 DIRECTORY_PRIVATE
  10. #include "or/or.h"
  11. #include "test/test.h"
  12. #include "or/bridges.h"
  13. #include "or/circuitlist.h"
  14. #include "or/circuitbuild.h"
  15. #include "or/config.h"
  16. #include "or/confparse.h"
  17. #include "lib/crypt_ops/crypto_rand.h"
  18. #include "or/directory.h"
  19. #include "or/entrynodes.h"
  20. #include "or/nodelist.h"
  21. #include "or/networkstatus.h"
  22. #include "or/policies.h"
  23. #include "or/routerlist.h"
  24. #include "or/routerparse.h"
  25. #include "or/routerset.h"
  26. #include "or/statefile.h"
  27. #include "common/util.h"
  28. #include "or/cpath_build_state_st.h"
  29. #include "or/crypt_path_st.h"
  30. #include "or/dir_connection_st.h"
  31. #include "or/microdesc_st.h"
  32. #include "or/networkstatus_st.h"
  33. #include "or/node_st.h"
  34. #include "or/origin_circuit_st.h"
  35. #include "or/or_state_st.h"
  36. #include "or/routerinfo_st.h"
  37. #include "or/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. n->is_possible_guard = 0;
  1001. }
  1002. update_approx_time(start + 30);
  1003. {
  1004. /* try this with no live networkstatus. Nothing should happen! */
  1005. ns_tmp = dummy_consensus;
  1006. dummy_consensus = NULL;
  1007. sampled_guards_update_from_consensus(gs);
  1008. tt_i64_op(smartlist_len(gs->sampled_entry_guards), OP_EQ, n_sampled_pre);
  1009. tt_i64_op(num_reachable_filtered_guards(gs, NULL), OP_EQ, n_filtered_pre);
  1010. /* put the networkstatus back. */
  1011. dummy_consensus = ns_tmp;
  1012. ns_tmp = NULL;
  1013. }
  1014. /* Now those guards should become unlisted, and drop off the filter, but
  1015. * stay in the sample. */
  1016. update_approx_time(start + 60);
  1017. sampled_guards_update_from_consensus(gs);
  1018. tt_i64_op(smartlist_len(gs->sampled_entry_guards), OP_EQ, n_sampled_pre);
  1019. tt_i64_op(num_reachable_filtered_guards(gs, NULL), OP_EQ, n_filtered_pre-5);
  1020. for (i = 0; i < 5; ++i) {
  1021. entry_guard_t *g = smartlist_get(gs->sampled_entry_guards, i);
  1022. tt_assert(! g->currently_listed);
  1023. tt_i64_op(g->unlisted_since_date, OP_EQ, start+60);
  1024. }
  1025. for (i = 5; i < n_sampled_pre; ++i) {
  1026. entry_guard_t *g = smartlist_get(gs->sampled_entry_guards, i);
  1027. tt_assert(g->currently_listed);
  1028. tt_i64_op(g->unlisted_since_date, OP_EQ, 0);
  1029. }
  1030. /* Now re-list one, and remove one completely. */
  1031. {
  1032. entry_guard_t *g = smartlist_get(gs->sampled_entry_guards, 0);
  1033. node_t *n = (node_t*) bfn_mock_node_get_by_id(g->identity);
  1034. n->is_possible_guard = 1;
  1035. }
  1036. {
  1037. /* try removing the node, to make sure we don't crash on an absent node
  1038. */
  1039. entry_guard_t *g = smartlist_get(gs->sampled_entry_guards, 5);
  1040. node_t *n = (node_t*) bfn_mock_node_get_by_id(g->identity);
  1041. smartlist_remove(big_fake_net_nodes, n);
  1042. test_node_free(n);
  1043. }
  1044. update_approx_time(start + 300);
  1045. sampled_guards_update_from_consensus(gs);
  1046. /* guards 1..5 are now unlisted; 0,6,7.. are listed. */
  1047. tt_i64_op(smartlist_len(gs->sampled_entry_guards), OP_EQ, n_sampled_pre);
  1048. for (i = 1; i < 6; ++i) {
  1049. entry_guard_t *g = smartlist_get(gs->sampled_entry_guards, i);
  1050. tt_assert(! g->currently_listed);
  1051. if (i == 5)
  1052. tt_i64_op(g->unlisted_since_date, OP_EQ, start+300);
  1053. else
  1054. tt_i64_op(g->unlisted_since_date, OP_EQ, start+60);
  1055. }
  1056. for (i = 0; i < n_sampled_pre; i = (!i) ? 6 : i+1) { /* 0,6,7,8, ... */
  1057. entry_guard_t *g = smartlist_get(gs->sampled_entry_guards, i);
  1058. tt_assert(g->currently_listed);
  1059. tt_i64_op(g->unlisted_since_date, OP_EQ, 0);
  1060. }
  1061. done:
  1062. tor_free(ns_tmp); /* in case we couldn't put it back */
  1063. guard_selection_free(gs);
  1064. UNMOCK(randomize_time);
  1065. }
  1066. static void
  1067. test_entry_guard_update_from_consensus_repair(void *arg)
  1068. {
  1069. /* Here we'll make sure that our code to repair the unlisted-since
  1070. * times is correct. */
  1071. (void)arg;
  1072. int i;
  1073. time_t start = approx_time();
  1074. guard_selection_t *gs = guard_selection_new("default", GS_TYPE_NORMAL);
  1075. /* Don't randomly backdate stuff; it will make correctness harder to check.*/
  1076. MOCK(randomize_time, mock_randomize_time_no_randomization);
  1077. /* First, sample some guards. */
  1078. entry_guards_expand_sample(gs);
  1079. int n_sampled_pre = smartlist_len(gs->sampled_entry_guards);
  1080. int n_filtered_pre = num_reachable_filtered_guards(gs, NULL);
  1081. tt_i64_op(n_sampled_pre, OP_EQ, n_filtered_pre);
  1082. tt_i64_op(n_sampled_pre, OP_GT, 10);
  1083. /* Now corrupt the list a bit. Call some unlisted-since-never, and some
  1084. * listed-and-unlisted-since-a-time. */
  1085. update_approx_time(start + 300);
  1086. for (i = 0; i < 3; ++i) {
  1087. /* these will get a date. */
  1088. entry_guard_t *g = smartlist_get(gs->sampled_entry_guards, i);
  1089. node_t *n = (node_t*) bfn_mock_node_get_by_id(g->identity);
  1090. n->is_possible_guard = 0;
  1091. g->currently_listed = 0;
  1092. }
  1093. for (i = 3; i < 6; ++i) {
  1094. /* these will become listed. */
  1095. entry_guard_t *g = smartlist_get(gs->sampled_entry_guards, i);
  1096. g->unlisted_since_date = start+100;
  1097. }
  1098. setup_full_capture_of_logs(LOG_WARN);
  1099. sampled_guards_update_from_consensus(gs);
  1100. expect_log_msg_containing(
  1101. "was listed, but with unlisted_since_date set");
  1102. expect_log_msg_containing(
  1103. "was unlisted, but with unlisted_since_date unset");
  1104. teardown_capture_of_logs();
  1105. tt_int_op(smartlist_len(gs->sampled_entry_guards), OP_EQ, n_sampled_pre);
  1106. tt_int_op(num_reachable_filtered_guards(gs, NULL), OP_EQ, n_filtered_pre-3);
  1107. for (i = 3; i < n_sampled_pre; ++i) {
  1108. /* these will become listed. */
  1109. entry_guard_t *g = smartlist_get(gs->sampled_entry_guards, i);
  1110. if (i < 3) {
  1111. tt_assert(! g->currently_listed);
  1112. tt_i64_op(g->unlisted_since_date, OP_EQ, start+300);
  1113. } else {
  1114. tt_assert(g->currently_listed);
  1115. tt_i64_op(g->unlisted_since_date, OP_EQ, 0);
  1116. }
  1117. }
  1118. done:
  1119. teardown_capture_of_logs();
  1120. guard_selection_free(gs);
  1121. UNMOCK(randomize_time);
  1122. }
  1123. static void
  1124. test_entry_guard_update_from_consensus_remove(void *arg)
  1125. {
  1126. /* Now let's check the logic responsible for removing guards from the
  1127. * sample entirely. */
  1128. (void)arg;
  1129. //int i;
  1130. guard_selection_t *gs = guard_selection_new("default", GS_TYPE_NORMAL);
  1131. smartlist_t *keep_ids = smartlist_new();
  1132. smartlist_t *remove_ids = smartlist_new();
  1133. /* Don't randomly backdate stuff; it will make correctness harder to check.*/
  1134. MOCK(randomize_time, mock_randomize_time_no_randomization);
  1135. /* First, sample some guards. */
  1136. entry_guards_expand_sample(gs);
  1137. int n_sampled_pre = smartlist_len(gs->sampled_entry_guards);
  1138. int n_filtered_pre = num_reachable_filtered_guards(gs, NULL);
  1139. tt_i64_op(n_sampled_pre, OP_EQ, n_filtered_pre);
  1140. tt_i64_op(n_sampled_pre, OP_GT, 10);
  1141. const time_t one_day_ago = approx_time() - 1*24*60*60;
  1142. const time_t one_year_ago = approx_time() - 365*24*60*60;
  1143. const time_t two_years_ago = approx_time() - 2*365*24*60*60;
  1144. /* 0: unlisted for a day. (keep this) */
  1145. {
  1146. entry_guard_t *g = smartlist_get(gs->sampled_entry_guards, 0);
  1147. node_t *n = (node_t*) bfn_mock_node_get_by_id(g->identity);
  1148. n->is_possible_guard = 0;
  1149. g->currently_listed = 0;
  1150. g->unlisted_since_date = one_day_ago;
  1151. smartlist_add(keep_ids, tor_memdup(g->identity, 20));
  1152. }
  1153. /* 1: unlisted for a year. (remove this) */
  1154. {
  1155. entry_guard_t *g = smartlist_get(gs->sampled_entry_guards, 1);
  1156. node_t *n = (node_t*) bfn_mock_node_get_by_id(g->identity);
  1157. n->is_possible_guard = 0;
  1158. g->currently_listed = 0;
  1159. g->unlisted_since_date = one_year_ago;
  1160. smartlist_add(remove_ids, tor_memdup(g->identity, 20));
  1161. }
  1162. /* 2: added a day ago, never confirmed. (keep this) */
  1163. {
  1164. entry_guard_t *g = smartlist_get(gs->sampled_entry_guards, 2);
  1165. g->sampled_on_date = one_day_ago;
  1166. smartlist_add(keep_ids, tor_memdup(g->identity, 20));
  1167. }
  1168. /* 3: added a year ago, never confirmed. (remove this) */
  1169. {
  1170. entry_guard_t *g = smartlist_get(gs->sampled_entry_guards, 3);
  1171. g->sampled_on_date = one_year_ago;
  1172. smartlist_add(remove_ids, tor_memdup(g->identity, 20));
  1173. }
  1174. /* 4: added two year ago, confirmed yesterday, primary. (keep this.) */
  1175. {
  1176. entry_guard_t *g = smartlist_get(gs->sampled_entry_guards, 4);
  1177. g->sampled_on_date = one_year_ago;
  1178. g->confirmed_on_date = one_day_ago;
  1179. g->confirmed_idx = 0;
  1180. g->is_primary = 1;
  1181. smartlist_add(gs->confirmed_entry_guards, g);
  1182. smartlist_add(gs->primary_entry_guards, g);
  1183. smartlist_add(keep_ids, tor_memdup(g->identity, 20));
  1184. }
  1185. /* 5: added two years ago, confirmed a year ago, primary. (remove this) */
  1186. {
  1187. entry_guard_t *g = smartlist_get(gs->sampled_entry_guards, 5);
  1188. g->sampled_on_date = two_years_ago;
  1189. g->confirmed_on_date = one_year_ago;
  1190. g->confirmed_idx = 1;
  1191. g->is_primary = 1;
  1192. smartlist_add(gs->confirmed_entry_guards, g);
  1193. smartlist_add(gs->primary_entry_guards, g);
  1194. smartlist_add(remove_ids, tor_memdup(g->identity, 20));
  1195. }
  1196. sampled_guards_update_from_consensus(gs);
  1197. /* Did we remove the right ones? */
  1198. SMARTLIST_FOREACH(keep_ids, uint8_t *, id, {
  1199. tt_assert(get_sampled_guard_with_id(gs, id) != NULL);
  1200. });
  1201. SMARTLIST_FOREACH(remove_ids, uint8_t *, id, {
  1202. tt_want(get_sampled_guard_with_id(gs, id) == NULL);
  1203. });
  1204. /* Did we remove the right number? */
  1205. tt_int_op(smartlist_len(gs->sampled_entry_guards), OP_EQ, n_sampled_pre - 3);
  1206. done:
  1207. guard_selection_free(gs);
  1208. UNMOCK(randomize_time);
  1209. SMARTLIST_FOREACH(keep_ids, char *, cp, tor_free(cp));
  1210. SMARTLIST_FOREACH(remove_ids, char *, cp, tor_free(cp));
  1211. smartlist_free(keep_ids);
  1212. smartlist_free(remove_ids);
  1213. }
  1214. static void
  1215. test_entry_guard_confirming_guards(void *arg)
  1216. {
  1217. (void)arg;
  1218. /* Now let's check the logic responsible for manipulating the list
  1219. * of confirmed guards */
  1220. guard_selection_t *gs = guard_selection_new("default", GS_TYPE_NORMAL);
  1221. MOCK(randomize_time, mock_randomize_time_no_randomization);
  1222. /* Create the sample. */
  1223. entry_guards_expand_sample(gs);
  1224. /* Confirm a few guards. */
  1225. time_t start = approx_time();
  1226. entry_guard_t *g1 = smartlist_get(gs->sampled_entry_guards, 0);
  1227. entry_guard_t *g2 = smartlist_get(gs->sampled_entry_guards, 1);
  1228. entry_guard_t *g3 = smartlist_get(gs->sampled_entry_guards, 8);
  1229. make_guard_confirmed(gs, g2);
  1230. update_approx_time(start + 10);
  1231. make_guard_confirmed(gs, g1);
  1232. make_guard_confirmed(gs, g3);
  1233. /* Were the correct dates and indices fed in? */
  1234. tt_int_op(g1->confirmed_idx, OP_EQ, 1);
  1235. tt_int_op(g2->confirmed_idx, OP_EQ, 0);
  1236. tt_int_op(g3->confirmed_idx, OP_EQ, 2);
  1237. tt_i64_op(g1->confirmed_on_date, OP_EQ, start+10);
  1238. tt_i64_op(g2->confirmed_on_date, OP_EQ, start);
  1239. tt_i64_op(g3->confirmed_on_date, OP_EQ, start+10);
  1240. tt_ptr_op(smartlist_get(gs->confirmed_entry_guards, 0), OP_EQ, g2);
  1241. tt_ptr_op(smartlist_get(gs->confirmed_entry_guards, 1), OP_EQ, g1);
  1242. tt_ptr_op(smartlist_get(gs->confirmed_entry_guards, 2), OP_EQ, g3);
  1243. /* Now make sure we can regenerate the confirmed_entry_guards list. */
  1244. smartlist_clear(gs->confirmed_entry_guards);
  1245. g2->confirmed_idx = 0;
  1246. g1->confirmed_idx = 10;
  1247. g3->confirmed_idx = 100;
  1248. entry_guards_update_confirmed(gs);
  1249. tt_int_op(g1->confirmed_idx, OP_EQ, 1);
  1250. tt_int_op(g2->confirmed_idx, OP_EQ, 0);
  1251. tt_int_op(g3->confirmed_idx, OP_EQ, 2);
  1252. tt_ptr_op(smartlist_get(gs->confirmed_entry_guards, 0), OP_EQ, g2);
  1253. tt_ptr_op(smartlist_get(gs->confirmed_entry_guards, 1), OP_EQ, g1);
  1254. tt_ptr_op(smartlist_get(gs->confirmed_entry_guards, 2), OP_EQ, g3);
  1255. /* Now make sure we can regenerate the confirmed_entry_guards list if
  1256. * the indices are messed up. */
  1257. g1->confirmed_idx = g2->confirmed_idx = g3->confirmed_idx = 999;
  1258. smartlist_clear(gs->confirmed_entry_guards);
  1259. entry_guards_update_confirmed(gs);
  1260. tt_int_op(g1->confirmed_idx, OP_GE, 0);
  1261. tt_int_op(g2->confirmed_idx, OP_GE, 0);
  1262. tt_int_op(g3->confirmed_idx, OP_GE, 0);
  1263. tt_int_op(g1->confirmed_idx, OP_LE, 2);
  1264. tt_int_op(g2->confirmed_idx, OP_LE, 2);
  1265. tt_int_op(g3->confirmed_idx, OP_LE, 2);
  1266. g1 = smartlist_get(gs->confirmed_entry_guards, 0);
  1267. g2 = smartlist_get(gs->confirmed_entry_guards, 1);
  1268. g3 = smartlist_get(gs->confirmed_entry_guards, 2);
  1269. tt_int_op(g1->confirmed_idx, OP_EQ, 0);
  1270. tt_int_op(g2->confirmed_idx, OP_EQ, 1);
  1271. tt_int_op(g3->confirmed_idx, OP_EQ, 2);
  1272. tt_assert(g1 != g2);
  1273. tt_assert(g1 != g3);
  1274. tt_assert(g2 != g3);
  1275. done:
  1276. UNMOCK(randomize_time);
  1277. guard_selection_free(gs);
  1278. }
  1279. static void
  1280. test_entry_guard_sample_reachable_filtered(void *arg)
  1281. {
  1282. (void)arg;
  1283. guard_selection_t *gs = guard_selection_new("default", GS_TYPE_NORMAL);
  1284. entry_guards_expand_sample(gs);
  1285. const int N = 10000;
  1286. bitarray_t *selected = NULL;
  1287. int i, j;
  1288. /* We've got a sampled list now; let's make one non-usable-filtered; some
  1289. * confirmed, some primary, some pending.
  1290. */
  1291. int n_guards = smartlist_len(gs->sampled_entry_guards);
  1292. tt_int_op(n_guards, OP_GT, 10);
  1293. entry_guard_t *g;
  1294. g = smartlist_get(gs->sampled_entry_guards, 0);
  1295. g->is_pending = 1;
  1296. g = smartlist_get(gs->sampled_entry_guards, 1);
  1297. make_guard_confirmed(gs, g);
  1298. g = smartlist_get(gs->sampled_entry_guards, 2);
  1299. g->is_primary = 1;
  1300. g = smartlist_get(gs->sampled_entry_guards, 3);
  1301. g->pb.path_bias_disabled = 1;
  1302. entry_guards_update_filtered_sets(gs);
  1303. gs->primary_guards_up_to_date = 1;
  1304. tt_int_op(num_reachable_filtered_guards(gs, NULL), OP_EQ, n_guards - 1);
  1305. tt_int_op(smartlist_len(gs->sampled_entry_guards), OP_EQ, n_guards);
  1306. // +1 since the one we made disabled will make another one get added.
  1307. ++n_guards;
  1308. /* Try a bunch of selections. */
  1309. const struct {
  1310. int flag; int idx;
  1311. } tests[] = {
  1312. { 0, -1 },
  1313. { SAMPLE_EXCLUDE_CONFIRMED, 1 },
  1314. { SAMPLE_EXCLUDE_PRIMARY|SAMPLE_NO_UPDATE_PRIMARY, 2 },
  1315. { SAMPLE_EXCLUDE_PENDING, 0 },
  1316. { -1, -1},
  1317. };
  1318. for (j = 0; tests[j].flag >= 0; ++j) {
  1319. selected = bitarray_init_zero(n_guards);
  1320. const int excluded_flags = tests[j].flag;
  1321. const int excluded_idx = tests[j].idx;
  1322. for (i = 0; i < N; ++i) {
  1323. g = sample_reachable_filtered_entry_guards(gs, NULL, excluded_flags);
  1324. tor_assert(g);
  1325. int pos = smartlist_pos(gs->sampled_entry_guards, g);
  1326. tt_int_op(smartlist_len(gs->sampled_entry_guards), OP_EQ, n_guards);
  1327. tt_int_op(pos, OP_GE, 0);
  1328. tt_int_op(pos, OP_LT, n_guards);
  1329. bitarray_set(selected, pos);
  1330. }
  1331. for (i = 0; i < n_guards; ++i) {
  1332. const int should_be_set = (i != excluded_idx &&
  1333. i != 3); // filtered out.
  1334. tt_int_op(!!bitarray_is_set(selected, i), OP_EQ, should_be_set);
  1335. }
  1336. bitarray_free(selected);
  1337. selected = NULL;
  1338. }
  1339. done:
  1340. guard_selection_free(gs);
  1341. bitarray_free(selected);
  1342. }
  1343. static void
  1344. test_entry_guard_sample_reachable_filtered_empty(void *arg)
  1345. {
  1346. (void)arg;
  1347. guard_selection_t *gs = guard_selection_new("default", GS_TYPE_NORMAL);
  1348. /* What if we try to sample from a set of 0? */
  1349. SMARTLIST_FOREACH(big_fake_net_nodes, node_t *, n,
  1350. n->is_possible_guard = 0);
  1351. entry_guard_t *g = sample_reachable_filtered_entry_guards(gs, NULL, 0);
  1352. tt_ptr_op(g, OP_EQ, NULL);
  1353. done:
  1354. guard_selection_free(gs);
  1355. }
  1356. static void
  1357. test_entry_guard_retry_unreachable(void *arg)
  1358. {
  1359. (void)arg;
  1360. guard_selection_t *gs = guard_selection_new("default", GS_TYPE_NORMAL);
  1361. entry_guards_expand_sample(gs);
  1362. /* Let's say that we have two guards, and they're down.
  1363. */
  1364. time_t start = approx_time();
  1365. entry_guard_t *g1 = smartlist_get(gs->sampled_entry_guards, 0);
  1366. entry_guard_t *g2 = smartlist_get(gs->sampled_entry_guards, 1);
  1367. entry_guard_t *g3 = smartlist_get(gs->sampled_entry_guards, 2);
  1368. g1->is_reachable = GUARD_REACHABLE_NO;
  1369. g2->is_reachable = GUARD_REACHABLE_NO;
  1370. g1->is_primary = 1;
  1371. g1->failing_since = g2->failing_since = start;
  1372. g1->last_tried_to_connect = g2->last_tried_to_connect = start;
  1373. /* Wait 5 minutes. Nothing will get retried. */
  1374. update_approx_time(start + 5 * 60);
  1375. entry_guard_consider_retry(g1);
  1376. entry_guard_consider_retry(g2);
  1377. entry_guard_consider_retry(g3); // just to make sure this doesn't crash.
  1378. tt_int_op(g1->is_reachable, OP_EQ, GUARD_REACHABLE_NO);
  1379. tt_int_op(g2->is_reachable, OP_EQ, GUARD_REACHABLE_NO);
  1380. tt_int_op(g3->is_reachable, OP_EQ, GUARD_REACHABLE_MAYBE);
  1381. /* After 30 min, the primary one gets retried */
  1382. update_approx_time(start + 35 * 60);
  1383. entry_guard_consider_retry(g1);
  1384. entry_guard_consider_retry(g2);
  1385. tt_int_op(g1->is_reachable, OP_EQ, GUARD_REACHABLE_MAYBE);
  1386. tt_int_op(g2->is_reachable, OP_EQ, GUARD_REACHABLE_NO);
  1387. g1->is_reachable = GUARD_REACHABLE_NO;
  1388. g1->last_tried_to_connect = start + 55*60;
  1389. /* After 1 hour, we'll retry the nonprimary one. */
  1390. update_approx_time(start + 61 * 60);
  1391. entry_guard_consider_retry(g1);
  1392. entry_guard_consider_retry(g2);
  1393. tt_int_op(g1->is_reachable, OP_EQ, GUARD_REACHABLE_NO);
  1394. tt_int_op(g2->is_reachable, OP_EQ, GUARD_REACHABLE_MAYBE);
  1395. g2->is_reachable = GUARD_REACHABLE_NO;
  1396. g2->last_tried_to_connect = start + 61*60;
  1397. /* And then the primary one again. */
  1398. update_approx_time(start + 66 * 60);
  1399. entry_guard_consider_retry(g1);
  1400. entry_guard_consider_retry(g2);
  1401. tt_int_op(g1->is_reachable, OP_EQ, GUARD_REACHABLE_MAYBE);
  1402. tt_int_op(g2->is_reachable, OP_EQ, GUARD_REACHABLE_NO);
  1403. done:
  1404. guard_selection_free(gs);
  1405. }
  1406. static void
  1407. test_entry_guard_manage_primary(void *arg)
  1408. {
  1409. (void)arg;
  1410. guard_selection_t *gs = guard_selection_new("default", GS_TYPE_NORMAL);
  1411. smartlist_t *prev_guards = smartlist_new();
  1412. /* If no guards are confirmed, we should pick a few reachable guards and
  1413. * call them all primary. But not confirmed.*/
  1414. entry_guards_update_primary(gs);
  1415. int n_primary = smartlist_len(gs->primary_entry_guards);
  1416. tt_int_op(n_primary, OP_GE, 1);
  1417. SMARTLIST_FOREACH(gs->primary_entry_guards, entry_guard_t *, g, {
  1418. tt_assert(g->is_primary);
  1419. tt_assert(g->confirmed_idx == -1);
  1420. });
  1421. /* Calling it a second time should leave the guards unchanged. */
  1422. smartlist_add_all(prev_guards, gs->primary_entry_guards);
  1423. entry_guards_update_primary(gs);
  1424. tt_int_op(smartlist_len(gs->primary_entry_guards), OP_EQ, n_primary);
  1425. SMARTLIST_FOREACH(gs->primary_entry_guards, entry_guard_t *, g, {
  1426. tt_ptr_op(g, OP_EQ, smartlist_get(prev_guards, g_sl_idx));
  1427. });
  1428. /* If we have one confirmed guard, that guards becomes the first primary
  1429. * guard, and the other primary guards get kept. */
  1430. /* find a non-primary guard... */
  1431. entry_guard_t *confirmed = NULL;
  1432. SMARTLIST_FOREACH(gs->sampled_entry_guards, entry_guard_t *, g, {
  1433. if (! g->is_primary) {
  1434. confirmed = g;
  1435. break;
  1436. }
  1437. });
  1438. tt_assert(confirmed);
  1439. /* make it confirmed. */
  1440. make_guard_confirmed(gs, confirmed);
  1441. /* update the list... */
  1442. smartlist_clear(prev_guards);
  1443. smartlist_add_all(prev_guards, gs->primary_entry_guards);
  1444. entry_guards_update_primary(gs);
  1445. /* and see what's primary now! */
  1446. tt_int_op(smartlist_len(gs->primary_entry_guards), OP_EQ, n_primary);
  1447. tt_ptr_op(smartlist_get(gs->primary_entry_guards, 0), OP_EQ, confirmed);
  1448. SMARTLIST_FOREACH(gs->primary_entry_guards, entry_guard_t *, g, {
  1449. tt_assert(g->is_primary);
  1450. if (g_sl_idx == 0)
  1451. continue;
  1452. tt_ptr_op(g, OP_EQ, smartlist_get(prev_guards, g_sl_idx - 1));
  1453. });
  1454. {
  1455. entry_guard_t *prev_last_guard = smartlist_get(prev_guards, n_primary-1);
  1456. tt_assert(! prev_last_guard->is_primary);
  1457. }
  1458. /* Calling it a fourth time should leave the guards unchanged. */
  1459. smartlist_clear(prev_guards);
  1460. smartlist_add_all(prev_guards, gs->primary_entry_guards);
  1461. entry_guards_update_primary(gs);
  1462. tt_int_op(smartlist_len(gs->primary_entry_guards), OP_EQ, n_primary);
  1463. SMARTLIST_FOREACH(gs->primary_entry_guards, entry_guard_t *, g, {
  1464. tt_ptr_op(g, OP_EQ, smartlist_get(prev_guards, g_sl_idx));
  1465. });
  1466. /* Do some dirinfo checks */
  1467. {
  1468. /* Check that we have all required dirinfo for the primaries (that's done
  1469. * in big_fake_network_setup()) */
  1470. char *dir_info_str =
  1471. guard_selection_get_err_str_if_dir_info_missing(gs, 0, 0, 0);
  1472. tt_assert(!dir_info_str);
  1473. /* Now artificially remove the first primary's descriptor and re-check */
  1474. entry_guard_t *first_primary;
  1475. first_primary = smartlist_get(gs->primary_entry_guards, 0);
  1476. /* Change the first primary's identity digest so that the mocked functions
  1477. * can't find its descriptor */
  1478. memset(first_primary->identity, 9, sizeof(first_primary->identity));
  1479. dir_info_str =guard_selection_get_err_str_if_dir_info_missing(gs, 1, 2, 3);
  1480. tt_str_op(dir_info_str, OP_EQ,
  1481. "We're missing descriptors for 1/2 of our primary entry guards "
  1482. "(total microdescriptors: 2/3).");
  1483. tor_free(dir_info_str);
  1484. }
  1485. done:
  1486. guard_selection_free(gs);
  1487. smartlist_free(prev_guards);
  1488. }
  1489. static void
  1490. test_entry_guard_guard_preferred(void *arg)
  1491. {
  1492. (void) arg;
  1493. entry_guard_t *g1 = tor_malloc_zero(sizeof(entry_guard_t));
  1494. entry_guard_t *g2 = tor_malloc_zero(sizeof(entry_guard_t));
  1495. g1->confirmed_idx = g2->confirmed_idx = -1;
  1496. g1->last_tried_to_connect = approx_time();
  1497. g2->last_tried_to_connect = approx_time();
  1498. tt_int_op(0, OP_EQ, entry_guard_has_higher_priority(g1, g1));
  1499. /* Neither is pending; priorities equal. */
  1500. tt_int_op(0, OP_EQ, entry_guard_has_higher_priority(g2, g1));
  1501. tt_int_op(0, OP_EQ, entry_guard_has_higher_priority(g1, g2));
  1502. /* If one is pending, the pending one has higher priority */
  1503. g1->is_pending = 1;
  1504. tt_int_op(1, OP_EQ, entry_guard_has_higher_priority(g1, g2));
  1505. tt_int_op(0, OP_EQ, entry_guard_has_higher_priority(g2, g1));
  1506. /* If both are pending, and last_tried_to_connect is equal:
  1507. priorities equal */
  1508. g2->is_pending = 1;
  1509. tt_int_op(0, OP_EQ, entry_guard_has_higher_priority(g2, g1));
  1510. tt_int_op(0, OP_EQ, entry_guard_has_higher_priority(g1, g2));
  1511. /* One had a connection that startied earlier: it has higher priority. */
  1512. g2->last_tried_to_connect -= 10;
  1513. tt_int_op(1, OP_EQ, entry_guard_has_higher_priority(g2, g1));
  1514. tt_int_op(0, OP_EQ, entry_guard_has_higher_priority(g1, g2));
  1515. /* Now, say that g1 is confirmed. It will get higher priority. */
  1516. g1->confirmed_idx = 5;
  1517. tt_int_op(0, OP_EQ, entry_guard_has_higher_priority(g2, g1));
  1518. tt_int_op(1, OP_EQ, entry_guard_has_higher_priority(g1, g2));
  1519. /* But if g2 was confirmed first, it will get priority */
  1520. g2->confirmed_idx = 2;
  1521. tt_int_op(1, OP_EQ, entry_guard_has_higher_priority(g2, g1));
  1522. tt_int_op(0, OP_EQ, entry_guard_has_higher_priority(g1, g2));
  1523. done:
  1524. tor_free(g1);
  1525. tor_free(g2);
  1526. }
  1527. static void
  1528. test_entry_guard_select_for_circuit_no_confirmed(void *arg)
  1529. {
  1530. /* Simpler cases: no gaurds are confirmed yet. */
  1531. (void)arg;
  1532. guard_selection_t *gs = guard_selection_new("default", GS_TYPE_NORMAL);
  1533. entry_guard_restriction_t *rst = NULL;
  1534. /* simple starting configuration */
  1535. entry_guards_update_primary(gs);
  1536. unsigned state = 9999;
  1537. entry_guard_t *g = select_entry_guard_for_circuit(gs, GUARD_USAGE_TRAFFIC,
  1538. NULL, &state);
  1539. tt_assert(g);
  1540. tt_assert(g->is_primary);
  1541. tt_int_op(g->confirmed_idx, OP_EQ, -1);
  1542. tt_uint_op(g->is_pending, OP_EQ, 0); // primary implies non-pending.
  1543. tt_uint_op(state, OP_EQ, GUARD_CIRC_STATE_USABLE_ON_COMPLETION);
  1544. tt_i64_op(g->last_tried_to_connect, OP_EQ, approx_time());
  1545. // If we do that again, we should get the same guard.
  1546. entry_guard_t *g2 = select_entry_guard_for_circuit(gs, GUARD_USAGE_TRAFFIC,
  1547. NULL, &state);
  1548. tt_ptr_op(g2, OP_EQ, g);
  1549. // if we mark that guard down, we should get a different primary guard.
  1550. // auto-retry it.
  1551. g->is_reachable = GUARD_REACHABLE_NO;
  1552. g->failing_since = approx_time() - 10;
  1553. g->last_tried_to_connect = approx_time() - 10;
  1554. state = 9999;
  1555. g2 = select_entry_guard_for_circuit(gs, GUARD_USAGE_TRAFFIC, NULL, &state);
  1556. tt_ptr_op(g2, OP_NE, g);
  1557. tt_assert(g2);
  1558. tt_assert(g2->is_primary);
  1559. tt_int_op(g2->confirmed_idx, OP_EQ, -1);
  1560. tt_uint_op(g2->is_pending, OP_EQ, 0); // primary implies non-pending.
  1561. tt_uint_op(state, OP_EQ, GUARD_CIRC_STATE_USABLE_ON_COMPLETION);
  1562. tt_i64_op(g2->last_tried_to_connect, OP_EQ, approx_time());
  1563. // If we say that the first primary guard was last tried a long time ago, we
  1564. // should get an automatic retry on it.
  1565. g->failing_since = approx_time() - 72*60*60;
  1566. g->last_tried_to_connect = approx_time() - 72*60*60;
  1567. state = 9999;
  1568. g2 = select_entry_guard_for_circuit(gs, GUARD_USAGE_TRAFFIC, NULL, &state);
  1569. tt_ptr_op(g2, OP_EQ, g);
  1570. tt_assert(g2);
  1571. tt_uint_op(state, OP_EQ, GUARD_CIRC_STATE_USABLE_ON_COMPLETION);
  1572. tt_i64_op(g2->last_tried_to_connect, OP_EQ, approx_time());
  1573. tt_int_op(g2->is_reachable, OP_EQ, GUARD_REACHABLE_MAYBE);
  1574. // And if we mark ALL the primary guards down, we should get another guard
  1575. // at random.
  1576. SMARTLIST_FOREACH(gs->primary_entry_guards, entry_guard_t *, guard, {
  1577. guard->is_reachable = GUARD_REACHABLE_NO;
  1578. guard->last_tried_to_connect = approx_time() - 5;
  1579. guard->failing_since = approx_time() - 30;
  1580. });
  1581. state = 9999;
  1582. g2 = select_entry_guard_for_circuit(gs, GUARD_USAGE_TRAFFIC, NULL, &state);
  1583. tt_assert(g2);
  1584. tt_assert(!g2->is_primary);
  1585. tt_int_op(g2->confirmed_idx, OP_EQ, -1);
  1586. tt_uint_op(g2->is_pending, OP_EQ, 1);
  1587. tt_uint_op(state, OP_EQ, GUARD_CIRC_STATE_USABLE_IF_NO_BETTER_GUARD);
  1588. tt_i64_op(g2->last_tried_to_connect, OP_EQ, approx_time());
  1589. tt_int_op(g2->is_reachable, OP_EQ, GUARD_REACHABLE_MAYBE);
  1590. // As a bonus, maybe we should be retrying the primary guards. Let's say so.
  1591. mark_primary_guards_maybe_reachable(gs);
  1592. SMARTLIST_FOREACH(gs->primary_entry_guards, entry_guard_t *, guard, {
  1593. tt_int_op(guard->is_reachable, OP_EQ, GUARD_REACHABLE_MAYBE);
  1594. tt_assert(guard->is_usable_filtered_guard == 1);
  1595. // no change to these fields.
  1596. tt_i64_op(guard->last_tried_to_connect, OP_EQ, approx_time() - 5);
  1597. tt_i64_op(guard->failing_since, OP_EQ, approx_time() - 30);
  1598. });
  1599. /* Let's try again and we should get the first primary guard again */
  1600. g = select_entry_guard_for_circuit(gs, GUARD_USAGE_TRAFFIC, NULL, &state);
  1601. tt_ptr_op(g, OP_EQ, smartlist_get(gs->primary_entry_guards, 0));
  1602. g2 = select_entry_guard_for_circuit(gs, GUARD_USAGE_TRAFFIC, NULL, &state);
  1603. tt_ptr_op(g2, OP_EQ, g);
  1604. /* But if we impose a restriction, we don't get the same guard */
  1605. rst = guard_create_exit_restriction((uint8_t*)g->identity);
  1606. g2 = select_entry_guard_for_circuit(gs, GUARD_USAGE_TRAFFIC, rst, &state);
  1607. tt_ptr_op(g2, OP_NE, g);
  1608. done:
  1609. guard_selection_free(gs);
  1610. entry_guard_restriction_free(rst);
  1611. }
  1612. static void
  1613. test_entry_guard_select_for_circuit_confirmed(void *arg)
  1614. {
  1615. /* Case 2: if all the primary guards are down, and there are more confirmed
  1616. guards, we use a confirmed guard. */
  1617. (void)arg;
  1618. int i;
  1619. entry_guard_restriction_t *rst = NULL;
  1620. guard_selection_t *gs = guard_selection_new("default", GS_TYPE_NORMAL);
  1621. const int N_CONFIRMED = 10;
  1622. /* slightly more complicated simple starting configuration */
  1623. entry_guards_update_primary(gs);
  1624. for (i = 0; i < N_CONFIRMED; ++i) {
  1625. entry_guard_t *guard = smartlist_get(gs->sampled_entry_guards, i);
  1626. make_guard_confirmed(gs, guard);
  1627. }
  1628. entry_guards_update_primary(gs); // rebuild the primary list.
  1629. unsigned state = 9999;
  1630. // As above, this gives us a primary guard.
  1631. entry_guard_t *g = select_entry_guard_for_circuit(gs, GUARD_USAGE_TRAFFIC,
  1632. NULL, &state);
  1633. tt_assert(g);
  1634. tt_assert(g->is_primary);
  1635. tt_int_op(g->confirmed_idx, OP_EQ, 0);
  1636. tt_uint_op(g->is_pending, OP_EQ, 0); // primary implies non-pending.
  1637. tt_uint_op(state, OP_EQ, GUARD_CIRC_STATE_USABLE_ON_COMPLETION);
  1638. tt_i64_op(g->last_tried_to_connect, OP_EQ, approx_time());
  1639. tt_ptr_op(g, OP_EQ, smartlist_get(gs->primary_entry_guards, 0));
  1640. // But if we mark all the primary guards down...
  1641. SMARTLIST_FOREACH(gs->primary_entry_guards, entry_guard_t *, guard, {
  1642. guard->last_tried_to_connect = approx_time();
  1643. entry_guards_note_guard_failure(gs, guard);
  1644. });
  1645. // ... we should get a confirmed guard.
  1646. state = 9999;
  1647. g = select_entry_guard_for_circuit(gs, GUARD_USAGE_TRAFFIC, NULL, &state);
  1648. tt_assert(g);
  1649. tt_assert(! g->is_primary);
  1650. tt_int_op(g->confirmed_idx, OP_EQ, smartlist_len(gs->primary_entry_guards));
  1651. tt_assert(g->is_pending);
  1652. tt_uint_op(state, OP_EQ, GUARD_CIRC_STATE_USABLE_IF_NO_BETTER_GUARD);
  1653. tt_i64_op(g->last_tried_to_connect, OP_EQ, approx_time());
  1654. // And if we try again, we should get a different confirmed guard, since
  1655. // that one is pending.
  1656. state = 9999;
  1657. entry_guard_t *g2 = select_entry_guard_for_circuit(gs, GUARD_USAGE_TRAFFIC,
  1658. NULL, &state);
  1659. tt_assert(g2);
  1660. tt_assert(! g2->is_primary);
  1661. tt_ptr_op(g2, OP_NE, g);
  1662. tt_int_op(g2->confirmed_idx, OP_EQ,
  1663. smartlist_len(gs->primary_entry_guards)+1);
  1664. tt_assert(g2->is_pending);
  1665. tt_uint_op(state, OP_EQ, GUARD_CIRC_STATE_USABLE_IF_NO_BETTER_GUARD);
  1666. tt_i64_op(g2->last_tried_to_connect, OP_EQ, approx_time());
  1667. // If we say that the next confirmed guard in order is excluded, and
  1668. // we disable EnforceDistinctSubnets, we get the guard AFTER the
  1669. // one we excluded.
  1670. get_options_mutable()->EnforceDistinctSubnets = 0;
  1671. g = smartlist_get(gs->confirmed_entry_guards,
  1672. smartlist_len(gs->primary_entry_guards)+2);
  1673. rst = guard_create_exit_restriction((uint8_t*)g->identity);
  1674. g2 = select_entry_guard_for_circuit(gs, GUARD_USAGE_TRAFFIC, rst, &state);
  1675. tt_ptr_op(g2, OP_NE, NULL);
  1676. tt_ptr_op(g2, OP_NE, g);
  1677. tt_int_op(g2->confirmed_idx, OP_EQ,
  1678. smartlist_len(gs->primary_entry_guards)+3);
  1679. // If we make every confirmed guard become pending then we start poking
  1680. // other guards.
  1681. const int n_remaining_confirmed =
  1682. N_CONFIRMED - 3 - smartlist_len(gs->primary_entry_guards);
  1683. for (i = 0; i < n_remaining_confirmed; ++i) {
  1684. g = select_entry_guard_for_circuit(gs, GUARD_USAGE_TRAFFIC, NULL, &state);
  1685. tt_int_op(g->confirmed_idx, OP_GE, 0);
  1686. tt_assert(g);
  1687. }
  1688. state = 9999;
  1689. g = select_entry_guard_for_circuit(gs, GUARD_USAGE_TRAFFIC, NULL, &state);
  1690. tt_assert(g);
  1691. tt_assert(g->is_pending);
  1692. tt_int_op(g->confirmed_idx, OP_EQ, -1);
  1693. // If we EnforceDistinctSubnets and apply a restriction, we get
  1694. // nothing, since we put all of the nodes in the same /16.
  1695. // Regression test for bug 22753/TROVE-2017-006.
  1696. get_options_mutable()->EnforceDistinctSubnets = 1;
  1697. g = smartlist_get(gs->confirmed_entry_guards, 0);
  1698. memcpy(rst->exclude_id, g->identity, DIGEST_LEN);
  1699. g2 = select_entry_guard_for_circuit(gs, GUARD_USAGE_TRAFFIC, rst, &state);
  1700. tt_ptr_op(g2, OP_EQ, NULL);
  1701. done:
  1702. guard_selection_free(gs);
  1703. entry_guard_restriction_free(rst);
  1704. }
  1705. static void
  1706. test_entry_guard_select_for_circuit_highlevel_primary(void *arg)
  1707. {
  1708. /* Play around with selecting primary guards for circuits and markign
  1709. * them up and down */
  1710. (void)arg;
  1711. guard_selection_t *gs = guard_selection_new("default", GS_TYPE_NORMAL);
  1712. time_t start = approx_time();
  1713. const node_t *node = NULL;
  1714. circuit_guard_state_t *guard = NULL;
  1715. entry_guard_t *g;
  1716. guard_usable_t u;
  1717. /*
  1718. * Make sure that the pick-for-circuit API basically works. We'll get
  1719. * a primary guard, so it'll be usable on completion.
  1720. */
  1721. int r = entry_guard_pick_for_circuit(gs, GUARD_USAGE_TRAFFIC, NULL,
  1722. &node, &guard);
  1723. tt_int_op(r, OP_EQ, 0);
  1724. tt_assert(node);
  1725. tt_assert(guard);
  1726. tt_int_op(guard->state, OP_EQ, GUARD_CIRC_STATE_USABLE_ON_COMPLETION);
  1727. g = entry_guard_handle_get(guard->guard);
  1728. tt_assert(g);
  1729. tt_mem_op(g->identity, OP_EQ, node->identity, DIGEST_LEN);
  1730. tt_int_op(g->is_primary, OP_EQ, 1);
  1731. tt_i64_op(g->last_tried_to_connect, OP_EQ, start);
  1732. tt_int_op(g->confirmed_idx, OP_EQ, -1);
  1733. /* Call that circuit successful. */
  1734. update_approx_time(start+15);
  1735. u = entry_guard_succeeded(&guard);
  1736. tt_int_op(u, OP_EQ, GUARD_USABLE_NOW); /* We can use it now. */
  1737. tt_assert(guard);
  1738. tt_int_op(guard->state, OP_EQ, GUARD_CIRC_STATE_COMPLETE);
  1739. g = entry_guard_handle_get(guard->guard);
  1740. tt_assert(g);
  1741. tt_int_op(g->is_reachable, OP_EQ, GUARD_REACHABLE_YES);
  1742. tt_int_op(g->confirmed_idx, OP_EQ, 0);
  1743. circuit_guard_state_free(guard);
  1744. guard = NULL;
  1745. node = NULL;
  1746. g = NULL;
  1747. /* Try again. We'll also get a primary guard this time. (The same one,
  1748. in fact.) But this time, we'll say the connection has failed. */
  1749. update_approx_time(start+35);
  1750. r = entry_guard_pick_for_circuit(gs, GUARD_USAGE_TRAFFIC, NULL,
  1751. &node, &guard);
  1752. tt_int_op(r, OP_EQ, 0);
  1753. tt_assert(node);
  1754. tt_assert(guard);
  1755. tt_int_op(guard->state, OP_EQ, GUARD_CIRC_STATE_USABLE_ON_COMPLETION);
  1756. tt_i64_op(guard->state_set_at, OP_EQ, start+35);
  1757. g = entry_guard_handle_get(guard->guard);
  1758. tt_assert(g);
  1759. tt_mem_op(g->identity, OP_EQ, node->identity, DIGEST_LEN);
  1760. tt_int_op(g->is_primary, OP_EQ, 1);
  1761. tt_i64_op(g->last_tried_to_connect, OP_EQ, start+35);
  1762. tt_int_op(g->confirmed_idx, OP_EQ, 0); // same one.
  1763. /* It's failed! What will happen to our poor guard? */
  1764. update_approx_time(start+45);
  1765. entry_guard_failed(&guard);
  1766. tt_assert(guard);
  1767. tt_int_op(guard->state, OP_EQ, GUARD_CIRC_STATE_DEAD);
  1768. tt_i64_op(guard->state_set_at, OP_EQ, start+45);
  1769. g = entry_guard_handle_get(guard->guard);
  1770. tt_assert(g);
  1771. tt_int_op(g->is_reachable, OP_EQ, GUARD_REACHABLE_NO);
  1772. tt_i64_op(g->failing_since, OP_EQ, start+45);
  1773. tt_int_op(g->confirmed_idx, OP_EQ, 0); // still confirmed.
  1774. circuit_guard_state_free(guard);
  1775. guard = NULL;
  1776. node = NULL;
  1777. entry_guard_t *g_prev = g;
  1778. g = NULL;
  1779. /* Now try a third time. Since the other one is down, we'll get a different
  1780. * (still primary) guard.
  1781. */
  1782. update_approx_time(start+60);
  1783. r = entry_guard_pick_for_circuit(gs, GUARD_USAGE_TRAFFIC, NULL,
  1784. &node, &guard);
  1785. tt_int_op(r, OP_EQ, 0);
  1786. tt_assert(node);
  1787. tt_assert(guard);
  1788. tt_int_op(guard->state, OP_EQ, GUARD_CIRC_STATE_USABLE_ON_COMPLETION);
  1789. g = entry_guard_handle_get(guard->guard);
  1790. tt_assert(g);
  1791. tt_ptr_op(g, OP_NE, g_prev);
  1792. tt_mem_op(g->identity, OP_EQ, node->identity, DIGEST_LEN);
  1793. tt_mem_op(g->identity, OP_NE, g_prev->identity, DIGEST_LEN);
  1794. tt_int_op(g->is_primary, OP_EQ, 1);
  1795. tt_i64_op(g->last_tried_to_connect, OP_EQ, start+60);
  1796. tt_int_op(g->confirmed_idx, OP_EQ, -1); // not confirmed now.
  1797. /* Call this one up; watch it get confirmed. */
  1798. update_approx_time(start+90);
  1799. u = entry_guard_succeeded(&guard);
  1800. tt_int_op(u, OP_EQ, GUARD_USABLE_NOW);
  1801. tt_assert(guard);
  1802. tt_int_op(guard->state, OP_EQ, GUARD_CIRC_STATE_COMPLETE);
  1803. g = entry_guard_handle_get(guard->guard);
  1804. tt_assert(g);
  1805. tt_int_op(g->is_reachable, OP_EQ, GUARD_REACHABLE_YES);
  1806. tt_int_op(g->confirmed_idx, OP_EQ, 1);
  1807. done:
  1808. guard_selection_free(gs);
  1809. circuit_guard_state_free(guard);
  1810. }
  1811. static void
  1812. test_entry_guard_select_for_circuit_highlevel_confirm_other(void *arg)
  1813. {
  1814. (void) arg;
  1815. const int N_PRIMARY = DFLT_N_PRIMARY_GUARDS;
  1816. /* At the start, we have no confirmed guards. We'll mark the primary guards
  1817. * down, then confirm something else. As soon as we do, it should become
  1818. * primary, and we should get it next time. */
  1819. time_t start = approx_time();
  1820. guard_selection_t *gs = guard_selection_new("default", GS_TYPE_NORMAL);
  1821. circuit_guard_state_t *guard = NULL;
  1822. int i, r;
  1823. const node_t *node = NULL;
  1824. guard_usable_t u;
  1825. /* Declare that we're on the internet. */
  1826. entry_guards_note_internet_connectivity(gs);
  1827. /* Primary guards are down! */
  1828. for (i = 0; i < N_PRIMARY; ++i) {
  1829. r = entry_guard_pick_for_circuit(gs, GUARD_USAGE_TRAFFIC, NULL,
  1830. &node, &guard);
  1831. tt_assert(node);
  1832. tt_assert(guard);
  1833. tt_int_op(r, OP_EQ, 0);
  1834. tt_int_op(guard->state, OP_EQ, GUARD_CIRC_STATE_USABLE_ON_COMPLETION);
  1835. entry_guard_failed(&guard);
  1836. circuit_guard_state_free(guard);
  1837. guard = NULL;
  1838. node = NULL;
  1839. }
  1840. /* Next guard should be non-primary. */
  1841. node = NULL;
  1842. r = entry_guard_pick_for_circuit(gs, GUARD_USAGE_TRAFFIC, NULL,
  1843. &node, &guard);
  1844. tt_assert(node);
  1845. tt_assert(guard);
  1846. tt_int_op(r, OP_EQ, 0);
  1847. entry_guard_t *g = entry_guard_handle_get(guard->guard);
  1848. tt_assert(g);
  1849. tt_int_op(guard->state, OP_EQ, GUARD_CIRC_STATE_USABLE_IF_NO_BETTER_GUARD);
  1850. tt_int_op(g->confirmed_idx, OP_EQ, -1);
  1851. tt_int_op(g->is_primary, OP_EQ, 0);
  1852. tt_int_op(g->is_pending, OP_EQ, 1);
  1853. (void)start;
  1854. u = entry_guard_succeeded(&guard);
  1855. /* We're on the internet (by fiat), so this guard will get called "confirmed"
  1856. * and should immediately become primary.
  1857. */
  1858. tt_int_op(guard->state, OP_EQ, GUARD_CIRC_STATE_COMPLETE);
  1859. tt_assert(u == GUARD_USABLE_NOW);
  1860. tt_int_op(g->confirmed_idx, OP_EQ, 0);
  1861. tt_int_op(g->is_primary, OP_EQ, 1);
  1862. tt_int_op(g->is_pending, OP_EQ, 0);
  1863. done:
  1864. guard_selection_free(gs);
  1865. circuit_guard_state_free(guard);
  1866. }
  1867. static void
  1868. test_entry_guard_select_for_circuit_highlevel_primary_retry(void *arg)
  1869. {
  1870. (void) arg;
  1871. const int N_PRIMARY = DFLT_N_PRIMARY_GUARDS;
  1872. /* At the start, we have no confirmed guards. We'll mark the primary guards
  1873. * down, then confirm something else. As soon as we do, it should become
  1874. * primary, and we should get it next time. */
  1875. time_t start = approx_time();
  1876. guard_selection_t *gs = guard_selection_new("default", GS_TYPE_NORMAL);
  1877. circuit_guard_state_t *guard = NULL, *guard2 = NULL;
  1878. int i, r;
  1879. const node_t *node = NULL;
  1880. entry_guard_t *g;
  1881. guard_usable_t u;
  1882. /* Declare that we're on the internet. */
  1883. entry_guards_note_internet_connectivity(gs);
  1884. /* Make primary guards confirmed (so they won't be superseded by a later
  1885. * guard), then mark them down. */
  1886. for (i = 0; i < N_PRIMARY; ++i) {
  1887. r = entry_guard_pick_for_circuit(gs, GUARD_USAGE_TRAFFIC, NULL,
  1888. &node, &guard);
  1889. tt_assert(node);
  1890. tt_assert(guard);
  1891. tt_int_op(r, OP_EQ, 0);
  1892. tt_int_op(guard->state, OP_EQ, GUARD_CIRC_STATE_USABLE_ON_COMPLETION);
  1893. g = entry_guard_handle_get(guard->guard);
  1894. make_guard_confirmed(gs, g);
  1895. tt_int_op(g->is_primary, OP_EQ, 1);
  1896. entry_guard_failed(&guard);
  1897. circuit_guard_state_free(guard);
  1898. tt_int_op(g->is_reachable, OP_EQ, GUARD_REACHABLE_NO);
  1899. guard = NULL;
  1900. node = NULL;
  1901. }
  1902. /* Get another guard that we might try. */
  1903. r = entry_guard_pick_for_circuit(gs, GUARD_USAGE_TRAFFIC, NULL,
  1904. &node, &guard);
  1905. tt_assert(node);
  1906. tt_assert(guard);
  1907. tt_int_op(r, OP_EQ, 0);
  1908. tt_int_op(guard->state, OP_EQ, GUARD_CIRC_STATE_USABLE_IF_NO_BETTER_GUARD);
  1909. g = entry_guard_handle_get(guard->guard);
  1910. tt_int_op(g->is_primary, OP_EQ, 0);
  1911. tt_assert(entry_guards_all_primary_guards_are_down(gs));
  1912. /* And an hour has passed ... */
  1913. update_approx_time(start + 3600);
  1914. /* Say that guard has succeeded! */
  1915. u = entry_guard_succeeded(&guard);
  1916. tt_int_op(u, OP_EQ, GUARD_MAYBE_USABLE_LATER);
  1917. tt_int_op(guard->state, OP_EQ, GUARD_CIRC_STATE_WAITING_FOR_BETTER_GUARD);
  1918. g = entry_guard_handle_get(guard->guard);
  1919. /* The primary guards should have been marked up! */
  1920. SMARTLIST_FOREACH(gs->primary_entry_guards, entry_guard_t *, pg, {
  1921. tt_int_op(pg->is_primary, OP_EQ, 1);
  1922. tt_ptr_op(g, OP_NE, pg);
  1923. tt_int_op(pg->is_reachable, OP_EQ, GUARD_REACHABLE_MAYBE);
  1924. });
  1925. /* Have a circuit to a primary guard succeed. */
  1926. r = entry_guard_pick_for_circuit(gs, GUARD_USAGE_TRAFFIC, NULL,
  1927. &node, &guard2);
  1928. tt_int_op(r, OP_EQ, 0);
  1929. tt_int_op(guard2->state, OP_EQ, GUARD_CIRC_STATE_USABLE_ON_COMPLETION);
  1930. u = entry_guard_succeeded(&guard2);
  1931. tt_assert(u == GUARD_USABLE_NOW);
  1932. tt_int_op(guard2->state, OP_EQ, GUARD_CIRC_STATE_COMPLETE);
  1933. tt_assert(! entry_guards_all_primary_guards_are_down(gs));
  1934. done:
  1935. guard_selection_free(gs);
  1936. circuit_guard_state_free(guard);
  1937. circuit_guard_state_free(guard2);
  1938. }
  1939. static void
  1940. test_entry_guard_select_and_cancel(void *arg)
  1941. {
  1942. (void) arg;
  1943. const int N_PRIMARY = DFLT_N_PRIMARY_GUARDS;
  1944. int i,r;
  1945. const node_t *node = NULL;
  1946. circuit_guard_state_t *guard;
  1947. guard_selection_t *gs = guard_selection_new("default", GS_TYPE_NORMAL);
  1948. entry_guard_t *g;
  1949. /* Once more, we mark all the primary guards down. */
  1950. entry_guards_note_internet_connectivity(gs);
  1951. for (i = 0; i < N_PRIMARY; ++i) {
  1952. r = entry_guard_pick_for_circuit(gs, GUARD_USAGE_TRAFFIC, NULL,
  1953. &node, &guard);
  1954. tt_int_op(r, OP_EQ, 0);
  1955. tt_int_op(guard->state, OP_EQ, GUARD_CIRC_STATE_USABLE_ON_COMPLETION);
  1956. g = entry_guard_handle_get(guard->guard);
  1957. tt_int_op(g->is_primary, OP_EQ, 1);
  1958. tt_int_op(g->is_pending, OP_EQ, 0);
  1959. make_guard_confirmed(gs, g);
  1960. entry_guard_failed(&guard);
  1961. circuit_guard_state_free(guard);
  1962. guard = NULL;
  1963. node = NULL;
  1964. }
  1965. tt_assert(entry_guards_all_primary_guards_are_down(gs));
  1966. /* Now get another guard we could try... */
  1967. r = entry_guard_pick_for_circuit(gs, GUARD_USAGE_TRAFFIC, NULL,
  1968. &node, &guard);
  1969. tt_assert(node);
  1970. tt_assert(guard);
  1971. tt_int_op(r, OP_EQ, 0);
  1972. tt_int_op(guard->state, OP_EQ, GUARD_CIRC_STATE_USABLE_IF_NO_BETTER_GUARD);
  1973. g = entry_guard_handle_get(guard->guard);
  1974. tt_int_op(g->is_primary, OP_EQ, 0);
  1975. tt_int_op(g->is_pending, OP_EQ, 1);
  1976. /* Whoops! We should never have asked for this guard. Cancel the request! */
  1977. entry_guard_cancel(&guard);
  1978. tt_ptr_op(guard, OP_EQ, NULL);
  1979. tt_int_op(g->is_primary, OP_EQ, 0);
  1980. tt_int_op(g->is_pending, OP_EQ, 0);
  1981. done:
  1982. guard_selection_free(gs);
  1983. circuit_guard_state_free(guard);
  1984. }
  1985. static void
  1986. test_entry_guard_drop_guards(void *arg)
  1987. {
  1988. (void) arg;
  1989. int r;
  1990. const node_t *node = NULL;
  1991. circuit_guard_state_t *guard;
  1992. guard_selection_t *gs = get_guard_selection_info();
  1993. // Pick a guard, to get things set up.
  1994. r = entry_guard_pick_for_circuit(gs, GUARD_USAGE_TRAFFIC, NULL,
  1995. &node, &guard);
  1996. tt_int_op(r, OP_EQ, 0);
  1997. tt_int_op(smartlist_len(gs->sampled_entry_guards), OP_GE,
  1998. DFLT_MIN_FILTERED_SAMPLE_SIZE);
  1999. tt_ptr_op(gs, OP_EQ, get_guard_selection_info());
  2000. // Drop all the guards! (This is a bad idea....)
  2001. remove_all_entry_guards_for_guard_selection(gs);
  2002. gs = get_guard_selection_info();
  2003. tt_int_op(smartlist_len(gs->sampled_entry_guards), OP_EQ, 0);
  2004. tt_int_op(smartlist_len(gs->primary_entry_guards), OP_EQ, 0);
  2005. tt_int_op(smartlist_len(gs->confirmed_entry_guards), OP_EQ, 0);
  2006. done:
  2007. circuit_guard_state_free(guard);
  2008. guard_selection_free(gs);
  2009. }
  2010. /* Unit test setup function: Create a fake network, and set everything up
  2011. * for testing the upgrade-a-waiting-circuit code. */
  2012. typedef struct {
  2013. guard_selection_t *gs;
  2014. time_t start;
  2015. circuit_guard_state_t *guard1_state;
  2016. circuit_guard_state_t *guard2_state;
  2017. entry_guard_t *guard1;
  2018. entry_guard_t *guard2;
  2019. origin_circuit_t *circ1;
  2020. origin_circuit_t *circ2;
  2021. smartlist_t *all_origin_circuits;
  2022. } upgrade_circuits_data_t;
  2023. static void *
  2024. upgrade_circuits_setup(const struct testcase_t *testcase)
  2025. {
  2026. upgrade_circuits_data_t *data = tor_malloc_zero(sizeof(*data));
  2027. guard_selection_t *gs = data->gs =
  2028. guard_selection_new("default", GS_TYPE_NORMAL);
  2029. circuit_guard_state_t *guard;
  2030. const node_t *node;
  2031. entry_guard_t *g;
  2032. int i;
  2033. const int N_PRIMARY = DFLT_N_PRIMARY_GUARDS;
  2034. const char *argument = testcase->setup_data;
  2035. const int make_circ1_succeed = strstr(argument, "c1-done") != NULL;
  2036. const int make_circ2_succeed = strstr(argument, "c2-done") != NULL;
  2037. big_fake_network_setup(testcase);
  2038. /* We're going to set things up in a state where a circuit will be ready to
  2039. * be upgraded. Each test can make a single change (or not) that should
  2040. * block the upgrade.
  2041. */
  2042. /* First, make all the primary guards confirmed, and down. */
  2043. data->start = approx_time();
  2044. entry_guards_note_internet_connectivity(gs);
  2045. for (i = 0; i < N_PRIMARY; ++i) {
  2046. entry_guard_pick_for_circuit(gs, GUARD_USAGE_TRAFFIC, NULL, &node, &guard);
  2047. g = entry_guard_handle_get(guard->guard);
  2048. make_guard_confirmed(gs, g);
  2049. entry_guard_failed(&guard);
  2050. circuit_guard_state_free(guard);
  2051. }
  2052. /* Grab another couple of guards */
  2053. data->all_origin_circuits = smartlist_new();
  2054. update_approx_time(data->start + 27);
  2055. entry_guard_pick_for_circuit(gs, GUARD_USAGE_TRAFFIC, NULL,
  2056. &node, &data->guard1_state);
  2057. origin_circuit_t *circ;
  2058. data->circ1 = circ = origin_circuit_new();
  2059. circ->base_.purpose = CIRCUIT_PURPOSE_C_GENERAL;
  2060. circ->guard_state = data->guard1_state;
  2061. smartlist_add(data->all_origin_circuits, circ);
  2062. update_approx_time(data->start + 30);
  2063. entry_guard_pick_for_circuit(gs, GUARD_USAGE_TRAFFIC, NULL,
  2064. &node, &data->guard2_state);
  2065. data->circ2 = circ = origin_circuit_new();
  2066. circ->base_.purpose = CIRCUIT_PURPOSE_C_GENERAL;
  2067. circ->guard_state = data->guard2_state;
  2068. smartlist_add(data->all_origin_circuits, circ);
  2069. data->guard1 = entry_guard_handle_get(data->guard1_state->guard);
  2070. data->guard2 = entry_guard_handle_get(data->guard2_state->guard);
  2071. tor_assert(data->guard1 != data->guard2);
  2072. tor_assert(data->guard1_state->state ==
  2073. GUARD_CIRC_STATE_USABLE_IF_NO_BETTER_GUARD);
  2074. tor_assert(data->guard2_state->state ==
  2075. GUARD_CIRC_STATE_USABLE_IF_NO_BETTER_GUARD);
  2076. guard_usable_t r;
  2077. update_approx_time(data->start + 32);
  2078. if (make_circ1_succeed) {
  2079. r = entry_guard_succeeded(&data->guard1_state);
  2080. tor_assert(r == GUARD_MAYBE_USABLE_LATER);
  2081. tor_assert(data->guard1_state->state ==
  2082. GUARD_CIRC_STATE_WAITING_FOR_BETTER_GUARD);
  2083. }
  2084. update_approx_time(data->start + 33);
  2085. if (make_circ2_succeed) {
  2086. r = entry_guard_succeeded(&data->guard2_state);
  2087. tor_assert(r == GUARD_MAYBE_USABLE_LATER);
  2088. tor_assert(data->guard2_state->state ==
  2089. GUARD_CIRC_STATE_WAITING_FOR_BETTER_GUARD);
  2090. }
  2091. return data;
  2092. }
  2093. static int
  2094. upgrade_circuits_cleanup(const struct testcase_t *testcase, void *ptr)
  2095. {
  2096. upgrade_circuits_data_t *data = ptr;
  2097. // circuit_guard_state_free(data->guard1_state); // held in circ1
  2098. // circuit_guard_state_free(data->guard2_state); // held in circ2
  2099. guard_selection_free(data->gs);
  2100. smartlist_free(data->all_origin_circuits);
  2101. circuit_free_(TO_CIRCUIT(data->circ1));
  2102. circuit_free_(TO_CIRCUIT(data->circ2));
  2103. tor_free(data);
  2104. return big_fake_network_cleanup(testcase, NULL);
  2105. }
  2106. static void
  2107. test_entry_guard_upgrade_a_circuit(void *arg)
  2108. {
  2109. upgrade_circuits_data_t *data = arg;
  2110. /* This is the easy case: we have no COMPLETED circuits, all the
  2111. * primary guards are down, we have two WAITING circuits: one will
  2112. * get upgraded to COMPLETED! (The one that started first.)
  2113. */
  2114. smartlist_t *result = smartlist_new();
  2115. int r;
  2116. r = entry_guards_upgrade_waiting_circuits(data->gs,
  2117. data->all_origin_circuits,
  2118. result);
  2119. tt_int_op(r, OP_EQ, 1);
  2120. tt_int_op(smartlist_len(result), OP_EQ, 1);
  2121. origin_circuit_t *oc = smartlist_get(result, 0);
  2122. /* circ1 was started first, so we'll get told to ugrade it... */
  2123. tt_ptr_op(oc, OP_EQ, data->circ1);
  2124. /* And the guard state should be complete */
  2125. tt_ptr_op(data->guard1_state, OP_NE, NULL);
  2126. tt_int_op(data->guard1_state->state, OP_EQ, GUARD_CIRC_STATE_COMPLETE);
  2127. done:
  2128. smartlist_free(result);
  2129. }
  2130. static void
  2131. test_entry_guard_upgrade_blocked_by_live_primary_guards(void *arg)
  2132. {
  2133. upgrade_circuits_data_t *data = arg;
  2134. /* If any primary guards might be up, we can't upgrade any waiting
  2135. * circuits.
  2136. */
  2137. mark_primary_guards_maybe_reachable(data->gs);
  2138. smartlist_t *result = smartlist_new();
  2139. int r;
  2140. setup_capture_of_logs(LOG_DEBUG);
  2141. r = entry_guards_upgrade_waiting_circuits(data->gs,
  2142. data->all_origin_circuits,
  2143. result);
  2144. tt_int_op(r, OP_EQ, 0);
  2145. tt_int_op(smartlist_len(result), OP_EQ, 0);
  2146. expect_log_msg_containing("not all primary guards were definitely down.");
  2147. done:
  2148. teardown_capture_of_logs();
  2149. smartlist_free(result);
  2150. }
  2151. static void
  2152. test_entry_guard_upgrade_blocked_by_lack_of_waiting_circuits(void *arg)
  2153. {
  2154. upgrade_circuits_data_t *data = arg;
  2155. /* If no circuits are waiting, we can't upgrade anything. (The test
  2156. * setup in this case was told not to make any of the circuits "waiting".)
  2157. */
  2158. smartlist_t *result = smartlist_new();
  2159. int r;
  2160. setup_capture_of_logs(LOG_DEBUG);
  2161. r = entry_guards_upgrade_waiting_circuits(data->gs,
  2162. data->all_origin_circuits,
  2163. result);
  2164. tt_int_op(r, OP_EQ, 0);
  2165. tt_int_op(smartlist_len(result), OP_EQ, 0);
  2166. expect_log_msg_containing("Considered upgrading guard-stalled circuits, "
  2167. "but didn't find any.");
  2168. done:
  2169. teardown_capture_of_logs();
  2170. smartlist_free(result);
  2171. }
  2172. static void
  2173. test_entry_guard_upgrade_blocked_by_better_circ_complete(void *arg)
  2174. {
  2175. upgrade_circuits_data_t *data = arg;
  2176. /* We'll run through the logic of upgrade_a_circuit below...
  2177. * and then try again to make sure that circ2 isn't also upgraded.
  2178. */
  2179. smartlist_t *result = smartlist_new();
  2180. int r;
  2181. r = entry_guards_upgrade_waiting_circuits(data->gs,
  2182. data->all_origin_circuits,
  2183. result);
  2184. tt_int_op(r, OP_EQ, 1);
  2185. tt_int_op(smartlist_len(result), OP_EQ, 1);
  2186. origin_circuit_t *oc = smartlist_get(result, 0);
  2187. tt_ptr_op(oc, OP_EQ, data->circ1);
  2188. tt_ptr_op(data->guard1_state, OP_NE, NULL);
  2189. tt_int_op(data->guard1_state->state, OP_EQ, GUARD_CIRC_STATE_COMPLETE);
  2190. /* Now, try again. Make sure that circ2 isn't upgraded. */
  2191. smartlist_clear(result);
  2192. setup_capture_of_logs(LOG_DEBUG);
  2193. r = entry_guards_upgrade_waiting_circuits(data->gs,
  2194. data->all_origin_circuits,
  2195. result);
  2196. tt_int_op(r, OP_EQ, 0);
  2197. tt_int_op(smartlist_len(result), OP_EQ, 0);
  2198. expect_log_msg_containing("At least one complete circuit had higher "
  2199. "priority, so not upgrading.");
  2200. done:
  2201. teardown_capture_of_logs();
  2202. smartlist_free(result);
  2203. }
  2204. static void
  2205. test_entry_guard_upgrade_not_blocked_by_restricted_circ_complete(void *arg)
  2206. {
  2207. upgrade_circuits_data_t *data = arg;
  2208. /* Once more, let circ1 become complete. But this time, we'll claim
  2209. * that circ2 was restricted to not use the same guard as circ1. */
  2210. data->guard2_state->restrictions =
  2211. guard_create_exit_restriction((uint8_t*)data->guard1->identity);
  2212. smartlist_t *result = smartlist_new();
  2213. int r;
  2214. r = entry_guards_upgrade_waiting_circuits(data->gs,
  2215. data->all_origin_circuits,
  2216. result);
  2217. tt_int_op(r, OP_EQ, 1);
  2218. tt_int_op(smartlist_len(result), OP_EQ, 1);
  2219. origin_circuit_t *oc = smartlist_get(result, 0);
  2220. tt_ptr_op(oc, OP_EQ, data->circ1);
  2221. tt_ptr_op(data->guard1_state, OP_NE, NULL);
  2222. tt_int_op(data->guard1_state->state, OP_EQ, GUARD_CIRC_STATE_COMPLETE);
  2223. /* Now, we try again. Since circ2 has a restriction that circ1 doesn't obey,
  2224. * circ2 _is_ eligible for upgrade. */
  2225. smartlist_clear(result);
  2226. r = entry_guards_upgrade_waiting_circuits(data->gs,
  2227. data->all_origin_circuits,
  2228. result);
  2229. tt_int_op(r, OP_EQ, 1);
  2230. tt_int_op(smartlist_len(result), OP_EQ, 1);
  2231. origin_circuit_t *oc2 = smartlist_get(result, 0);
  2232. tt_ptr_op(oc2, OP_EQ, data->circ2);
  2233. done:
  2234. smartlist_free(result);
  2235. }
  2236. static void
  2237. test_entry_guard_upgrade_not_blocked_by_worse_circ_complete(void *arg)
  2238. {
  2239. upgrade_circuits_data_t *data = arg;
  2240. smartlist_t *result = smartlist_new();
  2241. /* here we manually make circ2 COMPLETE, and make sure that circ1
  2242. * gets made complete anyway, since guard1 has higher priority
  2243. */
  2244. update_approx_time(data->start + 300);
  2245. data->guard2_state->state = GUARD_CIRC_STATE_COMPLETE;
  2246. data->guard2_state->state_set_at = approx_time();
  2247. update_approx_time(data->start + 301);
  2248. /* Now, try again. Make sure that circ1 is approved. */
  2249. int r;
  2250. r = entry_guards_upgrade_waiting_circuits(data->gs,
  2251. data->all_origin_circuits,
  2252. result);
  2253. tt_int_op(r, OP_EQ, 1);
  2254. tt_int_op(smartlist_len(result), OP_EQ, 1);
  2255. origin_circuit_t *oc = smartlist_get(result, 0);
  2256. tt_ptr_op(oc, OP_EQ, data->circ1);
  2257. done:
  2258. smartlist_free(result);
  2259. }
  2260. static void
  2261. test_entry_guard_upgrade_blocked_by_better_circ_pending(void *arg)
  2262. {
  2263. upgrade_circuits_data_t *data = arg;
  2264. /* circ2 is done, but circ1 is still pending. Since circ1 is better,
  2265. * we won't upgrade circ2. */
  2266. /* XXXX Prop271 -- this is a kludge. I'm making sure circ1 _is_ better,
  2267. * by messing with the guards' confirmed_idx */
  2268. make_guard_confirmed(data->gs, data->guard1);
  2269. {
  2270. int tmp;
  2271. tmp = data->guard1->confirmed_idx;
  2272. data->guard1->confirmed_idx = data->guard2->confirmed_idx;
  2273. data->guard2->confirmed_idx = tmp;
  2274. }
  2275. smartlist_t *result = smartlist_new();
  2276. setup_capture_of_logs(LOG_DEBUG);
  2277. int r;
  2278. r = entry_guards_upgrade_waiting_circuits(data->gs,
  2279. data->all_origin_circuits,
  2280. result);
  2281. tt_int_op(r, OP_EQ, 0);
  2282. tt_int_op(smartlist_len(result), OP_EQ, 0);
  2283. expect_log_msg_containing("but 1 pending circuit(s) had higher guard "
  2284. "priority, so not upgrading.");
  2285. done:
  2286. teardown_capture_of_logs();
  2287. smartlist_free(result);
  2288. }
  2289. static void
  2290. test_entry_guard_upgrade_not_blocked_by_restricted_circ_pending(void *arg)
  2291. {
  2292. upgrade_circuits_data_t *data = arg;
  2293. /* circ2 is done, but circ1 is still pending. But when there is a
  2294. restriction on circ2 that circ1 can't satisfy, circ1 can't block
  2295. circ2. */
  2296. /* XXXX Prop271 -- this is a kludge. I'm making sure circ1 _is_ better,
  2297. * by messing with the guards' confirmed_idx */
  2298. make_guard_confirmed(data->gs, data->guard1);
  2299. {
  2300. int tmp;
  2301. tmp = data->guard1->confirmed_idx;
  2302. data->guard1->confirmed_idx = data->guard2->confirmed_idx;
  2303. data->guard2->confirmed_idx = tmp;
  2304. }
  2305. data->guard2_state->restrictions =
  2306. guard_create_exit_restriction((uint8_t*)data->guard1->identity);
  2307. smartlist_t *result = smartlist_new();
  2308. int r;
  2309. r = entry_guards_upgrade_waiting_circuits(data->gs,
  2310. data->all_origin_circuits,
  2311. result);
  2312. tt_int_op(r, OP_EQ, 1);
  2313. tt_int_op(smartlist_len(result), OP_EQ, 1);
  2314. origin_circuit_t *oc = smartlist_get(result, 0);
  2315. tt_ptr_op(oc, OP_EQ, data->circ2);
  2316. done:
  2317. smartlist_free(result);
  2318. }
  2319. static void
  2320. test_entry_guard_upgrade_not_blocked_by_worse_circ_pending(void *arg)
  2321. {
  2322. upgrade_circuits_data_t *data = arg;
  2323. /* circ1 is done, but circ2 is still pending. Since circ1 is better,
  2324. * we will upgrade it. */
  2325. smartlist_t *result = smartlist_new();
  2326. int r;
  2327. r = entry_guards_upgrade_waiting_circuits(data->gs,
  2328. data->all_origin_circuits,
  2329. result);
  2330. tt_int_op(r, OP_EQ, 1);
  2331. tt_int_op(smartlist_len(result), OP_EQ, 1);
  2332. origin_circuit_t *oc = smartlist_get(result, 0);
  2333. tt_ptr_op(oc, OP_EQ, data->circ1);
  2334. done:
  2335. smartlist_free(result);
  2336. }
  2337. static void
  2338. test_enty_guard_should_expire_waiting(void *arg)
  2339. {
  2340. (void)arg;
  2341. circuit_guard_state_t *fake_state = tor_malloc_zero(sizeof(*fake_state));
  2342. /* We'll leave "guard" unset -- it won't matter here. */
  2343. /* No state? Can't expire. */
  2344. tt_assert(! entry_guard_state_should_expire(NULL));
  2345. /* Let's try one that expires. */
  2346. fake_state->state = GUARD_CIRC_STATE_WAITING_FOR_BETTER_GUARD;
  2347. fake_state->state_set_at =
  2348. approx_time() - DFLT_NONPRIMARY_GUARD_IDLE_TIMEOUT - 1;
  2349. tt_assert(entry_guard_state_should_expire(fake_state));
  2350. /* But it wouldn't expire if we changed the state. */
  2351. fake_state->state = GUARD_CIRC_STATE_USABLE_IF_NO_BETTER_GUARD;
  2352. tt_assert(! entry_guard_state_should_expire(fake_state));
  2353. /* And it wouldn't have expired a few seconds ago. */
  2354. fake_state->state = GUARD_CIRC_STATE_WAITING_FOR_BETTER_GUARD;
  2355. fake_state->state_set_at =
  2356. approx_time() - DFLT_NONPRIMARY_GUARD_IDLE_TIMEOUT + 5;
  2357. tt_assert(! entry_guard_state_should_expire(fake_state));
  2358. done:
  2359. tor_free(fake_state);
  2360. }
  2361. /** Test that the number of primary guards can be controlled using torrc */
  2362. static void
  2363. test_entry_guard_number_of_primaries(void *arg)
  2364. {
  2365. (void) arg;
  2366. /* Get default value */
  2367. tt_int_op(get_n_primary_guards(), OP_EQ, DFLT_N_PRIMARY_GUARDS);
  2368. /* Set number of primaries using torrc */
  2369. get_options_mutable()->NumPrimaryGuards = 42;
  2370. tt_int_op(get_n_primary_guards(), OP_EQ, 42);
  2371. done:
  2372. ;
  2373. }
  2374. static void
  2375. mock_directory_initiate_request(directory_request_t *req)
  2376. {
  2377. if (req->guard_state) {
  2378. circuit_guard_state_free(req->guard_state);
  2379. }
  2380. }
  2381. static networkstatus_t *mock_ns_val = NULL;
  2382. static networkstatus_t *
  2383. mock_ns_get_by_flavor(consensus_flavor_t f)
  2384. {
  2385. (void)f;
  2386. return mock_ns_val;
  2387. }
  2388. /** Test that when we fetch microdescriptors we skip guards that have
  2389. * previously failed to serve us needed microdescriptors. */
  2390. static void
  2391. test_entry_guard_outdated_dirserver_exclusion(void *arg)
  2392. {
  2393. int retval;
  2394. response_handler_args_t *args = NULL;
  2395. dir_connection_t *conn = NULL;
  2396. (void) arg;
  2397. /* Test prep: Make a new guard selection */
  2398. guard_selection_t *gs = get_guard_selection_by_name("default",
  2399. GS_TYPE_NORMAL, 1);
  2400. /* ... we want to use entry guards */
  2401. or_options_t *options = get_options_mutable();
  2402. options->UseEntryGuards = 1;
  2403. options->UseBridges = 0;
  2404. /* ... prepare some md digests we want to download in the future */
  2405. smartlist_t *digests = smartlist_new();
  2406. const char *prose = "unhurried and wise, we perceive.";
  2407. for (int i = 0; i < 20; i++) {
  2408. smartlist_add(digests, (char*)prose);
  2409. }
  2410. tt_int_op(smartlist_len(digests), OP_EQ, 20);
  2411. /* ... now mock some functions */
  2412. mock_ns_val = tor_malloc_zero(sizeof(networkstatus_t));
  2413. MOCK(networkstatus_get_latest_consensus_by_flavor, mock_ns_get_by_flavor);
  2414. MOCK(directory_initiate_request, mock_directory_initiate_request);
  2415. /* Test logic:
  2416. * 0. Create a proper guard set and primary guard list.
  2417. * 1. Pretend to fail microdescriptor fetches from all the primary guards.
  2418. * 2. Order another microdescriptor fetch and make sure that primary guards
  2419. * get skipped since they failed previous fetches.
  2420. */
  2421. { /* Setup primary guard list */
  2422. int i;
  2423. entry_guards_update_primary(gs);
  2424. for (i = 0; i < DFLT_N_PRIMARY_GUARDS; ++i) {
  2425. entry_guard_t *guard = smartlist_get(gs->sampled_entry_guards, i);
  2426. make_guard_confirmed(gs, guard);
  2427. }
  2428. entry_guards_update_primary(gs);
  2429. }
  2430. {
  2431. /* Fail microdesc fetches with all the primary guards */
  2432. args = tor_malloc_zero(sizeof(response_handler_args_t));
  2433. args->status_code = 404;
  2434. args->reason = NULL;
  2435. args->body = NULL;
  2436. args->body_len = 0;
  2437. conn = tor_malloc_zero(sizeof(dir_connection_t));
  2438. conn->requested_resource = tor_strdup("d/jlinblackorigami");
  2439. conn->base_.purpose = DIR_PURPOSE_FETCH_MICRODESC;
  2440. /* Pretend to fail fetches with all primary guards */
  2441. SMARTLIST_FOREACH_BEGIN(gs->primary_entry_guards,const entry_guard_t *,g) {
  2442. memcpy(conn->identity_digest, g->identity, DIGEST_LEN);
  2443. retval = handle_response_fetch_microdesc(conn, args);
  2444. tt_int_op(retval, OP_EQ, 0);
  2445. } SMARTLIST_FOREACH_END(g);
  2446. }
  2447. {
  2448. /* Now order the final md download */
  2449. setup_full_capture_of_logs(LOG_INFO);
  2450. initiate_descriptor_downloads(NULL, DIR_PURPOSE_FETCH_MICRODESC,
  2451. digests, 3, 7, 0);
  2452. /* ... and check that because we failed to fetch microdescs from all our
  2453. * primaries, we didnt end up selecting a primary for fetching dir info */
  2454. expect_log_msg_containing("No primary or confirmed guards available.");
  2455. teardown_capture_of_logs();
  2456. }
  2457. done:
  2458. smartlist_free(digests);
  2459. tor_free(args);
  2460. if (conn) {
  2461. tor_free(conn->requested_resource);
  2462. tor_free(conn);
  2463. }
  2464. }
  2465. /** Test helper to extend the <b>oc</b> circuit path <b>n</b> times and then
  2466. * ensure that the circuit is now complete. */
  2467. static void
  2468. helper_extend_circuit_path_n_times(origin_circuit_t *oc, int n)
  2469. {
  2470. int retval;
  2471. int i;
  2472. /* Extend path n times */
  2473. for (i = 0 ; i < n ; i++) {
  2474. retval = onion_extend_cpath(oc);
  2475. tt_int_op(retval, OP_EQ, 0);
  2476. tt_int_op(circuit_get_cpath_len(oc), OP_EQ, i+1);
  2477. }
  2478. /* Now do it one last time and see that circ is complete */
  2479. retval = onion_extend_cpath(oc);
  2480. tt_int_op(retval, OP_EQ, 1);
  2481. done:
  2482. ;
  2483. }
  2484. /** Test for basic Tor path selection. Makes sure we build 3-hop circuits. */
  2485. static void
  2486. test_entry_guard_basic_path_selection(void *arg)
  2487. {
  2488. (void) arg;
  2489. int retval;
  2490. /* Enable entry guards */
  2491. or_options_t *options = get_options_mutable();
  2492. options->UseEntryGuards = 1;
  2493. /* disables /16 check since all nodes have the same addr... */
  2494. options->EnforceDistinctSubnets = 0;
  2495. /* Create our circuit */
  2496. circuit_t *circ = dummy_origin_circuit_new(30);
  2497. origin_circuit_t *oc = TO_ORIGIN_CIRCUIT(circ);
  2498. oc->build_state = tor_malloc_zero(sizeof(cpath_build_state_t));
  2499. /* First pick the exit and pin it on the build_state */
  2500. retval = onion_pick_cpath_exit(oc, NULL, 0);
  2501. tt_int_op(retval, OP_EQ, 0);
  2502. /* Extend path 3 times. First we pick guard, then middle, then exit. */
  2503. helper_extend_circuit_path_n_times(oc, 3);
  2504. done:
  2505. circuit_free_(circ);
  2506. }
  2507. /** Test helper to build an L2 and L3 vanguard list. The vanguard lists
  2508. * produced should be completely disjoint. */
  2509. static void
  2510. helper_setup_vanguard_list(or_options_t *options)
  2511. {
  2512. int i = 0;
  2513. /* Add some nodes to the vanguard L2 list */
  2514. options->HSLayer2Nodes = routerset_new();
  2515. for (i = 0; i < 10 ; i += 2) {
  2516. node_t *vanguard_node = smartlist_get(big_fake_net_nodes, i);
  2517. tt_assert(vanguard_node->is_possible_guard);
  2518. routerset_parse(options->HSLayer2Nodes, vanguard_node->rs->nickname, "l2");
  2519. }
  2520. /* also add some nodes to vanguard L3 list
  2521. * (L2 list and L3 list should be disjoint for this test to work) */
  2522. options->HSLayer3Nodes = routerset_new();
  2523. for (i = 10; i < 20 ; i += 2) {
  2524. node_t *vanguard_node = smartlist_get(big_fake_net_nodes, i);
  2525. tt_assert(vanguard_node->is_possible_guard);
  2526. routerset_parse(options->HSLayer3Nodes, vanguard_node->rs->nickname, "l3");
  2527. }
  2528. done:
  2529. ;
  2530. }
  2531. /** Test to ensure that vanguard path selection works properly. Ensures that
  2532. * default vanguard circuits are 4 hops, and that path selection works
  2533. * correctly given the vanguard settings. */
  2534. static void
  2535. test_entry_guard_vanguard_path_selection(void *arg)
  2536. {
  2537. (void) arg;
  2538. int retval;
  2539. /* Enable entry guards */
  2540. or_options_t *options = get_options_mutable();
  2541. options->UseEntryGuards = 1;
  2542. /* XXX disables /16 check */
  2543. options->EnforceDistinctSubnets = 0;
  2544. /* Setup our vanguard list */
  2545. helper_setup_vanguard_list(options);
  2546. /* Create our circuit */
  2547. circuit_t *circ = dummy_origin_circuit_new(30);
  2548. origin_circuit_t *oc = TO_ORIGIN_CIRCUIT(circ);
  2549. oc->build_state = tor_malloc_zero(sizeof(cpath_build_state_t));
  2550. oc->build_state->is_internal = 1;
  2551. /* Switch circuit purpose to vanguards */
  2552. circ->purpose = CIRCUIT_PURPOSE_HS_VANGUARDS;
  2553. /* First pick the exit and pin it on the build_state */
  2554. tt_int_op(oc->build_state->desired_path_len, OP_EQ, 0);
  2555. retval = onion_pick_cpath_exit(oc, NULL, 0);
  2556. tt_int_op(retval, OP_EQ, 0);
  2557. /* Ensure that vanguards make 4-hop circuits by default */
  2558. tt_int_op(oc->build_state->desired_path_len, OP_EQ, 4);
  2559. /* Extend path as many times as needed to have complete circ. */
  2560. helper_extend_circuit_path_n_times(oc, oc->build_state->desired_path_len);
  2561. /* Test that the cpath linked list is set correctly. */
  2562. crypt_path_t *l1_node = oc->cpath;
  2563. crypt_path_t *l2_node = l1_node->next;
  2564. crypt_path_t *l3_node = l2_node->next;
  2565. crypt_path_t *l4_node = l3_node->next;
  2566. crypt_path_t *l1_node_again = l4_node->next;
  2567. tt_ptr_op(l1_node, OP_EQ, l1_node_again);
  2568. /* Test that L2 is indeed HSLayer2Node */
  2569. retval = routerset_contains_extendinfo(options->HSLayer2Nodes,
  2570. l2_node->extend_info);
  2571. tt_int_op(retval, OP_EQ, 4);
  2572. /* test that L3 node is _not_ contained in HSLayer2Node */
  2573. retval = routerset_contains_extendinfo(options->HSLayer2Nodes,
  2574. l3_node->extend_info);
  2575. tt_int_op(retval, OP_LT, 4);
  2576. /* Test that L3 is indeed HSLayer3Node */
  2577. retval = routerset_contains_extendinfo(options->HSLayer3Nodes,
  2578. l3_node->extend_info);
  2579. tt_int_op(retval, OP_EQ, 4);
  2580. /* test that L2 node is _not_ contained in HSLayer3Node */
  2581. retval = routerset_contains_extendinfo(options->HSLayer3Nodes,
  2582. l2_node->extend_info);
  2583. tt_int_op(retval, OP_LT, 4);
  2584. /* TODO: Test that L1 can be the same as exit. To test this we need start
  2585. enforcing EnforceDistinctSubnets again, which means that we need to give
  2586. each test node a different address which currently breaks some tests. */
  2587. done:
  2588. circuit_free_(circ);
  2589. }
  2590. static const struct testcase_setup_t big_fake_network = {
  2591. big_fake_network_setup, big_fake_network_cleanup
  2592. };
  2593. static const struct testcase_setup_t upgrade_circuits = {
  2594. upgrade_circuits_setup, upgrade_circuits_cleanup
  2595. };
  2596. #define BFN_TEST(name) \
  2597. { #name, test_entry_guard_ ## name, TT_FORK, &big_fake_network, NULL }
  2598. #define UPGRADE_TEST(name, arg) \
  2599. { #name, test_entry_guard_ ## name, TT_FORK, &upgrade_circuits, \
  2600. (void*)(arg) }
  2601. struct testcase_t entrynodes_tests[] = {
  2602. { "node_preferred_orport",
  2603. test_node_preferred_orport,
  2604. 0, NULL, NULL },
  2605. { "entry_guard_describe", test_entry_guard_describe, 0, NULL, NULL },
  2606. { "randomize_time", test_entry_guard_randomize_time, 0, NULL, NULL },
  2607. { "encode_for_state_minimal",
  2608. test_entry_guard_encode_for_state_minimal, 0, NULL, NULL },
  2609. { "encode_for_state_maximal",
  2610. test_entry_guard_encode_for_state_maximal, 0, NULL, NULL },
  2611. { "parse_from_state_minimal",
  2612. test_entry_guard_parse_from_state_minimal, 0, NULL, NULL },
  2613. { "parse_from_state_maximal",
  2614. test_entry_guard_parse_from_state_maximal, 0, NULL, NULL },
  2615. { "parse_from_state_failure",
  2616. test_entry_guard_parse_from_state_failure, 0, NULL, NULL },
  2617. { "parse_from_state_partial_failure",
  2618. test_entry_guard_parse_from_state_partial_failure, 0, NULL, NULL },
  2619. { "parse_from_state_full",
  2620. test_entry_guard_parse_from_state_full, TT_FORK, NULL, NULL },
  2621. { "parse_from_state_broken",
  2622. test_entry_guard_parse_from_state_broken, TT_FORK, NULL, NULL },
  2623. { "get_guard_selection_by_name",
  2624. test_entry_guard_get_guard_selection_by_name, TT_FORK, NULL, NULL },
  2625. { "number_of_primaries",
  2626. test_entry_guard_number_of_primaries, TT_FORK, NULL, NULL },
  2627. BFN_TEST(choose_selection_initial),
  2628. BFN_TEST(add_single_guard),
  2629. BFN_TEST(node_filter),
  2630. BFN_TEST(expand_sample),
  2631. BFN_TEST(expand_sample_small_net),
  2632. BFN_TEST(update_from_consensus_status),
  2633. BFN_TEST(update_from_consensus_repair),
  2634. BFN_TEST(update_from_consensus_remove),
  2635. BFN_TEST(confirming_guards),
  2636. BFN_TEST(sample_reachable_filtered),
  2637. BFN_TEST(sample_reachable_filtered_empty),
  2638. BFN_TEST(retry_unreachable),
  2639. BFN_TEST(manage_primary),
  2640. { "guard_preferred", test_entry_guard_guard_preferred, TT_FORK, NULL, NULL },
  2641. BFN_TEST(select_for_circuit_no_confirmed),
  2642. BFN_TEST(select_for_circuit_confirmed),
  2643. BFN_TEST(select_for_circuit_highlevel_primary),
  2644. BFN_TEST(select_for_circuit_highlevel_confirm_other),
  2645. BFN_TEST(select_for_circuit_highlevel_primary_retry),
  2646. BFN_TEST(select_and_cancel),
  2647. BFN_TEST(drop_guards),
  2648. BFN_TEST(outdated_dirserver_exclusion),
  2649. BFN_TEST(basic_path_selection),
  2650. BFN_TEST(vanguard_path_selection),
  2651. UPGRADE_TEST(upgrade_a_circuit, "c1-done c2-done"),
  2652. UPGRADE_TEST(upgrade_blocked_by_live_primary_guards, "c1-done c2-done"),
  2653. UPGRADE_TEST(upgrade_blocked_by_lack_of_waiting_circuits, ""),
  2654. UPGRADE_TEST(upgrade_blocked_by_better_circ_complete, "c1-done c2-done"),
  2655. UPGRADE_TEST(upgrade_not_blocked_by_restricted_circ_complete,
  2656. "c1-done c2-done"),
  2657. UPGRADE_TEST(upgrade_not_blocked_by_worse_circ_complete, "c1-done c2-done"),
  2658. UPGRADE_TEST(upgrade_blocked_by_better_circ_pending, "c2-done"),
  2659. UPGRADE_TEST(upgrade_not_blocked_by_restricted_circ_pending,
  2660. "c2-done"),
  2661. UPGRADE_TEST(upgrade_not_blocked_by_worse_circ_pending, "c1-done"),
  2662. { "should_expire_waiting", test_enty_guard_should_expire_waiting, TT_FORK,
  2663. NULL, NULL },
  2664. END_OF_TESTCASES
  2665. };