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")?, .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 module = HostRef::new(Module::new(&store, &data).context("failed to create wasm module")?);
let imports = module let imports = module
.borrow() .borrow()
.imports() .imports()
.iter() .iter()
.map(|i| { .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(); 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()) Ok(export.clone())
} else { } else {
bail!( bail!(

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

@ -9,7 +9,6 @@ publish = false
[dependencies] [dependencies]
libc = "0.2.65" libc = "0.2.65"
wasi = "0.9.0" wasi = "0.9.0"
wasi-old = { version = "0.7.0", package = "wasi" }
more-asserts = "0.2.1" more-asserts = "0.2.1"
# This crate is built with the wasm32-wasi target, so it's separate # 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 more_asserts::assert_gt;
use std::{env, process}; 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) { unsafe fn test_close_preopen(dir_fd: wasi::Fd) {
let pre_fd: wasi::Fd = (libc::STDERR_FILENO + 1) as 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. // Try to close a preopened directory handle.
assert_eq!( 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, wasi::ERRNO_NOTSUP,
"closing a preopened file descriptor", "errno should ERRNO_NOTSUP",
); );
// Try to renumber over a preopened directory handle. // Try to renumber over a preopened directory handle.
assert_eq!( 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, wasi::ERRNO_NOTSUP,
"renumbering over a preopened file descriptor", "errno should be ERRNO_NOTSUP",
); );
// Ensure that dir_fd is still open. // 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. // Try to renumber a preopened directory handle.
assert_eq!( 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, wasi::ERRNO_NOTSUP,
"renumbering over a preopened file descriptor", "errno should be ERRNO_NOTSUP",
); );
// Ensure that dir_fd is still open. // Ensure that dir_fd is still open.
@ -56,7 +62,7 @@ fn main() {
}; };
// Open scratch directory // 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, Ok(dir_fd) => dir_fd,
Err(err) => { Err(err) => {
eprintln!("{}", err); eprintln!("{}", err);

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

@ -1,6 +1,6 @@
use more_asserts::assert_gt; use more_asserts::assert_gt;
use std::{env, process}; 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) { unsafe fn test_dangling_fd(dir_fd: wasi::Fd) {
// Create a file, open it, delete it without closing the handle, // Create a file, open it, delete it without closing the handle,
@ -41,7 +41,7 @@ fn main() {
}; };
// Open scratch directory // 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, Ok(dir_fd) => dir_fd,
Err(err) => { Err(err) => {
eprintln!("{}", err); eprintln!("{}", err);

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

@ -1,21 +1,17 @@
use std::{env, process}; 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) { unsafe fn test_dangling_symlink(dir_fd: wasi::Fd) {
// First create a dangling symlink. // First create a dangling symlink.
assert!( wasi::path_symlink("target", dir_fd, "symlink").expect("creating a symlink");
wasi::path_symlink("target", dir_fd, "symlink").is_ok(),
"creating a symlink"
);
// Try to open it as a directory with O_NOFOLLOW. // 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!( 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, wasi::ERRNO_LOOP,
"opening a dangling symlink as a directory", "errno should be ERRNO_LOOP",
); );
// Clean up. // Clean up.
@ -33,7 +29,7 @@ fn main() {
}; };
// Open scratch directory // 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, Ok(dir_fd) => dir_fd,
Err(err) => { Err(err) => {
eprintln!("{}", err); eprintln!("{}", err);

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

@ -1,6 +1,6 @@
use more_asserts::assert_gt; use more_asserts::assert_gt;
use std::{env, process}; 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) { unsafe fn test_directory_seek(dir_fd: wasi::Fd) {
// Create a directory in the scratch directory. // Create a directory in the scratch directory.
@ -16,13 +16,12 @@ unsafe fn test_directory_seek(dir_fd: wasi::Fd) {
); );
// Attempt to seek. // Attempt to seek.
let status = wasi::fd_seek(fd, 0, wasi::WHENCE_CUR)
.err()
.expect("failed to seek");
assert_eq!( assert_eq!(
status.raw_error(), wasi::fd_seek(fd, 0, wasi::WHENCE_CUR)
.expect_err("seek on a directory")
.raw_error(),
wasi::ERRNO_NOTCAPABLE, wasi::ERRNO_NOTCAPABLE,
"seek on a directory" "errno should be ERRNO_NOTCAPABLE"
); );
// Check if we obtained the right to seek. // Check if we obtained the right to seek.
@ -54,7 +53,7 @@ fn main() {
}; };
// Open scratch directory // 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, Ok(dir_fd) => dir_fd,
Err(err) => { Err(err) => {
eprintln!("{}", 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 more_asserts::assert_gt;
use std::{env, process}; 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) { unsafe fn test_fd_advise(dir_fd: wasi::Fd) {
// Create a file in the scratch directory. // 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"); assert_eq!(stat.size, 0, "file size should be 0");
// Allocate some size // Allocate some size
assert!( wasi::fd_allocate(file_fd, 0, 100).expect("allocating size");
wasi::fd_allocate(file_fd, 0, 100).is_ok(),
"allocating size"
);
let stat = wasi::fd_filestat_get(file_fd).expect("failed to fdstat 2"); let stat = wasi::fd_filestat_get(file_fd).expect("failed to fdstat 2");
assert_eq!(stat.size, 100, "file size should be 100"); assert_eq!(stat.size, 100, "file size should be 100");
@ -51,7 +47,7 @@ fn main() {
}; };
// Open scratch directory // 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, Ok(dir_fd) => dir_fd,
Err(err) => { Err(err) => {
eprintln!("{}", 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 more_asserts::assert_gt;
use std::{env, process}; 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) { unsafe fn test_fd_filestat_set(dir_fd: wasi::Fd) {
// Create a file in the scratch directory. // 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, wasi::RIGHTS_FD_READ | wasi::RIGHTS_FD_WRITE,
0, 0,
0, 0,
).expect("failed to create file"); )
.expect("failed to create file");
assert_gt!( assert_gt!(
file_fd, file_fd,
libc::STDERR_FILENO as wasi::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"); assert_eq!(stat.size, 0, "file size should be 0");
// Check fd_filestat_set_size // Check fd_filestat_set_size
assert!( wasi::fd_filestat_set_size(file_fd, 100).expect("fd_filestat_set_size");
wasi::fd_filestat_set_size(file_fd, 100).is_ok(),
"fd_filestat_set_size"
);
let stat = wasi::fd_filestat_get(file_fd).expect("failed filestat 2"); let stat = wasi::fd_filestat_get(file_fd).expect("failed filestat 2");
assert_eq!(stat.size, 100, "file size should be 100"); 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 // Check fd_filestat_set_times
let old_atim = stat.atim; let old_atim = stat.atim;
let new_mtim = stat.mtim - 100; let new_mtim = stat.mtim - 100;
assert!( wasi::fd_filestat_set_times(file_fd, new_mtim, new_mtim, wasi::FSTFLAGS_MTIM)
wasi::fd_filestat_set_times( .expect("fd_filestat_set_times");
file_fd,
new_mtim,
new_mtim,
wasi::FSTFLAGS_MTIM,
)
.is_ok(),
"fd_filestat_set_times"
);
let stat = wasi::fd_filestat_get(file_fd).expect("failed filestat 3"); let stat = wasi::fd_filestat_get(file_fd).expect("failed filestat 3");
assert_eq!( assert_eq!(stat.size, 100, "file size should remain unchanged at 100");
stat.size, 100,
"file size should remain unchanged at 100"
);
assert_eq!(stat.mtim, new_mtim, "mtim should change"); assert_eq!(stat.mtim, new_mtim, "mtim should change");
assert_eq!(stat.atim, old_atim, "atim should not change"); assert_eq!(stat.atim, old_atim, "atim should not change");
@ -71,7 +58,7 @@ fn main() {
}; };
// Open scratch directory // 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, Ok(dir_fd) => dir_fd,
Err(err) => { Err(err) => {
eprintln!("{}", err); eprintln!("{}", err);

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

@ -1,6 +1,6 @@
use more_asserts::assert_gt; use more_asserts::assert_gt;
use std::{cmp::min, env, mem, process, slice, str}; 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; const BUF_LEN: usize = 256;
@ -48,12 +48,10 @@ impl<'a> Iterator for ReadDir<'a> {
} }
} }
unsafe fn exec_fd_readdir( unsafe fn exec_fd_readdir(fd: wasi::Fd, cookie: wasi::Dircookie) -> Vec<DirEntry> {
fd: wasi::Fd,
cookie: wasi::Dircookie,
) -> Vec<DirEntry> {
let mut buf: [u8; BUF_LEN] = [0; BUF_LEN]; 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 sl = slice::from_raw_parts(buf.as_ptr(), min(BUF_LEN, bufused));
let dirs: Vec<_> = ReadDir::from_slice(sl).collect(); 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 `.` // the first entry should be `.`
let dir = dirs.next().expect("first entry is None"); let dir = dirs.next().expect("first entry is None");
assert_eq!(dir.name, ".", "first name"); assert_eq!(dir.name, ".", "first name");
assert_eq!( assert_eq!(dir.dirent.d_type, wasi::FILETYPE_DIRECTORY, "first type");
dir.dirent.d_type,
wasi::FILETYPE_DIRECTORY,
"first type"
);
assert_eq!(dir.dirent.d_ino, stat.ino); assert_eq!(dir.dirent.d_ino, stat.ino);
assert_eq!(dir.dirent.d_namlen, 1); assert_eq!(dir.dirent.d_namlen, 1);
// the second entry should be `..` // the second entry should be `..`
let dir = dirs.next().expect("second entry is None"); let dir = dirs.next().expect("second entry is None");
assert_eq!(dir.name, "..", "second name"); assert_eq!(dir.name, "..", "second name");
assert_eq!( assert_eq!(dir.dirent.d_type, wasi::FILETYPE_DIRECTORY, "second type");
dir.dirent.d_type,
wasi::FILETYPE_DIRECTORY,
"second type"
);
assert!( assert!(
dirs.next().is_none(), 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, wasi::RIGHTS_FD_READ | wasi::RIGHTS_FD_WRITE,
0, 0,
0, 0,
).expect("failed to create file"); )
.expect("failed to create file");
assert_gt!( assert_gt!(
file_fd, file_fd,
libc::STDERR_FILENO as wasi::Fd, libc::STDERR_FILENO as wasi::Fd,
@ -152,7 +143,7 @@ fn main() {
}; };
// Open scratch directory // 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, Ok(dir_fd) => dir_fd,
Err(err) => { Err(err) => {
eprintln!("{}", err); eprintln!("{}", err);

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

@ -1,103 +1,46 @@
use more_asserts::assert_gt; use more_asserts::assert_gt;
use std::{env, process}; use std::{env, process};
use wasi_old::wasi_unstable;
use wasi_tests::open_scratch_directory; 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. // Create a file in the scratch directory.
let mut file_fd = wasi_unstable::Fd::max_value() - 1; let file_fd = wasi::path_open(
let status = wasi_path_open(
dir_fd, dir_fd,
0, 0,
"file", "file",
wasi_unstable::O_CREAT, wasi::OFLAGS_CREAT,
wasi_unstable::RIGHT_FD_READ | wasi_unstable::RIGHT_FD_WRITE, wasi::RIGHTS_FD_READ | wasi::RIGHTS_FD_WRITE,
0, 0,
0, 0,
&mut file_fd, )
); .expect("opening a file");
assert_eq!(
status,
wasi_unstable::raw::__WASI_ESUCCESS,
"opening a file"
);
assert_gt!( assert_gt!(
file_fd, file_fd,
libc::STDERR_FILENO as wasi_unstable::Fd, libc::STDERR_FILENO as wasi::Fd,
"file descriptor range check", "file descriptor range check",
); );
// Check file size // Check file size
let mut stat = wasi_unstable::FileStat { let mut stat = wasi::fd_filestat_get(file_fd).expect("reading file stats");
st_dev: 0, assert_eq!(stat.size, 0, "file size should be 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");
// Allocate some size // Allocate some size
assert!( wasi::fd_allocate(file_fd, 0, 100).expect("allocating size");
wasi_unstable::fd_allocate(file_fd, 0, 100).is_ok(), stat = wasi::fd_filestat_get(file_fd).expect("reading file stats");
"allocating size" assert_eq!(stat.size, 100, "file size should be 100");
);
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");
// Allocate should not modify if less than current size // Allocate should not modify if less than current size
assert!( wasi::fd_allocate(file_fd, 10, 10).expect("allocating size less than current size");
wasi_unstable::fd_allocate(file_fd, 10, 10).is_ok(), stat = wasi::fd_filestat_get(file_fd).expect("reading file stats");
"allocating size less than current size" assert_eq!(stat.size, 100, "file size should remain unchanged at 100");
);
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"
);
// Allocate should modify if offset+len > current_len // Allocate should modify if offset+len > current_len
assert!( wasi::fd_allocate(file_fd, 90, 20).expect("allocating size larger than current size");
wasi_unstable::fd_allocate(file_fd, 90, 20).is_ok(), stat = wasi::fd_filestat_get(file_fd).expect("reading file stats");
"allocating size larger than current size" assert_eq!(stat.size, 110, "file size should increase from 100 to 110");
);
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"
);
close_fd(file_fd); wasi::fd_close(file_fd).expect("closing a file");
cleanup_file(dir_fd, "file"); wasi::path_unlink_file(dir_fd, "file").expect("removing a file");
} }
fn main() { 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 more_asserts::assert_gt;
use std::{env, process}; use std::{env, process};
use wasi_old::wasi_unstable;
use wasi_tests::open_scratch_directory; 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. // Create a file in the scratch directory.
let mut file_fd = wasi_unstable::Fd::max_value() - 1; let file_fd = wasi::path_open(
let mut status = wasi_path_open(
dir_fd, dir_fd,
0, 0,
"file", "file",
wasi_unstable::O_CREAT, wasi::OFLAGS_CREAT,
wasi_unstable::RIGHT_FD_READ | wasi_unstable::RIGHT_FD_SEEK | wasi_unstable::RIGHT_FD_WRITE, wasi::RIGHTS_FD_READ | wasi::RIGHTS_FD_SEEK | wasi::RIGHTS_FD_WRITE,
0, 0,
0, 0,
&mut file_fd, )
); .expect("opening a file");
assert_eq!(
status,
wasi_unstable::raw::__WASI_ESUCCESS,
"opening a file"
);
assert_gt!( assert_gt!(
file_fd, file_fd,
libc::STDERR_FILENO as wasi_unstable::Fd, libc::STDERR_FILENO as wasi::Fd,
"file descriptor range check", "file descriptor range check",
); );
let contents = &[0u8, 1, 2, 3]; let contents = &[0u8, 1, 2, 3];
let ciovec = wasi_unstable::CIoVec { let ciovec = wasi::Ciovec {
buf: contents.as_ptr() as *const libc::c_void, buf: contents.as_ptr() as *const _,
buf_len: contents.len(), buf_len: contents.len(),
}; };
let mut nwritten = 0; let mut nwritten =
status = wasi_fd_pwrite(file_fd, &mut [ciovec], 0, &mut nwritten); wasi::fd_pwrite(file_fd, &mut [ciovec], 0).expect("writing bytes at offset 0");
assert_eq!(
status,
wasi_unstable::raw::__WASI_ESUCCESS,
"writing bytes at offset 0"
);
assert_eq!(nwritten, 4, "nwritten bytes check"); assert_eq!(nwritten, 4, "nwritten bytes check");
let contents = &mut [0u8; 4]; let contents = &mut [0u8; 4];
let iovec = wasi_unstable::IoVec { let iovec = wasi::Iovec {
buf: contents.as_mut_ptr() as *mut libc::c_void, buf: contents.as_mut_ptr() as *mut _,
buf_len: contents.len(), buf_len: contents.len(),
}; };
let mut nread = 0; let mut nread = wasi::fd_pread(file_fd, &[iovec], 0).expect("reading bytes at offset 0");
status = wasi_fd_pread(file_fd, &[iovec], 0, &mut nread);
assert_eq!(
status,
wasi_unstable::raw::__WASI_ESUCCESS,
"reading bytes at offset 0"
);
assert_eq!(nread, 4, "nread bytes check"); assert_eq!(nread, 4, "nread bytes check");
assert_eq!(contents, &[0u8, 1, 2, 3], "written bytes equal read bytes"); assert_eq!(contents, &[0u8, 1, 2, 3], "written bytes equal read bytes");
let contents = &mut [0u8; 4]; let contents = &mut [0u8; 4];
let iovec = wasi_unstable::IoVec { let iovec = wasi::Iovec {
buf: contents.as_mut_ptr() as *mut libc::c_void, buf: contents.as_mut_ptr() as *mut _,
buf_len: contents.len(), buf_len: contents.len(),
}; };
let mut nread = 0; nread = wasi::fd_pread(file_fd, &[iovec], 2).expect("reading bytes at offset 2");
status = wasi_fd_pread(file_fd, &[iovec], 2, &mut nread);
assert_eq!(
status,
wasi_unstable::raw::__WASI_ESUCCESS,
"reading bytes at offset 2"
);
assert_eq!(nread, 2, "nread bytes check"); assert_eq!(nread, 2, "nread bytes check");
assert_eq!(contents, &[2u8, 3, 0, 0], "file cursor was overwritten"); assert_eq!(contents, &[2u8, 3, 0, 0], "file cursor was overwritten");
let contents = &[1u8, 0]; let contents = &[1u8, 0];
let ciovec = wasi_unstable::CIoVec { let ciovec = wasi::Ciovec {
buf: contents.as_ptr() as *const libc::c_void, buf: contents.as_ptr() as *const _,
buf_len: contents.len(), buf_len: contents.len(),
}; };
let mut nwritten = 0; nwritten = wasi::fd_pwrite(file_fd, &mut [ciovec], 2).expect("writing bytes at offset 2");
status = wasi_fd_pwrite(file_fd, &mut [ciovec], 2, &mut nwritten);
assert_eq!(
status,
wasi_unstable::raw::__WASI_ESUCCESS,
"writing bytes at offset 2"
);
assert_eq!(nwritten, 2, "nwritten bytes check"); assert_eq!(nwritten, 2, "nwritten bytes check");
let contents = &mut [0u8; 4]; let contents = &mut [0u8; 4];
let iovec = wasi_unstable::IoVec { let iovec = wasi::Iovec {
buf: contents.as_mut_ptr() as *mut libc::c_void, buf: contents.as_mut_ptr() as *mut _,
buf_len: contents.len(), buf_len: contents.len(),
}; };
let mut nread = 0; nread = wasi::fd_pread(file_fd, &[iovec], 0).expect("reading bytes at offset 0");
status = wasi_fd_pread(file_fd, &[iovec], 0, &mut nread);
assert_eq!(
status,
wasi_unstable::raw::__WASI_ESUCCESS,
"reading bytes at offset 0"
);
assert_eq!(nread, 4, "nread bytes check"); assert_eq!(nread, 4, "nread bytes check");
assert_eq!(contents, &[0u8, 1, 1, 0], "file cursor was overwritten"); assert_eq!(contents, &[0u8, 1, 1, 0], "file cursor was overwritten");
close_fd(file_fd); wasi::fd_close(file_fd).expect("closing a file");
cleanup_file(dir_fd, "file"); wasi::path_unlink_file(dir_fd, "file").expect("removing a file");
} }
fn main() { 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 more_asserts::assert_gt;
use std::{env, process}; use std::{env, process};
use wasi_old::wasi_unstable;
use wasi_tests::open_scratch_directory; 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. // Create a file in the scratch directory.
let mut file_fd = wasi_unstable::Fd::max_value() - 1; let file_fd = wasi::path_open(
let mut status = wasi_path_open(
dir_fd, dir_fd,
0, 0,
"file", "file",
wasi_unstable::O_CREAT, wasi::OFLAGS_CREAT,
wasi_unstable::RIGHT_FD_READ | wasi_unstable::RIGHT_FD_WRITE, wasi::RIGHTS_FD_READ | wasi::RIGHTS_FD_WRITE,
0, 0,
0, 0,
&mut file_fd, )
); .expect("opening a file");
assert_eq!(
status,
wasi_unstable::raw::__WASI_ESUCCESS,
"opening a file"
);
assert_gt!( assert_gt!(
file_fd, file_fd,
libc::STDERR_FILENO as wasi_unstable::Fd, libc::STDERR_FILENO as wasi::Fd,
"file descriptor range check", "file descriptor range check",
); );
// Check current offset // Check current offset
let mut offset: wasi_unstable::FileSize = 0; let mut offset = wasi::fd_tell(file_fd).expect("getting initial file offset");
status = wasi_fd_tell(file_fd, &mut offset);
assert_eq!(
status,
wasi_unstable::raw::__WASI_ESUCCESS,
"getting initial file offset"
);
assert_eq!(offset, 0, "current offset should be 0"); assert_eq!(offset, 0, "current offset should be 0");
// Write to file // Write to file
let buf = &[0u8; 100]; let buf = &[0u8; 100];
let iov = wasi_unstable::CIoVec { let iov = wasi::Ciovec {
buf: buf.as_ptr() as *const _, buf: buf.as_ptr() as *const _,
buf_len: buf.len(), buf_len: buf.len(),
}; };
let iovs = &[iov]; let nwritten = wasi::fd_write(file_fd, &[iov]).expect("writing to a file");
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"
);
assert_eq!(nwritten, 100, "should write 100 bytes to file"); assert_eq!(nwritten, 100, "should write 100 bytes to file");
// Check current offset // Check current offset
status = wasi_fd_tell(file_fd, &mut offset); offset = wasi::fd_tell(file_fd).expect("getting file offset after writing");
assert_eq!(
status,
wasi_unstable::raw::__WASI_ESUCCESS,
"getting file offset after writing"
);
assert_eq!(offset, 100, "offset after writing should be 100"); assert_eq!(offset, 100, "offset after writing should be 100");
// Seek to middle of the file // Seek to middle of the file
let mut newoffset = 1; let mut newoffset =
status = wasi_fd_seek(file_fd, -50, wasi_unstable::WHENCE_CUR, &mut newoffset); wasi::fd_seek(file_fd, -50, wasi::WHENCE_CUR).expect("seeking to the middle of a file");
assert_eq!(
status,
wasi_unstable::raw::__WASI_ESUCCESS,
"seeking to the middle of a file"
);
assert_eq!( assert_eq!(
newoffset, 50, newoffset, 50,
"offset after seeking to the middle should be at 50" "offset after seeking to the middle should be at 50"
); );
// Seek to the beginning of the file // Seek to the beginning of the file
status = wasi_fd_seek(file_fd, 0, wasi_unstable::WHENCE_SET, &mut newoffset); newoffset =
assert_eq!( wasi::fd_seek(file_fd, 0, wasi::WHENCE_SET).expect("seeking to the beginning of the file");
status,
wasi_unstable::raw::__WASI_ESUCCESS,
"seeking to the beginning of the file"
);
assert_eq!( assert_eq!(
newoffset, 0, newoffset, 0,
"offset after seeking to the beginning of the file should be at 0" "offset after seeking to the beginning of the file should be at 0"
); );
// Seek beyond the file should be possible // Seek beyond the file should be possible
status = wasi_fd_seek(file_fd, 1000, wasi_unstable::WHENCE_CUR, &mut newoffset); wasi::fd_seek(file_fd, 1000, wasi::WHENCE_CUR).expect("seeking beyond the end of the file");
assert_eq!(
status,
wasi_unstable::raw::__WASI_ESUCCESS,
"seeking beyond the end of the file"
);
// Seek before byte 0 is an error though // Seek before byte 0 is an error though
status = wasi_fd_seek(file_fd, -2000, wasi_unstable::WHENCE_CUR, &mut newoffset);
assert_eq!( assert_eq!(
status, wasi::fd_seek(file_fd, -2000, wasi::WHENCE_CUR)
wasi_unstable::raw::__WASI_EINVAL, .expect_err("seeking before byte 0 should be an error")
"seeking before byte 0 is an error" .raw_error(),
wasi::ERRNO_INVAL,
"errno should be ERRNO_INVAL",
); );
close_fd(file_fd); wasi::fd_close(file_fd).expect("closing a file");
cleanup_file(dir_fd, "file"); wasi::path_unlink_file(dir_fd, "file").expect("deleting a file");
} }
fn main() { fn main() {
let mut args = env::args(); 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 more_asserts::assert_gt;
use std::{env, process}; use std::{env, process};
use wasi_old::wasi_unstable;
use wasi_tests::open_scratch_directory; 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) { unsafe fn test_file_unbuffered_write(dir_fd: wasi::Fd) {
// Create file // Create and open file for reading
create_file(dir_fd, "file"); let fd_read = wasi::path_open(
// Open file for reading
let mut fd_read = wasi_unstable::Fd::max_value() - 1;
let mut status = wasi_path_open(
dir_fd, dir_fd,
0, 0,
"file", "file",
0, wasi::OFLAGS_CREAT,
wasi_unstable::RIGHT_FD_READ, wasi::RIGHTS_FD_READ,
0, 0,
0, 0,
&mut fd_read, )
); .expect("create and open file for reading");
assert_eq!(
status,
wasi_unstable::raw::__WASI_ESUCCESS,
"opening a file"
);
assert_gt!( assert_gt!(
fd_read, fd_read,
libc::STDERR_FILENO as wasi_unstable::Fd, libc::STDERR_FILENO as wasi::Fd,
"file descriptor range check", "file descriptor range check",
); );
// Open the same file but for writing // Open the same file but for writing
let mut fd_write = wasi_unstable::Fd::max_value() - 1; let fd_write = wasi::path_open(dir_fd, 0, "file", 0, wasi::RIGHTS_FD_WRITE, 0, 0)
status = wasi_path_open( .expect("opening file for writing");
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"
);
assert_gt!( assert_gt!(
fd_write, fd_write,
libc::STDERR_FILENO as wasi_unstable::Fd, libc::STDERR_FILENO as wasi::Fd,
"file descriptor range check", "file descriptor range check",
); );
// Write to file // Write to file
let contents = &[1u8]; let contents = &[1u8];
let ciovec = wasi_unstable::CIoVec { let ciovec = wasi::Ciovec {
buf: contents.as_ptr() as *const libc::c_void, buf: contents.as_ptr() as *const _,
buf_len: contents.len(), buf_len: contents.len(),
}; };
let mut nwritten = 0; let nwritten = wasi::fd_write(fd_write, &[ciovec]).expect("writing byte to file");
status = wasi_fd_write(fd_write, &[ciovec], &mut nwritten);
assert_eq!(
status,
wasi_unstable::raw::__WASI_ESUCCESS,
"writing byte to file"
);
assert_eq!(nwritten, 1, "nwritten bytes check"); assert_eq!(nwritten, 1, "nwritten bytes check");
// Read from file // Read from file
let contents = &mut [0u8; 1]; let contents = &mut [0u8; 1];
let iovec = wasi_unstable::IoVec { let iovec = wasi::Iovec {
buf: contents.as_mut_ptr() as *mut libc::c_void, buf: contents.as_mut_ptr() as *mut _,
buf_len: contents.len(), buf_len: contents.len(),
}; };
let mut nread = 0; let nread = wasi::fd_read(fd_read, &[iovec]).expect("reading bytes from file");
status = wasi_fd_read(fd_read, &[iovec], &mut nread);
assert_eq!(
status,
wasi_unstable::raw::__WASI_ESUCCESS,
"reading bytes from file"
);
assert_eq!(nread, 1, "nread bytes check"); assert_eq!(nread, 1, "nread bytes check");
assert_eq!(contents, &[1u8], "written bytes equal read bytes"); assert_eq!(contents, &[1u8], "written bytes equal read bytes");
// Clean up // Clean up
close_fd(fd_write); wasi::fd_close(fd_write).expect("closing a file");
close_fd(fd_read); wasi::fd_close(fd_read).expect("closing a file");
cleanup_file(dir_fd, "file"); wasi::path_unlink_file(dir_fd, "file").expect("removing a file");
} }
fn main() { fn main() {
let mut args = env::args(); 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 more_asserts::assert_gt;
use std::{env, process}; use std::{env, process};
use wasi_old::wasi_unstable; use wasi_tests::{create_file, open_scratch_directory};
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,
};
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 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 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 a file in the nested directory.
create_file(dir_fd, "dir/nested/file"); create_file(dir_fd, "dir/nested/file");
// Now open it with an absolute path. // 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!( assert_eq!(
status, wasi::path_open(dir_fd, 0, "/dir/nested/file", 0, 0, 0, 0)
wasi_unstable::raw::__WASI_ENOTCAPABLE, .expect_err("opening a file with an absolute path")
"opening a file with an absolute path" .raw_error(),
); wasi::ERRNO_NOTCAPABLE,
assert_eq!( "errno should be ERRNO_NOTCAPABLE"
file_fd,
wasi_unstable::Fd::max_value(),
"failed open should set the file descriptor to -1",
); );
// Now open it with a path containing "..". // Now open it with a path containing "..".
status = wasi_path_open( let mut file_fd = wasi::path_open(
dir_fd, dir_fd,
0, 0,
"dir/.//nested/../../dir/nested/../nested///./file", "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, 0,
0, 0,
&mut file_fd, )
); .expect("opening a file with \"..\" in the path");
assert_eq!(
status,
wasi_unstable::raw::__WASI_ESUCCESS,
"opening a file with \"..\" in the path"
);
assert_gt!( assert_gt!(
file_fd, file_fd,
libc::STDERR_FILENO as wasi_unstable::Fd, libc::STDERR_FILENO as wasi::Fd,
"file descriptor range check", "file descriptor range check",
); );
close_fd(file_fd); wasi::fd_close(file_fd).expect("closing a file");
// Now open it with a trailing NUL. // 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!( assert_eq!(
status, wasi::path_open(dir_fd, 0, "dir/nested/file\0", 0, 0, 0, 0)
wasi_unstable::raw::__WASI_EILSEQ, .expect_err("opening a file with a trailing NUL")
"opening a file with a trailing NUL" .raw_error(),
); wasi::ERRNO_ILSEQ,
assert_eq!( "errno should be ERRNO_ILSEQ",
file_fd,
wasi_unstable::Fd::max_value(),
"failed open should set the file descriptor to -1",
); );
// Now open it with a trailing slash. // 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!( assert_eq!(
file_fd, wasi::path_open(dir_fd, 0, "dir/nested/file/", 0, 0, 0, 0)
wasi_unstable::Fd::max_value(), .expect_err("opening a file with a trailing slash should fail")
"failed open should set the file descriptor to -1", .raw_error(),
wasi::ERRNO_NOTDIR,
"errno should be ERRNO_NOTDIR",
); );
// Now open it with trailing slashes. // 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!( assert_eq!(
file_fd, wasi::path_open(dir_fd, 0, "dir/nested/file///", 0, 0, 0, 0)
wasi_unstable::Fd::max_value(), .expect_err("opening a file with trailing slashes should fail")
"failed open should set the file descriptor to -1", .raw_error(),
wasi::ERRNO_NOTDIR,
"errno should be ERRNO_NOTDIR",
); );
// Now open the directory with a trailing slash. // Now open the directory with a trailing slash.
status = wasi_path_open(dir_fd, 0, "dir/nested/", 0, 0, 0, 0, &mut file_fd); file_fd = wasi::path_open(dir_fd, 0, "dir/nested/", 0, 0, 0, 0)
assert_eq!( .expect("opening a directory with a trailing slash");
status,
wasi_unstable::raw::__WASI_ESUCCESS,
"opening a directory with a trailing slash"
);
assert_gt!( assert_gt!(
file_fd, file_fd,
libc::STDERR_FILENO as wasi_unstable::Fd, libc::STDERR_FILENO as wasi::Fd,
"file descriptor range check", "file descriptor range check",
); );
close_fd(file_fd); wasi::fd_close(file_fd).expect("closing a file");
// Now open the directory with trailing slashes. // Now open the directory with trailing slashes.
status = wasi_path_open(dir_fd, 0, "dir/nested///", 0, 0, 0, 0, &mut file_fd); file_fd = wasi::path_open(dir_fd, 0, "dir/nested///", 0, 0, 0, 0)
assert_eq!( .expect("opening a directory with trailing slashes");
status,
wasi_unstable::raw::__WASI_ESUCCESS,
"opening a directory with trailing slashes"
);
assert_gt!( assert_gt!(
file_fd, file_fd,
libc::STDERR_FILENO as wasi_unstable::Fd, libc::STDERR_FILENO as wasi::Fd,
"file descriptor range check", "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. // Now open it with a path containing too many ".."s.
let bad_path = format!("dir/nested/../../../{}/dir/nested/file", arg); 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!( assert_eq!(
status, wasi::path_open(dir_fd, 0, &bad_path, 0, 0, 0, 0)
wasi_unstable::raw::__WASI_ENOTCAPABLE, .expect_err("opening a file with too many \"..\"s in the path should fail")
"opening a file with too many \"..\"s in the path" .raw_error(),
); wasi::ERRNO_NOTCAPABLE,
assert_eq!( "errno should be ERRNO_NOTCAPABLE",
file_fd, );
wasi_unstable::Fd::max_value(), wasi::path_unlink_file(dir_fd, "dir/nested/file")
"failed open should set the file descriptor to -1", .expect("unlink_file on a symlink should succeed");
); wasi::path_remove_directory(dir_fd, "dir/nested")
assert!( .expect("remove_directory on a directory should succeed");
wasi_path_unlink_file(dir_fd, "dir/nested/file").is_ok(), wasi::path_remove_directory(dir_fd, "dir")
"unlink_file on a symlink should succeed" .expect("remove_directory on a directory 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"
);
} }
fn main() { fn main() {

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

@ -1,31 +1,14 @@
use more_asserts::assert_gt; use more_asserts::assert_gt;
use std::{env, process}; use std::{env, process};
use wasi_old::wasi_unstable;
use wasi_tests::open_scratch_directory; 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. // 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 file_fd =
let status = wasi_path_open( wasi::path_open(dir_fd, 0, "file", wasi::OFLAGS_CREAT, 0, 0, 0).expect("opening a file");
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"
);
assert_gt!( assert_gt!(
file_fd, file_fd,
libc::STDERR_FILENO as wasi_unstable::Fd, libc::STDERR_FILENO as wasi::Fd,
"file descriptor range check", "file descriptor range check",
); );
assert_eq!( assert_eq!(
@ -33,9 +16,8 @@ unsafe fn test_isatty(dir_fd: wasi_unstable::Fd) {
0, 0,
"file is a tty" "file is a tty"
); );
close_fd(file_fd); wasi::fd_close(file_fd).expect("closing a file");
wasi::path_unlink_file(dir_fd, "file").expect("removing a file");
cleanup_file(dir_fd, "file");
} }
fn main() { fn main() {

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

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

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

@ -1,169 +1,124 @@
use more_asserts::assert_gt; use more_asserts::assert_gt;
use std::{env, process}; use std::{env, process};
use wasi_old::wasi_unstable;
use wasi_tests::open_scratch_directory; 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!( assert_ne!(
fdstat.fs_rights_base & wasi_unstable::RIGHT_PATH_FILESTAT_GET, fdstat.fs_rights_base & wasi::RIGHTS_PATH_FILESTAT_GET,
0, 0,
"the scratch directory should have RIGHT_PATH_FILESTAT_GET as base right", "the scratch directory should have RIGHT_PATH_FILESTAT_GET as base right",
); );
assert_ne!( assert_ne!(
fdstat.fs_rights_inheriting & wasi_unstable::RIGHT_PATH_FILESTAT_GET, fdstat.fs_rights_inheriting & wasi::RIGHTS_PATH_FILESTAT_GET,
0, 0,
"the scratch directory should have RIGHT_PATH_FILESTAT_GET as base right", "the scratch directory should have RIGHT_PATH_FILESTAT_GET as base right",
); );
// Create a file in the scratch directory. // Create a file in the scratch directory.
let mut file_fd = wasi_unstable::Fd::max_value() - 1; let file_fd = wasi::path_open(
let filename = "file";
let status = wasi_path_open(
dir_fd, dir_fd,
0, 0,
filename, "file",
wasi_unstable::O_CREAT, wasi::OFLAGS_CREAT,
wasi_unstable::RIGHT_FD_READ wasi::RIGHTS_FD_READ | wasi::RIGHTS_FD_WRITE | wasi::RIGHTS_PATH_FILESTAT_GET,
| wasi_unstable::RIGHT_FD_WRITE
| wasi_unstable::RIGHT_PATH_FILESTAT_GET,
0, 0,
// Pass some flags for later retrieval // Pass some flags for later retrieval
wasi_unstable::FDFLAG_APPEND | wasi_unstable::FDFLAG_SYNC, wasi::FDFLAGS_APPEND | wasi::FDFLAGS_SYNC,
&mut file_fd, )
); .expect("opening a file");
assert_eq!(
status,
wasi_unstable::raw::__WASI_ESUCCESS,
"opening a file"
);
assert_gt!( assert_gt!(
file_fd, file_fd,
libc::STDERR_FILENO as wasi_unstable::Fd, libc::STDERR_FILENO as wasi::Fd,
"file descriptor range check", "file descriptor range check",
); );
let status = wasi_fd_fdstat_get(file_fd, &mut fdstat); fdstat = wasi::fd_fdstat_get(file_fd).expect("fd_fdstat_get");
assert_eq!(status, wasi_unstable::raw::__WASI_ESUCCESS, "fd_fdstat_get");
assert_eq!( assert_eq!(
fdstat.fs_rights_base & wasi_unstable::RIGHT_PATH_FILESTAT_GET, fdstat.fs_rights_base & wasi::RIGHTS_PATH_FILESTAT_GET,
0, 0,
"files shouldn't have rights for path_* syscalls even if manually given", "files shouldn't have rights for path_* syscalls even if manually given",
); );
assert_eq!( assert_eq!(
fdstat.fs_rights_inheriting & wasi_unstable::RIGHT_PATH_FILESTAT_GET, fdstat.fs_rights_inheriting & wasi::RIGHTS_PATH_FILESTAT_GET,
0, 0,
"files shouldn't have rights for path_* syscalls even if manually given", "files shouldn't have rights for path_* syscalls even if manually given",
); );
assert_ne!( assert_ne!(
fdstat.fs_flags & (wasi_unstable::FDFLAG_APPEND | wasi_unstable::FDFLAG_SYNC), fdstat.fs_flags & (wasi::FDFLAGS_APPEND | wasi::FDFLAGS_SYNC),
0, 0,
"file should have the same flags used to create the file" "file should have the same flags used to create the file"
); );
// Check file size // Check file size
let mut stat = wasi_unstable::FileStat { let mut stat = wasi::path_filestat_get(dir_fd, 0, "file").expect("reading file stats");
st_dev: 0, assert_eq!(stat.size, 0, "file size should be 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");
// Check path_filestat_set_times // Check path_filestat_set_times
let old_atim = stat.st_atim; let old_atim = stat.atim;
let new_mtim = stat.st_mtim - 100; let new_mtim = stat.mtim - 100;
assert!( wasi::path_filestat_set_times(
wasi_path_filestat_set_times( dir_fd,
dir_fd, 0,
0, "file",
filename, // on purpose: the syscall should not touch atim, because
// on purpose: the syscall should not touch atim, because // neither of the ATIM flags is set
// neither of the ATIM flags is set new_mtim,
new_mtim, new_mtim,
new_mtim, wasi::FSTFLAGS_MTIM,
wasi_unstable::FILESTAT_SET_MTIM, )
) .expect("path_filestat_set_times should succeed");
.is_ok(),
"path_filestat_set_times should succeed"
);
let status = wasi_path_filestat_get(dir_fd, 0, filename, filename.len(), &mut stat); stat = wasi::path_filestat_get(dir_fd, 0, "file")
assert_eq!( .expect("reading file stats after path_filestat_set_times");
status, assert_eq!(stat.mtim, new_mtim, "mtim should change");
wasi_unstable::raw::__WASI_ESUCCESS, assert_eq!(stat.atim, old_atim, "atim should not change");
"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");
assert_eq!( assert_eq!(
wasi_path_filestat_set_times( wasi::path_filestat_set_times(
dir_fd, dir_fd,
0, 0,
filename, "file",
new_mtim, new_mtim,
new_mtim, new_mtim,
wasi_unstable::FILESTAT_SET_MTIM | wasi_unstable::FILESTAT_SET_MTIM_NOW, wasi::FSTFLAGS_MTIM | wasi::FSTFLAGS_MTIM_NOW,
), )
Err(wasi_unstable::EINVAL), .expect_err("MTIM and MTIM_NOW can't both be set")
"MTIM & MTIM_NOW can't both be set" .raw_error(),
wasi::ERRNO_INVAL,
"errno should be ERRNO_INVAL"
); );
// check if the times were untouched // check if the times were untouched
let status = wasi_path_filestat_get(dir_fd, 0, filename, filename.len(), &mut stat); stat = wasi::path_filestat_get(dir_fd, 0, "file")
assert_eq!( .expect("reading file stats after ERRNO_INVAL fd_filestat_set_times");
status, assert_eq!(stat.mtim, new_mtim, "mtim should not change");
wasi_unstable::raw::__WASI_ESUCCESS, assert_eq!(stat.atim, old_atim, "atim should not change");
"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");
let new_atim = old_atim - 100; let new_atim = old_atim - 100;
assert_eq!( assert_eq!(
wasi_path_filestat_set_times( wasi::path_filestat_set_times(
dir_fd, dir_fd,
0, 0,
filename, "file",
new_atim, new_atim,
new_atim, new_atim,
wasi_unstable::FILESTAT_SET_ATIM | wasi_unstable::FILESTAT_SET_ATIM_NOW, wasi::FSTFLAGS_ATIM | wasi::FSTFLAGS_ATIM_NOW,
), )
Err(wasi_unstable::EINVAL), .expect_err("ATIM & ATIM_NOW can't both be set")
"ATIM & ATIM_NOW can't both be set" .raw_error(),
wasi::ERRNO_INVAL,
"errno should be ERRNO_INVAL"
); );
// check if the times were untouched // check if the times were untouched
let status = wasi_path_filestat_get(dir_fd, 0, filename, filename.len(), &mut stat); stat = wasi::path_filestat_get(dir_fd, 0, "file")
assert_eq!( .expect("reading file stats after ERRNO_INVAL path_filestat_set_times");
status, assert_eq!(stat.mtim, new_mtim, "mtim should not change");
wasi_unstable::raw::__WASI_ESUCCESS, assert_eq!(stat.atim, old_atim, "atim should not change");
"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");
close_fd(file_fd); wasi::fd_close(file_fd).expect("closing a file");
cleanup_file(dir_fd, "file"); wasi::path_unlink_file(dir_fd, "file").expect("removing a file");
} }
fn main() { fn main() {
let mut args = env::args(); 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 more_asserts::assert_gt;
use std::{env, process}; use std::{env, process};
use wasi_old::wasi_unstable; use wasi_tests::{create_file, open_scratch_directory};
use wasi_tests::open_scratch_directory;
use wasi_tests::utils::{cleanup_dir, cleanup_file, create_dir, create_file}; unsafe fn create_or_open(dir_fd: wasi::Fd, name: &str, flags: wasi::Oflags) -> wasi::Fd {
use wasi_tests::wasi_wrappers::{ let file_fd = wasi::path_open(dir_fd, 0, name, flags, 0, 0, 0)
wasi_fd_fdstat_get, wasi_fd_filestat_get, wasi_path_link, wasi_path_open, wasi_path_symlink, .unwrap_or_else(|_| panic!("opening '{}'", name));
};
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
);
assert_gt!( assert_gt!(
file_fd, file_fd,
libc::STDERR_FILENO as wasi_unstable::Fd, libc::STDERR_FILENO as wasi::Fd,
"file descriptor range check", "file descriptor range check",
); );
file_fd file_fd
} }
unsafe fn open_link(dir_fd: wasi_unstable::Fd, name: &str) -> wasi_unstable::Fd { unsafe fn open_link(dir_fd: wasi::Fd, name: &str) -> wasi::Fd {
let mut file_fd = wasi_unstable::Fd::max_value() - 1; let file_fd = wasi::path_open(dir_fd, 0, name, 0, 0, 0, 0)
let status = wasi_path_open(dir_fd, 0, name, 0, 0, 0, 0, &mut file_fd); .unwrap_or_else(|_| panic!("opening a link '{}'", name));
assert_eq!(
status,
wasi_unstable::raw::__WASI_ESUCCESS,
"opening a link '{}'",
name
);
assert_gt!( assert_gt!(
file_fd, file_fd,
libc::STDERR_FILENO as wasi_unstable::Fd, libc::STDERR_FILENO as wasi::Fd,
"file descriptor range check", "file descriptor range check",
); );
file_fd file_fd
} }
unsafe fn check_rights(orig_fd: wasi_unstable::Fd, link_fd: wasi_unstable::Fd) { // This is temporary until `wasi` implements `Debug` and `PartialEq` for
use std::mem::MaybeUninit; // `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 // This is temporary until `wasi` implements `Debug` and `PartialEq` for
let mut orig_filestat: wasi_unstable::FileStat = MaybeUninit::zeroed().assume_init(); // `wasi::Fdstat`.
let mut link_filestat: wasi_unstable::FileStat = MaybeUninit::zeroed().assume_init(); fn fdstats_assert_eq(left: wasi::Fdstat, right: wasi::Fdstat) {
let mut status = wasi_fd_filestat_get(orig_fd, &mut orig_filestat); 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 source"
);
status = wasi_fd_filestat_get(link_fd, &mut link_filestat);
assert_eq!( assert_eq!(
status, left.fs_filetype, right.fs_filetype,
wasi_unstable::raw::__WASI_ESUCCESS, "fs_filetype should be equal"
"reading filestat of the link"
); );
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!( assert_eq!(
status, left.fs_rights_base, right.fs_rights_base,
wasi_unstable::raw::__WASI_ESUCCESS, "fs_rights_base should be equal"
"reading fdstat of the source"
); );
status = wasi_fd_fdstat_get(link_fd, &mut link_fdstat);
assert_eq!( assert_eq!(
status, left.fs_rights_inheriting, right.fs_rights_inheriting,
wasi_unstable::raw::__WASI_ESUCCESS, "fs_rights_inheriting should be equal"
"reading fdstat of the link"
); );
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 // 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 // Create a link in the same directory and compare rights
assert!( wasi::path_link(dir_fd, 0, "file", dir_fd, "link")
wasi_path_link(dir_fd, 0, "file", dir_fd, "link").is_ok(), .expect("creating a link in the same directory");
"creating a link in the same directory"
);
let mut link_fd = open_link(dir_fd, "link"); let mut link_fd = open_link(dir_fd, "link");
check_rights(file_fd, link_fd); 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 a link in a different directory and compare rights
create_dir(dir_fd, "subdir"); wasi::path_create_directory(dir_fd, "subdir").expect("creating a subdirectory");
let subdir_fd = create_or_open(dir_fd, "subdir", wasi_unstable::O_DIRECTORY); let subdir_fd = create_or_open(dir_fd, "subdir", wasi::OFLAGS_DIRECTORY);
assert!( wasi::path_link(dir_fd, 0, "file", subdir_fd, "link").expect("creating a link in subdirectory");
wasi_path_link(dir_fd, 0, "file", subdir_fd, "link").is_ok(),
"creating a link in subdirectory"
);
link_fd = open_link(subdir_fd, "link"); link_fd = open_link(subdir_fd, "link");
check_rights(file_fd, link_fd); check_rights(file_fd, link_fd);
cleanup_file(subdir_fd, "link"); wasi::path_unlink_file(subdir_fd, "link").expect("removing a link");
cleanup_dir(dir_fd, "subdir"); wasi::path_remove_directory(dir_fd, "subdir").expect("removing a subdirectory");
// Create a link to a path that already exists // Create a link to a path that already exists
create_file(dir_fd, "link"); create_file(dir_fd, "link");
assert_eq!( assert_eq!(
wasi_path_link(dir_fd, 0, "file", dir_fd, "link"), wasi::path_link(dir_fd, 0, "file", dir_fd, "link")
Err(wasi_unstable::EEXIST), .expect_err("creating a link to existing path should fail")
"creating a link to existing path" .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 // Create a link to itself
assert_eq!( assert_eq!(
wasi_path_link(dir_fd, 0, "file", dir_fd, "file"), wasi::path_link(dir_fd, 0, "file", dir_fd, "file")
Err(wasi_unstable::EEXIST), .expect_err("creating a link to itself should fail")
"creating a link to itself" .raw_error(),
wasi::ERRNO_EXIST,
"errno should be ERRNO_EXIST"
); );
// Create a link where target is a directory // 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!( assert_eq!(
wasi_path_link(dir_fd, 0, "file", dir_fd, "link"), wasi::path_link(dir_fd, 0, "file", dir_fd, "link")
Err(wasi_unstable::EEXIST), .expect_err("creating a link where target is a directory should fail")
"creating a link where target is a directory" .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 a link to a directory
create_dir(dir_fd, "subdir"); wasi::path_create_directory(dir_fd, "subdir").expect("creating a subdirectory");
create_or_open(dir_fd, "subdir", wasi_unstable::O_DIRECTORY); create_or_open(dir_fd, "subdir", wasi::OFLAGS_DIRECTORY);
assert_eq!( assert_eq!(
wasi_path_link(dir_fd, 0, "subdir", dir_fd, "link"), wasi::path_link(dir_fd, 0, "subdir", dir_fd, "link")
Err(wasi_unstable::EPERM), .expect_err("creating a link to a directory should fail")
"creating a link to a directory" .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 // Create a link to a file with trailing slash
assert_eq!( assert_eq!(
wasi_path_link(dir_fd, 0, "file", dir_fd, "link/"), wasi::path_link(dir_fd, 0, "file", dir_fd, "link/")
Err(wasi_unstable::ENOENT), .expect_err("creating a link to a file with trailing slash should fail")
"creating a link to a file with trailing slash" .raw_error(),
wasi::ERRNO_NOENT,
"errno should be ERRNO_NOENT"
); );
// Create a link to a dangling symlink // Create a link to a dangling symlink
assert!( wasi::path_symlink("target", dir_fd, "symlink").expect("creating a dangling symlink");
wasi_path_symlink("target", dir_fd, "symlink").is_ok(),
"creating a dangling symlink"
);
assert_eq!( assert_eq!(
wasi_path_link(dir_fd, 0, "symlink", dir_fd, "link"), wasi::path_link(dir_fd, 0, "symlink", dir_fd, "link")
Err(wasi_unstable::ENOENT), .expect_err("creating a link to a dangling symlink should fail")
"creating a link to a dangling symlink" .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 // Create a link to a symlink loop
assert!( wasi::path_symlink("symlink", dir_fd, "symlink").expect("creating a symlink loop");
wasi_path_symlink("symlink", dir_fd, "symlink").is_ok(),
"creating a symlink loop"
);
assert_eq!( assert_eq!(
wasi_path_link(dir_fd, 0, "symlink", dir_fd, "link"), wasi::path_link(dir_fd, 0, "symlink", dir_fd, "link")
Err(wasi_unstable::ELOOP), .expect_err("creating a link to a symlink loop")
"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 // Create a link where target is a dangling symlink
assert!( wasi::path_symlink("target", dir_fd, "symlink").expect("creating a dangling symlink");
wasi_path_symlink("target", dir_fd, "symlink").is_ok(),
"creating a dangling symlink"
);
assert_eq!( assert_eq!(
wasi_path_link(dir_fd, 0, "file", dir_fd, "symlink"), wasi::path_link(dir_fd, 0, "file", dir_fd, "symlink")
Err(wasi_unstable::EEXIST), .expect_err("creating a link where target is a dangling symlink")
"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 // Create a link to a file following symlinks
assert!( wasi::path_symlink("file", dir_fd, "symlink").expect("creating a valid symlink");
wasi_path_symlink("file", dir_fd, "symlink").is_ok(), wasi::path_link(
"creating a valid symlink" dir_fd,
); wasi::LOOKUPFLAGS_SYMLINK_FOLLOW,
assert!( "symlink",
wasi_path_link( dir_fd,
dir_fd, "link",
wasi_unstable::LOOKUP_SYMLINK_FOLLOW, )
"symlink", .expect("creating a link to a file following symlinks");
dir_fd,
"link"
)
.is_ok(),
"creating a link to a file following symlinks",
);
link_fd = open_link(dir_fd, "link"); link_fd = open_link(dir_fd, "link");
check_rights(file_fd, link_fd); check_rights(file_fd, link_fd);
cleanup_file(dir_fd, "link"); wasi::path_unlink_file(dir_fd, "link").expect("removing a link");
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 following symlinks // Create a link where target is a dangling symlink following symlinks
assert!( wasi::path_symlink("target", dir_fd, "symlink").expect("creating a dangling symlink");
wasi_path_symlink("target", dir_fd, "symlink").is_ok(),
"creating a dangling symlink"
);
assert_eq!( assert_eq!(
wasi_path_link( wasi::path_link(
dir_fd, dir_fd,
wasi_unstable::LOOKUP_SYMLINK_FOLLOW, wasi::LOOKUPFLAGS_SYMLINK_FOLLOW,
"symlink", "symlink",
dir_fd, dir_fd,
"link" "link",
), )
Err(wasi_unstable::ENOENT), .expect_err("creating a link where target is a dangling symlink following symlinks")
"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 // Create a link to a symlink loop following symlinks
assert!( wasi::path_symlink("symlink", dir_fd, "symlink").expect("creating a symlink loop");
wasi_path_symlink("symlink", dir_fd, "symlink").is_ok(),
"creating a symlink loop"
);
assert_eq!( assert_eq!(
wasi_path_link( wasi::path_link(
dir_fd, dir_fd,
wasi_unstable::LOOKUP_SYMLINK_FOLLOW, wasi::LOOKUPFLAGS_SYMLINK_FOLLOW,
"symlink", "symlink",
dir_fd, dir_fd,
"link" "link",
), )
Err(wasi_unstable::ELOOP), .expect_err("creating a link to a symlink loop following symlinks")
"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. // Clean up.
cleanup_file(dir_fd, "file"); wasi::path_unlink_file(dir_fd, "file").expect("removing a file");
} }
fn main() { fn main() {

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

@ -1,44 +1,24 @@
use std::{env, process}; use std::{env, process};
use wasi_old::wasi_unstable; use wasi_tests::{create_file, open_scratch_directory};
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_path_open_create_existing(dir_fd: wasi_unstable::Fd) { unsafe fn test_path_open_create_existing(dir_fd: wasi::Fd) {
let mut fd: wasi_unstable::Fd = wasi_unstable::Fd::max_value() - 1; create_file(dir_fd, "file");
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,
);
assert_eq!( assert_eq!(
status, wasi::path_open(
wasi_unstable::raw::__WASI_EEXIST, dir_fd,
"trying to create a file that already exists" 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() { 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 std::{env, process};
use wasi_old::wasi_unstable;
use wasi_tests::open_scratch_directory; 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. // Open a file.
let mut file_fd: wasi_unstable::Fd = wasi_unstable::Fd::max_value() - 1; let file_fd =
let mut status = wasi_path_open( wasi::path_open(dir_fd, 0, "file", wasi::OFLAGS_CREAT, 0, 0, 0).expect("opening a file");
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"
);
// Now try to open a file underneath it as if it were a directory. // 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!( assert_eq!(
status, wasi::path_open(file_fd, 0, "foo", wasi::OFLAGS_CREAT, 0, 0, 0)
wasi_unstable::raw::__WASI_ENOTDIR, .expect_err("non-directory base fd should get ERRNO_NOTDIR")
"non-directory base fd should get ENOTDIR" .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() { fn main() {

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

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

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

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

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

@ -1,60 +1,65 @@
use std::{env, process}; use std::{env, process};
use wasi_old::wasi_unstable; use wasi_tests::{create_file, open_scratch_directory};
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;
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. // Link destination shouldn't end with a slash.
assert_eq!( assert_eq!(
wasi_path_symlink("source", dir_fd, "target/"), wasi::path_symlink("source", dir_fd, "target/")
Err(wasi_unstable::ENOENT), .expect_err("link destination ending with a slash should fail")
"link destination ending with a slash" .raw_error(),
wasi::ERRNO_NOENT,
"errno should be ERRNO_NOENT"
); );
// Without the trailing slash, this should succeed. // Without the trailing slash, this should succeed.
assert_eq!( wasi::path_symlink("source", dir_fd, "target").expect("link destination ending with a slash");
wasi_path_symlink("source", dir_fd, "target"), wasi::path_unlink_file(dir_fd, "target").expect("removing a file");
Ok(()),
"link destination ending with a slash"
);
cleanup_file(dir_fd, "target");
// Link destination already exists, target has trailing slash. // 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!( assert_eq!(
wasi_path_symlink("source", dir_fd, "target/"), wasi::path_symlink("source", dir_fd, "target/")
Err(wasi_unstable::EEXIST), .expect_err("link destination already exists")
"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. // 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!( assert_eq!(
wasi_path_symlink("source", dir_fd, "target"), wasi::path_symlink("source", dir_fd, "target")
Err(wasi_unstable::EEXIST), .expect_err("link destination already exists")
"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. // Link destination already exists, target has trailing slash.
create_file(dir_fd, "target"); create_file(dir_fd, "target");
assert_eq!( assert_eq!(
wasi_path_symlink("source", dir_fd, "target/"), wasi::path_symlink("source", dir_fd, "target/")
Err(wasi_unstable::EEXIST), .expect_err("link destination already exists")
"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. // Link destination already exists, target has no trailing slash.
create_file(dir_fd, "target"); create_file(dir_fd, "target");
assert_eq!( assert_eq!(
wasi_path_symlink("source", dir_fd, "target"), wasi::path_symlink("source", dir_fd, "target")
Err(wasi_unstable::EEXIST), .expect_err("link destination already exists")
"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() { fn main() {

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

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

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

@ -1,32 +1,22 @@
use std::{env, process}; use std::{env, process};
use wasi_old::wasi_unstable;
use wasi_tests::open_scratch_directory; 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. // First create a dangling symlink.
assert!( wasi::path_symlink("target", dir_fd, "symlink").expect("creating a symlink");
wasi_path_symlink("target", dir_fd, "symlink").is_ok(),
"creating a symlink"
);
// Readlink it into a non-existent buffer. // Readlink it into a non-existent buffer.
let mut bufused: usize = 1; let bufused = wasi::path_readlink(dir_fd, "symlink", (&mut []).as_mut_ptr(), 0)
let status = wasi_path_readlink(dir_fd, "symlink", &mut [], &mut bufused); .expect("readlink with a 0-sized buffer should succeed");
assert_eq!(
status,
wasi_unstable::raw::__WASI_ESUCCESS,
"readlink with a 0-sized buffer should succeed"
);
assert_eq!( assert_eq!(
bufused, 0, bufused, 0,
"readlink with a 0-sized buffer should return 'bufused' 0" "readlink with a 0-sized buffer should return 'bufused' 0"
); );
// Clean up. // Clean up.
cleanup_file(dir_fd, "symlink"); wasi::path_unlink_file(dir_fd, "symlink").expect("removing a file");
} }
fn main() { fn main() {
let mut args = env::args(); let mut args = env::args();
let prog = args.next().unwrap(); 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 std::{env, process};
use wasi_old::wasi_unstable; use wasi_tests::{create_file, open_scratch_directory};
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;
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 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. // Test that removing it succeeds.
assert_eq!( wasi::path_remove_directory(dir_fd, "dir")
wasi_path_remove_directory(dir_fd, "dir"), .expect("remove_directory on a directory should succeed");
Ok(()),
"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. // Test that removing it with a trailing flash succeeds.
assert_eq!( wasi::path_remove_directory(dir_fd, "dir/")
wasi_path_remove_directory(dir_fd, "dir/"), .expect("remove_directory with a trailing slash on a directory should succeed");
Ok(()),
"remove_directory with a trailing slash on a directory should succeed"
);
// Create a temporary file. // Create a temporary file.
create_file(dir_fd, "file"); create_file(dir_fd, "file");
// Test that removing it with no trailing flash fails. // Test that removing it with no trailing flash fails.
assert_eq!( assert_eq!(
wasi_path_remove_directory(dir_fd, "file"), wasi::path_remove_directory(dir_fd, "file")
Err(wasi_unstable::ENOTDIR), .expect_err("remove_directory without a trailing slash on a file should fail")
"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. // Test that removing it with a trailing flash fails.
assert_eq!( assert_eq!(
wasi_path_remove_directory(dir_fd, "file/"), wasi::path_remove_directory(dir_fd, "file/")
Err(wasi_unstable::ENOTDIR), .expect_err("remove_directory with a trailing slash on a file should fail")
"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() { fn main() {

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

@ -1,29 +1,26 @@
use std::{env, process}; use std::{env, process};
use wasi_old::wasi_unstable;
use wasi_tests::open_scratch_directory; 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 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 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. // Test that attempting to unlink the first directory returns the expected error code.
assert_eq!( assert_eq!(
wasi_path_remove_directory(dir_fd, "dir"), wasi::path_remove_directory(dir_fd, "dir")
Err(wasi_unstable::ENOTEMPTY), .expect_err("remove_directory on a directory should return ENOTEMPTY")
"remove_directory on a directory should return ENOTEMPTY", .raw_error(),
wasi::ERRNO_NOTEMPTY,
"errno should be ERRNO_NOTEMPTY",
); );
// Removing the directories. // Removing the directories.
assert!( wasi::path_remove_directory(dir_fd, "dir/nested")
wasi_path_remove_directory(dir_fd, "dir/nested").is_ok(), .expect("remove_directory on a nested directory should succeed");
"remove_directory on a nested directory should succeed", wasi::path_remove_directory(dir_fd, "dir").expect("removing a directory");
);
cleanup_dir(dir_fd, "dir");
} }
fn main() { fn main() {

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

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

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

@ -1,10 +1,8 @@
use wasi_old::wasi_unstable; unsafe fn test_sched_yield() {
wasi::sched_yield().expect("sched_yield");
fn test_sched_yield() {
assert!(wasi_unstable::sched_yield().is_ok(), "sched_yield");
} }
fn main() { fn main() {
// Run tests // 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_tests::{STDERR_FD, STDIN_FD, STDOUT_FD};
use wasi_old::wasi_unstable;
use wasi_tests::wasi_wrappers::wasi_fd_fdstat_get;
unsafe fn test_stdio() { unsafe fn test_stdio() {
for fd in &[ for fd in &[STDIN_FD, STDOUT_FD, STDERR_FD] {
wasi_unstable::STDIN_FD, wasi::fd_fdstat_get(*fd).expect("fd_fdstat_get on stdio");
wasi_unstable::STDOUT_FD, wasi::fd_renumber(*fd, *fd + 100).expect("renumbering stdio");
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",
);
} }
} }

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

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

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

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

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

@ -1,46 +1,46 @@
use std::{env, process}; use std::{env, process};
use wasi_old::wasi_unstable; use wasi_tests::{create_file, open_scratch_directory};
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;
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 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. // Test that unlinking it fails.
assert_eq!( assert_eq!(
wasi_path_unlink_file(dir_fd, "dir"), wasi::path_unlink_file(dir_fd, "dir")
Err(wasi_unstable::EISDIR), .expect_err("unlink_file on a directory should fail")
"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. // Test that unlinking it with a trailing flash fails.
assert_eq!( assert_eq!(
wasi_path_unlink_file(dir_fd, "dir/"), wasi::path_unlink_file(dir_fd, "dir/")
Err(wasi_unstable::EISDIR), .expect_err("unlink_file on a directory should fail")
"unlink_file on a directory should fail" .raw_error(),
wasi::ERRNO_ISDIR,
"errno should be ERRNO_ISDIR"
); );
// Clean up. // Clean up.
cleanup_dir(dir_fd, "dir"); wasi::path_remove_directory(dir_fd, "dir").expect("removing a directory");
// Create a temporary file. // Create a temporary file.
create_file(dir_fd, "file"); create_file(dir_fd, "file");
// Test that unlinking it with a trailing flash fails. // Test that unlinking it with a trailing flash fails.
assert_eq!( assert_eq!(
wasi_path_unlink_file(dir_fd, "file/"), wasi::path_unlink_file(dir_fd, "file/")
Err(wasi_unstable::ENOTDIR), .expect_err("unlink_file with a trailing slash should fail")
"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. // Test that unlinking it with no trailing flash succeeds.
assert_eq!( wasi::path_unlink_file(dir_fd, "file")
wasi_path_unlink_file(dir_fd, "file"), .expect("unlink_file with no trailing slash should succeed");
Ok(()),
"unlink_file with no trailing slash should succeed"
);
} }
fn main() { fn main() {

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

@ -1,39 +1,14 @@
pub mod utils; use more_asserts::assert_gt;
pub mod wasi_wrappers;
use libc; // The `wasi` crate version 0.9.0 and beyond, doesn't
use std::ffi::CString; // seem to define these constants, so we do it ourselves.
use std::io; pub const STDIN_FD: wasi::Fd = 0x0;
use wasi_old::wasi_unstable; 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 /// 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 /// directory.
/// `wasi_snapshot_preview1`. This is getting phased out and will likely be pub fn open_scratch_directory(path: &str) -> Result<wasi::Fd, String> {
/// 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> {
unsafe { unsafe {
for i in 3.. { for i in 3.. {
let stat = match wasi::fd_prestat_get(i) { 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")) 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