test_hs_client.c 20 KB

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