From 88ad0c50c35a0a9d082ab760379664ef9d80df28 Mon Sep 17 00:00:00 2001 From: Juozas Vainauskas <71255955+JuozasVainauskas@users.noreply.github.com> Date: Sat, 11 Nov 2023 16:12:57 +0200 Subject: [PATCH] Add tests (#4) --- .cargo/config.toml | 3 ++ Cargo.lock | 57 ++++++++++++++++++++++++++++++++++- Cargo.toml | 12 +++++++- src/lib.rs | 70 +++++++++++++++++++++++++++++++++++++++++++ src/main.rs | 28 +++++++++++++++-- src/serial.rs | 34 +++++++++++++++++++++ src/vga_buffer.rs | 7 +++++ tests/boot.rs | 25 ++++++++++++++++ tests/should_panic.rs | 25 ++++++++++++++++ 9 files changed, 256 insertions(+), 5 deletions(-) create mode 100644 src/lib.rs create mode 100644 src/serial.rs create mode 100644 tests/boot.rs create mode 100644 tests/should_panic.rs diff --git a/.cargo/config.toml b/.cargo/config.toml index 9724387..bb8c111 100644 --- a/.cargo/config.toml +++ b/.cargo/config.toml @@ -6,3 +6,6 @@ target = "x86_64-os.json" build-std = ["core", "compiler_builtins"] # Enable memory related functions from `compiler_builtins` crate build-std-features = ["compiler-builtins-mem"] + +[target.'cfg(target_os = "none")'] +runner = "bootimage runner" diff --git a/Cargo.lock b/Cargo.lock index 7f8879a..cd70fe0 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,6 +2,24 @@ # It is not intended for manual editing. version = 3 +[[package]] +name = "bit_field" +version = "0.10.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dc827186963e592360843fb5ba4b973e145841266c1357f7180c43526f2e5b61" + +[[package]] +name = "bitflags" +version = "1.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" + +[[package]] +name = "bitflags" +version = "2.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "327762f6e5a765692301e5bb513e0d9fef63be86bbc14528052b1cd3e6f03e07" + [[package]] name = "bootloader" version = "0.9.23" @@ -24,17 +42,54 @@ dependencies = [ "bootloader", "lazy_static", "spin", - "volatile", + "uart_16550", + "volatile 0.2.7", + "x86_64", ] +[[package]] +name = "rustversion" +version = "1.0.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7ffc183a10b4478d04cbbbfc96d0873219d962dd5accaff2ffbd4ceb7df837f4" + [[package]] name = "spin" version = "0.5.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6e63cff320ae2c57904679ba7cb63280a3dc4613885beafb148ee7bf9aa9042d" +[[package]] +name = "uart_16550" +version = "0.2.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "614ff2a87880d4bd4374722268598a970bbad05ced8bf630439417347254ab2e" +dependencies = [ + "bitflags 1.3.2", + "rustversion", + "x86_64", +] + [[package]] name = "volatile" version = "0.2.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f6b06ad3ed06fef1713569d547cdbdb439eafed76341820fb0e0344f29a41945" + +[[package]] +name = "volatile" +version = "0.4.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "442887c63f2c839b346c192d047a7c87e73d0689c9157b00b53dcc27dd5ea793" + +[[package]] +name = "x86_64" +version = "0.14.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3b835097a84e4457323331ec5d6eb23d096066cbfb215d54096dcb4b2e85f500" +dependencies = [ + "bit_field", + "bitflags 2.4.1", + "rustversion", + "volatile 0.4.6", +] diff --git a/Cargo.toml b/Cargo.toml index 3de5301..4c380c8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,7 +9,17 @@ edition = "2021" bootloader = "0.9.23" volatile = "0.2.6" spin = "0.5.2" +x86_64 = "0.14.2" +uart_16550 = "0.2.0" [dependencies.lazy_static] version = "1.0" -features = ["spin_no_std"] \ No newline at end of file +features = ["spin_no_std"] + +[package.metadata.bootimage] +test-args = ["-device", "isa-debug-exit,iobase=0xf4,iosize=0x04", "-serial", "stdio", "-display", "none"] +test-success-exit-code = 33 # (0x10 << 1) | 1 + +[[test]] +name = "should_panic" +harness = false \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..052452f --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,70 @@ +#![no_std] +#![cfg_attr(test, no_main)] +#![feature(custom_test_frameworks)] +#![test_runner(crate::test_runner)] +#![reexport_test_harness_main = "test_main"] + +use core::panic::PanicInfo; + +pub mod serial; +pub mod vga_buffer; + +pub trait Testable { + fn run(&self) -> (); +} + +impl Testable for T +where + T: Fn(), +{ + fn run(&self) { + serial_print!("{}...\t", core::any::type_name::()); + self(); + serial_println!("[ok]"); + } +} + +pub fn test_runner(tests: &[&dyn Testable]) { + serial_println!("Running {} tests", tests.len()); + for test in tests { + test.run(); + } + exit_qemu(QemuExitCode::Success); +} + +pub fn test_panic_handler(info: &PanicInfo) -> ! { + serial_println!("[failed]\n"); + serial_println!("Error: {}\n", info); + exit_qemu(QemuExitCode::Failed); + loop {} +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +#[repr(u32)] +pub enum QemuExitCode { + Success = 0x10, + Failed = 0x11, +} + +pub fn exit_qemu(exit_code: QemuExitCode) { + use x86_64::instructions::port::Port; + + unsafe { + let mut port = Port::new(0xf4); + port.write(exit_code as u32); + } +} + +/// Entry point for `cargo xtest` +#[cfg(test)] +#[no_mangle] +pub extern "C" fn _start() -> ! { + test_main(); + loop {} +} + +#[cfg(test)] +#[panic_handler] +fn panic(info: &PanicInfo) -> ! { + test_panic_handler(info) +} diff --git a/src/main.rs b/src/main.rs index eebe12d..f7996e4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,9 +3,14 @@ // disable all Rust-level entry points #![no_main] -use core::panic::PanicInfo; -mod vga_buffer; +// Tests related features +#![feature(custom_test_frameworks)] +#![test_runner(os::test_runner)] +#![reexport_test_harness_main = "test_main"] + +use core::panic::PanicInfo; +use os::println; #[no_mangle] pub extern "C" fn _start() -> ! { @@ -13,12 +18,29 @@ pub extern "C" fn _start() -> ! { // named `_start` by default println!("System booted successfully"); + + + #[cfg(test)] + test_main(); + loop {} } -// This function is called on panic. +// This function is called on panic in non-test mode. +#[cfg(not(test))] // new attribute #[panic_handler] fn panic(info: &PanicInfo) -> ! { println!("{}", info); loop {} } + +#[cfg(test)] +#[panic_handler] +fn panic(info: &PanicInfo) -> ! { + os::test_panic_handler(info) +} + +#[test_case] +fn trivial_assertion() { + assert_eq!(1, 1); +} diff --git a/src/serial.rs b/src/serial.rs new file mode 100644 index 0000000..e8073d9 --- /dev/null +++ b/src/serial.rs @@ -0,0 +1,34 @@ +use uart_16550::SerialPort; +use spin::Mutex; +use lazy_static::lazy_static; + +lazy_static! { + pub static ref SERIAL1: Mutex = { + let mut serial_port = unsafe { SerialPort::new(0x3F8) }; + serial_port.init(); + Mutex::new(serial_port) + }; +} + +#[doc(hidden)] +pub fn _print(args: ::core::fmt::Arguments) { + use core::fmt::Write; + SERIAL1.lock().write_fmt(args).expect("Printing to serial failed"); +} + +/// Prints to the host through the serial interface. +#[macro_export] +macro_rules! serial_print { + ($($arg:tt)*) => { + $crate::serial::_print(format_args!($($arg)*)); + }; +} + +/// Prints to the host through the serial interface, appending a newline. +#[macro_export] +macro_rules! serial_println { + () => ($crate::serial_print!("\n")); + ($fmt:expr) => ($crate::serial_print!(concat!($fmt, "\n"))); + ($fmt:expr, $($arg:tt)*) => ($crate::serial_print!( + concat!($fmt, "\n"), $($arg)*)); +} diff --git a/src/vga_buffer.rs b/src/vga_buffer.rs index cf11ae0..e1340cf 100644 --- a/src/vga_buffer.rs +++ b/src/vga_buffer.rs @@ -146,3 +146,10 @@ pub fn _print(args: fmt::Arguments) { use core::fmt::Write; WRITER.lock().write_fmt(args).unwrap(); } + +#[test_case] +fn test_println_many() { + for _ in 0..200 { + println!("Test"); + } +} diff --git a/tests/boot.rs b/tests/boot.rs new file mode 100644 index 0000000..8be53e8 --- /dev/null +++ b/tests/boot.rs @@ -0,0 +1,25 @@ +#![no_std] +#![no_main] +#![feature(custom_test_frameworks)] +#![test_runner(os::test_runner)] +#![reexport_test_harness_main = "test_main"] + +use os::println; +use core::panic::PanicInfo; + +#[no_mangle] // don't mangle the name of this function +pub extern "C" fn _start() -> ! { + test_main(); + + loop {} +} + +#[panic_handler] +fn panic(info: &PanicInfo) -> ! { + os::test_panic_handler(info) +} + +#[test_case] +fn test_println() { + println!("test_println output"); +} diff --git a/tests/should_panic.rs b/tests/should_panic.rs new file mode 100644 index 0000000..52d546d --- /dev/null +++ b/tests/should_panic.rs @@ -0,0 +1,25 @@ +#![no_std] +#![no_main] + +use os::{exit_qemu, serial_print, serial_println, QemuExitCode}; +use core::panic::PanicInfo; + +#[no_mangle] +pub extern "C" fn _start() -> ! { + should_fail(); + serial_println!("[test did not panic]"); + exit_qemu(QemuExitCode::Failed); + loop {} +} + +fn should_fail() { + serial_print!("should_panic::should_fail...\t"); + assert_eq!(0, 1); +} + +#[panic_handler] +fn panic(_info: &PanicInfo) -> ! { + serial_println!("[ok]"); + exit_qemu(QemuExitCode::Success); + loop {} +}