test_hs_common.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828
  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. int identity_idx,
  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. uint8_t identity[DIGEST_LEN];
  311. uint8_t curr_hsdir_index[DIGEST256_LEN];
  312. tor_addr_t ipv4_addr;
  313. memset(identity, identity_idx, sizeof(identity));
  314. memset(curr_hsdir_index, identity_idx, sizeof(curr_hsdir_index));
  315. memcpy(rs->identity_digest, identity, DIGEST_LEN);
  316. rs->is_hs_dir = is_hsdir;
  317. rs->supports_v3_hsdir = 1;
  318. tor_addr_parse(&ipv4_addr, "1.2.3.4");
  319. ri->addr = tor_addr_to_ipv4h(&ipv4_addr);
  320. ri->nickname = tor_strdup(nickname);
  321. ri->protocol_list = tor_strdup("HSDir=1-2 LinkAuth=3");
  322. memcpy(ri->cache_info.identity_digest, identity, DIGEST_LEN);
  323. tt_assert(nodelist_set_routerinfo(ri, NULL));
  324. node_t *node = node_get_mutable_by_id(ri->cache_info.identity_digest);
  325. tt_assert(node);
  326. node->rs = rs;
  327. memcpy(node->hsdir_index->current, curr_hsdir_index,
  328. sizeof(node->hsdir_index->current));
  329. smartlist_add(ns->routerstatus_list, rs);
  330. done:
  331. ;
  332. }
  333. static networkstatus_t *mock_ns = NULL;
  334. static networkstatus_t *
  335. mock_networkstatus_get_latest_consensus(void)
  336. {
  337. time_t now = approx_time();
  338. /* If initialized, return it */
  339. if (mock_ns) {
  340. return mock_ns;
  341. }
  342. /* Initialize fake consensus */
  343. mock_ns = tor_malloc_zero(sizeof(networkstatus_t));
  344. /* This consensus is live */
  345. mock_ns->valid_after = now-1;
  346. mock_ns->fresh_until = now+1;
  347. mock_ns->valid_until = now+2;
  348. /* Create routerstatus list */
  349. mock_ns->routerstatus_list = smartlist_new();
  350. return mock_ns;
  351. }
  352. /** Test the responsible HSDirs calculation function */
  353. static void
  354. test_responsible_hsdirs(void *arg)
  355. {
  356. time_t now = approx_time();
  357. smartlist_t *responsible_dirs = smartlist_new();
  358. networkstatus_t *ns = NULL;
  359. int retval;
  360. (void) arg;
  361. hs_init();
  362. MOCK(networkstatus_get_latest_consensus,
  363. mock_networkstatus_get_latest_consensus);
  364. ns = networkstatus_get_latest_consensus();
  365. { /* First router: HSdir */
  366. helper_add_hsdir_to_networkstatus(ns, 1, "igor", 1);
  367. }
  368. { /* Second HSDir */
  369. helper_add_hsdir_to_networkstatus(ns, 2, "victor", 1);
  370. }
  371. { /* Third relay but not HSDir */
  372. helper_add_hsdir_to_networkstatus(ns, 3, "spyro", 0);
  373. }
  374. ed25519_keypair_t kp;
  375. retval = ed25519_keypair_generate(&kp, 0);
  376. tt_int_op(retval, OP_EQ , 0);
  377. uint64_t time_period_num = hs_get_time_period_num(now);
  378. hs_get_responsible_hsdirs(&kp.pubkey, time_period_num,
  379. 0, 0, responsible_dirs);
  380. /* Make sure that we only found 2 responsible HSDirs.
  381. * The third relay was not an hsdir! */
  382. tt_int_op(smartlist_len(responsible_dirs), OP_EQ, 2);
  383. /** TODO: Build a bigger network and do more tests here */
  384. done:
  385. SMARTLIST_FOREACH(ns->routerstatus_list,
  386. routerstatus_t *, rs, routerstatus_free(rs));
  387. smartlist_free(responsible_dirs);
  388. smartlist_clear(ns->routerstatus_list);
  389. networkstatus_vote_free(mock_ns);
  390. }
  391. static void
  392. mock_directory_initiate_request(directory_request_t *req)
  393. {
  394. (void)req;
  395. return;
  396. }
  397. static int
  398. mock_hs_desc_encode_descriptor(const hs_descriptor_t *desc,
  399. const ed25519_keypair_t *signing_kp,
  400. char **encoded_out)
  401. {
  402. (void)desc;
  403. (void)signing_kp;
  404. tor_asprintf(encoded_out, "lulu");
  405. return 0;
  406. }
  407. static or_state_t dummy_state;
  408. /* Mock function to get fake or state (used for rev counters) */
  409. static or_state_t *
  410. get_or_state_replacement(void)
  411. {
  412. return &dummy_state;
  413. }
  414. static int
  415. mock_router_have_minimum_dir_info(void)
  416. {
  417. return 1;
  418. }
  419. /** Test that we correctly detect when the HSDir hash ring changes so that we
  420. * reupload our descriptor. */
  421. static void
  422. test_desc_reupload_logic(void *arg)
  423. {
  424. networkstatus_t *ns = NULL;
  425. (void) arg;
  426. hs_init();
  427. MOCK(router_have_minimum_dir_info,
  428. mock_router_have_minimum_dir_info);
  429. MOCK(get_or_state,
  430. get_or_state_replacement);
  431. MOCK(networkstatus_get_latest_consensus,
  432. mock_networkstatus_get_latest_consensus);
  433. MOCK(directory_initiate_request,
  434. mock_directory_initiate_request);
  435. MOCK(hs_desc_encode_descriptor,
  436. mock_hs_desc_encode_descriptor);
  437. ns = networkstatus_get_latest_consensus();
  438. /** Test logic:
  439. * 1) Upload descriptor to HSDirs
  440. * CHECK that previous_hsdirs list was populated.
  441. * 2) Then call router_dir_info_changed() without an HSDir set change.
  442. * CHECK that no reuplod occurs.
  443. * 3) Now change the HSDir set, and call dir_info_changed() again.
  444. * CHECK that reupload occurs.
  445. * 4) Finally call service_desc_schedule_upload().
  446. * CHECK that previous_hsdirs list was cleared.
  447. **/
  448. /* Let's start by building our descriptor and service */
  449. hs_service_descriptor_t *desc = service_descriptor_new();
  450. hs_service_t *service = NULL;
  451. char onion_addr[HS_SERVICE_ADDR_LEN_BASE32 + 1];
  452. ed25519_public_key_t pubkey;
  453. memset(&pubkey, '\x42', sizeof(pubkey));
  454. hs_build_address(&pubkey, HS_VERSION_THREE, onion_addr);
  455. service = tor_malloc_zero(sizeof(hs_service_t));
  456. memcpy(service->onion_address, onion_addr, sizeof(service->onion_address));
  457. ed25519_secret_key_generate(&service->keys.identity_sk, 0);
  458. ed25519_public_key_generate(&service->keys.identity_pk,
  459. &service->keys.identity_sk);
  460. service->desc_current = desc;
  461. /* Also add service to service map */
  462. hs_service_ht *service_map = get_hs_service_map();
  463. tt_assert(service_map);
  464. tt_int_op(hs_service_get_num_services(), OP_EQ, 0);
  465. register_service(service_map, service);
  466. tt_int_op(hs_service_get_num_services(), OP_EQ, 1);
  467. /* Now let's create our hash ring: */
  468. { /* First HSDir */
  469. helper_add_hsdir_to_networkstatus(ns, 1, "dingus", 1);
  470. }
  471. { /* Second HSDir */
  472. helper_add_hsdir_to_networkstatus(ns, 2, "clive", 1);
  473. }
  474. /* Now let's upload our desc to all hsdirs */
  475. upload_descriptor_to_all(service, desc, 0);
  476. /* Check that previous hsdirs were populated */
  477. tt_int_op(smartlist_len(desc->previous_hsdirs), OP_EQ, 2);
  478. /* Poison next upload time so that we can see if it was changed by
  479. * router_dir_info_changed(). No changes in hash ring so far, so the upload
  480. * time should stay as is. */
  481. desc->next_upload_time = 42;
  482. router_dir_info_changed();
  483. tt_int_op(desc->next_upload_time, OP_EQ, 42);
  484. /* Now change the HSDir hash ring by adding another node */
  485. { /* Third HSDir */
  486. helper_add_hsdir_to_networkstatus(ns, 3, "ringo", 1);
  487. }
  488. /* Now call service_desc_hsdirs_changed() and see that it detected the hash
  489. ring change */
  490. time_t now = approx_time();
  491. tt_assert(now);
  492. tt_int_op(service_desc_hsdirs_changed(service, desc), OP_EQ, 1);
  493. /* Now pretend that the descriptor changed, and order a reupload to all
  494. HSDirs. Make sure that the set of previous HSDirs was cleared. */
  495. service_desc_schedule_upload(desc, now, 1);
  496. tt_int_op(smartlist_len(desc->previous_hsdirs), OP_EQ, 0);
  497. /* Now reupload again: see that the prev hsdir set got populated again. */
  498. upload_descriptor_to_all(service, desc, 0);
  499. tt_int_op(smartlist_len(desc->previous_hsdirs), OP_EQ, 3);
  500. done:
  501. hs_free_all();
  502. }
  503. /** Test disaster SRV computation and caching */
  504. static void
  505. test_disaster_srv(void *arg)
  506. {
  507. uint8_t *cached_disaster_srv_one = NULL;
  508. uint8_t *cached_disaster_srv_two = NULL;
  509. uint8_t srv_one[DIGEST256_LEN] = {0};
  510. uint8_t srv_two[DIGEST256_LEN] = {0};
  511. uint8_t srv_three[DIGEST256_LEN] = {0};
  512. uint8_t srv_four[DIGEST256_LEN] = {0};
  513. uint8_t srv_five[DIGEST256_LEN] = {0};
  514. (void) arg;
  515. /* Get the cached SRVs: we gonna use them later for verification */
  516. cached_disaster_srv_one = get_first_cached_disaster_srv();
  517. cached_disaster_srv_two = get_second_cached_disaster_srv();
  518. /* Compute some srvs */
  519. get_disaster_srv(1, srv_one);
  520. get_disaster_srv(2, srv_two);
  521. /* Check that the cached ones where updated */
  522. tt_mem_op(cached_disaster_srv_one, OP_EQ, srv_one, DIGEST256_LEN);
  523. tt_mem_op(cached_disaster_srv_two, OP_EQ, srv_two, DIGEST256_LEN);
  524. /* Ask for an SRV that has already been computed */
  525. get_disaster_srv(2, srv_two);
  526. /* and check that the cache entries have not changed */
  527. tt_mem_op(cached_disaster_srv_one, OP_EQ, srv_one, DIGEST256_LEN);
  528. tt_mem_op(cached_disaster_srv_two, OP_EQ, srv_two, DIGEST256_LEN);
  529. /* Ask for a new SRV */
  530. get_disaster_srv(3, srv_three);
  531. tt_mem_op(cached_disaster_srv_one, OP_EQ, srv_three, DIGEST256_LEN);
  532. tt_mem_op(cached_disaster_srv_two, OP_EQ, srv_two, DIGEST256_LEN);
  533. /* Ask for another SRV: none of the original SRVs should now be cached */
  534. get_disaster_srv(4, srv_four);
  535. tt_mem_op(cached_disaster_srv_one, OP_EQ, srv_three, DIGEST256_LEN);
  536. tt_mem_op(cached_disaster_srv_two, OP_EQ, srv_four, DIGEST256_LEN);
  537. /* Ask for yet another SRV */
  538. get_disaster_srv(5, srv_five);
  539. tt_mem_op(cached_disaster_srv_one, OP_EQ, srv_five, DIGEST256_LEN);
  540. tt_mem_op(cached_disaster_srv_two, OP_EQ, srv_four, DIGEST256_LEN);
  541. done:
  542. ;
  543. }
  544. /** Test our HS descriptor request tracker by making various requests and
  545. * checking whether they get tracked properly. */
  546. static void
  547. test_hid_serv_request_tracker(void *arg)
  548. {
  549. (void) arg;
  550. time_t retval;
  551. routerstatus_t *hsdir = NULL, *hsdir2 = NULL, *hsdir3 = NULL;
  552. time_t now = approx_time();
  553. const char *req_key_str_first =
  554. "vd4zb6zesaubtrjvdqcr2w7x7lhw2up4Xnw4526ThUNbL5o1go+EdUuEqlKxHkNbnK41pRzizzs";
  555. const char *req_key_str_second =
  556. "g53o7iavcd62oihswhr24u6czmqws5kpXnw4526ThUNbL5o1go+EdUuEqlKxHkNbnK41pRzizzs";
  557. const char *req_key_str_small = "ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ";
  558. /*************************** basic test *******************************/
  559. /* Get request tracker and make sure it's empty */
  560. strmap_t *request_tracker = get_last_hid_serv_requests();
  561. tt_int_op(strmap_size(request_tracker),OP_EQ, 0);
  562. /* Let's register a hid serv request */
  563. hsdir = tor_malloc_zero(sizeof(routerstatus_t));
  564. memset(hsdir->identity_digest, 'Z', DIGEST_LEN);
  565. retval = hs_lookup_last_hid_serv_request(hsdir, req_key_str_first,
  566. now, 1);
  567. tt_int_op(retval, OP_EQ, now);
  568. tt_int_op(strmap_size(request_tracker),OP_EQ, 1);
  569. /* Let's lookup a non-existent hidserv request */
  570. retval = hs_lookup_last_hid_serv_request(hsdir, req_key_str_second,
  571. now+1, 0);
  572. tt_int_op(retval, OP_EQ, 0);
  573. tt_int_op(strmap_size(request_tracker),OP_EQ, 1);
  574. /* Let's lookup a real hidserv request */
  575. retval = hs_lookup_last_hid_serv_request(hsdir, req_key_str_first,
  576. now+2, 0);
  577. tt_int_op(retval, OP_EQ, now); /* we got it */
  578. tt_int_op(strmap_size(request_tracker),OP_EQ, 1);
  579. /**********************************************************************/
  580. /* Let's add another request for the same HS but on a different HSDir. */
  581. hsdir2 = tor_malloc_zero(sizeof(routerstatus_t));
  582. memset(hsdir2->identity_digest, 2, DIGEST_LEN);
  583. retval = hs_lookup_last_hid_serv_request(hsdir2, req_key_str_first,
  584. now+3, 1);
  585. tt_int_op(retval, OP_EQ, now+3);
  586. tt_int_op(strmap_size(request_tracker),OP_EQ, 2);
  587. /* Check that we can clean the first request based on time */
  588. hs_clean_last_hid_serv_requests(now+3+REND_HID_SERV_DIR_REQUERY_PERIOD);
  589. tt_int_op(strmap_size(request_tracker),OP_EQ, 1);
  590. /* Check that it doesn't exist anymore */
  591. retval = hs_lookup_last_hid_serv_request(hsdir, req_key_str_first,
  592. now+2, 0);
  593. tt_int_op(retval, OP_EQ, 0);
  594. /* Now let's add a smaller req key str */
  595. hsdir3 = tor_malloc_zero(sizeof(routerstatus_t));
  596. memset(hsdir3->identity_digest, 3, DIGEST_LEN);
  597. retval = hs_lookup_last_hid_serv_request(hsdir3, req_key_str_small,
  598. now+4, 1);
  599. tt_int_op(retval, OP_EQ, now+4);
  600. tt_int_op(strmap_size(request_tracker),OP_EQ, 2);
  601. /*************************** deleting entries **************************/
  602. /* Add another request with very short key */
  603. retval = hs_lookup_last_hid_serv_request(hsdir, "l", now, 1);
  604. tt_int_op(strmap_size(request_tracker),OP_EQ, 3);
  605. /* Try deleting entries with a dummy key. Check that our previous requests
  606. * are still there */
  607. tor_capture_bugs_(1);
  608. hs_purge_hid_serv_from_last_hid_serv_requests("a");
  609. tt_int_op(strmap_size(request_tracker),OP_EQ, 3);
  610. tor_end_capture_bugs_();
  611. /* Try another dummy key. Check that requests are still there */
  612. {
  613. char dummy[2000];
  614. memset(dummy, 'Z', 2000);
  615. dummy[1999] = '\x00';
  616. hs_purge_hid_serv_from_last_hid_serv_requests(dummy);
  617. tt_int_op(strmap_size(request_tracker),OP_EQ, 3);
  618. }
  619. /* Another dummy key! */
  620. hs_purge_hid_serv_from_last_hid_serv_requests(req_key_str_second);
  621. tt_int_op(strmap_size(request_tracker),OP_EQ, 3);
  622. /* Now actually delete a request! */
  623. hs_purge_hid_serv_from_last_hid_serv_requests(req_key_str_first);
  624. tt_int_op(strmap_size(request_tracker),OP_EQ, 2);
  625. /* Purge it all! */
  626. hs_purge_last_hid_serv_requests();
  627. request_tracker = get_last_hid_serv_requests();
  628. tt_int_op(strmap_size(request_tracker),OP_EQ, 0);
  629. done:
  630. tor_free(hsdir);
  631. tor_free(hsdir2);
  632. tor_free(hsdir3);
  633. }
  634. static void
  635. test_parse_extended_hostname(void *arg)
  636. {
  637. (void) arg;
  638. char address1[] = "fooaddress.onion";
  639. char address2[] = "aaaaaaaaaaaaaaaa.onion";
  640. char address3[] = "fooaddress.exit";
  641. char address4[] = "www.torproject.org";
  642. char address5[] = "foo.abcdefghijklmnop.onion";
  643. char address6[] = "foo.bar.abcdefghijklmnop.onion";
  644. char address7[] = ".abcdefghijklmnop.onion";
  645. char address8[] =
  646. "www.p3xnclpu4mu22dwaurjtsybyqk4xfjmcfz6z62yl24uwmhjatiwnlnad.onion";
  647. tt_assert(BAD_HOSTNAME == parse_extended_hostname(address1));
  648. tt_assert(ONION_V2_HOSTNAME == parse_extended_hostname(address2));
  649. tt_str_op(address2,OP_EQ, "aaaaaaaaaaaaaaaa");
  650. tt_assert(EXIT_HOSTNAME == parse_extended_hostname(address3));
  651. tt_assert(NORMAL_HOSTNAME == parse_extended_hostname(address4));
  652. tt_assert(ONION_V2_HOSTNAME == parse_extended_hostname(address5));
  653. tt_str_op(address5,OP_EQ, "abcdefghijklmnop");
  654. tt_assert(ONION_V2_HOSTNAME == parse_extended_hostname(address6));
  655. tt_str_op(address6,OP_EQ, "abcdefghijklmnop");
  656. tt_assert(BAD_HOSTNAME == parse_extended_hostname(address7));
  657. tt_assert(ONION_V3_HOSTNAME == parse_extended_hostname(address8));
  658. tt_str_op(address8, OP_EQ,
  659. "p3xnclpu4mu22dwaurjtsybyqk4xfjmcfz6z62yl24uwmhjatiwnlnad");
  660. done: ;
  661. }
  662. struct testcase_t hs_common_tests[] = {
  663. { "build_address", test_build_address, TT_FORK,
  664. NULL, NULL },
  665. { "validate_address", test_validate_address, TT_FORK,
  666. NULL, NULL },
  667. { "time_period", test_time_period, TT_FORK,
  668. NULL, NULL },
  669. { "start_time_of_next_time_period", test_start_time_of_next_time_period,
  670. TT_FORK, NULL, NULL },
  671. { "desc_overlap_period", test_desc_overlap_period, TT_FORK,
  672. NULL, NULL },
  673. { "desc_overlap_period_testnet", test_desc_overlap_period_testnet, TT_FORK,
  674. NULL, NULL },
  675. { "responsible_hsdirs", test_responsible_hsdirs, TT_FORK,
  676. NULL, NULL },
  677. { "desc_reupload_logic", test_desc_reupload_logic, TT_FORK,
  678. NULL, NULL },
  679. { "disaster_srv", test_disaster_srv, TT_FORK,
  680. NULL, NULL },
  681. { "hid_serv_request_tracker", test_hid_serv_request_tracker, TT_FORK,
  682. NULL, NULL },
  683. { "parse_extended_hostname", test_parse_extended_hostname, TT_FORK,
  684. NULL, NULL },
  685. END_OF_TESTCASES
  686. };