diff --git a/Cargo.lock b/Cargo.lock index 5e09d63c8..0b5246e56 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3974,12 +3974,15 @@ dependencies = [ "rand_core 0.6.4", "slotmap", "thiserror", + "tor-chanmgr", "tor-circmgr", "tor-config", "tor-error", + "tor-guardmgr", "tor-hscrypto", "tor-llcrypto", "tor-netdir", + "tor-persist", "tor-proto", "tor-rtcompat", "tracing", diff --git a/crates/tor-hsclient/Cargo.toml b/crates/tor-hsclient/Cargo.toml index 9ed28bc56..eea6bfca5 100644 --- a/crates/tor-hsclient/Cargo.toml +++ b/crates/tor-hsclient/Cargo.toml @@ -35,3 +35,9 @@ tor-rtcompat = { version = "0.8.1", path = "../tor-rtcompat" } tracing = "0.1.18" [dev-dependencies] +tor-chanmgr = { path = "../tor-chanmgr", version = "0.8.1" } +tor-circmgr = { version = "0.7.2", path = "../tor-circmgr", features = ["hs-client", "testing"] } +tor-guardmgr = { path = "../tor-guardmgr", version = "0.8.1", features = ["testing"] } +tor-netdir = { path = "../tor-netdir", version = "0.8.0", features = ["testing"] } +tor-persist = { path = "../tor-persist", version = "0.6.1", features = ["testing"] } +tor-rtcompat = { path = "../tor-rtcompat", version = "0.8.1", features = ["tokio", "native-tls"] } diff --git a/crates/tor-hsclient/src/state.rs b/crates/tor-hsclient/src/state.rs index 0877cc7b7..bf25d5d64 100644 --- a/crates/tor-hsclient/src/state.rs +++ b/crates/tor-hsclient/src/state.rs @@ -338,3 +338,103 @@ pub trait MockableConnectorData: Default + Debug + Send + Sync + 'static { /// Is circuit OK? Ie, not `.is_closing()`. fn circuit_is_ok(circuit: &Self::ClientCirc) -> bool; } + +#[cfg(test)] +mod test { + // @@ begin test lint list maintained by maint/add_warning @@ + #![allow(clippy::bool_assert_comparison)] + #![allow(clippy::clone_on_copy)] + #![allow(clippy::dbg_macro)] + #![allow(clippy::print_stderr)] + #![allow(clippy::print_stdout)] + #![allow(clippy::single_char_pattern)] + #![allow(clippy::unwrap_used)] + #![allow(clippy::unchecked_duration_subtraction)] + //! + use super::*; + use crate::*; + use tor_rtcompat::test_with_one_runtime; + + #[derive(Debug, Default)] + struct MockData { + // things will appear here when we have more sophisticated tests + } + + #[derive(Debug, Clone, Default)] + struct MockGlobalState { + // things will appear here when we have more sophisticated tests + } + + #[derive(Clone, Debug)] + struct MockCirc { + ok: Arc>, + } + + #[async_trait] + impl MockableConnectorData for MockData { + type ClientCirc = MockCirc; + type MockGlobalState = MockGlobalState; + + async fn connect( + _connector: &HsClientConnector, + _data: &mut MockData, + _secret_keys: HsClientSecretKeys, + ) -> Result { + let ok = Arc::new(Mutex::new(true)); + Ok(MockCirc { ok }) + } + + fn circuit_is_ok(circuit: &Self::ClientCirc) -> bool { + *circuit.ok.lock().unwrap() + } + } + + fn new_hsconn_mocked(runtime: R) -> HsClientConnector { + let chanmgr = tor_chanmgr::ChanMgr::new( + runtime.clone(), + &Default::default(), + tor_chanmgr::Dormancy::Dormant, + &Default::default(), + ); + let guardmgr = tor_guardmgr::GuardMgr::new( + runtime.clone(), + tor_persist::TestingStateMgr::new(), + &tor_guardmgr::TestConfig::default(), + ).unwrap(); + let circmgr = tor_circmgr::CircMgr::new( + &tor_circmgr::TestConfig::default(), + tor_persist::TestingStateMgr::new(), + &runtime, + Arc::new(chanmgr), + guardmgr, + ).unwrap(); + let netdir_provider = tor_netdir::testprovider::TestNetDirProvider::new(); + let netdir_provider = Arc::new(netdir_provider); + #[allow(clippy::let_and_return)] // we'll probably add more in this function + let hscc = HsClientConnector { + runtime, + circmgr, + netdir_provider, + services: Default::default(), + mock_for_state: MockGlobalState {}, + }; + hscc + } + + #[test] + fn simple() { + test_with_one_runtime!(|runtime| async { + let hsconn = new_hsconn_mocked(runtime); + let hs_id = [0_u8; 32].into(); + let isolation = tor_circmgr::IsolationToken::no_isolation(); + let secret_keys = HsClientSecretKeysBuilder::default().build().unwrap(); + let circuit = Services::get_or_launch_connection( + &hsconn, + hs_id, + isolation.into(), + secret_keys, + ).await.unwrap(); + eprintln!("{:?}", circuit); + }); + } +}