• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2022 The ChromiumOS Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 use std::fs::File;
6 
7 use crate::Result;
8 
apply_raw_disk_file_options(_raw_image: &File, _is_sparse_file: bool) -> Result<()>9 pub fn apply_raw_disk_file_options(_raw_image: &File, _is_sparse_file: bool) -> Result<()> {
10     // No op on unix.
11     Ok(())
12 }
13 
14 #[cfg(test)]
15 mod tests {
16     use std::fs::File;
17     use std::fs::OpenOptions;
18     use std::io::Write;
19 
20     use cros_async::Executor;
21     use cros_async::MemRegion;
22     use vm_memory::GuestAddress;
23     use vm_memory::GuestMemory;
24 
25     use crate::*;
26 
27     #[test]
read_async()28     fn read_async() {
29         async fn read_zeros_async(ex: &Executor) {
30             let guest_mem = Arc::new(GuestMemory::new(&[(GuestAddress(0), 4096)]).unwrap());
31             let f = File::open("/dev/zero").unwrap();
32             let async_file = SingleFileDisk::new(f, ex).unwrap();
33             let result = async_file
34                 .read_to_mem(0, guest_mem, &[MemRegion { offset: 0, len: 48 }])
35                 .await;
36             assert_eq!(48, result.unwrap());
37         }
38 
39         let ex = Executor::new().unwrap();
40         ex.run_until(read_zeros_async(&ex)).unwrap();
41     }
42 
43     #[test]
write_async()44     fn write_async() {
45         async fn write_zeros_async(ex: &Executor) {
46             let guest_mem = Arc::new(GuestMemory::new(&[(GuestAddress(0), 4096)]).unwrap());
47             let f = OpenOptions::new().write(true).open("/dev/null").unwrap();
48             let async_file = SingleFileDisk::new(f, ex).unwrap();
49             let result = async_file
50                 .write_from_mem(0, guest_mem, &[MemRegion { offset: 0, len: 48 }])
51                 .await;
52             assert_eq!(48, result.unwrap());
53         }
54 
55         let ex = Executor::new().unwrap();
56         ex.run_until(write_zeros_async(&ex)).unwrap();
57     }
58 
59     #[test]
detect_image_type_raw()60     fn detect_image_type_raw() {
61         let mut t = tempfile::tempfile().unwrap();
62         // Fill the first block of the file with "random" data.
63         let buf = "ABCD".as_bytes().repeat(1024);
64         t.write_all(&buf).unwrap();
65         let image_type = detect_image_type(&t).expect("failed to detect image type");
66         assert_eq!(image_type, ImageType::Raw);
67     }
68 
69     #[test]
70     #[cfg(feature = "qcow")]
detect_image_type_qcow2()71     fn detect_image_type_qcow2() {
72         let mut t = tempfile::tempfile().unwrap();
73         // Write the qcow2 magic signature. The rest of the header is not filled in, so if
74         // detect_image_type is ever updated to validate more of the header, this test would need
75         // to be updated.
76         let buf: &[u8] = &[0x51, 0x46, 0x49, 0xfb];
77         t.write_all(buf).unwrap();
78         let image_type = detect_image_type(&t).expect("failed to detect image type");
79         assert_eq!(image_type, ImageType::Qcow2);
80     }
81 
82     #[test]
83     #[cfg(feature = "android-sparse")]
detect_image_type_android_sparse()84     fn detect_image_type_android_sparse() {
85         let mut t = tempfile::tempfile().unwrap();
86         // Write the Android sparse magic signature. The rest of the header is not filled in, so if
87         // detect_image_type is ever updated to validate more of the header, this test would need
88         // to be updated.
89         let buf: &[u8] = &[0x3a, 0xff, 0x26, 0xed];
90         t.write_all(buf).unwrap();
91         let image_type = detect_image_type(&t).expect("failed to detect image type");
92         assert_eq!(image_type, ImageType::AndroidSparse);
93     }
94 
95     #[test]
96     #[cfg(feature = "composite-disk")]
detect_image_type_composite()97     fn detect_image_type_composite() {
98         let mut t = tempfile::tempfile().unwrap();
99         // Write the composite disk magic signature. The rest of the header is not filled in, so if
100         // detect_image_type is ever updated to validate more of the header, this test would need
101         // to be updated.
102         let buf = "composite_disk\x1d".as_bytes();
103         t.write_all(buf).unwrap();
104         let image_type = detect_image_type(&t).expect("failed to detect image type");
105         assert_eq!(image_type, ImageType::CompositeDisk);
106     }
107 
108     #[test]
detect_image_type_small_file()109     fn detect_image_type_small_file() {
110         let mut t = tempfile::tempfile().unwrap();
111         // Write a file smaller than the four-byte qcow2/sparse magic to ensure the small file logic
112         // works correctly and handles it as a raw file.
113         let buf: &[u8] = &[0xAA, 0xBB];
114         t.write_all(buf).unwrap();
115         let image_type = detect_image_type(&t).expect("failed to detect image type");
116         assert_eq!(image_type, ImageType::Raw);
117     }
118 }
119