guest_test_uefi/uefi/tests/
mod.rs

1// Copyright (c) Microsoft Corporation.
2// Licensed under the MIT License.
3
4use crate::uefi::Splashes;
5use crate::uefi::splash;
6use core::num::NonZeroU8;
7use uefi::boot;
8use uefi::cstr16;
9use uefi::println;
10use uefi::runtime;
11use uefi::runtime::VariableVendor;
12
13// TODO: add runtime config for which tests to run (e.g: skipping watchdog)
14pub fn run_tests() {
15    println!("running tests...");
16    let mut splash_seq = 2u8;
17
18    macro_rules! do_test {
19        ($test_fn:ident) => {{
20            let name = stringify!($test_fn);
21            println!(">>>>>> [TEST]: running '{}'", name);
22            $test_fn();
23            splash_seq = splash_seq.wrapping_shl(1);
24            splash::draw_splash(Splashes(NonZeroU8::new(splash_seq).unwrap()));
25            boot::stall(1000000); // stall for 1 seconds
26        }};
27    }
28
29    do_test!(test_global_alloc);
30
31    do_test!(test_dbdefault);
32    // TODO: re-enable when UEFI handles dbDefault correctly
33    //do_test!(test_readonly);
34
35    // leave the watchdog test for last, since it blows away the VM
36    do_test!(test_watchdog);
37}
38
39fn test_global_alloc() {
40    let s = format!("hello {}!", "world");
41    println!("look 'ma, i'm outputting a heap allocated string: {}", s);
42}
43
44fn test_watchdog() {
45    boot::set_watchdog_timer(5, 0xdeadbeef, None).unwrap();
46    boot::stall(1000000 * 6); // stall for 6 seconds
47    panic!("watchdog should've expired, but we're still running!")
48}
49
50fn test_dbdefault() {
51    let db = runtime::get_variable_boxed(cstr16!("db"), &VariableVendor::IMAGE_SECURITY_DATABASE);
52    let dbdefault =
53        runtime::get_variable_boxed(cstr16!("dbDefault"), &VariableVendor::GLOBAL_VARIABLE);
54
55    let (db_data, _) = db.expect("db not found");
56    let (dbdefault_data, _) = dbdefault.expect("dbDefault not found");
57
58    assert_eq!(db_data, dbdefault_data);
59}
60
61/* TODO: re-enable when UEFI handles dbDefault correctly
62fn test_readonly(rt: &RuntimeServices) {
63    match rt.set_variable(
64        cstr16!("dbDefault"),
65        &VariableVendor::GLOBAL_VARIABLE,
66        VariableAttributes::BOOTSERVICE_ACCESS | VariableAttributes::RUNTIME_ACCESS,
67        &[0, 1, 2],
68    ) {
69        Ok(_) => {
70            panic!("Expected an error, but got Ok")
71        }
72        Err(error) => {
73            assert_eq!(
74                error.status(),
75                Status::WRITE_PROTECTED,
76                "Error status: {:?}",
77                error.status()
78            )
79        }
80    }
81}
82*/