Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
bytecodealliance
GitHub Repository: bytecodealliance/wasmtime
Path: blob/main/crates/wiggle/tests/handles.rs
1692 views
1
use proptest::prelude::*;
2
use wiggle::{GuestMemory, GuestPtr, GuestType};
3
use wiggle_test::{HostMemory, MemArea, WasiCtx, impl_errno};
4
5
const FD_VAL: u32 = 123;
6
7
wiggle::from_witx!({
8
witx: ["tests/handles.witx"],
9
});
10
11
impl_errno!(types::Errno);
12
13
impl<'a> handle_examples::HandleExamples for WasiCtx<'a> {
14
fn fd_create(&mut self, _memory: &mut GuestMemory<'_>) -> Result<types::Fd, types::Errno> {
15
Ok(types::Fd::from(FD_VAL))
16
}
17
fn fd_consume(
18
&mut self,
19
_memory: &mut GuestMemory<'_>,
20
fd: types::Fd,
21
) -> Result<(), types::Errno> {
22
println!("FD_CONSUME {fd}");
23
if fd == types::Fd::from(FD_VAL) {
24
Ok(())
25
} else {
26
Err(types::Errno::InvalidArg)
27
}
28
}
29
}
30
31
#[derive(Debug)]
32
struct HandleExercise {
33
pub return_loc: MemArea,
34
}
35
36
impl HandleExercise {
37
pub fn test(&self) {
38
let mut ctx = WasiCtx::new();
39
let mut host_memory = HostMemory::new();
40
let mut memory = host_memory.guest_memory();
41
42
let e =
43
handle_examples::fd_create(&mut ctx, &mut memory, self.return_loc.ptr as i32).unwrap();
44
45
assert_eq!(e, types::Errno::Ok as i32, "fd_create error");
46
47
let h_got: u32 = memory
48
.read(GuestPtr::new(self.return_loc.ptr))
49
.expect("return ref_mut");
50
51
assert_eq!(h_got, 123, "fd_create return val");
52
53
let e = handle_examples::fd_consume(&mut ctx, &mut memory, h_got as i32).unwrap();
54
55
assert_eq!(e, types::Errno::Ok as i32, "fd_consume error");
56
57
let e = handle_examples::fd_consume(&mut ctx, &mut memory, h_got as i32 + 1).unwrap();
58
59
assert_eq!(
60
e,
61
types::Errno::InvalidArg as i32,
62
"fd_consume invalid error"
63
);
64
}
65
66
pub fn strat() -> BoxedStrategy<Self> {
67
(HostMemory::mem_area_strat(types::Fd::guest_size()))
68
.prop_map(|return_loc| HandleExercise { return_loc })
69
.boxed()
70
}
71
}
72
73
proptest! {
74
#[test]
75
fn handle_exercise(e in HandleExercise::strat()) {
76
e.test()
77
}
78
}
79
80