test_hs_common.c 27 KB

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