use super::super::super::*;
#[test]
fn mmio_read_oversized_fills_0xff() {
let dev = VirtioConsole::new();
let mut buf = [0u8; 8];
dev.mmio_read(VIRTIO_MMIO_VERSION as u64, &mut buf);
assert_eq!(
buf, [0xff; 8],
"8-byte read must fill with 0xff — the device is 4-byte \
register width per virtio-v1.2 §4.2.2",
);
}
#[test]
fn mmio_read_1byte_fills_0xff() {
let dev = VirtioConsole::new();
let mut buf = [0u8; 1];
dev.mmio_read(VIRTIO_MMIO_VERSION as u64, &mut buf);
assert_eq!(buf, [0xff], "1-byte read must fill with 0xff",);
}
#[test]
fn mmio_read_3byte_fills_0xff() {
let dev = VirtioConsole::new();
let mut buf = [0u8; 3];
dev.mmio_read(VIRTIO_MMIO_VERSION as u64, &mut buf);
assert_eq!(
buf,
[0xff, 0xff, 0xff],
"3-byte read must fill with 0xff — the device is exactly \
4-byte register width, not 'at least 4'",
);
}
#[test]
fn mmio_read_4byte_returns_register_value() {
let dev = VirtioConsole::new();
let mut buf = [0u8; 4];
dev.mmio_read(VIRTIO_MMIO_VERSION as u64, &mut buf);
assert_eq!(
u32::from_le_bytes(buf),
MMIO_VERSION,
"4-byte read at VIRTIO_MMIO_VERSION must return the \
actual register value, NOT the 0xff-fill defense",
);
}
#[test]
fn config_read_out_of_range_fills_0xff() {
let dev = VirtioConsole::new();
let mut buf = [0u8; 4];
dev.mmio_read(0x100 + 10, &mut buf);
assert_eq!(
buf, [0xff; 4],
"config_read past byte 11 must fill 0xff — the defense \
prevents a panic from cfg[start..end] when end > 12",
);
}
#[test]
fn config_read_one_byte_past_end_fills_0xff() {
let dev = VirtioConsole::new();
let mut buf = [0u8; 4];
dev.mmio_read(0x100 + 9, &mut buf);
assert_eq!(
buf, [0xff; 4],
"config_read with end one byte past struct must fill 0xff",
);
}
#[test]
fn config_read_at_exact_end_returns_data() {
let dev = VirtioConsole::new();
let mut buf = [0u8; 4];
dev.mmio_read(0x100 + 8, &mut buf);
assert_eq!(
buf, [0; 4],
"read at the last 4-byte slot of the config struct \
(emerg_wr, offset 8..12) must return actual data, NOT \
the 0xff out-of-range fill",
);
}
#[test]
fn config_read_1byte_inside_struct_returns_data() {
let dev = VirtioConsole::new();
let mut buf = [0u8; 1];
dev.mmio_read(0x100, &mut buf);
assert_eq!(
buf,
[0],
"1-byte read inside config struct must return actual \
data, not 0xff fill",
);
}