test_hs_common.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720
  1. /* Copyright (c) 2017, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. /**
  4. * \file test_hs_common.c
  5. * \brief Test hidden service common functionalities.
  6. */
  7. #define HS_COMMON_PRIVATE
  8. #define HS_SERVICE_PRIVATE
  9. #include "test.h"
  10. #include "test_helpers.h"
  11. #include "log_test_helpers.h"
  12. #include "hs_test_helpers.h"
  13. #include "hs_common.h"
  14. #include "hs_service.h"
  15. #include "config.h"
  16. #include "networkstatus.h"
  17. #include "directory.h"
  18. #include "nodelist.h"
  19. #include "statefile.h"
  20. /** Test the validation of HS v3 addresses */
  21. static void
  22. test_validate_address(void *arg)
  23. {
  24. int ret;
  25. (void) arg;
  26. /* Address too short and too long. */
  27. setup_full_capture_of_logs(LOG_WARN);
  28. ret = hs_address_is_valid("blah");
  29. tt_int_op(ret, OP_EQ, 0);
  30. expect_log_msg_containing("has an invalid length");
  31. teardown_capture_of_logs();
  32. setup_full_capture_of_logs(LOG_WARN);
  33. ret = hs_address_is_valid(
  34. "p3xnclpu4mu22dwaurjtsybyqk4xfjmcfz6z62yl24uwmhjatiwnlnadb");
  35. tt_int_op(ret, OP_EQ, 0);
  36. expect_log_msg_containing("has an invalid length");
  37. teardown_capture_of_logs();
  38. /* Invalid checksum (taken from prop224) */
  39. setup_full_capture_of_logs(LOG_WARN);
  40. ret = hs_address_is_valid(
  41. "l5satjgud6gucryazcyvyvhuxhr74u6ygigiuyixe3a6ysis67ororad");
  42. tt_int_op(ret, OP_EQ, 0);
  43. expect_log_msg_containing("invalid checksum");
  44. teardown_capture_of_logs();
  45. setup_full_capture_of_logs(LOG_WARN);
  46. ret = hs_address_is_valid(
  47. "btojiu7nu5y5iwut64eufevogqdw4wmqzugnoluw232r4t3ecsfv37ad");
  48. tt_int_op(ret, OP_EQ, 0);
  49. expect_log_msg_containing("invalid checksum");
  50. teardown_capture_of_logs();
  51. /* Non base32 decodable string. */
  52. setup_full_capture_of_logs(LOG_WARN);
  53. ret = hs_address_is_valid(
  54. "????????????????????????????????????????????????????????");
  55. tt_int_op(ret, OP_EQ, 0);
  56. expect_log_msg_containing("can't be decoded");
  57. teardown_capture_of_logs();
  58. /* Valid address. */
  59. ret = hs_address_is_valid(
  60. "p3xnclpu4mu22dwaurjtsybyqk4xfjmcfz6z62yl24uwmhjatiwnlnad");
  61. tt_int_op(ret, OP_EQ, 1);
  62. done:
  63. ;
  64. }
  65. static int
  66. mock_write_str_to_file(const char *path, const char *str, int bin)
  67. {
  68. (void)bin;
  69. tt_str_op(path, OP_EQ, "/double/five"PATH_SEPARATOR"squared");
  70. tt_str_op(str, OP_EQ,
  71. "ijbeeqscijbeeqscijbeeqscijbeeqscijbeeqscijbeeqscijbezhid.onion\n");
  72. done:
  73. return 0;
  74. }
  75. /** Test building HS v3 onion addresses */
  76. static void
  77. test_build_address(void *arg)
  78. {
  79. int ret;
  80. char onion_addr[HS_SERVICE_ADDR_LEN_BASE32 + 1];
  81. ed25519_public_key_t pubkey;
  82. hs_service_t *service = NULL;
  83. (void) arg;
  84. MOCK(write_str_to_file, mock_write_str_to_file);
  85. /* The following has been created with hs_build_address.py script that
  86. * follows proposal 224 specification to build an onion address. */
  87. static const char *test_addr =
  88. "ijbeeqscijbeeqscijbeeqscijbeeqscijbeeqscijbeeqscijbezhid";
  89. /* Let's try to build the same onion address that the script can do. Key is
  90. * a long set of very random \x42 :). */
  91. memset(&pubkey, '\x42', sizeof(pubkey));
  92. hs_build_address(&pubkey, HS_VERSION_THREE, onion_addr);
  93. tt_str_op(test_addr, OP_EQ, onion_addr);
  94. /* Validate that address. */
  95. ret = hs_address_is_valid(onion_addr);
  96. tt_int_op(ret, OP_EQ, 1);
  97. service = tor_malloc_zero(sizeof(hs_service_t));
  98. memcpy(service->onion_address, onion_addr, sizeof(service->onion_address));
  99. tor_asprintf(&service->config.directory_path, "/double/five");
  100. ret = write_address_to_file(service, "squared");
  101. tt_int_op(ret, OP_EQ, 0);
  102. done:
  103. hs_service_free(service);
  104. }
  105. /** Test that our HS time period calculation functions work properly */
  106. static void
  107. test_time_period(void *arg)
  108. {
  109. (void) arg;
  110. uint64_t tn;
  111. int retval;
  112. time_t fake_time, correct_time, start_time;
  113. /* Let's do the example in prop224 section [TIME-PERIODS] */
  114. retval = parse_rfc1123_time("Wed, 13 Apr 2016 11:00:00 UTC",
  115. &fake_time);
  116. tt_int_op(retval, ==, 0);
  117. /* Check that the time period number is right */
  118. tn = hs_get_time_period_num(fake_time);
  119. tt_u64_op(tn, ==, 16903);
  120. /* Increase current time to 11:59:59 UTC and check that the time period
  121. number is still the same */
  122. fake_time += 3599;
  123. tn = hs_get_time_period_num(fake_time);
  124. tt_u64_op(tn, ==, 16903);
  125. { /* Check start time of next time period */
  126. retval = parse_rfc1123_time("Wed, 13 Apr 2016 12:00:00 UTC",
  127. &correct_time);
  128. tt_int_op(retval, ==, 0);
  129. start_time = hs_get_start_time_of_next_time_period(fake_time);
  130. tt_int_op(start_time, OP_EQ, correct_time);
  131. }
  132. /* Now take time to 12:00:00 UTC and check that the time period rotated */
  133. fake_time += 1;
  134. tn = hs_get_time_period_num(fake_time);
  135. tt_u64_op(tn, ==, 16904);
  136. /* Now also check our hs_get_next_time_period_num() function */
  137. tn = hs_get_next_time_period_num(fake_time);
  138. tt_u64_op(tn, ==, 16905);
  139. { /* Check start time of next time period again */
  140. retval = parse_rfc1123_time("Wed, 14 Apr 2016 12:00:00 UTC",
  141. &correct_time);
  142. tt_int_op(retval, ==, 0);
  143. start_time = hs_get_start_time_of_next_time_period(fake_time);
  144. tt_int_op(start_time, OP_EQ, correct_time);
  145. }
  146. /* Now do another sanity check: The time period number at the start of the
  147. * next time period, must be the same time period number as the one returned
  148. * from hs_get_next_time_period_num() */
  149. {
  150. time_t next_tp_start = hs_get_start_time_of_next_time_period(fake_time);
  151. tt_u64_op(hs_get_time_period_num(next_tp_start), OP_EQ,
  152. hs_get_next_time_period_num(fake_time));
  153. }
  154. done:
  155. ;
  156. }
  157. /** Test that we can correctly find the start time of the next time period */
  158. static void
  159. test_start_time_of_next_time_period(void *arg)
  160. {
  161. (void) arg;
  162. int retval;
  163. time_t fake_time;
  164. char tbuf[ISO_TIME_LEN + 1];
  165. time_t next_tp_start_time;
  166. /* Do some basic tests */
  167. retval = parse_rfc1123_time("Wed, 13 Apr 2016 11:00:00 UTC",
  168. &fake_time);
  169. tt_int_op(retval, ==, 0);
  170. next_tp_start_time = hs_get_start_time_of_next_time_period(fake_time);
  171. /* Compare it with the correct result */
  172. format_iso_time(tbuf, next_tp_start_time);
  173. tt_str_op("2016-04-13 12:00:00", OP_EQ, tbuf);
  174. /* Another test with an edge-case time (start of TP) */
  175. retval = parse_rfc1123_time("Wed, 13 Apr 2016 12:00:00 UTC",
  176. &fake_time);
  177. tt_int_op(retval, ==, 0);
  178. next_tp_start_time = hs_get_start_time_of_next_time_period(fake_time);
  179. format_iso_time(tbuf, next_tp_start_time);
  180. tt_str_op("2016-04-14 12:00:00", OP_EQ, tbuf);
  181. {
  182. /* Now pretend we are on a testing network and alter the voting schedule to
  183. be every 10 seconds. This means that a time period has length 10*24
  184. seconds (4 minutes). It also means that we apply a rotational offset of
  185. 120 seconds to the time period, so that it starts at 00:02:00 instead of
  186. 00:00:00. */
  187. or_options_t *options = get_options_mutable();
  188. options->TestingTorNetwork = 1;
  189. options->V3AuthVotingInterval = 10;
  190. options->TestingV3AuthInitialVotingInterval = 10;
  191. retval = parse_rfc1123_time("Wed, 13 Apr 2016 00:00:00 UTC",
  192. &fake_time);
  193. tt_int_op(retval, ==, 0);
  194. next_tp_start_time = hs_get_start_time_of_next_time_period(fake_time);
  195. /* Compare it with the correct result */
  196. format_iso_time(tbuf, next_tp_start_time);
  197. tt_str_op("2016-04-13 00:02:00", OP_EQ, tbuf);
  198. retval = parse_rfc1123_time("Wed, 13 Apr 2016 00:02:00 UTC",
  199. &fake_time);
  200. tt_int_op(retval, ==, 0);
  201. next_tp_start_time = hs_get_start_time_of_next_time_period(fake_time);
  202. /* Compare it with the correct result */
  203. format_iso_time(tbuf, next_tp_start_time);
  204. tt_str_op("2016-04-13 00:06:00", OP_EQ, tbuf);
  205. }
  206. done:
  207. ;
  208. }
  209. /** Test that our HS overlap period functions work properly. */
  210. static void
  211. test_desc_overlap_period(void *arg)
  212. {
  213. (void) arg;
  214. int retval;
  215. time_t now = time(NULL);
  216. networkstatus_t *dummy_consensus = NULL;
  217. /* First try with a consensus just inside the overlap period */
  218. dummy_consensus = tor_malloc_zero(sizeof(networkstatus_t));
  219. retval = parse_rfc1123_time("Wed, 13 Apr 2016 00:00:00 UTC",
  220. &dummy_consensus->valid_after);
  221. tt_int_op(retval, ==, 0);
  222. retval = hs_overlap_mode_is_active(dummy_consensus, now);
  223. tt_int_op(retval, ==, 1);
  224. /* Now increase the valid_after so that it goes to 11:00:00 UTC. Overlap
  225. period is still active. */
  226. dummy_consensus->valid_after += 3600*11;
  227. retval = hs_overlap_mode_is_active(dummy_consensus, now);
  228. tt_int_op(retval, ==, 1);
  229. /* Now increase the valid_after so that it goes to 11:59:59 UTC. Overlap
  230. period is still active. */
  231. dummy_consensus->valid_after += 3599;
  232. retval = hs_overlap_mode_is_active(dummy_consensus, now);
  233. tt_int_op(retval, ==, 1);
  234. /* Now increase the valid_after so that it drifts to noon, and check that
  235. overlap mode is not active anymore. */
  236. dummy_consensus->valid_after += 1;
  237. retval = hs_overlap_mode_is_active(dummy_consensus, now);
  238. tt_int_op(retval, ==, 0);
  239. /* Check that overlap mode is also inactive at 23:59:59 UTC */
  240. retval = parse_rfc1123_time("Wed, 13 Apr 2016 23:59:59 UTC",
  241. &dummy_consensus->valid_after);
  242. tt_int_op(retval, ==, 0);
  243. retval = hs_overlap_mode_is_active(dummy_consensus, now);
  244. tt_int_op(retval, ==, 0);
  245. done:
  246. tor_free(dummy_consensus);
  247. }
  248. /* Test the overlap period functions on a testnet with altered voting
  249. * schedule */
  250. static void
  251. test_desc_overlap_period_testnet(void *arg)
  252. {
  253. int retval;
  254. time_t now = approx_time();
  255. networkstatus_t *dummy_consensus = NULL;
  256. or_options_t *options = get_options_mutable();
  257. (void) arg;
  258. /* Set the testnet option and a 10-second voting interval */
  259. options->TestingTorNetwork = 1;
  260. options->V3AuthVotingInterval = 10;
  261. options->TestingV3AuthInitialVotingInterval = 10;
  262. dummy_consensus = tor_malloc_zero(sizeof(networkstatus_t));
  263. /* A 10-second voting interval means that the lengths of an SRV run and of a
  264. * time period are both 10*24 seconds (4 minutes). The SRV gets published at
  265. * 00:00:00 and the TP starts at 00:02:00 (rotation offset: 2 mins). Those
  266. * two minutes between SRV publish and TP start is the overlap period
  267. * window. Let's test it: */
  268. retval = parse_rfc1123_time("Wed, 13 Apr 2016 00:00:00 UTC",
  269. &dummy_consensus->valid_after);
  270. tt_int_op(retval, ==, 0);
  271. retval = hs_overlap_mode_is_active(dummy_consensus, now);
  272. tt_int_op(retval, ==, 1);
  273. retval = parse_rfc1123_time("Wed, 13 Apr 2016 00:01:59 UTC",
  274. &dummy_consensus->valid_after);
  275. tt_int_op(retval, ==, 0);
  276. retval = hs_overlap_mode_is_active(dummy_consensus, now);
  277. tt_int_op(retval, ==, 1);
  278. retval = parse_rfc1123_time("Wed, 13 Apr 2016 00:02:00 UTC",
  279. &dummy_consensus->valid_after);
  280. tt_int_op(retval, ==, 0);
  281. retval = hs_overlap_mode_is_active(dummy_consensus, now);
  282. tt_int_op(retval, ==, 0);
  283. retval = parse_rfc1123_time("Wed, 13 Apr 2016 00:04:00 UTC",
  284. &dummy_consensus->valid_after);
  285. tt_int_op(retval, ==, 0);
  286. retval = hs_overlap_mode_is_active(dummy_consensus, now);
  287. tt_int_op(retval, ==, 1);
  288. retval = parse_rfc1123_time("Wed, 13 Apr 2016 00:05:59 UTC",
  289. &dummy_consensus->valid_after);
  290. tt_int_op(retval, ==, 0);
  291. retval = hs_overlap_mode_is_active(dummy_consensus, now);
  292. tt_int_op(retval, ==, 1);
  293. retval = parse_rfc1123_time("Wed, 13 Apr 2016 00:06:00 UTC",
  294. &dummy_consensus->valid_after);
  295. tt_int_op(retval, ==, 0);
  296. retval = hs_overlap_mode_is_active(dummy_consensus, now);
  297. tt_int_op(retval, ==, 0);
  298. done:
  299. tor_free(dummy_consensus);
  300. }
  301. static void
  302. helper_add_hsdir_to_networkstatus(networkstatus_t *ns,
  303. const uint8_t *identity,
  304. const uint8_t *curr_hsdir_index,
  305. const char *nickname,
  306. int is_hsdir)
  307. {
  308. routerstatus_t *rs = tor_malloc_zero(sizeof(routerstatus_t));
  309. routerinfo_t *ri = tor_malloc_zero(sizeof(routerinfo_t));
  310. tor_addr_t ipv4_addr;
  311. memcpy(rs->identity_digest, identity, DIGEST_LEN);
  312. rs->is_hs_dir = is_hsdir;
  313. rs->supports_v3_hsdir = 1;
  314. tor_addr_parse(&ipv4_addr, "1.2.3.4");
  315. ri->addr = tor_addr_to_ipv4h(&ipv4_addr);
  316. ri->nickname = tor_strdup(nickname);
  317. ri->protocol_list = tor_strdup("HSDir=1-2 LinkAuth=3");
  318. memcpy(ri->cache_info.identity_digest, identity, DIGEST_LEN);
  319. tt_assert(nodelist_set_routerinfo(ri, NULL));
  320. node_t *node = node_get_mutable_by_id(ri->cache_info.identity_digest);
  321. tt_assert(node);
  322. node->rs = rs;
  323. memcpy(node->hsdir_index->current, curr_hsdir_index,
  324. sizeof(node->hsdir_index->current));
  325. smartlist_add(ns->routerstatus_list, rs);
  326. done:
  327. ;
  328. }
  329. static networkstatus_t *mock_ns = NULL;
  330. static networkstatus_t *
  331. mock_networkstatus_get_latest_consensus(void)
  332. {
  333. time_t now = approx_time();
  334. /* If initialized, return it */
  335. if (mock_ns) {
  336. return mock_ns;
  337. }
  338. /* Initialize fake consensus */
  339. mock_ns = tor_malloc_zero(sizeof(networkstatus_t));
  340. /* This consensus is live */
  341. mock_ns->valid_after = now-1;
  342. mock_ns->fresh_until = now+1;
  343. mock_ns->valid_until = now+2;
  344. /* Create routerstatus list */
  345. mock_ns->routerstatus_list = smartlist_new();
  346. return mock_ns;
  347. }
  348. /** Test the responsible HSDirs calculation function */
  349. static void
  350. test_responsible_hsdirs(void *arg)
  351. {
  352. time_t now = approx_time();
  353. smartlist_t *responsible_dirs = smartlist_new();
  354. networkstatus_t *ns = NULL;
  355. int retval;
  356. (void) arg;
  357. hs_init();
  358. MOCK(networkstatus_get_latest_consensus,
  359. mock_networkstatus_get_latest_consensus);
  360. ns = networkstatus_get_latest_consensus();
  361. { /* First router: HSdir */
  362. uint8_t identity[DIGEST_LEN];
  363. uint8_t curr_hsdir_index[DIGEST256_LEN];
  364. char nickname[] = "let_me";
  365. memset(identity, 1, sizeof(identity));
  366. memset(curr_hsdir_index, 1, sizeof(curr_hsdir_index));
  367. helper_add_hsdir_to_networkstatus(ns, identity,
  368. curr_hsdir_index, nickname, 1);
  369. }
  370. { /* Second HSDir */
  371. uint8_t identity[DIGEST_LEN];
  372. uint8_t curr_hsdir_index[DIGEST256_LEN];
  373. char nickname[] = "show_you";
  374. memset(identity, 2, sizeof(identity));
  375. memset(curr_hsdir_index, 2, sizeof(curr_hsdir_index));
  376. helper_add_hsdir_to_networkstatus(ns, identity,
  377. curr_hsdir_index, nickname, 1);
  378. }
  379. { /* Third relay but not HSDir */
  380. uint8_t identity[DIGEST_LEN];
  381. uint8_t curr_hsdir_index[DIGEST256_LEN];
  382. char nickname[] = "how_to_dance";
  383. memset(identity, 3, sizeof(identity));
  384. memset(curr_hsdir_index, 3, sizeof(curr_hsdir_index));
  385. helper_add_hsdir_to_networkstatus(ns, identity,
  386. curr_hsdir_index, nickname, 0);
  387. }
  388. ed25519_keypair_t kp;
  389. retval = ed25519_keypair_generate(&kp, 0);
  390. tt_int_op(retval, OP_EQ , 0);
  391. uint64_t time_period_num = hs_get_time_period_num(now);
  392. hs_get_responsible_hsdirs(&kp.pubkey, time_period_num,
  393. 0, 0, responsible_dirs);
  394. /* Make sure that we only found 2 responsible HSDirs.
  395. * The third relay was not an hsdir! */
  396. tt_int_op(smartlist_len(responsible_dirs), OP_EQ, 2);
  397. /** TODO: Build a bigger network and do more tests here */
  398. done:
  399. SMARTLIST_FOREACH(ns->routerstatus_list,
  400. routerstatus_t *, rs, routerstatus_free(rs));
  401. smartlist_free(responsible_dirs);
  402. smartlist_clear(ns->routerstatus_list);
  403. networkstatus_vote_free(mock_ns);
  404. }
  405. static void
  406. mock_directory_initiate_request(directory_request_t *req)
  407. {
  408. (void)req;
  409. return;
  410. }
  411. static int
  412. mock_hs_desc_encode_descriptor(const hs_descriptor_t *desc,
  413. const ed25519_keypair_t *signing_kp,
  414. char **encoded_out)
  415. {
  416. (void)desc;
  417. (void)signing_kp;
  418. tor_asprintf(encoded_out, "lulu");
  419. return 0;
  420. }
  421. static or_state_t dummy_state;
  422. /* Mock function to get fake or state (used for rev counters) */
  423. static or_state_t *
  424. get_or_state_replacement(void)
  425. {
  426. return &dummy_state;
  427. }
  428. static int
  429. mock_router_have_minimum_dir_info(void)
  430. {
  431. return 1;
  432. }
  433. /** Test that we correctly detect when the HSDir hash ring changes so that we
  434. * reupload our descriptor. */
  435. static void
  436. test_desc_reupload_logic(void *arg)
  437. {
  438. networkstatus_t *ns = NULL;
  439. (void) arg;
  440. hs_init();
  441. MOCK(router_have_minimum_dir_info,
  442. mock_router_have_minimum_dir_info);
  443. MOCK(get_or_state,
  444. get_or_state_replacement);
  445. MOCK(networkstatus_get_latest_consensus,
  446. mock_networkstatus_get_latest_consensus);
  447. MOCK(directory_initiate_request,
  448. mock_directory_initiate_request);
  449. MOCK(hs_desc_encode_descriptor,
  450. mock_hs_desc_encode_descriptor);
  451. ns = networkstatus_get_latest_consensus();
  452. /** Test logic:
  453. * 1) Upload descriptor to HSDirs
  454. * CHECK that previous_hsdirs list was populated.
  455. * 2) Then call router_dir_info_changed() without an HSDir set change.
  456. * CHECK that no reuplod occurs.
  457. * 3) Now change the HSDir set, and call dir_info_changed() again.
  458. * CHECK that reupload occurs.
  459. * 4) Finally call service_desc_schedule_upload().
  460. * CHECK that previous_hsdirs list was cleared.
  461. **/
  462. /* Let's start by building our descriptor and service */
  463. hs_service_descriptor_t *desc = service_descriptor_new();
  464. hs_service_t *service = NULL;
  465. char onion_addr[HS_SERVICE_ADDR_LEN_BASE32 + 1];
  466. ed25519_public_key_t pubkey;
  467. memset(&pubkey, '\x42', sizeof(pubkey));
  468. hs_build_address(&pubkey, HS_VERSION_THREE, onion_addr);
  469. service = tor_malloc_zero(sizeof(hs_service_t));
  470. memcpy(service->onion_address, onion_addr, sizeof(service->onion_address));
  471. ed25519_secret_key_generate(&service->keys.identity_sk, 0);
  472. ed25519_public_key_generate(&service->keys.identity_pk,
  473. &service->keys.identity_sk);
  474. service->desc_current = desc;
  475. /* Also add service to service map */
  476. hs_service_ht *service_map = get_hs_service_map();
  477. tt_assert(service_map);
  478. tt_int_op(hs_service_get_num_services(), OP_EQ, 0);
  479. register_service(service_map, service);
  480. tt_int_op(hs_service_get_num_services(), OP_EQ, 1);
  481. /* Now let's create our hash ring: */
  482. { /* First HSDir */
  483. uint8_t identity[DIGEST_LEN];
  484. uint8_t curr_hsdir_index[DIGEST256_LEN];
  485. char nickname[] = "let_me";
  486. memset(identity, 1, sizeof(identity));
  487. memset(curr_hsdir_index, 1, sizeof(curr_hsdir_index));
  488. helper_add_hsdir_to_networkstatus(ns, identity,
  489. curr_hsdir_index, nickname, 1);
  490. }
  491. { /* Second HSDir */
  492. uint8_t identity[DIGEST_LEN];
  493. uint8_t curr_hsdir_index[DIGEST256_LEN];
  494. char nickname[] = "show_you";
  495. memset(identity, 2, sizeof(identity));
  496. memset(curr_hsdir_index, 2, sizeof(curr_hsdir_index));
  497. helper_add_hsdir_to_networkstatus(ns, identity,
  498. curr_hsdir_index, nickname, 1);
  499. }
  500. /* Now let's upload our desc to all hsdirs */
  501. upload_descriptor_to_all(service, desc, 0);
  502. /* Check that previous hsdirs were populated */
  503. tt_int_op(smartlist_len(desc->previous_hsdirs), OP_EQ, 2);
  504. /* Poison next upload time so that we can see if it was changed by
  505. * router_dir_info_changed(). No changes in hash ring so far, so the upload
  506. * time should stay as is. */
  507. desc->next_upload_time = 42;
  508. router_dir_info_changed();
  509. tt_int_op(desc->next_upload_time, OP_EQ, 42);
  510. /* Now change the HSDir hash ring by adding another node */
  511. { /* Third HSDir */
  512. uint8_t identity[DIGEST_LEN];
  513. uint8_t curr_hsdir_index[DIGEST256_LEN];
  514. char nickname[] = "how_to_dance";
  515. memset(identity, 3, sizeof(identity));
  516. memset(curr_hsdir_index, 3, sizeof(curr_hsdir_index));
  517. helper_add_hsdir_to_networkstatus(ns, identity,
  518. curr_hsdir_index, nickname, 1);
  519. }
  520. /* Now call router_dir_info_changed() again and see that it detected the hash
  521. ring change and updated the upload time */
  522. time_t now = approx_time();
  523. tt_assert(now);
  524. router_dir_info_changed();
  525. tt_int_op(desc->next_upload_time, OP_EQ, now);
  526. /* Now pretend that the descriptor changed, and order a reupload to all
  527. HSDirs. Make sure that the set of previous HSDirs was cleared. */
  528. service_desc_schedule_upload(desc, now, 1);
  529. tt_int_op(smartlist_len(desc->previous_hsdirs), OP_EQ, 0);
  530. /* Now reupload again: see that the prev hsdir set got populated again. */
  531. upload_descriptor_to_all(service, desc, 0);
  532. tt_int_op(smartlist_len(desc->previous_hsdirs), OP_EQ, 3);
  533. done:
  534. hs_free_all();
  535. }
  536. /** Test disaster SRV computation and caching */
  537. static void
  538. test_disaster_srv(void *arg)
  539. {
  540. uint8_t *cached_disaster_srv_one = NULL;
  541. uint8_t *cached_disaster_srv_two = NULL;
  542. uint8_t srv_one[DIGEST256_LEN] = {0};
  543. uint8_t srv_two[DIGEST256_LEN] = {0};
  544. uint8_t srv_three[DIGEST256_LEN] = {0};
  545. uint8_t srv_four[DIGEST256_LEN] = {0};
  546. uint8_t srv_five[DIGEST256_LEN] = {0};
  547. (void) arg;
  548. /* Get the cached SRVs: we gonna use them later for verification */
  549. cached_disaster_srv_one = get_first_cached_disaster_srv();
  550. cached_disaster_srv_two = get_second_cached_disaster_srv();
  551. /* Compute some srvs */
  552. get_disaster_srv(1, srv_one);
  553. get_disaster_srv(2, srv_two);
  554. /* Check that the cached ones where updated */
  555. tt_mem_op(cached_disaster_srv_one, OP_EQ, srv_one, DIGEST256_LEN);
  556. tt_mem_op(cached_disaster_srv_two, OP_EQ, srv_two, DIGEST256_LEN);
  557. /* Ask for an SRV that has already been computed */
  558. get_disaster_srv(2, srv_two);
  559. /* and check that the cache entries have not changed */
  560. tt_mem_op(cached_disaster_srv_one, OP_EQ, srv_one, DIGEST256_LEN);
  561. tt_mem_op(cached_disaster_srv_two, OP_EQ, srv_two, DIGEST256_LEN);
  562. /* Ask for a new SRV */
  563. get_disaster_srv(3, srv_three);
  564. tt_mem_op(cached_disaster_srv_one, OP_EQ, srv_three, DIGEST256_LEN);
  565. tt_mem_op(cached_disaster_srv_two, OP_EQ, srv_two, DIGEST256_LEN);
  566. /* Ask for another SRV: none of the original SRVs should now be cached */
  567. get_disaster_srv(4, srv_four);
  568. tt_mem_op(cached_disaster_srv_one, OP_EQ, srv_three, DIGEST256_LEN);
  569. tt_mem_op(cached_disaster_srv_two, OP_EQ, srv_four, DIGEST256_LEN);
  570. /* Ask for yet another SRV */
  571. get_disaster_srv(5, srv_five);
  572. tt_mem_op(cached_disaster_srv_one, OP_EQ, srv_five, DIGEST256_LEN);
  573. tt_mem_op(cached_disaster_srv_two, OP_EQ, srv_four, DIGEST256_LEN);
  574. done:
  575. ;
  576. }
  577. struct testcase_t hs_common_tests[] = {
  578. { "build_address", test_build_address, TT_FORK,
  579. NULL, NULL },
  580. { "validate_address", test_validate_address, TT_FORK,
  581. NULL, NULL },
  582. { "time_period", test_time_period, TT_FORK,
  583. NULL, NULL },
  584. { "start_time_of_next_time_period", test_start_time_of_next_time_period,
  585. TT_FORK, NULL, NULL },
  586. { "desc_overlap_period", test_desc_overlap_period, TT_FORK,
  587. NULL, NULL },
  588. { "desc_overlap_period_testnet", test_desc_overlap_period_testnet, TT_FORK,
  589. NULL, NULL },
  590. { "responsible_hsdirs", test_responsible_hsdirs, TT_FORK,
  591. NULL, NULL },
  592. { "desc_reupload_logic", test_desc_reupload_logic, TT_FORK,
  593. NULL, NULL },
  594. { "disaster_srv", test_disaster_srv, TT_FORK, NULL, NULL },
  595. END_OF_TESTCASES
  596. };