#[cfg(feature = "dummy")]
use rings_core::dht::entry::EntryKind;
use rings_core::message::MessageSigner;
#[cfg(feature = "dummy")]
use super::controlled::ControlledNetwork;
use super::*;
#[cfg(feature = "dummy")]
use crate::consts::DATA_REDUNDANT;
use crate::test_support::TestTask;
use crate::tests::activity::probe_on_activity;
use crate::tests::activity::record_activity;
use crate::tests::exit_descriptor::ExitDescriptorFixture;
#[cfg(not(feature = "dummy"))]
use crate::tests::native::network_test_guard as native_network_lock;
#[cfg(feature = "dummy")]
use crate::tests::native::signal_edge;
use crate::tests::test_processor_builder;
use crate::tests::test_processor_config;
pub(super) fn bounded_policy(
allowed_targets: &[&str],
denied_targets: &[&str],
) -> Result<OnionExitPolicy> {
let mut policy = onion_policy(allowed_targets, denied_targets)?;
policy.max_sessions = 8;
policy.max_bytes_per_minute = 4096;
Ok(policy)
}
pub(super) fn onion_policy(
allowed_targets: &[&str],
denied_targets: &[&str],
) -> Result<OnionExitPolicy> {
OnionExitPolicy::from_target_strings(
allowed_targets
.iter()
.map(|target| (*target).to_string())
.collect(),
denied_targets
.iter()
.map(|target| (*target).to_string())
.collect(),
)
}
pub(super) struct ApplicationRecorder {
inbound: Arc<Mutex<Vec<ApplicationData>>>,
_reader: TestTask<()>,
}
impl ApplicationRecorder {
pub(super) fn attach(processor: &Processor) -> Arc<Self> {
let inbox = processor
.swarm
.application_inbox()
.expect("the test processor's inbox is unclaimed");
let inbound = Arc::new(Mutex::new(Vec::new()));
let recorded = inbound.clone();
let reader = TestTask::spawn(async move {
while let Some(delivery) = inbox.next().await {
if let ApplicationDelivery::Message(message) = delivery {
recorded.lock().unwrap().push(message.data);
}
record_activity();
}
});
Arc::new(Self {
inbound,
_reader: reader,
})
}
}
pub(super) struct NetworkTestGuard {
#[cfg(feature = "dummy")]
pub(super) network: ControlledNetwork,
#[cfg(not(feature = "dummy"))]
_serial: tokio::sync::MutexGuard<'static, ()>,
}
pub(super) async fn network_test_guard() -> NetworkTestGuard {
NetworkTestGuard {
#[cfg(feature = "dummy")]
network: ControlledNetwork::start(),
#[cfg(not(feature = "dummy"))]
_serial: native_network_lock().await,
}
}
pub(super) async fn prepare_processor_with_identity_key(identity_key: SecretKey) -> Processor {
prepare_processor_with_identity_key_and_network(identity_key, 0).await
}
pub(super) async fn prepare_processor_with_identity_key_and_network(
identity_key: SecretKey,
network_id: u32,
) -> Processor {
prepare_processor_with_identity_key_network_and_virtual_nodes(identity_key, network_id, {
rings_core::dht::DEFAULT_STORAGE_VIRTUAL_POSITIONS_PER_OWNER
})
.await
}
pub(super) async fn prepare_processor_with_identity_key_network_and_virtual_nodes(
identity_key: SecretKey,
network_id: u32,
dht_virtual_nodes: u16,
) -> Processor {
test_processor_builder(&test_processor_config(&identity_key, network_id))
.dht_virtual_nodes(dht_virtual_nodes)
.build()
.unwrap()
}
#[cfg(feature = "dummy")]
pub(super) async fn prepare_online_node_registry_pair(
network_id: u32,
) -> Result<(Processor, Processor)> {
let registry_key = entry::Entry::gen_did(ONLINE_NODES_TOPIC)?;
let placement_keys = registry_key.rotate_affine(DATA_REDUNDANT)?;
for _ in 0..512 {
let first_key = SecretKey::random();
let second_key = SecretKey::random();
let first_did = first_key.address().into();
let second_did = second_key.address().into();
let first_owns_all = owns_all_placements(first_did, second_did, placement_keys.as_slice());
let second_owns_all = owns_all_placements(second_did, first_did, placement_keys.as_slice());
let Some((publisher_key, owner_key)) = (match (first_owns_all, second_owns_all) {
(true, false) => Some((second_key, first_key)),
(false, true) => Some((first_key, second_key)),
_ => None,
}) else {
continue;
};
let publisher = prepare_processor_with_identity_key_network_and_virtual_nodes(
publisher_key,
network_id,
0,
)
.await;
let owner =
prepare_processor_with_identity_key_network_and_virtual_nodes(owner_key, network_id, 0)
.await;
return Ok((publisher, owner));
}
Err(Error::InvalidConfig(
"could not generate an online-node registry owner covering every placement".to_string(),
))
}
#[cfg(feature = "dummy")]
pub(super) fn owns_all_placements(local: Did, successor: Did, placements: &[Did]) -> bool {
placements
.iter()
.all(|placement| *placement - local <= successor - local)
}
pub(super) async fn prepare_processor_with_network(network_id: u32) -> Processor {
prepare_processor_with_network_and_virtual_nodes(network_id, 0).await
}
pub(super) async fn prepare_processor_with_network_and_virtual_nodes(
network_id: u32,
dht_virtual_nodes: u16,
) -> Processor {
prepare_processor_with_identity_key_network_and_virtual_nodes(
SecretKey::random(),
network_id,
dht_virtual_nodes,
)
.await
}
#[cfg(feature = "dummy")]
pub(super) fn owns_entry_placement(processor: &Processor, placement_key: Did) -> Result<bool> {
match processor.swarm.dht().find_successor(placement_key)? {
PeerRingAction::Some(_) => Ok(true),
PeerRingAction::RemoteAction(_, PeerRingRemoteAction::FindSuccessor(_)) => Ok(false),
action => Err(Error::InvalidConfig(format!(
"unexpected registry owner lookup action: {action:?}"
))),
}
}
pub(super) async fn prepare_processor_with_online_node_type(
node_type: OnlineNodeType,
) -> Processor {
test_processor_builder(&test_processor_config(&SecretKey::random(), 0))
.online_node_type(node_type)
.build()
.unwrap()
}
pub(super) fn onion_exit_descriptor_for_processor(
processor: &Processor,
service: &str,
now_ms: u128,
) -> Result<OnionExitDescriptor> {
ExitDescriptorFixture::live(
processor,
OnionServiceName::parse(service)?,
bounded_policy(&["127.0.0.1:8080", "example.com:443"], &[])?,
now_ms,
)
.sign()
}
pub(super) fn online_relay_descriptor_for_processor(
processor: &Processor,
now_ms: u128,
) -> Result<OnlineNodeDescriptor> {
let capabilities = OnlineNodeCapabilities::onion_relay(processor.onion_process_epoch.get());
let delegatee_key = processor.delegatee_key();
OnlineNodeDescriptor::new_signed(
OnlineNodeDescriptorBody {
did: processor.did(),
public_key: processor
.swarm
.delegator_verification_pubkey()
.map_err(Error::CoreError)?,
delegatee_public_key: delegatee_key.delegatee_public_key(),
node_type: default_online_node_type(),
network_id: processor.swarm.network_id(),
storage_redundancy: processor.swarm.storage_redundancy(),
dht_virtual_nodes: processor.swarm.dht_virtual_nodes(),
capabilities,
endpoint_hint: None,
started_at_ms: now_ms,
fresh_for_ms: 90_000,
expires_at_ms: now_ms + 90_000,
version: crate::util::build_version(),
},
MessageSigner::new(&delegatee_key, processor.swarm.network_id()),
)
.map_err(Error::CoreError)
}
pub(super) async fn store_onion_relays(
processor: &Processor,
exits: &[&Processor],
relays: usize,
) -> Result<Vec<Processor>> {
let mut relay_processors = Vec::with_capacity(relays);
for _ in 0..relays {
relay_processors.push(prepare_processor().await);
}
let now_ms = get_epoch_ms();
let descriptors = exits
.iter()
.copied()
.chain(relay_processors.iter())
.map(|relay| online_relay_descriptor_for_processor(relay, now_ms))
.collect::<Result<Vec<_>>>()?;
processor
.storage_store(Processor::registry_entry(ONLINE_NODES_TOPIC, descriptors)?)
.await?;
Ok(relay_processors)
}
pub(super) fn mismatched_storage_redundancy(value: u16) -> u16 {
if value == u16::MAX {
value.saturating_sub(1)
} else {
value.saturating_add(1)
}
}
pub(super) async fn prepare_measured_processor() -> Processor {
let measure = PeriodicMeasure::new(Box::new(MemStorage::new()))
.await
.unwrap();
test_processor_builder(&test_processor_config(&SecretKey::random(), 0))
.measure(measure)
.build()
.unwrap()
}
#[cfg(feature = "dummy")]
pub(super) async fn connect_processors(p1: &Processor, p2: &Processor) {
signal_edge(p1, p2).await.expect("the edge is signalled");
wait_processors_connected(p1, p2).await;
}
#[cfg(feature = "dummy")]
pub(super) const PROCESSOR_TEST_HANG_GUARD: Duration = Duration::from_secs(5);
#[cfg(not(feature = "dummy"))]
pub(super) const PROCESSOR_TEST_HANG_GUARD: Duration = Duration::from_secs(60);
pub(super) async fn wait_processors_connected(p1: &Processor, p2: &Processor) {
probe_on_activity(
"processors admitted each other",
PROCESSOR_TEST_HANG_GUARD,
|| {
let admitted = processor_has_admitted_peer(p1, p2.did())
&& processor_has_admitted_peer(p2, p1.did());
async move { Ok(admitted.then_some(())) }
},
)
.await
.unwrap();
}
pub(super) fn processor_has_admitted_peer(processor: &Processor, peer: Did) -> bool {
processor.swarm.peer_dids().contains(&peer)
}
#[cfg(feature = "dummy")]
pub(super) async fn wait_for_mutual_dht_topology(
processor: &Processor,
other: &Processor,
) -> Result<()> {
let stabilizer = processor.swarm.stabilizer();
let other_stabilizer = other.swarm.stabilizer();
futures::try_join!(stabilizer.stabilize(), other_stabilizer.stabilize(),)
.map_err(Error::CoreError)?;
let did = processor.did().to_string();
let other_did = other.did().to_string();
let (did, other_did) = (&did, &other_did);
probe_on_activity(
"mutual DHT topology",
PROCESSOR_TEST_HANG_GUARD,
|| async move {
let inspect = processor.swarm.inspect().await;
let other_inspect = other.swarm.inspect().await;
let processor_sees_other = inspect
.dht
.successors
.iter()
.any(|successor| successor == other_did)
&& inspect.dht.predecessor.as_ref() == Some(other_did);
let other_sees_processor = other_inspect
.dht
.successors
.iter()
.any(|successor| successor == did)
&& other_inspect.dht.predecessor.as_ref() == Some(did);
Ok((processor_sees_other && other_sees_processor).then_some(()))
},
)
.await
}
#[cfg(feature = "dummy")]
pub(super) async fn wait_for_online_node_dids_in_storage(
processor: &Processor,
placement_keys: &[Did],
expected: &BTreeSet<Did>,
context: &str,
) -> Result<()> {
probe_on_activity(
&format!("online node registry storage covers {expected:?} during {context}"),
PROCESSOR_TEST_HANG_GUARD,
|| async move {
for placement_key in placement_keys {
let observed = match processor
.swarm
.dht()
.stored_entry_at(EntryKind::Data, *placement_key, get_epoch_ms())
.await
.map_err(Error::Storage)?
{
Some(entry) => OnlineNodeRegistration::descriptors_from_entry(&entry)
.into_iter()
.map(|descriptor| descriptor.did)
.collect::<BTreeSet<_>>(),
None => BTreeSet::new(),
};
if !expected.is_subset(&observed) {
return Ok(None);
}
}
Ok(Some(()))
},
)
.await
}
#[cfg(feature = "dummy")]
pub(super) async fn wait_for_peer_measurement(
processor: &Processor,
did: Did,
predicate: impl Fn(&PeerMeasurement) -> bool,
) -> PeerMeasurement {
let predicate = &predicate;
probe_on_activity(
"peer measurement updated",
PROCESSOR_TEST_HANG_GUARD,
|| async move {
Ok(processor
.peer_measurement(did)
.await
.filter(|measurement| predicate(measurement)))
},
)
.await
.unwrap()
}
pub(super) async fn wait_for_application_data(
recorder: &ApplicationRecorder,
predicate: impl Fn(&ApplicationData) -> bool,
) -> ApplicationData {
probe_on_activity(
"application data delivered",
PROCESSOR_TEST_HANG_GUARD,
|| {
let found = recorder
.inbound
.lock()
.unwrap()
.iter()
.find(|msg| predicate(msg))
.cloned();
async move { Ok(found) }
},
)
.await
.unwrap()
}
pub(super) fn e2e_stream_complete<'a>(
frames: impl IntoIterator<Item = &'a E2eStreamFrame>,
) -> bool {
let (sequences, finals): (BTreeSet<u64>, Vec<u64>) = frames.into_iter().fold(
(BTreeSet::new(), Vec::new()),
|(mut sequences, mut finals), frame| {
sequences.insert(frame.sequence);
if frame.is_final {
finals.push(frame.sequence);
}
(sequences, finals)
},
);
finals
.into_iter()
.any(|last| (0..=last).all(|sequence| sequences.contains(&sequence)))
}
#[cfg(feature = "dummy")]
pub(super) fn received_e2e_stream_frames(
recorder: &ApplicationRecorder,
stream_id: e2e::E2eStreamId,
) -> Vec<E2eStreamFrame> {
recorder
.inbound
.lock()
.unwrap()
.iter()
.filter_map(|data| match data {
ApplicationData::E2eStreamFrame(frame) if frame.stream_id == stream_id => {
Some(frame.clone())
}
_ => None,
})
.collect()
}