test_hs_client.c 20 KB

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