test_hs_common.c 67 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830
  1. /* Copyright (c) 2017, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. /**
  4. * \file test_hs_common.c
  5. * \brief Test hidden service common functionalities.
  6. */
  7. #define HS_COMMON_PRIVATE
  8. #define HS_CLIENT_PRIVATE
  9. #define HS_SERVICE_PRIVATE
  10. #define NODELIST_PRIVATE
  11. #include "test.h"
  12. #include "test_helpers.h"
  13. #include "log_test_helpers.h"
  14. #include "hs_test_helpers.h"
  15. #include "connection_edge.h"
  16. #include "crypto_rand.h"
  17. #include "hs_common.h"
  18. #include "hs_client.h"
  19. #include "hs_service.h"
  20. #include "config.h"
  21. #include "networkstatus.h"
  22. #include "directory.h"
  23. #include "dirauth/dirvote.h"
  24. #include "nodelist.h"
  25. #include "routerlist.h"
  26. #include "statefile.h"
  27. #include "circuitlist.h"
  28. #include "dirauth/shared_random.h"
  29. #include "util.h"
  30. #include "voting_schedule.h"
  31. #include "microdesc_st.h"
  32. #include "networkstatus_st.h"
  33. #include "node_st.h"
  34. #include "routerinfo_st.h"
  35. #include "routerstatus_st.h"
  36. /** Test the validation of HS v3 addresses */
  37. static void
  38. test_validate_address(void *arg)
  39. {
  40. int ret;
  41. (void) arg;
  42. /* Address too short and too long. */
  43. setup_full_capture_of_logs(LOG_WARN);
  44. ret = hs_address_is_valid("blah");
  45. tt_int_op(ret, OP_EQ, 0);
  46. expect_log_msg_containing("has an invalid length");
  47. teardown_capture_of_logs();
  48. setup_full_capture_of_logs(LOG_WARN);
  49. ret = hs_address_is_valid(
  50. "p3xnclpu4mu22dwaurjtsybyqk4xfjmcfz6z62yl24uwmhjatiwnlnadb");
  51. tt_int_op(ret, OP_EQ, 0);
  52. expect_log_msg_containing("has an invalid length");
  53. teardown_capture_of_logs();
  54. /* Invalid checksum (taken from prop224) */
  55. setup_full_capture_of_logs(LOG_WARN);
  56. ret = hs_address_is_valid(
  57. "l5satjgud6gucryazcyvyvhuxhr74u6ygigiuyixe3a6ysis67ororad");
  58. tt_int_op(ret, OP_EQ, 0);
  59. expect_log_msg_containing("invalid checksum");
  60. teardown_capture_of_logs();
  61. setup_full_capture_of_logs(LOG_WARN);
  62. ret = hs_address_is_valid(
  63. "btojiu7nu5y5iwut64eufevogqdw4wmqzugnoluw232r4t3ecsfv37ad");
  64. tt_int_op(ret, OP_EQ, 0);
  65. expect_log_msg_containing("invalid checksum");
  66. teardown_capture_of_logs();
  67. /* Non base32 decodable string. */
  68. setup_full_capture_of_logs(LOG_WARN);
  69. ret = hs_address_is_valid(
  70. "????????????????????????????????????????????????????????");
  71. tt_int_op(ret, OP_EQ, 0);
  72. expect_log_msg_containing("can't be decoded");
  73. teardown_capture_of_logs();
  74. /* Valid address. */
  75. ret = hs_address_is_valid(
  76. "25njqamcweflpvkl73j4szahhihoc4xt3ktcgjnpaingr5yhkenl5sid");
  77. tt_int_op(ret, OP_EQ, 1);
  78. done:
  79. ;
  80. }
  81. static int
  82. mock_write_str_to_file(const char *path, const char *str, int bin)
  83. {
  84. (void)bin;
  85. tt_str_op(path, OP_EQ, "/double/five"PATH_SEPARATOR"squared");
  86. tt_str_op(str, OP_EQ,
  87. "25njqamcweflpvkl73j4szahhihoc4xt3ktcgjnpaingr5yhkenl5sid.onion\n");
  88. done:
  89. return 0;
  90. }
  91. /** Test building HS v3 onion addresses. Uses test vectors from the
  92. * ./hs_build_address.py script. */
  93. static void
  94. test_build_address(void *arg)
  95. {
  96. int ret;
  97. char onion_addr[HS_SERVICE_ADDR_LEN_BASE32 + 1];
  98. ed25519_public_key_t pubkey;
  99. /* hex-encoded ed25519 pubkey used in hs_build_address.py */
  100. char pubkey_hex[] =
  101. "d75a980182b10ab7d54bfed3c964073a0ee172f3daa62325af021a68f707511a";
  102. hs_service_t *service = NULL;
  103. (void) arg;
  104. MOCK(write_str_to_file, mock_write_str_to_file);
  105. /* The following has been created with hs_build_address.py script that
  106. * follows proposal 224 specification to build an onion address. */
  107. static const char *test_addr =
  108. "25njqamcweflpvkl73j4szahhihoc4xt3ktcgjnpaingr5yhkenl5sid";
  109. /* Let's try to build the same onion address as the script */
  110. base16_decode((char*)pubkey.pubkey, sizeof(pubkey.pubkey),
  111. pubkey_hex, strlen(pubkey_hex));
  112. hs_build_address(&pubkey, HS_VERSION_THREE, onion_addr);
  113. tt_str_op(test_addr, OP_EQ, onion_addr);
  114. /* Validate that address. */
  115. ret = hs_address_is_valid(onion_addr);
  116. tt_int_op(ret, OP_EQ, 1);
  117. service = tor_malloc_zero(sizeof(hs_service_t));
  118. memcpy(service->onion_address, onion_addr, sizeof(service->onion_address));
  119. tor_asprintf(&service->config.directory_path, "/double/five");
  120. ret = write_address_to_file(service, "squared");
  121. tt_int_op(ret, OP_EQ, 0);
  122. done:
  123. hs_service_free(service);
  124. }
  125. /** Test that our HS time period calculation functions work properly */
  126. static void
  127. test_time_period(void *arg)
  128. {
  129. (void) arg;
  130. uint64_t tn;
  131. int retval;
  132. time_t fake_time, correct_time, start_time;
  133. /* Let's do the example in prop224 section [TIME-PERIODS] */
  134. retval = parse_rfc1123_time("Wed, 13 Apr 2016 11:00:00 UTC",
  135. &fake_time);
  136. tt_int_op(retval, OP_EQ, 0);
  137. /* Check that the time period number is right */
  138. tn = hs_get_time_period_num(fake_time);
  139. tt_u64_op(tn, OP_EQ, 16903);
  140. /* Increase current time to 11:59:59 UTC and check that the time period
  141. number is still the same */
  142. fake_time += 3599;
  143. tn = hs_get_time_period_num(fake_time);
  144. tt_u64_op(tn, OP_EQ, 16903);
  145. { /* Check start time of next time period */
  146. retval = parse_rfc1123_time("Wed, 13 Apr 2016 12:00:00 UTC",
  147. &correct_time);
  148. tt_int_op(retval, OP_EQ, 0);
  149. start_time = hs_get_start_time_of_next_time_period(fake_time);
  150. tt_int_op(start_time, OP_EQ, correct_time);
  151. }
  152. /* Now take time to 12:00:00 UTC and check that the time period rotated */
  153. fake_time += 1;
  154. tn = hs_get_time_period_num(fake_time);
  155. tt_u64_op(tn, OP_EQ, 16904);
  156. /* Now also check our hs_get_next_time_period_num() function */
  157. tn = hs_get_next_time_period_num(fake_time);
  158. tt_u64_op(tn, OP_EQ, 16905);
  159. { /* Check start time of next time period again */
  160. retval = parse_rfc1123_time("Wed, 14 Apr 2016 12:00:00 UTC",
  161. &correct_time);
  162. tt_int_op(retval, OP_EQ, 0);
  163. start_time = hs_get_start_time_of_next_time_period(fake_time);
  164. tt_int_op(start_time, OP_EQ, correct_time);
  165. }
  166. /* Now do another sanity check: The time period number at the start of the
  167. * next time period, must be the same time period number as the one returned
  168. * from hs_get_next_time_period_num() */
  169. {
  170. time_t next_tp_start = hs_get_start_time_of_next_time_period(fake_time);
  171. tt_u64_op(hs_get_time_period_num(next_tp_start), OP_EQ,
  172. hs_get_next_time_period_num(fake_time));
  173. }
  174. done:
  175. ;
  176. }
  177. /** Test that we can correctly find the start time of the next time period */
  178. static void
  179. test_start_time_of_next_time_period(void *arg)
  180. {
  181. (void) arg;
  182. int retval;
  183. time_t fake_time;
  184. char tbuf[ISO_TIME_LEN + 1];
  185. time_t next_tp_start_time;
  186. /* Do some basic tests */
  187. retval = parse_rfc1123_time("Wed, 13 Apr 2016 11:00:00 UTC",
  188. &fake_time);
  189. tt_int_op(retval, OP_EQ, 0);
  190. next_tp_start_time = hs_get_start_time_of_next_time_period(fake_time);
  191. /* Compare it with the correct result */
  192. format_iso_time(tbuf, next_tp_start_time);
  193. tt_str_op("2016-04-13 12:00:00", OP_EQ, tbuf);
  194. /* Another test with an edge-case time (start of TP) */
  195. retval = parse_rfc1123_time("Wed, 13 Apr 2016 12:00:00 UTC",
  196. &fake_time);
  197. tt_int_op(retval, OP_EQ, 0);
  198. next_tp_start_time = hs_get_start_time_of_next_time_period(fake_time);
  199. format_iso_time(tbuf, next_tp_start_time);
  200. tt_str_op("2016-04-14 12:00:00", OP_EQ, tbuf);
  201. {
  202. /* Now pretend we are on a testing network and alter the voting schedule to
  203. be every 10 seconds. This means that a time period has length 10*24
  204. seconds (4 minutes). It also means that we apply a rotational offset of
  205. 120 seconds to the time period, so that it starts at 00:02:00 instead of
  206. 00:00:00. */
  207. or_options_t *options = get_options_mutable();
  208. options->TestingTorNetwork = 1;
  209. options->V3AuthVotingInterval = 10;
  210. options->TestingV3AuthInitialVotingInterval = 10;
  211. retval = parse_rfc1123_time("Wed, 13 Apr 2016 00:00:00 UTC",
  212. &fake_time);
  213. tt_int_op(retval, OP_EQ, 0);
  214. next_tp_start_time = hs_get_start_time_of_next_time_period(fake_time);
  215. /* Compare it with the correct result */
  216. format_iso_time(tbuf, next_tp_start_time);
  217. tt_str_op("2016-04-13 00:02:00", OP_EQ, tbuf);
  218. retval = parse_rfc1123_time("Wed, 13 Apr 2016 00:02:00 UTC",
  219. &fake_time);
  220. tt_int_op(retval, OP_EQ, 0);
  221. next_tp_start_time = hs_get_start_time_of_next_time_period(fake_time);
  222. /* Compare it with the correct result */
  223. format_iso_time(tbuf, next_tp_start_time);
  224. tt_str_op("2016-04-13 00:06:00", OP_EQ, tbuf);
  225. }
  226. done:
  227. ;
  228. }
  229. /* Cleanup the global nodelist. It also frees the "md" in the node_t because
  230. * we allocate the memory in helper_add_hsdir_to_networkstatus(). */
  231. static void
  232. cleanup_nodelist(void)
  233. {
  234. smartlist_t *nodelist = nodelist_get_list();
  235. SMARTLIST_FOREACH_BEGIN(nodelist, node_t *, node) {
  236. tor_free(node->md);
  237. node->md = NULL;
  238. } SMARTLIST_FOREACH_END(node);
  239. nodelist_free_all();
  240. }
  241. static void
  242. helper_add_hsdir_to_networkstatus(networkstatus_t *ns,
  243. int identity_idx,
  244. const char *nickname,
  245. int is_hsdir)
  246. {
  247. routerstatus_t *rs = tor_malloc_zero(sizeof(routerstatus_t));
  248. routerinfo_t *ri = tor_malloc_zero(sizeof(routerinfo_t));
  249. uint8_t identity[DIGEST_LEN];
  250. tor_addr_t ipv4_addr;
  251. memset(identity, identity_idx, sizeof(identity));
  252. memcpy(rs->identity_digest, identity, DIGEST_LEN);
  253. rs->is_hs_dir = is_hsdir;
  254. rs->pv.supports_v3_hsdir = 1;
  255. strlcpy(rs->nickname, nickname, sizeof(rs->nickname));
  256. tor_addr_parse(&ipv4_addr, "1.2.3.4");
  257. ri->addr = tor_addr_to_ipv4h(&ipv4_addr);
  258. rs->addr = tor_addr_to_ipv4h(&ipv4_addr);
  259. ri->nickname = tor_strdup(nickname);
  260. ri->protocol_list = tor_strdup("HSDir=1-2 LinkAuth=3");
  261. memcpy(ri->cache_info.identity_digest, identity, DIGEST_LEN);
  262. ri->cache_info.signing_key_cert = tor_malloc_zero(sizeof(tor_cert_t));
  263. /* Needed for the HSDir index computation. */
  264. memset(&ri->cache_info.signing_key_cert->signing_key,
  265. identity_idx, ED25519_PUBKEY_LEN);
  266. tt_assert(nodelist_set_routerinfo(ri, NULL));
  267. node_t *node = node_get_mutable_by_id(ri->cache_info.identity_digest);
  268. tt_assert(node);
  269. node->rs = rs;
  270. /* We need this to exist for node_has_preferred_descriptor() to return
  271. * true. */
  272. node->md = tor_malloc_zero(sizeof(microdesc_t));
  273. /* Do this now the nodelist_set_routerinfo() function needs a "rs" to set
  274. * the indexes which it doesn't have when it is called. */
  275. node_set_hsdir_index(node, ns);
  276. node->ri = NULL;
  277. smartlist_add(ns->routerstatus_list, rs);
  278. done:
  279. routerinfo_free(ri);
  280. }
  281. static networkstatus_t *mock_ns = NULL;
  282. static networkstatus_t *
  283. mock_networkstatus_get_latest_consensus(void)
  284. {
  285. time_t now = approx_time();
  286. /* If initialized, return it */
  287. if (mock_ns) {
  288. return mock_ns;
  289. }
  290. /* Initialize fake consensus */
  291. mock_ns = tor_malloc_zero(sizeof(networkstatus_t));
  292. /* This consensus is live */
  293. mock_ns->valid_after = now-1;
  294. mock_ns->fresh_until = now+1;
  295. mock_ns->valid_until = now+2;
  296. /* Create routerstatus list */
  297. mock_ns->routerstatus_list = smartlist_new();
  298. mock_ns->type = NS_TYPE_CONSENSUS;
  299. return mock_ns;
  300. }
  301. static networkstatus_t *
  302. mock_networkstatus_get_live_consensus(time_t now)
  303. {
  304. (void) now;
  305. tt_assert(mock_ns);
  306. done:
  307. return mock_ns;
  308. }
  309. /** Test the responsible HSDirs calculation function */
  310. static void
  311. test_responsible_hsdirs(void *arg)
  312. {
  313. smartlist_t *responsible_dirs = smartlist_new();
  314. networkstatus_t *ns = NULL;
  315. (void) arg;
  316. hs_init();
  317. MOCK(networkstatus_get_latest_consensus,
  318. mock_networkstatus_get_latest_consensus);
  319. ns = networkstatus_get_latest_consensus();
  320. { /* First router: HSdir */
  321. helper_add_hsdir_to_networkstatus(ns, 1, "igor", 1);
  322. }
  323. { /* Second HSDir */
  324. helper_add_hsdir_to_networkstatus(ns, 2, "victor", 1);
  325. }
  326. { /* Third relay but not HSDir */
  327. helper_add_hsdir_to_networkstatus(ns, 3, "spyro", 0);
  328. }
  329. /* Use a fixed time period and pub key so we always take the same path */
  330. ed25519_public_key_t pubkey;
  331. uint64_t time_period_num = 17653; // 2 May, 2018, 14:00.
  332. memset(&pubkey, 42, sizeof(pubkey));
  333. hs_get_responsible_hsdirs(&pubkey, time_period_num,
  334. 0, 0, responsible_dirs);
  335. /* Make sure that we only found 2 responsible HSDirs.
  336. * The third relay was not an hsdir! */
  337. tt_int_op(smartlist_len(responsible_dirs), OP_EQ, 2);
  338. /** TODO: Build a bigger network and do more tests here */
  339. done:
  340. SMARTLIST_FOREACH(ns->routerstatus_list,
  341. routerstatus_t *, rs, routerstatus_free(rs));
  342. smartlist_free(responsible_dirs);
  343. smartlist_clear(ns->routerstatus_list);
  344. networkstatus_vote_free(mock_ns);
  345. cleanup_nodelist();
  346. }
  347. static void
  348. mock_directory_initiate_request(directory_request_t *req)
  349. {
  350. (void)req;
  351. return;
  352. }
  353. static int
  354. mock_hs_desc_encode_descriptor(const hs_descriptor_t *desc,
  355. const ed25519_keypair_t *signing_kp,
  356. char **encoded_out)
  357. {
  358. (void)desc;
  359. (void)signing_kp;
  360. tor_asprintf(encoded_out, "lulu");
  361. return 0;
  362. }
  363. static or_state_t dummy_state;
  364. /* Mock function to get fake or state (used for rev counters) */
  365. static or_state_t *
  366. get_or_state_replacement(void)
  367. {
  368. return &dummy_state;
  369. }
  370. static int
  371. mock_router_have_minimum_dir_info(void)
  372. {
  373. return 1;
  374. }
  375. /** Test that we correctly detect when the HSDir hash ring changes so that we
  376. * reupload our descriptor. */
  377. static void
  378. test_desc_reupload_logic(void *arg)
  379. {
  380. networkstatus_t *ns = NULL;
  381. (void) arg;
  382. hs_init();
  383. MOCK(router_have_minimum_dir_info,
  384. mock_router_have_minimum_dir_info);
  385. MOCK(get_or_state,
  386. get_or_state_replacement);
  387. MOCK(networkstatus_get_latest_consensus,
  388. mock_networkstatus_get_latest_consensus);
  389. MOCK(directory_initiate_request,
  390. mock_directory_initiate_request);
  391. MOCK(hs_desc_encode_descriptor,
  392. mock_hs_desc_encode_descriptor);
  393. ns = networkstatus_get_latest_consensus();
  394. /** Test logic:
  395. * 1) Upload descriptor to HSDirs
  396. * CHECK that previous_hsdirs list was populated.
  397. * 2) Then call router_dir_info_changed() without an HSDir set change.
  398. * CHECK that no reuplod occurs.
  399. * 3) Now change the HSDir set, and call dir_info_changed() again.
  400. * CHECK that reupload occurs.
  401. * 4) Finally call service_desc_schedule_upload().
  402. * CHECK that previous_hsdirs list was cleared.
  403. **/
  404. /* Let's start by building our descriptor and service */
  405. hs_service_descriptor_t *desc = service_descriptor_new();
  406. hs_service_t *service = NULL;
  407. /* hex-encoded ed25519 pubkey used in hs_build_address.py */
  408. char pubkey_hex[] =
  409. "d75a980182b10ab7d54bfed3c964073a0ee172f3daa62325af021a68f707511a";
  410. char onion_addr[HS_SERVICE_ADDR_LEN_BASE32 + 1];
  411. ed25519_public_key_t pubkey;
  412. base16_decode((char*)pubkey.pubkey, sizeof(pubkey.pubkey),
  413. pubkey_hex, strlen(pubkey_hex));
  414. hs_build_address(&pubkey, HS_VERSION_THREE, onion_addr);
  415. service = tor_malloc_zero(sizeof(hs_service_t));
  416. memcpy(service->onion_address, onion_addr, sizeof(service->onion_address));
  417. ed25519_secret_key_generate(&service->keys.identity_sk, 0);
  418. ed25519_public_key_generate(&service->keys.identity_pk,
  419. &service->keys.identity_sk);
  420. service->desc_current = desc;
  421. /* Also add service to service map */
  422. hs_service_ht *service_map = get_hs_service_map();
  423. tt_assert(service_map);
  424. tt_int_op(hs_service_get_num_services(), OP_EQ, 0);
  425. register_service(service_map, service);
  426. tt_int_op(hs_service_get_num_services(), OP_EQ, 1);
  427. /* Now let's create our hash ring: */
  428. {
  429. helper_add_hsdir_to_networkstatus(ns, 1, "dingus", 1);
  430. helper_add_hsdir_to_networkstatus(ns, 2, "clive", 1);
  431. helper_add_hsdir_to_networkstatus(ns, 3, "aaron", 1);
  432. helper_add_hsdir_to_networkstatus(ns, 4, "lizzie", 1);
  433. helper_add_hsdir_to_networkstatus(ns, 5, "daewon", 1);
  434. helper_add_hsdir_to_networkstatus(ns, 6, "clarke", 1);
  435. }
  436. /* Now let's upload our desc to all hsdirs */
  437. upload_descriptor_to_all(service, desc);
  438. /* Check that previous hsdirs were populated */
  439. tt_int_op(smartlist_len(desc->previous_hsdirs), OP_EQ, 6);
  440. /* Poison next upload time so that we can see if it was changed by
  441. * router_dir_info_changed(). No changes in hash ring so far, so the upload
  442. * time should stay as is. */
  443. desc->next_upload_time = 42;
  444. router_dir_info_changed();
  445. tt_int_op(desc->next_upload_time, OP_EQ, 42);
  446. /* Now change the HSDir hash ring by swapping nora for aaron.
  447. * Start by clearing the hash ring */
  448. {
  449. SMARTLIST_FOREACH(ns->routerstatus_list,
  450. routerstatus_t *, rs, routerstatus_free(rs));
  451. smartlist_clear(ns->routerstatus_list);
  452. cleanup_nodelist();
  453. routerlist_free_all();
  454. }
  455. { /* Now add back all the nodes */
  456. helper_add_hsdir_to_networkstatus(ns, 1, "dingus", 1);
  457. helper_add_hsdir_to_networkstatus(ns, 2, "clive", 1);
  458. helper_add_hsdir_to_networkstatus(ns, 4, "lizzie", 1);
  459. helper_add_hsdir_to_networkstatus(ns, 5, "daewon", 1);
  460. helper_add_hsdir_to_networkstatus(ns, 6, "clarke", 1);
  461. helper_add_hsdir_to_networkstatus(ns, 7, "nora", 1);
  462. }
  463. /* Now call service_desc_hsdirs_changed() and see that it detected the hash
  464. ring change */
  465. time_t now = approx_time();
  466. tt_assert(now);
  467. tt_int_op(service_desc_hsdirs_changed(service, desc), OP_EQ, 1);
  468. tt_int_op(smartlist_len(desc->previous_hsdirs), OP_EQ, 6);
  469. /* Now order another upload and see that we keep having 6 prev hsdirs */
  470. upload_descriptor_to_all(service, desc);
  471. /* Check that previous hsdirs were populated */
  472. tt_int_op(smartlist_len(desc->previous_hsdirs), OP_EQ, 6);
  473. /* Now restore the HSDir hash ring to its original state by swapping back
  474. aaron for nora */
  475. /* First clear up the hash ring */
  476. {
  477. SMARTLIST_FOREACH(ns->routerstatus_list,
  478. routerstatus_t *, rs, routerstatus_free(rs));
  479. smartlist_clear(ns->routerstatus_list);
  480. cleanup_nodelist();
  481. routerlist_free_all();
  482. }
  483. { /* Now populate the hash ring again */
  484. helper_add_hsdir_to_networkstatus(ns, 1, "dingus", 1);
  485. helper_add_hsdir_to_networkstatus(ns, 2, "clive", 1);
  486. helper_add_hsdir_to_networkstatus(ns, 3, "aaron", 1);
  487. helper_add_hsdir_to_networkstatus(ns, 4, "lizzie", 1);
  488. helper_add_hsdir_to_networkstatus(ns, 5, "daewon", 1);
  489. helper_add_hsdir_to_networkstatus(ns, 6, "clarke", 1);
  490. }
  491. /* Check that our algorithm catches this change of hsdirs */
  492. tt_int_op(service_desc_hsdirs_changed(service, desc), OP_EQ, 1);
  493. /* Now pretend that the descriptor changed, and order a reupload to all
  494. HSDirs. Make sure that the set of previous HSDirs was cleared. */
  495. service_desc_schedule_upload(desc, now, 1);
  496. tt_int_op(smartlist_len(desc->previous_hsdirs), OP_EQ, 0);
  497. /* Now reupload again: see that the prev hsdir set got populated again. */
  498. upload_descriptor_to_all(service, desc);
  499. tt_int_op(smartlist_len(desc->previous_hsdirs), OP_EQ, 6);
  500. done:
  501. SMARTLIST_FOREACH(ns->routerstatus_list,
  502. routerstatus_t *, rs, routerstatus_free(rs));
  503. smartlist_clear(ns->routerstatus_list);
  504. networkstatus_vote_free(ns);
  505. cleanup_nodelist();
  506. hs_free_all();
  507. }
  508. /** Test disaster SRV computation and caching */
  509. static void
  510. test_disaster_srv(void *arg)
  511. {
  512. uint8_t *cached_disaster_srv_one = NULL;
  513. uint8_t *cached_disaster_srv_two = NULL;
  514. uint8_t srv_one[DIGEST256_LEN] = {0};
  515. uint8_t srv_two[DIGEST256_LEN] = {0};
  516. uint8_t srv_three[DIGEST256_LEN] = {0};
  517. uint8_t srv_four[DIGEST256_LEN] = {0};
  518. uint8_t srv_five[DIGEST256_LEN] = {0};
  519. (void) arg;
  520. /* Get the cached SRVs: we gonna use them later for verification */
  521. cached_disaster_srv_one = get_first_cached_disaster_srv();
  522. cached_disaster_srv_two = get_second_cached_disaster_srv();
  523. /* Compute some srvs */
  524. get_disaster_srv(1, srv_one);
  525. get_disaster_srv(2, srv_two);
  526. /* Check that the cached ones where updated */
  527. tt_mem_op(cached_disaster_srv_one, OP_EQ, srv_one, DIGEST256_LEN);
  528. tt_mem_op(cached_disaster_srv_two, OP_EQ, srv_two, DIGEST256_LEN);
  529. /* Ask for an SRV that has already been computed */
  530. get_disaster_srv(2, srv_two);
  531. /* and check that the cache entries have not changed */
  532. tt_mem_op(cached_disaster_srv_one, OP_EQ, srv_one, DIGEST256_LEN);
  533. tt_mem_op(cached_disaster_srv_two, OP_EQ, srv_two, DIGEST256_LEN);
  534. /* Ask for a new SRV */
  535. get_disaster_srv(3, srv_three);
  536. tt_mem_op(cached_disaster_srv_one, OP_EQ, srv_three, DIGEST256_LEN);
  537. tt_mem_op(cached_disaster_srv_two, OP_EQ, srv_two, DIGEST256_LEN);
  538. /* Ask for another SRV: none of the original SRVs should now be cached */
  539. get_disaster_srv(4, srv_four);
  540. tt_mem_op(cached_disaster_srv_one, OP_EQ, srv_three, DIGEST256_LEN);
  541. tt_mem_op(cached_disaster_srv_two, OP_EQ, srv_four, DIGEST256_LEN);
  542. /* Ask for yet another SRV */
  543. get_disaster_srv(5, srv_five);
  544. tt_mem_op(cached_disaster_srv_one, OP_EQ, srv_five, DIGEST256_LEN);
  545. tt_mem_op(cached_disaster_srv_two, OP_EQ, srv_four, DIGEST256_LEN);
  546. done:
  547. ;
  548. }
  549. /** Test our HS descriptor request tracker by making various requests and
  550. * checking whether they get tracked properly. */
  551. static void
  552. test_hid_serv_request_tracker(void *arg)
  553. {
  554. (void) arg;
  555. time_t retval;
  556. routerstatus_t *hsdir = NULL, *hsdir2 = NULL, *hsdir3 = NULL;
  557. time_t now = approx_time();
  558. const char *req_key_str_first =
  559. "vd4zb6zesaubtrjvdqcr2w7x7lhw2up4Xnw4526ThUNbL5o1go+EdUuEqlKxHkNbnK41pRzizzs";
  560. const char *req_key_str_second =
  561. "g53o7iavcd62oihswhr24u6czmqws5kpXnw4526ThUNbL5o1go+EdUuEqlKxHkNbnK41pRzizzs";
  562. const char *req_key_str_small = "ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ";
  563. /*************************** basic test *******************************/
  564. /* Get request tracker and make sure it's empty */
  565. strmap_t *request_tracker = get_last_hid_serv_requests();
  566. tt_int_op(strmap_size(request_tracker),OP_EQ, 0);
  567. /* Let's register a hid serv request */
  568. hsdir = tor_malloc_zero(sizeof(routerstatus_t));
  569. memset(hsdir->identity_digest, 'Z', DIGEST_LEN);
  570. retval = hs_lookup_last_hid_serv_request(hsdir, req_key_str_first,
  571. now, 1);
  572. tt_int_op(retval, OP_EQ, now);
  573. tt_int_op(strmap_size(request_tracker),OP_EQ, 1);
  574. /* Let's lookup a non-existent hidserv request */
  575. retval = hs_lookup_last_hid_serv_request(hsdir, req_key_str_second,
  576. now+1, 0);
  577. tt_int_op(retval, OP_EQ, 0);
  578. tt_int_op(strmap_size(request_tracker),OP_EQ, 1);
  579. /* Let's lookup a real hidserv request */
  580. retval = hs_lookup_last_hid_serv_request(hsdir, req_key_str_first,
  581. now+2, 0);
  582. tt_int_op(retval, OP_EQ, now); /* we got it */
  583. tt_int_op(strmap_size(request_tracker),OP_EQ, 1);
  584. /**********************************************************************/
  585. /* Let's add another request for the same HS but on a different HSDir. */
  586. hsdir2 = tor_malloc_zero(sizeof(routerstatus_t));
  587. memset(hsdir2->identity_digest, 2, DIGEST_LEN);
  588. retval = hs_lookup_last_hid_serv_request(hsdir2, req_key_str_first,
  589. now+3, 1);
  590. tt_int_op(retval, OP_EQ, now+3);
  591. tt_int_op(strmap_size(request_tracker),OP_EQ, 2);
  592. /* Check that we can clean the first request based on time */
  593. hs_clean_last_hid_serv_requests(now+3+REND_HID_SERV_DIR_REQUERY_PERIOD);
  594. tt_int_op(strmap_size(request_tracker),OP_EQ, 1);
  595. /* Check that it doesn't exist anymore */
  596. retval = hs_lookup_last_hid_serv_request(hsdir, req_key_str_first,
  597. now+2, 0);
  598. tt_int_op(retval, OP_EQ, 0);
  599. /* Now let's add a smaller req key str */
  600. hsdir3 = tor_malloc_zero(sizeof(routerstatus_t));
  601. memset(hsdir3->identity_digest, 3, DIGEST_LEN);
  602. retval = hs_lookup_last_hid_serv_request(hsdir3, req_key_str_small,
  603. now+4, 1);
  604. tt_int_op(retval, OP_EQ, now+4);
  605. tt_int_op(strmap_size(request_tracker),OP_EQ, 2);
  606. /*************************** deleting entries **************************/
  607. /* Add another request with very short key */
  608. retval = hs_lookup_last_hid_serv_request(hsdir, "l", now, 1);
  609. tt_int_op(retval, OP_EQ, now);
  610. tt_int_op(strmap_size(request_tracker),OP_EQ, 3);
  611. /* Try deleting entries with a dummy key. Check that our previous requests
  612. * are still there */
  613. tor_capture_bugs_(1);
  614. hs_purge_hid_serv_from_last_hid_serv_requests("a");
  615. tt_int_op(strmap_size(request_tracker),OP_EQ, 3);
  616. tor_end_capture_bugs_();
  617. /* Try another dummy key. Check that requests are still there */
  618. {
  619. char dummy[2000];
  620. memset(dummy, 'Z', 2000);
  621. dummy[1999] = '\x00';
  622. hs_purge_hid_serv_from_last_hid_serv_requests(dummy);
  623. tt_int_op(strmap_size(request_tracker),OP_EQ, 3);
  624. }
  625. /* Another dummy key! */
  626. hs_purge_hid_serv_from_last_hid_serv_requests(req_key_str_second);
  627. tt_int_op(strmap_size(request_tracker),OP_EQ, 3);
  628. /* Now actually delete a request! */
  629. hs_purge_hid_serv_from_last_hid_serv_requests(req_key_str_first);
  630. tt_int_op(strmap_size(request_tracker),OP_EQ, 2);
  631. /* Purge it all! */
  632. hs_purge_last_hid_serv_requests();
  633. request_tracker = get_last_hid_serv_requests();
  634. tt_int_op(strmap_size(request_tracker),OP_EQ, 0);
  635. done:
  636. tor_free(hsdir);
  637. tor_free(hsdir2);
  638. tor_free(hsdir3);
  639. }
  640. static void
  641. test_parse_extended_hostname(void *arg)
  642. {
  643. (void) arg;
  644. char address1[] = "fooaddress.onion";
  645. char address2[] = "aaaaaaaaaaaaaaaa.onion";
  646. char address3[] = "fooaddress.exit";
  647. char address4[] = "www.torproject.org";
  648. char address5[] = "foo.abcdefghijklmnop.onion";
  649. char address6[] = "foo.bar.abcdefghijklmnop.onion";
  650. char address7[] = ".abcdefghijklmnop.onion";
  651. char address8[] =
  652. "www.25njqamcweflpvkl73j4szahhihoc4xt3ktcgjnpaingr5yhkenl5sid.onion";
  653. tt_assert(BAD_HOSTNAME == parse_extended_hostname(address1));
  654. tt_assert(ONION_V2_HOSTNAME == parse_extended_hostname(address2));
  655. tt_str_op(address2,OP_EQ, "aaaaaaaaaaaaaaaa");
  656. tt_assert(EXIT_HOSTNAME == parse_extended_hostname(address3));
  657. tt_assert(NORMAL_HOSTNAME == parse_extended_hostname(address4));
  658. tt_assert(ONION_V2_HOSTNAME == parse_extended_hostname(address5));
  659. tt_str_op(address5,OP_EQ, "abcdefghijklmnop");
  660. tt_assert(ONION_V2_HOSTNAME == parse_extended_hostname(address6));
  661. tt_str_op(address6,OP_EQ, "abcdefghijklmnop");
  662. tt_assert(BAD_HOSTNAME == parse_extended_hostname(address7));
  663. tt_assert(ONION_V3_HOSTNAME == parse_extended_hostname(address8));
  664. tt_str_op(address8, OP_EQ,
  665. "25njqamcweflpvkl73j4szahhihoc4xt3ktcgjnpaingr5yhkenl5sid");
  666. done: ;
  667. }
  668. static void
  669. test_time_between_tp_and_srv(void *arg)
  670. {
  671. int ret;
  672. networkstatus_t ns;
  673. (void) arg;
  674. /* This function should be returning true where "^" are:
  675. *
  676. * +------------------------------------------------------------------+
  677. * | |
  678. * | 00:00 12:00 00:00 12:00 00:00 12:00 |
  679. * | SRV#1 TP#1 SRV#2 TP#2 SRV#3 TP#3 |
  680. * | |
  681. * | $==========|-----------$===========|-----------$===========| |
  682. * | ^^^^^^^^^^^^ ^^^^^^^^^^^^ |
  683. * | |
  684. * +------------------------------------------------------------------+
  685. */
  686. ret = parse_rfc1123_time("Sat, 26 Oct 1985 00:00:00 UTC", &ns.valid_after);
  687. tt_int_op(ret, OP_EQ, 0);
  688. ret = parse_rfc1123_time("Sat, 26 Oct 1985 01:00:00 UTC", &ns.fresh_until);
  689. tt_int_op(ret, OP_EQ, 0);
  690. voting_schedule_recalculate_timing(get_options(), ns.valid_after);
  691. ret = hs_in_period_between_tp_and_srv(&ns, 0);
  692. tt_int_op(ret, OP_EQ, 0);
  693. ret = parse_rfc1123_time("Sat, 26 Oct 1985 11:00:00 UTC", &ns.valid_after);
  694. tt_int_op(ret, OP_EQ, 0);
  695. ret = parse_rfc1123_time("Sat, 26 Oct 1985 12:00:00 UTC", &ns.fresh_until);
  696. tt_int_op(ret, OP_EQ, 0);
  697. voting_schedule_recalculate_timing(get_options(), ns.valid_after);
  698. ret = hs_in_period_between_tp_and_srv(&ns, 0);
  699. tt_int_op(ret, OP_EQ, 0);
  700. ret = parse_rfc1123_time("Sat, 26 Oct 1985 12:00:00 UTC", &ns.valid_after);
  701. tt_int_op(ret, OP_EQ, 0);
  702. ret = parse_rfc1123_time("Sat, 26 Oct 1985 13:00:00 UTC", &ns.fresh_until);
  703. tt_int_op(ret, OP_EQ, 0);
  704. voting_schedule_recalculate_timing(get_options(), ns.valid_after);
  705. ret = hs_in_period_between_tp_and_srv(&ns, 0);
  706. tt_int_op(ret, OP_EQ, 1);
  707. ret = parse_rfc1123_time("Sat, 26 Oct 1985 23:00:00 UTC", &ns.valid_after);
  708. tt_int_op(ret, OP_EQ, 0);
  709. ret = parse_rfc1123_time("Sat, 27 Oct 1985 00:00:00 UTC", &ns.fresh_until);
  710. tt_int_op(ret, OP_EQ, 0);
  711. voting_schedule_recalculate_timing(get_options(), ns.valid_after);
  712. ret = hs_in_period_between_tp_and_srv(&ns, 0);
  713. tt_int_op(ret, OP_EQ, 1);
  714. ret = parse_rfc1123_time("Sat, 27 Oct 1985 00:00:00 UTC", &ns.valid_after);
  715. tt_int_op(ret, OP_EQ, 0);
  716. ret = parse_rfc1123_time("Sat, 27 Oct 1985 01:00:00 UTC", &ns.fresh_until);
  717. tt_int_op(ret, OP_EQ, 0);
  718. voting_schedule_recalculate_timing(get_options(), ns.valid_after);
  719. ret = hs_in_period_between_tp_and_srv(&ns, 0);
  720. tt_int_op(ret, OP_EQ, 0);
  721. done:
  722. ;
  723. }
  724. /************ Reachability Test (it is huge) ****************/
  725. /* Simulate different consensus for client and service. Used by the
  726. * reachability test. The SRV and responsible HSDir list are used by all
  727. * reachability tests so make them common to simplify setup and teardown. */
  728. static networkstatus_t *mock_service_ns = NULL;
  729. static networkstatus_t *mock_client_ns = NULL;
  730. static sr_srv_t current_srv, previous_srv;
  731. static smartlist_t *service_responsible_hsdirs = NULL;
  732. static smartlist_t *client_responsible_hsdirs = NULL;
  733. static networkstatus_t *
  734. mock_networkstatus_get_live_consensus_service(time_t now)
  735. {
  736. (void) now;
  737. if (mock_service_ns) {
  738. return mock_service_ns;
  739. }
  740. mock_service_ns = tor_malloc_zero(sizeof(networkstatus_t));
  741. mock_service_ns->routerstatus_list = smartlist_new();
  742. mock_service_ns->type = NS_TYPE_CONSENSUS;
  743. return mock_service_ns;
  744. }
  745. static networkstatus_t *
  746. mock_networkstatus_get_latest_consensus_service(void)
  747. {
  748. return mock_networkstatus_get_live_consensus_service(0);
  749. }
  750. static networkstatus_t *
  751. mock_networkstatus_get_live_consensus_client(time_t now)
  752. {
  753. (void) now;
  754. if (mock_client_ns) {
  755. return mock_client_ns;
  756. }
  757. mock_client_ns = tor_malloc_zero(sizeof(networkstatus_t));
  758. mock_client_ns->routerstatus_list = smartlist_new();
  759. mock_client_ns->type = NS_TYPE_CONSENSUS;
  760. return mock_client_ns;
  761. }
  762. static networkstatus_t *
  763. mock_networkstatus_get_latest_consensus_client(void)
  764. {
  765. return mock_networkstatus_get_live_consensus_client(0);
  766. }
  767. /* Mock function because we are not trying to test the close circuit that does
  768. * an awful lot of checks on the circuit object. */
  769. static void
  770. mock_circuit_mark_for_close(circuit_t *circ, int reason, int line,
  771. const char *file)
  772. {
  773. (void) circ;
  774. (void) reason;
  775. (void) line;
  776. (void) file;
  777. return;
  778. }
  779. /* Initialize a big HSDir V3 hash ring. */
  780. static void
  781. helper_initialize_big_hash_ring(networkstatus_t *ns)
  782. {
  783. int ret;
  784. /* Generate 250 hsdirs! :) */
  785. for (int counter = 1 ; counter < 251 ; counter++) {
  786. /* Let's generate random nickname for each hsdir... */
  787. char nickname_binary[8];
  788. char nickname_str[13] = {0};
  789. crypto_rand(nickname_binary, sizeof(nickname_binary));
  790. ret = base64_encode(nickname_str, sizeof(nickname_str),
  791. nickname_binary, sizeof(nickname_binary), 0);
  792. tt_int_op(ret, OP_EQ, 12);
  793. helper_add_hsdir_to_networkstatus(ns, counter, nickname_str, 1);
  794. }
  795. /* Make sure we have 200 hsdirs in our list */
  796. tt_int_op(smartlist_len(ns->routerstatus_list), OP_EQ, 250);
  797. done:
  798. ;
  799. }
  800. /** Initialize service and publish its descriptor as needed. Return the newly
  801. * allocated service object to the caller. */
  802. static hs_service_t *
  803. helper_init_service(time_t now)
  804. {
  805. int retval;
  806. hs_service_t *service = hs_service_new(get_options());
  807. tt_assert(service);
  808. service->config.version = HS_VERSION_THREE;
  809. ed25519_secret_key_generate(&service->keys.identity_sk, 0);
  810. ed25519_public_key_generate(&service->keys.identity_pk,
  811. &service->keys.identity_sk);
  812. /* Register service to global map. */
  813. retval = register_service(get_hs_service_map(), service);
  814. tt_int_op(retval, OP_EQ, 0);
  815. /* Initialize service descriptor */
  816. build_all_descriptors(now);
  817. tt_assert(service->desc_current);
  818. tt_assert(service->desc_next);
  819. done:
  820. return service;
  821. }
  822. /* Helper function to set the RFC 1123 time string into t. */
  823. static void
  824. set_consensus_times(const char *timestr, time_t *t)
  825. {
  826. tt_assert(timestr);
  827. tt_assert(t);
  828. int ret = parse_rfc1123_time(timestr, t);
  829. tt_int_op(ret, OP_EQ, 0);
  830. done:
  831. return;
  832. }
  833. /* Helper function to cleanup the mock consensus (client and service) */
  834. static void
  835. cleanup_mock_ns(void)
  836. {
  837. if (mock_service_ns) {
  838. SMARTLIST_FOREACH(mock_service_ns->routerstatus_list,
  839. routerstatus_t *, rs, routerstatus_free(rs));
  840. smartlist_clear(mock_service_ns->routerstatus_list);
  841. mock_service_ns->sr_info.current_srv = NULL;
  842. mock_service_ns->sr_info.previous_srv = NULL;
  843. networkstatus_vote_free(mock_service_ns);
  844. mock_service_ns = NULL;
  845. }
  846. if (mock_client_ns) {
  847. SMARTLIST_FOREACH(mock_client_ns->routerstatus_list,
  848. routerstatus_t *, rs, routerstatus_free(rs));
  849. smartlist_clear(mock_client_ns->routerstatus_list);
  850. mock_client_ns->sr_info.current_srv = NULL;
  851. mock_client_ns->sr_info.previous_srv = NULL;
  852. networkstatus_vote_free(mock_client_ns);
  853. mock_client_ns = NULL;
  854. }
  855. }
  856. /* Helper function to setup a reachability test. Once called, the
  857. * cleanup_reachability_test MUST be called at the end. */
  858. static void
  859. setup_reachability_test(void)
  860. {
  861. MOCK(circuit_mark_for_close_, mock_circuit_mark_for_close);
  862. MOCK(get_or_state, get_or_state_replacement);
  863. hs_init();
  864. /* Baseline to start with. */
  865. memset(&current_srv, 0, sizeof(current_srv));
  866. memset(&previous_srv, 1, sizeof(previous_srv));
  867. /* Initialize the consensuses. */
  868. mock_networkstatus_get_latest_consensus_service();
  869. mock_networkstatus_get_latest_consensus_client();
  870. service_responsible_hsdirs = smartlist_new();
  871. client_responsible_hsdirs = smartlist_new();
  872. }
  873. /* Helper function to cleanup a reachability test initial setup. */
  874. static void
  875. cleanup_reachability_test(void)
  876. {
  877. smartlist_free(service_responsible_hsdirs);
  878. service_responsible_hsdirs = NULL;
  879. smartlist_free(client_responsible_hsdirs);
  880. client_responsible_hsdirs = NULL;
  881. hs_free_all();
  882. cleanup_mock_ns();
  883. UNMOCK(get_or_state);
  884. UNMOCK(circuit_mark_for_close_);
  885. }
  886. /* A reachability test always check if the resulting service and client
  887. * responsible HSDir for the given parameters are equal.
  888. *
  889. * Return true iff the same exact nodes are in both list. */
  890. static int
  891. are_responsible_hsdirs_equal(void)
  892. {
  893. int count = 0;
  894. tt_int_op(smartlist_len(client_responsible_hsdirs), OP_EQ, 6);
  895. tt_int_op(smartlist_len(service_responsible_hsdirs), OP_EQ, 8);
  896. SMARTLIST_FOREACH_BEGIN(client_responsible_hsdirs,
  897. const routerstatus_t *, c_rs) {
  898. SMARTLIST_FOREACH_BEGIN(service_responsible_hsdirs,
  899. const routerstatus_t *, s_rs) {
  900. if (tor_memeq(c_rs->identity_digest, s_rs->identity_digest,
  901. DIGEST_LEN)) {
  902. count++;
  903. break;
  904. }
  905. } SMARTLIST_FOREACH_END(s_rs);
  906. } SMARTLIST_FOREACH_END(c_rs);
  907. done:
  908. return (count == 6);
  909. }
  910. /* Tor doesn't use such a function to get the previous HSDir, it is only used
  911. * in node_set_hsdir_index(). We need it here so we can test the reachability
  912. * scenario 6 that requires the previous time period to compute the list of
  913. * responsible HSDir because of the client state timing. */
  914. static uint64_t
  915. get_previous_time_period(time_t now)
  916. {
  917. return hs_get_time_period_num(now) - 1;
  918. }
  919. /* Configuration of a reachability test scenario. */
  920. typedef struct reachability_cfg_t {
  921. /* Consensus timings to be set. They have to be compliant with
  922. * RFC 1123 time format. */
  923. const char *service_valid_after;
  924. const char *service_valid_until;
  925. const char *client_valid_after;
  926. const char *client_valid_until;
  927. /* SRVs that the service and client should use. */
  928. sr_srv_t *service_current_srv;
  929. sr_srv_t *service_previous_srv;
  930. sr_srv_t *client_current_srv;
  931. sr_srv_t *client_previous_srv;
  932. /* A time period function for the service to use for this scenario. For a
  933. * successful reachability test, the client always use the current time
  934. * period thus why no client function. */
  935. uint64_t (*service_time_period_fn)(time_t);
  936. /* Is the client and service expected to be in a new time period. After
  937. * setting the consensus time, the reachability test checks
  938. * hs_in_period_between_tp_and_srv() and test the returned value against
  939. * this. */
  940. unsigned int service_in_new_tp;
  941. unsigned int client_in_new_tp;
  942. /* Some scenario requires a hint that the client, because of its consensus
  943. * time, will request the "next" service descriptor so this indicates if it
  944. * is the case or not. */
  945. unsigned int client_fetch_next_desc;
  946. } reachability_cfg_t;
  947. /* Some defines to help with semantic while reading a configuration below. */
  948. #define NOT_IN_NEW_TP 0
  949. #define IN_NEW_TP 1
  950. #define DONT_NEED_NEXT_DESC 0
  951. #define NEED_NEXT_DESC 1
  952. static reachability_cfg_t reachability_scenarios[] = {
  953. /* Scenario 1
  954. *
  955. * +------------------------------------------------------------------+
  956. * | |
  957. * | 00:00 12:00 00:00 12:00 00:00 12:00 |
  958. * | SRV#1 TP#1 SRV#2 TP#2 SRV#3 TP#3 |
  959. * | |
  960. * | $==========|-----------$===========|-----------$===========| |
  961. * | ^ ^ |
  962. * | S C |
  963. * +------------------------------------------------------------------+
  964. *
  965. * S: Service, C: Client
  966. *
  967. * Service consensus valid_after time is set to 13:00 and client to 15:00,
  968. * both are after TP#1 thus have access to SRV#1. Service and client should
  969. * be using TP#1.
  970. */
  971. { "Sat, 26 Oct 1985 13:00:00 UTC", /* Service valid_after */
  972. "Sat, 26 Oct 1985 14:00:00 UTC", /* Service valid_until */
  973. "Sat, 26 Oct 1985 15:00:00 UTC", /* Client valid_after */
  974. "Sat, 26 Oct 1985 16:00:00 UTC", /* Client valid_until. */
  975. &current_srv, NULL, /* Service current and previous SRV */
  976. &current_srv, NULL, /* Client current and previous SRV */
  977. hs_get_time_period_num, /* Service time period function. */
  978. IN_NEW_TP, /* Is service in new TP? */
  979. IN_NEW_TP, /* Is client in new TP? */
  980. NEED_NEXT_DESC },
  981. /* Scenario 2
  982. *
  983. * +------------------------------------------------------------------+
  984. * | |
  985. * | 00:00 12:00 00:00 12:00 00:00 12:00 |
  986. * | SRV#1 TP#1 SRV#2 TP#2 SRV#3 TP#3 |
  987. * | |
  988. * | $==========|-----------$===========|-----------$===========| |
  989. * | ^ ^ |
  990. * | S C |
  991. * +------------------------------------------------------------------+
  992. *
  993. * S: Service, C: Client
  994. *
  995. * Service consensus valid_after time is set to 23:00 and client to 01:00,
  996. * which makes the client after the SRV#2 and the service just before. The
  997. * service should only be using TP#1. The client should be using TP#1.
  998. */
  999. { "Sat, 26 Oct 1985 23:00:00 UTC", /* Service valid_after */
  1000. "Sat, 27 Oct 1985 00:00:00 UTC", /* Service valid_until */
  1001. "Sat, 27 Oct 1985 01:00:00 UTC", /* Client valid_after */
  1002. "Sat, 27 Oct 1985 02:00:00 UTC", /* Client valid_until. */
  1003. &previous_srv, NULL, /* Service current and previous SRV */
  1004. &current_srv, &previous_srv, /* Client current and previous SRV */
  1005. hs_get_time_period_num, /* Service time period function. */
  1006. IN_NEW_TP, /* Is service in new TP? */
  1007. NOT_IN_NEW_TP, /* Is client in new TP? */
  1008. NEED_NEXT_DESC },
  1009. /* Scenario 3
  1010. *
  1011. * +------------------------------------------------------------------+
  1012. * | |
  1013. * | 00:00 12:00 00:00 12:00 00:00 12:00 |
  1014. * | SRV#1 TP#1 SRV#2 TP#2 SRV#3 TP#3 |
  1015. * | |
  1016. * | $==========|-----------$===========|----------$===========| |
  1017. * | ^ ^ |
  1018. * | S C |
  1019. * +------------------------------------------------------------------+
  1020. *
  1021. * S: Service, C: Client
  1022. *
  1023. * Service consensus valid_after time is set to 03:00 and client to 05:00,
  1024. * which makes both after SRV#2. The service should be using TP#1 as its
  1025. * current time period. The client should be using TP#1.
  1026. */
  1027. { "Sat, 27 Oct 1985 03:00:00 UTC", /* Service valid_after */
  1028. "Sat, 27 Oct 1985 04:00:00 UTC", /* Service valid_until */
  1029. "Sat, 27 Oct 1985 05:00:00 UTC", /* Client valid_after */
  1030. "Sat, 27 Oct 1985 06:00:00 UTC", /* Client valid_until. */
  1031. &current_srv, &previous_srv, /* Service current and previous SRV */
  1032. &current_srv, &previous_srv, /* Client current and previous SRV */
  1033. hs_get_time_period_num, /* Service time period function. */
  1034. NOT_IN_NEW_TP, /* Is service in new TP? */
  1035. NOT_IN_NEW_TP, /* Is client in new TP? */
  1036. DONT_NEED_NEXT_DESC },
  1037. /* Scenario 4
  1038. *
  1039. * +------------------------------------------------------------------+
  1040. * | |
  1041. * | 00:00 12:00 00:00 12:00 00:00 12:00 |
  1042. * | SRV#1 TP#1 SRV#2 TP#2 SRV#3 TP#3 |
  1043. * | |
  1044. * | $==========|-----------$===========|-----------$===========| |
  1045. * | ^ ^ |
  1046. * | S C |
  1047. * +------------------------------------------------------------------+
  1048. *
  1049. * S: Service, C: Client
  1050. *
  1051. * Service consensus valid_after time is set to 11:00 and client to 13:00,
  1052. * which makes the service before TP#2 and the client just after. The
  1053. * service should be using TP#1 as its current time period and TP#2 as the
  1054. * next. The client should be using TP#2 time period.
  1055. */
  1056. { "Sat, 27 Oct 1985 11:00:00 UTC", /* Service valid_after */
  1057. "Sat, 27 Oct 1985 12:00:00 UTC", /* Service valid_until */
  1058. "Sat, 27 Oct 1985 13:00:00 UTC", /* Client valid_after */
  1059. "Sat, 27 Oct 1985 14:00:00 UTC", /* Client valid_until. */
  1060. &current_srv, &previous_srv, /* Service current and previous SRV */
  1061. &current_srv, &previous_srv, /* Client current and previous SRV */
  1062. hs_get_next_time_period_num, /* Service time period function. */
  1063. NOT_IN_NEW_TP, /* Is service in new TP? */
  1064. IN_NEW_TP, /* Is client in new TP? */
  1065. NEED_NEXT_DESC },
  1066. /* Scenario 5
  1067. *
  1068. * +------------------------------------------------------------------+
  1069. * | |
  1070. * | 00:00 12:00 00:00 12:00 00:00 12:00 |
  1071. * | SRV#1 TP#1 SRV#2 TP#2 SRV#3 TP#3 |
  1072. * | |
  1073. * | $==========|-----------$===========|-----------$===========| |
  1074. * | ^ ^ |
  1075. * | C S |
  1076. * +------------------------------------------------------------------+
  1077. *
  1078. * S: Service, C: Client
  1079. *
  1080. * Service consensus valid_after time is set to 01:00 and client to 23:00,
  1081. * which makes the service after SRV#2 and the client just before. The
  1082. * service should be using TP#1 as its current time period and TP#2 as the
  1083. * next. The client should be using TP#1 time period.
  1084. */
  1085. { "Sat, 27 Oct 1985 01:00:00 UTC", /* Service valid_after */
  1086. "Sat, 27 Oct 1985 02:00:00 UTC", /* Service valid_until */
  1087. "Sat, 26 Oct 1985 23:00:00 UTC", /* Client valid_after */
  1088. "Sat, 27 Oct 1985 00:00:00 UTC", /* Client valid_until. */
  1089. &current_srv, &previous_srv, /* Service current and previous SRV */
  1090. &previous_srv, NULL, /* Client current and previous SRV */
  1091. hs_get_time_period_num, /* Service time period function. */
  1092. NOT_IN_NEW_TP, /* Is service in new TP? */
  1093. IN_NEW_TP, /* Is client in new TP? */
  1094. DONT_NEED_NEXT_DESC },
  1095. /* Scenario 6
  1096. *
  1097. * +------------------------------------------------------------------+
  1098. * | |
  1099. * | 00:00 12:00 00:00 12:00 00:00 12:00 |
  1100. * | SRV#1 TP#1 SRV#2 TP#2 SRV#3 TP#3 |
  1101. * | |
  1102. * | $==========|-----------$===========|-----------$===========| |
  1103. * | ^ ^ |
  1104. * | C S |
  1105. * +------------------------------------------------------------------+
  1106. *
  1107. * S: Service, C: Client
  1108. *
  1109. * Service consensus valid_after time is set to 13:00 and client to 11:00,
  1110. * which makes the service outside after TP#2 and the client just before.
  1111. * The service should be using TP#1 as its current time period and TP#2 as
  1112. * its next. The client should be using TP#1 time period.
  1113. */
  1114. { "Sat, 27 Oct 1985 13:00:00 UTC", /* Service valid_after */
  1115. "Sat, 27 Oct 1985 14:00:00 UTC", /* Service valid_until */
  1116. "Sat, 27 Oct 1985 11:00:00 UTC", /* Client valid_after */
  1117. "Sat, 27 Oct 1985 12:00:00 UTC", /* Client valid_until. */
  1118. &current_srv, &previous_srv, /* Service current and previous SRV */
  1119. &current_srv, &previous_srv, /* Client current and previous SRV */
  1120. get_previous_time_period, /* Service time period function. */
  1121. IN_NEW_TP, /* Is service in new TP? */
  1122. NOT_IN_NEW_TP, /* Is client in new TP? */
  1123. DONT_NEED_NEXT_DESC },
  1124. /* End marker. */
  1125. { NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0, 0, 0}
  1126. };
  1127. /* Run a single reachability scenario. num_scenario is the corresponding
  1128. * scenario number from the documentation. It is used to log it in case of
  1129. * failure so we know which scenario fails. */
  1130. static int
  1131. run_reachability_scenario(const reachability_cfg_t *cfg, int num_scenario)
  1132. {
  1133. int ret = -1;
  1134. hs_service_t *service;
  1135. uint64_t service_tp, client_tp;
  1136. ed25519_public_key_t service_blinded_pk, client_blinded_pk;
  1137. setup_reachability_test();
  1138. tt_assert(cfg);
  1139. /* Set service consensus time. */
  1140. set_consensus_times(cfg->service_valid_after,
  1141. &mock_service_ns->valid_after);
  1142. set_consensus_times(cfg->service_valid_until,
  1143. &mock_service_ns->valid_until);
  1144. set_consensus_times(cfg->service_valid_until,
  1145. &mock_service_ns->fresh_until);
  1146. voting_schedule_recalculate_timing(get_options(),
  1147. mock_service_ns->valid_after);
  1148. /* Set client consensus time. */
  1149. set_consensus_times(cfg->client_valid_after,
  1150. &mock_client_ns->valid_after);
  1151. set_consensus_times(cfg->client_valid_until,
  1152. &mock_client_ns->valid_until);
  1153. set_consensus_times(cfg->client_valid_until,
  1154. &mock_client_ns->fresh_until);
  1155. voting_schedule_recalculate_timing(get_options(),
  1156. mock_client_ns->valid_after);
  1157. /* New time period checks for this scenario. */
  1158. tt_int_op(hs_in_period_between_tp_and_srv(mock_service_ns, 0), OP_EQ,
  1159. cfg->service_in_new_tp);
  1160. tt_int_op(hs_in_period_between_tp_and_srv(mock_client_ns, 0), OP_EQ,
  1161. cfg->client_in_new_tp);
  1162. /* Set the SRVs for this scenario. */
  1163. mock_client_ns->sr_info.current_srv = cfg->client_current_srv;
  1164. mock_client_ns->sr_info.previous_srv = cfg->client_previous_srv;
  1165. mock_service_ns->sr_info.current_srv = cfg->service_current_srv;
  1166. mock_service_ns->sr_info.previous_srv = cfg->service_previous_srv;
  1167. /* Initialize a service to get keys. */
  1168. service = helper_init_service(time(NULL));
  1169. /*
  1170. * === Client setup ===
  1171. */
  1172. MOCK(networkstatus_get_live_consensus,
  1173. mock_networkstatus_get_live_consensus_client);
  1174. MOCK(networkstatus_get_latest_consensus,
  1175. mock_networkstatus_get_latest_consensus_client);
  1176. /* Make networkstatus_is_live() happy. */
  1177. update_approx_time(mock_client_ns->valid_after);
  1178. /* Initialize a big hashring for this consensus with the hsdir index set. */
  1179. helper_initialize_big_hash_ring(mock_client_ns);
  1180. /* Client ONLY use the current time period. This is the whole point of these
  1181. * reachability test that is to make sure the client can always reach the
  1182. * service using only its current time period. */
  1183. client_tp = hs_get_time_period_num(0);
  1184. hs_build_blinded_pubkey(&service->keys.identity_pk, NULL, 0,
  1185. client_tp, &client_blinded_pk);
  1186. hs_get_responsible_hsdirs(&client_blinded_pk, client_tp, 0, 1,
  1187. client_responsible_hsdirs);
  1188. /* Cleanup the nodelist so we can let the service computes its own set of
  1189. * node with its own hashring. */
  1190. cleanup_nodelist();
  1191. tt_int_op(smartlist_len(client_responsible_hsdirs), OP_EQ, 6);
  1192. UNMOCK(networkstatus_get_latest_consensus);
  1193. UNMOCK(networkstatus_get_live_consensus);
  1194. /*
  1195. * === Service setup ===
  1196. */
  1197. MOCK(networkstatus_get_live_consensus,
  1198. mock_networkstatus_get_live_consensus_service);
  1199. MOCK(networkstatus_get_latest_consensus,
  1200. mock_networkstatus_get_latest_consensus_service);
  1201. /* Make networkstatus_is_live() happy. */
  1202. update_approx_time(mock_service_ns->valid_after);
  1203. /* Initialize a big hashring for this consensus with the hsdir index set. */
  1204. helper_initialize_big_hash_ring(mock_service_ns);
  1205. service_tp = cfg->service_time_period_fn(0);
  1206. hs_build_blinded_pubkey(&service->keys.identity_pk, NULL, 0,
  1207. service_tp, &service_blinded_pk);
  1208. /* A service builds two lists of responsible HSDir, for the current and the
  1209. * next descriptor. Depending on the scenario, the client timing indicate if
  1210. * it is fetching the current or the next descriptor so we use the
  1211. * "client_fetch_next_desc" to know which one the client is trying to get to
  1212. * confirm that the service computes the same hashring for the same blinded
  1213. * key and service time period function. */
  1214. hs_get_responsible_hsdirs(&service_blinded_pk, service_tp,
  1215. cfg->client_fetch_next_desc, 0,
  1216. service_responsible_hsdirs);
  1217. cleanup_nodelist();
  1218. tt_int_op(smartlist_len(service_responsible_hsdirs), OP_EQ, 8);
  1219. UNMOCK(networkstatus_get_latest_consensus);
  1220. UNMOCK(networkstatus_get_live_consensus);
  1221. /* Some testing of the values we just got from the client and service. */
  1222. tt_mem_op(&client_blinded_pk, OP_EQ, &service_blinded_pk,
  1223. ED25519_PUBKEY_LEN);
  1224. tt_int_op(are_responsible_hsdirs_equal(), OP_EQ, 1);
  1225. /* Everything went well. */
  1226. ret = 0;
  1227. done:
  1228. cleanup_reachability_test();
  1229. if (ret == -1) {
  1230. /* Do this so we can know which scenario failed. */
  1231. char msg[32];
  1232. tor_snprintf(msg, sizeof(msg), "Scenario %d failed", num_scenario);
  1233. tt_fail_msg(msg);
  1234. }
  1235. return ret;
  1236. }
  1237. static void
  1238. test_reachability(void *arg)
  1239. {
  1240. (void) arg;
  1241. /* NOTE: An important axiom to understand here is that SRV#N must only be
  1242. * used with TP#N value. For example, SRV#2 with TP#1 should NEVER be used
  1243. * together. The HSDir index computation is based on this axiom.*/
  1244. for (int i = 0; reachability_scenarios[i].service_valid_after; ++i) {
  1245. int ret = run_reachability_scenario(&reachability_scenarios[i], i + 1);
  1246. if (ret < 0) {
  1247. return;
  1248. }
  1249. }
  1250. }
  1251. /** Pick an HSDir for service with <b>onion_identity_pk</b> as a client. Put
  1252. * its identity digest in <b>hsdir_digest_out</b>. */
  1253. static void
  1254. helper_client_pick_hsdir(const ed25519_public_key_t *onion_identity_pk,
  1255. char *hsdir_digest_out)
  1256. {
  1257. tt_assert(onion_identity_pk);
  1258. routerstatus_t *client_hsdir = pick_hsdir_v3(onion_identity_pk);
  1259. tt_assert(client_hsdir);
  1260. digest_to_base64(hsdir_digest_out, client_hsdir->identity_digest);
  1261. done:
  1262. ;
  1263. }
  1264. static void
  1265. test_hs_indexes(void *arg)
  1266. {
  1267. int ret;
  1268. uint64_t period_num = 42;
  1269. ed25519_public_key_t pubkey;
  1270. (void) arg;
  1271. /* Build the hs_index */
  1272. {
  1273. uint8_t hs_index[DIGEST256_LEN];
  1274. const char *b32_test_vector =
  1275. "37e5cbbd56a22823714f18f1623ece5983a0d64c78495a8cfab854245e5f9a8a";
  1276. char test_vector[DIGEST256_LEN];
  1277. ret = base16_decode(test_vector, sizeof(test_vector), b32_test_vector,
  1278. strlen(b32_test_vector));
  1279. tt_int_op(ret, OP_EQ, sizeof(test_vector));
  1280. /* Our test vector uses a public key set to 32 bytes of \x42. */
  1281. memset(&pubkey, '\x42', sizeof(pubkey));
  1282. hs_build_hs_index(1, &pubkey, period_num, hs_index);
  1283. tt_mem_op(hs_index, OP_EQ, test_vector, sizeof(hs_index));
  1284. }
  1285. /* Build the hsdir_index */
  1286. {
  1287. uint8_t srv[DIGEST256_LEN];
  1288. uint8_t hsdir_index[DIGEST256_LEN];
  1289. const char *b32_test_vector =
  1290. "db475361014a09965e7e5e4d4a25b8f8d4b8f16cb1d8a7e95eed50249cc1a2d5";
  1291. char test_vector[DIGEST256_LEN];
  1292. ret = base16_decode(test_vector, sizeof(test_vector), b32_test_vector,
  1293. strlen(b32_test_vector));
  1294. tt_int_op(ret, OP_EQ, sizeof(test_vector));
  1295. /* Our test vector uses a public key set to 32 bytes of \x42. */
  1296. memset(&pubkey, '\x42', sizeof(pubkey));
  1297. memset(srv, '\x43', sizeof(srv));
  1298. hs_build_hsdir_index(&pubkey, srv, period_num, hsdir_index);
  1299. tt_mem_op(hsdir_index, OP_EQ, test_vector, sizeof(hsdir_index));
  1300. }
  1301. done:
  1302. ;
  1303. }
  1304. #define EARLY_IN_SRV_TO_TP 0
  1305. #define LATE_IN_SRV_TO_TP 1
  1306. #define EARLY_IN_TP_TO_SRV 2
  1307. #define LATE_IN_TP_TO_SRV 3
  1308. /** Set the consensus and system time based on <b>position</b>. See the
  1309. * following diagram for details:
  1310. *
  1311. * +------------------------------------------------------------------+
  1312. * | |
  1313. * | 00:00 12:00 00:00 12:00 00:00 12:00 |
  1314. * | SRV#1 TP#1 SRV#2 TP#2 SRV#3 TP#3 |
  1315. * | |
  1316. * | $==========|-----------$===========|----------$===========| |
  1317. * | |
  1318. * | |
  1319. * +------------------------------------------------------------------+
  1320. */
  1321. static time_t
  1322. helper_set_consensus_and_system_time(networkstatus_t *ns, int position)
  1323. {
  1324. time_t real_time = 0;
  1325. /* The period between SRV#N and TP#N is from 00:00 to 12:00 UTC. Consensus
  1326. * valid_after is what matters here, the rest is just to specify the voting
  1327. * period correctly. */
  1328. if (position == LATE_IN_SRV_TO_TP) {
  1329. parse_rfc1123_time("Wed, 13 Apr 2016 11:00:00 UTC", &ns->valid_after);
  1330. parse_rfc1123_time("Wed, 13 Apr 2016 12:00:00 UTC", &ns->fresh_until);
  1331. parse_rfc1123_time("Wed, 13 Apr 2016 14:00:00 UTC", &ns->valid_until);
  1332. } else if (position == EARLY_IN_TP_TO_SRV) {
  1333. parse_rfc1123_time("Wed, 13 Apr 2016 13:00:00 UTC", &ns->valid_after);
  1334. parse_rfc1123_time("Wed, 13 Apr 2016 14:00:00 UTC", &ns->fresh_until);
  1335. parse_rfc1123_time("Wed, 13 Apr 2016 16:00:00 UTC", &ns->valid_until);
  1336. } else if (position == LATE_IN_TP_TO_SRV) {
  1337. parse_rfc1123_time("Wed, 13 Apr 2016 23:00:00 UTC", &ns->valid_after);
  1338. parse_rfc1123_time("Wed, 14 Apr 2016 00:00:00 UTC", &ns->fresh_until);
  1339. parse_rfc1123_time("Wed, 14 Apr 2016 02:00:00 UTC", &ns->valid_until);
  1340. } else if (position == EARLY_IN_SRV_TO_TP) {
  1341. parse_rfc1123_time("Wed, 14 Apr 2016 01:00:00 UTC", &ns->valid_after);
  1342. parse_rfc1123_time("Wed, 14 Apr 2016 02:00:00 UTC", &ns->fresh_until);
  1343. parse_rfc1123_time("Wed, 14 Apr 2016 04:00:00 UTC", &ns->valid_until);
  1344. } else {
  1345. tt_assert(0);
  1346. }
  1347. voting_schedule_recalculate_timing(get_options(), ns->valid_after);
  1348. /* Set system time: pretend to be just 2 minutes before consensus expiry */
  1349. real_time = ns->valid_until - 120;
  1350. update_approx_time(real_time);
  1351. done:
  1352. return real_time;
  1353. }
  1354. /** Helper function that carries out the actual test for
  1355. * test_client_service_sync() */
  1356. static void
  1357. helper_test_hsdir_sync(networkstatus_t *ns,
  1358. int service_position, int client_position,
  1359. int client_fetches_next_desc)
  1360. {
  1361. hs_service_descriptor_t *desc;
  1362. int retval;
  1363. /** Test logic:
  1364. * 1) Initialize service time: consensus and system time.
  1365. * 1.1) Initialize service hash ring
  1366. * 2) Initialize service and publish descriptors.
  1367. * 3) Initialize client time: consensus and system time.
  1368. * 3.1) Initialize client hash ring
  1369. * 4) Try to fetch descriptor as client, and CHECK that the HSDir picked by
  1370. * the client was also picked by service.
  1371. */
  1372. /* 1) Initialize service time: consensus and real time */
  1373. time_t now = helper_set_consensus_and_system_time(ns, service_position);
  1374. helper_initialize_big_hash_ring(ns);
  1375. /* 2) Initialize service */
  1376. hs_service_t *service = helper_init_service(now);
  1377. desc = client_fetches_next_desc ? service->desc_next : service->desc_current;
  1378. /* Now let's upload our desc to all hsdirs */
  1379. upload_descriptor_to_all(service, desc);
  1380. /* Cleanup right now so we don't memleak on error. */
  1381. cleanup_nodelist();
  1382. /* Check that previous hsdirs were populated */
  1383. tt_int_op(smartlist_len(desc->previous_hsdirs), OP_EQ, 8);
  1384. /* 3) Initialize client time */
  1385. helper_set_consensus_and_system_time(ns, client_position);
  1386. cleanup_nodelist();
  1387. SMARTLIST_FOREACH(ns->routerstatus_list,
  1388. routerstatus_t *, rs, routerstatus_free(rs));
  1389. smartlist_clear(ns->routerstatus_list);
  1390. helper_initialize_big_hash_ring(ns);
  1391. /* 4) Pick 6 HSDirs as a client and check that they were also chosen by the
  1392. service. */
  1393. for (int y = 0 ; y < 6 ; y++) {
  1394. char client_hsdir_b64_digest[BASE64_DIGEST_LEN+1] = {0};
  1395. helper_client_pick_hsdir(&service->keys.identity_pk,
  1396. client_hsdir_b64_digest);
  1397. /* CHECK: Go through the hsdirs chosen by the service and make sure that it
  1398. * contains the one picked by the client! */
  1399. retval = smartlist_contains_string(desc->previous_hsdirs,
  1400. client_hsdir_b64_digest);
  1401. tt_int_op(retval, OP_EQ, 1);
  1402. }
  1403. /* Finally, try to pick a 7th hsdir and see that NULL is returned since we
  1404. * exhausted all of them: */
  1405. tt_assert(!pick_hsdir_v3(&service->keys.identity_pk));
  1406. done:
  1407. /* At the end: free all services and initialize the subsystem again, we will
  1408. * need it for next scenario. */
  1409. cleanup_nodelist();
  1410. hs_service_free_all();
  1411. hs_service_init();
  1412. SMARTLIST_FOREACH(ns->routerstatus_list,
  1413. routerstatus_t *, rs, routerstatus_free(rs));
  1414. smartlist_clear(ns->routerstatus_list);
  1415. }
  1416. /** This test ensures that client and service will pick the same HSDirs, under
  1417. * various timing scenarios:
  1418. * a) Scenario where both client and service are in the time segment between
  1419. * SRV#N and TP#N:
  1420. * b) Scenario where both client and service are in the time segment between
  1421. * TP#N and SRV#N+1.
  1422. * c) Scenario where service is between SRV#N and TP#N, but client is between
  1423. * TP#N and SRV#N+1.
  1424. * d) Scenario where service is between TP#N and SRV#N+1, but client is
  1425. * between SRV#N and TP#N.
  1426. *
  1427. * This test is important because it tests that upload_descriptor_to_all() is
  1428. * in synch with pick_hsdir_v3(). That's not the case for the
  1429. * test_reachability() test which only compares the responsible hsdir sets.
  1430. */
  1431. static void
  1432. test_client_service_hsdir_set_sync(void *arg)
  1433. {
  1434. networkstatus_t *ns = NULL;
  1435. (void) arg;
  1436. MOCK(networkstatus_get_latest_consensus,
  1437. mock_networkstatus_get_latest_consensus);
  1438. MOCK(networkstatus_get_live_consensus,
  1439. mock_networkstatus_get_live_consensus);
  1440. MOCK(get_or_state,
  1441. get_or_state_replacement);
  1442. MOCK(hs_desc_encode_descriptor,
  1443. mock_hs_desc_encode_descriptor);
  1444. MOCK(directory_initiate_request,
  1445. mock_directory_initiate_request);
  1446. hs_init();
  1447. /* Initialize a big hash ring: we want it to be big so that client and
  1448. * service cannot accidentally select the same HSDirs */
  1449. ns = networkstatus_get_latest_consensus();
  1450. tt_assert(ns);
  1451. /** Now test the various synch scenarios. See the helper function for more
  1452. details: */
  1453. /* a) Scenario where both client and service are in the time segment between
  1454. * SRV#N and TP#N. At this time the client fetches the first HS desc:
  1455. *
  1456. * +------------------------------------------------------------------+
  1457. * | |
  1458. * | 00:00 12:00 00:00 12:00 00:00 12:00 |
  1459. * | SRV#1 TP#1 SRV#2 TP#2 SRV#3 TP#3 |
  1460. * | |
  1461. * | $==========|-----------$===========|----------$===========| |
  1462. * | ^ ^ |
  1463. * | S C |
  1464. * +------------------------------------------------------------------+
  1465. */
  1466. helper_test_hsdir_sync(ns, LATE_IN_SRV_TO_TP, LATE_IN_SRV_TO_TP, 0);
  1467. /* b) Scenario where both client and service are in the time segment between
  1468. * TP#N and SRV#N+1. At this time the client fetches the second HS
  1469. * desc:
  1470. *
  1471. * +------------------------------------------------------------------+
  1472. * | |
  1473. * | 00:00 12:00 00:00 12:00 00:00 12:00 |
  1474. * | SRV#1 TP#1 SRV#2 TP#2 SRV#3 TP#3 |
  1475. * | |
  1476. * | $==========|-----------$===========|-----------$===========| |
  1477. * | ^ ^ |
  1478. * | S C |
  1479. * +------------------------------------------------------------------+
  1480. */
  1481. helper_test_hsdir_sync(ns, LATE_IN_TP_TO_SRV, LATE_IN_TP_TO_SRV, 1);
  1482. /* c) Scenario where service is between SRV#N and TP#N, but client is
  1483. * between TP#N and SRV#N+1. Client is forward in time so it fetches the
  1484. * second HS desc.
  1485. *
  1486. * +------------------------------------------------------------------+
  1487. * | |
  1488. * | 00:00 12:00 00:00 12:00 00:00 12:00 |
  1489. * | SRV#1 TP#1 SRV#2 TP#2 SRV#3 TP#3 |
  1490. * | |
  1491. * | $==========|-----------$===========|-----------$===========| |
  1492. * | ^ ^ |
  1493. * | S C |
  1494. * +------------------------------------------------------------------+
  1495. */
  1496. helper_test_hsdir_sync(ns, LATE_IN_SRV_TO_TP, EARLY_IN_TP_TO_SRV, 1);
  1497. /* d) Scenario where service is between TP#N and SRV#N+1, but client is
  1498. * between SRV#N and TP#N. Client is backwards in time so it fetches the
  1499. * first HS desc.
  1500. *
  1501. * +------------------------------------------------------------------+
  1502. * | |
  1503. * | 00:00 12:00 00:00 12:00 00:00 12:00 |
  1504. * | SRV#1 TP#1 SRV#2 TP#2 SRV#3 TP#3 |
  1505. * | |
  1506. * | $==========|-----------$===========|-----------$===========| |
  1507. * | ^ ^ |
  1508. * | C S |
  1509. * +------------------------------------------------------------------+
  1510. */
  1511. helper_test_hsdir_sync(ns, EARLY_IN_TP_TO_SRV, LATE_IN_SRV_TO_TP, 0);
  1512. /* e) Scenario where service is between SRV#N and TP#N, but client is
  1513. * between TP#N-1 and SRV#3. Client is backwards in time so it fetches
  1514. * the first HS desc.
  1515. *
  1516. * +------------------------------------------------------------------+
  1517. * | |
  1518. * | 00:00 12:00 00:00 12:00 00:00 12:00 |
  1519. * | SRV#1 TP#1 SRV#2 TP#2 SRV#3 TP#3 |
  1520. * | |
  1521. * | $==========|-----------$===========|-----------$===========| |
  1522. * | ^ ^ |
  1523. * | C S |
  1524. * +------------------------------------------------------------------+
  1525. */
  1526. helper_test_hsdir_sync(ns, EARLY_IN_SRV_TO_TP, LATE_IN_TP_TO_SRV, 0);
  1527. /* f) Scenario where service is between TP#N and SRV#N+1, but client is
  1528. * between SRV#N+1 and TP#N+1. Client is forward in time so it fetches
  1529. * the second HS desc.
  1530. *
  1531. * +------------------------------------------------------------------+
  1532. * | |
  1533. * | 00:00 12:00 00:00 12:00 00:00 12:00 |
  1534. * | SRV#1 TP#1 SRV#2 TP#2 SRV#3 TP#3 |
  1535. * | |
  1536. * | $==========|-----------$===========|-----------$===========| |
  1537. * | ^ ^ |
  1538. * | S C |
  1539. * +------------------------------------------------------------------+
  1540. */
  1541. helper_test_hsdir_sync(ns, LATE_IN_TP_TO_SRV, EARLY_IN_SRV_TO_TP, 1);
  1542. done:
  1543. networkstatus_vote_free(ns);
  1544. nodelist_free_all();
  1545. hs_free_all();
  1546. }
  1547. struct testcase_t hs_common_tests[] = {
  1548. { "build_address", test_build_address, TT_FORK,
  1549. NULL, NULL },
  1550. { "validate_address", test_validate_address, TT_FORK,
  1551. NULL, NULL },
  1552. { "time_period", test_time_period, TT_FORK,
  1553. NULL, NULL },
  1554. { "start_time_of_next_time_period", test_start_time_of_next_time_period,
  1555. TT_FORK, NULL, NULL },
  1556. { "responsible_hsdirs", test_responsible_hsdirs, TT_FORK,
  1557. NULL, NULL },
  1558. { "desc_reupload_logic", test_desc_reupload_logic, TT_FORK,
  1559. NULL, NULL },
  1560. { "disaster_srv", test_disaster_srv, TT_FORK,
  1561. NULL, NULL },
  1562. { "hid_serv_request_tracker", test_hid_serv_request_tracker, TT_FORK,
  1563. NULL, NULL },
  1564. { "parse_extended_hostname", test_parse_extended_hostname, TT_FORK,
  1565. NULL, NULL },
  1566. { "time_between_tp_and_srv", test_time_between_tp_and_srv, TT_FORK,
  1567. NULL, NULL },
  1568. { "reachability", test_reachability, TT_FORK,
  1569. NULL, NULL },
  1570. { "client_service_hsdir_set_sync", test_client_service_hsdir_set_sync,
  1571. TT_FORK, NULL, NULL },
  1572. { "hs_indexes", test_hs_indexes, TT_FORK,
  1573. NULL, NULL },
  1574. END_OF_TESTCASES
  1575. };