mitoxide_wasm/
test_utils.rs

1//! Test utilities for WASM module testing
2
3pub mod test_modules {
4    use std::sync::OnceLock;
5    
6    /// Generate test WASM modules using wat
7    fn generate_minimal_wasm() -> Vec<u8> {
8        wat::parse_str("(module)").unwrap()
9    }
10    
11    fn generate_simple_function_wasm() -> Vec<u8> {
12        wat::parse_str(r#"
13            (module
14              (func $add (param i32 i32) (result i32)
15                local.get 0
16                local.get 1
17                i32.add)
18              (export "add" (func $add)))
19        "#).unwrap()
20    }
21    
22    fn generate_wasi_hello_wasm() -> Vec<u8> {
23        wat::parse_str(r#"
24            (module
25              (import "wasi_snapshot_preview1" "fd_write" 
26                (func $fd_write (param i32 i32 i32 i32) (result i32)))
27              (import "wasi_snapshot_preview1" "environ_get"
28                (func $environ_get (param i32 i32) (result i32)))
29              (func $_start
30                nop)
31              (export "_start" (func $_start))
32              (memory 1)
33              (export "memory" (memory 0)))
34        "#).unwrap()
35    }
36    
37    // Use OnceLock to cache the generated WASM modules
38    static MINIMAL_WASM: OnceLock<Vec<u8>> = OnceLock::new();
39    static SIMPLE_FUNCTION_WASM: OnceLock<Vec<u8>> = OnceLock::new();
40    static WASI_HELLO_WASM: OnceLock<Vec<u8>> = OnceLock::new();
41    
42    /// A minimal valid WASM module that does nothing
43    pub fn minimal_wasm() -> &'static [u8] {
44        MINIMAL_WASM.get_or_init(generate_minimal_wasm)
45    }
46    
47    /// A WASM module with a simple function export
48    pub fn simple_function_wasm() -> &'static [u8] {
49        SIMPLE_FUNCTION_WASM.get_or_init(generate_simple_function_wasm)
50    }
51    
52    /// A WASI-compatible WASM module with _start export
53    pub fn wasi_hello_wasm() -> &'static [u8] {
54        WASI_HELLO_WASM.get_or_init(generate_wasi_hello_wasm)
55    }
56    
57    /// Invalid WASM with wrong magic number
58    pub const INVALID_MAGIC_WASM: &[u8] = &[
59        0xFF, 0xFF, 0xFF, 0xFF, // wrong magic
60        0x01, 0x00, 0x00, 0x00, // version
61    ];
62}