test_entrynodes.c 90 KB

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