test_entrynodes.c 103 KB

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