mod test_builder;
mod test_carry;
mod test_cell;
mod test_header;
mod test_layer;
mod test_peel_cost;
mod test_seed;
use std::num::NonZeroUsize;
use rand::rngs::StdRng;
use rand::Rng;
use rand::RngCore;
use rand::SeedableRng;
use rings_core::delegation::DelegateeKey;
use rings_core::dht::Did;
use super::cell::Charged;
use super::cell::OnionAdmittedCell;
use super::cell::OnionCell;
use super::header::OnionHeaderHop;
use super::header::OnionHeaderRoute;
use super::layer::OnionApplication;
use super::layer::OnionArguments;
use super::layer::OnionLayer;
use super::layer::OnionLayerApplication;
use super::layer::OnionLayerHead;
use super::layer::ONION_ARGUMENT_BYTES;
use super::seed::OnionCarrySeed;
use super::seed::OnionSegmentSeed;
use crate::onion::circuit::OnionAdmissionLink;
use crate::onion::circuit::OnionAdmissionState;
use crate::onion::circuit::OnionExpiry;
use crate::onion::circuit::OnionReplayFilterKey;
use crate::onion::circuit::OnionReplayNonce;
use crate::onion::tests::fixture::fixture_secret;
use crate::onion::tests::fixture::grid_expiry;
use crate::onion::OnionProcessEpoch;
use crate::onion::OnionServiceName;
const FIXTURE_EPOCH: OnionProcessEpoch = OnionProcessEpoch::new([1; 16]);
const FIXTURE_ARRIVAL_MS: u128 = 60_000;
fn fixture_link() -> OnionAdmissionLink {
OnionAdmissionLink::new(Did::from(1_u32), 0)
}
fn fixture_expiry(offset: u32) -> OnionExpiry {
grid_expiry(u128::from(3 + offset))
}
fn fixture_admission() -> OnionAdmissionState {
let mut admission = OnionAdmissionState::new(
FIXTURE_EPOCH,
OnionReplayFilterKey::new([7; 32]),
NonZeroUsize::MIN,
);
admission
.link_opened(FIXTURE_ARRIVAL_MS, fixture_link())
.expect("an empty table admits a link");
admission
}
fn charged(bytes: Vec<u8>) -> Charged<OnionCell> {
fixture_admission()
.charge(
FIXTURE_ARRIVAL_MS,
fixture_link(),
OnionCell::parse(&bytes).expect("a cell"),
)
.expect("the fixture link has budget")
}
fn admitted(bytes: Vec<u8>, key: &DelegateeKey) -> OnionAdmittedCell {
let mut admission = fixture_admission();
admission
.charge(
FIXTURE_ARRIVAL_MS,
fixture_link(),
OnionCell::parse(&bytes).expect("a cell"),
)
.expect("the fixture link has budget")
.peel(key)
.expect("peel")
.admit(&mut admission, FIXTURE_ARRIVAL_MS)
.expect("a fixture layer is admissible")
}
fn fixture_rng(seed: u64) -> StdRng {
StdRng::seed_from_u64(seed)
}
fn hop_key(index: usize) -> DelegateeKey {
let byte = u8::try_from(index).expect("fixture index fits a byte") + 1;
DelegateeKey::new_with_seckey(&fixture_secret(byte)).expect("fixture delegation")
}
fn fixture_layer(seed: u64, position: usize, hops: usize) -> OnionLayer {
let offset = u64::try_from(position).expect("fixture position fits u64");
let mut rng = fixture_rng(seed.wrapping_mul(1 << 16).wrapping_add(offset));
let application = if position + 1 == hops {
let mut arguments = [0; ONION_ARGUMENT_BYTES];
rng.fill_bytes(&mut arguments);
OnionLayerApplication::Apply(OnionApplication {
symbol: OnionServiceName::tcp(),
arguments: OnionArguments::new(arguments),
})
} else {
OnionLayerApplication::Relay
};
OnionLayer {
head: OnionLayerHead {
application,
next: Did::from(u32::try_from(position).expect("fixture position fits u32")),
epoch: FIXTURE_EPOCH,
expiry: fixture_expiry(0),
nonce: OnionReplayNonce::new(rng.gen()),
},
inbound: OnionCarrySeed::new(rng.gen()),
outbound: OnionSegmentSeed::random(&mut rng),
}
}
fn fixture_route(seed: u64, keys: &[DelegateeKey]) -> OnionHeaderRoute {
OnionHeaderRoute::new(
keys.iter()
.enumerate()
.map(|(position, key)| OnionHeaderHop {
public_key: key.delegatee_public_key(),
layer: fixture_layer(seed, position, keys.len()),
})
.collect(),
)
.expect("fixture loop length")
}
fn fixture_keys(hops: usize) -> Vec<DelegateeKey> {
(0..hops).map(hop_key).collect()
}