test_hs_common.c 68 KB

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