|
1 | 1 | // SPDX-License-Identifier: MIT OR Apache-2.0
|
2 | 2 |
|
3 | | -use uefi::boot; |
| 3 | +use uefi::boot::ScopedProtocol; |
4 | 4 | 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 | +} |
5 | 46 |
|
6 | 47 | pub fn test() {
|
7 | 48 | info!("Running shell protocol tests");
|
8 | 49 |
|
9 | 50 | let handle = boot::get_handle_for_protocol::<Shell>().expect("No Shell handles");
|
10 | 51 |
|
11 | | - let mut _shell = |
| 52 | + let shell = |
12 | 53 | boot::open_protocol_exclusive::<Shell>(handle).expect("Failed to open Shell protocol");
|
| 54 | + |
| 55 | + test_env(&shell); |
13 | 56 | }
|
0 commit comments