test_entrynodes.c 100 KB

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