test_entrynodes.c 72 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243
  1. /* Copyright (c) 2014-2016, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. #include "orconfig.h"
  4. #define STATEFILE_PRIVATE
  5. #define ENTRYNODES_PRIVATE
  6. #define ROUTERLIST_PRIVATE
  7. #include "or.h"
  8. #include "test.h"
  9. #include "bridges.h"
  10. #include "config.h"
  11. #include "entrynodes.h"
  12. #include "nodelist.h"
  13. #include "networkstatus.h"
  14. #include "policies.h"
  15. #include "routerlist.h"
  16. #include "routerparse.h"
  17. #include "routerset.h"
  18. #include "statefile.h"
  19. #include "util.h"
  20. #include "test_helpers.h"
  21. #include "log_test_helpers.h"
  22. /* TODO:
  23. * choose_random_entry() test with state set.
  24. *
  25. * parse_state() tests with more than one guards.
  26. *
  27. * More tests for set_from_config(): Multiple nodes, use fingerprints,
  28. * use country codes.
  29. */
  30. /** Dummy Tor state used in unittests. */
  31. static or_state_t *dummy_state = NULL;
  32. static or_state_t *
  33. get_or_state_replacement(void)
  34. {
  35. return dummy_state;
  36. }
  37. /* Unittest cleanup function: Cleanup the fake network. */
  38. static int
  39. fake_network_cleanup(const struct testcase_t *testcase, void *ptr)
  40. {
  41. (void) testcase;
  42. (void) ptr;
  43. routerlist_free_all();
  44. nodelist_free_all();
  45. entry_guards_free_all();
  46. or_state_free(dummy_state);
  47. return 1; /* NOP */
  48. }
  49. /* Unittest setup function: Setup a fake network. */
  50. static void *
  51. fake_network_setup(const struct testcase_t *testcase)
  52. {
  53. (void) testcase;
  54. /* Setup fake state */
  55. dummy_state = tor_malloc_zero(sizeof(or_state_t));
  56. MOCK(get_or_state,
  57. get_or_state_replacement);
  58. /* Setup fake routerlist. */
  59. helper_setup_fake_routerlist();
  60. /* Return anything but NULL (it's interpreted as test fail) */
  61. return dummy_state;
  62. }
  63. static networkstatus_t *dummy_consensus = NULL;
  64. static smartlist_t *big_fake_net_nodes = NULL;
  65. static smartlist_t *
  66. bfn_mock_nodelist_get_list(void)
  67. {
  68. return big_fake_net_nodes;
  69. }
  70. static networkstatus_t *
  71. bfn_mock_networkstatus_get_live_consensus(time_t now)
  72. {
  73. (void)now;
  74. return dummy_consensus;
  75. }
  76. static const node_t *
  77. bfn_mock_node_get_by_id(const char *id)
  78. {
  79. SMARTLIST_FOREACH(big_fake_net_nodes, node_t *, n,
  80. if (fast_memeq(n->identity, id, 20))
  81. return n);
  82. return NULL;
  83. }
  84. /* Unittest cleanup function: Cleanup the fake network. */
  85. static int
  86. big_fake_network_cleanup(const struct testcase_t *testcase, void *ptr)
  87. {
  88. (void) testcase;
  89. (void) ptr;
  90. if (big_fake_net_nodes) {
  91. SMARTLIST_FOREACH(big_fake_net_nodes, node_t *, n, {
  92. tor_free(n->rs);
  93. tor_free(n->md);
  94. tor_free(n);
  95. });
  96. smartlist_free(big_fake_net_nodes);
  97. }
  98. UNMOCK(nodelist_get_list);
  99. UNMOCK(node_get_by_id);
  100. UNMOCK(get_or_state);
  101. UNMOCK(networkstatus_get_live_consensus);
  102. or_state_free(dummy_state);
  103. dummy_state = NULL;
  104. tor_free(dummy_consensus);
  105. return 1; /* NOP */
  106. }
  107. /* Unittest setup function: Setup a fake network. */
  108. static void *
  109. big_fake_network_setup(const struct testcase_t *testcase)
  110. {
  111. int i;
  112. /* These are minimal node_t objects that only contain the aspects of node_t
  113. * that we need for entrynodes.c. */
  114. const int N_NODES = 271;
  115. big_fake_net_nodes = smartlist_new();
  116. for (i = 0; i < N_NODES; ++i) {
  117. node_t *n = tor_malloc_zero(sizeof(node_t));
  118. n->md = tor_malloc_zero(sizeof(microdesc_t));
  119. crypto_rand(n->identity, sizeof(n->identity));
  120. n->rs = tor_malloc_zero(sizeof(routerstatus_t));
  121. memcpy(n->rs->identity_digest, n->identity, DIGEST_LEN);
  122. n->is_running = n->is_valid = n->is_fast = n->is_stable = 1;
  123. n->rs->addr = 0x04020202;
  124. n->rs->or_port = 1234;
  125. n->rs->is_v2_dir = 1;
  126. n->rs->has_bandwidth = 1;
  127. n->rs->bandwidth_kb = 30;
  128. /* Call half of the nodes a possible guard. */
  129. if (i % 2 == 0) {
  130. n->is_possible_guard = 1;
  131. n->rs->guardfraction_percentage = 100;
  132. n->rs->has_guardfraction = 1;
  133. }
  134. smartlist_add(big_fake_net_nodes, n);
  135. }
  136. dummy_state = tor_malloc_zero(sizeof(or_state_t));
  137. dummy_consensus = tor_malloc_zero(sizeof(networkstatus_t));
  138. dummy_consensus->valid_after = approx_time() - 3600;
  139. dummy_consensus->valid_until = approx_time() + 3600;
  140. MOCK(nodelist_get_list, bfn_mock_nodelist_get_list);
  141. MOCK(node_get_by_id, bfn_mock_node_get_by_id);
  142. MOCK(get_or_state,
  143. get_or_state_replacement);
  144. MOCK(networkstatus_get_live_consensus,
  145. bfn_mock_networkstatus_get_live_consensus);
  146. /* Return anything but NULL (it's interpreted as test fail) */
  147. return (void*)testcase;
  148. }
  149. static time_t
  150. mock_randomize_time_no_randomization(time_t a, time_t b)
  151. {
  152. (void) b;
  153. return a;
  154. }
  155. static or_options_t mocked_options;
  156. static const or_options_t *
  157. mock_get_options(void)
  158. {
  159. return &mocked_options;
  160. }
  161. /** Test choose_random_entry() with none of our routers being guard nodes. */
  162. static void
  163. test_choose_random_entry_no_guards(void *arg)
  164. {
  165. const node_t *chosen_entry = NULL;
  166. (void) arg;
  167. MOCK(get_options, mock_get_options);
  168. /* Check that we get a guard if it passes preferred
  169. * address settings */
  170. memset(&mocked_options, 0, sizeof(mocked_options));
  171. mocked_options.ClientUseIPv4 = 1;
  172. mocked_options.ClientPreferIPv6ORPort = 0;
  173. mocked_options.UseDeprecatedGuardAlgorithm = 1;
  174. /* Try to pick an entry even though none of our routers are guards. */
  175. chosen_entry = choose_random_entry(NULL);
  176. /* Unintuitively, we actually pick a random node as our entry,
  177. because router_choose_random_node() relaxes its constraints if it
  178. can't find a proper entry guard. */
  179. tt_assert(chosen_entry);
  180. /* And with the other IP version active */
  181. mocked_options.ClientUseIPv6 = 1;
  182. chosen_entry = choose_random_entry(NULL);
  183. tt_assert(chosen_entry);
  184. /* And with the preference on auto */
  185. mocked_options.ClientPreferIPv6ORPort = -1;
  186. chosen_entry = choose_random_entry(NULL);
  187. tt_assert(chosen_entry);
  188. /* Check that we don't get a guard if it doesn't pass mandatory address
  189. * settings */
  190. memset(&mocked_options, 0, sizeof(mocked_options));
  191. mocked_options.ClientUseIPv4 = 0;
  192. mocked_options.ClientPreferIPv6ORPort = 0;
  193. mocked_options.UseDeprecatedGuardAlgorithm = 1;
  194. chosen_entry = choose_random_entry(NULL);
  195. /* If we don't allow IPv4 at all, we don't get a guard*/
  196. tt_assert(!chosen_entry);
  197. /* Check that we get a guard if it passes allowed but not preferred address
  198. * settings */
  199. memset(&mocked_options, 0, sizeof(mocked_options));
  200. mocked_options.ClientUseIPv4 = 1;
  201. mocked_options.ClientUseIPv6 = 1;
  202. mocked_options.ClientPreferIPv6ORPort = 1;
  203. mocked_options.UseDeprecatedGuardAlgorithm = 1;
  204. chosen_entry = choose_random_entry(NULL);
  205. tt_assert(chosen_entry);
  206. /* Check that we get a guard if it passes preferred address settings when
  207. * they're auto */
  208. memset(&mocked_options, 0, sizeof(mocked_options));
  209. mocked_options.ClientUseIPv4 = 1;
  210. mocked_options.ClientPreferIPv6ORPort = -1;
  211. mocked_options.UseDeprecatedGuardAlgorithm = 1;
  212. chosen_entry = choose_random_entry(NULL);
  213. tt_assert(chosen_entry);
  214. /* And with IPv6 active */
  215. mocked_options.ClientUseIPv6 = 1;
  216. chosen_entry = choose_random_entry(NULL);
  217. tt_assert(chosen_entry);
  218. done:
  219. memset(&mocked_options, 0, sizeof(mocked_options));
  220. UNMOCK(get_options);
  221. }
  222. /** Test choose_random_entry() with only one of our routers being a
  223. guard node. */
  224. static void
  225. test_choose_random_entry_one_possible_guard(void *arg)
  226. {
  227. const node_t *chosen_entry = NULL;
  228. node_t *the_guard = NULL;
  229. smartlist_t *our_nodelist = NULL;
  230. (void) arg;
  231. MOCK(get_options, mock_get_options);
  232. /* Set one of the nodes to be a guard. */
  233. our_nodelist = nodelist_get_list();
  234. the_guard = smartlist_get(our_nodelist, 4); /* chosen by fair dice roll */
  235. the_guard->is_possible_guard = 1;
  236. /* Check that we get the guard if it passes preferred
  237. * address settings */
  238. memset(&mocked_options, 0, sizeof(mocked_options));
  239. mocked_options.ClientUseIPv4 = 1;
  240. mocked_options.ClientPreferIPv6ORPort = 0;
  241. mocked_options.UseDeprecatedGuardAlgorithm = 1;
  242. /* Pick an entry. Make sure we pick the node we marked as guard. */
  243. chosen_entry = choose_random_entry(NULL);
  244. tt_ptr_op(chosen_entry, OP_EQ, the_guard);
  245. /* And with the other IP version active */
  246. mocked_options.ClientUseIPv6 = 1;
  247. chosen_entry = choose_random_entry(NULL);
  248. tt_ptr_op(chosen_entry, OP_EQ, the_guard);
  249. /* And with the preference on auto */
  250. mocked_options.ClientPreferIPv6ORPort = -1;
  251. chosen_entry = choose_random_entry(NULL);
  252. tt_ptr_op(chosen_entry, OP_EQ, the_guard);
  253. /* Check that we don't get a guard if it doesn't pass mandatory address
  254. * settings */
  255. memset(&mocked_options, 0, sizeof(mocked_options));
  256. mocked_options.ClientUseIPv4 = 0;
  257. mocked_options.ClientPreferIPv6ORPort = 0;
  258. mocked_options.UseDeprecatedGuardAlgorithm = 1;
  259. chosen_entry = choose_random_entry(NULL);
  260. /* If we don't allow IPv4 at all, we don't get a guard*/
  261. tt_assert(!chosen_entry);
  262. /* Check that we get a node if it passes allowed but not preferred
  263. * address settings */
  264. memset(&mocked_options, 0, sizeof(mocked_options));
  265. mocked_options.ClientUseIPv4 = 1;
  266. mocked_options.ClientUseIPv6 = 1;
  267. mocked_options.ClientPreferIPv6ORPort = 1;
  268. mocked_options.UseDeprecatedGuardAlgorithm = 1;
  269. chosen_entry = choose_random_entry(NULL);
  270. /* We disable the guard check and the preferred address check at the same
  271. * time, so we can't be sure we get the guard */
  272. tt_assert(chosen_entry);
  273. /* Check that we get a node if it is allowed but not preferred when settings
  274. * are auto */
  275. memset(&mocked_options, 0, sizeof(mocked_options));
  276. mocked_options.ClientUseIPv4 = 1;
  277. mocked_options.ClientPreferIPv6ORPort = -1;
  278. mocked_options.UseDeprecatedGuardAlgorithm = 1;
  279. chosen_entry = choose_random_entry(NULL);
  280. /* We disable the guard check and the preferred address check at the same
  281. * time, so we can't be sure we get the guard */
  282. tt_assert(chosen_entry);
  283. /* and with IPv6 active */
  284. mocked_options.ClientUseIPv6 = 1;
  285. chosen_entry = choose_random_entry(NULL);
  286. tt_assert(chosen_entry);
  287. done:
  288. memset(&mocked_options, 0, sizeof(mocked_options));
  289. UNMOCK(get_options);
  290. }
  291. /** Helper to conduct tests for populate_live_entry_guards().
  292. This test adds some entry guards to our list, and then tests
  293. populate_live_entry_guards() to mke sure it filters them correctly.
  294. <b>num_needed</b> is the number of guard nodes we support. It's
  295. configurable to make sure we function properly with 1 or 3 guard
  296. nodes configured.
  297. */
  298. static void
  299. populate_live_entry_guards_test_helper(int num_needed)
  300. {
  301. smartlist_t *our_nodelist = NULL;
  302. smartlist_t *live_entry_guards = smartlist_new();
  303. guard_selection_t *gs = get_guard_selection_info();
  304. const smartlist_t *all_entry_guards =
  305. get_entry_guards_for_guard_selection(gs);
  306. or_options_t *options = get_options_mutable();
  307. int retval;
  308. /* Set NumEntryGuards to the provided number. */
  309. options->NumEntryGuards = num_needed;
  310. tt_int_op(num_needed, OP_EQ, decide_num_guards(options, 0));
  311. /* The global entry guards smartlist should be empty now. */
  312. tt_int_op(smartlist_len(all_entry_guards), OP_EQ, 0);
  313. /* Walk the nodelist and add all nodes as entry guards. */
  314. our_nodelist = nodelist_get_list();
  315. tt_int_op(smartlist_len(our_nodelist), OP_EQ, HELPER_NUMBER_OF_DESCRIPTORS);
  316. SMARTLIST_FOREACH_BEGIN(our_nodelist, const node_t *, node) {
  317. const node_t *node_tmp;
  318. node_tmp = add_an_entry_guard(gs, node, 0, 1, 0, 0);
  319. tt_assert(node_tmp);
  320. } SMARTLIST_FOREACH_END(node);
  321. /* Make sure the nodes were added as entry guards. */
  322. tt_int_op(smartlist_len(all_entry_guards), OP_EQ,
  323. HELPER_NUMBER_OF_DESCRIPTORS);
  324. /* Ensure that all the possible entry guards are enough to satisfy us. */
  325. tt_int_op(smartlist_len(all_entry_guards), OP_GE, num_needed);
  326. /* Walk the entry guard list for some sanity checking */
  327. SMARTLIST_FOREACH_BEGIN(all_entry_guards, const entry_guard_t *, entry) {
  328. /* Since we called add_an_entry_guard() with 'for_discovery' being
  329. False, all guards should have made_contact enabled. */
  330. tt_int_op(entry->made_contact, OP_EQ, 1);
  331. } SMARTLIST_FOREACH_END(entry);
  332. /* First, try to get some fast guards. This should fail. */
  333. retval = populate_live_entry_guards(live_entry_guards,
  334. all_entry_guards,
  335. NULL,
  336. NO_DIRINFO, /* Don't care about DIRINFO*/
  337. 0, 0,
  338. 1); /* We want fast guard! */
  339. tt_int_op(retval, OP_EQ, 0);
  340. tt_int_op(smartlist_len(live_entry_guards), OP_EQ, 0);
  341. /* Now try to get some stable guards. This should fail too. */
  342. retval = populate_live_entry_guards(live_entry_guards,
  343. all_entry_guards,
  344. NULL,
  345. NO_DIRINFO,
  346. 0,
  347. 1, /* We want stable guard! */
  348. 0);
  349. tt_int_op(retval, OP_EQ, 0);
  350. tt_int_op(smartlist_len(live_entry_guards), OP_EQ, 0);
  351. /* Now try to get any guard we can find. This should succeed. */
  352. retval = populate_live_entry_guards(live_entry_guards,
  353. all_entry_guards,
  354. NULL,
  355. NO_DIRINFO,
  356. 0, 0, 0); /* No restrictions! */
  357. /* Since we had more than enough guards in 'all_entry_guards', we
  358. should have added 'num_needed' of them to live_entry_guards.
  359. 'retval' should be 1 since we now have enough live entry guards
  360. to pick one. */
  361. tt_int_op(retval, OP_EQ, 1);
  362. tt_int_op(smartlist_len(live_entry_guards), OP_EQ, num_needed);
  363. done:
  364. smartlist_free(live_entry_guards);
  365. }
  366. /* Test populate_live_entry_guards() for 1 guard node. */
  367. static void
  368. test_populate_live_entry_guards_1guard(void *arg)
  369. {
  370. (void) arg;
  371. populate_live_entry_guards_test_helper(1);
  372. }
  373. /* Test populate_live_entry_guards() for 3 guard nodes. */
  374. static void
  375. test_populate_live_entry_guards_3guards(void *arg)
  376. {
  377. (void) arg;
  378. populate_live_entry_guards_test_helper(3);
  379. }
  380. /** Append some EntryGuard lines to the Tor state at <b>state</b>.
  381. <b>entry_guard_lines</b> is a smartlist containing 2-tuple
  382. smartlists that carry the key and values of the statefile.
  383. As an example:
  384. entry_guard_lines =
  385. (("EntryGuard", "name 67E72FF33D7D41BF11C569646A0A7B4B188340DF DirCache"),
  386. ("EntryGuardDownSince", "2014-06-07 16:02:46 2014-06-07 16:02:46"))
  387. */
  388. static void
  389. state_insert_entry_guard_helper(or_state_t *state,
  390. smartlist_t *entry_guard_lines)
  391. {
  392. config_line_t **next, *line;
  393. next = &state->EntryGuards;
  394. *next = NULL;
  395. /* Loop over all the state lines in the smartlist */
  396. SMARTLIST_FOREACH_BEGIN(entry_guard_lines, const smartlist_t *,state_lines) {
  397. /* Get key and value for each line */
  398. const char *state_key = smartlist_get(state_lines, 0);
  399. const char *state_value = smartlist_get(state_lines, 1);
  400. *next = line = tor_malloc_zero(sizeof(config_line_t));
  401. line->key = tor_strdup(state_key);
  402. tor_asprintf(&line->value, "%s", state_value);
  403. next = &(line->next);
  404. } SMARTLIST_FOREACH_END(state_lines);
  405. }
  406. /** Free memory occupied by <b>entry_guard_lines</b>. */
  407. static void
  408. state_lines_free(smartlist_t *entry_guard_lines)
  409. {
  410. SMARTLIST_FOREACH_BEGIN(entry_guard_lines, smartlist_t *, state_lines) {
  411. char *state_key = smartlist_get(state_lines, 0);
  412. char *state_value = smartlist_get(state_lines, 1);
  413. tor_free(state_key);
  414. tor_free(state_value);
  415. smartlist_free(state_lines);
  416. } SMARTLIST_FOREACH_END(state_lines);
  417. smartlist_free(entry_guard_lines);
  418. }
  419. /* Tests entry_guards_parse_state(). It creates a fake Tor state with
  420. a saved entry guard and makes sure that Tor can parse it and
  421. creates the right entry node out of it.
  422. */
  423. static void
  424. test_entry_guards_parse_state_simple(void *arg)
  425. {
  426. or_state_t *state = or_state_new();
  427. const smartlist_t *all_entry_guards = get_entry_guards();
  428. smartlist_t *entry_state_lines = smartlist_new();
  429. char *msg = NULL;
  430. int retval;
  431. /* Details of our fake guard node */
  432. const char *nickname = "hagbard";
  433. const char *fpr = "B29D536DD1752D542E1FBB3C9CE4449D51298212";
  434. const char *tor_version = "0.2.5.3-alpha-dev";
  435. const char *added_at = get_yesterday_date_str();
  436. const char *unlisted_since = "2014-06-08 16:16:50";
  437. (void) arg;
  438. /* The global entry guards smartlist should be empty now. */
  439. tt_int_op(smartlist_len(all_entry_guards), OP_EQ, 0);
  440. { /* Prepare the state entry */
  441. /* Prepare the smartlist to hold the key/value of each line */
  442. smartlist_t *state_line = smartlist_new();
  443. smartlist_add_asprintf(state_line, "EntryGuard");
  444. smartlist_add_asprintf(state_line, "%s %s %s", nickname, fpr, "DirCache");
  445. smartlist_add(entry_state_lines, state_line);
  446. state_line = smartlist_new();
  447. smartlist_add_asprintf(state_line, "EntryGuardAddedBy");
  448. smartlist_add_asprintf(state_line, "%s %s %s", fpr, tor_version, added_at);
  449. smartlist_add(entry_state_lines, state_line);
  450. state_line = smartlist_new();
  451. smartlist_add_asprintf(state_line, "EntryGuardUnlistedSince");
  452. smartlist_add_asprintf(state_line, "%s", unlisted_since);
  453. smartlist_add(entry_state_lines, state_line);
  454. }
  455. /* Inject our lines in the state */
  456. state_insert_entry_guard_helper(state, entry_state_lines);
  457. /* Parse state */
  458. retval = entry_guards_parse_state(state, 1, &msg);
  459. tt_int_op(retval, OP_GE, 0);
  460. /* Test that the guard was registered.
  461. We need to re-get the entry guard list since its pointer was
  462. overwritten in entry_guards_parse_state(). */
  463. all_entry_guards = get_entry_guards();
  464. tt_int_op(smartlist_len(all_entry_guards), OP_EQ, 1);
  465. { /* Test the entry guard structure */
  466. char hex_digest[1024];
  467. char str_time[1024];
  468. const entry_guard_t *e = smartlist_get(all_entry_guards, 0);
  469. tt_str_op(e->nickname, OP_EQ, nickname); /* Verify nickname */
  470. base16_encode(hex_digest, sizeof(hex_digest),
  471. e->identity, DIGEST_LEN);
  472. tt_str_op(hex_digest, OP_EQ, fpr); /* Verify fingerprint */
  473. tt_assert(e->is_dir_cache); /* Verify dirness */
  474. tt_str_op(e->chosen_by_version, OP_EQ, tor_version); /* Verify version */
  475. tt_assert(e->made_contact); /* All saved guards have been contacted */
  476. tt_assert(e->bad_since); /* Verify bad_since timestamp */
  477. format_iso_time(str_time, e->bad_since);
  478. tt_str_op(str_time, OP_EQ, unlisted_since);
  479. /* The rest should be unset */
  480. tt_assert(!e->unreachable_since);
  481. tt_assert(!e->can_retry);
  482. tt_assert(!e->pb.path_bias_noticed);
  483. tt_assert(!e->pb.path_bias_warned);
  484. tt_assert(!e->pb.path_bias_extreme);
  485. tt_assert(!e->pb.path_bias_disabled);
  486. tt_assert(!e->pb.path_bias_use_noticed);
  487. tt_assert(!e->pb.path_bias_use_extreme);
  488. tt_assert(!e->last_attempted);
  489. }
  490. done:
  491. state_lines_free(entry_state_lines);
  492. or_state_free(state);
  493. tor_free(msg);
  494. }
  495. /** Similar to test_entry_guards_parse_state_simple() but aims to test
  496. the PathBias-related details of the entry guard. */
  497. static void
  498. test_entry_guards_parse_state_pathbias(void *arg)
  499. {
  500. or_state_t *state = or_state_new();
  501. const smartlist_t *all_entry_guards = get_entry_guards();
  502. char *msg = NULL;
  503. int retval;
  504. smartlist_t *entry_state_lines = smartlist_new();
  505. /* Path bias details of the fake guard */
  506. const double circ_attempts = 9;
  507. const double circ_successes = 8;
  508. const double successful_closed = 4;
  509. const double collapsed = 2;
  510. const double unusable = 0;
  511. const double timeouts = 1;
  512. (void) arg;
  513. /* The global entry guards smartlist should be empty now. */
  514. tt_int_op(smartlist_len(all_entry_guards), OP_EQ, 0);
  515. { /* Prepare the state entry */
  516. /* Prepare the smartlist to hold the key/value of each line */
  517. smartlist_t *state_line = smartlist_new();
  518. smartlist_add_asprintf(state_line, "EntryGuard");
  519. smartlist_add_asprintf(state_line,
  520. "givethanks B29D536DD1752D542E1FBB3C9CE4449D51298212 NoDirCache");
  521. smartlist_add(entry_state_lines, state_line);
  522. state_line = smartlist_new();
  523. smartlist_add_asprintf(state_line, "EntryGuardAddedBy");
  524. smartlist_add_asprintf(state_line,
  525. "B29D536DD1752D542E1FBB3C9CE4449D51298212 0.2.5.3-alpha-dev "
  526. "%s", get_yesterday_date_str());
  527. smartlist_add(entry_state_lines, state_line);
  528. state_line = smartlist_new();
  529. smartlist_add_asprintf(state_line, "EntryGuardUnlistedSince");
  530. smartlist_add_asprintf(state_line, "2014-06-08 16:16:50");
  531. smartlist_add(entry_state_lines, state_line);
  532. state_line = smartlist_new();
  533. smartlist_add_asprintf(state_line, "EntryGuardPathBias");
  534. smartlist_add_asprintf(state_line, "%f %f %f %f %f %f",
  535. circ_attempts, circ_successes, successful_closed,
  536. collapsed, unusable, timeouts);
  537. smartlist_add(entry_state_lines, state_line);
  538. }
  539. /* Inject our lines in the state */
  540. state_insert_entry_guard_helper(state, entry_state_lines);
  541. /* Parse state */
  542. retval = entry_guards_parse_state(state, 1, &msg);
  543. tt_int_op(retval, OP_GE, 0);
  544. /* Test that the guard was registered */
  545. all_entry_guards = get_entry_guards();
  546. tt_int_op(smartlist_len(all_entry_guards), OP_EQ, 1);
  547. { /* Test the path bias of this guard */
  548. const entry_guard_t *e = smartlist_get(all_entry_guards, 0);
  549. tt_assert(!e->is_dir_cache);
  550. tt_assert(!e->can_retry);
  551. /* XXX tt_double_op doesn't support equality. Cast to int for now. */
  552. tt_int_op((int)e->pb.circ_attempts, OP_EQ, (int)circ_attempts);
  553. tt_int_op((int)e->pb.circ_successes, OP_EQ, (int)circ_successes);
  554. tt_int_op((int)e->pb.successful_circuits_closed, OP_EQ,
  555. (int)successful_closed);
  556. tt_int_op((int)e->pb.timeouts, OP_EQ, (int)timeouts);
  557. tt_int_op((int)e->pb.collapsed_circuits, OP_EQ, (int)collapsed);
  558. tt_int_op((int)e->pb.unusable_circuits, OP_EQ, (int)unusable);
  559. }
  560. done:
  561. or_state_free(state);
  562. state_lines_free(entry_state_lines);
  563. tor_free(msg);
  564. }
  565. /* Simple test of entry_guards_set_from_config() by specifying a
  566. particular EntryNode and making sure it gets picked. */
  567. static void
  568. test_entry_guards_set_from_config(void *arg)
  569. {
  570. or_options_t *options = get_options_mutable();
  571. options->UseDeprecatedGuardAlgorithm = 1;
  572. guard_selection_t *gs = get_guard_selection_info();
  573. const smartlist_t *all_entry_guards =
  574. get_entry_guards_for_guard_selection(gs);
  575. const char *entrynodes_str = "test003r";
  576. const node_t *chosen_entry = NULL;
  577. int retval;
  578. (void) arg;
  579. /* Prase EntryNodes as a routerset. */
  580. options->EntryNodes = routerset_new();
  581. retval = routerset_parse(options->EntryNodes,
  582. entrynodes_str,
  583. "test_entrynodes");
  584. tt_int_op(retval, OP_GE, 0);
  585. /* Read nodes from EntryNodes */
  586. entry_guards_set_from_config(gs, options);
  587. /* Test that only one guard was added. */
  588. tt_int_op(smartlist_len(all_entry_guards), OP_EQ, 1);
  589. /* Make sure it was the guard we specified. */
  590. chosen_entry = choose_random_entry(NULL);
  591. tt_str_op(chosen_entry->ri->nickname, OP_EQ, entrynodes_str);
  592. done:
  593. routerset_free(options->EntryNodes);
  594. }
  595. static void
  596. test_entry_is_time_to_retry(void *arg)
  597. {
  598. entry_guard_t *test_guard;
  599. time_t now;
  600. int retval;
  601. (void)arg;
  602. now = time(NULL);
  603. test_guard = tor_malloc_zero(sizeof(entry_guard_t));
  604. test_guard->last_attempted = now - 10;
  605. test_guard->unreachable_since = now - 1;
  606. retval = entry_is_time_to_retry(test_guard,now);
  607. tt_int_op(retval,OP_EQ,1);
  608. test_guard->unreachable_since = now - (6*60*60 - 1);
  609. test_guard->last_attempted = now - (60*60 + 1);
  610. retval = entry_is_time_to_retry(test_guard,now);
  611. tt_int_op(retval,OP_EQ,1);
  612. test_guard->last_attempted = now - (60*60 - 1);
  613. retval = entry_is_time_to_retry(test_guard,now);
  614. tt_int_op(retval,OP_EQ,0);
  615. test_guard->unreachable_since = now - (6*60*60 + 1);
  616. test_guard->last_attempted = now - (4*60*60 + 1);
  617. retval = entry_is_time_to_retry(test_guard,now);
  618. tt_int_op(retval,OP_EQ,1);
  619. test_guard->unreachable_since = now - (3*24*60*60 - 1);
  620. test_guard->last_attempted = now - (4*60*60 + 1);
  621. retval = entry_is_time_to_retry(test_guard,now);
  622. tt_int_op(retval,OP_EQ,1);
  623. test_guard->unreachable_since = now - (3*24*60*60 + 1);
  624. test_guard->last_attempted = now - (18*60*60 + 1);
  625. retval = entry_is_time_to_retry(test_guard,now);
  626. tt_int_op(retval,OP_EQ,1);
  627. test_guard->unreachable_since = now - (7*24*60*60 - 1);
  628. test_guard->last_attempted = now - (18*60*60 + 1);
  629. retval = entry_is_time_to_retry(test_guard,now);
  630. tt_int_op(retval,OP_EQ,1);
  631. test_guard->last_attempted = now - (18*60*60 - 1);
  632. retval = entry_is_time_to_retry(test_guard,now);
  633. tt_int_op(retval,OP_EQ,0);
  634. test_guard->unreachable_since = now - (7*24*60*60 + 1);
  635. test_guard->last_attempted = now - (36*60*60 + 1);
  636. retval = entry_is_time_to_retry(test_guard,now);
  637. tt_int_op(retval,OP_EQ,1);
  638. test_guard->unreachable_since = now - (7*24*60*60 + 1);
  639. test_guard->last_attempted = now - (36*60*60 + 1);
  640. retval = entry_is_time_to_retry(test_guard,now);
  641. tt_int_op(retval,OP_EQ,1);
  642. done:
  643. tor_free(test_guard);
  644. }
  645. /** XXX Do some tests that entry_is_live() */
  646. static void
  647. test_entry_is_live(void *arg)
  648. {
  649. smartlist_t *our_nodelist = NULL;
  650. guard_selection_t *gs = get_guard_selection_info();
  651. const smartlist_t *all_entry_guards =
  652. get_entry_guards_for_guard_selection(gs);
  653. const node_t *test_node = NULL;
  654. const entry_guard_t *test_entry = NULL;
  655. const char *msg;
  656. int which_node;
  657. (void) arg;
  658. /* The global entry guards smartlist should be empty now. */
  659. tt_int_op(smartlist_len(all_entry_guards), OP_EQ, 0);
  660. /* Walk the nodelist and add all nodes as entry guards. */
  661. our_nodelist = nodelist_get_list();
  662. tt_int_op(smartlist_len(our_nodelist), OP_EQ, HELPER_NUMBER_OF_DESCRIPTORS);
  663. SMARTLIST_FOREACH_BEGIN(our_nodelist, const node_t *, node) {
  664. const node_t *node_tmp;
  665. node_tmp = add_an_entry_guard(gs, node, 0, 1, 0, 0);
  666. tt_assert(node_tmp);
  667. tt_int_op(node->is_stable, OP_EQ, 0);
  668. tt_int_op(node->is_fast, OP_EQ, 0);
  669. } SMARTLIST_FOREACH_END(node);
  670. /* Make sure the nodes were added as entry guards. */
  671. tt_int_op(smartlist_len(all_entry_guards), OP_EQ,
  672. HELPER_NUMBER_OF_DESCRIPTORS);
  673. /* Now get a random test entry that we will use for this unit test. */
  674. which_node = 3; /* (chosen by fair dice roll) */
  675. test_entry = smartlist_get(all_entry_guards, which_node);
  676. /* Let's do some entry_is_live() tests! */
  677. /* Require the node to be stable, but it's not. Should fail.
  678. Also enable 'assume_reachable' because why not. */
  679. test_node = entry_is_live(test_entry,
  680. ENTRY_NEED_UPTIME | ENTRY_ASSUME_REACHABLE,
  681. &msg);
  682. tt_assert(!test_node);
  683. /* Require the node to be fast, but it's not. Should fail. */
  684. test_node = entry_is_live(test_entry,
  685. ENTRY_NEED_CAPACITY | ENTRY_ASSUME_REACHABLE,
  686. &msg);
  687. tt_assert(!test_node);
  688. /* Don't impose any restrictions on the node. Should succeed. */
  689. test_node = entry_is_live(test_entry, 0, &msg);
  690. tt_assert(test_node);
  691. tt_ptr_op(test_node, OP_EQ, node_get_by_id(test_entry->identity));
  692. /* Require descriptor for this node. It has one so it should succeed. */
  693. test_node = entry_is_live(test_entry, ENTRY_NEED_DESCRIPTOR, &msg);
  694. tt_assert(test_node);
  695. tt_ptr_op(test_node, OP_EQ, node_get_by_id(test_entry->identity));
  696. done:
  697. ; /* XXX */
  698. }
  699. #define TEST_IPV4_ADDR "123.45.67.89"
  700. #define TEST_IPV6_ADDR "[1234:5678:90ab:cdef::]"
  701. static void
  702. test_node_preferred_orport(void *arg)
  703. {
  704. (void)arg;
  705. tor_addr_t ipv4_addr;
  706. const uint16_t ipv4_port = 4444;
  707. tor_addr_t ipv6_addr;
  708. const uint16_t ipv6_port = 6666;
  709. routerinfo_t node_ri;
  710. node_t node;
  711. tor_addr_port_t ap;
  712. /* Setup options */
  713. memset(&mocked_options, 0, sizeof(mocked_options));
  714. /* We don't test ClientPreferIPv6ORPort here, because it's used in
  715. * nodelist_set_consensus to setup node.ipv6_preferred, which we set
  716. * directly. */
  717. MOCK(get_options, mock_get_options);
  718. /* Setup IP addresses */
  719. tor_addr_parse(&ipv4_addr, TEST_IPV4_ADDR);
  720. tor_addr_parse(&ipv6_addr, TEST_IPV6_ADDR);
  721. /* Setup node_ri */
  722. memset(&node_ri, 0, sizeof(node_ri));
  723. node_ri.addr = tor_addr_to_ipv4h(&ipv4_addr);
  724. node_ri.or_port = ipv4_port;
  725. tor_addr_copy(&node_ri.ipv6_addr, &ipv6_addr);
  726. node_ri.ipv6_orport = ipv6_port;
  727. /* Setup node */
  728. memset(&node, 0, sizeof(node));
  729. node.ri = &node_ri;
  730. /* Check the preferred address is IPv4 if we're only using IPv4, regardless
  731. * of whether we prefer it or not */
  732. mocked_options.ClientUseIPv4 = 1;
  733. mocked_options.ClientUseIPv6 = 0;
  734. node.ipv6_preferred = 0;
  735. node_get_pref_orport(&node, &ap);
  736. tt_assert(tor_addr_eq(&ap.addr, &ipv4_addr));
  737. tt_assert(ap.port == ipv4_port);
  738. node.ipv6_preferred = 1;
  739. node_get_pref_orport(&node, &ap);
  740. tt_assert(tor_addr_eq(&ap.addr, &ipv4_addr));
  741. tt_assert(ap.port == ipv4_port);
  742. /* Check the preferred address is IPv4 if we're using IPv4 and IPv6, but
  743. * don't prefer the IPv6 address */
  744. mocked_options.ClientUseIPv4 = 1;
  745. mocked_options.ClientUseIPv6 = 1;
  746. node.ipv6_preferred = 0;
  747. node_get_pref_orport(&node, &ap);
  748. tt_assert(tor_addr_eq(&ap.addr, &ipv4_addr));
  749. tt_assert(ap.port == ipv4_port);
  750. /* Check the preferred address is IPv6 if we prefer it and
  751. * ClientUseIPv6 is 1, regardless of ClientUseIPv4 */
  752. mocked_options.ClientUseIPv4 = 1;
  753. mocked_options.ClientUseIPv6 = 1;
  754. node.ipv6_preferred = 1;
  755. node_get_pref_orport(&node, &ap);
  756. tt_assert(tor_addr_eq(&ap.addr, &ipv6_addr));
  757. tt_assert(ap.port == ipv6_port);
  758. mocked_options.ClientUseIPv4 = 0;
  759. node_get_pref_orport(&node, &ap);
  760. tt_assert(tor_addr_eq(&ap.addr, &ipv6_addr));
  761. tt_assert(ap.port == ipv6_port);
  762. /* Check the preferred address is IPv6 if we don't prefer it, but
  763. * ClientUseIPv4 is 0 */
  764. mocked_options.ClientUseIPv4 = 0;
  765. mocked_options.ClientUseIPv6 = 1;
  766. node.ipv6_preferred = fascist_firewall_prefer_ipv6_orport(&mocked_options);
  767. node_get_pref_orport(&node, &ap);
  768. tt_assert(tor_addr_eq(&ap.addr, &ipv6_addr));
  769. tt_assert(ap.port == ipv6_port);
  770. done:
  771. UNMOCK(get_options);
  772. }
  773. static void
  774. test_entry_guard_describe(void *arg)
  775. {
  776. (void)arg;
  777. entry_guard_t g;
  778. memset(&g, 0, sizeof(g));
  779. strlcpy(g.nickname, "okefenokee", sizeof(g.nickname));
  780. memcpy(g.identity, "theforestprimeval---", DIGEST_LEN);
  781. tt_str_op(entry_guard_describe(&g), OP_EQ,
  782. "okefenokee ($746865666F726573747072696D6576616C2D2D2D)");
  783. done:
  784. ;
  785. }
  786. static void
  787. test_entry_guard_randomize_time(void *arg)
  788. {
  789. const time_t now = 1479153573;
  790. const int delay = 86400;
  791. const int N = 1000;
  792. (void)arg;
  793. time_t t;
  794. int i;
  795. for (i = 0; i < N; ++i) {
  796. t = randomize_time(now, delay);
  797. tt_int_op(t, OP_LE, now);
  798. tt_int_op(t, OP_GE, now-delay);
  799. }
  800. /* now try the corner cases */
  801. for (i = 0; i < N; ++i) {
  802. t = randomize_time(100, delay);
  803. tt_int_op(t, OP_GE, 1);
  804. tt_int_op(t, OP_LE, 100);
  805. t = randomize_time(0, delay);
  806. tt_int_op(t, OP_EQ, 1);
  807. }
  808. done:
  809. ;
  810. }
  811. static void
  812. test_entry_guard_encode_for_state_minimal(void *arg)
  813. {
  814. (void) arg;
  815. entry_guard_t *eg = tor_malloc_zero(sizeof(entry_guard_t));
  816. memcpy(eg->identity, "plurpyflurpyslurpydo", DIGEST_LEN);
  817. eg->sampled_on_date = 1479081600;
  818. eg->confirmed_idx = -1;
  819. char *s = NULL;
  820. s = entry_guard_encode_for_state(eg);
  821. tt_str_op(s, OP_EQ,
  822. "rsa_id=706C75727079666C75727079736C75727079646F "
  823. "sampled_on=2016-11-14T00:00:00 "
  824. "listed=0");
  825. done:
  826. entry_guard_free(eg);
  827. tor_free(s);
  828. }
  829. static void
  830. test_entry_guard_encode_for_state_maximal(void *arg)
  831. {
  832. (void) arg;
  833. entry_guard_t *eg = tor_malloc_zero(sizeof(entry_guard_t));
  834. strlcpy(eg->nickname, "Fred", sizeof(eg->nickname));
  835. memcpy(eg->identity, "plurpyflurpyslurpydo", DIGEST_LEN);
  836. eg->sampled_on_date = 1479081600;
  837. eg->sampled_by_version = tor_strdup("1.2.3");
  838. eg->unlisted_since_date = 1479081645;
  839. eg->currently_listed = 1;
  840. eg->confirmed_on_date = 1479081690;
  841. eg->confirmed_idx = 333;
  842. eg->extra_state_fields = tor_strdup("and the green grass grew all around");
  843. char *s = NULL;
  844. s = entry_guard_encode_for_state(eg);
  845. tt_str_op(s, OP_EQ,
  846. "rsa_id=706C75727079666C75727079736C75727079646F "
  847. "nickname=Fred "
  848. "sampled_on=2016-11-14T00:00:00 "
  849. "sampled_by=1.2.3 "
  850. "unlisted_since=2016-11-14T00:00:45 "
  851. "listed=1 "
  852. "confirmed_on=2016-11-14T00:01:30 "
  853. "confirmed_idx=333 "
  854. "and the green grass grew all around");
  855. done:
  856. entry_guard_free(eg);
  857. tor_free(s);
  858. }
  859. static void
  860. test_entry_guard_parse_from_state_minimal(void *arg)
  861. {
  862. (void)arg;
  863. char *mem_op_hex_tmp = NULL;
  864. entry_guard_t *eg = NULL;
  865. time_t t = approx_time();
  866. eg = entry_guard_parse_from_state(
  867. "rsa_id=596f75206d6179206e656564206120686f626279");
  868. tt_assert(eg);
  869. test_mem_op_hex(eg->identity, OP_EQ,
  870. "596f75206d6179206e656564206120686f626279");
  871. tt_str_op(eg->nickname, OP_EQ, "$596F75206D6179206E656564206120686F626279");
  872. tt_i64_op(eg->sampled_on_date, OP_GE, t);
  873. tt_i64_op(eg->sampled_on_date, OP_LE, t+86400);
  874. tt_i64_op(eg->unlisted_since_date, OP_EQ, 0);
  875. tt_ptr_op(eg->sampled_by_version, OP_EQ, NULL);
  876. tt_int_op(eg->currently_listed, OP_EQ, 0);
  877. tt_i64_op(eg->confirmed_on_date, OP_EQ, 0);
  878. tt_int_op(eg->confirmed_idx, OP_EQ, -1);
  879. tt_int_op(eg->last_tried_to_connect, OP_EQ, 0);
  880. tt_int_op(eg->is_reachable, OP_EQ, GUARD_REACHABLE_MAYBE);
  881. done:
  882. entry_guard_free(eg);
  883. tor_free(mem_op_hex_tmp);
  884. }
  885. static void
  886. test_entry_guard_parse_from_state_maximal(void *arg)
  887. {
  888. (void)arg;
  889. char *mem_op_hex_tmp = NULL;
  890. entry_guard_t *eg = NULL;
  891. eg = entry_guard_parse_from_state(
  892. "rsa_id=706C75727079666C75727079736C75727079646F "
  893. "nickname=Fred "
  894. "sampled_on=2016-11-14T00:00:00 "
  895. "sampled_by=1.2.3 "
  896. "unlisted_since=2016-11-14T00:00:45 "
  897. "listed=1 "
  898. "confirmed_on=2016-11-14T00:01:30 "
  899. "confirmed_idx=333 "
  900. "and the green grass grew all around "
  901. "rsa_id=all,around");
  902. tt_assert(eg);
  903. test_mem_op_hex(eg->identity, OP_EQ,
  904. "706C75727079666C75727079736C75727079646F");
  905. tt_str_op(eg->nickname, OP_EQ, "Fred");
  906. tt_i64_op(eg->sampled_on_date, OP_EQ, 1479081600);
  907. tt_i64_op(eg->unlisted_since_date, OP_EQ, 1479081645);
  908. tt_str_op(eg->sampled_by_version, OP_EQ, "1.2.3");
  909. tt_int_op(eg->currently_listed, OP_EQ, 1);
  910. tt_i64_op(eg->confirmed_on_date, OP_EQ, 1479081690);
  911. tt_int_op(eg->confirmed_idx, OP_EQ, 333);
  912. tt_str_op(eg->extra_state_fields, OP_EQ,
  913. "and the green grass grew all around rsa_id=all,around");
  914. tt_int_op(eg->last_tried_to_connect, OP_EQ, 0);
  915. tt_int_op(eg->is_reachable, OP_EQ, GUARD_REACHABLE_MAYBE);
  916. done:
  917. entry_guard_free(eg);
  918. tor_free(mem_op_hex_tmp);
  919. }
  920. static void
  921. test_entry_guard_parse_from_state_failure(void *arg)
  922. {
  923. (void)arg;
  924. entry_guard_t *eg = NULL;
  925. /* no RSA ID. */
  926. eg = entry_guard_parse_from_state("nickname=Fred");
  927. tt_assert(! eg);
  928. /* Bad RSA ID: bad character. */
  929. eg = entry_guard_parse_from_state(
  930. "rsa_id=596f75206d6179206e656564206120686f62627q");
  931. tt_assert(! eg);
  932. /* Bad RSA ID: too long.*/
  933. eg = entry_guard_parse_from_state(
  934. "rsa_id=596f75206d6179206e656564206120686f6262703");
  935. tt_assert(! eg);
  936. /* Bad RSA ID: too short.*/
  937. eg = entry_guard_parse_from_state(
  938. "rsa_id=596f75206d6179206e65656420612");
  939. tt_assert(! eg);
  940. done:
  941. entry_guard_free(eg);
  942. }
  943. static void
  944. test_entry_guard_parse_from_state_partial_failure(void *arg)
  945. {
  946. (void)arg;
  947. char *mem_op_hex_tmp = NULL;
  948. entry_guard_t *eg = NULL;
  949. time_t t = approx_time();
  950. eg = entry_guard_parse_from_state(
  951. "rsa_id=706C75727079666C75727079736C75727079646F "
  952. "nickname=FredIsANodeWithAStrangeNicknameThatIsTooLong "
  953. "sampled_on=2016-11-14T00:00:99 "
  954. "sampled_by=1.2.3 stuff in the middle "
  955. "unlisted_since=2016-xx-14T00:00:45 "
  956. "listed=0 "
  957. "confirmed_on=2016-11-14T00:01:30zz "
  958. "confirmed_idx=idx "
  959. "and the green grass grew all around "
  960. "rsa_id=all,around");
  961. tt_assert(eg);
  962. test_mem_op_hex(eg->identity, OP_EQ,
  963. "706C75727079666C75727079736C75727079646F");
  964. tt_str_op(eg->nickname, OP_EQ, "FredIsANodeWithAStrangeNicknameThatIsTooL");
  965. tt_i64_op(eg->sampled_on_date, OP_EQ, t);
  966. tt_i64_op(eg->unlisted_since_date, OP_EQ, 0);
  967. tt_str_op(eg->sampled_by_version, OP_EQ, "1.2.3");
  968. tt_int_op(eg->currently_listed, OP_EQ, 0);
  969. tt_i64_op(eg->confirmed_on_date, OP_EQ, 0);
  970. tt_int_op(eg->confirmed_idx, OP_EQ, -1);
  971. tt_str_op(eg->extra_state_fields, OP_EQ,
  972. "stuff in the middle and the green grass grew all around "
  973. "rsa_id=all,around");
  974. tt_int_op(eg->last_tried_to_connect, OP_EQ, 0);
  975. tt_int_op(eg->is_reachable, OP_EQ, GUARD_REACHABLE_MAYBE);
  976. done:
  977. entry_guard_free(eg);
  978. tor_free(mem_op_hex_tmp);
  979. }
  980. static void
  981. test_entry_guard_add_single_guard(void *arg)
  982. {
  983. (void)arg;
  984. guard_selection_t *gs = guard_selection_new();
  985. /* 1: Add a single guard to the sample. */
  986. node_t *n1 = smartlist_get(big_fake_net_nodes, 0);
  987. time_t now = approx_time();
  988. tt_assert(n1->is_possible_guard == 1);
  989. entry_guard_t *g1 = entry_guard_add_to_sample(gs, n1);
  990. tt_assert(g1);
  991. /* Make sure its fields look right. */
  992. tt_mem_op(n1->identity, OP_EQ, g1->identity, DIGEST_LEN);
  993. tt_i64_op(g1->sampled_on_date, OP_GE, now - 12*86400);
  994. tt_i64_op(g1->sampled_on_date, OP_LE, now);
  995. tt_str_op(g1->sampled_by_version, OP_EQ, VERSION);
  996. tt_assert(g1->currently_listed == 1);
  997. tt_i64_op(g1->confirmed_on_date, OP_EQ, 0);
  998. tt_int_op(g1->confirmed_idx, OP_EQ, -1);
  999. tt_int_op(g1->last_tried_to_connect, OP_EQ, 0);
  1000. tt_uint_op(g1->is_reachable, OP_EQ, GUARD_REACHABLE_MAYBE);
  1001. tt_i64_op(g1->failing_since, OP_EQ, 0);
  1002. tt_assert(g1->is_filtered_guard == 1);
  1003. tt_assert(g1->is_usable_filtered_guard == 1);
  1004. tt_assert(g1->is_primary == 0);
  1005. tt_assert(g1->extra_state_fields == NULL);
  1006. /* Make sure it got added. */
  1007. tt_int_op(1, OP_EQ, smartlist_len(gs->sampled_entry_guards));
  1008. tt_ptr_op(g1, OP_EQ, smartlist_get(gs->sampled_entry_guards, 0));
  1009. tt_ptr_op(g1, OP_EQ, get_sampled_guard_with_id(gs, (uint8_t*)n1->identity));
  1010. const uint8_t bad_id[20] = {0};
  1011. tt_ptr_op(NULL, OP_EQ, get_sampled_guard_with_id(gs, bad_id));
  1012. done:
  1013. guard_selection_free(gs);
  1014. }
  1015. static void
  1016. test_entry_guard_node_filter(void *arg)
  1017. {
  1018. (void)arg;
  1019. guard_selection_t *gs = guard_selection_new();
  1020. bridge_line_t *bl = NULL;
  1021. /* Initialize a bunch of node objects that are all guards. */
  1022. const int NUM = 7;
  1023. node_t *n[NUM];
  1024. entry_guard_t *g[NUM];
  1025. int i;
  1026. for (i=0; i < NUM; ++i) {
  1027. n[i] = smartlist_get(big_fake_net_nodes, i*2); // even ones are guards.
  1028. g[i] = entry_guard_add_to_sample(gs, n[i]);
  1029. // everything starts out filtered-in
  1030. tt_assert(g[i]->is_filtered_guard == 1);
  1031. tt_assert(g[i]->is_usable_filtered_guard == 1);
  1032. }
  1033. tt_int_op(num_reachable_filtered_guards(gs), OP_EQ, NUM);
  1034. /* Make sure refiltering doesn't hurt */
  1035. entry_guards_update_filtered_sets(gs);
  1036. for (i = 0; i < NUM; ++i) {
  1037. tt_assert(g[i]->is_filtered_guard == 1);
  1038. tt_assert(g[i]->is_usable_filtered_guard == 1);
  1039. }
  1040. tt_int_op(num_reachable_filtered_guards(gs), OP_EQ, NUM);
  1041. /* Now start doing things to make the guards get filtered out, 1 by 1. */
  1042. /* 0: Not listed. */
  1043. g[0]->currently_listed = 0;
  1044. /* 1: path bias says this guard is maybe eeeevil. */
  1045. g[1]->pb.path_bias_disabled = 1;
  1046. /* 2: Unreachable address. */
  1047. n[2]->rs->addr = 0;
  1048. /* 3: ExcludeNodes */
  1049. n[3]->rs->addr = 0x90902020;
  1050. routerset_free(get_options_mutable()->ExcludeNodes);
  1051. get_options_mutable()->ExcludeNodes = routerset_new();
  1052. routerset_parse(get_options_mutable()->ExcludeNodes, "144.144.0.0/16", "");
  1053. /* 4: Bridge. */
  1054. sweep_bridge_list();
  1055. bl = tor_malloc_zero(sizeof(bridge_line_t));
  1056. tor_addr_from_ipv4h(&bl->addr, n[4]->rs->addr);
  1057. bl->port = n[4]->rs->or_port;
  1058. memcpy(bl->digest, n[4]->identity, 20);
  1059. bridge_add_from_config(bl);
  1060. bl = NULL; // prevent free.
  1061. /* 5: Unreachable. This stays in the filter, but isn't in usable-filtered */
  1062. g[5]->last_tried_to_connect = approx_time(); // prevent retry.
  1063. g[5]->is_reachable = GUARD_REACHABLE_NO;
  1064. /* 6: no change. */
  1065. /* Now refilter and inspect. */
  1066. entry_guards_update_filtered_sets(gs);
  1067. for (i = 0; i < NUM; ++i) {
  1068. tt_assert(g[i]->is_filtered_guard == (i == 5 || i == 6));
  1069. tt_assert(g[i]->is_usable_filtered_guard == (i == 6));
  1070. }
  1071. tt_int_op(num_reachable_filtered_guards(gs), OP_EQ, 1);
  1072. done:
  1073. guard_selection_free(gs);
  1074. tor_free(bl);
  1075. }
  1076. static void
  1077. test_entry_guard_expand_sample(void *arg)
  1078. {
  1079. (void)arg;
  1080. guard_selection_t *gs = guard_selection_new();
  1081. digestmap_t *node_by_id = digestmap_new();
  1082. entry_guard_t *guard = entry_guards_expand_sample(gs);
  1083. tt_assert(guard); // the last guard returned.
  1084. // Every sampled guard here should be filtered and reachable for now.
  1085. tt_int_op(smartlist_len(gs->sampled_entry_guards), OP_EQ,
  1086. num_reachable_filtered_guards(gs));
  1087. /* Make sure we got the right number. */
  1088. tt_int_op(MIN_FILTERED_SAMPLE_SIZE, OP_EQ,
  1089. num_reachable_filtered_guards(gs));
  1090. // Make sure everything we got was from our fake node list, and everything
  1091. // was unique.
  1092. SMARTLIST_FOREACH_BEGIN(gs->sampled_entry_guards, entry_guard_t *, g) {
  1093. const node_t *n = bfn_mock_node_get_by_id(g->identity);
  1094. tt_assert(n);
  1095. tt_ptr_op(NULL, OP_EQ, digestmap_get(node_by_id, g->identity));
  1096. digestmap_set(node_by_id, g->identity, (void*) n);
  1097. int idx = smartlist_pos(big_fake_net_nodes, n);
  1098. // The even ones are the guards; make sure we got guards.
  1099. tt_int_op(idx & 1, OP_EQ, 0);
  1100. } SMARTLIST_FOREACH_END(g);
  1101. // Nothing became unusable/unfiltered, so a subsequent expand should
  1102. // make no changes.
  1103. guard = entry_guards_expand_sample(gs);
  1104. tt_assert(! guard); // no guard was added.
  1105. tt_int_op(MIN_FILTERED_SAMPLE_SIZE, OP_EQ,
  1106. num_reachable_filtered_guards(gs));
  1107. // Make a few guards unreachable.
  1108. guard = smartlist_get(gs->sampled_entry_guards, 0);
  1109. guard->is_usable_filtered_guard = 0;
  1110. guard = smartlist_get(gs->sampled_entry_guards, 1);
  1111. guard->is_usable_filtered_guard = 0;
  1112. guard = smartlist_get(gs->sampled_entry_guards, 2);
  1113. guard->is_usable_filtered_guard = 0;
  1114. tt_int_op(MIN_FILTERED_SAMPLE_SIZE - 3, OP_EQ,
  1115. num_reachable_filtered_guards(gs));
  1116. // This time, expanding the sample will add some more guards.
  1117. guard = entry_guards_expand_sample(gs);
  1118. tt_assert(guard); // no guard was added.
  1119. tt_int_op(MIN_FILTERED_SAMPLE_SIZE, OP_EQ,
  1120. num_reachable_filtered_guards(gs));
  1121. tt_int_op(smartlist_len(gs->sampled_entry_guards), OP_EQ,
  1122. num_reachable_filtered_guards(gs)+3);
  1123. // Still idempotent.
  1124. guard = entry_guards_expand_sample(gs);
  1125. tt_assert(! guard); // no guard was added.
  1126. tt_int_op(MIN_FILTERED_SAMPLE_SIZE, OP_EQ,
  1127. num_reachable_filtered_guards(gs));
  1128. // Now, do a nasty trick: tell the filter to exclude 31/32 of the guards.
  1129. // This will cause the sample size to get reeeeally huge, while the
  1130. // filtered sample size grows only slowly.
  1131. routerset_free(get_options_mutable()->ExcludeNodes);
  1132. get_options_mutable()->ExcludeNodes = routerset_new();
  1133. routerset_parse(get_options_mutable()->ExcludeNodes, "144.144.0.0/16", "");
  1134. SMARTLIST_FOREACH(big_fake_net_nodes, node_t *, n, {
  1135. if (n_sl_idx % 64 != 0) {
  1136. n->rs->addr = 0x90903030;
  1137. }
  1138. });
  1139. entry_guards_update_filtered_sets(gs);
  1140. // Surely (p ~ 1-2**-60), one of our guards has been excluded.
  1141. tt_int_op(num_reachable_filtered_guards(gs), OP_LT,
  1142. MIN_FILTERED_SAMPLE_SIZE);
  1143. // Try to regenerate the guards.
  1144. guard = entry_guards_expand_sample(gs);
  1145. tt_assert(guard); // no guard was added.
  1146. /* this time, it's possible that we didn't add enough sampled guards. */
  1147. tt_int_op(num_reachable_filtered_guards(gs), OP_LE,
  1148. MIN_FILTERED_SAMPLE_SIZE);
  1149. /* but we definitely didn't exceed the sample maximum. */
  1150. tt_int_op(smartlist_len(gs->sampled_entry_guards), OP_LE,
  1151. (int)((271 / 2) * .3));
  1152. done:
  1153. guard_selection_free(gs);
  1154. digestmap_free(node_by_id, NULL);
  1155. }
  1156. static void
  1157. test_entry_guard_expand_sample_small_net(void *arg)
  1158. {
  1159. (void)arg;
  1160. guard_selection_t *gs = guard_selection_new();
  1161. /* Fun corner case: not enough guards to make up our whole sample size. */
  1162. SMARTLIST_FOREACH(big_fake_net_nodes, node_t *, n, {
  1163. if (n_sl_idx >= 40) {
  1164. tor_free(n->rs);
  1165. tor_free(n->md);
  1166. tor_free(n);
  1167. SMARTLIST_DEL_CURRENT(big_fake_net_nodes, n);
  1168. } else {
  1169. n->rs->addr = 0; // make the filter reject this.
  1170. }
  1171. });
  1172. entry_guard_t *guard = entry_guards_expand_sample(gs);
  1173. tt_assert(guard); // the last guard returned -- some guard was added.
  1174. tt_int_op(smartlist_len(gs->sampled_entry_guards), OP_GT, 0);
  1175. tt_int_op(smartlist_len(gs->sampled_entry_guards), OP_LT, 10);
  1176. tt_int_op(num_reachable_filtered_guards(gs), OP_EQ, 0);
  1177. done:
  1178. guard_selection_free(gs);
  1179. }
  1180. static void
  1181. test_entry_guard_update_from_consensus_status(void *arg)
  1182. {
  1183. /* Here we're going to have some nodes become un-guardy, and say we got a
  1184. * new consensus. This should cause those nodes to get detected as
  1185. * unreachable. */
  1186. (void)arg;
  1187. int i;
  1188. time_t start = approx_time();
  1189. guard_selection_t *gs = guard_selection_new();
  1190. networkstatus_t *ns_tmp = NULL;
  1191. /* Don't randomly backdate stuff; it will make correctness harder to check.*/
  1192. MOCK(randomize_time, mock_randomize_time_no_randomization);
  1193. /* First, sample some guards. */
  1194. entry_guards_expand_sample(gs);
  1195. int n_sampled_pre = smartlist_len(gs->sampled_entry_guards);
  1196. int n_filtered_pre = num_reachable_filtered_guards(gs);
  1197. tt_i64_op(n_sampled_pre, OP_EQ, n_filtered_pre);
  1198. tt_i64_op(n_sampled_pre, OP_GT, 10);
  1199. /* At this point, it should be a no-op to do this: */
  1200. sampled_guards_update_from_consensus(gs);
  1201. /* Now let's make some of our guards become unlisted. The easiest way to
  1202. * do that would be to take away their guard flag. */
  1203. for (i = 0; i < 5; ++i) {
  1204. entry_guard_t *g = smartlist_get(gs->sampled_entry_guards, i);
  1205. node_t *n = (node_t*) bfn_mock_node_get_by_id(g->identity);
  1206. n->is_possible_guard = 0;
  1207. }
  1208. update_approx_time(start + 30);
  1209. {
  1210. /* try this with no live networkstatus. Nothing should happen! */
  1211. ns_tmp = dummy_consensus;
  1212. dummy_consensus = NULL;
  1213. sampled_guards_update_from_consensus(gs);
  1214. tt_i64_op(smartlist_len(gs->sampled_entry_guards), OP_EQ, n_sampled_pre);
  1215. tt_i64_op(num_reachable_filtered_guards(gs), OP_EQ, n_filtered_pre);
  1216. /* put the networkstatus back. */
  1217. dummy_consensus = ns_tmp;
  1218. ns_tmp = NULL;
  1219. }
  1220. /* Now those guards should become unlisted, and drop off the filter, but
  1221. * stay in the sample. */
  1222. update_approx_time(start + 60);
  1223. sampled_guards_update_from_consensus(gs);
  1224. tt_i64_op(smartlist_len(gs->sampled_entry_guards), OP_EQ, n_sampled_pre);
  1225. tt_i64_op(num_reachable_filtered_guards(gs), OP_EQ, n_filtered_pre - 5);
  1226. for (i = 0; i < 5; ++i) {
  1227. entry_guard_t *g = smartlist_get(gs->sampled_entry_guards, i);
  1228. tt_assert(! g->currently_listed);
  1229. tt_i64_op(g->unlisted_since_date, OP_EQ, start+60);
  1230. }
  1231. for (i = 5; i < n_sampled_pre; ++i) {
  1232. entry_guard_t *g = smartlist_get(gs->sampled_entry_guards, i);
  1233. tt_assert(g->currently_listed);
  1234. tt_i64_op(g->unlisted_since_date, OP_EQ, 0);
  1235. }
  1236. /* Now re-list one, and remove one completely. */
  1237. {
  1238. entry_guard_t *g = smartlist_get(gs->sampled_entry_guards, 0);
  1239. node_t *n = (node_t*) bfn_mock_node_get_by_id(g->identity);
  1240. n->is_possible_guard = 1;
  1241. }
  1242. {
  1243. /* try removing the node, to make sure we don't crash on an absent node
  1244. */
  1245. entry_guard_t *g = smartlist_get(gs->sampled_entry_guards, 5);
  1246. node_t *n = (node_t*) bfn_mock_node_get_by_id(g->identity);
  1247. smartlist_remove(big_fake_net_nodes, n);
  1248. tor_free(n->rs);
  1249. tor_free(n->md);
  1250. tor_free(n);
  1251. }
  1252. update_approx_time(start + 300);
  1253. sampled_guards_update_from_consensus(gs);
  1254. /* guards 1..5 are now unlisted; 0,6,7.. are listed. */
  1255. tt_i64_op(smartlist_len(gs->sampled_entry_guards), OP_EQ, n_sampled_pre);
  1256. for (i = 1; i < 6; ++i) {
  1257. entry_guard_t *g = smartlist_get(gs->sampled_entry_guards, i);
  1258. tt_assert(! g->currently_listed);
  1259. if (i == 5)
  1260. tt_i64_op(g->unlisted_since_date, OP_EQ, start+300);
  1261. else
  1262. tt_i64_op(g->unlisted_since_date, OP_EQ, start+60);
  1263. }
  1264. for (i = 0; i < n_sampled_pre; i = (!i) ? 6 : i+1) { /* 0,6,7,8, ... */
  1265. entry_guard_t *g = smartlist_get(gs->sampled_entry_guards, i);
  1266. tt_assert(g->currently_listed);
  1267. tt_i64_op(g->unlisted_since_date, OP_EQ, 0);
  1268. }
  1269. done:
  1270. tor_free(ns_tmp); /* in case we couldn't put it back */
  1271. guard_selection_free(gs);
  1272. UNMOCK(randomize_time);
  1273. }
  1274. static void
  1275. test_entry_guard_update_from_consensus_repair(void *arg)
  1276. {
  1277. /* Here we'll make sure that our code to repair the unlisted-since
  1278. * times is correct. */
  1279. (void)arg;
  1280. int i;
  1281. time_t start = approx_time();
  1282. guard_selection_t *gs = guard_selection_new();
  1283. /* Don't randomly backdate stuff; it will make correctness harder to check.*/
  1284. MOCK(randomize_time, mock_randomize_time_no_randomization);
  1285. /* First, sample some guards. */
  1286. entry_guards_expand_sample(gs);
  1287. int n_sampled_pre = smartlist_len(gs->sampled_entry_guards);
  1288. int n_filtered_pre = num_reachable_filtered_guards(gs);
  1289. tt_i64_op(n_sampled_pre, OP_EQ, n_filtered_pre);
  1290. tt_i64_op(n_sampled_pre, OP_GT, 10);
  1291. /* Now corrupt the list a bit. Call some unlisted-since-never, and some
  1292. * listed-and-unlisted-since-a-time. */
  1293. update_approx_time(start + 300);
  1294. for (i = 0; i < 3; ++i) {
  1295. /* these will get a date. */
  1296. entry_guard_t *g = smartlist_get(gs->sampled_entry_guards, i);
  1297. node_t *n = (node_t*) bfn_mock_node_get_by_id(g->identity);
  1298. n->is_possible_guard = 0;
  1299. g->currently_listed = 0;
  1300. }
  1301. for (i = 3; i < 6; ++i) {
  1302. /* these will become listed. */
  1303. entry_guard_t *g = smartlist_get(gs->sampled_entry_guards, i);
  1304. g->unlisted_since_date = start+100;
  1305. }
  1306. setup_full_capture_of_logs(LOG_WARN);
  1307. sampled_guards_update_from_consensus(gs);
  1308. expect_log_msg_containing(
  1309. "was listed, but with unlisted_since_date set");
  1310. expect_log_msg_containing(
  1311. "was unlisted, but with unlisted_since_date unset");
  1312. teardown_capture_of_logs();
  1313. tt_int_op(smartlist_len(gs->sampled_entry_guards), OP_EQ, n_sampled_pre);
  1314. tt_int_op(num_reachable_filtered_guards(gs), OP_EQ, n_filtered_pre - 3);
  1315. for (i = 3; i < n_sampled_pre; ++i) {
  1316. /* these will become listed. */
  1317. entry_guard_t *g = smartlist_get(gs->sampled_entry_guards, i);
  1318. if (i < 3) {
  1319. tt_assert(! g->currently_listed);
  1320. tt_i64_op(g->unlisted_since_date, OP_EQ, start+300);
  1321. } else {
  1322. tt_assert(g->currently_listed);
  1323. tt_i64_op(g->unlisted_since_date, OP_EQ, 0);
  1324. }
  1325. }
  1326. done:
  1327. teardown_capture_of_logs();
  1328. guard_selection_free(gs);
  1329. UNMOCK(randomize_time);
  1330. }
  1331. static void
  1332. test_entry_guard_update_from_consensus_remove(void *arg)
  1333. {
  1334. /* Now let's check the logic responsible for removing guards from the
  1335. * sample entirely. */
  1336. (void)arg;
  1337. //int i;
  1338. guard_selection_t *gs = guard_selection_new();
  1339. smartlist_t *keep_ids = smartlist_new();
  1340. smartlist_t *remove_ids = smartlist_new();
  1341. /* Don't randomly backdate stuff; it will make correctness harder to check.*/
  1342. MOCK(randomize_time, mock_randomize_time_no_randomization);
  1343. /* First, sample some guards. */
  1344. entry_guards_expand_sample(gs);
  1345. int n_sampled_pre = smartlist_len(gs->sampled_entry_guards);
  1346. int n_filtered_pre = num_reachable_filtered_guards(gs);
  1347. tt_i64_op(n_sampled_pre, OP_EQ, n_filtered_pre);
  1348. tt_i64_op(n_sampled_pre, OP_GT, 10);
  1349. const time_t one_day_ago = approx_time() - 1*24*60*60;
  1350. const time_t one_year_ago = approx_time() - 365*24*60*60;
  1351. const time_t two_years_ago = approx_time() - 2*365*24*60*60;
  1352. /* 0: unlisted for a day. (keep this) */
  1353. {
  1354. entry_guard_t *g = smartlist_get(gs->sampled_entry_guards, 0);
  1355. node_t *n = (node_t*) bfn_mock_node_get_by_id(g->identity);
  1356. n->is_possible_guard = 0;
  1357. g->currently_listed = 0;
  1358. g->unlisted_since_date = one_day_ago;
  1359. smartlist_add(keep_ids, tor_memdup(g->identity, 20));
  1360. }
  1361. /* 1: unlisted for a year. (remove this) */
  1362. {
  1363. entry_guard_t *g = smartlist_get(gs->sampled_entry_guards, 1);
  1364. node_t *n = (node_t*) bfn_mock_node_get_by_id(g->identity);
  1365. n->is_possible_guard = 0;
  1366. g->currently_listed = 0;
  1367. g->unlisted_since_date = one_year_ago;
  1368. smartlist_add(remove_ids, tor_memdup(g->identity, 20));
  1369. }
  1370. /* 2: added a day ago, never confirmed. (keep this) */
  1371. {
  1372. entry_guard_t *g = smartlist_get(gs->sampled_entry_guards, 2);
  1373. g->sampled_on_date = one_day_ago;
  1374. smartlist_add(keep_ids, tor_memdup(g->identity, 20));
  1375. }
  1376. /* 3: added a year ago, never confirmed. (remove this) */
  1377. {
  1378. entry_guard_t *g = smartlist_get(gs->sampled_entry_guards, 3);
  1379. g->sampled_on_date = one_year_ago;
  1380. smartlist_add(remove_ids, tor_memdup(g->identity, 20));
  1381. }
  1382. /* 4: added two year ago, confirmed yesterday, primary. (keep this.) */
  1383. {
  1384. entry_guard_t *g = smartlist_get(gs->sampled_entry_guards, 4);
  1385. g->sampled_on_date = one_year_ago;
  1386. g->confirmed_on_date = one_day_ago;
  1387. g->confirmed_idx = 0;
  1388. g->is_primary = 1;
  1389. smartlist_add(gs->confirmed_entry_guards, g);
  1390. smartlist_add(gs->primary_entry_guards, g);
  1391. smartlist_add(keep_ids, tor_memdup(g->identity, 20));
  1392. }
  1393. /* 5: added two years ago, confirmed a year ago, primary. (remove this) */
  1394. {
  1395. entry_guard_t *g = smartlist_get(gs->sampled_entry_guards, 5);
  1396. g->sampled_on_date = two_years_ago;
  1397. g->confirmed_on_date = one_year_ago;
  1398. g->confirmed_idx = 1;
  1399. g->is_primary = 1;
  1400. smartlist_add(gs->confirmed_entry_guards, g);
  1401. smartlist_add(gs->primary_entry_guards, g);
  1402. smartlist_add(remove_ids, tor_memdup(g->identity, 20));
  1403. }
  1404. sampled_guards_update_from_consensus(gs);
  1405. /* Did we remove the right ones? */
  1406. SMARTLIST_FOREACH(keep_ids, uint8_t *, id, {
  1407. tt_assert(get_sampled_guard_with_id(gs, id) != NULL);
  1408. });
  1409. SMARTLIST_FOREACH(remove_ids, uint8_t *, id, {
  1410. tt_want(get_sampled_guard_with_id(gs, id) == NULL);
  1411. });
  1412. /* Did we remove the right number? */
  1413. tt_int_op(smartlist_len(gs->sampled_entry_guards), OP_EQ, n_sampled_pre - 3);
  1414. done:
  1415. guard_selection_free(gs);
  1416. UNMOCK(randomize_time);
  1417. SMARTLIST_FOREACH(keep_ids, char *, cp, tor_free(cp));
  1418. SMARTLIST_FOREACH(remove_ids, char *, cp, tor_free(cp));
  1419. smartlist_free(keep_ids);
  1420. smartlist_free(remove_ids);
  1421. }
  1422. static void
  1423. test_entry_guard_confirming_guards(void *arg)
  1424. {
  1425. (void)arg;
  1426. /* Now let's check the logic responsible for manipulating the list
  1427. * of confirmed guards */
  1428. guard_selection_t *gs = guard_selection_new();
  1429. MOCK(randomize_time, mock_randomize_time_no_randomization);
  1430. /* Create the sample. */
  1431. entry_guards_expand_sample(gs);
  1432. /* Confirm a few guards. */
  1433. time_t start = approx_time();
  1434. entry_guard_t *g1 = smartlist_get(gs->sampled_entry_guards, 0);
  1435. entry_guard_t *g2 = smartlist_get(gs->sampled_entry_guards, 1);
  1436. entry_guard_t *g3 = smartlist_get(gs->sampled_entry_guards, 8);
  1437. make_guard_confirmed(gs, g2);
  1438. update_approx_time(start + 10);
  1439. make_guard_confirmed(gs, g1);
  1440. make_guard_confirmed(gs, g3);
  1441. /* Were the correct dates and indices fed in? */
  1442. tt_int_op(g1->confirmed_idx, OP_EQ, 1);
  1443. tt_int_op(g2->confirmed_idx, OP_EQ, 0);
  1444. tt_int_op(g3->confirmed_idx, OP_EQ, 2);
  1445. tt_i64_op(g1->confirmed_on_date, OP_EQ, start+10);
  1446. tt_i64_op(g2->confirmed_on_date, OP_EQ, start);
  1447. tt_i64_op(g3->confirmed_on_date, OP_EQ, start+10);
  1448. tt_ptr_op(smartlist_get(gs->confirmed_entry_guards, 0), OP_EQ, g2);
  1449. tt_ptr_op(smartlist_get(gs->confirmed_entry_guards, 1), OP_EQ, g1);
  1450. tt_ptr_op(smartlist_get(gs->confirmed_entry_guards, 2), OP_EQ, g3);
  1451. /* Now make sure we can regenerate the confirmed_entry_guards list. */
  1452. smartlist_clear(gs->confirmed_entry_guards);
  1453. g2->confirmed_idx = 0;
  1454. g1->confirmed_idx = 10;
  1455. g3->confirmed_idx = 100;
  1456. entry_guards_update_confirmed(gs);
  1457. tt_int_op(g1->confirmed_idx, OP_EQ, 1);
  1458. tt_int_op(g2->confirmed_idx, OP_EQ, 0);
  1459. tt_int_op(g3->confirmed_idx, OP_EQ, 2);
  1460. tt_ptr_op(smartlist_get(gs->confirmed_entry_guards, 0), OP_EQ, g2);
  1461. tt_ptr_op(smartlist_get(gs->confirmed_entry_guards, 1), OP_EQ, g1);
  1462. tt_ptr_op(smartlist_get(gs->confirmed_entry_guards, 2), OP_EQ, g3);
  1463. /* Now make sure we can regenerate the confirmed_entry_guards list if
  1464. * the indices are messed up. */
  1465. g1->confirmed_idx = g2->confirmed_idx = g3->confirmed_idx = 999;
  1466. smartlist_clear(gs->confirmed_entry_guards);
  1467. entry_guards_update_confirmed(gs);
  1468. tt_int_op(g1->confirmed_idx, OP_GE, 0);
  1469. tt_int_op(g2->confirmed_idx, OP_GE, 0);
  1470. tt_int_op(g3->confirmed_idx, OP_GE, 0);
  1471. tt_int_op(g1->confirmed_idx, OP_LE, 2);
  1472. tt_int_op(g2->confirmed_idx, OP_LE, 2);
  1473. tt_int_op(g3->confirmed_idx, OP_LE, 2);
  1474. g1 = smartlist_get(gs->confirmed_entry_guards, 0);
  1475. g2 = smartlist_get(gs->confirmed_entry_guards, 1);
  1476. g3 = smartlist_get(gs->confirmed_entry_guards, 2);
  1477. tt_int_op(g1->confirmed_idx, OP_EQ, 0);
  1478. tt_int_op(g2->confirmed_idx, OP_EQ, 1);
  1479. tt_int_op(g3->confirmed_idx, OP_EQ, 2);
  1480. tt_assert(g1 != g2);
  1481. tt_assert(g1 != g3);
  1482. tt_assert(g2 != g3);
  1483. done:
  1484. UNMOCK(randomize_time);
  1485. guard_selection_free(gs);
  1486. }
  1487. static void
  1488. test_entry_guard_sample_reachable_filtered(void *arg)
  1489. {
  1490. (void)arg;
  1491. guard_selection_t *gs = guard_selection_new();
  1492. entry_guards_expand_sample(gs);
  1493. const int N = 10000;
  1494. bitarray_t *selected = NULL;
  1495. int i, j;
  1496. /* We've got a sampled list now; let's make one non-usable-filtered; some
  1497. * confirmed, some primary, some pending.
  1498. */
  1499. int n_guards = smartlist_len(gs->sampled_entry_guards);
  1500. tt_int_op(n_guards, OP_GT, 10);
  1501. entry_guard_t *g;
  1502. g = smartlist_get(gs->sampled_entry_guards, 0);
  1503. g->is_pending = 1;
  1504. g = smartlist_get(gs->sampled_entry_guards, 1);
  1505. make_guard_confirmed(gs, g);
  1506. g = smartlist_get(gs->sampled_entry_guards, 2);
  1507. g->is_primary = 1;
  1508. g = smartlist_get(gs->sampled_entry_guards, 3);
  1509. g->pb.path_bias_disabled = 1;
  1510. entry_guards_update_filtered_sets(gs);
  1511. tt_int_op(num_reachable_filtered_guards(gs), OP_EQ, n_guards - 1);
  1512. tt_int_op(smartlist_len(gs->sampled_entry_guards), OP_EQ, n_guards);
  1513. // +1 since the one we made disabled will make another one get added.
  1514. ++n_guards;
  1515. /* Try a bunch of selections. */
  1516. const struct {
  1517. int flag; int idx;
  1518. } tests[] = {
  1519. { 0, -1 },
  1520. { SAMPLE_EXCLUDE_CONFIRMED, 1 },
  1521. { SAMPLE_EXCLUDE_PRIMARY, 2 },
  1522. { SAMPLE_EXCLUDE_PENDING, 0 },
  1523. { -1, -1},
  1524. };
  1525. for (j = 0; tests[j].flag >= 0; ++j) {
  1526. selected = bitarray_init_zero(n_guards);
  1527. const int excluded_flags = tests[j].flag;
  1528. const int excluded_idx = tests[j].idx;
  1529. for (i = 0; i < N; ++i) {
  1530. g = sample_reachable_filtered_entry_guards(gs, excluded_flags);
  1531. tor_assert(g);
  1532. int pos = smartlist_pos(gs->sampled_entry_guards, g);
  1533. tt_int_op(smartlist_len(gs->sampled_entry_guards), OP_EQ, n_guards);
  1534. tt_int_op(pos, OP_GE, 0);
  1535. tt_int_op(pos, OP_LT, n_guards);
  1536. bitarray_set(selected, pos);
  1537. }
  1538. for (i = 0; i < n_guards; ++i) {
  1539. const int should_be_set = (i != excluded_idx &&
  1540. i != 3); // filtered out.
  1541. tt_int_op(!!bitarray_is_set(selected, i), OP_EQ, should_be_set);
  1542. }
  1543. bitarray_free(selected);
  1544. selected = NULL;
  1545. }
  1546. done:
  1547. guard_selection_free(gs);
  1548. bitarray_free(selected);
  1549. }
  1550. static void
  1551. test_entry_guard_sample_reachable_filtered_empty(void *arg)
  1552. {
  1553. (void)arg;
  1554. guard_selection_t *gs = guard_selection_new();
  1555. /* What if we try to sample from a set of 0? */
  1556. SMARTLIST_FOREACH(big_fake_net_nodes, node_t *, n,
  1557. n->is_possible_guard = 0);
  1558. entry_guard_t *g = sample_reachable_filtered_entry_guards(gs, 0);
  1559. tt_ptr_op(g, OP_EQ, NULL);
  1560. done:
  1561. guard_selection_free(gs);
  1562. }
  1563. static void
  1564. test_entry_guard_retry_unreachable(void *arg)
  1565. {
  1566. (void)arg;
  1567. guard_selection_t *gs = guard_selection_new();
  1568. entry_guards_expand_sample(gs);
  1569. /* Let's say that we have two guards, and they're down.
  1570. */
  1571. time_t start = approx_time();;
  1572. entry_guard_t *g1 = smartlist_get(gs->sampled_entry_guards, 0);
  1573. entry_guard_t *g2 = smartlist_get(gs->sampled_entry_guards, 1);
  1574. entry_guard_t *g3 = smartlist_get(gs->sampled_entry_guards, 2);
  1575. g1->is_reachable = GUARD_REACHABLE_NO;
  1576. g2->is_reachable = GUARD_REACHABLE_NO;
  1577. g1->is_primary = 1;
  1578. g1->failing_since = g2->failing_since = start;
  1579. g1->last_tried_to_connect = g2->last_tried_to_connect = start;
  1580. /* Wait 5 minutes. Nothing will get retried. */
  1581. update_approx_time(start + 5 * 60);
  1582. entry_guard_consider_retry(g1);
  1583. entry_guard_consider_retry(g2);
  1584. entry_guard_consider_retry(g3); // just to make sure this doesn't crash.
  1585. tt_int_op(g1->is_reachable, OP_EQ, GUARD_REACHABLE_NO);
  1586. tt_int_op(g2->is_reachable, OP_EQ, GUARD_REACHABLE_NO);
  1587. tt_int_op(g3->is_reachable, OP_EQ, GUARD_REACHABLE_MAYBE);
  1588. /* After 30 min, the primary one gets retried */
  1589. update_approx_time(start + 35 * 60);
  1590. entry_guard_consider_retry(g1);
  1591. entry_guard_consider_retry(g2);
  1592. tt_int_op(g1->is_reachable, OP_EQ, GUARD_REACHABLE_MAYBE);
  1593. tt_int_op(g2->is_reachable, OP_EQ, GUARD_REACHABLE_NO);
  1594. g1->is_reachable = GUARD_REACHABLE_NO;
  1595. g1->last_tried_to_connect = start + 35*60;
  1596. /* After 1 hour, we'll retry the nonprimary one. */
  1597. update_approx_time(start + 61 * 60);
  1598. entry_guard_consider_retry(g1);
  1599. entry_guard_consider_retry(g2);
  1600. tt_int_op(g1->is_reachable, OP_EQ, GUARD_REACHABLE_NO);
  1601. tt_int_op(g2->is_reachable, OP_EQ, GUARD_REACHABLE_MAYBE);
  1602. g2->is_reachable = GUARD_REACHABLE_NO;
  1603. g2->last_tried_to_connect = start + 61*60;
  1604. /* And then the primary one again. */
  1605. update_approx_time(start + 66 * 60);
  1606. entry_guard_consider_retry(g1);
  1607. entry_guard_consider_retry(g2);
  1608. tt_int_op(g1->is_reachable, OP_EQ, GUARD_REACHABLE_MAYBE);
  1609. tt_int_op(g2->is_reachable, OP_EQ, GUARD_REACHABLE_NO);
  1610. done:
  1611. guard_selection_free(gs);
  1612. }
  1613. static void
  1614. test_entry_guard_manage_primary(void *arg)
  1615. {
  1616. (void)arg;
  1617. guard_selection_t *gs = guard_selection_new();
  1618. smartlist_t *prev_guards = smartlist_new();
  1619. /* If no guards are confirmed, we should pick a few reachable guards and
  1620. * call them all primary. But not confirmed.*/
  1621. entry_guards_update_primary(gs);
  1622. int n_primary = smartlist_len(gs->primary_entry_guards);
  1623. tt_int_op(n_primary, OP_GE, 1);
  1624. SMARTLIST_FOREACH(gs->primary_entry_guards, entry_guard_t *, g, {
  1625. tt_assert(g->is_primary);
  1626. tt_assert(g->confirmed_idx == -1);
  1627. });
  1628. /* Calling it a second time should leave the guards unchanged. */
  1629. smartlist_add_all(prev_guards, gs->primary_entry_guards);
  1630. entry_guards_update_primary(gs);
  1631. tt_int_op(smartlist_len(gs->primary_entry_guards), OP_EQ, n_primary);
  1632. SMARTLIST_FOREACH(gs->primary_entry_guards, entry_guard_t *, g, {
  1633. tt_ptr_op(g, OP_EQ, smartlist_get(prev_guards, g_sl_idx));
  1634. });
  1635. /* If we have one confirmed guard, that guards becomes the first primary
  1636. * guard, and the other primary guards get kept. */
  1637. /* find a non-primary guard... */
  1638. entry_guard_t *confirmed = NULL;
  1639. SMARTLIST_FOREACH(gs->sampled_entry_guards, entry_guard_t *, g, {
  1640. if (! g->is_primary) {
  1641. confirmed = g;
  1642. break;
  1643. }
  1644. });
  1645. tt_assert(confirmed);
  1646. /* make it confirmed. */
  1647. make_guard_confirmed(gs, confirmed);
  1648. /* update the list... */
  1649. smartlist_clear(prev_guards);
  1650. smartlist_add_all(prev_guards, gs->primary_entry_guards);
  1651. entry_guards_update_primary(gs);
  1652. /* and see what's primary now! */
  1653. tt_int_op(smartlist_len(gs->primary_entry_guards), OP_EQ, n_primary);
  1654. tt_ptr_op(smartlist_get(gs->primary_entry_guards, 0), OP_EQ, confirmed);
  1655. SMARTLIST_FOREACH(gs->primary_entry_guards, entry_guard_t *, g, {
  1656. tt_assert(g->is_primary);
  1657. if (g_sl_idx == 0)
  1658. continue;
  1659. tt_ptr_op(g, OP_EQ, smartlist_get(prev_guards, g_sl_idx - 1));
  1660. });
  1661. {
  1662. entry_guard_t *prev_last_guard = smartlist_get(prev_guards, n_primary-1);
  1663. tt_assert(! prev_last_guard->is_primary);
  1664. }
  1665. /* Calling it a fourth time should leave the guards unchanged. */
  1666. smartlist_clear(prev_guards);
  1667. smartlist_add_all(prev_guards, gs->primary_entry_guards);
  1668. entry_guards_update_primary(gs);
  1669. tt_int_op(smartlist_len(gs->primary_entry_guards), OP_EQ, n_primary);
  1670. SMARTLIST_FOREACH(gs->primary_entry_guards, entry_guard_t *, g, {
  1671. tt_ptr_op(g, OP_EQ, smartlist_get(prev_guards, g_sl_idx));
  1672. });
  1673. done:
  1674. guard_selection_free(gs);
  1675. smartlist_free(prev_guards);
  1676. }
  1677. static void
  1678. test_entry_guard_select_for_circuit_no_confirmed(void *arg)
  1679. {
  1680. /* Simpler cases: no gaurds are confirmed yet. */
  1681. (void)arg;
  1682. guard_selection_t *gs = guard_selection_new();
  1683. /* simple starting configuration */
  1684. entry_guards_update_primary(gs);
  1685. unsigned state = 9999;
  1686. entry_guard_t *g = select_entry_guard_for_circuit(gs, &state);
  1687. tt_assert(g);
  1688. tt_assert(g->is_primary);
  1689. tt_int_op(g->confirmed_idx, OP_EQ, -1);
  1690. tt_assert(g->is_pending == 0); // primary implies non-pending.
  1691. tt_uint_op(state, OP_EQ, GUARD_CIRC_STATE_USABLE_ON_COMPLETION);
  1692. tt_i64_op(g->last_tried_to_connect, OP_EQ, approx_time());
  1693. // If we do that again, we should get the same guard.
  1694. entry_guard_t *g2 = select_entry_guard_for_circuit(gs, &state);
  1695. tt_ptr_op(g2, OP_EQ, g);
  1696. // if we mark that guard down, we should get a different primary guard.
  1697. // auto-retry it.
  1698. g->is_reachable = GUARD_REACHABLE_NO;
  1699. g->unreachable_since = approx_time() - 10;
  1700. g->last_tried_to_connect = approx_time() - 10;
  1701. state = 9999;
  1702. g2 = select_entry_guard_for_circuit(gs, &state);
  1703. tt_ptr_op(g2, OP_NE, g);
  1704. tt_assert(g2);
  1705. tt_assert(g2->is_primary);
  1706. tt_int_op(g2->confirmed_idx, OP_EQ, -1);
  1707. tt_assert(g2->is_pending == 0); // primary implies non-pending.
  1708. tt_uint_op(state, OP_EQ, GUARD_CIRC_STATE_USABLE_ON_COMPLETION);
  1709. tt_i64_op(g2->last_tried_to_connect, OP_EQ, approx_time());
  1710. // If we say that the first primary guard was last tried a long time ago, we
  1711. // should get an automatic retry on it.
  1712. g->unreachable_since = approx_time() - 72*60*60;
  1713. g->last_tried_to_connect = approx_time() - 72*60*60;
  1714. state = 9999;
  1715. g2 = select_entry_guard_for_circuit(gs, &state);
  1716. tt_ptr_op(g2, OP_EQ, g);
  1717. tt_assert(g2);
  1718. tt_uint_op(state, OP_EQ, GUARD_CIRC_STATE_USABLE_ON_COMPLETION);
  1719. tt_i64_op(g2->last_tried_to_connect, OP_EQ, approx_time());
  1720. tt_int_op(g2->is_reachable, OP_EQ, GUARD_REACHABLE_MAYBE);
  1721. // And if we mark ALL the primary guards down, we should get another guard
  1722. // at random.
  1723. SMARTLIST_FOREACH(gs->primary_entry_guards, entry_guard_t *, guard, {
  1724. guard->is_reachable = GUARD_REACHABLE_NO;
  1725. guard->last_tried_to_connect = approx_time() - 5;
  1726. guard->unreachable_since = approx_time() - 30;
  1727. });
  1728. state = 9999;
  1729. g2 = select_entry_guard_for_circuit(gs, &state);
  1730. tt_assert(g2);
  1731. tt_assert(!g2->is_primary);
  1732. tt_int_op(g2->confirmed_idx, OP_EQ, -1);
  1733. tt_assert(g2->is_pending == 1);
  1734. tt_uint_op(state, OP_EQ, GUARD_CIRC_STATE_USABLE_IF_NO_BETTER_GUARD);
  1735. tt_i64_op(g2->last_tried_to_connect, OP_EQ, approx_time());
  1736. tt_int_op(g2->is_reachable, OP_EQ, GUARD_REACHABLE_MAYBE);
  1737. // As a bonus, maybe we should be retrying the primary guards. Let's say so.
  1738. mark_primary_guards_maybe_reachable(gs);
  1739. SMARTLIST_FOREACH(gs->primary_entry_guards, entry_guard_t *, guard, {
  1740. tt_int_op(guard->is_reachable, OP_EQ, GUARD_REACHABLE_MAYBE);
  1741. tt_assert(guard->is_usable_filtered_guard == 1);
  1742. // no change to these fields.
  1743. tt_i64_op(guard->last_tried_to_connect, OP_EQ, approx_time() - 5);
  1744. tt_i64_op(guard->unreachable_since, OP_EQ, approx_time() - 30);
  1745. });
  1746. done:
  1747. guard_selection_free(gs);
  1748. }
  1749. static void
  1750. test_entry_guard_select_for_circuit_confirmed(void *arg)
  1751. {
  1752. /* Case 2: if all the primary guards are down, and there are more confirmed
  1753. guards, we use a confirmed guard. */
  1754. (void)arg;
  1755. int i;
  1756. guard_selection_t *gs = guard_selection_new();
  1757. const int N_CONFIRMED = 10;
  1758. /* slightly more complicated simple starting configuration */
  1759. entry_guards_update_primary(gs);
  1760. for (i = 0; i < N_CONFIRMED; ++i) {
  1761. entry_guard_t *guard = smartlist_get(gs->sampled_entry_guards, i);
  1762. make_guard_confirmed(gs, guard);
  1763. }
  1764. entry_guards_update_primary(gs); // rebuild the primary list.
  1765. unsigned state = 9999;
  1766. // As above, this gives us a primary guard.
  1767. entry_guard_t *g = select_entry_guard_for_circuit(gs, &state);
  1768. tt_assert(g);
  1769. tt_assert(g->is_primary);
  1770. tt_int_op(g->confirmed_idx, OP_EQ, 0);
  1771. tt_assert(g->is_pending == 0); // primary implies non-pending.
  1772. tt_uint_op(state, OP_EQ, GUARD_CIRC_STATE_USABLE_ON_COMPLETION);
  1773. tt_i64_op(g->last_tried_to_connect, OP_EQ, approx_time());
  1774. tt_ptr_op(g, OP_EQ, smartlist_get(gs->primary_entry_guards, 0));
  1775. // But if we mark all the primary guards down...
  1776. SMARTLIST_FOREACH(gs->primary_entry_guards, entry_guard_t *, guard, {
  1777. guard->last_tried_to_connect = approx_time();
  1778. entry_guards_note_guard_failure(gs, guard);
  1779. });
  1780. // ... we should get a confirmed guard.
  1781. state = 9999;
  1782. g = select_entry_guard_for_circuit(gs, &state);
  1783. tt_assert(g);
  1784. tt_assert(! g->is_primary);
  1785. tt_int_op(g->confirmed_idx, OP_EQ, smartlist_len(gs->primary_entry_guards));
  1786. tt_assert(g->is_pending);
  1787. tt_uint_op(state, OP_EQ, GUARD_CIRC_STATE_USABLE_IF_NO_BETTER_GUARD);
  1788. tt_i64_op(g->last_tried_to_connect, OP_EQ, approx_time());
  1789. // And if we try again, we should get a different confirmed guard, since
  1790. // that one is pending.
  1791. state = 9999;
  1792. entry_guard_t *g2 = select_entry_guard_for_circuit(gs, &state);
  1793. tt_assert(g2);
  1794. tt_assert(! g2->is_primary);
  1795. tt_ptr_op(g2, OP_NE, g);
  1796. tt_int_op(g2->confirmed_idx, OP_EQ,
  1797. smartlist_len(gs->primary_entry_guards)+1);
  1798. tt_assert(g2->is_pending);
  1799. tt_uint_op(state, OP_EQ, GUARD_CIRC_STATE_USABLE_IF_NO_BETTER_GUARD);
  1800. tt_i64_op(g2->last_tried_to_connect, OP_EQ, approx_time());
  1801. // If we make every confirmed guard become pending then we start poking
  1802. // other guards.
  1803. const int n_remaining_confirmed =
  1804. N_CONFIRMED - 2 - smartlist_len(gs->primary_entry_guards);
  1805. for (i = 0; i < n_remaining_confirmed; ++i) {
  1806. g = select_entry_guard_for_circuit(gs, &state);
  1807. tt_int_op(g->confirmed_idx, OP_GE, 0);
  1808. tt_assert(g);
  1809. }
  1810. state = 9999;
  1811. g = select_entry_guard_for_circuit(gs, &state);
  1812. tt_assert(g);
  1813. tt_assert(g->is_pending);
  1814. tt_int_op(g->confirmed_idx, OP_EQ, -1);
  1815. done:
  1816. guard_selection_free(gs);
  1817. }
  1818. static const struct testcase_setup_t fake_network = {
  1819. fake_network_setup, fake_network_cleanup
  1820. };
  1821. static const struct testcase_setup_t big_fake_network = {
  1822. big_fake_network_setup, big_fake_network_cleanup
  1823. };
  1824. #define BFN_TEST(name) \
  1825. { #name, test_entry_guard_ ## name, TT_FORK, &big_fake_network, NULL }
  1826. struct testcase_t entrynodes_tests[] = {
  1827. { "entry_is_time_to_retry", test_entry_is_time_to_retry,
  1828. TT_FORK, NULL, NULL },
  1829. { "choose_random_entry_no_guards", test_choose_random_entry_no_guards,
  1830. TT_FORK, &fake_network, NULL },
  1831. { "choose_random_entry_one_possible_guard",
  1832. test_choose_random_entry_one_possible_guard,
  1833. TT_FORK, &fake_network, NULL },
  1834. { "populate_live_entry_guards_1guard",
  1835. test_populate_live_entry_guards_1guard,
  1836. TT_FORK, &fake_network, NULL },
  1837. { "populate_live_entry_guards_3guards",
  1838. test_populate_live_entry_guards_3guards,
  1839. TT_FORK, &fake_network, NULL },
  1840. { "entry_guards_parse_state_simple",
  1841. test_entry_guards_parse_state_simple,
  1842. TT_FORK, &fake_network, NULL },
  1843. { "entry_guards_parse_state_pathbias",
  1844. test_entry_guards_parse_state_pathbias,
  1845. TT_FORK, &fake_network, NULL },
  1846. { "entry_guards_set_from_config",
  1847. test_entry_guards_set_from_config,
  1848. TT_FORK, &fake_network, NULL },
  1849. { "entry_is_live",
  1850. test_entry_is_live,
  1851. TT_FORK, &fake_network, NULL },
  1852. { "node_preferred_orport",
  1853. test_node_preferred_orport,
  1854. 0, NULL, NULL },
  1855. { "entry_guard_describe", test_entry_guard_describe, 0, NULL, NULL },
  1856. { "randomize_time", test_entry_guard_randomize_time, 0, NULL, NULL },
  1857. { "encode_for_state_minimal",
  1858. test_entry_guard_encode_for_state_minimal, 0, NULL, NULL },
  1859. { "encode_for_state_maximal",
  1860. test_entry_guard_encode_for_state_maximal, 0, NULL, NULL },
  1861. { "parse_from_state_minimal",
  1862. test_entry_guard_parse_from_state_minimal, 0, NULL, NULL },
  1863. { "parse_from_state_maximal",
  1864. test_entry_guard_parse_from_state_maximal, 0, NULL, NULL },
  1865. { "parse_from_state_failure",
  1866. test_entry_guard_parse_from_state_failure, 0, NULL, NULL },
  1867. { "parse_from_state_partial_failure",
  1868. test_entry_guard_parse_from_state_partial_failure, 0, NULL, NULL },
  1869. BFN_TEST(add_single_guard),
  1870. BFN_TEST(node_filter),
  1871. BFN_TEST(expand_sample),
  1872. BFN_TEST(expand_sample_small_net),
  1873. BFN_TEST(update_from_consensus_status),
  1874. BFN_TEST(update_from_consensus_repair),
  1875. BFN_TEST(update_from_consensus_remove),
  1876. BFN_TEST(confirming_guards),
  1877. BFN_TEST(sample_reachable_filtered),
  1878. BFN_TEST(sample_reachable_filtered_empty),
  1879. BFN_TEST(retry_unreachable),
  1880. BFN_TEST(manage_primary),
  1881. BFN_TEST(select_for_circuit_no_confirmed),
  1882. BFN_TEST(select_for_circuit_confirmed),
  1883. END_OF_TESTCASES
  1884. };