Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 4d4b584

Browse files
committedApr 9, 2022
Update test framework to rewrite
1 parent c7a5b33 commit 4d4b584

36 files changed

+186
-319
lines changed
 

‎.cargo/config.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
[target.x86_64-unknown-uefi]
2-
runner = "cargo run -p runner"
1+
[unstable]
2+
bindeps = true

‎Cargo.lock

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

‎Cargo.toml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
cargo-features = ["profile-rustflags"]
2+
13
[package]
24
name = "bootloader"
35
version = "0.11.0-alpha.0"
@@ -31,6 +33,13 @@ raw-cpuid = { version = "10.2.0", optional = true }
3133
rand = { version = "0.8.4", optional = true, default-features = false }
3234
rand_chacha = { version = "0.3.1", optional = true, default-features = false }
3335

36+
[dev-dependencies]
37+
bootloader_test_runner = { path = "tests/runner" }
38+
test_kernel_default_settings = { path = "tests/test_kernels/default_settings", artifact = "bin", target = "x86_64-unknown-none" }
39+
test_kernel_higher_half = { path = "tests/test_kernels/higher_half", artifact = "bin", target = "x86_64-unknown-none" }
40+
test_kernel_map_phys_mem = { path = "tests/test_kernels/map_phys_mem", artifact = "bin", target = "x86_64-unknown-none" }
41+
test_kernel_pie = { path = "tests/test_kernels/pie", artifact = "bin", target = "x86_64-unknown-none" }
42+
3443
# [dependencies.bootloader-x86_64-uefi]
3544
# version = "0.1.0"
3645
# path = "uefi"
@@ -55,6 +64,15 @@ codegen-units = 1
5564
debug = false
5665
overflow-checks = false
5766

67+
[profile.test.package.test_kernel_higher_half]
68+
rustflags = [
69+
"-C",
70+
"link-args=--image-base 0xFFFF800000000000",
71+
"-C",
72+
"relocation-model=static", # pic in higher half not supported yet
73+
"-C",
74+
"code-model=large",
75+
]
5876

5977
[package.metadata.docs.rs]
6078
default-target = "x86_64-unknown-linux-gnu"

‎rust-toolchain.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
[toolchain]
22
channel = "nightly"
33
components = ["rustfmt", "clippy", "rust-src", "llvm-tools-preview"]
4+
targets = ["x86_64-unknown-none"]

‎src/lib.rs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -67,14 +67,13 @@ for all possible configuration options.
6767

6868
#![warn(missing_docs)]
6969

70+
use anyhow::Context;
7071
use std::{
7172
fs::{self, File},
7273
io::{self, Seek},
7374
path::Path,
7475
};
7576

76-
use anyhow::Context;
77-
7877
pub fn create_uefi_disk_image(
7978
kernel_binary: &Path,
8079
out_fat_path: &Path,
@@ -200,8 +199,3 @@ fn create_gpt_disk(fat_image: &Path, out_gpt_path: &Path) {
200199
disk.seek(io::SeekFrom::Start(start_offset)).unwrap();
201200
io::copy(&mut File::open(&fat_image).unwrap(), &mut disk).unwrap();
202201
}
203-
204-
// Provides a function to turn a bootloader executable into a disk image.
205-
//
206-
// Used by the `builder` binary. Only available when the `builder` feature is enabled.
207-
// pub mod disk_image;

‎tests/default_settings.rs

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,22 @@
1-
use std::process::Command;
1+
use bootloader_test_runner::run_test_kernel;
22

33
#[test]
44
fn basic_boot() {
5-
run_test_binary("basic_boot");
5+
run_test_kernel(env!(
6+
"CARGO_BIN_FILE_TEST_KERNEL_DEFAULT_SETTINGS_basic_boot"
7+
));
68
}
79

810
#[test]
911
fn should_panic() {
10-
run_test_binary("should_panic");
12+
run_test_kernel(env!(
13+
"CARGO_BIN_FILE_TEST_KERNEL_DEFAULT_SETTINGS_should_panic"
14+
));
1115
}
1216

1317
#[test]
1418
fn check_boot_info() {
15-
run_test_binary("check_boot_info");
16-
}
17-
18-
fn run_test_binary(bin_name: &str) {
19-
let mut cmd = Command::new(env!("CARGO"));
20-
cmd.current_dir("tests/test_kernels/default_settings");
21-
cmd.arg("run");
22-
cmd.arg("--bin").arg(bin_name);
23-
cmd.arg("--target").arg("x86_64-default_settings.json");
24-
cmd.arg("-Zbuild-std=core");
25-
cmd.arg("-Zbuild-std-features=compiler-builtins-mem");
26-
assert!(cmd.status().unwrap().success());
19+
run_test_kernel(env!(
20+
"CARGO_BIN_FILE_TEST_KERNEL_DEFAULT_SETTINGS_check_boot_info"
21+
));
2722
}

‎tests/higher_half.rs

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,25 @@
1-
use std::process::Command;
1+
use bootloader_test_runner::run_test_kernel;
22

33
#[test]
44
fn basic_boot() {
5-
run_test_binary("basic_boot");
5+
run_test_kernel(env!("CARGO_BIN_FILE_TEST_KERNEL_HIGHER_HALF_basic_boot"));
66
}
77

88
#[test]
99
fn should_panic() {
10-
run_test_binary("should_panic");
10+
run_test_kernel(env!("CARGO_BIN_FILE_TEST_KERNEL_HIGHER_HALF_should_panic"));
1111
}
1212

1313
#[test]
1414
fn check_boot_info() {
15-
run_test_binary("check_boot_info");
15+
run_test_kernel(env!(
16+
"CARGO_BIN_FILE_TEST_KERNEL_HIGHER_HALF_check_boot_info"
17+
));
1618
}
1719

1820
#[test]
1921
fn verify_higher_half() {
20-
run_test_binary("verify_higher_half");
21-
}
22-
23-
fn run_test_binary(bin_name: &str) {
24-
let mut cmd = Command::new(env!("CARGO"));
25-
cmd.current_dir("tests/test_kernels/higher_half");
26-
cmd.arg("run");
27-
cmd.arg("--bin").arg(bin_name);
28-
cmd.arg("--target").arg("x86_64-higher_half.json");
29-
cmd.arg("-Zbuild-std=core");
30-
cmd.arg("-Zbuild-std-features=compiler-builtins-mem");
31-
assert!(cmd.status().unwrap().success());
22+
run_test_kernel(env!(
23+
"CARGO_BIN_FILE_TEST_KERNEL_HIGHER_HALF_verify_higher_half"
24+
));
3225
}

‎tests/map_phys_mem.rs

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,15 @@
1-
use std::process::Command;
1+
use bootloader_test_runner::run_test_kernel;
22

33
#[test]
44
fn check_boot_info() {
5-
run_test_binary("check_boot_info");
5+
run_test_kernel(env!(
6+
"CARGO_BIN_FILE_TEST_KERNEL_MAP_PHYS_MEM_check_boot_info"
7+
));
68
}
79

810
#[test]
911
fn access_phys_mem() {
10-
run_test_binary("access_phys_mem");
11-
}
12-
13-
fn run_test_binary(bin_name: &str) {
14-
let mut cmd = Command::new(env!("CARGO"));
15-
cmd.current_dir("tests/test_kernels/map_phys_mem");
16-
cmd.arg("run");
17-
cmd.arg("--bin").arg(bin_name);
18-
cmd.arg("--target").arg("x86_64-map_phys_mem.json");
19-
cmd.arg("-Zbuild-std=core");
20-
cmd.arg("-Zbuild-std-features=compiler-builtins-mem");
21-
assert!(cmd.status().unwrap().success());
12+
run_test_kernel(env!(
13+
"CARGO_BIN_FILE_TEST_KERNEL_MAP_PHYS_MEM_access_phys_mem"
14+
));
2215
}

‎tests/pie.rs

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,21 @@
1-
use std::process::Command;
1+
use bootloader_test_runner::run_test_kernel;
22

33
#[test]
44
fn basic_boot() {
5-
run_test_binary("basic_boot");
5+
run_test_kernel(env!("CARGO_BIN_FILE_TEST_KERNEL_PIE_basic_boot"));
66
}
77

88
#[test]
99
fn should_panic() {
10-
run_test_binary("should_panic");
10+
run_test_kernel(env!("CARGO_BIN_FILE_TEST_KERNEL_PIE_should_panic"));
1111
}
1212

1313
#[test]
1414
fn check_boot_info() {
15-
run_test_binary("check_boot_info");
15+
run_test_kernel(env!("CARGO_BIN_FILE_TEST_KERNEL_PIE_check_boot_info"));
1616
}
1717

1818
#[test]
1919
fn global_variable() {
20-
run_test_binary("global_variable");
21-
}
22-
23-
fn run_test_binary(bin_name: &str) {
24-
let mut cmd = Command::new(env!("CARGO"));
25-
cmd.current_dir("tests/test_kernels/pie");
26-
cmd.arg("run");
27-
cmd.arg("--bin").arg(bin_name);
28-
cmd.arg("--target").arg("x86_64-pie.json");
29-
cmd.arg("-Zbuild-std=core");
30-
cmd.arg("-Zbuild-std-features=compiler-builtins-mem");
31-
assert!(cmd.status().unwrap().success());
20+
run_test_kernel(env!("CARGO_BIN_FILE_TEST_KERNEL_PIE_global_variable"));
3221
}

‎tests/runner/Cargo.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
[package]
2-
name = "runner"
2+
name = "bootloader_test_runner"
33
version = "0.1.0"
44
authors = ["Philipp Oppermann <dev@phil-opp.com>"]
55
edition = "2018"
66

77
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
88

99
[dependencies]
10-
bootloader-locator = "0.0.4"
11-
locate-cargo-manifest = "0.2.1"
10+
bootloader = { path = "../.." }
11+
strip-ansi-escapes = "0.1.1"

‎tests/runner/src/lib.rs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
use std::{io::Write, path::Path, process::Command};
2+
3+
const QEMU_ARGS: &[&str] = &[
4+
"-device",
5+
"isa-debug-exit,iobase=0xf4,iosize=0x04",
6+
"-serial",
7+
"stdio",
8+
"-display",
9+
"none",
10+
"--no-reboot",
11+
];
12+
13+
pub fn run_test_kernel(kernel_binary_path: &str) {
14+
let kernel_path = Path::new(kernel_binary_path);
15+
let out_fat_path = kernel_path.with_extension("fat");
16+
let out_gpt_path = kernel_path.with_extension("gpt");
17+
bootloader::create_uefi_disk_image(kernel_path, &out_fat_path, &out_gpt_path).unwrap();
18+
19+
let mut run_cmd = Command::new("qemu-system-x86_64");
20+
run_cmd
21+
.arg("-drive")
22+
.arg(format!("format=raw,file={}", out_gpt_path.display()));
23+
run_cmd.args(QEMU_ARGS);
24+
run_cmd.args(std::env::args().skip(2).collect::<Vec<_>>());
25+
run_cmd.arg("-bios").arg("OVMF-pure-efi.fd");
26+
27+
let child_output = run_cmd.output().unwrap();
28+
strip_ansi_escapes::Writer::new(std::io::stderr())
29+
.write_all(&child_output.stderr)
30+
.unwrap();
31+
strip_ansi_escapes::Writer::new(std::io::stderr())
32+
.write_all(&child_output.stdout)
33+
.unwrap();
34+
35+
match child_output.status.code() {
36+
Some(33) => {} // success
37+
Some(35) => panic!("Test failed"), // success
38+
other => panic!("Test failed with unexpected exit code `{:?}`", other),
39+
}
40+
}

‎tests/runner/src/main.rs

Lines changed: 0 additions & 81 deletions
This file was deleted.

‎tests/test_kernels/default_settings/.cargo/config.toml

Lines changed: 0 additions & 10 deletions
This file was deleted.

‎tests/test_kernels/default_settings/.gitignore

Lines changed: 0 additions & 1 deletion
This file was deleted.

‎tests/test_kernels/default_settings/src/bin/check_boot_info.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ fn kernel_main(boot_info: &'static mut BootInfo) -> ! {
5050
// check rsdp_addr
5151
let rsdp = boot_info.rsdp_addr.into_option().unwrap();
5252
assert!(rsdp > 0x000E0000);
53-
assert!(rsdp < 0x000FFFFF);
5453

5554
// the test kernel has no TLS template
5655
assert_eq!(boot_info.tls_template.into_option(), None);

‎tests/test_kernels/default_settings/x86_64-default_settings.json

Lines changed: 0 additions & 15 deletions
This file was deleted.

‎tests/test_kernels/higher_half/.cargo/config.toml

Lines changed: 0 additions & 10 deletions
This file was deleted.

‎tests/test_kernels/higher_half/.gitignore

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
cargo-features = ["profile-rustflags"]
2+
13
[package]
24
name = "test_kernel_higher_half"
35
version = "0.1.0"
@@ -6,5 +8,10 @@ edition = "2018"
68

79
[dependencies]
810
bootloader_api = { path = "../../../api" }
9-
x86_64 = { version = "0.14.7", default-features = false, features = ["instructions", "inline_asm"] }
11+
x86_64 = { version = "0.14.7", default-features = false, features = [
12+
"instructions",
13+
"inline_asm",
14+
] }
1015
uart_16550 = "0.2.10"
16+
17+
# set to higher half through profile.test.rustflags key in top-level Cargo.toml

‎tests/test_kernels/higher_half/src/bin/check_boot_info.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ fn kernel_main(boot_info: &'static mut BootInfo) -> ! {
5050
// check rsdp_addr
5151
let rsdp = boot_info.rsdp_addr.into_option().unwrap();
5252
assert!(rsdp > 0x000E0000);
53-
assert!(rsdp < 0x000FFFFF);
5453

5554
// the test kernel has no TLS template
5655
assert_eq!(boot_info.tls_template.into_option(), None);

‎tests/test_kernels/higher_half/x86_64-higher_half.json

Lines changed: 0 additions & 18 deletions
This file was deleted.

‎tests/test_kernels/map_phys_mem/.cargo/config.toml

Lines changed: 0 additions & 10 deletions
This file was deleted.

‎tests/test_kernels/map_phys_mem/.gitignore

Lines changed: 0 additions & 1 deletion
This file was deleted.

‎tests/test_kernels/map_phys_mem/Cargo.toml

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,8 @@ edition = "2018"
66

77
[target.'cfg(target_arch = "x86_64")'.dependencies]
88
bootloader_api = { path = "../../../api" }
9-
x86_64 = { version = "0.14.7", default-features = false, features = ["instructions", "inline_asm"] }
9+
x86_64 = { version = "0.14.7", default-features = false, features = [
10+
"instructions",
11+
"inline_asm",
12+
] }
1013
uart_16550 = "0.2.10"
11-
12-
[package.metadata.bootloader]
13-
map-physical-memory = true
14-
physical-memory-offset = 0x0000_4000_0000_0000

‎tests/test_kernels/map_phys_mem/src/bin/access_phys_mem.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33

44
use bootloader_api::{entry_point, BootInfo};
55
use core::panic::PanicInfo;
6-
use test_kernel_map_phys_mem::{exit_qemu, serial, QemuExitCode};
6+
use test_kernel_map_phys_mem::{exit_qemu, serial, QemuExitCode, BOOTLOADER_CONFIG};
77

8-
entry_point!(kernel_main);
8+
entry_point!(kernel_main, config = &BOOTLOADER_CONFIG);
99

1010
fn kernel_main(boot_info: &'static mut BootInfo) -> ! {
1111
let phys_mem_offset = boot_info.physical_memory_offset.into_option().unwrap();

‎tests/test_kernels/map_phys_mem/src/bin/check_boot_info.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33

44
use bootloader_api::{entry_point, info::PixelFormat, BootInfo};
55
use core::panic::PanicInfo;
6-
use test_kernel_map_phys_mem::{exit_qemu, serial, QemuExitCode};
6+
use test_kernel_map_phys_mem::{exit_qemu, serial, QemuExitCode, BOOTLOADER_CONFIG};
77

8-
entry_point!(kernel_main);
8+
entry_point!(kernel_main, config = &BOOTLOADER_CONFIG);
99

1010
fn kernel_main(boot_info: &'static mut BootInfo) -> ! {
1111
// check memory regions
@@ -53,7 +53,6 @@ fn kernel_main(boot_info: &'static mut BootInfo) -> ! {
5353
// check rsdp_addr
5454
let rsdp = boot_info.rsdp_addr.into_option().unwrap();
5555
assert!(rsdp > 0x000E0000);
56-
assert!(rsdp < 0x000FFFFF);
5756

5857
// the test kernel has no TLS template
5958
assert_eq!(boot_info.tls_template.into_option(), None);

‎tests/test_kernels/map_phys_mem/src/lib.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
#![no_std]
22

3+
use bootloader_api::{config::Mapping, BootloaderConfig};
4+
5+
pub const BOOTLOADER_CONFIG: BootloaderConfig = {
6+
let mut config = BootloaderConfig::new_default();
7+
config.mappings.physical_memory = Some(Mapping::FixedAddress(0x0000_4000_0000_0000));
8+
config
9+
};
10+
311
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
412
#[repr(u32)]
513
pub enum QemuExitCode {

‎tests/test_kernels/map_phys_mem/x86_64-map_phys_mem.json

Lines changed: 0 additions & 15 deletions
This file was deleted.

‎tests/test_kernels/pie/.cargo/config.toml

Lines changed: 0 additions & 10 deletions
This file was deleted.

‎tests/test_kernels/pie/.gitignore

Lines changed: 0 additions & 1 deletion
This file was deleted.

‎tests/test_kernels/pie/Cargo.toml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ authors = ["Tom Dohrmann <erbse.13@gmx.de>"]
55
edition = "2018"
66

77
[dependencies]
8-
bootloader = { path = "../../.." }
9-
x86_64 = { version = "0.14.7", default-features = false, features = ["instructions", "inline_asm"] }
8+
bootloader_api = { path = "../../../api" }
9+
x86_64 = { version = "0.14.7", default-features = false, features = [
10+
"instructions",
11+
"inline_asm",
12+
] }
1013
uart_16550 = "0.2.10"
11-
12-
[package.metadata.bootloader]
13-
aslr = true

‎tests/test_kernels/pie/src/bin/basic_boot.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#![no_std] // don't link the Rust standard library
22
#![no_main] // disable all Rust-level entry points
33

4-
use bootloader::{entry_point, BootInfo};
4+
use bootloader_api::{entry_point, BootInfo};
55
use core::panic::PanicInfo;
66
use test_kernel_pie::{exit_qemu, QemuExitCode};
77

‎tests/test_kernels/pie/src/bin/check_boot_info.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#![no_std] // don't link the Rust standard library
22
#![no_main] // disable all Rust-level entry points
33

4-
use bootloader::{boot_info::PixelFormat, entry_point, BootInfo};
4+
use bootloader_api::{entry_point, info::PixelFormat, BootInfo};
55
use core::panic::PanicInfo;
66
use test_kernel_pie::{exit_qemu, QemuExitCode};
77

@@ -35,7 +35,7 @@ fn kernel_main(boot_info: &'static mut BootInfo) -> ! {
3535
if ![640, 1024].contains(&framebuffer.info().stride) {
3636
panic!("unexpected stride `{}`", framebuffer.info().stride);
3737
}
38-
assert_eq!(framebuffer.info().pixel_format, PixelFormat::BGR);
38+
assert_eq!(framebuffer.info().pixel_format, PixelFormat::Bgr);
3939
assert_eq!(
4040
framebuffer.buffer().len(),
4141
framebuffer.info().stride
@@ -50,7 +50,6 @@ fn kernel_main(boot_info: &'static mut BootInfo) -> ! {
5050
// check rsdp_addr
5151
let rsdp = boot_info.rsdp_addr.into_option().unwrap();
5252
assert!(rsdp > 0x000E0000);
53-
assert!(rsdp < 0x000FFFFF);
5453

5554
// the test kernel has no TLS template
5655
assert_eq!(boot_info.tls_template.into_option(), None);

‎tests/test_kernels/pie/src/bin/global_variable.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#![no_std] // don't link the Rust standard library
22
#![no_main] // disable all Rust-level entry points
33

4-
use bootloader::{entry_point, BootInfo};
4+
use bootloader_api::{entry_point, BootInfo};
55
use core::{
66
panic::PanicInfo,
77
sync::atomic::{AtomicU64, Ordering},

‎tests/test_kernels/pie/src/bin/should_panic.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#![no_std] // don't link the Rust standard library
22
#![no_main] // disable all Rust-level entry points
33

4-
use bootloader::{entry_point, BootInfo};
4+
use bootloader_api::{entry_point, BootInfo};
55
use core::panic::PanicInfo;
66
use test_kernel_pie::{exit_qemu, QemuExitCode};
77

‎tests/test_kernels/pie/x86_64-pie.json

Lines changed: 0 additions & 16 deletions
This file was deleted.

0 commit comments

Comments
 (0)
Please sign in to comment.