diff options
author | Daniel Verkamp <dverkamp@chromium.org> | 2019-11-27 09:31:44 -0800 |
---|---|---|
committer | Commit Bot <commit-bot@chromium.org> | 2019-12-06 04:28:37 +0000 |
commit | c0a1f5318e3b5aa9d7dc850536b4b52298245334 (patch) | |
tree | ebbf9727322b1ad241d4b928032c39aba397e954 | |
parent | bfbe8880418957e22af2ede1dbffc3c16a017c42 (diff) | |
download | crosvm-c0a1f5318e3b5aa9d7dc850536b4b52298245334.tar crosvm-c0a1f5318e3b5aa9d7dc850536b4b52298245334.tar.gz crosvm-c0a1f5318e3b5aa9d7dc850536b4b52298245334.tar.bz2 crosvm-c0a1f5318e3b5aa9d7dc850536b4b52298245334.tar.lz crosvm-c0a1f5318e3b5aa9d7dc850536b4b52298245334.tar.xz crosvm-c0a1f5318e3b5aa9d7dc850536b4b52298245334.tar.zst crosvm-c0a1f5318e3b5aa9d7dc850536b4b52298245334.zip |
devices: use File rather than shm in tests
Two virtio descriptor_utils tests were using SharedMemory to stand in for I/o targets with a fixed size; replace these with File to avoid needing the FileReadWriteVolatile impl for SharedMemory, which isn't used anywhere else in the crosvm code base. This slightly changes the behavior under test in the reader_failing_io test, since it was previously using the SharedMemory seal functionality to make the region ungrowable; this is an unusual corner case, and (as mentioned in the comment that was previously at the end of the test) it is testing implementation details of write() on shared memory on Linux. Instead, just use a read-only file so that write() to it will fail and cause the same observable result. BUG=None TEST=./build_test.py Change-Id: I6d62cd70791f1dec625b750ecd01cc51e307f971 Signed-off-by: Daniel Verkamp <dverkamp@chromium.org> Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/1939783 Reviewed-by: Stephen Barber <smbarber@chromium.org> Reviewed-by: Chirantan Ekbote <chirantan@chromium.org> Tested-by: kokoro <noreply+kokoro@google.com>
-rw-r--r-- | devices/src/virtio/descriptor_utils.rs | 36 |
1 files changed, 19 insertions, 17 deletions
diff --git a/devices/src/virtio/descriptor_utils.rs b/devices/src/virtio/descriptor_utils.rs index f9ef737..fcd18ec 100644 --- a/devices/src/virtio/descriptor_utils.rs +++ b/devices/src/virtio/descriptor_utils.rs @@ -637,7 +637,8 @@ pub fn create_descriptor_chain( #[cfg(test)] mod tests { use super::*; - use sys_util::{MemfdSeals, SharedMemory}; + use std::fs::{File, OpenOptions}; + use tempfile::TempDir; #[test] fn reader_test_simple_chain() { @@ -789,22 +790,14 @@ mod tests { let mut reader = Reader::new(&memory, chain).expect("failed to create Reader"); - // GuestMemory's write_from_memory requires raw file descriptor. - let mut shm = SharedMemory::anon().unwrap(); - shm.set_size(384).unwrap(); - - // Prevent shared memory from growing on `write` call. - let mut fd_seals = MemfdSeals::new(); - fd_seals.set_grow_seal(); - shm.add_seals(fd_seals).unwrap(); + // Open a file in read-only mode so writes to it to trigger an I/O error. + let mut ro_file = File::open("/dev/zero").expect("failed to open /dev/zero"); reader - .read_exact_to(&mut shm, 512) + .read_exact_to(&mut ro_file, 512) .expect_err("successfully read more bytes than SharedMemory size"); - // Linux doesn't do partial writes if you give a buffer larger than the remaining length of - // the shared memory. And since we passed an iovec with the full contents of the - // DescriptorChain we ended up not writing any bytes at all. + // The write above should have failed entirely, so we end up not writing any bytes at all. assert_eq!(reader.available_bytes(), 512); assert_eq!(reader.bytes_read(), 0); } @@ -827,12 +820,21 @@ mod tests { let mut writer = Writer::new(&memory, chain).expect("failed to create Writer"); - // GuestMemory's read_to_memory requires raw file descriptor. - let mut shm = SharedMemory::anon().unwrap(); - shm.set_size(384).unwrap(); + let tempdir = TempDir::new().unwrap(); + let mut path = tempdir.path().to_owned(); + path.push("test_file"); + + let mut file = OpenOptions::new() + .read(true) + .write(true) + .create_new(true) + .open(&path) + .expect("failed to create temp file"); + + file.set_len(384).unwrap(); writer - .write_all_from(&mut shm, 512) + .write_all_from(&mut file, 512) .expect_err("successfully wrote more bytes than in SharedMemory"); assert_eq!(writer.available_bytes(), 128); |