mirror of
https://github.com/cloud-hypervisor/cloud-hypervisor.git
synced 2026-08-05 02:19:16 +00:00
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 <anbelski@linux.microsoft.com>
This commit is contained in:
committed by
Rob Bradford
parent
158882e6dd
commit
a502354619
@@ -296,10 +296,15 @@ pub unsafe fn gather_from_iovecs(iovecs: &[libc::iovec], start: usize, len: usiz
|
|||||||
pub(crate) mod unit_tests {
|
pub(crate) mod unit_tests {
|
||||||
use std::fs::File;
|
use std::fs::File;
|
||||||
use std::io::{Read, Seek, SeekFrom, Write};
|
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 byteorder::{BigEndian, ReadBytesExt, WriteBytesExt};
|
||||||
use flate2::write::DeflateEncoder;
|
|
||||||
use flate2::Compression;
|
use flate2::Compression;
|
||||||
|
use flate2::write::DeflateEncoder;
|
||||||
|
use vmm_sys_util::tempfile::TempFile;
|
||||||
|
|
||||||
|
use super::pread_alloc;
|
||||||
|
|
||||||
const COMPRESSED_FLAG: u64 = 1 << 62;
|
const COMPRESSED_FLAG: u64 = 1 << 62;
|
||||||
const CLUSTER_USED_FLAG: u64 = 1 << 63;
|
const CLUSTER_USED_FLAG: u64 = 1 << 63;
|
||||||
@@ -401,4 +406,20 @@ pub(crate) mod unit_tests {
|
|||||||
|
|
||||||
file.flush().unwrap();
|
file.flush().unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_pread_alloc() {
|
||||||
|
let temp = TempFile::new().unwrap();
|
||||||
|
let file = temp.as_file();
|
||||||
|
let data: Vec<u8> = (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();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user