test_hs_common.c 28 KB

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