test_hs_client.c 20 KB

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