Browse Source

Update WASI tests to use wasi crate v0.9.0 (#743)

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.
pull/752/head
Jakub Konka 5 years ago
committed by Dan Gohman
parent
commit
51f3ac0c45
  1. 29
      crates/test-programs/tests/wasm_tests/runtime.rs
  2. 1
      crates/test-programs/wasi-tests/Cargo.toml
  3. 22
      crates/test-programs/wasi-tests/src/bin/close_preopen.rs
  4. 4
      crates/test-programs/wasi-tests/src/bin/dangling_fd.rs
  5. 18
      crates/test-programs/wasi-tests/src/bin/dangling_symlink.rs
  6. 13
      crates/test-programs/wasi-tests/src/bin/directory_seek.rs
  7. 10
      crates/test-programs/wasi-tests/src/bin/fd_advise.rs
  8. 29
      crates/test-programs/wasi-tests/src/bin/fd_filestat_set.rs
  9. 27
      crates/test-programs/wasi-tests/src/bin/fd_readdir.rs
  10. 97
      crates/test-programs/wasi-tests/src/bin/file_allocate.rs
  11. 89
      crates/test-programs/wasi-tests/src/bin/file_pread_pwrite.rs
  12. 87
      crates/test-programs/wasi-tests/src/bin/file_seek_tell.rs
  13. 78
      crates/test-programs/wasi-tests/src/bin/file_unbuffered_write.rs
  14. 144
      crates/test-programs/wasi-tests/src/bin/interesting_paths.rs
  15. 30
      crates/test-programs/wasi-tests/src/bin/isatty.rs
  16. 159
      crates/test-programs/wasi-tests/src/bin/nofollow_errors.rs
  17. 167
      crates/test-programs/wasi-tests/src/bin/path_filestat.rs
  18. 306
      crates/test-programs/wasi-tests/src/bin/path_link.rs
  19. 54
      crates/test-programs/wasi-tests/src/bin/path_open_create_existing.rs
  20. 45
      crates/test-programs/wasi-tests/src/bin/path_open_dirfd_not_dir.rs
  21. 226
      crates/test-programs/wasi-tests/src/bin/path_rename.rs
  22. 59
      crates/test-programs/wasi-tests/src/bin/path_rename_trailing_slashes.rs
  23. 69
      crates/test-programs/wasi-tests/src/bin/path_symlink_trailing_slashes.rs
  24. 189
      crates/test-programs/wasi-tests/src/bin/poll_oneoff.rs
  25. 34
      crates/test-programs/wasi-tests/src/bin/readlink.rs
  26. 22
      crates/test-programs/wasi-tests/src/bin/readlink_no_buffer.rs
  27. 43
      crates/test-programs/wasi-tests/src/bin/remove_directory_trailing_slashes.rs
  28. 25
      crates/test-programs/wasi-tests/src/bin/remove_nonempty_directory.rs
  29. 79
      crates/test-programs/wasi-tests/src/bin/renumber.rs
  30. 8
      crates/test-programs/wasi-tests/src/bin/sched_yield.rs
  31. 24
      crates/test-programs/wasi-tests/src/bin/stdio.rs
  32. 21
      crates/test-programs/wasi-tests/src/bin/symlink_loop.rs
  33. 119
      crates/test-programs/wasi-tests/src/bin/truncation_rights.rs
  34. 42
      crates/test-programs/wasi-tests/src/bin/unlink_file_trailing_slashes.rs
  35. 52
      crates/test-programs/wasi-tests/src/lib.rs
  36. 54
      crates/test-programs/wasi-tests/src/utils.rs
  37. 233
      crates/test-programs/wasi-tests/src/wasi_wrappers.rs

29
crates/test-programs/tests/wasm_tests/runtime.rs

@ -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!(

1
crates/test-programs/wasi-tests/Cargo.toml

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

22
crates/test-programs/wasi-tests/src/bin/close_preopen.rs

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

4
crates/test-programs/wasi-tests/src/bin/dangling_fd.rs

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

18
crates/test-programs/wasi-tests/src/bin/dangling_symlink.rs

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

13
crates/test-programs/wasi-tests/src/bin/directory_seek.rs

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

10
crates/test-programs/wasi-tests/src/bin/fd_advise.rs

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

29
crates/test-programs/wasi-tests/src/bin/fd_filestat_set.rs

@ -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.
@ -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,
@ -24,10 +25,7 @@ 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");
@ -35,22 +33,11 @@ unsafe fn test_fd_filestat_set(dir_fd: wasi::Fd) {
// 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");
@ -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);

27
crates/test-programs/wasi-tests/src/bin/fd_readdir.rs

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

97
crates/test-programs/wasi-tests/src/bin/file_allocate.rs

@ -1,103 +1,46 @@
use more_asserts::assert_gt;
use std::{env, process};
use wasi_old::wasi_unstable;
use wasi_tests::open_scratch_directory;
use wasi_tests::utils::{cleanup_file, close_fd};
use wasi_tests::wasi_wrappers::{wasi_fd_filestat_get, wasi_path_open};
unsafe fn test_file_allocate(dir_fd: wasi_unstable::Fd) {
unsafe fn test_file_allocate(dir_fd: wasi::Fd) {
// Create a file in the scratch directory.
let mut file_fd = wasi_unstable::Fd::max_value() - 1;
let status = wasi_path_open(
let file_fd = wasi::path_open(
dir_fd,
0,
"file",
wasi_unstable::O_CREAT,
wasi_unstable::RIGHT_FD_READ | wasi_unstable::RIGHT_FD_WRITE,
wasi::OFLAGS_CREAT,
wasi::RIGHTS_FD_READ | wasi::RIGHTS_FD_WRITE,
0,
0,
&mut file_fd,
);
assert_eq!(
status,
wasi_unstable::raw::__WASI_ESUCCESS,
"opening a file"
);
)
.expect("opening a file");
assert_gt!(
file_fd,
libc::STDERR_FILENO as wasi_unstable::Fd,
libc::STDERR_FILENO as wasi::Fd,
"file descriptor range check",
);
// Check file size
let mut stat = wasi_unstable::FileStat {
st_dev: 0,
st_ino: 0,
st_filetype: 0,
st_nlink: 0,
st_size: 0,
st_atim: 0,
st_mtim: 0,
st_ctim: 0,
};
let status = wasi_fd_filestat_get(file_fd, &mut stat);
assert_eq!(
status,
wasi_unstable::raw::__WASI_ESUCCESS,
"reading file stats"
);
assert_eq!(stat.st_size, 0, "file size should be 0");
let mut stat = wasi::fd_filestat_get(file_fd).expect("reading file stats");
assert_eq!(stat.size, 0, "file size should be 0");
// Allocate some size
assert!(
wasi_unstable::fd_allocate(file_fd, 0, 100).is_ok(),
"allocating size"
);
let status = wasi_fd_filestat_get(file_fd, &mut stat);
assert_eq!(
status,
wasi_unstable::raw::__WASI_ESUCCESS,
"reading file stats after initial allocation"
);
assert_eq!(stat.st_size, 100, "file size should be 100");
wasi::fd_allocate(file_fd, 0, 100).expect("allocating size");
stat = wasi::fd_filestat_get(file_fd).expect("reading file stats");
assert_eq!(stat.size, 100, "file size should be 100");
// Allocate should not modify if less than current size
assert!(
wasi_unstable::fd_allocate(file_fd, 10, 10).is_ok(),
"allocating size less than current size"
);
let status = wasi_fd_filestat_get(file_fd, &mut stat);
assert_eq!(
status,
wasi_unstable::raw::__WASI_ESUCCESS,
"reading file stats after additional allocation was not required"
);
assert_eq!(
stat.st_size, 100,
"file size should remain unchanged at 100"
);
wasi::fd_allocate(file_fd, 10, 10).expect("allocating size less than current size");
stat = wasi::fd_filestat_get(file_fd).expect("reading file stats");
assert_eq!(stat.size, 100, "file size should remain unchanged at 100");
// Allocate should modify if offset+len > current_len
assert!(
wasi_unstable::fd_allocate(file_fd, 90, 20).is_ok(),
"allocating size larger than current size"
);
let status = wasi_fd_filestat_get(file_fd, &mut stat);
assert_eq!(
status,
wasi_unstable::raw::__WASI_ESUCCESS,
"reading file stats after additional allocation was required"
);
assert_eq!(
stat.st_size, 110,
"file size should increase from 100 to 110"
);
wasi::fd_allocate(file_fd, 90, 20).expect("allocating size larger than current size");
stat = wasi::fd_filestat_get(file_fd).expect("reading file stats");
assert_eq!(stat.size, 110, "file size should increase from 100 to 110");
close_fd(file_fd);
cleanup_file(dir_fd, "file");
wasi::fd_close(file_fd).expect("closing a file");
wasi::path_unlink_file(dir_fd, "file").expect("removing a file");
}
fn main() {

89
crates/test-programs/wasi-tests/src/bin/file_pread_pwrite.rs

@ -1,110 +1,71 @@
use libc;
use more_asserts::assert_gt;
use std::{env, process};
use wasi_old::wasi_unstable;
use wasi_tests::open_scratch_directory;
use wasi_tests::utils::{cleanup_file, close_fd};
use wasi_tests::wasi_wrappers::{wasi_fd_pread, wasi_fd_pwrite, wasi_path_open};
unsafe fn test_file_pread_pwrite(dir_fd: wasi_unstable::Fd) {
unsafe fn test_file_pread_pwrite(dir_fd: wasi::Fd) {
// Create a file in the scratch directory.
let mut file_fd = wasi_unstable::Fd::max_value() - 1;
let mut status = wasi_path_open(
let file_fd = wasi::path_open(
dir_fd,
0,
"file",
wasi_unstable::O_CREAT,
wasi_unstable::RIGHT_FD_READ | wasi_unstable::RIGHT_FD_SEEK | wasi_unstable::RIGHT_FD_WRITE,
wasi::OFLAGS_CREAT,
wasi::RIGHTS_FD_READ | wasi::RIGHTS_FD_SEEK | wasi::RIGHTS_FD_WRITE,
0,
0,
&mut file_fd,
);
assert_eq!(
status,
wasi_unstable::raw::__WASI_ESUCCESS,
"opening a file"
);
)
.expect("opening a file");
assert_gt!(
file_fd,
libc::STDERR_FILENO as wasi_unstable::Fd,
libc::STDERR_FILENO as wasi::Fd,
"file descriptor range check",
);
let contents = &[0u8, 1, 2, 3];
let ciovec = wasi_unstable::CIoVec {
buf: contents.as_ptr() as *const libc::c_void,
let ciovec = wasi::Ciovec {
buf: contents.as_ptr() as *const _,
buf_len: contents.len(),
};
let mut nwritten = 0;
status = wasi_fd_pwrite(file_fd, &mut [ciovec], 0, &mut nwritten);
assert_eq!(
status,
wasi_unstable::raw::__WASI_ESUCCESS,
"writing bytes at offset 0"
);
let mut nwritten =
wasi::fd_pwrite(file_fd, &mut [ciovec], 0).expect("writing bytes at offset 0");
assert_eq!(nwritten, 4, "nwritten bytes check");
let contents = &mut [0u8; 4];
let iovec = wasi_unstable::IoVec {
buf: contents.as_mut_ptr() as *mut libc::c_void,
let iovec = wasi::Iovec {
buf: contents.as_mut_ptr() as *mut _,
buf_len: contents.len(),
};
let mut nread = 0;
status = wasi_fd_pread(file_fd, &[iovec], 0, &mut nread);
assert_eq!(
status,
wasi_unstable::raw::__WASI_ESUCCESS,
"reading bytes at offset 0"
);
let mut nread = wasi::fd_pread(file_fd, &[iovec], 0).expect("reading bytes at offset 0");
assert_eq!(nread, 4, "nread bytes check");
assert_eq!(contents, &[0u8, 1, 2, 3], "written bytes equal read bytes");
let contents = &mut [0u8; 4];
let iovec = wasi_unstable::IoVec {
buf: contents.as_mut_ptr() as *mut libc::c_void,
let iovec = wasi::Iovec {
buf: contents.as_mut_ptr() as *mut _,
buf_len: contents.len(),
};
let mut nread = 0;
status = wasi_fd_pread(file_fd, &[iovec], 2, &mut nread);
assert_eq!(
status,
wasi_unstable::raw::__WASI_ESUCCESS,
"reading bytes at offset 2"
);
nread = wasi::fd_pread(file_fd, &[iovec], 2).expect("reading bytes at offset 2");
assert_eq!(nread, 2, "nread bytes check");
assert_eq!(contents, &[2u8, 3, 0, 0], "file cursor was overwritten");
let contents = &[1u8, 0];
let ciovec = wasi_unstable::CIoVec {
buf: contents.as_ptr() as *const libc::c_void,
let ciovec = wasi::Ciovec {
buf: contents.as_ptr() as *const _,
buf_len: contents.len(),
};
let mut nwritten = 0;
status = wasi_fd_pwrite(file_fd, &mut [ciovec], 2, &mut nwritten);
assert_eq!(
status,
wasi_unstable::raw::__WASI_ESUCCESS,
"writing bytes at offset 2"
);
nwritten = wasi::fd_pwrite(file_fd, &mut [ciovec], 2).expect("writing bytes at offset 2");
assert_eq!(nwritten, 2, "nwritten bytes check");
let contents = &mut [0u8; 4];
let iovec = wasi_unstable::IoVec {
buf: contents.as_mut_ptr() as *mut libc::c_void,
let iovec = wasi::Iovec {
buf: contents.as_mut_ptr() as *mut _,
buf_len: contents.len(),
};
let mut nread = 0;
status = wasi_fd_pread(file_fd, &[iovec], 0, &mut nread);
assert_eq!(
status,
wasi_unstable::raw::__WASI_ESUCCESS,
"reading bytes at offset 0"
);
nread = wasi::fd_pread(file_fd, &[iovec], 0).expect("reading bytes at offset 0");
assert_eq!(nread, 4, "nread bytes check");
assert_eq!(contents, &[0u8, 1, 1, 0], "file cursor was overwritten");
close_fd(file_fd);
cleanup_file(dir_fd, "file");
wasi::fd_close(file_fd).expect("closing a file");
wasi::path_unlink_file(dir_fd, "file").expect("removing a file");
}
fn main() {

87
crates/test-programs/wasi-tests/src/bin/file_seek_tell.rs

@ -1,113 +1,72 @@
use libc;
use more_asserts::assert_gt;
use std::{env, process};
use wasi_old::wasi_unstable;
use wasi_tests::open_scratch_directory;
use wasi_tests::utils::{cleanup_file, close_fd};
use wasi_tests::wasi_wrappers::{wasi_fd_seek, wasi_fd_tell, wasi_fd_write, wasi_path_open};
unsafe fn test_file_seek_tell(dir_fd: wasi_unstable::Fd) {
unsafe fn test_file_seek_tell(dir_fd: wasi::Fd) {
// Create a file in the scratch directory.
let mut file_fd = wasi_unstable::Fd::max_value() - 1;
let mut status = wasi_path_open(
let file_fd = wasi::path_open(
dir_fd,
0,
"file",
wasi_unstable::O_CREAT,
wasi_unstable::RIGHT_FD_READ | wasi_unstable::RIGHT_FD_WRITE,
wasi::OFLAGS_CREAT,
wasi::RIGHTS_FD_READ | wasi::RIGHTS_FD_WRITE,
0,
0,
&mut file_fd,
);
assert_eq!(
status,
wasi_unstable::raw::__WASI_ESUCCESS,
"opening a file"
);
)
.expect("opening a file");
assert_gt!(
file_fd,
libc::STDERR_FILENO as wasi_unstable::Fd,
libc::STDERR_FILENO as wasi::Fd,
"file descriptor range check",
);
// Check current offset
let mut offset: wasi_unstable::FileSize = 0;
status = wasi_fd_tell(file_fd, &mut offset);
assert_eq!(
status,
wasi_unstable::raw::__WASI_ESUCCESS,
"getting initial file offset"
);
let mut offset = wasi::fd_tell(file_fd).expect("getting initial file offset");
assert_eq!(offset, 0, "current offset should be 0");
// Write to file
let buf = &[0u8; 100];
let iov = wasi_unstable::CIoVec {
let iov = wasi::Ciovec {
buf: buf.as_ptr() as *const _,
buf_len: buf.len(),
};
let iovs = &[iov];
let mut nwritten = 0;
status = wasi_fd_write(file_fd, iovs, &mut nwritten);
assert_eq!(
status,
wasi_unstable::raw::__WASI_ESUCCESS,
"writing to a file"
);
let nwritten = wasi::fd_write(file_fd, &[iov]).expect("writing to a file");
assert_eq!(nwritten, 100, "should write 100 bytes to file");
// Check current offset
status = wasi_fd_tell(file_fd, &mut offset);
assert_eq!(
status,
wasi_unstable::raw::__WASI_ESUCCESS,
"getting file offset after writing"
);
offset = wasi::fd_tell(file_fd).expect("getting file offset after writing");
assert_eq!(offset, 100, "offset after writing should be 100");
// Seek to middle of the file
let mut newoffset = 1;
status = wasi_fd_seek(file_fd, -50, wasi_unstable::WHENCE_CUR, &mut newoffset);
assert_eq!(
status,
wasi_unstable::raw::__WASI_ESUCCESS,
"seeking to the middle of a file"
);
let mut newoffset =
wasi::fd_seek(file_fd, -50, wasi::WHENCE_CUR).expect("seeking to the middle of a file");
assert_eq!(
newoffset, 50,
"offset after seeking to the middle should be at 50"
);
// Seek to the beginning of the file
status = wasi_fd_seek(file_fd, 0, wasi_unstable::WHENCE_SET, &mut newoffset);
assert_eq!(
status,
wasi_unstable::raw::__WASI_ESUCCESS,
"seeking to the beginning of the file"
);
newoffset =
wasi::fd_seek(file_fd, 0, wasi::WHENCE_SET).expect("seeking to the beginning of the file");
assert_eq!(
newoffset, 0,
"offset after seeking to the beginning of the file should be at 0"
);
// Seek beyond the file should be possible
status = wasi_fd_seek(file_fd, 1000, wasi_unstable::WHENCE_CUR, &mut newoffset);
assert_eq!(
status,
wasi_unstable::raw::__WASI_ESUCCESS,
"seeking beyond the end of the file"
);
wasi::fd_seek(file_fd, 1000, wasi::WHENCE_CUR).expect("seeking beyond the end of the file");
// Seek before byte 0 is an error though
status = wasi_fd_seek(file_fd, -2000, wasi_unstable::WHENCE_CUR, &mut newoffset);
assert_eq!(
status,
wasi_unstable::raw::__WASI_EINVAL,
"seeking before byte 0 is an error"
wasi::fd_seek(file_fd, -2000, wasi::WHENCE_CUR)
.expect_err("seeking before byte 0 should be an error")
.raw_error(),
wasi::ERRNO_INVAL,
"errno should be ERRNO_INVAL",
);
close_fd(file_fd);
cleanup_file(dir_fd, "file");
wasi::fd_close(file_fd).expect("closing a file");
wasi::path_unlink_file(dir_fd, "file").expect("deleting a file");
}
fn main() {
let mut args = env::args();

78
crates/test-programs/wasi-tests/src/bin/file_unbuffered_write.rs

@ -1,95 +1,57 @@
use more_asserts::assert_gt;
use std::{env, process};
use wasi_old::wasi_unstable;
use wasi_tests::open_scratch_directory;
use wasi_tests::utils::{cleanup_file, close_fd, create_file};
use wasi_tests::wasi_wrappers::{wasi_fd_read, wasi_fd_write, wasi_path_open};
unsafe fn test_file_unbuffered_write(dir_fd: wasi_unstable::Fd) {
// Create file
create_file(dir_fd, "file");
// Open file for reading
let mut fd_read = wasi_unstable::Fd::max_value() - 1;
let mut status = wasi_path_open(
unsafe fn test_file_unbuffered_write(dir_fd: wasi::Fd) {
// Create and open file for reading
let fd_read = wasi::path_open(
dir_fd,
0,
"file",
0,
wasi_unstable::RIGHT_FD_READ,
wasi::OFLAGS_CREAT,
wasi::RIGHTS_FD_READ,
0,
0,
&mut fd_read,
);
assert_eq!(
status,
wasi_unstable::raw::__WASI_ESUCCESS,
"opening a file"
);
)
.expect("create and open file for reading");
assert_gt!(
fd_read,
libc::STDERR_FILENO as wasi_unstable::Fd,
libc::STDERR_FILENO as wasi::Fd,
"file descriptor range check",
);
// Open the same file but for writing
let mut fd_write = wasi_unstable::Fd::max_value() - 1;
status = wasi_path_open(
dir_fd,
0,
"file",
0,
wasi_unstable::RIGHT_FD_WRITE,
0,
0,
&mut fd_write,
);
assert_eq!(
status,
wasi_unstable::raw::__WASI_ESUCCESS,
"opening a file"
);
let fd_write = wasi::path_open(dir_fd, 0, "file", 0, wasi::RIGHTS_FD_WRITE, 0, 0)
.expect("opening file for writing");
assert_gt!(
fd_write,
libc::STDERR_FILENO as wasi_unstable::Fd,
libc::STDERR_FILENO as wasi::Fd,
"file descriptor range check",
);
// Write to file
let contents = &[1u8];
let ciovec = wasi_unstable::CIoVec {
buf: contents.as_ptr() as *const libc::c_void,
let ciovec = wasi::Ciovec {
buf: contents.as_ptr() as *const _,
buf_len: contents.len(),
};
let mut nwritten = 0;
status = wasi_fd_write(fd_write, &[ciovec], &mut nwritten);
assert_eq!(
status,
wasi_unstable::raw::__WASI_ESUCCESS,
"writing byte to file"
);
let nwritten = wasi::fd_write(fd_write, &[ciovec]).expect("writing byte to file");
assert_eq!(nwritten, 1, "nwritten bytes check");
// Read from file
let contents = &mut [0u8; 1];
let iovec = wasi_unstable::IoVec {
buf: contents.as_mut_ptr() as *mut libc::c_void,
let iovec = wasi::Iovec {
buf: contents.as_mut_ptr() as *mut _,
buf_len: contents.len(),
};
let mut nread = 0;
status = wasi_fd_read(fd_read, &[iovec], &mut nread);
assert_eq!(
status,
wasi_unstable::raw::__WASI_ESUCCESS,
"reading bytes from file"
);
let nread = wasi::fd_read(fd_read, &[iovec]).expect("reading bytes from file");
assert_eq!(nread, 1, "nread bytes check");
assert_eq!(contents, &[1u8], "written bytes equal read bytes");
// Clean up
close_fd(fd_write);
close_fd(fd_read);
cleanup_file(dir_fd, "file");
wasi::fd_close(fd_write).expect("closing a file");
wasi::fd_close(fd_read).expect("closing a file");
wasi::path_unlink_file(dir_fd, "file").expect("removing a file");
}
fn main() {
let mut args = env::args();

144
crates/test-programs/wasi-tests/src/bin/interesting_paths.rs

@ -1,39 +1,28 @@
use libc;
use more_asserts::assert_gt;
use std::{env, process};
use wasi_old::wasi_unstable;
use wasi_tests::open_scratch_directory;
use wasi_tests::utils::{close_fd, create_dir, create_file};
use wasi_tests::wasi_wrappers::{
wasi_path_open, wasi_path_remove_directory, wasi_path_unlink_file,
};
use wasi_tests::{create_file, open_scratch_directory};
unsafe fn test_interesting_paths(dir_fd: wasi_unstable::Fd, arg: &str) {
unsafe fn test_interesting_paths(dir_fd: wasi::Fd, arg: &str) {
// Create a directory in the scratch directory.
create_dir(dir_fd, "dir");
wasi::path_create_directory(dir_fd, "dir").expect("creating dir");
// Create a directory in the directory we just created.
create_dir(dir_fd, "dir/nested");
wasi::path_create_directory(dir_fd, "dir/nested").expect("creating a nested dir");
// Create a file in the nested directory.
create_file(dir_fd, "dir/nested/file");
// Now open it with an absolute path.
let mut file_fd: wasi_unstable::Fd = wasi_unstable::Fd::max_value() - 1;
let mut status = wasi_path_open(dir_fd, 0, "/dir/nested/file", 0, 0, 0, 0, &mut file_fd);
assert_eq!(
status,
wasi_unstable::raw::__WASI_ENOTCAPABLE,
"opening a file with an absolute path"
);
assert_eq!(
file_fd,
wasi_unstable::Fd::max_value(),
"failed open should set the file descriptor to -1",
wasi::path_open(dir_fd, 0, "/dir/nested/file", 0, 0, 0, 0)
.expect_err("opening a file with an absolute path")
.raw_error(),
wasi::ERRNO_NOTCAPABLE,
"errno should be ERRNO_NOTCAPABLE"
);
// Now open it with a path containing "..".
status = wasi_path_open(
let mut file_fd = wasi::path_open(
dir_fd,
0,
"dir/.//nested/../../dir/nested/../nested///./file",
@ -41,112 +30,77 @@ unsafe fn test_interesting_paths(dir_fd: wasi_unstable::Fd, arg: &str) {
0,
0,
0,
&mut file_fd,
);
assert_eq!(
status,
wasi_unstable::raw::__WASI_ESUCCESS,
"opening a file with \"..\" in the path"
);
)
.expect("opening a file with \"..\" in the path");
assert_gt!(
file_fd,
libc::STDERR_FILENO as wasi_unstable::Fd,
libc::STDERR_FILENO as wasi::Fd,
"file descriptor range check",
);
close_fd(file_fd);
wasi::fd_close(file_fd).expect("closing a file");
// Now open it with a trailing NUL.
status = wasi_path_open(dir_fd, 0, "dir/nested/file\0", 0, 0, 0, 0, &mut file_fd);
assert_eq!(
status,
wasi_unstable::raw::__WASI_EILSEQ,
"opening a file with a trailing NUL"
);
assert_eq!(
file_fd,
wasi_unstable::Fd::max_value(),
"failed open should set the file descriptor to -1",
wasi::path_open(dir_fd, 0, "dir/nested/file\0", 0, 0, 0, 0)
.expect_err("opening a file with a trailing NUL")
.raw_error(),
wasi::ERRNO_ILSEQ,
"errno should be ERRNO_ILSEQ",
);
// Now open it with a trailing slash.
status = wasi_path_open(dir_fd, 0, "dir/nested/file/", 0, 0, 0, 0, &mut file_fd);
assert_eq!(
status,
wasi_unstable::raw::__WASI_ENOTDIR,
"opening a file with a trailing slash"
);
assert_eq!(
file_fd,
wasi_unstable::Fd::max_value(),
"failed open should set the file descriptor to -1",
wasi::path_open(dir_fd, 0, "dir/nested/file/", 0, 0, 0, 0)
.expect_err("opening a file with a trailing slash should fail")
.raw_error(),
wasi::ERRNO_NOTDIR,
"errno should be ERRNO_NOTDIR",
);
// Now open it with trailing slashes.
status = wasi_path_open(dir_fd, 0, "dir/nested/file///", 0, 0, 0, 0, &mut file_fd);
assert_eq!(
status,
wasi_unstable::raw::__WASI_ENOTDIR,
"opening a file with trailing slashes"
);
assert_eq!(
file_fd,
wasi_unstable::Fd::max_value(),
"failed open should set the file descriptor to -1",
wasi::path_open(dir_fd, 0, "dir/nested/file///", 0, 0, 0, 0)
.expect_err("opening a file with trailing slashes should fail")
.raw_error(),
wasi::ERRNO_NOTDIR,
"errno should be ERRNO_NOTDIR",
);
// Now open the directory with a trailing slash.
status = wasi_path_open(dir_fd, 0, "dir/nested/", 0, 0, 0, 0, &mut file_fd);
assert_eq!(
status,
wasi_unstable::raw::__WASI_ESUCCESS,
"opening a directory with a trailing slash"
);
file_fd = wasi::path_open(dir_fd, 0, "dir/nested/", 0, 0, 0, 0)
.expect("opening a directory with a trailing slash");
assert_gt!(
file_fd,
libc::STDERR_FILENO as wasi_unstable::Fd,
libc::STDERR_FILENO as wasi::Fd,
"file descriptor range check",
);
close_fd(file_fd);
wasi::fd_close(file_fd).expect("closing a file");
// Now open the directory with trailing slashes.
status = wasi_path_open(dir_fd, 0, "dir/nested///", 0, 0, 0, 0, &mut file_fd);
assert_eq!(
status,
wasi_unstable::raw::__WASI_ESUCCESS,
"opening a directory with trailing slashes"
);
file_fd = wasi::path_open(dir_fd, 0, "dir/nested///", 0, 0, 0, 0)
.expect("opening a directory with trailing slashes");
assert_gt!(
file_fd,
libc::STDERR_FILENO as wasi_unstable::Fd,
libc::STDERR_FILENO as wasi::Fd,
"file descriptor range check",
);
close_fd(file_fd);
wasi::fd_close(file_fd).expect("closing a file");
// Now open it with a path containing too many ".."s.
let bad_path = format!("dir/nested/../../../{}/dir/nested/file", arg);
status = wasi_path_open(dir_fd, 0, &bad_path, 0, 0, 0, 0, &mut file_fd);
assert_eq!(
status,
wasi_unstable::raw::__WASI_ENOTCAPABLE,
"opening a file with too many \"..\"s in the path"
);
assert_eq!(
file_fd,
wasi_unstable::Fd::max_value(),
"failed open should set the file descriptor to -1",
);
assert!(
wasi_path_unlink_file(dir_fd, "dir/nested/file").is_ok(),
"unlink_file on a symlink should succeed"
);
assert!(
wasi_path_remove_directory(dir_fd, "dir/nested").is_ok(),
"remove_directory on a directory should succeed"
);
assert!(
wasi_path_remove_directory(dir_fd, "dir").is_ok(),
"remove_directory on a directory should succeed"
);
wasi::path_open(dir_fd, 0, &bad_path, 0, 0, 0, 0)
.expect_err("opening a file with too many \"..\"s in the path should fail")
.raw_error(),
wasi::ERRNO_NOTCAPABLE,
"errno should be ERRNO_NOTCAPABLE",
);
wasi::path_unlink_file(dir_fd, "dir/nested/file")
.expect("unlink_file on a symlink should succeed");
wasi::path_remove_directory(dir_fd, "dir/nested")
.expect("remove_directory on a directory should succeed");
wasi::path_remove_directory(dir_fd, "dir")
.expect("remove_directory on a directory should succeed");
}
fn main() {

30
crates/test-programs/wasi-tests/src/bin/isatty.rs

@ -1,31 +1,14 @@
use more_asserts::assert_gt;
use std::{env, process};
use wasi_old::wasi_unstable;
use wasi_tests::open_scratch_directory;
use wasi_tests::utils::{cleanup_file, close_fd};
use wasi_tests::wasi_wrappers::wasi_path_open;
unsafe fn test_isatty(dir_fd: wasi_unstable::Fd) {
unsafe fn test_isatty(dir_fd: wasi::Fd) {
// Create a file in the scratch directory and test if it's a tty.
let mut file_fd: wasi_unstable::Fd = wasi_unstable::Fd::max_value() - 1;
let status = wasi_path_open(
dir_fd,
0,
"file",
wasi_unstable::O_CREAT,
0,
0,
0,
&mut file_fd,
);
assert_eq!(
status,
wasi_unstable::raw::__WASI_ESUCCESS,
"opening a file"
);
let file_fd =
wasi::path_open(dir_fd, 0, "file", wasi::OFLAGS_CREAT, 0, 0, 0).expect("opening a file");
assert_gt!(
file_fd,
libc::STDERR_FILENO as wasi_unstable::Fd,
libc::STDERR_FILENO as wasi::Fd,
"file descriptor range check",
);
assert_eq!(
@ -33,9 +16,8 @@ unsafe fn test_isatty(dir_fd: wasi_unstable::Fd) {
0,
"file is a tty"
);
close_fd(file_fd);
cleanup_file(dir_fd, "file");
wasi::fd_close(file_fd).expect("closing a file");
wasi::path_unlink_file(dir_fd, "file").expect("removing a file");
}
fn main() {

159
crates/test-programs/wasi-tests/src/bin/nofollow_errors.rs

@ -1,154 +1,99 @@
use libc;
use more_asserts::assert_gt;
use std::{env, process};
use wasi_old::wasi_unstable;
use wasi_tests::open_scratch_directory;
use wasi_tests::utils::{cleanup_file, close_fd, create_dir, create_file};
use wasi_tests::wasi_wrappers::{wasi_path_open, wasi_path_remove_directory, wasi_path_symlink};
unsafe fn test_nofollow_errors(dir_fd: wasi_unstable::Fd) {
unsafe fn test_nofollow_errors(dir_fd: wasi::Fd) {
// Create a directory for the symlink to point to.
create_dir(dir_fd, "target");
wasi::path_create_directory(dir_fd, "target").expect("creating a dir");
// Create a 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 again.
let mut file_fd: wasi_unstable::Fd = wasi_unstable::Fd::max_value() - 1;
let mut status = wasi_path_open(
dir_fd,
0,
"symlink",
wasi_unstable::O_DIRECTORY,
0,
0,
0,
&mut file_fd,
);
assert_eq!(
status,
wasi_unstable::raw::__WASI_ELOOP,
"opening a directory symlink as a directory",
);
assert_eq!(
file_fd,
wasi_unstable::Fd::max_value(),
"failed open should set the file descriptor to -1",
wasi::path_open(dir_fd, 0, "symlink", wasi::OFLAGS_DIRECTORY, 0, 0, 0)
.expect_err("opening a directory symlink as a directory should fail")
.raw_error(),
wasi::ERRNO_LOOP,
"errno should be ERRNO_LOOP",
);
// Try to open it with just O_NOFOLLOW.
status = wasi_path_open(dir_fd, 0, "symlink", 0, 0, 0, 0, &mut file_fd);
assert_eq!(
status,
wasi_unstable::raw::__WASI_ELOOP,
"opening a symlink with O_NOFOLLOW should return ELOOP",
);
assert_eq!(
file_fd,
wasi_unstable::Fd::max_value(),
"failed open should set the file descriptor to -1",
wasi::path_open(dir_fd, 0, "symlink", 0, 0, 0, 0)
.expect_err("opening a symlink with O_NOFOLLOW should fail")
.raw_error(),
wasi::ERRNO_LOOP,
"errno should be ERRNO_LOOP",
);
// Try to open it as a directory without O_NOFOLLOW.
status = wasi_path_open(
let file_fd = wasi::path_open(
dir_fd,
wasi_unstable::LOOKUP_SYMLINK_FOLLOW,
wasi::LOOKUPFLAGS_SYMLINK_FOLLOW,
"symlink",
wasi_unstable::O_DIRECTORY,
wasi::OFLAGS_DIRECTORY,
0,
0,
0,
&mut file_fd,
);
assert_eq!(
status,
wasi_unstable::raw::__WASI_ESUCCESS,
"opening a symlink as a directory"
);
)
.expect("opening a symlink as a directory");
assert_gt!(
file_fd,
libc::STDERR_FILENO as wasi_unstable::Fd,
libc::STDERR_FILENO as wasi::Fd,
"file descriptor range check",
);
close_fd(file_fd);
wasi::fd_close(file_fd).expect("closing a file");
// Replace the target directory with a file.
cleanup_file(dir_fd, "symlink");
wasi::path_unlink_file(dir_fd, "symlink").expect("removing a file");
wasi::path_remove_directory(dir_fd, "target")
.expect("remove_directory on a directory should succeed");
assert!(
wasi_path_remove_directory(dir_fd, "target").is_ok(),
"remove_directory on a directory should succeed"
);
create_file(dir_fd, "target");
assert!(
wasi_path_symlink("target", dir_fd, "symlink").is_ok(),
"creating a symlink"
);
let file_fd =
wasi::path_open(dir_fd, 0, "target", wasi::OFLAGS_CREAT, 0, 0, 0).expect("creating a file");
wasi::fd_close(file_fd).expect("closing a file");
wasi::path_symlink("target", dir_fd, "symlink").expect("creating a symlink");
// Try to open it as a directory with O_NOFOLLOW again.
status = wasi_path_open(
dir_fd,
0,
"symlink",
wasi_unstable::O_DIRECTORY,
0,
0,
0,
&mut file_fd,
);
assert_eq!(
status,
wasi_unstable::raw::__WASI_ELOOP,
"opening a directory symlink as a directory",
);
assert_eq!(
file_fd,
wasi_unstable::Fd::max_value(),
"failed open should set the file descriptor to -1",
wasi::path_open(dir_fd, 0, "symlink", wasi::OFLAGS_DIRECTORY, 0, 0, 0)
.expect_err("opening a directory symlink as a directory should fail")
.raw_error(),
wasi::ERRNO_LOOP,
"errno should be ERRNO_LOOP",
);
// Try to open it with just O_NOFOLLOW.
status = wasi_path_open(dir_fd, 0, "symlink", 0, 0, 0, 0, &mut file_fd);
assert_eq!(
status,
wasi_unstable::raw::__WASI_ELOOP,
"opening a symlink with O_NOFOLLOW should return ELOOP",
);
assert_eq!(
file_fd,
wasi_unstable::Fd::max_value(),
"failed open should set the file descriptor to -1",
wasi::path_open(dir_fd, 0, "symlink", 0, 0, 0, 0)
.expect_err("opening a symlink with NOFOLLOW should fail")
.raw_error(),
wasi::ERRNO_LOOP,
"errno should be ERRNO_LOOP",
);
// Try to open it as a directory without O_NOFOLLOW.
status = wasi_path_open(
dir_fd,
wasi_unstable::LOOKUP_SYMLINK_FOLLOW,
"symlink",
wasi_unstable::O_DIRECTORY,
0,
0,
0,
&mut file_fd,
);
assert_eq!(
status,
wasi_unstable::raw::__WASI_ENOTDIR,
"opening a symlink to a file as a directory",
);
assert_eq!(
file_fd,
wasi_unstable::Fd::max_value(),
"failed open should set the file descriptor to -1",
wasi::path_open(
dir_fd,
wasi::LOOKUPFLAGS_SYMLINK_FOLLOW,
"symlink",
wasi::OFLAGS_DIRECTORY,
0,
0,
0,
)
.expect_err("opening a symlink to a file as a directory")
.raw_error(),
wasi::ERRNO_NOTDIR,
"errno should be ERRNO_NOTDIR",
);
// Clean up.
cleanup_file(dir_fd, "target");
cleanup_file(dir_fd, "symlink");
wasi::path_unlink_file(dir_fd, "target").expect("removing a file");
wasi::path_unlink_file(dir_fd, "symlink").expect("removing a file");
}
fn main() {

167
crates/test-programs/wasi-tests/src/bin/path_filestat.rs

@ -1,169 +1,124 @@
use more_asserts::assert_gt;
use std::{env, process};
use wasi_old::wasi_unstable;
use wasi_tests::open_scratch_directory;
use wasi_tests::utils::{cleanup_file, close_fd};
use wasi_tests::wasi_wrappers::{
wasi_fd_fdstat_get, wasi_path_filestat_get, wasi_path_filestat_set_times, wasi_path_open,
};
unsafe fn test_path_filestat(dir_fd: wasi_unstable::Fd) {
let mut fdstat: wasi_unstable::FdStat = std::mem::zeroed();
let status = wasi_fd_fdstat_get(dir_fd, &mut fdstat);
assert_eq!(status, wasi_unstable::raw::__WASI_ESUCCESS, "fd_fdstat_get");
unsafe fn test_path_filestat(dir_fd: wasi::Fd) {
let mut fdstat = wasi::fd_fdstat_get(dir_fd).expect("fd_fdstat_get");
assert_ne!(
fdstat.fs_rights_base & wasi_unstable::RIGHT_PATH_FILESTAT_GET,
fdstat.fs_rights_base & wasi::RIGHTS_PATH_FILESTAT_GET,
0,
"the scratch directory should have RIGHT_PATH_FILESTAT_GET as base right",
);
assert_ne!(
fdstat.fs_rights_inheriting & wasi_unstable::RIGHT_PATH_FILESTAT_GET,
fdstat.fs_rights_inheriting & wasi::RIGHTS_PATH_FILESTAT_GET,
0,
"the scratch directory should have RIGHT_PATH_FILESTAT_GET as base right",
);
// Create a file in the scratch directory.
let mut file_fd = wasi_unstable::Fd::max_value() - 1;
let filename = "file";
let status = wasi_path_open(
let file_fd = wasi::path_open(
dir_fd,
0,
filename,
wasi_unstable::O_CREAT,
wasi_unstable::RIGHT_FD_READ
| wasi_unstable::RIGHT_FD_WRITE
| wasi_unstable::RIGHT_PATH_FILESTAT_GET,
"file",
wasi::OFLAGS_CREAT,
wasi::RIGHTS_FD_READ | wasi::RIGHTS_FD_WRITE | wasi::RIGHTS_PATH_FILESTAT_GET,
0,
// Pass some flags for later retrieval
wasi_unstable::FDFLAG_APPEND | wasi_unstable::FDFLAG_SYNC,
&mut file_fd,
);
assert_eq!(
status,
wasi_unstable::raw::__WASI_ESUCCESS,
"opening a file"
);
wasi::FDFLAGS_APPEND | wasi::FDFLAGS_SYNC,
)
.expect("opening a file");
assert_gt!(
file_fd,
libc::STDERR_FILENO as wasi_unstable::Fd,
libc::STDERR_FILENO as wasi::Fd,
"file descriptor range check",
);
let status = wasi_fd_fdstat_get(file_fd, &mut fdstat);
assert_eq!(status, wasi_unstable::raw::__WASI_ESUCCESS, "fd_fdstat_get");
fdstat = wasi::fd_fdstat_get(file_fd).expect("fd_fdstat_get");
assert_eq!(
fdstat.fs_rights_base & wasi_unstable::RIGHT_PATH_FILESTAT_GET,
fdstat.fs_rights_base & wasi::RIGHTS_PATH_FILESTAT_GET,
0,
"files shouldn't have rights for path_* syscalls even if manually given",
);
assert_eq!(
fdstat.fs_rights_inheriting & wasi_unstable::RIGHT_PATH_FILESTAT_GET,
fdstat.fs_rights_inheriting & wasi::RIGHTS_PATH_FILESTAT_GET,
0,
"files shouldn't have rights for path_* syscalls even if manually given",
);
assert_ne!(
fdstat.fs_flags & (wasi_unstable::FDFLAG_APPEND | wasi_unstable::FDFLAG_SYNC),
fdstat.fs_flags & (wasi::FDFLAGS_APPEND | wasi::FDFLAGS_SYNC),
0,
"file should have the same flags used to create the file"
);
// Check file size
let mut stat = wasi_unstable::FileStat {
st_dev: 0,
st_ino: 0,
st_filetype: 0,
st_nlink: 0,
st_size: 0,
st_atim: 0,
st_mtim: 0,
st_ctim: 0,
};
let status = wasi_path_filestat_get(dir_fd, 0, filename, filename.len(), &mut stat);
assert_eq!(
status,
wasi_unstable::raw::__WASI_ESUCCESS,
"reading file stats"
);
assert_eq!(stat.st_size, 0, "file size should be 0");
let mut stat = wasi::path_filestat_get(dir_fd, 0, "file").expect("reading file stats");
assert_eq!(stat.size, 0, "file size should be 0");
// Check path_filestat_set_times
let old_atim = stat.st_atim;
let new_mtim = stat.st_mtim - 100;
assert!(
wasi_path_filestat_set_times(
dir_fd,
0,
filename,
// on purpose: the syscall should not touch atim, because
// neither of the ATIM flags is set
new_mtim,
new_mtim,
wasi_unstable::FILESTAT_SET_MTIM,
)
.is_ok(),
"path_filestat_set_times should succeed"
);
let old_atim = stat.atim;
let new_mtim = stat.mtim - 100;
wasi::path_filestat_set_times(
dir_fd,
0,
"file",
// on purpose: the syscall should not touch atim, because
// neither of the ATIM flags is set
new_mtim,
new_mtim,
wasi::FSTFLAGS_MTIM,
)
.expect("path_filestat_set_times should succeed");
let status = wasi_path_filestat_get(dir_fd, 0, filename, filename.len(), &mut stat);
assert_eq!(
status,
wasi_unstable::raw::__WASI_ESUCCESS,
"reading file stats after path_filestat_set_times"
);
assert_eq!(stat.st_mtim, new_mtim, "mtim should change");
assert_eq!(stat.st_atim, old_atim, "atim should not change");
stat = wasi::path_filestat_get(dir_fd, 0, "file")
.expect("reading file stats after path_filestat_set_times");
assert_eq!(stat.mtim, new_mtim, "mtim should change");
assert_eq!(stat.atim, old_atim, "atim should not change");
assert_eq!(
wasi_path_filestat_set_times(
wasi::path_filestat_set_times(
dir_fd,
0,
filename,
"file",
new_mtim,
new_mtim,
wasi_unstable::FILESTAT_SET_MTIM | wasi_unstable::FILESTAT_SET_MTIM_NOW,
),
Err(wasi_unstable::EINVAL),
"MTIM & MTIM_NOW can't both be set"
wasi::FSTFLAGS_MTIM | wasi::FSTFLAGS_MTIM_NOW,
)
.expect_err("MTIM and MTIM_NOW can't both be set")
.raw_error(),
wasi::ERRNO_INVAL,
"errno should be ERRNO_INVAL"
);
// check if the times were untouched
let status = wasi_path_filestat_get(dir_fd, 0, filename, filename.len(), &mut stat);
assert_eq!(
status,
wasi_unstable::raw::__WASI_ESUCCESS,
"reading file stats after EINVAL fd_filestat_set_times"
);
assert_eq!(stat.st_mtim, new_mtim, "mtim should not change");
assert_eq!(stat.st_atim, old_atim, "atim should not change");
stat = wasi::path_filestat_get(dir_fd, 0, "file")
.expect("reading file stats after ERRNO_INVAL fd_filestat_set_times");
assert_eq!(stat.mtim, new_mtim, "mtim should not change");
assert_eq!(stat.atim, old_atim, "atim should not change");
let new_atim = old_atim - 100;
assert_eq!(
wasi_path_filestat_set_times(
wasi::path_filestat_set_times(
dir_fd,
0,
filename,
"file",
new_atim,
new_atim,
wasi_unstable::FILESTAT_SET_ATIM | wasi_unstable::FILESTAT_SET_ATIM_NOW,
),
Err(wasi_unstable::EINVAL),
"ATIM & ATIM_NOW can't both be set"
wasi::FSTFLAGS_ATIM | wasi::FSTFLAGS_ATIM_NOW,
)
.expect_err("ATIM & ATIM_NOW can't both be set")
.raw_error(),
wasi::ERRNO_INVAL,
"errno should be ERRNO_INVAL"
);
// check if the times were untouched
let status = wasi_path_filestat_get(dir_fd, 0, filename, filename.len(), &mut stat);
assert_eq!(
status,
wasi_unstable::raw::__WASI_ESUCCESS,
"reading file stats after EINVAL path_filestat_set_times"
);
assert_eq!(stat.st_mtim, new_mtim, "mtim should not change");
assert_eq!(stat.st_atim, old_atim, "atim should not change");
stat = wasi::path_filestat_get(dir_fd, 0, "file")
.expect("reading file stats after ERRNO_INVAL path_filestat_set_times");
assert_eq!(stat.mtim, new_mtim, "mtim should not change");
assert_eq!(stat.atim, old_atim, "atim should not change");
close_fd(file_fd);
cleanup_file(dir_fd, "file");
wasi::fd_close(file_fd).expect("closing a file");
wasi::path_unlink_file(dir_fd, "file").expect("removing a file");
}
fn main() {
let mut args = env::args();

306
crates/test-programs/wasi-tests/src/bin/path_link.rs

@ -1,250 +1,236 @@
use more_asserts::assert_gt;
use std::{env, process};
use wasi_old::wasi_unstable;
use wasi_tests::open_scratch_directory;
use wasi_tests::utils::{cleanup_dir, cleanup_file, create_dir, create_file};
use wasi_tests::wasi_wrappers::{
wasi_fd_fdstat_get, wasi_fd_filestat_get, wasi_path_link, wasi_path_open, wasi_path_symlink,
};
unsafe fn create_or_open(
dir_fd: wasi_unstable::Fd,
name: &str,
flags: wasi_unstable::OFlags,
) -> wasi_unstable::Fd {
let mut file_fd = wasi_unstable::Fd::max_value() - 1;
let status = wasi_path_open(dir_fd, 0, name, flags, 0, 0, 0, &mut file_fd);
assert_eq!(
status,
wasi_unstable::raw::__WASI_ESUCCESS,
"opening '{}'",
name
);
use wasi_tests::{create_file, open_scratch_directory};
unsafe fn create_or_open(dir_fd: wasi::Fd, name: &str, flags: wasi::Oflags) -> wasi::Fd {
let file_fd = wasi::path_open(dir_fd, 0, name, flags, 0, 0, 0)
.unwrap_or_else(|_| panic!("opening '{}'", name));
assert_gt!(
file_fd,
libc::STDERR_FILENO as wasi_unstable::Fd,
libc::STDERR_FILENO as wasi::Fd,
"file descriptor range check",
);
file_fd
}
unsafe fn open_link(dir_fd: wasi_unstable::Fd, name: &str) -> wasi_unstable::Fd {
let mut file_fd = wasi_unstable::Fd::max_value() - 1;
let status = wasi_path_open(dir_fd, 0, name, 0, 0, 0, 0, &mut file_fd);
assert_eq!(
status,
wasi_unstable::raw::__WASI_ESUCCESS,
"opening a link '{}'",
name
);
unsafe fn open_link(dir_fd: wasi::Fd, name: &str) -> wasi::Fd {
let file_fd = wasi::path_open(dir_fd, 0, name, 0, 0, 0, 0)
.unwrap_or_else(|_| panic!("opening a link '{}'", name));
assert_gt!(
file_fd,
libc::STDERR_FILENO as wasi_unstable::Fd,
libc::STDERR_FILENO as wasi::Fd,
"file descriptor range check",
);
file_fd
}
unsafe fn check_rights(orig_fd: wasi_unstable::Fd, link_fd: wasi_unstable::Fd) {
use std::mem::MaybeUninit;
// This is temporary until `wasi` implements `Debug` and `PartialEq` for
// `wasi::Filestat`.
fn filestats_assert_eq(left: wasi::Filestat, right: wasi::Filestat) {
assert_eq!(left.dev, right.dev, "dev should be equal");
assert_eq!(left.ino, right.ino, "ino should be equal");
assert_eq!(left.atim, right.atim, "atim should be equal");
assert_eq!(left.ctim, right.ctim, "ctim should be equal");
assert_eq!(left.mtim, right.mtim, "mtim should be equal");
assert_eq!(left.size, right.size, "size should be equal");
assert_eq!(left.nlink, right.nlink, "nlink should be equal");
assert_eq!(left.filetype, right.filetype, "filetype should be equal");
}
// Compare FileStats
let mut orig_filestat: wasi_unstable::FileStat = MaybeUninit::zeroed().assume_init();
let mut link_filestat: wasi_unstable::FileStat = MaybeUninit::zeroed().assume_init();
let mut status = wasi_fd_filestat_get(orig_fd, &mut orig_filestat);
assert_eq!(
status,
wasi_unstable::raw::__WASI_ESUCCESS,
"reading filestat of the source"
);
status = wasi_fd_filestat_get(link_fd, &mut link_filestat);
// This is temporary until `wasi` implements `Debug` and `PartialEq` for
// `wasi::Fdstat`.
fn fdstats_assert_eq(left: wasi::Fdstat, right: wasi::Fdstat) {
assert_eq!(left.fs_flags, right.fs_flags, "fs_flags should be equal");
assert_eq!(
status,
wasi_unstable::raw::__WASI_ESUCCESS,
"reading filestat of the link"
left.fs_filetype, right.fs_filetype,
"fs_filetype should be equal"
);
assert_eq!(orig_filestat, link_filestat, "filestats should match");
// Compare FdStats
let mut orig_fdstat: wasi_unstable::FdStat = MaybeUninit::zeroed().assume_init();
let mut link_fdstat: wasi_unstable::FdStat = MaybeUninit::zeroed().assume_init();
status = wasi_fd_fdstat_get(orig_fd, &mut orig_fdstat);
assert_eq!(
status,
wasi_unstable::raw::__WASI_ESUCCESS,
"reading fdstat of the source"
left.fs_rights_base, right.fs_rights_base,
"fs_rights_base should be equal"
);
status = wasi_fd_fdstat_get(link_fd, &mut link_fdstat);
assert_eq!(
status,
wasi_unstable::raw::__WASI_ESUCCESS,
"reading fdstat of the link"
left.fs_rights_inheriting, right.fs_rights_inheriting,
"fs_rights_inheriting should be equal"
);
assert_eq!(orig_fdstat, link_fdstat, "fdstats should match");
}
unsafe fn test_path_link(dir_fd: wasi_unstable::Fd) {
unsafe fn check_rights(orig_fd: wasi::Fd, link_fd: wasi::Fd) {
// Compare Filestats
let orig_filestat = wasi::fd_filestat_get(orig_fd).expect("reading filestat of the source");
let link_filestat = wasi::fd_filestat_get(link_fd).expect("reading filestat of the link");
filestats_assert_eq(orig_filestat, link_filestat);
// Compare Fdstats
let orig_fdstat = wasi::fd_fdstat_get(orig_fd).expect("reading fdstat of the source");
let link_fdstat = wasi::fd_fdstat_get(link_fd).expect("reading fdstat of the link");
fdstats_assert_eq(orig_fdstat, link_fdstat);
}
unsafe fn test_path_link(dir_fd: wasi::Fd) {
// Create a file
let file_fd = create_or_open(dir_fd, "file", wasi_unstable::O_CREAT);
let file_fd = create_or_open(dir_fd, "file", wasi::OFLAGS_CREAT);
// Create a link in the same directory and compare rights
assert!(
wasi_path_link(dir_fd, 0, "file", dir_fd, "link").is_ok(),
"creating a link in the same directory"
);
wasi::path_link(dir_fd, 0, "file", dir_fd, "link")
.expect("creating a link in the same directory");
let mut link_fd = open_link(dir_fd, "link");
check_rights(file_fd, link_fd);
cleanup_file(dir_fd, "link");
wasi::path_unlink_file(dir_fd, "link").expect("removing a link");
// Create a link in a different directory and compare rights
create_dir(dir_fd, "subdir");
let subdir_fd = create_or_open(dir_fd, "subdir", wasi_unstable::O_DIRECTORY);
assert!(
wasi_path_link(dir_fd, 0, "file", subdir_fd, "link").is_ok(),
"creating a link in subdirectory"
);
wasi::path_create_directory(dir_fd, "subdir").expect("creating a subdirectory");
let subdir_fd = create_or_open(dir_fd, "subdir", wasi::OFLAGS_DIRECTORY);
wasi::path_link(dir_fd, 0, "file", subdir_fd, "link").expect("creating a link in subdirectory");
link_fd = open_link(subdir_fd, "link");
check_rights(file_fd, link_fd);
cleanup_file(subdir_fd, "link");
cleanup_dir(dir_fd, "subdir");
wasi::path_unlink_file(subdir_fd, "link").expect("removing a link");
wasi::path_remove_directory(dir_fd, "subdir").expect("removing a subdirectory");
// Create a link to a path that already exists
create_file(dir_fd, "link");
assert_eq!(
wasi_path_link(dir_fd, 0, "file", dir_fd, "link"),
Err(wasi_unstable::EEXIST),
"creating a link to existing path"
wasi::path_link(dir_fd, 0, "file", dir_fd, "link")
.expect_err("creating a link to existing path should fail")
.raw_error(),
wasi::ERRNO_EXIST,
"errno should be ERRNO_EXIST"
);
cleanup_file(dir_fd, "link");
wasi::path_unlink_file(dir_fd, "link").expect("removing a file");
// Create a link to itself
assert_eq!(
wasi_path_link(dir_fd, 0, "file", dir_fd, "file"),
Err(wasi_unstable::EEXIST),
"creating a link to itself"
wasi::path_link(dir_fd, 0, "file", dir_fd, "file")
.expect_err("creating a link to itself should fail")
.raw_error(),
wasi::ERRNO_EXIST,
"errno should be ERRNO_EXIST"
);
// Create a link where target is a directory
create_dir(dir_fd, "link");
wasi::path_create_directory(dir_fd, "link").expect("creating a dir");
assert_eq!(
wasi_path_link(dir_fd, 0, "file", dir_fd, "link"),
Err(wasi_unstable::EEXIST),
"creating a link where target is a directory"
wasi::path_link(dir_fd, 0, "file", dir_fd, "link")
.expect_err("creating a link where target is a directory should fail")
.raw_error(),
wasi::ERRNO_EXIST,
"errno should be ERRNO_EXIST"
);
cleanup_dir(dir_fd, "link");
wasi::path_remove_directory(dir_fd, "link").expect("removing a dir");
// Create a link to a directory
create_dir(dir_fd, "subdir");
create_or_open(dir_fd, "subdir", wasi_unstable::O_DIRECTORY);
wasi::path_create_directory(dir_fd, "subdir").expect("creating a subdirectory");
create_or_open(dir_fd, "subdir", wasi::OFLAGS_DIRECTORY);
assert_eq!(
wasi_path_link(dir_fd, 0, "subdir", dir_fd, "link"),
Err(wasi_unstable::EPERM),
"creating a link to a directory"
wasi::path_link(dir_fd, 0, "subdir", dir_fd, "link")
.expect_err("creating a link to a directory should fail")
.raw_error(),
wasi::ERRNO_PERM,
"errno should be ERRNO_PERM"
);
cleanup_dir(dir_fd, "subdir");
wasi::path_remove_directory(dir_fd, "subdir").expect("removing a subdirectory");
// Create a link to a file with trailing slash
assert_eq!(
wasi_path_link(dir_fd, 0, "file", dir_fd, "link/"),
Err(wasi_unstable::ENOENT),
"creating a link to a file with trailing slash"
wasi::path_link(dir_fd, 0, "file", dir_fd, "link/")
.expect_err("creating a link to a file with trailing slash should fail")
.raw_error(),
wasi::ERRNO_NOENT,
"errno should be ERRNO_NOENT"
);
// Create a link to a dangling symlink
assert!(
wasi_path_symlink("target", dir_fd, "symlink").is_ok(),
"creating a dangling symlink"
);
wasi::path_symlink("target", dir_fd, "symlink").expect("creating a dangling symlink");
assert_eq!(
wasi_path_link(dir_fd, 0, "symlink", dir_fd, "link"),
Err(wasi_unstable::ENOENT),
"creating a link to a dangling symlink"
wasi::path_link(dir_fd, 0, "symlink", dir_fd, "link")
.expect_err("creating a link to a dangling symlink should fail")
.raw_error(),
wasi::ERRNO_NOENT,
"errno should be ERRNO_NOENT"
);
cleanup_file(dir_fd, "symlink");
wasi::path_unlink_file(dir_fd, "symlink").expect("removing a symlink");
// Create a link to a symlink loop
assert!(
wasi_path_symlink("symlink", dir_fd, "symlink").is_ok(),
"creating a symlink loop"
);
wasi::path_symlink("symlink", dir_fd, "symlink").expect("creating a symlink loop");
assert_eq!(
wasi_path_link(dir_fd, 0, "symlink", dir_fd, "link"),
Err(wasi_unstable::ELOOP),
"creating a link to a symlink loop"
wasi::path_link(dir_fd, 0, "symlink", dir_fd, "link")
.expect_err("creating a link to a symlink loop")
.raw_error(),
wasi::ERRNO_LOOP,
"errno should be ERRNO_LOOP"
);
cleanup_file(dir_fd, "symlink");
wasi::path_unlink_file(dir_fd, "symlink").expect("removing a symlink");
// Create a link where target is a dangling symlink
assert!(
wasi_path_symlink("target", dir_fd, "symlink").is_ok(),
"creating a dangling symlink"
);
wasi::path_symlink("target", dir_fd, "symlink").expect("creating a dangling symlink");
assert_eq!(
wasi_path_link(dir_fd, 0, "file", dir_fd, "symlink"),
Err(wasi_unstable::EEXIST),
"creating a link where target is a dangling symlink"
wasi::path_link(dir_fd, 0, "file", dir_fd, "symlink")
.expect_err("creating a link where target is a dangling symlink")
.raw_error(),
wasi::ERRNO_EXIST,
"errno should be ERRNO_EXIST"
);
cleanup_file(dir_fd, "symlink");
wasi::path_unlink_file(dir_fd, "symlink").expect("removing a symlink");
// Create a link to a file following symlinks
assert!(
wasi_path_symlink("file", dir_fd, "symlink").is_ok(),
"creating a valid symlink"
);
assert!(
wasi_path_link(
dir_fd,
wasi_unstable::LOOKUP_SYMLINK_FOLLOW,
"symlink",
dir_fd,
"link"
)
.is_ok(),
"creating a link to a file following symlinks",
);
wasi::path_symlink("file", dir_fd, "symlink").expect("creating a valid symlink");
wasi::path_link(
dir_fd,
wasi::LOOKUPFLAGS_SYMLINK_FOLLOW,
"symlink",
dir_fd,
"link",
)
.expect("creating a link to a file following symlinks");
link_fd = open_link(dir_fd, "link");
check_rights(file_fd, link_fd);
cleanup_file(dir_fd, "link");
cleanup_file(dir_fd, "symlink");
wasi::path_unlink_file(dir_fd, "link").expect("removing a link");
wasi::path_unlink_file(dir_fd, "symlink").expect("removing a symlink");
// Create a link where target is a dangling symlink following symlinks
assert!(
wasi_path_symlink("target", dir_fd, "symlink").is_ok(),
"creating a dangling symlink"
);
wasi::path_symlink("target", dir_fd, "symlink").expect("creating a dangling symlink");
assert_eq!(
wasi_path_link(
wasi::path_link(
dir_fd,
wasi_unstable::LOOKUP_SYMLINK_FOLLOW,
wasi::LOOKUPFLAGS_SYMLINK_FOLLOW,
"symlink",
dir_fd,
"link"
),
Err(wasi_unstable::ENOENT),
"creating a link where target is a dangling symlink following symlinks"
"link",
)
.expect_err("creating a link where target is a dangling symlink following symlinks")
.raw_error(),
wasi::ERRNO_NOENT,
"errno should be ERRNO_NOENT"
);
cleanup_file(dir_fd, "symlink");
wasi::path_unlink_file(dir_fd, "symlink").expect("removing a symlink");
// Create a link to a symlink loop following symlinks
assert!(
wasi_path_symlink("symlink", dir_fd, "symlink").is_ok(),
"creating a symlink loop"
);
wasi::path_symlink("symlink", dir_fd, "symlink").expect("creating a symlink loop");
assert_eq!(
wasi_path_link(
wasi::path_link(
dir_fd,
wasi_unstable::LOOKUP_SYMLINK_FOLLOW,
wasi::LOOKUPFLAGS_SYMLINK_FOLLOW,
"symlink",
dir_fd,
"link"
),
Err(wasi_unstable::ELOOP),
"creating a link to a symlink loop following symlinks"
"link",
)
.expect_err("creating a link to a symlink loop following symlinks")
.raw_error(),
wasi::ERRNO_LOOP,
"errno should be ERRNO_LOOP"
);
cleanup_file(dir_fd, "symlink");
wasi::path_unlink_file(dir_fd, "symlink").expect("removing a symlink");
// Clean up.
cleanup_file(dir_fd, "file");
wasi::path_unlink_file(dir_fd, "file").expect("removing a file");
}
fn main() {

54
crates/test-programs/wasi-tests/src/bin/path_open_create_existing.rs

@ -1,44 +1,24 @@
use std::{env, process};
use wasi_old::wasi_unstable;
use wasi_tests::open_scratch_directory;
use wasi_tests::utils::{cleanup_file, close_fd};
use wasi_tests::wasi_wrappers::wasi_path_open;
use wasi_tests::{create_file, open_scratch_directory};
unsafe fn test_path_open_create_existing(dir_fd: wasi_unstable::Fd) {
let mut fd: wasi_unstable::Fd = wasi_unstable::Fd::max_value() - 1;
let mut status = wasi_path_open(
dir_fd,
0,
"file",
wasi_unstable::O_CREAT | wasi_unstable::O_EXCL,
0,
0,
0,
&mut fd,
);
assert_eq!(
status,
wasi_unstable::raw::__WASI_ESUCCESS,
"creating a file"
);
close_fd(fd);
fd = wasi_unstable::Fd::max_value() - 1;
status = wasi_path_open(
dir_fd,
0,
"file",
wasi_unstable::O_CREAT | wasi_unstable::O_EXCL,
0,
0,
0,
&mut fd,
);
unsafe fn test_path_open_create_existing(dir_fd: wasi::Fd) {
create_file(dir_fd, "file");
assert_eq!(
status,
wasi_unstable::raw::__WASI_EEXIST,
"trying to create a file that already exists"
wasi::path_open(
dir_fd,
0,
"file",
wasi::OFLAGS_CREAT | wasi::OFLAGS_EXCL,
0,
0,
0,
)
.expect_err("trying to create a file that already exists")
.raw_error(),
wasi::ERRNO_EXIST,
"errno should be ERRNO_EXIST"
);
cleanup_file(dir_fd, "file");
wasi::path_unlink_file(dir_fd, "file").expect("removing a file");
}
fn main() {

45
crates/test-programs/wasi-tests/src/bin/path_open_dirfd_not_dir.rs

@ -1,46 +1,19 @@
use std::{env, process};
use wasi_old::wasi_unstable;
use wasi_tests::open_scratch_directory;
use wasi_tests::utils::close_fd;
use wasi_tests::wasi_wrappers::wasi_path_open;
unsafe fn test_dirfd_not_dir(dir_fd: wasi_unstable::Fd) {
unsafe fn test_dirfd_not_dir(dir_fd: wasi::Fd) {
// Open a file.
let mut file_fd: wasi_unstable::Fd = wasi_unstable::Fd::max_value() - 1;
let mut status = wasi_path_open(
dir_fd,
0,
"file",
wasi_unstable::O_CREAT,
0,
0,
0,
&mut file_fd,
);
assert_eq!(
status,
wasi_unstable::raw::__WASI_ESUCCESS,
"opening a file"
);
let file_fd =
wasi::path_open(dir_fd, 0, "file", wasi::OFLAGS_CREAT, 0, 0, 0).expect("opening a file");
// Now try to open a file underneath it as if it were a directory.
let mut new_file_fd: wasi_unstable::Fd = wasi_unstable::Fd::max_value() - 1;
status = wasi_path_open(
file_fd,
0,
"foo",
wasi_unstable::O_CREAT,
0,
0,
0,
&mut new_file_fd,
);
assert_eq!(
status,
wasi_unstable::raw::__WASI_ENOTDIR,
"non-directory base fd should get ENOTDIR"
wasi::path_open(file_fd, 0, "foo", wasi::OFLAGS_CREAT, 0, 0, 0)
.expect_err("non-directory base fd should get ERRNO_NOTDIR")
.raw_error(),
wasi::ERRNO_NOTDIR,
"errno should be ERRNO_NOTDIR"
);
close_fd(file_fd);
wasi::fd_close(file_fd).expect("closing a file");
}
fn main() {

226
crates/test-programs/wasi-tests/src/bin/path_rename.rs

@ -1,237 +1,153 @@
use more_asserts::assert_gt;
use std::{env, process};
use wasi_old::wasi_unstable;
use wasi_tests::open_scratch_directory;
use wasi_tests::utils::{cleanup_dir, cleanup_file, close_fd, create_dir, create_file};
use wasi_tests::wasi_wrappers::{wasi_path_open, wasi_path_rename};
use wasi_tests::{create_file, open_scratch_directory};
unsafe fn test_path_rename(dir_fd: wasi_unstable::Fd) {
unsafe fn test_path_rename(dir_fd: wasi::Fd) {
// First, try renaming a dir to nonexistent path
// Create source directory
create_dir(dir_fd, "source");
wasi::path_create_directory(dir_fd, "source").expect("creating a directory");
// Try renaming the directory
assert!(
wasi_path_rename(dir_fd, "source", dir_fd, "target").is_ok(),
"renaming a directory"
);
wasi::path_rename(dir_fd, "source", dir_fd, "target").expect("renaming a directory");
// Check that source directory doesn't exist anymore
let mut fd: wasi_unstable::Fd = wasi_unstable::Fd::max_value() - 1;
let mut status = wasi_path_open(
dir_fd,
0,
"source",
wasi_unstable::O_DIRECTORY,
0,
0,
0,
&mut fd,
);
assert_eq!(
status,
wasi_unstable::raw::__WASI_ENOENT,
"opening a nonexistent path as a directory"
);
assert_eq!(
fd,
wasi_unstable::Fd::max_value(),
"failed open should set the file descriptor to -1",
wasi::path_open(dir_fd, 0, "source", wasi::OFLAGS_DIRECTORY, 0, 0, 0)
.expect_err("opening a nonexistent path as a directory should fail")
.raw_error(),
wasi::ERRNO_NOENT,
"errno should be ERRNO_NOENT"
);
// Check that target directory exists
status = wasi_path_open(
dir_fd,
0,
"target",
wasi_unstable::O_DIRECTORY,
0,
0,
0,
&mut fd,
);
assert_eq!(
status,
wasi_unstable::raw::__WASI_ESUCCESS,
"opening renamed path as a directory"
);
let mut fd = wasi::path_open(dir_fd, 0, "target", wasi::OFLAGS_DIRECTORY, 0, 0, 0)
.expect("opening renamed path as a directory");
assert_gt!(
fd,
libc::STDERR_FILENO as wasi_unstable::Fd,
libc::STDERR_FILENO as wasi::Fd,
"file descriptor range check",
);
close_fd(fd);
cleanup_dir(dir_fd, "target");
wasi::fd_close(fd).expect("closing a file");
wasi::path_remove_directory(dir_fd, "target").expect("removing a directory");
// Now, try renaming renaming a dir to existing empty dir
create_dir(dir_fd, "source");
create_dir(dir_fd, "target");
assert!(
wasi_path_rename(dir_fd, "source", dir_fd, "target").is_ok(),
"renaming a directory"
);
wasi::path_create_directory(dir_fd, "source").expect("creating a directory");
wasi::path_create_directory(dir_fd, "target").expect("creating a directory");
wasi::path_rename(dir_fd, "source", dir_fd, "target").expect("renaming a directory");
// Check that source directory doesn't exist anymore
fd = wasi_unstable::Fd::max_value() - 1;
status = wasi_path_open(
dir_fd,
0,
"source",
wasi_unstable::O_DIRECTORY,
0,
0,
0,
&mut fd,
);
assert_eq!(
status,
wasi_unstable::raw::__WASI_ENOENT,
"opening a nonexistent path as a directory"
);
assert_eq!(
fd,
wasi_unstable::Fd::max_value(),
"failed open should set the file descriptor to -1",
wasi::path_open(dir_fd, 0, "source", wasi::OFLAGS_DIRECTORY, 0, 0, 0)
.expect_err("opening a nonexistent path as a directory")
.raw_error(),
wasi::ERRNO_NOENT,
"errno should be ERRNO_NOENT"
);
// Check that target directory exists
status = wasi_path_open(
dir_fd,
0,
"target",
wasi_unstable::O_DIRECTORY,
0,
0,
0,
&mut fd,
);
assert_eq!(
status,
wasi_unstable::raw::__WASI_ESUCCESS,
"opening renamed path as a directory"
);
fd = wasi::path_open(dir_fd, 0, "target", wasi::OFLAGS_DIRECTORY, 0, 0, 0)
.expect("opening renamed path as a directory");
assert_gt!(
fd,
libc::STDERR_FILENO as wasi_unstable::Fd,
libc::STDERR_FILENO as wasi::Fd,
"file descriptor range check",
);
close_fd(fd);
cleanup_dir(dir_fd, "target");
wasi::fd_close(fd).expect("closing a file");
wasi::path_remove_directory(dir_fd, "target").expect("removing a directory");
// Now, try renaming a dir to existing non-empty dir
create_dir(dir_fd, "source");
create_dir(dir_fd, "target");
wasi::path_create_directory(dir_fd, "source").expect("creating a directory");
wasi::path_create_directory(dir_fd, "target").expect("creating a directory");
create_file(dir_fd, "target/file");
assert_eq!(
wasi_path_rename(dir_fd, "source", dir_fd, "target"),
Err(wasi_unstable::ENOTEMPTY),
"renaming directory to a nonempty directory"
wasi::path_rename(dir_fd, "source", dir_fd, "target")
.expect_err("renaming directory to a nonempty directory")
.raw_error(),
wasi::ERRNO_NOTEMPTY,
"errno should be ERRNO_NOTEMPTY"
);
// Try renaming dir to a file
assert_eq!(
wasi_path_rename(dir_fd, "source", dir_fd, "target/file"),
Err(wasi_unstable::ENOTDIR),
"renaming directory to a file"
wasi::path_rename(dir_fd, "source", dir_fd, "target/file")
.expect_err("renaming a directory to a file")
.raw_error(),
wasi::ERRNO_NOTDIR,
"errno should be ERRNO_NOTDIR"
);
cleanup_file(dir_fd, "target/file");
cleanup_dir(dir_fd, "target");
cleanup_dir(dir_fd, "source");
wasi::path_unlink_file(dir_fd, "target/file").expect("removing a file");
wasi::path_remove_directory(dir_fd, "target").expect("removing a directory");
wasi::path_remove_directory(dir_fd, "source").expect("removing a directory");
// Now, try renaming a file to a nonexistent path
create_file(dir_fd, "source");
assert!(
wasi_path_rename(dir_fd, "source", dir_fd, "target").is_ok(),
"renaming a file"
);
wasi::path_rename(dir_fd, "source", dir_fd, "target").expect("renaming a file");
// Check that source file doesn't exist anymore
fd = wasi_unstable::Fd::max_value() - 1;
status = wasi_path_open(dir_fd, 0, "source", 0, 0, 0, 0, &mut fd);
assert_eq!(
status,
wasi_unstable::raw::__WASI_ENOENT,
"opening a nonexistent path"
);
assert_eq!(
fd,
wasi_unstable::Fd::max_value(),
"failed open should set the file descriptor to -1",
wasi::path_open(dir_fd, 0, "source", 0, 0, 0, 0)
.expect_err("opening a nonexistent path should fail")
.raw_error(),
wasi::ERRNO_NOENT,
"errno should be ERRNO_NOENT"
);
// Check that target file exists
status = wasi_path_open(dir_fd, 0, "target", 0, 0, 0, 0, &mut fd);
assert_eq!(
status,
wasi_unstable::raw::__WASI_ESUCCESS,
"opening renamed path"
);
fd = wasi::path_open(dir_fd, 0, "target", 0, 0, 0, 0).expect("opening renamed path");
assert_gt!(
fd,
libc::STDERR_FILENO as wasi_unstable::Fd,
libc::STDERR_FILENO as wasi::Fd,
"file descriptor range check",
);
close_fd(fd);
cleanup_file(dir_fd, "target");
wasi::fd_close(fd).expect("closing a file");
wasi::path_unlink_file(dir_fd, "target").expect("removing a file");
// Now, try renaming file to an existing file
create_file(dir_fd, "source");
create_file(dir_fd, "target");
assert!(
wasi_path_rename(dir_fd, "source", dir_fd, "target").is_ok(),
"renaming file to another existing file"
);
wasi::path_rename(dir_fd, "source", dir_fd, "target")
.expect("renaming file to another existing file");
// Check that source file doesn't exist anymore
fd = wasi_unstable::Fd::max_value() - 1;
status = wasi_path_open(dir_fd, 0, "source", 0, 0, 0, 0, &mut fd);
assert_eq!(
status,
wasi_unstable::raw::__WASI_ENOENT,
"opening a nonexistent path"
);
assert_eq!(
fd,
wasi_unstable::Fd::max_value(),
"failed open should set the file descriptor to -1",
wasi::path_open(dir_fd, 0, "source", 0, 0, 0, 0)
.expect_err("opening a nonexistent path")
.raw_error(),
wasi::ERRNO_NOENT,
"errno should be ERRNO_NOENT"
);
// Check that target file exists
status = wasi_path_open(dir_fd, 0, "target", 0, 0, 0, 0, &mut fd);
assert_eq!(
status,
wasi_unstable::raw::__WASI_ESUCCESS,
"opening renamed path"
);
fd = wasi::path_open(dir_fd, 0, "target", 0, 0, 0, 0).expect("opening renamed path");
assert_gt!(
fd,
libc::STDERR_FILENO as wasi_unstable::Fd,
libc::STDERR_FILENO as wasi::Fd,
"file descriptor range check",
);
close_fd(fd);
cleanup_file(dir_fd, "target");
wasi::fd_close(fd).expect("closing a file");
wasi::path_unlink_file(dir_fd, "target").expect("removing a file");
// Try renaming to an (empty) directory instead
create_file(dir_fd, "source");
create_dir(dir_fd, "target");
wasi::path_create_directory(dir_fd, "target").expect("creating a directory");
assert_eq!(
wasi_path_rename(dir_fd, "source", dir_fd, "target"),
Err(wasi_unstable::EISDIR),
"renaming file to existing directory"
wasi::path_rename(dir_fd, "source", dir_fd, "target")
.expect_err("renaming a file to existing directory should fail")
.raw_error(),
wasi::ERRNO_ISDIR,
"errno should be ERRNO_ISDIR"
);
cleanup_dir(dir_fd, "target");
cleanup_file(dir_fd, "source");
wasi::path_remove_directory(dir_fd, "target").expect("removing a directory");
wasi::path_unlink_file(dir_fd, "source").expect("removing a file");
}
fn main() {

59
crates/test-programs/wasi-tests/src/bin/path_rename_trailing_slashes.rs

@ -1,47 +1,42 @@
use std::{env, process};
use wasi_old::wasi_unstable;
use wasi_tests::open_scratch_directory;
use wasi_tests::utils::{cleanup_dir, cleanup_file, create_dir, create_file};
use wasi_tests::wasi_wrappers::wasi_path_rename;
use wasi_tests::{create_file, open_scratch_directory};
unsafe fn test_path_rename_trailing_slashes(dir_fd: wasi_unstable::Fd) {
unsafe fn test_path_rename_trailing_slashes(dir_fd: wasi::Fd) {
// Test renaming a file with a trailing slash in the name.
create_file(dir_fd, "source");
assert_eq!(
wasi_path_rename(dir_fd, "source/", dir_fd, "target"),
Err(wasi_unstable::ENOTDIR),
"renaming a file with a trailing slash in the source name"
wasi::path_rename(dir_fd, "source/", dir_fd, "target")
.expect_err("renaming a file with a trailing slash in the source name should fail")
.raw_error(),
wasi::ERRNO_NOTDIR,
"errno should be ERRNO_NOTDIR"
);
assert_eq!(
wasi_path_rename(dir_fd, "source", dir_fd, "target/"),
Err(wasi_unstable::ENOTDIR),
"renaming a file with a trailing slash in the destination name"
wasi::path_rename(dir_fd, "source", dir_fd, "target/")
.expect_err("renaming a file with a trailing slash in the destination name should fail")
.raw_error(),
wasi::ERRNO_NOTDIR,
"errno should be ERRNO_NOTDIR"
);
assert_eq!(
wasi_path_rename(dir_fd, "source/", dir_fd, "target/"),
Err(wasi_unstable::ENOTDIR),
"renaming a file with a trailing slash in the source and destination names"
wasi::path_rename(dir_fd, "source/", dir_fd, "target/")
.expect_err("renaming a file with a trailing slash in the source and destination names should fail")
.raw_error(),
wasi::ERRNO_NOTDIR,
"errno should be ERRNO_NOTDIR"
);
cleanup_file(dir_fd, "source");
wasi::path_unlink_file(dir_fd, "source").expect("removing a file");
// Test renaming a directory with a trailing slash in the name.
create_dir(dir_fd, "source");
assert_eq!(
wasi_path_rename(dir_fd, "source/", dir_fd, "target"),
Ok(()),
"renaming a directory with a trailing slash in the source name"
);
assert_eq!(
wasi_path_rename(dir_fd, "target", dir_fd, "source/"),
Ok(()),
"renaming a directory with a trailing slash in the destination name"
);
assert_eq!(
wasi_path_rename(dir_fd, "source/", dir_fd, "target/"),
Ok(()),
"renaming a directory with a trailing slash in the source and destination names"
);
cleanup_dir(dir_fd, "target");
wasi::path_create_directory(dir_fd, "source").expect("creating a directory");
wasi::path_rename(dir_fd, "source/", dir_fd, "target")
.expect("renaming a directory with a trailing slash in the source name");
wasi::path_rename(dir_fd, "target", dir_fd, "source/")
.expect("renaming a directory with a trailing slash in the destination name");
wasi::path_rename(dir_fd, "source/", dir_fd, "target/")
.expect("renaming a directory with a trailing slash in the source and destination names");
wasi::path_remove_directory(dir_fd, "target").expect("removing a directory");
}
fn main() {

69
crates/test-programs/wasi-tests/src/bin/path_symlink_trailing_slashes.rs

@ -1,60 +1,65 @@
use std::{env, process};
use wasi_old::wasi_unstable;
use wasi_tests::open_scratch_directory;
use wasi_tests::utils::{cleanup_dir, cleanup_file, create_dir, create_file};
use wasi_tests::wasi_wrappers::wasi_path_symlink;
use wasi_tests::{create_file, open_scratch_directory};
unsafe fn test_path_symlink_trailing_slashes(dir_fd: wasi_unstable::Fd) {
unsafe fn test_path_symlink_trailing_slashes(dir_fd: wasi::Fd) {
// Link destination shouldn't end with a slash.
assert_eq!(
wasi_path_symlink("source", dir_fd, "target/"),
Err(wasi_unstable::ENOENT),
"link destination ending with a slash"
wasi::path_symlink("source", dir_fd, "target/")
.expect_err("link destination ending with a slash should fail")
.raw_error(),
wasi::ERRNO_NOENT,
"errno should be ERRNO_NOENT"
);
// Without the trailing slash, this should succeed.
assert_eq!(
wasi_path_symlink("source", dir_fd, "target"),
Ok(()),
"link destination ending with a slash"
);
cleanup_file(dir_fd, "target");
wasi::path_symlink("source", dir_fd, "target").expect("link destination ending with a slash");
wasi::path_unlink_file(dir_fd, "target").expect("removing a file");
// Link destination already exists, target has trailing slash.
create_dir(dir_fd, "target");
wasi::path_create_directory(dir_fd, "target").expect("creating a directory");
assert_eq!(
wasi_path_symlink("source", dir_fd, "target/"),
Err(wasi_unstable::EEXIST),
"link destination already exists"
wasi::path_symlink("source", dir_fd, "target/")
.expect_err("link destination already exists")
.raw_error(),
wasi::ERRNO_EXIST,
"errno should be ERRNO_EXIST"
);
cleanup_dir(dir_fd, "target");
wasi::path_remove_directory(dir_fd, "target").expect("removing a directory");
// Link destination already exists, target has no trailing slash.
create_dir(dir_fd, "target");
wasi::path_create_directory(dir_fd, "target").expect("creating a directory");
assert_eq!(
wasi_path_symlink("source", dir_fd, "target"),
Err(wasi_unstable::EEXIST),
"link destination already exists"
wasi::path_symlink("source", dir_fd, "target")
.expect_err("link destination already exists")
.raw_error(),
wasi::ERRNO_EXIST,
"errno should be ERRNO_EXIST"
);
cleanup_dir(dir_fd, "target");
wasi::path_remove_directory(dir_fd, "target").expect("removing a directory");
// Link destination already exists, target has trailing slash.
create_file(dir_fd, "target");
assert_eq!(
wasi_path_symlink("source", dir_fd, "target/"),
Err(wasi_unstable::EEXIST),
"link destination already exists"
wasi::path_symlink("source", dir_fd, "target/")
.expect_err("link destination already exists")
.raw_error(),
wasi::ERRNO_EXIST,
"errno should be ERRNO_EXIST"
);
cleanup_file(dir_fd, "target");
wasi::path_unlink_file(dir_fd, "target").expect("removing a file");
// Link destination already exists, target has no trailing slash.
create_file(dir_fd, "target");
assert_eq!(
wasi_path_symlink("source", dir_fd, "target"),
Err(wasi_unstable::EEXIST),
"link destination already exists"
wasi::path_symlink("source", dir_fd, "target")
.expect_err("link destination already exists")
.raw_error(),
wasi::ERRNO_EXIST,
"errno should be ERRNO_EXIST"
);
cleanup_file(dir_fd, "target");
wasi::path_unlink_file(dir_fd, "target").expect("removing a file");
}
fn main() {

189
crates/test-programs/wasi-tests/src/bin/poll_oneoff.rs

@ -1,26 +1,18 @@
use more_asserts::assert_gt;
use std::{env, mem::MaybeUninit, process};
use wasi_old::wasi_unstable;
use wasi_tests::{
open_scratch_directory,
utils::{cleanup_file, close_fd},
wasi_wrappers::wasi_path_open,
};
use wasi_tests::{open_scratch_directory, STDERR_FD, STDIN_FD, STDOUT_FD};
const CLOCK_ID: wasi_unstable::Userdata = 0x0123_45678;
const CLOCK_ID: wasi::Userdata = 0x0123_45678;
unsafe fn poll_oneoff_impl(
in_: &[wasi_unstable::Subscription],
nexpected: usize,
) -> Vec<wasi_unstable::Event> {
let mut out: Vec<wasi_unstable::Event> = Vec::new();
out.resize_with(in_.len(), || {
MaybeUninit::<wasi_unstable::Event>::zeroed().assume_init()
unsafe fn poll_oneoff_impl(r#in: &[wasi::Subscription], nexpected: usize) -> Vec<wasi::Event> {
let mut out: Vec<wasi::Event> = Vec::new();
out.resize_with(r#in.len(), || {
MaybeUninit::<wasi::Event>::zeroed().assume_init()
});
let res = wasi_unstable::poll_oneoff(&in_, out.as_mut_slice());
let res = res.expect("poll_oneoff should succeed");
let size = wasi::poll_oneoff(r#in.as_ptr(), out.as_mut_ptr(), r#in.len())
.expect("poll_oneoff should succeed");
assert_eq!(
res, nexpected,
size, nexpected,
"poll_oneoff should return {} events",
nexpected
);
@ -28,19 +20,18 @@ unsafe fn poll_oneoff_impl(
}
unsafe fn test_timeout() {
let clock = wasi_unstable::raw::__wasi_subscription_u_clock_t {
identifier: CLOCK_ID,
clock_id: wasi_unstable::CLOCK_MONOTONIC,
let clock = wasi::SubscriptionClock {
id: wasi::CLOCKID_MONOTONIC,
timeout: 5_000_000u64, // 5 milliseconds
precision: 0,
flags: 0,
};
let in_ = [wasi_unstable::Subscription {
let r#in = [wasi::Subscription {
userdata: CLOCK_ID,
type_: wasi_unstable::EVENTTYPE_CLOCK,
u: wasi_unstable::raw::__wasi_subscription_u { clock },
r#type: wasi::EVENTTYPE_CLOCK,
u: wasi::SubscriptionU { clock },
}];
let out = poll_oneoff_impl(&in_, 1);
let out = poll_oneoff_impl(&r#in, 1);
let event = &out[0];
assert_eq!(
event.userdata, CLOCK_ID,
@ -48,40 +39,39 @@ unsafe fn test_timeout() {
);
assert_eq!(
event.error,
wasi_unstable::raw::__WASI_ESUCCESS,
wasi::ERRNO_SUCCESS,
"the event.error should be set to ESUCCESS"
);
assert_eq!(
event.type_,
wasi_unstable::EVENTTYPE_CLOCK,
"the event.type_ should equal clock"
event.r#type,
wasi::EVENTTYPE_CLOCK,
"the event.type should equal clock"
);
}
unsafe fn test_stdin_read() {
let clock = wasi_unstable::raw::__wasi_subscription_u_clock_t {
identifier: CLOCK_ID,
clock_id: wasi_unstable::CLOCK_MONOTONIC,
let clock = wasi::SubscriptionClock {
id: wasi::CLOCKID_MONOTONIC,
timeout: 5_000_000u64, // 5 milliseconds
precision: 0,
flags: 0,
};
let fd_readwrite = wasi_unstable::raw::__wasi_subscription_u_fd_readwrite_t {
fd: wasi_unstable::STDIN_FD,
let fd_readwrite = wasi::SubscriptionFdReadwrite {
file_descriptor: STDIN_FD,
};
let in_ = [
wasi_unstable::Subscription {
let r#in = [
wasi::Subscription {
userdata: CLOCK_ID,
type_: wasi_unstable::EVENTTYPE_CLOCK,
u: wasi_unstable::raw::__wasi_subscription_u { clock },
r#type: wasi::EVENTTYPE_CLOCK,
u: wasi::SubscriptionU { clock },
},
wasi_unstable::Subscription {
wasi::Subscription {
userdata: 1,
type_: wasi_unstable::EVENTTYPE_FD_READ,
u: wasi_unstable::raw::__wasi_subscription_u { fd_readwrite },
r#type: wasi::EVENTTYPE_FD_READ,
u: wasi::SubscriptionU { fd_readwrite },
},
];
let out = poll_oneoff_impl(&in_, 1);
let out = poll_oneoff_impl(&r#in, 1);
let event = &out[0];
assert_eq!(
event.userdata, CLOCK_ID,
@ -89,54 +79,53 @@ unsafe fn test_stdin_read() {
);
assert_eq!(
event.error,
wasi_unstable::raw::__WASI_ESUCCESS,
wasi::ERRNO_SUCCESS,
"the event.error should be set to ESUCCESS"
);
assert_eq!(
event.type_,
wasi_unstable::EVENTTYPE_CLOCK,
"the event.type_ should equal clock"
event.r#type,
wasi::EVENTTYPE_CLOCK,
"the event.type should equal clock"
);
}
unsafe fn test_stdout_stderr_write() {
let stdout_readwrite = wasi_unstable::raw::__wasi_subscription_u_fd_readwrite_t {
fd: wasi_unstable::STDOUT_FD,
let stdout_readwrite = wasi::SubscriptionFdReadwrite {
file_descriptor: STDOUT_FD,
};
let stderr_readwrite = wasi_unstable::raw::__wasi_subscription_u_fd_readwrite_t {
fd: wasi_unstable::STDERR_FD,
let stderr_readwrite = wasi::SubscriptionFdReadwrite {
file_descriptor: STDERR_FD,
};
let in_ = [
wasi_unstable::Subscription {
let r#in = [
wasi::Subscription {
userdata: 1,
type_: wasi_unstable::EVENTTYPE_FD_WRITE,
u: wasi_unstable::raw::__wasi_subscription_u {
r#type: wasi::EVENTTYPE_FD_WRITE,
u: wasi::SubscriptionU {
fd_readwrite: stdout_readwrite,
},
},
wasi_unstable::Subscription {
wasi::Subscription {
userdata: 2,
type_: wasi_unstable::EVENTTYPE_FD_WRITE,
u: wasi_unstable::raw::__wasi_subscription_u {
r#type: wasi::EVENTTYPE_FD_WRITE,
u: wasi::SubscriptionU {
fd_readwrite: stderr_readwrite,
},
},
];
let out = poll_oneoff_impl(&in_, 2);
let out = poll_oneoff_impl(&r#in, 2);
assert_eq!(
out[0].userdata, 1,
"the event.userdata should contain fd userdata specified by the user"
);
assert_eq!(
out[0].error,
wasi_unstable::raw::__WASI_ESUCCESS,
"the event.error should be set to {}",
wasi_unstable::raw::__WASI_ESUCCESS
wasi::ERRNO_SUCCESS,
"the event.error should be set to ERRNO_SUCCESS",
);
assert_eq!(
out[0].type_,
wasi_unstable::EVENTTYPE_FD_WRITE,
"the event.type_ should equal FD_WRITE"
out[0].r#type,
wasi::EVENTTYPE_FD_WRITE,
"the event.type should equal FD_WRITE"
);
assert_eq!(
out[1].userdata, 2,
@ -144,32 +133,33 @@ unsafe fn test_stdout_stderr_write() {
);
assert_eq!(
out[1].error,
wasi_unstable::raw::__WASI_ESUCCESS,
"the event.error should be set to {}",
wasi_unstable::raw::__WASI_ESUCCESS
wasi::ERRNO_SUCCESS,
"the event.error should be set to ERRNO_SUCCESS",
);
assert_eq!(
out[1].type_,
wasi_unstable::EVENTTYPE_FD_WRITE,
"the event.type_ should equal FD_WRITE"
out[1].r#type,
wasi::EVENTTYPE_FD_WRITE,
"the event.type should equal FD_WRITE"
);
}
unsafe fn test_fd_readwrite(fd: wasi_unstable::Fd, error_code: wasi_unstable::raw::__wasi_errno_t) {
let fd_readwrite = wasi_unstable::raw::__wasi_subscription_u_fd_readwrite_t { fd };
let in_ = [
wasi_unstable::Subscription {
unsafe fn test_fd_readwrite(fd: wasi::Fd, error_code: wasi::Errno) {
let fd_readwrite = wasi::SubscriptionFdReadwrite {
file_descriptor: fd,
};
let r#in = [
wasi::Subscription {
userdata: 1,
type_: wasi_unstable::EVENTTYPE_FD_READ,
u: wasi_unstable::raw::__wasi_subscription_u { fd_readwrite },
r#type: wasi::EVENTTYPE_FD_READ,
u: wasi::SubscriptionU { fd_readwrite },
},
wasi_unstable::Subscription {
wasi::Subscription {
userdata: 2,
type_: wasi_unstable::EVENTTYPE_FD_WRITE,
u: wasi_unstable::raw::__wasi_subscription_u { fd_readwrite },
r#type: wasi::EVENTTYPE_FD_WRITE,
u: wasi::SubscriptionU { fd_readwrite },
},
];
let out = poll_oneoff_impl(&in_, 2);
let out = poll_oneoff_impl(&r#in, 2);
assert_eq!(
out[0].userdata, 1,
"the event.userdata should contain fd userdata specified by the user"
@ -180,8 +170,8 @@ unsafe fn test_fd_readwrite(fd: wasi_unstable::Fd, error_code: wasi_unstable::ra
error_code
);
assert_eq!(
out[0].type_,
wasi_unstable::EVENTTYPE_FD_READ,
out[0].r#type,
wasi::EVENTTYPE_FD_READ,
"the event.type_ should equal FD_READ"
);
assert_eq!(
@ -194,50 +184,41 @@ unsafe fn test_fd_readwrite(fd: wasi_unstable::Fd, error_code: wasi_unstable::ra
error_code
);
assert_eq!(
out[1].type_,
wasi_unstable::EVENTTYPE_FD_WRITE,
out[1].r#type,
wasi::EVENTTYPE_FD_WRITE,
"the event.type_ should equal FD_WRITE"
);
}
unsafe fn test_fd_readwrite_valid_fd(dir_fd: wasi_unstable::Fd) {
unsafe fn test_fd_readwrite_valid_fd(dir_fd: wasi::Fd) {
// Create a file in the scratch directory.
let mut file_fd = wasi_unstable::Fd::max_value() - 1;
let status = wasi_path_open(
let file_fd = wasi::path_open(
dir_fd,
0,
"file",
wasi_unstable::O_CREAT,
wasi_unstable::RIGHT_FD_READ | wasi_unstable::RIGHT_FD_WRITE,
wasi::OFLAGS_CREAT,
wasi::RIGHTS_FD_READ | wasi::RIGHTS_FD_WRITE,
0,
0,
&mut file_fd,
);
assert_eq!(
status,
wasi_unstable::raw::__WASI_ESUCCESS,
"opening a file"
);
)
.expect("opening a file");
assert_gt!(
file_fd,
libc::STDERR_FILENO as wasi_unstable::Fd,
libc::STDERR_FILENO as wasi::Fd,
"file descriptor range check",
);
test_fd_readwrite(file_fd, wasi_unstable::raw::__WASI_ESUCCESS);
test_fd_readwrite(file_fd, wasi::ERRNO_SUCCESS);
close_fd(file_fd);
cleanup_file(dir_fd, "file");
wasi::fd_close(file_fd).expect("closing a file");
wasi::path_unlink_file(dir_fd, "file").expect("removing a file");
}
unsafe fn test_fd_readwrite_invalid_fd() {
test_fd_readwrite(
wasi_unstable::Fd::max_value(),
wasi_unstable::raw::__WASI_EBADF,
)
test_fd_readwrite(wasi::Fd::max_value(), wasi::ERRNO_BADF)
}
unsafe fn test_poll_oneoff(dir_fd: wasi_unstable::Fd) {
unsafe fn test_poll_oneoff(dir_fd: wasi::Fd) {
test_timeout();
// NB we assume that stdin/stdout/stderr are valid and open
// for the duration of the test case

34
crates/test-programs/wasi-tests/src/bin/readlink.rs

@ -1,28 +1,17 @@
use std::{env, process};
use wasi_old::wasi_unstable;
use wasi_tests::open_scratch_directory;
use wasi_tests::utils::{cleanup_file, create_file};
use wasi_tests::wasi_wrappers::{wasi_path_readlink, wasi_path_symlink};
use wasi_tests::{create_file, open_scratch_directory};
unsafe fn test_readlink(dir_fd: wasi_unstable::Fd) {
unsafe fn test_readlink(dir_fd: wasi::Fd) {
// Create a file in the scratch directory.
create_file(dir_fd, "target");
// Create a 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");
// Read link into the buffer
let buf = &mut [0u8; 10];
let mut bufused: usize = 0;
let mut status = wasi_path_readlink(dir_fd, "symlink", buf, &mut bufused);
assert_eq!(
status,
wasi_unstable::raw::__WASI_ESUCCESS,
"readlink should succeed"
);
let mut bufused = wasi::path_readlink(dir_fd, "symlink", buf.as_mut_ptr(), buf.len())
.expect("readlink should succeed");
assert_eq!(bufused, 6, "should use 6 bytes of the buffer");
assert_eq!(&buf[..6], b"target", "buffer should contain 'target'");
assert_eq!(
@ -33,19 +22,14 @@ unsafe fn test_readlink(dir_fd: wasi_unstable::Fd) {
// Read link into smaller buffer than the actual link's length
let buf = &mut [0u8; 4];
let mut bufused: usize = 0;
status = wasi_path_readlink(dir_fd, "symlink", buf, &mut bufused);
assert_eq!(
status,
wasi_unstable::raw::__WASI_ESUCCESS,
"readlink should succeed"
);
bufused = wasi::path_readlink(dir_fd, "symlink", buf.as_mut_ptr(), buf.len())
.expect("readlink should succeed");
assert_eq!(bufused, 4, "should use all 4 bytes of the buffer");
assert_eq!(buf, b"targ", "buffer should contain 'targ'");
// Clean up.
cleanup_file(dir_fd, "target");
cleanup_file(dir_fd, "symlink");
wasi::path_unlink_file(dir_fd, "target").expect("removing a file");
wasi::path_unlink_file(dir_fd, "symlink").expect("removing a file");
}
fn main() {

22
crates/test-programs/wasi-tests/src/bin/readlink_no_buffer.rs

@ -1,32 +1,22 @@
use std::{env, process};
use wasi_old::wasi_unstable;
use wasi_tests::open_scratch_directory;
use wasi_tests::utils::cleanup_file;
use wasi_tests::wasi_wrappers::{wasi_path_readlink, wasi_path_symlink};
unsafe fn test_readlink_no_buffer(dir_fd: wasi_unstable::Fd) {
unsafe fn test_readlink_no_buffer(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");
// Readlink it into a non-existent buffer.
let mut bufused: usize = 1;
let status = wasi_path_readlink(dir_fd, "symlink", &mut [], &mut bufused);
assert_eq!(
status,
wasi_unstable::raw::__WASI_ESUCCESS,
"readlink with a 0-sized buffer should succeed"
);
let bufused = wasi::path_readlink(dir_fd, "symlink", (&mut []).as_mut_ptr(), 0)
.expect("readlink with a 0-sized buffer should succeed");
assert_eq!(
bufused, 0,
"readlink with a 0-sized buffer should return 'bufused' 0"
);
// Clean up.
cleanup_file(dir_fd, "symlink");
wasi::path_unlink_file(dir_fd, "symlink").expect("removing a file");
}
fn main() {
let mut args = env::args();
let prog = args.next().unwrap();

43
crates/test-programs/wasi-tests/src/bin/remove_directory_trailing_slashes.rs

@ -1,47 +1,42 @@
use std::{env, process};
use wasi_old::wasi_unstable;
use wasi_tests::open_scratch_directory;
use wasi_tests::utils::{cleanup_file, create_dir, create_file};
use wasi_tests::wasi_wrappers::wasi_path_remove_directory;
use wasi_tests::{create_file, open_scratch_directory};
unsafe fn test_remove_directory_trailing_slashes(dir_fd: wasi_unstable::Fd) {
unsafe fn test_remove_directory_trailing_slashes(dir_fd: wasi::Fd) {
// Create a directory in the scratch directory.
create_dir(dir_fd, "dir");
wasi::path_create_directory(dir_fd, "dir").expect("creating a directory");
// Test that removing it succeeds.
assert_eq!(
wasi_path_remove_directory(dir_fd, "dir"),
Ok(()),
"remove_directory on a directory should succeed"
);
wasi::path_remove_directory(dir_fd, "dir")
.expect("remove_directory on a directory should succeed");
create_dir(dir_fd, "dir");
wasi::path_create_directory(dir_fd, "dir").expect("creating a directory");
// Test that removing it with a trailing flash succeeds.
assert_eq!(
wasi_path_remove_directory(dir_fd, "dir/"),
Ok(()),
"remove_directory with a trailing slash on a directory should succeed"
);
wasi::path_remove_directory(dir_fd, "dir/")
.expect("remove_directory with a trailing slash on a directory should succeed");
// Create a temporary file.
create_file(dir_fd, "file");
// Test that removing it with no trailing flash fails.
assert_eq!(
wasi_path_remove_directory(dir_fd, "file"),
Err(wasi_unstable::ENOTDIR),
"remove_directory without a trailing slash on a file should fail"
wasi::path_remove_directory(dir_fd, "file")
.expect_err("remove_directory without a trailing slash on a file should fail")
.raw_error(),
wasi::ERRNO_NOTDIR,
"errno should be ERRNO_NOTDIR"
);
// Test that removing it with a trailing flash fails.
assert_eq!(
wasi_path_remove_directory(dir_fd, "file/"),
Err(wasi_unstable::ENOTDIR),
"remove_directory with a trailing slash on a file should fail"
wasi::path_remove_directory(dir_fd, "file/")
.expect_err("remove_directory with a trailing slash on a file should fail")
.raw_error(),
wasi::ERRNO_NOTDIR,
"errno should be ERRNO_NOTDIR"
);
cleanup_file(dir_fd, "file");
wasi::path_unlink_file(dir_fd, "file").expect("removing a file");
}
fn main() {

25
crates/test-programs/wasi-tests/src/bin/remove_nonempty_directory.rs

@ -1,29 +1,26 @@
use std::{env, process};
use wasi_old::wasi_unstable;
use wasi_tests::open_scratch_directory;
use wasi_tests::utils::{cleanup_dir, create_dir};
use wasi_tests::wasi_wrappers::wasi_path_remove_directory;
unsafe fn test_remove_nonempty_directory(dir_fd: wasi_unstable::Fd) {
unsafe fn test_remove_nonempty_directory(dir_fd: wasi::Fd) {
// Create a directory in the scratch directory.
create_dir(dir_fd, "dir");
wasi::path_create_directory(dir_fd, "dir").expect("creating a directory");
// Create a directory in the directory we just created.
create_dir(dir_fd, "dir/nested");
wasi::path_create_directory(dir_fd, "dir/nested").expect("creating a subdirectory");
// Test that attempting to unlink the first directory returns the expected error code.
assert_eq!(
wasi_path_remove_directory(dir_fd, "dir"),
Err(wasi_unstable::ENOTEMPTY),
"remove_directory on a directory should return ENOTEMPTY",
wasi::path_remove_directory(dir_fd, "dir")
.expect_err("remove_directory on a directory should return ENOTEMPTY")
.raw_error(),
wasi::ERRNO_NOTEMPTY,
"errno should be ERRNO_NOTEMPTY",
);
// Removing the directories.
assert!(
wasi_path_remove_directory(dir_fd, "dir/nested").is_ok(),
"remove_directory on a nested directory should succeed",
);
cleanup_dir(dir_fd, "dir");
wasi::path_remove_directory(dir_fd, "dir/nested")
.expect("remove_directory on a nested directory should succeed");
wasi::path_remove_directory(dir_fd, "dir").expect("removing a directory");
}
fn main() {

79
crates/test-programs/wasi-tests/src/bin/renumber.rs

@ -1,92 +1,65 @@
use libc;
use more_asserts::assert_gt;
use std::{env, mem, process};
use wasi_old::wasi_unstable;
use std::{env, process};
use wasi_tests::open_scratch_directory;
use wasi_tests::utils::close_fd;
use wasi_tests::wasi_wrappers::{wasi_fd_fdstat_get, wasi_path_open};
unsafe fn test_renumber(dir_fd: wasi_unstable::Fd) {
let pre_fd: wasi_unstable::Fd = (libc::STDERR_FILENO + 1) as wasi_unstable::Fd;
unsafe fn test_renumber(dir_fd: wasi::Fd) {
let pre_fd: wasi::Fd = (libc::STDERR_FILENO + 1) as wasi::Fd;
assert_gt!(dir_fd, pre_fd, "dir_fd number");
// Create a file in the scratch directory.
let mut fd_from = wasi_unstable::Fd::max_value() - 1;
let mut status = wasi_path_open(
let fd_from = wasi::path_open(
dir_fd,
0,
"file1",
wasi_unstable::O_CREAT,
wasi_unstable::RIGHT_FD_READ | wasi_unstable::RIGHT_FD_WRITE,
wasi::OFLAGS_CREAT,
wasi::RIGHTS_FD_READ | wasi::RIGHTS_FD_WRITE,
0,
0,
&mut fd_from,
);
assert_eq!(
status,
wasi_unstable::raw::__WASI_ESUCCESS,
"opening a file"
);
)
.expect("opening a file");
assert_gt!(
fd_from,
libc::STDERR_FILENO as wasi_unstable::Fd,
libc::STDERR_FILENO as wasi::Fd,
"file descriptor range check",
);
// Get fd_from fdstat attributes
let mut fdstat_from: wasi_unstable::FdStat = mem::zeroed();
status = wasi_fd_fdstat_get(fd_from, &mut fdstat_from);
assert_eq!(
status,
wasi_unstable::raw::__WASI_ESUCCESS,
"calling fd_fdstat on the open file descriptor"
);
let fdstat_from =
wasi::fd_fdstat_get(fd_from).expect("calling fd_fdstat on the open file descriptor");
// Create another file in the scratch directory.
let mut fd_to = wasi_unstable::Fd::max_value() - 1;
status = wasi_path_open(
let fd_to = wasi::path_open(
dir_fd,
0,
"file2",
wasi_unstable::O_CREAT,
wasi_unstable::RIGHT_FD_READ | wasi_unstable::RIGHT_FD_WRITE,
wasi::OFLAGS_CREAT,
wasi::RIGHTS_FD_READ | wasi::RIGHTS_FD_WRITE,
0,
0,
&mut fd_to,
);
assert_eq!(
status,
wasi_unstable::raw::__WASI_ESUCCESS,
"opening a file"
);
)
.expect("opening a file");
assert_gt!(
fd_to,
libc::STDERR_FILENO as wasi_unstable::Fd,
libc::STDERR_FILENO as wasi::Fd,
"file descriptor range check",
);
// Renumber fd of file1 into fd of file2
assert!(
wasi_unstable::fd_renumber(fd_from, fd_to).is_ok(),
"renumbering two descriptors",
);
wasi::fd_renumber(fd_from, fd_to).expect("renumbering two descriptors");
// Ensure that fd_from is closed
assert_eq!(
wasi_unstable::fd_close(fd_from),
Err(wasi_unstable::EBADF),
"closing already closed file descriptor"
wasi::fd_close(fd_from)
.expect_err("closing already closed file descriptor")
.raw_error(),
wasi::ERRNO_BADF,
"errno should be ERRNO_BADF"
);
// Ensure that fd_to is still open.
let mut fdstat_to: wasi_unstable::FdStat = mem::zeroed();
status = wasi_fd_fdstat_get(fd_to, &mut fdstat_to);
assert_eq!(
status,
wasi_unstable::raw::__WASI_ESUCCESS,
"calling fd_fdstat on the open file descriptor"
);
let fdstat_to =
wasi::fd_fdstat_get(fd_to).expect("calling fd_fdstat on the open file descriptor");
assert_eq!(
fdstat_from.fs_filetype, fdstat_to.fs_filetype,
"expected fd_to have the same fdstat as fd_from"
@ -104,7 +77,7 @@ unsafe fn test_renumber(dir_fd: wasi_unstable::Fd) {
"expected fd_to have the same fdstat as fd_from"
);
close_fd(fd_to);
wasi::fd_close(fd_to).expect("closing a file");
}
fn main() {

8
crates/test-programs/wasi-tests/src/bin/sched_yield.rs

@ -1,10 +1,8 @@
use wasi_old::wasi_unstable;
fn test_sched_yield() {
assert!(wasi_unstable::sched_yield().is_ok(), "sched_yield");
unsafe fn test_sched_yield() {
wasi::sched_yield().expect("sched_yield");
}
fn main() {
// Run tests
test_sched_yield()
unsafe { test_sched_yield() }
}

24
crates/test-programs/wasi-tests/src/bin/stdio.rs

@ -1,25 +1,9 @@
use std::mem::MaybeUninit;
use wasi_old::wasi_unstable;
use wasi_tests::wasi_wrappers::wasi_fd_fdstat_get;
use wasi_tests::{STDERR_FD, STDIN_FD, STDOUT_FD};
unsafe fn test_stdio() {
for fd in &[
wasi_unstable::STDIN_FD,
wasi_unstable::STDOUT_FD,
wasi_unstable::STDERR_FD,
] {
let mut fdstat: wasi_unstable::FdStat = MaybeUninit::zeroed().assume_init();
let status = wasi_fd_fdstat_get(*fd, &mut fdstat);
assert_eq!(
status,
wasi_unstable::raw::__WASI_ESUCCESS,
"fd_fdstat_get on stdio"
);
assert!(
wasi_unstable::fd_renumber(*fd, *fd + 100).is_ok(),
"renumbering stdio",
);
for fd in &[STDIN_FD, STDOUT_FD, STDERR_FD] {
wasi::fd_fdstat_get(*fd).expect("fd_fdstat_get on stdio");
wasi::fd_renumber(*fd, *fd + 100).expect("renumbering stdio");
}
}

21
crates/test-programs/wasi-tests/src/bin/symlink_loop.rs

@ -1,26 +1,21 @@
use std::{env, process};
use wasi_old::wasi_unstable;
use wasi_tests::open_scratch_directory;
use wasi_tests::utils::cleanup_file;
use wasi_tests::wasi_wrappers::{wasi_path_open, wasi_path_symlink};
unsafe fn test_symlink_loop(dir_fd: wasi_unstable::Fd) {
unsafe fn test_symlink_loop(dir_fd: wasi::Fd) {
// Create a self-referencing symlink.
assert!(
wasi_path_symlink("symlink", dir_fd, "symlink").is_ok(),
"creating a symlink"
);
wasi::path_symlink("symlink", dir_fd, "symlink").expect("creating a symlink");
// Try to open it.
let mut file_fd: wasi_unstable::Fd = wasi_unstable::Fd::max_value() - 1;
assert_eq!(
wasi_path_open(dir_fd, 0, "symlink", 0, 0, 0, 0, &mut file_fd),
wasi_unstable::raw::__WASI_ELOOP,
"opening a self-referencing symlink",
wasi::path_open(dir_fd, 0, "symlink", 0, 0, 0, 0)
.expect_err("opening a self-referencing symlink")
.raw_error(),
wasi::ERRNO_LOOP,
"errno should be ERRNO_LOOP",
);
// Clean up.
cleanup_file(dir_fd, "symlink");
wasi::path_unlink_file(dir_fd, "symlink").expect("removing a file");
}
fn main() {

119
crates/test-programs/wasi-tests/src/bin/truncation_rights.rs

@ -1,24 +1,16 @@
use std::{env, mem, process};
use wasi_old::wasi_unstable;
use wasi_tests::open_scratch_directory;
use wasi_tests::utils::{cleanup_file, close_fd, create_file};
use wasi_tests::wasi_wrappers::{wasi_fd_fdstat_get, wasi_path_open};
use std::{env, process};
use wasi_tests::{create_file, open_scratch_directory};
unsafe fn test_truncation_rights(dir_fd: wasi_unstable::Fd) {
unsafe fn test_truncation_rights(dir_fd: wasi::Fd) {
// Create a file in the scratch directory.
create_file(dir_fd, "file");
// Get the rights for the scratch directory.
let mut dir_fdstat: wasi_unstable::FdStat = mem::zeroed();
let mut status = wasi_fd_fdstat_get(dir_fd, &mut dir_fdstat);
assert_eq!(
status,
wasi_unstable::raw::__WASI_ESUCCESS,
"calling fd_fdstat on the scratch directory"
);
let mut dir_fdstat =
wasi::fd_fdstat_get(dir_fd).expect("calling fd_fdstat on the scratch directory");
assert_eq!(
dir_fdstat.fs_filetype,
wasi_unstable::FILETYPE_DIRECTORY,
wasi::FILETYPE_DIRECTORY,
"expected the scratch directory to be a directory",
);
assert_eq!(
@ -26,111 +18,62 @@ unsafe fn test_truncation_rights(dir_fd: wasi_unstable::Fd) {
"expected the scratch directory to have no special flags",
);
assert_eq!(
dir_fdstat.fs_rights_base & wasi_unstable::RIGHT_FD_FILESTAT_SET_SIZE,
dir_fdstat.fs_rights_base & wasi::RIGHTS_FD_FILESTAT_SET_SIZE,
0,
"directories shouldn't have the fd_filestat_set_size right",
);
// If we have the right to set sizes from paths, test that it works.
if (dir_fdstat.fs_rights_base & wasi_unstable::RIGHT_PATH_FILESTAT_SET_SIZE) == 0 {
if (dir_fdstat.fs_rights_base & wasi::RIGHTS_PATH_FILESTAT_SET_SIZE) == 0 {
eprintln!("implementation doesn't support setting file sizes, skipping");
} else {
// Test that we can truncate the file.
let mut file_fd: wasi_unstable::Fd = wasi_unstable::Fd::max_value() - 1;
status = wasi_path_open(
dir_fd,
0,
"file",
wasi_unstable::O_TRUNC,
0,
0,
0,
&mut file_fd,
);
assert_eq!(
status,
wasi_unstable::raw::__WASI_ESUCCESS,
"truncating a file"
);
close_fd(file_fd);
let mut file_fd = wasi::path_open(dir_fd, 0, "file", wasi::OFLAGS_TRUNC, 0, 0, 0)
.expect("truncating a file");
wasi::fd_close(file_fd).expect("closing a file");
let mut rights_base: wasi_unstable::Rights = dir_fdstat.fs_rights_base;
let mut rights_inheriting: wasi_unstable::Rights = dir_fdstat.fs_rights_inheriting;
let mut rights_base: wasi::Rights = dir_fdstat.fs_rights_base;
let mut rights_inheriting: wasi::Rights = dir_fdstat.fs_rights_inheriting;
if (rights_inheriting & wasi_unstable::RIGHT_FD_FILESTAT_SET_SIZE) == 0 {
if (rights_inheriting & wasi::RIGHTS_FD_FILESTAT_SET_SIZE) == 0 {
eprintln!("implementation doesn't support setting file sizes through file descriptors, skipping");
} else {
rights_inheriting &= !wasi_unstable::RIGHT_FD_FILESTAT_SET_SIZE;
assert!(
wasi_unstable::fd_fdstat_set_rights(dir_fd, rights_base, rights_inheriting).is_ok(),
"droping fd_filestat_set_size inheriting right on a directory",
);
rights_inheriting &= !wasi::RIGHTS_FD_FILESTAT_SET_SIZE;
wasi::fd_fdstat_set_rights(dir_fd, rights_base, rights_inheriting)
.expect("droping fd_filestat_set_size inheriting right on a directory");
}
// Test that we can truncate the file without the
// wasi_unstable::RIGHT_FD_FILESTAT_SET_SIZE right.
status = wasi_path_open(
dir_fd,
0,
"file",
wasi_unstable::O_TRUNC,
0,
0,
0,
&mut file_fd,
);
assert_eq!(
status,
wasi_unstable::raw::__WASI_ESUCCESS,
"truncating a file without fd_filestat_set_size right",
);
close_fd(file_fd);
file_fd = wasi::path_open(dir_fd, 0, "file", wasi::OFLAGS_TRUNC, 0, 0, 0)
.expect("truncating a file without fd_filestat_set_size right");
wasi::fd_close(file_fd).expect("closing a file");
rights_base &= !wasi_unstable::RIGHT_PATH_FILESTAT_SET_SIZE;
assert!(
wasi_unstable::fd_fdstat_set_rights(dir_fd, rights_base, rights_inheriting).is_ok(),
"droping path_filestat_set_size base right on a directory",
);
rights_base &= !wasi::RIGHTS_PATH_FILESTAT_SET_SIZE;
wasi::fd_fdstat_set_rights(dir_fd, rights_base, rights_inheriting)
.expect("droping path_filestat_set_size base right on a directory");
// Test that clearing wasi_unstable::RIGHT_PATH_FILESTAT_SET_SIZE actually
// took effect.
status = wasi_fd_fdstat_get(dir_fd, &mut dir_fdstat);
dir_fdstat = wasi::fd_fdstat_get(dir_fd).expect("reading the fdstat from a directory");
assert_eq!(
status,
wasi_unstable::raw::__WASI_ESUCCESS,
"reading the fdstat from a directory",
);
assert_eq!(
(dir_fdstat.fs_rights_base & wasi_unstable::RIGHT_PATH_FILESTAT_SET_SIZE),
(dir_fdstat.fs_rights_base & wasi::RIGHTS_PATH_FILESTAT_SET_SIZE),
0,
"reading the fdstat from a directory",
);
// Test that we can't truncate the file without the
// wasi_unstable::RIGHT_PATH_FILESTAT_SET_SIZE right.
status = wasi_path_open(
dir_fd,
0,
"file",
wasi_unstable::O_TRUNC,
0,
0,
0,
&mut file_fd,
);
assert_eq!(
status,
wasi_unstable::raw::__WASI_ENOTCAPABLE,
"truncating a file without path_filestat_set_size right",
);
assert_eq!(
file_fd,
wasi_unstable::Fd::max_value(),
"failed open should set the file descriptor to -1",
wasi::path_open(dir_fd, 0, "file", wasi::OFLAGS_TRUNC, 0, 0, 0)
.expect_err("truncating a file without path_filestat_set_size right")
.raw_error(),
wasi::ERRNO_NOTCAPABLE,
"errno should be ERRNO_NOTCAPABLE",
);
}
cleanup_file(dir_fd, "file");
wasi::path_unlink_file(dir_fd, "file").expect("removing a file");
}
fn main() {

42
crates/test-programs/wasi-tests/src/bin/unlink_file_trailing_slashes.rs

@ -1,46 +1,46 @@
use std::{env, process};
use wasi_old::wasi_unstable;
use wasi_tests::open_scratch_directory;
use wasi_tests::utils::{cleanup_dir, create_dir, create_file};
use wasi_tests::wasi_wrappers::wasi_path_unlink_file;
use wasi_tests::{create_file, open_scratch_directory};
unsafe fn test_unlink_file_trailing_slashes(dir_fd: wasi_unstable::Fd) {
unsafe fn test_unlink_file_trailing_slashes(dir_fd: wasi::Fd) {
// Create a directory in the scratch directory.
create_dir(dir_fd, "dir");
wasi::path_create_directory(dir_fd, "dir").expect("creating a directory");
// Test that unlinking it fails.
assert_eq!(
wasi_path_unlink_file(dir_fd, "dir"),
Err(wasi_unstable::EISDIR),
"unlink_file on a directory should fail"
wasi::path_unlink_file(dir_fd, "dir")
.expect_err("unlink_file on a directory should fail")
.raw_error(),
wasi::ERRNO_ISDIR,
"errno should be ERRNO_ISDIR"
);
// Test that unlinking it with a trailing flash fails.
assert_eq!(
wasi_path_unlink_file(dir_fd, "dir/"),
Err(wasi_unstable::EISDIR),
"unlink_file on a directory should fail"
wasi::path_unlink_file(dir_fd, "dir/")
.expect_err("unlink_file on a directory should fail")
.raw_error(),
wasi::ERRNO_ISDIR,
"errno should be ERRNO_ISDIR"
);
// Clean up.
cleanup_dir(dir_fd, "dir");
wasi::path_remove_directory(dir_fd, "dir").expect("removing a directory");
// Create a temporary file.
create_file(dir_fd, "file");
// Test that unlinking it with a trailing flash fails.
assert_eq!(
wasi_path_unlink_file(dir_fd, "file/"),
Err(wasi_unstable::ENOTDIR),
"unlink_file with a trailing slash should fail"
wasi::path_unlink_file(dir_fd, "file/")
.expect_err("unlink_file with a trailing slash should fail")
.raw_error(),
wasi::ERRNO_NOTDIR,
"errno should be ERRNO_NOTDIR"
);
// Test that unlinking it with no trailing flash succeeds.
assert_eq!(
wasi_path_unlink_file(dir_fd, "file"),
Ok(()),
"unlink_file with no trailing slash should succeed"
);
wasi::path_unlink_file(dir_fd, "file")
.expect("unlink_file with no trailing slash should succeed");
}
fn main() {

52
crates/test-programs/wasi-tests/src/lib.rs

@ -1,39 +1,14 @@
pub mod utils;
pub mod wasi_wrappers;
use more_asserts::assert_gt;
use libc;
use std::ffi::CString;
use std::io;
use wasi_old::wasi_unstable;
// The `wasi` crate version 0.9.0 and beyond, doesn't
// seem to define these constants, so we do it ourselves.
pub const STDIN_FD: wasi::Fd = 0x0;
pub const STDOUT_FD: wasi::Fd = 0x1;
pub const STDERR_FD: wasi::Fd = 0x2;
/// Opens a fresh file descriptor for `path` where `path` should be a preopened
/// directory. This is intended to be used with `wasi_unstable`, not with
/// `wasi_snapshot_preview1`. This is getting phased out and will likely be
/// deleted soon.
pub fn open_scratch_directory(path: &str) -> Result<wasi_unstable::Fd, String> {
// Open the scratch directory.
let dir_fd: wasi_unstable::Fd = unsafe {
let cstr = CString::new(path.as_bytes()).unwrap();
libc::open(cstr.as_ptr(), libc::O_RDONLY | libc::O_DIRECTORY)
} as wasi_unstable::Fd;
if (dir_fd as std::os::raw::c_int) < 0 {
Err(format!(
"error opening scratch directory '{}': {}",
path,
io::Error::last_os_error()
))
} else {
Ok(dir_fd)
}
}
/// Same as `open_scratch_directory` above, except uses `wasi_snapshot_preview1`
/// APIs instead of `wasi_unstable` ones.
///
/// This is intended to replace `open_scratch_directory` once all the tests are
/// updated.
pub fn open_scratch_directory_new(path: &str) -> Result<wasi::Fd, String> {
/// directory.
pub fn open_scratch_directory(path: &str) -> Result<wasi::Fd, String> {
unsafe {
for i in 3.. {
let stat = match wasi::fd_prestat_get(i) {
@ -57,3 +32,14 @@ pub fn open_scratch_directory_new(path: &str) -> Result<wasi::Fd, String> {
Err(format!("failed to find scratch dir"))
}
}
pub unsafe fn create_file(dir_fd: wasi::Fd, filename: &str) {
let file_fd =
wasi::path_open(dir_fd, 0, filename, wasi::OFLAGS_CREAT, 0, 0, 0).expect("creating a file");
assert_gt!(
file_fd,
libc::STDERR_FILENO as wasi::Fd,
"file descriptor range check",
);
wasi::fd_close(file_fd).expect("closing a file");
}

54
crates/test-programs/wasi-tests/src/utils.rs

@ -1,54 +0,0 @@
use crate::wasi_wrappers::*;
use more_asserts::assert_gt;
use wasi_old::wasi_unstable;
pub unsafe fn create_dir(dir_fd: wasi_unstable::Fd, dir_name: &str) {
assert!(
wasi_path_create_directory(dir_fd, dir_name).is_ok(),
"creating a directory"
);
}
pub unsafe fn cleanup_dir(dir_fd: wasi_unstable::Fd, dir_name: &str) {
assert!(
wasi_path_remove_directory(dir_fd, dir_name).is_ok(),
"remove_directory on an empty directory should succeed"
);
}
/// Create an empty file with the given name.
pub unsafe fn create_file(dir_fd: wasi_unstable::Fd, file_name: &str) {
let mut file_fd = wasi_unstable::Fd::max_value() - 1;
let status = wasi_path_open(
dir_fd,
0,
file_name,
wasi_unstable::O_CREAT,
0,
0,
0,
&mut file_fd,
);
assert_eq!(
status,
wasi_unstable::raw::__WASI_ESUCCESS,
"creating a file"
);
assert_gt!(
file_fd,
libc::STDERR_FILENO as wasi_unstable::Fd,
"file descriptor range check",
);
close_fd(file_fd);
}
pub unsafe fn cleanup_file(dir_fd: wasi_unstable::Fd, file_name: &str) {
assert!(
wasi_path_unlink_file(dir_fd, file_name).is_ok(),
"unlink_file on a symlink should succeed"
);
}
pub unsafe fn close_fd(fd: wasi_unstable::Fd) {
assert!(wasi_unstable::fd_close(fd).is_ok(), "closing a file");
}

233
crates/test-programs/wasi-tests/src/wasi_wrappers.rs

@ -1,233 +0,0 @@
//! Minimal wrappers around WASI functions to allow use of `&str` rather than
//! pointer-length pairs.
//!
//! Where possible, we use the idiomatic wasi_unstable wrappers rather than the
//! raw interfaces, however for functions with out parameters, we use the raw
//! interfaces so that we can test whether they are stored to. In the future,
//! WASI should switch to multi-value and eliminate out parameters altogether.
use wasi_old::wasi_unstable;
pub unsafe fn wasi_path_create_directory(
dir_fd: wasi_unstable::Fd,
dir_name: &str,
) -> Result<(), wasi_unstable::Error> {
wasi_unstable::path_create_directory(dir_fd, dir_name.as_bytes())
}
pub unsafe fn wasi_path_remove_directory(
dir_fd: wasi_unstable::Fd,
dir_name: &str,
) -> Result<(), wasi_unstable::Error> {
wasi_unstable::path_remove_directory(dir_fd, dir_name.as_bytes())
}
pub unsafe fn wasi_path_unlink_file(
dir_fd: wasi_unstable::Fd,
file_name: &str,
) -> Result<(), wasi_unstable::Error> {
wasi_unstable::path_unlink_file(dir_fd, file_name.as_bytes())
}
#[allow(clippy::too_many_arguments)]
pub unsafe fn wasi_path_open(
dirfd: wasi_unstable::Fd,
dirflags: wasi_unstable::LookupFlags,
path: &str,
oflags: wasi_unstable::OFlags,
fs_rights_base: wasi_unstable::Rights,
fs_rights_inheriting: wasi_unstable::Rights,
fs_flags: wasi_unstable::FdFlags,
fd: &mut wasi_unstable::Fd,
) -> wasi_unstable::Errno {
wasi_unstable::raw::__wasi_path_open(
dirfd,
dirflags,
path.as_ptr(),
path.len(),
oflags,
fs_rights_base,
fs_rights_inheriting,
fs_flags,
fd,
)
}
pub unsafe fn wasi_path_symlink(
old_path: &str,
dirfd: wasi_unstable::Fd,
new_path: &str,
) -> Result<(), wasi_unstable::Error> {
wasi_unstable::path_symlink(old_path.as_bytes(), dirfd, new_path.as_bytes())
}
pub unsafe fn wasi_path_link(
old_fd: wasi_unstable::Fd,
old_flags: wasi_unstable::LookupFlags,
old_path: &str,
new_fd: wasi_unstable::Fd,
new_path: &str,
) -> Result<(), wasi_unstable::Error> {
wasi_unstable::path_link(
old_fd,
old_flags,
old_path.as_bytes(),
new_fd,
new_path.as_bytes(),
)
}
pub unsafe fn wasi_path_readlink(
dirfd: wasi_unstable::Fd,
path: &str,
buf: &mut [u8],
bufused: &mut usize,
) -> wasi_unstable::Errno {
wasi_unstable::raw::__wasi_path_readlink(
dirfd,
path.as_ptr(),
path.len(),
buf.as_mut_ptr(),
buf.len(),
bufused,
)
}
pub unsafe fn wasi_path_rename(
old_dirfd: wasi_unstable::Fd,
old_path: &str,
new_dirfd: wasi_unstable::Fd,
new_path: &str,
) -> Result<(), wasi_unstable::Error> {
wasi_unstable::path_rename(
old_dirfd,
old_path.as_bytes(),
new_dirfd,
new_path.as_bytes(),
)
}
pub unsafe fn wasi_fd_fdstat_get(
fd: wasi_unstable::Fd,
fdstat: &mut wasi_unstable::FdStat,
) -> wasi_unstable::Errno {
wasi_unstable::raw::__wasi_fd_fdstat_get(fd, fdstat)
}
pub unsafe fn wasi_fd_seek(
fd: wasi_unstable::Fd,
offset: wasi_unstable::FileDelta,
whence: wasi_unstable::Whence,
newoffset: &mut wasi_unstable::FileSize,
) -> wasi_unstable::Errno {
wasi_unstable::raw::__wasi_fd_seek(fd, offset, whence, newoffset)
}
pub unsafe fn wasi_fd_tell(
fd: wasi_unstable::Fd,
offset: &mut wasi_unstable::FileSize,
) -> wasi_unstable::Errno {
wasi_unstable::raw::__wasi_fd_tell(fd, offset)
}
pub unsafe fn wasi_clock_time_get(
clock_id: wasi_unstable::ClockId,
precision: wasi_unstable::Timestamp,
time: &mut wasi_unstable::Timestamp,
) -> wasi_unstable::Errno {
wasi_unstable::raw::__wasi_clock_time_get(clock_id, precision, time)
}
pub unsafe fn wasi_fd_filestat_get(
fd: wasi_unstable::Fd,
filestat: &mut wasi_unstable::FileStat,
) -> wasi_unstable::Errno {
wasi_unstable::raw::__wasi_fd_filestat_get(fd, filestat)
}
pub unsafe fn wasi_fd_write(
fd: wasi_unstable::Fd,
iovs: &[wasi_unstable::CIoVec],
nwritten: &mut libc::size_t,
) -> wasi_unstable::Errno {
wasi_unstable::raw::__wasi_fd_write(fd, iovs.as_ptr(), iovs.len(), nwritten)
}
pub unsafe fn wasi_fd_read(
fd: wasi_unstable::Fd,
iovs: &[wasi_unstable::IoVec],
nread: &mut libc::size_t,
) -> wasi_unstable::Errno {
wasi_unstable::raw::__wasi_fd_read(fd, iovs.as_ptr(), iovs.len(), nread)
}
pub unsafe fn wasi_fd_pread(
fd: wasi_unstable::Fd,
iovs: &[wasi_unstable::IoVec],
offset: wasi_unstable::FileSize,
nread: &mut usize,
) -> wasi_unstable::Errno {
wasi_unstable::raw::__wasi_fd_pread(fd, iovs.as_ptr(), iovs.len(), offset, nread)
}
pub unsafe fn wasi_fd_pwrite(
fd: wasi_unstable::Fd,
iovs: &mut [wasi_unstable::CIoVec],
offset: wasi_unstable::FileSize,
nwritten: &mut usize,
) -> wasi_unstable::Errno {
wasi_unstable::raw::__wasi_fd_pwrite(fd, iovs.as_ptr(), iovs.len(), offset, nwritten)
}
pub unsafe fn wasi_path_filestat_get(
fd: wasi_unstable::Fd,
dirflags: wasi_unstable::LookupFlags,
path: &str,
path_len: usize,
filestat: &mut wasi_unstable::FileStat,
) -> wasi_unstable::Errno {
wasi_unstable::raw::__wasi_path_filestat_get(fd, dirflags, path.as_ptr(), path_len, filestat)
}
pub unsafe fn wasi_path_filestat_set_times(
fd: wasi_unstable::Fd,
dirflags: wasi_unstable::LookupFlags,
path: &str,
st_atim: wasi_unstable::Timestamp,
st_mtim: wasi_unstable::Timestamp,
fst_flags: wasi_unstable::FstFlags,
) -> Result<(), wasi_unstable::Error> {
wasi_unstable::path_filestat_set_times(
fd,
dirflags,
path.as_bytes(),
st_atim,
st_mtim,
fst_flags,
)
}
pub unsafe fn wasi_fd_readdir(
fd: wasi_unstable::Fd,
buf: &mut [u8],
buf_len: usize,
cookie: wasi_unstable::DirCookie,
buf_used: &mut usize,
) -> wasi_unstable::Errno {
wasi_unstable::raw::__wasi_fd_readdir(
fd,
buf.as_mut_ptr() as *mut libc::c_void,
buf_len,
cookie,
buf_used,
)
}
pub unsafe fn wasi_fd_advise(
fd: wasi_unstable::Fd,
offset: wasi_unstable::FileSize,
len: wasi_unstable::FileSize,
advice: wasi_unstable::Advice,
) -> wasi_unstable::Errno {
wasi_unstable::raw::__wasi_fd_advise(fd, offset, len, advice)
}
Loading…
Cancel
Save