refactor(tests): modularize

This commit is contained in:
Kristofers Solo 2025-09-28 20:01:34 +03:00
parent e98542b23a
commit 92b2ce9d8f
Signed by: kristoferssolo
GPG Key ID: 8687F2D3EEE6F0ED
4 changed files with 46 additions and 27 deletions

View File

@ -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};

27
tests/common.rs Normal file
View File

@ -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<HostState>, Store<HostState>) {
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)
}

View File

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

View File

@ -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);