22
33use uefi:: boot:: ScopedProtocol ;
44use uefi:: proto:: shell:: Shell ;
5- use uefi:: { Error , Status , boot, cstr16} ;
5+ use uefi:: { Error , Status , boot, cstr16, CStr16 } ;
66
77/// Test `current_dir()` and `set_current_dir()`
88pub fn test_current_dir ( shell : & ScopedProtocol < Shell > ) {
@@ -100,6 +100,45 @@ pub fn test_current_dir(shell: &ScopedProtocol<Shell>) {
100100 assert_eq ! ( cur_fs_str, expected_fs_str) ;
101101}
102102
103+ /// Test ``get_env()`` and ``set_env()``
104+ pub fn test_env ( shell : & ScopedProtocol < Shell > ) {
105+ let mut test_buf = [ 0u16 ; 128 ] ;
106+
107+ /* Test retrieving list of environment variable names (null input) */
108+ let cur_env_vec = shell
109+ . get_env ( None )
110+ . expect ( "Could not get environment variable" ) ;
111+ assert_eq ! (
112+ * cur_env_vec. first( ) . unwrap( ) ,
113+ CStr16 :: from_str_with_buf( "path" , & mut test_buf) . unwrap( )
114+ ) ;
115+ assert_eq ! (
116+ * cur_env_vec. get( 1 ) . unwrap( ) ,
117+ CStr16 :: from_str_with_buf( "nonesting" , & mut test_buf) . unwrap( )
118+ ) ;
119+
120+ /* Test setting and getting a specific environment variable */
121+ let mut test_env_buf = [ 0u16 ; 32 ] ;
122+ let test_var = CStr16 :: from_str_with_buf ( "test_var" , & mut test_env_buf) . unwrap ( ) ;
123+ let mut test_val_buf = [ 0u16 ; 32 ] ;
124+ let test_val = CStr16 :: from_str_with_buf ( "test_val" , & mut test_val_buf) . unwrap ( ) ;
125+ assert ! ( shell. get_env( Some ( test_var) ) . is_none( ) ) ;
126+ let status = shell. set_env ( test_var, test_val, false ) ;
127+ assert_eq ! ( status, Status :: SUCCESS ) ;
128+ let cur_env_str = * shell
129+ . get_env ( Some ( test_var) )
130+ . expect ( "Could not get environment variable" )
131+ . first ( )
132+ . unwrap ( ) ;
133+ assert_eq ! ( cur_env_str, test_val) ;
134+
135+ /* Test deleting environment variable */
136+ let test_val = CStr16 :: from_str_with_buf ( "" , & mut test_val_buf) . unwrap ( ) ;
137+ let status = shell. set_env ( test_var, test_val, false ) ;
138+ assert_eq ! ( status, Status :: SUCCESS ) ;
139+ assert ! ( shell. get_env( Some ( test_var) ) . is_none( ) ) ;
140+ }
141+
103142pub fn test ( ) {
104143 info ! ( "Running shell protocol tests" ) ;
105144
@@ -109,4 +148,5 @@ pub fn test() {
109148 boot:: open_protocol_exclusive :: < Shell > ( handle) . expect ( "Failed to open Shell protocol" ) ;
110149
111150 test_current_dir ( & shell) ;
151+ test_env ( & shell) ;
112152}
0 commit comments