Skip to content

Commit b4f4ddf

Browse files
test-runner: Refactor disk test for clarity
Simplify the big disk test function a little by moving the loop to find the test disk into a new function.
1 parent cd3f2b6 commit b4f4ddf

File tree

1 file changed

+72
-60
lines changed
  • uefi-test-runner/src/proto/media

1 file changed

+72
-60
lines changed

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

Lines changed: 72 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ use uefi::proto::media::file::{
99
};
1010
use uefi::proto::media::fs::SimpleFileSystem;
1111
use uefi::proto::media::partition::{MbrOsType, PartitionInfo};
12-
use uefi::table::boot::{EventType, OpenProtocolAttributes, OpenProtocolParams, Tpl};
12+
use uefi::table::boot::{
13+
EventType, OpenProtocolAttributes, OpenProtocolParams, ScopedProtocol, Tpl,
14+
};
1315
use uefi::table::runtime::{Daylight, Time, TimeParams};
1416

1517
/// Test directory entry iteration.
@@ -354,76 +356,86 @@ fn test_partition_info(bt: &BootServices, disk_handle: Handle) {
354356
assert_eq!(mbr.os_type, MbrOsType(6));
355357
}
356358

357-
/// Run various file-system related tests on a special test disk. The disk is created by
358-
/// `xtask/src/disk.rs`.
359-
pub fn test(bt: &BootServices) {
359+
/// Find the disk with the "MbrTestDisk" label. Return the handle and opened
360+
/// `SimpleFileSystem` protocol for that disk.
361+
fn find_test_disk(bt: &BootServices) -> (Handle, ScopedProtocol<SimpleFileSystem>) {
360362
let handles = bt
361363
.find_handles::<SimpleFileSystem>()
362364
.expect("Failed to get handles for `SimpleFileSystem` protocol");
363365
assert_eq!(handles.len(), 2);
364366

365-
let mut found_test_disk = false;
366367
for handle in handles {
367-
{
368-
let mut sfs = bt
369-
.open_protocol_exclusive::<SimpleFileSystem>(handle)
370-
.expect("Failed to get simple file system");
371-
let mut root_directory = sfs.open_volume().unwrap();
372-
373-
// test is_directory() and is_regular_file() from the File trait which is the
374-
// base for into_type() used later in the test.
375-
{
376-
// because File is "Sized", we cannot cast it to &dyn
377-
fn test_is_directory(file: &impl File) {
378-
assert_eq!(Ok(true), file.is_directory());
379-
assert_eq!(Ok(false), file.is_regular_file());
380-
}
381-
test_is_directory(&root_directory);
382-
}
368+
let mut sfs = bt
369+
.open_protocol_exclusive::<SimpleFileSystem>(handle)
370+
.expect("Failed to get simple file system");
371+
let mut root_directory = sfs.open_volume().unwrap();
383372

384-
let mut fs_info_buf = vec![0; 128];
385-
let fs_info = root_directory
386-
.get_info::<FileSystemInfo>(&mut fs_info_buf)
387-
.unwrap();
373+
let vol_info = root_directory
374+
.get_boxed_info::<FileSystemVolumeLabel>()
375+
.unwrap();
388376

389-
if fs_info.volume_label().to_string() == "MbrTestDisk" {
390-
info!("Checking MbrTestDisk");
391-
found_test_disk = true;
392-
} else {
393-
continue;
394-
}
377+
if vol_info.volume_label().to_string() == "MbrTestDisk" {
378+
return (handle, sfs);
379+
}
380+
}
395381

396-
assert!(!fs_info.read_only());
397-
assert_eq!(fs_info.volume_size(), 512 * 1192);
398-
assert_eq!(fs_info.free_space(), 512 * 1190);
399-
assert_eq!(fs_info.block_size(), 512);
400-
401-
// Check that `get_boxed_info` returns the same info.
402-
let boxed_fs_info = root_directory.get_boxed_info::<FileSystemInfo>().unwrap();
403-
assert_eq!(*fs_info, *boxed_fs_info);
404-
405-
// Check that `FileSystemVolumeLabel` provides the same volume label
406-
// as `FileSystemInfo`.
407-
let mut fs_vol_buf = vec![0; 128];
408-
let fs_vol = root_directory
409-
.get_info::<FileSystemVolumeLabel>(&mut fs_vol_buf)
410-
.unwrap();
411-
assert_eq!(fs_info.volume_label(), fs_vol.volume_label());
412-
413-
test_existing_dir(&mut root_directory);
414-
test_delete_warning(&mut root_directory);
415-
test_existing_file(&mut root_directory);
416-
test_create_file(&mut root_directory);
417-
test_create_directory(&mut root_directory);
418-
419-
test_partition_info(bt, handle);
382+
panic!("MbrTestDisk not found");
383+
}
384+
385+
/// Run various file-system related tests on a special test disk. The disk is created by
386+
/// `xtask/src/disk.rs`.
387+
pub fn test(bt: &BootServices) {
388+
let (handle, mut sfs) = find_test_disk(bt);
389+
390+
{
391+
let mut root_directory = sfs.open_volume().unwrap();
392+
393+
// test is_directory() and is_regular_file() from the File trait which is the
394+
// base for into_type() used later in the test.
395+
{
396+
// because File is "Sized", we cannot cast it to &dyn
397+
fn test_is_directory(file: &impl File) {
398+
assert_eq!(Ok(true), file.is_directory());
399+
assert_eq!(Ok(false), file.is_regular_file());
400+
}
401+
test_is_directory(&root_directory);
420402
}
421403

422-
test_raw_disk_io(handle, bt);
423-
test_raw_disk_io2(handle, bt);
404+
let mut fs_info_buf = vec![0; 128];
405+
let fs_info = root_directory
406+
.get_info::<FileSystemInfo>(&mut fs_info_buf)
407+
.unwrap();
408+
409+
assert!(!fs_info.read_only());
410+
assert_eq!(fs_info.volume_size(), 512 * 1192);
411+
assert_eq!(fs_info.free_space(), 512 * 1190);
412+
assert_eq!(fs_info.block_size(), 512);
413+
assert_eq!(fs_info.volume_label().to_string(), "MbrTestDisk");
414+
415+
// Check that `get_boxed_info` returns the same info.
416+
let boxed_fs_info = root_directory.get_boxed_info::<FileSystemInfo>().unwrap();
417+
assert_eq!(*fs_info, *boxed_fs_info);
418+
419+
// Check that `FileSystemVolumeLabel` provides the same volume label
420+
// as `FileSystemInfo`.
421+
let mut fs_vol_buf = vec![0; 128];
422+
let fs_vol = root_directory
423+
.get_info::<FileSystemVolumeLabel>(&mut fs_vol_buf)
424+
.unwrap();
425+
assert_eq!(fs_info.volume_label(), fs_vol.volume_label());
426+
427+
test_existing_dir(&mut root_directory);
428+
test_delete_warning(&mut root_directory);
429+
test_existing_file(&mut root_directory);
430+
test_create_file(&mut root_directory);
431+
test_create_directory(&mut root_directory);
432+
433+
test_partition_info(bt, handle);
424434
}
425435

426-
if !found_test_disk {
427-
panic!("MbrTestDisk not found");
428-
}
436+
// Close the `SimpleFileSystem` protocol so that the raw disk tests work.
437+
drop(sfs);
438+
439+
test_raw_disk_io(handle, bt);
440+
test_raw_disk_io2(handle, bt);
429441
}

0 commit comments

Comments
 (0)