test_hs_client.c 20 KB

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