Skip to content

Commit b5fc228

Browse files
committed
add tests
1 parent a99fc04 commit b5fc228

File tree

7 files changed

+108
-0
lines changed

7 files changed

+108
-0
lines changed

.github/workflows/build.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,10 @@ jobs:
116116
working-directory: example-kernels/runner-doctest
117117
name: 'Run `cargo test -Z doctest-xcompile` for "runner-doctest" kernel'
118118

119+
- run: cargo test
120+
working-directory: example-kernels/runner-fail-reboot
121+
name: 'Run `cargo test` for "runner-fail-reboot" kernel'
122+
119123
check_formatting:
120124
name: "Check Formatting"
121125
runs-on: ubuntu-latest

example-kernels/Cargo.lock

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

example-kernels/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@ members = [
33
"basic",
44
"runner",
55
"runner-doctest",
6+
"runner-fail-reboot",
67
"runner-test",
78
]
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[build]
2+
target = "../x86_64-bootimage-example-kernels.json"
3+
4+
[target.'cfg(target_os = "none")']
5+
runner = "bootimage runner"
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/target/
2+
**/*.rs.bk
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
[package]
2+
name = "runner-fail-reboot"
3+
version = "0.1.0"
4+
authors = ["Philipp Oppermann <[email protected]>"]
5+
edition = "2018"
6+
7+
[dependencies]
8+
bootloader = "0.9.7"
9+
x86_64 = "0.11.0"
10+
rlibc = "1.0.0"
11+
12+
[package.metadata.bootimage]
13+
test-success-exit-code = 0 # this will test for the reboot
14+
test-args = ["-device", "isa-debug-exit,iobase=0xf4,iosize=0x04", "-display", "none"]
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
#![no_std]
2+
#![cfg_attr(test, no_main)]
3+
#![feature(custom_test_frameworks)]
4+
#![test_runner(crate::test_runner)]
5+
#![reexport_test_harness_main = "test_main"]
6+
7+
extern crate rlibc;
8+
9+
pub fn test_runner(tests: &[&dyn Fn()]) {
10+
for test in tests.iter() {
11+
test();
12+
}
13+
14+
unsafe {
15+
exit_qemu(ExitCode::Success);
16+
}
17+
}
18+
19+
#[test_case]
20+
fn should_reboot() {
21+
// this overflows the stack which leads to a triple fault
22+
// the as-if rule might allow this to get optimized away on release builds
23+
#[allow(unconditional_recursion)]
24+
fn stack_overflow() {
25+
stack_overflow()
26+
}
27+
stack_overflow()
28+
}
29+
30+
pub enum ExitCode {
31+
Success,
32+
Failed,
33+
}
34+
35+
impl ExitCode {
36+
fn code(&self) -> u32 {
37+
match self {
38+
ExitCode::Success => 0x10,
39+
ExitCode::Failed => 0x11,
40+
}
41+
}
42+
}
43+
44+
/// exit QEMU (see https://os.phil-opp.com/integration-tests/#shutting-down-qemu)
45+
pub unsafe fn exit_qemu(exit_code: ExitCode) {
46+
use x86_64::instructions::port::Port;
47+
48+
let mut port = Port::<u32>::new(0xf4);
49+
port.write(exit_code.code());
50+
}
51+
52+
#[cfg(test)]
53+
#[no_mangle]
54+
pub extern "C" fn _start() -> ! {
55+
test_main();
56+
57+
unsafe {
58+
exit_qemu(ExitCode::Failed);
59+
}
60+
61+
loop {}
62+
}
63+
64+
#[cfg(test)]
65+
#[panic_handler]
66+
fn panic(_info: &core::panic::PanicInfo) -> ! {
67+
unsafe {
68+
exit_qemu(ExitCode::Failed);
69+
}
70+
loop {}
71+
}

0 commit comments

Comments
 (0)