Skip to content

Commit

Permalink
add integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
AtsukiTak committed Jul 16, 2019
1 parent 944771c commit aed874c
Show file tree
Hide file tree
Showing 9 changed files with 346 additions and 24 deletions.
105 changes: 96 additions & 9 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,13 @@ bootloader = "~0.6"
volatile = "~0.2"
lazy_static = { version = "~1.0", features = ["spin_no_std"] }
spin = "~0.4"
x86_64 = "~0.7"
uart_16550 = "~0.2"

[package.metadata.bootimage]
test-args = ["-device", "isa-debug-exit,iobase=0xf4,iosize=0x04", "-serial", "stdio", "-display", "none"]
test-success-exit-code = 33

[[test]]
name = "panic_handler"
harness = false
25 changes: 25 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#![no_std]
#![cfg_attr(test, no_main)]
#![feature(custom_test_frameworks)]
#![test_runner(test_utils::test_runner)]
#![reexport_test_harness_main = "test_main"]

pub mod vga;
pub mod test_utils;

#[cfg(test)]
use core::panic::PanicInfo;

/// 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_utils::test_panic_handler(info)
}
21 changes: 6 additions & 15 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
#![no_std]
#![no_main]
#![feature(custom_test_frameworks)]
#![test_runner(crate::test_runner)]
#![test_runner(atomix::test_utils::test_runner)]
#![reexport_test_harness_main = "test_main"]

mod vga;

use core::panic::PanicInfo;
use atomix::println;

#[no_mangle]
pub extern "C" fn _start() -> ! {
Expand All @@ -18,23 +17,15 @@ pub extern "C" fn _start() -> ! {
loop {}
}

#[test_case]
fn trivial_assertion() {
print!("trivial assertion... ");
assert_eq!(1, 1);
println!("[ok]");
}

#[cfg(not(test))]
#[panic_handler]
fn panic(info: &PanicInfo) -> ! {
println!("{}", info);
loop {}
}

#[cfg(test)]
fn test_runner(tests: &[&dyn Fn()]) {
println!("Running {} tests", tests.len());
for test in tests {
test();
}
#[panic_handler]
fn panic(info: &PanicInfo) -> ! {
atomix::test_utils::test_panic_handler(info)
}
38 changes: 38 additions & 0 deletions src/test_utils.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#[macro_use]
pub mod serial;

use core::panic::PanicInfo;

pub fn test_runner(tests: &[&dyn Fn()]) {
serial_println!("Running {} tests", tests.len());

for test in tests {
test();
}

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);
}
}
37 changes: 37 additions & 0 deletions src/test_utils/serial.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
use lazy_static::lazy_static;
use spin::Mutex;
use uart_16550::SerialPort;

lazy_static! {
pub static ref SERIAL1: Mutex<SerialPort> = {
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::test_utils::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)*));
}
Loading

0 comments on commit aed874c

Please sign in to comment.