|
| 1 | +use alloc::string::ToString; |
| 2 | +use uefi::prelude::*; |
| 3 | +use uefi::proto::media::file::{ |
| 4 | + Directory, File, FileAttribute, FileInfo, FileMode, FileSystemInfo, |
| 5 | +}; |
| 6 | +use uefi::proto::media::fs::SimpleFileSystem; |
| 7 | +use uefi::table::boot::{OpenProtocolAttributes, OpenProtocolParams}; |
| 8 | +use uefi::table::runtime::{Daylight, Time}; |
| 9 | +use uefi::CString16; |
| 10 | + |
| 11 | +/// Test directory entry iteration. |
| 12 | +fn test_existing_dir(directory: &mut Directory) { |
| 13 | + info!("Testing existing directory"); |
| 14 | + |
| 15 | + let input_dir_path = CString16::try_from("test_dir").unwrap(); |
| 16 | + let mut dir = directory |
| 17 | + .open(&input_dir_path, FileMode::Read, FileAttribute::empty()) |
| 18 | + .expect("failed to open directory") |
| 19 | + .into_directory() |
| 20 | + .expect("not a directory"); |
| 21 | + |
| 22 | + // Collect and validate the directory entries. |
| 23 | + let mut entry_names = vec![]; |
| 24 | + let mut buf = vec![0; 200]; |
| 25 | + loop { |
| 26 | + let entry = dir.read_entry(&mut buf).expect("failed to read directory"); |
| 27 | + if let Some(entry) = entry { |
| 28 | + entry_names.push(entry.file_name().to_string()); |
| 29 | + } else { |
| 30 | + break; |
| 31 | + } |
| 32 | + } |
| 33 | + assert_eq!(entry_names, [".", "..", "test_input.txt"]); |
| 34 | +} |
| 35 | + |
| 36 | +/// Test that deleting a file opened in read-only mode fails with a |
| 37 | +/// warning. This is mostly just an excuse to verify that warnings are |
| 38 | +/// properly converted to errors. |
| 39 | +fn test_delete_warning(directory: &mut Directory) { |
| 40 | + let input_file_path = CString16::try_from("test_dir\\test_input.txt").unwrap(); |
| 41 | + let file = directory |
| 42 | + .open(&input_file_path, FileMode::Read, FileAttribute::empty()) |
| 43 | + .expect("failed to open file") |
| 44 | + .into_regular_file() |
| 45 | + .expect("not a regular file"); |
| 46 | + |
| 47 | + assert_eq!( |
| 48 | + file.delete().unwrap_err().status(), |
| 49 | + Status::WARN_DELETE_FAILURE |
| 50 | + ); |
| 51 | +} |
| 52 | + |
| 53 | +/// Test operations on an existing file. |
| 54 | +fn test_existing_file(directory: &mut Directory) { |
| 55 | + info!("Testing existing file"); |
| 56 | + |
| 57 | + // Open an existing file. |
| 58 | + let input_file_path = CString16::try_from("test_dir\\test_input.txt").unwrap(); |
| 59 | + let mut file = directory |
| 60 | + .open( |
| 61 | + &input_file_path, |
| 62 | + FileMode::ReadWrite, |
| 63 | + FileAttribute::empty(), |
| 64 | + ) |
| 65 | + .expect("failed to open file") |
| 66 | + .into_regular_file() |
| 67 | + .expect("not a regular file"); |
| 68 | + |
| 69 | + // Read the file. |
| 70 | + let mut buffer = vec![0; 128]; |
| 71 | + let size = file.read(&mut buffer).expect("failed to read file"); |
| 72 | + let buffer = &buffer[..size]; |
| 73 | + info!("Successfully read {}", input_file_path); |
| 74 | + assert_eq!(buffer, b"test input data"); |
| 75 | + |
| 76 | + // Check file metadata. |
| 77 | + let mut info_buffer = vec![0; 128]; |
| 78 | + let info = file.get_info::<FileInfo>(&mut info_buffer).unwrap(); |
| 79 | + assert_eq!(info.file_size(), 15); |
| 80 | + assert_eq!(info.physical_size(), 512); |
| 81 | + assert_eq!( |
| 82 | + *info.create_time(), |
| 83 | + Time::new(2000, 1, 24, 0, 0, 0, 0, 2047, Daylight::empty()) |
| 84 | + ); |
| 85 | + assert_eq!( |
| 86 | + *info.last_access_time(), |
| 87 | + Time::new(2001, 2, 25, 0, 0, 0, 0, 2047, Daylight::empty()) |
| 88 | + ); |
| 89 | + assert_eq!( |
| 90 | + *info.modification_time(), |
| 91 | + Time::new(2002, 3, 26, 0, 0, 0, 0, 2047, Daylight::empty()) |
| 92 | + ); |
| 93 | + assert_eq!(info.attribute(), FileAttribute::empty()); |
| 94 | + assert_eq!( |
| 95 | + info.file_name(), |
| 96 | + CString16::try_from("test_input.txt").unwrap() |
| 97 | + ); |
| 98 | + |
| 99 | + // Delete the file. |
| 100 | + file.delete().unwrap(); |
| 101 | + |
| 102 | + // Verify the file is gone. |
| 103 | + assert!(directory |
| 104 | + .open(&input_file_path, FileMode::Read, FileAttribute::empty()) |
| 105 | + .is_err()); |
| 106 | +} |
| 107 | + |
| 108 | +/// Test file creation. |
| 109 | +fn test_create_file(directory: &mut Directory) { |
| 110 | + info!("Testing file creation"); |
| 111 | + |
| 112 | + // Create a new file. |
| 113 | + let mut file = directory |
| 114 | + .open( |
| 115 | + &CString16::try_from("new_test_file.txt").unwrap(), |
| 116 | + FileMode::CreateReadWrite, |
| 117 | + FileAttribute::empty(), |
| 118 | + ) |
| 119 | + .expect("failed to create file") |
| 120 | + .into_regular_file() |
| 121 | + .expect("not a regular file"); |
| 122 | + file.write(b"test output data").unwrap(); |
| 123 | +} |
| 124 | + |
| 125 | +/// Run various tests on a special test disk. The disk is created by |
| 126 | +/// xtask/src/disk.rs. |
| 127 | +pub fn test_known_disk(image: Handle, bt: &BootServices) { |
| 128 | + // This test is only valid when running in the specially-prepared |
| 129 | + // qemu with the test disk. |
| 130 | + if !cfg!(feature = "qemu") { |
| 131 | + return; |
| 132 | + } |
| 133 | + |
| 134 | + let handles = bt |
| 135 | + .find_handles::<SimpleFileSystem>() |
| 136 | + .expect("Failed to get handles for `SimpleFileSystem` protocol"); |
| 137 | + assert_eq!(handles.len(), 2); |
| 138 | + |
| 139 | + let mut found_test_disk = false; |
| 140 | + for handle in handles { |
| 141 | + let sfs = bt |
| 142 | + .open_protocol::<SimpleFileSystem>( |
| 143 | + OpenProtocolParams { |
| 144 | + handle, |
| 145 | + agent: image, |
| 146 | + controller: None, |
| 147 | + }, |
| 148 | + OpenProtocolAttributes::Exclusive, |
| 149 | + ) |
| 150 | + .expect("Failed to get simple file system"); |
| 151 | + let sfs = unsafe { &mut *sfs.interface.get() }; |
| 152 | + let mut directory = sfs.open_volume().unwrap(); |
| 153 | + |
| 154 | + let mut fs_info_buf = vec![0; 128]; |
| 155 | + let fs_info = directory |
| 156 | + .get_info::<FileSystemInfo>(&mut fs_info_buf) |
| 157 | + .unwrap(); |
| 158 | + |
| 159 | + if fs_info.volume_label().to_string() == "MbrTestDisk" { |
| 160 | + info!("Checking MbrTestDisk"); |
| 161 | + found_test_disk = true; |
| 162 | + } else { |
| 163 | + continue; |
| 164 | + } |
| 165 | + |
| 166 | + assert!(!fs_info.read_only()); |
| 167 | + assert_eq!(fs_info.volume_size(), 512 * 1192); |
| 168 | + assert_eq!(fs_info.free_space(), 512 * 1190); |
| 169 | + assert_eq!(fs_info.block_size(), 512); |
| 170 | + |
| 171 | + test_existing_dir(&mut directory); |
| 172 | + test_delete_warning(&mut directory); |
| 173 | + test_existing_file(&mut directory); |
| 174 | + test_create_file(&mut directory); |
| 175 | + } |
| 176 | + |
| 177 | + if !found_test_disk { |
| 178 | + panic!("MbrTestDisk not found"); |
| 179 | + } |
| 180 | +} |
0 commit comments