test_entrynodes.c 103 KB

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