22
33use uefi:: boot:: ScopedProtocol ;
44use uefi:: proto:: shell:: Shell ;
5- use uefi:: { CStr16 , boot } ;
5+ use uefi:: { boot , cstr16 } ;
66use uefi_raw:: Status ;
77
8- /// Test `` get_env()`` , `` get_envs()`` , and `` set_env()` `
8+ /// Test `get_env()`, `get_envs()`, and `set_env()`
99pub fn test_env ( shell : & ScopedProtocol < Shell > ) {
10- let mut test_buf = [ 0u16 ; 128 ] ;
11-
1210 /* 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" ) , ) ;
1315 let cur_env_vec = shell. get_envs ( ) ;
14- assert_eq ! (
15- * cur_env_vec. first( ) . unwrap( ) ,
16- CStr16 :: from_str_with_buf( "path" , & mut test_buf) . unwrap( )
17- ) ;
18- assert_eq ! (
19- * cur_env_vec. get( 1 ) . unwrap( ) ,
20- CStr16 :: from_str_with_buf( "nonesting" , & mut test_buf) . unwrap( )
21- ) ;
22- let default_len = cur_env_vec. len ( ) ;
16+ let default_len = cur_env_vec. count ( ) ;
2317
2418 /* Test setting and getting a specific environment variable */
25- let mut test_env_buf = [ 0u16 ; 32 ] ;
26- let test_var = CStr16 :: from_str_with_buf ( "test_var" , & mut test_env_buf) . unwrap ( ) ;
27- let mut test_val_buf = [ 0u16 ; 32 ] ;
28- let test_val = CStr16 :: from_str_with_buf ( "test_val" , & mut test_val_buf) . unwrap ( ) ;
19+ let cur_env_vec = shell. get_envs ( ) ;
20+ let test_var = cstr16 ! ( "test_var" ) ;
21+ let test_val = cstr16 ! ( "test_val" ) ;
2922 assert ! ( shell. get_env( test_var) . is_none( ) ) ;
3023 let status = shell. set_env ( test_var, test_val, false ) ;
3124 assert_eq ! ( status, Status :: SUCCESS ) ;
@@ -34,64 +27,81 @@ pub fn test_env(shell: &ScopedProtocol<Shell>) {
3427 . expect ( "Could not get environment variable" ) ;
3528 assert_eq ! ( cur_env_str, test_val) ;
3629
37- assert ! ( !cur_env_vec. contains( & test_var) ) ;
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+
3846 let cur_env_vec = shell. get_envs ( ) ;
39- assert ! ( cur_env_vec. contains( & test_var) ) ;
40- assert_eq ! ( cur_env_vec. len( ) , default_len + 1 ) ;
47+ assert_eq ! ( cur_env_vec. count( ) , default_len + 1 ) ;
4148
4249 /* Test deleting environment variable */
43- let test_val = CStr16 :: from_str_with_buf ( "" , & mut test_val_buf ) . unwrap ( ) ;
50+ let test_val = cstr16 ! ( "" ) ;
4451 let status = shell. set_env ( test_var, test_val, false ) ;
4552 assert_eq ! ( status, Status :: SUCCESS ) ;
4653 assert ! ( shell. get_env( test_var) . is_none( ) ) ;
4754
4855 let cur_env_vec = shell. get_envs ( ) ;
49- assert ! ( !cur_env_vec. contains( & test_var) ) ;
50- assert_eq ! ( cur_env_vec. len( ) , default_len) ;
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) ;
5165}
5266
53- /// Test `` get_cur_dir()`` and `` set_cur_dir()` `
67+ /// Test `get_cur_dir()` and `set_cur_dir()`
5468pub fn test_cur_dir ( shell : & ScopedProtocol < Shell > ) {
55- let mut test_buf = [ 0u16 ; 128 ] ;
56-
5769 /* Test setting and getting current file system and current directory */
58- let mut fs_buf = [ 0u16 ; 16 ] ;
59- let fs_var = CStr16 :: from_str_with_buf ( "fs0:" , & mut fs_buf) . unwrap ( ) ;
60- let mut dir_buf = [ 0u16 ; 32 ] ;
61- let dir_var = CStr16 :: from_str_with_buf ( "/" , & mut dir_buf) . unwrap ( ) ;
70+ let fs_var = cstr16 ! ( "fs0:" ) ;
71+ let dir_var = cstr16 ! ( "/" ) ;
6272 let status = shell. set_cur_dir ( Some ( fs_var) , Some ( dir_var) ) ;
6373 assert_eq ! ( status, Status :: SUCCESS ) ;
6474
6575 let cur_fs_str = shell
6676 . get_cur_dir ( Some ( fs_var) )
6777 . expect ( "Could not get the current file system mapping" ) ;
68- let expected_fs_str = CStr16 :: from_str_with_buf ( "FS0:\\ " , & mut test_buf ) . unwrap ( ) ;
78+ let expected_fs_str = cstr16 ! ( "FS0:\\ " ) ;
6979 assert_eq ! ( cur_fs_str, expected_fs_str) ;
7080
7181 // Changing current file system
72- let fs_var = CStr16 :: from_str_with_buf ( "fs1:" , & mut fs_buf ) . unwrap ( ) ;
73- let dir_var = CStr16 :: from_str_with_buf ( "/" , & mut dir_buf ) . unwrap ( ) ;
82+ let fs_var = cstr16 ! ( "fs1:" ) ;
83+ let dir_var = cstr16 ! ( "/" ) ;
7484 let status = shell. set_cur_dir ( Some ( fs_var) , Some ( dir_var) ) ;
7585 assert_eq ! ( status, Status :: SUCCESS ) ;
7686
7787 let cur_fs_str = shell
7888 . get_cur_dir ( Some ( fs_var) )
7989 . expect ( "Could not get the current file system mapping" ) ;
8090 assert_ne ! ( cur_fs_str, expected_fs_str) ;
81- let expected_fs_str = CStr16 :: from_str_with_buf ( "FS1:\\ " , & mut test_buf ) . unwrap ( ) ;
91+ let expected_fs_str = cstr16 ! ( "FS1:\\ " ) ;
8292 assert_eq ! ( cur_fs_str, expected_fs_str) ;
8393
8494 // Changing current file system and current directory
85- let fs_var = CStr16 :: from_str_with_buf ( "fs0:" , & mut fs_buf ) . unwrap ( ) ;
86- let dir_var = CStr16 :: from_str_with_buf ( "efi/" , & mut dir_buf ) . unwrap ( ) ;
95+ let fs_var = cstr16 ! ( "fs0:" ) ;
96+ let dir_var = cstr16 ! ( "efi/" ) ;
8797 let status = shell. set_cur_dir ( Some ( fs_var) , Some ( dir_var) ) ;
8898 assert_eq ! ( status, Status :: SUCCESS ) ;
8999
90100 let cur_fs_str = shell
91101 . get_cur_dir ( Some ( fs_var) )
92102 . expect ( "Could not get the current file system mapping" ) ;
93103 assert_ne ! ( cur_fs_str, expected_fs_str) ;
94- let expected_fs_str = CStr16 :: from_str_with_buf ( "FS0:\\ efi" , & mut test_buf ) . unwrap ( ) ;
104+ let expected_fs_str = cstr16 ! ( "FS0:\\ efi" ) ;
95105 assert_eq ! ( cur_fs_str, expected_fs_str) ;
96106
97107 /* Test current working directory cases */
@@ -101,13 +111,13 @@ pub fn test_cur_dir(shell: &ScopedProtocol<Shell>) {
101111 assert ! ( shell. get_cur_dir( None ) . is_none( ) ) ;
102112
103113 // Setting the current working file system and current working directory
104- let dir_var = CStr16 :: from_str_with_buf ( "fs0:/" , & mut dir_buf ) . unwrap ( ) ;
114+ let dir_var = cstr16 ! ( "fs0:/" ) ;
105115 let status = shell. set_cur_dir ( None , Some ( dir_var) ) ;
106116 assert_eq ! ( status, Status :: SUCCESS ) ;
107117 let cur_fs_str = shell
108118 . get_cur_dir ( Some ( fs_var) )
109119 . expect ( "Could not get the current file system mapping" ) ;
110- let expected_fs_str = CStr16 :: from_str_with_buf ( "FS0:" , & mut test_buf ) . unwrap ( ) ;
120+ let expected_fs_str = cstr16 ! ( "FS0:" ) ;
111121 assert_eq ! ( cur_fs_str, expected_fs_str) ;
112122
113123 let cur_fs_str = shell
@@ -116,30 +126,30 @@ pub fn test_cur_dir(shell: &ScopedProtocol<Shell>) {
116126 assert_eq ! ( cur_fs_str, expected_fs_str) ;
117127
118128 // Changing current working directory
119- let dir_var = CStr16 :: from_str_with_buf ( "/efi" , & mut dir_buf ) . unwrap ( ) ;
129+ let dir_var = cstr16 ! ( "/efi" ) ;
120130 let status = shell. set_cur_dir ( None , Some ( dir_var) ) ;
121131 assert_eq ! ( status, Status :: SUCCESS ) ;
122132 let cur_fs_str = shell
123133 . get_cur_dir ( Some ( fs_var) )
124134 . expect ( "Could not get the current file system mapping" ) ;
125- let expected_fs_str = CStr16 :: from_str_with_buf ( "FS0:\\ efi" , & mut test_buf ) . unwrap ( ) ;
135+ let expected_fs_str = cstr16 ! ( "FS0:\\ efi" ) ;
126136 assert_eq ! ( cur_fs_str, expected_fs_str) ;
127137 let cur_fs_str = shell
128138 . get_cur_dir ( None )
129139 . expect ( "Could not get the current file system mapping" ) ;
130140 assert_eq ! ( cur_fs_str, expected_fs_str) ;
131141
132142 // Changing current directory in a non-current working file system
133- let fs_var = CStr16 :: from_str_with_buf ( "fs0:" , & mut fs_buf ) . unwrap ( ) ;
134- let dir_var = CStr16 :: from_str_with_buf ( "efi/tools" , & mut dir_buf ) . unwrap ( ) ;
143+ let fs_var = cstr16 ! ( "fs0:" ) ;
144+ let dir_var = cstr16 ! ( "efi/tools" ) ;
135145 let status = shell. set_cur_dir ( Some ( fs_var) , Some ( dir_var) ) ;
136146 assert_eq ! ( status, Status :: SUCCESS ) ;
137147 let cur_fs_str = shell
138148 . get_cur_dir ( None )
139149 . expect ( "Could not get the current file system mapping" ) ;
140150 assert_ne ! ( cur_fs_str, expected_fs_str) ;
141151
142- let expected_fs_str = CStr16 :: from_str_with_buf ( "FS0:\\ efi\\ tools" , & mut test_buf ) . unwrap ( ) ;
152+ let expected_fs_str = cstr16 ! ( "FS0:\\ efi\\ tools" ) ;
143153 let cur_fs_str = shell
144154 . get_cur_dir ( Some ( fs_var) )
145155 . expect ( "Could not get the current file system mapping" ) ;
0 commit comments