1
1
use log:: info;
2
- use uefi:: guid;
3
2
use uefi:: prelude:: * ;
4
3
use uefi:: table:: runtime:: { VariableAttributes , VariableVendor } ;
4
+ use uefi:: { guid, runtime, CStr16 , Error } ;
5
5
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" ;
10
14
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 ) ;
13
18
19
+ fn test_variables ( rt : & RuntimeServices ) {
14
20
info ! ( "Testing set_variable" ) ;
15
- rt. set_variable ( name , & vendor , test_attrs , test_value )
21
+ rt. set_variable ( NAME , VENDOR , ATTRS , VALUE )
16
22
. expect ( "failed to set variable" ) ;
17
23
18
24
info ! ( "Testing get_variable_size" ) ;
19
25
let size = rt
20
- . get_variable_size ( name , & vendor )
26
+ . get_variable_size ( NAME , VENDOR )
21
27
. expect ( "failed to get variable size" ) ;
22
- assert_eq ! ( size, test_value . len( ) ) ;
28
+ assert_eq ! ( size, VALUE . len( ) ) ;
23
29
24
30
info ! ( "Testing get_variable" ) ;
25
31
let mut buf = [ 0u8 ; 9 ] ;
26
32
let ( data, attrs) = rt
27
- . get_variable ( name , & vendor , & mut buf)
33
+ . get_variable ( NAME , VENDOR , & mut buf)
28
34
. 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 ) ;
31
37
32
38
info ! ( "Testing get_variable_boxed" ) ;
33
39
let ( data, attrs) = rt
34
- . get_variable_boxed ( name , & vendor )
40
+ . get_variable_boxed ( NAME , VENDOR )
35
41
. 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 ) ;
38
44
39
45
info ! ( "Testing variable_keys" ) ;
40
46
let variable_keys = rt. variable_keys ( ) . expect ( "failed to get variable keys" ) ;
@@ -46,10 +52,44 @@ fn test_variables(rt: &RuntimeServices) {
46
52
}
47
53
48
54
info ! ( "Testing delete_variable()" ) ;
49
- rt. delete_variable ( name , & vendor )
55
+ rt. delete_variable ( NAME , VENDOR )
50
56
. expect ( "failed to delete variable" ) ;
51
57
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)
53
93
. unwrap_err( )
54
94
. status( ) ,
55
95
Status :: NOT_FOUND
@@ -76,4 +116,5 @@ fn test_variable_info(rt: &RuntimeServices) {
76
116
pub fn test ( rt : & RuntimeServices ) {
77
117
test_variables ( rt) ;
78
118
test_variable_info ( rt) ;
119
+ test_variables_freestanding ( ) ;
79
120
}
0 commit comments