mod test_hop;
mod test_session_ends;
mod test_tags;
mod test_wire_golden;
use std::num::NonZeroUsize;
use rand::rngs::StdRng;
use rand::SeedableRng;
use rings_core::delegation::DelegateeKey;
use rings_core::dht::Did;
use super::admission::OnionAdmissionState;
use super::admission::OnionReplayFilterKey;
use super::hop::hop;
use super::hop::OnionHopOutcome;
use super::OnionExpiry;
use crate::onion::circuit::OnionAdmissionLink;
use crate::onion::session::OnionSessionArguments;
use crate::onion::session::OnionSessionId;
use crate::onion::session::OnionTargetDigest;
use crate::onion::sphinx::builder::build_loop;
use crate::onion::sphinx::builder::OnionBuiltLoop;
use crate::onion::sphinx::cell::OnionCell;
use crate::onion::sphinx::cell::OnionStep;
use crate::onion::sphinx::class::OnionLoopClass;
use crate::onion::sphinx::header::OnionLoopTag;
use crate::onion::sphinx::layer::OnionApplication;
use crate::onion::tests::fixture::fixture_route_hop;
use crate::onion::tests::fixture::fixture_secret;
use crate::onion::tests::fixture::grid_expiry;
use crate::onion::tests::fixture::session_loop;
use crate::onion::OnionLoop;
use crate::onion::OnionProcessEpoch;
use crate::onion::OnionRouteHop;
use crate::onion::OnionServiceName;
const EPOCH: OnionProcessEpoch = OnionProcessEpoch::new([0x43; 16]);
const NOW_MS: u128 = 60_000;
const CLIENT: u32 = 99;
fn expiry() -> OnionExpiry {
grid_expiry(5)
}
struct Hop {
key: DelegateeKey,
admission: OnionAdmissionState,
}
impl Hop {
fn new(seed: u8) -> Self {
Self {
key: DelegateeKey::new_with_seckey(&fixture_secret(seed)).expect("fixture delegation"),
admission: OnionAdmissionState::new(
EPOCH,
OnionReplayFilterKey::new([seed; 32]),
NonZeroUsize::new(2).expect("non-zero"),
),
}
}
fn linked(mut self, peers: &[Did]) -> Self {
for peer in peers {
self.admission
.link_opened(NOW_MS, OnionAdmissionLink::new(*peer, 0))
.expect("the fixture's table holds its links");
}
self
}
fn did(&self) -> Did {
self.key.delegator_did()
}
fn route_hop(&self) -> OnionRouteHop {
fixture_route_hop(&self.key, EPOCH)
}
fn step(&mut self, from: Did, cell: OnionCell) -> OnionHopOutcome {
hop(
&mut self.admission,
&self.key,
true,
|_| false,
from,
NOW_MS,
cell,
)
}
}
struct Fixture {
hops: [Hop; 4],
}
impl Fixture {
fn new() -> Self {
let client = Did::from(CLIENT);
let [g, r02, h, r11] = [1, 2, 3, 4].map(Hop::new);
let dids = [g.did(), r02.did(), h.did(), r11.did()];
Self {
hops: [
g.linked(&[client, dids[1], dids[3]]),
r02.linked(&[dids[0], dids[2]]),
h.linked(&[dids[1], dids[3]]),
r11.linked(&[dids[2], dids[0]]),
],
}
}
fn route(&self) -> OnionLoop<OnionRouteHop> {
session_loop(self.hops.each_ref().map(Hop::route_hop))
}
fn build(&self, seed: u64, value: &[u8]) -> OnionBuiltLoop {
build_loop(
&self.route(),
&[OnionApplication {
symbol: OnionServiceName::tcp(),
arguments: arguments().encode(),
}],
Did::from(CLIENT),
OnionLoopClass::DEFAULT,
expiry(),
value,
&mut StdRng::seed_from_u64(seed),
)
.expect("build the fixture loop")
}
fn run_forward_segment(&mut self, cell: OnionCell) -> OnionCell {
let client = Did::from(CLIENT);
let guard = self.hops[0].did();
let cell = relayed(self.hops[0].step(client, cell), self.hops[1].did());
relayed(self.hops[1].step(guard, cell), self.hops[2].did())
}
fn run_return_segment(&mut self, cell: OnionCell) -> OnionCell {
let h = self.hops[2].did();
let r11 = self.hops[3].did();
let cell = relayed(self.hops[3].step(h, cell), self.hops[0].did());
relayed(self.hops[0].step(r11, cell), Did::from(CLIENT))
}
}
fn arguments() -> OnionSessionArguments {
OnionSessionArguments {
session: OnionSessionId::new([0x51; 16]),
digest: OnionTargetDigest::of(b"example.com:443"),
}
}
fn relayed(outcome: OnionHopOutcome, next: Did) -> OnionCell {
match outcome {
OnionHopOutcome::Stepped(OnionStep::Relayed { next: to, cell }) => {
assert_eq!(to, next);
cell
}
other => panic!("expected a relay to {next}, got {other:?}"),
}
}
fn is_tag(expected: OnionLoopTag) -> impl Fn(&OnionLoopTag) -> bool {
move |tag| *tag == expected
}