Skip to content

Commit 778e393

Browse files
committed
Add a runner-test kernel for testing new runner functionality
1 parent 3788a77 commit 778e393

File tree

10 files changed

+186
-0
lines changed

10 files changed

+186
-0
lines changed

azure-pipelines.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,10 @@ jobs:
199199
workingDirectory: example-kernels/runner
200200
displayName: 'Run `cargo xrun` for "runner" kernel'
201201
202+
- script: cargo xtest
203+
workingDirectory: example-kernels/runner-test
204+
displayName: 'Run `cargo xtest` for "runner-test" kernel'
205+
202206
- job: formatting
203207
displayName: Check Formatting
204208

example-kernels/Cargo.lock

Lines changed: 8 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
@@ -4,6 +4,7 @@ members = [
44
"default-target-bootimage",
55
"default-target-cargo",
66
"runner",
7+
"runner-test",
78
"testing-qemu-exit-code",
89
"testing-serial-result",
910
]
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: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
[package]
2+
name = "runner-test"
3+
version = "0.1.0"
4+
authors = ["Philipp Oppermann <[email protected]>"]
5+
edition = "2018"
6+
7+
[[test]]
8+
name = "no-harness"
9+
harness = false
10+
11+
[dependencies]
12+
bootloader = "0.5.1"
13+
x86_64 = "0.5.3"
14+
15+
[package.metadata.bootimage]
16+
test-success-exit-code = 33 # (0x10 << 1) | 1
17+
test-args = ["-device", "isa-debug-exit,iobase=0xf4,iosize=0x04", "-display", "none"]
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
#![no_std]
2+
#![cfg_attr(test, no_main)]
3+
4+
#![feature(custom_test_frameworks)]
5+
#![test_runner(crate::test_runner)]
6+
#![reexport_test_harness_main = "test_main"]
7+
8+
pub fn test_runner(tests: &[&dyn Fn()]) {
9+
for test in tests.iter() {
10+
test();
11+
}
12+
13+
unsafe { exit_qemu(ExitCode::Success); }
14+
}
15+
16+
#[test_case]
17+
fn test1() {
18+
assert_eq!(0, 0);
19+
}
20+
21+
#[test_case]
22+
fn test2() {
23+
assert_eq!(1, 1);
24+
}
25+
26+
pub enum ExitCode {
27+
Success,
28+
Failed
29+
}
30+
31+
impl ExitCode {
32+
fn code(&self) -> u32 {
33+
match self {
34+
ExitCode::Success => 0x10,
35+
ExitCode::Failed => 0x11,
36+
}
37+
}
38+
}
39+
40+
/// exit QEMU (see https://os.phil-opp.com/integration-tests/#shutting-down-qemu)
41+
pub unsafe fn exit_qemu(exit_code: ExitCode) {
42+
use x86_64::instructions::port::Port;
43+
44+
let mut port = Port::<u32>::new(0xf4);
45+
port.write(exit_code.code());
46+
}
47+
48+
#[cfg(test)]
49+
#[no_mangle]
50+
pub extern "C" fn _start() -> ! {
51+
test_main();
52+
53+
unsafe { exit_qemu(ExitCode::Failed); }
54+
55+
loop {}
56+
}
57+
58+
#[cfg(test)]
59+
#[panic_handler]
60+
fn panic(_info: &core::panic::PanicInfo) -> ! {
61+
unsafe { exit_qemu(ExitCode::Failed); }
62+
loop {}
63+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#![no_std]
2+
#![no_main]
3+
4+
#![feature(custom_test_frameworks)]
5+
#![test_runner(runner_test::test_runner)]
6+
#![reexport_test_harness_main = "test_main"]
7+
8+
use core::panic::PanicInfo;
9+
use runner_test::{exit_qemu, ExitCode};
10+
11+
#[no_mangle]
12+
pub extern "C" fn _start() -> ! {
13+
#[cfg(test)]
14+
test_main();
15+
16+
unsafe { exit_qemu(ExitCode::Failed); }
17+
18+
loop {}
19+
}
20+
21+
#[test_case]
22+
fn test1() {
23+
assert_eq!(0, 0);
24+
}
25+
26+
#[panic_handler]
27+
fn panic(_info: &PanicInfo) -> ! {
28+
unsafe { exit_qemu(ExitCode::Failed); }
29+
loop {}
30+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#![no_std]
2+
#![no_main]
3+
4+
use runner_test::{exit_qemu, ExitCode};
5+
use core::panic::PanicInfo;
6+
7+
#[no_mangle]
8+
pub extern "C" fn _start() -> ! {
9+
unsafe {
10+
exit_qemu(ExitCode::Success);
11+
}
12+
loop {}
13+
}
14+
15+
#[panic_handler]
16+
fn panic(_info: &PanicInfo) -> ! {
17+
unsafe {
18+
exit_qemu(ExitCode::Failed);
19+
}
20+
loop {}
21+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#![no_std] // don't link the Rust standard library
2+
#![no_main] // disable all Rust-level entry points
3+
4+
#![feature(custom_test_frameworks)]
5+
#![test_runner(crate::test_runner)]
6+
#![reexport_test_harness_main = "test_main"]
7+
8+
use core::panic::PanicInfo;
9+
use runner_test::{exit_qemu, ExitCode};
10+
11+
#[no_mangle] // don't mangle the name of this function
12+
pub extern "C" fn _start() -> ! {
13+
test_main();
14+
15+
unsafe { exit_qemu(ExitCode::Failed); }
16+
17+
loop {}
18+
}
19+
20+
pub fn test_runner(tests: &[&dyn Fn()]) {
21+
for test in tests.iter() {
22+
test();
23+
}
24+
}
25+
26+
#[test_case]
27+
fn should_panic() {
28+
assert_eq!(1, 2);
29+
}
30+
31+
#[panic_handler]
32+
fn panic(_info: &PanicInfo) -> ! {
33+
unsafe { exit_qemu(ExitCode::Success); }
34+
loop {}
35+
}

0 commit comments

Comments
 (0)