From 021838b63cb963b4bb8e323f8d2ab74a1c54557b Mon Sep 17 00:00:00 2001 From: Anatol Belski Date: Mon, 13 Apr 2026 22:30:25 +0200 Subject: [PATCH] block: qcow: Add aligned_pread bounce buffer test Test that aligned_pread correctly uses a bounce buffer when the caller buffer address is not aligned. Signed-off-by: Anatol Belski --- block/src/qcow_sync.rs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/block/src/qcow_sync.rs b/block/src/qcow_sync.rs index c3e57da2b..cf3b27c25 100644 --- a/block/src/qcow_sync.rs +++ b/block/src/qcow_sync.rs @@ -1752,4 +1752,21 @@ mod unit_tests { let expected: Vec = (0..size).map(|i| (i % 251) as u8).collect(); assert_eq!(abuf.as_slice(size), &expected[..]); } + + #[test] + fn test_aligned_pread_bounce_unaligned_buffer() { + // Force a misaligned buffer so aligned_pread must take the + // bounce path. A plain vec![0u8; 4096] is often page-aligned + // by the allocator, which would skip the bounce entirely. + let size = 4096usize; + let (_tf, fd) = create_pattern_file(size); + let alignment = 512; + + let mut backing = vec![0u8; size + 1]; + let buf = &mut backing[1..size + 1]; + aligned_pread(fd, buf, 0, alignment).unwrap(); + + let expected: Vec = (0..size).map(|i| (i % 251) as u8).collect(); + assert_eq!(buf, &expected[..]); + } }