From 88df078d5672f4f8bf7f0c6de8efb78c7fd2ef1d Mon Sep 17 00:00:00 2001 From: able Date: Fri, 5 Aug 2022 06:22:23 -0500 Subject: [PATCH] test framework layout --- ableos/Cargo.toml | 8 ++- ableos/assets/kernel.toml | 3 +- ableos/src/arch/x86_64/interrupts.rs | 8 ++- ableos/src/devices/pci/mod.rs | 19 ++++-- ableos/src/lib.rs | 20 +++++- ableos/src/logger.rs | 44 ++++++++----- ableos/src/panic.rs | 8 ++- ableos/src/scratchpad.rs | 12 +++- ableos/src/tests/mod.rs | 70 +++++++++++++++++++++ ableos/src/{tests.rs => tests/old_tests.rs} | 6 ++ repbuild/src/main.rs | 21 +++++-- 11 files changed, 183 insertions(+), 36 deletions(-) create mode 100644 ableos/src/tests/mod.rs rename ableos/src/{tests.rs => tests/old_tests.rs} (98%) diff --git a/ableos/Cargo.toml b/ableos/Cargo.toml index acbe6a3..62b2b1b 100644 --- a/ableos/Cargo.toml +++ b/ableos/Cargo.toml @@ -24,7 +24,9 @@ run-args = [ "pcspk", "-device", "VGA", - # "-device", "virtio-gpu-pci", + "-device", + "virtio-gpu-pci", + # "-machine", "pcspk-audiodev=0", @@ -49,7 +51,6 @@ test-args = [ lazy_static = { version = "1.4.0", features = ["spin_no_std"] } qrcode = { path = "../qrcode-rust" } bitflags = "1.2.1" -linked_list_allocator = "0.9.0" lliw = "0.2.0" spin = "0.9" pretty-hex = "0.2.1" @@ -63,6 +64,9 @@ versioning = { git = "https://git.ablecorp.us/able/aos_userland" } pc-keyboard = "0.5" # mini-backtrace = "0.1" +[dependencies.linked_list_allocator] +version = "0.9.0" +features = ["use_spin_nightly"] [dependencies.log] version = "0.4.17" default-features = false diff --git a/ableos/assets/kernel.toml b/ableos/assets/kernel.toml index d0df47e..af5569e 100644 --- a/ableos/assets/kernel.toml +++ b/ableos/assets/kernel.toml @@ -6,8 +6,7 @@ user_processes = ["shell"] enabled = true level = "Trace" log_to_serial = true -filter = ["ableos::ps2_mouse"] -# "ableos::vterm"] +filter = ["ableos::ps2_mouse", "ableos::vterm"] [tests] diff --git a/ableos/src/arch/x86_64/interrupts.rs b/ableos/src/arch/x86_64/interrupts.rs index 38aa65b..2f6cfcf 100644 --- a/ableos/src/arch/x86_64/interrupts.rs +++ b/ableos/src/arch/x86_64/interrupts.rs @@ -1,6 +1,12 @@ +/* +* Copyright (c) 2022, able +* +* SPDX-License-Identifier: MPL-2.0 +*/ + use core::panic::PanicInfo; -use crate::{arch::gdt, print, println, rhai_shell::KEYBUFF}; +use crate::{arch::gdt, println, rhai_shell::KEYBUFF}; use cpuio::outb; use pic8259::ChainedPics; use qrcode::QrCode; diff --git a/ableos/src/devices/pci/mod.rs b/ableos/src/devices/pci/mod.rs index 6d087b0..00d94f5 100644 --- a/ableos/src/devices/pci/mod.rs +++ b/ableos/src/devices/pci/mod.rs @@ -49,7 +49,7 @@ impl Display for PciDeviceInfo { let vendor_name = name_for_vendor_id(self.vendor_id); writeln!( f, - "Device {:X} | Bus {:X} | Vendor: {}", + "Device {} | Bus {:X} | Vendor: {}", self.device, self.bus, vendor_name )?; writeln!( @@ -88,11 +88,18 @@ impl Display for PciDeviceInfo { /// Converts a u16 vendor id into a human-readable name. pub fn name_for_vendor_id(vendor_id: u16) -> String { - match vendor_id { - 0x8086 => "Intel Corp. (0x8086)".into(), - 0x1234 => "QEMU (0x1234)".into(), - _ => format!("Unknown({:#06X})", vendor_id), - } + let mut ret = match vendor_id { + 0x1234 => "\0PINK\0QEMU (0x1234)".into(), + 0x1AF4 => "\0PINK\0VirtIO (0x1AF4)".into(), + + 0x5333 => "\0YELLOW\0S3 Incorporated (0x5333)".into(), + 0x8086 => "\0BLUE\0Intel Corp. (0x8086)".into(), + _ => format!("\0BROWN\0Unknown({:#06X})", vendor_id), + }; + + // trace!("{}", ret); + ret.push_str("\0RESET\0"); + ret } /// Brute force scans for devices 0-31 on buses 0-255. diff --git a/ableos/src/lib.rs b/ableos/src/lib.rs index 0c917e2..779fcb0 100644 --- a/ableos/src/lib.rs +++ b/ableos/src/lib.rs @@ -1,3 +1,9 @@ +/* + * Copyright (c) 2022, Able + * + * SPDX-License-Identifier: MPL-2.0 + */ + //! main library for the AbleOS kernel. //! exposing all the kernel functionality to the rest of the kernel. //! @@ -11,6 +17,9 @@ naked_functions, prelude_import, )] +#![feature(custom_test_frameworks)] +#![test_runner(crate::test_runner)] +#![reexport_test_harness_main = "test_main"] #[macro_use] pub extern crate log; @@ -74,8 +83,8 @@ pub mod ipc; pub mod panic; mod unicode_utils; pub mod vga_e; -// pub mod vgai; pub mod vterm; +// pub mod vgai; #[prelude_import] pub use prelude::rust_2021::*; @@ -94,3 +103,12 @@ pub use scratchpad::*; pub use utils::*; pub use virtio::*; pub use wasm::*; + +#[cfg(test)] +pub mod tests; + +#[cfg(test)] +pub use tests::test_kernel_main; + +#[cfg(test)] +use crate::tests::test_runner; diff --git a/ableos/src/logger.rs b/ableos/src/logger.rs index f00e425..883d3d4 100644 --- a/ableos/src/logger.rs +++ b/ableos/src/logger.rs @@ -1,3 +1,9 @@ +/* + * Copyright (c) 2022, Able + * + * SPDX-License-Identifier: MPL-2.0 + */ + use crate::{kmain::KERNEL_CONF, time::fetch_time}; use lliw::Fg; use log::{Level, LevelFilter, Metadata, Record, SetLoggerError}; @@ -18,22 +24,13 @@ impl log::Log for SimpleLogger { use Fg::*; let color = match record.level() { - log::Level::Error => (Fg::Red, "$RED$"), - log::Level::Warn => (Fg::LightYellow, "$LIGHTYELLOW$"), - log::Level::Info => (Fg::LightWhite, "$LIGHTGRAY$"), - log::Level::Debug => (Fg::Blue, "$BLUE$"), - log::Level::Trace => (Fg::Yellow, "$YELLOW$"), + log::Level::Error => (Fg::Red, "\0RED\0"), + log::Level::Warn => (Fg::LightYellow, "\0LIGHTYELLOW\0"), + log::Level::Info => (Fg::LightWhite, "\0LIGHTGREY\0"), + log::Level::Debug => (Fg::Blue, "\0BLUE\0"), + log::Level::Trace => (Fg::Yellow, "\0YELLOW\0"), }; - /* - let msg = format!( - "[{}{}$RESET$][$GREEN${}$RESET$]{}\n", - color.1, - record.level(), - time_float, - record.args() - ); - */ let mod_path = match record.module_path() { Some(p) => { if KERNEL_CONF.logging.filter.contains(&p.to_string()) { @@ -50,16 +47,29 @@ impl log::Log for SimpleLogger { None => 0, }; + let msg = format!( + // "[{}{}$RESET$][$GREEN${}$RESET$]{}\n", + "[{}{:05}\0RESET\0][\0GREEN\0{}\0RESET\0][\0BLUE\0{}@{}\0RESET\0] {}", + color.1, + record.level(), + time_float, + mod_path, + line, + record.args(), + ); + + println!("{msg}"); + if KERNEL_CONF.logging.log_to_serial { serial_println!( "[{}{:05}{}][{}{}{}][{}{}@{}{}] {}", color.0, record.level(), - Fg::Reset, - Fg::Green, + Reset, + Green, time_float, Reset, - Fg::Blue, + Blue, mod_path, line, Reset, diff --git a/ableos/src/panic.rs b/ableos/src/panic.rs index d2a7938..41b0837 100644 --- a/ableos/src/panic.rs +++ b/ableos/src/panic.rs @@ -1,8 +1,14 @@ +/* +* Copyright (c) 2022, able +* +* SPDX-License-Identifier: MPL-2.0 +*/ + use core::panic::PanicInfo; use log::error; use crate::arch::interrupts::{bsod, BSODSource}; - +#[cfg(not(test))] #[panic_handler] fn panic_handler(info: &PanicInfo) -> ! { error!("{:?}", info); diff --git a/ableos/src/scratchpad.rs b/ableos/src/scratchpad.rs index b91a90c..c059fef 100644 --- a/ableos/src/scratchpad.rs +++ b/ableos/src/scratchpad.rs @@ -1,5 +1,12 @@ +/* + * Copyright (c) 2022, Able + * + * SPDX-License-Identifier: MPL-2.0 + */ + use crate::arch::drivers::sysinfo::master; use crate::arch::interrupts::{reset_pit_for_cpu, set_pit_2}; +use crate::devices::pci::brute_force_scan; use crate::image::mono_bitmap::bruh; use crate::systeminfo::{KERNEL_VERSION, RELEASE_TYPE}; use crate::time::fetch_time; @@ -64,13 +71,14 @@ pub fn scratchpad() { // bruh(); // panic!(":>"); - println!("\0RED\0OHNO\0RESET\0"); - // for c in 0..144_697 { // trace!("{:?}", char::from_u32(c)); // } // bruh(); + for x in brute_force_scan() { + println!("{}", x); + } disable(); let tick_time = fetch_time(); diff --git a/ableos/src/tests/mod.rs b/ableos/src/tests/mod.rs new file mode 100644 index 0000000..d6474ac --- /dev/null +++ b/ableos/src/tests/mod.rs @@ -0,0 +1,70 @@ +/* +* Copyright (c) 2022, able +* +* SPDX-License-Identifier: MPL-2.0 +*/ + +use core::panic::PanicInfo; + +use kernel::arch::arch::sloop; +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); +} + +#[panic_handler] +fn panic(info: &PanicInfo) -> ! { + test_panic_handler(info); +} + +#[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); + } + loop {} +} + +entry_point!(test_kernel_main); +/// Entry point for `cargo test` +pub fn test_kernel_main(boot_info: &'static BootInfo) -> ! { + // init(boot_info.physical_memory_offset); + test_main(); + loop {} +} +use bootloader::{entry_point, BootInfo}; + +// use crate::test_main; diff --git a/ableos/src/tests.rs b/ableos/src/tests/old_tests.rs similarity index 98% rename from ableos/src/tests.rs rename to ableos/src/tests/old_tests.rs index ae6177f..0813157 100644 --- a/ableos/src/tests.rs +++ b/ableos/src/tests/old_tests.rs @@ -1,3 +1,9 @@ +/* +* Copyright (c) 2022, able +* +* SPDX-License-Identifier: MPL-2.0 +*/ + use crate::{ kmain::KERNEL_CONF, network::socket::{SimpleSock, Socket, SocketReturns}, diff --git a/repbuild/src/main.rs b/repbuild/src/main.rs index 9a45532..768acd6 100644 --- a/repbuild/src/main.rs +++ b/repbuild/src/main.rs @@ -1,8 +1,9 @@ /* - * Copyright (c) 2022, Umut İnan Erdoğan - * - * SPDX-License-Identifier: MPL-2.0 - */ +* Copyright (c) 2022, Umut İnan Erdoğan +* Copyright (c) 2022, able +* +* SPDX-License-Identifier: MPL-2.0 +*/ use std::{fs, process::Command}; @@ -18,6 +19,8 @@ enum Subcommand { Help, Run, Empty, + /// Run all tests for all architectures + Test, Unknown(String), } @@ -27,6 +30,7 @@ impl Subcommand { "doc" => Subcommand::Doc, "help" => Subcommand::Help, "run" => Subcommand::Run, + "test" => Subcommand::Test, "" => Subcommand::Empty, unknown => Subcommand::Unknown(unknown.to_string()), } @@ -44,6 +48,15 @@ fn main() { let options = options(); match options.subcommand { + Subcommand::Test => { + Command::new("cargo") + .args(["test", "--target=json_targets/x86_64-ableos.json"]) + .current_dir(fs::canonicalize("./ableos").unwrap()) + .status() + .unwrap(); + + // panic!("Test Infrastructure missing"); + } Subcommand::Doc => { let machine_text = options .arguments