test_entrynodes.c 93 KB

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