test_hs_common.c 29 KB

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