test_hs_service.c 74 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180
  1. /* Copyright (c) 2016-2019, 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 CONNECTION_EDGE_PRIVATE
  12. #define CRYPTO_PRIVATE
  13. #define HS_COMMON_PRIVATE
  14. #define HS_SERVICE_PRIVATE
  15. #define HS_INTROPOINT_PRIVATE
  16. #define HS_CIRCUIT_PRIVATE
  17. #define MAINLOOP_PRIVATE
  18. #define NETWORKSTATUS_PRIVATE
  19. #define STATEFILE_PRIVATE
  20. #define TOR_CHANNEL_INTERNAL_
  21. #define HS_CLIENT_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 "core/or/or.h"
  28. #include "app/config/config.h"
  29. #include "app/config/statefile.h"
  30. #include "core/crypto/hs_ntor.h"
  31. #include "core/mainloop/connection.h"
  32. #include "core/mainloop/mainloop.h"
  33. #include "core/or/circuitbuild.h"
  34. #include "core/or/circuitlist.h"
  35. #include "core/or/circuituse.h"
  36. #include "core/or/connection_edge.h"
  37. #include "core/or/edge_connection_st.h"
  38. #include "core/or/relay.h"
  39. #include "core/or/versions.h"
  40. #include "feature/dirauth/dirvote.h"
  41. #include "feature/dirauth/shared_random_state.h"
  42. #include "feature/dircommon/voting_schedule.h"
  43. #include "feature/hs/hs_circuit.h"
  44. #include "feature/hs/hs_circuitmap.h"
  45. #include "feature/hs/hs_client.h"
  46. #include "feature/hs/hs_common.h"
  47. #include "feature/hs/hs_config.h"
  48. #include "feature/hs/hs_ident.h"
  49. #include "feature/hs/hs_intropoint.h"
  50. #include "feature/hs/hs_service.h"
  51. #include "feature/nodelist/networkstatus.h"
  52. #include "feature/nodelist/nodelist.h"
  53. #include "feature/rend/rendservice.h"
  54. #include "lib/crypt_ops/crypto_rand.h"
  55. #include "lib/fs/dir.h"
  56. #include "core/or/cpath_build_state_st.h"
  57. #include "core/or/crypt_path_st.h"
  58. #include "feature/nodelist/networkstatus_st.h"
  59. #include "feature/nodelist/node_st.h"
  60. #include "core/or/origin_circuit_st.h"
  61. #include "app/config/or_state_st.h"
  62. #include "feature/nodelist/routerinfo_st.h"
  63. /* Trunnel */
  64. #include "trunnel/hs/cell_establish_intro.h"
  65. #ifdef HAVE_SYS_STAT_H
  66. #include <sys/stat.h>
  67. #endif
  68. #ifdef HAVE_UNISTD_H
  69. #include <unistd.h>
  70. #endif
  71. static networkstatus_t mock_ns;
  72. static networkstatus_t *
  73. mock_networkstatus_get_live_consensus(time_t now)
  74. {
  75. (void) now;
  76. return &mock_ns;
  77. }
  78. static or_state_t *dummy_state = NULL;
  79. /* Mock function to get fake or state (used for rev counters) */
  80. static or_state_t *
  81. get_or_state_replacement(void)
  82. {
  83. return dummy_state;
  84. }
  85. /* Mock function because we are not trying to test the close circuit that does
  86. * an awful lot of checks on the circuit object. */
  87. static void
  88. mock_circuit_mark_for_close(circuit_t *circ, int reason, int line,
  89. const char *file)
  90. {
  91. (void) circ;
  92. (void) reason;
  93. (void) line;
  94. (void) file;
  95. return;
  96. }
  97. static int
  98. mock_relay_send_command_from_edge(streamid_t stream_id, circuit_t *circ,
  99. uint8_t relay_command, const char *payload,
  100. size_t payload_len,
  101. crypt_path_t *cpath_layer,
  102. const char *filename, int lineno)
  103. {
  104. (void) stream_id;
  105. (void) circ;
  106. (void) relay_command;
  107. (void) payload;
  108. (void) payload_len;
  109. (void) cpath_layer;
  110. (void) filename;
  111. (void) lineno;
  112. return 0;
  113. }
  114. /* Helper: from a set of options in conf, configure a service which will add
  115. * it to the staging list of the HS subsytem. */
  116. static int
  117. helper_config_service(const char *conf)
  118. {
  119. int ret = 0;
  120. or_options_t *options = NULL;
  121. tt_assert(conf);
  122. options = helper_parse_options(conf);
  123. tt_assert(options);
  124. ret = hs_config_service_all(options, 0);
  125. done:
  126. or_options_free(options);
  127. return ret;
  128. }
  129. /* Test: Ensure that setting up rendezvous circuits works correctly. */
  130. static void
  131. test_e2e_rend_circuit_setup(void *arg)
  132. {
  133. ed25519_public_key_t service_pk;
  134. origin_circuit_t *or_circ;
  135. int retval;
  136. /** In this test we create a v3 prop224 service-side rendezvous circuit.
  137. * We simulate an HS ntor key exchange with a client, and check that
  138. * the circuit was setup correctly and is ready to accept rendezvous data */
  139. (void) arg;
  140. /* Now make dummy circuit */
  141. {
  142. or_circ = origin_circuit_new();
  143. or_circ->base_.purpose = CIRCUIT_PURPOSE_S_CONNECT_REND;
  144. or_circ->build_state = tor_malloc_zero(sizeof(cpath_build_state_t));
  145. or_circ->build_state->is_internal = 1;
  146. /* prop224: Setup hs conn identifier on the stream */
  147. ed25519_secret_key_t sk;
  148. tt_int_op(0, OP_EQ, ed25519_secret_key_generate(&sk, 0));
  149. tt_int_op(0, OP_EQ, ed25519_public_key_generate(&service_pk, &sk));
  150. or_circ->hs_ident = hs_ident_circuit_new(&service_pk,
  151. HS_IDENT_CIRCUIT_RENDEZVOUS);
  152. TO_CIRCUIT(or_circ)->state = CIRCUIT_STATE_OPEN;
  153. }
  154. /* Check number of hops */
  155. retval = cpath_get_n_hops(&or_circ->cpath);
  156. tt_int_op(retval, OP_EQ, 0);
  157. /* Setup the circuit: do the ntor key exchange */
  158. {
  159. uint8_t ntor_key_seed[DIGEST256_LEN] = {2};
  160. retval = hs_circuit_setup_e2e_rend_circ(or_circ,
  161. ntor_key_seed, sizeof(ntor_key_seed),
  162. 1);
  163. tt_int_op(retval, OP_EQ, 0);
  164. }
  165. /* See that a hop was added to the circuit's cpath */
  166. retval = cpath_get_n_hops(&or_circ->cpath);
  167. tt_int_op(retval, OP_EQ, 1);
  168. /* Check the digest algo */
  169. tt_int_op(crypto_digest_get_algorithm(or_circ->cpath->crypto.f_digest),
  170. OP_EQ, DIGEST_SHA3_256);
  171. tt_int_op(crypto_digest_get_algorithm(or_circ->cpath->crypto.b_digest),
  172. OP_EQ, DIGEST_SHA3_256);
  173. tt_assert(or_circ->cpath->crypto.f_crypto);
  174. tt_assert(or_circ->cpath->crypto.b_crypto);
  175. /* Ensure that circ purpose was changed */
  176. tt_int_op(or_circ->base_.purpose, OP_EQ, CIRCUIT_PURPOSE_S_REND_JOINED);
  177. done:
  178. circuit_free_(TO_CIRCUIT(or_circ));
  179. }
  180. /* Helper: Return a newly allocated and initialized origin circuit with
  181. * purpose and flags. A default HS identifier is set to an ed25519
  182. * authentication key for introduction point. */
  183. static origin_circuit_t *
  184. helper_create_origin_circuit(int purpose, int flags)
  185. {
  186. origin_circuit_t *circ = NULL;
  187. circ = origin_circuit_init(purpose, flags);
  188. tor_assert(circ);
  189. circ->cpath = tor_malloc_zero(sizeof(crypt_path_t));
  190. circ->cpath->magic = CRYPT_PATH_MAGIC;
  191. circ->cpath->state = CPATH_STATE_OPEN;
  192. circ->cpath->package_window = circuit_initial_package_window();
  193. circ->cpath->deliver_window = CIRCWINDOW_START;
  194. circ->cpath->prev = circ->cpath;
  195. /* Random nonce. */
  196. crypto_rand(circ->cpath->prev->rend_circ_nonce, DIGEST_LEN);
  197. /* Create a default HS identifier. */
  198. circ->hs_ident = tor_malloc_zero(sizeof(hs_ident_circuit_t));
  199. return circ;
  200. }
  201. /* Helper: Return a newly allocated authorized client object with
  202. * and a newly generated public key. */
  203. static hs_service_authorized_client_t *
  204. helper_create_authorized_client(void)
  205. {
  206. int ret;
  207. hs_service_authorized_client_t *client;
  208. curve25519_secret_key_t seckey;
  209. client = tor_malloc_zero(sizeof(hs_service_authorized_client_t));
  210. ret = curve25519_secret_key_generate(&seckey, 0);
  211. tt_int_op(ret, OP_EQ, 0);
  212. curve25519_public_key_generate(&client->client_pk, &seckey);
  213. done:
  214. return client;
  215. }
  216. /* Helper: Return a newly allocated authorized client object with the
  217. * same client name and the same public key as the given client. */
  218. static hs_service_authorized_client_t *
  219. helper_clone_authorized_client(const hs_service_authorized_client_t *client)
  220. {
  221. hs_service_authorized_client_t *client_out;
  222. tor_assert(client);
  223. client_out = tor_malloc_zero(sizeof(hs_service_authorized_client_t));
  224. memcpy(client_out->client_pk.public_key,
  225. client->client_pk.public_key, CURVE25519_PUBKEY_LEN);
  226. return client_out;
  227. }
  228. /* Helper: Return a newly allocated service object with the identity keypair
  229. * sets and the current descriptor. Then register it to the global map.
  230. * Caller should use hs_free_all() to free this service or remove it from the
  231. * global map before freeing. */
  232. static hs_service_t *
  233. helper_create_service(void)
  234. {
  235. /* Set a service for this circuit. */
  236. hs_service_t *service = hs_service_new(get_options());
  237. tor_assert(service);
  238. service->config.version = HS_VERSION_THREE;
  239. ed25519_secret_key_generate(&service->keys.identity_sk, 0);
  240. ed25519_public_key_generate(&service->keys.identity_pk,
  241. &service->keys.identity_sk);
  242. service->desc_current = service_descriptor_new();
  243. tt_assert(service->desc_current);
  244. /* Register service to global map. */
  245. int ret = register_service(get_hs_service_map(), service);
  246. tt_int_op(ret, OP_EQ, 0);
  247. done:
  248. return service;
  249. }
  250. /* Helper: Deallocate a given service object, its child objects and
  251. * remove it from onion service map.
  252. * */
  253. static void
  254. helper_destroy_service(hs_service_t *service)
  255. {
  256. if (!service)
  257. return;
  258. remove_service(get_hs_service_map(), service);
  259. hs_service_free(service);
  260. }
  261. /* Helper: Return a newly allocated service object with clients. */
  262. static hs_service_t *
  263. helper_create_service_with_clients(int num_clients)
  264. {
  265. int i;
  266. hs_service_t *service = helper_create_service();
  267. tt_assert(service);
  268. service->config.is_client_auth_enabled = 1;
  269. service->config.clients = smartlist_new();
  270. for (i = 0; i < num_clients; i++) {
  271. hs_service_authorized_client_t *client;
  272. client = helper_create_authorized_client();
  273. smartlist_add(service->config.clients, client);
  274. }
  275. done:
  276. return service;
  277. }
  278. /* Helper: Return a newly allocated service intro point with two link
  279. * specifiers, one IPv4 and one legacy ID set to As. */
  280. static hs_service_intro_point_t *
  281. helper_create_service_ip(void)
  282. {
  283. hs_desc_link_specifier_t *ls;
  284. hs_service_intro_point_t *ip = service_intro_point_new(NULL, 0, 0);
  285. tor_assert(ip);
  286. /* Add a first unused link specifier. */
  287. ls = tor_malloc_zero(sizeof(*ls));
  288. ls->type = LS_IPV4;
  289. smartlist_add(ip->base.link_specifiers, ls);
  290. /* Add a second link specifier used by a test. */
  291. ls = tor_malloc_zero(sizeof(*ls));
  292. ls->type = LS_LEGACY_ID;
  293. memset(ls->u.legacy_id, 'A', sizeof(ls->u.legacy_id));
  294. smartlist_add(ip->base.link_specifiers, ls);
  295. return ip;
  296. }
  297. static void
  298. test_load_keys(void *arg)
  299. {
  300. int ret;
  301. char *conf = NULL;
  302. char *hsdir_v2 = tor_strdup(get_fname("hs2"));
  303. char *hsdir_v3 = tor_strdup(get_fname("hs3"));
  304. char addr[HS_SERVICE_ADDR_LEN_BASE32 + 1];
  305. (void) arg;
  306. /* We'll register two services, a v2 and a v3, then we'll load keys and
  307. * validate that both are in a correct state. */
  308. hs_init();
  309. #define conf_fmt \
  310. "HiddenServiceDir %s\n" \
  311. "HiddenServiceVersion %d\n" \
  312. "HiddenServicePort 65535\n"
  313. /* v2 service. */
  314. tor_asprintf(&conf, conf_fmt, hsdir_v2, HS_VERSION_TWO);
  315. ret = helper_config_service(conf);
  316. tor_free(conf);
  317. tt_int_op(ret, OP_EQ, 0);
  318. /* This one should now be registered into the v2 list. */
  319. tt_int_op(get_hs_service_staging_list_size(), OP_EQ, 0);
  320. tt_int_op(rend_num_services(), OP_EQ, 1);
  321. /* v3 service. */
  322. tor_asprintf(&conf, conf_fmt, hsdir_v3, HS_VERSION_THREE);
  323. ret = helper_config_service(conf);
  324. tor_free(conf);
  325. tt_int_op(ret, OP_EQ, 0);
  326. /* It's in staging? */
  327. tt_int_op(get_hs_service_staging_list_size(), OP_EQ, 1);
  328. #undef conf_fmt
  329. /* Load the keys for these. After that, the v3 service should be registered
  330. * in the global map. */
  331. hs_service_load_all_keys();
  332. tt_int_op(get_hs_service_map_size(), OP_EQ, 1);
  333. hs_service_t *s = get_first_service();
  334. tt_assert(s);
  335. /* Ok we have the service object. Validate few things. */
  336. tt_assert(!tor_mem_is_zero(s->onion_address, sizeof(s->onion_address)));
  337. tt_int_op(hs_address_is_valid(s->onion_address), OP_EQ, 1);
  338. tt_assert(!tor_mem_is_zero((char *) s->keys.identity_sk.seckey,
  339. ED25519_SECKEY_LEN));
  340. tt_assert(!tor_mem_is_zero((char *) s->keys.identity_pk.pubkey,
  341. ED25519_PUBKEY_LEN));
  342. /* Check onion address from identity key. */
  343. hs_build_address(&s->keys.identity_pk, s->config.version, addr);
  344. tt_int_op(hs_address_is_valid(addr), OP_EQ, 1);
  345. tt_str_op(addr, OP_EQ, s->onion_address);
  346. /* Check that the is_client_auth_enabled is not set. */
  347. tt_assert(!s->config.is_client_auth_enabled);
  348. done:
  349. tor_free(hsdir_v2);
  350. tor_free(hsdir_v3);
  351. hs_free_all();
  352. }
  353. static void
  354. test_client_filename_is_valid(void *arg)
  355. {
  356. (void) arg;
  357. /* Valid file name. */
  358. tt_assert(client_filename_is_valid("a.auth"));
  359. /* Valid file name with special character. */
  360. tt_assert(client_filename_is_valid("a-.auth"));
  361. /* Invalid extension. */
  362. tt_assert(!client_filename_is_valid("a.ath"));
  363. /* Nothing before the extension. */
  364. tt_assert(!client_filename_is_valid(".auth"));
  365. done:
  366. ;
  367. }
  368. static void
  369. test_parse_authorized_client(void *arg)
  370. {
  371. hs_service_authorized_client_t *client = NULL;
  372. (void) arg;
  373. /* Valid authorized client. */
  374. client = parse_authorized_client(
  375. "descriptor:x25519:dz4q5xqlb4ldnbs72iarrml4ephk3du4i7o2cgiva5lwr6wkquja");
  376. tt_assert(client);
  377. /* Wrong number of fields. */
  378. tt_assert(!parse_authorized_client("a:b:c:d:e"));
  379. /* Wrong auth type. */
  380. tt_assert(!parse_authorized_client(
  381. "x:x25519:dz4q5xqlb4ldnbs72iarrml4ephk3du4i7o2cgiva5lwr6wkquja"));
  382. /* Wrong key type. */
  383. tt_assert(!parse_authorized_client(
  384. "descriptor:x:dz4q5xqlb4ldnbs72iarrml4ephk3du4i7o2cgiva5lwr6wkquja"));
  385. /* Some malformed string. */
  386. tt_assert(!parse_authorized_client("descriptor:x25519:aa=="));
  387. tt_assert(!parse_authorized_client("descriptor:"));
  388. tt_assert(!parse_authorized_client("descriptor:x25519"));
  389. tt_assert(!parse_authorized_client("descriptor:x25519:"));
  390. tt_assert(!parse_authorized_client(""));
  391. done:
  392. service_authorized_client_free(client);
  393. }
  394. static char *
  395. mock_read_file_to_str(const char *filename, int flags, struct stat *stat_out)
  396. {
  397. char *ret = NULL;
  398. (void) flags;
  399. (void) stat_out;
  400. if (!strcmp(filename, get_fname("hs3" PATH_SEPARATOR
  401. "authorized_clients" PATH_SEPARATOR
  402. "client1.auth"))) {
  403. ret = tor_strdup("descriptor:x25519:"
  404. "dz4q5xqlb4ldnbs72iarrml4ephk3du4i7o2cgiva5lwr6wkquja");
  405. goto done;
  406. }
  407. if (!strcmp(filename, get_fname("hs3" PATH_SEPARATOR
  408. "authorized_clients" PATH_SEPARATOR
  409. "dummy.xxx"))) {
  410. ret = tor_strdup("descriptor:x25519:"
  411. "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
  412. goto done;
  413. }
  414. if (!strcmp(filename, get_fname("hs3" PATH_SEPARATOR
  415. "authorized_clients" PATH_SEPARATOR
  416. "client2.auth"))) {
  417. ret = tor_strdup("descriptor:x25519:"
  418. "okoi2gml3wd6x7jganlk5d66xxyjgg24sxw4y7javx4giqr66zta");
  419. goto done;
  420. }
  421. done:
  422. return ret;
  423. }
  424. static smartlist_t *
  425. mock_tor_listdir(const char *dirname)
  426. {
  427. smartlist_t *file_list = smartlist_new();
  428. (void) dirname;
  429. smartlist_add(file_list, tor_strdup("client1.auth"));
  430. smartlist_add(file_list, tor_strdup("dummy.xxx"));
  431. smartlist_add(file_list, tor_strdup("client2.auth"));
  432. return file_list;
  433. }
  434. static void
  435. test_load_keys_with_client_auth(void *arg)
  436. {
  437. int ret;
  438. char *conf = NULL;
  439. smartlist_t *pubkey_b32_list = smartlist_new();
  440. char *hsdir_v3 = tor_strdup(get_fname("hs3"));
  441. hs_service_t *service;
  442. (void) arg;
  443. hs_init();
  444. smartlist_add(pubkey_b32_list, tor_strdup(
  445. "dz4q5xqlb4ldnbs72iarrml4ephk3du4i7o2cgiva5lwr6wkquja"));
  446. smartlist_add(pubkey_b32_list, tor_strdup(
  447. "okoi2gml3wd6x7jganlk5d66xxyjgg24sxw4y7javx4giqr66zta"));
  448. #define conf_fmt \
  449. "HiddenServiceDir %s\n" \
  450. "HiddenServiceVersion %d\n" \
  451. "HiddenServicePort 65534\n"
  452. tor_asprintf(&conf, conf_fmt, hsdir_v3, HS_VERSION_THREE);
  453. ret = helper_config_service(conf);
  454. tor_free(conf);
  455. tt_int_op(ret, OP_EQ, 0);
  456. /* It's in staging? */
  457. tt_int_op(get_hs_service_staging_list_size(), OP_EQ, 1);
  458. #undef conf_fmt
  459. MOCK(read_file_to_str, mock_read_file_to_str);
  460. MOCK(tor_listdir, mock_tor_listdir);
  461. /* Load the keys for these. After that, the v3 service should be registered
  462. * in the global map. */
  463. hs_service_load_all_keys();
  464. tt_int_op(get_hs_service_map_size(), OP_EQ, 1);
  465. service = get_first_service();
  466. tt_assert(service);
  467. tt_assert(service->config.clients);
  468. tt_int_op(smartlist_len(service->config.clients), OP_EQ,
  469. smartlist_len(pubkey_b32_list));
  470. /* Test that the is_client_auth_enabled flag is set. */
  471. tt_assert(service->config.is_client_auth_enabled);
  472. /* Test that the keys in clients are correct. */
  473. SMARTLIST_FOREACH_BEGIN(pubkey_b32_list, char *, pubkey_b32) {
  474. curve25519_public_key_t pubkey;
  475. /* This flag will be set if the key is found in clients. */
  476. int is_found = 0;
  477. base32_decode((char *) pubkey.public_key, sizeof(pubkey.public_key),
  478. pubkey_b32, strlen(pubkey_b32));
  479. SMARTLIST_FOREACH_BEGIN(service->config.clients,
  480. hs_service_authorized_client_t *, client) {
  481. if (tor_memeq(&pubkey, &client->client_pk, sizeof(pubkey))) {
  482. is_found = 1;
  483. break;
  484. }
  485. } SMARTLIST_FOREACH_END(client);
  486. tt_assert(is_found);
  487. } SMARTLIST_FOREACH_END(pubkey_b32);
  488. done:
  489. SMARTLIST_FOREACH(pubkey_b32_list, char *, s, tor_free(s));
  490. smartlist_free(pubkey_b32_list);
  491. tor_free(hsdir_v3);
  492. hs_free_all();
  493. UNMOCK(read_file_to_str);
  494. UNMOCK(tor_listdir);
  495. }
  496. static void
  497. test_access_service(void *arg)
  498. {
  499. int ret;
  500. char *conf = NULL;
  501. char *hsdir_v3 = tor_strdup(get_fname("hs3"));
  502. hs_service_ht *global_map;
  503. hs_service_t *s = NULL;
  504. (void) arg;
  505. /* We'll register two services, a v2 and a v3, then we'll load keys and
  506. * validate that both are in a correct state. */
  507. hs_init();
  508. #define conf_fmt \
  509. "HiddenServiceDir %s\n" \
  510. "HiddenServiceVersion %d\n" \
  511. "HiddenServicePort 65535\n"
  512. /* v3 service. */
  513. tor_asprintf(&conf, conf_fmt, hsdir_v3, HS_VERSION_THREE);
  514. ret = helper_config_service(conf);
  515. tor_free(conf);
  516. tt_int_op(ret, OP_EQ, 0);
  517. /* It's in staging? */
  518. tt_int_op(get_hs_service_staging_list_size(), OP_EQ, 1);
  519. /* Load the keys for these. After that, the v3 service should be registered
  520. * in the global map. */
  521. hs_service_load_all_keys();
  522. tt_int_op(get_hs_service_map_size(), OP_EQ, 1);
  523. s = get_first_service();
  524. tt_assert(s);
  525. global_map = get_hs_service_map();
  526. tt_assert(global_map);
  527. /* From here, we'll try the service accessors. */
  528. hs_service_t *query = find_service(global_map, &s->keys.identity_pk);
  529. tt_assert(query);
  530. tt_mem_op(query, OP_EQ, s, sizeof(hs_service_t));
  531. /* Remove service, check if it actually works and then put it back. */
  532. remove_service(global_map, s);
  533. tt_int_op(get_hs_service_map_size(), OP_EQ, 0);
  534. query = find_service(global_map, &s->keys.identity_pk);
  535. tt_ptr_op(query, OP_EQ, NULL);
  536. /* Register back the service in the map. */
  537. ret = register_service(global_map, s);
  538. tt_int_op(ret, OP_EQ, 0);
  539. tt_int_op(get_hs_service_map_size(), OP_EQ, 1);
  540. /* Twice should fail. */
  541. ret = register_service(global_map, s);
  542. tt_int_op(ret, OP_EQ, -1);
  543. /* Remove service from map so we don't double free on cleanup. */
  544. remove_service(global_map, s);
  545. tt_int_op(get_hs_service_map_size(), OP_EQ, 0);
  546. query = find_service(global_map, &s->keys.identity_pk);
  547. tt_ptr_op(query, OP_EQ, NULL);
  548. /* Let's try to remove twice for fun. */
  549. setup_full_capture_of_logs(LOG_WARN);
  550. remove_service(global_map, s);
  551. expect_log_msg_containing("Could not find service in the global map");
  552. teardown_capture_of_logs();
  553. done:
  554. hs_service_free(s);
  555. tor_free(hsdir_v3);
  556. hs_free_all();
  557. }
  558. /** Test that we can create intro point objects, index them and find them */
  559. static void
  560. test_service_intro_point(void *arg)
  561. {
  562. hs_service_t *service = NULL;
  563. hs_service_intro_point_t *ip = NULL;
  564. (void) arg;
  565. /* Test simple creation of an object. */
  566. {
  567. time_t now = time(NULL);
  568. ip = helper_create_service_ip();
  569. tt_assert(ip);
  570. /* Make sure the authentication keypair is not zeroes. */
  571. tt_int_op(tor_mem_is_zero((const char *) &ip->auth_key_kp,
  572. sizeof(ed25519_keypair_t)), OP_EQ, 0);
  573. /* The introduce2_max MUST be in that range. */
  574. tt_u64_op(ip->introduce2_max, OP_GE,
  575. INTRO_POINT_MIN_LIFETIME_INTRODUCTIONS);
  576. tt_u64_op(ip->introduce2_max, OP_LE,
  577. INTRO_POINT_MAX_LIFETIME_INTRODUCTIONS);
  578. /* Time to expire MUST also be in that range. We subtract 500 seconds
  579. * because there could be a gap between setting now and the time taken in
  580. * service_intro_point_new. On ARM and other older CPUs, it can be
  581. * surprisingly slow... */
  582. tt_u64_op(ip->time_to_expire, OP_GE,
  583. now + INTRO_POINT_LIFETIME_MIN_SECONDS - 500);
  584. /* We add 500 seconds, because this time we're testing against the
  585. * maximum allowed time. */
  586. tt_u64_op(ip->time_to_expire, OP_LE,
  587. now + INTRO_POINT_LIFETIME_MAX_SECONDS + 500);
  588. tt_assert(ip->replay_cache);
  589. tt_assert(ip->base.link_specifiers);
  590. /* By default, this is NOT a legacy object. */
  591. tt_int_op(ip->base.is_only_legacy, OP_EQ, 0);
  592. }
  593. /* Test functions that uses a service intropoints map with that previously
  594. * created object (non legacy). */
  595. {
  596. ed25519_public_key_t garbage = { {0} };
  597. hs_service_intro_point_t *query;
  598. service = hs_service_new(get_options());
  599. tt_assert(service);
  600. service->desc_current = service_descriptor_new();
  601. tt_assert(service->desc_current);
  602. /* Add intropoint to descriptor map. */
  603. service_intro_point_add(service->desc_current->intro_points.map, ip);
  604. query = service_intro_point_find(service, &ip->auth_key_kp.pubkey);
  605. tt_mem_op(query, OP_EQ, ip, sizeof(hs_service_intro_point_t));
  606. query = service_intro_point_find(service, &garbage);
  607. tt_ptr_op(query, OP_EQ, NULL);
  608. /* While at it, can I find the descriptor with the intro point? */
  609. hs_service_descriptor_t *desc_lookup =
  610. service_desc_find_by_intro(service, ip);
  611. tt_mem_op(service->desc_current, OP_EQ, desc_lookup,
  612. sizeof(hs_service_descriptor_t));
  613. /* Remove object from service descriptor and make sure it is out. */
  614. service_intro_point_remove(service, ip);
  615. query = service_intro_point_find(service, &ip->auth_key_kp.pubkey);
  616. tt_ptr_op(query, OP_EQ, NULL);
  617. }
  618. done:
  619. /* If the test succeed, this object is no longer referenced in the service
  620. * so we can free it without use after free. Else, it might explode because
  621. * it's still in the service descriptor map. */
  622. service_intro_point_free(ip);
  623. hs_service_free(service);
  624. }
  625. static node_t mock_node;
  626. static const node_t *
  627. mock_node_get_by_id(const char *digest)
  628. {
  629. (void) digest;
  630. memset(mock_node.identity, 'A', DIGEST_LEN);
  631. /* Only return the matchin identity of As */
  632. if (!tor_memcmp(mock_node.identity, digest, DIGEST_LEN)) {
  633. return &mock_node;
  634. }
  635. return NULL;
  636. }
  637. static void
  638. test_helper_functions(void *arg)
  639. {
  640. int ret;
  641. hs_service_t *service = NULL;
  642. hs_service_intro_point_t *ip = NULL;
  643. hs_ident_circuit_t ident;
  644. (void) arg;
  645. MOCK(node_get_by_id, mock_node_get_by_id);
  646. hs_service_init();
  647. time_t now = time(NULL);
  648. update_approx_time(now);
  649. service = helper_create_service();
  650. ip = helper_create_service_ip();
  651. /* Immediately add the intro point to the service so the free service at the
  652. * end cleans it as well. */
  653. service_intro_point_add(service->desc_current->intro_points.map, ip);
  654. /* Setup the circuit identifier. */
  655. ed25519_pubkey_copy(&ident.intro_auth_pk, &ip->auth_key_kp.pubkey);
  656. ed25519_pubkey_copy(&ident.identity_pk, &service->keys.identity_pk);
  657. /* Testing get_objects_from_ident(). */
  658. {
  659. hs_service_t *s_lookup = NULL;
  660. hs_service_intro_point_t *ip_lookup = NULL;
  661. hs_service_descriptor_t *desc_lookup = NULL;
  662. get_objects_from_ident(&ident, &s_lookup, &ip_lookup, &desc_lookup);
  663. tt_mem_op(s_lookup, OP_EQ, service, sizeof(hs_service_t));
  664. tt_mem_op(ip_lookup, OP_EQ, ip, sizeof(hs_service_intro_point_t));
  665. tt_mem_op(desc_lookup, OP_EQ, service->desc_current,
  666. sizeof(hs_service_descriptor_t));
  667. /* Reset */
  668. s_lookup = NULL; ip_lookup = NULL; desc_lookup = NULL;
  669. /* NULL parameter should work. */
  670. get_objects_from_ident(&ident, NULL, &ip_lookup, &desc_lookup);
  671. tt_mem_op(ip_lookup, OP_EQ, ip, sizeof(hs_service_intro_point_t));
  672. tt_mem_op(desc_lookup, OP_EQ, service->desc_current,
  673. sizeof(hs_service_descriptor_t));
  674. /* Reset. */
  675. s_lookup = NULL; ip_lookup = NULL; desc_lookup = NULL;
  676. /* Break the ident and we should find nothing. */
  677. memset(&ident, 0, sizeof(ident));
  678. get_objects_from_ident(&ident, &s_lookup, &ip_lookup, &desc_lookup);
  679. tt_ptr_op(s_lookup, OP_EQ, NULL);
  680. tt_ptr_op(ip_lookup, OP_EQ, NULL);
  681. tt_ptr_op(desc_lookup, OP_EQ, NULL);
  682. }
  683. /* Testing get_node_from_intro_point() */
  684. {
  685. const node_t *node = get_node_from_intro_point(ip);
  686. tt_ptr_op(node, OP_EQ, &mock_node);
  687. SMARTLIST_FOREACH_BEGIN(ip->base.link_specifiers,
  688. hs_desc_link_specifier_t *, ls) {
  689. if (ls->type == LS_LEGACY_ID) {
  690. /* Change legacy id in link specifier which is not the mock node. */
  691. memset(ls->u.legacy_id, 'B', sizeof(ls->u.legacy_id));
  692. }
  693. } SMARTLIST_FOREACH_END(ls);
  694. node = get_node_from_intro_point(ip);
  695. tt_ptr_op(node, OP_EQ, NULL);
  696. }
  697. /* Testing can_service_launch_intro_circuit() */
  698. {
  699. /* Put the start of the retry period back in time, we should be allowed.
  700. * to launch intro circuit. */
  701. service->state.num_intro_circ_launched = 2;
  702. service->state.intro_circ_retry_started_time =
  703. (now - INTRO_CIRC_RETRY_PERIOD - 1);
  704. ret = can_service_launch_intro_circuit(service, now);
  705. tt_int_op(ret, OP_EQ, 1);
  706. tt_u64_op(service->state.intro_circ_retry_started_time, OP_EQ, now);
  707. tt_u64_op(service->state.num_intro_circ_launched, OP_EQ, 0);
  708. /* Call it again, we should still be allowed because we are under
  709. * MAX_INTRO_CIRCS_PER_PERIOD which been set to 0 previously. */
  710. ret = can_service_launch_intro_circuit(service, now);
  711. tt_int_op(ret, OP_EQ, 1);
  712. tt_u64_op(service->state.intro_circ_retry_started_time, OP_EQ, now);
  713. tt_u64_op(service->state.num_intro_circ_launched, OP_EQ, 0);
  714. /* Too many intro circuit launched means we are not allowed. */
  715. service->state.num_intro_circ_launched = 20;
  716. ret = can_service_launch_intro_circuit(service, now);
  717. tt_int_op(ret, OP_EQ, 0);
  718. }
  719. /* Testing intro_point_should_expire(). */
  720. {
  721. /* Just some basic test of the current state. */
  722. tt_u64_op(ip->introduce2_max, OP_GE,
  723. INTRO_POINT_MIN_LIFETIME_INTRODUCTIONS);
  724. tt_u64_op(ip->introduce2_max, OP_LE,
  725. INTRO_POINT_MAX_LIFETIME_INTRODUCTIONS);
  726. tt_u64_op(ip->time_to_expire, OP_GE,
  727. now + INTRO_POINT_LIFETIME_MIN_SECONDS);
  728. tt_u64_op(ip->time_to_expire, OP_LE,
  729. now + INTRO_POINT_LIFETIME_MAX_SECONDS);
  730. /* This newly created IP from above shouldn't expire now. */
  731. ret = intro_point_should_expire(ip, now);
  732. tt_int_op(ret, OP_EQ, 0);
  733. /* Maximum number of INTRODUCE2 cell reached, it should expire. */
  734. ip->introduce2_count = INTRO_POINT_MAX_LIFETIME_INTRODUCTIONS + 1;
  735. ret = intro_point_should_expire(ip, now);
  736. tt_int_op(ret, OP_EQ, 1);
  737. ip->introduce2_count = 0;
  738. /* It should expire if time to expire has been reached. */
  739. ip->time_to_expire = now - 1000;
  740. ret = intro_point_should_expire(ip, now);
  741. tt_int_op(ret, OP_EQ, 1);
  742. }
  743. done:
  744. /* This will free the service and all objects associated to it. */
  745. hs_service_free_all();
  746. UNMOCK(node_get_by_id);
  747. }
  748. /** Test that we do the right operations when an intro circuit opens */
  749. static void
  750. test_intro_circuit_opened(void *arg)
  751. {
  752. int flags = CIRCLAUNCH_NEED_UPTIME | CIRCLAUNCH_IS_INTERNAL;
  753. hs_service_t *service;
  754. origin_circuit_t *circ = NULL;
  755. (void) arg;
  756. hs_init();
  757. MOCK(circuit_mark_for_close_, mock_circuit_mark_for_close);
  758. MOCK(relay_send_command_from_edge_, mock_relay_send_command_from_edge);
  759. circ = helper_create_origin_circuit(CIRCUIT_PURPOSE_S_ESTABLISH_INTRO,
  760. flags);
  761. /* No service associated with this circuit. */
  762. setup_full_capture_of_logs(LOG_WARN);
  763. hs_service_circuit_has_opened(circ);
  764. expect_log_msg_containing("Unknown service identity key");
  765. teardown_capture_of_logs();
  766. /* Set a service for this circuit. */
  767. {
  768. service = helper_create_service();
  769. ed25519_pubkey_copy(&circ->hs_ident->identity_pk,
  770. &service->keys.identity_pk);
  771. /* No intro point associated with this circuit. */
  772. setup_full_capture_of_logs(LOG_WARN);
  773. hs_service_circuit_has_opened(circ);
  774. expect_log_msg_containing("Unknown introduction point auth key");
  775. teardown_capture_of_logs();
  776. }
  777. /* Set an IP object now for this circuit. */
  778. {
  779. hs_service_intro_point_t *ip = helper_create_service_ip();
  780. service_intro_point_add(service->desc_current->intro_points.map, ip);
  781. /* Update ident to contain the intro point auth key. */
  782. ed25519_pubkey_copy(&circ->hs_ident->intro_auth_pk,
  783. &ip->auth_key_kp.pubkey);
  784. }
  785. /* This one should go all the way. */
  786. setup_full_capture_of_logs(LOG_INFO);
  787. hs_service_circuit_has_opened(circ);
  788. expect_log_msg_containing("Introduction circuit 0 established for service");
  789. teardown_capture_of_logs();
  790. done:
  791. circuit_free_(TO_CIRCUIT(circ));
  792. hs_free_all();
  793. UNMOCK(circuit_mark_for_close_);
  794. UNMOCK(relay_send_command_from_edge_);
  795. }
  796. /** Test the operations we do on a circuit after we learn that we successfully
  797. * established an intro point on it */
  798. static void
  799. test_intro_established(void *arg)
  800. {
  801. int ret;
  802. int flags = CIRCLAUNCH_NEED_UPTIME | CIRCLAUNCH_IS_INTERNAL;
  803. uint8_t payload[RELAY_PAYLOAD_SIZE] = {0};
  804. origin_circuit_t *circ = NULL;
  805. hs_service_t *service;
  806. hs_service_intro_point_t *ip = NULL;
  807. (void) arg;
  808. hs_init();
  809. MOCK(circuit_mark_for_close_, mock_circuit_mark_for_close);
  810. circ = helper_create_origin_circuit(CIRCUIT_PURPOSE_S_ESTABLISH_INTRO,
  811. flags);
  812. tt_assert(circ);
  813. /* Test a wrong purpose. */
  814. TO_CIRCUIT(circ)->purpose = CIRCUIT_PURPOSE_S_INTRO;
  815. setup_full_capture_of_logs(LOG_WARN);
  816. ret = hs_service_receive_intro_established(circ, payload, sizeof(payload));
  817. tt_int_op(ret, OP_EQ, -1);
  818. expect_log_msg_containing("Received an INTRO_ESTABLISHED cell on a "
  819. "non introduction circuit of purpose");
  820. teardown_capture_of_logs();
  821. /* Back to normal. */
  822. TO_CIRCUIT(circ)->purpose = CIRCUIT_PURPOSE_S_ESTABLISH_INTRO;
  823. /* No service associated to it. */
  824. setup_full_capture_of_logs(LOG_WARN);
  825. ret = hs_service_receive_intro_established(circ, payload, sizeof(payload));
  826. tt_int_op(ret, OP_EQ, -1);
  827. expect_log_msg_containing("Unknown service identity key");
  828. teardown_capture_of_logs();
  829. /* Set a service for this circuit. */
  830. service = helper_create_service();
  831. ed25519_pubkey_copy(&circ->hs_ident->identity_pk,
  832. &service->keys.identity_pk);
  833. /* No introduction point associated to it. */
  834. setup_full_capture_of_logs(LOG_WARN);
  835. ret = hs_service_receive_intro_established(circ, payload, sizeof(payload));
  836. tt_int_op(ret, OP_EQ, -1);
  837. expect_log_msg_containing("Introduction circuit established without an "
  838. "intro point object on circuit");
  839. teardown_capture_of_logs();
  840. /* Set an IP object now for this circuit. */
  841. {
  842. ip = helper_create_service_ip();
  843. service_intro_point_add(service->desc_current->intro_points.map, ip);
  844. /* Update ident to contain the intro point auth key. */
  845. ed25519_pubkey_copy(&circ->hs_ident->intro_auth_pk,
  846. &ip->auth_key_kp.pubkey);
  847. }
  848. /* Send an empty payload. INTRO_ESTABLISHED cells are basically zeroes. */
  849. ret = hs_service_receive_intro_established(circ, payload, sizeof(payload));
  850. tt_int_op(ret, OP_EQ, 0);
  851. tt_u64_op(ip->circuit_established, OP_EQ, 1);
  852. tt_int_op(TO_CIRCUIT(circ)->purpose, OP_EQ, CIRCUIT_PURPOSE_S_INTRO);
  853. done:
  854. if (circ)
  855. circuit_free_(TO_CIRCUIT(circ));
  856. hs_free_all();
  857. UNMOCK(circuit_mark_for_close_);
  858. }
  859. /** Check the operations we do on a rendezvous circuit after we learn it's
  860. * open */
  861. static void
  862. test_rdv_circuit_opened(void *arg)
  863. {
  864. int flags = CIRCLAUNCH_NEED_UPTIME | CIRCLAUNCH_IS_INTERNAL;
  865. origin_circuit_t *circ = NULL;
  866. hs_service_t *service;
  867. (void) arg;
  868. hs_init();
  869. MOCK(circuit_mark_for_close_, mock_circuit_mark_for_close);
  870. MOCK(relay_send_command_from_edge_, mock_relay_send_command_from_edge);
  871. circ = helper_create_origin_circuit(CIRCUIT_PURPOSE_S_CONNECT_REND, flags);
  872. crypto_rand((char *) circ->hs_ident->rendezvous_cookie, REND_COOKIE_LEN);
  873. crypto_rand((char *) circ->hs_ident->rendezvous_handshake_info,
  874. sizeof(circ->hs_ident->rendezvous_handshake_info));
  875. /* No service associated with this circuit. */
  876. setup_full_capture_of_logs(LOG_WARN);
  877. hs_service_circuit_has_opened(circ);
  878. expect_log_msg_containing("Unknown service identity key");
  879. teardown_capture_of_logs();
  880. /* This should be set to a non zero timestamp. */
  881. tt_u64_op(TO_CIRCUIT(circ)->timestamp_dirty, OP_NE, 0);
  882. /* Set a service for this circuit. */
  883. service = helper_create_service();
  884. ed25519_pubkey_copy(&circ->hs_ident->identity_pk,
  885. &service->keys.identity_pk);
  886. /* Should be all good. */
  887. hs_service_circuit_has_opened(circ);
  888. tt_int_op(TO_CIRCUIT(circ)->purpose, OP_EQ, CIRCUIT_PURPOSE_S_REND_JOINED);
  889. done:
  890. circuit_free_(TO_CIRCUIT(circ));
  891. hs_free_all();
  892. UNMOCK(circuit_mark_for_close_);
  893. UNMOCK(relay_send_command_from_edge_);
  894. }
  895. static void
  896. mock_assert_circuit_ok(const circuit_t *c)
  897. {
  898. (void) c;
  899. return;
  900. }
  901. /** Test for the general mechanism for closing intro circs.
  902. * Also a way to identify that #23603 has been fixed. */
  903. static void
  904. test_closing_intro_circs(void *arg)
  905. {
  906. hs_service_t *service = NULL;
  907. hs_service_intro_point_t *ip = NULL, *entry = NULL;
  908. origin_circuit_t *intro_circ = NULL, *tmp_circ;
  909. int flags = CIRCLAUNCH_NEED_UPTIME | CIRCLAUNCH_IS_INTERNAL;
  910. (void) arg;
  911. MOCK(assert_circuit_ok, mock_assert_circuit_ok);
  912. hs_init();
  913. /* Initialize service */
  914. service = helper_create_service();
  915. /* Initialize intro point */
  916. ip = helper_create_service_ip();
  917. tt_assert(ip);
  918. service_intro_point_add(service->desc_current->intro_points.map, ip);
  919. /* Initialize intro circuit */
  920. intro_circ = origin_circuit_init(CIRCUIT_PURPOSE_S_ESTABLISH_INTRO, flags);
  921. intro_circ->hs_ident = hs_ident_circuit_new(&service->keys.identity_pk,
  922. HS_IDENT_CIRCUIT_INTRO);
  923. /* Register circuit in the circuitmap . */
  924. hs_circuitmap_register_intro_circ_v3_service_side(intro_circ,
  925. &ip->auth_key_kp.pubkey);
  926. tmp_circ =
  927. hs_circuitmap_get_intro_circ_v3_service_side(&ip->auth_key_kp.pubkey);
  928. tt_ptr_op(tmp_circ, OP_EQ, intro_circ);
  929. /* Pretend that intro point has failed too much */
  930. ip->circuit_retries = MAX_INTRO_POINT_CIRCUIT_RETRIES+1;
  931. /* Now pretend we are freeing this intro circuit. We want to see that our
  932. * destructor is not gonna kill our intro point structure since that's the
  933. * job of the cleanup routine. */
  934. circuit_free_(TO_CIRCUIT(intro_circ));
  935. intro_circ = NULL;
  936. entry = service_intro_point_find(service, &ip->auth_key_kp.pubkey);
  937. tt_assert(entry);
  938. /* The free should also remove the circuit from the circuitmap. */
  939. tmp_circ =
  940. hs_circuitmap_get_intro_circ_v3_service_side(&ip->auth_key_kp.pubkey);
  941. tt_assert(!tmp_circ);
  942. /* Now pretend that a new intro point circ was launched and opened. Check
  943. * that the intro point will be established correctly. */
  944. intro_circ = origin_circuit_init(CIRCUIT_PURPOSE_S_ESTABLISH_INTRO, flags);
  945. intro_circ->hs_ident = hs_ident_circuit_new(&service->keys.identity_pk,
  946. HS_IDENT_CIRCUIT_INTRO);
  947. ed25519_pubkey_copy(&intro_circ->hs_ident->intro_auth_pk,
  948. &ip->auth_key_kp.pubkey);
  949. /* Register circuit in the circuitmap . */
  950. hs_circuitmap_register_intro_circ_v3_service_side(intro_circ,
  951. &ip->auth_key_kp.pubkey);
  952. tmp_circ =
  953. hs_circuitmap_get_intro_circ_v3_service_side(&ip->auth_key_kp.pubkey);
  954. tt_ptr_op(tmp_circ, OP_EQ, intro_circ);
  955. tt_int_op(TO_CIRCUIT(intro_circ)->marked_for_close, OP_EQ, 0);
  956. circuit_mark_for_close(TO_CIRCUIT(intro_circ), END_CIRC_REASON_INTERNAL);
  957. tt_int_op(TO_CIRCUIT(intro_circ)->marked_for_close, OP_NE, 0);
  958. /* At this point, we should not be able to find it in the circuitmap. */
  959. tmp_circ =
  960. hs_circuitmap_get_intro_circ_v3_service_side(&ip->auth_key_kp.pubkey);
  961. tt_assert(!tmp_circ);
  962. done:
  963. if (intro_circ) {
  964. circuit_free_(TO_CIRCUIT(intro_circ));
  965. }
  966. /* Frees the service object. */
  967. hs_free_all();
  968. UNMOCK(assert_circuit_ok);
  969. }
  970. /** Test sending and receiving introduce2 cells */
  971. static void
  972. test_introduce2(void *arg)
  973. {
  974. int ret;
  975. int flags = CIRCLAUNCH_NEED_UPTIME | CIRCLAUNCH_IS_INTERNAL;
  976. uint8_t payload[RELAY_PAYLOAD_SIZE] = {0};
  977. origin_circuit_t *circ = NULL;
  978. hs_service_t *service;
  979. hs_service_intro_point_t *ip = NULL;
  980. (void) arg;
  981. hs_init();
  982. MOCK(circuit_mark_for_close_, mock_circuit_mark_for_close);
  983. MOCK(get_or_state,
  984. get_or_state_replacement);
  985. dummy_state = tor_malloc_zero(sizeof(or_state_t));
  986. circ = helper_create_origin_circuit(CIRCUIT_PURPOSE_S_INTRO, flags);
  987. tt_assert(circ);
  988. /* Test a wrong purpose. */
  989. TO_CIRCUIT(circ)->purpose = CIRCUIT_PURPOSE_S_ESTABLISH_INTRO;
  990. setup_full_capture_of_logs(LOG_WARN);
  991. ret = hs_service_receive_introduce2(circ, payload, sizeof(payload));
  992. tt_int_op(ret, OP_EQ, -1);
  993. expect_log_msg_containing("Received an INTRODUCE2 cell on a "
  994. "non introduction circuit of purpose");
  995. teardown_capture_of_logs();
  996. /* Back to normal. */
  997. TO_CIRCUIT(circ)->purpose = CIRCUIT_PURPOSE_S_INTRO;
  998. /* No service associated to it. */
  999. setup_full_capture_of_logs(LOG_WARN);
  1000. ret = hs_service_receive_introduce2(circ, payload, sizeof(payload));
  1001. tt_int_op(ret, OP_EQ, -1);
  1002. expect_log_msg_containing("Unknown service identity key");
  1003. teardown_capture_of_logs();
  1004. /* Set a service for this circuit. */
  1005. service = helper_create_service();
  1006. ed25519_pubkey_copy(&circ->hs_ident->identity_pk,
  1007. &service->keys.identity_pk);
  1008. /* No introduction point associated to it. */
  1009. setup_full_capture_of_logs(LOG_WARN);
  1010. ret = hs_service_receive_introduce2(circ, payload, sizeof(payload));
  1011. tt_int_op(ret, OP_EQ, -1);
  1012. expect_log_msg_containing("Unknown introduction auth key when handling "
  1013. "an INTRODUCE2 cell on circuit");
  1014. teardown_capture_of_logs();
  1015. /* Set an IP object now for this circuit. */
  1016. {
  1017. ip = helper_create_service_ip();
  1018. service_intro_point_add(service->desc_current->intro_points.map, ip);
  1019. /* Update ident to contain the intro point auth key. */
  1020. ed25519_pubkey_copy(&circ->hs_ident->intro_auth_pk,
  1021. &ip->auth_key_kp.pubkey);
  1022. }
  1023. /* This will fail because receiving an INTRODUCE2 cell implies a valid cell
  1024. * and then launching circuits so let's not do that and instead test that
  1025. * behaviour differently. */
  1026. ret = hs_service_receive_introduce2(circ, payload, sizeof(payload));
  1027. tt_int_op(ret, OP_EQ, -1);
  1028. tt_u64_op(ip->introduce2_count, OP_EQ, 0);
  1029. done:
  1030. or_state_free(dummy_state);
  1031. dummy_state = NULL;
  1032. if (circ)
  1033. circuit_free_(TO_CIRCUIT(circ));
  1034. hs_free_all();
  1035. UNMOCK(circuit_mark_for_close_);
  1036. }
  1037. /** Test basic hidden service housekeeping operations (maintaining intro
  1038. * points, etc) */
  1039. static void
  1040. test_service_event(void *arg)
  1041. {
  1042. int flags = CIRCLAUNCH_NEED_UPTIME | CIRCLAUNCH_IS_INTERNAL;
  1043. time_t now = time(NULL);
  1044. hs_service_t *service;
  1045. origin_circuit_t *circ = NULL;
  1046. (void) arg;
  1047. hs_init();
  1048. MOCK(circuit_mark_for_close_, mock_circuit_mark_for_close);
  1049. circ = helper_create_origin_circuit(CIRCUIT_PURPOSE_S_INTRO, flags);
  1050. /* Set a service for this circuit. */
  1051. service = helper_create_service();
  1052. ed25519_pubkey_copy(&circ->hs_ident->identity_pk,
  1053. &service->keys.identity_pk);
  1054. /* Currently this consists of cleaning invalid intro points. So adding IPs
  1055. * here that should get cleaned up. */
  1056. {
  1057. hs_service_intro_point_t *ip = helper_create_service_ip();
  1058. service_intro_point_add(service->desc_current->intro_points.map, ip);
  1059. /* This run will remove the IP because we have no circuits nor node_t
  1060. * associated with it. */
  1061. run_housekeeping_event(now);
  1062. tt_int_op(digest256map_size(service->desc_current->intro_points.map),
  1063. OP_EQ, 0);
  1064. /* We'll trigger a removal because we've reached our maximum amount of
  1065. * times we should retry a circuit. For this, we need to have a node_t
  1066. * that matches the identity of this IP. */
  1067. routerinfo_t ri;
  1068. memset(&ri, 0, sizeof(ri));
  1069. ip = helper_create_service_ip();
  1070. service_intro_point_add(service->desc_current->intro_points.map, ip);
  1071. memset(ri.cache_info.identity_digest, 'A', DIGEST_LEN);
  1072. /* This triggers a node_t creation. */
  1073. tt_assert(nodelist_set_routerinfo(&ri, NULL));
  1074. ip->circuit_retries = MAX_INTRO_POINT_CIRCUIT_RETRIES + 1;
  1075. run_housekeeping_event(now);
  1076. tt_int_op(digest256map_size(service->desc_current->intro_points.map),
  1077. OP_EQ, 0);
  1078. /* No removal but no circuit so this means the IP object will stay in the
  1079. * descriptor map so we can retry it. */
  1080. ip = helper_create_service_ip();
  1081. service_intro_point_add(service->desc_current->intro_points.map, ip);
  1082. ip->circuit_established = 1; /* We'll test that, it MUST be 0 after. */
  1083. run_housekeeping_event(now);
  1084. tt_int_op(digest256map_size(service->desc_current->intro_points.map),
  1085. OP_EQ, 1);
  1086. /* Remove the IP object at once for the next test. */
  1087. ip->circuit_retries = MAX_INTRO_POINT_CIRCUIT_RETRIES + 1;
  1088. run_housekeeping_event(now);
  1089. tt_int_op(digest256map_size(service->desc_current->intro_points.map),
  1090. OP_EQ, 0);
  1091. /* Now, we'll create an IP with a registered circuit. The IP object
  1092. * shouldn't go away. */
  1093. ip = helper_create_service_ip();
  1094. service_intro_point_add(service->desc_current->intro_points.map, ip);
  1095. ed25519_pubkey_copy(&circ->hs_ident->intro_auth_pk,
  1096. &ip->auth_key_kp.pubkey);
  1097. hs_circuitmap_register_intro_circ_v3_service_side(
  1098. circ, &ip->auth_key_kp.pubkey);
  1099. run_housekeeping_event(now);
  1100. tt_int_op(digest256map_size(service->desc_current->intro_points.map),
  1101. OP_EQ, 1);
  1102. /* We'll mangle the IP object to expire. */
  1103. ip->time_to_expire = now;
  1104. run_housekeeping_event(now);
  1105. tt_int_op(digest256map_size(service->desc_current->intro_points.map),
  1106. OP_EQ, 0);
  1107. }
  1108. done:
  1109. hs_circuitmap_remove_circuit(TO_CIRCUIT(circ));
  1110. circuit_free_(TO_CIRCUIT(circ));
  1111. hs_free_all();
  1112. UNMOCK(circuit_mark_for_close_);
  1113. }
  1114. /** Test that we rotate descriptors correctly. */
  1115. static void
  1116. test_rotate_descriptors(void *arg)
  1117. {
  1118. int ret;
  1119. time_t next_rotation_time, now;
  1120. hs_service_t *service;
  1121. hs_service_descriptor_t *desc_next;
  1122. (void) arg;
  1123. dummy_state = tor_malloc_zero(sizeof(or_state_t));
  1124. hs_init();
  1125. MOCK(get_or_state, get_or_state_replacement);
  1126. MOCK(circuit_mark_for_close_, mock_circuit_mark_for_close);
  1127. MOCK(networkstatus_get_live_consensus,
  1128. mock_networkstatus_get_live_consensus);
  1129. /* Descriptor rotation happens with a consensus with a new SRV. */
  1130. ret = parse_rfc1123_time("Sat, 26 Oct 1985 13:00:00 UTC",
  1131. &mock_ns.valid_after);
  1132. tt_int_op(ret, OP_EQ, 0);
  1133. ret = parse_rfc1123_time("Sat, 26 Oct 1985 14:00:00 UTC",
  1134. &mock_ns.fresh_until);
  1135. tt_int_op(ret, OP_EQ, 0);
  1136. voting_schedule_recalculate_timing(get_options(), mock_ns.valid_after);
  1137. update_approx_time(mock_ns.valid_after+1);
  1138. now = mock_ns.valid_after+1;
  1139. /* Create a service with a default descriptor and state. It's added to the
  1140. * global map. */
  1141. service = helper_create_service();
  1142. service_descriptor_free(service->desc_current);
  1143. service->desc_current = NULL;
  1144. /* This triggers a build for both descriptors. The time now is only used in
  1145. * the descriptor certificate which is important to be now else the decoding
  1146. * will complain that the cert has expired if we use valid_after. */
  1147. build_all_descriptors(now);
  1148. tt_assert(service->desc_current);
  1149. tt_assert(service->desc_next);
  1150. /* Tweak our service next rotation time so we can use a custom time. */
  1151. service->state.next_rotation_time = next_rotation_time =
  1152. mock_ns.valid_after + (11 * 60 * 60);
  1153. /* Nothing should happen, we are not at a new SRV. Our next rotation time
  1154. * should be untouched. */
  1155. rotate_all_descriptors(mock_ns.valid_after);
  1156. tt_u64_op(service->state.next_rotation_time, OP_EQ, next_rotation_time);
  1157. tt_assert(service->desc_current);
  1158. tt_assert(service->desc_next);
  1159. tt_u64_op(service->desc_current->time_period_num, OP_EQ,
  1160. hs_get_previous_time_period_num(0));
  1161. tt_u64_op(service->desc_next->time_period_num, OP_EQ,
  1162. hs_get_time_period_num(0));
  1163. /* Keep a reference so we can compare it after rotation to the current. */
  1164. desc_next = service->desc_next;
  1165. /* Going right after a new SRV. */
  1166. ret = parse_rfc1123_time("Sat, 27 Oct 1985 01:00:00 UTC",
  1167. &mock_ns.valid_after);
  1168. tt_int_op(ret, OP_EQ, 0);
  1169. ret = parse_rfc1123_time("Sat, 27 Oct 1985 02:00:00 UTC",
  1170. &mock_ns.fresh_until);
  1171. tt_int_op(ret, OP_EQ, 0);
  1172. voting_schedule_recalculate_timing(get_options(), mock_ns.valid_after);
  1173. update_approx_time(mock_ns.valid_after+1);
  1174. now = mock_ns.valid_after+1;
  1175. /* Note down what to expect for the next rotation time which is 01:00 + 23h
  1176. * meaning 00:00:00. */
  1177. next_rotation_time = mock_ns.valid_after + (23 * 60 * 60);
  1178. /* We should have our next rotation time modified, our current descriptor
  1179. * cleaned up and the next descriptor becoming the current. */
  1180. rotate_all_descriptors(mock_ns.valid_after);
  1181. tt_u64_op(service->state.next_rotation_time, OP_EQ, next_rotation_time);
  1182. tt_mem_op(service->desc_current, OP_EQ, desc_next, sizeof(*desc_next));
  1183. tt_assert(service->desc_next == NULL);
  1184. /* A second time should do nothing. */
  1185. rotate_all_descriptors(mock_ns.valid_after);
  1186. tt_u64_op(service->state.next_rotation_time, OP_EQ, next_rotation_time);
  1187. tt_mem_op(service->desc_current, OP_EQ, desc_next, sizeof(*desc_next));
  1188. tt_assert(service->desc_next == NULL);
  1189. build_all_descriptors(now);
  1190. tt_mem_op(service->desc_current, OP_EQ, desc_next, sizeof(*desc_next));
  1191. tt_u64_op(service->desc_current->time_period_num, OP_EQ,
  1192. hs_get_time_period_num(0));
  1193. tt_u64_op(service->desc_next->time_period_num, OP_EQ,
  1194. hs_get_next_time_period_num(0));
  1195. tt_assert(service->desc_next);
  1196. done:
  1197. hs_free_all();
  1198. UNMOCK(get_or_state);
  1199. UNMOCK(circuit_mark_for_close_);
  1200. UNMOCK(networkstatus_get_live_consensus);
  1201. }
  1202. /** Test building descriptors: picking intro points, setting up their link
  1203. * specifiers, etc. */
  1204. static void
  1205. test_build_update_descriptors(void *arg)
  1206. {
  1207. int ret;
  1208. node_t *node;
  1209. hs_service_t *service;
  1210. hs_service_intro_point_t *ip_cur, *ip_next;
  1211. routerinfo_t ri;
  1212. (void) arg;
  1213. hs_init();
  1214. MOCK(get_or_state,
  1215. get_or_state_replacement);
  1216. MOCK(networkstatus_get_live_consensus,
  1217. mock_networkstatus_get_live_consensus);
  1218. dummy_state = tor_malloc_zero(sizeof(or_state_t));
  1219. ret = parse_rfc1123_time("Sat, 26 Oct 1985 03:00:00 UTC",
  1220. &mock_ns.valid_after);
  1221. tt_int_op(ret, OP_EQ, 0);
  1222. ret = parse_rfc1123_time("Sat, 26 Oct 1985 04:00:00 UTC",
  1223. &mock_ns.fresh_until);
  1224. tt_int_op(ret, OP_EQ, 0);
  1225. voting_schedule_recalculate_timing(get_options(), mock_ns.valid_after);
  1226. update_approx_time(mock_ns.valid_after+1);
  1227. time_t now = mock_ns.valid_after+1;
  1228. /* Create a service without a current descriptor to trigger a build. */
  1229. service = helper_create_service();
  1230. tt_assert(service);
  1231. /* Unfortunately, the helper creates a dummy descriptor so get rid of it. */
  1232. service_descriptor_free(service->desc_current);
  1233. service->desc_current = NULL;
  1234. /* We have a fresh service so this should trigger a build for both
  1235. * descriptors for specific time period that we'll test. */
  1236. build_all_descriptors(now);
  1237. /* Check *current* descriptor. */
  1238. tt_assert(service->desc_current);
  1239. tt_assert(service->desc_current->desc);
  1240. tt_assert(service->desc_current->intro_points.map);
  1241. /* The current time period is the one expected when starting at 03:00. */
  1242. tt_u64_op(service->desc_current->time_period_num, OP_EQ,
  1243. hs_get_time_period_num(0));
  1244. /* This should be untouched, the update descriptor process changes it. */
  1245. tt_u64_op(service->desc_current->next_upload_time, OP_EQ, 0);
  1246. /* Check *next* descriptor. */
  1247. tt_assert(service->desc_next);
  1248. tt_assert(service->desc_next->desc);
  1249. tt_assert(service->desc_next->intro_points.map);
  1250. tt_assert(service->desc_current != service->desc_next);
  1251. tt_u64_op(service->desc_next->time_period_num, OP_EQ,
  1252. hs_get_next_time_period_num(0));
  1253. /* This should be untouched, the update descriptor process changes it. */
  1254. tt_u64_op(service->desc_next->next_upload_time, OP_EQ, 0);
  1255. /* Time to test the update of those descriptors. At first, we have no node
  1256. * in the routerlist so this will find NO suitable node for the IPs. */
  1257. setup_full_capture_of_logs(LOG_INFO);
  1258. update_all_descriptors_intro_points(now);
  1259. expect_log_msg_containing("Unable to find a suitable node to be an "
  1260. "introduction point for service");
  1261. teardown_capture_of_logs();
  1262. tt_int_op(digest256map_size(service->desc_current->intro_points.map),
  1263. OP_EQ, 0);
  1264. tt_int_op(digest256map_size(service->desc_next->intro_points.map),
  1265. OP_EQ, 0);
  1266. /* Now, we'll setup a node_t. */
  1267. {
  1268. tor_addr_t ipv4_addr;
  1269. curve25519_secret_key_t curve25519_secret_key;
  1270. memset(&ri, 0, sizeof(routerinfo_t));
  1271. tor_addr_parse(&ipv4_addr, "127.0.0.1");
  1272. ri.addr = tor_addr_to_ipv4h(&ipv4_addr);
  1273. ri.or_port = 1337;
  1274. ri.purpose = ROUTER_PURPOSE_GENERAL;
  1275. /* Ugly yes but we never free the "ri" object so this just makes things
  1276. * easier. */
  1277. ri.protocol_list = (char *) "HSDir=1-2 LinkAuth=3";
  1278. summarize_protover_flags(&ri.pv, ri.protocol_list, NULL);
  1279. ret = curve25519_secret_key_generate(&curve25519_secret_key, 0);
  1280. tt_int_op(ret, OP_EQ, 0);
  1281. ri.onion_curve25519_pkey =
  1282. tor_malloc_zero(sizeof(curve25519_public_key_t));
  1283. ri.onion_pkey = tor_malloc_zero(140);
  1284. curve25519_public_key_generate(ri.onion_curve25519_pkey,
  1285. &curve25519_secret_key);
  1286. memset(ri.cache_info.identity_digest, 'A', DIGEST_LEN);
  1287. /* Setup ed25519 identity */
  1288. ed25519_keypair_t kp1;
  1289. ed25519_keypair_generate(&kp1, 0);
  1290. ri.cache_info.signing_key_cert = tor_malloc_zero(sizeof(tor_cert_t));
  1291. tt_assert(ri.cache_info.signing_key_cert);
  1292. ed25519_pubkey_copy(&ri.cache_info.signing_key_cert->signing_key,
  1293. &kp1.pubkey);
  1294. nodelist_set_routerinfo(&ri, NULL);
  1295. node = node_get_mutable_by_id(ri.cache_info.identity_digest);
  1296. tt_assert(node);
  1297. node->is_running = node->is_valid = node->is_fast = node->is_stable = 1;
  1298. }
  1299. /* We have to set this, or the lack of microdescriptors for these
  1300. * nodes will make them unusable. */
  1301. get_options_mutable()->UseMicrodescriptors = 0;
  1302. /* We expect to pick only one intro point from the node above. */
  1303. setup_full_capture_of_logs(LOG_INFO);
  1304. update_all_descriptors_intro_points(now);
  1305. tor_free(node->ri->onion_curve25519_pkey); /* Avoid memleak. */
  1306. tor_free(node->ri->cache_info.signing_key_cert);
  1307. tor_free(node->ri->onion_pkey);
  1308. expect_log_msg_containing("just picked 1 intro points and wanted 3 for next "
  1309. "descriptor. It currently has 0 intro points. "
  1310. "Launching ESTABLISH_INTRO circuit shortly.");
  1311. teardown_capture_of_logs();
  1312. tt_int_op(digest256map_size(service->desc_current->intro_points.map),
  1313. OP_EQ, 1);
  1314. tt_int_op(digest256map_size(service->desc_next->intro_points.map),
  1315. OP_EQ, 1);
  1316. /* Get the IP object. Because we don't have the auth key of the IP, we can't
  1317. * query it so get the first element in the map. */
  1318. {
  1319. void *obj = NULL;
  1320. const uint8_t *key;
  1321. digest256map_iter_t *iter =
  1322. digest256map_iter_init(service->desc_current->intro_points.map);
  1323. digest256map_iter_get(iter, &key, &obj);
  1324. tt_assert(obj);
  1325. ip_cur = obj;
  1326. /* Get also the IP from the next descriptor. We'll make sure it's not the
  1327. * same object as in the current descriptor. */
  1328. iter = digest256map_iter_init(service->desc_next->intro_points.map);
  1329. digest256map_iter_get(iter, &key, &obj);
  1330. tt_assert(obj);
  1331. ip_next = obj;
  1332. }
  1333. tt_mem_op(ip_cur, OP_NE, ip_next, sizeof(hs_desc_intro_point_t));
  1334. /* We won't test the service IP object because there is a specific test
  1335. * already for this but we'll make sure that the state is coherent.*/
  1336. /* Three link specifiers are mandatoy so make sure we do have them. */
  1337. tt_int_op(smartlist_len(ip_cur->base.link_specifiers), OP_EQ, 3);
  1338. /* Make sure we have a valid encryption keypair generated when we pick an
  1339. * intro point in the update process. */
  1340. tt_assert(!tor_mem_is_zero((char *) ip_cur->enc_key_kp.seckey.secret_key,
  1341. CURVE25519_SECKEY_LEN));
  1342. tt_assert(!tor_mem_is_zero((char *) ip_cur->enc_key_kp.pubkey.public_key,
  1343. CURVE25519_PUBKEY_LEN));
  1344. tt_u64_op(ip_cur->time_to_expire, OP_GE, now +
  1345. INTRO_POINT_LIFETIME_MIN_SECONDS);
  1346. tt_u64_op(ip_cur->time_to_expire, OP_LE, now +
  1347. INTRO_POINT_LIFETIME_MAX_SECONDS);
  1348. /* Now, we will try to set up a service after a new time period has started
  1349. * and see if it behaves as expected. */
  1350. ret = parse_rfc1123_time("Sat, 26 Oct 1985 13:00:00 UTC",
  1351. &mock_ns.valid_after);
  1352. tt_int_op(ret, OP_EQ, 0);
  1353. ret = parse_rfc1123_time("Sat, 26 Oct 1985 14:00:00 UTC",
  1354. &mock_ns.fresh_until);
  1355. tt_int_op(ret, OP_EQ, 0);
  1356. update_approx_time(mock_ns.valid_after+1);
  1357. now = mock_ns.valid_after+1;
  1358. /* Create a service without a current descriptor to trigger a build. */
  1359. service = helper_create_service();
  1360. tt_assert(service);
  1361. /* Unfortunately, the helper creates a dummy descriptor so get rid of it. */
  1362. service_descriptor_free(service->desc_current);
  1363. service->desc_current = NULL;
  1364. /* We have a fresh service so this should trigger a build for both
  1365. * descriptors for specific time period that we'll test. */
  1366. build_all_descriptors(now);
  1367. /* Check *current* descriptor. */
  1368. tt_assert(service->desc_current);
  1369. tt_assert(service->desc_current->desc);
  1370. tt_assert(service->desc_current->intro_points.map);
  1371. /* This should be for the previous time period. */
  1372. tt_u64_op(service->desc_current->time_period_num, OP_EQ,
  1373. hs_get_previous_time_period_num(0));
  1374. /* This should be untouched, the update descriptor process changes it. */
  1375. tt_u64_op(service->desc_current->next_upload_time, OP_EQ, 0);
  1376. /* Check *next* descriptor. */
  1377. tt_assert(service->desc_next);
  1378. tt_assert(service->desc_next->desc);
  1379. tt_assert(service->desc_next->intro_points.map);
  1380. tt_assert(service->desc_current != service->desc_next);
  1381. tt_u64_op(service->desc_next->time_period_num, OP_EQ,
  1382. hs_get_time_period_num(0));
  1383. /* This should be untouched, the update descriptor process changes it. */
  1384. tt_u64_op(service->desc_next->next_upload_time, OP_EQ, 0);
  1385. /* Let's remove the next descriptor to simulate a rotation. */
  1386. service_descriptor_free(service->desc_next);
  1387. service->desc_next = NULL;
  1388. build_all_descriptors(now);
  1389. /* Check *next* descriptor. */
  1390. tt_assert(service->desc_next);
  1391. tt_assert(service->desc_next->desc);
  1392. tt_assert(service->desc_next->intro_points.map);
  1393. tt_assert(service->desc_current != service->desc_next);
  1394. tt_u64_op(service->desc_next->time_period_num, OP_EQ,
  1395. hs_get_next_time_period_num(0));
  1396. /* This should be untouched, the update descriptor process changes it. */
  1397. tt_u64_op(service->desc_next->next_upload_time, OP_EQ, 0);
  1398. done:
  1399. hs_free_all();
  1400. nodelist_free_all();
  1401. }
  1402. /** Test building descriptors. We use this separate function instead of
  1403. * using test_build_update_descriptors because that function is too complex
  1404. * and also too interactive. */
  1405. static void
  1406. test_build_descriptors(void *arg)
  1407. {
  1408. int ret;
  1409. time_t now = time(NULL);
  1410. hs_service_t *last_service = NULL;
  1411. (void) arg;
  1412. hs_init();
  1413. MOCK(get_or_state,
  1414. get_or_state_replacement);
  1415. MOCK(networkstatus_get_live_consensus,
  1416. mock_networkstatus_get_live_consensus);
  1417. dummy_state = tor_malloc_zero(sizeof(or_state_t));
  1418. ret = parse_rfc1123_time("Sat, 26 Oct 1985 03:00:00 UTC",
  1419. &mock_ns.valid_after);
  1420. tt_int_op(ret, OP_EQ, 0);
  1421. ret = parse_rfc1123_time("Sat, 26 Oct 1985 04:00:00 UTC",
  1422. &mock_ns.fresh_until);
  1423. tt_int_op(ret, OP_EQ, 0);
  1424. voting_schedule_recalculate_timing(get_options(), mock_ns.valid_after);
  1425. /* Generate a valid number of fake auth clients when a client authorization
  1426. * is disabled. */
  1427. {
  1428. hs_service_t *service = helper_create_service();
  1429. last_service = service;
  1430. service_descriptor_free(service->desc_current);
  1431. service->desc_current = NULL;
  1432. build_all_descriptors(now);
  1433. tt_assert(service->desc_current);
  1434. tt_assert(service->desc_current->desc);
  1435. hs_desc_superencrypted_data_t *superencrypted;
  1436. superencrypted = &service->desc_current->desc->superencrypted_data;
  1437. tt_int_op(smartlist_len(superencrypted->clients), OP_EQ, 16);
  1438. helper_destroy_service(service);
  1439. last_service = NULL;
  1440. }
  1441. /* Generate a valid number of fake auth clients when the number of
  1442. * clients is zero. */
  1443. {
  1444. hs_service_t *service = helper_create_service_with_clients(0);
  1445. last_service = service;
  1446. service_descriptor_free(service->desc_current);
  1447. service->desc_current = NULL;
  1448. build_all_descriptors(now);
  1449. hs_desc_superencrypted_data_t *superencrypted;
  1450. superencrypted = &service->desc_current->desc->superencrypted_data;
  1451. tt_int_op(smartlist_len(superencrypted->clients), OP_EQ, 16);
  1452. helper_destroy_service(service);
  1453. last_service = NULL;
  1454. }
  1455. /* Generate a valid number of fake auth clients when the number of
  1456. * clients is not a multiple of 16. */
  1457. {
  1458. hs_service_t *service = helper_create_service_with_clients(20);
  1459. last_service = service;
  1460. service_descriptor_free(service->desc_current);
  1461. service->desc_current = NULL;
  1462. build_all_descriptors(now);
  1463. hs_desc_superencrypted_data_t *superencrypted;
  1464. superencrypted = &service->desc_current->desc->superencrypted_data;
  1465. tt_int_op(smartlist_len(superencrypted->clients), OP_EQ, 32);
  1466. helper_destroy_service(service);
  1467. last_service = NULL;
  1468. }
  1469. /* Do not generate any fake desc client when the number of clients is
  1470. * a multiple of 16 but not zero. */
  1471. {
  1472. hs_service_t *service = helper_create_service_with_clients(32);
  1473. last_service = service;
  1474. service_descriptor_free(service->desc_current);
  1475. service->desc_current = NULL;
  1476. build_all_descriptors(now);
  1477. hs_desc_superencrypted_data_t *superencrypted;
  1478. superencrypted = &service->desc_current->desc->superencrypted_data;
  1479. tt_int_op(smartlist_len(superencrypted->clients), OP_EQ, 32);
  1480. helper_destroy_service(service);
  1481. last_service = NULL;
  1482. }
  1483. done:
  1484. helper_destroy_service(last_service);
  1485. hs_free_all();
  1486. }
  1487. static void
  1488. test_upload_descriptors(void *arg)
  1489. {
  1490. int ret;
  1491. time_t now;
  1492. hs_service_t *service;
  1493. (void) arg;
  1494. hs_init();
  1495. MOCK(get_or_state,
  1496. get_or_state_replacement);
  1497. MOCK(networkstatus_get_live_consensus,
  1498. mock_networkstatus_get_live_consensus);
  1499. dummy_state = tor_malloc_zero(sizeof(or_state_t));
  1500. ret = parse_rfc1123_time("Sat, 26 Oct 1985 13:00:00 UTC",
  1501. &mock_ns.valid_after);
  1502. tt_int_op(ret, OP_EQ, 0);
  1503. ret = parse_rfc1123_time("Sat, 26 Oct 1985 14:00:00 UTC",
  1504. &mock_ns.fresh_until);
  1505. tt_int_op(ret, OP_EQ, 0);
  1506. voting_schedule_recalculate_timing(get_options(), mock_ns.valid_after);
  1507. update_approx_time(mock_ns.valid_after+1);
  1508. now = mock_ns.valid_after+1;
  1509. /* Create a service with no descriptor. It's added to the global map. */
  1510. service = hs_service_new(get_options());
  1511. tt_assert(service);
  1512. service->config.version = HS_VERSION_THREE;
  1513. ed25519_secret_key_generate(&service->keys.identity_sk, 0);
  1514. ed25519_public_key_generate(&service->keys.identity_pk,
  1515. &service->keys.identity_sk);
  1516. /* Register service to global map. */
  1517. ret = register_service(get_hs_service_map(), service);
  1518. tt_int_op(ret, OP_EQ, 0);
  1519. /* But first, build our descriptor. */
  1520. build_all_descriptors(now);
  1521. /* Nothing should happen because we have 0 introduction circuit established
  1522. * and we want (by default) 3 intro points. */
  1523. run_upload_descriptor_event(now);
  1524. /* If no upload happened, this should be untouched. */
  1525. tt_u64_op(service->desc_current->next_upload_time, OP_EQ, 0);
  1526. /* We'll simulate that we've opened our intro point circuit and that we only
  1527. * want one intro point. */
  1528. service->config.num_intro_points = 1;
  1529. /* Set our next upload time after now which will skip the upload. */
  1530. service->desc_current->next_upload_time = now + 1000;
  1531. run_upload_descriptor_event(now);
  1532. /* If no upload happened, this should be untouched. */
  1533. tt_u64_op(service->desc_current->next_upload_time, OP_EQ, now + 1000);
  1534. done:
  1535. hs_free_all();
  1536. UNMOCK(get_or_state);
  1537. }
  1538. /** Global vars used by test_rendezvous1_parsing() */
  1539. static char rend1_payload[RELAY_PAYLOAD_SIZE];
  1540. static size_t rend1_payload_len = 0;
  1541. /** Mock for relay_send_command_from_edge() to send a RENDEZVOUS1 cell. Instead
  1542. * of sending it to the network, instead save it to the global `rend1_payload`
  1543. * variable so that we can inspect it in the test_rendezvous1_parsing()
  1544. * test. */
  1545. static int
  1546. mock_relay_send_rendezvous1(streamid_t stream_id, circuit_t *circ,
  1547. uint8_t relay_command, const char *payload,
  1548. size_t payload_len,
  1549. crypt_path_t *cpath_layer,
  1550. const char *filename, int lineno)
  1551. {
  1552. (void) stream_id;
  1553. (void) circ;
  1554. (void) relay_command;
  1555. (void) cpath_layer;
  1556. (void) filename;
  1557. (void) lineno;
  1558. memcpy(rend1_payload, payload, payload_len);
  1559. rend1_payload_len = payload_len;
  1560. return 0;
  1561. }
  1562. /** Send a RENDEZVOUS1 as a service, and parse it as a client. */
  1563. static void
  1564. test_rendezvous1_parsing(void *arg)
  1565. {
  1566. int retval;
  1567. static const char *test_addr =
  1568. "4acth47i6kxnvkewtm6q7ib2s3ufpo5sqbsnzjpbi7utijcltosqemad.onion";
  1569. hs_service_t *service = NULL;
  1570. origin_circuit_t *service_circ = NULL;
  1571. origin_circuit_t *client_circ = NULL;
  1572. ed25519_keypair_t ip_auth_kp;
  1573. curve25519_keypair_t ephemeral_kp;
  1574. curve25519_keypair_t client_kp;
  1575. curve25519_keypair_t ip_enc_kp;
  1576. int flags = CIRCLAUNCH_NEED_UPTIME | CIRCLAUNCH_IS_INTERNAL;
  1577. (void) arg;
  1578. MOCK(relay_send_command_from_edge_, mock_relay_send_rendezvous1);
  1579. {
  1580. /* Let's start by setting up the service that will start the rend */
  1581. service = tor_malloc_zero(sizeof(hs_service_t));
  1582. ed25519_secret_key_generate(&service->keys.identity_sk, 0);
  1583. ed25519_public_key_generate(&service->keys.identity_pk,
  1584. &service->keys.identity_sk);
  1585. memcpy(service->onion_address, test_addr, sizeof(service->onion_address));
  1586. tt_assert(service);
  1587. }
  1588. {
  1589. /* Now let's set up the service rendezvous circuit and its keys. */
  1590. service_circ = helper_create_origin_circuit(CIRCUIT_PURPOSE_S_CONNECT_REND,
  1591. flags);
  1592. tor_free(service_circ->hs_ident);
  1593. hs_ntor_rend_cell_keys_t hs_ntor_rend_cell_keys;
  1594. uint8_t rendezvous_cookie[HS_REND_COOKIE_LEN];
  1595. curve25519_keypair_generate(&ip_enc_kp, 0);
  1596. curve25519_keypair_generate(&ephemeral_kp, 0);
  1597. curve25519_keypair_generate(&client_kp, 0);
  1598. ed25519_keypair_generate(&ip_auth_kp, 0);
  1599. retval = hs_ntor_service_get_rendezvous1_keys(&ip_auth_kp.pubkey,
  1600. &ip_enc_kp,
  1601. &ephemeral_kp,
  1602. &client_kp.pubkey,
  1603. &hs_ntor_rend_cell_keys);
  1604. tt_int_op(retval, OP_EQ, 0);
  1605. memset(rendezvous_cookie, 2, sizeof(rendezvous_cookie));
  1606. service_circ->hs_ident =
  1607. create_rp_circuit_identifier(service, rendezvous_cookie,
  1608. &ephemeral_kp.pubkey,
  1609. &hs_ntor_rend_cell_keys);
  1610. }
  1611. /* Send out the RENDEZVOUS1 and make sure that our mock func worked */
  1612. tt_assert(tor_mem_is_zero(rend1_payload, 32));
  1613. hs_circ_service_rp_has_opened(service, service_circ);
  1614. tt_assert(!tor_mem_is_zero(rend1_payload, 32));
  1615. tt_int_op(rend1_payload_len, OP_EQ, HS_LEGACY_RENDEZVOUS_CELL_SIZE);
  1616. /******************************/
  1617. /** Now let's create the client rendezvous circuit */
  1618. client_circ =
  1619. helper_create_origin_circuit(CIRCUIT_PURPOSE_C_REND_READY_INTRO_ACKED,
  1620. flags);
  1621. /* fix up its circ ident */
  1622. ed25519_pubkey_copy(&client_circ->hs_ident->intro_auth_pk,
  1623. &ip_auth_kp.pubkey);
  1624. memcpy(&client_circ->hs_ident->rendezvous_client_kp,
  1625. &client_kp, sizeof(client_circ->hs_ident->rendezvous_client_kp));
  1626. memcpy(&client_circ->hs_ident->intro_enc_pk.public_key,
  1627. &ip_enc_kp.pubkey.public_key,
  1628. sizeof(client_circ->hs_ident->intro_enc_pk.public_key));
  1629. /* Now parse the rendezvous2 circuit and make sure it was fine. We are
  1630. * skipping 20 bytes off its payload, since that's the rendezvous cookie
  1631. * which is only present in REND1. */
  1632. retval = handle_rendezvous2(client_circ,
  1633. (uint8_t*)rend1_payload+20,
  1634. rend1_payload_len-20);
  1635. tt_int_op(retval, OP_EQ, 0);
  1636. /* TODO: We are only simulating client/service here. We could also simulate
  1637. * the rendezvous point by plugging in rend_mid_establish_rendezvous(). We
  1638. * would need an extra circuit and some more stuff but it's doable. */
  1639. done:
  1640. circuit_free_(TO_CIRCUIT(service_circ));
  1641. circuit_free_(TO_CIRCUIT(client_circ));
  1642. hs_service_free(service);
  1643. hs_free_all();
  1644. UNMOCK(relay_send_command_from_edge_);
  1645. }
  1646. static void
  1647. test_authorized_client_config_equal(void *arg)
  1648. {
  1649. int ret;
  1650. hs_service_config_t *config1, *config2;
  1651. (void) arg;
  1652. config1 = tor_malloc_zero(sizeof(*config1));
  1653. config2 = tor_malloc_zero(sizeof(*config2));
  1654. /* Both configs are empty. */
  1655. {
  1656. config1->clients = smartlist_new();
  1657. config2->clients = smartlist_new();
  1658. ret = service_authorized_client_config_equal(config1, config2);
  1659. tt_int_op(ret, OP_EQ, 1);
  1660. service_clear_config(config1);
  1661. service_clear_config(config2);
  1662. }
  1663. /* Both configs have exactly the same client config. */
  1664. {
  1665. config1->clients = smartlist_new();
  1666. config2->clients = smartlist_new();
  1667. hs_service_authorized_client_t *client1, *client2;
  1668. client1 = helper_create_authorized_client();
  1669. client2 = helper_create_authorized_client();
  1670. smartlist_add(config1->clients, client1);
  1671. smartlist_add(config1->clients, client2);
  1672. /* We should swap the order of clients here to test that the order
  1673. * does not matter. */
  1674. smartlist_add(config2->clients, helper_clone_authorized_client(client2));
  1675. smartlist_add(config2->clients, helper_clone_authorized_client(client1));
  1676. ret = service_authorized_client_config_equal(config1, config2);
  1677. tt_int_op(ret, OP_EQ, 1);
  1678. service_clear_config(config1);
  1679. service_clear_config(config2);
  1680. }
  1681. /* The numbers of clients in both configs are not equal. */
  1682. {
  1683. config1->clients = smartlist_new();
  1684. config2->clients = smartlist_new();
  1685. hs_service_authorized_client_t *client1, *client2;
  1686. client1 = helper_create_authorized_client();
  1687. client2 = helper_create_authorized_client();
  1688. smartlist_add(config1->clients, client1);
  1689. smartlist_add(config1->clients, client2);
  1690. smartlist_add(config2->clients, helper_clone_authorized_client(client1));
  1691. ret = service_authorized_client_config_equal(config1, config2);
  1692. tt_int_op(ret, OP_EQ, 0);
  1693. service_clear_config(config1);
  1694. service_clear_config(config2);
  1695. }
  1696. /* The first config has two distinct clients while the second config
  1697. * has two clients but they are duplicate. */
  1698. {
  1699. config1->clients = smartlist_new();
  1700. config2->clients = smartlist_new();
  1701. hs_service_authorized_client_t *client1, *client2;
  1702. client1 = helper_create_authorized_client();
  1703. client2 = helper_create_authorized_client();
  1704. smartlist_add(config1->clients, client1);
  1705. smartlist_add(config1->clients, client2);
  1706. smartlist_add(config2->clients, helper_clone_authorized_client(client1));
  1707. smartlist_add(config2->clients, helper_clone_authorized_client(client1));
  1708. ret = service_authorized_client_config_equal(config1, config2);
  1709. tt_int_op(ret, OP_EQ, 0);
  1710. service_clear_config(config1);
  1711. service_clear_config(config2);
  1712. }
  1713. /* Both configs have totally distinct clients. */
  1714. {
  1715. config1->clients = smartlist_new();
  1716. config2->clients = smartlist_new();
  1717. hs_service_authorized_client_t *client1, *client2, *client3, *client4;
  1718. client1 = helper_create_authorized_client();
  1719. client2 = helper_create_authorized_client();
  1720. client3 = helper_create_authorized_client();
  1721. client4 = helper_create_authorized_client();
  1722. smartlist_add(config1->clients, client1);
  1723. smartlist_add(config1->clients, client2);
  1724. smartlist_add(config2->clients, client3);
  1725. smartlist_add(config2->clients, client4);
  1726. ret = service_authorized_client_config_equal(config1, config2);
  1727. tt_int_op(ret, OP_EQ, 0);
  1728. service_clear_config(config1);
  1729. service_clear_config(config2);
  1730. }
  1731. done:
  1732. tor_free(config1);
  1733. tor_free(config2);
  1734. }
  1735. /** Test that client circuit ID gets correctly exported */
  1736. static void
  1737. test_export_client_circuit_id(void *arg)
  1738. {
  1739. origin_circuit_t *or_circ = NULL;
  1740. size_t sz;
  1741. char *cp1=NULL, *cp2=NULL;
  1742. connection_t *conn = NULL;
  1743. (void) arg;
  1744. MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
  1745. hs_service_init();
  1746. /* Create service */
  1747. hs_service_t *service = helper_create_service();
  1748. /* Check that export circuit ID detection works */
  1749. service->config.circuit_id_protocol = HS_CIRCUIT_ID_PROTOCOL_NONE;
  1750. tt_int_op(0, OP_EQ,
  1751. hs_service_exports_circuit_id(&service->keys.identity_pk));
  1752. service->config.circuit_id_protocol = HS_CIRCUIT_ID_PROTOCOL_HAPROXY;
  1753. tt_int_op(1, OP_EQ,
  1754. hs_service_exports_circuit_id(&service->keys.identity_pk));
  1755. /* Create client connection */
  1756. conn = test_conn_get_connection(AP_CONN_STATE_CIRCUIT_WAIT, CONN_TYPE_AP, 0);
  1757. /* Create client edge conn hs_ident */
  1758. edge_connection_t *edge_conn = TO_EDGE_CONN(conn);
  1759. edge_conn->hs_ident = hs_ident_edge_conn_new(&service->keys.identity_pk);
  1760. edge_conn->hs_ident->orig_virtual_port = 42;
  1761. /* Create rend circuit */
  1762. or_circ = origin_circuit_new();
  1763. or_circ->base_.purpose = CIRCUIT_PURPOSE_C_REND_JOINED;
  1764. edge_conn->on_circuit = TO_CIRCUIT(or_circ);
  1765. or_circ->global_identifier = 666;
  1766. /* Export circuit ID */
  1767. export_hs_client_circuit_id(edge_conn, service->config.circuit_id_protocol);
  1768. /* Check contents */
  1769. cp1 = buf_get_contents(conn->outbuf, &sz);
  1770. tt_str_op(cp1, OP_EQ,
  1771. "PROXY TCP6 fc00:dead:beef:4dad::0:29a ::1 666 42\r\n");
  1772. /* Change circ GID and see that the reported circuit ID also changes */
  1773. or_circ->global_identifier = 22;
  1774. /* check changes */
  1775. export_hs_client_circuit_id(edge_conn, service->config.circuit_id_protocol);
  1776. cp2 = buf_get_contents(conn->outbuf, &sz);
  1777. tt_str_op(cp1, OP_NE, cp2);
  1778. tor_free(cp1);
  1779. /* Check that GID with UINT32_MAX works. */
  1780. or_circ->global_identifier = UINT32_MAX;
  1781. export_hs_client_circuit_id(edge_conn, service->config.circuit_id_protocol);
  1782. cp1 = buf_get_contents(conn->outbuf, &sz);
  1783. tt_str_op(cp1, OP_EQ,
  1784. "PROXY TCP6 fc00:dead:beef:4dad::ffff:ffff ::1 65535 42\r\n");
  1785. tor_free(cp1);
  1786. /* Check that GID with UINT16_MAX works. */
  1787. or_circ->global_identifier = UINT16_MAX;
  1788. export_hs_client_circuit_id(edge_conn, service->config.circuit_id_protocol);
  1789. cp1 = buf_get_contents(conn->outbuf, &sz);
  1790. tt_str_op(cp1, OP_EQ,
  1791. "PROXY TCP6 fc00:dead:beef:4dad::0:ffff ::1 65535 42\r\n");
  1792. tor_free(cp1);
  1793. /* Check that GID with UINT16_MAX + 7 works. */
  1794. or_circ->global_identifier = UINT16_MAX + 7;
  1795. export_hs_client_circuit_id(edge_conn, service->config.circuit_id_protocol);
  1796. cp1 = buf_get_contents(conn->outbuf, &sz);
  1797. tt_str_op(cp1, OP_EQ, "PROXY TCP6 fc00:dead:beef:4dad::1:6 ::1 6 42\r\n");
  1798. done:
  1799. UNMOCK(connection_write_to_buf_impl_);
  1800. circuit_free_(TO_CIRCUIT(or_circ));
  1801. connection_free_minimal(conn);
  1802. hs_service_free(service);
  1803. tor_free(cp1);
  1804. tor_free(cp2);
  1805. }
  1806. struct testcase_t hs_service_tests[] = {
  1807. { "e2e_rend_circuit_setup", test_e2e_rend_circuit_setup, TT_FORK,
  1808. NULL, NULL },
  1809. { "load_keys", test_load_keys, TT_FORK,
  1810. NULL, NULL },
  1811. { "client_filename_is_valid", test_client_filename_is_valid, TT_FORK,
  1812. NULL, NULL },
  1813. { "parse_authorized_client", test_parse_authorized_client, TT_FORK,
  1814. NULL, NULL },
  1815. { "load_keys_with_client_auth", test_load_keys_with_client_auth, TT_FORK,
  1816. NULL, NULL },
  1817. { "access_service", test_access_service, TT_FORK,
  1818. NULL, NULL },
  1819. { "service_intro_point", test_service_intro_point, TT_FORK,
  1820. NULL, NULL },
  1821. { "helper_functions", test_helper_functions, TT_FORK,
  1822. NULL, NULL },
  1823. { "intro_circuit_opened", test_intro_circuit_opened, TT_FORK,
  1824. NULL, NULL },
  1825. { "intro_established", test_intro_established, TT_FORK,
  1826. NULL, NULL },
  1827. { "closing_intro_circs", test_closing_intro_circs, TT_FORK,
  1828. NULL, NULL },
  1829. { "rdv_circuit_opened", test_rdv_circuit_opened, TT_FORK,
  1830. NULL, NULL },
  1831. { "introduce2", test_introduce2, TT_FORK,
  1832. NULL, NULL },
  1833. { "service_event", test_service_event, TT_FORK,
  1834. NULL, NULL },
  1835. { "rotate_descriptors", test_rotate_descriptors, TT_FORK,
  1836. NULL, NULL },
  1837. { "build_update_descriptors", test_build_update_descriptors, TT_FORK,
  1838. NULL, NULL },
  1839. { "build_descriptors", test_build_descriptors, TT_FORK,
  1840. NULL, NULL },
  1841. { "upload_descriptors", test_upload_descriptors, TT_FORK,
  1842. NULL, NULL },
  1843. { "rendezvous1_parsing", test_rendezvous1_parsing, TT_FORK,
  1844. NULL, NULL },
  1845. { "authorized_client_config_equal", test_authorized_client_config_equal,
  1846. TT_FORK, NULL, NULL },
  1847. { "export_client_circuit_id", test_export_client_circuit_id, TT_FORK,
  1848. NULL, NULL },
  1849. END_OF_TESTCASES
  1850. };