test_entrynodes.c 103 KB

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