-
Notifications
You must be signed in to change notification settings - Fork 259
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Lann Martin <[email protected]>
- Loading branch information
Showing
10 changed files
with
286 additions
and
4 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
[build] | ||
target = "wasm32-wasi" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
[package] | ||
name = "core-wasi-test" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[profile.release] | ||
debug = true | ||
|
||
[workspace] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
//! This test program takes argument(s) that determine which WASI feature to | ||
//! exercise and returns an exit code of 0 for success, 1 for WASI interface | ||
//! failure (which is sometimes expected in a test), and some other code on | ||
//! invalid argument(s). | ||
type Result = std::result::Result<(), Box<dyn std::error::Error>>; | ||
|
||
fn main() -> Result { | ||
let mut args = std::env::args(); | ||
let cmd = args.next().expect("cmd"); | ||
match cmd.as_str() { | ||
"noop" => (), | ||
"echo" => { | ||
eprintln!("echo"); | ||
std::io::copy(&mut std::io::stdin(), &mut std::io::stdout())?; | ||
} | ||
"alloc" => { | ||
let size: usize = args.next().expect("size").parse().expect("size"); | ||
eprintln!("alloc {size}"); | ||
let layout = std::alloc::Layout::from_size_align(size, 8).expect("layout"); | ||
unsafe { | ||
let p = std::alloc::alloc(layout); | ||
if p.is_null() { | ||
return Err("allocation failed".into()); | ||
} | ||
// Force allocation to actually happen | ||
p.read_volatile(); | ||
} | ||
} | ||
"read" => { | ||
let path = args.next().expect("path"); | ||
eprintln!("read {path}"); | ||
std::fs::read(path)?; | ||
} | ||
"write" => { | ||
let path = args.next().expect("path"); | ||
eprintln!("write {path}"); | ||
std::fs::write(path, "content")?; | ||
} | ||
"panic" => { | ||
eprintln!("panic"); | ||
panic!("intentional panic"); | ||
} | ||
cmd => panic!("unknown cmd {cmd}"), | ||
}; | ||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
use std::{io::Cursor, path::PathBuf}; | ||
|
||
use spin_core::{Config, Engine, Module, StoreBuilder, Trap}; | ||
use tempfile::TempDir; | ||
use wasi_common::pipe::WritePipe; | ||
use wasmtime::TrapCode; | ||
|
||
#[tokio::test(flavor = "multi_thread")] | ||
async fn test_stdio() { | ||
let stdout_pipe = WritePipe::new_in_memory(); | ||
|
||
run_core_wasi_test(["echo"], |store_builder| { | ||
store_builder.stdin_pipe(Cursor::new(b"DATA")); | ||
store_builder.stdout(stdout_pipe.clone()); | ||
}) | ||
.await | ||
.unwrap(); | ||
|
||
assert_eq!(stdout_pipe.try_into_inner().unwrap().into_inner(), b"DATA"); | ||
} | ||
|
||
#[tokio::test(flavor = "multi_thread")] | ||
async fn test_read_only_preopened_dir() { | ||
let filename = "test_file"; | ||
let tempdir = TempDir::new().unwrap(); | ||
std::fs::write(tempdir.path().join(filename), "x").unwrap(); | ||
|
||
run_core_wasi_test(["read", filename], |store_builder| { | ||
store_builder | ||
.read_only_preopened_dir(&tempdir, "/".into()) | ||
.unwrap(); | ||
}) | ||
.await | ||
.unwrap(); | ||
} | ||
|
||
#[tokio::test(flavor = "multi_thread")] | ||
async fn test_read_only_preopened_dir_write_fails() { | ||
let filename = "test_file"; | ||
let tempdir = TempDir::new().unwrap(); | ||
std::fs::write(tempdir.path().join(filename), "x").unwrap(); | ||
|
||
let err = run_core_wasi_test(["write", filename], |store_builder| { | ||
store_builder | ||
.read_only_preopened_dir(&tempdir, "/".into()) | ||
.unwrap(); | ||
}) | ||
.await | ||
.unwrap_err(); | ||
let trap = err.downcast::<Trap>().expect("trap"); | ||
assert_eq!(trap.i32_exit_status(), Some(1)); | ||
} | ||
|
||
#[tokio::test(flavor = "multi_thread")] | ||
async fn test_read_write_preopened_dir() { | ||
let filename = "test_file"; | ||
let tempdir = TempDir::new().unwrap(); | ||
|
||
run_core_wasi_test(["write", filename], |store_builder| { | ||
store_builder | ||
.read_write_preopened_dir(&tempdir, "/".into()) | ||
.unwrap(); | ||
}) | ||
.await | ||
.unwrap(); | ||
|
||
let content = std::fs::read(tempdir.path().join(filename)).unwrap(); | ||
assert_eq!(content, b"content"); | ||
} | ||
|
||
#[tokio::test(flavor = "multi_thread")] | ||
async fn test_max_memory_size_obeyed() { | ||
let max = 10_000_000; | ||
let alloc = max / 10; | ||
run_core_wasi_test(["alloc", &format!("{alloc}")], |store_builder| { | ||
store_builder.max_memory_size(max); | ||
}) | ||
.await | ||
.unwrap(); | ||
} | ||
|
||
#[tokio::test(flavor = "multi_thread")] | ||
async fn test_max_memory_size_violated() { | ||
let max = 10_000_000; | ||
let alloc = max * 2; | ||
let err = run_core_wasi_test(["alloc", &format!("{alloc}")], |store_builder| { | ||
store_builder.max_memory_size(max); | ||
}) | ||
.await | ||
.unwrap_err(); | ||
let trap = err.downcast::<Trap>().expect("trap"); | ||
assert_eq!(trap.i32_exit_status(), Some(1)); | ||
} | ||
|
||
#[tokio::test(flavor = "multi_thread")] | ||
#[cfg(not(tarpaulin))] | ||
async fn test_panic() { | ||
let err = run_core_wasi_test(["panic"], |_| {}).await.unwrap_err(); | ||
let trap = err.downcast::<Trap>().expect("trap"); | ||
assert_eq!(trap.trap_code(), Some(TrapCode::UnreachableCodeReached)); | ||
} | ||
|
||
async fn run_core_wasi_test<'a>( | ||
args: impl IntoIterator<Item = &'a str>, | ||
f: impl FnOnce(&mut StoreBuilder), | ||
) -> anyhow::Result<()> { | ||
let mut config = Config::default(); | ||
config | ||
.wasmtime_config() | ||
.wasm_backtrace_details(wasmtime::WasmBacktraceDetails::Enable); | ||
|
||
let engine: Engine<()> = Engine::builder(&config).unwrap().build(); | ||
|
||
let mut store_builder: StoreBuilder = engine.store_builder(); | ||
|
||
f(&mut store_builder); | ||
store_builder.stderr_pipe(TestWriter); | ||
store_builder.args(args).unwrap(); | ||
|
||
let mut store = store_builder.build().unwrap(); | ||
|
||
let module_path = PathBuf::from(env!("CARGO_MANIFEST_DIR")) | ||
.join("../../target/test-programs/core-wasi-test.wasm"); | ||
let module = Module::from_file(engine.as_ref(), module_path).unwrap(); | ||
|
||
let instance_pre = engine.instantiate_pre(&module).unwrap(); | ||
|
||
let instance = instance_pre.instantiate_async(&mut store).await.unwrap(); | ||
|
||
let func = instance.get_func(&mut store, "_start").unwrap(); | ||
|
||
func.call_async(&mut store, &[], &mut []).await | ||
} | ||
|
||
// Write with `print!`, required for test output capture | ||
struct TestWriter; | ||
|
||
impl std::io::Write for TestWriter { | ||
fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> { | ||
print!("{}", String::from_utf8_lossy(buf)); | ||
Ok(buf.len()) | ||
} | ||
|
||
fn flush(&mut self) -> std::io::Result<()> { | ||
Ok(()) | ||
} | ||
} |