Skip to content

Commit

Permalink
Add a test for opening and reading a file
Browse files Browse the repository at this point in the history
  • Loading branch information
nicholasbishop authored and GabrielMajeri committed Feb 19, 2022
1 parent bc0de8d commit 1c7bf77
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
36 changes: 36 additions & 0 deletions uefi-test-runner/src/proto/media/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,41 @@
use core::convert::TryFrom;
use uefi::prelude::*;
use uefi::proto::media::file::{Directory, File, FileAttribute, FileMode, FileType};
use uefi::proto::media::fs::SimpleFileSystem;
use uefi::proto::media::partition::PartitionInfo;
use uefi::table::boot::{OpenProtocolAttributes, OpenProtocolParams};
use uefi::CString16;

/// Open and read a test file in the boot directory.
pub fn test_open_and_read(directory: &mut Directory) {
let test_input_path = CString16::try_from("EFI\\BOOT\\test_input.txt").unwrap();
match directory.open(&test_input_path, FileMode::Read, FileAttribute::empty()) {
Ok(file) => {
let file = file.unwrap().into_type().unwrap_success();
if let FileType::Regular(mut file) = file {
let mut buffer = vec![0; 128];
let size = file
.read(&mut buffer)
.expect_success(&format!("failed to read {}", test_input_path));
let buffer = &buffer[..size];
info!("Successfully read {}", test_input_path);
assert_eq!(buffer, b"test input data");
} else {
panic!("{} is not a regular file", test_input_path);
}
}
Err(err) => {
let msg = format!("Failed to open {}: {:?}", test_input_path, err);
// The file might reasonably not be present when running on real
// hardware, so only panic on failure under qemu.
if cfg!(feature = "qemu") {
panic!("{}", msg);
} else {
warn!("{}", msg);
}
}
}
}

pub fn test(image: Handle, bt: &BootServices) {
info!("Testing Media Access protocols");
Expand Down Expand Up @@ -31,6 +65,8 @@ pub fn test(image: Handle, bt: &BootServices) {
info!("Root directory entry: {:?}", file_info);
}
directory.reset_entry_readout().unwrap().unwrap();

test_open_and_read(&mut directory);
} else {
warn!("`SimpleFileSystem` protocol is not available");
}
Expand Down
4 changes: 4 additions & 0 deletions xtask/src/qemu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,10 @@ fn build_esp_dir(opt: &QemuOpt) -> Result<PathBuf> {
fs_err::create_dir_all(&boot_dir)?;
}
fs_err::copy(built_file, boot_dir.join(output_file))?;

// Add a test file that is used in the media protocol tests.
fs_err::write(boot_dir.join("test_input.txt"), "test input data")?;

Ok(esp_dir)
}

Expand Down

0 comments on commit 1c7bf77

Please sign in to comment.