test_hs_common.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  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 "nodelist.h"
  18. /** Test the validation of HS v3 addresses */
  19. static void
  20. test_validate_address(void *arg)
  21. {
  22. int ret;
  23. (void) arg;
  24. /* Address too short and too long. */
  25. setup_full_capture_of_logs(LOG_WARN);
  26. ret = hs_address_is_valid("blah");
  27. tt_int_op(ret, OP_EQ, 0);
  28. expect_log_msg_containing("has an invalid length");
  29. teardown_capture_of_logs();
  30. setup_full_capture_of_logs(LOG_WARN);
  31. ret = hs_address_is_valid(
  32. "p3xnclpu4mu22dwaurjtsybyqk4xfjmcfz6z62yl24uwmhjatiwnlnadb");
  33. tt_int_op(ret, OP_EQ, 0);
  34. expect_log_msg_containing("has an invalid length");
  35. teardown_capture_of_logs();
  36. /* Invalid checksum (taken from prop224) */
  37. setup_full_capture_of_logs(LOG_WARN);
  38. ret = hs_address_is_valid(
  39. "l5satjgud6gucryazcyvyvhuxhr74u6ygigiuyixe3a6ysis67ororad");
  40. tt_int_op(ret, OP_EQ, 0);
  41. expect_log_msg_containing("invalid checksum");
  42. teardown_capture_of_logs();
  43. setup_full_capture_of_logs(LOG_WARN);
  44. ret = hs_address_is_valid(
  45. "btojiu7nu5y5iwut64eufevogqdw4wmqzugnoluw232r4t3ecsfv37ad");
  46. tt_int_op(ret, OP_EQ, 0);
  47. expect_log_msg_containing("invalid checksum");
  48. teardown_capture_of_logs();
  49. /* Non base32 decodable string. */
  50. setup_full_capture_of_logs(LOG_WARN);
  51. ret = hs_address_is_valid(
  52. "????????????????????????????????????????????????????????");
  53. tt_int_op(ret, OP_EQ, 0);
  54. expect_log_msg_containing("can't be decoded");
  55. teardown_capture_of_logs();
  56. /* Valid address. */
  57. ret = hs_address_is_valid(
  58. "p3xnclpu4mu22dwaurjtsybyqk4xfjmcfz6z62yl24uwmhjatiwnlnad");
  59. tt_int_op(ret, OP_EQ, 1);
  60. done:
  61. ;
  62. }
  63. static int
  64. mock_write_str_to_file(const char *path, const char *str, int bin)
  65. {
  66. (void)bin;
  67. tt_str_op(path, OP_EQ, "/double/five/squared");
  68. tt_str_op(str, OP_EQ,
  69. "ijbeeqscijbeeqscijbeeqscijbeeqscijbeeqscijbeeqscijbezhid.onion\n");
  70. done:
  71. return 0;
  72. }
  73. /** Test building HS v3 onion addresses */
  74. static void
  75. test_build_address(void *arg)
  76. {
  77. int ret;
  78. char onion_addr[HS_SERVICE_ADDR_LEN_BASE32 + 1];
  79. ed25519_public_key_t pubkey;
  80. hs_service_t *service = NULL;
  81. (void) arg;
  82. MOCK(write_str_to_file, mock_write_str_to_file);
  83. /* The following has been created with hs_build_address.py script that
  84. * follows proposal 224 specification to build an onion address. */
  85. static const char *test_addr =
  86. "ijbeeqscijbeeqscijbeeqscijbeeqscijbeeqscijbeeqscijbezhid";
  87. /* Let's try to build the same onion address that the script can do. Key is
  88. * a long set of very random \x42 :). */
  89. memset(&pubkey, '\x42', sizeof(pubkey));
  90. hs_build_address(&pubkey, HS_VERSION_THREE, onion_addr);
  91. tt_str_op(test_addr, OP_EQ, onion_addr);
  92. /* Validate that address. */
  93. ret = hs_address_is_valid(onion_addr);
  94. tt_int_op(ret, OP_EQ, 1);
  95. service = tor_malloc_zero(sizeof(hs_service_t));
  96. memcpy(service->onion_address, onion_addr, sizeof(service->onion_address));
  97. tor_asprintf(&service->config.directory_path, "/double/five");
  98. ret = write_address_to_file(service, "squared");
  99. tt_int_op(ret, OP_EQ, 0);
  100. done:
  101. hs_service_free(service);
  102. }
  103. /** Test that our HS time period calculation functions work properly */
  104. static void
  105. test_time_period(void *arg)
  106. {
  107. (void) arg;
  108. uint64_t tn;
  109. int retval;
  110. time_t fake_time, correct_time, start_time;
  111. /* Let's do the example in prop224 section [TIME-PERIODS] */
  112. retval = parse_rfc1123_time("Wed, 13 Apr 2016 11:00:00 UTC",
  113. &fake_time);
  114. tt_int_op(retval, ==, 0);
  115. /* Check that the time period number is right */
  116. tn = hs_get_time_period_num(fake_time);
  117. tt_u64_op(tn, ==, 16903);
  118. /* Increase current time to 11:59:59 UTC and check that the time period
  119. number is still the same */
  120. fake_time += 3599;
  121. tn = hs_get_time_period_num(fake_time);
  122. tt_u64_op(tn, ==, 16903);
  123. { /* Check start time of next time period */
  124. retval = parse_rfc1123_time("Wed, 13 Apr 2016 12:00:00 UTC",
  125. &correct_time);
  126. tt_int_op(retval, ==, 0);
  127. start_time = hs_get_start_time_of_next_time_period(fake_time);
  128. tt_int_op(start_time, OP_EQ, correct_time);
  129. }
  130. /* Now take time to 12:00:00 UTC and check that the time period rotated */
  131. fake_time += 1;
  132. tn = hs_get_time_period_num(fake_time);
  133. tt_u64_op(tn, ==, 16904);
  134. /* Now also check our hs_get_next_time_period_num() function */
  135. tn = hs_get_next_time_period_num(fake_time);
  136. tt_u64_op(tn, ==, 16905);
  137. { /* Check start time of next time period again */
  138. retval = parse_rfc1123_time("Wed, 14 Apr 2016 12:00:00 UTC",
  139. &correct_time);
  140. tt_int_op(retval, ==, 0);
  141. start_time = hs_get_start_time_of_next_time_period(fake_time);
  142. tt_int_op(start_time, OP_EQ, correct_time);
  143. }
  144. /* Now do another sanity check: The time period number at the start of the
  145. * next time period, must be the same time period number as the one returned
  146. * from hs_get_next_time_period_num() */
  147. {
  148. time_t next_tp_start = hs_get_start_time_of_next_time_period(fake_time);
  149. tt_u64_op(hs_get_time_period_num(next_tp_start), OP_EQ,
  150. hs_get_next_time_period_num(fake_time));
  151. }
  152. done:
  153. ;
  154. }
  155. /** Test that we can correctly find the start time of the next time period */
  156. static void
  157. test_start_time_of_next_time_period(void *arg)
  158. {
  159. (void) arg;
  160. int retval;
  161. time_t fake_time;
  162. char tbuf[ISO_TIME_LEN + 1];
  163. time_t next_tp_start_time;
  164. /* Do some basic tests */
  165. retval = parse_rfc1123_time("Wed, 13 Apr 2016 11:00:00 UTC",
  166. &fake_time);
  167. tt_int_op(retval, ==, 0);
  168. next_tp_start_time = hs_get_start_time_of_next_time_period(fake_time);
  169. /* Compare it with the correct result */
  170. format_iso_time(tbuf, next_tp_start_time);
  171. tt_str_op("2016-04-13 12:00:00", OP_EQ, tbuf);
  172. /* Another test with an edge-case time (start of TP) */
  173. retval = parse_rfc1123_time("Wed, 13 Apr 2016 12:00:00 UTC",
  174. &fake_time);
  175. tt_int_op(retval, ==, 0);
  176. next_tp_start_time = hs_get_start_time_of_next_time_period(fake_time);
  177. format_iso_time(tbuf, next_tp_start_time);
  178. tt_str_op("2016-04-14 12:00:00", OP_EQ, tbuf);
  179. {
  180. /* Now pretend we are on a testing network and alter the voting schedule to
  181. be every 10 seconds. This means that a time period has length 10*24
  182. seconds (4 minutes). It also means that we apply a rotational offset of
  183. 120 seconds to the time period, so that it starts at 00:02:00 instead of
  184. 00:00:00. */
  185. or_options_t *options = get_options_mutable();
  186. options->TestingTorNetwork = 1;
  187. options->V3AuthVotingInterval = 10;
  188. options->TestingV3AuthInitialVotingInterval = 10;
  189. retval = parse_rfc1123_time("Wed, 13 Apr 2016 00:00:00 UTC",
  190. &fake_time);
  191. tt_int_op(retval, ==, 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 00:02:00", OP_EQ, tbuf);
  196. retval = parse_rfc1123_time("Wed, 13 Apr 2016 00:02:00 UTC",
  197. &fake_time);
  198. tt_int_op(retval, ==, 0);
  199. next_tp_start_time = hs_get_start_time_of_next_time_period(fake_time);
  200. /* Compare it with the correct result */
  201. format_iso_time(tbuf, next_tp_start_time);
  202. tt_str_op("2016-04-13 00:06:00", OP_EQ, tbuf);
  203. }
  204. done:
  205. ;
  206. }
  207. /** Test that our HS overlap period functions work properly. */
  208. static void
  209. test_desc_overlap_period(void *arg)
  210. {
  211. (void) arg;
  212. int retval;
  213. time_t now = time(NULL);
  214. networkstatus_t *dummy_consensus = NULL;
  215. /* First try with a consensus just inside the overlap period */
  216. dummy_consensus = tor_malloc_zero(sizeof(networkstatus_t));
  217. retval = parse_rfc1123_time("Wed, 13 Apr 2016 00:00:00 UTC",
  218. &dummy_consensus->valid_after);
  219. tt_int_op(retval, ==, 0);
  220. retval = hs_overlap_mode_is_active(dummy_consensus, now);
  221. tt_int_op(retval, ==, 1);
  222. /* Now increase the valid_after so that it goes to 11:00:00 UTC. Overlap
  223. period is still active. */
  224. dummy_consensus->valid_after += 3600*11;
  225. retval = hs_overlap_mode_is_active(dummy_consensus, now);
  226. tt_int_op(retval, ==, 1);
  227. /* Now increase the valid_after so that it goes to 11:59:59 UTC. Overlap
  228. period is still active. */
  229. dummy_consensus->valid_after += 3599;
  230. retval = hs_overlap_mode_is_active(dummy_consensus, now);
  231. tt_int_op(retval, ==, 1);
  232. /* Now increase the valid_after so that it drifts to noon, and check that
  233. overlap mode is not active anymore. */
  234. dummy_consensus->valid_after += 1;
  235. retval = hs_overlap_mode_is_active(dummy_consensus, now);
  236. tt_int_op(retval, ==, 0);
  237. /* Check that overlap mode is also inactive at 23:59:59 UTC */
  238. retval = parse_rfc1123_time("Wed, 13 Apr 2016 23:59:59 UTC",
  239. &dummy_consensus->valid_after);
  240. tt_int_op(retval, ==, 0);
  241. retval = hs_overlap_mode_is_active(dummy_consensus, now);
  242. tt_int_op(retval, ==, 0);
  243. done:
  244. tor_free(dummy_consensus);
  245. }
  246. /* Test the overlap period functions on a testnet with altered voting
  247. * schedule */
  248. static void
  249. test_desc_overlap_period_testnet(void *arg)
  250. {
  251. int retval;
  252. time_t now = approx_time();
  253. networkstatus_t *dummy_consensus = NULL;
  254. or_options_t *options = get_options_mutable();
  255. (void) arg;
  256. /* Set the testnet option and a 10-second voting interval */
  257. options->TestingTorNetwork = 1;
  258. options->V3AuthVotingInterval = 10;
  259. options->TestingV3AuthInitialVotingInterval = 10;
  260. dummy_consensus = tor_malloc_zero(sizeof(networkstatus_t));
  261. /* A 10-second voting interval means that the lengths of an SRV run and of a
  262. * time period are both 10*24 seconds (4 minutes). The SRV gets published at
  263. * 00:00:00 and the TP starts at 00:02:00 (rotation offset: 2 mins). Those
  264. * two minutes between SRV publish and TP start is the overlap period
  265. * window. Let's test it: */
  266. retval = parse_rfc1123_time("Wed, 13 Apr 2016 00:00:00 UTC",
  267. &dummy_consensus->valid_after);
  268. tt_int_op(retval, ==, 0);
  269. retval = hs_overlap_mode_is_active(dummy_consensus, now);
  270. tt_int_op(retval, ==, 1);
  271. retval = parse_rfc1123_time("Wed, 13 Apr 2016 00:01:59 UTC",
  272. &dummy_consensus->valid_after);
  273. tt_int_op(retval, ==, 0);
  274. retval = hs_overlap_mode_is_active(dummy_consensus, now);
  275. tt_int_op(retval, ==, 1);
  276. retval = parse_rfc1123_time("Wed, 13 Apr 2016 00:02:00 UTC",
  277. &dummy_consensus->valid_after);
  278. tt_int_op(retval, ==, 0);
  279. retval = hs_overlap_mode_is_active(dummy_consensus, now);
  280. tt_int_op(retval, ==, 0);
  281. retval = parse_rfc1123_time("Wed, 13 Apr 2016 00:04:00 UTC",
  282. &dummy_consensus->valid_after);
  283. tt_int_op(retval, ==, 0);
  284. retval = hs_overlap_mode_is_active(dummy_consensus, now);
  285. tt_int_op(retval, ==, 1);
  286. retval = parse_rfc1123_time("Wed, 13 Apr 2016 00:05:59 UTC",
  287. &dummy_consensus->valid_after);
  288. tt_int_op(retval, ==, 0);
  289. retval = hs_overlap_mode_is_active(dummy_consensus, now);
  290. tt_int_op(retval, ==, 1);
  291. retval = parse_rfc1123_time("Wed, 13 Apr 2016 00:06:00 UTC",
  292. &dummy_consensus->valid_after);
  293. tt_int_op(retval, ==, 0);
  294. retval = hs_overlap_mode_is_active(dummy_consensus, now);
  295. tt_int_op(retval, ==, 0);
  296. done:
  297. tor_free(dummy_consensus);
  298. }
  299. static networkstatus_t *mock_ns = NULL;
  300. static networkstatus_t *
  301. mock_networkstatus_get_latest_consensus(void)
  302. {
  303. time_t now = approx_time();
  304. /* If initialized, return it */
  305. if (mock_ns) {
  306. return mock_ns;
  307. }
  308. /* Initialize fake consensus */
  309. mock_ns = tor_malloc_zero(sizeof(networkstatus_t));
  310. /* This consensus is live */
  311. mock_ns->valid_after = now-1;
  312. mock_ns->fresh_until = now+1;
  313. mock_ns->valid_until = now+2;
  314. /* Create routerstatus list */
  315. mock_ns->routerstatus_list = smartlist_new();
  316. return mock_ns;
  317. }
  318. /** Test the responsible HSDirs calculation function */
  319. static void
  320. test_responsible_hsdirs(void *arg)
  321. {
  322. time_t now = approx_time();
  323. smartlist_t *responsible_dirs = smartlist_new();
  324. networkstatus_t *ns = NULL;
  325. routerstatus_t *rs = tor_malloc_zero(sizeof(routerstatus_t));
  326. (void) arg;
  327. hs_init();
  328. MOCK(networkstatus_get_latest_consensus,
  329. mock_networkstatus_get_latest_consensus);
  330. ns = networkstatus_get_latest_consensus();
  331. { /* First router: HSdir */
  332. tor_addr_t ipv4_addr;
  333. memset(rs->identity_digest, 'A', DIGEST_LEN);
  334. rs->is_hs_dir = 1;
  335. rs->supports_v3_hsdir = 1;
  336. routerinfo_t ri;
  337. memset(&ri, 0 ,sizeof(routerinfo_t));
  338. tor_addr_parse(&ipv4_addr, "127.0.0.1");
  339. ri.addr = tor_addr_to_ipv4h(&ipv4_addr);
  340. ri.nickname = tor_strdup("fatal");
  341. ri.protocol_list = (char *) "HSDir=1-2 LinkAuth=3";
  342. memset(ri.cache_info.identity_digest, 'A', DIGEST_LEN);
  343. tt_assert(nodelist_set_routerinfo(&ri, NULL));
  344. node_t *node = node_get_mutable_by_id(ri.cache_info.identity_digest);
  345. memset(node->hsdir_index->current, 'Z',
  346. sizeof(node->hsdir_index->current));
  347. smartlist_add(ns->routerstatus_list, rs);
  348. }
  349. ed25519_public_key_t blinded_pk;
  350. uint64_t time_period_num = hs_get_time_period_num(now);
  351. hs_get_responsible_hsdirs(&blinded_pk, time_period_num,
  352. 0, 0, responsible_dirs);
  353. tt_int_op(smartlist_len(responsible_dirs), OP_EQ, 1);
  354. /** TODO: Build a bigger network and do more tests here */
  355. done:
  356. routerstatus_free(rs);
  357. smartlist_free(responsible_dirs);
  358. smartlist_clear(ns->routerstatus_list);
  359. networkstatus_vote_free(mock_ns);
  360. }
  361. /** Test disaster SRV computation and caching */
  362. static void
  363. test_disaster_srv(void *arg)
  364. {
  365. uint8_t *cached_disaster_srv_one = NULL;
  366. uint8_t *cached_disaster_srv_two = NULL;
  367. uint8_t srv_one[DIGEST256_LEN] = {0};
  368. uint8_t srv_two[DIGEST256_LEN] = {0};
  369. uint8_t srv_three[DIGEST256_LEN] = {0};
  370. uint8_t srv_four[DIGEST256_LEN] = {0};
  371. uint8_t srv_five[DIGEST256_LEN] = {0};
  372. (void) arg;
  373. /* Get the cached SRVs: we gonna use them later for verification */
  374. cached_disaster_srv_one = get_first_cached_disaster_srv();
  375. cached_disaster_srv_two = get_second_cached_disaster_srv();
  376. /* Compute some srvs */
  377. get_disaster_srv(1, srv_one);
  378. get_disaster_srv(2, srv_two);
  379. /* Check that the cached ones where updated */
  380. tt_mem_op(cached_disaster_srv_one, OP_EQ, srv_one, DIGEST256_LEN);
  381. tt_mem_op(cached_disaster_srv_two, OP_EQ, srv_two, DIGEST256_LEN);
  382. /* Ask for an SRV that has already been computed */
  383. get_disaster_srv(2, srv_two);
  384. /* and check that the cache entries have not changed */
  385. tt_mem_op(cached_disaster_srv_one, OP_EQ, srv_one, DIGEST256_LEN);
  386. tt_mem_op(cached_disaster_srv_two, OP_EQ, srv_two, DIGEST256_LEN);
  387. /* Ask for a new SRV */
  388. get_disaster_srv(3, srv_three);
  389. tt_mem_op(cached_disaster_srv_one, OP_EQ, srv_three, DIGEST256_LEN);
  390. tt_mem_op(cached_disaster_srv_two, OP_EQ, srv_two, DIGEST256_LEN);
  391. /* Ask for another SRV: none of the original SRVs should now be cached */
  392. get_disaster_srv(4, srv_four);
  393. tt_mem_op(cached_disaster_srv_one, OP_EQ, srv_three, DIGEST256_LEN);
  394. tt_mem_op(cached_disaster_srv_two, OP_EQ, srv_four, DIGEST256_LEN);
  395. /* Ask for yet another SRV */
  396. get_disaster_srv(5, srv_five);
  397. tt_mem_op(cached_disaster_srv_one, OP_EQ, srv_five, DIGEST256_LEN);
  398. tt_mem_op(cached_disaster_srv_two, OP_EQ, srv_four, DIGEST256_LEN);
  399. done:
  400. ;
  401. }
  402. struct testcase_t hs_common_tests[] = {
  403. { "build_address", test_build_address, TT_FORK,
  404. NULL, NULL },
  405. { "validate_address", test_validate_address, TT_FORK,
  406. NULL, NULL },
  407. { "time_period", test_time_period, TT_FORK,
  408. NULL, NULL },
  409. { "start_time_of_next_time_period", test_start_time_of_next_time_period,
  410. TT_FORK, NULL, NULL },
  411. { "desc_overlap_period", test_desc_overlap_period, TT_FORK,
  412. NULL, NULL },
  413. { "desc_overlap_period_testnet", test_desc_overlap_period_testnet, TT_FORK,
  414. NULL, NULL },
  415. { "desc_responsible_hsdirs", test_responsible_hsdirs, TT_FORK,
  416. NULL, NULL },
  417. { "disaster_srv", test_disaster_srv, TT_FORK, NULL, NULL },
  418. END_OF_TESTCASES
  419. };