|
| 1 | +use std::{io::Cursor, path::PathBuf}; |
| 2 | + |
| 3 | +use spin_core::{Config, Engine, Module, StoreBuilder, Trap}; |
| 4 | +use tempfile::TempDir; |
| 5 | +use wasi_common::pipe::WritePipe; |
| 6 | +use wasmtime::TrapCode; |
| 7 | + |
| 8 | +#[tokio::test(flavor = "multi_thread")] |
| 9 | +async fn test_stdio() { |
| 10 | + let stdout_pipe = WritePipe::new_in_memory(); |
| 11 | + |
| 12 | + run_core_wasi_test(["echo"], |store_builder| { |
| 13 | + store_builder.stdin_pipe(Cursor::new(b"DATA")); |
| 14 | + store_builder.stdout(stdout_pipe.clone()); |
| 15 | + }) |
| 16 | + .await |
| 17 | + .unwrap(); |
| 18 | + |
| 19 | + assert_eq!(stdout_pipe.try_into_inner().unwrap().into_inner(), b"DATA"); |
| 20 | +} |
| 21 | + |
| 22 | +#[tokio::test(flavor = "multi_thread")] |
| 23 | +async fn test_read_only_preopened_dir() { |
| 24 | + let filename = "test_file"; |
| 25 | + let tempdir = TempDir::new().unwrap(); |
| 26 | + std::fs::write(tempdir.path().join(filename), "x").unwrap(); |
| 27 | + |
| 28 | + run_core_wasi_test(["read", filename], |store_builder| { |
| 29 | + store_builder |
| 30 | + .read_only_preopened_dir(&tempdir, "/".into()) |
| 31 | + .unwrap(); |
| 32 | + }) |
| 33 | + .await |
| 34 | + .unwrap(); |
| 35 | +} |
| 36 | + |
| 37 | +#[tokio::test(flavor = "multi_thread")] |
| 38 | +async fn test_read_only_preopened_dir_write_fails() { |
| 39 | + let filename = "test_file"; |
| 40 | + let tempdir = TempDir::new().unwrap(); |
| 41 | + std::fs::write(tempdir.path().join(filename), "x").unwrap(); |
| 42 | + |
| 43 | + let err = run_core_wasi_test(["write", filename], |store_builder| { |
| 44 | + store_builder |
| 45 | + .read_only_preopened_dir(&tempdir, "/".into()) |
| 46 | + .unwrap(); |
| 47 | + }) |
| 48 | + .await |
| 49 | + .unwrap_err(); |
| 50 | + let trap = err.downcast::<Trap>().expect("trap"); |
| 51 | + assert_eq!(trap.i32_exit_status(), Some(1)); |
| 52 | +} |
| 53 | + |
| 54 | +#[tokio::test(flavor = "multi_thread")] |
| 55 | +async fn test_read_write_preopened_dir() { |
| 56 | + let filename = "test_file"; |
| 57 | + let tempdir = TempDir::new().unwrap(); |
| 58 | + |
| 59 | + run_core_wasi_test(["write", filename], |store_builder| { |
| 60 | + store_builder |
| 61 | + .read_write_preopened_dir(&tempdir, "/".into()) |
| 62 | + .unwrap(); |
| 63 | + }) |
| 64 | + .await |
| 65 | + .unwrap(); |
| 66 | + |
| 67 | + let content = std::fs::read(tempdir.path().join(filename)).unwrap(); |
| 68 | + assert_eq!(content, b"content"); |
| 69 | +} |
| 70 | + |
| 71 | +#[tokio::test(flavor = "multi_thread")] |
| 72 | +async fn test_max_memory_size_obeyed() { |
| 73 | + let max = 10_000_000; |
| 74 | + let alloc = max / 10; |
| 75 | + run_core_wasi_test(["alloc", &format!("{alloc}")], |store_builder| { |
| 76 | + store_builder.max_memory_size(max); |
| 77 | + }) |
| 78 | + .await |
| 79 | + .unwrap(); |
| 80 | +} |
| 81 | + |
| 82 | +#[tokio::test(flavor = "multi_thread")] |
| 83 | +async fn test_max_memory_size_violated() { |
| 84 | + let max = 10_000_000; |
| 85 | + let alloc = max * 2; |
| 86 | + let err = run_core_wasi_test(["alloc", &format!("{alloc}")], |store_builder| { |
| 87 | + store_builder.max_memory_size(max); |
| 88 | + }) |
| 89 | + .await |
| 90 | + .unwrap_err(); |
| 91 | + let trap = err.downcast::<Trap>().expect("trap"); |
| 92 | + assert_eq!(trap.i32_exit_status(), Some(1)); |
| 93 | +} |
| 94 | + |
| 95 | +#[tokio::test(flavor = "multi_thread")] |
| 96 | +#[cfg(not(tarpaulin))] |
| 97 | +async fn test_panic() { |
| 98 | + let err = run_core_wasi_test(["panic"], |_| {}).await.unwrap_err(); |
| 99 | + let trap = err.downcast::<Trap>().expect("trap"); |
| 100 | + assert_eq!(trap.trap_code(), Some(TrapCode::UnreachableCodeReached)); |
| 101 | +} |
| 102 | + |
| 103 | +async fn run_core_wasi_test<'a>( |
| 104 | + args: impl IntoIterator<Item = &'a str>, |
| 105 | + f: impl FnOnce(&mut StoreBuilder), |
| 106 | +) -> anyhow::Result<()> { |
| 107 | + let mut config = Config::default(); |
| 108 | + config |
| 109 | + .wasmtime_config() |
| 110 | + .wasm_backtrace_details(wasmtime::WasmBacktraceDetails::Enable); |
| 111 | + |
| 112 | + let engine: Engine<()> = Engine::builder(&config).unwrap().build(); |
| 113 | + |
| 114 | + let mut store_builder: StoreBuilder = engine.store_builder(); |
| 115 | + |
| 116 | + f(&mut store_builder); |
| 117 | + store_builder.stderr_pipe(TestWriter); |
| 118 | + store_builder.args(args).unwrap(); |
| 119 | + |
| 120 | + let mut store = store_builder.build().unwrap(); |
| 121 | + |
| 122 | + let module_path = PathBuf::from(env!("CARGO_MANIFEST_DIR")) |
| 123 | + .join("../../target/test-programs/core-wasi-test.wasm"); |
| 124 | + let module = Module::from_file(engine.as_ref(), module_path).unwrap(); |
| 125 | + |
| 126 | + let instance_pre = engine.instantiate_pre(&module).unwrap(); |
| 127 | + |
| 128 | + let instance = instance_pre.instantiate_async(&mut store).await.unwrap(); |
| 129 | + |
| 130 | + let func = instance.get_func(&mut store, "_start").unwrap(); |
| 131 | + |
| 132 | + func.call_async(&mut store, &[], &mut []).await |
| 133 | +} |
| 134 | + |
| 135 | +// Write with `print!`, required for test output capture |
| 136 | +struct TestWriter; |
| 137 | + |
| 138 | +impl std::io::Write for TestWriter { |
| 139 | + fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> { |
| 140 | + print!("{}", String::from_utf8_lossy(buf)); |
| 141 | + Ok(buf.len()) |
| 142 | + } |
| 143 | + |
| 144 | + fn flush(&mut self) -> std::io::Result<()> { |
| 145 | + Ok(()) |
| 146 | + } |
| 147 | +} |
0 commit comments