|
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::{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 | +} |
5 | 99 |
|
6 | 100 | pub fn test() {
|
7 | 101 | info!("Running shell protocol tests");
|
8 | 102 |
|
9 | 103 | let handle = boot::get_handle_for_protocol::<Shell>().expect("No Shell handles");
|
10 | 104 |
|
11 |
| - let mut _shell = |
| 105 | + let shell = |
12 | 106 | boot::open_protocol_exclusive::<Shell>(handle).expect("Failed to open Shell protocol");
|
| 107 | + |
| 108 | + test_cur_dir(&shell); |
13 | 109 | }
|
0 commit comments