test_entrynodes.c 105 KB

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