test_hs_client.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603
  1. /* Copyright (c) 2016-2017, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. /**
  4. * \file test_hs_client.c
  5. * \brief Test prop224 HS client functionality.
  6. */
  7. #define CRYPTO_PRIVATE
  8. #define MAIN_PRIVATE
  9. #define HS_CLIENT_PRIVATE
  10. #define TOR_CHANNEL_INTERNAL_
  11. #define CIRCUITBUILD_PRIVATE
  12. #define CIRCUITLIST_PRIVATE
  13. #define CONNECTION_PRIVATE
  14. #include "test.h"
  15. #include "test_helpers.h"
  16. #include "log_test_helpers.h"
  17. #include "rend_test_helpers.h"
  18. #include "hs_test_helpers.h"
  19. #include "config.h"
  20. #include "crypto.h"
  21. #include "channeltls.h"
  22. #include "main.h"
  23. #include "nodelist.h"
  24. #include "routerset.h"
  25. #include "hs_circuit.h"
  26. #include "hs_client.h"
  27. #include "hs_ident.h"
  28. #include "hs_cache.h"
  29. #include "circuitlist.h"
  30. #include "circuitbuild.h"
  31. #include "connection.h"
  32. #include "connection_edge.h"
  33. #include "networkstatus.h"
  34. static int
  35. mock_connection_ap_handshake_send_begin(entry_connection_t *ap_conn)
  36. {
  37. (void) ap_conn;
  38. return 0;
  39. }
  40. static networkstatus_t mock_ns;
  41. /* Always return NULL. */
  42. static networkstatus_t *
  43. mock_networkstatus_get_live_consensus_false(time_t now)
  44. {
  45. (void) now;
  46. return NULL;
  47. }
  48. static networkstatus_t *
  49. mock_networkstatus_get_live_consensus(time_t now)
  50. {
  51. (void) now;
  52. return &mock_ns;
  53. }
  54. /* Test helper function: Setup a circuit and a stream with the same hidden
  55. * service destination, and put them in <b>circ_out</b> and
  56. * <b>conn_out</b>. Make the stream wait for circuits to be established to the
  57. * hidden service. */
  58. static int
  59. helper_get_circ_and_stream_for_test(origin_circuit_t **circ_out,
  60. connection_t **conn_out,
  61. int is_legacy)
  62. {
  63. int retval;
  64. channel_tls_t *n_chan=NULL;
  65. rend_data_t *conn_rend_data = NULL;
  66. origin_circuit_t *or_circ = NULL;
  67. connection_t *conn = NULL;
  68. ed25519_public_key_t service_pk;
  69. /* Make a dummy connection stream and make it wait for our circuit */
  70. conn = test_conn_get_connection(AP_CONN_STATE_CIRCUIT_WAIT,
  71. CONN_TYPE_AP /* ??? */,
  72. 0);
  73. if (is_legacy) {
  74. /* Legacy: Setup rend_data of stream */
  75. char service_id[REND_SERVICE_ID_LEN_BASE32+1] = {0};
  76. TO_EDGE_CONN(conn)->rend_data = mock_rend_data(service_id);
  77. conn_rend_data = TO_EDGE_CONN(conn)->rend_data;
  78. } else {
  79. /* prop224: Setup hs conn identifier on the stream */
  80. ed25519_secret_key_t sk;
  81. tt_int_op(0, OP_EQ, ed25519_secret_key_generate(&sk, 0));
  82. tt_int_op(0, OP_EQ, ed25519_public_key_generate(&service_pk, &sk));
  83. /* Setup hs_conn_identifier of stream */
  84. TO_EDGE_CONN(conn)->hs_ident = hs_ident_edge_conn_new(&service_pk);
  85. }
  86. /* Make it wait for circuit */
  87. connection_ap_mark_as_pending_circuit(TO_ENTRY_CONN(conn));
  88. /* This is needed to silence a BUG warning from
  89. connection_edge_update_circuit_isolation() */
  90. TO_ENTRY_CONN(conn)->original_dest_address =
  91. tor_strdup(TO_ENTRY_CONN(conn)->socks_request->address);
  92. /****************************************************/
  93. /* Now make dummy circuit */
  94. or_circ = origin_circuit_new();
  95. or_circ->base_.purpose = CIRCUIT_PURPOSE_C_REND_READY_INTRO_ACKED;
  96. or_circ->build_state = tor_malloc_zero(sizeof(cpath_build_state_t));
  97. or_circ->build_state->is_internal = 1;
  98. if (is_legacy) {
  99. /* Legacy: Setup rend data and final cpath */
  100. or_circ->build_state->pending_final_cpath =
  101. tor_malloc_zero(sizeof(crypt_path_t));
  102. or_circ->build_state->pending_final_cpath->magic = CRYPT_PATH_MAGIC;
  103. or_circ->build_state->pending_final_cpath->rend_dh_handshake_state =
  104. crypto_dh_new(DH_TYPE_REND);
  105. tt_assert(
  106. or_circ->build_state->pending_final_cpath->rend_dh_handshake_state);
  107. retval = crypto_dh_generate_public(
  108. or_circ->build_state->pending_final_cpath->rend_dh_handshake_state);
  109. tt_int_op(retval, OP_EQ, 0);
  110. or_circ->rend_data = rend_data_dup(conn_rend_data);
  111. } else {
  112. /* prop224: Setup hs ident on the circuit */
  113. or_circ->hs_ident = hs_ident_circuit_new(&service_pk,
  114. HS_IDENT_CIRCUIT_RENDEZVOUS);
  115. }
  116. TO_CIRCUIT(or_circ)->state = CIRCUIT_STATE_OPEN;
  117. /* fake n_chan */
  118. n_chan = tor_malloc_zero(sizeof(channel_tls_t));
  119. n_chan->base_.global_identifier = 1;
  120. or_circ->base_.n_chan = &(n_chan->base_);
  121. *circ_out = or_circ;
  122. *conn_out = conn;
  123. return 0;
  124. done:
  125. /* something failed */
  126. return -1;
  127. }
  128. /* Test: Ensure that setting up legacy e2e rendezvous circuits works
  129. * correctly. */
  130. static void
  131. test_e2e_rend_circuit_setup_legacy(void *arg)
  132. {
  133. ssize_t retval;
  134. origin_circuit_t *or_circ = NULL;
  135. connection_t *conn = NULL;
  136. (void) arg;
  137. /** In this test we create a v2 legacy HS stream and a circuit with the same
  138. * hidden service destination. We make the stream wait for circuits to be
  139. * established to the hidden service, and then we complete the circuit using
  140. * the hs_circuit_setup_e2e_rend_circ_legacy_client() function. We then
  141. * check that the end-to-end cpath was setup correctly and that the stream
  142. * was attached to the circuit as expected. */
  143. MOCK(connection_ap_handshake_send_begin,
  144. mock_connection_ap_handshake_send_begin);
  145. /* Setup */
  146. retval = helper_get_circ_and_stream_for_test( &or_circ, &conn, 1);
  147. tt_int_op(retval, OP_EQ, 0);
  148. tt_assert(or_circ);
  149. tt_assert(conn);
  150. /* Check number of hops */
  151. retval = cpath_get_n_hops(&or_circ->cpath);
  152. tt_int_op(retval, OP_EQ, 0);
  153. /* Check that our stream is not attached on any circuits */
  154. tt_ptr_op(TO_EDGE_CONN(conn)->on_circuit, OP_EQ, NULL);
  155. /********************************************** */
  156. /* Make a good RENDEZVOUS1 cell body because it needs to pass key exchange
  157. * digest verification... */
  158. uint8_t rend_cell_body[DH_KEY_LEN+DIGEST_LEN] = {2};
  159. {
  160. char keys[DIGEST_LEN+CPATH_KEY_MATERIAL_LEN];
  161. crypto_dh_t *dh_state =
  162. or_circ->build_state->pending_final_cpath->rend_dh_handshake_state;
  163. /* compute and overwrite digest of cell body with the right value */
  164. retval = crypto_dh_compute_secret(LOG_PROTOCOL_WARN, dh_state,
  165. (char*)rend_cell_body, DH_KEY_LEN,
  166. keys, DIGEST_LEN+CPATH_KEY_MATERIAL_LEN);
  167. tt_int_op(retval, OP_GT, 0);
  168. memcpy(rend_cell_body+DH_KEY_LEN, keys, DIGEST_LEN);
  169. }
  170. /* Setup the circuit */
  171. retval = hs_circuit_setup_e2e_rend_circ_legacy_client(or_circ,
  172. rend_cell_body);
  173. tt_int_op(retval, OP_EQ, 0);
  174. /**********************************************/
  175. /* See that a hop was added to the circuit's cpath */
  176. retval = cpath_get_n_hops(&or_circ->cpath);
  177. tt_int_op(retval, OP_EQ, 1);
  178. /* Check the digest algo */
  179. tt_int_op(crypto_digest_get_algorithm(or_circ->cpath->crypto.f_digest),
  180. OP_EQ, DIGEST_SHA1);
  181. tt_int_op(crypto_digest_get_algorithm(or_circ->cpath->crypto.b_digest),
  182. OP_EQ, DIGEST_SHA1);
  183. tt_assert(or_circ->cpath->crypto.f_crypto);
  184. tt_assert(or_circ->cpath->crypto.b_crypto);
  185. /* Ensure that circ purpose was changed */
  186. tt_int_op(or_circ->base_.purpose, OP_EQ, CIRCUIT_PURPOSE_C_REND_JOINED);
  187. /* Test that stream got attached */
  188. tt_ptr_op(TO_EDGE_CONN(conn)->on_circuit, OP_EQ, TO_CIRCUIT(or_circ));
  189. done:
  190. connection_free_minimal(conn);
  191. if (or_circ)
  192. tor_free(TO_CIRCUIT(or_circ)->n_chan);
  193. circuit_free_(TO_CIRCUIT(or_circ));
  194. }
  195. /* Test: Ensure that setting up v3 rendezvous circuits works correctly. */
  196. static void
  197. test_e2e_rend_circuit_setup(void *arg)
  198. {
  199. uint8_t ntor_key_seed[DIGEST256_LEN] = {0};
  200. origin_circuit_t *or_circ = NULL;
  201. int retval;
  202. connection_t *conn = NULL;
  203. (void) arg;
  204. /** In this test we create a prop224 v3 HS stream and a circuit with the same
  205. * hidden service destination. We make the stream wait for circuits to be
  206. * established to the hidden service, and then we complete the circuit using
  207. * the hs_circuit_setup_e2e_rend_circ() function. We then check that the
  208. * end-to-end cpath was setup correctly and that the stream was attached to
  209. * the circuit as expected. */
  210. MOCK(connection_ap_handshake_send_begin,
  211. mock_connection_ap_handshake_send_begin);
  212. /* Setup */
  213. retval = helper_get_circ_and_stream_for_test( &or_circ, &conn, 0);
  214. tt_int_op(retval, OP_EQ, 0);
  215. tt_assert(or_circ);
  216. tt_assert(conn);
  217. /* Check number of hops: There should be no hops yet to this circ */
  218. retval = cpath_get_n_hops(&or_circ->cpath);
  219. tt_int_op(retval, OP_EQ, 0);
  220. tt_ptr_op(or_circ->cpath, OP_EQ, NULL);
  221. /* Check that our stream is not attached on any circuits */
  222. tt_ptr_op(TO_EDGE_CONN(conn)->on_circuit, OP_EQ, NULL);
  223. /**********************************************/
  224. /* Setup the circuit */
  225. retval = hs_circuit_setup_e2e_rend_circ(or_circ,
  226. ntor_key_seed, sizeof(ntor_key_seed),
  227. 0);
  228. tt_int_op(retval, OP_EQ, 0);
  229. /**********************************************/
  230. /* See that a hop was added to the circuit's cpath */
  231. retval = cpath_get_n_hops(&or_circ->cpath);
  232. tt_int_op(retval, OP_EQ, 1);
  233. /* Check that the crypt path has prop224 algorithm parameters */
  234. tt_int_op(crypto_digest_get_algorithm(or_circ->cpath->crypto.f_digest),
  235. OP_EQ, DIGEST_SHA3_256);
  236. tt_int_op(crypto_digest_get_algorithm(or_circ->cpath->crypto.b_digest),
  237. OP_EQ, DIGEST_SHA3_256);
  238. tt_assert(or_circ->cpath->crypto.f_crypto);
  239. tt_assert(or_circ->cpath->crypto.b_crypto);
  240. /* Ensure that circ purpose was changed */
  241. tt_int_op(or_circ->base_.purpose, OP_EQ, CIRCUIT_PURPOSE_C_REND_JOINED);
  242. /* Test that stream got attached */
  243. tt_ptr_op(TO_EDGE_CONN(conn)->on_circuit, OP_EQ, TO_CIRCUIT(or_circ));
  244. done:
  245. connection_free_minimal(conn);
  246. if (or_circ)
  247. tor_free(TO_CIRCUIT(or_circ)->n_chan);
  248. circuit_free_(TO_CIRCUIT(or_circ));
  249. }
  250. /** Test client logic for picking intro points from a descriptor. Also test how
  251. * ExcludeNodes and intro point failures affect picking intro points. */
  252. static void
  253. test_client_pick_intro(void *arg)
  254. {
  255. int ret;
  256. ed25519_keypair_t service_kp;
  257. hs_descriptor_t *desc = NULL;
  258. MOCK(networkstatus_get_live_consensus,
  259. mock_networkstatus_get_live_consensus);
  260. (void) arg;
  261. hs_init();
  262. /* Generate service keypair */
  263. tt_int_op(0, OP_EQ, ed25519_keypair_generate(&service_kp, 0));
  264. /* Set time */
  265. ret = parse_rfc1123_time("Sat, 26 Oct 1985 13:00:00 UTC",
  266. &mock_ns.valid_after);
  267. tt_int_op(ret, OP_EQ, 0);
  268. ret = parse_rfc1123_time("Sat, 26 Oct 1985 14:00:00 UTC",
  269. &mock_ns.fresh_until);
  270. tt_int_op(ret, OP_EQ, 0);
  271. update_approx_time(mock_ns.fresh_until-10);
  272. time_t now = approx_time();
  273. /* Test logic:
  274. *
  275. * 1) Add our desc with intro points to the HS cache.
  276. *
  277. * 2) Mark all descriptor intro points except _the chosen one_ as
  278. * failed. Then query the desc to get a random intro: check that we got
  279. * _the chosen one_. Then fail the chosen one as well, and see that no
  280. * intros are returned.
  281. *
  282. * 3) Then clean the intro state cache and get an intro point.
  283. *
  284. * 4) Try fetching an intro with the wrong service key: shouldn't work
  285. *
  286. * 5) Set StrictNodes and put all our intro points in ExcludeNodes: see that
  287. * nothing is returned.
  288. */
  289. /* 1) Add desc to HS cache */
  290. {
  291. char *encoded = NULL;
  292. desc = hs_helper_build_hs_desc_with_ip(&service_kp);
  293. ret = hs_desc_encode_descriptor(desc, &service_kp, &encoded);
  294. tt_int_op(ret, OP_EQ, 0);
  295. tt_assert(encoded);
  296. /* store it */
  297. hs_cache_store_as_client(encoded, &service_kp.pubkey);
  298. /* fetch it to make sure it works */
  299. const hs_descriptor_t *fetched_desc =
  300. hs_cache_lookup_as_client(&service_kp.pubkey);
  301. tt_assert(fetched_desc);
  302. tt_mem_op(fetched_desc->subcredential, OP_EQ, desc->subcredential,
  303. DIGEST256_LEN);
  304. tt_assert(!tor_mem_is_zero((char*)fetched_desc->subcredential,
  305. DIGEST256_LEN));
  306. tor_free(encoded);
  307. }
  308. /* 2) Mark all intro points except _the chosen one_ as failed. Then query the
  309. * desc and get a random intro: check that we got _the chosen one_. */
  310. {
  311. /* Pick the chosen intro point and get its ei */
  312. hs_desc_intro_point_t *chosen_intro_point =
  313. smartlist_get(desc->encrypted_data.intro_points, 0);
  314. extend_info_t *chosen_intro_ei =
  315. desc_intro_point_to_extend_info(chosen_intro_point);
  316. tt_assert(chosen_intro_point);
  317. tt_assert(chosen_intro_ei);
  318. /* Now mark all other intro points as failed */
  319. SMARTLIST_FOREACH_BEGIN(desc->encrypted_data.intro_points,
  320. hs_desc_intro_point_t *, ip) {
  321. /* Skip the chosen intro point */
  322. if (ip == chosen_intro_point) {
  323. continue;
  324. }
  325. ed25519_public_key_t *intro_auth_key = &ip->auth_key_cert->signed_key;
  326. hs_cache_client_intro_state_note(&service_kp.pubkey,
  327. intro_auth_key,
  328. INTRO_POINT_FAILURE_GENERIC);
  329. } SMARTLIST_FOREACH_END(ip);
  330. /* Try to get a random intro: Should return the chosen one! */
  331. /* (We try several times, to make sure this behavior is consistent, and to
  332. * cover the different cases of client_get_random_intro().) */
  333. for (int i = 0; i < 64; ++i) {
  334. extend_info_t *ip = client_get_random_intro(&service_kp.pubkey);
  335. tor_assert(ip);
  336. tt_assert(!tor_mem_is_zero((char*)ip->identity_digest, DIGEST_LEN));
  337. tt_mem_op(ip->identity_digest, OP_EQ, chosen_intro_ei->identity_digest,
  338. DIGEST_LEN);
  339. extend_info_free(ip);
  340. }
  341. extend_info_free(chosen_intro_ei);
  342. /* Now also mark the chosen one as failed: See that we can't get any intro
  343. points anymore. */
  344. hs_cache_client_intro_state_note(&service_kp.pubkey,
  345. &chosen_intro_point->auth_key_cert->signed_key,
  346. INTRO_POINT_FAILURE_TIMEOUT);
  347. extend_info_t *ip = client_get_random_intro(&service_kp.pubkey);
  348. tor_assert(!ip);
  349. }
  350. /* 3) Clean the intro state cache and get an intro point */
  351. {
  352. /* Pretend we are 5 mins in the future and order a cleanup of the intro
  353. * state. This should clean up the intro point failures and allow us to get
  354. * an intro. */
  355. hs_cache_client_intro_state_clean(now + 5*60);
  356. /* Get an intro. It should work! */
  357. extend_info_t *ip = client_get_random_intro(&service_kp.pubkey);
  358. tor_assert(ip);
  359. extend_info_free(ip);
  360. }
  361. /* 4) Try fetching an intro with the wrong service key: shouldn't work */
  362. {
  363. ed25519_keypair_t dummy_kp;
  364. tt_int_op(0, OP_EQ, ed25519_keypair_generate(&dummy_kp, 0));
  365. extend_info_t *ip = client_get_random_intro(&dummy_kp.pubkey);
  366. tor_assert(!ip);
  367. }
  368. /* 5) Set StrictNodes and put all our intro points in ExcludeNodes: see that
  369. * nothing is returned. */
  370. {
  371. get_options_mutable()->ExcludeNodes = routerset_new();
  372. get_options_mutable()->StrictNodes = 1;
  373. SMARTLIST_FOREACH_BEGIN(desc->encrypted_data.intro_points,
  374. hs_desc_intro_point_t *, ip) {
  375. extend_info_t *intro_ei = desc_intro_point_to_extend_info(ip);
  376. if (intro_ei) {
  377. const char *ptr;
  378. char ip_addr[TOR_ADDR_BUF_LEN];
  379. /* We need to decorate in case it is an IPv6 else routerset_parse()
  380. * doesn't like it. */
  381. ptr = tor_addr_to_str(ip_addr, &intro_ei->addr, sizeof(ip_addr), 1);
  382. tt_assert(ptr == ip_addr);
  383. ret = routerset_parse(get_options_mutable()->ExcludeNodes,
  384. ip_addr, "");
  385. tt_int_op(ret, OP_EQ, 0);
  386. extend_info_free(intro_ei);
  387. }
  388. } SMARTLIST_FOREACH_END(ip);
  389. extend_info_t *ip = client_get_random_intro(&service_kp.pubkey);
  390. tt_assert(!ip);
  391. }
  392. done:
  393. hs_descriptor_free(desc);
  394. }
  395. static int
  396. mock_router_have_minimum_dir_info_false(void)
  397. {
  398. return 0;
  399. }
  400. static int
  401. mock_router_have_minimum_dir_info_true(void)
  402. {
  403. return 1;
  404. }
  405. static hs_client_fetch_status_t
  406. mock_fetch_v3_desc_error(const ed25519_public_key_t *key)
  407. {
  408. (void) key;
  409. return HS_CLIENT_FETCH_ERROR;
  410. }
  411. static void
  412. mock_connection_mark_unattached_ap_(entry_connection_t *conn, int endreason,
  413. int line, const char *file)
  414. {
  415. (void) line;
  416. (void) file;
  417. conn->edge_.end_reason = endreason;
  418. }
  419. static void
  420. test_descriptor_fetch(void *arg)
  421. {
  422. int ret;
  423. entry_connection_t *ec = NULL;
  424. ed25519_public_key_t service_pk;
  425. ed25519_secret_key_t service_sk;
  426. (void) arg;
  427. hs_init();
  428. memset(&service_sk, 'A', sizeof(service_sk));
  429. ret = ed25519_public_key_generate(&service_pk, &service_sk);
  430. tt_int_op(ret, OP_EQ, 0);
  431. /* Initialize this so get_voting_interval() doesn't freak out. */
  432. ret = parse_rfc1123_time("Sat, 26 Oct 1985 13:00:00 UTC",
  433. &mock_ns.valid_after);
  434. tt_int_op(ret, OP_EQ, 0);
  435. ret = parse_rfc1123_time("Sat, 26 Oct 1985 14:00:00 UTC",
  436. &mock_ns.fresh_until);
  437. tt_int_op(ret, OP_EQ, 0);
  438. ec = entry_connection_new(CONN_TYPE_AP, AF_INET);
  439. tt_assert(ec);
  440. ENTRY_TO_EDGE_CONN(ec)->hs_ident = hs_ident_edge_conn_new(&service_pk);
  441. tt_assert(ENTRY_TO_EDGE_CONN(ec)->hs_ident);
  442. TO_CONN(ENTRY_TO_EDGE_CONN(ec))->state = AP_CONN_STATE_RENDDESC_WAIT;
  443. smartlist_add(get_connection_array(), &ec->edge_.base_);
  444. /* 1. FetchHidServDescriptors is false so we shouldn't be able to fetch. */
  445. get_options_mutable()->FetchHidServDescriptors = 0;
  446. ret = hs_client_refetch_hsdesc(&service_pk);
  447. tt_int_op(ret, OP_EQ, HS_CLIENT_FETCH_NOT_ALLOWED);
  448. get_options_mutable()->FetchHidServDescriptors = 1;
  449. /* 2. We don't have a live consensus. */
  450. MOCK(networkstatus_get_live_consensus,
  451. mock_networkstatus_get_live_consensus_false);
  452. ret = hs_client_refetch_hsdesc(&service_pk);
  453. UNMOCK(networkstatus_get_live_consensus);
  454. tt_int_op(ret, OP_EQ, HS_CLIENT_FETCH_MISSING_INFO);
  455. /* From now on, return a live consensus. */
  456. MOCK(networkstatus_get_live_consensus,
  457. mock_networkstatus_get_live_consensus);
  458. /* 3. Not enough dir information. */
  459. MOCK(router_have_minimum_dir_info,
  460. mock_router_have_minimum_dir_info_false);
  461. ret = hs_client_refetch_hsdesc(&service_pk);
  462. UNMOCK(router_have_minimum_dir_info);
  463. tt_int_op(ret, OP_EQ, HS_CLIENT_FETCH_MISSING_INFO);
  464. /* From now on, we do have enough directory information. */
  465. MOCK(router_have_minimum_dir_info,
  466. mock_router_have_minimum_dir_info_true);
  467. /* 4. We do have a pending directory request. */
  468. {
  469. dir_connection_t *dir_conn = dir_connection_new(AF_INET);
  470. dir_conn->hs_ident = tor_malloc_zero(sizeof(hs_ident_dir_conn_t));
  471. TO_CONN(dir_conn)->purpose = DIR_PURPOSE_FETCH_HSDESC;
  472. ed25519_pubkey_copy(&dir_conn->hs_ident->identity_pk, &service_pk);
  473. smartlist_add(get_connection_array(), TO_CONN(dir_conn));
  474. ret = hs_client_refetch_hsdesc(&service_pk);
  475. smartlist_remove(get_connection_array(), TO_CONN(dir_conn));
  476. connection_free_minimal(TO_CONN(dir_conn));
  477. tt_int_op(ret, OP_EQ, HS_CLIENT_FETCH_PENDING);
  478. }
  479. /* 5. We'll trigger an error on the fetch_desc_v3 and force to close all
  480. * pending SOCKS request. */
  481. MOCK(router_have_minimum_dir_info,
  482. mock_router_have_minimum_dir_info_true);
  483. MOCK(fetch_v3_desc, mock_fetch_v3_desc_error);
  484. MOCK(connection_mark_unattached_ap_,
  485. mock_connection_mark_unattached_ap_);
  486. ret = hs_client_refetch_hsdesc(&service_pk);
  487. UNMOCK(fetch_v3_desc);
  488. UNMOCK(connection_mark_unattached_ap_);
  489. tt_int_op(ret, OP_EQ, HS_CLIENT_FETCH_ERROR);
  490. /* The close waiting for descriptor function has been called. */
  491. tt_int_op(ec->edge_.end_reason, OP_EQ, END_STREAM_REASON_RESOLVEFAILED);
  492. done:
  493. connection_free_minimal(ENTRY_TO_CONN(ec));
  494. UNMOCK(networkstatus_get_live_consensus);
  495. UNMOCK(router_have_minimum_dir_info);
  496. hs_free_all();
  497. }
  498. struct testcase_t hs_client_tests[] = {
  499. { "e2e_rend_circuit_setup_legacy", test_e2e_rend_circuit_setup_legacy,
  500. TT_FORK, NULL, NULL },
  501. { "e2e_rend_circuit_setup", test_e2e_rend_circuit_setup,
  502. TT_FORK, NULL, NULL },
  503. { "client_pick_intro", test_client_pick_intro,
  504. TT_FORK, NULL, NULL },
  505. { "descriptor_fetch", test_descriptor_fetch,
  506. TT_FORK, NULL, NULL },
  507. END_OF_TESTCASES
  508. };