From 05d52d035371047f0e82a77b422ef20a0a9659a1 Mon Sep 17 00:00:00 2001 From: Anatol Belski Date: Mon, 13 Apr 2026 22:31:35 +0200 Subject: [PATCH] block: qcow: Add aligned_pread unaligned offset test Test that aligned_pread handles a non aligned offset by rounding down, reading an aligned region, and returning the correct slice from within the bounce buffer. Signed-off-by: Anatol Belski --- block/src/qcow_sync.rs | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/block/src/qcow_sync.rs b/block/src/qcow_sync.rs index cf3b27c25..5b4a6b216 100644 --- a/block/src/qcow_sync.rs +++ b/block/src/qcow_sync.rs @@ -1769,4 +1769,24 @@ mod unit_tests { let expected: Vec = (0..size).map(|i| (i % 251) as u8).collect(); assert_eq!(buf, &expected[..]); } + + #[test] + fn test_aligned_pread_unaligned_offset() { + // Read at an offset that is not a multiple of alignment. + // aligned_pread should round down the offset, read an aligned + // region, then copy the correct slice into the caller buffer. + let file_size = 8192usize; + let (_tf, fd) = create_pattern_file(file_size); + let alignment = 512; + + let offset = 100u64; + let len = 200usize; + let mut buf = vec![0u8; len]; + aligned_pread(fd, &mut buf, offset, alignment).unwrap(); + + let expected: Vec = (offset as usize..offset as usize + len) + .map(|i| (i % 251) as u8) + .collect(); + assert_eq!(buf, expected); + } }