This commit is contained in:
Ashwin Naren
2025-11-28 15:16:39 -08:00
committed by Matthew Leach
parent e2534ffd9c
commit b05c606a2a
6 changed files with 129 additions and 2 deletions

7
Cargo.lock generated
View File

@@ -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"

View File

@@ -1,5 +1,5 @@
[workspace]
members = [ "libkernel" ]
members = ["libkernel", "usertest"]
[package]
name = "moss"

View File

@@ -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"]

View 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
View 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
View 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());
}