From 92b2ce9d8f712c13f01235a13781827666606345 Mon Sep 17 00:00:00 2001 From: Kristofers Solo Date: Sun, 28 Sep 2025 20:01:34 +0300 Subject: [PATCH] refactor(tests): modularize --- src/lib.rs | 10 +++++++--- tests/common.rs | 27 +++++++++++++++++++++++++++ tests/{captra.rs => manifest.rs} | 5 ++--- tests/wasm.rs | 31 ++++++++++--------------------- 4 files changed, 46 insertions(+), 27 deletions(-) create mode 100644 tests/common.rs rename tests/{captra.rs => manifest.rs} (97%) diff --git a/src/lib.rs b/src/lib.rs index 3bc6077..9036d90 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,3 +1,7 @@ -pub mod host; -pub mod manifest; -pub mod trace; +mod host; +mod manifest; +mod trace; + +pub use host::{CapError, HostState, HostStatus, add_wasm_linker_funcs, init_tracing}; +pub use manifest::{Capability, CapabilityManifest, ManifestError, load_manifest}; +pub use trace::{CapEventSubtype, EventType, SignedTrace, TraceError, TraceEvent, load_trace}; diff --git a/tests/common.rs b/tests/common.rs new file mode 100644 index 0000000..6ab46d1 --- /dev/null +++ b/tests/common.rs @@ -0,0 +1,27 @@ +use captra::{CapabilityManifest, HostState, add_wasm_linker_funcs, load_manifest}; +use ed25519_dalek::SigningKey; +use rand::rngs::OsRng; +use wasmtime::{Engine, Linker, Store}; + +/// Load the example manifest bundled with the repo. +pub fn load_example_manifest() -> CapabilityManifest { + load_manifest("examples/manifest.json").expect("examples/manifest.json must exists") +} + +/// Build a HostState with a fixed seed and a fresh SigningKey (OsRng). +pub fn make_host_with_seed(seed: u64) -> HostState { + let manifest = load_example_manifest(); + let mut csprng = OsRng; + let keypair = SigningKey::generate(&mut csprng); + HostState::new(manifest, seed, keypair) +} + +/// Create a wasmtime engine + linker with your host functions registered, +/// and a Store that owns the given HostState. +pub fn wasm_store_with_hosts(host: HostState) -> (Engine, Linker, Store) { + let engine = Engine::default(); + let mut linker = Linker::new(&engine); + add_wasm_linker_funcs(&mut linker).expect("linker registration"); + let store = Store::new(&engine, host); + (engine, linker, store) +} diff --git a/tests/captra.rs b/tests/manifest.rs similarity index 97% rename from tests/captra.rs rename to tests/manifest.rs index 49059d9..fad8cc1 100644 --- a/tests/captra.rs +++ b/tests/manifest.rs @@ -1,8 +1,7 @@ use base64::{Engine, engine::general_purpose::STANDARD}; use captra::{ - host::{CapError, HostState, init_tracing}, - manifest::{ManifestError, load_manifest}, - trace::{EventType, TraceEvent, load_trace}, + CapError, EventType, HostState, ManifestError, TraceEvent, init_tracing, load_manifest, + load_trace, }; use claims::{assert_err, assert_matches, assert_ok, assert_some}; use ed25519_dalek::SigningKey; diff --git a/tests/wasm.rs b/tests/wasm.rs index 2aa5012..5b21cfb 100644 --- a/tests/wasm.rs +++ b/tests/wasm.rs @@ -1,26 +1,17 @@ mod common; -use captra::{HostState, HostStatus, add_wasm_linker_funcs, load_manifest}; +use captra::HostStatus; use claims::{assert_ok, assert_some}; -use ed25519_dalek::SigningKey; -use rand::rngs::OsRng; -use wasmtime::{Engine, Linker, Module, Store}; +use wasmtime::Module; + +use crate::common::{make_host_with_seed, wasm_store_with_hosts}; #[test] fn wasm_integration_allowed() { - let manifest = assert_ok!(load_manifest("examples/manifest.json"), "Load failed"); - let fixed_seed = 12345; - let mut csprng = OsRng; - let keypair = SigningKey::generate(&mut csprng); - let host = HostState::new(manifest, fixed_seed, keypair); - - let engine = Engine::default(); - let mut linker = Linker::new(&engine); - - assert_ok!(add_wasm_linker_funcs(&mut linker)); + let host = make_host_with_seed(12345); + let (engine, linker, mut store) = wasm_store_with_hosts(host); let path = "./workspace/test.txt"; - let path_len = path.as_bytes().len(); let wat = format!( r#" (module @@ -30,21 +21,19 @@ fn wasm_integration_allowed() { (data (i32.const 0) "{path}") (func (export "run") (result i32) i32.const 0 - i32.const {path_len} + i32.const {len} call $host_read_file call $host_status_allowed i32.eq ;; returns 1 if allowed, 0 if denied, -1 if error ) ) - "# + "#, + len = path.as_bytes().len() ); - let module = assert_ok!(Module::new(&engine, wat)); - let mut store = Store::new(&engine, host); - + let module = assert_ok!(Module::new(&engine, &wat)); let instance = assert_ok!(linker.instantiate(&mut store, &module)); let run = assert_ok!(instance.get_typed_func::<(), i32>(&mut store, "run")); - let ret = assert_ok!(run.call(&mut store, ())); assert_eq!(ret, HostStatus::Denied as i32);