test_hs_service.c 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591
  1. /* Copyright (c) 2016-2018, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. /**
  4. * \file test_hs_service.c
  5. * \brief Test hidden service functionality.
  6. */
  7. #define CIRCUITBUILD_PRIVATE
  8. #define CIRCUITLIST_PRIVATE
  9. #define CONFIG_PRIVATE
  10. #define CONNECTION_PRIVATE
  11. #define CRYPTO_PRIVATE
  12. #define HS_COMMON_PRIVATE
  13. #define HS_SERVICE_PRIVATE
  14. #define HS_INTROPOINT_PRIVATE
  15. #define HS_CIRCUIT_PRIVATE
  16. #define MAIN_PRIVATE
  17. #define NETWORKSTATUS_PRIVATE
  18. #define STATEFILE_PRIVATE
  19. #define TOR_CHANNEL_INTERNAL_
  20. #define HS_CLIENT_PRIVATE
  21. #define ROUTERPARSE_PRIVATE
  22. #include "test/test.h"
  23. #include "test/test_helpers.h"
  24. #include "test/log_test_helpers.h"
  25. #include "test/rend_test_helpers.h"
  26. #include "test/hs_test_helpers.h"
  27. #include "or/or.h"
  28. #include "or/config.h"
  29. #include "or/circuitbuild.h"
  30. #include "or/circuitlist.h"
  31. #include "or/circuituse.h"
  32. #include "lib/crypt_ops/crypto_rand.h"
  33. #include "or/dirauth/dirvote.h"
  34. #include "or/networkstatus.h"
  35. #include "or/nodelist.h"
  36. #include "or/relay.h"
  37. #include "or/routerparse.h"
  38. #include "or/hs_common.h"
  39. #include "or/hs_config.h"
  40. #include "or/hs_ident.h"
  41. #include "or/hs_intropoint.h"
  42. #include "or/hs_ntor.h"
  43. #include "or/hs_circuit.h"
  44. #include "or/hs_service.h"
  45. #include "or/hs_client.h"
  46. #include "or/main.h"
  47. #include "or/rendservice.h"
  48. #include "or/statefile.h"
  49. #include "or/dirauth/shared_random_state.h"
  50. #include "or/voting_schedule.h"
  51. #include "or/cpath_build_state_st.h"
  52. #include "or/crypt_path_st.h"
  53. #include "or/networkstatus_st.h"
  54. #include "or/node_st.h"
  55. #include "or/origin_circuit_st.h"
  56. #include "or/routerinfo_st.h"
  57. /* Trunnel */
  58. #include "trunnel/hs/cell_establish_intro.h"
  59. static networkstatus_t mock_ns;
  60. static networkstatus_t *
  61. mock_networkstatus_get_live_consensus(time_t now)
  62. {
  63. (void) now;
  64. return &mock_ns;
  65. }
  66. static or_state_t *dummy_state = NULL;
  67. /* Mock function to get fake or state (used for rev counters) */
  68. static or_state_t *
  69. get_or_state_replacement(void)
  70. {
  71. return dummy_state;
  72. }
  73. /* Mock function because we are not trying to test the close circuit that does
  74. * an awful lot of checks on the circuit object. */
  75. static void
  76. mock_circuit_mark_for_close(circuit_t *circ, int reason, int line,
  77. const char *file)
  78. {
  79. (void) circ;
  80. (void) reason;
  81. (void) line;
  82. (void) file;
  83. return;
  84. }
  85. static int
  86. mock_relay_send_command_from_edge(streamid_t stream_id, circuit_t *circ,
  87. uint8_t relay_command, const char *payload,
  88. size_t payload_len,
  89. crypt_path_t *cpath_layer,
  90. const char *filename, int lineno)
  91. {
  92. (void) stream_id;
  93. (void) circ;
  94. (void) relay_command;
  95. (void) payload;
  96. (void) payload_len;
  97. (void) cpath_layer;
  98. (void) filename;
  99. (void) lineno;
  100. return 0;
  101. }
  102. /* Helper: from a set of options in conf, configure a service which will add
  103. * it to the staging list of the HS subsytem. */
  104. static int
  105. helper_config_service(const char *conf)
  106. {
  107. int ret = 0;
  108. or_options_t *options = NULL;
  109. tt_assert(conf);
  110. options = helper_parse_options(conf);
  111. tt_assert(options);
  112. ret = hs_config_service_all(options, 0);
  113. done:
  114. or_options_free(options);
  115. return ret;
  116. }
  117. /* Test: Ensure that setting up rendezvous circuits works correctly. */
  118. static void
  119. test_e2e_rend_circuit_setup(void *arg)
  120. {
  121. ed25519_public_key_t service_pk;
  122. origin_circuit_t *or_circ;
  123. int retval;
  124. /** In this test we create a v3 prop224 service-side rendezvous circuit.
  125. * We simulate an HS ntor key exchange with a client, and check that
  126. * the circuit was setup correctly and is ready to accept rendezvous data */
  127. (void) arg;
  128. /* Now make dummy circuit */
  129. {
  130. or_circ = origin_circuit_new();
  131. or_circ->base_.purpose = CIRCUIT_PURPOSE_S_CONNECT_REND;
  132. or_circ->build_state = tor_malloc_zero(sizeof(cpath_build_state_t));
  133. or_circ->build_state->is_internal = 1;
  134. /* prop224: Setup hs conn identifier on the stream */
  135. ed25519_secret_key_t sk;
  136. tt_int_op(0, OP_EQ, ed25519_secret_key_generate(&sk, 0));
  137. tt_int_op(0, OP_EQ, ed25519_public_key_generate(&service_pk, &sk));
  138. or_circ->hs_ident = hs_ident_circuit_new(&service_pk,
  139. HS_IDENT_CIRCUIT_RENDEZVOUS);
  140. TO_CIRCUIT(or_circ)->state = CIRCUIT_STATE_OPEN;
  141. }
  142. /* Check number of hops */
  143. retval = cpath_get_n_hops(&or_circ->cpath);
  144. tt_int_op(retval, OP_EQ, 0);
  145. /* Setup the circuit: do the ntor key exchange */
  146. {
  147. uint8_t ntor_key_seed[DIGEST256_LEN] = {2};
  148. retval = hs_circuit_setup_e2e_rend_circ(or_circ,
  149. ntor_key_seed, sizeof(ntor_key_seed),
  150. 1);
  151. tt_int_op(retval, OP_EQ, 0);
  152. }
  153. /* See that a hop was added to the circuit's cpath */
  154. retval = cpath_get_n_hops(&or_circ->cpath);
  155. tt_int_op(retval, OP_EQ, 1);
  156. /* Check the digest algo */
  157. tt_int_op(crypto_digest_get_algorithm(or_circ->cpath->crypto.f_digest),
  158. OP_EQ, DIGEST_SHA3_256);
  159. tt_int_op(crypto_digest_get_algorithm(or_circ->cpath->crypto.b_digest),
  160. OP_EQ, DIGEST_SHA3_256);
  161. tt_assert(or_circ->cpath->crypto.f_crypto);
  162. tt_assert(or_circ->cpath->crypto.b_crypto);
  163. /* Ensure that circ purpose was changed */
  164. tt_int_op(or_circ->base_.purpose, OP_EQ, CIRCUIT_PURPOSE_S_REND_JOINED);
  165. done:
  166. circuit_free_(TO_CIRCUIT(or_circ));
  167. }
  168. /* Helper: Return a newly allocated and initialized origin circuit with
  169. * purpose and flags. A default HS identifier is set to an ed25519
  170. * authentication key for introduction point. */
  171. static origin_circuit_t *
  172. helper_create_origin_circuit(int purpose, int flags)
  173. {
  174. origin_circuit_t *circ = NULL;
  175. circ = origin_circuit_init(purpose, flags);
  176. tor_assert(circ);
  177. circ->cpath = tor_malloc_zero(sizeof(crypt_path_t));
  178. circ->cpath->magic = CRYPT_PATH_MAGIC;
  179. circ->cpath->state = CPATH_STATE_OPEN;
  180. circ->cpath->package_window = circuit_initial_package_window();
  181. circ->cpath->deliver_window = CIRCWINDOW_START;
  182. circ->cpath->prev = circ->cpath;
  183. /* Random nonce. */
  184. crypto_rand(circ->cpath->prev->rend_circ_nonce, DIGEST_LEN);
  185. /* Create a default HS identifier. */
  186. circ->hs_ident = tor_malloc_zero(sizeof(hs_ident_circuit_t));
  187. return circ;
  188. }
  189. /* Helper: Return a newly allocated service object with the identity keypair
  190. * sets and the current descriptor. Then register it to the global map.
  191. * Caller should us hs_free_all() to free this service or remove it from the
  192. * global map before freeing. */
  193. static hs_service_t *
  194. helper_create_service(void)
  195. {
  196. /* Set a service for this circuit. */
  197. hs_service_t *service = hs_service_new(get_options());
  198. tor_assert(service);
  199. service->config.version = HS_VERSION_THREE;
  200. ed25519_secret_key_generate(&service->keys.identity_sk, 0);
  201. ed25519_public_key_generate(&service->keys.identity_pk,
  202. &service->keys.identity_sk);
  203. service->desc_current = service_descriptor_new();
  204. tt_assert(service->desc_current);
  205. /* Register service to global map. */
  206. int ret = register_service(get_hs_service_map(), service);
  207. tt_int_op(ret, OP_EQ, 0);
  208. done:
  209. return service;
  210. }
  211. /* Helper: Return a newly allocated service intro point with two link
  212. * specifiers, one IPv4 and one legacy ID set to As. */
  213. static hs_service_intro_point_t *
  214. helper_create_service_ip(void)
  215. {
  216. hs_desc_link_specifier_t *ls;
  217. hs_service_intro_point_t *ip = service_intro_point_new(NULL, 0);
  218. tor_assert(ip);
  219. /* Add a first unused link specifier. */
  220. ls = tor_malloc_zero(sizeof(*ls));
  221. ls->type = LS_IPV4;
  222. smartlist_add(ip->base.link_specifiers, ls);
  223. /* Add a second link specifier used by a test. */
  224. ls = tor_malloc_zero(sizeof(*ls));
  225. ls->type = LS_LEGACY_ID;
  226. memset(ls->u.legacy_id, 'A', sizeof(ls->u.legacy_id));
  227. smartlist_add(ip->base.link_specifiers, ls);
  228. return ip;
  229. }
  230. static void
  231. test_load_keys(void *arg)
  232. {
  233. int ret;
  234. char *conf = NULL;
  235. char *hsdir_v2 = tor_strdup(get_fname("hs2"));
  236. char *hsdir_v3 = tor_strdup(get_fname("hs3"));
  237. char addr[HS_SERVICE_ADDR_LEN_BASE32 + 1];
  238. (void) arg;
  239. /* We'll register two services, a v2 and a v3, then we'll load keys and
  240. * validate that both are in a correct state. */
  241. hs_init();
  242. #define conf_fmt \
  243. "HiddenServiceDir %s\n" \
  244. "HiddenServiceVersion %d\n" \
  245. "HiddenServicePort 65535\n"
  246. /* v2 service. */
  247. tor_asprintf(&conf, conf_fmt, hsdir_v2, HS_VERSION_TWO);
  248. ret = helper_config_service(conf);
  249. tor_free(conf);
  250. tt_int_op(ret, OP_EQ, 0);
  251. /* This one should now be registered into the v2 list. */
  252. tt_int_op(get_hs_service_staging_list_size(), OP_EQ, 0);
  253. tt_int_op(rend_num_services(), OP_EQ, 1);
  254. /* v3 service. */
  255. tor_asprintf(&conf, conf_fmt, hsdir_v3, HS_VERSION_THREE);
  256. ret = helper_config_service(conf);
  257. tor_free(conf);
  258. tt_int_op(ret, OP_EQ, 0);
  259. /* It's in staging? */
  260. tt_int_op(get_hs_service_staging_list_size(), OP_EQ, 1);
  261. /* Load the keys for these. After that, the v3 service should be registered
  262. * in the global map. */
  263. hs_service_load_all_keys();
  264. tt_int_op(get_hs_service_map_size(), OP_EQ, 1);
  265. hs_service_t *s = get_first_service();
  266. tt_assert(s);
  267. /* Ok we have the service object. Validate few things. */
  268. tt_assert(!tor_mem_is_zero(s->onion_address, sizeof(s->onion_address)));
  269. tt_int_op(hs_address_is_valid(s->onion_address), OP_EQ, 1);
  270. tt_assert(!tor_mem_is_zero((char *) s->keys.identity_sk.seckey,
  271. ED25519_SECKEY_LEN));
  272. tt_assert(!tor_mem_is_zero((char *) s->keys.identity_pk.pubkey,
  273. ED25519_PUBKEY_LEN));
  274. /* Check onion address from identity key. */
  275. hs_build_address(&s->keys.identity_pk, s->config.version, addr);
  276. tt_int_op(hs_address_is_valid(addr), OP_EQ, 1);
  277. tt_str_op(addr, OP_EQ, s->onion_address);
  278. done:
  279. tor_free(hsdir_v2);
  280. tor_free(hsdir_v3);
  281. hs_free_all();
  282. }
  283. static void
  284. test_access_service(void *arg)
  285. {
  286. int ret;
  287. char *conf = NULL;
  288. char *hsdir_v3 = tor_strdup(get_fname("hs3"));
  289. hs_service_ht *global_map;
  290. hs_service_t *s = NULL;
  291. (void) arg;
  292. /* We'll register two services, a v2 and a v3, then we'll load keys and
  293. * validate that both are in a correct state. */
  294. hs_init();
  295. #define conf_fmt \
  296. "HiddenServiceDir %s\n" \
  297. "HiddenServiceVersion %d\n" \
  298. "HiddenServicePort 65535\n"
  299. /* v3 service. */
  300. tor_asprintf(&conf, conf_fmt, hsdir_v3, HS_VERSION_THREE);
  301. ret = helper_config_service(conf);
  302. tor_free(conf);
  303. tt_int_op(ret, OP_EQ, 0);
  304. /* It's in staging? */
  305. tt_int_op(get_hs_service_staging_list_size(), OP_EQ, 1);
  306. /* Load the keys for these. After that, the v3 service should be registered
  307. * in the global map. */
  308. hs_service_load_all_keys();
  309. tt_int_op(get_hs_service_map_size(), OP_EQ, 1);
  310. s = get_first_service();
  311. tt_assert(s);
  312. global_map = get_hs_service_map();
  313. tt_assert(global_map);
  314. /* From here, we'll try the service accessors. */
  315. hs_service_t *query = find_service(global_map, &s->keys.identity_pk);
  316. tt_assert(query);
  317. tt_mem_op(query, OP_EQ, s, sizeof(hs_service_t));
  318. /* Remove service, check if it actually works and then put it back. */
  319. remove_service(global_map, s);
  320. tt_int_op(get_hs_service_map_size(), OP_EQ, 0);
  321. query = find_service(global_map, &s->keys.identity_pk);
  322. tt_ptr_op(query, OP_EQ, NULL);
  323. /* Register back the service in the map. */
  324. ret = register_service(global_map, s);
  325. tt_int_op(ret, OP_EQ, 0);
  326. tt_int_op(get_hs_service_map_size(), OP_EQ, 1);
  327. /* Twice should fail. */
  328. ret = register_service(global_map, s);
  329. tt_int_op(ret, OP_EQ, -1);
  330. /* Remove service from map so we don't double free on cleanup. */
  331. remove_service(global_map, s);
  332. tt_int_op(get_hs_service_map_size(), OP_EQ, 0);
  333. query = find_service(global_map, &s->keys.identity_pk);
  334. tt_ptr_op(query, OP_EQ, NULL);
  335. /* Let's try to remove twice for fun. */
  336. setup_full_capture_of_logs(LOG_WARN);
  337. remove_service(global_map, s);
  338. expect_log_msg_containing("Could not find service in the global map");
  339. teardown_capture_of_logs();
  340. done:
  341. hs_service_free(s);
  342. tor_free(hsdir_v3);
  343. hs_free_all();
  344. }
  345. /** Test that we can create intro point objects, index them and find them */
  346. static void
  347. test_service_intro_point(void *arg)
  348. {
  349. hs_service_t *service = NULL;
  350. hs_service_intro_point_t *ip = NULL;
  351. (void) arg;
  352. /* Test simple creation of an object. */
  353. {
  354. time_t now = time(NULL);
  355. ip = helper_create_service_ip();
  356. tt_assert(ip);
  357. /* Make sure the authentication keypair is not zeroes. */
  358. tt_int_op(tor_mem_is_zero((const char *) &ip->auth_key_kp,
  359. sizeof(ed25519_keypair_t)), OP_EQ, 0);
  360. /* The introduce2_max MUST be in that range. */
  361. tt_u64_op(ip->introduce2_max, OP_GE,
  362. INTRO_POINT_MIN_LIFETIME_INTRODUCTIONS);
  363. tt_u64_op(ip->introduce2_max, OP_LE,
  364. INTRO_POINT_MAX_LIFETIME_INTRODUCTIONS);
  365. /* Time to expire MUST also be in that range. We subtract 500 seconds
  366. * because there could be a gap between setting now and the time taken in
  367. * service_intro_point_new. On ARM and other older CPUs, it can be
  368. * surprisingly slow... */
  369. tt_u64_op(ip->time_to_expire, OP_GE,
  370. now + INTRO_POINT_LIFETIME_MIN_SECONDS - 500);
  371. /* We add 500 seconds, because this time we're testing against the
  372. * maximum allowed time. */
  373. tt_u64_op(ip->time_to_expire, OP_LE,
  374. now + INTRO_POINT_LIFETIME_MAX_SECONDS + 500);
  375. tt_assert(ip->replay_cache);
  376. tt_assert(ip->base.link_specifiers);
  377. /* By default, this is NOT a legacy object. */
  378. tt_int_op(ip->base.is_only_legacy, OP_EQ, 0);
  379. }
  380. /* Test functions that uses a service intropoints map with that previously
  381. * created object (non legacy). */
  382. {
  383. ed25519_public_key_t garbage = { {0} };
  384. hs_service_intro_point_t *query;
  385. service = hs_service_new(get_options());
  386. tt_assert(service);
  387. service->desc_current = service_descriptor_new();
  388. tt_assert(service->desc_current);
  389. /* Add intropoint to descriptor map. */
  390. service_intro_point_add(service->desc_current->intro_points.map, ip);
  391. query = service_intro_point_find(service, &ip->auth_key_kp.pubkey);
  392. tt_mem_op(query, OP_EQ, ip, sizeof(hs_service_intro_point_t));
  393. query = service_intro_point_find(service, &garbage);
  394. tt_ptr_op(query, OP_EQ, NULL);
  395. /* While at it, can I find the descriptor with the intro point? */
  396. hs_service_descriptor_t *desc_lookup =
  397. service_desc_find_by_intro(service, ip);
  398. tt_mem_op(service->desc_current, OP_EQ, desc_lookup,
  399. sizeof(hs_service_descriptor_t));
  400. /* Remove object from service descriptor and make sure it is out. */
  401. service_intro_point_remove(service, ip);
  402. query = service_intro_point_find(service, &ip->auth_key_kp.pubkey);
  403. tt_ptr_op(query, OP_EQ, NULL);
  404. }
  405. done:
  406. /* If the test succeed, this object is no longer referenced in the service
  407. * so we can free it without use after free. Else, it might explode because
  408. * it's still in the service descriptor map. */
  409. service_intro_point_free(ip);
  410. hs_service_free(service);
  411. }
  412. static node_t mock_node;
  413. static const node_t *
  414. mock_node_get_by_id(const char *digest)
  415. {
  416. (void) digest;
  417. memset(mock_node.identity, 'A', DIGEST_LEN);
  418. /* Only return the matchin identity of As */
  419. if (!tor_memcmp(mock_node.identity, digest, DIGEST_LEN)) {
  420. return &mock_node;
  421. }
  422. return NULL;
  423. }
  424. static void
  425. test_helper_functions(void *arg)
  426. {
  427. int ret;
  428. hs_service_t *service = NULL;
  429. hs_service_intro_point_t *ip = NULL;
  430. hs_ident_circuit_t ident;
  431. (void) arg;
  432. MOCK(node_get_by_id, mock_node_get_by_id);
  433. hs_service_init();
  434. service = helper_create_service();
  435. ip = helper_create_service_ip();
  436. /* Immediately add the intro point to the service so the free service at the
  437. * end cleans it as well. */
  438. service_intro_point_add(service->desc_current->intro_points.map, ip);
  439. /* Setup the circuit identifier. */
  440. ed25519_pubkey_copy(&ident.intro_auth_pk, &ip->auth_key_kp.pubkey);
  441. ed25519_pubkey_copy(&ident.identity_pk, &service->keys.identity_pk);
  442. /* Testing get_objects_from_ident(). */
  443. {
  444. hs_service_t *s_lookup = NULL;
  445. hs_service_intro_point_t *ip_lookup = NULL;
  446. hs_service_descriptor_t *desc_lookup = NULL;
  447. get_objects_from_ident(&ident, &s_lookup, &ip_lookup, &desc_lookup);
  448. tt_mem_op(s_lookup, OP_EQ, service, sizeof(hs_service_t));
  449. tt_mem_op(ip_lookup, OP_EQ, ip, sizeof(hs_service_intro_point_t));
  450. tt_mem_op(desc_lookup, OP_EQ, service->desc_current,
  451. sizeof(hs_service_descriptor_t));
  452. /* Reset */
  453. s_lookup = NULL; ip_lookup = NULL; desc_lookup = NULL;
  454. /* NULL parameter should work. */
  455. get_objects_from_ident(&ident, NULL, &ip_lookup, &desc_lookup);
  456. tt_mem_op(ip_lookup, OP_EQ, ip, sizeof(hs_service_intro_point_t));
  457. tt_mem_op(desc_lookup, OP_EQ, service->desc_current,
  458. sizeof(hs_service_descriptor_t));
  459. /* Reset. */
  460. s_lookup = NULL; ip_lookup = NULL; desc_lookup = NULL;
  461. /* Break the ident and we should find nothing. */
  462. memset(&ident, 0, sizeof(ident));
  463. get_objects_from_ident(&ident, &s_lookup, &ip_lookup, &desc_lookup);
  464. tt_ptr_op(s_lookup, OP_EQ, NULL);
  465. tt_ptr_op(ip_lookup, OP_EQ, NULL);
  466. tt_ptr_op(desc_lookup, OP_EQ, NULL);
  467. }
  468. /* Testing get_node_from_intro_point() */
  469. {
  470. const node_t *node = get_node_from_intro_point(ip);
  471. tt_ptr_op(node, OP_EQ, &mock_node);
  472. SMARTLIST_FOREACH_BEGIN(ip->base.link_specifiers,
  473. hs_desc_link_specifier_t *, ls) {
  474. if (ls->type == LS_LEGACY_ID) {
  475. /* Change legacy id in link specifier which is not the mock node. */
  476. memset(ls->u.legacy_id, 'B', sizeof(ls->u.legacy_id));
  477. }
  478. } SMARTLIST_FOREACH_END(ls);
  479. node = get_node_from_intro_point(ip);
  480. tt_ptr_op(node, OP_EQ, NULL);
  481. }
  482. /* Testing can_service_launch_intro_circuit() */
  483. {
  484. time_t now = time(NULL);
  485. /* Put the start of the retry period back in time, we should be allowed.
  486. * to launch intro circuit. */
  487. service->state.num_intro_circ_launched = 2;
  488. service->state.intro_circ_retry_started_time =
  489. (now - INTRO_CIRC_RETRY_PERIOD - 1);
  490. ret = can_service_launch_intro_circuit(service, now);
  491. tt_int_op(ret, OP_EQ, 1);
  492. tt_u64_op(service->state.intro_circ_retry_started_time, OP_EQ, now);
  493. tt_u64_op(service->state.num_intro_circ_launched, OP_EQ, 0);
  494. /* Call it again, we should still be allowed because we are under
  495. * MAX_INTRO_CIRCS_PER_PERIOD which been set to 0 previously. */
  496. ret = can_service_launch_intro_circuit(service, now);
  497. tt_int_op(ret, OP_EQ, 1);
  498. tt_u64_op(service->state.intro_circ_retry_started_time, OP_EQ, now);
  499. tt_u64_op(service->state.num_intro_circ_launched, OP_EQ, 0);
  500. /* Too many intro circuit launched means we are not allowed. */
  501. service->state.num_intro_circ_launched = 20;
  502. ret = can_service_launch_intro_circuit(service, now);
  503. tt_int_op(ret, OP_EQ, 0);
  504. }
  505. /* Testing intro_point_should_expire(). */
  506. {
  507. time_t now = time(NULL);
  508. /* Just some basic test of the current state. */
  509. tt_u64_op(ip->introduce2_max, OP_GE,
  510. INTRO_POINT_MIN_LIFETIME_INTRODUCTIONS);
  511. tt_u64_op(ip->introduce2_max, OP_LE,
  512. INTRO_POINT_MAX_LIFETIME_INTRODUCTIONS);
  513. tt_u64_op(ip->time_to_expire, OP_GE,
  514. now + INTRO_POINT_LIFETIME_MIN_SECONDS);
  515. tt_u64_op(ip->time_to_expire, OP_LE,
  516. now + INTRO_POINT_LIFETIME_MAX_SECONDS);
  517. /* This newly created IP from above shouldn't expire now. */
  518. ret = intro_point_should_expire(ip, now);
  519. tt_int_op(ret, OP_EQ, 0);
  520. /* Maximum number of INTRODUCE2 cell reached, it should expire. */
  521. ip->introduce2_count = INTRO_POINT_MAX_LIFETIME_INTRODUCTIONS + 1;
  522. ret = intro_point_should_expire(ip, now);
  523. tt_int_op(ret, OP_EQ, 1);
  524. ip->introduce2_count = 0;
  525. /* It should expire if time to expire has been reached. */
  526. ip->time_to_expire = now - 1000;
  527. ret = intro_point_should_expire(ip, now);
  528. tt_int_op(ret, OP_EQ, 1);
  529. }
  530. done:
  531. /* This will free the service and all objects associated to it. */
  532. hs_service_free_all();
  533. UNMOCK(node_get_by_id);
  534. }
  535. /** Test that we do the right operations when an intro circuit opens */
  536. static void
  537. test_intro_circuit_opened(void *arg)
  538. {
  539. int flags = CIRCLAUNCH_NEED_UPTIME | CIRCLAUNCH_IS_INTERNAL;
  540. hs_service_t *service;
  541. origin_circuit_t *circ = NULL;
  542. (void) arg;
  543. hs_init();
  544. MOCK(circuit_mark_for_close_, mock_circuit_mark_for_close);
  545. MOCK(relay_send_command_from_edge_, mock_relay_send_command_from_edge);
  546. circ = helper_create_origin_circuit(CIRCUIT_PURPOSE_S_ESTABLISH_INTRO,
  547. flags);
  548. /* No service associated with this circuit. */
  549. setup_full_capture_of_logs(LOG_WARN);
  550. hs_service_circuit_has_opened(circ);
  551. expect_log_msg_containing("Unknown service identity key");
  552. teardown_capture_of_logs();
  553. /* Set a service for this circuit. */
  554. {
  555. service = helper_create_service();
  556. ed25519_pubkey_copy(&circ->hs_ident->identity_pk,
  557. &service->keys.identity_pk);
  558. /* No intro point associated with this circuit. */
  559. setup_full_capture_of_logs(LOG_WARN);
  560. hs_service_circuit_has_opened(circ);
  561. expect_log_msg_containing("Unknown introduction point auth key");
  562. teardown_capture_of_logs();
  563. }
  564. /* Set an IP object now for this circuit. */
  565. {
  566. hs_service_intro_point_t *ip = helper_create_service_ip();
  567. service_intro_point_add(service->desc_current->intro_points.map, ip);
  568. /* Update ident to contain the intro point auth key. */
  569. ed25519_pubkey_copy(&circ->hs_ident->intro_auth_pk,
  570. &ip->auth_key_kp.pubkey);
  571. }
  572. /* This one should go all the way. */
  573. setup_full_capture_of_logs(LOG_INFO);
  574. hs_service_circuit_has_opened(circ);
  575. expect_log_msg_containing("Introduction circuit 0 established for service");
  576. teardown_capture_of_logs();
  577. done:
  578. circuit_free_(TO_CIRCUIT(circ));
  579. hs_free_all();
  580. UNMOCK(circuit_mark_for_close_);
  581. UNMOCK(relay_send_command_from_edge_);
  582. }
  583. /** Test the operations we do on a circuit after we learn that we successfully
  584. * established an intro point on it */
  585. static void
  586. test_intro_established(void *arg)
  587. {
  588. int ret;
  589. int flags = CIRCLAUNCH_NEED_UPTIME | CIRCLAUNCH_IS_INTERNAL;
  590. uint8_t payload[RELAY_PAYLOAD_SIZE] = {0};
  591. origin_circuit_t *circ = NULL;
  592. hs_service_t *service;
  593. hs_service_intro_point_t *ip = NULL;
  594. (void) arg;
  595. hs_init();
  596. MOCK(circuit_mark_for_close_, mock_circuit_mark_for_close);
  597. circ = helper_create_origin_circuit(CIRCUIT_PURPOSE_S_ESTABLISH_INTRO,
  598. flags);
  599. tt_assert(circ);
  600. /* Test a wrong purpose. */
  601. TO_CIRCUIT(circ)->purpose = CIRCUIT_PURPOSE_S_INTRO;
  602. setup_full_capture_of_logs(LOG_WARN);
  603. ret = hs_service_receive_intro_established(circ, payload, sizeof(payload));
  604. tt_int_op(ret, OP_EQ, -1);
  605. expect_log_msg_containing("Received an INTRO_ESTABLISHED cell on a "
  606. "non introduction circuit of purpose");
  607. teardown_capture_of_logs();
  608. /* Back to normal. */
  609. TO_CIRCUIT(circ)->purpose = CIRCUIT_PURPOSE_S_ESTABLISH_INTRO;
  610. /* No service associated to it. */
  611. setup_full_capture_of_logs(LOG_WARN);
  612. ret = hs_service_receive_intro_established(circ, payload, sizeof(payload));
  613. tt_int_op(ret, OP_EQ, -1);
  614. expect_log_msg_containing("Unknown service identity key");
  615. teardown_capture_of_logs();
  616. /* Set a service for this circuit. */
  617. service = helper_create_service();
  618. ed25519_pubkey_copy(&circ->hs_ident->identity_pk,
  619. &service->keys.identity_pk);
  620. /* No introduction point associated to it. */
  621. setup_full_capture_of_logs(LOG_WARN);
  622. ret = hs_service_receive_intro_established(circ, payload, sizeof(payload));
  623. tt_int_op(ret, OP_EQ, -1);
  624. expect_log_msg_containing("Introduction circuit established without an "
  625. "intro point object on circuit");
  626. teardown_capture_of_logs();
  627. /* Set an IP object now for this circuit. */
  628. {
  629. ip = helper_create_service_ip();
  630. service_intro_point_add(service->desc_current->intro_points.map, ip);
  631. /* Update ident to contain the intro point auth key. */
  632. ed25519_pubkey_copy(&circ->hs_ident->intro_auth_pk,
  633. &ip->auth_key_kp.pubkey);
  634. }
  635. /* Send an empty payload. INTRO_ESTABLISHED cells are basically zeroes. */
  636. ret = hs_service_receive_intro_established(circ, payload, sizeof(payload));
  637. tt_int_op(ret, OP_EQ, 0);
  638. tt_u64_op(ip->circuit_established, OP_EQ, 1);
  639. tt_int_op(TO_CIRCUIT(circ)->purpose, OP_EQ, CIRCUIT_PURPOSE_S_INTRO);
  640. done:
  641. if (circ)
  642. circuit_free_(TO_CIRCUIT(circ));
  643. hs_free_all();
  644. UNMOCK(circuit_mark_for_close_);
  645. }
  646. /** Check the operations we do on a rendezvous circuit after we learn it's
  647. * open */
  648. static void
  649. test_rdv_circuit_opened(void *arg)
  650. {
  651. int flags = CIRCLAUNCH_NEED_UPTIME | CIRCLAUNCH_IS_INTERNAL;
  652. origin_circuit_t *circ = NULL;
  653. hs_service_t *service;
  654. (void) arg;
  655. hs_init();
  656. MOCK(circuit_mark_for_close_, mock_circuit_mark_for_close);
  657. MOCK(relay_send_command_from_edge_, mock_relay_send_command_from_edge);
  658. circ = helper_create_origin_circuit(CIRCUIT_PURPOSE_S_CONNECT_REND, flags);
  659. crypto_rand((char *) circ->hs_ident->rendezvous_cookie, REND_COOKIE_LEN);
  660. crypto_rand((char *) circ->hs_ident->rendezvous_handshake_info,
  661. sizeof(circ->hs_ident->rendezvous_handshake_info));
  662. /* No service associated with this circuit. */
  663. setup_full_capture_of_logs(LOG_WARN);
  664. hs_service_circuit_has_opened(circ);
  665. expect_log_msg_containing("Unknown service identity key");
  666. teardown_capture_of_logs();
  667. /* This should be set to a non zero timestamp. */
  668. tt_u64_op(TO_CIRCUIT(circ)->timestamp_dirty, OP_NE, 0);
  669. /* Set a service for this circuit. */
  670. service = helper_create_service();
  671. ed25519_pubkey_copy(&circ->hs_ident->identity_pk,
  672. &service->keys.identity_pk);
  673. /* Should be all good. */
  674. hs_service_circuit_has_opened(circ);
  675. tt_int_op(TO_CIRCUIT(circ)->purpose, OP_EQ, CIRCUIT_PURPOSE_S_REND_JOINED);
  676. done:
  677. circuit_free_(TO_CIRCUIT(circ));
  678. hs_free_all();
  679. UNMOCK(circuit_mark_for_close_);
  680. UNMOCK(relay_send_command_from_edge_);
  681. }
  682. static void
  683. mock_assert_circuit_ok(const circuit_t *c)
  684. {
  685. (void) c;
  686. return;
  687. }
  688. /** Test for the general mechanism for closing intro circs.
  689. * Also a way to identify that #23603 has been fixed. */
  690. static void
  691. test_closing_intro_circs(void *arg)
  692. {
  693. hs_service_t *service = NULL;
  694. hs_service_intro_point_t *ip = NULL, *entry = NULL;
  695. origin_circuit_t *intro_circ = NULL, *tmp_circ;
  696. int flags = CIRCLAUNCH_NEED_UPTIME | CIRCLAUNCH_IS_INTERNAL;
  697. (void) arg;
  698. MOCK(assert_circuit_ok, mock_assert_circuit_ok);
  699. hs_init();
  700. /* Initialize service */
  701. service = helper_create_service();
  702. /* Initialize intro point */
  703. ip = helper_create_service_ip();
  704. tt_assert(ip);
  705. service_intro_point_add(service->desc_current->intro_points.map, ip);
  706. /* Initialize intro circuit */
  707. intro_circ = origin_circuit_init(CIRCUIT_PURPOSE_S_ESTABLISH_INTRO, flags);
  708. intro_circ->hs_ident = hs_ident_circuit_new(&service->keys.identity_pk,
  709. HS_IDENT_CIRCUIT_INTRO);
  710. /* Register circuit in the circuitmap . */
  711. hs_circuitmap_register_intro_circ_v3_service_side(intro_circ,
  712. &ip->auth_key_kp.pubkey);
  713. tmp_circ =
  714. hs_circuitmap_get_intro_circ_v3_service_side(&ip->auth_key_kp.pubkey);
  715. tt_ptr_op(tmp_circ, OP_EQ, intro_circ);
  716. /* Pretend that intro point has failed too much */
  717. ip->circuit_retries = MAX_INTRO_POINT_CIRCUIT_RETRIES+1;
  718. /* Now pretend we are freeing this intro circuit. We want to see that our
  719. * destructor is not gonna kill our intro point structure since that's the
  720. * job of the cleanup routine. */
  721. circuit_free_(TO_CIRCUIT(intro_circ));
  722. intro_circ = NULL;
  723. entry = service_intro_point_find(service, &ip->auth_key_kp.pubkey);
  724. tt_assert(entry);
  725. /* The free should also remove the circuit from the circuitmap. */
  726. tmp_circ =
  727. hs_circuitmap_get_intro_circ_v3_service_side(&ip->auth_key_kp.pubkey);
  728. tt_assert(!tmp_circ);
  729. /* Now pretend that a new intro point circ was launched and opened. Check
  730. * that the intro point will be established correctly. */
  731. intro_circ = origin_circuit_init(CIRCUIT_PURPOSE_S_ESTABLISH_INTRO, flags);
  732. intro_circ->hs_ident = hs_ident_circuit_new(&service->keys.identity_pk,
  733. HS_IDENT_CIRCUIT_INTRO);
  734. ed25519_pubkey_copy(&intro_circ->hs_ident->intro_auth_pk,
  735. &ip->auth_key_kp.pubkey);
  736. /* Register circuit in the circuitmap . */
  737. hs_circuitmap_register_intro_circ_v3_service_side(intro_circ,
  738. &ip->auth_key_kp.pubkey);
  739. tmp_circ =
  740. hs_circuitmap_get_intro_circ_v3_service_side(&ip->auth_key_kp.pubkey);
  741. tt_ptr_op(tmp_circ, OP_EQ, intro_circ);
  742. tt_int_op(TO_CIRCUIT(intro_circ)->marked_for_close, OP_EQ, 0);
  743. circuit_mark_for_close(TO_CIRCUIT(intro_circ), END_CIRC_REASON_INTERNAL);
  744. tt_int_op(TO_CIRCUIT(intro_circ)->marked_for_close, OP_NE, 0);
  745. /* At this point, we should not be able to find it in the circuitmap. */
  746. tmp_circ =
  747. hs_circuitmap_get_intro_circ_v3_service_side(&ip->auth_key_kp.pubkey);
  748. tt_assert(!tmp_circ);
  749. done:
  750. if (intro_circ) {
  751. circuit_free_(TO_CIRCUIT(intro_circ));
  752. }
  753. /* Frees the service object. */
  754. hs_free_all();
  755. UNMOCK(assert_circuit_ok);
  756. }
  757. /** Test sending and receiving introduce2 cells */
  758. static void
  759. test_introduce2(void *arg)
  760. {
  761. int ret;
  762. int flags = CIRCLAUNCH_NEED_UPTIME | CIRCLAUNCH_IS_INTERNAL;
  763. uint8_t payload[RELAY_PAYLOAD_SIZE] = {0};
  764. origin_circuit_t *circ = NULL;
  765. hs_service_t *service;
  766. hs_service_intro_point_t *ip = NULL;
  767. (void) arg;
  768. hs_init();
  769. MOCK(circuit_mark_for_close_, mock_circuit_mark_for_close);
  770. MOCK(get_or_state,
  771. get_or_state_replacement);
  772. dummy_state = tor_malloc_zero(sizeof(or_state_t));
  773. circ = helper_create_origin_circuit(CIRCUIT_PURPOSE_S_INTRO, flags);
  774. tt_assert(circ);
  775. /* Test a wrong purpose. */
  776. TO_CIRCUIT(circ)->purpose = CIRCUIT_PURPOSE_S_ESTABLISH_INTRO;
  777. setup_full_capture_of_logs(LOG_WARN);
  778. ret = hs_service_receive_introduce2(circ, payload, sizeof(payload));
  779. tt_int_op(ret, OP_EQ, -1);
  780. expect_log_msg_containing("Received an INTRODUCE2 cell on a "
  781. "non introduction circuit of purpose");
  782. teardown_capture_of_logs();
  783. /* Back to normal. */
  784. TO_CIRCUIT(circ)->purpose = CIRCUIT_PURPOSE_S_INTRO;
  785. /* No service associated to it. */
  786. setup_full_capture_of_logs(LOG_WARN);
  787. ret = hs_service_receive_introduce2(circ, payload, sizeof(payload));
  788. tt_int_op(ret, OP_EQ, -1);
  789. expect_log_msg_containing("Unknown service identity key");
  790. teardown_capture_of_logs();
  791. /* Set a service for this circuit. */
  792. service = helper_create_service();
  793. ed25519_pubkey_copy(&circ->hs_ident->identity_pk,
  794. &service->keys.identity_pk);
  795. /* No introduction point associated to it. */
  796. setup_full_capture_of_logs(LOG_WARN);
  797. ret = hs_service_receive_introduce2(circ, payload, sizeof(payload));
  798. tt_int_op(ret, OP_EQ, -1);
  799. expect_log_msg_containing("Unknown introduction auth key when handling "
  800. "an INTRODUCE2 cell on circuit");
  801. teardown_capture_of_logs();
  802. /* Set an IP object now for this circuit. */
  803. {
  804. ip = helper_create_service_ip();
  805. service_intro_point_add(service->desc_current->intro_points.map, ip);
  806. /* Update ident to contain the intro point auth key. */
  807. ed25519_pubkey_copy(&circ->hs_ident->intro_auth_pk,
  808. &ip->auth_key_kp.pubkey);
  809. }
  810. /* This will fail because receiving an INTRODUCE2 cell implies a valid cell
  811. * and then launching circuits so let's not do that and instead test that
  812. * behaviour differently. */
  813. ret = hs_service_receive_introduce2(circ, payload, sizeof(payload));
  814. tt_int_op(ret, OP_EQ, -1);
  815. tt_u64_op(ip->introduce2_count, OP_EQ, 0);
  816. done:
  817. or_state_free(dummy_state);
  818. dummy_state = NULL;
  819. if (circ)
  820. circuit_free_(TO_CIRCUIT(circ));
  821. hs_free_all();
  822. UNMOCK(circuit_mark_for_close_);
  823. }
  824. /** Test basic hidden service housekeeping operations (maintaining intro
  825. * points, etc) */
  826. static void
  827. test_service_event(void *arg)
  828. {
  829. int flags = CIRCLAUNCH_NEED_UPTIME | CIRCLAUNCH_IS_INTERNAL;
  830. time_t now = time(NULL);
  831. hs_service_t *service;
  832. origin_circuit_t *circ = NULL;
  833. (void) arg;
  834. hs_init();
  835. MOCK(circuit_mark_for_close_, mock_circuit_mark_for_close);
  836. circ = helper_create_origin_circuit(CIRCUIT_PURPOSE_S_INTRO, flags);
  837. /* Set a service for this circuit. */
  838. service = helper_create_service();
  839. ed25519_pubkey_copy(&circ->hs_ident->identity_pk,
  840. &service->keys.identity_pk);
  841. /* Currently this consists of cleaning invalid intro points. So adding IPs
  842. * here that should get cleaned up. */
  843. {
  844. hs_service_intro_point_t *ip = helper_create_service_ip();
  845. service_intro_point_add(service->desc_current->intro_points.map, ip);
  846. /* This run will remove the IP because we have no circuits nor node_t
  847. * associated with it. */
  848. run_housekeeping_event(now);
  849. tt_int_op(digest256map_size(service->desc_current->intro_points.map),
  850. OP_EQ, 0);
  851. /* We'll trigger a removal because we've reached our maximum amount of
  852. * times we should retry a circuit. For this, we need to have a node_t
  853. * that matches the identity of this IP. */
  854. routerinfo_t ri;
  855. memset(&ri, 0, sizeof(ri));
  856. ip = helper_create_service_ip();
  857. service_intro_point_add(service->desc_current->intro_points.map, ip);
  858. memset(ri.cache_info.identity_digest, 'A', DIGEST_LEN);
  859. /* This triggers a node_t creation. */
  860. tt_assert(nodelist_set_routerinfo(&ri, NULL));
  861. ip->circuit_retries = MAX_INTRO_POINT_CIRCUIT_RETRIES + 1;
  862. run_housekeeping_event(now);
  863. tt_int_op(digest256map_size(service->desc_current->intro_points.map),
  864. OP_EQ, 0);
  865. /* No removal but no circuit so this means the IP object will stay in the
  866. * descriptor map so we can retry it. */
  867. ip = helper_create_service_ip();
  868. service_intro_point_add(service->desc_current->intro_points.map, ip);
  869. ip->circuit_established = 1; /* We'll test that, it MUST be 0 after. */
  870. run_housekeeping_event(now);
  871. tt_int_op(digest256map_size(service->desc_current->intro_points.map),
  872. OP_EQ, 1);
  873. /* Remove the IP object at once for the next test. */
  874. ip->circuit_retries = MAX_INTRO_POINT_CIRCUIT_RETRIES + 1;
  875. run_housekeeping_event(now);
  876. tt_int_op(digest256map_size(service->desc_current->intro_points.map),
  877. OP_EQ, 0);
  878. /* Now, we'll create an IP with a registered circuit. The IP object
  879. * shouldn't go away. */
  880. ip = helper_create_service_ip();
  881. service_intro_point_add(service->desc_current->intro_points.map, ip);
  882. ed25519_pubkey_copy(&circ->hs_ident->intro_auth_pk,
  883. &ip->auth_key_kp.pubkey);
  884. hs_circuitmap_register_intro_circ_v3_service_side(
  885. circ, &ip->auth_key_kp.pubkey);
  886. run_housekeeping_event(now);
  887. tt_int_op(digest256map_size(service->desc_current->intro_points.map),
  888. OP_EQ, 1);
  889. /* We'll mangle the IP object to expire. */
  890. ip->time_to_expire = now;
  891. run_housekeeping_event(now);
  892. tt_int_op(digest256map_size(service->desc_current->intro_points.map),
  893. OP_EQ, 0);
  894. }
  895. done:
  896. hs_circuitmap_remove_circuit(TO_CIRCUIT(circ));
  897. circuit_free_(TO_CIRCUIT(circ));
  898. hs_free_all();
  899. UNMOCK(circuit_mark_for_close_);
  900. }
  901. /** Test that we rotate descriptors correctly. */
  902. static void
  903. test_rotate_descriptors(void *arg)
  904. {
  905. int ret;
  906. time_t next_rotation_time, now;
  907. hs_service_t *service;
  908. hs_service_descriptor_t *desc_next;
  909. (void) arg;
  910. dummy_state = tor_malloc_zero(sizeof(or_state_t));
  911. hs_init();
  912. MOCK(get_or_state, get_or_state_replacement);
  913. MOCK(circuit_mark_for_close_, mock_circuit_mark_for_close);
  914. MOCK(networkstatus_get_live_consensus,
  915. mock_networkstatus_get_live_consensus);
  916. /* Descriptor rotation happens with a consensus with a new SRV. */
  917. ret = parse_rfc1123_time("Sat, 26 Oct 1985 13:00:00 UTC",
  918. &mock_ns.valid_after);
  919. tt_int_op(ret, OP_EQ, 0);
  920. ret = parse_rfc1123_time("Sat, 26 Oct 1985 14:00:00 UTC",
  921. &mock_ns.fresh_until);
  922. tt_int_op(ret, OP_EQ, 0);
  923. voting_schedule_recalculate_timing(get_options(), mock_ns.valid_after);
  924. update_approx_time(mock_ns.valid_after+1);
  925. now = mock_ns.valid_after+1;
  926. /* Create a service with a default descriptor and state. It's added to the
  927. * global map. */
  928. service = helper_create_service();
  929. service_descriptor_free(service->desc_current);
  930. service->desc_current = NULL;
  931. /* This triggers a build for both descriptors. The time now is only used in
  932. * the descriptor certificate which is important to be now else the decoding
  933. * will complain that the cert has expired if we use valid_after. */
  934. build_all_descriptors(now);
  935. tt_assert(service->desc_current);
  936. tt_assert(service->desc_next);
  937. /* Tweak our service next rotation time so we can use a custom time. */
  938. service->state.next_rotation_time = next_rotation_time =
  939. mock_ns.valid_after + (11 * 60 * 60);
  940. /* Nothing should happen, we are not at a new SRV. Our next rotation time
  941. * should be untouched. */
  942. rotate_all_descriptors(mock_ns.valid_after);
  943. tt_u64_op(service->state.next_rotation_time, OP_EQ, next_rotation_time);
  944. tt_assert(service->desc_current);
  945. tt_assert(service->desc_next);
  946. tt_u64_op(service->desc_current->time_period_num, OP_EQ,
  947. hs_get_previous_time_period_num(0));
  948. tt_u64_op(service->desc_next->time_period_num, OP_EQ,
  949. hs_get_time_period_num(0));
  950. /* Keep a reference so we can compare it after rotation to the current. */
  951. desc_next = service->desc_next;
  952. /* Going right after a new SRV. */
  953. ret = parse_rfc1123_time("Sat, 27 Oct 1985 01:00:00 UTC",
  954. &mock_ns.valid_after);
  955. tt_int_op(ret, OP_EQ, 0);
  956. ret = parse_rfc1123_time("Sat, 27 Oct 1985 02:00:00 UTC",
  957. &mock_ns.fresh_until);
  958. tt_int_op(ret, OP_EQ, 0);
  959. voting_schedule_recalculate_timing(get_options(), mock_ns.valid_after);
  960. update_approx_time(mock_ns.valid_after+1);
  961. now = mock_ns.valid_after+1;
  962. /* Note down what to expect for the next rotation time which is 01:00 + 23h
  963. * meaning 00:00:00. */
  964. next_rotation_time = mock_ns.valid_after + (23 * 60 * 60);
  965. /* We should have our next rotation time modified, our current descriptor
  966. * cleaned up and the next descriptor becoming the current. */
  967. rotate_all_descriptors(mock_ns.valid_after);
  968. tt_u64_op(service->state.next_rotation_time, OP_EQ, next_rotation_time);
  969. tt_mem_op(service->desc_current, OP_EQ, desc_next, sizeof(*desc_next));
  970. tt_assert(service->desc_next == NULL);
  971. /* A second time should do nothing. */
  972. rotate_all_descriptors(mock_ns.valid_after);
  973. tt_u64_op(service->state.next_rotation_time, OP_EQ, next_rotation_time);
  974. tt_mem_op(service->desc_current, OP_EQ, desc_next, sizeof(*desc_next));
  975. tt_assert(service->desc_next == NULL);
  976. build_all_descriptors(now);
  977. tt_mem_op(service->desc_current, OP_EQ, desc_next, sizeof(*desc_next));
  978. tt_u64_op(service->desc_current->time_period_num, OP_EQ,
  979. hs_get_time_period_num(0));
  980. tt_u64_op(service->desc_next->time_period_num, OP_EQ,
  981. hs_get_next_time_period_num(0));
  982. tt_assert(service->desc_next);
  983. done:
  984. hs_free_all();
  985. UNMOCK(get_or_state);
  986. UNMOCK(circuit_mark_for_close_);
  987. UNMOCK(networkstatus_get_live_consensus);
  988. }
  989. /** Test building descriptors: picking intro points, setting up their link
  990. * specifiers, etc. */
  991. static void
  992. test_build_update_descriptors(void *arg)
  993. {
  994. int ret;
  995. time_t now = time(NULL);
  996. node_t *node;
  997. hs_service_t *service;
  998. hs_service_intro_point_t *ip_cur, *ip_next;
  999. routerinfo_t ri;
  1000. (void) arg;
  1001. hs_init();
  1002. MOCK(get_or_state,
  1003. get_or_state_replacement);
  1004. MOCK(networkstatus_get_live_consensus,
  1005. mock_networkstatus_get_live_consensus);
  1006. dummy_state = tor_malloc_zero(sizeof(or_state_t));
  1007. ret = parse_rfc1123_time("Sat, 26 Oct 1985 03:00:00 UTC",
  1008. &mock_ns.valid_after);
  1009. tt_int_op(ret, OP_EQ, 0);
  1010. ret = parse_rfc1123_time("Sat, 26 Oct 1985 04:00:00 UTC",
  1011. &mock_ns.fresh_until);
  1012. tt_int_op(ret, OP_EQ, 0);
  1013. voting_schedule_recalculate_timing(get_options(), mock_ns.valid_after);
  1014. update_approx_time(mock_ns.valid_after+1);
  1015. now = mock_ns.valid_after+1;
  1016. /* Create a service without a current descriptor to trigger a build. */
  1017. service = helper_create_service();
  1018. tt_assert(service);
  1019. /* Unfortunately, the helper creates a dummy descriptor so get rid of it. */
  1020. service_descriptor_free(service->desc_current);
  1021. service->desc_current = NULL;
  1022. /* We have a fresh service so this should trigger a build for both
  1023. * descriptors for specific time period that we'll test. */
  1024. build_all_descriptors(now);
  1025. /* Check *current* descriptor. */
  1026. tt_assert(service->desc_current);
  1027. tt_assert(service->desc_current->desc);
  1028. tt_assert(service->desc_current->intro_points.map);
  1029. /* The current time period is the one expected when starting at 03:00. */
  1030. tt_u64_op(service->desc_current->time_period_num, OP_EQ,
  1031. hs_get_time_period_num(0));
  1032. /* This should be untouched, the update descriptor process changes it. */
  1033. tt_u64_op(service->desc_current->next_upload_time, OP_EQ, 0);
  1034. /* Check *next* descriptor. */
  1035. tt_assert(service->desc_next);
  1036. tt_assert(service->desc_next->desc);
  1037. tt_assert(service->desc_next->intro_points.map);
  1038. tt_assert(service->desc_current != service->desc_next);
  1039. tt_u64_op(service->desc_next->time_period_num, OP_EQ,
  1040. hs_get_next_time_period_num(0));
  1041. /* This should be untouched, the update descriptor process changes it. */
  1042. tt_u64_op(service->desc_next->next_upload_time, OP_EQ, 0);
  1043. /* Time to test the update of those descriptors. At first, we have no node
  1044. * in the routerlist so this will find NO suitable node for the IPs. */
  1045. setup_full_capture_of_logs(LOG_INFO);
  1046. update_all_descriptors(now);
  1047. expect_log_msg_containing("Unable to find a suitable node to be an "
  1048. "introduction point for service");
  1049. teardown_capture_of_logs();
  1050. tt_int_op(digest256map_size(service->desc_current->intro_points.map),
  1051. OP_EQ, 0);
  1052. tt_int_op(digest256map_size(service->desc_next->intro_points.map),
  1053. OP_EQ, 0);
  1054. /* Now, we'll setup a node_t. */
  1055. {
  1056. tor_addr_t ipv4_addr;
  1057. curve25519_secret_key_t curve25519_secret_key;
  1058. memset(&ri, 0, sizeof(routerinfo_t));
  1059. tor_addr_parse(&ipv4_addr, "127.0.0.1");
  1060. ri.addr = tor_addr_to_ipv4h(&ipv4_addr);
  1061. ri.or_port = 1337;
  1062. ri.purpose = ROUTER_PURPOSE_GENERAL;
  1063. /* Ugly yes but we never free the "ri" object so this just makes things
  1064. * easier. */
  1065. ri.protocol_list = (char *) "HSDir=1-2 LinkAuth=3";
  1066. summarize_protover_flags(&ri.pv, ri.protocol_list, NULL);
  1067. ret = curve25519_secret_key_generate(&curve25519_secret_key, 0);
  1068. tt_int_op(ret, OP_EQ, 0);
  1069. ri.onion_curve25519_pkey =
  1070. tor_malloc_zero(sizeof(curve25519_public_key_t));
  1071. ri.onion_pkey = crypto_pk_new();
  1072. curve25519_public_key_generate(ri.onion_curve25519_pkey,
  1073. &curve25519_secret_key);
  1074. memset(ri.cache_info.identity_digest, 'A', DIGEST_LEN);
  1075. /* Setup ed25519 identity */
  1076. ed25519_keypair_t kp1;
  1077. ed25519_keypair_generate(&kp1, 0);
  1078. ri.cache_info.signing_key_cert = tor_malloc_zero(sizeof(tor_cert_t));
  1079. tt_assert(ri.cache_info.signing_key_cert);
  1080. ed25519_pubkey_copy(&ri.cache_info.signing_key_cert->signing_key,
  1081. &kp1.pubkey);
  1082. nodelist_set_routerinfo(&ri, NULL);
  1083. node = node_get_mutable_by_id(ri.cache_info.identity_digest);
  1084. tt_assert(node);
  1085. node->is_running = node->is_valid = node->is_fast = node->is_stable = 1;
  1086. }
  1087. /* We have to set this, or the lack of microdescriptors for these
  1088. * nodes will make them unusable. */
  1089. get_options_mutable()->UseMicrodescriptors = 0;
  1090. /* We expect to pick only one intro point from the node above. */
  1091. setup_full_capture_of_logs(LOG_INFO);
  1092. update_all_descriptors(now);
  1093. tor_free(node->ri->onion_curve25519_pkey); /* Avoid memleak. */
  1094. tor_free(node->ri->cache_info.signing_key_cert);
  1095. crypto_pk_free(node->ri->onion_pkey);
  1096. expect_log_msg_containing("just picked 1 intro points and wanted 3 for next "
  1097. "descriptor. It currently has 0 intro points. "
  1098. "Launching ESTABLISH_INTRO circuit shortly.");
  1099. teardown_capture_of_logs();
  1100. tt_int_op(digest256map_size(service->desc_current->intro_points.map),
  1101. OP_EQ, 1);
  1102. tt_int_op(digest256map_size(service->desc_next->intro_points.map),
  1103. OP_EQ, 1);
  1104. /* Get the IP object. Because we don't have the auth key of the IP, we can't
  1105. * query it so get the first element in the map. */
  1106. {
  1107. void *obj = NULL;
  1108. const uint8_t *key;
  1109. digest256map_iter_t *iter =
  1110. digest256map_iter_init(service->desc_current->intro_points.map);
  1111. digest256map_iter_get(iter, &key, &obj);
  1112. tt_assert(obj);
  1113. ip_cur = obj;
  1114. /* Get also the IP from the next descriptor. We'll make sure it's not the
  1115. * same object as in the current descriptor. */
  1116. iter = digest256map_iter_init(service->desc_next->intro_points.map);
  1117. digest256map_iter_get(iter, &key, &obj);
  1118. tt_assert(obj);
  1119. ip_next = obj;
  1120. }
  1121. tt_mem_op(ip_cur, OP_NE, ip_next, sizeof(hs_desc_intro_point_t));
  1122. /* We won't test the service IP object because there is a specific test
  1123. * already for this but we'll make sure that the state is coherent.*/
  1124. /* Three link specifiers are mandatoy so make sure we do have them. */
  1125. tt_int_op(smartlist_len(ip_cur->base.link_specifiers), OP_EQ, 3);
  1126. /* Make sure we have a valid encryption keypair generated when we pick an
  1127. * intro point in the update process. */
  1128. tt_assert(!tor_mem_is_zero((char *) ip_cur->enc_key_kp.seckey.secret_key,
  1129. CURVE25519_SECKEY_LEN));
  1130. tt_assert(!tor_mem_is_zero((char *) ip_cur->enc_key_kp.pubkey.public_key,
  1131. CURVE25519_PUBKEY_LEN));
  1132. tt_u64_op(ip_cur->time_to_expire, OP_GE, now +
  1133. INTRO_POINT_LIFETIME_MIN_SECONDS);
  1134. tt_u64_op(ip_cur->time_to_expire, OP_LE, now +
  1135. INTRO_POINT_LIFETIME_MAX_SECONDS);
  1136. /* Now, we will try to set up a service after a new time period has started
  1137. * and see if it behaves as expected. */
  1138. ret = parse_rfc1123_time("Sat, 26 Oct 1985 13:00:00 UTC",
  1139. &mock_ns.valid_after);
  1140. tt_int_op(ret, OP_EQ, 0);
  1141. ret = parse_rfc1123_time("Sat, 26 Oct 1985 14:00:00 UTC",
  1142. &mock_ns.fresh_until);
  1143. tt_int_op(ret, OP_EQ, 0);
  1144. update_approx_time(mock_ns.valid_after+1);
  1145. now = mock_ns.valid_after+1;
  1146. /* Create a service without a current descriptor to trigger a build. */
  1147. service = helper_create_service();
  1148. tt_assert(service);
  1149. /* Unfortunately, the helper creates a dummy descriptor so get rid of it. */
  1150. service_descriptor_free(service->desc_current);
  1151. service->desc_current = NULL;
  1152. /* We have a fresh service so this should trigger a build for both
  1153. * descriptors for specific time period that we'll test. */
  1154. build_all_descriptors(now);
  1155. /* Check *current* descriptor. */
  1156. tt_assert(service->desc_current);
  1157. tt_assert(service->desc_current->desc);
  1158. tt_assert(service->desc_current->intro_points.map);
  1159. /* This should be for the previous time period. */
  1160. tt_u64_op(service->desc_current->time_period_num, OP_EQ,
  1161. hs_get_previous_time_period_num(0));
  1162. /* This should be untouched, the update descriptor process changes it. */
  1163. tt_u64_op(service->desc_current->next_upload_time, OP_EQ, 0);
  1164. /* Check *next* descriptor. */
  1165. tt_assert(service->desc_next);
  1166. tt_assert(service->desc_next->desc);
  1167. tt_assert(service->desc_next->intro_points.map);
  1168. tt_assert(service->desc_current != service->desc_next);
  1169. tt_u64_op(service->desc_next->time_period_num, OP_EQ,
  1170. hs_get_time_period_num(0));
  1171. /* This should be untouched, the update descriptor process changes it. */
  1172. tt_u64_op(service->desc_next->next_upload_time, OP_EQ, 0);
  1173. /* Let's remove the next descriptor to simulate a rotation. */
  1174. service_descriptor_free(service->desc_next);
  1175. service->desc_next = NULL;
  1176. build_all_descriptors(now);
  1177. /* Check *next* descriptor. */
  1178. tt_assert(service->desc_next);
  1179. tt_assert(service->desc_next->desc);
  1180. tt_assert(service->desc_next->intro_points.map);
  1181. tt_assert(service->desc_current != service->desc_next);
  1182. tt_u64_op(service->desc_next->time_period_num, OP_EQ,
  1183. hs_get_next_time_period_num(0));
  1184. /* This should be untouched, the update descriptor process changes it. */
  1185. tt_u64_op(service->desc_next->next_upload_time, OP_EQ, 0);
  1186. done:
  1187. hs_free_all();
  1188. nodelist_free_all();
  1189. }
  1190. static void
  1191. test_upload_descriptors(void *arg)
  1192. {
  1193. int ret;
  1194. time_t now;
  1195. hs_service_t *service;
  1196. (void) arg;
  1197. hs_init();
  1198. MOCK(get_or_state,
  1199. get_or_state_replacement);
  1200. MOCK(networkstatus_get_live_consensus,
  1201. mock_networkstatus_get_live_consensus);
  1202. dummy_state = tor_malloc_zero(sizeof(or_state_t));
  1203. ret = parse_rfc1123_time("Sat, 26 Oct 1985 13:00:00 UTC",
  1204. &mock_ns.valid_after);
  1205. tt_int_op(ret, OP_EQ, 0);
  1206. ret = parse_rfc1123_time("Sat, 26 Oct 1985 14:00:00 UTC",
  1207. &mock_ns.fresh_until);
  1208. tt_int_op(ret, OP_EQ, 0);
  1209. voting_schedule_recalculate_timing(get_options(), mock_ns.valid_after);
  1210. update_approx_time(mock_ns.valid_after+1);
  1211. now = mock_ns.valid_after+1;
  1212. /* Create a service with no descriptor. It's added to the global map. */
  1213. service = hs_service_new(get_options());
  1214. tt_assert(service);
  1215. service->config.version = HS_VERSION_THREE;
  1216. ed25519_secret_key_generate(&service->keys.identity_sk, 0);
  1217. ed25519_public_key_generate(&service->keys.identity_pk,
  1218. &service->keys.identity_sk);
  1219. /* Register service to global map. */
  1220. ret = register_service(get_hs_service_map(), service);
  1221. tt_int_op(ret, OP_EQ, 0);
  1222. /* But first, build our descriptor. */
  1223. build_all_descriptors(now);
  1224. /* Nothing should happen because we have 0 introduction circuit established
  1225. * and we want (by default) 3 intro points. */
  1226. run_upload_descriptor_event(now);
  1227. /* If no upload happened, this should be untouched. */
  1228. tt_u64_op(service->desc_current->next_upload_time, OP_EQ, 0);
  1229. /* We'll simulate that we've opened our intro point circuit and that we only
  1230. * want one intro point. */
  1231. service->config.num_intro_points = 1;
  1232. /* Set our next upload time after now which will skip the upload. */
  1233. service->desc_current->next_upload_time = now + 1000;
  1234. run_upload_descriptor_event(now);
  1235. /* If no upload happened, this should be untouched. */
  1236. tt_u64_op(service->desc_current->next_upload_time, OP_EQ, now + 1000);
  1237. done:
  1238. hs_free_all();
  1239. UNMOCK(get_or_state);
  1240. }
  1241. /** Global vars used by test_rendezvous1_parsing() */
  1242. static char rend1_payload[RELAY_PAYLOAD_SIZE];
  1243. static size_t rend1_payload_len = 0;
  1244. /** Mock for relay_send_command_from_edge() to send a RENDEZVOUS1 cell. Instead
  1245. * of sending it to the network, instead save it to the global `rend1_payload`
  1246. * variable so that we can inspect it in the test_rendezvous1_parsing()
  1247. * test. */
  1248. static int
  1249. mock_relay_send_rendezvous1(streamid_t stream_id, circuit_t *circ,
  1250. uint8_t relay_command, const char *payload,
  1251. size_t payload_len,
  1252. crypt_path_t *cpath_layer,
  1253. const char *filename, int lineno)
  1254. {
  1255. (void) stream_id;
  1256. (void) circ;
  1257. (void) relay_command;
  1258. (void) cpath_layer;
  1259. (void) filename;
  1260. (void) lineno;
  1261. memcpy(rend1_payload, payload, payload_len);
  1262. rend1_payload_len = payload_len;
  1263. return 0;
  1264. }
  1265. /** Send a RENDEZVOUS1 as a service, and parse it as a client. */
  1266. static void
  1267. test_rendezvous1_parsing(void *arg)
  1268. {
  1269. int retval;
  1270. static const char *test_addr =
  1271. "4acth47i6kxnvkewtm6q7ib2s3ufpo5sqbsnzjpbi7utijcltosqemad.onion";
  1272. hs_service_t *service = NULL;
  1273. origin_circuit_t *service_circ = NULL;
  1274. origin_circuit_t *client_circ = NULL;
  1275. ed25519_keypair_t ip_auth_kp;
  1276. curve25519_keypair_t ephemeral_kp;
  1277. curve25519_keypair_t client_kp;
  1278. curve25519_keypair_t ip_enc_kp;
  1279. int flags = CIRCLAUNCH_NEED_UPTIME | CIRCLAUNCH_IS_INTERNAL;
  1280. (void) arg;
  1281. MOCK(relay_send_command_from_edge_, mock_relay_send_rendezvous1);
  1282. {
  1283. /* Let's start by setting up the service that will start the rend */
  1284. service = tor_malloc_zero(sizeof(hs_service_t));
  1285. ed25519_secret_key_generate(&service->keys.identity_sk, 0);
  1286. ed25519_public_key_generate(&service->keys.identity_pk,
  1287. &service->keys.identity_sk);
  1288. memcpy(service->onion_address, test_addr, sizeof(service->onion_address));
  1289. tt_assert(service);
  1290. }
  1291. {
  1292. /* Now let's set up the service rendezvous circuit and its keys. */
  1293. service_circ = helper_create_origin_circuit(CIRCUIT_PURPOSE_S_CONNECT_REND,
  1294. flags);
  1295. tor_free(service_circ->hs_ident);
  1296. hs_ntor_rend_cell_keys_t hs_ntor_rend_cell_keys;
  1297. uint8_t rendezvous_cookie[HS_REND_COOKIE_LEN];
  1298. curve25519_keypair_generate(&ip_enc_kp, 0);
  1299. curve25519_keypair_generate(&ephemeral_kp, 0);
  1300. curve25519_keypair_generate(&client_kp, 0);
  1301. ed25519_keypair_generate(&ip_auth_kp, 0);
  1302. retval = hs_ntor_service_get_rendezvous1_keys(&ip_auth_kp.pubkey,
  1303. &ip_enc_kp,
  1304. &ephemeral_kp,
  1305. &client_kp.pubkey,
  1306. &hs_ntor_rend_cell_keys);
  1307. tt_int_op(retval, OP_EQ, 0);
  1308. memset(rendezvous_cookie, 2, sizeof(rendezvous_cookie));
  1309. service_circ->hs_ident =
  1310. create_rp_circuit_identifier(service, rendezvous_cookie,
  1311. &ephemeral_kp.pubkey,
  1312. &hs_ntor_rend_cell_keys);
  1313. }
  1314. /* Send out the RENDEZVOUS1 and make sure that our mock func worked */
  1315. tt_assert(tor_mem_is_zero(rend1_payload, 32));
  1316. hs_circ_service_rp_has_opened(service, service_circ);
  1317. tt_assert(!tor_mem_is_zero(rend1_payload, 32));
  1318. tt_int_op(rend1_payload_len, OP_EQ, HS_LEGACY_RENDEZVOUS_CELL_SIZE);
  1319. /******************************/
  1320. /** Now let's create the client rendezvous circuit */
  1321. client_circ =
  1322. helper_create_origin_circuit(CIRCUIT_PURPOSE_C_REND_READY_INTRO_ACKED,
  1323. flags);
  1324. /* fix up its circ ident */
  1325. ed25519_pubkey_copy(&client_circ->hs_ident->intro_auth_pk,
  1326. &ip_auth_kp.pubkey);
  1327. memcpy(&client_circ->hs_ident->rendezvous_client_kp,
  1328. &client_kp, sizeof(client_circ->hs_ident->rendezvous_client_kp));
  1329. memcpy(&client_circ->hs_ident->intro_enc_pk.public_key,
  1330. &ip_enc_kp.pubkey.public_key,
  1331. sizeof(client_circ->hs_ident->intro_enc_pk.public_key));
  1332. /* Now parse the rendezvous2 circuit and make sure it was fine. We are
  1333. * skipping 20 bytes off its payload, since that's the rendezvous cookie
  1334. * which is only present in REND1. */
  1335. retval = handle_rendezvous2(client_circ,
  1336. (uint8_t*)rend1_payload+20,
  1337. rend1_payload_len-20);
  1338. tt_int_op(retval, OP_EQ, 0);
  1339. /* TODO: We are only simulating client/service here. We could also simulate
  1340. * the rendezvous point by plugging in rend_mid_establish_rendezvous(). We
  1341. * would need an extra circuit and some more stuff but it's doable. */
  1342. done:
  1343. circuit_free_(TO_CIRCUIT(service_circ));
  1344. circuit_free_(TO_CIRCUIT(client_circ));
  1345. hs_service_free(service);
  1346. hs_free_all();
  1347. UNMOCK(relay_send_command_from_edge_);
  1348. }
  1349. struct testcase_t hs_service_tests[] = {
  1350. { "e2e_rend_circuit_setup", test_e2e_rend_circuit_setup, TT_FORK,
  1351. NULL, NULL },
  1352. { "load_keys", test_load_keys, TT_FORK,
  1353. NULL, NULL },
  1354. { "access_service", test_access_service, TT_FORK,
  1355. NULL, NULL },
  1356. { "service_intro_point", test_service_intro_point, TT_FORK,
  1357. NULL, NULL },
  1358. { "helper_functions", test_helper_functions, TT_FORK,
  1359. NULL, NULL },
  1360. { "intro_circuit_opened", test_intro_circuit_opened, TT_FORK,
  1361. NULL, NULL },
  1362. { "intro_established", test_intro_established, TT_FORK,
  1363. NULL, NULL },
  1364. { "closing_intro_circs", test_closing_intro_circs, TT_FORK,
  1365. NULL, NULL },
  1366. { "rdv_circuit_opened", test_rdv_circuit_opened, TT_FORK,
  1367. NULL, NULL },
  1368. { "introduce2", test_introduce2, TT_FORK,
  1369. NULL, NULL },
  1370. { "service_event", test_service_event, TT_FORK,
  1371. NULL, NULL },
  1372. { "rotate_descriptors", test_rotate_descriptors, TT_FORK,
  1373. NULL, NULL },
  1374. { "build_update_descriptors", test_build_update_descriptors, TT_FORK,
  1375. NULL, NULL },
  1376. { "upload_descriptors", test_upload_descriptors, TT_FORK,
  1377. NULL, NULL },
  1378. { "rendezvous1_parsing", test_rendezvous1_parsing, TT_FORK,
  1379. NULL, NULL },
  1380. END_OF_TESTCASES
  1381. };