Skip to content

Commit 0f30078

Browse files
committed
uefi-test-runner: Added tests for EFI Shell env and cur_dir functions
This commit includes tests for the following EFI Shell Protocol functions: get_env(), set_env(), get_cur_dir(), and set_cur_dir().
1 parent 92e07bb commit 0f30078

File tree

1 file changed

+142
-2
lines changed

1 file changed

+142
-2
lines changed

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

Lines changed: 142 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,153 @@
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::{CStr16, boot};
6+
use uefi_raw::Status;
7+
8+
/// Test ``get_env()`` and ``set_env()``
9+
pub fn test_env(shell: &ScopedProtocol<Shell>) {
10+
let mut test_buf = [0u16; 128];
11+
12+
/* Test retrieving list of environment variable names (null input) */
13+
let cur_env_vec = shell
14+
.get_env(None)
15+
.expect("Could not get environment variable");
16+
assert_eq!(
17+
*cur_env_vec.first().unwrap(),
18+
CStr16::from_str_with_buf("path", &mut test_buf).unwrap()
19+
);
20+
assert_eq!(
21+
*cur_env_vec.get(1).unwrap(),
22+
CStr16::from_str_with_buf("nonesting", &mut test_buf).unwrap()
23+
);
24+
25+
/* Test setting and getting a specific environment variable */
26+
let mut test_env_buf = [0u16; 32];
27+
let test_var = CStr16::from_str_with_buf("test_var", &mut test_env_buf).unwrap();
28+
let mut test_val_buf = [0u16; 32];
29+
let test_val = CStr16::from_str_with_buf("test_val", &mut test_val_buf).unwrap();
30+
assert!(shell.get_env(Some(test_var)).is_none());
31+
let status = shell.set_env(test_var, test_val, false);
32+
assert_eq!(status, Status::SUCCESS);
33+
let cur_env_str = *shell
34+
.get_env(Some(test_var))
35+
.expect("Could not get environment variable")
36+
.first()
37+
.unwrap();
38+
assert_eq!(cur_env_str, test_val);
39+
40+
/* Test deleting environment variable */
41+
let test_val = CStr16::from_str_with_buf("", &mut test_val_buf).unwrap();
42+
let status = shell.set_env(test_var, test_val, false);
43+
assert_eq!(status, Status::SUCCESS);
44+
assert!(shell.get_env(Some(test_var)).is_none());
45+
}
46+
47+
/// Test ``get_cur_dir()`` and ``set_cur_dir()``
48+
pub fn test_cur_dir(shell: &ScopedProtocol<Shell>) {
49+
let mut test_buf = [0u16; 128];
50+
51+
/* Test setting and getting current file system and current directory */
52+
let mut fs_buf = [0u16; 16];
53+
let fs_var = CStr16::from_str_with_buf("fs0:", &mut fs_buf).unwrap();
54+
let mut dir_buf = [0u16; 32];
55+
let dir_var = CStr16::from_str_with_buf("/", &mut dir_buf).unwrap();
56+
let status = shell.set_cur_dir(Some(fs_var), Some(dir_var));
57+
assert_eq!(status, Status::SUCCESS);
58+
59+
let cur_fs_str = shell
60+
.get_cur_dir(Some(fs_var))
61+
.expect("Could not get the current file system mapping");
62+
let expected_fs_str = CStr16::from_str_with_buf("FS0:\\", &mut test_buf).unwrap();
63+
assert_eq!(cur_fs_str, expected_fs_str);
64+
65+
// Changing current file system
66+
let fs_var = CStr16::from_str_with_buf("fs1:", &mut fs_buf).unwrap();
67+
let dir_var = CStr16::from_str_with_buf("/", &mut dir_buf).unwrap();
68+
let status = shell.set_cur_dir(Some(fs_var), Some(dir_var));
69+
assert_eq!(status, Status::SUCCESS);
70+
71+
let cur_fs_str = shell
72+
.get_cur_dir(Some(fs_var))
73+
.expect("Could not get the current file system mapping");
74+
assert_ne!(cur_fs_str, expected_fs_str);
75+
let expected_fs_str = CStr16::from_str_with_buf("FS1:\\", &mut test_buf).unwrap();
76+
assert_eq!(cur_fs_str, expected_fs_str);
77+
78+
// Changing current file system and current directory
79+
let fs_var = CStr16::from_str_with_buf("fs0:", &mut fs_buf).unwrap();
80+
let dir_var = CStr16::from_str_with_buf("efi/", &mut dir_buf).unwrap();
81+
let status = shell.set_cur_dir(Some(fs_var), Some(dir_var));
82+
assert_eq!(status, Status::SUCCESS);
83+
84+
let cur_fs_str = shell
85+
.get_cur_dir(Some(fs_var))
86+
.expect("Could not get the current file system mapping");
87+
assert_ne!(cur_fs_str, expected_fs_str);
88+
let expected_fs_str = CStr16::from_str_with_buf("FS0:\\efi", &mut test_buf).unwrap();
89+
assert_eq!(cur_fs_str, expected_fs_str);
90+
91+
/* Test current working directory cases */
92+
93+
// At this point, the current working file system has not been set
94+
// So we expect a NULL output
95+
assert!(shell.get_cur_dir(None).is_none());
96+
97+
// Setting the current working file system and current working directory
98+
let dir_var = CStr16::from_str_with_buf("fs0:/", &mut dir_buf).unwrap();
99+
let status = shell.set_cur_dir(None, Some(dir_var));
100+
assert_eq!(status, Status::SUCCESS);
101+
let cur_fs_str = shell
102+
.get_cur_dir(Some(fs_var))
103+
.expect("Could not get the current file system mapping");
104+
let expected_fs_str = CStr16::from_str_with_buf("FS0:", &mut test_buf).unwrap();
105+
assert_eq!(cur_fs_str, expected_fs_str);
106+
107+
let cur_fs_str = shell
108+
.get_cur_dir(None)
109+
.expect("Could not get the current file system mapping");
110+
assert_eq!(cur_fs_str, expected_fs_str);
111+
112+
// Changing current working directory
113+
let dir_var = CStr16::from_str_with_buf("/efi", &mut dir_buf).unwrap();
114+
let status = shell.set_cur_dir(None, Some(dir_var));
115+
assert_eq!(status, Status::SUCCESS);
116+
let cur_fs_str = shell
117+
.get_cur_dir(Some(fs_var))
118+
.expect("Could not get the current file system mapping");
119+
let expected_fs_str = CStr16::from_str_with_buf("FS0:\\efi", &mut test_buf).unwrap();
120+
assert_eq!(cur_fs_str, expected_fs_str);
121+
let cur_fs_str = shell
122+
.get_cur_dir(None)
123+
.expect("Could not get the current file system mapping");
124+
assert_eq!(cur_fs_str, expected_fs_str);
125+
126+
// Changing current directory in a non-current working file system
127+
let fs_var = CStr16::from_str_with_buf("fs0:", &mut fs_buf).unwrap();
128+
let dir_var = CStr16::from_str_with_buf("efi/tools", &mut dir_buf).unwrap();
129+
let status = shell.set_cur_dir(Some(fs_var), Some(dir_var));
130+
assert_eq!(status, Status::SUCCESS);
131+
let cur_fs_str = shell
132+
.get_cur_dir(None)
133+
.expect("Could not get the current file system mapping");
134+
assert_ne!(cur_fs_str, expected_fs_str);
135+
136+
let expected_fs_str = CStr16::from_str_with_buf("FS0:\\efi\\tools", &mut test_buf).unwrap();
137+
let cur_fs_str = shell
138+
.get_cur_dir(Some(fs_var))
139+
.expect("Could not get the current file system mapping");
140+
assert_eq!(cur_fs_str, expected_fs_str);
141+
}
5142

6143
pub fn test() {
7144
info!("Running shell protocol tests");
8145

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

11-
let mut _shell =
148+
let shell =
12149
boot::open_protocol_exclusive::<Shell>(handle).expect("Failed to open Shell protocol");
150+
151+
test_env(&shell);
152+
test_cur_dir(&shell);
13153
}

0 commit comments

Comments
 (0)