[domistream] Tests: rustfmt
This commit is contained in:
@@ -93,7 +93,9 @@ fn builder_matches_the_protocol_md_examples() {
|
||||
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")
|
||||
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"
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
@@ -101,7 +103,9 @@ fn vectors() -> Vec<(InputEvent, Vec<u8>)> {
|
||||
let full = GamepadState {
|
||||
controller: 15,
|
||||
active_mask: 0x8001,
|
||||
buttons: ButtonFlags(ButtonFlags::A | ButtonFlags::DPAD_UP | ButtonFlags::HOME | ButtonFlags::PADDLE1 | ButtonFlags::MISC),
|
||||
buttons: ButtonFlags(
|
||||
ButtonFlags::A | ButtonFlags::DPAD_UP | ButtonFlags::HOME | ButtonFlags::PADDLE1 | ButtonFlags::MISC,
|
||||
),
|
||||
left_trigger: 0x80,
|
||||
right_trigger: 0xFF,
|
||||
left_stick_x: i16::MIN,
|
||||
@@ -122,7 +126,10 @@ fn vectors() -> Vec<(InputEvent, Vec<u8>)> {
|
||||
(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::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)),
|
||||
@@ -149,11 +156,21 @@ fn vectors() -> Vec<(InputEvent, Vec<u8>)> {
|
||||
arrival(0, 0, 0x02, 0),
|
||||
),
|
||||
(
|
||||
InputEvent::ControllerArrival { controller: 15, kind: ControllerKind::Nintendo, capabilities: ControllerCapabilities(0), supported_buttons: ButtonFlags(ButtonFlags::A) },
|
||||
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) },
|
||||
InputEvent::ControllerArrival {
|
||||
controller: 3,
|
||||
kind: ControllerKind::Xbox,
|
||||
capabilities: ControllerCapabilities(0x23),
|
||||
supported_buttons: ButtonFlags(0xFFFF),
|
||||
},
|
||||
arrival(3, 1, 0x23, 0xFFFF),
|
||||
),
|
||||
]
|
||||
@@ -210,13 +227,28 @@ fn unknown_and_unsupported_types() {
|
||||
#[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 (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})");
|
||||
assert_err!(
|
||||
parse_input(&input_message(code, &data)),
|
||||
Error::Malformed { .. },
|
||||
"type {code:#x} with {wrong} bytes (wants {len})"
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -225,15 +257,78 @@ fn wrong_data_lengths() {
|
||||
fn lying_length_and_size_fields() {
|
||||
let good = rel(5, 5);
|
||||
let cases: Vec<(Vec<u8>, &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"),
|
||||
(
|
||||
{
|
||||
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 {
|
||||
@@ -246,7 +341,11 @@ 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");
|
||||
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]);
|
||||
@@ -276,39 +375,61 @@ fn bad_field_values() {
|
||||
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) },
|
||||
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<Value = InputEvent> {
|
||||
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::<u16>(), any::<u32>(), any::<u8>(), any::<u8>(), 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],
|
||||
});
|
||||
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::<u16>(), any::<u32>(), any::<u8>(), any::<u8>(), 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::<i16>(), any::<i16>()).prop_map(|(dx, dy)| InputEvent::MouseMoveRel { dx, dy }),
|
||||
(any::<i16>(), any::<i16>(), any::<u16>(), any::<u16>()).prop_map(|(x, y, width, height)| InputEvent::MouseMoveAbs { x, y, width, height }),
|
||||
(any::<i16>(), any::<i16>(), any::<u16>(), any::<u16>())
|
||||
.prop_map(|(x, y, width, height)| InputEvent::MouseMoveAbs { x, y, width, height }),
|
||||
(any::<bool>(), button).prop_map(|(pressed, button)| InputEvent::MouseButton { pressed, button }),
|
||||
(any::<bool>(), any::<u8>(), any::<u16>(), any::<u8>()).prop_map(|(pressed, flags, key_code, modifiers)| InputEvent::Key { pressed, flags, key_code, modifiers }),
|
||||
(any::<bool>(), any::<u8>(), any::<u16>(), any::<u8>())
|
||||
.prop_map(|(pressed, flags, key_code, modifiers)| InputEvent::Key { pressed, flags, key_code, modifiers }),
|
||||
any::<i16>().prop_map(|amount| InputEvent::Scroll { amount }),
|
||||
any::<i16>().prop_map(|amount| InputEvent::HScroll { amount }),
|
||||
gamepad.prop_map(InputEvent::Gamepad),
|
||||
(0u8..=15, kind, any::<u8>(), any::<u32>()).prop_map(|(controller, kind, c, b)| InputEvent::ControllerArrival {
|
||||
controller,
|
||||
kind,
|
||||
capabilities: ControllerCapabilities(c),
|
||||
supported_buttons: ButtonFlags(b),
|
||||
(0u8..=15, kind, any::<u8>(), any::<u32>()).prop_map(|(controller, kind, c, b)| {
|
||||
InputEvent::ControllerArrival {
|
||||
controller,
|
||||
kind,
|
||||
capabilities: ControllerCapabilities(c),
|
||||
supported_buttons: ButtonFlags(b),
|
||||
}
|
||||
}),
|
||||
]
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user