test_entrynodes.c 73 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264
  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_options_t *options = get_options_mutable();
  427. options->UseDeprecatedGuardAlgorithm = 1;
  428. or_state_t *state = or_state_new();
  429. const smartlist_t *all_entry_guards = get_entry_guards();
  430. smartlist_t *entry_state_lines = smartlist_new();
  431. char *msg = NULL;
  432. int retval;
  433. /* Details of our fake guard node */
  434. const char *nickname = "hagbard";
  435. const char *fpr = "B29D536DD1752D542E1FBB3C9CE4449D51298212";
  436. const char *tor_version = "0.2.5.3-alpha-dev";
  437. const char *added_at = get_yesterday_date_str();
  438. const char *unlisted_since = "2014-06-08 16:16:50";
  439. (void) arg;
  440. /* The global entry guards smartlist should be empty now. */
  441. tt_int_op(smartlist_len(all_entry_guards), OP_EQ, 0);
  442. { /* Prepare the state entry */
  443. /* Prepare the smartlist to hold the key/value of each line */
  444. smartlist_t *state_line = smartlist_new();
  445. smartlist_add_asprintf(state_line, "EntryGuard");
  446. smartlist_add_asprintf(state_line, "%s %s %s", nickname, fpr, "DirCache");
  447. smartlist_add(entry_state_lines, state_line);
  448. state_line = smartlist_new();
  449. smartlist_add_asprintf(state_line, "EntryGuardAddedBy");
  450. smartlist_add_asprintf(state_line, "%s %s %s", fpr, tor_version, added_at);
  451. smartlist_add(entry_state_lines, state_line);
  452. state_line = smartlist_new();
  453. smartlist_add_asprintf(state_line, "EntryGuardUnlistedSince");
  454. smartlist_add_asprintf(state_line, "%s", unlisted_since);
  455. smartlist_add(entry_state_lines, state_line);
  456. }
  457. /* Inject our lines in the state */
  458. state_insert_entry_guard_helper(state, entry_state_lines);
  459. /* Parse state */
  460. retval = entry_guards_parse_state(state, 1, &msg);
  461. tt_int_op(retval, OP_GE, 0);
  462. /* Test that the guard was registered.
  463. We need to re-get the entry guard list since its pointer was
  464. overwritten in entry_guards_parse_state(). */
  465. all_entry_guards = get_entry_guards();
  466. tt_int_op(smartlist_len(all_entry_guards), OP_EQ, 1);
  467. { /* Test the entry guard structure */
  468. char hex_digest[1024];
  469. char str_time[1024];
  470. const entry_guard_t *e = smartlist_get(all_entry_guards, 0);
  471. tt_str_op(e->nickname, OP_EQ, nickname); /* Verify nickname */
  472. base16_encode(hex_digest, sizeof(hex_digest),
  473. e->identity, DIGEST_LEN);
  474. tt_str_op(hex_digest, OP_EQ, fpr); /* Verify fingerprint */
  475. tt_assert(e->is_dir_cache); /* Verify dirness */
  476. tt_str_op(e->chosen_by_version, OP_EQ, tor_version); /* Verify version */
  477. tt_assert(e->made_contact); /* All saved guards have been contacted */
  478. tt_assert(e->bad_since); /* Verify bad_since timestamp */
  479. format_iso_time(str_time, e->bad_since);
  480. tt_str_op(str_time, OP_EQ, unlisted_since);
  481. /* The rest should be unset */
  482. tt_assert(!e->unreachable_since);
  483. tt_assert(!e->can_retry);
  484. tt_assert(!e->pb.path_bias_noticed);
  485. tt_assert(!e->pb.path_bias_warned);
  486. tt_assert(!e->pb.path_bias_extreme);
  487. tt_assert(!e->pb.path_bias_disabled);
  488. tt_assert(!e->pb.path_bias_use_noticed);
  489. tt_assert(!e->pb.path_bias_use_extreme);
  490. tt_assert(!e->last_attempted);
  491. }
  492. done:
  493. state_lines_free(entry_state_lines);
  494. or_state_free(state);
  495. tor_free(msg);
  496. }
  497. /** Similar to test_entry_guards_parse_state_simple() but aims to test
  498. the PathBias-related details of the entry guard. */
  499. static void
  500. test_entry_guards_parse_state_pathbias(void *arg)
  501. {
  502. or_options_t *options = get_options_mutable();
  503. options->UseDeprecatedGuardAlgorithm = 1;
  504. or_state_t *state = or_state_new();
  505. const smartlist_t *all_entry_guards = get_entry_guards();
  506. char *msg = NULL;
  507. int retval;
  508. smartlist_t *entry_state_lines = smartlist_new();
  509. /* Path bias details of the fake guard */
  510. const double circ_attempts = 9;
  511. const double circ_successes = 8;
  512. const double successful_closed = 4;
  513. const double collapsed = 2;
  514. const double unusable = 0;
  515. const double timeouts = 1;
  516. (void) arg;
  517. /* The global entry guards smartlist should be empty now. */
  518. tt_int_op(smartlist_len(all_entry_guards), OP_EQ, 0);
  519. { /* Prepare the state entry */
  520. /* Prepare the smartlist to hold the key/value of each line */
  521. smartlist_t *state_line = smartlist_new();
  522. smartlist_add_asprintf(state_line, "EntryGuard");
  523. smartlist_add_asprintf(state_line,
  524. "givethanks B29D536DD1752D542E1FBB3C9CE4449D51298212 NoDirCache");
  525. smartlist_add(entry_state_lines, state_line);
  526. state_line = smartlist_new();
  527. smartlist_add_asprintf(state_line, "EntryGuardAddedBy");
  528. smartlist_add_asprintf(state_line,
  529. "B29D536DD1752D542E1FBB3C9CE4449D51298212 0.2.5.3-alpha-dev "
  530. "%s", get_yesterday_date_str());
  531. smartlist_add(entry_state_lines, state_line);
  532. state_line = smartlist_new();
  533. smartlist_add_asprintf(state_line, "EntryGuardUnlistedSince");
  534. smartlist_add_asprintf(state_line, "2014-06-08 16:16:50");
  535. smartlist_add(entry_state_lines, state_line);
  536. state_line = smartlist_new();
  537. smartlist_add_asprintf(state_line, "EntryGuardPathBias");
  538. smartlist_add_asprintf(state_line, "%f %f %f %f %f %f",
  539. circ_attempts, circ_successes, successful_closed,
  540. collapsed, unusable, timeouts);
  541. smartlist_add(entry_state_lines, state_line);
  542. }
  543. /* Inject our lines in the state */
  544. state_insert_entry_guard_helper(state, entry_state_lines);
  545. /* Parse state */
  546. retval = entry_guards_parse_state(state, 1, &msg);
  547. tt_int_op(retval, OP_GE, 0);
  548. /* Test that the guard was registered */
  549. all_entry_guards = get_entry_guards();
  550. tt_int_op(smartlist_len(all_entry_guards), OP_EQ, 1);
  551. { /* Test the path bias of this guard */
  552. const entry_guard_t *e = smartlist_get(all_entry_guards, 0);
  553. tt_assert(!e->is_dir_cache);
  554. tt_assert(!e->can_retry);
  555. /* XXX tt_double_op doesn't support equality. Cast to int for now. */
  556. tt_int_op((int)e->pb.circ_attempts, OP_EQ, (int)circ_attempts);
  557. tt_int_op((int)e->pb.circ_successes, OP_EQ, (int)circ_successes);
  558. tt_int_op((int)e->pb.successful_circuits_closed, OP_EQ,
  559. (int)successful_closed);
  560. tt_int_op((int)e->pb.timeouts, OP_EQ, (int)timeouts);
  561. tt_int_op((int)e->pb.collapsed_circuits, OP_EQ, (int)collapsed);
  562. tt_int_op((int)e->pb.unusable_circuits, OP_EQ, (int)unusable);
  563. }
  564. done:
  565. or_state_free(state);
  566. state_lines_free(entry_state_lines);
  567. tor_free(msg);
  568. }
  569. /* Simple test of entry_guards_set_from_config() by specifying a
  570. particular EntryNode and making sure it gets picked. */
  571. static void
  572. test_entry_guards_set_from_config(void *arg)
  573. {
  574. or_options_t *options = get_options_mutable();
  575. options->UseDeprecatedGuardAlgorithm = 1;
  576. guard_selection_t *gs = get_guard_selection_info();
  577. const smartlist_t *all_entry_guards =
  578. get_entry_guards_for_guard_selection(gs);
  579. const char *entrynodes_str = "test003r";
  580. const node_t *chosen_entry = NULL;
  581. int retval;
  582. (void) arg;
  583. /* Prase EntryNodes as a routerset. */
  584. options->EntryNodes = routerset_new();
  585. retval = routerset_parse(options->EntryNodes,
  586. entrynodes_str,
  587. "test_entrynodes");
  588. tt_int_op(retval, OP_GE, 0);
  589. /* Read nodes from EntryNodes */
  590. entry_guards_set_from_config(gs, options);
  591. /* Test that only one guard was added. */
  592. tt_int_op(smartlist_len(all_entry_guards), OP_EQ, 1);
  593. /* Make sure it was the guard we specified. */
  594. chosen_entry = choose_random_entry(NULL);
  595. tt_str_op(chosen_entry->ri->nickname, OP_EQ, entrynodes_str);
  596. done:
  597. routerset_free(options->EntryNodes);
  598. }
  599. static void
  600. test_entry_is_time_to_retry(void *arg)
  601. {
  602. entry_guard_t *test_guard;
  603. time_t now;
  604. int retval;
  605. (void)arg;
  606. now = time(NULL);
  607. test_guard = tor_malloc_zero(sizeof(entry_guard_t));
  608. test_guard->last_attempted = now - 10;
  609. test_guard->unreachable_since = now - 1;
  610. retval = entry_is_time_to_retry(test_guard,now);
  611. tt_int_op(retval,OP_EQ,1);
  612. test_guard->unreachable_since = now - (6*60*60 - 1);
  613. test_guard->last_attempted = now - (60*60 + 1);
  614. retval = entry_is_time_to_retry(test_guard,now);
  615. tt_int_op(retval,OP_EQ,1);
  616. test_guard->last_attempted = now - (60*60 - 1);
  617. retval = entry_is_time_to_retry(test_guard,now);
  618. tt_int_op(retval,OP_EQ,0);
  619. test_guard->unreachable_since = now - (6*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 - (4*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 - (3*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->unreachable_since = now - (7*24*60*60 - 1);
  632. test_guard->last_attempted = now - (18*60*60 + 1);
  633. retval = entry_is_time_to_retry(test_guard,now);
  634. tt_int_op(retval,OP_EQ,1);
  635. test_guard->last_attempted = now - (18*60*60 - 1);
  636. retval = entry_is_time_to_retry(test_guard,now);
  637. tt_int_op(retval,OP_EQ,0);
  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. test_guard->unreachable_since = now - (7*24*60*60 + 1);
  643. test_guard->last_attempted = now - (36*60*60 + 1);
  644. retval = entry_is_time_to_retry(test_guard,now);
  645. tt_int_op(retval,OP_EQ,1);
  646. done:
  647. tor_free(test_guard);
  648. }
  649. /** XXX Do some tests that entry_is_live() */
  650. static void
  651. test_entry_is_live(void *arg)
  652. {
  653. smartlist_t *our_nodelist = NULL;
  654. guard_selection_t *gs = get_guard_selection_info();
  655. const smartlist_t *all_entry_guards =
  656. get_entry_guards_for_guard_selection(gs);
  657. const node_t *test_node = NULL;
  658. const entry_guard_t *test_entry = NULL;
  659. const char *msg;
  660. int which_node;
  661. (void) arg;
  662. /* The global entry guards smartlist should be empty now. */
  663. tt_int_op(smartlist_len(all_entry_guards), OP_EQ, 0);
  664. /* Walk the nodelist and add all nodes as entry guards. */
  665. our_nodelist = nodelist_get_list();
  666. tt_int_op(smartlist_len(our_nodelist), OP_EQ, HELPER_NUMBER_OF_DESCRIPTORS);
  667. SMARTLIST_FOREACH_BEGIN(our_nodelist, const node_t *, node) {
  668. const node_t *node_tmp;
  669. node_tmp = add_an_entry_guard(gs, node, 0, 1, 0, 0);
  670. tt_assert(node_tmp);
  671. tt_int_op(node->is_stable, OP_EQ, 0);
  672. tt_int_op(node->is_fast, OP_EQ, 0);
  673. } SMARTLIST_FOREACH_END(node);
  674. /* Make sure the nodes were added as entry guards. */
  675. tt_int_op(smartlist_len(all_entry_guards), OP_EQ,
  676. HELPER_NUMBER_OF_DESCRIPTORS);
  677. /* Now get a random test entry that we will use for this unit test. */
  678. which_node = 3; /* (chosen by fair dice roll) */
  679. test_entry = smartlist_get(all_entry_guards, which_node);
  680. /* Let's do some entry_is_live() tests! */
  681. /* Require the node to be stable, but it's not. Should fail.
  682. Also enable 'assume_reachable' because why not. */
  683. test_node = entry_is_live(test_entry,
  684. ENTRY_NEED_UPTIME | ENTRY_ASSUME_REACHABLE,
  685. &msg);
  686. tt_assert(!test_node);
  687. /* Require the node to be fast, but it's not. Should fail. */
  688. test_node = entry_is_live(test_entry,
  689. ENTRY_NEED_CAPACITY | ENTRY_ASSUME_REACHABLE,
  690. &msg);
  691. tt_assert(!test_node);
  692. /* Don't impose any restrictions on the node. Should succeed. */
  693. test_node = entry_is_live(test_entry, 0, &msg);
  694. tt_assert(test_node);
  695. tt_ptr_op(test_node, OP_EQ, node_get_by_id(test_entry->identity));
  696. /* Require descriptor for this node. It has one so it should succeed. */
  697. test_node = entry_is_live(test_entry, ENTRY_NEED_DESCRIPTOR, &msg);
  698. tt_assert(test_node);
  699. tt_ptr_op(test_node, OP_EQ, node_get_by_id(test_entry->identity));
  700. done:
  701. ; /* XXX */
  702. }
  703. #define TEST_IPV4_ADDR "123.45.67.89"
  704. #define TEST_IPV6_ADDR "[1234:5678:90ab:cdef::]"
  705. static void
  706. test_node_preferred_orport(void *arg)
  707. {
  708. (void)arg;
  709. tor_addr_t ipv4_addr;
  710. const uint16_t ipv4_port = 4444;
  711. tor_addr_t ipv6_addr;
  712. const uint16_t ipv6_port = 6666;
  713. routerinfo_t node_ri;
  714. node_t node;
  715. tor_addr_port_t ap;
  716. /* Setup options */
  717. memset(&mocked_options, 0, sizeof(mocked_options));
  718. /* We don't test ClientPreferIPv6ORPort here, because it's used in
  719. * nodelist_set_consensus to setup node.ipv6_preferred, which we set
  720. * directly. */
  721. MOCK(get_options, mock_get_options);
  722. /* Setup IP addresses */
  723. tor_addr_parse(&ipv4_addr, TEST_IPV4_ADDR);
  724. tor_addr_parse(&ipv6_addr, TEST_IPV6_ADDR);
  725. /* Setup node_ri */
  726. memset(&node_ri, 0, sizeof(node_ri));
  727. node_ri.addr = tor_addr_to_ipv4h(&ipv4_addr);
  728. node_ri.or_port = ipv4_port;
  729. tor_addr_copy(&node_ri.ipv6_addr, &ipv6_addr);
  730. node_ri.ipv6_orport = ipv6_port;
  731. /* Setup node */
  732. memset(&node, 0, sizeof(node));
  733. node.ri = &node_ri;
  734. /* Check the preferred address is IPv4 if we're only using IPv4, regardless
  735. * of whether we prefer it or not */
  736. mocked_options.ClientUseIPv4 = 1;
  737. mocked_options.ClientUseIPv6 = 0;
  738. node.ipv6_preferred = 0;
  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. node.ipv6_preferred = 1;
  743. node_get_pref_orport(&node, &ap);
  744. tt_assert(tor_addr_eq(&ap.addr, &ipv4_addr));
  745. tt_assert(ap.port == ipv4_port);
  746. /* Check the preferred address is IPv4 if we're using IPv4 and IPv6, but
  747. * don't prefer the IPv6 address */
  748. mocked_options.ClientUseIPv4 = 1;
  749. mocked_options.ClientUseIPv6 = 1;
  750. node.ipv6_preferred = 0;
  751. node_get_pref_orport(&node, &ap);
  752. tt_assert(tor_addr_eq(&ap.addr, &ipv4_addr));
  753. tt_assert(ap.port == ipv4_port);
  754. /* Check the preferred address is IPv6 if we prefer it and
  755. * ClientUseIPv6 is 1, regardless of ClientUseIPv4 */
  756. mocked_options.ClientUseIPv4 = 1;
  757. mocked_options.ClientUseIPv6 = 1;
  758. node.ipv6_preferred = 1;
  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. mocked_options.ClientUseIPv4 = 0;
  763. node_get_pref_orport(&node, &ap);
  764. tt_assert(tor_addr_eq(&ap.addr, &ipv6_addr));
  765. tt_assert(ap.port == ipv6_port);
  766. /* Check the preferred address is IPv6 if we don't prefer it, but
  767. * ClientUseIPv4 is 0 */
  768. mocked_options.ClientUseIPv4 = 0;
  769. mocked_options.ClientUseIPv6 = 1;
  770. node.ipv6_preferred = fascist_firewall_prefer_ipv6_orport(&mocked_options);
  771. node_get_pref_orport(&node, &ap);
  772. tt_assert(tor_addr_eq(&ap.addr, &ipv6_addr));
  773. tt_assert(ap.port == ipv6_port);
  774. done:
  775. UNMOCK(get_options);
  776. }
  777. static void
  778. test_entry_guard_describe(void *arg)
  779. {
  780. (void)arg;
  781. entry_guard_t g;
  782. memset(&g, 0, sizeof(g));
  783. strlcpy(g.nickname, "okefenokee", sizeof(g.nickname));
  784. memcpy(g.identity, "theforestprimeval---", DIGEST_LEN);
  785. tt_str_op(entry_guard_describe(&g), OP_EQ,
  786. "okefenokee ($746865666F726573747072696D6576616C2D2D2D)");
  787. done:
  788. ;
  789. }
  790. static void
  791. test_entry_guard_randomize_time(void *arg)
  792. {
  793. const time_t now = 1479153573;
  794. const int delay = 86400;
  795. const int N = 1000;
  796. (void)arg;
  797. time_t t;
  798. int i;
  799. for (i = 0; i < N; ++i) {
  800. t = randomize_time(now, delay);
  801. tt_int_op(t, OP_LE, now);
  802. tt_int_op(t, OP_GE, now-delay);
  803. }
  804. /* now try the corner cases */
  805. for (i = 0; i < N; ++i) {
  806. t = randomize_time(100, delay);
  807. tt_int_op(t, OP_GE, 1);
  808. tt_int_op(t, OP_LE, 100);
  809. t = randomize_time(0, delay);
  810. tt_int_op(t, OP_EQ, 1);
  811. }
  812. done:
  813. ;
  814. }
  815. static void
  816. test_entry_guard_encode_for_state_minimal(void *arg)
  817. {
  818. (void) arg;
  819. entry_guard_t *eg = tor_malloc_zero(sizeof(entry_guard_t));
  820. eg->selection_name = tor_strdup("wubwub");
  821. memcpy(eg->identity, "plurpyflurpyslurpydo", DIGEST_LEN);
  822. eg->sampled_on_date = 1479081600;
  823. eg->confirmed_idx = -1;
  824. char *s = NULL;
  825. s = entry_guard_encode_for_state(eg);
  826. tt_str_op(s, OP_EQ,
  827. "in=wubwub "
  828. "rsa_id=706C75727079666C75727079736C75727079646F "
  829. "sampled_on=2016-11-14T00:00:00 "
  830. "listed=0");
  831. done:
  832. entry_guard_free(eg);
  833. tor_free(s);
  834. }
  835. static void
  836. test_entry_guard_encode_for_state_maximal(void *arg)
  837. {
  838. (void) arg;
  839. entry_guard_t *eg = tor_malloc_zero(sizeof(entry_guard_t));
  840. strlcpy(eg->nickname, "Fred", sizeof(eg->nickname));
  841. eg->selection_name = tor_strdup("default");
  842. memcpy(eg->identity, "plurpyflurpyslurpydo", DIGEST_LEN);
  843. eg->sampled_on_date = 1479081600;
  844. eg->sampled_by_version = tor_strdup("1.2.3");
  845. eg->unlisted_since_date = 1479081645;
  846. eg->currently_listed = 1;
  847. eg->confirmed_on_date = 1479081690;
  848. eg->confirmed_idx = 333;
  849. eg->extra_state_fields = tor_strdup("and the green grass grew all around");
  850. char *s = NULL;
  851. s = entry_guard_encode_for_state(eg);
  852. tt_str_op(s, OP_EQ,
  853. "in=default "
  854. "rsa_id=706C75727079666C75727079736C75727079646F "
  855. "nickname=Fred "
  856. "sampled_on=2016-11-14T00:00:00 "
  857. "sampled_by=1.2.3 "
  858. "unlisted_since=2016-11-14T00:00:45 "
  859. "listed=1 "
  860. "confirmed_on=2016-11-14T00:01:30 "
  861. "confirmed_idx=333 "
  862. "and the green grass grew all around");
  863. done:
  864. entry_guard_free(eg);
  865. tor_free(s);
  866. }
  867. static void
  868. test_entry_guard_parse_from_state_minimal(void *arg)
  869. {
  870. (void)arg;
  871. char *mem_op_hex_tmp = NULL;
  872. entry_guard_t *eg = NULL;
  873. time_t t = approx_time();
  874. eg = entry_guard_parse_from_state(
  875. "in=default_plus "
  876. "rsa_id=596f75206d6179206e656564206120686f626279");
  877. tt_assert(eg);
  878. tt_str_op(eg->selection_name, OP_EQ, "default_plus");
  879. test_mem_op_hex(eg->identity, OP_EQ,
  880. "596f75206d6179206e656564206120686f626279");
  881. tt_str_op(eg->nickname, OP_EQ, "$596F75206D6179206E656564206120686F626279");
  882. tt_i64_op(eg->sampled_on_date, OP_GE, t);
  883. tt_i64_op(eg->sampled_on_date, OP_LE, t+86400);
  884. tt_i64_op(eg->unlisted_since_date, OP_EQ, 0);
  885. tt_ptr_op(eg->sampled_by_version, OP_EQ, NULL);
  886. tt_int_op(eg->currently_listed, OP_EQ, 0);
  887. tt_i64_op(eg->confirmed_on_date, OP_EQ, 0);
  888. tt_int_op(eg->confirmed_idx, OP_EQ, -1);
  889. tt_int_op(eg->last_tried_to_connect, OP_EQ, 0);
  890. tt_int_op(eg->is_reachable, OP_EQ, GUARD_REACHABLE_MAYBE);
  891. done:
  892. entry_guard_free(eg);
  893. tor_free(mem_op_hex_tmp);
  894. }
  895. static void
  896. test_entry_guard_parse_from_state_maximal(void *arg)
  897. {
  898. (void)arg;
  899. char *mem_op_hex_tmp = NULL;
  900. entry_guard_t *eg = NULL;
  901. eg = entry_guard_parse_from_state(
  902. "in=fred "
  903. "rsa_id=706C75727079666C75727079736C75727079646F "
  904. "nickname=Fred "
  905. "sampled_on=2016-11-14T00:00:00 "
  906. "sampled_by=1.2.3 "
  907. "unlisted_since=2016-11-14T00:00:45 "
  908. "listed=1 "
  909. "confirmed_on=2016-11-14T00:01:30 "
  910. "confirmed_idx=333 "
  911. "and the green grass grew all around "
  912. "rsa_id=all,around");
  913. tt_assert(eg);
  914. test_mem_op_hex(eg->identity, OP_EQ,
  915. "706C75727079666C75727079736C75727079646F");
  916. tt_str_op(eg->nickname, OP_EQ, "Fred");
  917. tt_i64_op(eg->sampled_on_date, OP_EQ, 1479081600);
  918. tt_i64_op(eg->unlisted_since_date, OP_EQ, 1479081645);
  919. tt_str_op(eg->sampled_by_version, OP_EQ, "1.2.3");
  920. tt_int_op(eg->currently_listed, OP_EQ, 1);
  921. tt_i64_op(eg->confirmed_on_date, OP_EQ, 1479081690);
  922. tt_int_op(eg->confirmed_idx, OP_EQ, 333);
  923. tt_str_op(eg->extra_state_fields, OP_EQ,
  924. "and the green grass grew all around rsa_id=all,around");
  925. tt_int_op(eg->last_tried_to_connect, OP_EQ, 0);
  926. tt_int_op(eg->is_reachable, OP_EQ, GUARD_REACHABLE_MAYBE);
  927. done:
  928. entry_guard_free(eg);
  929. tor_free(mem_op_hex_tmp);
  930. }
  931. static void
  932. test_entry_guard_parse_from_state_failure(void *arg)
  933. {
  934. (void)arg;
  935. entry_guard_t *eg = NULL;
  936. /* no selection */
  937. eg = entry_guard_parse_from_state(
  938. "rsa_id=596f75206d6179206e656564206120686f626270");
  939. tt_assert(! eg);
  940. /* no RSA ID. */
  941. eg = entry_guard_parse_from_state("in=default nickname=Fred");
  942. tt_assert(! eg);
  943. /* Bad RSA ID: bad character. */
  944. eg = entry_guard_parse_from_state(
  945. "in=default "
  946. "rsa_id=596f75206d6179206e656564206120686f62627q");
  947. tt_assert(! eg);
  948. /* Bad RSA ID: too long.*/
  949. eg = entry_guard_parse_from_state(
  950. "in=default "
  951. "rsa_id=596f75206d6179206e656564206120686f6262703");
  952. tt_assert(! eg);
  953. /* Bad RSA ID: too short.*/
  954. eg = entry_guard_parse_from_state(
  955. "in=default "
  956. "rsa_id=596f75206d6179206e65656420612");
  957. tt_assert(! eg);
  958. done:
  959. entry_guard_free(eg);
  960. }
  961. static void
  962. test_entry_guard_parse_from_state_partial_failure(void *arg)
  963. {
  964. (void)arg;
  965. char *mem_op_hex_tmp = NULL;
  966. entry_guard_t *eg = NULL;
  967. time_t t = approx_time();
  968. eg = entry_guard_parse_from_state(
  969. "in=default "
  970. "rsa_id=706C75727079666C75727079736C75727079646F "
  971. "nickname=FredIsANodeWithAStrangeNicknameThatIsTooLong "
  972. "sampled_on=2016-11-14T00:00:99 "
  973. "sampled_by=1.2.3 stuff in the middle "
  974. "unlisted_since=2016-xx-14T00:00:45 "
  975. "listed=0 "
  976. "confirmed_on=2016-11-14T00:01:30zz "
  977. "confirmed_idx=idx "
  978. "and the green grass grew all around "
  979. "rsa_id=all,around");
  980. tt_assert(eg);
  981. test_mem_op_hex(eg->identity, OP_EQ,
  982. "706C75727079666C75727079736C75727079646F");
  983. tt_str_op(eg->nickname, OP_EQ, "FredIsANodeWithAStrangeNicknameThatIsTooL");
  984. tt_i64_op(eg->sampled_on_date, OP_EQ, t);
  985. tt_i64_op(eg->unlisted_since_date, OP_EQ, 0);
  986. tt_str_op(eg->sampled_by_version, OP_EQ, "1.2.3");
  987. tt_int_op(eg->currently_listed, OP_EQ, 0);
  988. tt_i64_op(eg->confirmed_on_date, OP_EQ, 0);
  989. tt_int_op(eg->confirmed_idx, OP_EQ, -1);
  990. tt_str_op(eg->extra_state_fields, OP_EQ,
  991. "stuff in the middle and the green grass grew all around "
  992. "rsa_id=all,around");
  993. tt_int_op(eg->last_tried_to_connect, OP_EQ, 0);
  994. tt_int_op(eg->is_reachable, OP_EQ, GUARD_REACHABLE_MAYBE);
  995. done:
  996. entry_guard_free(eg);
  997. tor_free(mem_op_hex_tmp);
  998. }
  999. static void
  1000. test_entry_guard_add_single_guard(void *arg)
  1001. {
  1002. (void)arg;
  1003. guard_selection_t *gs = guard_selection_new("default");
  1004. /* 1: Add a single guard to the sample. */
  1005. node_t *n1 = smartlist_get(big_fake_net_nodes, 0);
  1006. time_t now = approx_time();
  1007. tt_assert(n1->is_possible_guard == 1);
  1008. entry_guard_t *g1 = entry_guard_add_to_sample(gs, n1);
  1009. tt_assert(g1);
  1010. /* Make sure its fields look right. */
  1011. tt_mem_op(n1->identity, OP_EQ, g1->identity, DIGEST_LEN);
  1012. tt_i64_op(g1->sampled_on_date, OP_GE, now - 12*86400);
  1013. tt_i64_op(g1->sampled_on_date, OP_LE, now);
  1014. tt_str_op(g1->sampled_by_version, OP_EQ, VERSION);
  1015. tt_assert(g1->currently_listed == 1);
  1016. tt_i64_op(g1->confirmed_on_date, OP_EQ, 0);
  1017. tt_int_op(g1->confirmed_idx, OP_EQ, -1);
  1018. tt_int_op(g1->last_tried_to_connect, OP_EQ, 0);
  1019. tt_uint_op(g1->is_reachable, OP_EQ, GUARD_REACHABLE_MAYBE);
  1020. tt_i64_op(g1->failing_since, OP_EQ, 0);
  1021. tt_assert(g1->is_filtered_guard == 1);
  1022. tt_assert(g1->is_usable_filtered_guard == 1);
  1023. tt_assert(g1->is_primary == 0);
  1024. tt_assert(g1->extra_state_fields == NULL);
  1025. /* Make sure it got added. */
  1026. tt_int_op(1, OP_EQ, smartlist_len(gs->sampled_entry_guards));
  1027. tt_ptr_op(g1, OP_EQ, smartlist_get(gs->sampled_entry_guards, 0));
  1028. tt_ptr_op(g1, OP_EQ, get_sampled_guard_with_id(gs, (uint8_t*)n1->identity));
  1029. const uint8_t bad_id[20] = {0};
  1030. tt_ptr_op(NULL, OP_EQ, get_sampled_guard_with_id(gs, bad_id));
  1031. done:
  1032. guard_selection_free(gs);
  1033. }
  1034. static void
  1035. test_entry_guard_node_filter(void *arg)
  1036. {
  1037. (void)arg;
  1038. guard_selection_t *gs = guard_selection_new("default");
  1039. bridge_line_t *bl = NULL;
  1040. /* Initialize a bunch of node objects that are all guards. */
  1041. const int NUM = 7;
  1042. node_t *n[NUM];
  1043. entry_guard_t *g[NUM];
  1044. int i;
  1045. for (i=0; i < NUM; ++i) {
  1046. n[i] = smartlist_get(big_fake_net_nodes, i*2); // even ones are guards.
  1047. g[i] = entry_guard_add_to_sample(gs, n[i]);
  1048. // everything starts out filtered-in
  1049. tt_assert(g[i]->is_filtered_guard == 1);
  1050. tt_assert(g[i]->is_usable_filtered_guard == 1);
  1051. }
  1052. tt_int_op(num_reachable_filtered_guards(gs), OP_EQ, NUM);
  1053. /* Make sure refiltering doesn't hurt */
  1054. entry_guards_update_filtered_sets(gs);
  1055. for (i = 0; i < NUM; ++i) {
  1056. tt_assert(g[i]->is_filtered_guard == 1);
  1057. tt_assert(g[i]->is_usable_filtered_guard == 1);
  1058. }
  1059. tt_int_op(num_reachable_filtered_guards(gs), OP_EQ, NUM);
  1060. /* Now start doing things to make the guards get filtered out, 1 by 1. */
  1061. /* 0: Not listed. */
  1062. g[0]->currently_listed = 0;
  1063. /* 1: path bias says this guard is maybe eeeevil. */
  1064. g[1]->pb.path_bias_disabled = 1;
  1065. /* 2: Unreachable address. */
  1066. n[2]->rs->addr = 0;
  1067. /* 3: ExcludeNodes */
  1068. n[3]->rs->addr = 0x90902020;
  1069. routerset_free(get_options_mutable()->ExcludeNodes);
  1070. get_options_mutable()->ExcludeNodes = routerset_new();
  1071. routerset_parse(get_options_mutable()->ExcludeNodes, "144.144.0.0/16", "");
  1072. /* 4: Bridge. */
  1073. sweep_bridge_list();
  1074. bl = tor_malloc_zero(sizeof(bridge_line_t));
  1075. tor_addr_from_ipv4h(&bl->addr, n[4]->rs->addr);
  1076. bl->port = n[4]->rs->or_port;
  1077. memcpy(bl->digest, n[4]->identity, 20);
  1078. bridge_add_from_config(bl);
  1079. bl = NULL; // prevent free.
  1080. /* 5: Unreachable. This stays in the filter, but isn't in usable-filtered */
  1081. g[5]->last_tried_to_connect = approx_time(); // prevent retry.
  1082. g[5]->is_reachable = GUARD_REACHABLE_NO;
  1083. /* 6: no change. */
  1084. /* Now refilter and inspect. */
  1085. entry_guards_update_filtered_sets(gs);
  1086. for (i = 0; i < NUM; ++i) {
  1087. tt_assert(g[i]->is_filtered_guard == (i == 5 || i == 6));
  1088. tt_assert(g[i]->is_usable_filtered_guard == (i == 6));
  1089. }
  1090. tt_int_op(num_reachable_filtered_guards(gs), OP_EQ, 1);
  1091. done:
  1092. guard_selection_free(gs);
  1093. tor_free(bl);
  1094. }
  1095. static void
  1096. test_entry_guard_expand_sample(void *arg)
  1097. {
  1098. (void)arg;
  1099. guard_selection_t *gs = guard_selection_new("default");
  1100. digestmap_t *node_by_id = digestmap_new();
  1101. entry_guard_t *guard = entry_guards_expand_sample(gs);
  1102. tt_assert(guard); // the last guard returned.
  1103. // Every sampled guard here should be filtered and reachable for now.
  1104. tt_int_op(smartlist_len(gs->sampled_entry_guards), OP_EQ,
  1105. num_reachable_filtered_guards(gs));
  1106. /* Make sure we got the right number. */
  1107. tt_int_op(MIN_FILTERED_SAMPLE_SIZE, OP_EQ,
  1108. num_reachable_filtered_guards(gs));
  1109. // Make sure everything we got was from our fake node list, and everything
  1110. // was unique.
  1111. SMARTLIST_FOREACH_BEGIN(gs->sampled_entry_guards, entry_guard_t *, g) {
  1112. const node_t *n = bfn_mock_node_get_by_id(g->identity);
  1113. tt_assert(n);
  1114. tt_ptr_op(NULL, OP_EQ, digestmap_get(node_by_id, g->identity));
  1115. digestmap_set(node_by_id, g->identity, (void*) n);
  1116. int idx = smartlist_pos(big_fake_net_nodes, n);
  1117. // The even ones are the guards; make sure we got guards.
  1118. tt_int_op(idx & 1, OP_EQ, 0);
  1119. } SMARTLIST_FOREACH_END(g);
  1120. // Nothing became unusable/unfiltered, so a subsequent expand should
  1121. // make no changes.
  1122. guard = entry_guards_expand_sample(gs);
  1123. tt_assert(! guard); // no guard was added.
  1124. tt_int_op(MIN_FILTERED_SAMPLE_SIZE, OP_EQ,
  1125. num_reachable_filtered_guards(gs));
  1126. // Make a few guards unreachable.
  1127. guard = smartlist_get(gs->sampled_entry_guards, 0);
  1128. guard->is_usable_filtered_guard = 0;
  1129. guard = smartlist_get(gs->sampled_entry_guards, 1);
  1130. guard->is_usable_filtered_guard = 0;
  1131. guard = smartlist_get(gs->sampled_entry_guards, 2);
  1132. guard->is_usable_filtered_guard = 0;
  1133. tt_int_op(MIN_FILTERED_SAMPLE_SIZE - 3, OP_EQ,
  1134. num_reachable_filtered_guards(gs));
  1135. // This time, expanding the sample will add some more guards.
  1136. guard = entry_guards_expand_sample(gs);
  1137. tt_assert(guard); // no guard was added.
  1138. tt_int_op(MIN_FILTERED_SAMPLE_SIZE, OP_EQ,
  1139. num_reachable_filtered_guards(gs));
  1140. tt_int_op(smartlist_len(gs->sampled_entry_guards), OP_EQ,
  1141. num_reachable_filtered_guards(gs)+3);
  1142. // Still idempotent.
  1143. guard = entry_guards_expand_sample(gs);
  1144. tt_assert(! guard); // no guard was added.
  1145. tt_int_op(MIN_FILTERED_SAMPLE_SIZE, OP_EQ,
  1146. num_reachable_filtered_guards(gs));
  1147. // Now, do a nasty trick: tell the filter to exclude 31/32 of the guards.
  1148. // This will cause the sample size to get reeeeally huge, while the
  1149. // filtered sample size grows only slowly.
  1150. routerset_free(get_options_mutable()->ExcludeNodes);
  1151. get_options_mutable()->ExcludeNodes = routerset_new();
  1152. routerset_parse(get_options_mutable()->ExcludeNodes, "144.144.0.0/16", "");
  1153. SMARTLIST_FOREACH(big_fake_net_nodes, node_t *, n, {
  1154. if (n_sl_idx % 64 != 0) {
  1155. n->rs->addr = 0x90903030;
  1156. }
  1157. });
  1158. entry_guards_update_filtered_sets(gs);
  1159. // Surely (p ~ 1-2**-60), one of our guards has been excluded.
  1160. tt_int_op(num_reachable_filtered_guards(gs), OP_LT,
  1161. MIN_FILTERED_SAMPLE_SIZE);
  1162. // Try to regenerate the guards.
  1163. guard = entry_guards_expand_sample(gs);
  1164. tt_assert(guard); // no guard was added.
  1165. /* this time, it's possible that we didn't add enough sampled guards. */
  1166. tt_int_op(num_reachable_filtered_guards(gs), OP_LE,
  1167. MIN_FILTERED_SAMPLE_SIZE);
  1168. /* but we definitely didn't exceed the sample maximum. */
  1169. tt_int_op(smartlist_len(gs->sampled_entry_guards), OP_LE,
  1170. (int)((271 / 2) * .3));
  1171. done:
  1172. guard_selection_free(gs);
  1173. digestmap_free(node_by_id, NULL);
  1174. }
  1175. static void
  1176. test_entry_guard_expand_sample_small_net(void *arg)
  1177. {
  1178. (void)arg;
  1179. guard_selection_t *gs = guard_selection_new("default");
  1180. /* Fun corner case: not enough guards to make up our whole sample size. */
  1181. SMARTLIST_FOREACH(big_fake_net_nodes, node_t *, n, {
  1182. if (n_sl_idx >= 40) {
  1183. tor_free(n->rs);
  1184. tor_free(n->md);
  1185. tor_free(n);
  1186. SMARTLIST_DEL_CURRENT(big_fake_net_nodes, n);
  1187. } else {
  1188. n->rs->addr = 0; // make the filter reject this.
  1189. }
  1190. });
  1191. entry_guard_t *guard = entry_guards_expand_sample(gs);
  1192. tt_assert(guard); // the last guard returned -- some guard was added.
  1193. tt_int_op(smartlist_len(gs->sampled_entry_guards), OP_GT, 0);
  1194. tt_int_op(smartlist_len(gs->sampled_entry_guards), OP_LT, 10);
  1195. tt_int_op(num_reachable_filtered_guards(gs), OP_EQ, 0);
  1196. done:
  1197. guard_selection_free(gs);
  1198. }
  1199. static void
  1200. test_entry_guard_update_from_consensus_status(void *arg)
  1201. {
  1202. /* Here we're going to have some nodes become un-guardy, and say we got a
  1203. * new consensus. This should cause those nodes to get detected as
  1204. * unreachable. */
  1205. (void)arg;
  1206. int i;
  1207. time_t start = approx_time();
  1208. guard_selection_t *gs = guard_selection_new("default");
  1209. networkstatus_t *ns_tmp = NULL;
  1210. /* Don't randomly backdate stuff; it will make correctness harder to check.*/
  1211. MOCK(randomize_time, mock_randomize_time_no_randomization);
  1212. /* First, sample some guards. */
  1213. entry_guards_expand_sample(gs);
  1214. int n_sampled_pre = smartlist_len(gs->sampled_entry_guards);
  1215. int n_filtered_pre = num_reachable_filtered_guards(gs);
  1216. tt_i64_op(n_sampled_pre, OP_EQ, n_filtered_pre);
  1217. tt_i64_op(n_sampled_pre, OP_GT, 10);
  1218. /* At this point, it should be a no-op to do this: */
  1219. sampled_guards_update_from_consensus(gs);
  1220. /* Now let's make some of our guards become unlisted. The easiest way to
  1221. * do that would be to take away their guard flag. */
  1222. for (i = 0; i < 5; ++i) {
  1223. entry_guard_t *g = smartlist_get(gs->sampled_entry_guards, i);
  1224. node_t *n = (node_t*) bfn_mock_node_get_by_id(g->identity);
  1225. n->is_possible_guard = 0;
  1226. }
  1227. update_approx_time(start + 30);
  1228. {
  1229. /* try this with no live networkstatus. Nothing should happen! */
  1230. ns_tmp = dummy_consensus;
  1231. dummy_consensus = NULL;
  1232. sampled_guards_update_from_consensus(gs);
  1233. tt_i64_op(smartlist_len(gs->sampled_entry_guards), OP_EQ, n_sampled_pre);
  1234. tt_i64_op(num_reachable_filtered_guards(gs), OP_EQ, n_filtered_pre);
  1235. /* put the networkstatus back. */
  1236. dummy_consensus = ns_tmp;
  1237. ns_tmp = NULL;
  1238. }
  1239. /* Now those guards should become unlisted, and drop off the filter, but
  1240. * stay in the sample. */
  1241. update_approx_time(start + 60);
  1242. sampled_guards_update_from_consensus(gs);
  1243. tt_i64_op(smartlist_len(gs->sampled_entry_guards), OP_EQ, n_sampled_pre);
  1244. tt_i64_op(num_reachable_filtered_guards(gs), OP_EQ, n_filtered_pre - 5);
  1245. for (i = 0; i < 5; ++i) {
  1246. entry_guard_t *g = smartlist_get(gs->sampled_entry_guards, i);
  1247. tt_assert(! g->currently_listed);
  1248. tt_i64_op(g->unlisted_since_date, OP_EQ, start+60);
  1249. }
  1250. for (i = 5; i < n_sampled_pre; ++i) {
  1251. entry_guard_t *g = smartlist_get(gs->sampled_entry_guards, i);
  1252. tt_assert(g->currently_listed);
  1253. tt_i64_op(g->unlisted_since_date, OP_EQ, 0);
  1254. }
  1255. /* Now re-list one, and remove one completely. */
  1256. {
  1257. entry_guard_t *g = smartlist_get(gs->sampled_entry_guards, 0);
  1258. node_t *n = (node_t*) bfn_mock_node_get_by_id(g->identity);
  1259. n->is_possible_guard = 1;
  1260. }
  1261. {
  1262. /* try removing the node, to make sure we don't crash on an absent node
  1263. */
  1264. entry_guard_t *g = smartlist_get(gs->sampled_entry_guards, 5);
  1265. node_t *n = (node_t*) bfn_mock_node_get_by_id(g->identity);
  1266. smartlist_remove(big_fake_net_nodes, n);
  1267. tor_free(n->rs);
  1268. tor_free(n->md);
  1269. tor_free(n);
  1270. }
  1271. update_approx_time(start + 300);
  1272. sampled_guards_update_from_consensus(gs);
  1273. /* guards 1..5 are now unlisted; 0,6,7.. are listed. */
  1274. tt_i64_op(smartlist_len(gs->sampled_entry_guards), OP_EQ, n_sampled_pre);
  1275. for (i = 1; i < 6; ++i) {
  1276. entry_guard_t *g = smartlist_get(gs->sampled_entry_guards, i);
  1277. tt_assert(! g->currently_listed);
  1278. if (i == 5)
  1279. tt_i64_op(g->unlisted_since_date, OP_EQ, start+300);
  1280. else
  1281. tt_i64_op(g->unlisted_since_date, OP_EQ, start+60);
  1282. }
  1283. for (i = 0; i < n_sampled_pre; i = (!i) ? 6 : i+1) { /* 0,6,7,8, ... */
  1284. entry_guard_t *g = smartlist_get(gs->sampled_entry_guards, i);
  1285. tt_assert(g->currently_listed);
  1286. tt_i64_op(g->unlisted_since_date, OP_EQ, 0);
  1287. }
  1288. done:
  1289. tor_free(ns_tmp); /* in case we couldn't put it back */
  1290. guard_selection_free(gs);
  1291. UNMOCK(randomize_time);
  1292. }
  1293. static void
  1294. test_entry_guard_update_from_consensus_repair(void *arg)
  1295. {
  1296. /* Here we'll make sure that our code to repair the unlisted-since
  1297. * times is correct. */
  1298. (void)arg;
  1299. int i;
  1300. time_t start = approx_time();
  1301. guard_selection_t *gs = guard_selection_new("default");
  1302. /* Don't randomly backdate stuff; it will make correctness harder to check.*/
  1303. MOCK(randomize_time, mock_randomize_time_no_randomization);
  1304. /* First, sample some guards. */
  1305. entry_guards_expand_sample(gs);
  1306. int n_sampled_pre = smartlist_len(gs->sampled_entry_guards);
  1307. int n_filtered_pre = num_reachable_filtered_guards(gs);
  1308. tt_i64_op(n_sampled_pre, OP_EQ, n_filtered_pre);
  1309. tt_i64_op(n_sampled_pre, OP_GT, 10);
  1310. /* Now corrupt the list a bit. Call some unlisted-since-never, and some
  1311. * listed-and-unlisted-since-a-time. */
  1312. update_approx_time(start + 300);
  1313. for (i = 0; i < 3; ++i) {
  1314. /* these will get a date. */
  1315. entry_guard_t *g = smartlist_get(gs->sampled_entry_guards, i);
  1316. node_t *n = (node_t*) bfn_mock_node_get_by_id(g->identity);
  1317. n->is_possible_guard = 0;
  1318. g->currently_listed = 0;
  1319. }
  1320. for (i = 3; i < 6; ++i) {
  1321. /* these will become listed. */
  1322. entry_guard_t *g = smartlist_get(gs->sampled_entry_guards, i);
  1323. g->unlisted_since_date = start+100;
  1324. }
  1325. setup_full_capture_of_logs(LOG_WARN);
  1326. sampled_guards_update_from_consensus(gs);
  1327. expect_log_msg_containing(
  1328. "was listed, but with unlisted_since_date set");
  1329. expect_log_msg_containing(
  1330. "was unlisted, but with unlisted_since_date unset");
  1331. teardown_capture_of_logs();
  1332. tt_int_op(smartlist_len(gs->sampled_entry_guards), OP_EQ, n_sampled_pre);
  1333. tt_int_op(num_reachable_filtered_guards(gs), OP_EQ, n_filtered_pre - 3);
  1334. for (i = 3; i < n_sampled_pre; ++i) {
  1335. /* these will become listed. */
  1336. entry_guard_t *g = smartlist_get(gs->sampled_entry_guards, i);
  1337. if (i < 3) {
  1338. tt_assert(! g->currently_listed);
  1339. tt_i64_op(g->unlisted_since_date, OP_EQ, start+300);
  1340. } else {
  1341. tt_assert(g->currently_listed);
  1342. tt_i64_op(g->unlisted_since_date, OP_EQ, 0);
  1343. }
  1344. }
  1345. done:
  1346. teardown_capture_of_logs();
  1347. guard_selection_free(gs);
  1348. UNMOCK(randomize_time);
  1349. }
  1350. static void
  1351. test_entry_guard_update_from_consensus_remove(void *arg)
  1352. {
  1353. /* Now let's check the logic responsible for removing guards from the
  1354. * sample entirely. */
  1355. (void)arg;
  1356. //int i;
  1357. guard_selection_t *gs = guard_selection_new("default");
  1358. smartlist_t *keep_ids = smartlist_new();
  1359. smartlist_t *remove_ids = smartlist_new();
  1360. /* Don't randomly backdate stuff; it will make correctness harder to check.*/
  1361. MOCK(randomize_time, mock_randomize_time_no_randomization);
  1362. /* First, sample some guards. */
  1363. entry_guards_expand_sample(gs);
  1364. int n_sampled_pre = smartlist_len(gs->sampled_entry_guards);
  1365. int n_filtered_pre = num_reachable_filtered_guards(gs);
  1366. tt_i64_op(n_sampled_pre, OP_EQ, n_filtered_pre);
  1367. tt_i64_op(n_sampled_pre, OP_GT, 10);
  1368. const time_t one_day_ago = approx_time() - 1*24*60*60;
  1369. const time_t one_year_ago = approx_time() - 365*24*60*60;
  1370. const time_t two_years_ago = approx_time() - 2*365*24*60*60;
  1371. /* 0: unlisted for a day. (keep this) */
  1372. {
  1373. entry_guard_t *g = smartlist_get(gs->sampled_entry_guards, 0);
  1374. node_t *n = (node_t*) bfn_mock_node_get_by_id(g->identity);
  1375. n->is_possible_guard = 0;
  1376. g->currently_listed = 0;
  1377. g->unlisted_since_date = one_day_ago;
  1378. smartlist_add(keep_ids, tor_memdup(g->identity, 20));
  1379. }
  1380. /* 1: unlisted for a year. (remove this) */
  1381. {
  1382. entry_guard_t *g = smartlist_get(gs->sampled_entry_guards, 1);
  1383. node_t *n = (node_t*) bfn_mock_node_get_by_id(g->identity);
  1384. n->is_possible_guard = 0;
  1385. g->currently_listed = 0;
  1386. g->unlisted_since_date = one_year_ago;
  1387. smartlist_add(remove_ids, tor_memdup(g->identity, 20));
  1388. }
  1389. /* 2: added a day ago, never confirmed. (keep this) */
  1390. {
  1391. entry_guard_t *g = smartlist_get(gs->sampled_entry_guards, 2);
  1392. g->sampled_on_date = one_day_ago;
  1393. smartlist_add(keep_ids, tor_memdup(g->identity, 20));
  1394. }
  1395. /* 3: added a year ago, never confirmed. (remove this) */
  1396. {
  1397. entry_guard_t *g = smartlist_get(gs->sampled_entry_guards, 3);
  1398. g->sampled_on_date = one_year_ago;
  1399. smartlist_add(remove_ids, tor_memdup(g->identity, 20));
  1400. }
  1401. /* 4: added two year ago, confirmed yesterday, primary. (keep this.) */
  1402. {
  1403. entry_guard_t *g = smartlist_get(gs->sampled_entry_guards, 4);
  1404. g->sampled_on_date = one_year_ago;
  1405. g->confirmed_on_date = one_day_ago;
  1406. g->confirmed_idx = 0;
  1407. g->is_primary = 1;
  1408. smartlist_add(gs->confirmed_entry_guards, g);
  1409. smartlist_add(gs->primary_entry_guards, g);
  1410. smartlist_add(keep_ids, tor_memdup(g->identity, 20));
  1411. }
  1412. /* 5: added two years ago, confirmed a year ago, primary. (remove this) */
  1413. {
  1414. entry_guard_t *g = smartlist_get(gs->sampled_entry_guards, 5);
  1415. g->sampled_on_date = two_years_ago;
  1416. g->confirmed_on_date = one_year_ago;
  1417. g->confirmed_idx = 1;
  1418. g->is_primary = 1;
  1419. smartlist_add(gs->confirmed_entry_guards, g);
  1420. smartlist_add(gs->primary_entry_guards, g);
  1421. smartlist_add(remove_ids, tor_memdup(g->identity, 20));
  1422. }
  1423. sampled_guards_update_from_consensus(gs);
  1424. /* Did we remove the right ones? */
  1425. SMARTLIST_FOREACH(keep_ids, uint8_t *, id, {
  1426. tt_assert(get_sampled_guard_with_id(gs, id) != NULL);
  1427. });
  1428. SMARTLIST_FOREACH(remove_ids, uint8_t *, id, {
  1429. tt_want(get_sampled_guard_with_id(gs, id) == NULL);
  1430. });
  1431. /* Did we remove the right number? */
  1432. tt_int_op(smartlist_len(gs->sampled_entry_guards), OP_EQ, n_sampled_pre - 3);
  1433. done:
  1434. guard_selection_free(gs);
  1435. UNMOCK(randomize_time);
  1436. SMARTLIST_FOREACH(keep_ids, char *, cp, tor_free(cp));
  1437. SMARTLIST_FOREACH(remove_ids, char *, cp, tor_free(cp));
  1438. smartlist_free(keep_ids);
  1439. smartlist_free(remove_ids);
  1440. }
  1441. static void
  1442. test_entry_guard_confirming_guards(void *arg)
  1443. {
  1444. (void)arg;
  1445. /* Now let's check the logic responsible for manipulating the list
  1446. * of confirmed guards */
  1447. guard_selection_t *gs = guard_selection_new("default");
  1448. MOCK(randomize_time, mock_randomize_time_no_randomization);
  1449. /* Create the sample. */
  1450. entry_guards_expand_sample(gs);
  1451. /* Confirm a few guards. */
  1452. time_t start = approx_time();
  1453. entry_guard_t *g1 = smartlist_get(gs->sampled_entry_guards, 0);
  1454. entry_guard_t *g2 = smartlist_get(gs->sampled_entry_guards, 1);
  1455. entry_guard_t *g3 = smartlist_get(gs->sampled_entry_guards, 8);
  1456. make_guard_confirmed(gs, g2);
  1457. update_approx_time(start + 10);
  1458. make_guard_confirmed(gs, g1);
  1459. make_guard_confirmed(gs, g3);
  1460. /* Were the correct dates and indices fed in? */
  1461. tt_int_op(g1->confirmed_idx, OP_EQ, 1);
  1462. tt_int_op(g2->confirmed_idx, OP_EQ, 0);
  1463. tt_int_op(g3->confirmed_idx, OP_EQ, 2);
  1464. tt_i64_op(g1->confirmed_on_date, OP_EQ, start+10);
  1465. tt_i64_op(g2->confirmed_on_date, OP_EQ, start);
  1466. tt_i64_op(g3->confirmed_on_date, OP_EQ, start+10);
  1467. tt_ptr_op(smartlist_get(gs->confirmed_entry_guards, 0), OP_EQ, g2);
  1468. tt_ptr_op(smartlist_get(gs->confirmed_entry_guards, 1), OP_EQ, g1);
  1469. tt_ptr_op(smartlist_get(gs->confirmed_entry_guards, 2), OP_EQ, g3);
  1470. /* Now make sure we can regenerate the confirmed_entry_guards list. */
  1471. smartlist_clear(gs->confirmed_entry_guards);
  1472. g2->confirmed_idx = 0;
  1473. g1->confirmed_idx = 10;
  1474. g3->confirmed_idx = 100;
  1475. entry_guards_update_confirmed(gs);
  1476. tt_int_op(g1->confirmed_idx, OP_EQ, 1);
  1477. tt_int_op(g2->confirmed_idx, OP_EQ, 0);
  1478. tt_int_op(g3->confirmed_idx, OP_EQ, 2);
  1479. tt_ptr_op(smartlist_get(gs->confirmed_entry_guards, 0), OP_EQ, g2);
  1480. tt_ptr_op(smartlist_get(gs->confirmed_entry_guards, 1), OP_EQ, g1);
  1481. tt_ptr_op(smartlist_get(gs->confirmed_entry_guards, 2), OP_EQ, g3);
  1482. /* Now make sure we can regenerate the confirmed_entry_guards list if
  1483. * the indices are messed up. */
  1484. g1->confirmed_idx = g2->confirmed_idx = g3->confirmed_idx = 999;
  1485. smartlist_clear(gs->confirmed_entry_guards);
  1486. entry_guards_update_confirmed(gs);
  1487. tt_int_op(g1->confirmed_idx, OP_GE, 0);
  1488. tt_int_op(g2->confirmed_idx, OP_GE, 0);
  1489. tt_int_op(g3->confirmed_idx, OP_GE, 0);
  1490. tt_int_op(g1->confirmed_idx, OP_LE, 2);
  1491. tt_int_op(g2->confirmed_idx, OP_LE, 2);
  1492. tt_int_op(g3->confirmed_idx, OP_LE, 2);
  1493. g1 = smartlist_get(gs->confirmed_entry_guards, 0);
  1494. g2 = smartlist_get(gs->confirmed_entry_guards, 1);
  1495. g3 = smartlist_get(gs->confirmed_entry_guards, 2);
  1496. tt_int_op(g1->confirmed_idx, OP_EQ, 0);
  1497. tt_int_op(g2->confirmed_idx, OP_EQ, 1);
  1498. tt_int_op(g3->confirmed_idx, OP_EQ, 2);
  1499. tt_assert(g1 != g2);
  1500. tt_assert(g1 != g3);
  1501. tt_assert(g2 != g3);
  1502. done:
  1503. UNMOCK(randomize_time);
  1504. guard_selection_free(gs);
  1505. }
  1506. static void
  1507. test_entry_guard_sample_reachable_filtered(void *arg)
  1508. {
  1509. (void)arg;
  1510. guard_selection_t *gs = guard_selection_new("default");
  1511. entry_guards_expand_sample(gs);
  1512. const int N = 10000;
  1513. bitarray_t *selected = NULL;
  1514. int i, j;
  1515. /* We've got a sampled list now; let's make one non-usable-filtered; some
  1516. * confirmed, some primary, some pending.
  1517. */
  1518. int n_guards = smartlist_len(gs->sampled_entry_guards);
  1519. tt_int_op(n_guards, OP_GT, 10);
  1520. entry_guard_t *g;
  1521. g = smartlist_get(gs->sampled_entry_guards, 0);
  1522. g->is_pending = 1;
  1523. g = smartlist_get(gs->sampled_entry_guards, 1);
  1524. make_guard_confirmed(gs, g);
  1525. g = smartlist_get(gs->sampled_entry_guards, 2);
  1526. g->is_primary = 1;
  1527. g = smartlist_get(gs->sampled_entry_guards, 3);
  1528. g->pb.path_bias_disabled = 1;
  1529. entry_guards_update_filtered_sets(gs);
  1530. gs->primary_guards_up_to_date = 1;
  1531. tt_int_op(num_reachable_filtered_guards(gs), OP_EQ, n_guards - 1);
  1532. tt_int_op(smartlist_len(gs->sampled_entry_guards), OP_EQ, n_guards);
  1533. // +1 since the one we made disabled will make another one get added.
  1534. ++n_guards;
  1535. /* Try a bunch of selections. */
  1536. const struct {
  1537. int flag; int idx;
  1538. } tests[] = {
  1539. { 0, -1 },
  1540. { SAMPLE_EXCLUDE_CONFIRMED, 1 },
  1541. { SAMPLE_EXCLUDE_PRIMARY|SAMPLE_NO_UPDATE_PRIMARY, 2 },
  1542. { SAMPLE_EXCLUDE_PENDING, 0 },
  1543. { -1, -1},
  1544. };
  1545. for (j = 0; tests[j].flag >= 0; ++j) {
  1546. selected = bitarray_init_zero(n_guards);
  1547. const int excluded_flags = tests[j].flag;
  1548. const int excluded_idx = tests[j].idx;
  1549. for (i = 0; i < N; ++i) {
  1550. g = sample_reachable_filtered_entry_guards(gs, excluded_flags);
  1551. tor_assert(g);
  1552. int pos = smartlist_pos(gs->sampled_entry_guards, g);
  1553. tt_int_op(smartlist_len(gs->sampled_entry_guards), OP_EQ, n_guards);
  1554. tt_int_op(pos, OP_GE, 0);
  1555. tt_int_op(pos, OP_LT, n_guards);
  1556. bitarray_set(selected, pos);
  1557. }
  1558. for (i = 0; i < n_guards; ++i) {
  1559. const int should_be_set = (i != excluded_idx &&
  1560. i != 3); // filtered out.
  1561. tt_int_op(!!bitarray_is_set(selected, i), OP_EQ, should_be_set);
  1562. }
  1563. bitarray_free(selected);
  1564. selected = NULL;
  1565. }
  1566. done:
  1567. guard_selection_free(gs);
  1568. bitarray_free(selected);
  1569. }
  1570. static void
  1571. test_entry_guard_sample_reachable_filtered_empty(void *arg)
  1572. {
  1573. (void)arg;
  1574. guard_selection_t *gs = guard_selection_new("default");
  1575. /* What if we try to sample from a set of 0? */
  1576. SMARTLIST_FOREACH(big_fake_net_nodes, node_t *, n,
  1577. n->is_possible_guard = 0);
  1578. entry_guard_t *g = sample_reachable_filtered_entry_guards(gs, 0);
  1579. tt_ptr_op(g, OP_EQ, NULL);
  1580. done:
  1581. guard_selection_free(gs);
  1582. }
  1583. static void
  1584. test_entry_guard_retry_unreachable(void *arg)
  1585. {
  1586. (void)arg;
  1587. guard_selection_t *gs = guard_selection_new("default");
  1588. entry_guards_expand_sample(gs);
  1589. /* Let's say that we have two guards, and they're down.
  1590. */
  1591. time_t start = approx_time();;
  1592. entry_guard_t *g1 = smartlist_get(gs->sampled_entry_guards, 0);
  1593. entry_guard_t *g2 = smartlist_get(gs->sampled_entry_guards, 1);
  1594. entry_guard_t *g3 = smartlist_get(gs->sampled_entry_guards, 2);
  1595. g1->is_reachable = GUARD_REACHABLE_NO;
  1596. g2->is_reachable = GUARD_REACHABLE_NO;
  1597. g1->is_primary = 1;
  1598. g1->failing_since = g2->failing_since = start;
  1599. g1->last_tried_to_connect = g2->last_tried_to_connect = start;
  1600. /* Wait 5 minutes. Nothing will get retried. */
  1601. update_approx_time(start + 5 * 60);
  1602. entry_guard_consider_retry(g1);
  1603. entry_guard_consider_retry(g2);
  1604. entry_guard_consider_retry(g3); // just to make sure this doesn't crash.
  1605. tt_int_op(g1->is_reachable, OP_EQ, GUARD_REACHABLE_NO);
  1606. tt_int_op(g2->is_reachable, OP_EQ, GUARD_REACHABLE_NO);
  1607. tt_int_op(g3->is_reachable, OP_EQ, GUARD_REACHABLE_MAYBE);
  1608. /* After 30 min, the primary one gets retried */
  1609. update_approx_time(start + 35 * 60);
  1610. entry_guard_consider_retry(g1);
  1611. entry_guard_consider_retry(g2);
  1612. tt_int_op(g1->is_reachable, OP_EQ, GUARD_REACHABLE_MAYBE);
  1613. tt_int_op(g2->is_reachable, OP_EQ, GUARD_REACHABLE_NO);
  1614. g1->is_reachable = GUARD_REACHABLE_NO;
  1615. g1->last_tried_to_connect = start + 35*60;
  1616. /* After 1 hour, we'll retry the nonprimary one. */
  1617. update_approx_time(start + 61 * 60);
  1618. entry_guard_consider_retry(g1);
  1619. entry_guard_consider_retry(g2);
  1620. tt_int_op(g1->is_reachable, OP_EQ, GUARD_REACHABLE_NO);
  1621. tt_int_op(g2->is_reachable, OP_EQ, GUARD_REACHABLE_MAYBE);
  1622. g2->is_reachable = GUARD_REACHABLE_NO;
  1623. g2->last_tried_to_connect = start + 61*60;
  1624. /* And then the primary one again. */
  1625. update_approx_time(start + 66 * 60);
  1626. entry_guard_consider_retry(g1);
  1627. entry_guard_consider_retry(g2);
  1628. tt_int_op(g1->is_reachable, OP_EQ, GUARD_REACHABLE_MAYBE);
  1629. tt_int_op(g2->is_reachable, OP_EQ, GUARD_REACHABLE_NO);
  1630. done:
  1631. guard_selection_free(gs);
  1632. }
  1633. static void
  1634. test_entry_guard_manage_primary(void *arg)
  1635. {
  1636. (void)arg;
  1637. guard_selection_t *gs = guard_selection_new("default");
  1638. smartlist_t *prev_guards = smartlist_new();
  1639. /* If no guards are confirmed, we should pick a few reachable guards and
  1640. * call them all primary. But not confirmed.*/
  1641. entry_guards_update_primary(gs);
  1642. int n_primary = smartlist_len(gs->primary_entry_guards);
  1643. tt_int_op(n_primary, OP_GE, 1);
  1644. SMARTLIST_FOREACH(gs->primary_entry_guards, entry_guard_t *, g, {
  1645. tt_assert(g->is_primary);
  1646. tt_assert(g->confirmed_idx == -1);
  1647. });
  1648. /* Calling it a second time should leave the guards unchanged. */
  1649. smartlist_add_all(prev_guards, gs->primary_entry_guards);
  1650. entry_guards_update_primary(gs);
  1651. tt_int_op(smartlist_len(gs->primary_entry_guards), OP_EQ, n_primary);
  1652. SMARTLIST_FOREACH(gs->primary_entry_guards, entry_guard_t *, g, {
  1653. tt_ptr_op(g, OP_EQ, smartlist_get(prev_guards, g_sl_idx));
  1654. });
  1655. /* If we have one confirmed guard, that guards becomes the first primary
  1656. * guard, and the other primary guards get kept. */
  1657. /* find a non-primary guard... */
  1658. entry_guard_t *confirmed = NULL;
  1659. SMARTLIST_FOREACH(gs->sampled_entry_guards, entry_guard_t *, g, {
  1660. if (! g->is_primary) {
  1661. confirmed = g;
  1662. break;
  1663. }
  1664. });
  1665. tt_assert(confirmed);
  1666. /* make it confirmed. */
  1667. make_guard_confirmed(gs, confirmed);
  1668. /* update the list... */
  1669. smartlist_clear(prev_guards);
  1670. smartlist_add_all(prev_guards, gs->primary_entry_guards);
  1671. entry_guards_update_primary(gs);
  1672. /* and see what's primary now! */
  1673. tt_int_op(smartlist_len(gs->primary_entry_guards), OP_EQ, n_primary);
  1674. tt_ptr_op(smartlist_get(gs->primary_entry_guards, 0), OP_EQ, confirmed);
  1675. SMARTLIST_FOREACH(gs->primary_entry_guards, entry_guard_t *, g, {
  1676. tt_assert(g->is_primary);
  1677. if (g_sl_idx == 0)
  1678. continue;
  1679. tt_ptr_op(g, OP_EQ, smartlist_get(prev_guards, g_sl_idx - 1));
  1680. });
  1681. {
  1682. entry_guard_t *prev_last_guard = smartlist_get(prev_guards, n_primary-1);
  1683. tt_assert(! prev_last_guard->is_primary);
  1684. }
  1685. /* Calling it a fourth time should leave the guards unchanged. */
  1686. smartlist_clear(prev_guards);
  1687. smartlist_add_all(prev_guards, gs->primary_entry_guards);
  1688. entry_guards_update_primary(gs);
  1689. tt_int_op(smartlist_len(gs->primary_entry_guards), OP_EQ, n_primary);
  1690. SMARTLIST_FOREACH(gs->primary_entry_guards, entry_guard_t *, g, {
  1691. tt_ptr_op(g, OP_EQ, smartlist_get(prev_guards, g_sl_idx));
  1692. });
  1693. done:
  1694. guard_selection_free(gs);
  1695. smartlist_free(prev_guards);
  1696. }
  1697. static void
  1698. test_entry_guard_select_for_circuit_no_confirmed(void *arg)
  1699. {
  1700. /* Simpler cases: no gaurds are confirmed yet. */
  1701. (void)arg;
  1702. guard_selection_t *gs = guard_selection_new("default");
  1703. /* simple starting configuration */
  1704. entry_guards_update_primary(gs);
  1705. unsigned state = 9999;
  1706. entry_guard_t *g = select_entry_guard_for_circuit(gs, &state);
  1707. tt_assert(g);
  1708. tt_assert(g->is_primary);
  1709. tt_int_op(g->confirmed_idx, OP_EQ, -1);
  1710. tt_assert(g->is_pending == 0); // primary implies non-pending.
  1711. tt_uint_op(state, OP_EQ, GUARD_CIRC_STATE_USABLE_ON_COMPLETION);
  1712. tt_i64_op(g->last_tried_to_connect, OP_EQ, approx_time());
  1713. // If we do that again, we should get the same guard.
  1714. entry_guard_t *g2 = select_entry_guard_for_circuit(gs, &state);
  1715. tt_ptr_op(g2, OP_EQ, g);
  1716. // if we mark that guard down, we should get a different primary guard.
  1717. // auto-retry it.
  1718. g->is_reachable = GUARD_REACHABLE_NO;
  1719. g->unreachable_since = approx_time() - 10;
  1720. g->last_tried_to_connect = approx_time() - 10;
  1721. state = 9999;
  1722. g2 = select_entry_guard_for_circuit(gs, &state);
  1723. tt_ptr_op(g2, OP_NE, g);
  1724. tt_assert(g2);
  1725. tt_assert(g2->is_primary);
  1726. tt_int_op(g2->confirmed_idx, OP_EQ, -1);
  1727. tt_assert(g2->is_pending == 0); // primary implies non-pending.
  1728. tt_uint_op(state, OP_EQ, GUARD_CIRC_STATE_USABLE_ON_COMPLETION);
  1729. tt_i64_op(g2->last_tried_to_connect, OP_EQ, approx_time());
  1730. // If we say that the first primary guard was last tried a long time ago, we
  1731. // should get an automatic retry on it.
  1732. g->unreachable_since = approx_time() - 72*60*60;
  1733. g->last_tried_to_connect = approx_time() - 72*60*60;
  1734. state = 9999;
  1735. g2 = select_entry_guard_for_circuit(gs, &state);
  1736. tt_ptr_op(g2, OP_EQ, g);
  1737. tt_assert(g2);
  1738. tt_uint_op(state, OP_EQ, GUARD_CIRC_STATE_USABLE_ON_COMPLETION);
  1739. tt_i64_op(g2->last_tried_to_connect, OP_EQ, approx_time());
  1740. tt_int_op(g2->is_reachable, OP_EQ, GUARD_REACHABLE_MAYBE);
  1741. // And if we mark ALL the primary guards down, we should get another guard
  1742. // at random.
  1743. SMARTLIST_FOREACH(gs->primary_entry_guards, entry_guard_t *, guard, {
  1744. guard->is_reachable = GUARD_REACHABLE_NO;
  1745. guard->last_tried_to_connect = approx_time() - 5;
  1746. guard->unreachable_since = approx_time() - 30;
  1747. });
  1748. state = 9999;
  1749. g2 = select_entry_guard_for_circuit(gs, &state);
  1750. tt_assert(g2);
  1751. tt_assert(!g2->is_primary);
  1752. tt_int_op(g2->confirmed_idx, OP_EQ, -1);
  1753. tt_assert(g2->is_pending == 1);
  1754. tt_uint_op(state, OP_EQ, GUARD_CIRC_STATE_USABLE_IF_NO_BETTER_GUARD);
  1755. tt_i64_op(g2->last_tried_to_connect, OP_EQ, approx_time());
  1756. tt_int_op(g2->is_reachable, OP_EQ, GUARD_REACHABLE_MAYBE);
  1757. // As a bonus, maybe we should be retrying the primary guards. Let's say so.
  1758. mark_primary_guards_maybe_reachable(gs);
  1759. SMARTLIST_FOREACH(gs->primary_entry_guards, entry_guard_t *, guard, {
  1760. tt_int_op(guard->is_reachable, OP_EQ, GUARD_REACHABLE_MAYBE);
  1761. tt_assert(guard->is_usable_filtered_guard == 1);
  1762. // no change to these fields.
  1763. tt_i64_op(guard->last_tried_to_connect, OP_EQ, approx_time() - 5);
  1764. tt_i64_op(guard->unreachable_since, OP_EQ, approx_time() - 30);
  1765. });
  1766. done:
  1767. guard_selection_free(gs);
  1768. }
  1769. static void
  1770. test_entry_guard_select_for_circuit_confirmed(void *arg)
  1771. {
  1772. /* Case 2: if all the primary guards are down, and there are more confirmed
  1773. guards, we use a confirmed guard. */
  1774. (void)arg;
  1775. int i;
  1776. guard_selection_t *gs = guard_selection_new("default");
  1777. const int N_CONFIRMED = 10;
  1778. /* slightly more complicated simple starting configuration */
  1779. entry_guards_update_primary(gs);
  1780. for (i = 0; i < N_CONFIRMED; ++i) {
  1781. entry_guard_t *guard = smartlist_get(gs->sampled_entry_guards, i);
  1782. make_guard_confirmed(gs, guard);
  1783. }
  1784. entry_guards_update_primary(gs); // rebuild the primary list.
  1785. unsigned state = 9999;
  1786. // As above, this gives us a primary guard.
  1787. entry_guard_t *g = select_entry_guard_for_circuit(gs, &state);
  1788. tt_assert(g);
  1789. tt_assert(g->is_primary);
  1790. tt_int_op(g->confirmed_idx, OP_EQ, 0);
  1791. tt_assert(g->is_pending == 0); // primary implies non-pending.
  1792. tt_uint_op(state, OP_EQ, GUARD_CIRC_STATE_USABLE_ON_COMPLETION);
  1793. tt_i64_op(g->last_tried_to_connect, OP_EQ, approx_time());
  1794. tt_ptr_op(g, OP_EQ, smartlist_get(gs->primary_entry_guards, 0));
  1795. // But if we mark all the primary guards down...
  1796. SMARTLIST_FOREACH(gs->primary_entry_guards, entry_guard_t *, guard, {
  1797. guard->last_tried_to_connect = approx_time();
  1798. entry_guards_note_guard_failure(gs, guard);
  1799. });
  1800. // ... we should get a confirmed guard.
  1801. state = 9999;
  1802. g = select_entry_guard_for_circuit(gs, &state);
  1803. tt_assert(g);
  1804. tt_assert(! g->is_primary);
  1805. tt_int_op(g->confirmed_idx, OP_EQ, smartlist_len(gs->primary_entry_guards));
  1806. tt_assert(g->is_pending);
  1807. tt_uint_op(state, OP_EQ, GUARD_CIRC_STATE_USABLE_IF_NO_BETTER_GUARD);
  1808. tt_i64_op(g->last_tried_to_connect, OP_EQ, approx_time());
  1809. // And if we try again, we should get a different confirmed guard, since
  1810. // that one is pending.
  1811. state = 9999;
  1812. entry_guard_t *g2 = select_entry_guard_for_circuit(gs, &state);
  1813. tt_assert(g2);
  1814. tt_assert(! g2->is_primary);
  1815. tt_ptr_op(g2, OP_NE, g);
  1816. tt_int_op(g2->confirmed_idx, OP_EQ,
  1817. smartlist_len(gs->primary_entry_guards)+1);
  1818. tt_assert(g2->is_pending);
  1819. tt_uint_op(state, OP_EQ, GUARD_CIRC_STATE_USABLE_IF_NO_BETTER_GUARD);
  1820. tt_i64_op(g2->last_tried_to_connect, OP_EQ, approx_time());
  1821. // If we make every confirmed guard become pending then we start poking
  1822. // other guards.
  1823. const int n_remaining_confirmed =
  1824. N_CONFIRMED - 2 - smartlist_len(gs->primary_entry_guards);
  1825. for (i = 0; i < n_remaining_confirmed; ++i) {
  1826. g = select_entry_guard_for_circuit(gs, &state);
  1827. tt_int_op(g->confirmed_idx, OP_GE, 0);
  1828. tt_assert(g);
  1829. }
  1830. state = 9999;
  1831. g = select_entry_guard_for_circuit(gs, &state);
  1832. tt_assert(g);
  1833. tt_assert(g->is_pending);
  1834. tt_int_op(g->confirmed_idx, OP_EQ, -1);
  1835. done:
  1836. guard_selection_free(gs);
  1837. }
  1838. static const struct testcase_setup_t fake_network = {
  1839. fake_network_setup, fake_network_cleanup
  1840. };
  1841. static const struct testcase_setup_t big_fake_network = {
  1842. big_fake_network_setup, big_fake_network_cleanup
  1843. };
  1844. #define BFN_TEST(name) \
  1845. { #name, test_entry_guard_ ## name, TT_FORK, &big_fake_network, NULL }
  1846. struct testcase_t entrynodes_tests[] = {
  1847. { "entry_is_time_to_retry", test_entry_is_time_to_retry,
  1848. TT_FORK, NULL, NULL },
  1849. { "choose_random_entry_no_guards", test_choose_random_entry_no_guards,
  1850. TT_FORK, &fake_network, NULL },
  1851. { "choose_random_entry_one_possible_guard",
  1852. test_choose_random_entry_one_possible_guard,
  1853. TT_FORK, &fake_network, NULL },
  1854. { "populate_live_entry_guards_1guard",
  1855. test_populate_live_entry_guards_1guard,
  1856. TT_FORK, &fake_network, NULL },
  1857. { "populate_live_entry_guards_3guards",
  1858. test_populate_live_entry_guards_3guards,
  1859. TT_FORK, &fake_network, NULL },
  1860. { "entry_guards_parse_state_simple",
  1861. test_entry_guards_parse_state_simple,
  1862. TT_FORK, &fake_network, NULL },
  1863. { "entry_guards_parse_state_pathbias",
  1864. test_entry_guards_parse_state_pathbias,
  1865. TT_FORK, &fake_network, NULL },
  1866. { "entry_guards_set_from_config",
  1867. test_entry_guards_set_from_config,
  1868. TT_FORK, &fake_network, NULL },
  1869. { "entry_is_live",
  1870. test_entry_is_live,
  1871. TT_FORK, &fake_network, NULL },
  1872. { "node_preferred_orport",
  1873. test_node_preferred_orport,
  1874. 0, NULL, NULL },
  1875. { "entry_guard_describe", test_entry_guard_describe, 0, NULL, NULL },
  1876. { "randomize_time", test_entry_guard_randomize_time, 0, NULL, NULL },
  1877. { "encode_for_state_minimal",
  1878. test_entry_guard_encode_for_state_minimal, 0, NULL, NULL },
  1879. { "encode_for_state_maximal",
  1880. test_entry_guard_encode_for_state_maximal, 0, NULL, NULL },
  1881. { "parse_from_state_minimal",
  1882. test_entry_guard_parse_from_state_minimal, 0, NULL, NULL },
  1883. { "parse_from_state_maximal",
  1884. test_entry_guard_parse_from_state_maximal, 0, NULL, NULL },
  1885. { "parse_from_state_failure",
  1886. test_entry_guard_parse_from_state_failure, 0, NULL, NULL },
  1887. { "parse_from_state_partial_failure",
  1888. test_entry_guard_parse_from_state_partial_failure, 0, NULL, NULL },
  1889. BFN_TEST(add_single_guard),
  1890. BFN_TEST(node_filter),
  1891. BFN_TEST(expand_sample),
  1892. BFN_TEST(expand_sample_small_net),
  1893. BFN_TEST(update_from_consensus_status),
  1894. BFN_TEST(update_from_consensus_repair),
  1895. BFN_TEST(update_from_consensus_remove),
  1896. BFN_TEST(confirming_guards),
  1897. BFN_TEST(sample_reachable_filtered),
  1898. BFN_TEST(sample_reachable_filtered_empty),
  1899. BFN_TEST(retry_unreachable),
  1900. BFN_TEST(manage_primary),
  1901. BFN_TEST(select_for_circuit_no_confirmed),
  1902. BFN_TEST(select_for_circuit_confirmed),
  1903. END_OF_TESTCASES
  1904. };