use std::collections::BTreeSet;
use super::common::*;
use super::*;
use crate::descriptor::RegistryElement;
use crate::onion::OnionProcessEpoch;
use crate::onion::OnionProcessEpochCell;
use crate::onion::OnionServiceName;
use crate::tests::exit_descriptor::ExitDescriptorFixture;
#[tokio::test]
async fn test_onion_exit_lookup_uses_dedicated_exit_registry() -> Result<()> {
let processor = prepare_processor().await;
let relay_only = prepare_processor().await;
let exit = prepare_processor().await;
let relay_descriptor = relay_only.online_node_descriptor_at(get_epoch_ms())?;
let exit_descriptor = onion_exit_descriptor_for_processor(&exit, "tcp", get_epoch_ms())?;
processor
.storage_store(Processor::registry_entry(ONLINE_NODES_TOPIC, vec![
relay_descriptor,
])?)
.await?;
processor
.storage_store(Processor::registry_entry(ONION_EXITS_TOPIC, vec![
exit_descriptor.clone(),
])?)
.await?;
let exits = processor.lookup_onion_exits("tcp", false).await?;
assert_eq!(
exits
.iter()
.map(|descriptor| descriptor.to_element().ok())
.collect::<Vec<_>>(),
vec![exit_descriptor.to_element().ok()]
);
assert!(exits
.iter()
.all(|descriptor| descriptor.verify_signature(processor.swarm.network_id())));
assert!(!exits
.iter()
.any(|descriptor| descriptor.did == relay_only.did()));
Ok(())
}
#[tokio::test]
async fn test_onion_exit_lookup_preserves_distinct_services_for_same_did() -> Result<()> {
let processor = prepare_processor().await;
let exit = prepare_processor().await;
let now_ms = get_epoch_ms();
let older_tcp = onion_exit_descriptor_for_processor(&exit, "tcp", now_ms)?;
let newer_https = onion_exit_descriptor_for_processor(&exit, "https", now_ms + 1)?;
processor
.storage_store(Processor::registry_entry(ONION_EXITS_TOPIC, vec![
older_tcp,
newer_https,
])?)
.await?;
assert_eq!(processor.lookup_onion_exits("tcp", false).await?.len(), 1);
assert_eq!(processor.lookup_onion_exits("https", false).await?.len(), 1);
assert_eq!(processor.lookup_onion_exits("", false).await?.len(), 2);
Ok(())
}
#[tokio::test]
async fn test_onion_proxy_route_uses_presence_relays_without_exit_descriptor() -> Result<()> {
let processor = prepare_processor().await;
let exit = prepare_processor().await;
let exit_descriptor = onion_exit_descriptor_for_processor(&exit, "tcp", get_epoch_ms())?;
let relays = store_onion_relays(&processor, &[&exit], 3).await?;
processor
.storage_store(Processor::registry_entry(ONION_EXITS_TOPIC, vec![
exit_descriptor.clone(),
])?)
.await?;
let route = directory::build_onion_proxy_route_with_first_hop(
&processor,
OnionProxyProtocol::TcpConnect,
OnionProxyTarget::parse_authority("example.com:443")?,
|_| true,
)
.await?
.route;
let dids = route
.hops()
.positions()
.map(|hop| hop.did)
.collect::<Vec<_>>();
let relay_dids = relays.iter().map(Processor::did).collect::<BTreeSet<_>>();
assert_eq!(dids.len(), 5);
assert_eq!(dids.first(), dids.last());
assert_eq!(dids.get(2), Some(&exit.did()));
assert!(dids
.iter()
.enumerate()
.all(|(index, did)| index == 2 || relay_dids.contains(did)));
assert_eq!(
route.exit().to_element().ok(),
exit_descriptor.to_element().ok()
);
Ok(())
}
#[tokio::test]
async fn test_onion_proxy_route_rejects_exit_with_stale_process_epoch() -> Result<()> {
let processor = prepare_processor().await;
let exit = prepare_processor().await;
let mut restarted = exit.clone();
restarted.onion_process_epoch = OnionProcessEpochCell::new(OnionProcessEpoch::new([0x5a; 16]));
let stale_descriptor = onion_exit_descriptor_for_processor(&exit, "tcp", get_epoch_ms())?;
store_onion_relays(&processor, &[&restarted], 3).await?;
processor
.storage_store(Processor::registry_entry(ONION_EXITS_TOPIC, vec![
stale_descriptor,
])?)
.await?;
let error = processor
.build_onion_proxy_route(
OnionProxyProtocol::TcpConnect,
OnionProxyTarget::parse_authority("example.com:443")?,
)
.await
.err()
.ok_or_else(|| Error::InvalidConfig("expected route failure".to_string()))?;
assert!(matches!(
error,
Error::OnionRouteError(OnionRouteError::ExitRelayRegistrationMismatch { service })
if service == "tcp"
));
Ok(())
}
#[tokio::test]
async fn test_onion_proxy_route_uses_protocol_service_class() -> Result<()> {
let processor = prepare_processor().await;
let tcp_exit = prepare_processor().await;
let https_exit = prepare_processor().await;
let now_ms = get_epoch_ms();
let tcp_exit_descriptor = onion_exit_descriptor_for_processor(&tcp_exit, "tcp", now_ms)?;
let https_exit_descriptor = onion_exit_descriptor_for_processor(&https_exit, "https", now_ms)?;
processor
.storage_store(Processor::registry_entry(ONION_EXITS_TOPIC, vec![
tcp_exit_descriptor,
https_exit_descriptor,
])?)
.await?;
store_onion_relays(&processor, &[&tcp_exit, &https_exit], 2).await?;
let target = OnionProxyTarget::parse_authority("example.com:443")?;
let tcp_route = directory::build_onion_proxy_route_with_first_hop(
&processor,
OnionProxyProtocol::TcpConnect,
target.clone(),
|_| true,
)
.await?;
let https_route = directory::build_onion_proxy_route_with_first_hop(
&processor,
OnionProxyProtocol::HttpsProxy,
target,
|_| true,
)
.await?;
assert_eq!(tcp_route.exit_service(), "tcp");
assert_eq!(tcp_route.exit_did(), tcp_exit.did());
assert_eq!(https_route.exit_service(), "https");
assert_eq!(https_route.exit_did(), https_exit.did());
Ok(())
}
#[tokio::test]
async fn test_tcp_connect_route_rejects_browser_tcp_exit_descriptor() -> Result<()> {
let processor = prepare_processor().await;
let browser_exit = prepare_processor().await;
let descriptor = ExitDescriptorFixture {
node_type: OnlineNodeType::Browser,
..ExitDescriptorFixture::live(
&browser_exit,
OnionServiceName::tcp(),
bounded_policy(&["example.com:443"], &[])?,
get_epoch_ms(),
)
}
.sign()?;
processor
.storage_store(Processor::registry_entry(ONION_EXITS_TOPIC, vec![
descriptor,
])?)
.await?;
let target = OnionProxyTarget::parse_authority("example.com:443")?;
let error = processor
.build_onion_proxy_route(OnionProxyProtocol::TcpConnect, target)
.await
.err()
.ok_or_else(|| Error::InvalidConfig("expected route failure".to_string()))?;
assert!(matches!(
error,
Error::OnionRouteError(OnionRouteError::NoExitForProxyProtocol { service, protocol })
if service == "tcp" && protocol == "tcp-connect"
));
Ok(())
}
#[tokio::test]
async fn test_onion_proxy_route_filters_exits_by_target_policy() -> Result<()> {
let processor = prepare_processor().await;
let allowed_exit = prepare_processor().await;
let denied_exit = prepare_processor().await;
let now_ms = get_epoch_ms();
let allowed_descriptor = ExitDescriptorFixture::live(
&allowed_exit,
OnionServiceName::https(),
bounded_policy(&["example.com:443"], &[])?,
now_ms,
)
.sign()?;
let denied_descriptor = ExitDescriptorFixture::live(
&denied_exit,
OnionServiceName::https(),
bounded_policy(&["example.com:443"], &["example.com:443"])?,
now_ms,
)
.sign()?;
processor
.storage_store(Processor::registry_entry(ONION_EXITS_TOPIC, vec![
denied_descriptor,
allowed_descriptor,
])?)
.await?;
store_onion_relays(&processor, &[&allowed_exit, &denied_exit], 2).await?;
let target = OnionProxyTarget::parse_authority("example.com:443")?;
let route = directory::build_onion_proxy_route_with_first_hop(
&processor,
OnionProxyProtocol::HttpsProxy,
target,
|_| true,
)
.await?;
assert_eq!(route.exit_did(), allowed_exit.did());
Ok(())
}
#[tokio::test]
async fn test_onion_proxy_route_reports_policy_denied_target() -> Result<()> {
let processor = prepare_processor().await;
let denied_exit = prepare_processor().await;
let now_ms = get_epoch_ms();
let denied_descriptor = ExitDescriptorFixture::live(
&denied_exit,
OnionServiceName::https(),
bounded_policy(&["other.example.com:443"], &[])?,
now_ms,
)
.sign()?;
processor
.storage_store(Processor::registry_entry(ONION_EXITS_TOPIC, vec![
denied_descriptor,
])?)
.await?;
let target = OnionProxyTarget::parse_authority("example.com:443")?;
let error = processor
.build_onion_proxy_route(OnionProxyProtocol::HttpsProxy, target)
.await
.err()
.ok_or_else(|| Error::InvalidConfig("expected route failure".to_string()))?;
assert!(matches!(
error,
Error::OnionRouteError(OnionRouteError::NoExitAllowsTarget { service, target })
if service == "https" && target == "example.com:443"
));
Ok(())
}
#[tokio::test]
async fn test_https_proxy_route_skips_browser_exit_for_domain_target_and_allows_public_ip_literal(
) -> Result<()> {
let processor = prepare_processor().await;
let browser_exit = prepare_processor().await;
let now_ms = get_epoch_ms();
let descriptor = ExitDescriptorFixture {
node_type: OnlineNodeType::Browser,
..ExitDescriptorFixture::live(
&browser_exit,
OnionServiceName::https(),
bounded_policy(&["*:*"], &[])?,
now_ms,
)
}
.sign()?;
processor
.storage_store(Processor::registry_entry(ONION_EXITS_TOPIC, vec![
descriptor,
])?)
.await?;
store_onion_relays(&processor, &[&browser_exit], 3).await?;
let domain_target = OnionProxyTarget::parse_authority("example.com:443")?;
let domain_error = directory::build_onion_proxy_route_with_first_hop(
&processor,
OnionProxyProtocol::HttpsProxy,
domain_target,
|_| true,
)
.await
.err()
.ok_or_else(|| Error::InvalidConfig("expected domain target rejection".to_string()))?;
assert!(matches!(
domain_error,
Error::OnionRouteError(OnionRouteError::NoExitAllowsTarget { service, target })
if service == "https" && target == "example.com:443"
));
let public_ip_target = OnionProxyTarget::parse_authority("1.1.1.1:443")?;
let ip_route = directory::build_onion_proxy_route_with_first_hop(
&processor,
OnionProxyProtocol::HttpsProxy,
public_ip_target,
|_| true,
)
.await?;
assert_eq!(ip_route.exit_did(), browser_exit.did());
assert!(bounded_policy(&["example.com:443"], &[])?
.validate_browser_targets()
.is_err());
assert!(bounded_policy(&["1.1.1.1:443"], &[])?
.validate_browser_targets()
.is_ok());
Ok(())
}
#[tokio::test]
async fn test_onion_proxy_route_requires_direct_peer_for_first_hop() -> Result<()> {
let processor = prepare_processor().await;
let exit = prepare_processor().await;
let exit_descriptor = onion_exit_descriptor_for_processor(&exit, "tcp", get_epoch_ms())?;
store_onion_relays(&processor, &[&exit], 3).await?;
processor
.storage_store(Processor::registry_entry(ONION_EXITS_TOPIC, vec![
exit_descriptor,
])?)
.await?;
let error = processor
.build_onion_proxy_route(
OnionProxyProtocol::TcpConnect,
OnionProxyTarget::parse_authority("example.com:443")?,
)
.await
.err()
.ok_or_else(|| Error::InvalidConfig("expected NoPermittedFirstHop".to_string()))?;
assert!(matches!(
error,
Error::OnionRouteError(OnionRouteError::NoPermittedFirstHop)
));
Ok(())
}
#[cfg(feature = "dummy")]
#[tokio::test]
async fn test_onion_proxy_route_selects_only_directly_connected_first_hop_guard() -> Result<()> {
let _net = network_test_guard().await;
let processor = prepare_processor().await;
let exit = prepare_processor().await;
let exit_descriptor = onion_exit_descriptor_for_processor(&exit, "tcp", get_epoch_ms())?;
let relays = store_onion_relays(&processor, &[&exit], 3).await?;
processor
.storage_store(Processor::registry_entry(ONION_EXITS_TOPIC, vec![
exit_descriptor,
])?)
.await?;
let _client_rec = ApplicationRecorder::attach(&processor);
let _guard_rec = ApplicationRecorder::attach(&relays[1]);
connect_processors(&processor, &relays[1]).await;
let route = processor
.build_onion_proxy_route(
OnionProxyProtocol::TcpConnect,
OnionProxyTarget::parse_authority("example.com:443")?,
)
.await?
.route;
assert_eq!(route.hops().guard().did, relays[1].did());
Ok(())
}