test_entrynodes.c 116 KB

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