test_hs_common.c 67 KB

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