test_hs_client.c 20 KB

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