mirror of
https://github.com/kristoferssolo/captra.git
synced 2025-12-20 11:04:39 +00:00
refactor(tests): modularize
This commit is contained in:
parent
e98542b23a
commit
92b2ce9d8f
10
src/lib.rs
10
src/lib.rs
@ -1,3 +1,7 @@
|
|||||||
pub mod host;
|
mod host;
|
||||||
pub mod manifest;
|
mod manifest;
|
||||||
pub mod trace;
|
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
27
tests/common.rs
Normal 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)
|
||||||
|
}
|
||||||
@ -1,8 +1,7 @@
|
|||||||
use base64::{Engine, engine::general_purpose::STANDARD};
|
use base64::{Engine, engine::general_purpose::STANDARD};
|
||||||
use captra::{
|
use captra::{
|
||||||
host::{CapError, HostState, init_tracing},
|
CapError, EventType, HostState, ManifestError, TraceEvent, init_tracing, load_manifest,
|
||||||
manifest::{ManifestError, load_manifest},
|
load_trace,
|
||||||
trace::{EventType, TraceEvent, load_trace},
|
|
||||||
};
|
};
|
||||||
use claims::{assert_err, assert_matches, assert_ok, assert_some};
|
use claims::{assert_err, assert_matches, assert_ok, assert_some};
|
||||||
use ed25519_dalek::SigningKey;
|
use ed25519_dalek::SigningKey;
|
||||||
@ -1,26 +1,17 @@
|
|||||||
mod common;
|
mod common;
|
||||||
|
|
||||||
use captra::{HostState, HostStatus, add_wasm_linker_funcs, load_manifest};
|
use captra::HostStatus;
|
||||||
use claims::{assert_ok, assert_some};
|
use claims::{assert_ok, assert_some};
|
||||||
use ed25519_dalek::SigningKey;
|
use wasmtime::Module;
|
||||||
use rand::rngs::OsRng;
|
|
||||||
use wasmtime::{Engine, Linker, Module, Store};
|
use crate::common::{make_host_with_seed, wasm_store_with_hosts};
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn wasm_integration_allowed() {
|
fn wasm_integration_allowed() {
|
||||||
let manifest = assert_ok!(load_manifest("examples/manifest.json"), "Load failed");
|
let host = make_host_with_seed(12345);
|
||||||
let fixed_seed = 12345;
|
let (engine, linker, mut store) = wasm_store_with_hosts(host);
|
||||||
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 path = "./workspace/test.txt";
|
let path = "./workspace/test.txt";
|
||||||
let path_len = path.as_bytes().len();
|
|
||||||
let wat = format!(
|
let wat = format!(
|
||||||
r#"
|
r#"
|
||||||
(module
|
(module
|
||||||
@ -30,21 +21,19 @@ fn wasm_integration_allowed() {
|
|||||||
(data (i32.const 0) "{path}")
|
(data (i32.const 0) "{path}")
|
||||||
(func (export "run") (result i32)
|
(func (export "run") (result i32)
|
||||||
i32.const 0
|
i32.const 0
|
||||||
i32.const {path_len}
|
i32.const {len}
|
||||||
call $host_read_file
|
call $host_read_file
|
||||||
call $host_status_allowed
|
call $host_status_allowed
|
||||||
i32.eq ;; returns 1 if allowed, 0 if denied, -1 if error
|
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 module = assert_ok!(Module::new(&engine, &wat));
|
||||||
let mut store = Store::new(&engine, host);
|
|
||||||
|
|
||||||
let instance = assert_ok!(linker.instantiate(&mut store, &module));
|
let instance = assert_ok!(linker.instantiate(&mut store, &module));
|
||||||
let run = assert_ok!(instance.get_typed_func::<(), i32>(&mut store, "run"));
|
let run = assert_ok!(instance.get_typed_func::<(), i32>(&mut store, "run"));
|
||||||
|
|
||||||
let ret = assert_ok!(run.call(&mut store, ()));
|
let ret = assert_ok!(run.call(&mut store, ()));
|
||||||
assert_eq!(ret, HostStatus::Denied as i32);
|
assert_eq!(ret, HostStatus::Denied as i32);
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user