virtio-devices: Test PCI CFG access length clamping

Verify that bar_access_params clamps the access length to cap.length
when the PCI config read buffer is larger.

Signed-off-by: Anatol Belski <anbelski@linux.microsoft.com>
This commit is contained in:
Anatol Belski
2026-05-23 16:09:39 +02:00
committed by Rob Bradford
parent feb1c4a2d6
commit 28aa81e66b

View File

@@ -1486,4 +1486,19 @@ mod unit_tests {
(size_of::<VirtioPciCap64>() as u8) + VIRTIO_PCI_CAP_LEN_OFFSET
);
}
#[test]
fn bar_access_params_clamps_to_cap_length() {
let mut cap = VirtioPciCfgCap::new();
let slice = cap.as_mut_slice();
// Program cap.offset = 0x14, cap.length = 1 (byte access)
slice[6..10].copy_from_slice(&0x14u32.to_le_bytes());
slice[10..14].copy_from_slice(&1u32.to_le_bytes());
// PCI config reads always produce 4 bytes, but cap.length = 1
let (bar_offset, access_len) = cap.bar_access_params(4);
assert_eq!(bar_offset, 0x14);
assert_eq!(access_len, 1);
}
}