11use log:: info;
2- use uefi:: guid;
32use uefi:: prelude:: * ;
43use uefi:: table:: runtime:: { VariableAttributes , VariableVendor } ;
4+ use uefi:: { guid, runtime, CStr16 , Error } ;
55
6- fn test_variables ( rt : & RuntimeServices ) {
7- let name = cstr16 ! ( "UefiRsTestVar" ) ;
8- let test_value = b"TestValue" ;
9- let test_attrs = VariableAttributes :: BOOTSERVICE_ACCESS | VariableAttributes :: RUNTIME_ACCESS ;
6+ /// Test variable name.
7+ const NAME : & CStr16 = cstr16 ! ( "UefiRsTestVar" ) ;
8+
9+ /// Test variable vendor.
10+ const VENDOR : & VariableVendor = & VariableVendor ( guid ! ( "9baf21cf-e187-497e-ae77-5bd8b0e09703" ) ) ;
11+
12+ /// Test variable value.
13+ const VALUE : & [ u8 ] = b"TestValue" ;
1014
11- // Arbitrary GUID generated for this test.
12- let vendor = VariableVendor ( guid ! ( "9baf21cf-e187-497e-ae77-5bd8b0e09703" ) ) ;
15+ /// Test variable attributes.
16+ const ATTRS : VariableAttributes =
17+ VariableAttributes :: BOOTSERVICE_ACCESS . union ( VariableAttributes :: RUNTIME_ACCESS ) ;
1318
19+ fn test_variables ( rt : & RuntimeServices ) {
1420 info ! ( "Testing set_variable" ) ;
15- rt. set_variable ( name , & vendor , test_attrs , test_value )
21+ rt. set_variable ( NAME , VENDOR , ATTRS , VALUE )
1622 . expect ( "failed to set variable" ) ;
1723
1824 info ! ( "Testing get_variable_size" ) ;
1925 let size = rt
20- . get_variable_size ( name , & vendor )
26+ . get_variable_size ( NAME , VENDOR )
2127 . expect ( "failed to get variable size" ) ;
22- assert_eq ! ( size, test_value . len( ) ) ;
28+ assert_eq ! ( size, VALUE . len( ) ) ;
2329
2430 info ! ( "Testing get_variable" ) ;
2531 let mut buf = [ 0u8 ; 9 ] ;
2632 let ( data, attrs) = rt
27- . get_variable ( name , & vendor , & mut buf)
33+ . get_variable ( NAME , VENDOR , & mut buf)
2834 . expect ( "failed to get variable" ) ;
29- assert_eq ! ( data, test_value ) ;
30- assert_eq ! ( attrs, test_attrs ) ;
35+ assert_eq ! ( data, VALUE ) ;
36+ assert_eq ! ( attrs, ATTRS ) ;
3137
3238 info ! ( "Testing get_variable_boxed" ) ;
3339 let ( data, attrs) = rt
34- . get_variable_boxed ( name , & vendor )
40+ . get_variable_boxed ( NAME , VENDOR )
3541 . expect ( "failed to get variable" ) ;
36- assert_eq ! ( & * data, test_value ) ;
37- assert_eq ! ( attrs, test_attrs ) ;
42+ assert_eq ! ( & * data, VALUE ) ;
43+ assert_eq ! ( attrs, ATTRS ) ;
3844
3945 info ! ( "Testing variable_keys" ) ;
4046 let variable_keys = rt. variable_keys ( ) . expect ( "failed to get variable keys" ) ;
@@ -46,10 +52,44 @@ fn test_variables(rt: &RuntimeServices) {
4652 }
4753
4854 info ! ( "Testing delete_variable()" ) ;
49- rt. delete_variable ( name , & vendor )
55+ rt. delete_variable ( NAME , VENDOR )
5056 . expect ( "failed to delete variable" ) ;
5157 assert_eq ! (
52- rt. get_variable( name, & vendor, & mut buf)
58+ rt. get_variable( NAME , VENDOR , & mut buf)
59+ . unwrap_err( )
60+ . status( ) ,
61+ Status :: NOT_FOUND
62+ ) ;
63+ }
64+
65+ /// Test the variable functions in `uefi::runtime`.
66+ fn test_variables_freestanding ( ) {
67+ // Create the test variable.
68+ runtime:: set_variable ( NAME , VENDOR , ATTRS , VALUE ) . expect ( "failed to set variable" ) ;
69+
70+ // Test `get_variable` with too small of a buffer.
71+ let mut buf = [ 0u8 ; 0 ] ;
72+ assert_eq ! (
73+ runtime:: get_variable( NAME , VENDOR , & mut buf) . unwrap_err( ) ,
74+ Error :: new( Status :: BUFFER_TOO_SMALL , Some ( 9 ) )
75+ ) ;
76+
77+ // Test `get_variable`.
78+ let mut buf = [ 0u8 ; 9 ] ;
79+ let ( data, attrs) =
80+ runtime:: get_variable ( NAME , VENDOR , & mut buf) . expect ( "failed to get variable" ) ;
81+ assert_eq ! ( data, VALUE ) ;
82+ assert_eq ! ( attrs, ATTRS ) ;
83+
84+ // Test `get_variable_boxed`.
85+ let ( data, attrs) = runtime:: get_variable_boxed ( NAME , VENDOR ) . expect ( "failed to get variable" ) ;
86+ assert_eq ! ( & * data, VALUE ) ;
87+ assert_eq ! ( attrs, ATTRS ) ;
88+
89+ // Delete the variable and verify it can no longer be read.
90+ runtime:: delete_variable ( NAME , VENDOR ) . expect ( "failed to delete variable" ) ;
91+ assert_eq ! (
92+ runtime:: get_variable( NAME , VENDOR , & mut buf)
5393 . unwrap_err( )
5494 . status( ) ,
5595 Status :: NOT_FOUND
@@ -76,4 +116,5 @@ fn test_variable_info(rt: &RuntimeServices) {
76116pub fn test ( rt : & RuntimeServices ) {
77117 test_variables ( rt) ;
78118 test_variable_info ( rt) ;
119+ test_variables_freestanding ( ) ;
79120}
0 commit comments