Skip to content

Commit

Permalink
Update WASI tests to use wasi crate v0.9.0 (#743)
Browse files Browse the repository at this point in the history
This commit updates _all_ WASI test programs to use the latest
version of the `wasi` crate (`v0.9.0`). While at it, it also
unifies asserting error conditions across all test programs.
  • Loading branch information
Jakub Konka authored and sunfishcode committed Dec 24, 2019
1 parent 907e7aa commit 51f3ac0
Show file tree
Hide file tree
Showing 37 changed files with 855 additions and 1,853 deletions.
29 changes: 1 addition & 28 deletions crates/test-programs/tests/wasm_tests/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,41 +55,14 @@ pub fn instantiate(data: &[u8], bin_name: &str, workspace: Option<&Path>) -> any
.context("failed to instantiate wasi")?,
);

// ... and then do the same as above but for the old snapshot of wasi, since
// a few tests still test that
let mut builder = wasi_common::old::snapshot_0::WasiCtxBuilder::new()
.arg(bin_name)
.arg(".")
.inherit_stdio();
for (dir, file) in get_preopens(workspace)? {
builder = builder.preopened_dir(file, dir);
}
let (reader, _writer) = os_pipe::pipe()?;
builder = builder.stdin(reader_to_file(reader));
let snapshot0 = Instance::from_handle(
&store,
wasmtime_wasi::old::snapshot_0::instantiate_wasi_with_context(
global_exports.clone(),
builder.build().context("failed to build wasi context")?,
)
.context("failed to instantiate wasi")?,
);

let module = HostRef::new(Module::new(&store, &data).context("failed to create wasm module")?);
let imports = module
.borrow()
.imports()
.iter()
.map(|i| {
let instance = if i.module() == "wasi_unstable" {
&snapshot0
} else if i.module() == "wasi_snapshot_preview1" {
&snapshot1
} else {
bail!("import module {} was not found", i.module())
};
let field_name = i.name();
if let Some(export) = instance.find_export_by_name(field_name) {
if let Some(export) = snapshot1.find_export_by_name(field_name) {
Ok(export.clone())
} else {
bail!(
Expand Down
1 change: 0 additions & 1 deletion crates/test-programs/wasi-tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ publish = false
[dependencies]
libc = "0.2.65"
wasi = "0.9.0"
wasi-old = { version = "0.7.0", package = "wasi" }
more-asserts = "0.2.1"

# This crate is built with the wasm32-wasi target, so it's separate
Expand Down
22 changes: 14 additions & 8 deletions crates/test-programs/wasi-tests/src/bin/close_preopen.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use more_asserts::assert_gt;
use std::{env, process};
use wasi_tests::open_scratch_directory_new;
use wasi_tests::open_scratch_directory;

unsafe fn test_close_preopen(dir_fd: wasi::Fd) {
let pre_fd: wasi::Fd = (libc::STDERR_FILENO + 1) as wasi::Fd;
Expand All @@ -9,16 +9,20 @@ unsafe fn test_close_preopen(dir_fd: wasi::Fd) {

// Try to close a preopened directory handle.
assert_eq!(
wasi::fd_close(pre_fd).unwrap_err().raw_error(),
wasi::fd_close(pre_fd)
.expect_err("closing a preopened file descriptor")
.raw_error(),
wasi::ERRNO_NOTSUP,
"closing a preopened file descriptor",
"errno should ERRNO_NOTSUP",
);

// Try to renumber over a preopened directory handle.
assert_eq!(
wasi::fd_renumber(dir_fd, pre_fd).unwrap_err().raw_error(),
wasi::fd_renumber(dir_fd, pre_fd)
.expect_err("renumbering over a preopened file descriptor")
.raw_error(),
wasi::ERRNO_NOTSUP,
"renumbering over a preopened file descriptor",
"errno should be ERRNO_NOTSUP",
);

// Ensure that dir_fd is still open.
Expand All @@ -31,9 +35,11 @@ unsafe fn test_close_preopen(dir_fd: wasi::Fd) {

// Try to renumber a preopened directory handle.
assert_eq!(
wasi::fd_renumber(pre_fd, dir_fd).unwrap_err().raw_error(),
wasi::fd_renumber(pre_fd, dir_fd)
.expect_err("renumbering over a preopened file descriptor")
.raw_error(),
wasi::ERRNO_NOTSUP,
"renumbering over a preopened file descriptor",
"errno should be ERRNO_NOTSUP",
);

// Ensure that dir_fd is still open.
Expand All @@ -56,7 +62,7 @@ fn main() {
};

// Open scratch directory
let dir_fd = match open_scratch_directory_new(&arg) {
let dir_fd = match open_scratch_directory(&arg) {
Ok(dir_fd) => dir_fd,
Err(err) => {
eprintln!("{}", err);
Expand Down
4 changes: 2 additions & 2 deletions crates/test-programs/wasi-tests/src/bin/dangling_fd.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use more_asserts::assert_gt;
use std::{env, process};
use wasi_tests::open_scratch_directory_new;
use wasi_tests::open_scratch_directory;

unsafe fn test_dangling_fd(dir_fd: wasi::Fd) {
// Create a file, open it, delete it without closing the handle,
Expand Down Expand Up @@ -41,7 +41,7 @@ fn main() {
};

// Open scratch directory
let dir_fd = match open_scratch_directory_new(&arg) {
let dir_fd = match open_scratch_directory(&arg) {
Ok(dir_fd) => dir_fd,
Err(err) => {
eprintln!("{}", err);
Expand Down
18 changes: 7 additions & 11 deletions crates/test-programs/wasi-tests/src/bin/dangling_symlink.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,17 @@
use std::{env, process};
use wasi_tests::open_scratch_directory_new;
use wasi_tests::open_scratch_directory;

unsafe fn test_dangling_symlink(dir_fd: wasi::Fd) {
// First create a dangling symlink.
assert!(
wasi::path_symlink("target", dir_fd, "symlink").is_ok(),
"creating a symlink"
);
wasi::path_symlink("target", dir_fd, "symlink").expect("creating a symlink");

// Try to open it as a directory with O_NOFOLLOW.
let status = wasi::path_open(dir_fd, 0, "symlink", wasi::OFLAGS_DIRECTORY, 0, 0, 0)
.err()
.expect("failed to open symlink");
assert_eq!(
status.raw_error(),
wasi::path_open(dir_fd, 0, "symlink", wasi::OFLAGS_DIRECTORY, 0, 0, 0)
.expect_err("opening a dangling symlink as a directory")
.raw_error(),
wasi::ERRNO_LOOP,
"opening a dangling symlink as a directory",
"errno should be ERRNO_LOOP",
);

// Clean up.
Expand All @@ -33,7 +29,7 @@ fn main() {
};

// Open scratch directory
let dir_fd = match open_scratch_directory_new(&arg) {
let dir_fd = match open_scratch_directory(&arg) {
Ok(dir_fd) => dir_fd,
Err(err) => {
eprintln!("{}", err);
Expand Down
13 changes: 6 additions & 7 deletions crates/test-programs/wasi-tests/src/bin/directory_seek.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use more_asserts::assert_gt;
use std::{env, process};
use wasi_tests::open_scratch_directory_new;
use wasi_tests::open_scratch_directory;

unsafe fn test_directory_seek(dir_fd: wasi::Fd) {
// Create a directory in the scratch directory.
Expand All @@ -16,13 +16,12 @@ unsafe fn test_directory_seek(dir_fd: wasi::Fd) {
);

// Attempt to seek.
let status = wasi::fd_seek(fd, 0, wasi::WHENCE_CUR)
.err()
.expect("failed to seek");
assert_eq!(
status.raw_error(),
wasi::fd_seek(fd, 0, wasi::WHENCE_CUR)
.expect_err("seek on a directory")
.raw_error(),
wasi::ERRNO_NOTCAPABLE,
"seek on a directory"
"errno should be ERRNO_NOTCAPABLE"
);

// Check if we obtained the right to seek.
Expand Down Expand Up @@ -54,7 +53,7 @@ fn main() {
};

// Open scratch directory
let dir_fd = match open_scratch_directory_new(&arg) {
let dir_fd = match open_scratch_directory(&arg) {
Ok(dir_fd) => dir_fd,
Err(err) => {
eprintln!("{}", err);
Expand Down
10 changes: 3 additions & 7 deletions crates/test-programs/wasi-tests/src/bin/fd_advise.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use libc;
use more_asserts::assert_gt;
use std::{env, process};
use wasi_tests::open_scratch_directory_new;
use wasi_tests::open_scratch_directory;

unsafe fn test_fd_advise(dir_fd: wasi::Fd) {
// Create a file in the scratch directory.
Expand All @@ -26,10 +25,7 @@ unsafe fn test_fd_advise(dir_fd: wasi::Fd) {
assert_eq!(stat.size, 0, "file size should be 0");

// Allocate some size
assert!(
wasi::fd_allocate(file_fd, 0, 100).is_ok(),
"allocating size"
);
wasi::fd_allocate(file_fd, 0, 100).expect("allocating size");

let stat = wasi::fd_filestat_get(file_fd).expect("failed to fdstat 2");
assert_eq!(stat.size, 100, "file size should be 100");
Expand All @@ -51,7 +47,7 @@ fn main() {
};

// Open scratch directory
let dir_fd = match open_scratch_directory_new(&arg) {
let dir_fd = match open_scratch_directory(&arg) {
Ok(dir_fd) => dir_fd,
Err(err) => {
eprintln!("{}", err);
Expand Down
29 changes: 8 additions & 21 deletions crates/test-programs/wasi-tests/src/bin/fd_filestat_set.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use more_asserts::assert_gt;
use std::{env, process};
use wasi_tests::open_scratch_directory_new;
use wasi_tests::open_scratch_directory;

unsafe fn test_fd_filestat_set(dir_fd: wasi::Fd) {
// Create a file in the scratch directory.
Expand All @@ -12,7 +12,8 @@ unsafe fn test_fd_filestat_set(dir_fd: wasi::Fd) {
wasi::RIGHTS_FD_READ | wasi::RIGHTS_FD_WRITE,
0,
0,
).expect("failed to create file");
)
.expect("failed to create file");
assert_gt!(
file_fd,
libc::STDERR_FILENO as wasi::Fd,
Expand All @@ -24,33 +25,19 @@ unsafe fn test_fd_filestat_set(dir_fd: wasi::Fd) {
assert_eq!(stat.size, 0, "file size should be 0");

// Check fd_filestat_set_size
assert!(
wasi::fd_filestat_set_size(file_fd, 100).is_ok(),
"fd_filestat_set_size"
);
wasi::fd_filestat_set_size(file_fd, 100).expect("fd_filestat_set_size");

let stat = wasi::fd_filestat_get(file_fd).expect("failed filestat 2");
assert_eq!(stat.size, 100, "file size should be 100");

// Check fd_filestat_set_times
let old_atim = stat.atim;
let new_mtim = stat.mtim - 100;
assert!(
wasi::fd_filestat_set_times(
file_fd,
new_mtim,
new_mtim,
wasi::FSTFLAGS_MTIM,
)
.is_ok(),
"fd_filestat_set_times"
);
wasi::fd_filestat_set_times(file_fd, new_mtim, new_mtim, wasi::FSTFLAGS_MTIM)
.expect("fd_filestat_set_times");

let stat = wasi::fd_filestat_get(file_fd).expect("failed filestat 3");
assert_eq!(
stat.size, 100,
"file size should remain unchanged at 100"
);
assert_eq!(stat.size, 100, "file size should remain unchanged at 100");
assert_eq!(stat.mtim, new_mtim, "mtim should change");
assert_eq!(stat.atim, old_atim, "atim should not change");

Expand All @@ -71,7 +58,7 @@ fn main() {
};

// Open scratch directory
let dir_fd = match open_scratch_directory_new(&arg) {
let dir_fd = match open_scratch_directory(&arg) {
Ok(dir_fd) => dir_fd,
Err(err) => {
eprintln!("{}", err);
Expand Down
27 changes: 9 additions & 18 deletions crates/test-programs/wasi-tests/src/bin/fd_readdir.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use more_asserts::assert_gt;
use std::{cmp::min, env, mem, process, slice, str};
use wasi_tests::open_scratch_directory_new;
use wasi_tests::open_scratch_directory;

const BUF_LEN: usize = 256;

Expand Down Expand Up @@ -48,12 +48,10 @@ impl<'a> Iterator for ReadDir<'a> {
}
}

unsafe fn exec_fd_readdir(
fd: wasi::Fd,
cookie: wasi::Dircookie,
) -> Vec<DirEntry> {
unsafe fn exec_fd_readdir(fd: wasi::Fd, cookie: wasi::Dircookie) -> Vec<DirEntry> {
let mut buf: [u8; BUF_LEN] = [0; BUF_LEN];
let bufused = wasi::fd_readdir(fd, buf.as_mut_ptr(), BUF_LEN, cookie).expect("failed fd_readdir");
let bufused =
wasi::fd_readdir(fd, buf.as_mut_ptr(), BUF_LEN, cookie).expect("failed fd_readdir");

let sl = slice::from_raw_parts(buf.as_ptr(), min(BUF_LEN, bufused));
let dirs: Vec<_> = ReadDir::from_slice(sl).collect();
Expand All @@ -72,22 +70,14 @@ unsafe fn test_fd_readdir(dir_fd: wasi::Fd) {
// the first entry should be `.`
let dir = dirs.next().expect("first entry is None");
assert_eq!(dir.name, ".", "first name");
assert_eq!(
dir.dirent.d_type,
wasi::FILETYPE_DIRECTORY,
"first type"
);
assert_eq!(dir.dirent.d_type, wasi::FILETYPE_DIRECTORY, "first type");
assert_eq!(dir.dirent.d_ino, stat.ino);
assert_eq!(dir.dirent.d_namlen, 1);

// the second entry should be `..`
let dir = dirs.next().expect("second entry is None");
assert_eq!(dir.name, "..", "second name");
assert_eq!(
dir.dirent.d_type,
wasi::FILETYPE_DIRECTORY,
"second type"
);
assert_eq!(dir.dirent.d_type, wasi::FILETYPE_DIRECTORY, "second type");

assert!(
dirs.next().is_none(),
Expand All @@ -103,7 +93,8 @@ unsafe fn test_fd_readdir(dir_fd: wasi::Fd) {
wasi::RIGHTS_FD_READ | wasi::RIGHTS_FD_WRITE,
0,
0,
).expect("failed to create file");
)
.expect("failed to create file");
assert_gt!(
file_fd,
libc::STDERR_FILENO as wasi::Fd,
Expand Down Expand Up @@ -152,7 +143,7 @@ fn main() {
};

// Open scratch directory
let dir_fd = match open_scratch_directory_new(&arg) {
let dir_fd = match open_scratch_directory(&arg) {
Ok(dir_fd) => dir_fd,
Err(err) => {
eprintln!("{}", err);
Expand Down
Loading

0 comments on commit 51f3ac0

Please sign in to comment.