[domistream] Control host: tests first (ControlStream against ControlHost, replay, stop deadline), API stubs
Input arrives typed and byte-identical to what the client encoded; host rumble, trigger rumble, LED, motion and termination reach the client; replays, forgeries and malformed messages are dropped; the stop deadline ends a session without PERIODIC_PING. Alloc caps for the host parsers. They fail against the stubs.
This commit is contained in:
@@ -0,0 +1,156 @@
|
||||
//! "Caps every length before it allocates", for the host side of ENet and control: a counting global allocator
|
||||
//! (`peak_alloc`) checks that lying fragment lengths and lying message lengths never reserve what they claim, and
|
||||
//! that arbitrary input stays within a bound tied to the input and the caps.
|
||||
//!
|
||||
//! The counters are process-wide, so this file holds exactly one #[test] that runs everything in sequence.
|
||||
|
||||
#![allow(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::indexing_slicing, missing_docs)]
|
||||
|
||||
mod common;
|
||||
|
||||
use core::time::Duration;
|
||||
|
||||
use domistream::Instant;
|
||||
use domistream::control::{ControlCipher, ControlHost, ControlHostConfig, Direction, parse_client_message};
|
||||
use domistream::enet::protocol::{Command, CommandBody, ConnectParams, Datagram, NO_PEER_ID, encode_datagram};
|
||||
use domistream::enet::{EnetHostConfig, EnetHostPeer, MAX_PACKET_BYTES};
|
||||
use peak_alloc::PeakAlloc;
|
||||
use proptest::prelude::*;
|
||||
use proptest::test_runner::{Config, TestRunner};
|
||||
|
||||
#[global_allocator]
|
||||
static ALLOC: PeakAlloc = PeakAlloc;
|
||||
|
||||
fn peak_of<R>(f: impl FnOnce() -> R) -> usize {
|
||||
ALLOC.reset_peak_usage();
|
||||
let before = ALLOC.current_usage();
|
||||
let r = f();
|
||||
let peak = ALLOC.peak_usage();
|
||||
drop(r);
|
||||
peak.saturating_sub(before)
|
||||
}
|
||||
|
||||
fn budget(input: usize) -> usize {
|
||||
64 * input + 256 * 1024
|
||||
}
|
||||
|
||||
fn enet_config() -> EnetHostConfig {
|
||||
EnetHostConfig {
|
||||
peer_id: 1,
|
||||
channel_limit: 4,
|
||||
mtu: 1400,
|
||||
ping_interval: Duration::from_millis(500),
|
||||
timeout: Duration::from_secs(10),
|
||||
}
|
||||
}
|
||||
|
||||
fn connect() -> Vec<u8> {
|
||||
let params = ConnectParams {
|
||||
outgoing_peer_id: 0,
|
||||
incoming_session_id: 0xFF,
|
||||
outgoing_session_id: 0xFF,
|
||||
mtu: 1400,
|
||||
window_size: 65536,
|
||||
channel_count: 4,
|
||||
incoming_bandwidth: 0,
|
||||
outgoing_bandwidth: 0,
|
||||
packet_throttle_interval: 5000,
|
||||
packet_throttle_acceleration: 2,
|
||||
packet_throttle_deceleration: 2,
|
||||
connect_id: 1,
|
||||
};
|
||||
encode_datagram(&Datagram {
|
||||
peer_id: NO_PEER_ID,
|
||||
session_id: 0,
|
||||
sent_time: Some(0),
|
||||
commands: vec![Command {
|
||||
acknowledge: true,
|
||||
channel_id: 0xFF,
|
||||
reliable_sequence: 1,
|
||||
body: CommandBody::Connect { params, data: 0 },
|
||||
}],
|
||||
})
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
/// A connected host peer (verify acknowledged).
|
||||
fn connected() -> EnetHostPeer {
|
||||
let mut h = EnetHostPeer::accept(enet_config(), Instant::ZERO, &connect()).unwrap();
|
||||
while h.poll_transmit(Instant::ZERO).is_some() {}
|
||||
let ack = encode_datagram(&Datagram {
|
||||
peer_id: 1,
|
||||
session_id: 0,
|
||||
sent_time: Some(1),
|
||||
commands: vec![Command {
|
||||
acknowledge: false,
|
||||
channel_id: 0xFF,
|
||||
reliable_sequence: 1,
|
||||
body: CommandBody::Acknowledge { received_reliable_sequence: 1, received_sent_time: 0 },
|
||||
}],
|
||||
})
|
||||
.unwrap();
|
||||
h.handle_datagram(Instant::from_micros(1000), &ack).unwrap();
|
||||
h
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn host_parsers_never_allocate_past_their_caps() {
|
||||
// Fragments of 4 billion pieces and gigabyte totals on every channel and sequence: at most the reassembly cap.
|
||||
let used = peak_of(|| {
|
||||
let mut h = connected();
|
||||
for i in 0..256u16 {
|
||||
let mut d = vec![0x80, 0x01, 0x00, 0x02];
|
||||
d.extend_from_slice(&[0x88, (i % 4) as u8]);
|
||||
d.extend_from_slice(&(i + 1).to_be_bytes());
|
||||
d.extend_from_slice(&(i + 1).to_be_bytes()); // start sequence
|
||||
d.extend_from_slice(&4u16.to_be_bytes());
|
||||
d.extend_from_slice(&u32::MAX.to_be_bytes());
|
||||
d.extend_from_slice(&0u32.to_be_bytes());
|
||||
d.extend_from_slice(&(MAX_PACKET_BYTES as u32).to_be_bytes());
|
||||
d.extend_from_slice(&0u32.to_be_bytes());
|
||||
d.extend_from_slice(&[1, 2, 3, 4]);
|
||||
let _ = h.handle_datagram(Instant::from_micros(2000), &d);
|
||||
let mut lie = d.clone();
|
||||
lie[20..24].copy_from_slice(&u32::MAX.to_be_bytes()); // total length
|
||||
|
||||
let _ = h.handle_datagram(Instant::from_micros(2000), &lie);
|
||||
}
|
||||
h
|
||||
});
|
||||
assert!(used <= 4 * MAX_PACKET_BYTES + 512 * 1024, "enet host: {used} bytes at peak");
|
||||
|
||||
// A control message whose length field claims 64 KiB.
|
||||
let lie = [0x01, 0x02, 0xFF, 0xFF, 1, 2, 3];
|
||||
let used = peak_of(|| common::implemented(&parse_client_message(&lie)));
|
||||
assert!(used <= budget(lie.len()), "client message: {used}");
|
||||
|
||||
// The control host fed sealed frames that lie inside.
|
||||
let used = peak_of(|| {
|
||||
let config = ControlHostConfig { enet: enet_config(), rikey: [3; 16], stop_timeout: None };
|
||||
let mut h = ControlHost::accept(config, Instant::ZERO, &connect()).unwrap();
|
||||
let cipher = ControlCipher::new([3; 16]);
|
||||
for seq in 0..64u32 {
|
||||
let frame = cipher.seal(seq, Direction::ClientToHost, &[0x06, 0x02, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF]).unwrap();
|
||||
let mut d = vec![0x80, 0x01, 0x00, 0x02, 0x86, 0x00];
|
||||
d.extend_from_slice(&((seq + 1) as u16).to_be_bytes());
|
||||
d.extend_from_slice(&(frame.len() as u16).to_be_bytes());
|
||||
d.extend_from_slice(&frame);
|
||||
let _ = h.handle_datagram(Instant::from_micros(2000), &d);
|
||||
while h.poll_event().is_some() {}
|
||||
}
|
||||
h
|
||||
});
|
||||
assert!(used <= 1024 * 1024, "control host: {used}");
|
||||
|
||||
// Arbitrary inputs.
|
||||
let mut runner = TestRunner::new(Config { cases: 300, ..Config::default() });
|
||||
runner
|
||||
.run(&proptest::collection::vec(any::<u8>(), 0..1500), |data| {
|
||||
let used = peak_of(|| common::implemented(&parse_client_message(&data)));
|
||||
prop_assert!(used <= budget(data.len()), "client message: {} for {}", used, data.len());
|
||||
let used = peak_of(|| common::implemented(&EnetHostPeer::accept(enet_config(), Instant::ZERO, &data)));
|
||||
prop_assert!(used <= budget(data.len()), "accept: {} for {}", used, data.len());
|
||||
Ok(())
|
||||
})
|
||||
.unwrap();
|
||||
}
|
||||
Reference in New Issue
Block a user