test_entrynodes.c 91 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690
  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. guard_selection_t *gs_legacy =
  645. get_guard_selection_by_name("legacy", GS_TYPE_LEGACY, 0);
  646. tt_assert(gs_legacy == NULL);
  647. done:
  648. config_free_lines(lines);
  649. tor_free(state);
  650. tor_free(msg);
  651. UNMOCK(get_or_state);
  652. }
  653. static void
  654. test_entry_guard_get_guard_selection_by_name(void *arg)
  655. {
  656. (void)arg;
  657. guard_selection_t *gs1, *gs2, *gs3;
  658. gs1 = get_guard_selection_by_name("unlikely", GS_TYPE_NORMAL, 0);
  659. tt_assert(gs1 == NULL);
  660. gs1 = get_guard_selection_by_name("unlikely", GS_TYPE_NORMAL, 1);
  661. tt_assert(gs1 != NULL);
  662. gs2 = get_guard_selection_by_name("unlikely", GS_TYPE_NORMAL, 1);
  663. tt_assert(gs2 == gs1);
  664. gs2 = get_guard_selection_by_name("unlikely", GS_TYPE_NORMAL, 0);
  665. tt_assert(gs2 == gs1);
  666. gs2 = get_guard_selection_by_name("implausible", GS_TYPE_NORMAL, 0);
  667. tt_assert(gs2 == NULL);
  668. gs2 = get_guard_selection_by_name("implausible", GS_TYPE_NORMAL, 1);
  669. tt_assert(gs2 != NULL);
  670. tt_assert(gs2 != gs1);
  671. gs3 = get_guard_selection_by_name("implausible", GS_TYPE_NORMAL, 0);
  672. tt_assert(gs3 == gs2);
  673. gs3 = get_guard_selection_by_name("default", GS_TYPE_NORMAL, 0);
  674. tt_assert(gs3 == NULL);
  675. gs3 = get_guard_selection_by_name("default", GS_TYPE_NORMAL, 1);
  676. tt_assert(gs3 != NULL);
  677. tt_assert(gs3 != gs2);
  678. tt_assert(gs3 != gs1);
  679. tt_assert(gs3 == get_guard_selection_info());
  680. done:
  681. entry_guards_free_all();
  682. }
  683. static void
  684. test_entry_guard_choose_selection_initial(void *arg)
  685. {
  686. /* Tests for picking our initial guard selection (based on having had
  687. * no previous selection */
  688. (void)arg;
  689. guard_selection_type_t type = GS_TYPE_INFER;
  690. const char *name = choose_guard_selection(get_options(),
  691. dummy_consensus, NULL, &type);
  692. tt_str_op(name, OP_EQ, "default");
  693. tt_int_op(type, OP_EQ, GS_TYPE_NORMAL);
  694. /* If we're using bridges, we get the bridge selection. */
  695. get_options_mutable()->UseBridges = 1;
  696. name = choose_guard_selection(get_options(),
  697. dummy_consensus, NULL, &type);
  698. tt_str_op(name, OP_EQ, "bridges");
  699. tt_int_op(type, OP_EQ, GS_TYPE_BRIDGE);
  700. get_options_mutable()->UseBridges = 0;
  701. /* If we discard >99% of our guards, though, we should be in the restricted
  702. * set. */
  703. tt_assert(get_options_mutable()->EntryNodes == NULL);
  704. get_options_mutable()->EntryNodes = routerset_new();
  705. routerset_parse(get_options_mutable()->EntryNodes, "1.0.0.0/8", "foo");
  706. name = choose_guard_selection(get_options(),
  707. dummy_consensus, NULL, &type);
  708. tt_str_op(name, OP_EQ, "restricted");
  709. tt_int_op(type, OP_EQ, GS_TYPE_RESTRICTED);
  710. done:
  711. ;
  712. }
  713. static void
  714. test_entry_guard_add_single_guard(void *arg)
  715. {
  716. (void)arg;
  717. guard_selection_t *gs = guard_selection_new("default", GS_TYPE_NORMAL);
  718. /* 1: Add a single guard to the sample. */
  719. node_t *n1 = smartlist_get(big_fake_net_nodes, 0);
  720. time_t now = approx_time();
  721. tt_assert(n1->is_possible_guard == 1);
  722. entry_guard_t *g1 = entry_guard_add_to_sample(gs, n1);
  723. tt_assert(g1);
  724. /* Make sure its fields look right. */
  725. tt_mem_op(n1->identity, OP_EQ, g1->identity, DIGEST_LEN);
  726. tt_i64_op(g1->sampled_on_date, OP_GE, now - 12*86400);
  727. tt_i64_op(g1->sampled_on_date, OP_LE, now);
  728. tt_str_op(g1->sampled_by_version, OP_EQ, VERSION);
  729. tt_assert(g1->currently_listed == 1);
  730. tt_i64_op(g1->confirmed_on_date, OP_EQ, 0);
  731. tt_int_op(g1->confirmed_idx, OP_EQ, -1);
  732. tt_int_op(g1->last_tried_to_connect, OP_EQ, 0);
  733. tt_uint_op(g1->is_reachable, OP_EQ, GUARD_REACHABLE_MAYBE);
  734. tt_i64_op(g1->failing_since, OP_EQ, 0);
  735. tt_assert(g1->is_filtered_guard == 1);
  736. tt_assert(g1->is_usable_filtered_guard == 1);
  737. tt_assert(g1->is_primary == 0);
  738. tt_assert(g1->extra_state_fields == NULL);
  739. /* Make sure it got added. */
  740. tt_int_op(1, OP_EQ, smartlist_len(gs->sampled_entry_guards));
  741. tt_ptr_op(g1, OP_EQ, smartlist_get(gs->sampled_entry_guards, 0));
  742. tt_ptr_op(g1, OP_EQ, get_sampled_guard_with_id(gs, (uint8_t*)n1->identity));
  743. const uint8_t bad_id[20] = {0};
  744. tt_ptr_op(NULL, OP_EQ, get_sampled_guard_with_id(gs, bad_id));
  745. done:
  746. guard_selection_free(gs);
  747. }
  748. static void
  749. test_entry_guard_node_filter(void *arg)
  750. {
  751. (void)arg;
  752. guard_selection_t *gs = guard_selection_new("default", GS_TYPE_NORMAL);
  753. bridge_line_t *bl = NULL;
  754. /* Initialize a bunch of node objects that are all guards. */
  755. #define NUM 7
  756. node_t *n[NUM];
  757. entry_guard_t *g[NUM];
  758. int i;
  759. for (i=0; i < NUM; ++i) {
  760. n[i] = smartlist_get(big_fake_net_nodes, i*2); // even ones are guards.
  761. g[i] = entry_guard_add_to_sample(gs, n[i]);
  762. // everything starts out filtered-in
  763. tt_assert(g[i]->is_filtered_guard == 1);
  764. tt_assert(g[i]->is_usable_filtered_guard == 1);
  765. }
  766. tt_int_op(num_reachable_filtered_guards(gs, NULL), OP_EQ, NUM);
  767. /* Make sure refiltering doesn't hurt */
  768. entry_guards_update_filtered_sets(gs);
  769. for (i = 0; i < NUM; ++i) {
  770. tt_assert(g[i]->is_filtered_guard == 1);
  771. tt_assert(g[i]->is_usable_filtered_guard == 1);
  772. }
  773. tt_int_op(num_reachable_filtered_guards(gs, NULL), OP_EQ, NUM);
  774. /* Now start doing things to make the guards get filtered out, 1 by 1. */
  775. /* 0: Not listed. */
  776. g[0]->currently_listed = 0;
  777. /* 1: path bias says this guard is maybe eeeevil. */
  778. g[1]->pb.path_bias_disabled = 1;
  779. /* 2: Unreachable address. */
  780. n[2]->rs->addr = 0;
  781. /* 3: ExcludeNodes */
  782. n[3]->rs->addr = 0x90902020;
  783. routerset_free(get_options_mutable()->ExcludeNodes);
  784. get_options_mutable()->ExcludeNodes = routerset_new();
  785. routerset_parse(get_options_mutable()->ExcludeNodes, "144.144.0.0/16", "");
  786. /* 4: Bridge. */
  787. sweep_bridge_list();
  788. bl = tor_malloc_zero(sizeof(bridge_line_t));
  789. tor_addr_from_ipv4h(&bl->addr, n[4]->rs->addr);
  790. bl->port = n[4]->rs->or_port;
  791. memcpy(bl->digest, n[4]->identity, 20);
  792. bridge_add_from_config(bl);
  793. bl = NULL; // prevent free.
  794. /* 5: Unreachable. This stays in the filter, but isn't in usable-filtered */
  795. g[5]->last_tried_to_connect = approx_time(); // prevent retry.
  796. g[5]->is_reachable = GUARD_REACHABLE_NO;
  797. /* 6: no change. */
  798. /* Now refilter and inspect. */
  799. entry_guards_update_filtered_sets(gs);
  800. for (i = 0; i < NUM; ++i) {
  801. tt_assert(g[i]->is_filtered_guard == (i == 5 || i == 6));
  802. tt_assert(g[i]->is_usable_filtered_guard == (i == 6));
  803. }
  804. tt_int_op(num_reachable_filtered_guards(gs, NULL), OP_EQ, 1);
  805. /* Now make sure we have no live consensus, and no nodes. Nothing should
  806. * pass the filter any more. */
  807. tor_free(dummy_consensus);
  808. dummy_consensus = NULL;
  809. SMARTLIST_FOREACH(big_fake_net_nodes, node_t *, node, {
  810. memset(node->identity, 0xff, 20);
  811. });
  812. entry_guards_update_filtered_sets(gs);
  813. for (i = 0; i < NUM; ++i) {
  814. tt_assert(g[i]->is_filtered_guard == 0);
  815. tt_assert(g[i]->is_usable_filtered_guard == 0);
  816. }
  817. tt_int_op(num_reachable_filtered_guards(gs, NULL), OP_EQ, 0);
  818. done:
  819. guard_selection_free(gs);
  820. tor_free(bl);
  821. #undef NUM
  822. }
  823. static void
  824. test_entry_guard_expand_sample(void *arg)
  825. {
  826. (void)arg;
  827. guard_selection_t *gs = guard_selection_new("default", GS_TYPE_NORMAL);
  828. digestmap_t *node_by_id = digestmap_new();
  829. entry_guard_t *guard = entry_guards_expand_sample(gs);
  830. tt_assert(guard); // the last guard returned.
  831. // Every sampled guard here should be filtered and reachable for now.
  832. tt_int_op(smartlist_len(gs->sampled_entry_guards), OP_EQ,
  833. num_reachable_filtered_guards(gs, NULL));
  834. /* Make sure we got the right number. */
  835. tt_int_op(DFLT_MIN_FILTERED_SAMPLE_SIZE, OP_EQ,
  836. num_reachable_filtered_guards(gs, NULL));
  837. // Make sure everything we got was from our fake node list, and everything
  838. // was unique.
  839. SMARTLIST_FOREACH_BEGIN(gs->sampled_entry_guards, entry_guard_t *, g) {
  840. const node_t *n = bfn_mock_node_get_by_id(g->identity);
  841. tt_assert(n);
  842. tt_ptr_op(NULL, OP_EQ, digestmap_get(node_by_id, g->identity));
  843. digestmap_set(node_by_id, g->identity, (void*) n);
  844. int idx = smartlist_pos(big_fake_net_nodes, n);
  845. // The even ones are the guards; make sure we got guards.
  846. tt_int_op(idx & 1, OP_EQ, 0);
  847. } SMARTLIST_FOREACH_END(g);
  848. // Nothing became unusable/unfiltered, so a subsequent expand should
  849. // make no changes.
  850. guard = entry_guards_expand_sample(gs);
  851. tt_assert(! guard); // no guard was added.
  852. tt_int_op(DFLT_MIN_FILTERED_SAMPLE_SIZE, OP_EQ,
  853. num_reachable_filtered_guards(gs, NULL));
  854. // Make a few guards unreachable.
  855. guard = smartlist_get(gs->sampled_entry_guards, 0);
  856. guard->is_usable_filtered_guard = 0;
  857. guard = smartlist_get(gs->sampled_entry_guards, 1);
  858. guard->is_usable_filtered_guard = 0;
  859. guard = smartlist_get(gs->sampled_entry_guards, 2);
  860. guard->is_usable_filtered_guard = 0;
  861. tt_int_op(DFLT_MIN_FILTERED_SAMPLE_SIZE - 3, OP_EQ,
  862. num_reachable_filtered_guards(gs, NULL));
  863. // This time, expanding the sample will add some more guards.
  864. guard = entry_guards_expand_sample(gs);
  865. tt_assert(guard); // no guard was added.
  866. tt_int_op(DFLT_MIN_FILTERED_SAMPLE_SIZE, OP_EQ,
  867. num_reachable_filtered_guards(gs, NULL));
  868. tt_int_op(smartlist_len(gs->sampled_entry_guards), OP_EQ,
  869. num_reachable_filtered_guards(gs, NULL)+3);
  870. // Still idempotent.
  871. guard = entry_guards_expand_sample(gs);
  872. tt_assert(! guard); // no guard was added.
  873. tt_int_op(DFLT_MIN_FILTERED_SAMPLE_SIZE, OP_EQ,
  874. num_reachable_filtered_guards(gs, NULL));
  875. // Now, do a nasty trick: tell the filter to exclude 31/32 of the guards.
  876. // This will cause the sample size to get reeeeally huge, while the
  877. // filtered sample size grows only slowly.
  878. routerset_free(get_options_mutable()->ExcludeNodes);
  879. get_options_mutable()->ExcludeNodes = routerset_new();
  880. routerset_parse(get_options_mutable()->ExcludeNodes, "144.144.0.0/16", "");
  881. SMARTLIST_FOREACH(big_fake_net_nodes, node_t *, n, {
  882. if (n_sl_idx % 64 != 0) {
  883. n->rs->addr = 0x90903030;
  884. }
  885. });
  886. entry_guards_update_filtered_sets(gs);
  887. // Surely (p ~ 1-2**-60), one of our guards has been excluded.
  888. tt_int_op(num_reachable_filtered_guards(gs, NULL), OP_LT,
  889. DFLT_MIN_FILTERED_SAMPLE_SIZE);
  890. // Try to regenerate the guards.
  891. guard = entry_guards_expand_sample(gs);
  892. tt_assert(guard); // no guard was added.
  893. /* this time, it's possible that we didn't add enough sampled guards. */
  894. tt_int_op(num_reachable_filtered_guards(gs, NULL), OP_LE,
  895. DFLT_MIN_FILTERED_SAMPLE_SIZE);
  896. /* but we definitely didn't exceed the sample maximum. */
  897. const int n_guards = 271 / 2;
  898. tt_int_op(smartlist_len(gs->sampled_entry_guards), OP_LE,
  899. (int)(n_guards * .3));
  900. done:
  901. guard_selection_free(gs);
  902. digestmap_free(node_by_id, NULL);
  903. }
  904. static void
  905. test_entry_guard_expand_sample_small_net(void *arg)
  906. {
  907. (void)arg;
  908. guard_selection_t *gs = guard_selection_new("default", GS_TYPE_NORMAL);
  909. /* Fun corner case: not enough guards to make up our whole sample size. */
  910. SMARTLIST_FOREACH(big_fake_net_nodes, node_t *, n, {
  911. if (n_sl_idx >= 15) {
  912. tor_free(n->rs);
  913. tor_free(n->md);
  914. tor_free(n);
  915. SMARTLIST_DEL_CURRENT(big_fake_net_nodes, n);
  916. } else {
  917. n->rs->addr = 0; // make the filter reject this.
  918. }
  919. });
  920. entry_guard_t *guard = entry_guards_expand_sample(gs);
  921. tt_assert(guard); // the last guard returned -- some guard was added.
  922. // half the nodes are guards, so we have 8 guards left. The set
  923. // is small, so we sampled everything.
  924. tt_int_op(smartlist_len(gs->sampled_entry_guards), OP_EQ, 8);
  925. tt_int_op(num_reachable_filtered_guards(gs, NULL), OP_EQ, 0);
  926. done:
  927. guard_selection_free(gs);
  928. }
  929. static void
  930. test_entry_guard_update_from_consensus_status(void *arg)
  931. {
  932. /* Here we're going to have some nodes become un-guardy, and say we got a
  933. * new consensus. This should cause those nodes to get detected as
  934. * unreachable. */
  935. (void)arg;
  936. int i;
  937. time_t start = approx_time();
  938. guard_selection_t *gs = guard_selection_new("default", GS_TYPE_NORMAL);
  939. networkstatus_t *ns_tmp = NULL;
  940. /* Don't randomly backdate stuff; it will make correctness harder to check.*/
  941. MOCK(randomize_time, mock_randomize_time_no_randomization);
  942. /* First, sample some guards. */
  943. entry_guards_expand_sample(gs);
  944. int n_sampled_pre = smartlist_len(gs->sampled_entry_guards);
  945. int n_filtered_pre = num_reachable_filtered_guards(gs, NULL);
  946. tt_i64_op(n_sampled_pre, OP_EQ, n_filtered_pre);
  947. tt_i64_op(n_sampled_pre, OP_GT, 10);
  948. /* At this point, it should be a no-op to do this: */
  949. sampled_guards_update_from_consensus(gs);
  950. /* Now let's make some of our guards become unlisted. The easiest way to
  951. * do that would be to take away their guard flag. */
  952. for (i = 0; i < 5; ++i) {
  953. entry_guard_t *g = smartlist_get(gs->sampled_entry_guards, i);
  954. node_t *n = (node_t*) bfn_mock_node_get_by_id(g->identity);
  955. n->is_possible_guard = 0;
  956. }
  957. update_approx_time(start + 30);
  958. {
  959. /* try this with no live networkstatus. Nothing should happen! */
  960. ns_tmp = dummy_consensus;
  961. dummy_consensus = NULL;
  962. sampled_guards_update_from_consensus(gs);
  963. tt_i64_op(smartlist_len(gs->sampled_entry_guards), OP_EQ, n_sampled_pre);
  964. tt_i64_op(num_reachable_filtered_guards(gs, NULL), OP_EQ, n_filtered_pre);
  965. /* put the networkstatus back. */
  966. dummy_consensus = ns_tmp;
  967. ns_tmp = NULL;
  968. }
  969. /* Now those guards should become unlisted, and drop off the filter, but
  970. * stay in the sample. */
  971. update_approx_time(start + 60);
  972. sampled_guards_update_from_consensus(gs);
  973. tt_i64_op(smartlist_len(gs->sampled_entry_guards), OP_EQ, n_sampled_pre);
  974. tt_i64_op(num_reachable_filtered_guards(gs, NULL), OP_EQ, n_filtered_pre-5);
  975. for (i = 0; i < 5; ++i) {
  976. entry_guard_t *g = smartlist_get(gs->sampled_entry_guards, i);
  977. tt_assert(! g->currently_listed);
  978. tt_i64_op(g->unlisted_since_date, OP_EQ, start+60);
  979. }
  980. for (i = 5; i < n_sampled_pre; ++i) {
  981. entry_guard_t *g = smartlist_get(gs->sampled_entry_guards, i);
  982. tt_assert(g->currently_listed);
  983. tt_i64_op(g->unlisted_since_date, OP_EQ, 0);
  984. }
  985. /* Now re-list one, and remove one completely. */
  986. {
  987. entry_guard_t *g = smartlist_get(gs->sampled_entry_guards, 0);
  988. node_t *n = (node_t*) bfn_mock_node_get_by_id(g->identity);
  989. n->is_possible_guard = 1;
  990. }
  991. {
  992. /* try removing the node, to make sure we don't crash on an absent node
  993. */
  994. entry_guard_t *g = smartlist_get(gs->sampled_entry_guards, 5);
  995. node_t *n = (node_t*) bfn_mock_node_get_by_id(g->identity);
  996. smartlist_remove(big_fake_net_nodes, n);
  997. tor_free(n->rs);
  998. tor_free(n->md);
  999. tor_free(n);
  1000. }
  1001. update_approx_time(start + 300);
  1002. sampled_guards_update_from_consensus(gs);
  1003. /* guards 1..5 are now unlisted; 0,6,7.. are listed. */
  1004. tt_i64_op(smartlist_len(gs->sampled_entry_guards), OP_EQ, n_sampled_pre);
  1005. for (i = 1; i < 6; ++i) {
  1006. entry_guard_t *g = smartlist_get(gs->sampled_entry_guards, i);
  1007. tt_assert(! g->currently_listed);
  1008. if (i == 5)
  1009. tt_i64_op(g->unlisted_since_date, OP_EQ, start+300);
  1010. else
  1011. tt_i64_op(g->unlisted_since_date, OP_EQ, start+60);
  1012. }
  1013. for (i = 0; i < n_sampled_pre; i = (!i) ? 6 : i+1) { /* 0,6,7,8, ... */
  1014. entry_guard_t *g = smartlist_get(gs->sampled_entry_guards, i);
  1015. tt_assert(g->currently_listed);
  1016. tt_i64_op(g->unlisted_since_date, OP_EQ, 0);
  1017. }
  1018. done:
  1019. tor_free(ns_tmp); /* in case we couldn't put it back */
  1020. guard_selection_free(gs);
  1021. UNMOCK(randomize_time);
  1022. }
  1023. static void
  1024. test_entry_guard_update_from_consensus_repair(void *arg)
  1025. {
  1026. /* Here we'll make sure that our code to repair the unlisted-since
  1027. * times is correct. */
  1028. (void)arg;
  1029. int i;
  1030. time_t start = approx_time();
  1031. guard_selection_t *gs = guard_selection_new("default", GS_TYPE_NORMAL);
  1032. /* Don't randomly backdate stuff; it will make correctness harder to check.*/
  1033. MOCK(randomize_time, mock_randomize_time_no_randomization);
  1034. /* First, sample some guards. */
  1035. entry_guards_expand_sample(gs);
  1036. int n_sampled_pre = smartlist_len(gs->sampled_entry_guards);
  1037. int n_filtered_pre = num_reachable_filtered_guards(gs, NULL);
  1038. tt_i64_op(n_sampled_pre, OP_EQ, n_filtered_pre);
  1039. tt_i64_op(n_sampled_pre, OP_GT, 10);
  1040. /* Now corrupt the list a bit. Call some unlisted-since-never, and some
  1041. * listed-and-unlisted-since-a-time. */
  1042. update_approx_time(start + 300);
  1043. for (i = 0; i < 3; ++i) {
  1044. /* these will get a date. */
  1045. entry_guard_t *g = smartlist_get(gs->sampled_entry_guards, i);
  1046. node_t *n = (node_t*) bfn_mock_node_get_by_id(g->identity);
  1047. n->is_possible_guard = 0;
  1048. g->currently_listed = 0;
  1049. }
  1050. for (i = 3; i < 6; ++i) {
  1051. /* these will become listed. */
  1052. entry_guard_t *g = smartlist_get(gs->sampled_entry_guards, i);
  1053. g->unlisted_since_date = start+100;
  1054. }
  1055. setup_full_capture_of_logs(LOG_WARN);
  1056. sampled_guards_update_from_consensus(gs);
  1057. expect_log_msg_containing(
  1058. "was listed, but with unlisted_since_date set");
  1059. expect_log_msg_containing(
  1060. "was unlisted, but with unlisted_since_date unset");
  1061. teardown_capture_of_logs();
  1062. tt_int_op(smartlist_len(gs->sampled_entry_guards), OP_EQ, n_sampled_pre);
  1063. tt_int_op(num_reachable_filtered_guards(gs, NULL), OP_EQ, n_filtered_pre-3);
  1064. for (i = 3; i < n_sampled_pre; ++i) {
  1065. /* these will become listed. */
  1066. entry_guard_t *g = smartlist_get(gs->sampled_entry_guards, i);
  1067. if (i < 3) {
  1068. tt_assert(! g->currently_listed);
  1069. tt_i64_op(g->unlisted_since_date, OP_EQ, start+300);
  1070. } else {
  1071. tt_assert(g->currently_listed);
  1072. tt_i64_op(g->unlisted_since_date, OP_EQ, 0);
  1073. }
  1074. }
  1075. done:
  1076. teardown_capture_of_logs();
  1077. guard_selection_free(gs);
  1078. UNMOCK(randomize_time);
  1079. }
  1080. static void
  1081. test_entry_guard_update_from_consensus_remove(void *arg)
  1082. {
  1083. /* Now let's check the logic responsible for removing guards from the
  1084. * sample entirely. */
  1085. (void)arg;
  1086. //int i;
  1087. guard_selection_t *gs = guard_selection_new("default", GS_TYPE_NORMAL);
  1088. smartlist_t *keep_ids = smartlist_new();
  1089. smartlist_t *remove_ids = smartlist_new();
  1090. /* Don't randomly backdate stuff; it will make correctness harder to check.*/
  1091. MOCK(randomize_time, mock_randomize_time_no_randomization);
  1092. /* First, sample some guards. */
  1093. entry_guards_expand_sample(gs);
  1094. int n_sampled_pre = smartlist_len(gs->sampled_entry_guards);
  1095. int n_filtered_pre = num_reachable_filtered_guards(gs, NULL);
  1096. tt_i64_op(n_sampled_pre, OP_EQ, n_filtered_pre);
  1097. tt_i64_op(n_sampled_pre, OP_GT, 10);
  1098. const time_t one_day_ago = approx_time() - 1*24*60*60;
  1099. const time_t one_year_ago = approx_time() - 365*24*60*60;
  1100. const time_t two_years_ago = approx_time() - 2*365*24*60*60;
  1101. /* 0: unlisted for a day. (keep this) */
  1102. {
  1103. entry_guard_t *g = smartlist_get(gs->sampled_entry_guards, 0);
  1104. node_t *n = (node_t*) bfn_mock_node_get_by_id(g->identity);
  1105. n->is_possible_guard = 0;
  1106. g->currently_listed = 0;
  1107. g->unlisted_since_date = one_day_ago;
  1108. smartlist_add(keep_ids, tor_memdup(g->identity, 20));
  1109. }
  1110. /* 1: unlisted for a year. (remove this) */
  1111. {
  1112. entry_guard_t *g = smartlist_get(gs->sampled_entry_guards, 1);
  1113. node_t *n = (node_t*) bfn_mock_node_get_by_id(g->identity);
  1114. n->is_possible_guard = 0;
  1115. g->currently_listed = 0;
  1116. g->unlisted_since_date = one_year_ago;
  1117. smartlist_add(remove_ids, tor_memdup(g->identity, 20));
  1118. }
  1119. /* 2: added a day ago, never confirmed. (keep this) */
  1120. {
  1121. entry_guard_t *g = smartlist_get(gs->sampled_entry_guards, 2);
  1122. g->sampled_on_date = one_day_ago;
  1123. smartlist_add(keep_ids, tor_memdup(g->identity, 20));
  1124. }
  1125. /* 3: added a year ago, never confirmed. (remove this) */
  1126. {
  1127. entry_guard_t *g = smartlist_get(gs->sampled_entry_guards, 3);
  1128. g->sampled_on_date = one_year_ago;
  1129. smartlist_add(remove_ids, tor_memdup(g->identity, 20));
  1130. }
  1131. /* 4: added two year ago, confirmed yesterday, primary. (keep this.) */
  1132. {
  1133. entry_guard_t *g = smartlist_get(gs->sampled_entry_guards, 4);
  1134. g->sampled_on_date = one_year_ago;
  1135. g->confirmed_on_date = one_day_ago;
  1136. g->confirmed_idx = 0;
  1137. g->is_primary = 1;
  1138. smartlist_add(gs->confirmed_entry_guards, g);
  1139. smartlist_add(gs->primary_entry_guards, g);
  1140. smartlist_add(keep_ids, tor_memdup(g->identity, 20));
  1141. }
  1142. /* 5: added two years ago, confirmed a year ago, primary. (remove this) */
  1143. {
  1144. entry_guard_t *g = smartlist_get(gs->sampled_entry_guards, 5);
  1145. g->sampled_on_date = two_years_ago;
  1146. g->confirmed_on_date = one_year_ago;
  1147. g->confirmed_idx = 1;
  1148. g->is_primary = 1;
  1149. smartlist_add(gs->confirmed_entry_guards, g);
  1150. smartlist_add(gs->primary_entry_guards, g);
  1151. smartlist_add(remove_ids, tor_memdup(g->identity, 20));
  1152. }
  1153. sampled_guards_update_from_consensus(gs);
  1154. /* Did we remove the right ones? */
  1155. SMARTLIST_FOREACH(keep_ids, uint8_t *, id, {
  1156. tt_assert(get_sampled_guard_with_id(gs, id) != NULL);
  1157. });
  1158. SMARTLIST_FOREACH(remove_ids, uint8_t *, id, {
  1159. tt_want(get_sampled_guard_with_id(gs, id) == NULL);
  1160. });
  1161. /* Did we remove the right number? */
  1162. tt_int_op(smartlist_len(gs->sampled_entry_guards), OP_EQ, n_sampled_pre - 3);
  1163. done:
  1164. guard_selection_free(gs);
  1165. UNMOCK(randomize_time);
  1166. SMARTLIST_FOREACH(keep_ids, char *, cp, tor_free(cp));
  1167. SMARTLIST_FOREACH(remove_ids, char *, cp, tor_free(cp));
  1168. smartlist_free(keep_ids);
  1169. smartlist_free(remove_ids);
  1170. }
  1171. static void
  1172. test_entry_guard_confirming_guards(void *arg)
  1173. {
  1174. (void)arg;
  1175. /* Now let's check the logic responsible for manipulating the list
  1176. * of confirmed guards */
  1177. guard_selection_t *gs = guard_selection_new("default", GS_TYPE_NORMAL);
  1178. MOCK(randomize_time, mock_randomize_time_no_randomization);
  1179. /* Create the sample. */
  1180. entry_guards_expand_sample(gs);
  1181. /* Confirm a few guards. */
  1182. time_t start = approx_time();
  1183. entry_guard_t *g1 = smartlist_get(gs->sampled_entry_guards, 0);
  1184. entry_guard_t *g2 = smartlist_get(gs->sampled_entry_guards, 1);
  1185. entry_guard_t *g3 = smartlist_get(gs->sampled_entry_guards, 8);
  1186. make_guard_confirmed(gs, g2);
  1187. update_approx_time(start + 10);
  1188. make_guard_confirmed(gs, g1);
  1189. make_guard_confirmed(gs, g3);
  1190. /* Were the correct dates and indices fed in? */
  1191. tt_int_op(g1->confirmed_idx, OP_EQ, 1);
  1192. tt_int_op(g2->confirmed_idx, OP_EQ, 0);
  1193. tt_int_op(g3->confirmed_idx, OP_EQ, 2);
  1194. tt_i64_op(g1->confirmed_on_date, OP_EQ, start+10);
  1195. tt_i64_op(g2->confirmed_on_date, OP_EQ, start);
  1196. tt_i64_op(g3->confirmed_on_date, OP_EQ, start+10);
  1197. tt_ptr_op(smartlist_get(gs->confirmed_entry_guards, 0), OP_EQ, g2);
  1198. tt_ptr_op(smartlist_get(gs->confirmed_entry_guards, 1), OP_EQ, g1);
  1199. tt_ptr_op(smartlist_get(gs->confirmed_entry_guards, 2), OP_EQ, g3);
  1200. /* Now make sure we can regenerate the confirmed_entry_guards list. */
  1201. smartlist_clear(gs->confirmed_entry_guards);
  1202. g2->confirmed_idx = 0;
  1203. g1->confirmed_idx = 10;
  1204. g3->confirmed_idx = 100;
  1205. entry_guards_update_confirmed(gs);
  1206. tt_int_op(g1->confirmed_idx, OP_EQ, 1);
  1207. tt_int_op(g2->confirmed_idx, OP_EQ, 0);
  1208. tt_int_op(g3->confirmed_idx, OP_EQ, 2);
  1209. tt_ptr_op(smartlist_get(gs->confirmed_entry_guards, 0), OP_EQ, g2);
  1210. tt_ptr_op(smartlist_get(gs->confirmed_entry_guards, 1), OP_EQ, g1);
  1211. tt_ptr_op(smartlist_get(gs->confirmed_entry_guards, 2), OP_EQ, g3);
  1212. /* Now make sure we can regenerate the confirmed_entry_guards list if
  1213. * the indices are messed up. */
  1214. g1->confirmed_idx = g2->confirmed_idx = g3->confirmed_idx = 999;
  1215. smartlist_clear(gs->confirmed_entry_guards);
  1216. entry_guards_update_confirmed(gs);
  1217. tt_int_op(g1->confirmed_idx, OP_GE, 0);
  1218. tt_int_op(g2->confirmed_idx, OP_GE, 0);
  1219. tt_int_op(g3->confirmed_idx, OP_GE, 0);
  1220. tt_int_op(g1->confirmed_idx, OP_LE, 2);
  1221. tt_int_op(g2->confirmed_idx, OP_LE, 2);
  1222. tt_int_op(g3->confirmed_idx, OP_LE, 2);
  1223. g1 = smartlist_get(gs->confirmed_entry_guards, 0);
  1224. g2 = smartlist_get(gs->confirmed_entry_guards, 1);
  1225. g3 = smartlist_get(gs->confirmed_entry_guards, 2);
  1226. tt_int_op(g1->confirmed_idx, OP_EQ, 0);
  1227. tt_int_op(g2->confirmed_idx, OP_EQ, 1);
  1228. tt_int_op(g3->confirmed_idx, OP_EQ, 2);
  1229. tt_assert(g1 != g2);
  1230. tt_assert(g1 != g3);
  1231. tt_assert(g2 != g3);
  1232. done:
  1233. UNMOCK(randomize_time);
  1234. guard_selection_free(gs);
  1235. }
  1236. static void
  1237. test_entry_guard_sample_reachable_filtered(void *arg)
  1238. {
  1239. (void)arg;
  1240. guard_selection_t *gs = guard_selection_new("default", GS_TYPE_NORMAL);
  1241. entry_guards_expand_sample(gs);
  1242. const int N = 10000;
  1243. bitarray_t *selected = NULL;
  1244. int i, j;
  1245. /* We've got a sampled list now; let's make one non-usable-filtered; some
  1246. * confirmed, some primary, some pending.
  1247. */
  1248. int n_guards = smartlist_len(gs->sampled_entry_guards);
  1249. tt_int_op(n_guards, OP_GT, 10);
  1250. entry_guard_t *g;
  1251. g = smartlist_get(gs->sampled_entry_guards, 0);
  1252. g->is_pending = 1;
  1253. g = smartlist_get(gs->sampled_entry_guards, 1);
  1254. make_guard_confirmed(gs, g);
  1255. g = smartlist_get(gs->sampled_entry_guards, 2);
  1256. g->is_primary = 1;
  1257. g = smartlist_get(gs->sampled_entry_guards, 3);
  1258. g->pb.path_bias_disabled = 1;
  1259. entry_guards_update_filtered_sets(gs);
  1260. gs->primary_guards_up_to_date = 1;
  1261. tt_int_op(num_reachable_filtered_guards(gs, NULL), OP_EQ, n_guards - 1);
  1262. tt_int_op(smartlist_len(gs->sampled_entry_guards), OP_EQ, n_guards);
  1263. // +1 since the one we made disabled will make another one get added.
  1264. ++n_guards;
  1265. /* Try a bunch of selections. */
  1266. const struct {
  1267. int flag; int idx;
  1268. } tests[] = {
  1269. { 0, -1 },
  1270. { SAMPLE_EXCLUDE_CONFIRMED, 1 },
  1271. { SAMPLE_EXCLUDE_PRIMARY|SAMPLE_NO_UPDATE_PRIMARY, 2 },
  1272. { SAMPLE_EXCLUDE_PENDING, 0 },
  1273. { -1, -1},
  1274. };
  1275. for (j = 0; tests[j].flag >= 0; ++j) {
  1276. selected = bitarray_init_zero(n_guards);
  1277. const int excluded_flags = tests[j].flag;
  1278. const int excluded_idx = tests[j].idx;
  1279. for (i = 0; i < N; ++i) {
  1280. g = sample_reachable_filtered_entry_guards(gs, NULL, excluded_flags);
  1281. tor_assert(g);
  1282. int pos = smartlist_pos(gs->sampled_entry_guards, g);
  1283. tt_int_op(smartlist_len(gs->sampled_entry_guards), OP_EQ, n_guards);
  1284. tt_int_op(pos, OP_GE, 0);
  1285. tt_int_op(pos, OP_LT, n_guards);
  1286. bitarray_set(selected, pos);
  1287. }
  1288. for (i = 0; i < n_guards; ++i) {
  1289. const int should_be_set = (i != excluded_idx &&
  1290. i != 3); // filtered out.
  1291. tt_int_op(!!bitarray_is_set(selected, i), OP_EQ, should_be_set);
  1292. }
  1293. bitarray_free(selected);
  1294. selected = NULL;
  1295. }
  1296. done:
  1297. guard_selection_free(gs);
  1298. bitarray_free(selected);
  1299. }
  1300. static void
  1301. test_entry_guard_sample_reachable_filtered_empty(void *arg)
  1302. {
  1303. (void)arg;
  1304. guard_selection_t *gs = guard_selection_new("default", GS_TYPE_NORMAL);
  1305. /* What if we try to sample from a set of 0? */
  1306. SMARTLIST_FOREACH(big_fake_net_nodes, node_t *, n,
  1307. n->is_possible_guard = 0);
  1308. entry_guard_t *g = sample_reachable_filtered_entry_guards(gs, NULL, 0);
  1309. tt_ptr_op(g, OP_EQ, NULL);
  1310. done:
  1311. guard_selection_free(gs);
  1312. }
  1313. static void
  1314. test_entry_guard_retry_unreachable(void *arg)
  1315. {
  1316. (void)arg;
  1317. guard_selection_t *gs = guard_selection_new("default", GS_TYPE_NORMAL);
  1318. entry_guards_expand_sample(gs);
  1319. /* Let's say that we have two guards, and they're down.
  1320. */
  1321. time_t start = approx_time();;
  1322. entry_guard_t *g1 = smartlist_get(gs->sampled_entry_guards, 0);
  1323. entry_guard_t *g2 = smartlist_get(gs->sampled_entry_guards, 1);
  1324. entry_guard_t *g3 = smartlist_get(gs->sampled_entry_guards, 2);
  1325. g1->is_reachable = GUARD_REACHABLE_NO;
  1326. g2->is_reachable = GUARD_REACHABLE_NO;
  1327. g1->is_primary = 1;
  1328. g1->failing_since = g2->failing_since = start;
  1329. g1->last_tried_to_connect = g2->last_tried_to_connect = start;
  1330. /* Wait 5 minutes. Nothing will get retried. */
  1331. update_approx_time(start + 5 * 60);
  1332. entry_guard_consider_retry(g1);
  1333. entry_guard_consider_retry(g2);
  1334. entry_guard_consider_retry(g3); // just to make sure this doesn't crash.
  1335. tt_int_op(g1->is_reachable, OP_EQ, GUARD_REACHABLE_NO);
  1336. tt_int_op(g2->is_reachable, OP_EQ, GUARD_REACHABLE_NO);
  1337. tt_int_op(g3->is_reachable, OP_EQ, GUARD_REACHABLE_MAYBE);
  1338. /* After 30 min, the primary one gets retried */
  1339. update_approx_time(start + 35 * 60);
  1340. entry_guard_consider_retry(g1);
  1341. entry_guard_consider_retry(g2);
  1342. tt_int_op(g1->is_reachable, OP_EQ, GUARD_REACHABLE_MAYBE);
  1343. tt_int_op(g2->is_reachable, OP_EQ, GUARD_REACHABLE_NO);
  1344. g1->is_reachable = GUARD_REACHABLE_NO;
  1345. g1->last_tried_to_connect = start + 55*60;
  1346. /* After 1 hour, we'll retry the nonprimary one. */
  1347. update_approx_time(start + 61 * 60);
  1348. entry_guard_consider_retry(g1);
  1349. entry_guard_consider_retry(g2);
  1350. tt_int_op(g1->is_reachable, OP_EQ, GUARD_REACHABLE_NO);
  1351. tt_int_op(g2->is_reachable, OP_EQ, GUARD_REACHABLE_MAYBE);
  1352. g2->is_reachable = GUARD_REACHABLE_NO;
  1353. g2->last_tried_to_connect = start + 61*60;
  1354. /* And then the primary one again. */
  1355. update_approx_time(start + 66 * 60);
  1356. entry_guard_consider_retry(g1);
  1357. entry_guard_consider_retry(g2);
  1358. tt_int_op(g1->is_reachable, OP_EQ, GUARD_REACHABLE_MAYBE);
  1359. tt_int_op(g2->is_reachable, OP_EQ, GUARD_REACHABLE_NO);
  1360. done:
  1361. guard_selection_free(gs);
  1362. }
  1363. static void
  1364. test_entry_guard_manage_primary(void *arg)
  1365. {
  1366. (void)arg;
  1367. guard_selection_t *gs = guard_selection_new("default", GS_TYPE_NORMAL);
  1368. smartlist_t *prev_guards = smartlist_new();
  1369. /* If no guards are confirmed, we should pick a few reachable guards and
  1370. * call them all primary. But not confirmed.*/
  1371. entry_guards_update_primary(gs);
  1372. int n_primary = smartlist_len(gs->primary_entry_guards);
  1373. tt_int_op(n_primary, OP_GE, 1);
  1374. SMARTLIST_FOREACH(gs->primary_entry_guards, entry_guard_t *, g, {
  1375. tt_assert(g->is_primary);
  1376. tt_assert(g->confirmed_idx == -1);
  1377. });
  1378. /* Calling it a second time should leave the guards unchanged. */
  1379. smartlist_add_all(prev_guards, gs->primary_entry_guards);
  1380. entry_guards_update_primary(gs);
  1381. tt_int_op(smartlist_len(gs->primary_entry_guards), OP_EQ, n_primary);
  1382. SMARTLIST_FOREACH(gs->primary_entry_guards, entry_guard_t *, g, {
  1383. tt_ptr_op(g, OP_EQ, smartlist_get(prev_guards, g_sl_idx));
  1384. });
  1385. /* If we have one confirmed guard, that guards becomes the first primary
  1386. * guard, and the other primary guards get kept. */
  1387. /* find a non-primary guard... */
  1388. entry_guard_t *confirmed = NULL;
  1389. SMARTLIST_FOREACH(gs->sampled_entry_guards, entry_guard_t *, g, {
  1390. if (! g->is_primary) {
  1391. confirmed = g;
  1392. break;
  1393. }
  1394. });
  1395. tt_assert(confirmed);
  1396. /* make it confirmed. */
  1397. make_guard_confirmed(gs, confirmed);
  1398. /* update the list... */
  1399. smartlist_clear(prev_guards);
  1400. smartlist_add_all(prev_guards, gs->primary_entry_guards);
  1401. entry_guards_update_primary(gs);
  1402. /* and see what's primary now! */
  1403. tt_int_op(smartlist_len(gs->primary_entry_guards), OP_EQ, n_primary);
  1404. tt_ptr_op(smartlist_get(gs->primary_entry_guards, 0), OP_EQ, confirmed);
  1405. SMARTLIST_FOREACH(gs->primary_entry_guards, entry_guard_t *, g, {
  1406. tt_assert(g->is_primary);
  1407. if (g_sl_idx == 0)
  1408. continue;
  1409. tt_ptr_op(g, OP_EQ, smartlist_get(prev_guards, g_sl_idx - 1));
  1410. });
  1411. {
  1412. entry_guard_t *prev_last_guard = smartlist_get(prev_guards, n_primary-1);
  1413. tt_assert(! prev_last_guard->is_primary);
  1414. }
  1415. /* Calling it a fourth time should leave the guards unchanged. */
  1416. smartlist_clear(prev_guards);
  1417. smartlist_add_all(prev_guards, gs->primary_entry_guards);
  1418. entry_guards_update_primary(gs);
  1419. tt_int_op(smartlist_len(gs->primary_entry_guards), OP_EQ, n_primary);
  1420. SMARTLIST_FOREACH(gs->primary_entry_guards, entry_guard_t *, g, {
  1421. tt_ptr_op(g, OP_EQ, smartlist_get(prev_guards, g_sl_idx));
  1422. });
  1423. done:
  1424. guard_selection_free(gs);
  1425. smartlist_free(prev_guards);
  1426. }
  1427. static void
  1428. test_entry_guard_guard_preferred(void *arg)
  1429. {
  1430. (void) arg;
  1431. entry_guard_t *g1 = tor_malloc_zero(sizeof(entry_guard_t));
  1432. entry_guard_t *g2 = tor_malloc_zero(sizeof(entry_guard_t));
  1433. g1->confirmed_idx = g2->confirmed_idx = -1;
  1434. g1->last_tried_to_connect = approx_time();
  1435. g2->last_tried_to_connect = approx_time();
  1436. tt_int_op(0, OP_EQ, entry_guard_has_higher_priority(g1, g1));
  1437. /* Neither is pending; priorities equal. */
  1438. tt_int_op(0, OP_EQ, entry_guard_has_higher_priority(g2, g1));
  1439. tt_int_op(0, OP_EQ, entry_guard_has_higher_priority(g1, g2));
  1440. /* If one is pending, the pending one has higher priority */
  1441. g1->is_pending = 1;
  1442. tt_int_op(1, OP_EQ, entry_guard_has_higher_priority(g1, g2));
  1443. tt_int_op(0, OP_EQ, entry_guard_has_higher_priority(g2, g1));
  1444. /* If both are pending, and last_tried_to_connect is equal:
  1445. priorities equal */
  1446. g2->is_pending = 1;
  1447. tt_int_op(0, OP_EQ, entry_guard_has_higher_priority(g2, g1));
  1448. tt_int_op(0, OP_EQ, entry_guard_has_higher_priority(g1, g2));
  1449. /* One had a connection that startied earlier: it has higher priority. */
  1450. g2->last_tried_to_connect -= 10;
  1451. tt_int_op(1, OP_EQ, entry_guard_has_higher_priority(g2, g1));
  1452. tt_int_op(0, OP_EQ, entry_guard_has_higher_priority(g1, g2));
  1453. /* Now, say that g1 is confirmed. It will get higher priority. */
  1454. g1->confirmed_idx = 5;
  1455. tt_int_op(0, OP_EQ, entry_guard_has_higher_priority(g2, g1));
  1456. tt_int_op(1, OP_EQ, entry_guard_has_higher_priority(g1, g2));
  1457. /* But if g2 was confirmed first, it will get priority */
  1458. g2->confirmed_idx = 2;
  1459. tt_int_op(1, OP_EQ, entry_guard_has_higher_priority(g2, g1));
  1460. tt_int_op(0, OP_EQ, entry_guard_has_higher_priority(g1, g2));
  1461. done:
  1462. tor_free(g1);
  1463. tor_free(g2);
  1464. }
  1465. static void
  1466. test_entry_guard_select_for_circuit_no_confirmed(void *arg)
  1467. {
  1468. /* Simpler cases: no gaurds are confirmed yet. */
  1469. (void)arg;
  1470. guard_selection_t *gs = guard_selection_new("default", GS_TYPE_NORMAL);
  1471. /* simple starting configuration */
  1472. entry_guards_update_primary(gs);
  1473. unsigned state = 9999;
  1474. entry_guard_t *g = select_entry_guard_for_circuit(gs, GUARD_USAGE_TRAFFIC,
  1475. NULL, &state);
  1476. tt_assert(g);
  1477. tt_assert(g->is_primary);
  1478. tt_int_op(g->confirmed_idx, OP_EQ, -1);
  1479. tt_assert(g->is_pending == 0); // primary implies non-pending.
  1480. tt_uint_op(state, OP_EQ, GUARD_CIRC_STATE_USABLE_ON_COMPLETION);
  1481. tt_i64_op(g->last_tried_to_connect, OP_EQ, approx_time());
  1482. // If we do that again, we should get the same guard.
  1483. entry_guard_t *g2 = select_entry_guard_for_circuit(gs, GUARD_USAGE_TRAFFIC,
  1484. NULL, &state);
  1485. tt_ptr_op(g2, OP_EQ, g);
  1486. // if we mark that guard down, we should get a different primary guard.
  1487. // auto-retry it.
  1488. g->is_reachable = GUARD_REACHABLE_NO;
  1489. g->failing_since = approx_time() - 10;
  1490. g->last_tried_to_connect = approx_time() - 10;
  1491. state = 9999;
  1492. g2 = select_entry_guard_for_circuit(gs, GUARD_USAGE_TRAFFIC, NULL, &state);
  1493. tt_ptr_op(g2, OP_NE, g);
  1494. tt_assert(g2);
  1495. tt_assert(g2->is_primary);
  1496. tt_int_op(g2->confirmed_idx, OP_EQ, -1);
  1497. tt_assert(g2->is_pending == 0); // primary implies non-pending.
  1498. tt_uint_op(state, OP_EQ, GUARD_CIRC_STATE_USABLE_ON_COMPLETION);
  1499. tt_i64_op(g2->last_tried_to_connect, OP_EQ, approx_time());
  1500. // If we say that the first primary guard was last tried a long time ago, we
  1501. // should get an automatic retry on it.
  1502. g->failing_since = approx_time() - 72*60*60;
  1503. g->last_tried_to_connect = approx_time() - 72*60*60;
  1504. state = 9999;
  1505. g2 = select_entry_guard_for_circuit(gs, GUARD_USAGE_TRAFFIC, NULL, &state);
  1506. tt_ptr_op(g2, OP_EQ, g);
  1507. tt_assert(g2);
  1508. tt_uint_op(state, OP_EQ, GUARD_CIRC_STATE_USABLE_ON_COMPLETION);
  1509. tt_i64_op(g2->last_tried_to_connect, OP_EQ, approx_time());
  1510. tt_int_op(g2->is_reachable, OP_EQ, GUARD_REACHABLE_MAYBE);
  1511. // And if we mark ALL the primary guards down, we should get another guard
  1512. // at random.
  1513. SMARTLIST_FOREACH(gs->primary_entry_guards, entry_guard_t *, guard, {
  1514. guard->is_reachable = GUARD_REACHABLE_NO;
  1515. guard->last_tried_to_connect = approx_time() - 5;
  1516. guard->failing_since = approx_time() - 30;
  1517. });
  1518. state = 9999;
  1519. g2 = select_entry_guard_for_circuit(gs, GUARD_USAGE_TRAFFIC, NULL, &state);
  1520. tt_assert(g2);
  1521. tt_assert(!g2->is_primary);
  1522. tt_int_op(g2->confirmed_idx, OP_EQ, -1);
  1523. tt_assert(g2->is_pending == 1);
  1524. tt_uint_op(state, OP_EQ, GUARD_CIRC_STATE_USABLE_IF_NO_BETTER_GUARD);
  1525. tt_i64_op(g2->last_tried_to_connect, OP_EQ, approx_time());
  1526. tt_int_op(g2->is_reachable, OP_EQ, GUARD_REACHABLE_MAYBE);
  1527. // As a bonus, maybe we should be retrying the primary guards. Let's say so.
  1528. mark_primary_guards_maybe_reachable(gs);
  1529. SMARTLIST_FOREACH(gs->primary_entry_guards, entry_guard_t *, guard, {
  1530. tt_int_op(guard->is_reachable, OP_EQ, GUARD_REACHABLE_MAYBE);
  1531. tt_assert(guard->is_usable_filtered_guard == 1);
  1532. // no change to these fields.
  1533. tt_i64_op(guard->last_tried_to_connect, OP_EQ, approx_time() - 5);
  1534. tt_i64_op(guard->failing_since, OP_EQ, approx_time() - 30);
  1535. });
  1536. /* Let's try again and we should get the first primary guard again */
  1537. g = select_entry_guard_for_circuit(gs, GUARD_USAGE_TRAFFIC, NULL, &state);
  1538. tt_ptr_op(g, OP_EQ, smartlist_get(gs->primary_entry_guards, 0));
  1539. g2 = select_entry_guard_for_circuit(gs, GUARD_USAGE_TRAFFIC, NULL, &state);
  1540. tt_ptr_op(g2, OP_EQ, g);
  1541. /* But if we impose a restriction, we don't get the same guard */
  1542. entry_guard_restriction_t rst;
  1543. memset(&rst, 0, sizeof(rst));
  1544. memcpy(rst.exclude_id, g->identity, DIGEST_LEN);
  1545. g2 = select_entry_guard_for_circuit(gs, GUARD_USAGE_TRAFFIC, &rst, &state);
  1546. tt_ptr_op(g2, OP_NE, g);
  1547. done:
  1548. guard_selection_free(gs);
  1549. }
  1550. static void
  1551. test_entry_guard_select_for_circuit_confirmed(void *arg)
  1552. {
  1553. /* Case 2: if all the primary guards are down, and there are more confirmed
  1554. guards, we use a confirmed guard. */
  1555. (void)arg;
  1556. int i;
  1557. guard_selection_t *gs = guard_selection_new("default", GS_TYPE_NORMAL);
  1558. const int N_CONFIRMED = 10;
  1559. /* slightly more complicated simple starting configuration */
  1560. entry_guards_update_primary(gs);
  1561. for (i = 0; i < N_CONFIRMED; ++i) {
  1562. entry_guard_t *guard = smartlist_get(gs->sampled_entry_guards, i);
  1563. make_guard_confirmed(gs, guard);
  1564. }
  1565. entry_guards_update_primary(gs); // rebuild the primary list.
  1566. unsigned state = 9999;
  1567. // As above, this gives us a primary guard.
  1568. entry_guard_t *g = select_entry_guard_for_circuit(gs, GUARD_USAGE_TRAFFIC,
  1569. NULL, &state);
  1570. tt_assert(g);
  1571. tt_assert(g->is_primary);
  1572. tt_int_op(g->confirmed_idx, OP_EQ, 0);
  1573. tt_assert(g->is_pending == 0); // primary implies non-pending.
  1574. tt_uint_op(state, OP_EQ, GUARD_CIRC_STATE_USABLE_ON_COMPLETION);
  1575. tt_i64_op(g->last_tried_to_connect, OP_EQ, approx_time());
  1576. tt_ptr_op(g, OP_EQ, smartlist_get(gs->primary_entry_guards, 0));
  1577. // But if we mark all the primary guards down...
  1578. SMARTLIST_FOREACH(gs->primary_entry_guards, entry_guard_t *, guard, {
  1579. guard->last_tried_to_connect = approx_time();
  1580. entry_guards_note_guard_failure(gs, guard);
  1581. });
  1582. // ... we should get a confirmed guard.
  1583. state = 9999;
  1584. g = select_entry_guard_for_circuit(gs, GUARD_USAGE_TRAFFIC, NULL, &state);
  1585. tt_assert(g);
  1586. tt_assert(! g->is_primary);
  1587. tt_int_op(g->confirmed_idx, OP_EQ, smartlist_len(gs->primary_entry_guards));
  1588. tt_assert(g->is_pending);
  1589. tt_uint_op(state, OP_EQ, GUARD_CIRC_STATE_USABLE_IF_NO_BETTER_GUARD);
  1590. tt_i64_op(g->last_tried_to_connect, OP_EQ, approx_time());
  1591. // And if we try again, we should get a different confirmed guard, since
  1592. // that one is pending.
  1593. state = 9999;
  1594. entry_guard_t *g2 = select_entry_guard_for_circuit(gs, GUARD_USAGE_TRAFFIC,
  1595. NULL, &state);
  1596. tt_assert(g2);
  1597. tt_assert(! g2->is_primary);
  1598. tt_ptr_op(g2, OP_NE, g);
  1599. tt_int_op(g2->confirmed_idx, OP_EQ,
  1600. smartlist_len(gs->primary_entry_guards)+1);
  1601. tt_assert(g2->is_pending);
  1602. tt_uint_op(state, OP_EQ, GUARD_CIRC_STATE_USABLE_IF_NO_BETTER_GUARD);
  1603. tt_i64_op(g2->last_tried_to_connect, OP_EQ, approx_time());
  1604. // If we say that the next confirmed guard in order is excluded, we get
  1605. // The one AFTER that.
  1606. g = smartlist_get(gs->confirmed_entry_guards,
  1607. smartlist_len(gs->primary_entry_guards)+2);
  1608. entry_guard_restriction_t rst;
  1609. memset(&rst, 0, sizeof(rst));
  1610. memcpy(rst.exclude_id, g->identity, DIGEST_LEN);
  1611. g2 = select_entry_guard_for_circuit(gs, GUARD_USAGE_TRAFFIC, &rst, &state);
  1612. tt_ptr_op(g2, OP_NE, g);
  1613. tt_int_op(g2->confirmed_idx, OP_EQ,
  1614. smartlist_len(gs->primary_entry_guards)+3);
  1615. // If we make every confirmed guard become pending then we start poking
  1616. // other guards.
  1617. const int n_remaining_confirmed =
  1618. N_CONFIRMED - 3 - smartlist_len(gs->primary_entry_guards);
  1619. for (i = 0; i < n_remaining_confirmed; ++i) {
  1620. g = select_entry_guard_for_circuit(gs, GUARD_USAGE_TRAFFIC, NULL, &state);
  1621. tt_int_op(g->confirmed_idx, OP_GE, 0);
  1622. tt_assert(g);
  1623. }
  1624. state = 9999;
  1625. g = select_entry_guard_for_circuit(gs, GUARD_USAGE_TRAFFIC, NULL, &state);
  1626. tt_assert(g);
  1627. tt_assert(g->is_pending);
  1628. tt_int_op(g->confirmed_idx, OP_EQ, -1);
  1629. done:
  1630. guard_selection_free(gs);
  1631. }
  1632. static void
  1633. test_entry_guard_select_for_circuit_highlevel_primary(void *arg)
  1634. {
  1635. /* Play around with selecting primary guards for circuits and markign
  1636. * them up and down */
  1637. (void)arg;
  1638. guard_selection_t *gs = guard_selection_new("default", GS_TYPE_NORMAL);
  1639. time_t start = approx_time();
  1640. const node_t *node = NULL;
  1641. circuit_guard_state_t *guard = NULL;
  1642. entry_guard_t *g;
  1643. guard_usable_t u;
  1644. /*
  1645. * Make sure that the pick-for-circuit API basically works. We'll get
  1646. * a primary guard, so it'll be usable on completion.
  1647. */
  1648. int r = entry_guard_pick_for_circuit(gs, GUARD_USAGE_TRAFFIC, NULL,
  1649. &node, &guard);
  1650. tt_assert(r == 0);
  1651. tt_assert(node);
  1652. tt_assert(guard);
  1653. tt_int_op(guard->state, OP_EQ, GUARD_CIRC_STATE_USABLE_ON_COMPLETION);
  1654. g = entry_guard_handle_get(guard->guard);
  1655. tt_assert(g);
  1656. tt_mem_op(g->identity, OP_EQ, node->identity, DIGEST_LEN);
  1657. tt_int_op(g->is_primary, OP_EQ, 1);
  1658. tt_i64_op(g->last_tried_to_connect, OP_EQ, start);
  1659. tt_int_op(g->confirmed_idx, OP_EQ, -1);
  1660. /* Call that circuit successful. */
  1661. update_approx_time(start+15);
  1662. u = entry_guard_succeeded(&guard);
  1663. tt_int_op(u, OP_EQ, GUARD_USABLE_NOW); /* We can use it now. */
  1664. tt_assert(guard);
  1665. tt_int_op(guard->state, OP_EQ, GUARD_CIRC_STATE_COMPLETE);
  1666. g = entry_guard_handle_get(guard->guard);
  1667. tt_assert(g);
  1668. tt_int_op(g->is_reachable, OP_EQ, GUARD_REACHABLE_YES);
  1669. tt_int_op(g->confirmed_idx, OP_EQ, 0);
  1670. circuit_guard_state_free(guard);
  1671. guard = NULL;
  1672. node = NULL;
  1673. g = NULL;
  1674. /* Try again. We'll also get a primary guard this time. (The same one,
  1675. in fact.) But this time, we'll say the connection has failed. */
  1676. update_approx_time(start+35);
  1677. r = entry_guard_pick_for_circuit(gs, GUARD_USAGE_TRAFFIC, NULL,
  1678. &node, &guard);
  1679. tt_assert(r == 0);
  1680. tt_assert(node);
  1681. tt_assert(guard);
  1682. tt_int_op(guard->state, OP_EQ, GUARD_CIRC_STATE_USABLE_ON_COMPLETION);
  1683. tt_i64_op(guard->state_set_at, OP_EQ, start+35);
  1684. g = entry_guard_handle_get(guard->guard);
  1685. tt_assert(g);
  1686. tt_mem_op(g->identity, OP_EQ, node->identity, DIGEST_LEN);
  1687. tt_int_op(g->is_primary, OP_EQ, 1);
  1688. tt_i64_op(g->last_tried_to_connect, OP_EQ, start+35);
  1689. tt_int_op(g->confirmed_idx, OP_EQ, 0); // same one.
  1690. /* It's failed! What will happen to our poor guard? */
  1691. update_approx_time(start+45);
  1692. entry_guard_failed(&guard);
  1693. tt_assert(guard);
  1694. tt_int_op(guard->state, OP_EQ, GUARD_CIRC_STATE_DEAD);
  1695. tt_i64_op(guard->state_set_at, OP_EQ, start+45);
  1696. g = entry_guard_handle_get(guard->guard);
  1697. tt_assert(g);
  1698. tt_int_op(g->is_reachable, OP_EQ, GUARD_REACHABLE_NO);
  1699. tt_i64_op(g->failing_since, OP_EQ, start+45);
  1700. tt_int_op(g->confirmed_idx, OP_EQ, 0); // still confirmed.
  1701. circuit_guard_state_free(guard);
  1702. guard = NULL;
  1703. node = NULL;
  1704. entry_guard_t *g_prev = g;
  1705. g = NULL;
  1706. /* Now try a third time. Since the other one is down, we'll get a different
  1707. * (still primary) guard.
  1708. */
  1709. update_approx_time(start+60);
  1710. r = entry_guard_pick_for_circuit(gs, GUARD_USAGE_TRAFFIC, NULL,
  1711. &node, &guard);
  1712. tt_assert(r == 0);
  1713. tt_assert(node);
  1714. tt_assert(guard);
  1715. tt_int_op(guard->state, OP_EQ, GUARD_CIRC_STATE_USABLE_ON_COMPLETION);
  1716. g = entry_guard_handle_get(guard->guard);
  1717. tt_assert(g);
  1718. tt_ptr_op(g, OP_NE, g_prev);
  1719. tt_mem_op(g->identity, OP_EQ, node->identity, DIGEST_LEN);
  1720. tt_mem_op(g->identity, OP_NE, g_prev->identity, DIGEST_LEN);
  1721. tt_int_op(g->is_primary, OP_EQ, 1);
  1722. tt_i64_op(g->last_tried_to_connect, OP_EQ, start+60);
  1723. tt_int_op(g->confirmed_idx, OP_EQ, -1); // not confirmd now.
  1724. /* Call this one up; watch it get confirmed. */
  1725. update_approx_time(start+90);
  1726. u = entry_guard_succeeded(&guard);
  1727. tt_int_op(u, OP_EQ, GUARD_USABLE_NOW);
  1728. tt_assert(guard);
  1729. tt_int_op(guard->state, OP_EQ, GUARD_CIRC_STATE_COMPLETE);
  1730. g = entry_guard_handle_get(guard->guard);
  1731. tt_assert(g);
  1732. tt_int_op(g->is_reachable, OP_EQ, GUARD_REACHABLE_YES);
  1733. tt_int_op(g->confirmed_idx, OP_EQ, 1);
  1734. done:
  1735. guard_selection_free(gs);
  1736. circuit_guard_state_free(guard);
  1737. }
  1738. static void
  1739. test_entry_guard_select_for_circuit_highlevel_confirm_other(void *arg)
  1740. {
  1741. (void) arg;
  1742. const int N_PRIMARY = DFLT_N_PRIMARY_GUARDS;
  1743. /* At the start, we have no confirmed guards. We'll mark the primary guards
  1744. * down, then confirm something else. As soon as we do, it should become
  1745. * primary, and we should get it next time. */
  1746. time_t start = approx_time();
  1747. guard_selection_t *gs = guard_selection_new("default", GS_TYPE_NORMAL);
  1748. circuit_guard_state_t *guard = NULL;
  1749. int i, r;
  1750. const node_t *node = NULL;
  1751. guard_usable_t u;
  1752. /* Declare that we're on the internet. */
  1753. entry_guards_note_internet_connectivity(gs);
  1754. /* Primary guards are down! */
  1755. for (i = 0; i < N_PRIMARY; ++i) {
  1756. r = entry_guard_pick_for_circuit(gs, GUARD_USAGE_TRAFFIC, NULL,
  1757. &node, &guard);
  1758. tt_assert(node);
  1759. tt_assert(guard);
  1760. tt_assert(r == 0);
  1761. tt_int_op(guard->state, OP_EQ, GUARD_CIRC_STATE_USABLE_ON_COMPLETION);
  1762. entry_guard_failed(&guard);
  1763. circuit_guard_state_free(guard);
  1764. guard = NULL;
  1765. node = NULL;
  1766. }
  1767. /* Next guard should be non-primary. */
  1768. node = NULL;
  1769. r = entry_guard_pick_for_circuit(gs, GUARD_USAGE_TRAFFIC, NULL,
  1770. &node, &guard);
  1771. tt_assert(node);
  1772. tt_assert(guard);
  1773. tt_assert(r == 0);
  1774. entry_guard_t *g = entry_guard_handle_get(guard->guard);
  1775. tt_assert(g);
  1776. tt_int_op(guard->state, OP_EQ, GUARD_CIRC_STATE_USABLE_IF_NO_BETTER_GUARD);
  1777. tt_int_op(g->confirmed_idx, OP_EQ, -1);
  1778. tt_int_op(g->is_primary, OP_EQ, 0);
  1779. tt_int_op(g->is_pending, OP_EQ, 1);
  1780. (void)start;
  1781. u = entry_guard_succeeded(&guard);
  1782. /* We're on the internet (by fiat), so this guard will get called "confirmed"
  1783. * and should immediately become primary.
  1784. * XXXX prop271 -- I don't like that behavior, but it's what is specified
  1785. */
  1786. tt_int_op(guard->state, OP_EQ, GUARD_CIRC_STATE_COMPLETE);
  1787. tt_assert(u == GUARD_USABLE_NOW);
  1788. tt_int_op(g->confirmed_idx, OP_EQ, 0);
  1789. tt_int_op(g->is_primary, OP_EQ, 1);
  1790. tt_int_op(g->is_pending, OP_EQ, 0);
  1791. done:
  1792. guard_selection_free(gs);
  1793. circuit_guard_state_free(guard);
  1794. }
  1795. static void
  1796. test_entry_guard_select_for_circuit_highlevel_primary_retry(void *arg)
  1797. {
  1798. (void) arg;
  1799. const int N_PRIMARY = DFLT_N_PRIMARY_GUARDS;
  1800. /* At the start, we have no confirmed guards. We'll mark the primary guards
  1801. * down, then confirm something else. As soon as we do, it should become
  1802. * primary, and we should get it next time. */
  1803. time_t start = approx_time();
  1804. guard_selection_t *gs = guard_selection_new("default", GS_TYPE_NORMAL);
  1805. circuit_guard_state_t *guard = NULL, *guard2 = NULL;
  1806. int i, r;
  1807. const node_t *node = NULL;
  1808. entry_guard_t *g;
  1809. guard_usable_t u;
  1810. /* Declare that we're on the internet. */
  1811. entry_guards_note_internet_connectivity(gs);
  1812. /* Make primary guards confirmed (so they won't be superseded by a later
  1813. * guard), then mark them down. */
  1814. for (i = 0; i < N_PRIMARY; ++i) {
  1815. r = entry_guard_pick_for_circuit(gs, GUARD_USAGE_TRAFFIC, NULL,
  1816. &node, &guard);
  1817. tt_assert(node);
  1818. tt_assert(guard);
  1819. tt_assert(r == 0);
  1820. tt_int_op(guard->state, OP_EQ, GUARD_CIRC_STATE_USABLE_ON_COMPLETION);
  1821. g = entry_guard_handle_get(guard->guard);
  1822. make_guard_confirmed(gs, g);
  1823. tt_int_op(g->is_primary, OP_EQ, 1);
  1824. entry_guard_failed(&guard);
  1825. circuit_guard_state_free(guard);
  1826. tt_int_op(g->is_reachable, OP_EQ, GUARD_REACHABLE_NO);
  1827. guard = NULL;
  1828. node = NULL;
  1829. }
  1830. /* Get another guard that we might try. */
  1831. r = entry_guard_pick_for_circuit(gs, GUARD_USAGE_TRAFFIC, NULL,
  1832. &node, &guard);
  1833. tt_assert(node);
  1834. tt_assert(guard);
  1835. tt_assert(r == 0);
  1836. tt_int_op(guard->state, OP_EQ, GUARD_CIRC_STATE_USABLE_IF_NO_BETTER_GUARD);
  1837. g = entry_guard_handle_get(guard->guard);
  1838. tt_int_op(g->is_primary, OP_EQ, 0);
  1839. tt_assert(entry_guards_all_primary_guards_are_down(gs));
  1840. /* And an hour has passed ... */
  1841. update_approx_time(start + 3600);
  1842. /* Say that guard has succeeded! */
  1843. u = entry_guard_succeeded(&guard);
  1844. tt_int_op(u, OP_EQ, GUARD_MAYBE_USABLE_LATER);
  1845. tt_int_op(guard->state, OP_EQ, GUARD_CIRC_STATE_WAITING_FOR_BETTER_GUARD);
  1846. g = entry_guard_handle_get(guard->guard);
  1847. /* The primary guards should have been marked up! */
  1848. SMARTLIST_FOREACH(gs->primary_entry_guards, entry_guard_t *, pg, {
  1849. tt_int_op(pg->is_primary, OP_EQ, 1);
  1850. tt_ptr_op(g, OP_NE, pg);
  1851. tt_int_op(pg->is_reachable, OP_EQ, GUARD_REACHABLE_MAYBE);
  1852. });
  1853. /* Have a circuit to a primary guard succeed. */
  1854. r = entry_guard_pick_for_circuit(gs, GUARD_USAGE_TRAFFIC, NULL,
  1855. &node, &guard2);
  1856. tt_assert(r == 0);
  1857. tt_int_op(guard2->state, OP_EQ, GUARD_CIRC_STATE_USABLE_ON_COMPLETION);
  1858. u = entry_guard_succeeded(&guard2);
  1859. tt_assert(u == GUARD_USABLE_NOW);
  1860. tt_int_op(guard2->state, OP_EQ, GUARD_CIRC_STATE_COMPLETE);
  1861. tt_assert(! entry_guards_all_primary_guards_are_down(gs));
  1862. done:
  1863. guard_selection_free(gs);
  1864. circuit_guard_state_free(guard);
  1865. circuit_guard_state_free(guard2);
  1866. }
  1867. static void
  1868. test_entry_guard_select_and_cancel(void *arg)
  1869. {
  1870. (void) arg;
  1871. const int N_PRIMARY = DFLT_N_PRIMARY_GUARDS;
  1872. int i,r;
  1873. const node_t *node = NULL;
  1874. circuit_guard_state_t *guard;
  1875. guard_selection_t *gs = guard_selection_new("default", GS_TYPE_NORMAL);
  1876. entry_guard_t *g;
  1877. /* Once more, we mark all the primary guards down. */
  1878. entry_guards_note_internet_connectivity(gs);
  1879. for (i = 0; i < N_PRIMARY; ++i) {
  1880. r = entry_guard_pick_for_circuit(gs, GUARD_USAGE_TRAFFIC, NULL,
  1881. &node, &guard);
  1882. tt_int_op(r, OP_EQ, 0);
  1883. tt_int_op(guard->state, OP_EQ, GUARD_CIRC_STATE_USABLE_ON_COMPLETION);
  1884. g = entry_guard_handle_get(guard->guard);
  1885. tt_int_op(g->is_primary, OP_EQ, 1);
  1886. tt_int_op(g->is_pending, OP_EQ, 0);
  1887. make_guard_confirmed(gs, g);
  1888. entry_guard_failed(&guard);
  1889. circuit_guard_state_free(guard);
  1890. guard = NULL;
  1891. node = NULL;
  1892. }
  1893. tt_assert(entry_guards_all_primary_guards_are_down(gs));
  1894. /* Now get another guard we could try... */
  1895. r = entry_guard_pick_for_circuit(gs, GUARD_USAGE_TRAFFIC, NULL,
  1896. &node, &guard);
  1897. tt_assert(node);
  1898. tt_assert(guard);
  1899. tt_assert(r == 0);
  1900. tt_int_op(guard->state, OP_EQ, GUARD_CIRC_STATE_USABLE_IF_NO_BETTER_GUARD);
  1901. g = entry_guard_handle_get(guard->guard);
  1902. tt_int_op(g->is_primary, OP_EQ, 0);
  1903. tt_int_op(g->is_pending, OP_EQ, 1);
  1904. /* Whoops! We should never have asked for this guard. Cancel the request! */
  1905. entry_guard_cancel(&guard);
  1906. tt_assert(guard == NULL);
  1907. tt_int_op(g->is_primary, OP_EQ, 0);
  1908. tt_int_op(g->is_pending, OP_EQ, 0);
  1909. done:
  1910. guard_selection_free(gs);
  1911. circuit_guard_state_free(guard);
  1912. }
  1913. /* Unit test setup function: Create a fake network, and set everything up
  1914. * for testing the upgrade-a-waiting-circuit code. */
  1915. typedef struct {
  1916. guard_selection_t *gs;
  1917. time_t start;
  1918. circuit_guard_state_t *guard1_state;
  1919. circuit_guard_state_t *guard2_state;
  1920. entry_guard_t *guard1;
  1921. entry_guard_t *guard2;
  1922. origin_circuit_t *circ1;
  1923. origin_circuit_t *circ2;
  1924. smartlist_t *all_origin_circuits;
  1925. } upgrade_circuits_data_t;
  1926. static void *
  1927. upgrade_circuits_setup(const struct testcase_t *testcase)
  1928. {
  1929. upgrade_circuits_data_t *data = tor_malloc_zero(sizeof(*data));
  1930. guard_selection_t *gs = data->gs =
  1931. guard_selection_new("default", GS_TYPE_NORMAL);
  1932. circuit_guard_state_t *guard;
  1933. const node_t *node;
  1934. entry_guard_t *g;
  1935. int i;
  1936. const int N_PRIMARY = DFLT_N_PRIMARY_GUARDS;
  1937. const char *argument = testcase->setup_data;
  1938. const int make_circ1_succeed = strstr(argument, "c1-done") != NULL;
  1939. const int make_circ2_succeed = strstr(argument, "c2-done") != NULL;
  1940. big_fake_network_setup(testcase);
  1941. /* We're going to set things up in a state where a circuit will be ready to
  1942. * be upgraded. Each test can make a single change (or not) that should
  1943. * block the upgrade.
  1944. */
  1945. /* First, make all the primary guards confirmed, and down. */
  1946. data->start = approx_time();
  1947. entry_guards_note_internet_connectivity(gs);
  1948. for (i = 0; i < N_PRIMARY; ++i) {
  1949. entry_guard_pick_for_circuit(gs, GUARD_USAGE_TRAFFIC, NULL, &node, &guard);
  1950. g = entry_guard_handle_get(guard->guard);
  1951. make_guard_confirmed(gs, g);
  1952. entry_guard_failed(&guard);
  1953. circuit_guard_state_free(guard);
  1954. }
  1955. /* Grab another couple of guards */
  1956. data->all_origin_circuits = smartlist_new();
  1957. update_approx_time(data->start + 27);
  1958. entry_guard_pick_for_circuit(gs, GUARD_USAGE_TRAFFIC, NULL,
  1959. &node, &data->guard1_state);
  1960. origin_circuit_t *circ;
  1961. data->circ1 = circ = origin_circuit_new();
  1962. circ->base_.purpose = CIRCUIT_PURPOSE_C_GENERAL;
  1963. circ->guard_state = data->guard1_state;
  1964. smartlist_add(data->all_origin_circuits, circ);
  1965. update_approx_time(data->start + 30);
  1966. entry_guard_pick_for_circuit(gs, GUARD_USAGE_TRAFFIC, NULL,
  1967. &node, &data->guard2_state);
  1968. data->circ2 = circ = origin_circuit_new();
  1969. circ->base_.purpose = CIRCUIT_PURPOSE_C_GENERAL;
  1970. circ->guard_state = data->guard2_state;
  1971. smartlist_add(data->all_origin_circuits, circ);
  1972. data->guard1 = entry_guard_handle_get(data->guard1_state->guard);
  1973. data->guard2 = entry_guard_handle_get(data->guard2_state->guard);
  1974. tor_assert(data->guard1 != data->guard2);
  1975. tor_assert(data->guard1_state->state ==
  1976. GUARD_CIRC_STATE_USABLE_IF_NO_BETTER_GUARD);
  1977. tor_assert(data->guard2_state->state ==
  1978. GUARD_CIRC_STATE_USABLE_IF_NO_BETTER_GUARD);
  1979. guard_usable_t r;
  1980. update_approx_time(data->start + 32);
  1981. if (make_circ1_succeed) {
  1982. r = entry_guard_succeeded(&data->guard1_state);
  1983. tor_assert(r == GUARD_MAYBE_USABLE_LATER);
  1984. tor_assert(data->guard1_state->state ==
  1985. GUARD_CIRC_STATE_WAITING_FOR_BETTER_GUARD);
  1986. }
  1987. update_approx_time(data->start + 33);
  1988. if (make_circ2_succeed) {
  1989. r = entry_guard_succeeded(&data->guard2_state);
  1990. tor_assert(r == GUARD_MAYBE_USABLE_LATER);
  1991. tor_assert(data->guard2_state->state ==
  1992. GUARD_CIRC_STATE_WAITING_FOR_BETTER_GUARD);
  1993. }
  1994. return data;
  1995. }
  1996. static int
  1997. upgrade_circuits_cleanup(const struct testcase_t *testcase, void *ptr)
  1998. {
  1999. upgrade_circuits_data_t *data = ptr;
  2000. // circuit_guard_state_free(data->guard1_state); // held in circ1
  2001. // circuit_guard_state_free(data->guard2_state); // held in circ2
  2002. guard_selection_free(data->gs);
  2003. smartlist_free(data->all_origin_circuits);
  2004. circuit_free(TO_CIRCUIT(data->circ1));
  2005. circuit_free(TO_CIRCUIT(data->circ2));
  2006. tor_free(data);
  2007. return big_fake_network_cleanup(testcase, NULL);
  2008. }
  2009. static void
  2010. test_entry_guard_upgrade_a_circuit(void *arg)
  2011. {
  2012. upgrade_circuits_data_t *data = arg;
  2013. /* This is the easy case: we have no COMPLETED circuits, all the
  2014. * primary guards are down, we have two WAITING circuits: one will
  2015. * get upgraded to COMPLETED! (The one that started first.)
  2016. */
  2017. /* XXXX prop271 -- perhaps the one that started first should
  2018. * also wind up in confirmed_entry_guards earlier?
  2019. */
  2020. smartlist_t *result = smartlist_new();
  2021. int r;
  2022. r = entry_guards_upgrade_waiting_circuits(data->gs,
  2023. data->all_origin_circuits,
  2024. result);
  2025. tt_int_op(r, OP_EQ, 1);
  2026. tt_int_op(smartlist_len(result), OP_EQ, 1);
  2027. origin_circuit_t *oc = smartlist_get(result, 0);
  2028. /* circ1 was started first, so we'll get told to ugrade it... */
  2029. tt_ptr_op(oc, OP_EQ, data->circ1);
  2030. /* And the guard state should be complete */
  2031. tt_ptr_op(data->guard1_state, OP_NE, NULL);
  2032. tt_int_op(data->guard1_state->state, OP_EQ, GUARD_CIRC_STATE_COMPLETE);
  2033. done:
  2034. smartlist_free(result);
  2035. }
  2036. static void
  2037. test_entry_guard_upgrade_blocked_by_live_primary_guards(void *arg)
  2038. {
  2039. upgrade_circuits_data_t *data = arg;
  2040. /* If any primary guards might be up, we can't upgrade any waiting
  2041. * circuits.
  2042. */
  2043. mark_primary_guards_maybe_reachable(data->gs);
  2044. smartlist_t *result = smartlist_new();
  2045. int r;
  2046. setup_capture_of_logs(LOG_DEBUG);
  2047. r = entry_guards_upgrade_waiting_circuits(data->gs,
  2048. data->all_origin_circuits,
  2049. result);
  2050. tt_int_op(r, OP_EQ, 0);
  2051. tt_int_op(smartlist_len(result), OP_EQ, 0);
  2052. expect_log_msg_containing("not all primary guards were definitely down.");
  2053. done:
  2054. teardown_capture_of_logs();
  2055. smartlist_free(result);
  2056. }
  2057. static void
  2058. test_entry_guard_upgrade_blocked_by_lack_of_waiting_circuits(void *arg)
  2059. {
  2060. upgrade_circuits_data_t *data = arg;
  2061. /* If no circuits are waiting, we can't upgrade anything. (The test
  2062. * setup in this case was told not to make any of the circuits "waiting".)
  2063. */
  2064. smartlist_t *result = smartlist_new();
  2065. int r;
  2066. setup_capture_of_logs(LOG_DEBUG);
  2067. r = entry_guards_upgrade_waiting_circuits(data->gs,
  2068. data->all_origin_circuits,
  2069. result);
  2070. tt_int_op(r, OP_EQ, 0);
  2071. tt_int_op(smartlist_len(result), OP_EQ, 0);
  2072. expect_log_msg_containing("Considered upgrading guard-stalled circuits, "
  2073. "but didn't find any.");
  2074. done:
  2075. teardown_capture_of_logs();
  2076. smartlist_free(result);
  2077. }
  2078. static void
  2079. test_entry_guard_upgrade_blocked_by_better_circ_complete(void *arg)
  2080. {
  2081. upgrade_circuits_data_t *data = arg;
  2082. /* We'll run through the logic of upgrade_a_circuit below...
  2083. * and then try again to make sure that circ2 isn't also upgraded.
  2084. */
  2085. smartlist_t *result = smartlist_new();
  2086. int r;
  2087. r = entry_guards_upgrade_waiting_circuits(data->gs,
  2088. data->all_origin_circuits,
  2089. result);
  2090. tt_int_op(r, OP_EQ, 1);
  2091. tt_int_op(smartlist_len(result), OP_EQ, 1);
  2092. origin_circuit_t *oc = smartlist_get(result, 0);
  2093. tt_ptr_op(oc, OP_EQ, data->circ1);
  2094. tt_ptr_op(data->guard1_state, OP_NE, NULL);
  2095. tt_int_op(data->guard1_state->state, OP_EQ, GUARD_CIRC_STATE_COMPLETE);
  2096. /* Now, try again. Make sure that circ2 isn't upgraded. */
  2097. smartlist_clear(result);
  2098. setup_capture_of_logs(LOG_DEBUG);
  2099. r = entry_guards_upgrade_waiting_circuits(data->gs,
  2100. data->all_origin_circuits,
  2101. result);
  2102. tt_int_op(r, OP_EQ, 0);
  2103. tt_int_op(smartlist_len(result), OP_EQ, 0);
  2104. expect_log_msg_containing("At least one complete circuit had higher "
  2105. "priority, so not upgrading.");
  2106. done:
  2107. teardown_capture_of_logs();
  2108. smartlist_free(result);
  2109. }
  2110. static void
  2111. test_entry_guard_upgrade_not_blocked_by_restricted_circ_complete(void *arg)
  2112. {
  2113. upgrade_circuits_data_t *data = arg;
  2114. /* Once more, let circ1 become complete. But this time, we'll claim
  2115. * that circ2 was restricted to not use the same guard as circ1. */
  2116. data->guard2_state->restrictions =
  2117. tor_malloc_zero(sizeof(entry_guard_restriction_t));
  2118. memcpy(data->guard2_state->restrictions->exclude_id,
  2119. data->guard1->identity, DIGEST_LEN);
  2120. smartlist_t *result = smartlist_new();
  2121. int r;
  2122. r = entry_guards_upgrade_waiting_circuits(data->gs,
  2123. data->all_origin_circuits,
  2124. result);
  2125. tt_int_op(r, OP_EQ, 1);
  2126. tt_int_op(smartlist_len(result), OP_EQ, 1);
  2127. origin_circuit_t *oc = smartlist_get(result, 0);
  2128. tt_ptr_op(oc, OP_EQ, data->circ1);
  2129. tt_ptr_op(data->guard1_state, OP_NE, NULL);
  2130. tt_int_op(data->guard1_state->state, OP_EQ, GUARD_CIRC_STATE_COMPLETE);
  2131. /* Now, we try again. Since circ2 has a restriction that circ1 doesn't obey,
  2132. * circ2 _is_ eligible for upgrade. */
  2133. smartlist_clear(result);
  2134. r = entry_guards_upgrade_waiting_circuits(data->gs,
  2135. data->all_origin_circuits,
  2136. result);
  2137. tt_int_op(r, OP_EQ, 1);
  2138. tt_int_op(smartlist_len(result), OP_EQ, 1);
  2139. origin_circuit_t *oc2 = smartlist_get(result, 0);
  2140. tt_ptr_op(oc2, OP_EQ, data->circ2);
  2141. done:
  2142. smartlist_free(result);
  2143. }
  2144. static void
  2145. test_entry_guard_upgrade_not_blocked_by_worse_circ_complete(void *arg)
  2146. {
  2147. upgrade_circuits_data_t *data = arg;
  2148. smartlist_t *result = smartlist_new();
  2149. /* here we manually make circ2 COMPLETE, and make sure that circ1
  2150. * gets made complete anyway, since guard1 has higher priority
  2151. */
  2152. update_approx_time(data->start + 300);
  2153. data->guard2_state->state = GUARD_CIRC_STATE_COMPLETE;
  2154. data->guard2_state->state_set_at = approx_time();
  2155. update_approx_time(data->start + 301);
  2156. /* Now, try again. Make sure that circ1 is approved. */
  2157. int r;
  2158. r = entry_guards_upgrade_waiting_circuits(data->gs,
  2159. data->all_origin_circuits,
  2160. result);
  2161. tt_int_op(r, OP_EQ, 1);
  2162. tt_int_op(smartlist_len(result), OP_EQ, 1);
  2163. origin_circuit_t *oc = smartlist_get(result, 0);
  2164. tt_ptr_op(oc, OP_EQ, data->circ1);
  2165. done:
  2166. smartlist_free(result);
  2167. }
  2168. static void
  2169. test_entry_guard_upgrade_blocked_by_better_circ_pending(void *arg)
  2170. {
  2171. upgrade_circuits_data_t *data = arg;
  2172. /* circ2 is done, but circ1 is still pending. Since circ1 is better,
  2173. * we won't upgrade circ2. */
  2174. /* XXXX Prop271 -- this is a kludge. I'm making sure circ1 _is_ better,
  2175. * by messing with the guards' confirmed_idx */
  2176. make_guard_confirmed(data->gs, data->guard1);
  2177. {
  2178. int tmp;
  2179. tmp = data->guard1->confirmed_idx;
  2180. data->guard1->confirmed_idx = data->guard2->confirmed_idx;
  2181. data->guard2->confirmed_idx = tmp;
  2182. }
  2183. smartlist_t *result = smartlist_new();
  2184. setup_capture_of_logs(LOG_DEBUG);
  2185. int r;
  2186. r = entry_guards_upgrade_waiting_circuits(data->gs,
  2187. data->all_origin_circuits,
  2188. result);
  2189. tt_int_op(r, OP_EQ, 0);
  2190. tt_int_op(smartlist_len(result), OP_EQ, 0);
  2191. expect_log_msg_containing("but 1 pending circuit(s) had higher guard "
  2192. "priority, so not upgrading.");
  2193. done:
  2194. teardown_capture_of_logs();
  2195. smartlist_free(result);
  2196. }
  2197. static void
  2198. test_entry_guard_upgrade_not_blocked_by_restricted_circ_pending(void *arg)
  2199. {
  2200. upgrade_circuits_data_t *data = arg;
  2201. /* circ2 is done, but circ1 is still pending. But when there is a
  2202. restriction on circ2 that circ1 can't satisfy, circ1 can't block
  2203. circ2. */
  2204. /* XXXX Prop271 -- this is a kludge. I'm making sure circ1 _is_ better,
  2205. * by messing with the guards' confirmed_idx */
  2206. make_guard_confirmed(data->gs, data->guard1);
  2207. {
  2208. int tmp;
  2209. tmp = data->guard1->confirmed_idx;
  2210. data->guard1->confirmed_idx = data->guard2->confirmed_idx;
  2211. data->guard2->confirmed_idx = tmp;
  2212. }
  2213. data->guard2_state->restrictions =
  2214. tor_malloc_zero(sizeof(entry_guard_restriction_t));
  2215. memcpy(data->guard2_state->restrictions->exclude_id,
  2216. data->guard1->identity, DIGEST_LEN);
  2217. smartlist_t *result = smartlist_new();
  2218. int r;
  2219. r = entry_guards_upgrade_waiting_circuits(data->gs,
  2220. data->all_origin_circuits,
  2221. result);
  2222. tt_int_op(r, OP_EQ, 1);
  2223. tt_int_op(smartlist_len(result), OP_EQ, 1);
  2224. origin_circuit_t *oc = smartlist_get(result, 0);
  2225. tt_ptr_op(oc, OP_EQ, data->circ2);
  2226. done:
  2227. smartlist_free(result);
  2228. }
  2229. static void
  2230. test_entry_guard_upgrade_not_blocked_by_worse_circ_pending(void *arg)
  2231. {
  2232. upgrade_circuits_data_t *data = arg;
  2233. /* circ1 is done, but circ2 is still pending. Since circ1 is better,
  2234. * we will upgrade it. */
  2235. smartlist_t *result = smartlist_new();
  2236. int r;
  2237. r = entry_guards_upgrade_waiting_circuits(data->gs,
  2238. data->all_origin_circuits,
  2239. result);
  2240. tt_int_op(r, OP_EQ, 1);
  2241. tt_int_op(smartlist_len(result), OP_EQ, 1);
  2242. origin_circuit_t *oc = smartlist_get(result, 0);
  2243. tt_ptr_op(oc, OP_EQ, data->circ1);
  2244. done:
  2245. smartlist_free(result);
  2246. }
  2247. static void
  2248. test_enty_guard_should_expire_waiting(void *arg)
  2249. {
  2250. (void)arg;
  2251. circuit_guard_state_t *fake_state = tor_malloc_zero(sizeof(*fake_state));
  2252. /* We'll leave "guard" unset -- it won't matter here. */
  2253. /* No state? Can't expire. */
  2254. tt_assert(! entry_guard_state_should_expire(NULL));
  2255. /* Let's try one that expires. */
  2256. fake_state->state = GUARD_CIRC_STATE_WAITING_FOR_BETTER_GUARD;
  2257. fake_state->state_set_at =
  2258. approx_time() - DFLT_NONPRIMARY_GUARD_IDLE_TIMEOUT - 1;
  2259. tt_assert(entry_guard_state_should_expire(fake_state));
  2260. /* But it wouldn't expire if we changed the state. */
  2261. fake_state->state = GUARD_CIRC_STATE_USABLE_IF_NO_BETTER_GUARD;
  2262. tt_assert(! entry_guard_state_should_expire(fake_state));
  2263. /* And it wouldn't have expired a few seconds ago. */
  2264. fake_state->state = GUARD_CIRC_STATE_WAITING_FOR_BETTER_GUARD;
  2265. fake_state->state_set_at =
  2266. approx_time() - DFLT_NONPRIMARY_GUARD_IDLE_TIMEOUT + 5;
  2267. tt_assert(! entry_guard_state_should_expire(fake_state));
  2268. done:
  2269. tor_free(fake_state);
  2270. }
  2271. static const struct testcase_setup_t big_fake_network = {
  2272. big_fake_network_setup, big_fake_network_cleanup
  2273. };
  2274. static const struct testcase_setup_t upgrade_circuits = {
  2275. upgrade_circuits_setup, upgrade_circuits_cleanup
  2276. };
  2277. #define BFN_TEST(name) \
  2278. { #name, test_entry_guard_ ## name, TT_FORK, &big_fake_network, NULL }
  2279. #define UPGRADE_TEST(name, arg) \
  2280. { #name, test_entry_guard_ ## name, TT_FORK, &upgrade_circuits, \
  2281. (void*)(arg) }
  2282. struct testcase_t entrynodes_tests[] = {
  2283. { "node_preferred_orport",
  2284. test_node_preferred_orport,
  2285. 0, NULL, NULL },
  2286. { "entry_guard_describe", test_entry_guard_describe, 0, NULL, NULL },
  2287. { "randomize_time", test_entry_guard_randomize_time, 0, NULL, NULL },
  2288. { "encode_for_state_minimal",
  2289. test_entry_guard_encode_for_state_minimal, 0, NULL, NULL },
  2290. { "encode_for_state_maximal",
  2291. test_entry_guard_encode_for_state_maximal, 0, NULL, NULL },
  2292. { "parse_from_state_minimal",
  2293. test_entry_guard_parse_from_state_minimal, 0, NULL, NULL },
  2294. { "parse_from_state_maximal",
  2295. test_entry_guard_parse_from_state_maximal, 0, NULL, NULL },
  2296. { "parse_from_state_failure",
  2297. test_entry_guard_parse_from_state_failure, 0, NULL, NULL },
  2298. { "parse_from_state_partial_failure",
  2299. test_entry_guard_parse_from_state_partial_failure, 0, NULL, NULL },
  2300. { "parse_from_state_full",
  2301. test_entry_guard_parse_from_state_full, TT_FORK, NULL, NULL },
  2302. { "parse_from_state_broken",
  2303. test_entry_guard_parse_from_state_broken, TT_FORK, NULL, NULL },
  2304. { "get_guard_selection_by_name",
  2305. test_entry_guard_get_guard_selection_by_name, TT_FORK, NULL, NULL },
  2306. BFN_TEST(choose_selection_initial),
  2307. BFN_TEST(add_single_guard),
  2308. BFN_TEST(node_filter),
  2309. BFN_TEST(expand_sample),
  2310. BFN_TEST(expand_sample_small_net),
  2311. BFN_TEST(update_from_consensus_status),
  2312. BFN_TEST(update_from_consensus_repair),
  2313. BFN_TEST(update_from_consensus_remove),
  2314. BFN_TEST(confirming_guards),
  2315. BFN_TEST(sample_reachable_filtered),
  2316. BFN_TEST(sample_reachable_filtered_empty),
  2317. BFN_TEST(retry_unreachable),
  2318. BFN_TEST(manage_primary),
  2319. { "guard_preferred", test_entry_guard_guard_preferred, TT_FORK, NULL, NULL },
  2320. BFN_TEST(select_for_circuit_no_confirmed),
  2321. BFN_TEST(select_for_circuit_confirmed),
  2322. BFN_TEST(select_for_circuit_highlevel_primary),
  2323. BFN_TEST(select_for_circuit_highlevel_confirm_other),
  2324. BFN_TEST(select_for_circuit_highlevel_primary_retry),
  2325. BFN_TEST(select_and_cancel),
  2326. UPGRADE_TEST(upgrade_a_circuit, "c1-done c2-done"),
  2327. UPGRADE_TEST(upgrade_blocked_by_live_primary_guards, "c1-done c2-done"),
  2328. UPGRADE_TEST(upgrade_blocked_by_lack_of_waiting_circuits, ""),
  2329. UPGRADE_TEST(upgrade_blocked_by_better_circ_complete, "c1-done c2-done"),
  2330. UPGRADE_TEST(upgrade_not_blocked_by_restricted_circ_complete,
  2331. "c1-done c2-done"),
  2332. UPGRADE_TEST(upgrade_not_blocked_by_worse_circ_complete, "c1-done c2-done"),
  2333. UPGRADE_TEST(upgrade_blocked_by_better_circ_pending, "c2-done"),
  2334. UPGRADE_TEST(upgrade_not_blocked_by_restricted_circ_pending,
  2335. "c2-done"),
  2336. UPGRADE_TEST(upgrade_not_blocked_by_worse_circ_pending, "c1-done"),
  2337. { "should_expire_waiting", test_enty_guard_should_expire_waiting, TT_FORK,
  2338. NULL, NULL },
  2339. END_OF_TESTCASES
  2340. };