Skip to content

Commit 6d6586e

Browse files
committed
uefi-test-runner: Added tests for EFI Shell cur_dir functions
This commit includes tests for the following EFI Shell Protocol functions: get_cur_dir() and set_cur_dir().
1 parent f7965e9 commit 6d6586e

File tree

2 files changed

+106
-8
lines changed

2 files changed

+106
-8
lines changed

uefi-test-runner/src/proto/shell.rs

Lines changed: 98 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,109 @@
11
// SPDX-License-Identifier: MIT OR Apache-2.0
22

3-
use uefi::boot;
3+
use uefi::boot::ScopedProtocol;
44
use uefi::proto::shell::Shell;
5+
use uefi::{boot, cstr16};
6+
use uefi_raw::Status;
7+
8+
/// Test `get_cur_dir()` and `set_cur_dir()`
9+
pub fn test_cur_dir(shell: &ScopedProtocol<Shell>) {
10+
/* Test setting and getting current file system and current directory */
11+
let fs_var = cstr16!("fs0:");
12+
let dir_var = cstr16!("/");
13+
let status = shell.set_cur_dir(Some(fs_var), Some(dir_var));
14+
assert_eq!(status, Status::SUCCESS);
15+
16+
let cur_fs_str = shell
17+
.get_cur_dir(Some(fs_var))
18+
.expect("Could not get the current file system mapping");
19+
let expected_fs_str = cstr16!("FS0:\\");
20+
assert_eq!(cur_fs_str, expected_fs_str);
21+
22+
// Changing current file system
23+
let fs_var = cstr16!("fs1:");
24+
let dir_var = cstr16!("/");
25+
let status = shell.set_cur_dir(Some(fs_var), Some(dir_var));
26+
assert_eq!(status, Status::SUCCESS);
27+
28+
let cur_fs_str = shell
29+
.get_cur_dir(Some(fs_var))
30+
.expect("Could not get the current file system mapping");
31+
assert_ne!(cur_fs_str, expected_fs_str);
32+
let expected_fs_str = cstr16!("FS1:\\");
33+
assert_eq!(cur_fs_str, expected_fs_str);
34+
35+
// Changing current file system and current directory
36+
let fs_var = cstr16!("fs0:");
37+
let dir_var = cstr16!("efi/");
38+
let status = shell.set_cur_dir(Some(fs_var), Some(dir_var));
39+
assert_eq!(status, Status::SUCCESS);
40+
41+
let cur_fs_str = shell
42+
.get_cur_dir(Some(fs_var))
43+
.expect("Could not get the current file system mapping");
44+
assert_ne!(cur_fs_str, expected_fs_str);
45+
let expected_fs_str = cstr16!("FS0:\\efi");
46+
assert_eq!(cur_fs_str, expected_fs_str);
47+
48+
/* Test current working directory cases */
49+
50+
// At this point, the current working file system has not been set
51+
// So we expect a NULL output
52+
assert!(shell.get_cur_dir(None).is_none());
53+
54+
// Setting the current working file system and current working directory
55+
let dir_var = cstr16!("fs0:/");
56+
let status = shell.set_cur_dir(None, Some(dir_var));
57+
assert_eq!(status, Status::SUCCESS);
58+
let cur_fs_str = shell
59+
.get_cur_dir(Some(fs_var))
60+
.expect("Could not get the current file system mapping");
61+
let expected_fs_str = cstr16!("FS0:");
62+
assert_eq!(cur_fs_str, expected_fs_str);
63+
64+
let cur_fs_str = shell
65+
.get_cur_dir(None)
66+
.expect("Could not get the current file system mapping");
67+
assert_eq!(cur_fs_str, expected_fs_str);
68+
69+
// Changing current working directory
70+
let dir_var = cstr16!("/efi");
71+
let status = shell.set_cur_dir(None, Some(dir_var));
72+
assert_eq!(status, Status::SUCCESS);
73+
let cur_fs_str = shell
74+
.get_cur_dir(Some(fs_var))
75+
.expect("Could not get the current file system mapping");
76+
let expected_fs_str = cstr16!("FS0:\\efi");
77+
assert_eq!(cur_fs_str, expected_fs_str);
78+
let cur_fs_str = shell
79+
.get_cur_dir(None)
80+
.expect("Could not get the current file system mapping");
81+
assert_eq!(cur_fs_str, expected_fs_str);
82+
83+
// Changing current directory in a non-current working file system
84+
let fs_var = cstr16!("fs0:");
85+
let dir_var = cstr16!("efi/tools");
86+
let status = shell.set_cur_dir(Some(fs_var), Some(dir_var));
87+
assert_eq!(status, Status::SUCCESS);
88+
let cur_fs_str = shell
89+
.get_cur_dir(None)
90+
.expect("Could not get the current file system mapping");
91+
assert_ne!(cur_fs_str, expected_fs_str);
92+
93+
let expected_fs_str = cstr16!("FS0:\\efi\\tools");
94+
let cur_fs_str = shell
95+
.get_cur_dir(Some(fs_var))
96+
.expect("Could not get the current file system mapping");
97+
assert_eq!(cur_fs_str, expected_fs_str);
98+
}
599

6100
pub fn test() {
7101
info!("Running shell protocol tests");
8102

9103
let handle = boot::get_handle_for_protocol::<Shell>().expect("No Shell handles");
10104

11-
let mut _shell =
105+
let shell =
12106
boot::open_protocol_exclusive::<Shell>(handle).expect("Failed to open Shell protocol");
107+
108+
test_cur_dir(&shell);
13109
}

uefi/src/proto/shell/mod.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@ use uefi_raw::Status;
77

88
use core::ptr;
99

10-
pub use uefi_raw::protocol::shell::ShellProtocol;
10+
use uefi_raw::protocol::shell::ShellProtocol;
1111

1212
use crate::{CStr16, Char16};
1313

1414
/// Shell Protocol
1515
#[derive(Debug)]
1616
#[repr(transparent)]
17-
#[unsafe_protocol(uefi_raw::protocol::shell::ShellProtocol::GUID)]
18-
pub struct Shell(uefi_raw::protocol::shell::ShellProtocol);
17+
#[unsafe_protocol(ShellProtocol::GUID)]
18+
pub struct Shell(ShellProtocol);
1919

2020
impl Shell {
2121
/// Returns the current directory on the specified device
@@ -24,12 +24,13 @@ impl Shell {
2424
///
2525
/// * `file_system_mapping` - The file system mapping for which to get
2626
/// the current directory
27+
///
2728
/// # Returns
2829
///
2930
/// * `Some(cwd)` - CStr16 containing the current working directory
3031
/// * `None` - Could not retrieve current directory
3132
#[must_use]
32-
pub fn get_cur_dir<'a>(&'a self, file_system_mapping: Option<&CStr16>) -> Option<&'a CStr16> {
33+
pub fn get_cur_dir(&self, file_system_mapping: Option<&CStr16>) -> Option<&CStr16> {
3334
let mapping_ptr: *const Char16 = file_system_mapping.map_or(ptr::null(), |x| (x.as_ptr()));
3435
let cur_dir = unsafe { (self.0.get_cur_dir)(mapping_ptr.cast()) };
3536
if cur_dir.is_null() {
@@ -46,13 +47,14 @@ impl Shell {
4647
/// * `file_system` - Pointer to the file system's mapped name.
4748
/// * `directory` - Points to the directory on the device specified by
4849
/// `file_system`.
50+
///
4951
/// # Returns
5052
///
51-
/// * `Status::SUCCESS` The directory was successfully set
53+
/// * `Status::SUCCESS` - The directory was successfully set
5254
///
5355
/// # Errors
5456
///
55-
/// * `Status::EFI_NOT_FOUND` The directory does not exist
57+
/// * `Status::EFI_NOT_FOUND` - The directory does not exist
5658
pub fn set_cur_dir(&self, file_system: Option<&CStr16>, directory: Option<&CStr16>) -> Status {
5759
let fs_ptr: *const Char16 = file_system.map_or(ptr::null(), |x| (x.as_ptr()));
5860
let dir_ptr: *const Char16 = directory.map_or(ptr::null(), |x| (x.as_ptr()));

0 commit comments

Comments
 (0)