From fbfcd588a5e296c36441839affe8c8d4a28b7113 Mon Sep 17 00:00:00 2001 From: mnovak Date: Wed, 25 Nov 2026 12:51:04 +0100 Subject: [PATCH] [domistream] Tests: input vectors from the field tables INPUT_DATA built from PROTOCOL.md 4 field tables, cross-checked against its example bytes; encode, parse, equal for every known type; unknown types, UNKNOWN-layout types, wrong lengths, lying length and size fields, changed constants and out-of-range values refused. --- crates/domistream/tests/input.rs | 343 +++++++++++++++++++++++++++++++ 1 file changed, 343 insertions(+) create mode 100644 crates/domistream/tests/input.rs diff --git a/crates/domistream/tests/input.rs b/crates/domistream/tests/input.rs new file mode 100644 index 0000000..7dc29b2 --- /dev/null +++ b/crates/domistream/tests/input.rs @@ -0,0 +1,343 @@ +//! INPUT_DATA (`docs/PROTOCOL.md` 4): vectors rebuilt from the field tables with our own builder, cross-checked +//! against the example bytes in PROTOCOL.md; encode, parse, equal; refusals of unknown types and wrong lengths. + +#![allow(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::indexing_slicing, missing_docs)] + +mod common; + +use common::hex; +use domistream::Error; +use domistream::input::{ + ButtonFlags, ControllerCapabilities, ControllerKind, GamepadState, INPUT_HEADER_LEN, InputEvent, InputType, + MAX_INPUT_MESSAGE_BYTES, MouseButton, encode_input, parse_input, +}; +use proptest::prelude::*; + +/// `0x0206 LE ‖ length LE (= 4 + size) ‖ size BE (= 4 + data) ‖ input type LE ‖ data`. +fn input_message(input_type: u32, data: &[u8]) -> Vec { + let size = 4 + data.len(); + let mut m = 0x0206u16.to_le_bytes().to_vec(); + m.extend_from_slice(&((4 + size) as u16).to_le_bytes()); + m.extend_from_slice(&(size as u32).to_be_bytes()); + m.extend_from_slice(&input_type.to_le_bytes()); + m.extend_from_slice(data); + m +} + +fn be16(v: i16) -> [u8; 2] { + v.to_be_bytes() +} + +fn rel(dx: i16, dy: i16) -> Vec { + input_message(0x07, &[be16(dx), be16(dy)].concat()) +} + +fn abs(x: i16, y: i16, w: u16, h: u16) -> Vec { + input_message(0x05, &[&be16(x)[..], &[0, 0], &w.to_be_bytes(), &h.to_be_bytes()].concat()) +} + +fn button(down: bool, b: u8) -> Vec { + input_message(if down { 0x08 } else { 0x09 }, &[b]) +} + +fn key(down: bool, flags: u8, code: u16, modifiers: u8) -> Vec { + input_message(if down { 0x03 } else { 0x04 }, &[&[flags][..], &code.to_le_bytes(), &[modifiers, 0, 0]].concat()) +} + +fn scroll(amount: i16) -> Vec { + // amount1 and amount2 both carry the amount, as in the PROTOCOL.md example; hosts read amount1. + input_message(0x0A, &[&be16(amount)[..], &be16(amount), &[0, 0]].concat()) +} + +fn hscroll(amount: i16) -> Vec { + input_message(0x5500_0001, &be16(amount)) +} + +fn gamepad(g: &GamepadState) -> Vec { + let mut d = Vec::new(); + d.extend_from_slice(&0x001Au16.to_le_bytes()); + d.extend_from_slice(&u16::from(g.controller).to_le_bytes()); + d.extend_from_slice(&g.active_mask.to_le_bytes()); + d.extend_from_slice(&0x0014u16.to_le_bytes()); + d.extend_from_slice(&((g.buttons.0 & 0xFFFF) as u16).to_le_bytes()); + d.push(g.left_trigger); + d.push(g.right_trigger); + for v in [g.left_stick_x, g.left_stick_y, g.right_stick_x, g.right_stick_y] { + d.extend_from_slice(&v.to_le_bytes()); + } + d.extend_from_slice(&0x009Cu16.to_le_bytes()); + d.extend_from_slice(&((g.buttons.0 >> 16) as u16).to_le_bytes()); + d.extend_from_slice(&0x0055u16.to_le_bytes()); + assert_eq!(d.len(), 26); + input_message(0x0C, &d) +} + +/// Arrival: controller, type, capabilities, supported buttons (u32, taken as LE like the other gamepad fields: +/// PROTOCOL.md gives no byte order, UNKNOWN U22). +fn arrival(controller: u8, kind: u8, caps: u8, buttons: u32) -> Vec { + input_message(0x5500_0004, &[&[controller, kind, caps][..], &buttons.to_le_bytes()].concat()) +} + +fn pad(controller: u8, mask: u16, ry: i16) -> GamepadState { + GamepadState { controller, active_mask: mask, right_stick_y: ry, ..GamepadState::default() } +} + +#[test] +fn builder_matches_the_protocol_md_examples() { + // Our field-table builder reproduces every example in PROTOCOL.md §4 byte for byte. + assert_eq!(rel(-1, 0), hex("06 02 0C 00 00 00 00 08 07 00 00 00 FF FF 00 00")); + assert_eq!(abs(1464, 528, 1919, 1079), hex("06 02 12 00 00 00 00 0E 05 00 00 00 05 B8 02 10 00 00 07 7F 04 37")); + assert_eq!(button(true, 1), hex("06 02 09 00 00 00 00 05 08 00 00 00 01")); + assert_eq!(key(true, 0, 0x80A4, 0x04), hex("06 02 0E 00 00 00 00 0A 03 00 00 00 00 A4 80 04 00 00")); + assert_eq!(scroll(-120), hex("06 02 0E 00 00 00 00 0A 0A 00 00 00 FF 88 FF 88 00 00")); + assert_eq!(hscroll(30), hex("06 02 0A 00 00 00 00 06 01 00 00 55 00 1E")); + assert_eq!( + gamepad(&pad(0, 1, -104)), + hex("06 02 22 00 00 00 00 1E 0C 00 00 00 1A 00 00 00 01 00 14 00 00 00 00 00 00 00 00 00 00 00 98 FF 9C 00 00 00 55 00") + ); +} + +fn vectors() -> Vec<(InputEvent, Vec)> { + let full = GamepadState { + controller: 15, + active_mask: 0x8001, + buttons: ButtonFlags(ButtonFlags::A | ButtonFlags::DPAD_UP | ButtonFlags::HOME | ButtonFlags::PADDLE1 | ButtonFlags::MISC), + left_trigger: 0x80, + right_trigger: 0xFF, + left_stick_x: i16::MIN, + left_stick_y: i16::MAX, + right_stick_x: -1, + right_stick_y: 1, + }; + vec![ + (InputEvent::MouseMoveRel { dx: -1, dy: 0 }, rel(-1, 0)), + (InputEvent::MouseMoveRel { dx: i16::MIN, dy: i16::MAX }, rel(i16::MIN, i16::MAX)), + (InputEvent::MouseMoveAbs { x: 1464, y: 528, width: 1919, height: 1079 }, abs(1464, 528, 1919, 1079)), + (InputEvent::MouseMoveAbs { x: -5, y: 0, width: 0xFFFF, height: 1 }, abs(-5, 0, 0xFFFF, 1)), + (InputEvent::MouseButton { pressed: true, button: MouseButton::Left }, button(true, 1)), + (InputEvent::MouseButton { pressed: false, button: MouseButton::Left }, button(false, 1)), + (InputEvent::MouseButton { pressed: true, button: MouseButton::Middle }, button(true, 2)), + (InputEvent::MouseButton { pressed: false, button: MouseButton::Right }, button(false, 3)), + (InputEvent::MouseButton { pressed: true, button: MouseButton::Side }, button(true, 4)), + (InputEvent::MouseButton { pressed: true, button: MouseButton::Extra }, button(true, 5)), + (InputEvent::Key { pressed: true, flags: 0, key_code: 0x80A4, modifiers: 0x04 }, key(true, 0, 0x80A4, 0x04)), + (InputEvent::Key { pressed: false, flags: 0, key_code: 0x0041, modifiers: 0 }, key(false, 0, 0x0041, 0)), + (InputEvent::Key { pressed: true, flags: 0xFF, key_code: 0xFFFF, modifiers: 0xFF }, key(true, 0xFF, 0xFFFF, 0xFF)), + (InputEvent::Scroll { amount: -120 }, scroll(-120)), + (InputEvent::Scroll { amount: 120 }, scroll(120)), + (InputEvent::HScroll { amount: 30 }, hscroll(30)), + (InputEvent::HScroll { amount: -32768 }, hscroll(-32768)), + (InputEvent::Gamepad(pad(0, 1, -104)), gamepad(&pad(0, 1, -104))), + (InputEvent::Gamepad(pad(1, 0, 0)), gamepad(&pad(1, 0, 0))), + (InputEvent::Gamepad(full), gamepad(&full)), + ( + InputEvent::ControllerArrival { + controller: 2, + kind: ControllerKind::PlayStation, + capabilities: ControllerCapabilities(0xFF), + supported_buttons: ButtonFlags(0x003F_FFFF), + }, + arrival(2, 2, 0xFF, 0x003F_FFFF), + ), + ( + InputEvent::ControllerArrival { + controller: 0, + kind: ControllerKind::Unknown, + capabilities: ControllerCapabilities(ControllerCapabilities::RUMBLE), + supported_buttons: ButtonFlags(0), + }, + arrival(0, 0, 0x02, 0), + ), + ( + InputEvent::ControllerArrival { controller: 15, kind: ControllerKind::Nintendo, capabilities: ControllerCapabilities(0), supported_buttons: ButtonFlags(ButtonFlags::A) }, + arrival(15, 3, 0, 0x1000), + ), + ( + InputEvent::ControllerArrival { controller: 3, kind: ControllerKind::Xbox, capabilities: ControllerCapabilities(0x23), supported_buttons: ButtonFlags(0xFFFF) }, + arrival(3, 1, 0x23, 0xFFFF), + ), + ] +} + +#[test] +fn encode_then_parse_then_equal() { + for (event, bytes) in vectors() { + assert_eq!(encode_input(&event).unwrap(), bytes, "encode {event:?}"); + assert_eq!(parse_input(&bytes).unwrap(), event, "parse {event:?}"); + assert_eq!(parse_input(&encode_input(&event).unwrap()).unwrap(), event); + } +} + +#[test] +fn input_type_codes() { + for (code, t) in [ + (0x03, InputType::KeyDown), + (0x04, InputType::KeyUp), + (0x05, InputType::MouseMoveAbs), + (0x07, InputType::MouseMoveRel), + (0x08, InputType::MouseButtonDown), + (0x09, InputType::MouseButtonUp), + (0x0A, InputType::MouseScroll), + (0x0C, InputType::ControllerMulti), + (0x0D, InputType::Haptics), + (0x17, InputType::Utf8Text), + (0x5500_0001, InputType::MouseHScroll), + (0x5500_0002, InputType::Touch), + (0x5500_0003, InputType::Pen), + (0x5500_0004, InputType::ControllerArrival), + (0x5500_0005, InputType::ControllerTouch), + (0x5500_0006, InputType::ControllerMotion), + (0x5500_0007, InputType::ControllerBattery), + ] { + assert_eq!(InputType::try_from(code), Ok(t), "{code:#x}"); + } + for code in [0u32, 1, 2, 6, 0x0B, 0x0E, 0x16, 0x18, 0x5500_0000, 0x5500_0008, 0x0100_0007, u32::MAX] { + assert_err!(InputType::try_from(code), Error::UnexpectedValue { .. }, "{code:#x}"); + } +} + +#[test] +fn unknown_and_unsupported_types() { + for code in [0u32, 1, 6, 0x5500_0008, 0x0700_0000, u32::MAX] { + assert_err!(parse_input(&input_message(code, &[0; 4])), Error::UnexpectedValue { .. }, "type {code:#x}"); + } + // Known types whose layout is UNKNOWN U21. + for code in [0x0D, 0x17, 0x5500_0002, 0x5500_0003, 0x5500_0005, 0x5500_0006, 0x5500_0007] { + assert_err!(parse_input(&input_message(code, &[0; 8])), Error::Unsupported { .. }, "type {code:#x}"); + } +} + +#[test] +fn wrong_data_lengths() { + // (type, correct data length) + for (code, len) in [(0x07u32, 4usize), (0x05, 10), (0x08, 1), (0x09, 1), (0x03, 6), (0x04, 6), (0x0A, 6), (0x5500_0001, 2), (0x0C, 26), (0x5500_0004, 7)] { + for wrong in [0, len - 1, len + 1, len + 16] { + let mut data = vec![0u8; wrong]; + if code == 0x08 || code == 0x09 { + data.fill(1); // a valid button number, so only the length is wrong + } + assert_err!(parse_input(&input_message(code, &data)), Error::Malformed { .. }, "type {code:#x} with {wrong} bytes (wants {len})"); + } + } +} + +#[test] +fn lying_length_and_size_fields() { + let good = rel(5, 5); + let cases: Vec<(Vec, &str)> = vec![ + ({ let mut m = good.clone(); m[2] += 1; m }, "length over"), + ({ let mut m = good.clone(); m[2] -= 1; m }, "length under"), + ({ let mut m = good.clone(); m[2..4].copy_from_slice(&0xFFFFu16.to_le_bytes()); m }, "length 65535"), + ({ let mut m = good.clone(); m[7] += 1; m }, "size over"), + ({ let mut m = good.clone(); m[7] -= 1; m }, "size under"), + ({ let mut m = good.clone(); m[4..8].copy_from_slice(&u32::MAX.to_be_bytes()); m }, "size u32::MAX"), + ({ let mut m = good.clone(); m[4..8].copy_from_slice(&8u32.to_le_bytes()); m }, "size written little-endian"), + ({ let mut m = good.clone(); m[0..2].copy_from_slice(&0x0207u16.to_le_bytes()); m }, "not INPUT_DATA"), + ({ let mut m = good.clone(); m[0..2].copy_from_slice(&0x0206u16.to_be_bytes()); m }, "message type big-endian"), + ([good.clone(), vec![0]].concat(), "trailing byte"), + ]; + for (m, why) in cases { + assert_err!(parse_input(&m), Error::Malformed { .. }, "{why}"); + } +} + +#[test] +fn hostile_sizes() { + assert_err!(parse_input(&[]), Error::Truncated { .. }); + assert_err!(parse_input(&[0x06]), Error::Truncated { .. }); + assert_err!(parse_input(&rel(1, 1)[..INPUT_HEADER_LEN - 1]), Error::Truncated { .. }); + assert_err!(parse_input(&input_message(0x07, &[])[..INPUT_HEADER_LEN]), Error::Malformed { .. }, "header only: no data"); + // max - 1, max, max + 1, huge: valid framing, data too long for any known type. + for len in [MAX_INPUT_MESSAGE_BYTES - 1, MAX_INPUT_MESSAGE_BYTES] { + let m = input_message(0x07, &vec![0; len - INPUT_HEADER_LEN]); + assert_eq!(m.len(), len); + assert_err!(parse_input(&m), Error::Malformed { .. }, "{len}"); + } + let m = input_message(0x07, &vec![0; MAX_INPUT_MESSAGE_BYTES + 1 - INPUT_HEADER_LEN]); + assert_too_large!(parse_input(&m), MAX_INPUT_MESSAGE_BYTES, MAX_INPUT_MESSAGE_BYTES + 1); + assert_too_large!(parse_input(&vec![0; 1 << 20]), MAX_INPUT_MESSAGE_BYTES, 1 << 20); +} + +#[test] +fn bad_field_values() { + for b in [0u8, 6, 0xFF] { + assert_err!(parse_input(&button(true, b)), Error::UnexpectedValue { .. }, "button {b}"); + } + let mut g = gamepad(&pad(16, 1, 0)); + assert_err!(parse_input(&g), Error::UnexpectedValue { .. }, "controller 16"); + for (offset, why) in [(12usize, "0x1A"), (18, "0x14"), (32, "0x9C"), (36, "0x55")] { + g = gamepad(&pad(0, 1, 0)); + g[offset] ^= 0x01; + assert_err!(parse_input(&g), Error::Malformed { .. }, "constant {why} changed"); + } + assert_err!(parse_input(&arrival(16, 1, 0, 0)), Error::UnexpectedValue { .. }, "arrival controller 16"); + assert_err!(parse_input(&arrival(0, 4, 0, 0)), Error::UnexpectedValue { .. }, "controller kind 4"); + + for e in [ + InputEvent::Gamepad(pad(16, 1, 0)), + InputEvent::Gamepad(pad(255, 1, 0)), + InputEvent::ControllerArrival { controller: 16, kind: ControllerKind::Xbox, capabilities: ControllerCapabilities(0), supported_buttons: ButtonFlags(0) }, + ] { + assert_err!(encode_input(&e), Error::UnexpectedValue { .. }, "{e:?}"); + } +} + +fn event() -> impl Strategy { + let button = prop_oneof![Just(MouseButton::Left), Just(MouseButton::Middle), Just(MouseButton::Right), Just(MouseButton::Side), Just(MouseButton::Extra)]; + let kind = prop_oneof![Just(ControllerKind::Unknown), Just(ControllerKind::Xbox), Just(ControllerKind::PlayStation), Just(ControllerKind::Nintendo)]; + let gamepad = (0u8..=15, any::(), any::(), any::(), any::(), any::<[i16; 4]>()).prop_map(|(c, m, b, l, r, s)| GamepadState { + controller: c, + active_mask: m, + buttons: ButtonFlags(b), + left_trigger: l, + right_trigger: r, + left_stick_x: s[0], + left_stick_y: s[1], + right_stick_x: s[2], + right_stick_y: s[3], + }); + prop_oneof![ + (any::(), any::()).prop_map(|(dx, dy)| InputEvent::MouseMoveRel { dx, dy }), + (any::(), any::(), any::(), any::()).prop_map(|(x, y, width, height)| InputEvent::MouseMoveAbs { x, y, width, height }), + (any::(), button).prop_map(|(pressed, button)| InputEvent::MouseButton { pressed, button }), + (any::(), any::(), any::(), any::()).prop_map(|(pressed, flags, key_code, modifiers)| InputEvent::Key { pressed, flags, key_code, modifiers }), + any::().prop_map(|amount| InputEvent::Scroll { amount }), + any::().prop_map(|amount| InputEvent::HScroll { amount }), + gamepad.prop_map(InputEvent::Gamepad), + (0u8..=15, kind, any::(), any::()).prop_map(|(controller, kind, c, b)| InputEvent::ControllerArrival { + controller, + kind, + capabilities: ControllerCapabilities(c), + supported_buttons: ButtonFlags(b), + }), + ] +} + +proptest! { + #![proptest_config(ProptestConfig { cases: 1024, ..ProptestConfig::default() })] + + #[test] + fn any_event_round_trips(e in event()) { + let bytes = encode_input(&e).map_err(|err| TestCaseError::fail(format!("{err:?}")))?; + prop_assert!(bytes.len() <= MAX_INPUT_MESSAGE_BYTES); + prop_assert_eq!(&bytes[..2], &[0x06, 0x02]); + prop_assert_eq!(u16::from_le_bytes([bytes[2], bytes[3]]) as usize, bytes.len() - 4); + prop_assert_eq!(u32::from_be_bytes([bytes[4], bytes[5], bytes[6], bytes[7]]) as usize, bytes.len() - 8); + prop_assert_eq!(parse_input(&bytes), Ok(e)); + } + + /// Arbitrary bytes never panic; whatever parses re-encodes to an equal event. + #[test] + fn parse_is_stable(data in proptest::collection::vec(any::(), 0..64)) { + if let Ok(e) = parse_input(&data) { + let again = encode_input(&e).map_err(|err| TestCaseError::fail(format!("{err:?}")))?; + prop_assert_eq!(parse_input(&again), Ok(e)); + } + } + + /// Valid framing around random data of every known type never panics. + #[test] + fn framed_random_data(code in prop_oneof![Just(3u32), Just(4), Just(5), Just(7), Just(8), Just(9), Just(0x0A), Just(0x0C), Just(0x5500_0001), Just(0x5500_0004)], data in proptest::collection::vec(any::(), 0..40)) { + let _ = parse_input(&input_message(code, &data)); + } +}