From 519acda794aee81f6d4a4a61beff12de91d1af9b Mon Sep 17 00:00:00 2001 From: Anatol Belski Date: Sat, 18 Apr 2026 00:36:23 +0200 Subject: [PATCH] block: qcow: Test compressed cluster read via QcowFile Write a known data pattern through QcowFile, compress all clusters in place, reopen and read back via the seek based file_read path. This covers the decompress_l2_cluster code path used by QcowFile which is separate from the pread based path in QcowSync/QcowAsync. Signed-off-by: Anatol Belski --- block/src/qcow/mod.rs | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/block/src/qcow/mod.rs b/block/src/qcow/mod.rs index 3e629bf57..32c2b0648 100644 --- a/block/src/qcow/mod.rs +++ b/block/src/qcow/mod.rs @@ -2353,6 +2353,7 @@ mod unit_tests { use super::util::{COMPRESSED_FLAG, ZERO_FLAG}; use super::*; + use crate::qcow_common::unit_tests::compress_allocated_clusters; fn valid_header_v3() -> Vec { vec![ @@ -4590,4 +4591,27 @@ mod unit_tests { assert_eq!(qcow.header.version, 2); }); } + + #[test] + fn test_compressed_read() { + let cluster_size = 65536usize; + let data: Vec = (0..=255).cycle().take(cluster_size).collect(); + let temp = TempFile::new().unwrap(); + { + let raw_file = RawFile::new(temp.as_file().try_clone().unwrap(), false); + let mut qcow = QcowFile::new(raw_file, 3, 100 * 1024 * 1024, false).unwrap(); + qcow.seek(SeekFrom::Start(0)).unwrap(); + qcow.write_all(&data).unwrap(); + qcow.flush().unwrap(); + } + + compress_allocated_clusters(&mut temp.as_file().try_clone().unwrap()); + + let raw_file = RawFile::new(temp.as_file().try_clone().unwrap(), false); + let mut qcow = QcowFile::from(raw_file).unwrap(); + qcow.seek(SeekFrom::Start(0)).unwrap(); + let mut buf = vec![0u8; cluster_size]; + qcow.read_exact(&mut buf).unwrap(); + assert_eq!(buf, data); + } }