Skip to content

Commit 1c7bf77

Browse files
nicholasbishopGabrielMajeri
authored andcommitted
Add a test for opening and reading a file
1 parent bc0de8d commit 1c7bf77

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

uefi-test-runner/src/proto/media/mod.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,41 @@
1+
use core::convert::TryFrom;
12
use uefi::prelude::*;
3+
use uefi::proto::media::file::{Directory, File, FileAttribute, FileMode, FileType};
24
use uefi::proto::media::fs::SimpleFileSystem;
35
use uefi::proto::media::partition::PartitionInfo;
46
use uefi::table::boot::{OpenProtocolAttributes, OpenProtocolParams};
7+
use uefi::CString16;
8+
9+
/// Open and read a test file in the boot directory.
10+
pub fn test_open_and_read(directory: &mut Directory) {
11+
let test_input_path = CString16::try_from("EFI\\BOOT\\test_input.txt").unwrap();
12+
match directory.open(&test_input_path, FileMode::Read, FileAttribute::empty()) {
13+
Ok(file) => {
14+
let file = file.unwrap().into_type().unwrap_success();
15+
if let FileType::Regular(mut file) = file {
16+
let mut buffer = vec![0; 128];
17+
let size = file
18+
.read(&mut buffer)
19+
.expect_success(&format!("failed to read {}", test_input_path));
20+
let buffer = &buffer[..size];
21+
info!("Successfully read {}", test_input_path);
22+
assert_eq!(buffer, b"test input data");
23+
} else {
24+
panic!("{} is not a regular file", test_input_path);
25+
}
26+
}
27+
Err(err) => {
28+
let msg = format!("Failed to open {}: {:?}", test_input_path, err);
29+
// The file might reasonably not be present when running on real
30+
// hardware, so only panic on failure under qemu.
31+
if cfg!(feature = "qemu") {
32+
panic!("{}", msg);
33+
} else {
34+
warn!("{}", msg);
35+
}
36+
}
37+
}
38+
}
539

640
pub fn test(image: Handle, bt: &BootServices) {
741
info!("Testing Media Access protocols");
@@ -31,6 +65,8 @@ pub fn test(image: Handle, bt: &BootServices) {
3165
info!("Root directory entry: {:?}", file_info);
3266
}
3367
directory.reset_entry_readout().unwrap().unwrap();
68+
69+
test_open_and_read(&mut directory);
3470
} else {
3571
warn!("`SimpleFileSystem` protocol is not available");
3672
}

xtask/src/qemu.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,10 @@ fn build_esp_dir(opt: &QemuOpt) -> Result<PathBuf> {
256256
fs_err::create_dir_all(&boot_dir)?;
257257
}
258258
fs_err::copy(built_file, boot_dir.join(output_file))?;
259+
260+
// Add a test file that is used in the media protocol tests.
261+
fs_err::write(boot_dir.join("test_input.txt"), "test input data")?;
262+
259263
Ok(esp_dir)
260264
}
261265

0 commit comments

Comments
 (0)