Files
wasmtime/crates/wiggle/tests/handles.rs
Alex Crichton a628dc315e Shuffle around the wiggle crates (#1414)
* Shuffle around the wiggle crates

This commit reorganizes the wiggle crates slightly by performing the
following transforms:

* The `crates/wiggle` crate, previously named `wiggle`, was moved to
  `crates/wiggle/crates/macro` and is renamed to `wiggle-macro`.

* The `crates/wiggle/crates/runtime` crate, previously named
  `wiggle-runtime`, was moved to `crates/wiggle` and is renamed to
  `wiggle`.

* The new `wiggle` crate depends on `wiggle-macro` and reexports the macro.

The goal here is that consumers only deal with the `wiggle` crate
itself. No more crates depend on `wiggle-runtime` and all dependencies
are entirely on just the `wiggle` crate.

* Remove the `crates/wiggle/crates` directory

Move everything into `crates/wiggle` directly, like `wasi-common`

* Add wiggle-macro to test-all script

* Fixup a test
2020-03-26 18:34:50 -05:00

75 lines
1.9 KiB
Rust

use proptest::prelude::*;
use wiggle::{GuestError, GuestMemory, GuestType};
use wiggle_test::{impl_errno, HostMemory, MemArea, WasiCtx};
const FD_VAL: u32 = 123;
wiggle::from_witx!({
witx: ["tests/handles.witx"],
ctx: WasiCtx,
});
impl_errno!(types::Errno);
impl<'a> handle_examples::HandleExamples for WasiCtx<'a> {
fn fd_create(&self) -> Result<types::Fd, types::Errno> {
Ok(types::Fd::from(FD_VAL))
}
fn fd_consume(&self, fd: types::Fd) -> Result<(), types::Errno> {
println!("FD_CONSUME {}", fd);
if fd == types::Fd::from(FD_VAL) {
Ok(())
} else {
Err(types::Errno::InvalidArg)
}
}
}
#[derive(Debug)]
struct HandleExercise {
pub return_loc: MemArea,
}
impl HandleExercise {
pub fn test(&self) {
let ctx = WasiCtx::new();
let host_memory = HostMemory::new();
let e = handle_examples::fd_create(&ctx, &host_memory, self.return_loc.ptr as i32);
assert_eq!(e, types::Errno::Ok.into(), "fd_create error");
let h_got: u32 = host_memory
.ptr(self.return_loc.ptr)
.read()
.expect("return ref_mut");
assert_eq!(h_got, 123, "fd_create return val");
let e = handle_examples::fd_consume(&ctx, &host_memory, h_got as i32);
assert_eq!(e, types::Errno::Ok.into(), "fd_consume error");
let e = handle_examples::fd_consume(&ctx, &host_memory, h_got as i32 + 1);
assert_eq!(
e,
types::Errno::InvalidArg.into(),
"fd_consume invalid error"
);
}
pub fn strat() -> BoxedStrategy<Self> {
(HostMemory::mem_area_strat(types::Fd::guest_size()))
.prop_map(|return_loc| HandleExercise { return_loc })
.boxed()
}
}
proptest! {
#[test]
fn handle_exercise(e in HandleExercise::strat()) {
e.test()
}
}