use rand::Rng;
use rings_core::dht::Did;
use super::admitted;
use super::fixture_expiry;
use super::fixture_keys;
use super::fixture_rng;
use super::hop_key;
use super::FIXTURE_EPOCH;
use crate::onion::circuit::OnionReplayNonce;
use crate::onion::sphinx::cell::OnionCell;
use crate::onion::sphinx::cell::OnionProduceError;
use crate::onion::sphinx::cell::OnionStep;
use crate::onion::sphinx::class::OnionLoopClass;
use crate::onion::sphinx::header::OnionHeaderHop;
use crate::onion::sphinx::header::OnionHeaderRoute;
use crate::onion::sphinx::layer::OnionApplication;
use crate::onion::sphinx::layer::OnionArguments;
use crate::onion::sphinx::layer::OnionLayer;
use crate::onion::sphinx::layer::OnionLayerApplication;
use crate::onion::sphinx::layer::OnionLayerHead;
use crate::onion::sphinx::seed::OnionCarrySeed;
use crate::onion::sphinx::seed::OnionSegmentSeed;
use crate::onion::OnionServiceName;
fn layer(
position: u32,
application: OnionLayerApplication,
inbound: OnionCarrySeed,
outbound: OnionSegmentSeed,
) -> OnionLayer {
OnionLayer {
head: OnionLayerHead {
application,
next: Did::from(100 + position),
epoch: FIXTURE_EPOCH,
expiry: fixture_expiry(position),
nonce: OnionReplayNonce::new([2; 16]),
},
inbound,
outbound,
}
}
#[test]
fn test_loop_carries_each_segment_value_to_its_consumer() {
let mut rng = fixture_rng(50);
let class = OnionLoopClass::DEFAULT;
let mut keys = fixture_keys(4);
keys.push(hop_key(0));
let (first, first_keys) = OnionSegmentSeed::draw(&mut rng).expect("strong segment");
let (second, _) = OnionSegmentSeed::draw(&mut rng).expect("strong segment");
let [inbound_1, inbound_2] = first.seeds().relays;
let [inbound_4, inbound_5] = second.seeds().relays;
let layers = [
layer(
0,
OnionLayerApplication::Relay,
inbound_1,
OnionSegmentSeed::random(&mut rng),
),
layer(
1,
OnionLayerApplication::Relay,
inbound_2,
OnionSegmentSeed::random(&mut rng),
),
layer(
2,
OnionLayerApplication::Apply(OnionApplication {
symbol: OnionServiceName::tcp(),
arguments: OnionArguments::new([3; 64]),
}),
first.seeds().consumer,
OnionSegmentSeed::new(*second.as_bytes()),
),
layer(
3,
OnionLayerApplication::Relay,
inbound_4,
OnionSegmentSeed::random(&mut rng),
),
layer(
4,
OnionLayerApplication::Relay,
inbound_5,
OnionSegmentSeed::random(&mut rng),
),
];
let route = OnionHeaderRoute::new(
keys.iter()
.zip(layers)
.map(|(key, layer)| OnionHeaderHop {
public_key: key.delegatee_public_key(),
layer,
})
.collect(),
)
.expect("loop length");
let input = rng.gen::<[u8; 32]>();
let output = rng.gen::<[u8; 24]>();
let (cell, tag) = OnionCell::client(&route, class, &first_keys, &input, &mut rng)
.expect("the client's first cell");
let peel = |cell: Vec<u8>, position: usize| {
let admitted = admitted(cell, &keys[position]);
let index = u32::try_from(position).expect("small");
assert_eq!(admitted.head().expiry, fixture_expiry(index));
let is_relay = admitted.head().application == OnionLayerApplication::Relay;
(is_relay, admitted.step().expect("carry step"))
};
let relay = |cell: Vec<u8>, position| {
let (is_relay, OnionStep::Relayed { cell, .. }) = peel(cell, position) else {
panic!("a relay position");
};
assert!(is_relay);
assert_eq!(cell.class(), class);
cell.into_bytes()
};
let cell = relay(relay(cell.into_bytes(), 0), 1);
let (is_relay, OnionStep::Consumed { value, surb, .. }) = peel(cell, 2) else {
panic!("the symbol position");
};
assert!(!is_relay);
assert_eq!(value.as_slice(), input);
assert_eq!(surb.expiry(), fixture_expiry(2));
let overwide = vec![0; surb.capacity() + 1];
let Err(OnionProduceError::ValueTooWide { surb, .. }) = surb.produce(&overwide) else {
panic!("a value one byte over capacity");
};
let (next, cell) = surb.produce(&output).expect("produce");
assert_eq!(next, Did::from(102_u32));
let cell = relay(relay(cell.into_bytes(), 3), 4);
let returned = OnionCell::parse(&cell).expect("cell");
assert_eq!(returned.loop_tag(), tag);
assert_eq!(
returned
.open(&second.seeds().consumer.key().expect("strong key"))
.expect("the client receives v₁")
.as_slice(),
output
);
}