mirror of
https://github.com/hexagonal-sun/moss-kernel.git
synced 2025-12-23 22:47:55 -05:00
testing
This commit is contained in:
committed by
Matthew Leach
parent
e2534ffd9c
commit
b05c606a2a
7
Cargo.lock
generated
7
Cargo.lock
generated
@@ -559,6 +559,13 @@ version = "1.0.22"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9312f7c4f6ff9069b165498234ce8be658059c6728633667c526e27dc2cf1df5"
|
||||
|
||||
[[package]]
|
||||
name = "usertest"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"libc",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "wasi"
|
||||
version = "0.11.1+wasi-snapshot-preview1"
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
[workspace]
|
||||
members = [ "libkernel" ]
|
||||
members = ["libkernel", "usertest"]
|
||||
|
||||
[package]
|
||||
name = "moss"
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
[toolchain]
|
||||
channel = "nightly"
|
||||
components = [ "rust-src", "llvm-tools-preview", "rustfmt", "clippy", "rust-analyzer" ]
|
||||
targets = [ "aarch64-unknown-none-softfloat" ]
|
||||
targets = ["aarch64-unknown-none-softfloat", "aarch64-unknown-linux-musl"]
|
||||
|
||||
5
usertest/.cargo/config.toml
Normal file
5
usertest/.cargo/config.toml
Normal file
@@ -0,0 +1,5 @@
|
||||
[build]
|
||||
target = "aarch64-unknown-linux-musl"
|
||||
|
||||
[target.aarch64-unknown-linux-musl]
|
||||
linker = "rust-lld"
|
||||
7
usertest/Cargo.toml
Normal file
7
usertest/Cargo.toml
Normal file
@@ -0,0 +1,7 @@
|
||||
[package]
|
||||
name = "usertest"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
libc = "0.2"
|
||||
108
usertest/src/main.rs
Normal file
108
usertest/src/main.rs
Normal file
@@ -0,0 +1,108 @@
|
||||
fn test_sync() {
|
||||
print!("Testing sync syscall ...");
|
||||
unsafe {
|
||||
libc::sync();
|
||||
}
|
||||
println!(" OK");
|
||||
}
|
||||
|
||||
fn test_opendir() {
|
||||
print!("Testing opendir syscall ...");
|
||||
let path = std::ffi::CString::new("/").unwrap();
|
||||
unsafe {
|
||||
let dir = libc::opendir(path.as_ptr());
|
||||
if dir.is_null() {
|
||||
panic!("opendir failed");
|
||||
}
|
||||
libc::closedir(dir);
|
||||
}
|
||||
println!(" OK");
|
||||
}
|
||||
|
||||
fn test_readdir() {
|
||||
print!("Testing readdir syscall ...");
|
||||
let path = std::ffi::CString::new("/").unwrap();
|
||||
unsafe {
|
||||
let dir = libc::opendir(path.as_ptr());
|
||||
if dir.is_null() {
|
||||
panic!("opendir failed");
|
||||
}
|
||||
let mut count = 0;
|
||||
loop {
|
||||
let entry = libc::readdir(dir);
|
||||
if entry.is_null() {
|
||||
break;
|
||||
}
|
||||
count += 1;
|
||||
}
|
||||
libc::closedir(dir);
|
||||
if count == 0 {
|
||||
panic!("readdir returned no entries");
|
||||
}
|
||||
}
|
||||
println!(" OK");
|
||||
}
|
||||
|
||||
fn test_chdir() {
|
||||
print!("Testing chdir syscall ...");
|
||||
let path = std::ffi::CString::new("/").unwrap();
|
||||
unsafe {
|
||||
if libc::chdir(path.as_ptr()) != 0 {
|
||||
panic!("chdir failed");
|
||||
}
|
||||
}
|
||||
println!(" OK");
|
||||
}
|
||||
|
||||
fn test_fork() {
|
||||
print!("Testing fork syscall ...");
|
||||
unsafe {
|
||||
let pid = libc::fork();
|
||||
if pid < 0 {
|
||||
panic!("fork failed");
|
||||
} else if pid == 0 {
|
||||
// Child process
|
||||
libc::_exit(0);
|
||||
} else {
|
||||
// Parent process
|
||||
let mut status = 0;
|
||||
libc::waitpid(pid, &mut status, 0);
|
||||
}
|
||||
}
|
||||
println!(" OK");
|
||||
}
|
||||
|
||||
fn run_test(test_fn: fn()) {
|
||||
// Fork a new process to run the test
|
||||
unsafe {
|
||||
let pid = libc::fork();
|
||||
if pid < 0 {
|
||||
panic!("fork failed");
|
||||
} else if pid == 0 {
|
||||
// Child process
|
||||
test_fn();
|
||||
libc::_exit(0);
|
||||
} else {
|
||||
// Parent process
|
||||
let mut status = 0;
|
||||
libc::waitpid(pid, &mut status, 0);
|
||||
if !libc::WIFEXITED(status) || libc::WEXITSTATUS(status) != 0 {
|
||||
panic!("Test failed in child process");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
println!("Running userspace tests ...");
|
||||
let start = std::time::Instant::now();
|
||||
// TODO: make tests their own process later
|
||||
run_test(test_sync);
|
||||
run_test(test_opendir);
|
||||
run_test(test_readdir);
|
||||
run_test(test_chdir);
|
||||
run_test(test_fork);
|
||||
let end = std::time::Instant::now();
|
||||
println!("All tests passed in {} ms", (end - start).as_millis());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user