Skip to content

Commit 94b5ee1

Browse files
committed
Add tests for the protocol handler services.
1 parent 89688e6 commit 94b5ee1

File tree

2 files changed

+72
-2
lines changed

2 files changed

+72
-2
lines changed

uefi-test-runner/src/boot/misc.rs

+71-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
use core::ffi::c_void;
22
use core::ptr::NonNull;
33

4-
use uefi::table::boot::{BootServices, EventType, TimerTrigger, Tpl};
5-
use uefi::Event;
4+
use uefi::proto::Protocol;
5+
use uefi::table::boot::{BootServices, EventType, SearchType, TimerTrigger, Tpl};
6+
use uefi::{unsafe_guid, Event, Identify};
67

78
pub fn test(bt: &BootServices) {
89
info!("Testing timer...");
@@ -12,6 +13,11 @@ pub fn test(bt: &BootServices) {
1213
test_callback_with_ctx(bt);
1314
info!("Testing watchdog...");
1415
test_watchdog(bt);
16+
info!("Testing protocol handler services...");
17+
test_register_protocol_notify(bt);
18+
test_install_protocol_interface(bt);
19+
test_reinstall_protocol_interface(bt);
20+
test_uninstall_protocol_interface(bt);
1521
}
1622

1723
fn test_timer(bt: &BootServices) {
@@ -72,3 +78,66 @@ fn test_watchdog(bt: &BootServices) {
7278
bt.set_watchdog_timer(0, 0x10000, None)
7379
.expect("Could not set watchdog timer");
7480
}
81+
82+
/// Dummy protocol for tests
83+
#[unsafe_guid("1a972918-3f69-4b5d-8cb4-cece2309c7f5")]
84+
#[derive(Protocol)]
85+
struct TestProtocol {}
86+
87+
unsafe extern "efiapi" fn _test_notify(_event: Event, _context: Option<NonNull<c_void>>) {
88+
info!("Protocol was (re)installed and this function notified.")
89+
}
90+
91+
fn test_register_protocol_notify(bt: &BootServices) {
92+
let protocol = &TestProtocol::GUID;
93+
let event = unsafe {
94+
bt.create_event(
95+
EventType::NOTIFY_SIGNAL,
96+
Tpl::NOTIFY,
97+
Some(_test_notify),
98+
None,
99+
)
100+
.expect("Failed to create an event")
101+
};
102+
103+
bt.register_protocol_notify(protocol, event)
104+
.expect("Failed to register protocol notify fn");
105+
}
106+
107+
fn test_install_protocol_interface(bt: &BootServices) {
108+
info!("Installing TestProtocol");
109+
110+
let _ = unsafe {
111+
bt.install_protocol_interface(None, &TestProtocol::GUID, None)
112+
.expect("Failed to install protocol interface")
113+
};
114+
115+
let _ = bt
116+
.locate_handle_buffer(SearchType::from_proto::<TestProtocol>())
117+
.expect("Failed to find protocol after it was installed");
118+
}
119+
120+
fn test_reinstall_protocol_interface(bt: &BootServices) {
121+
info!("Reinstalling TestProtocol");
122+
let handle = bt
123+
.locate_handle_buffer(SearchType::from_proto::<TestProtocol>())
124+
.expect("Failed to find protocol to uninstall")
125+
.handles()[0];
126+
127+
unsafe {
128+
let _ = bt.reinstall_protocol_interface(handle, &TestProtocol::GUID, None, None);
129+
}
130+
}
131+
132+
fn test_uninstall_protocol_interface(bt: &BootServices) {
133+
info!("Uninstalling TestProtocol");
134+
let handle = bt
135+
.locate_handle_buffer(SearchType::from_proto::<TestProtocol>())
136+
.expect("Failed to find protocol to uninstall")
137+
.handles()[0];
138+
139+
unsafe {
140+
bt.uninstall_protocol_interface(handle, &TestProtocol::GUID, None)
141+
.expect("Failed to uninstall protocol interface");
142+
}
143+
}

uefi-test-runner/src/main.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#![no_std]
22
#![no_main]
33
#![feature(abi_efiapi)]
4+
#![feature(negative_impls)]
45

56
#[macro_use]
67
extern crate log;

0 commit comments

Comments
 (0)