From a502354619a2d6b6897ef4fcd49242b1cb82cbc9 Mon Sep 17 00:00:00 2001 From: Anatol Belski Date: Fri, 17 Apr 2026 23:58:02 +0200 Subject: [PATCH] block: qcow: Test pread_alloc with offset reads and EOF Verify that pread_alloc returns the correct data for a full read from the start and a partial read at an arbitrary offset. Also confirm that reading past the end of file produces an error. Signed-off-by: Anatol Belski --- block/src/qcow_common.rs | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/block/src/qcow_common.rs b/block/src/qcow_common.rs index 195fb48aa..817436a58 100644 --- a/block/src/qcow_common.rs +++ b/block/src/qcow_common.rs @@ -296,10 +296,15 @@ pub unsafe fn gather_from_iovecs(iovecs: &[libc::iovec], start: usize, len: usiz pub(crate) mod unit_tests { use std::fs::File; use std::io::{Read, Seek, SeekFrom, Write}; + use std::os::unix::fs::FileExt; + use std::os::unix::io::AsRawFd; use byteorder::{BigEndian, ReadBytesExt, WriteBytesExt}; - use flate2::write::DeflateEncoder; use flate2::Compression; + use flate2::write::DeflateEncoder; + use vmm_sys_util::tempfile::TempFile; + + use super::pread_alloc; const COMPRESSED_FLAG: u64 = 1 << 62; const CLUSTER_USED_FLAG: u64 = 1 << 63; @@ -401,4 +406,20 @@ pub(crate) mod unit_tests { file.flush().unwrap(); } + + #[test] + fn test_pread_alloc() { + let temp = TempFile::new().unwrap(); + let file = temp.as_file(); + let data: Vec = (0..=255).cycle().take(4096).collect(); + file.write_all_at(&data, 0).unwrap(); + + let buf = pread_alloc(file.as_raw_fd(), 0, 4096).unwrap(); + assert_eq!(buf, data); + + let buf = pread_alloc(file.as_raw_fd(), 100, 200).unwrap(); + assert_eq!(buf, &data[100..300]); + + pread_alloc(file.as_raw_fd(), 4000, 200).unwrap_err(); + } }