Skip to content

Commit e0c8959

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 8e0f8c2 commit e0c8959

File tree

2 files changed

+285
-62
lines changed

2 files changed

+285
-62
lines changed

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

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

6159
pub fn test() {
7160
info!("Running shell protocol tests");
8161

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

11-
let mut _shell =
164+
let shell =
12165
boot::open_protocol_exclusive::<Shell>(handle).expect("Failed to open Shell protocol");
166+
167+
test_env(&shell);
168+
test_cur_dir(&shell);
13169
}

0 commit comments

Comments
 (0)