[domistream] Tests: rustfmt
This commit is contained in:
@@ -10,9 +10,7 @@ use core::time::Duration;
|
||||
|
||||
use common::SeqRng;
|
||||
use domistream::control::frame::{FRAME_HEADER_LEN, MAX_FRAME_BYTES, MIN_FRAME_BYTES, nonce};
|
||||
use domistream::control::message::{
|
||||
MAX_MESSAGE_BYTES, MotionKind, encode_client_message,
|
||||
};
|
||||
use domistream::control::message::{MAX_MESSAGE_BYTES, MotionKind, encode_client_message};
|
||||
use domistream::control::{
|
||||
ClientMessage, ControlCipher, ControlConfig, ControlEvent, ControlStream, Direction, HostMessage, MessageType,
|
||||
parse_encrypted_frame, parse_host_message, parse_message,
|
||||
@@ -62,7 +60,10 @@ fn nonce_layout() {
|
||||
// seq LE, then 6 zero bytes, then the direction byte, then 'C'.
|
||||
assert_eq!(nonce(0, Direction::ClientToHost).unwrap(), [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, b'C', b'C']);
|
||||
assert_eq!(nonce(1, Direction::HostToClient).unwrap(), [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, b'H', b'C']);
|
||||
assert_eq!(nonce(0x1234_5678, Direction::ClientToHost).unwrap(), [0x78, 0x56, 0x34, 0x12, 0, 0, 0, 0, 0, 0, b'C', b'C']);
|
||||
assert_eq!(
|
||||
nonce(0x1234_5678, Direction::ClientToHost).unwrap(),
|
||||
[0x78, 0x56, 0x34, 0x12, 0, 0, 0, 0, 0, 0, b'C', b'C']
|
||||
);
|
||||
for seq in [0xFFFF, 0x1_0000, 0xFFFF_FFFE, u32::MAX] {
|
||||
for d in [Direction::ClientToHost, Direction::HostToClient] {
|
||||
assert_eq!(nonce(seq, d).unwrap(), expected_nonce(seq, d), "{seq:#x} {d:?}");
|
||||
@@ -170,10 +171,20 @@ fn frame_parser_hostile_sizes() {
|
||||
assert_err!(parse_encrypted_frame(&[]), Error::Truncated { .. });
|
||||
assert_err!(parse_encrypted_frame(&[0x01]), Error::Truncated { .. });
|
||||
assert_err!(parse_encrypted_frame(&frame_of(MIN_FRAME_BYTES)[..MIN_FRAME_BYTES - 1]), Error::Truncated { .. });
|
||||
assert_err!(parse_encrypted_frame(&frame_of(FRAME_HEADER_LEN)), Error::Truncated { .. }, "no room for a message header");
|
||||
assert_err!(
|
||||
parse_encrypted_frame(&frame_of(FRAME_HEADER_LEN)),
|
||||
Error::Truncated { .. },
|
||||
"no room for a message header"
|
||||
);
|
||||
assert_eq!(parse_encrypted_frame(&frame_of(MIN_FRAME_BYTES)).unwrap().ciphertext.len(), 4);
|
||||
assert_eq!(parse_encrypted_frame(&frame_of(MAX_FRAME_BYTES - 1)).unwrap().ciphertext.len(), MAX_FRAME_BYTES - 1 - FRAME_HEADER_LEN);
|
||||
assert_eq!(parse_encrypted_frame(&frame_of(MAX_FRAME_BYTES)).unwrap().ciphertext.len(), MAX_FRAME_BYTES - FRAME_HEADER_LEN);
|
||||
assert_eq!(
|
||||
parse_encrypted_frame(&frame_of(MAX_FRAME_BYTES - 1)).unwrap().ciphertext.len(),
|
||||
MAX_FRAME_BYTES - 1 - FRAME_HEADER_LEN
|
||||
);
|
||||
assert_eq!(
|
||||
parse_encrypted_frame(&frame_of(MAX_FRAME_BYTES)).unwrap().ciphertext.len(),
|
||||
MAX_FRAME_BYTES - FRAME_HEADER_LEN
|
||||
);
|
||||
let mut over = frame_of(MAX_FRAME_BYTES);
|
||||
over.push(0);
|
||||
assert_too_large!(parse_encrypted_frame(&over), MAX_FRAME_BYTES, MAX_FRAME_BYTES + 1);
|
||||
@@ -185,7 +196,13 @@ fn frame_parser_lying_lengths() {
|
||||
let mut f = vec![0x01, 0x00, 0, 0, 1, 0, 0, 0];
|
||||
f.extend_from_slice(&[0; 16]);
|
||||
f.extend_from_slice(&[9; 10]);
|
||||
for (len, why) in [(f.len() - 4 + 1, "over by one"), (f.len() - 4 - 1, "under by one"), (0xFFFF, "max"), (0, "zero"), (20, "header only")] {
|
||||
for (len, why) in [
|
||||
(f.len() - 4 + 1, "over by one"),
|
||||
(f.len() - 4 - 1, "under by one"),
|
||||
(0xFFFF, "max"),
|
||||
(0, "zero"),
|
||||
(20, "header only"),
|
||||
] {
|
||||
let mut t = f.clone();
|
||||
t[2..4].copy_from_slice(&(len as u16).to_le_bytes());
|
||||
assert_err!(parse_encrypted_frame(&t), Error::Malformed { .. }, "{why}");
|
||||
@@ -225,7 +242,11 @@ fn message_split() {
|
||||
assert_err!(parse_message(&lie), Error::Malformed { .. }, "length 65535");
|
||||
let max = msg(0x7777, &vec![0; MAX_MESSAGE_BYTES - 4]);
|
||||
assert_eq!(parse_message(&max).unwrap().body.len(), MAX_MESSAGE_BYTES - 4);
|
||||
assert_too_large!(parse_message(&msg(0x7777, &vec![0; MAX_MESSAGE_BYTES - 3])), MAX_MESSAGE_BYTES, MAX_MESSAGE_BYTES + 1);
|
||||
assert_too_large!(
|
||||
parse_message(&msg(0x7777, &vec![0; MAX_MESSAGE_BYTES - 3])),
|
||||
MAX_MESSAGE_BYTES,
|
||||
MAX_MESSAGE_BYTES + 1
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -260,11 +281,26 @@ fn message_type_codes() {
|
||||
fn host_messages_from_the_field_tables() {
|
||||
// Host event bodies are taken as little-endian (UNKNOWN U23), except the extended termination code (BE).
|
||||
let cases = [
|
||||
(msg(0x010b, &[0, 0, 0, 0, 0x01, 0x00, 0xFF, 0x7F, 0x00, 0x80]), HostMessage::Rumble { controller: 1, low_frequency: 0x7FFF, high_frequency: 0x8000 }),
|
||||
(msg(0x5500, &[0x02, 0x00, 0x34, 0x12, 0xCD, 0xAB]), HostMessage::RumbleTriggers { controller: 2, left: 0x1234, right: 0xABCD }),
|
||||
(msg(0x5501, &[0x03, 0x00, 0xC8, 0x00, 0x01]), HostMessage::MotionEvent { controller: 3, rate_hz: 200, kind: MotionKind::Accelerometer }),
|
||||
(msg(0x5501, &[0x03, 0x00, 0x00, 0x00, 0x02]), HostMessage::MotionEvent { controller: 3, rate_hz: 0, kind: MotionKind::Gyroscope }),
|
||||
(msg(0x5502, &[0x03, 0x00, 0x10, 0x20, 0xFF]), HostMessage::RgbLed { controller: 3, r: 0x10, g: 0x20, b: 0xFF }),
|
||||
(
|
||||
msg(0x010b, &[0, 0, 0, 0, 0x01, 0x00, 0xFF, 0x7F, 0x00, 0x80]),
|
||||
HostMessage::Rumble { controller: 1, low_frequency: 0x7FFF, high_frequency: 0x8000 },
|
||||
),
|
||||
(
|
||||
msg(0x5500, &[0x02, 0x00, 0x34, 0x12, 0xCD, 0xAB]),
|
||||
HostMessage::RumbleTriggers { controller: 2, left: 0x1234, right: 0xABCD },
|
||||
),
|
||||
(
|
||||
msg(0x5501, &[0x03, 0x00, 0xC8, 0x00, 0x01]),
|
||||
HostMessage::MotionEvent { controller: 3, rate_hz: 200, kind: MotionKind::Accelerometer },
|
||||
),
|
||||
(
|
||||
msg(0x5501, &[0x03, 0x00, 0x00, 0x00, 0x02]),
|
||||
HostMessage::MotionEvent { controller: 3, rate_hz: 0, kind: MotionKind::Gyroscope },
|
||||
),
|
||||
(
|
||||
msg(0x5502, &[0x03, 0x00, 0x10, 0x20, 0xFF]),
|
||||
HostMessage::RgbLed { controller: 3, r: 0x10, g: 0x20, b: 0xFF },
|
||||
),
|
||||
(msg(0x0109, &[0x80, 0x03, 0x00, 0x23]), HostMessage::Termination { error_code: Some(0x8003_0023) }),
|
||||
(msg(0x0109, &[0x00, 0x00, 0x00, 0x01]), HostMessage::Termination { error_code: Some(1) }),
|
||||
(msg(0x0100, &[]), HostMessage::Termination { error_code: None }),
|
||||
@@ -296,11 +332,33 @@ fn host_messages_from_the_field_tables() {
|
||||
|
||||
#[test]
|
||||
fn host_messages_with_wrong_bodies() {
|
||||
for (ty, len) in [(0x010b, 9), (0x010b, 11), (0x5500, 5), (0x5500, 7), (0x5501, 4), (0x5501, 6), (0x5502, 4), (0x5502, 6), (0x0109, 3), (0x0109, 5), (0x010e, 0), (0x010e, 10), (0x010e, 32)] {
|
||||
assert_err!(parse_host_message(&msg(ty, &vec![0x01; len])), Error::Malformed { .. }, "{ty:#06x} with {len} bytes");
|
||||
for (ty, len) in [
|
||||
(0x010b, 9),
|
||||
(0x010b, 11),
|
||||
(0x5500, 5),
|
||||
(0x5500, 7),
|
||||
(0x5501, 4),
|
||||
(0x5501, 6),
|
||||
(0x5502, 4),
|
||||
(0x5502, 6),
|
||||
(0x0109, 3),
|
||||
(0x0109, 5),
|
||||
(0x010e, 0),
|
||||
(0x010e, 10),
|
||||
(0x010e, 32),
|
||||
] {
|
||||
assert_err!(
|
||||
parse_host_message(&msg(ty, &vec![0x01; len])),
|
||||
Error::Malformed { .. },
|
||||
"{ty:#06x} with {len} bytes"
|
||||
);
|
||||
}
|
||||
for kind in [0u8, 3, 0xFF] {
|
||||
assert_err!(parse_host_message(&msg(0x5501, &[0, 0, 1, 0, kind])), Error::UnexpectedValue { .. }, "motion kind {kind}");
|
||||
assert_err!(
|
||||
parse_host_message(&msg(0x5501, &[0, 0, 1, 0, kind])),
|
||||
Error::UnexpectedValue { .. },
|
||||
"motion kind {kind}"
|
||||
);
|
||||
}
|
||||
assert_err!(parse_host_message(&[0x0b, 0x01]), Error::Truncated { .. });
|
||||
let mut lie = msg(0x010b, &[0; 10]);
|
||||
@@ -318,9 +376,13 @@ fn client_messages() {
|
||||
want.extend_from_slice(&[0; 12]);
|
||||
assert_eq!(m, want);
|
||||
let ev = InputEvent::MouseMoveRel { dx: -1, dy: 0 };
|
||||
assert_eq!(encode_client_message(&ClientMessage::Input(ev.clone())).unwrap(), domistream::input::encode_input(&ev).unwrap());
|
||||
assert_eq!(
|
||||
encode_client_message(&ClientMessage::Input(ev.clone())).unwrap(),
|
||||
domistream::input::encode_input(&ev).unwrap()
|
||||
);
|
||||
// Bodies still UNKNOWN (U10) are refused rather than guessed.
|
||||
for m in [ClientMessage::StartA, ClientMessage::StartB, ClientMessage::PeriodicPing, ClientMessage::RequestIdrFrame] {
|
||||
for m in [ClientMessage::StartA, ClientMessage::StartB, ClientMessage::PeriodicPing, ClientMessage::RequestIdrFrame]
|
||||
{
|
||||
assert_err!(encode_client_message(&m), Error::Unsupported { .. }, "{m:?}");
|
||||
}
|
||||
}
|
||||
@@ -357,7 +419,13 @@ const KEY: [u8; 16] = *b"control-test-key";
|
||||
|
||||
fn stream_config() -> ControlConfig {
|
||||
ControlConfig {
|
||||
enet: EnetConfig { channel_count: 1, mtu: 1400, connect_data: 0, ping_interval: Duration::from_millis(500), timeout: Duration::from_secs(10) },
|
||||
enet: EnetConfig {
|
||||
channel_count: 1,
|
||||
mtu: 1400,
|
||||
connect_data: 0,
|
||||
ping_interval: Duration::from_millis(500),
|
||||
timeout: Duration::from_secs(10),
|
||||
},
|
||||
rikey: KEY,
|
||||
ping_interval: Duration::from_millis(100),
|
||||
}
|
||||
@@ -374,9 +442,20 @@ struct Host {
|
||||
impl Host {
|
||||
fn deliver(&mut self, frame: Vec<u8>) {
|
||||
self.reliable = self.reliable.wrapping_add(1);
|
||||
let c = Command { acknowledge: true, channel_id: 0, reliable_sequence: self.reliable, body: CommandBody::SendReliable(frame) };
|
||||
let c = Command {
|
||||
acknowledge: true,
|
||||
channel_id: 0,
|
||||
reliable_sequence: self.reliable,
|
||||
body: CommandBody::SendReliable(frame),
|
||||
};
|
||||
self.now_ms += 1;
|
||||
let d = encode_datagram(&Datagram { peer_id: self.to_client, session_id: 0, sent_time: Some(self.now_ms as u16), commands: vec![c] }).unwrap();
|
||||
let d = encode_datagram(&Datagram {
|
||||
peer_id: self.to_client,
|
||||
session_id: 0,
|
||||
sent_time: Some(self.now_ms as u16),
|
||||
commands: vec![c],
|
||||
})
|
||||
.unwrap();
|
||||
self.stream.handle_datagram(Instant::from_micros(self.now_ms * 1000), &d).expect("a well-formed datagram");
|
||||
}
|
||||
|
||||
@@ -410,7 +489,8 @@ impl Host {
|
||||
}
|
||||
|
||||
fn connected_stream() -> Host {
|
||||
let mut stream = ControlStream::connect(stream_config(), Instant::ZERO, &mut SeqRng::new(11)).expect("ControlStream::connect");
|
||||
let mut stream =
|
||||
ControlStream::connect(stream_config(), Instant::ZERO, &mut SeqRng::new(11)).expect("ControlStream::connect");
|
||||
let d = parse_datagram(&stream.poll_transmit(Instant::ZERO).expect("connect datagram")).unwrap();
|
||||
let (params, seq) = d
|
||||
.commands
|
||||
@@ -426,19 +506,36 @@ fn connected_stream() -> Host {
|
||||
session_id: 0,
|
||||
sent_time: Some(1),
|
||||
commands: vec![
|
||||
Command { acknowledge: false, channel_id: 0xFF, reliable_sequence: 0, body: CommandBody::Acknowledge { received_reliable_sequence: seq, received_sent_time: d.sent_time.unwrap_or(0) } },
|
||||
Command { acknowledge: true, channel_id: 0xFF, reliable_sequence: 1, body: CommandBody::VerifyConnect(verify) },
|
||||
Command {
|
||||
acknowledge: false,
|
||||
channel_id: 0xFF,
|
||||
reliable_sequence: 0,
|
||||
body: CommandBody::Acknowledge {
|
||||
received_reliable_sequence: seq,
|
||||
received_sent_time: d.sent_time.unwrap_or(0),
|
||||
},
|
||||
},
|
||||
Command {
|
||||
acknowledge: true,
|
||||
channel_id: 0xFF,
|
||||
reliable_sequence: 1,
|
||||
body: CommandBody::VerifyConnect(verify),
|
||||
},
|
||||
],
|
||||
})
|
||||
.unwrap();
|
||||
stream.handle_datagram(Instant::from_micros(1000), &reply).unwrap();
|
||||
let mut h = Host { stream, to_client: params.outgoing_peer_id, reliable: 0, now_ms: 1, host: ControlCipher::new(KEY) };
|
||||
let mut h =
|
||||
Host { stream, to_client: params.outgoing_peer_id, reliable: 0, now_ms: 1, host: ControlCipher::new(KEY) };
|
||||
assert_eq!(h.events(), vec![ControlEvent::Connected]);
|
||||
h
|
||||
}
|
||||
|
||||
fn rumble(controller: u16) -> (Vec<u8>, HostMessage) {
|
||||
(msg(0x010b, &[0, 0, 0, 0, controller as u8, 0, 1, 0, 2, 0]), HostMessage::Rumble { controller, low_frequency: 1, high_frequency: 2 })
|
||||
(
|
||||
msg(0x010b, &[0, 0, 0, 0, controller as u8, 0, 1, 0, 2, 0]),
|
||||
HostMessage::Rumble { controller, low_frequency: 1, high_frequency: 2 },
|
||||
)
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
Reference in New Issue
Block a user