Skip to content

A lightweight WebAssembly runtime that is fast, secure, and standards-compliant

Migrate wasip1 tests to wasip2 when duplicated in wasi-testsuite (#14428)

Alex Crichtona26724f25844
diff --git a/crates/test-programs/src/bin/p1_big_random_buf.rs b/crates/test-programs/src/bin/p1_big_random_buf.rs
deleted file mode 100644
index d6b9709867..0000000000
--- a/crates/test-programs/src/bin/p1_big_random_buf.rs
+++ /dev/null
@@ -1,15 +0,0 @@
-fn test_big_random_buf() {
- let mut buf = Vec::new();
- buf.resize(1024, 0);
- unsafe {
- wasip1::random_get(buf.as_mut_ptr(), 1024).expect("failed to call random_get");
- }
- // Chances are pretty good that at least *one* byte will be non-zero in
- // any meaningful random function producing 1024 u8 values.
- assert!(buf.iter().any(|x| *x != 0), "random_get returned all zeros");
-}
-
-fn main() {
- // Run the tests.
- test_big_random_buf()
-}
diff --git a/crates/test-programs/src/bin/p1_clock_time_get.rs b/crates/test-programs/src/bin/p1_clock_time_get.rs
deleted file mode 100644
index 608021e426..0000000000
--- a/crates/test-programs/src/bin/p1_clock_time_get.rs
+++ /dev/null
@@ -1,20 +0,0 @@
-#![expect(unsafe_op_in_unsafe_fn, reason = "old code, not worth updating yet")]
-
-unsafe fn test_clock_time_get() {
- // Test that clock_time_get succeeds. Even in environments where it's not
- // desirable to expose high-precision timers, it should still succeed.
- // clock_res_get is where information about precision can be provided.
- wasip1::clock_time_get(wasip1::CLOCKID_MONOTONIC, 1).expect("precision 1 should work");
-
- let first_time =
- wasip1::clock_time_get(wasip1::CLOCKID_MONOTONIC, 0).expect("precision 0 should work");
-
- let time =
- wasip1::clock_time_get(wasip1::CLOCKID_MONOTONIC, 0).expect("re-fetch time should work");
- assert!(first_time <= time, "CLOCK_MONOTONIC should be monotonic");
-}
-
-fn main() {
- // Run the tests.
- unsafe { test_clock_time_get() }
-}
diff --git a/crates/test-programs/src/bin/p1_close_preopen.rs b/crates/test-programs/src/bin/p1_close_preopen.rs
deleted file mode 100644
index 263b08bde2..0000000000
--- a/crates/test-programs/src/bin/p1_close_preopen.rs
+++ /dev/null
@@ -1,50 +0,0 @@
-#![expect(unsafe_op_in_unsafe_fn, reason = "old code, not worth updating yet")]
-
-use std::{env, process};
-use test_programs::preview1::{assert_errno, open_scratch_directory};
-
-unsafe fn test_close_preopen(dir_fd: wasip1::Fd) {
- let pre_fd: wasip1::Fd = (libc::STDERR_FILENO + 1) as wasip1::Fd;
-
- assert!(dir_fd > pre_fd, "dir_fd number");
-
- // Try to close a preopened directory handle.
- wasip1::fd_close(pre_fd).expect("closing a preopened file descriptor");
-
- // Ensure that dir_fd is still open.
- let dir_fdstat = wasip1::fd_fdstat_get(dir_fd).expect("failed fd_fdstat_get");
- assert_eq!(
- dir_fdstat.fs_filetype,
- wasip1::FILETYPE_DIRECTORY,
- "expected the scratch directory to be a directory",
- );
-
- // Ensure that pre_fd is closed.
- assert_errno!(
- wasip1::fd_fdstat_get(pre_fd).expect_err("failed fd_fdstat_get"),
- wasip1::ERRNO_BADF
- );
-}
-
-fn main() {
- let mut args = env::args();
- let prog = args.next().unwrap();
- let arg = if let Some(arg) = args.next() {
- arg
- } else {
- eprintln!("usage: {prog} <scratch directory>");
- process::exit(1);
- };
-
- // Open scratch directory
- let dir_fd = match open_scratch_directory(&arg) {
- Ok(dir_fd) => dir_fd,
- Err(err) => {
- eprintln!("{err}");
- process::exit(1)
- }
- };
-
- // Run the tests.
- unsafe { test_close_preopen(dir_fd) }
-}
diff --git a/crates/test-programs/src/bin/p1_dangling_fd.rs b/crates/test-programs/src/bin/p1_dangling_fd.rs
deleted file mode 100644
index b9f9549269..0000000000
--- a/crates/test-programs/src/bin/p1_dangling_fd.rs
+++ /dev/null
@@ -1,55 +0,0 @@
-#![expect(unsafe_op_in_unsafe_fn, reason = "old code, not worth updating yet")]
-
-use std::{env, process};
-use test_programs::preview1::{config, open_scratch_directory};
-
-unsafe fn test_dangling_fd(dir_fd: wasip1::Fd) {
- if config().support_dangling_filesystem() {
- // Create a file, open it, delete it without closing the handle,
- // and then try creating it again
- let fd = wasip1::path_open(dir_fd, 0, "file", wasip1::OFLAGS_CREAT, 0, 0, 0).unwrap();
- wasip1::fd_close(fd).unwrap();
- let file_fd = wasip1::path_open(dir_fd, 0, "file", 0, 0, 0, 0).expect("failed to open");
- assert!(
- file_fd > libc::STDERR_FILENO as wasip1::Fd,
- "file descriptor range check",
- );
- wasip1::path_unlink_file(dir_fd, "file").expect("failed to unlink");
- let fd = wasip1::path_open(dir_fd, 0, "file", wasip1::OFLAGS_CREAT, 0, 0, 0).unwrap();
- wasip1::fd_close(fd).unwrap();
-
- // Now, repeat the same process but for a directory
- wasip1::path_create_directory(dir_fd, "subdir").expect("failed to create dir");
- let subdir_fd = wasip1::path_open(dir_fd, 0, "subdir", wasip1::OFLAGS_DIRECTORY, 0, 0, 0)
- .expect("failed to open dir");
- assert!(
- subdir_fd > libc::STDERR_FILENO as wasip1::Fd,
- "file descriptor range check",
- );
- wasip1::path_remove_directory(dir_fd, "subdir").expect("failed to remove dir 2");
- wasip1::path_create_directory(dir_fd, "subdir").expect("failed to create dir 2");
- }
-}
-
-fn main() {
- let mut args = env::args();
- let prog = args.next().unwrap();
- let arg = if let Some(arg) = args.next() {
- arg
- } else {
- eprintln!("usage: {prog} <scratch directory>");
- process::exit(1);
- };
-
- // Open scratch directory
- let dir_fd = match open_scratch_directory(&arg) {
- Ok(dir_fd) => dir_fd,
- Err(err) => {
- eprintln!("{err}");
- process::exit(1)
- }
- };
-
- // Run the tests.
- unsafe { test_dangling_fd(dir_fd) }
-}
diff --git a/crates/test-programs/src/bin/p1_dangling_symlink.rs b/crates/test-programs/src/bin/p1_dangling_symlink.rs
deleted file mode 100644
index 6f52a39fa8..0000000000
--- a/crates/test-programs/src/bin/p1_dangling_symlink.rs
+++ /dev/null
@@ -1,54 +0,0 @@
-#![expect(unsafe_op_in_unsafe_fn, reason = "old code, not worth updating yet")]
-
-use std::{env, process};
-use test_programs::preview1::{assert_errno, config, open_scratch_directory};
-
-unsafe fn test_dangling_symlink(dir_fd: wasip1::Fd) {
- if config().support_dangling_filesystem() {
- // First create a dangling symlink.
- wasip1::path_symlink("target", dir_fd, "symlink").expect("creating a symlink");
-
- // Try to open it as a directory with O_NOFOLLOW.
- assert_errno!(
- wasip1::path_open(dir_fd, 0, "symlink", wasip1::OFLAGS_DIRECTORY, 0, 0, 0)
- .expect_err("opening a dangling symlink as a directory"),
- wasip1::ERRNO_NOTDIR,
- wasip1::ERRNO_LOOP,
- wasip1::ERRNO_NOENT
- );
-
- // Try to open it as a file with O_NOFOLLOW.
- assert_errno!(
- wasip1::path_open(dir_fd, 0, "symlink", 0, 0, 0, 0)
- .expect_err("opening a dangling symlink as a file"),
- wasip1::ERRNO_LOOP,
- wasip1::ERRNO_NOENT
- );
-
- // Clean up.
- wasip1::path_unlink_file(dir_fd, "symlink").expect("failed to remove file");
- }
-}
-
-fn main() {
- let mut args = env::args();
- let prog = args.next().unwrap();
- let arg = if let Some(arg) = args.next() {
- arg
- } else {
- eprintln!("usage: {prog} <scratch directory>");
- process::exit(1);
- };
-
- // Open scratch directory
- let dir_fd = match open_scratch_directory(&arg) {
- Ok(dir_fd) => dir_fd,
- Err(err) => {
- eprintln!("{err}");
- process::exit(1)
- }
- };
-
- // Run the tests.
- unsafe { test_dangling_symlink(dir_fd) }
-}
diff --git a/crates/test-programs/src/bin/p1_dir_fd_op_failures.rs b/crates/test-programs/src/bin/p1_dir_fd_op_failures.rs
deleted file mode 100644
index d4e288e7b4..0000000000
--- a/crates/test-programs/src/bin/p1_dir_fd_op_failures.rs
+++ /dev/null
@@ -1,117 +0,0 @@
-#![expect(unsafe_op_in_unsafe_fn, reason = "old code, not worth updating yet")]
-
-use std::{env, process};
-use test_programs::preview1::open_scratch_directory;
-
-unsafe fn test_fd_dir_ops(dir_fd: wasip1::Fd) {
- let stat = wasip1::fd_filestat_get(dir_fd).expect("failed to fdstat");
- assert_eq!(stat.filetype, wasip1::FILETYPE_DIRECTORY);
-
- let (pr_fd, pr_name_len) = (3..)
- .map_while(|fd| wasip1::fd_prestat_get(fd).ok().map(|stat| (fd, stat)))
- .find_map(|(fd, wasip1::Prestat { tag, u })| {
- (tag == wasip1::PREOPENTYPE_DIR.raw()).then_some((fd, u.dir.pr_name_len))
- })
- .expect("failed to find preopen directory");
-
- let mut pr_name = vec![];
- let r = wasip1::fd_prestat_dir_name(pr_fd, pr_name.as_mut_ptr(), 0);
- assert_eq!(r, Err(wasip1::ERRNO_NAMETOOLONG));
-
- // Test that passing a larger than necessary buffer works correctly
- let mut pr_name = vec![0; pr_name_len + 1];
- let r = wasip1::fd_prestat_dir_name(pr_fd, pr_name.as_mut_ptr(), pr_name_len + 1);
- assert_eq!(r, Ok(()));
-
- let mut read_buf = vec![0; 128].into_boxed_slice();
- let iovec = wasip1::Iovec {
- buf: read_buf.as_mut_ptr(),
- buf_len: read_buf.len(),
- };
- let r = wasip1::fd_read(dir_fd, &[iovec]);
- // On posix, this fails with ERRNO_ISDIR:
- assert_eq!(r, Err(wasip1::ERRNO_BADF), "fd_read error");
-
- let r = wasip1::fd_pread(dir_fd, &[iovec], 0);
- // On posix, this fails with ERRNO_ISDIR
- assert_eq!(r, Err(wasip1::ERRNO_BADF), "fd_pread error");
-
- let write_buf = vec![0; 128].into_boxed_slice();
- let ciovec = wasip1::Ciovec {
- buf: write_buf.as_ptr(),
- buf_len: write_buf.len(),
- };
- let r = wasip1::fd_write(dir_fd, &[ciovec]);
- // Same behavior as specified by POSIX:
- assert_eq!(r, Err(wasip1::ERRNO_BADF), "fd_write error");
-
- let r = wasip1::fd_pwrite(dir_fd, &[ciovec], 0);
- // Same behavior as specified by POSIX:
- assert_eq!(r, Err(wasip1::ERRNO_BADF), "fd_pwrite error");
-
- // Divergence from posix: lseek(dirfd) will return 0
- let r = wasip1::fd_seek(dir_fd, 0, wasip1::WHENCE_CUR);
- assert_eq!(r, Err(wasip1::ERRNO_BADF), "fd_seek WHENCE_CUR error");
- let r = wasip1::fd_seek(dir_fd, 0, wasip1::WHENCE_SET);
- assert_eq!(r, Err(wasip1::ERRNO_BADF), "fd_seek WHENCE_SET error");
- let r = wasip1::fd_seek(dir_fd, 0, wasip1::WHENCE_END);
- assert_eq!(r, Err(wasip1::ERRNO_BADF), "fd_seek WHENCE_END error");
-
- // Tell isn't in posix, its basically lseek with WHENCE_CUR above
- let r = wasip1::fd_tell(dir_fd);
- assert_eq!(r, Err(wasip1::ERRNO_BADF), "fd_tell error");
-
- // posix_fadvise(dirfd, 0, 0, POSIX_FADV_DONTNEED) will return 0 on linux.
- // not available on mac os.
- let r = wasip1::fd_advise(dir_fd, 0, 0, wasip1::ADVICE_DONTNEED);
- assert_eq!(r, Err(wasip1::ERRNO_BADF), "fd_advise error");
-
- // fallocate(dirfd, FALLOC_FL_ZERO_RANGE, 0, 1) will fail with errno EBADF on linux.
- // not available on mac os.
- let r = wasip1::fd_allocate(dir_fd, 0, 0);
- assert_eq!(r, Err(wasip1::ERRNO_BADF), "fd_allocate error");
-
- // fdatasync(dirfd) will return 0 on linux.
- // not available on mac os.
- let r = wasip1::fd_datasync(dir_fd);
- assert_eq!(r, Err(wasip1::ERRNO_BADF), "fd_datasync error");
-
- // fsync(dirfd) will return 0 on linux.
- // not available on mac os.
- let r = wasip1::fd_sync(dir_fd);
- assert_eq!(r, Err(wasip1::ERRNO_BADF), "fd_sync error");
-
- // fcntl(dirfd, F_SETFL, O_NONBLOCK) will return 0 on linux.
- // not available on mac os.
- let r = wasip1::fd_fdstat_set_flags(dir_fd, wasip1::FDFLAGS_NONBLOCK);
- assert_eq!(r, Err(wasip1::ERRNO_BADF), "fd_fdstat_set_flags error");
-
- // ftruncate(dirfd, 1) will fail with errno EINVAL on posix.
- // here, we fail with EBADF instead:
- let r = wasip1::fd_filestat_set_size(dir_fd, 0);
- assert_eq!(r, Err(wasip1::ERRNO_BADF), "fd_filestat_set_size error");
-}
-
-fn main() {
- let mut args = env::args();
- let prog = args.next().unwrap();
- let arg = if let Some(arg) = args.next() {
- arg
- } else {
- eprintln!("usage: {prog} <scratch directory>");
- process::exit(1);
- };
-
- // Open scratch directory
- let dir_fd = match open_scratch_directory(&arg) {
- Ok(dir_fd) => dir_fd,
- Err(err) => {
- eprintln!("{err}");
- process::exit(1)
- }
- };
-
- unsafe {
- test_fd_dir_ops(dir_fd);
- }
-}
diff --git a/crates/test-programs/src/bin/p1_directory_seek.rs b/crates/test-programs/src/bin/p1_directory_seek.rs
deleted file mode 100644
index abf96c3c04..0000000000
--- a/crates/test-programs/src/bin/p1_directory_seek.rs
+++ /dev/null
@@ -1,50 +0,0 @@
-#![expect(unsafe_op_in_unsafe_fn, reason = "old code, not worth updating yet")]
-
-use std::{env, process};
-use test_programs::preview1::{assert_errno, open_scratch_directory};
-
-unsafe fn test_directory_seek(dir_fd: wasip1::Fd) {
- // Create a directory in the scratch directory.
- wasip1::path_create_directory(dir_fd, "dir").expect("failed to make directory");
-
- // Open the directory and attempt to request rights for seeking.
- let fd = wasip1::path_open(dir_fd, 0, "dir", wasip1::OFLAGS_DIRECTORY, 0, 0, 0)
- .expect("failed to open file");
- assert!(
- fd > libc::STDERR_FILENO as wasip1::Fd,
- "file descriptor range check",
- );
-
- // Attempt to seek.
- assert_errno!(
- wasip1::fd_seek(fd, 0, wasip1::WHENCE_CUR).expect_err("seek on a directory"),
- wasip1::ERRNO_BADF
- );
-
- // Clean up.
- wasip1::fd_close(fd).expect("failed to close fd");
- wasip1::path_remove_directory(dir_fd, "dir").expect("failed to remove dir");
-}
-
-fn main() {
- let mut args = env::args();
- let prog = args.next().unwrap();
- let arg = if let Some(arg) = args.next() {
- arg
- } else {
- eprintln!("usage: {prog} <scratch directory>");
- process::exit(1);
- };
-
- // Open scratch directory
- let dir_fd = match open_scratch_directory(&arg) {
- Ok(dir_fd) => dir_fd,
- Err(err) => {
- eprintln!("{err}");
- process::exit(1)
- }
- };
-
- // Run the tests.
- unsafe { test_directory_seek(dir_fd) }
-}
diff --git a/crates/test-programs/src/bin/p1_fd_advise.rs b/crates/test-programs/src/bin/p1_fd_advise.rs
deleted file mode 100644
index 396a8ed389..0000000000
--- a/crates/test-programs/src/bin/p1_fd_advise.rs
+++ /dev/null
@@ -1,64 +0,0 @@
-#![expect(unsafe_op_in_unsafe_fn, reason = "old code, not worth updating yet")]
-
-use std::{env, process};
-use test_programs::preview1::open_scratch_directory;
-
-unsafe fn test_fd_advise(dir_fd: wasip1::Fd) {
- // Create a file in the scratch directory.
- let file_fd = wasip1::path_open(
- dir_fd,
- 0,
- "file",
- wasip1::OFLAGS_CREAT,
- wasip1::RIGHTS_FD_READ | wasip1::RIGHTS_FD_WRITE,
- 0,
- 0,
- )
- .expect("failed to open file");
- assert!(
- file_fd > libc::STDERR_FILENO as wasip1::Fd,
- "file descriptor range check",
- );
-
- // Check file size
- let stat = wasip1::fd_filestat_get(file_fd).expect("failed to fdstat");
- assert_eq!(stat.size, 0, "file size should be 0");
-
- // set_size it bigger
- wasip1::fd_filestat_set_size(file_fd, 100).expect("setting size");
-
- let stat = wasip1::fd_filestat_get(file_fd).expect("failed to fdstat 2");
- assert_eq!(stat.size, 100, "file size should be 100");
-
- // Advise the kernel
- wasip1::fd_advise(file_fd, 10, 50, wasip1::ADVICE_NORMAL).expect("failed advise");
-
- // Advise shouldn't change size
- let stat = wasip1::fd_filestat_get(file_fd).expect("failed to fdstat 3");
- assert_eq!(stat.size, 100, "file size should be 100");
-
- wasip1::fd_close(file_fd).expect("failed to close");
- wasip1::path_unlink_file(dir_fd, "file").expect("failed to unlink");
-}
-fn main() {
- let mut args = env::args();
- let prog = args.next().unwrap();
- let arg = if let Some(arg) = args.next() {
- arg
- } else {
- eprintln!("usage: {prog} <scratch directory>");
- process::exit(1);
- };
-
- // Open scratch directory
- let dir_fd = match open_scratch_directory(&arg) {
- Ok(dir_fd) => dir_fd,
- Err(err) => {
- eprintln!("{err}");
- process::exit(1)
- }
- };
-
- // Run the tests.
- unsafe { test_fd_advise(dir_fd) }
-}
diff --git a/crates/test-programs/src/bin/p1_fd_filestat_set.rs b/crates/test-programs/src/bin/p1_fd_filestat_set.rs
deleted file mode 100644
index d6ce6fa532..0000000000
--- a/crates/test-programs/src/bin/p1_fd_filestat_set.rs
+++ /dev/null
@@ -1,179 +0,0 @@
-#![expect(unsafe_op_in_unsafe_fn, reason = "old code, not worth updating yet")]
-
-use std::{env, process, time::Duration};
-use test_programs::preview1::{
- TestConfig, assert_errno, assert_fs_time_eq, open_scratch_directory,
-};
-
-unsafe fn test_fd_filestat_set_size_rw(dir_fd: wasip1::Fd) {
- // Create a file in the scratch directory, opened read/write
- let file_fd = wasip1::path_open(
- dir_fd,
- 0,
- "file",
- wasip1::OFLAGS_CREAT,
- wasip1::RIGHTS_FD_READ | wasip1::RIGHTS_FD_WRITE,
- 0,
- 0,
- )
- .expect("failed to create file");
- assert!(
- file_fd > libc::STDERR_FILENO as wasip1::Fd,
- "file descriptor range check",
- );
-
- // Check file size
- let stat = wasip1::fd_filestat_get(file_fd).expect("failed filestat");
- assert_eq!(stat.size, 0, "file size should be 0");
-
- // Check fd_filestat_set_size
- wasip1::fd_filestat_set_size(file_fd, 100).expect("fd_filestat_set_size");
-
- let stat = wasip1::fd_filestat_get(file_fd).expect("failed filestat 2");
- assert_eq!(stat.size, 100, "file size should be 100");
-
- wasip1::fd_close(file_fd).expect("failed to close fd");
- wasip1::path_unlink_file(dir_fd, "file").expect("failed to remove file");
-}
-
-unsafe fn test_fd_filestat_set_size_ro(dir_fd: wasip1::Fd) {
- // Create a file in the scratch directory. Creating a file implies opening it for writing, so
- // we have to close and re-open read-only to observe read-only behavior.
- let file_fd = wasip1::path_open(dir_fd, 0, "file", wasip1::OFLAGS_CREAT, 0, 0, 0)
- .expect("failed to create file");
- wasip1::fd_close(file_fd).expect("failed to close fd");
-
- // Open the created file read-only
- let file_fd = wasip1::path_open(dir_fd, 0, "file", 0, wasip1::RIGHTS_FD_READ, 0, 0)
- .expect("failed to create file");
-
- // Check file size
- let stat = wasip1::fd_filestat_get(file_fd).expect("failed filestat");
- assert_eq!(stat.size, 0, "file size should be 0");
-
- // Check fd_filestat_set_size on a file opened read-only fails with EINVAL, like ftruncate is defined to do on posix
- assert_errno!(
- wasip1::fd_filestat_set_size(file_fd, 100)
- .expect_err("fd_filestat_set_size should error when file is opened read-only"),
- windows => wasip1::ERRNO_ACCES,
- wasip1::ERRNO_INVAL
- );
-
- let stat = wasip1::fd_filestat_get(file_fd).expect("failed filestat 2");
- assert_eq!(stat.size, 0, "file size should remain 0");
-
- wasip1::fd_close(file_fd).expect("failed to close fd");
- wasip1::path_unlink_file(dir_fd, "file").expect("failed to remove file");
-}
-
-unsafe fn test_fd_filestat_set_times(dir_fd: wasip1::Fd, rights: wasip1::Rights) {
- let cfg = TestConfig::from_env();
-
- // Create a file in the scratch directory. OFLAGS_CREAT implies opening for writing, so we will
- // close it and re-open with the desired rights (FD_READ for read only, FD_READ | FD_WRITE for
- // readwrite)
- let file_fd = wasip1::path_open(dir_fd, 0, "file", wasip1::OFLAGS_CREAT, 0, 0, 0)
- .expect("failed to create file");
- wasip1::fd_close(file_fd).expect("failed to close fd");
-
- // Open the file with the rights given.
- let file_fd =
- wasip1::path_open(dir_fd, 0, "file", 0, rights, 0, 0).expect("failed to create file");
-
- let stat = wasip1::fd_filestat_get(file_fd).expect("failed filestat 2");
-
- // Check fd_filestat_set_times
- let old_atim = Duration::from_nanos(stat.atim);
- let new_mtim = Duration::from_nanos(stat.mtim) - cfg.fs_time_precision() * 2;
- wasip1::fd_filestat_set_times(
- file_fd,
- new_mtim.as_nanos() as u64,
- new_mtim.as_nanos() as u64,
- wasip1::FSTFLAGS_MTIM,
- )
- .expect("fd_filestat_set_times");
-
- let stat = wasip1::fd_filestat_get(file_fd).expect("failed filestat 3");
- assert_eq!(stat.size, 0, "file size should remain unchanged at 0");
-
- // Support accuracy up to at least 1ms
- assert_fs_time_eq!(
- Duration::from_nanos(stat.mtim),
- new_mtim,
- "mtim should change"
- );
- assert_fs_time_eq!(
- Duration::from_nanos(stat.atim),
- old_atim,
- "atim should not change"
- );
-
- // let status = wasi_fd_filestat_set_times(file_fd, new_mtim, new_mtim, wasip1::FILESTAT_SET_MTIM | wasip1::FILESTAT_SET_MTIM_NOW);
- // assert_eq!(status, wasip1::EINVAL, "ATIM & ATIM_NOW can't both be set");
-
- wasip1::fd_close(file_fd).expect("failed to close fd");
- wasip1::path_unlink_file(dir_fd, "file").expect("failed to remove file");
-}
-unsafe fn test_fd_filestat_set_times_on_directory(dir_fd: wasip1::Fd) {
- // Setting times on the directory descriptor itself has to work too,
- // because preopened directories are held open with `O_PATH`.
- let stat = wasip1::fd_filestat_get(dir_fd).expect("failed filestat on dir");
- let new_mtim = Duration::from_nanos(stat.mtim) + Duration::from_secs(100);
-
- wasip1::fd_filestat_set_times(
- dir_fd,
- new_mtim.as_nanos() as u64,
- new_mtim.as_nanos() as u64,
- wasip1::FSTFLAGS_MTIM,
- )
- .expect("fd_filestat_set_times on a directory");
-
- let stat = wasip1::fd_filestat_get(dir_fd).expect("failed filestat on dir");
- assert_fs_time_eq!(
- Duration::from_nanos(stat.mtim),
- new_mtim,
- "dir mtim should change"
- );
-}
-
-fn main() {
- let mut args = env::args();
- let prog = args.next().unwrap();
- let arg = if let Some(arg) = args.next() {
- arg
- } else {
- eprintln!("usage: {prog} <scratch directory>");
- process::exit(1);
- };
-
- // Open scratch directory
- let dir_fd = match open_scratch_directory(&arg) {
- Ok(dir_fd) => dir_fd,
- Err(err) => {
- eprintln!("{err}");
- process::exit(1)
- }
- };
-
- // Run the tests.
-
- unsafe { test_fd_filestat_set_size_rw(dir_fd) }
- unsafe { test_fd_filestat_set_size_ro(dir_fd) }
-
- // The fd_filestat_set_times function should behave the same whether the file
- // descriptor is open for read-only or read-write, because the underlying
- // permissions of the file determine whether or not the filestat can be
- // set or not, not than the open mode.
- if test_programs::preview1::config().support_dangling_filesystem() {
- // Guarding to run on non-windows filesystems. Windows rejects set-times on read-only
- // files.
- unsafe { test_fd_filestat_set_times(dir_fd, wasip1::RIGHTS_FD_READ) }
- }
- unsafe { test_fd_filestat_set_times(dir_fd, wasip1::RIGHTS_FD_READ | wasip1::RIGHTS_FD_WRITE) }
-
- // Same guard as above: Windows rejects set-times on a directory handle that
- // was opened read-only, the way it does for read-only files.
- if test_programs::preview1::config().support_dangling_filesystem() {
- unsafe { test_fd_filestat_set_times_on_directory(dir_fd) }
- }
-}
diff --git a/crates/test-programs/src/bin/p1_file_allocate.rs b/crates/test-programs/src/bin/p1_file_allocate.rs
deleted file mode 100644
index 6fe1e3ef41..0000000000
--- a/crates/test-programs/src/bin/p1_file_allocate.rs
+++ /dev/null
@@ -1,64 +0,0 @@
-#![expect(unsafe_op_in_unsafe_fn, reason = "old code, not worth updating yet")]
-
-use std::{env, process};
-use test_programs::preview1::open_scratch_directory;
-
-unsafe fn test_file_allocate(dir_fd: wasip1::Fd) {
- // Create a file in the scratch directory.
- let file_fd = wasip1::path_open(
- dir_fd,
- 0,
- "file",
- wasip1::OFLAGS_CREAT,
- wasip1::RIGHTS_FD_READ | wasip1::RIGHTS_FD_WRITE,
- 0,
- 0,
- )
- .expect("opening a file");
- assert!(
- file_fd > libc::STDERR_FILENO as wasip1::Fd,
- "file descriptor range check",
- );
-
- // Check file size
- let mut stat = wasip1::fd_filestat_get(file_fd).expect("reading file stats");
- assert_eq!(stat.size, 0, "file size should be 0");
-
- let err = wasip1::fd_allocate(file_fd, 0, 100)
- .err()
- .expect("fd_allocate must fail");
- assert_eq!(
- err,
- wasip1::ERRNO_NOTSUP,
- "fd_allocate should fail with NOTSUP"
- );
-
- stat = wasip1::fd_filestat_get(file_fd).expect("reading file stats");
- assert_eq!(stat.size, 0, "file size should still be 0");
-
- wasip1::fd_close(file_fd).expect("closing a file");
- wasip1::path_unlink_file(dir_fd, "file").expect("removing a file");
-}
-
-fn main() {
- let mut args = env::args();
- let prog = args.next().unwrap();
- let arg = if let Some(arg) = args.next() {
- arg
- } else {
- eprintln!("usage: {prog} <scratch directory>");
- process::exit(1);
- };
-
- // Open scratch directory
- let dir_fd = match open_scratch_directory(&arg) {
- Ok(dir_fd) => dir_fd,
- Err(err) => {
- eprintln!("{err}");
- process::exit(1)
- }
- };
-
- // Run the tests.
- unsafe { test_file_allocate(dir_fd) }
-}
diff --git a/crates/test-programs/src/bin/p1_interesting_paths.rs b/crates/test-programs/src/bin/p1_interesting_paths.rs
deleted file mode 100644
index e509b6b3f8..0000000000
--- a/crates/test-programs/src/bin/p1_interesting_paths.rs
+++ /dev/null
@@ -1,118 +0,0 @@
-#![expect(unsafe_op_in_unsafe_fn, reason = "old code, not worth updating yet")]
-
-use std::{env, process};
-use test_programs::preview1::{assert_errno, create_file, open_scratch_directory};
-
-unsafe fn test_interesting_paths(dir_fd: wasip1::Fd, arg: &str) {
- // Create a directory in the scratch directory.
- wasip1::path_create_directory(dir_fd, "dir").expect("creating dir");
-
- // Create a directory in the directory we just created.
- wasip1::path_create_directory(dir_fd, "dir/nested").expect("creating a nested dir");
-
- // Create a file in the nested directory.
- create_file(dir_fd, "dir/nested/file");
-
- // Now open it with an absolute path.
- assert_errno!(
- wasip1::path_open(dir_fd, 0, "/dir/nested/file", 0, 0, 0, 0)
- .expect_err("opening a file with an absolute path"),
- wasip1::ERRNO_PERM
- );
-
- // Now open it with a path containing "..".
- let mut file_fd = wasip1::path_open(
- dir_fd,
- 0,
- "dir/.//nested/../../dir/nested/../nested///./file",
- 0,
- 0,
- 0,
- 0,
- )
- .expect("opening a file with \"..\" in the path");
- assert!(
- file_fd > libc::STDERR_FILENO as wasip1::Fd,
- "file descriptor range check",
- );
- wasip1::fd_close(file_fd).expect("closing a file");
-
- // Now open it with a trailing NUL. Windows will allow this.
- assert_errno!(
- wasip1::path_open(dir_fd, 0, "dir/nested/file\0", 0, 0, 0, 0)
- .expect_err("opening a file with a trailing NUL"),
- unix => wasip1::ERRNO_INVAL,
- windows => wasip1::ERRNO_NOENT
- );
-
- // Now open it with a trailing slash.
- assert_errno!(
- wasip1::path_open(dir_fd, 0, "dir/nested/file/", 0, 0, 0, 0)
- .expect_err("opening a file with a trailing slash should fail"),
- wasip1::ERRNO_NOTDIR,
- wasip1::ERRNO_NOENT
- );
-
- // Now open it with trailing slashes.
- assert_errno!(
- wasip1::path_open(dir_fd, 0, "dir/nested/file///", 0, 0, 0, 0)
- .expect_err("opening a file with trailing slashes should fail"),
- wasip1::ERRNO_NOTDIR,
- wasip1::ERRNO_NOENT
- );
-
- // Now open the directory with a trailing slash.
- file_fd = wasip1::path_open(dir_fd, 0, "dir/nested/", 0, 0, 0, 0)
- .expect("opening a directory with a trailing slash");
- assert!(
- file_fd > libc::STDERR_FILENO as wasip1::Fd,
- "file descriptor range check",
- );
- wasip1::fd_close(file_fd).expect("closing a file");
-
- // Now open the directory with trailing slashes.
- file_fd = wasip1::path_open(dir_fd, 0, "dir/nested///", 0, 0, 0, 0)
- .expect("opening a directory with trailing slashes");
- assert!(
- file_fd > libc::STDERR_FILENO as wasip1::Fd,
- "file descriptor range check",
- );
- wasip1::fd_close(file_fd).expect("closing a file");
-
- // Now open it with a path containing too many ".."s.
- let bad_path = format!("dir/nested/../../../{arg}/dir/nested/file");
- assert_errno!(
- wasip1::path_open(dir_fd, 0, &bad_path, 0, 0, 0, 0)
- .expect_err("opening a file with too many \"..\"s in the path should fail"),
- wasip1::ERRNO_PERM
- );
- wasip1::path_unlink_file(dir_fd, "dir/nested/file")
- .expect("unlink_file on a symlink should succeed");
- wasip1::path_remove_directory(dir_fd, "dir/nested")
- .expect("remove_directory on a directory should succeed");
- wasip1::path_remove_directory(dir_fd, "dir")
- .expect("remove_directory on a directory should succeed");
-}
-
-fn main() {
- let mut args = env::args();
- let prog = args.next().unwrap();
- let arg = if let Some(arg) = args.next() {
- arg
- } else {
- eprintln!("usage: {prog} <scratch directory>");
- process::exit(1);
- };
-
- // Open scratch directory
- let dir_fd = match open_scratch_directory(&arg) {
- Ok(dir_fd) => dir_fd,
- Err(err) => {
- eprintln!("{err}");
- process::exit(1)
- }
- };
-
- // Run the tests.
- unsafe { test_interesting_paths(dir_fd, &arg) }
-}
diff --git a/crates/test-programs/src/bin/p1_nofollow_errors.rs b/crates/test-programs/src/bin/p1_nofollow_errors.rs
deleted file mode 100644
index d13bbdecaa..0000000000
--- a/crates/test-programs/src/bin/p1_nofollow_errors.rs
+++ /dev/null
@@ -1,112 +0,0 @@
-#![expect(unsafe_op_in_unsafe_fn, reason = "old code, not worth updating yet")]
-
-use std::{env, process};
-use test_programs::preview1::{assert_errno, open_scratch_directory};
-
-unsafe fn test_nofollow_errors(dir_fd: wasip1::Fd) {
- // Create a directory for the symlink to point to.
- wasip1::path_create_directory(dir_fd, "target").expect("creating a dir");
-
- // Create a symlink.
- wasip1::path_symlink("target", dir_fd, "symlink").expect("creating a symlink");
-
- // Try to open it as a directory with O_NOFOLLOW again.
- assert_errno!(
- wasip1::path_open(dir_fd, 0, "symlink", wasip1::OFLAGS_DIRECTORY, 0, 0, 0)
- .expect_err("opening a directory symlink as a directory should fail"),
- wasip1::ERRNO_LOOP,
- wasip1::ERRNO_NOTDIR
- );
-
- // Try to open it with just O_NOFOLLOW.
- assert_errno!(
- wasip1::path_open(dir_fd, 0, "symlink", 0, 0, 0, 0)
- .expect_err("opening a symlink with O_NOFOLLOW should fail"),
- wasip1::ERRNO_LOOP,
- wasip1::ERRNO_ACCES
- );
-
- // Try to open it as a directory without O_NOFOLLOW.
- let file_fd = wasip1::path_open(
- dir_fd,
- wasip1::LOOKUPFLAGS_SYMLINK_FOLLOW,
- "symlink",
- wasip1::OFLAGS_DIRECTORY,
- 0,
- 0,
- 0,
- )
- .expect("opening a symlink as a directory");
- assert!(
- file_fd > libc::STDERR_FILENO as wasip1::Fd,
- "file descriptor range check",
- );
- wasip1::fd_close(file_fd).expect("closing a file");
-
- // Replace the target directory with a file.
- wasip1::path_unlink_file(dir_fd, "symlink").expect("removing a file");
- wasip1::path_remove_directory(dir_fd, "target")
- .expect("remove_directory on a directory should succeed");
-
- let file_fd = wasip1::path_open(dir_fd, 0, "target", wasip1::OFLAGS_CREAT, 0, 0, 0)
- .expect("creating a file");
- wasip1::fd_close(file_fd).expect("closing a file");
- wasip1::path_symlink("target", dir_fd, "symlink").expect("creating a symlink");
-
- // Try to open it as a directory with O_NOFOLLOW again.
- assert_errno!(
- wasip1::path_open(dir_fd, 0, "symlink", wasip1::OFLAGS_DIRECTORY, 0, 0, 0)
- .expect_err("opening a directory symlink as a directory should fail"),
- wasip1::ERRNO_LOOP,
- wasip1::ERRNO_NOTDIR
- );
-
- // Try to open it with just O_NOFOLLOW.
- assert_errno!(
- wasip1::path_open(dir_fd, 0, "symlink", 0, 0, 0, 0)
- .expect_err("opening a symlink with NOFOLLOW should fail"),
- wasip1::ERRNO_LOOP
- );
-
- // Try to open it as a directory without O_NOFOLLOW.
- assert_errno!(
- wasip1::path_open(
- dir_fd,
- wasip1::LOOKUPFLAGS_SYMLINK_FOLLOW,
- "symlink",
- wasip1::OFLAGS_DIRECTORY,
- 0,
- 0,
- 0,
- )
- .expect_err("opening a symlink to a file as a directory"),
- wasip1::ERRNO_NOTDIR
- );
-
- // Clean up.
- wasip1::path_unlink_file(dir_fd, "target").expect("removing a file");
- wasip1::path_unlink_file(dir_fd, "symlink").expect("removing a file");
-}
-
-fn main() {
- let mut args = env::args();
- let prog = args.next().unwrap();
- let arg = if let Some(arg) = args.next() {
- arg
- } else {
- eprintln!("usage: {prog} <scratch directory>");
- process::exit(1);
- };
-
- // Open scratch directory
- let dir_fd = match open_scratch_directory(&arg) {
- Ok(dir_fd) => dir_fd,
- Err(err) => {
- eprintln!("{err}");
- process::exit(1)
- }
- };
-
- // Run the tests.
- unsafe { test_nofollow_errors(dir_fd) }
-}
diff --git a/crates/test-programs/src/bin/p1_overwrite_preopen.rs b/crates/test-programs/src/bin/p1_overwrite_preopen.rs
deleted file mode 100644
index ec10c34a9b..0000000000
--- a/crates/test-programs/src/bin/p1_overwrite_preopen.rs
+++ /dev/null
@@ -1,51 +0,0 @@
-#![expect(unsafe_op_in_unsafe_fn, reason = "old code, not worth updating yet")]
-
-use std::{env, process};
-use test_programs::preview1::{assert_errno, open_scratch_directory};
-
-unsafe fn test_overwrite_preopen(dir_fd: wasip1::Fd) {
- let pre_fd: wasip1::Fd = (libc::STDERR_FILENO + 1) as wasip1::Fd;
-
- assert!(dir_fd > pre_fd, "dir_fd number");
-
- let old_dir_filestat = wasip1::fd_filestat_get(dir_fd).expect("failed fd_filestat_get");
-
- // Try to renumber over a preopened directory handle.
- wasip1::fd_renumber(dir_fd, pre_fd).expect("renumbering over a preopened file descriptor");
-
- // Ensure that pre_fd is still open.
- let new_dir_filestat = wasip1::fd_filestat_get(pre_fd).expect("failed fd_filestat_get");
-
- // Ensure that we renumbered.
- assert_eq!(old_dir_filestat.dev, new_dir_filestat.dev);
- assert_eq!(old_dir_filestat.ino, new_dir_filestat.ino);
-
- // Ensure that dir_fd is closed.
- assert_errno!(
- wasip1::fd_fdstat_get(dir_fd).expect_err("failed fd_fdstat_get"),
- wasip1::ERRNO_BADF
- );
-}
-
-fn main() {
- let mut args = env::args();
- let prog = args.next().unwrap();
- let arg = if let Some(arg) = args.next() {
- arg
- } else {
- eprintln!("usage: {prog} <scratch directory>");
- process::exit(1);
- };
-
- // Open scratch directory
- let dir_fd = match open_scratch_directory(&arg) {
- Ok(dir_fd) => dir_fd,
- Err(err) => {
- eprintln!("{err}");
- process::exit(1)
- }
- };
-
- // Run the tests.
- unsafe { test_overwrite_preopen(dir_fd) }
-}
diff --git a/crates/test-programs/src/bin/p1_path_exists.rs b/crates/test-programs/src/bin/p1_path_exists.rs
deleted file mode 100644
index e0fee391c5..0000000000
--- a/crates/test-programs/src/bin/p1_path_exists.rs
+++ /dev/null
@@ -1,80 +0,0 @@
-#![expect(unsafe_op_in_unsafe_fn, reason = "old code, not worth updating yet")]
-
-use std::{env, process};
-use test_programs::preview1::{create_file, open_scratch_directory};
-
-unsafe fn test_path_exists(dir_fd: wasip1::Fd) {
- // Create a temporary directory
- wasip1::path_create_directory(dir_fd, "subdir").expect("create directory");
-
- // Check directory exists:
- let file_stat = wasip1::path_filestat_get(dir_fd, 0, "subdir").expect("reading file stats");
- assert_eq!(file_stat.filetype, wasip1::FILETYPE_DIRECTORY);
-
- // Should still exist with symlink follow flag:
- let file_stat = wasip1::path_filestat_get(dir_fd, wasip1::LOOKUPFLAGS_SYMLINK_FOLLOW, "subdir")
- .expect("reading file stats");
- assert_eq!(file_stat.filetype, wasip1::FILETYPE_DIRECTORY);
-
- // Create a file:
- create_file(dir_fd, "subdir/file");
- // Check directory exists:
- let file_stat =
- wasip1::path_filestat_get(dir_fd, 0, "subdir/file").expect("reading file stats");
- assert_eq!(file_stat.filetype, wasip1::FILETYPE_REGULAR_FILE);
-
- // Should still exist with symlink follow flag:
- let file_stat =
- wasip1::path_filestat_get(dir_fd, wasip1::LOOKUPFLAGS_SYMLINK_FOLLOW, "subdir/file")
- .expect("reading file stats");
- assert_eq!(file_stat.filetype, wasip1::FILETYPE_REGULAR_FILE);
-
- // Create a symlink to a file:
- wasip1::path_symlink("subdir/file", dir_fd, "link1").expect("create symlink");
- // Check symlink exists:
- let file_stat = wasip1::path_filestat_get(dir_fd, 0, "link1").expect("reading file stats");
- assert_eq!(file_stat.filetype, wasip1::FILETYPE_SYMBOLIC_LINK);
-
- // Should still exist with symlink follow flag, pointing to regular file
- let file_stat = wasip1::path_filestat_get(dir_fd, wasip1::LOOKUPFLAGS_SYMLINK_FOLLOW, "link1")
- .expect("reading file stats");
- assert_eq!(file_stat.filetype, wasip1::FILETYPE_REGULAR_FILE);
-
- // Create a symlink to a dir:
- wasip1::path_symlink("subdir", dir_fd, "link2").expect("create symlink");
- // Check symlink exists:
- let file_stat = wasip1::path_filestat_get(dir_fd, 0, "link2").expect("reading file stats");
- assert_eq!(file_stat.filetype, wasip1::FILETYPE_SYMBOLIC_LINK);
-
- // Should still exist with symlink follow flag, pointing to directory
- let file_stat = wasip1::path_filestat_get(dir_fd, wasip1::LOOKUPFLAGS_SYMLINK_FOLLOW, "link2")
- .expect("reading file stats");
- assert_eq!(file_stat.filetype, wasip1::FILETYPE_DIRECTORY);
-
- wasip1::path_unlink_file(dir_fd, "link1").expect("clean up");
- wasip1::path_unlink_file(dir_fd, "link2").expect("clean up");
- wasip1::path_unlink_file(dir_fd, "subdir/file").expect("clean up");
- wasip1::path_remove_directory(dir_fd, "subdir").expect("clean up");
-}
-
-fn main() {
- let mut args = env::args();
- let prog = args.next().unwrap();
- let arg = if let Some(arg) = args.next() {
- arg
- } else {
- eprintln!("usage: {prog} <scratch directory>");
- process::exit(1);
- };
-
- // Open scratch directory
- let dir_fd = match open_scratch_directory(&arg) {
- Ok(dir_fd) => dir_fd,
- Err(err) => {
- eprintln!("{err}");
- process::exit(1)
- }
- };
- // Run the tests.
- unsafe { test_path_exists(dir_fd) }
-}
diff --git a/crates/test-programs/src/bin/p1_path_filestat.rs b/crates/test-programs/src/bin/p1_path_filestat.rs
deleted file mode 100644
index 4e4a86bf61..0000000000
--- a/crates/test-programs/src/bin/p1_path_filestat.rs
+++ /dev/null
@@ -1,135 +0,0 @@
-#![expect(unsafe_op_in_unsafe_fn, reason = "old code, not worth updating yet")]
-
-use std::{env, process, time::Duration};
-use test_programs::preview1::{
- TestConfig, assert_errno, assert_fs_time_eq, open_scratch_directory,
-};
-
-unsafe fn test_path_filestat(dir_fd: wasip1::Fd) {
- let cfg = TestConfig::from_env();
- let fdflags = wasip1::FDFLAGS_APPEND;
-
- // Create a file in the scratch directory.
- let file_fd = wasip1::path_open(
- dir_fd,
- 0,
- "file",
- wasip1::OFLAGS_CREAT,
- wasip1::RIGHTS_FD_READ | wasip1::RIGHTS_FD_WRITE,
- 0,
- // Pass some flags for later retrieval
- fdflags,
- )
- .expect("opening a file");
- assert!(
- file_fd > libc::STDERR_FILENO as wasip1::Fd,
- "file descriptor range check",
- );
-
- let fdstat = wasip1::fd_fdstat_get(file_fd).expect("fd_fdstat_get");
- assert_eq!(
- fdstat.fs_flags & wasip1::FDFLAGS_APPEND,
- wasip1::FDFLAGS_APPEND,
- "file should have the APPEND fdflag used to create the file"
- );
- assert_errno!(
- wasip1::path_open(
- dir_fd,
- 0,
- "file",
- 0,
- wasip1::RIGHTS_FD_READ | wasip1::RIGHTS_FD_WRITE,
- 0,
- wasip1::FDFLAGS_SYNC,
- )
- .expect_err("FDFLAGS_SYNC not supported by platform"),
- wasip1::ERRNO_NOTSUP
- );
-
- // Check file size
- let file_stat = wasip1::path_filestat_get(dir_fd, 0, "file").expect("reading file stats");
- assert_eq!(file_stat.size, 0, "file size should be 0");
-
- // Check path_filestat_set_times
- let new_mtim = Duration::from_nanos(file_stat.mtim) - 2 * cfg.fs_time_precision();
- wasip1::path_filestat_set_times(
- dir_fd,
- 0,
- "file",
- 0,
- new_mtim.as_nanos() as u64,
- wasip1::FSTFLAGS_MTIM,
- )
- .expect("path_filestat_set_times should succeed");
-
- let modified_file_stat = wasip1::path_filestat_get(dir_fd, 0, "file")
- .expect("reading file stats after path_filestat_set_times");
-
- assert_fs_time_eq!(
- Duration::from_nanos(modified_file_stat.mtim),
- new_mtim,
- "mtim should change"
- );
-
- assert_errno!(
- wasip1::path_filestat_set_times(
- dir_fd,
- 0,
- "file",
- 0,
- new_mtim.as_nanos() as u64,
- wasip1::FSTFLAGS_MTIM | wasip1::FSTFLAGS_MTIM_NOW,
- )
- .expect_err("MTIM and MTIM_NOW can't both be set"),
- wasip1::ERRNO_INVAL
- );
-
- // check if the times were untouched
- let unmodified_file_stat = wasip1::path_filestat_get(dir_fd, 0, "file")
- .expect("reading file stats after ERRNO_INVAL fd_filestat_set_times");
-
- assert_fs_time_eq!(
- Duration::from_nanos(unmodified_file_stat.mtim),
- new_mtim,
- "mtim should not change"
- );
-
- // Invalid arguments to set_times:
- assert_errno!(
- wasip1::path_filestat_set_times(
- dir_fd,
- 0,
- "file",
- 0,
- 0,
- wasip1::FSTFLAGS_ATIM | wasip1::FSTFLAGS_ATIM_NOW,
- )
- .expect_err("ATIM & ATIM_NOW can't both be set"),
- wasip1::ERRNO_INVAL
- );
-
- wasip1::fd_close(file_fd).expect("closing a file");
- wasip1::path_unlink_file(dir_fd, "file").expect("removing a file");
-}
-fn main() {
- let mut args = env::args();
- let prog = args.next().unwrap();
- let arg = if let Some(arg) = args.next() {
- arg
- } else {
- eprintln!("usage: {prog} <scratch directory>");
- process::exit(1);
- };
-
- // Open scratch directory
- let dir_fd = match open_scratch_directory(&arg) {
- Ok(dir_fd) => dir_fd,
- Err(err) => {
- eprintln!("{err}");
- process::exit(1)
- }
- };
-
- // Run the tests.
- unsafe { test_path_filestat(dir_fd) }
-}
diff --git a/crates/test-programs/src/bin/p1_path_link.rs b/crates/test-programs/src/bin/p1_path_link.rs
deleted file mode 100644
index 1f725dc79f..0000000000
--- a/crates/test-programs/src/bin/p1_path_link.rs
+++ /dev/null
@@ -1,207 +0,0 @@
-#![expect(unsafe_op_in_unsafe_fn, reason = "old code, not worth updating yet")]
-
-use std::{env, process};
-use test_programs::preview1::{assert_errno, config, create_file, open_scratch_directory};
-
-// These are all macro-rules so the panic line number shows us where
-// things went wrong.
-
-macro_rules! filestats_assert_eq {
- ($left:ident, $right:ident) => {
- 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");
- };
-}
-
-macro_rules! fdstats_assert_eq {
- ($left:ident, $right:ident) => {
- assert_eq!($left.fs_flags, $right.fs_flags, "fs_flags should be equal");
- assert_eq!(
- $left.fs_filetype, $right.fs_filetype,
- "fs_filetype should be equal"
- );
- assert_eq!(
- $left.fs_rights_base, $right.fs_rights_base,
- "fs_rights_base should be equal"
- );
- assert_eq!(
- $left.fs_rights_inheriting, $right.fs_rights_inheriting,
- "fs_rights_inheriting should be equal"
- );
- };
-}
-
-macro_rules! check_rights {
- ($orig_fd:ident, $link_fd:ident) => {
- let orig_filestat =
- wasip1::fd_filestat_get($orig_fd).expect("reading filestat of the source");
- let link_filestat =
- wasip1::fd_filestat_get($link_fd).expect("reading filestat of the link");
- filestats_assert_eq!(orig_filestat, link_filestat);
-
- // Compare Fdstats
- let orig_fdstat = wasip1::fd_fdstat_get($orig_fd).expect("reading fdstat of the source");
- let link_fdstat = wasip1::fd_fdstat_get($link_fd).expect("reading fdstat of the link");
- fdstats_assert_eq!(orig_fdstat, link_fdstat);
- };
-}
-// Extra calls of fd_close are needed for Windows, which will not remove
-// the directory until all handles are closed.
-unsafe fn test_path_link(dir_fd: wasip1::Fd) {
- // Create a file
- let create_fd =
- wasip1::path_open(dir_fd, 0, "file", wasip1::OFLAGS_CREAT, 0, 0, 0).expect("create file");
- wasip1::fd_close(create_fd).unwrap();
-
- // Open a fresh descriptor to the file. We won't have a write right that was implied by OFLAGS_CREAT
- // above.
- let file_fd = wasip1::path_open(dir_fd, 0, "file", 0, 0, 0, 0).expect("open file");
-
- // Create a link in the same directory and compare rights
- wasip1::path_link(dir_fd, 0, "file", dir_fd, "link")
- .expect("creating a link in the same directory");
-
- let link_fd = wasip1::path_open(dir_fd, 0, "link", 0, 0, 0, 0).expect("open link");
-
- check_rights!(file_fd, link_fd);
- wasip1::fd_close(link_fd).expect("Closing link_fd"); // needed for Windows
- wasip1::path_unlink_file(dir_fd, "link").expect("removing a link");
-
- // Create a link in a different directory and compare rights
- wasip1::path_create_directory(dir_fd, "subdir").expect("creating a subdirectory");
- let subdir_fd = wasip1::path_open(dir_fd, 0, "subdir", wasip1::OFLAGS_DIRECTORY, 0, 0, 0)
- .expect("open subdir directory");
- wasip1::path_link(dir_fd, 0, "file", subdir_fd, "link")
- .expect("creating a link in subdirectory");
- let link_fd = wasip1::path_open(subdir_fd, 0, "link", 0, 0, 0, 0).expect("open link in subdir");
- check_rights!(file_fd, link_fd);
- wasip1::fd_close(link_fd).expect("Closing link_fd"); // needed for Windows
- wasip1::path_unlink_file(subdir_fd, "link").expect("removing a link");
- wasip1::fd_close(subdir_fd).expect("Closing subdir_fd"); // needed for Windows
- wasip1::path_remove_directory(dir_fd, "subdir").expect("removing a subdirectory");
-
- // Create a link to a path that already exists
- create_file(dir_fd, "link");
-
- assert_errno!(
- wasip1::path_link(dir_fd, 0, "file", dir_fd, "link")
- .expect_err("creating a link to existing path should fail"),
- wasip1::ERRNO_EXIST
- );
- wasip1::path_unlink_file(dir_fd, "link").expect("removing a file");
-
- // Create a link to itself
- assert_errno!(
- wasip1::path_link(dir_fd, 0, "file", dir_fd, "file")
- .expect_err("creating a link to itself should fail"),
- wasip1::ERRNO_EXIST
- );
-
- // Create a link where target is a directory
- wasip1::path_create_directory(dir_fd, "link").expect("creating a dir");
-
- assert_errno!(
- wasip1::path_link(dir_fd, 0, "file", dir_fd, "link")
- .expect_err("creating a link where target is a directory should fail"),
- wasip1::ERRNO_EXIST
- );
- wasip1::path_remove_directory(dir_fd, "link").expect("removing a dir");
-
- // Create a link to a directory
- wasip1::path_create_directory(dir_fd, "subdir").expect("creating a subdirectory");
- let subdir_fd = wasip1::path_open(dir_fd, 0, "subdir", wasip1::OFLAGS_DIRECTORY, 0, 0, 0)
- .expect("open new descriptor to subdir");
-
- assert_errno!(
- wasip1::path_link(dir_fd, 0, "subdir", dir_fd, "link")
- .expect_err("creating a link to a directory should fail"),
- wasip1::ERRNO_PERM,
- wasip1::ERRNO_ACCES
- );
- wasip1::fd_close(subdir_fd).expect("close subdir before deleting it");
- wasip1::path_remove_directory(dir_fd, "subdir").expect("removing a subdirectory");
-
- // Create a link to a file with trailing slash
- assert_errno!(
- wasip1::path_link(dir_fd, 0, "file", dir_fd, "link/")
- .expect_err("creating a link to a file with trailing slash should fail"),
- wasip1::ERRNO_NOENT
- );
-
- if config().support_dangling_filesystem() {
- // Create a link to a dangling symlink
- wasip1::path_symlink("target", dir_fd, "symlink").expect("creating a dangling symlink");
-
- // This should succeed, because we're not following symlinks
- wasip1::path_link(dir_fd, 0, "symlink", dir_fd, "link")
- .expect("creating a link to a dangling symlink should succeed");
- wasip1::path_unlink_file(dir_fd, "symlink").expect("removing a symlink");
- wasip1::path_unlink_file(dir_fd, "link").expect("removing a hardlink");
-
- // Create a link to a symlink loop
- wasip1::path_symlink("symlink", dir_fd, "symlink").expect("creating a symlink loop");
-
- wasip1::path_link(dir_fd, 0, "symlink", dir_fd, "link")
- .expect("creating a link to a symlink loop should succeed");
- wasip1::path_unlink_file(dir_fd, "symlink").expect("removing a symlink");
- wasip1::path_unlink_file(dir_fd, "link").expect("removing a hardlink");
-
- // Create a link where target is a dangling symlink
- wasip1::path_symlink("target", dir_fd, "symlink").expect("creating a dangling symlink");
-
- assert_errno!(
- wasip1::path_link(dir_fd, 0, "file", dir_fd, "symlink")
- .expect_err("creating a link where target is a dangling symlink"),
- wasip1::ERRNO_EXIST
- );
- wasip1::path_unlink_file(dir_fd, "symlink").expect("removing a symlink");
-
- // Create a link where target is a dangling symlink following symlinks
- wasip1::path_symlink("target", dir_fd, "symlink").expect("creating a dangling symlink");
-
- // Symlink following with path_link is rejected
- assert_errno!(
- wasip1::path_link(
- dir_fd,
- wasip1::LOOKUPFLAGS_SYMLINK_FOLLOW,
- "symlink",
- dir_fd,
- "link",
- )
- .expect_err("calling path_link with LOOKUPFLAGS_SYMLINK_FOLLOW should fail"),
- wasip1::ERRNO_INVAL
- );
-
- // Clean up.
- wasip1::path_unlink_file(dir_fd, "file").expect("removing a file");
- }
-}
-
-fn main() {
- let mut args = env::args();
- let prog = args.next().unwrap();
- let arg = if let Some(arg) = args.next() {
- arg
- } else {
- eprintln!("usage: {prog} <scratch directory>");
- process::exit(1);
- };
-
- // Open scratch directory
- let dir_fd = match open_scratch_directory(&arg) {
- Ok(dir_fd) => dir_fd,
- Err(err) => {
- eprintln!("{err}");
- process::exit(1)
- }
- };
-
- // Run the tests.
- unsafe { test_path_link(dir_fd) }
-}
diff --git a/crates/test-programs/src/bin/p1_path_open_create_existing.rs b/crates/test-programs/src/bin/p1_path_open_create_existing.rs
deleted file mode 100644
index 607cd426b3..0000000000
--- a/crates/test-programs/src/bin/p1_path_open_create_existing.rs
+++ /dev/null
@@ -1,45 +0,0 @@
-#![expect(unsafe_op_in_unsafe_fn, reason = "old code, not worth updating yet")]
-
-use std::{env, process};
-use test_programs::preview1::{assert_errno, create_file, open_scratch_directory};
-
-unsafe fn test_path_open_create_existing(dir_fd: wasip1::Fd) {
- create_file(dir_fd, "file");
- assert_errno!(
- wasip1::path_open(
- dir_fd,
- 0,
- "file",
- wasip1::OFLAGS_CREAT | wasip1::OFLAGS_EXCL,
- 0,
- 0,
- 0,
- )
- .expect_err("trying to create a file that already exists"),
- wasip1::ERRNO_EXIST
- );
- wasip1::path_unlink_file(dir_fd, "file").expect("removing a file");
-}
-
-fn main() {
- let mut args = env::args();
- let prog = args.next().unwrap();
- let arg = if let Some(arg) = args.next() {
- arg
- } else {
- eprintln!("usage: {prog} <scratch directory>");
- process::exit(1);
- };
-
- // Open scratch directory
- let dir_fd = match open_scratch_directory(&arg) {
- Ok(dir_fd) => dir_fd,
- Err(err) => {
- eprintln!("{err}");
- process::exit(1)
- }
- };
-
- // Run the tests.
- unsafe { test_path_open_create_existing(dir_fd) }
-}
diff --git a/crates/test-programs/src/bin/p1_path_open_dirfd_not_dir.rs b/crates/test-programs/src/bin/p1_path_open_dirfd_not_dir.rs
deleted file mode 100644
index c40bedd097..0000000000
--- a/crates/test-programs/src/bin/p1_path_open_dirfd_not_dir.rs
+++ /dev/null
@@ -1,40 +0,0 @@
-#![expect(unsafe_op_in_unsafe_fn, reason = "old code, not worth updating yet")]
-
-use std::{env, process};
-use test_programs::preview1::{assert_errno, open_scratch_directory};
-
-unsafe fn test_dirfd_not_dir(dir_fd: wasip1::Fd) {
- // Open a file.
- let file_fd = wasip1::path_open(dir_fd, 0, "file", wasip1::OFLAGS_CREAT, 0, 0, 0)
- .expect("opening a file");
- // Now try to open a file underneath it as if it were a directory.
- assert_errno!(
- wasip1::path_open(file_fd, 0, "foo", wasip1::OFLAGS_CREAT, 0, 0, 0)
- .expect_err("non-directory base fd should get ERRNO_NOTDIR"),
- wasip1::ERRNO_NOTDIR
- );
- wasip1::fd_close(file_fd).expect("closing a file");
-}
-
-fn main() {
- let mut args = env::args();
- let prog = args.next().unwrap();
- let arg = if let Some(arg) = args.next() {
- arg
- } else {
- eprintln!("usage: {prog} <scratch directory>");
- process::exit(1);
- };
-
- // Open scratch directory
- let dir_fd = match open_scratch_directory(&arg) {
- Ok(dir_fd) => dir_fd,
- Err(err) => {
- eprintln!("{err}");
- process::exit(1)
- }
- };
-
- // Run the tests.
- unsafe { test_dirfd_not_dir(dir_fd) }
-}
diff --git a/crates/test-programs/src/bin/p1_path_open_missing.rs b/crates/test-programs/src/bin/p1_path_open_missing.rs
deleted file mode 100644
index 64052f90f2..0000000000
--- a/crates/test-programs/src/bin/p1_path_open_missing.rs
+++ /dev/null
@@ -1,38 +0,0 @@
-#![expect(unsafe_op_in_unsafe_fn, reason = "old code, not worth updating yet")]
-
-use std::{env, process};
-use test_programs::preview1::{assert_errno, open_scratch_directory};
-
-unsafe fn test_path_open_missing(dir_fd: wasip1::Fd) {
- assert_errno!(
- wasip1::path_open(
- dir_fd, 0, "file", 0, // not passing O_CREAT here
- 0, 0, 0,
- )
- .expect_err("trying to open a file that doesn't exist"),
- wasip1::ERRNO_NOENT
- );
-}
-
-fn main() {
- let mut args = env::args();
- let prog = args.next().unwrap();
- let arg = if let Some(arg) = args.next() {
- arg
- } else {
- eprintln!("usage: {prog} <scratch directory>");
- process::exit(1);
- };
-
- // Open scratch directory
- let dir_fd = match open_scratch_directory(&arg) {
- Ok(dir_fd) => dir_fd,
- Err(err) => {
- eprintln!("{err}");
- process::exit(1)
- }
- };
-
- // Run the tests.
- unsafe { test_path_open_missing(dir_fd) }
-}
diff --git a/crates/test-programs/src/bin/p1_path_open_nonblock.rs b/crates/test-programs/src/bin/p1_path_open_nonblock.rs
deleted file mode 100644
index e473c3543d..0000000000
--- a/crates/test-programs/src/bin/p1_path_open_nonblock.rs
+++ /dev/null
@@ -1,32 +0,0 @@
-#![expect(unsafe_op_in_unsafe_fn, reason = "old code, not worth updating yet")]
-
-use std::{env, process};
-use test_programs::preview1::open_scratch_directory;
-
-unsafe fn try_path_open(dir_fd: wasip1::Fd) {
- let _fd = wasip1::path_open(dir_fd, 0, ".", 0, 0, 0, wasip1::FDFLAGS_NONBLOCK)
- .expect("opening the dir");
-}
-
-fn main() {
- let mut args = env::args();
- let prog = args.next().unwrap();
- let arg = if let Some(arg) = args.next() {
- arg
- } else {
- eprintln!("usage: {prog} <scratch directory>");
- process::exit(1);
- };
-
- // Open scratch directory
- let dir_fd = match open_scratch_directory(&arg) {
- Ok(dir_fd) => dir_fd,
- Err(err) => {
- eprintln!("{err}");
- process::exit(1)
- }
- };
-
- // Run the tests.
- unsafe { try_path_open(dir_fd) }
-}
diff --git a/crates/test-programs/src/bin/p1_path_open_preopen.rs b/crates/test-programs/src/bin/p1_path_open_preopen.rs
deleted file mode 100644
index 91d981855e..0000000000
--- a/crates/test-programs/src/bin/p1_path_open_preopen.rs
+++ /dev/null
@@ -1,153 +0,0 @@
-#![expect(unsafe_op_in_unsafe_fn, reason = "old code, not worth updating yet")]
-
-const FIRST_PREOPEN: u32 = 3;
-
-unsafe fn path_open_preopen() {
- let prestat = wasip1::fd_prestat_get(FIRST_PREOPEN).expect("fd 3 is a preopen");
- assert_eq!(
- prestat.tag,
- wasip1::PREOPENTYPE_DIR.raw(),
- "prestat is a directory"
- );
- let mut dst = Vec::with_capacity(prestat.u.dir.pr_name_len);
- wasip1::fd_prestat_dir_name(FIRST_PREOPEN, dst.as_mut_ptr(), dst.capacity())
- .expect("get preopen dir name");
- dst.set_len(prestat.u.dir.pr_name_len);
-
- let fdstat = wasip1::fd_fdstat_get(FIRST_PREOPEN).expect("get fdstat");
-
- println!(
- "preopen dir: {:?} base {:?} inheriting {:?}",
- String::from_utf8_lossy(&dst),
- fdstat.fs_rights_base,
- fdstat.fs_rights_inheriting
- );
- for (right, name) in directory_base_rights() {
- assert!(
- (fdstat.fs_rights_base & right) == right,
- "fs_rights_base does not have required right `{name}`"
- );
- }
- for (right, name) in directory_inheriting_rights() {
- assert!(
- (fdstat.fs_rights_inheriting & right) == right,
- "fs_rights_inheriting does not have required right `{name}`"
- );
- }
-
- // Open with same rights it has now:
- let _ = wasip1::path_open(
- FIRST_PREOPEN,
- 0,
- ".",
- 0,
- fdstat.fs_rights_base,
- fdstat.fs_rights_inheriting,
- 0,
- )
- .expect("open with same rights");
-
- // Open with an empty set of rights:
- let _ = wasip1::path_open(FIRST_PREOPEN, 0, ".", 0, 0, 0, 0).expect("open with empty rights");
-
- // Open OFLAGS_DIRECTORY with an empty set of rights:
- let _ = wasip1::path_open(FIRST_PREOPEN, 0, ".", wasip1::OFLAGS_DIRECTORY, 0, 0, 0)
- .expect("open with O_DIRECTORY empty rights");
-
- // Open OFLAGS_DIRECTORY with just the read right:
- let _ = wasip1::path_open(
- FIRST_PREOPEN,
- 0,
- ".",
- wasip1::OFLAGS_DIRECTORY,
- wasip1::RIGHTS_FD_READ,
- 0,
- 0,
- )
- .expect("open with O_DIRECTORY and read right");
-
- // Open OFLAGS_DIRECTORY and read/write rights should fail with isdir:
- let err = wasip1::path_open(
- FIRST_PREOPEN,
- 0,
- ".",
- wasip1::OFLAGS_DIRECTORY,
- wasip1::RIGHTS_FD_READ | wasip1::RIGHTS_FD_WRITE,
- 0,
- 0,
- )
- .err()
- .expect("open with O_DIRECTORY and read/write should fail");
- assert_eq!(
- err,
- wasip1::ERRNO_ISDIR,
- "opening directory read/write should fail with ISDIR"
- );
-}
-
-fn main() {
- unsafe {
- path_open_preopen();
- }
-}
-
-// Hard-code the set of rights expected for a preopened directory. This is
-// more brittle than we wanted to test for, but various userland
-// implementations expect (at least) this set of rights to be present on all
-// directories:
-
-fn directory_base_rights() -> Vec<(wasip1::Rights, &'static str)> {
- vec![
- (
- wasip1::RIGHTS_PATH_CREATE_DIRECTORY,
- "PATH_CREATE_DIRECTORY",
- ),
- (wasip1::RIGHTS_PATH_CREATE_FILE, "PATH_CREATE_FILE"),
- (wasip1::RIGHTS_PATH_LINK_SOURCE, "PATH_LINK_SOURCE"),
- (wasip1::RIGHTS_PATH_LINK_TARGET, "PATH_LINK_TARGET"),
- (wasip1::RIGHTS_PATH_OPEN, "PATH_OPEN"),
- (wasip1::RIGHTS_FD_READDIR, "FD_READDIR"),
- (wasip1::RIGHTS_PATH_READLINK, "PATH_READLINK"),
- (wasip1::RIGHTS_PATH_RENAME_SOURCE, "PATH_RENAME_SOURCE"),
- (wasip1::RIGHTS_PATH_RENAME_TARGET, "PATH_RENAME_TARGET"),
- (wasip1::RIGHTS_PATH_SYMLINK, "PATH_SYMLINK"),
- (
- wasip1::RIGHTS_PATH_REMOVE_DIRECTORY,
- "PATH_REMOVE_DIRECTORY",
- ),
- (wasip1::RIGHTS_PATH_UNLINK_FILE, "PATH_UNLINK_FILE"),
- (wasip1::RIGHTS_PATH_FILESTAT_GET, "PATH_FILESTAT_GET"),
- (
- wasip1::RIGHTS_PATH_FILESTAT_SET_TIMES,
- "PATH_FILESTAT_SET_TIMES",
- ),
- (wasip1::RIGHTS_FD_FILESTAT_GET, "FD_FILESTAT_GET"),
- (
- wasip1::RIGHTS_FD_FILESTAT_SET_TIMES,
- "FD_FILESTAT_SET_TIMES",
- ),
- ]
-}
-
-pub(crate) fn directory_inheriting_rights() -> Vec<(wasip1::Rights, &'static str)> {
- let mut rights = directory_base_rights();
- rights.extend_from_slice(&[
- (wasip1::RIGHTS_FD_DATASYNC, "FD_DATASYNC"),
- (wasip1::RIGHTS_FD_READ, "FD_READ"),
- (wasip1::RIGHTS_FD_SEEK, "FD_SEEK"),
- (wasip1::RIGHTS_FD_FDSTAT_SET_FLAGS, "FD_FDSTAT_SET_FLAGS"),
- (wasip1::RIGHTS_FD_SYNC, "FD_SYNC"),
- (wasip1::RIGHTS_FD_TELL, "FD_TELL"),
- (wasip1::RIGHTS_FD_WRITE, "FD_WRITE"),
- (wasip1::RIGHTS_FD_ADVISE, "FD_ADVISE"),
- (wasip1::RIGHTS_FD_ALLOCATE, "FD_ALLOCATE"),
- (wasip1::RIGHTS_FD_FILESTAT_GET, "FD_FILESTAT_GET"),
- (wasip1::RIGHTS_FD_FILESTAT_SET_SIZE, "FD_FILESTAT_SET_SIZE"),
- (
- wasip1::RIGHTS_FD_FILESTAT_SET_TIMES,
- "FD_FILESTAT_SET_TIMES",
- ),
- (wasip1::RIGHTS_POLL_FD_READWRITE, "POLL_FD_READWRITE"),
- ]);
- rights
-}
diff --git a/crates/test-programs/src/bin/p1_path_open_read_write.rs b/crates/test-programs/src/bin/p1_path_open_read_write.rs
deleted file mode 100644
index f856b8926c..0000000000
--- a/crates/test-programs/src/bin/p1_path_open_read_write.rs
+++ /dev/null
@@ -1,180 +0,0 @@
-#![expect(unsafe_op_in_unsafe_fn, reason = "old code, not worth updating yet")]
-
-use std::{env, process};
-use test_programs::preview1::{BlockingMode, assert_errno, create_file, open_scratch_directory};
-
-unsafe fn test_path_open_read_write(dir_fd: wasip1::Fd, blocking_mode: BlockingMode) {
- create_file(dir_fd, "file");
-
- let f_readonly = wasip1::path_open(
- dir_fd,
- 0,
- "file",
- 0,
- wasip1::RIGHTS_FD_READ,
- 0,
- blocking_mode.fd_flags(),
- )
- .expect("open file readonly");
-
- let stat = wasip1::fd_fdstat_get(f_readonly).expect("get fdstat readonly");
- assert!(
- stat.fs_rights_base & wasip1::RIGHTS_FD_READ == wasip1::RIGHTS_FD_READ,
- "readonly has read right"
- );
- assert!(
- stat.fs_rights_base & wasip1::RIGHTS_FD_WRITE == 0,
- "readonly does not have write right"
- );
-
- let buffer = &mut [0u8; 100];
- let iovec = wasip1::Iovec {
- buf: buffer.as_mut_ptr(),
- buf_len: buffer.len(),
- };
- let nread = blocking_mode
- .read(f_readonly, &[iovec])
- .expect("reading readonly file");
- assert_eq!(nread, 0, "readonly file is empty");
-
- let write_buffer = &[1u8; 50];
- let ciovec = wasip1::Ciovec {
- buf: write_buffer.as_ptr(),
- buf_len: write_buffer.len(),
- };
- // PERM is only the failure on windows under wasmtime-wasi. wasi-common
- // fails on windows with BADF, so we can't use the `windows =>` syntax
- // because that doesn't support alternatives like the agnostic syntax does.
- assert_errno!(
- blocking_mode
- .write(f_readonly, &[ciovec])
- .err()
- .expect("read of writeonly fails"),
- wasip1::ERRNO_PERM,
- wasip1::ERRNO_BADF,
- wasip1::ERRNO_ACCES
- );
-
- wasip1::fd_close(f_readonly).expect("close readonly");
-
- // =============== WRITE ONLY ==================
- let f_writeonly = wasip1::path_open(
- dir_fd,
- 0,
- "file",
- 0,
- wasip1::RIGHTS_FD_WRITE,
- 0,
- blocking_mode.fd_flags(),
- )
- .expect("open file writeonly");
-
- let stat = wasip1::fd_fdstat_get(f_writeonly).expect("get fdstat writeonly");
- assert!(
- stat.fs_rights_base & wasip1::RIGHTS_FD_READ == 0,
- "writeonly does not have read right"
- );
- assert!(
- stat.fs_rights_base & wasip1::RIGHTS_FD_READDIR == 0,
- "writeonly does not have readdir right"
- );
- assert!(
- stat.fs_rights_base & wasip1::RIGHTS_FD_WRITE == wasip1::RIGHTS_FD_WRITE,
- "writeonly has write right"
- );
-
- // See above for description of PERM
- assert_errno!(
- blocking_mode
- .read(f_writeonly, &[iovec])
- .err()
- .expect("read of writeonly fails"),
- wasip1::ERRNO_PERM,
- wasip1::ERRNO_BADF,
- wasip1::ERRNO_ACCES
- );
- let bytes_written = blocking_mode
- .write(f_writeonly, &[ciovec])
- .expect("write to writeonly");
- assert_eq!(bytes_written, write_buffer.len());
-
- wasip1::fd_close(f_writeonly).expect("close writeonly");
-
- // ============== READ WRITE =======================
-
- let f_readwrite = wasip1::path_open(
- dir_fd,
- 0,
- "file",
- 0,
- wasip1::RIGHTS_FD_READ | wasip1::RIGHTS_FD_WRITE,
- 0,
- blocking_mode.fd_flags(),
- )
- .expect("open file readwrite");
- let stat = wasip1::fd_fdstat_get(f_readwrite).expect("get fdstat readwrite");
- assert!(
- stat.fs_rights_base & wasip1::RIGHTS_FD_READ == wasip1::RIGHTS_FD_READ,
- "readwrite has read right"
- );
- assert!(
- stat.fs_rights_base & wasip1::RIGHTS_FD_WRITE == wasip1::RIGHTS_FD_WRITE,
- "readwrite has write right"
- );
-
- let nread = blocking_mode
- .read(f_readwrite, &[iovec])
- .expect("reading readwrite file");
- assert_eq!(
- nread,
- write_buffer.len(),
- "readwrite file contains contents from writeonly open"
- );
-
- let write_buffer_2 = &[2u8; 25];
- let ciovec = wasip1::Ciovec {
- buf: write_buffer_2.as_ptr(),
- buf_len: write_buffer_2.len(),
- };
- let bytes_written = blocking_mode
- .write(f_readwrite, &[ciovec])
- .expect("write to readwrite");
- assert_eq!(bytes_written, write_buffer_2.len());
-
- let filestat = wasip1::fd_filestat_get(f_readwrite).expect("get filestat readwrite");
- assert_eq!(
- filestat.size as usize,
- write_buffer.len() + write_buffer_2.len(),
- "total written is both write buffers"
- );
-
- wasip1::fd_close(f_readwrite).expect("close readwrite");
-
- wasip1::path_unlink_file(dir_fd, "file").expect("removing a file");
-}
-
-fn main() {
- let mut args = env::args();
- let prog = args.next().unwrap();
- let arg = if let Some(arg) = args.next() {
- arg
- } else {
- eprintln!("usage: {prog} <scratch directory>");
- process::exit(1);
- };
-
- // Open scratch directory
- let dir_fd = match open_scratch_directory(&arg) {
- Ok(dir_fd) => dir_fd,
- Err(err) => {
- eprintln!("{err}");
- process::exit(1)
- }
- };
-
- // Run the tests.
- unsafe {
- test_path_open_read_write(dir_fd, BlockingMode::Blocking);
- test_path_open_read_write(dir_fd, BlockingMode::NonBlocking);
- }
-}
diff --git a/crates/test-programs/src/bin/p1_path_rename.rs b/crates/test-programs/src/bin/p1_path_rename.rs
deleted file mode 100644
index 5cbc4df0b6..0000000000
--- a/crates/test-programs/src/bin/p1_path_rename.rs
+++ /dev/null
@@ -1,161 +0,0 @@
-#![expect(unsafe_op_in_unsafe_fn, reason = "old code, not worth updating yet")]
-
-use std::{env, process};
-use test_programs::preview1::{TestConfig, assert_errno, create_file, open_scratch_directory};
-
-unsafe fn test_path_rename(dir_fd: wasip1::Fd) {
- // First, try renaming a dir to nonexistent path
- // Create source directory
- wasip1::path_create_directory(dir_fd, "source").expect("creating a directory");
-
- // Try renaming the directory
- wasip1::path_rename(dir_fd, "source", dir_fd, "target").expect("renaming a directory");
-
- // Check that source directory doesn't exist anymore
- assert_errno!(
- wasip1::path_open(dir_fd, 0, "source", wasip1::OFLAGS_DIRECTORY, 0, 0, 0)
- .expect_err("opening a nonexistent path as a directory should fail"),
- wasip1::ERRNO_NOENT
- );
-
- // Check that target directory exists
- let mut fd = wasip1::path_open(dir_fd, 0, "target", wasip1::OFLAGS_DIRECTORY, 0, 0, 0)
- .expect("opening renamed path as a directory");
- assert!(
- fd > libc::STDERR_FILENO as wasip1::Fd,
- "file descriptor range check",
- );
-
- wasip1::fd_close(fd).expect("closing a file");
- wasip1::path_remove_directory(dir_fd, "target").expect("removing a directory");
-
- // Now, try renaming a dir to existing empty dir
- wasip1::path_create_directory(dir_fd, "source").expect("creating a directory");
- wasip1::path_create_directory(dir_fd, "target").expect("creating a directory");
- wasip1::path_rename(dir_fd, "source", dir_fd, "target").expect("renaming a directory");
-
- // Check that source directory doesn't exist anymore
- assert_errno!(
- wasip1::path_open(dir_fd, 0, "source", wasip1::OFLAGS_DIRECTORY, 0, 0, 0)
- .expect_err("opening a nonexistent path as a directory"),
- wasip1::ERRNO_NOENT
- );
-
- // Check that target directory exists
- fd = wasip1::path_open(dir_fd, 0, "target", wasip1::OFLAGS_DIRECTORY, 0, 0, 0)
- .expect("opening renamed path as a directory");
- assert!(
- fd > libc::STDERR_FILENO as wasip1::Fd,
- "file descriptor range check",
- );
-
- wasip1::fd_close(fd).expect("closing a file");
- wasip1::path_remove_directory(dir_fd, "target").expect("removing a directory");
-
- // Now, try renaming a dir to existing non-empty dir
- wasip1::path_create_directory(dir_fd, "source").expect("creating a directory");
- wasip1::path_create_directory(dir_fd, "target").expect("creating a directory");
- create_file(dir_fd, "target/file");
-
- assert_errno!(
- wasip1::path_rename(dir_fd, "source", dir_fd, "target")
- .expect_err("renaming directory to a nonempty directory"),
- wasip1::ERRNO_NOTEMPTY
- );
-
- // Try renaming dir to a file
- if TestConfig::from_env().support_rename_dir_onto_file() {
- wasip1::path_rename(dir_fd, "source", dir_fd, "target/file").unwrap();
- wasip1::path_remove_directory(dir_fd, "target/file").expect("removing a directory");
- } else {
- assert_errno!(
- wasip1::path_rename(dir_fd, "source", dir_fd, "target/file")
- .expect_err("renaming a directory to a file"),
- wasip1::ERRNO_NOTDIR
- );
- wasip1::path_unlink_file(dir_fd, "target/file").expect("removing a file");
- wasip1::path_remove_directory(dir_fd, "source").expect("removing a directory");
- }
- wasip1::path_remove_directory(dir_fd, "target").expect("removing a directory");
-
- // Now, try renaming a file to a nonexistent path
- create_file(dir_fd, "source");
- wasip1::path_rename(dir_fd, "source", dir_fd, "target").expect("renaming a file");
-
- // Check that source file doesn't exist anymore
- assert_errno!(
- wasip1::path_open(dir_fd, 0, "source", 0, 0, 0, 0)
- .expect_err("opening a nonexistent path should fail"),
- wasip1::ERRNO_NOENT
- );
-
- // Check that target file exists
- fd = wasip1::path_open(dir_fd, 0, "target", 0, 0, 0, 0).expect("opening renamed path");
- assert!(
- fd > libc::STDERR_FILENO as wasip1::Fd,
- "file descriptor range check",
- );
-
- wasip1::fd_close(fd).expect("closing a file");
- wasip1::path_unlink_file(dir_fd, "target").expect("removing a file");
-
- // Now, try renaming file to an existing file
- create_file(dir_fd, "source");
- create_file(dir_fd, "target");
-
- wasip1::path_rename(dir_fd, "source", dir_fd, "target")
- .expect("renaming file to another existing file");
-
- // Check that source file doesn't exist anymore
- assert_errno!(
- wasip1::path_open(dir_fd, 0, "source", 0, 0, 0, 0).expect_err("opening a nonexistent path"),
- wasip1::ERRNO_NOENT
- );
-
- // Check that target file exists
- fd = wasip1::path_open(dir_fd, 0, "target", 0, 0, 0, 0).expect("opening renamed path");
- assert!(
- fd > libc::STDERR_FILENO as wasip1::Fd,
- "file descriptor range check",
- );
-
- wasip1::fd_close(fd).expect("closing a file");
- wasip1::path_unlink_file(dir_fd, "target").expect("removing a file");
-
- // Try renaming to an (empty) directory instead
- create_file(dir_fd, "source");
- wasip1::path_create_directory(dir_fd, "target").expect("creating a directory");
-
- assert_errno!(
- wasip1::path_rename(dir_fd, "source", dir_fd, "target")
- .expect_err("renaming a file to existing directory should fail"),
- windows => wasip1::ERRNO_ACCES,
- unix => wasip1::ERRNO_ISDIR
- );
-
- wasip1::path_remove_directory(dir_fd, "target").expect("removing a directory");
- wasip1::path_unlink_file(dir_fd, "source").expect("removing a file");
-}
-
-fn main() {
- let mut args = env::args();
- let prog = args.next().unwrap();
- let arg = if let Some(arg) = args.next() {
- arg
- } else {
- eprintln!("usage: {prog} <scratch directory>");
- process::exit(1);
- };
-
- // Open scratch directory
- let dir_fd = match open_scratch_directory(&arg) {
- Ok(dir_fd) => dir_fd,
- Err(err) => {
- eprintln!("{err}");
- process::exit(1)
- }
- };
-
- // Run the tests.
- unsafe { test_path_rename(dir_fd) }
-}
diff --git a/crates/test-programs/src/bin/p1_path_rename_dir_trailing_slashes.rs b/crates/test-programs/src/bin/p1_path_rename_dir_trailing_slashes.rs
deleted file mode 100644
index 3fb4884deb..0000000000
--- a/crates/test-programs/src/bin/p1_path_rename_dir_trailing_slashes.rs
+++ /dev/null
@@ -1,41 +0,0 @@
-#![expect(unsafe_op_in_unsafe_fn, reason = "old code, not worth updating yet")]
-
-use std::{env, process};
-use test_programs::preview1::open_scratch_directory;
-
-unsafe fn test_path_rename_trailing_slashes(dir_fd: wasip1::Fd) {
- // Test renaming a directory with a trailing slash in the name.
- wasip1::path_create_directory(dir_fd, "source").expect("creating a directory");
- wasip1::path_rename(dir_fd, "source/", dir_fd, "target")
- .expect("renaming a directory with a trailing slash in the source name");
- wasip1::path_rename(dir_fd, "target", dir_fd, "source/")
- .expect("renaming a directory with a trailing slash in the destination name");
- wasip1::path_rename(dir_fd, "source/", dir_fd, "target/")
- .expect("renaming a directory with a trailing slash in the source and destination names");
- wasip1::path_rename(dir_fd, "target", dir_fd, "source")
- .expect("renaming a directory with no trailing slashes at all should work");
- wasip1::path_remove_directory(dir_fd, "source").expect("removing the directory");
-}
-
-fn main() {
- let mut args = env::args();
- let prog = args.next().unwrap();
- let arg = if let Some(arg) = args.next() {
- arg
- } else {
- eprintln!("usage: {prog} <scratch directory>");
- process::exit(1);
- };
-
- // Open scratch directory
- let dir_fd = match open_scratch_directory(&arg) {
- Ok(dir_fd) => dir_fd,
- Err(err) => {
- eprintln!("{err}");
- process::exit(1)
- }
- };
-
- // Run the tests.
- unsafe { test_path_rename_trailing_slashes(dir_fd) }
-}
diff --git a/crates/test-programs/src/bin/p1_path_symlink_trailing_slashes.rs b/crates/test-programs/src/bin/p1_path_symlink_trailing_slashes.rs
deleted file mode 100644
index fd1ab55038..0000000000
--- a/crates/test-programs/src/bin/p1_path_symlink_trailing_slashes.rs
+++ /dev/null
@@ -1,85 +0,0 @@
-#![expect(unsafe_op_in_unsafe_fn, reason = "old code, not worth updating yet")]
-
-use std::{env, process};
-use test_programs::preview1::{assert_errno, config, create_file, open_scratch_directory};
-
-unsafe fn test_path_symlink_trailing_slashes(dir_fd: wasip1::Fd) {
- if config().support_dangling_filesystem() {
- // Dangling symlink: Link destination shouldn't end with a slash.
- assert_errno!(
- wasip1::path_symlink("source", dir_fd, "target/")
- .expect_err("link destination ending with a slash should fail"),
- wasip1::ERRNO_NOENT
- );
-
- // Dangling symlink: Without the trailing slash, this should succeed.
- wasip1::path_symlink("source", dir_fd, "target")
- .expect("link destination ending with a slash");
- wasip1::path_unlink_file(dir_fd, "target").expect("removing a file");
- }
-
- // Link destination already exists, target has trailing slash.
- wasip1::path_create_directory(dir_fd, "target").expect("creating a directory");
- assert_errno!(
- wasip1::path_symlink("source", dir_fd, "target/")
- .expect_err("link destination already exists"),
- unix => wasip1::ERRNO_EXIST,
- windows => wasip1::ERRNO_NOENT
- );
- wasip1::path_remove_directory(dir_fd, "target").expect("removing a directory");
-
- // Link destination already exists, target has no trailing slash.
- wasip1::path_create_directory(dir_fd, "target").expect("creating a directory");
- assert_errno!(
- wasip1::path_symlink("source", dir_fd, "target")
- .expect_err("link destination already exists"),
- unix => wasip1::ERRNO_EXIST,
- windows => wasip1::ERRNO_NOENT
- );
- wasip1::path_remove_directory(dir_fd, "target").expect("removing a directory");
-
- // Link destination already exists, target has trailing slash.
- create_file(dir_fd, "target");
-
- assert_errno!(
- wasip1::path_symlink("source", dir_fd, "target/")
- .expect_err("link destination already exists"),
- unix => wasip1::ERRNO_NOTDIR,
- windows => wasip1::ERRNO_NOENT
- );
- wasip1::path_unlink_file(dir_fd, "target").expect("removing a file");
-
- // Link destination already exists, target has no trailing slash.
- create_file(dir_fd, "target");
-
- assert_errno!(
- wasip1::path_symlink("source", dir_fd, "target")
- .expect_err("link destination already exists"),
- unix => wasip1::ERRNO_EXIST,
- windows => wasip1::ERRNO_NOENT
- );
- wasip1::path_unlink_file(dir_fd, "target").expect("removing a file");
-}
-
-fn main() {
- let mut args = env::args();
- let prog = args.next().unwrap();
- let arg = if let Some(arg) = args.next() {
- arg
- } else {
- eprintln!("usage: {prog} <scratch directory>");
- process::exit(1);
- };
-
- // Open scratch directory
- let dir_fd = match open_scratch_directory(&arg) {
- Ok(dir_fd) => dir_fd,
- Err(err) => {
- eprintln!("{err}");
- process::exit(1)
- }
- };
-
- // Run the tests.
- unsafe { test_path_symlink_trailing_slashes(dir_fd) }
-}
diff --git a/crates/test-programs/src/bin/p1_poll_oneoff_stdio.rs b/crates/test-programs/src/bin/p1_poll_oneoff_stdio.rs
deleted file mode 100644
index c6a9f9e77d..0000000000
--- a/crates/test-programs/src/bin/p1_poll_oneoff_stdio.rs
+++ /dev/null
@@ -1,142 +0,0 @@
-#![expect(unsafe_op_in_unsafe_fn, reason = "old code, not worth updating yet")]
-
-use std::collections::HashMap;
-use std::mem::MaybeUninit;
-use test_programs::preview1::{STDERR_FD, STDIN_FD, STDOUT_FD, assert_errno};
-
-const TIMEOUT: u64 = 200_000_000u64; // 200 milliseconds, required to satisfy slow execution in CI
-const CLOCK_ID: wasip1::Userdata = 0x0123_45678;
-const STDIN_ID: wasip1::Userdata = 0x8765_43210;
-
-unsafe fn poll_oneoff_impl(
- r#in: &[wasip1::Subscription],
-) -> Result<Vec<wasip1::Event>, wasip1::Errno> {
- let mut out: Vec<wasip1::Event> = Vec::new();
- out.resize_with(r#in.len(), || {
- MaybeUninit::<wasip1::Event>::zeroed().assume_init()
- });
- let size = wasip1::poll_oneoff(r#in.as_ptr(), out.as_mut_ptr(), r#in.len())?;
- out.truncate(size);
- Ok(out)
-}
-
-unsafe fn test_stdin_read() {
- let clock = wasip1::SubscriptionClock {
- id: wasip1::CLOCKID_MONOTONIC,
- timeout: TIMEOUT,
- precision: 0,
- flags: 0,
- };
- let fd_readwrite = wasip1::SubscriptionFdReadwrite {
- file_descriptor: STDIN_FD,
- };
- // Either stdin can be ready for reading, or this poll can timeout.
- let r#in = [
- wasip1::Subscription {
- userdata: CLOCK_ID,
- u: wasip1::SubscriptionU {
- tag: wasip1::EVENTTYPE_CLOCK.raw(),
- u: wasip1::SubscriptionUU { clock },
- },
- },
- wasip1::Subscription {
- userdata: STDIN_ID,
- u: wasip1::SubscriptionU {
- tag: wasip1::EVENTTYPE_FD_READ.raw(),
- u: wasip1::SubscriptionUU {
- fd_read: fd_readwrite,
- },
- },
- },
- ];
- let out = poll_oneoff_impl(&r#in).unwrap();
- // The result should be either a timeout, or that stdin is ready for reading.
- // Both are valid behaviors that depend on the test environment.
- assert!(out.len() >= 1, "stdin read should return at least 1 event");
- for event in out {
- if event.type_ == wasip1::EVENTTYPE_CLOCK {
- assert_errno!(event.error, wasip1::ERRNO_SUCCESS);
- assert_eq!(
- event.userdata, CLOCK_ID,
- "the event.userdata should contain CLOCK_ID",
- );
- } else if event.type_ == wasip1::EVENTTYPE_FD_READ {
- assert_errno!(event.error, wasip1::ERRNO_SUCCESS);
- assert_eq!(
- event.userdata, STDIN_ID,
- "the event.userdata should contain STDIN_ID",
- );
- } else {
- panic!("unexpected event type {}", event.type_.raw());
- }
- }
-}
-
-fn writable_subs(h: &HashMap<u64, wasip1::Fd>) -> Vec<wasip1::Subscription> {
- h.iter()
- .map(|(ud, fd)| wasip1::Subscription {
- userdata: *ud,
- u: wasip1::SubscriptionU {
- tag: wasip1::EVENTTYPE_FD_WRITE.raw(),
- u: wasip1::SubscriptionUU {
- fd_write: wasip1::SubscriptionFdReadwrite {
- file_descriptor: *fd,
- },
- },
- },
- })
- .collect()
-}
-
-unsafe fn test_stdout_stderr_write() {
- let mut writable: HashMap<u64, wasip1::Fd> =
- [(1, STDOUT_FD), (2, STDERR_FD)].into_iter().collect();
-
- let clock = wasip1::Subscription {
- userdata: CLOCK_ID,
- u: wasip1::SubscriptionU {
- tag: wasip1::EVENTTYPE_CLOCK.raw(),
- u: wasip1::SubscriptionUU {
- clock: wasip1::SubscriptionClock {
- id: wasip1::CLOCKID_MONOTONIC,
- timeout: TIMEOUT,
- precision: 0,
- flags: 0,
- },
- },
- },
- };
- let mut timed_out = false;
- while !writable.is_empty() {
- if timed_out {
- panic!("timed out with the following pending subs: {writable:?}")
- }
- let mut subs = writable_subs(&writable);
- subs.push(clock);
- let out = poll_oneoff_impl(&subs).unwrap();
- for event in out {
- match event.userdata {
- CLOCK_ID => timed_out = true,
- ud => {
- if let Some(_) = writable.remove(&ud) {
- assert_eq!(event.type_, wasip1::EVENTTYPE_FD_WRITE);
- assert_errno!(event.error, wasip1::ERRNO_SUCCESS);
- } else {
- panic!("Unknown userdata {ud}, pending sub: {writable:?}")
- }
- }
- }
- }
- }
-}
-
-unsafe fn test_poll_oneoff() {
- // NB we assume that stdin/stdout/stderr are valid and open
- // for the duration of the test case
- test_stdin_read();
- test_stdout_stderr_write();
-}
-fn main() {
- // Run the tests.
- unsafe { test_poll_oneoff() }
-}
diff --git a/crates/test-programs/src/bin/p1_regular_file_isatty.rs b/crates/test-programs/src/bin/p1_regular_file_isatty.rs
deleted file mode 100644
index 7db70cb2e3..0000000000
--- a/crates/test-programs/src/bin/p1_regular_file_isatty.rs
+++ /dev/null
@@ -1,44 +0,0 @@
-#![expect(unsafe_op_in_unsafe_fn, reason = "old code, not worth updating yet")]
-
-use std::{env, process};
-use test_programs::preview1::open_scratch_directory;
-
-unsafe fn test_file_isatty(dir_fd: wasip1::Fd) {
- // Create a file in the scratch directory and test if it's a tty.
- let file_fd = wasip1::path_open(dir_fd, 0, "file", wasip1::OFLAGS_CREAT, 0, 0, 0)
- .expect("opening a file");
- assert!(
- file_fd > libc::STDERR_FILENO as wasip1::Fd,
- "file descriptor range check",
- );
- assert_eq!(
- libc::isatty(file_fd as std::os::raw::c_int),
- 0,
- "file is a tty"
- );
- wasip1::fd_close(file_fd).expect("closing a file");
- wasip1::path_unlink_file(dir_fd, "file").expect("removing a file");
-}
-
-fn main() {
- let mut args = env::args();
- let prog = args.next().unwrap();
- let arg = if let Some(arg) = args.next() {
- arg
- } else {
- eprintln!("usage: {prog} <scratch directory>");
- process::exit(1);
- };
-
- // Open scratch directory
- let dir_fd = match open_scratch_directory(&arg) {
- Ok(dir_fd) => dir_fd,
- Err(err) => {
- eprintln!("{err}");
- process::exit(1)
- }
- };
-
- // Run the tests.
- unsafe { test_file_isatty(dir_fd) }
-}
diff --git a/crates/test-programs/src/bin/p1_remove_directory.rs b/crates/test-programs/src/bin/p1_remove_directory.rs
deleted file mode 100644
index cb19ff5a2d..0000000000
--- a/crates/test-programs/src/bin/p1_remove_directory.rs
+++ /dev/null
@@ -1,59 +0,0 @@
-#![expect(unsafe_op_in_unsafe_fn, reason = "old code, not worth updating yet")]
-
-use std::{env, process};
-use test_programs::preview1::{assert_errno, create_file, open_scratch_directory};
-
-unsafe fn test_remove_directory(dir_fd: wasip1::Fd) {
- // Create a directory in the scratch directory.
- wasip1::path_create_directory(dir_fd, "dir").expect("creating a directory");
-
- // Test that removing it succeeds.
- wasip1::path_remove_directory(dir_fd, "dir")
- .expect("remove_directory on a directory should succeed");
-
- // There isn't consistient behavior across operating systems of whether removing with a
- // directory where the path has a trailing slash succeeds or fails, so we won't test
- // that behavior.
-
- // Create a temporary file.
- create_file(dir_fd, "file");
-
- // Test that removing it with no trailing slash fails.
- assert_errno!(
- wasip1::path_remove_directory(dir_fd, "file")
- .expect_err("remove_directory without a trailing slash on a file should fail"),
- wasip1::ERRNO_NOTDIR
- );
-
- // Test that removing it with a trailing slash fails.
- assert_errno!(
- wasip1::path_remove_directory(dir_fd, "file/")
- .expect_err("remove_directory with a trailing slash on a file should fail"),
- wasip1::ERRNO_NOTDIR
- );
-
- wasip1::path_unlink_file(dir_fd, "file").expect("removing a file");
-}
-
-fn main() {
- let mut args = env::args();
- let prog = args.next().unwrap();
- let arg = if let Some(arg) = args.next() {
- arg
- } else {
- eprintln!("usage: {prog} <scratch directory>");
- process::exit(1);
- };
-
- // Open scratch directory
- let dir_fd = match open_scratch_directory(&arg) {
- Ok(dir_fd) => dir_fd,
- Err(err) => {
- eprintln!("{err}");
- process::exit(1)
- }
- };
-
- // Run the tests.
- unsafe { test_remove_directory(dir_fd) }
-}
diff --git a/crates/test-programs/src/bin/p1_remove_nonempty_directory.rs b/crates/test-programs/src/bin/p1_remove_nonempty_directory.rs
deleted file mode 100644
index a715117752..0000000000
--- a/crates/test-programs/src/bin/p1_remove_nonempty_directory.rs
+++ /dev/null
@@ -1,47 +0,0 @@
-#![expect(unsafe_op_in_unsafe_fn, reason = "old code, not worth updating yet")]
-
-use std::{env, process};
-use test_programs::preview1::{assert_errno, open_scratch_directory};
-
-unsafe fn test_remove_nonempty_directory(dir_fd: wasip1::Fd) {
- // Create a directory in the scratch directory.
- wasip1::path_create_directory(dir_fd, "dir").expect("creating a directory");
-
- // Create a directory in the directory we just created.
- wasip1::path_create_directory(dir_fd, "dir/nested").expect("creating a subdirectory");
-
- // Test that attempting to unlink the first directory returns the expected error code.
- assert_errno!(
- wasip1::path_remove_directory(dir_fd, "dir")
- .expect_err("remove_directory on a directory should return ENOTEMPTY"),
- wasip1::ERRNO_NOTEMPTY
- );
-
- // Removing the directories.
- wasip1::path_remove_directory(dir_fd, "dir/nested")
- .expect("remove_directory on a nested directory should succeed");
- wasip1::path_remove_directory(dir_fd, "dir").expect("removing a directory");
-}
-
-fn main() {
- let mut args = env::args();
- let prog = args.next().unwrap();
- let arg = if let Some(arg) = args.next() {
- arg
- } else {
- eprintln!("usage: {prog} <scratch directory>");
- process::exit(1);
- };
-
- // Open scratch directory
- let dir_fd = match open_scratch_directory(&arg) {
- Ok(dir_fd) => dir_fd,
- Err(err) => {
- eprintln!("{err}");
- process::exit(1)
- }
- };
-
- // Run the tests.
- unsafe { test_remove_nonempty_directory(dir_fd) }
-}
diff --git a/crates/test-programs/src/bin/p1_sched_yield.rs b/crates/test-programs/src/bin/p1_sched_yield.rs
deleted file mode 100644
index d6ed2217db..0000000000
--- a/crates/test-programs/src/bin/p1_sched_yield.rs
+++ /dev/null
@@ -1,10 +0,0 @@
-#![expect(unsafe_op_in_unsafe_fn, reason = "old code, not worth updating yet")]
-
-unsafe fn test_sched_yield() {
- wasip1::sched_yield().expect("sched_yield");
-}
-
-fn main() {
- // Run tests
- unsafe { test_sched_yield() }
-}
diff --git a/crates/test-programs/src/bin/p1_stdio.rs b/crates/test-programs/src/bin/p1_stdio.rs
deleted file mode 100644
index ff5b1c0d62..0000000000
--- a/crates/test-programs/src/bin/p1_stdio.rs
+++ /dev/null
@@ -1,16 +0,0 @@
-#![expect(unsafe_op_in_unsafe_fn, reason = "old code, not worth updating yet")]
-
-use test_programs::preview1::{STDERR_FD, STDIN_FD, STDOUT_FD};
-
-unsafe fn test_stdio() {
- for fd in &[STDIN_FD, STDOUT_FD, STDERR_FD] {
- wasip1::fd_fdstat_get(*fd).expect("fd_fdstat_get on stdio");
- assert_eq!(wasip1::fd_renumber(*fd, *fd + 100), Err(wasip1::ERRNO_BADF));
- wasip1::fd_renumber(*fd, *fd).unwrap();
- }
-}
-
-fn main() {
- // Run the tests.
- unsafe { test_stdio() }
-}
diff --git a/crates/test-programs/src/bin/p1_symlink_create.rs b/crates/test-programs/src/bin/p1_symlink_create.rs
deleted file mode 100644
index 2fcb1f0488..0000000000
--- a/crates/test-programs/src/bin/p1_symlink_create.rs
+++ /dev/null
@@ -1,98 +0,0 @@
-#![expect(unsafe_op_in_unsafe_fn, reason = "old code, not worth updating yet")]
-
-use std::{env, process};
-use test_programs::preview1::open_scratch_directory;
-
-unsafe fn create_symlink_to_file(dir_fd: wasip1::Fd) {
- // Create a directory for the symlink to point to.
- let target_fd = wasip1::path_open(dir_fd, 0, "target", wasip1::OFLAGS_CREAT, 0, 0, 0)
- .expect("creating a file");
- wasip1::fd_close(target_fd).expect("closing file");
-
- // Create a symlink.
- wasip1::path_symlink("target", dir_fd, "symlink").expect("creating a symlink");
-
- // Try to open it as a directory without O_NOFOLLOW.
- let target_file_via_symlink = wasip1::path_open(
- dir_fd,
- wasip1::LOOKUPFLAGS_SYMLINK_FOLLOW,
- "symlink",
- 0,
- 0,
- 0,
- 0,
- )
- .expect("opening a symlink as a directory");
- assert!(
- target_file_via_symlink > libc::STDERR_FILENO as wasip1::Fd,
- "file descriptor range check",
- );
- wasip1::fd_close(target_file_via_symlink).expect("close the symlink file");
-
- // Replace the target directory with a file.
- wasip1::path_unlink_file(dir_fd, "symlink").expect("removing the symlink");
- wasip1::path_unlink_file(dir_fd, "target").expect("removing the target file");
-}
-
-unsafe fn create_symlink_to_directory(dir_fd: wasip1::Fd) {
- // Create a directory for the symlink to point to.
- wasip1::path_create_directory(dir_fd, "target").expect("creating a dir");
-
- // Create a symlink.
- wasip1::path_symlink("target", dir_fd, "symlink").expect("creating a symlink");
-
- // Try to open it as a directory without O_NOFOLLOW.
- let target_dir_via_symlink = wasip1::path_open(
- dir_fd,
- wasip1::LOOKUPFLAGS_SYMLINK_FOLLOW,
- "symlink",
- wasip1::OFLAGS_DIRECTORY,
- 0,
- 0,
- 0,
- )
- .expect("opening a symlink as a directory");
- assert!(
- target_dir_via_symlink > libc::STDERR_FILENO as wasip1::Fd,
- "file descriptor range check",
- );
- wasip1::fd_close(target_dir_via_symlink).expect("closing a file");
-
- // Replace the target directory with a file.
- wasip1::path_unlink_file(dir_fd, "symlink").expect("remove symlink to directory");
- wasip1::path_remove_directory(dir_fd, "target")
- .expect("remove_directory on a directory should succeed");
-}
-
-unsafe fn create_symlink_to_root(dir_fd: wasip1::Fd) {
- // Create a symlink.
- wasip1::path_symlink("/", dir_fd, "symlink")
- .expect_err("creating a symlink to an absolute path");
-}
-
-fn main() {
- let mut args = env::args();
- let prog = args.next().unwrap();
- let arg = if let Some(arg) = args.next() {
- arg
- } else {
- eprintln!("usage: {prog} <scratch directory>");
- process::exit(1);
- };
-
- // Open scratch directory
- let dir_fd = match open_scratch_directory(&arg) {
- Ok(dir_fd) => dir_fd,
- Err(err) => {
- eprintln!("{err}");
- process::exit(1)
- }
- };
-
- // Run the tests.
- unsafe {
- create_symlink_to_file(dir_fd);
- create_symlink_to_directory(dir_fd);
- create_symlink_to_root(dir_fd);
- }
-}
diff --git a/crates/test-programs/src/bin/p1_symlink_filestat.rs b/crates/test-programs/src/bin/p1_symlink_filestat.rs
deleted file mode 100644
index b8d0c388c2..0000000000
--- a/crates/test-programs/src/bin/p1_symlink_filestat.rs
+++ /dev/null
@@ -1,119 +0,0 @@
-#![expect(unsafe_op_in_unsafe_fn, reason = "old code, not worth updating yet")]
-
-use std::{env, process, time::Duration};
-use test_programs::preview1::{TestConfig, assert_fs_time_eq, open_scratch_directory};
-
-unsafe fn test_path_filestat(dir_fd: wasip1::Fd) {
- let cfg = TestConfig::from_env();
- // Create a file in the scratch directory.
- let file_fd = wasip1::path_open(
- dir_fd,
- 0,
- "file",
- wasip1::OFLAGS_CREAT,
- wasip1::RIGHTS_FD_READ | wasip1::RIGHTS_FD_WRITE,
- 0,
- 0,
- )
- .expect("opening a file");
- assert!(
- file_fd > libc::STDERR_FILENO as wasip1::Fd,
- "file descriptor range check",
- );
-
- // Check file size
- let file_stat = wasip1::path_filestat_get(dir_fd, 0, "file").expect("reading file stats");
- assert_eq!(file_stat.size, 0, "file size should be 0");
-
- // Create a symlink
- wasip1::path_symlink("file", dir_fd, "symlink").expect("creating symlink to a file");
-
- // Check path_filestat_set_times on the symlink itself
- let sym_stat = wasip1::path_filestat_get(dir_fd, 0, "symlink").expect("reading symlink stats");
-
- // Modify mtim of symlink
- let sym_new_mtim = Duration::from_nanos(sym_stat.mtim) - cfg.fs_time_precision() * 2;
- wasip1::path_filestat_set_times(
- dir_fd,
- 0,
- "symlink",
- 0,
- sym_new_mtim.as_nanos() as u64,
- wasip1::FSTFLAGS_MTIM,
- )
- .expect("path_filestat_set_times should succeed on symlink");
-
- // Check that symlink mtim motification worked
- let modified_sym_stat = wasip1::path_filestat_get(dir_fd, 0, "symlink")
- .expect("reading file stats after path_filestat_set_times");
-
- assert_fs_time_eq!(
- Duration::from_nanos(modified_sym_stat.mtim),
- sym_new_mtim,
- "symlink mtim should change"
- );
-
- // Check that pointee mtim is not modified
- let unmodified_file_stat = wasip1::path_filestat_get(dir_fd, 0, "file")
- .expect("reading file stats after path_filestat_set_times");
-
- assert_eq!(
- unmodified_file_stat.mtim, file_stat.mtim,
- "file mtim should not change"
- );
-
- // Now, dereference the symlink
- let deref_sym_stat =
- wasip1::path_filestat_get(dir_fd, wasip1::LOOKUPFLAGS_SYMLINK_FOLLOW, "symlink")
- .expect("reading file stats on the dereferenced symlink");
- assert_eq!(
- deref_sym_stat.mtim, file_stat.mtim,
- "symlink mtim should be equal to pointee's when dereferenced"
- );
-
- // Finally, change stat of the original file by dereferencing the symlink
- wasip1::path_filestat_set_times(
- dir_fd,
- wasip1::LOOKUPFLAGS_SYMLINK_FOLLOW,
- "symlink",
- 0,
- sym_stat.mtim,
- wasip1::FSTFLAGS_MTIM,
- )
- .expect("path_filestat_set_times should succeed on setting stat on original file");
-
- let new_file_stat = wasip1::path_filestat_get(dir_fd, 0, "file")
- .expect("reading file stats after path_filestat_set_times");
-
- assert_fs_time_eq!(
- Duration::from_nanos(new_file_stat.mtim),
- Duration::from_nanos(sym_stat.mtim),
- "mtim should change"
- );
-
- wasip1::fd_close(file_fd).expect("closing a file");
- wasip1::path_unlink_file(dir_fd, "symlink").expect("removing a symlink");
- wasip1::path_unlink_file(dir_fd, "file").expect("removing a file");
-}
-fn main() {
- let mut args = env::args();
- let prog = args.next().unwrap();
- let arg = if let Some(arg) = args.next() {
- arg
- } else {
- eprintln!("usage: {prog} <scratch directory>");
- process::exit(1);
- };
-
- // Open scratch directory
- let dir_fd = match open_scratch_directory(&arg) {
- Ok(dir_fd) => dir_fd,
- Err(err) => {
- eprintln!("{err}");
- process::exit(1)
- }
- };
-
- // Run the tests.
- unsafe { test_path_filestat(dir_fd) }
-}
diff --git a/crates/test-programs/src/bin/p1_symlink_loop.rs b/crates/test-programs/src/bin/p1_symlink_loop.rs
deleted file mode 100644
index e7129f4a34..0000000000
--- a/crates/test-programs/src/bin/p1_symlink_loop.rs
+++ /dev/null
@@ -1,44 +0,0 @@
-#![expect(unsafe_op_in_unsafe_fn, reason = "old code, not worth updating yet")]
-
-use std::{env, process};
-use test_programs::preview1::{assert_errno, config, open_scratch_directory};
-
-unsafe fn test_symlink_loop(dir_fd: wasip1::Fd) {
- if config().support_dangling_filesystem() {
- // Create a self-referencing symlink.
- wasip1::path_symlink("symlink", dir_fd, "symlink").expect("creating a symlink");
-
- // Try to open it.
- assert_errno!(
- wasip1::path_open(dir_fd, 0, "symlink", 0, 0, 0, 0)
- .expect_err("opening a self-referencing symlink"),
- wasip1::ERRNO_LOOP
- );
-
- // Clean up.
- wasip1::path_unlink_file(dir_fd, "symlink").expect("removing a file");
- }
-}
-
-fn main() {
- let mut args = env::args();
- let prog = args.next().unwrap();
- let arg = if let Some(arg) = args.next() {
- arg
- } else {
- eprintln!("usage: {prog} <scratch directory>");
- process::exit(1);
- };
-
- // Open scratch directory
- let dir_fd = match open_scratch_directory(&arg) {
- Ok(dir_fd) => dir_fd,
- Err(err) => {
- eprintln!("{err}");
- process::exit(1)
- }
- };
-
- // Run the tests.
- unsafe { test_symlink_loop(dir_fd) }
-}
diff --git a/crates/test-programs/src/bin/p1_unlink_file_trailing_slashes.rs b/crates/test-programs/src/bin/p1_unlink_file_trailing_slashes.rs
deleted file mode 100644
index ea88e22135..0000000000
--- a/crates/test-programs/src/bin/p1_unlink_file_trailing_slashes.rs
+++ /dev/null
@@ -1,67 +0,0 @@
-#![expect(unsafe_op_in_unsafe_fn, reason = "old code, not worth updating yet")]
-
-use std::{env, process};
-use test_programs::preview1::{assert_errno, create_file, open_scratch_directory};
-
-unsafe fn test_unlink_file_trailing_slashes(dir_fd: wasip1::Fd) {
- // Create a directory in the scratch directory.
- wasip1::path_create_directory(dir_fd, "dir").expect("creating a directory");
-
- // Test that unlinking it fails.
- assert_errno!(
- wasip1::path_unlink_file(dir_fd, "dir")
- .expect_err("unlink_file on a directory should fail"),
- macos => wasip1::ERRNO_PERM,
- unix => wasip1::ERRNO_ISDIR,
- windows => wasip1::ERRNO_ACCES
- );
-
- // Test that unlinking it with a trailing flash fails.
- assert_errno!(
- wasip1::path_unlink_file(dir_fd, "dir/")
- .expect_err("unlink_file on a directory should fail"),
- macos => wasip1::ERRNO_PERM,
- unix => wasip1::ERRNO_ISDIR,
- windows => wasip1::ERRNO_ACCES
- );
-
- // Clean up.
- wasip1::path_remove_directory(dir_fd, "dir").expect("removing a directory");
-
- // Create a temporary file.
- create_file(dir_fd, "file");
-
- // Test that unlinking it with a trailing flash fails.
- assert_errno!(
- wasip1::path_unlink_file(dir_fd, "file/")
- .expect_err("unlink_file with a trailing slash should fail"),
- wasip1::ERRNO_NOTDIR
- );
-
- // Test that unlinking it with no trailing flash succeeds.
- wasip1::path_unlink_file(dir_fd, "file")
- .expect("unlink_file with no trailing slash should succeed");
-}
-
-fn main() {
- let mut args = env::args();
- let prog = args.next().unwrap();
- let arg = if let Some(arg) = args.next() {
- arg
- } else {
- eprintln!("usage: {prog} <scratch directory>");
- process::exit(1);
- };
-
- // Open scratch directory
- let dir_fd = match open_scratch_directory(&arg) {
- Ok(dir_fd) => dir_fd,
- Err(err) => {
- eprintln!("{err}");
- process::exit(1)
- }
- };
-
- // Run the tests.
- unsafe { test_unlink_file_trailing_slashes(dir_fd) }
-}
diff --git a/crates/test-programs/src/bin/p2_big_random_buf.rs b/crates/test-programs/src/bin/p2_big_random_buf.rs
new file mode 100644
index 0000000000..de74a1a9f0
--- /dev/null
+++ b/crates/test-programs/src/bin/p2_big_random_buf.rs
@@ -0,0 +1,15 @@
+fn test_big_random_buf() {
+ let buf = wasip2::random::random::get_random_bytes(1024);
+ assert_eq!(buf.len(), 1024);
+ // Chances are pretty good that at least *one* byte will be non-zero in
+ // any meaningful random function producing 1024 u8 values.
+ assert!(
+ buf.iter().any(|x| *x != 0),
+ "get_random_bytes returned all zeros"
+ );
+}
+
+fn main() {
+ // Run the tests.
+ test_big_random_buf()
+}
diff --git a/crates/test-programs/src/bin/p2_clock_time_get.rs b/crates/test-programs/src/bin/p2_clock_time_get.rs
new file mode 100644
index 0000000000..5dbb4b2b11
--- /dev/null
+++ b/crates/test-programs/src/bin/p2_clock_time_get.rs
@@ -0,0 +1,16 @@
+use wasip2::clocks::monotonic_clock;
+
+fn test_clock_time_get() {
+ // Test that reading the monotonic clock succeeds. Even in environments
+ // where it's not desirable to expose high-precision timers, it should
+ // still succeed. `resolution` is where information about precision can be
+ // provided.
+ let first_time = monotonic_clock::now();
+ let time = monotonic_clock::now();
+ assert!(first_time <= time, "monotonic clock should be monotonic");
+}
+
+fn main() {
+ // Run the tests.
+ test_clock_time_get()
+}
diff --git a/crates/test-programs/src/bin/p2_close_preopen.rs b/crates/test-programs/src/bin/p2_close_preopen.rs
new file mode 100644
index 0000000000..b039be44fd
--- /dev/null
+++ b/crates/test-programs/src/bin/p2_close_preopen.rs
@@ -0,0 +1,40 @@
+use wasip2::filesystem::preopens::get_directories;
+use wasip2::filesystem::types::{DescriptorFlags, DescriptorType, OpenFlags, PathFlags};
+
+fn test_close_preopen() {
+ let mut preopens = get_directories();
+ let (preopen, _name) = preopens.remove(0);
+
+ // Open a second handle to the preopened directory.
+ let dir = preopen
+ .open_at(
+ PathFlags::empty(),
+ ".",
+ OpenFlags::DIRECTORY,
+ DescriptorFlags::READ,
+ )
+ .expect("opening the scratch directory");
+
+ // Try to close the preopened directory handle.
+ drop(preopen);
+
+ // Ensure that `dir` is still open.
+ assert_eq!(
+ dir.get_type().expect("failed get_type"),
+ DescriptorType::Directory,
+ "expected the scratch directory to be a directory",
+ );
+
+ // Ensure that the preopen itself is unaffected by closing a handle to it.
+ let preopens = get_directories();
+ assert_eq!(preopens.len(), 1);
+ assert_eq!(
+ preopens[0].0.get_type().expect("failed get_type"),
+ DescriptorType::Directory,
+ );
+}
+
+fn main() {
+ // Run the tests.
+ test_close_preopen()
+}
diff --git a/crates/test-programs/src/bin/p2_dangling_fd.rs b/crates/test-programs/src/bin/p2_dangling_fd.rs
new file mode 100644
index 0000000000..0f8226735a
--- /dev/null
+++ b/crates/test-programs/src/bin/p2_dangling_fd.rs
@@ -0,0 +1,48 @@
+use wasip2::filesystem::preopens::get_directories;
+use wasip2::filesystem::types::{Descriptor, DescriptorFlags, OpenFlags, PathFlags};
+
+fn open(dir: &Descriptor, path: &str, oflags: OpenFlags) -> Descriptor {
+ dir.open_at(PathFlags::empty(), path, oflags, DescriptorFlags::empty())
+ .expect("failed to open")
+}
+
+fn test_dangling_fd(dir: &Descriptor) {
+ // Create a file, open it, delete it without closing the handle,
+ // and then try creating it again
+ let fd = open(dir, "file", OpenFlags::CREATE);
+ drop(fd);
+ let file = open(dir, "file", OpenFlags::empty());
+ // Not all platforms (e.g. Windows) support removing files while a handle
+ // is still open, so skip the test if that fails.
+ if dir.unlink_file_at("file").is_err() {
+ drop(file);
+ dir.unlink_file_at("file").expect("failed to unlink");
+ return;
+ }
+ let fd = open(dir, "file", OpenFlags::CREATE);
+ drop(fd);
+
+ // Now, repeat the same process but for a directory
+ dir.create_directory_at("subdir")
+ .expect("failed to create dir");
+ let subdir = open(dir, "subdir", OpenFlags::DIRECTORY);
+ dir.remove_directory_at("subdir")
+ .expect("failed to remove dir 2");
+ dir.create_directory_at("subdir")
+ .expect("failed to create dir 2");
+
+ // Clean up.
+ drop(file);
+ drop(subdir);
+ dir.unlink_file_at("file").expect("failed to unlink");
+ dir.remove_directory_at("subdir")
+ .expect("failed to remove dir");
+}
+
+fn main() {
+ let preopens = get_directories();
+ let (dir, _name) = &preopens[0];
+
+ // Run the tests.
+ test_dangling_fd(dir)
+}
diff --git a/crates/test-programs/src/bin/p2_dangling_symlink.rs b/crates/test-programs/src/bin/p2_dangling_symlink.rs
new file mode 100644
index 0000000000..6ae4c51ec7
--- /dev/null
+++ b/crates/test-programs/src/bin/p2_dangling_symlink.rs
@@ -0,0 +1,53 @@
+use wasip2::filesystem::preopens::get_directories;
+use wasip2::filesystem::types::{Descriptor, DescriptorFlags, ErrorCode, OpenFlags, PathFlags};
+
+fn test_dangling_symlink(dir: &Descriptor) {
+ // First create a dangling symlink. Not all platforms (e.g. Windows) support
+ // dangling symlinks, so skip the test if creation fails.
+ if dir.symlink_at("target", "symlink").is_err() {
+ return;
+ }
+
+ // Try to open it as a directory with O_NOFOLLOW.
+ let err = dir
+ .open_at(
+ PathFlags::empty(),
+ "symlink",
+ OpenFlags::DIRECTORY,
+ DescriptorFlags::empty(),
+ )
+ .expect_err("opening a dangling symlink as a directory");
+ assert!(
+ matches!(
+ err,
+ ErrorCode::NotDirectory | ErrorCode::Loop | ErrorCode::NoEntry
+ ),
+ "unexpected error {err:?}"
+ );
+
+ // Try to open it as a file with O_NOFOLLOW.
+ let err = dir
+ .open_at(
+ PathFlags::empty(),
+ "symlink",
+ OpenFlags::empty(),
+ DescriptorFlags::empty(),
+ )
+ .expect_err("opening a dangling symlink as a file");
+ assert!(
+ matches!(err, ErrorCode::Loop | ErrorCode::NoEntry),
+ "unexpected error {err:?}"
+ );
+
+ // Clean up.
+ dir.unlink_file_at("symlink")
+ .expect("failed to remove file");
+}
+
+fn main() {
+ let preopens = get_directories();
+ let (dir, _name) = &preopens[0];
+
+ // Run the tests.
+ test_dangling_symlink(dir)
+}
diff --git a/crates/test-programs/src/bin/p2_dir_fd_op_failures.rs b/crates/test-programs/src/bin/p2_dir_fd_op_failures.rs
new file mode 100644
index 0000000000..a0295faf66
--- /dev/null
+++ b/crates/test-programs/src/bin/p2_dir_fd_op_failures.rs
@@ -0,0 +1,37 @@
+use wasip2::filesystem::preopens::get_directories;
+use wasip2::filesystem::types::{Advice, Descriptor, DescriptorType, ErrorCode};
+
+fn test_fd_dir_ops(dir: &Descriptor) {
+ assert_eq!(
+ dir.get_type().expect("failed get_type"),
+ DescriptorType::Directory
+ );
+
+ // On posix, this fails with ERRNO_ISDIR:
+ let r = dir.read_via_stream(0).err();
+ assert_eq!(r, Some(ErrorCode::IsDirectory), "read_via_stream error");
+
+ // Same behavior as specified by POSIX:
+ let r = dir.write_via_stream(0).err();
+ assert_eq!(r, Some(ErrorCode::BadDescriptor), "write_via_stream error");
+
+ let r = dir.append_via_stream().err();
+ assert_eq!(r, Some(ErrorCode::BadDescriptor), "append_via_stream error");
+
+ // posix_fadvise(dirfd, 0, 0, POSIX_FADV_DONTNEED) will return 0 on linux.
+ // not available on mac os.
+ let r = dir.advise(0, 0, Advice::DontNeed).err();
+ assert_eq!(r, Some(ErrorCode::BadDescriptor), "advise error");
+
+ // ftruncate(dirfd, 1) will fail with errno EINVAL on posix.
+ // here, we fail with bad-descriptor instead:
+ let r = dir.set_size(0).err();
+ assert_eq!(r, Some(ErrorCode::BadDescriptor), "set_size error");
+}
+
+fn main() {
+ let preopens = get_directories();
+ let (dir, _name) = &preopens[0];
+
+ test_fd_dir_ops(dir);
+}
diff --git a/crates/test-programs/src/bin/p2_fd_advise.rs b/crates/test-programs/src/bin/p2_fd_advise.rs
new file mode 100644
index 0000000000..d7e806d1de
--- /dev/null
+++ b/crates/test-programs/src/bin/p2_fd_advise.rs
@@ -0,0 +1,42 @@
+use wasip2::filesystem::preopens::get_directories;
+use wasip2::filesystem::types::{Advice, Descriptor, DescriptorFlags, OpenFlags, PathFlags};
+
+fn test_fd_advise(dir: &Descriptor) {
+ // Create a file in the scratch directory.
+ let file = dir
+ .open_at(
+ PathFlags::empty(),
+ "file",
+ OpenFlags::CREATE,
+ DescriptorFlags::READ | DescriptorFlags::WRITE,
+ )
+ .expect("failed to open file");
+
+ // Check file size
+ let stat = file.stat().expect("failed to stat");
+ assert_eq!(stat.size, 0, "file size should be 0");
+
+ // set_size it bigger
+ file.set_size(100).expect("setting size");
+
+ let stat = file.stat().expect("failed to stat 2");
+ assert_eq!(stat.size, 100, "file size should be 100");
+
+ // Advise the kernel
+ file.advise(10, 50, Advice::Normal).expect("failed advise");
+
+ // Advise shouldn't change size
+ let stat = file.stat().expect("failed to stat 3");
+ assert_eq!(stat.size, 100, "file size should be 100");
+
+ drop(file);
+ dir.unlink_file_at("file").expect("failed to unlink");
+}
+
+fn main() {
+ let preopens = get_directories();
+ let (dir, _name) = &preopens[0];
+
+ // Run the tests.
+ test_fd_advise(dir)
+}
diff --git a/crates/test-programs/src/bin/p2_fd_filestat_set.rs b/crates/test-programs/src/bin/p2_fd_filestat_set.rs
new file mode 100644
index 0000000000..051ff5c700
--- /dev/null
+++ b/crates/test-programs/src/bin/p2_fd_filestat_set.rs
@@ -0,0 +1,173 @@
+use std::time::Duration;
+use wasip2::clocks::wall_clock::Datetime;
+use wasip2::filesystem::preopens::get_directories;
+use wasip2::filesystem::types::{
+ Descriptor, DescriptorFlags, ErrorCode, NewTimestamp, OpenFlags, PathFlags,
+};
+
+fn to_duration(t: Option<Datetime>) -> Duration {
+ let t = t.expect("timestamp should be present");
+ Duration::new(t.seconds, t.nanoseconds)
+}
+
+fn to_timestamp(d: Duration) -> NewTimestamp {
+ NewTimestamp::Timestamp(Datetime {
+ seconds: d.as_secs(),
+ nanoseconds: d.subsec_nanos(),
+ })
+}
+
+fn create_file(dir: &Descriptor, path: &str) {
+ let file = dir
+ .open_at(
+ PathFlags::empty(),
+ path,
+ OpenFlags::CREATE,
+ DescriptorFlags::empty(),
+ )
+ .expect("failed to create file");
+ drop(file);
+}
+
+fn open_file(dir: &Descriptor, path: &str, flags: DescriptorFlags) -> Descriptor {
+ dir.open_at(PathFlags::empty(), path, OpenFlags::empty(), flags)
+ .expect("failed to open file")
+}
+
+fn test_fd_filestat_set_size_rw(dir: &Descriptor) {
+ // Create a file in the scratch directory, opened read/write
+ let file = dir
+ .open_at(
+ PathFlags::empty(),
+ "file",
+ OpenFlags::CREATE,
+ DescriptorFlags::READ | DescriptorFlags::WRITE,
+ )
+ .expect("failed to create file");
+
+ // Check file size
+ let stat = file.stat().expect("failed stat");
+ assert_eq!(stat.size, 0, "file size should be 0");
+
+ // Check set_size
+ file.set_size(100).expect("set_size");
+
+ let stat = file.stat().expect("failed stat 2");
+ assert_eq!(stat.size, 100, "file size should be 100");
+
+ drop(file);
+ dir.unlink_file_at("file").expect("failed to remove file");
+}
+
+fn test_fd_filestat_set_size_ro(dir: &Descriptor) {
+ // Create a file in the scratch directory. Creating a file implies opening it for writing, so
+ // we have to close and re-open read-only to observe read-only behavior.
+ create_file(dir, "file");
+
+ // Open the created file read-only
+ let file = open_file(dir, "file", DescriptorFlags::READ);
+
+ // Check file size
+ let stat = file.stat().expect("failed stat");
+ assert_eq!(stat.size, 0, "file size should be 0");
+
+ // Check set_size on a file opened read-only fails with `invalid`, like
+ // ftruncate is defined to do on posix (or `access` on Windows).
+ let err = file
+ .set_size(100)
+ .expect_err("set_size should error when file is opened read-only");
+ assert!(
+ matches!(err, ErrorCode::Invalid | ErrorCode::Access),
+ "unexpected error {err:?}"
+ );
+
+ let stat = file.stat().expect("failed stat 2");
+ assert_eq!(stat.size, 0, "file size should remain 0");
+
+ drop(file);
+ dir.unlink_file_at("file").expect("failed to remove file");
+}
+
+fn test_fd_filestat_set_times(dir: &Descriptor, flags: DescriptorFlags) {
+ // Create a file in the scratch directory. CREATE implies opening for writing, so we will
+ // close it and re-open with the desired flags (READ for read only, READ | WRITE for
+ // readwrite)
+ create_file(dir, "file");
+
+ // Open the file with the flags given.
+ let file = open_file(dir, "file", flags);
+
+ let stat = file.stat().expect("failed stat 2");
+
+ // Check set_times
+ let old_atim = to_duration(stat.data_access_timestamp);
+ let new_mtim = to_duration(stat.data_modification_timestamp) - Duration::from_secs(1);
+ let result = file.set_times(NewTimestamp::NoChange, to_timestamp(new_mtim));
+ if !flags.contains(DescriptorFlags::WRITE) && result.is_err() {
+ // Windows rejects set-times on read-only files, so skip the rest of
+ // this test in that case.
+ drop(file);
+ dir.unlink_file_at("file").expect("failed to remove file");
+ return;
+ }
+ result.expect("set_times");
+
+ let stat = file.stat().expect("failed stat 3");
+ assert_eq!(stat.size, 0, "file size should remain unchanged at 0");
+
+ assert_eq!(
+ to_duration(stat.data_modification_timestamp),
+ new_mtim,
+ "mtim should change"
+ );
+ assert_eq!(
+ to_duration(stat.data_access_timestamp),
+ old_atim,
+ "atim should not change"
+ );
+
+ drop(file);
+ dir.unlink_file_at("file").expect("failed to remove file");
+}
+
+fn test_fd_filestat_set_times_on_directory(dir: &Descriptor) {
+ // Setting times on the directory descriptor itself has to work too,
+ // because preopened directories are held open with `O_PATH`.
+ let stat = dir.stat().expect("failed stat on dir");
+ let new_mtim = to_duration(stat.data_modification_timestamp) + Duration::from_secs(100);
+
+ // Windows rejects set-times on a directory handle that was opened
+ // read-only, the way it does for read-only files, so skip the test in that
+ // case.
+ if dir
+ .set_times(NewTimestamp::NoChange, to_timestamp(new_mtim))
+ .is_err()
+ {
+ return;
+ }
+
+ let stat = dir.stat().expect("failed stat on dir");
+ assert_eq!(
+ to_duration(stat.data_modification_timestamp),
+ new_mtim,
+ "dir mtim should change"
+ );
+}
+
+fn main() {
+ let preopens = get_directories();
+ let (dir, _name) = &preopens[0];
+
+ // Run the tests.
+ test_fd_filestat_set_size_rw(dir);
+ test_fd_filestat_set_size_ro(dir);
+
+ // The set_times function should behave the same whether the file
+ // descriptor is open for read-only or read-write, because the underlying
+ // permissions of the file determine whether or not the filestat can be
+ // set or not, not than the open mode.
+ test_fd_filestat_set_times(dir, DescriptorFlags::READ);
+ test_fd_filestat_set_times(dir, DescriptorFlags::READ | DescriptorFlags::WRITE);
+
+ test_fd_filestat_set_times_on_directory(dir);
+}
diff --git a/crates/test-programs/src/bin/p2_interesting_paths.rs b/crates/test-programs/src/bin/p2_interesting_paths.rs
new file mode 100644
index 0000000000..668cdacf35
--- /dev/null
+++ b/crates/test-programs/src/bin/p2_interesting_paths.rs
@@ -0,0 +1,101 @@
+use wasip2::filesystem::preopens::get_directories;
+use wasip2::filesystem::types::{Descriptor, DescriptorFlags, ErrorCode, OpenFlags, PathFlags};
+
+fn create_file(dir: &Descriptor, path: &str) {
+ let file = dir
+ .open_at(
+ PathFlags::empty(),
+ path,
+ OpenFlags::CREATE,
+ DescriptorFlags::empty(),
+ )
+ .expect("creating a file");
+ drop(file);
+}
+
+fn open(dir: &Descriptor, path: &str) -> Result<Descriptor, ErrorCode> {
+ dir.open_at(
+ PathFlags::empty(),
+ path,
+ OpenFlags::empty(),
+ DescriptorFlags::empty(),
+ )
+}
+
+fn test_interesting_paths(dir: &Descriptor, arg: &str) {
+ // Create a directory in the scratch directory.
+ dir.create_directory_at("dir").expect("creating dir");
+
+ // Create a directory in the directory we just created.
+ dir.create_directory_at("dir/nested")
+ .expect("creating a nested dir");
+
+ // Create a file in the nested directory.
+ create_file(dir, "dir/nested/file");
+
+ // Now open it with an absolute path.
+ let err = open(dir, "/dir/nested/file").expect_err("opening a file with an absolute path");
+ assert!(
+ matches!(err, ErrorCode::NotPermitted),
+ "unexpected error {err:?}"
+ );
+
+ // Now open it with a path containing "..".
+ let file = open(dir, "dir/.//nested/../../dir/nested/../nested///./file")
+ .expect("opening a file with \"..\" in the path");
+ drop(file);
+
+ // Now open it with a trailing NUL. Windows will allow this.
+ let err = open(dir, "dir/nested/file\0").expect_err("opening a file with a trailing NUL");
+ assert!(
+ matches!(err, ErrorCode::Invalid | ErrorCode::NoEntry),
+ "unexpected error {err:?}"
+ );
+
+ // Now open it with a trailing slash.
+ let err = open(dir, "dir/nested/file/")
+ .expect_err("opening a file with a trailing slash should fail");
+ assert!(
+ matches!(err, ErrorCode::NotDirectory | ErrorCode::NoEntry),
+ "unexpected error {err:?}"
+ );
+
+ // Now open it with trailing slashes.
+ let err = open(dir, "dir/nested/file///")
+ .expect_err("opening a file with trailing slashes should fail");
+ assert!(
+ matches!(err, ErrorCode::NotDirectory | ErrorCode::NoEntry),
+ "unexpected error {err:?}"
+ );
+
+ // Now open the directory with a trailing slash.
+ let file = open(dir, "dir/nested/").expect("opening a directory with a trailing slash");
+ drop(file);
+
+ // Now open the directory with trailing slashes.
+ let file = open(dir, "dir/nested///").expect("opening a directory with trailing slashes");
+ drop(file);
+
+ // Now open it with a path containing too many ".."s.
+ let bad_path = format!("dir/nested/../../../{arg}/dir/nested/file");
+ let err = open(dir, &bad_path)
+ .expect_err("opening a file with too many \"..\"s in the path should fail");
+ assert!(
+ matches!(err, ErrorCode::NotPermitted),
+ "unexpected error {err:?}"
+ );
+ dir.unlink_file_at("dir/nested/file")
+ .expect("unlink_file on a symlink should succeed");
+ dir.remove_directory_at("dir/nested")
+ .expect("remove_directory on a directory should succeed");
+ dir.remove_directory_at("dir")
+ .expect("remove_directory on a directory should succeed");
+}
+
+fn main() {
+ let preopens = get_directories();
+ let (dir, name) = &preopens[0];
+
+ // Run the tests.
+ test_interesting_paths(dir, name)
+}
diff --git a/crates/test-programs/src/bin/p2_nofollow_errors.rs b/crates/test-programs/src/bin/p2_nofollow_errors.rs
new file mode 100644
index 0000000000..bcc0eed4b9
--- /dev/null
+++ b/crates/test-programs/src/bin/p2_nofollow_errors.rs
@@ -0,0 +1,118 @@
+use wasip2::filesystem::preopens::get_directories;
+use wasip2::filesystem::types::{Descriptor, DescriptorFlags, ErrorCode, OpenFlags, PathFlags};
+
+fn test_nofollow_errors(dir: &Descriptor) {
+ // Create a directory for the symlink to point to.
+ dir.create_directory_at("target").expect("creating a dir");
+
+ // Create a symlink.
+ dir.symlink_at("target", "symlink")
+ .expect("creating a symlink");
+
+ // Try to open it as a directory with O_NOFOLLOW again.
+ let err = dir
+ .open_at(
+ PathFlags::empty(),
+ "symlink",
+ OpenFlags::DIRECTORY,
+ DescriptorFlags::empty(),
+ )
+ .expect_err("opening a directory symlink as a directory should fail");
+ assert!(
+ matches!(err, ErrorCode::Loop | ErrorCode::NotDirectory),
+ "unexpected error {err:?}"
+ );
+
+ // Try to open it with just O_NOFOLLOW.
+ let err = dir
+ .open_at(
+ PathFlags::empty(),
+ "symlink",
+ OpenFlags::empty(),
+ DescriptorFlags::empty(),
+ )
+ .expect_err("opening a symlink with O_NOFOLLOW should fail");
+ assert!(
+ matches!(err, ErrorCode::Loop | ErrorCode::Access),
+ "unexpected error {err:?}"
+ );
+
+ // Try to open it as a directory without O_NOFOLLOW.
+ let file = dir
+ .open_at(
+ PathFlags::SYMLINK_FOLLOW,
+ "symlink",
+ OpenFlags::DIRECTORY,
+ DescriptorFlags::empty(),
+ )
+ .expect("opening a symlink as a directory");
+ drop(file);
+
+ // Replace the target directory with a file.
+ dir.unlink_file_at("symlink").expect("removing a file");
+ dir.remove_directory_at("target")
+ .expect("remove_directory on a directory should succeed");
+
+ let file = dir
+ .open_at(
+ PathFlags::empty(),
+ "target",
+ OpenFlags::CREATE,
+ DescriptorFlags::empty(),
+ )
+ .expect("creating a file");
+ drop(file);
+ dir.symlink_at("target", "symlink")
+ .expect("creating a symlink");
+
+ // Try to open it as a directory with O_NOFOLLOW again.
+ let err = dir
+ .open_at(
+ PathFlags::empty(),
+ "symlink",
+ OpenFlags::DIRECTORY,
+ DescriptorFlags::empty(),
+ )
+ .expect_err("opening a directory symlink as a directory should fail");
+ assert!(
+ matches!(err, ErrorCode::Loop | ErrorCode::NotDirectory),
+ "unexpected error {err:?}"
+ );
+
+ // Try to open it with just O_NOFOLLOW.
+ let err = dir
+ .open_at(
+ PathFlags::empty(),
+ "symlink",
+ OpenFlags::empty(),
+ DescriptorFlags::empty(),
+ )
+ .expect_err("opening a symlink with NOFOLLOW should fail");
+ assert!(matches!(err, ErrorCode::Loop), "unexpected error {err:?}");
+
+ // Try to open it as a directory without O_NOFOLLOW.
+ let err = dir
+ .open_at(
+ PathFlags::SYMLINK_FOLLOW,
+ "symlink",
+ OpenFlags::DIRECTORY,
+ DescriptorFlags::empty(),
+ )
+ .expect_err("opening a symlink to a file as a directory");
+ assert!(
+ matches!(err, ErrorCode::NotDirectory),
+ "unexpected error {err:?}"
+ );
+
+ // Clean up.
+ dir.unlink_file_at("target").expect("removing a file");
+ dir.unlink_file_at("symlink").expect("removing a file");
+}
+
+fn main() {
+ let preopens = get_directories();
+ let (dir, _name) = &preopens[0];
+
+ // Run the tests.
+ test_nofollow_errors(dir)
+}
diff --git a/crates/test-programs/src/bin/p2_path_exists.rs b/crates/test-programs/src/bin/p2_path_exists.rs
new file mode 100644
index 0000000000..c24c9da5ac
--- /dev/null
+++ b/crates/test-programs/src/bin/p2_path_exists.rs
@@ -0,0 +1,89 @@
+use wasip2::filesystem::preopens::get_directories;
+use wasip2::filesystem::types::{
+ Descriptor, DescriptorFlags, DescriptorType, OpenFlags, PathFlags,
+};
+
+fn create_file(dir: &Descriptor, path: &str) {
+ let file = dir
+ .open_at(
+ PathFlags::empty(),
+ path,
+ OpenFlags::CREATE,
+ DescriptorFlags::empty(),
+ )
+ .expect("creating a file");
+ drop(file);
+}
+
+fn test_path_exists(dir: &Descriptor) {
+ // Create a temporary directory
+ dir.create_directory_at("subdir").expect("create directory");
+
+ // Check directory exists:
+ let file_stat = dir
+ .stat_at(PathFlags::empty(), "subdir")
+ .expect("reading file stats");
+ assert_eq!(file_stat.type_, DescriptorType::Directory);
+
+ // Should still exist with symlink follow flag:
+ let file_stat = dir
+ .stat_at(PathFlags::SYMLINK_FOLLOW, "subdir")
+ .expect("reading file stats");
+ assert_eq!(file_stat.type_, DescriptorType::Directory);
+
+ // Create a file:
+ create_file(dir, "subdir/file");
+ // Check directory exists:
+ let file_stat = dir
+ .stat_at(PathFlags::empty(), "subdir/file")
+ .expect("reading file stats");
+ assert_eq!(file_stat.type_, DescriptorType::RegularFile);
+
+ // Should still exist with symlink follow flag:
+ let file_stat = dir
+ .stat_at(PathFlags::SYMLINK_FOLLOW, "subdir/file")
+ .expect("reading file stats");
+ assert_eq!(file_stat.type_, DescriptorType::RegularFile);
+
+ // Create a symlink to a file:
+ dir.symlink_at("subdir/file", "link1")
+ .expect("create symlink");
+ // Check symlink exists:
+ let file_stat = dir
+ .stat_at(PathFlags::empty(), "link1")
+ .expect("reading file stats");
+ assert_eq!(file_stat.type_, DescriptorType::SymbolicLink);
+
+ // Should still exist with symlink follow flag, pointing to regular file
+ let file_stat = dir
+ .stat_at(PathFlags::SYMLINK_FOLLOW, "link1")
+ .expect("reading file stats");
+ assert_eq!(file_stat.type_, DescriptorType::RegularFile);
+
+ // Create a symlink to a dir:
+ dir.symlink_at("subdir", "link2").expect("create symlink");
+ // Check symlink exists:
+ let file_stat = dir
+ .stat_at(PathFlags::empty(), "link2")
+ .expect("reading file stats");
+ assert_eq!(file_stat.type_, DescriptorType::SymbolicLink);
+
+ // Should still exist with symlink follow flag, pointing to directory
+ let file_stat = dir
+ .stat_at(PathFlags::SYMLINK_FOLLOW, "link2")
+ .expect("reading file stats");
+ assert_eq!(file_stat.type_, DescriptorType::Directory);
+
+ dir.unlink_file_at("link1").expect("clean up");
+ dir.unlink_file_at("link2").expect("clean up");
+ dir.unlink_file_at("subdir/file").expect("clean up");
+ dir.remove_directory_at("subdir").expect("clean up");
+}
+
+fn main() {
+ let preopens = get_directories();
+ let (dir, _name) = &preopens[0];
+
+ // Run the tests.
+ test_path_exists(dir)
+}
diff --git a/crates/test-programs/src/bin/p2_path_filestat.rs b/crates/test-programs/src/bin/p2_path_filestat.rs
new file mode 100644
index 0000000000..7f209c3352
--- /dev/null
+++ b/crates/test-programs/src/bin/p2_path_filestat.rs
@@ -0,0 +1,88 @@
+use std::time::Duration;
+use wasip2::clocks::wall_clock::Datetime;
+use wasip2::filesystem::preopens::get_directories;
+use wasip2::filesystem::types::{
+ Descriptor, DescriptorFlags, ErrorCode, NewTimestamp, OpenFlags, PathFlags,
+};
+
+fn to_duration(t: Option<Datetime>) -> Duration {
+ let t = t.expect("timestamp should be present");
+ Duration::new(t.seconds, t.nanoseconds)
+}
+
+fn to_timestamp(d: Duration) -> NewTimestamp {
+ NewTimestamp::Timestamp(Datetime {
+ seconds: d.as_secs(),
+ nanoseconds: d.subsec_nanos(),
+ })
+}
+
+fn test_path_filestat(dir: &Descriptor) {
+ let flags = DescriptorFlags::READ | DescriptorFlags::WRITE;
+
+ // Create a file in the scratch directory.
+ let file = dir
+ .open_at(
+ PathFlags::empty(),
+ "file",
+ OpenFlags::CREATE,
+ // Pass some flags for later retrieval
+ flags,
+ )
+ .expect("opening a file");
+
+ let file_flags = file.get_flags().expect("get_flags");
+ assert!(
+ file_flags.contains(flags),
+ "file should have the flags used to create the file"
+ );
+ let err = dir
+ .open_at(
+ PathFlags::empty(),
+ "file",
+ OpenFlags::empty(),
+ flags | DescriptorFlags::FILE_INTEGRITY_SYNC,
+ )
+ .expect_err("FILE_INTEGRITY_SYNC not supported by platform");
+ assert!(
+ matches!(err, ErrorCode::Unsupported),
+ "unexpected error {err:?}"
+ );
+
+ // Check file size
+ let file_stat = dir
+ .stat_at(PathFlags::empty(), "file")
+ .expect("reading file stats");
+ assert_eq!(file_stat.size, 0, "file size should be 0");
+
+ // Check set_times_at
+ let new_mtim = to_duration(file_stat.data_modification_timestamp) - Duration::from_secs(1);
+ dir.set_times_at(
+ PathFlags::empty(),
+ "file",
+ NewTimestamp::NoChange,
+ to_timestamp(new_mtim),
+ )
+ .expect("set_times_at should succeed");
+
+ let modified_file_stat = dir
+ .stat_at(PathFlags::empty(), "file")
+ .expect("reading file stats after set_times_at");
+
+ assert_eq!(
+ to_duration(modified_file_stat.data_modification_timestamp),
+ new_mtim,
+ "mtim should change"
+ );
+
+ drop(file);
+ dir.unlink_file_at("file").expect("removing a file");
+}
+
+fn main() {
+ let preopens = get_directories();
+ let (dir, _name) = &preopens[0];
+
+ // Run the tests.
+ test_path_filestat(dir)
+}
diff --git a/crates/test-programs/src/bin/p2_path_link.rs b/crates/test-programs/src/bin/p2_path_link.rs
new file mode 100644
index 0000000000..f41d84590a
--- /dev/null
+++ b/crates/test-programs/src/bin/p2_path_link.rs
@@ -0,0 +1,196 @@
+use wasip2::clocks::wall_clock::Datetime;
+use wasip2::filesystem::preopens::get_directories;
+use wasip2::filesystem::types::{Descriptor, DescriptorFlags, ErrorCode, OpenFlags, PathFlags};
+
+fn time(t: Option<Datetime>) -> Option<(u64, u32)> {
+ t.map(|t| (t.seconds, t.nanoseconds))
+}
+
+fn create_file(dir: &Descriptor, path: &str) {
+ let file = dir
+ .open_at(
+ PathFlags::empty(),
+ path,
+ OpenFlags::CREATE,
+ DescriptorFlags::empty(),
+ )
+ .expect("creating a file");
+ drop(file);
+}
+
+fn open(dir: &Descriptor, path: &str, oflags: OpenFlags) -> Descriptor {
+ dir.open_at(PathFlags::empty(), path, oflags, DescriptorFlags::empty())
+ .expect("opening a path")
+}
+
+fn link(dir: &Descriptor, old: &str, new_dir: &Descriptor, new: &str) -> Result<(), ErrorCode> {
+ dir.link_at(PathFlags::empty(), old, new_dir, new)
+}
+
+// This is a macro so the panic line number shows us where things went wrong.
+macro_rules! check_rights {
+ ($orig:ident, $link:ident) => {
+ let orig_stat = $orig.stat().expect("reading stat of the source");
+ let link_stat = $link.stat().expect("reading stat of the link");
+ assert_eq!(orig_stat.type_, link_stat.type_, "type should be equal");
+ assert_eq!(
+ orig_stat.link_count, link_stat.link_count,
+ "link_count should be equal"
+ );
+ assert_eq!(orig_stat.size, link_stat.size, "size should be equal");
+ assert_eq!(
+ time(orig_stat.data_access_timestamp),
+ time(link_stat.data_access_timestamp),
+ "atim should be equal"
+ );
+ assert_eq!(
+ time(orig_stat.data_modification_timestamp),
+ time(link_stat.data_modification_timestamp),
+ "mtim should be equal"
+ );
+ assert_eq!(
+ time(orig_stat.status_change_timestamp),
+ time(link_stat.status_change_timestamp),
+ "ctim should be equal"
+ );
+ assert!($orig.is_same_object(&$link), "should be the same object");
+
+ let orig_flags = $orig.get_flags().expect("reading flags of the source");
+ let link_flags = $link.get_flags().expect("reading flags of the link");
+ assert_eq!(orig_flags, link_flags, "flags should be equal");
+ let orig_type = $orig.get_type().expect("reading type of the source");
+ let link_type = $link.get_type().expect("reading type of the link");
+ assert_eq!(orig_type, link_type, "type should be equal");
+ };
+}
+
+// Extra drops are needed for Windows, which will not remove
+// the directory until all handles are closed.
+fn test_path_link(dir: &Descriptor) {
+ // Create a file
+ create_file(dir, "file");
+
+ // Open a fresh descriptor to the file. We won't have write access that was implied by
+ // CREATE above.
+ let file = open(dir, "file", OpenFlags::empty());
+
+ // Create a link in the same directory and compare rights
+ link(dir, "file", dir, "link").expect("creating a link in the same directory");
+
+ let link_fd = open(dir, "link", OpenFlags::empty());
+
+ check_rights!(file, link_fd);
+ drop(link_fd); // needed for Windows
+ dir.unlink_file_at("link").expect("removing a link");
+
+ // Create a link in a different directory and compare rights
+ dir.create_directory_at("subdir")
+ .expect("creating a subdirectory");
+ let subdir = open(dir, "subdir", OpenFlags::DIRECTORY);
+ link(dir, "file", &subdir, "link").expect("creating a link in subdirectory");
+ let link_fd = open(&subdir, "link", OpenFlags::empty());
+ check_rights!(file, link_fd);
+ drop(link_fd); // needed for Windows
+ subdir.unlink_file_at("link").expect("removing a link");
+ drop(subdir); // needed for Windows
+ dir.remove_directory_at("subdir")
+ .expect("removing a subdirectory");
+
+ // Create a link to a path that already exists
+ create_file(dir, "link");
+
+ let err =
+ link(dir, "file", dir, "link").expect_err("creating a link to existing path should fail");
+ assert!(matches!(err, ErrorCode::Exist), "unexpected error {err:?}");
+ dir.unlink_file_at("link").expect("removing a file");
+
+ // Create a link to itself
+ let err = link(dir, "file", dir, "file").expect_err("creating a link to itself should fail");
+ assert!(matches!(err, ErrorCode::Exist), "unexpected error {err:?}");
+
+ // Create a link where target is a directory
+ dir.create_directory_at("link").expect("creating a dir");
+
+ let err = link(dir, "file", dir, "link")
+ .expect_err("creating a link where target is a directory should fail");
+ assert!(matches!(err, ErrorCode::Exist), "unexpected error {err:?}");
+ dir.remove_directory_at("link").expect("removing a dir");
+
+ // Create a link to a directory
+ dir.create_directory_at("subdir")
+ .expect("creating a subdirectory");
+ let subdir = open(dir, "subdir", OpenFlags::DIRECTORY);
+
+ let err =
+ link(dir, "subdir", dir, "link").expect_err("creating a link to a directory should fail");
+ assert!(
+ matches!(err, ErrorCode::NotPermitted | ErrorCode::Access),
+ "unexpected error {err:?}"
+ );
+ drop(subdir); // close subdir before deleting it
+ dir.remove_directory_at("subdir")
+ .expect("removing a subdirectory");
+
+ // Create a link to a file with trailing slash
+ let err = link(dir, "file", dir, "link/")
+ .expect_err("creating a link to a file with trailing slash should fail");
+ assert!(
+ matches!(err, ErrorCode::NoEntry),
+ "unexpected error {err:?}"
+ );
+
+ // Not all platforms (e.g. Windows) support dangling symlinks, so skip the
+ // rest of the test if creation fails.
+ if dir.symlink_at("target", "symlink").is_ok() {
+ // Create a link to a dangling symlink. This should succeed, because
+ // we're not following symlinks
+ link(dir, "symlink", dir, "link")
+ .expect("creating a link to a dangling symlink should succeed");
+ dir.unlink_file_at("symlink").expect("removing a symlink");
+ dir.unlink_file_at("link").expect("removing a hardlink");
+
+ // Create a link to a symlink loop
+ dir.symlink_at("symlink", "symlink")
+ .expect("creating a symlink loop");
+
+ link(dir, "symlink", dir, "link")
+ .expect("creating a link to a symlink loop should succeed");
+ dir.unlink_file_at("symlink").expect("removing a symlink");
+ dir.unlink_file_at("link").expect("removing a hardlink");
+
+ // Create a link where target is a dangling symlink
+ dir.symlink_at("target", "symlink")
+ .expect("creating a dangling symlink");
+
+ let err = link(dir, "file", dir, "symlink")
+ .expect_err("creating a link where target is a dangling symlink");
+ assert!(matches!(err, ErrorCode::Exist), "unexpected error {err:?}");
+ dir.unlink_file_at("symlink").expect("removing a symlink");
+
+ // Create a link where target is a dangling symlink following symlinks
+ dir.symlink_at("target", "symlink")
+ .expect("creating a dangling symlink");
+
+ // Symlink following with link_at is rejected
+ let err = dir
+ .link_at(PathFlags::SYMLINK_FOLLOW, "symlink", dir, "link")
+ .expect_err("calling link_at with SYMLINK_FOLLOW should fail");
+ assert!(
+ matches!(err, ErrorCode::Invalid),
+ "unexpected error {err:?}"
+ );
+ dir.unlink_file_at("symlink").expect("removing a symlink");
+ }
+
+ // Clean up.
+ drop(file);
+ dir.unlink_file_at("file").expect("removing a file");
+}
+
+fn main() {
+ let preopens = get_directories();
+ let (dir, _name) = &preopens[0];
+
+ // Run the tests.
+ test_path_link(dir)
+}
diff --git a/crates/test-programs/src/bin/p2_path_open_create_existing.rs b/crates/test-programs/src/bin/p2_path_open_create_existing.rs
new file mode 100644
index 0000000000..dabcdd63bb
--- /dev/null
+++ b/crates/test-programs/src/bin/p2_path_open_create_existing.rs
@@ -0,0 +1,36 @@
+use wasip2::filesystem::preopens::get_directories;
+use wasip2::filesystem::types::{Descriptor, DescriptorFlags, ErrorCode, OpenFlags, PathFlags};
+
+fn create_file(dir: &Descriptor, path: &str) {
+ let file = dir
+ .open_at(
+ PathFlags::empty(),
+ path,
+ OpenFlags::CREATE,
+ DescriptorFlags::empty(),
+ )
+ .expect("creating a file");
+ drop(file);
+}
+
+fn test_path_open_create_existing(dir: &Descriptor) {
+ create_file(dir, "file");
+ let err = dir
+ .open_at(
+ PathFlags::empty(),
+ "file",
+ OpenFlags::CREATE | OpenFlags::EXCLUSIVE,
+ DescriptorFlags::empty(),
+ )
+ .expect_err("trying to create a file that already exists");
+ assert!(matches!(err, ErrorCode::Exist), "unexpected error {err:?}");
+ dir.unlink_file_at("file").expect("removing a file");
+}
+
+fn main() {
+ let preopens = get_directories();
+ let (dir, _name) = &preopens[0];
+
+ // Run the tests.
+ test_path_open_create_existing(dir)
+}
diff --git a/crates/test-programs/src/bin/p2_path_open_dirfd_not_dir.rs b/crates/test-programs/src/bin/p2_path_open_dirfd_not_dir.rs
new file mode 100644
index 0000000000..da33600aed
--- /dev/null
+++ b/crates/test-programs/src/bin/p2_path_open_dirfd_not_dir.rs
@@ -0,0 +1,37 @@
+use wasip2::filesystem::preopens::get_directories;
+use wasip2::filesystem::types::{Descriptor, DescriptorFlags, ErrorCode, OpenFlags, PathFlags};
+
+fn test_dirfd_not_dir(dir: &Descriptor) {
+ // Open a file.
+ let file = dir
+ .open_at(
+ PathFlags::empty(),
+ "file",
+ OpenFlags::CREATE,
+ DescriptorFlags::empty(),
+ )
+ .expect("opening a file");
+ // Now try to open a file underneath it as if it were a directory.
+ let err = file
+ .open_at(
+ PathFlags::empty(),
+ "foo",
+ OpenFlags::CREATE,
+ DescriptorFlags::empty(),
+ )
+ .expect_err("non-directory base descriptor should get not-directory");
+ assert!(
+ matches!(err, ErrorCode::NotDirectory),
+ "unexpected error {err:?}"
+ );
+ drop(file);
+ dir.unlink_file_at("file").expect("removing a file");
+}
+
+fn main() {
+ let preopens = get_directories();
+ let (dir, _name) = &preopens[0];
+
+ // Run the tests.
+ test_dirfd_not_dir(dir)
+}
diff --git a/crates/test-programs/src/bin/p2_path_open_missing.rs b/crates/test-programs/src/bin/p2_path_open_missing.rs
new file mode 100644
index 0000000000..c76b2256b6
--- /dev/null
+++ b/crates/test-programs/src/bin/p2_path_open_missing.rs
@@ -0,0 +1,25 @@
+use wasip2::filesystem::preopens::get_directories;
+use wasip2::filesystem::types::{Descriptor, DescriptorFlags, ErrorCode, OpenFlags, PathFlags};
+
+fn test_path_open_missing(dir: &Descriptor) {
+ let err = dir
+ .open_at(
+ PathFlags::empty(),
+ "file",
+ OpenFlags::empty(), // not passing CREATE here
+ DescriptorFlags::empty(),
+ )
+ .expect_err("trying to open a file that doesn't exist");
+ assert!(
+ matches!(err, ErrorCode::NoEntry),
+ "unexpected error {err:?}"
+ );
+}
+
+fn main() {
+ let preopens = get_directories();
+ let (dir, _name) = &preopens[0];
+
+ // Run the tests.
+ test_path_open_missing(dir)
+}
diff --git a/crates/test-programs/src/bin/p2_path_open_preopen.rs b/crates/test-programs/src/bin/p2_path_open_preopen.rs
new file mode 100644
index 0000000000..4d5625ffb7
--- /dev/null
+++ b/crates/test-programs/src/bin/p2_path_open_preopen.rs
@@ -0,0 +1,80 @@
+use wasip2::filesystem::preopens::get_directories;
+use wasip2::filesystem::types::{DescriptorFlags, DescriptorType, ErrorCode, OpenFlags, PathFlags};
+
+fn path_open_preopen() {
+ let preopens = get_directories();
+ let (preopen, _name) = &preopens[0];
+
+ assert_eq!(
+ preopen.get_type().expect("get type"),
+ DescriptorType::Directory,
+ "preopen is a directory"
+ );
+
+ // Hard-code the set of flags expected for a preopened directory. Various
+ // userland implementations expect (at least) this set of flags to be
+ // present on all directories:
+ let flags = preopen.get_flags().expect("get flags");
+ for (flag, name) in [
+ (DescriptorFlags::READ, "READ"),
+ (DescriptorFlags::MUTATE_DIRECTORY, "MUTATE_DIRECTORY"),
+ ] {
+ assert!(
+ flags.contains(flag),
+ "flags do not have required flag `{name}`"
+ );
+ }
+
+ // Open with same flags it has now:
+ let _ = preopen
+ .open_at(PathFlags::empty(), ".", OpenFlags::empty(), flags)
+ .expect("open with same flags");
+
+ // Open with an empty set of flags:
+ let _ = preopen
+ .open_at(
+ PathFlags::empty(),
+ ".",
+ OpenFlags::empty(),
+ DescriptorFlags::empty(),
+ )
+ .expect("open with empty flags");
+
+ // Open DIRECTORY with an empty set of flags:
+ let _ = preopen
+ .open_at(
+ PathFlags::empty(),
+ ".",
+ OpenFlags::DIRECTORY,
+ DescriptorFlags::empty(),
+ )
+ .expect("open with DIRECTORY empty flags");
+
+ // Open DIRECTORY with just the read flag:
+ let _ = preopen
+ .open_at(
+ PathFlags::empty(),
+ ".",
+ OpenFlags::DIRECTORY,
+ DescriptorFlags::READ,
+ )
+ .expect("open with DIRECTORY and read flag");
+
+ // Open DIRECTORY and read/write flags should fail with is-directory:
+ let err = preopen
+ .open_at(
+ PathFlags::empty(),
+ ".",
+ OpenFlags::DIRECTORY,
+ DescriptorFlags::READ | DescriptorFlags::WRITE,
+ )
+ .expect_err("open with DIRECTORY and read/write should fail");
+ assert!(
+ matches!(err, ErrorCode::IsDirectory),
+ "opening directory read/write should fail with is-directory, got {err:?}"
+ );
+}
+
+fn main() {
+ path_open_preopen();
+}
diff --git a/crates/test-programs/src/bin/p2_path_open_read_write.rs b/crates/test-programs/src/bin/p2_path_open_read_write.rs
new file mode 100644
index 0000000000..ef8021eeb9
--- /dev/null
+++ b/crates/test-programs/src/bin/p2_path_open_read_write.rs
@@ -0,0 +1,135 @@
+use wasip2::filesystem::preopens::get_directories;
+use wasip2::filesystem::types::{Descriptor, DescriptorFlags, OpenFlags, PathFlags};
+use wasip2::io::streams::StreamError;
+
+fn create_file(dir: &Descriptor, path: &str) {
+ let file = dir
+ .open_at(
+ PathFlags::empty(),
+ path,
+ OpenFlags::CREATE,
+ DescriptorFlags::empty(),
+ )
+ .expect("creating a file");
+ drop(file);
+}
+
+fn open(dir: &Descriptor, path: &str, flags: DescriptorFlags) -> Descriptor {
+ dir.open_at(PathFlags::empty(), path, OpenFlags::empty(), flags)
+ .expect("opening a file")
+}
+
+/// Reads the entire contents of `file` starting at `offset`.
+fn read(file: &Descriptor, offset: u64) -> Result<Vec<u8>, StreamError> {
+ let stream = file.read_via_stream(offset).expect("read_via_stream");
+ let mut buf = Vec::new();
+ loop {
+ match stream.blocking_read(100) {
+ Ok(chunk) => buf.extend(chunk),
+ Err(StreamError::Closed) => return Ok(buf),
+ Err(e) => return Err(e),
+ }
+ }
+}
+
+/// Writes all of `bytes` to `file` at `offset`.
+fn write(file: &Descriptor, offset: u64, bytes: &[u8]) -> Result<(), StreamError> {
+ let stream = file.write_via_stream(offset).expect("write_via_stream");
+ stream.blocking_write_and_flush(bytes)
+}
+
+fn test_path_open_read_write(dir: &Descriptor) {
+ create_file(dir, "file");
+
+ let f_readonly = open(dir, "file", DescriptorFlags::READ);
+
+ let flags = f_readonly.get_flags().expect("get flags readonly");
+ assert!(
+ flags.contains(DescriptorFlags::READ),
+ "readonly has read flag"
+ );
+ assert!(
+ !flags.contains(DescriptorFlags::WRITE),
+ "readonly does not have write flag"
+ );
+
+ let contents = read(&f_readonly, 0).expect("reading readonly file");
+ assert_eq!(contents.len(), 0, "readonly file is empty");
+
+ let write_buffer = &[1u8; 50];
+ let err = write(&f_readonly, 0, write_buffer)
+ .err()
+ .expect("write of readonly fails");
+ assert!(
+ matches!(err, StreamError::LastOperationFailed(_)),
+ "unexpected error {err:?}"
+ );
+
+ drop(f_readonly);
+
+ // =============== WRITE ONLY ==================
+ let f_writeonly = open(dir, "file", DescriptorFlags::WRITE);
+
+ let flags = f_writeonly.get_flags().expect("get flags writeonly");
+ assert!(
+ !flags.contains(DescriptorFlags::READ),
+ "writeonly does not have read flag"
+ );
+ assert!(
+ flags.contains(DescriptorFlags::WRITE),
+ "writeonly has write flag"
+ );
+
+ let err = read(&f_writeonly, 0)
+ .err()
+ .expect("read of writeonly fails");
+ assert!(
+ matches!(err, StreamError::LastOperationFailed(_)),
+ "unexpected error {err:?}"
+ );
+ write(&f_writeonly, 0, write_buffer).expect("write to writeonly");
+
+ drop(f_writeonly);
+
+ // ============== READ WRITE =======================
+
+ let f_readwrite = open(dir, "file", DescriptorFlags::READ | DescriptorFlags::WRITE);
+ let flags = f_readwrite.get_flags().expect("get flags readwrite");
+ assert!(
+ flags.contains(DescriptorFlags::READ),
+ "readwrite has read flag"
+ );
+ assert!(
+ flags.contains(DescriptorFlags::WRITE),
+ "readwrite has write flag"
+ );
+
+ let contents = read(&f_readwrite, 0).expect("reading readwrite file");
+ assert_eq!(
+ contents.len(),
+ write_buffer.len(),
+ "readwrite file contains contents from writeonly open"
+ );
+
+ let write_buffer_2 = &[2u8; 25];
+ write(&f_readwrite, write_buffer.len() as u64, write_buffer_2).expect("write to readwrite");
+
+ let stat = f_readwrite.stat().expect("get stat readwrite");
+ assert_eq!(
+ stat.size as usize,
+ write_buffer.len() + write_buffer_2.len(),
+ "total written is both write buffers"
+ );
+
+ drop(f_readwrite);
+
+ dir.unlink_file_at("file").expect("removing a file");
+}
+
+fn main() {
+ let preopens = get_directories();
+ let (dir, _name) = &preopens[0];
+
+ // Run the tests.
+ test_path_open_read_write(dir)
+}
diff --git a/crates/test-programs/src/bin/p2_path_rename.rs b/crates/test-programs/src/bin/p2_path_rename.rs
new file mode 100644
index 0000000000..6bc8f62424
--- /dev/null
+++ b/crates/test-programs/src/bin/p2_path_rename.rs
@@ -0,0 +1,166 @@
+use wasip2::filesystem::preopens::get_directories;
+use wasip2::filesystem::types::{Descriptor, DescriptorFlags, ErrorCode, OpenFlags, PathFlags};
+
+fn create_file(dir: &Descriptor, path: &str) {
+ let file = dir
+ .open_at(
+ PathFlags::empty(),
+ path,
+ OpenFlags::CREATE,
+ DescriptorFlags::empty(),
+ )
+ .expect("creating a file");
+ drop(file);
+}
+
+fn open(dir: &Descriptor, path: &str, oflags: OpenFlags) -> Result<Descriptor, ErrorCode> {
+ dir.open_at(PathFlags::empty(), path, oflags, DescriptorFlags::empty())
+}
+
+fn rename(dir: &Descriptor, source: &str, target: &str) -> Result<(), ErrorCode> {
+ dir.rename_at(source, dir, target)
+}
+
+fn test_path_rename(dir: &Descriptor) {
+ // First, try renaming a dir to nonexistent path
+ // Create source directory
+ dir.create_directory_at("source")
+ .expect("creating a directory");
+
+ // Try renaming the directory
+ rename(dir, "source", "target").expect("renaming a directory");
+
+ // Check that source directory doesn't exist anymore
+ let err = open(dir, "source", OpenFlags::DIRECTORY)
+ .expect_err("opening a nonexistent path as a directory should fail");
+ assert!(
+ matches!(err, ErrorCode::NoEntry),
+ "unexpected error {err:?}"
+ );
+
+ // Check that target directory exists
+ let fd =
+ open(dir, "target", OpenFlags::DIRECTORY).expect("opening renamed path as a directory");
+
+ drop(fd);
+ dir.remove_directory_at("target")
+ .expect("removing a directory");
+
+ // Now, try renaming a dir to existing empty dir
+ dir.create_directory_at("source")
+ .expect("creating a directory");
+ dir.create_directory_at("target")
+ .expect("creating a directory");
+ rename(dir, "source", "target").expect("renaming a directory");
+
+ // Check that source directory doesn't exist anymore
+ let err = open(dir, "source", OpenFlags::DIRECTORY)
+ .expect_err("opening a nonexistent path as a directory");
+ assert!(
+ matches!(err, ErrorCode::NoEntry),
+ "unexpected error {err:?}"
+ );
+
+ // Check that target directory exists
+ let fd =
+ open(dir, "target", OpenFlags::DIRECTORY).expect("opening renamed path as a directory");
+
+ drop(fd);
+ dir.remove_directory_at("target")
+ .expect("removing a directory");
+
+ // Now, try renaming a dir to existing non-empty dir
+ dir.create_directory_at("source")
+ .expect("creating a directory");
+ dir.create_directory_at("target")
+ .expect("creating a directory");
+ create_file(dir, "target/file");
+
+ let err =
+ rename(dir, "source", "target").expect_err("renaming directory to a nonempty directory");
+ assert!(
+ matches!(err, ErrorCode::NotEmpty),
+ "unexpected error {err:?}"
+ );
+
+ // Try renaming dir to a file. Some platforms (e.g. Windows) allow this,
+ // so accept either outcome.
+ match rename(dir, "source", "target/file") {
+ Ok(()) => {
+ dir.remove_directory_at("target/file")
+ .expect("removing a directory");
+ }
+ Err(err) => {
+ assert!(
+ matches!(err, ErrorCode::NotDirectory),
+ "unexpected error {err:?}"
+ );
+ dir.unlink_file_at("target/file").expect("removing a file");
+ dir.remove_directory_at("source")
+ .expect("removing a directory");
+ }
+ }
+ dir.remove_directory_at("target")
+ .expect("removing a directory");
+
+ // Now, try renaming a file to a nonexistent path
+ create_file(dir, "source");
+ rename(dir, "source", "target").expect("renaming a file");
+
+ // Check that source file doesn't exist anymore
+ let err = open(dir, "source", OpenFlags::empty())
+ .expect_err("opening a nonexistent path should fail");
+ assert!(
+ matches!(err, ErrorCode::NoEntry),
+ "unexpected error {err:?}"
+ );
+
+ // Check that target file exists
+ let fd = open(dir, "target", OpenFlags::empty()).expect("opening renamed path");
+
+ drop(fd);
+ dir.unlink_file_at("target").expect("removing a file");
+
+ // Now, try renaming file to an existing file
+ create_file(dir, "source");
+ create_file(dir, "target");
+
+ rename(dir, "source", "target").expect("renaming file to another existing file");
+
+ // Check that source file doesn't exist anymore
+ let err = open(dir, "source", OpenFlags::empty()).expect_err("opening a nonexistent path");
+ assert!(
+ matches!(err, ErrorCode::NoEntry),
+ "unexpected error {err:?}"
+ );
+
+ // Check that target file exists
+ let fd = open(dir, "target", OpenFlags::empty()).expect("opening renamed path");
+
+ drop(fd);
+ dir.unlink_file_at("target").expect("removing a file");
+
+ // Try renaming to an (empty) directory instead
+ create_file(dir, "source");
+ dir.create_directory_at("target")
+ .expect("creating a directory");
+
+ let err = rename(dir, "source", "target")
+ .expect_err("renaming a file to existing directory should fail");
+ assert!(
+ matches!(err, ErrorCode::Access | ErrorCode::IsDirectory),
+ "unexpected error {err:?}"
+ );
+
+ dir.remove_directory_at("target")
+ .expect("removing a directory");
+ dir.unlink_file_at("source").expect("removing a file");
+}
+
+fn main() {
+ let preopens = get_directories();
+ let (dir, _name) = &preopens[0];
+
+ // Run the tests.
+ test_path_rename(dir)
+}
diff --git a/crates/test-programs/src/bin/p2_path_rename_dir_trailing_slashes.rs b/crates/test-programs/src/bin/p2_path_rename_dir_trailing_slashes.rs
new file mode 100644
index 0000000000..46e92609c5
--- /dev/null
+++ b/crates/test-programs/src/bin/p2_path_rename_dir_trailing_slashes.rs
@@ -0,0 +1,26 @@
+use wasip2::filesystem::preopens::get_directories;
+use wasip2::filesystem::types::Descriptor;
+
+fn test_path_rename_trailing_slashes(dir: &Descriptor) {
+ // Test renaming a directory with a trailing slash in the name.
+ dir.create_directory_at("source")
+ .expect("creating a directory");
+ dir.rename_at("source/", dir, "target")
+ .expect("renaming a directory with a trailing slash in the source name");
+ dir.rename_at("target", dir, "source/")
+ .expect("renaming a directory with a trailing slash in the destination name");
+ dir.rename_at("source/", dir, "target/")
+ .expect("renaming a directory with a trailing slash in the source and destination names");
+ dir.rename_at("target", dir, "source")
+ .expect("renaming a directory with no trailing slashes at all should work");
+ dir.remove_directory_at("source")
+ .expect("removing the directory");
+}
+
+fn main() {
+ let preopens = get_directories();
+ let (dir, _name) = &preopens[0];
+
+ // Run the tests.
+ test_path_rename_trailing_slashes(dir)
+}
diff --git a/crates/test-programs/src/bin/p2_path_symlink_trailing_slashes.rs b/crates/test-programs/src/bin/p2_path_symlink_trailing_slashes.rs
new file mode 100644
index 0000000000..dd8fa2a3cf
--- /dev/null
+++ b/crates/test-programs/src/bin/p2_path_symlink_trailing_slashes.rs
@@ -0,0 +1,90 @@
+use wasip2::filesystem::preopens::get_directories;
+use wasip2::filesystem::types::{Descriptor, DescriptorFlags, ErrorCode, OpenFlags, PathFlags};
+
+fn create_file(dir: &Descriptor, path: &str) {
+ let file = dir
+ .open_at(
+ PathFlags::empty(),
+ path,
+ OpenFlags::CREATE,
+ DescriptorFlags::empty(),
+ )
+ .expect("creating a file");
+ drop(file);
+}
+
+fn test_path_symlink_trailing_slashes(dir: &Descriptor) {
+ // Dangling symlink: Without a trailing slash, this should succeed. Not
+ // all platforms (e.g. Windows) support dangling symlinks though, so skip
+ // the dangling tests if creation fails.
+ if dir.symlink_at("source", "target").is_ok() {
+ dir.unlink_file_at("target").expect("removing a file");
+
+ // Dangling symlink: Link destination shouldn't end with a slash.
+ let err = dir
+ .symlink_at("source", "target/")
+ .expect_err("link destination ending with a slash should fail");
+ assert!(
+ matches!(err, ErrorCode::NoEntry),
+ "unexpected error {err:?}"
+ );
+ }
+
+ // Link destination already exists, target has trailing slash.
+ dir.create_directory_at("target")
+ .expect("creating a directory");
+ let err = dir
+ .symlink_at("source", "target/")
+ .expect_err("link destination already exists");
+ assert!(
+ matches!(err, ErrorCode::Exist | ErrorCode::NoEntry),
+ "unexpected error {err:?}"
+ );
+ dir.remove_directory_at("target")
+ .expect("removing a directory");
+
+ // Link destination already exists, target has no trailing slash.
+ dir.create_directory_at("target")
+ .expect("creating a directory");
+ let err = dir
+ .symlink_at("source", "target")
+ .expect_err("link destination already exists");
+ assert!(
+ matches!(err, ErrorCode::Exist | ErrorCode::NoEntry),
+ "unexpected error {err:?}"
+ );
+ dir.remove_directory_at("target")
+ .expect("removing a directory");
+
+ // Link destination already exists, target has trailing slash.
+ create_file(dir, "target");
+
+ let err = dir
+ .symlink_at("source", "target/")
+ .expect_err("link destination already exists");
+ assert!(
+ matches!(err, ErrorCode::NotDirectory | ErrorCode::NoEntry),
+ "unexpected error {err:?}"
+ );
+ dir.unlink_file_at("target").expect("removing a file");
+
+ // Link destination already exists, target has no trailing slash.
+ create_file(dir, "target");
+
+ let err = dir
+ .symlink_at("source", "target")
+ .expect_err("link destination already exists");
+ assert!(
+ matches!(err, ErrorCode::Exist | ErrorCode::NoEntry),
+ "unexpected error {err:?}"
+ );
+ dir.unlink_file_at("target").expect("removing a file");
+}
+
+fn main() {
+ let preopens = get_directories();
+ let (dir, _name) = &preopens[0];
+
+ // Run the tests.
+ test_path_symlink_trailing_slashes(dir)
+}
diff --git a/crates/test-programs/src/bin/p2_poll_oneoff_stdio.rs b/crates/test-programs/src/bin/p2_poll_oneoff_stdio.rs
new file mode 100644
index 0000000000..bcbaf76801
--- /dev/null
+++ b/crates/test-programs/src/bin/p2_poll_oneoff_stdio.rs
@@ -0,0 +1,73 @@
+use std::collections::HashMap;
+use wasip2::clocks::monotonic_clock;
+use wasip2::io::poll::{Pollable, poll};
+
+const TIMEOUT: u64 = 200_000_000u64; // 200 milliseconds, required to satisfy slow execution in CI
+
+fn test_stdin_read() {
+ let stdin = wasip2::cli::stdin::get_stdin();
+ let clock = monotonic_clock::subscribe_duration(TIMEOUT);
+ let stdin_ready = stdin.subscribe();
+
+ // Either stdin can be ready for reading, or this poll can timeout.
+ let out = poll(&[&clock, &stdin_ready]);
+ // The result should be either a timeout, or that stdin is ready for reading.
+ // Both are valid behaviors that depend on the test environment.
+ assert!(out.len() >= 1, "stdin read should return at least 1 event");
+ for index in out {
+ match index {
+ 0 | 1 => {}
+ other => panic!("unexpected ready index {other}"),
+ }
+ }
+}
+
+fn test_stdout_stderr_write() {
+ let stdout = wasip2::cli::stdout::get_stdout();
+ let stderr = wasip2::cli::stderr::get_stderr();
+ let mut writable: HashMap<u64, Pollable> = [(1, stdout.subscribe()), (2, stderr.subscribe())]
+ .into_iter()
+ .collect();
+
+ let clock = monotonic_clock::subscribe_duration(TIMEOUT);
+ let mut timed_out = false;
+ while !writable.is_empty() {
+ if timed_out {
+ panic!(
+ "timed out with the following pending subs: {:?}",
+ writable.keys().collect::<Vec<_>>()
+ )
+ }
+ let mut ids = Vec::new();
+ let mut pollables = Vec::new();
+ for (id, pollable) in writable.iter() {
+ ids.push(*id);
+ pollables.push(pollable);
+ }
+ pollables.push(&clock);
+ let out = poll(&pollables);
+ for index in out {
+ let index = index as usize;
+ if index == ids.len() {
+ timed_out = true;
+ } else {
+ let id = ids[index];
+ if writable.remove(&id).is_none() {
+ panic!("Unknown id {id}, pending subs: {ids:?}")
+ }
+ }
+ }
+ }
+}
+
+fn test_poll_oneoff() {
+ // NB we assume that stdin/stdout/stderr are valid and open
+ // for the duration of the test case
+ test_stdin_read();
+ test_stdout_stderr_write();
+}
+
+fn main() {
+ // Run the tests.
+ test_poll_oneoff()
+}
diff --git a/crates/test-programs/src/bin/p2_remove_directory.rs b/crates/test-programs/src/bin/p2_remove_directory.rs
new file mode 100644
index 0000000000..8d6a10335f
--- /dev/null
+++ b/crates/test-programs/src/bin/p2_remove_directory.rs
@@ -0,0 +1,59 @@
+use wasip2::filesystem::preopens::get_directories;
+use wasip2::filesystem::types::{Descriptor, DescriptorFlags, ErrorCode, OpenFlags, PathFlags};
+
+fn create_file(dir: &Descriptor, path: &str) {
+ let file = dir
+ .open_at(
+ PathFlags::empty(),
+ path,
+ OpenFlags::CREATE,
+ DescriptorFlags::empty(),
+ )
+ .expect("creating a file");
+ drop(file);
+}
+
+fn test_remove_directory(dir: &Descriptor) {
+ // Create a directory in the scratch directory.
+ dir.create_directory_at("dir")
+ .expect("creating a directory");
+
+ // Test that removing it succeeds.
+ dir.remove_directory_at("dir")
+ .expect("remove_directory on a directory should succeed");
+
+ // There isn't consistient behavior across operating systems of whether removing with a
+ // directory where the path has a trailing slash succeeds or fails, so we won't test
+ // that behavior.
+
+ // Create a temporary file.
+ create_file(dir, "file");
+
+ // Test that removing it with no trailing slash fails.
+ let err = dir
+ .remove_directory_at("file")
+ .expect_err("remove_directory without a trailing slash on a file should fail");
+ assert!(
+ matches!(err, ErrorCode::NotDirectory),
+ "unexpected error {err:?}"
+ );
+
+ // Test that removing it with a trailing slash fails.
+ let err = dir
+ .remove_directory_at("file/")
+ .expect_err("remove_directory with a trailing slash on a file should fail");
+ assert!(
+ matches!(err, ErrorCode::NotDirectory),
+ "unexpected error {err:?}"
+ );
+
+ dir.unlink_file_at("file").expect("removing a file");
+}
+
+fn main() {
+ let preopens = get_directories();
+ let (dir, _name) = &preopens[0];
+
+ // Run the tests.
+ test_remove_directory(dir)
+}
diff --git a/crates/test-programs/src/bin/p2_remove_nonempty_directory.rs b/crates/test-programs/src/bin/p2_remove_nonempty_directory.rs
new file mode 100644
index 0000000000..ec306f653b
--- /dev/null
+++ b/crates/test-programs/src/bin/p2_remove_nonempty_directory.rs
@@ -0,0 +1,35 @@
+use wasip2::filesystem::preopens::get_directories;
+use wasip2::filesystem::types::{Descriptor, ErrorCode};
+
+fn test_remove_nonempty_directory(dir: &Descriptor) {
+ // Create a directory in the scratch directory.
+ dir.create_directory_at("dir")
+ .expect("creating a directory");
+
+ // Create a directory in the directory we just created.
+ dir.create_directory_at("dir/nested")
+ .expect("creating a subdirectory");
+
+ // Test that attempting to unlink the first directory returns the expected error code.
+ let err = dir
+ .remove_directory_at("dir")
+ .expect_err("remove_directory on a directory should return NotEmpty");
+ assert!(
+ matches!(err, ErrorCode::NotEmpty),
+ "unexpected error {err:?}"
+ );
+
+ // Removing the directories.
+ dir.remove_directory_at("dir/nested")
+ .expect("remove_directory on a nested directory should succeed");
+ dir.remove_directory_at("dir")
+ .expect("removing a directory");
+}
+
+fn main() {
+ let preopens = get_directories();
+ let (dir, _name) = &preopens[0];
+
+ // Run the tests.
+ test_remove_nonempty_directory(dir)
+}
diff --git a/crates/test-programs/src/bin/p2_symlink_create.rs b/crates/test-programs/src/bin/p2_symlink_create.rs
new file mode 100644
index 0000000000..3d09af18f4
--- /dev/null
+++ b/crates/test-programs/src/bin/p2_symlink_create.rs
@@ -0,0 +1,77 @@
+use wasip2::filesystem::preopens::get_directories;
+use wasip2::filesystem::types::{Descriptor, DescriptorFlags, OpenFlags, PathFlags};
+
+fn create_symlink_to_file(dir: &Descriptor) {
+ // Create a file for the symlink to point to.
+ let target = dir
+ .open_at(
+ PathFlags::empty(),
+ "target",
+ OpenFlags::CREATE,
+ DescriptorFlags::empty(),
+ )
+ .expect("creating a file");
+ drop(target);
+
+ // Create a symlink.
+ dir.symlink_at("target", "symlink")
+ .expect("creating a symlink");
+
+ // Try to open it as a file following symlinks.
+ let target_file_via_symlink = dir
+ .open_at(
+ PathFlags::SYMLINK_FOLLOW,
+ "symlink",
+ OpenFlags::empty(),
+ DescriptorFlags::empty(),
+ )
+ .expect("opening a symlink as a file");
+ drop(target_file_via_symlink);
+
+ // Clean up.
+ dir.unlink_file_at("symlink").expect("removing the symlink");
+ dir.unlink_file_at("target")
+ .expect("removing the target file");
+}
+
+fn create_symlink_to_directory(dir: &Descriptor) {
+ // Create a directory for the symlink to point to.
+ dir.create_directory_at("target").expect("creating a dir");
+
+ // Create a symlink.
+ dir.symlink_at("target", "symlink")
+ .expect("creating a symlink");
+
+ // Try to open it as a directory following symlinks.
+ let target_dir_via_symlink = dir
+ .open_at(
+ PathFlags::SYMLINK_FOLLOW,
+ "symlink",
+ OpenFlags::DIRECTORY,
+ DescriptorFlags::empty(),
+ )
+ .expect("opening a symlink as a directory");
+ drop(target_dir_via_symlink);
+
+ // Clean up.
+ dir.unlink_file_at("symlink")
+ .expect("remove symlink to directory");
+ dir.remove_directory_at("target")
+ .expect("remove_directory on a directory should succeed");
+}
+
+fn create_symlink_to_root(dir: &Descriptor) {
+ // Create a symlink.
+ dir.symlink_at("/", "symlink")
+ .expect_err("creating a symlink to an absolute path");
+}
+
+fn main() {
+ let preopens = get_directories();
+ let (dir, _name) = &preopens[0];
+
+ // Run the tests.
+ create_symlink_to_file(dir);
+ create_symlink_to_directory(dir);
+ create_symlink_to_root(dir);
+}
diff --git a/crates/test-programs/src/bin/p2_symlink_filestat.rs b/crates/test-programs/src/bin/p2_symlink_filestat.rs
new file mode 100644
index 0000000000..757eef5d42
--- /dev/null
+++ b/crates/test-programs/src/bin/p2_symlink_filestat.rs
@@ -0,0 +1,118 @@
+use std::time::Duration;
+use wasip2::clocks::wall_clock::Datetime;
+use wasip2::filesystem::preopens::get_directories;
+use wasip2::filesystem::types::{Descriptor, DescriptorFlags, NewTimestamp, OpenFlags, PathFlags};
+
+fn to_duration(t: Option<Datetime>) -> Duration {
+ let t = t.expect("timestamp should be present");
+ Duration::new(t.seconds, t.nanoseconds)
+}
+
+fn to_timestamp(d: Duration) -> NewTimestamp {
+ NewTimestamp::Timestamp(Datetime {
+ seconds: d.as_secs(),
+ nanoseconds: d.subsec_nanos(),
+ })
+}
+
+fn test_path_filestat(dir: &Descriptor) {
+ // Create a file in the scratch directory.
+ let file = dir
+ .open_at(
+ PathFlags::empty(),
+ "file",
+ OpenFlags::CREATE,
+ DescriptorFlags::READ | DescriptorFlags::WRITE,
+ )
+ .expect("opening a file");
+
+ // Check file size
+ let file_stat = dir
+ .stat_at(PathFlags::empty(), "file")
+ .expect("reading file stats");
+ assert_eq!(file_stat.size, 0, "file size should be 0");
+ let file_mtim = to_duration(file_stat.data_modification_timestamp);
+
+ // Create a symlink
+ dir.symlink_at("file", "symlink")
+ .expect("creating symlink to a file");
+
+ // Check set_times_at on the symlink itself
+ let sym_stat = dir
+ .stat_at(PathFlags::empty(), "symlink")
+ .expect("reading symlink stats");
+ let sym_mtim = to_duration(sym_stat.data_modification_timestamp);
+
+ // Modify mtim of symlink
+ let sym_new_mtim = sym_mtim - Duration::from_secs(1);
+ dir.set_times_at(
+ PathFlags::empty(),
+ "symlink",
+ NewTimestamp::NoChange,
+ to_timestamp(sym_new_mtim),
+ )
+ .expect("set_times_at should succeed on symlink");
+
+ // Check that symlink mtim motification worked
+ let modified_sym_stat = dir
+ .stat_at(PathFlags::empty(), "symlink")
+ .expect("reading file stats after set_times_at");
+
+ assert_eq!(
+ to_duration(modified_sym_stat.data_modification_timestamp),
+ sym_new_mtim,
+ "symlink mtim should change"
+ );
+
+ // Check that pointee mtim is not modified
+ let unmodified_file_stat = dir
+ .stat_at(PathFlags::empty(), "file")
+ .expect("reading file stats after set_times_at");
+
+ assert_eq!(
+ to_duration(unmodified_file_stat.data_modification_timestamp),
+ file_mtim,
+ "file mtim should not change"
+ );
+
+ // Now, dereference the symlink
+ let deref_sym_stat = dir
+ .stat_at(PathFlags::SYMLINK_FOLLOW, "symlink")
+ .expect("reading file stats on the dereferenced symlink");
+ assert_eq!(
+ to_duration(deref_sym_stat.data_modification_timestamp),
+ file_mtim,
+ "symlink mtim should be equal to pointee's when dereferenced"
+ );
+
+ // Finally, change stat of the original file by dereferencing the symlink
+ dir.set_times_at(
+ PathFlags::SYMLINK_FOLLOW,
+ "symlink",
+ NewTimestamp::NoChange,
+ to_timestamp(sym_mtim),
+ )
+ .expect("set_times_at should succeed on setting stat on original file");
+
+ let new_file_stat = dir
+ .stat_at(PathFlags::empty(), "file")
+ .expect("reading file stats after set_times_at");
+
+ assert_eq!(
+ to_duration(new_file_stat.data_modification_timestamp),
+ sym_mtim,
+ "mtim should change"
+ );
+
+ drop(file);
+ dir.unlink_file_at("symlink").expect("removing a symlink");
+ dir.unlink_file_at("file").expect("removing a file");
+}
+
+fn main() {
+ let preopens = get_directories();
+ let (dir, _name) = &preopens[0];
+
+ // Run the tests.
+ test_path_filestat(dir)
+}
diff --git a/crates/test-programs/src/bin/p2_symlink_loop.rs b/crates/test-programs/src/bin/p2_symlink_loop.rs
new file mode 100644
index 0000000000..280464b241
--- /dev/null
+++ b/crates/test-programs/src/bin/p2_symlink_loop.rs
@@ -0,0 +1,32 @@
+use wasip2::filesystem::preopens::get_directories;
+use wasip2::filesystem::types::{Descriptor, DescriptorFlags, ErrorCode, OpenFlags, PathFlags};
+
+fn test_symlink_loop(dir: &Descriptor) {
+ // Create a self-referencing symlink. Not all platforms (e.g. Windows)
+ // support dangling symlinks, so skip the test if creation fails.
+ if dir.symlink_at("symlink", "symlink").is_err() {
+ return;
+ }
+
+ // Try to open it.
+ let err = dir
+ .open_at(
+ PathFlags::empty(),
+ "symlink",
+ OpenFlags::empty(),
+ DescriptorFlags::empty(),
+ )
+ .expect_err("opening a self-referencing symlink");
+ assert!(matches!(err, ErrorCode::Loop), "unexpected error {err:?}");
+
+ // Clean up.
+ dir.unlink_file_at("symlink").expect("removing a file");
+}
+
+fn main() {
+ let preopens = get_directories();
+ let (dir, _name) = &preopens[0];
+
+ // Run the tests.
+ test_symlink_loop(dir)
+}
diff --git a/crates/test-programs/src/bin/p2_unlink_file_trailing_slashes.rs b/crates/test-programs/src/bin/p2_unlink_file_trailing_slashes.rs
new file mode 100644
index 0000000000..54f0b8efb1
--- /dev/null
+++ b/crates/test-programs/src/bin/p2_unlink_file_trailing_slashes.rs
@@ -0,0 +1,73 @@
+use wasip2::filesystem::preopens::get_directories;
+use wasip2::filesystem::types::{Descriptor, DescriptorFlags, ErrorCode, OpenFlags, PathFlags};
+
+fn create_file(dir: &Descriptor, path: &str) {
+ let file = dir
+ .open_at(
+ PathFlags::empty(),
+ path,
+ OpenFlags::CREATE,
+ DescriptorFlags::empty(),
+ )
+ .expect("creating a file");
+ drop(file);
+}
+
+fn test_unlink_file_trailing_slashes(dir: &Descriptor) {
+ // Create a directory in the scratch directory.
+ dir.create_directory_at("dir")
+ .expect("creating a directory");
+
+ // Test that unlinking it fails. macOS reports not-permitted, other unix
+ // platforms report is-directory, and Windows reports access.
+ let err = dir
+ .unlink_file_at("dir")
+ .expect_err("unlink_file on a directory should fail");
+ assert!(
+ matches!(
+ err,
+ ErrorCode::NotPermitted | ErrorCode::IsDirectory | ErrorCode::Access
+ ),
+ "unexpected error {err:?}"
+ );
+
+ // Test that unlinking it with a trailing flash fails.
+ let err = dir
+ .unlink_file_at("dir/")
+ .expect_err("unlink_file on a directory should fail");
+ assert!(
+ matches!(
+ err,
+ ErrorCode::NotPermitted | ErrorCode::IsDirectory | ErrorCode::Access
+ ),
+ "unexpected error {err:?}"
+ );
+
+ // Clean up.
+ dir.remove_directory_at("dir")
+ .expect("removing a directory");
+
+ // Create a temporary file.
+ create_file(dir, "file");
+
+ // Test that unlinking it with a trailing flash fails.
+ let err = dir
+ .unlink_file_at("file/")
+ .expect_err("unlink_file with a trailing slash should fail");
+ assert!(
+ matches!(err, ErrorCode::NotDirectory),
+ "unexpected error {err:?}"
+ );
+
+ // Test that unlinking it with no trailing flash succeeds.
+ dir.unlink_file_at("file")
+ .expect("unlink_file with no trailing slash should succeed");
+}
+
+fn main() {
+ let preopens = get_directories();
+ let (dir, _name) = &preopens[0];
+
+ // Run the tests.
+ test_unlink_file_trailing_slashes(dir)
+}
diff --git a/crates/wasi/tests/all/p1.rs b/crates/wasi/tests/all/p1.rs
index 99bee44964..ebaaf1f39c 100644
--- a/crates/wasi/tests/all/p1.rs
+++ b/crates/wasi/tests/all/p1.rs
@@ -38,45 +38,9 @@ foreach_p1!(assert_test_exists);
// Below here is mechanical: there should be one test for every binary in
// wasi-tests.
#[test_log::test(tokio::test(flavor = "multi_thread"))]
-async fn p1_big_random_buf() {
- run(P1_BIG_RANDOM_BUF, |_| {}).await.unwrap()
-}
-#[test_log::test(tokio::test(flavor = "multi_thread"))]
-async fn p1_clock_time_get() {
- run(P1_CLOCK_TIME_GET, |_| {}).await.unwrap()
-}
-#[test_log::test(tokio::test(flavor = "multi_thread"))]
-async fn p1_close_preopen() {
- run(P1_CLOSE_PREOPEN, |_| {}).await.unwrap()
-}
-#[test_log::test(tokio::test(flavor = "multi_thread"))]
-async fn p1_dangling_fd() {
- run(P1_DANGLING_FD, |_| {}).await.unwrap()
-}
-#[test_log::test(tokio::test(flavor = "multi_thread"))]
-async fn p1_dangling_symlink() {
- run(P1_DANGLING_SYMLINK, |_| {}).await.unwrap()
-}
-#[test_log::test(tokio::test(flavor = "multi_thread"))]
-async fn p1_directory_seek() {
- run(P1_DIRECTORY_SEEK, |_| {}).await.unwrap()
-}
-#[test_log::test(tokio::test(flavor = "multi_thread"))]
-async fn p1_dir_fd_op_failures() {
- run(P1_DIR_FD_OP_FAILURES, |_| {}).await.unwrap()
-}
-#[test_log::test(tokio::test(flavor = "multi_thread"))]
-async fn p1_fd_advise() {
- run(P1_FD_ADVISE, |_| {}).await.unwrap()
-}
-#[test_log::test(tokio::test(flavor = "multi_thread"))]
async fn p1_fd_filestat_get() {
run(P1_FD_FILESTAT_GET, |_| {}).await.unwrap()
}
-#[test_log::test(tokio::test(flavor = "multi_thread"))]
-async fn p1_fd_filestat_set() {
- run(P1_FD_FILESTAT_SET, |_| {}).await.unwrap()
-}
#[test_log::test(tokio::test(flavor = "multi_thread"))]
async fn p1_stat_extreme_host_mtime() {
@@ -97,10 +61,6 @@ async fn p1_fd_readdir() {
run(P1_FD_READDIR, |_| {}).await.unwrap()
}
#[test_log::test(tokio::test(flavor = "multi_thread"))]
-async fn p1_file_allocate() {
- run(P1_FILE_ALLOCATE, |_| {}).await.unwrap()
-}
-#[test_log::test(tokio::test(flavor = "multi_thread"))]
async fn p1_file_pread_pwrite() {
run(P1_FILE_PREAD_PWRITE, |_| {}).await.unwrap()
}
@@ -121,109 +81,19 @@ async fn p1_file_unbuffered_write() {
run(P1_FILE_UNBUFFERED_WRITE, |_| {}).await.unwrap()
}
#[test_log::test(tokio::test(flavor = "multi_thread"))]
-async fn p1_interesting_paths() {
- run(P1_INTERESTING_PATHS, |b| {
- b.inherit_stdio();
- })
- .await
- .unwrap()
-}
-#[test_log::test(tokio::test(flavor = "multi_thread"))]
-async fn p1_regular_file_isatty() {
- run(P1_REGULAR_FILE_ISATTY, |_| {}).await.unwrap()
-}
-#[test_log::test(tokio::test(flavor = "multi_thread"))]
-async fn p1_nofollow_errors() {
- run(P1_NOFOLLOW_ERRORS, |_| {}).await.unwrap()
-}
-#[test_log::test(tokio::test(flavor = "multi_thread"))]
-async fn p1_overwrite_preopen() {
- run(P1_OVERWRITE_PREOPEN, |_| {}).await.unwrap()
-}
-#[test_log::test(tokio::test(flavor = "multi_thread"))]
-async fn p1_path_exists() {
- run(P1_PATH_EXISTS, |_| {}).await.unwrap()
-}
-#[test_log::test(tokio::test(flavor = "multi_thread"))]
-async fn p1_path_filestat() {
- run(P1_PATH_FILESTAT, |_| {}).await.unwrap()
-}
-#[test_log::test(tokio::test(flavor = "multi_thread"))]
-async fn p1_path_link() {
- run(P1_PATH_LINK, |_| {}).await.unwrap()
-}
-#[test_log::test(tokio::test(flavor = "multi_thread"))]
-async fn p1_path_open_create_existing() {
- run(P1_PATH_OPEN_CREATE_EXISTING, |_| {}).await.unwrap()
-}
-#[test_log::test(tokio::test(flavor = "multi_thread"))]
-async fn p1_path_open_read_write() {
- run(P1_PATH_OPEN_READ_WRITE, |_| {}).await.unwrap()
-}
-#[test_log::test(tokio::test(flavor = "multi_thread"))]
-async fn p1_path_open_dirfd_not_dir() {
- run(P1_PATH_OPEN_DIRFD_NOT_DIR, |_| {}).await.unwrap()
-}
-#[test_log::test(tokio::test(flavor = "multi_thread"))]
-async fn p1_path_open_missing() {
- run(P1_PATH_OPEN_MISSING, |_| {}).await.unwrap()
-}
-#[test_log::test(tokio::test(flavor = "multi_thread"))]
-async fn p1_path_open_nonblock() {
- run(P1_PATH_OPEN_NONBLOCK, |_| {}).await.unwrap()
-}
-#[test_log::test(tokio::test(flavor = "multi_thread"))]
-async fn p1_path_rename_dir_trailing_slashes() {
- run(P1_PATH_RENAME_DIR_TRAILING_SLASHES, |_| {})
- .await
- .unwrap()
-}
-#[test_log::test(tokio::test(flavor = "multi_thread"))]
-async fn p1_path_rename() {
- run(P1_PATH_RENAME, |_| {}).await.unwrap()
-}
-#[test_log::test(tokio::test(flavor = "multi_thread"))]
-async fn p1_path_symlink_trailing_slashes() {
- run(P1_PATH_SYMLINK_TRAILING_SLASHES, |_| {}).await.unwrap()
-}
-#[test_log::test(tokio::test(flavor = "multi_thread"))]
async fn p1_poll_oneoff_files() {
run(P1_POLL_ONEOFF_FILES, |_| {}).await.unwrap()
}
-#[test_log::test(tokio::test(flavor = "multi_thread"))]
-async fn p1_poll_oneoff_stdio() {
- run(P1_POLL_ONEOFF_STDIO, |b| {
- b.inherit_stdio();
- })
- .await
- .unwrap()
-}
#[test_log::test(tokio::test(flavor = "multi_thread"))]
async fn p1_readlink() {
run(P1_READLINK, |_| {}).await.unwrap()
}
#[test_log::test(tokio::test(flavor = "multi_thread"))]
-async fn p1_remove_directory() {
- run(P1_REMOVE_DIRECTORY, |_| {}).await.unwrap()
-}
-#[test_log::test(tokio::test(flavor = "multi_thread"))]
-async fn p1_remove_nonempty_directory() {
- run(P1_REMOVE_NONEMPTY_DIRECTORY, |_| {}).await.unwrap()
-}
-#[test_log::test(tokio::test(flavor = "multi_thread"))]
async fn p1_renumber() {
run(P1_RENUMBER, |_| {}).await.unwrap()
}
#[test_log::test(tokio::test(flavor = "multi_thread"))]
-async fn p1_sched_yield() {
- run(P1_SCHED_YIELD, |_| {}).await.unwrap()
-}
-#[test_log::test(tokio::test(flavor = "multi_thread"))]
-async fn p1_stdio() {
- run(P1_STDIO, |_| {}).await.unwrap()
-}
-#[test_log::test(tokio::test(flavor = "multi_thread"))]
async fn p1_stdio_isatty() {
// If the test process is setup such that stdio is a terminal:
if test_programs_artifacts::stdio_is_terminal() {
@@ -241,26 +111,6 @@ async fn p1_stdio_not_isatty() {
run(P1_STDIO_NOT_ISATTY, |_| {}).await.unwrap()
}
#[test_log::test(tokio::test(flavor = "multi_thread"))]
-async fn p1_symlink_create() {
- run(P1_SYMLINK_CREATE, |_| {}).await.unwrap()
-}
-#[test_log::test(tokio::test(flavor = "multi_thread"))]
-async fn p1_symlink_filestat() {
- run(P1_SYMLINK_FILESTAT, |_| {}).await.unwrap()
-}
-#[test_log::test(tokio::test(flavor = "multi_thread"))]
-async fn p1_symlink_loop() {
- run(P1_SYMLINK_LOOP, |_| {}).await.unwrap()
-}
-#[test_log::test(tokio::test(flavor = "multi_thread"))]
-async fn p1_unlink_file_trailing_slashes() {
- run(P1_UNLINK_FILE_TRAILING_SLASHES, |_| {}).await.unwrap()
-}
-#[test_log::test(tokio::test(flavor = "multi_thread"))]
-async fn p1_path_open_preopen() {
- run(P1_PATH_OPEN_PREOPEN, |_| {}).await.unwrap()
-}
-#[test_log::test(tokio::test(flavor = "multi_thread"))]
async fn p1_unicode_output() {
run(P1_UNICODE_OUTPUT, |b| {
b.inherit_stdio();
diff --git a/crates/wasi/tests/all/p2/async_.rs b/crates/wasi/tests/all/p2/async_.rs
index c2d059b4d5..81bb0955f1 100644
--- a/crates/wasi/tests/all/p2/async_.rs
+++ b/crates/wasi/tests/all/p2/async_.rs
@@ -41,46 +41,10 @@ foreach_p2!(assert_test_exists);
// Below here is mechanical: there should be one test for every binary in
// wasi-tests.
#[test_log::test(tokio::test(flavor = "multi_thread"))]
-async fn p1_big_random_buf() {
- run(P1_BIG_RANDOM_BUF_COMPONENT, |_| {}).await.unwrap()
-}
-#[test_log::test(tokio::test(flavor = "multi_thread"))]
-async fn p1_clock_time_get() {
- run(P1_CLOCK_TIME_GET_COMPONENT, |_| {}).await.unwrap()
-}
-#[test_log::test(tokio::test(flavor = "multi_thread"))]
-async fn p1_close_preopen() {
- run(P1_CLOSE_PREOPEN_COMPONENT, |_| {}).await.unwrap()
-}
-#[test_log::test(tokio::test(flavor = "multi_thread"))]
-async fn p1_dangling_fd() {
- run(P1_DANGLING_FD_COMPONENT, |_| {}).await.unwrap()
-}
-#[test_log::test(tokio::test(flavor = "multi_thread"))]
-async fn p1_dangling_symlink() {
- run(P1_DANGLING_SYMLINK_COMPONENT, |_| {}).await.unwrap()
-}
-#[test_log::test(tokio::test(flavor = "multi_thread"))]
-async fn p1_directory_seek() {
- run(P1_DIRECTORY_SEEK_COMPONENT, |_| {}).await.unwrap()
-}
-#[test_log::test(tokio::test(flavor = "multi_thread"))]
-async fn p1_dir_fd_op_failures() {
- run(P1_DIR_FD_OP_FAILURES_COMPONENT, |_| {}).await.unwrap()
-}
-#[test_log::test(tokio::test(flavor = "multi_thread"))]
-async fn p1_fd_advise() {
- run(P1_FD_ADVISE_COMPONENT, |_| {}).await.unwrap()
-}
-#[test_log::test(tokio::test(flavor = "multi_thread"))]
async fn p1_fd_filestat_get() {
run(P1_FD_FILESTAT_GET_COMPONENT, |_| {}).await.unwrap()
}
#[test_log::test(tokio::test(flavor = "multi_thread"))]
-async fn p1_fd_filestat_set() {
- run(P1_FD_FILESTAT_SET_COMPONENT, |_| {}).await.unwrap()
-}
-#[test_log::test(tokio::test(flavor = "multi_thread"))]
async fn p1_stat_extreme_host_mtime() {
run_with_workspace_setup(
P1_STAT_EXTREME_HOST_MTIME_COMPONENT,
@@ -99,10 +63,6 @@ async fn p1_fd_readdir() {
run(P1_FD_READDIR_COMPONENT, |_| {}).await.unwrap()
}
#[test_log::test(tokio::test(flavor = "multi_thread"))]
-async fn p1_file_allocate() {
- run(P1_FILE_ALLOCATE_COMPONENT, |_| {}).await.unwrap()
-}
-#[test_log::test(tokio::test(flavor = "multi_thread"))]
async fn p1_file_pread_pwrite() {
run(P1_FILE_PREAD_PWRITE_COMPONENT, |_| {}).await.unwrap()
}
@@ -125,119 +85,19 @@ async fn p1_file_unbuffered_write() {
.unwrap()
}
#[test_log::test(tokio::test(flavor = "multi_thread"))]
-async fn p1_interesting_paths() {
- run(P1_INTERESTING_PATHS_COMPONENT, |b| {
- b.inherit_stdio();
- })
- .await
- .unwrap()
-}
-#[test_log::test(tokio::test(flavor = "multi_thread"))]
-async fn p1_regular_file_isatty() {
- run(P1_REGULAR_FILE_ISATTY_COMPONENT, |_| {}).await.unwrap()
-}
-#[test_log::test(tokio::test(flavor = "multi_thread"))]
-async fn p1_nofollow_errors() {
- run(P1_NOFOLLOW_ERRORS_COMPONENT, |_| {}).await.unwrap()
-}
-#[test_log::test(tokio::test(flavor = "multi_thread"))]
-async fn p1_overwrite_preopen() {
- run(P1_OVERWRITE_PREOPEN_COMPONENT, |_| {}).await.unwrap()
-}
-#[test_log::test(tokio::test(flavor = "multi_thread"))]
-async fn p1_path_exists() {
- run(P1_PATH_EXISTS_COMPONENT, |_| {}).await.unwrap()
-}
-#[test_log::test(tokio::test(flavor = "multi_thread"))]
-async fn p1_path_filestat() {
- run(P1_PATH_FILESTAT_COMPONENT, |_| {}).await.unwrap()
-}
-#[test_log::test(tokio::test(flavor = "multi_thread"))]
-async fn p1_path_link() {
- run(P1_PATH_LINK_COMPONENT, |_| {}).await.unwrap()
-}
-#[test_log::test(tokio::test(flavor = "multi_thread"))]
-async fn p1_path_open_create_existing() {
- run(P1_PATH_OPEN_CREATE_EXISTING_COMPONENT, |_| {})
- .await
- .unwrap()
-}
-#[test_log::test(tokio::test(flavor = "multi_thread"))]
-async fn p1_path_open_read_write() {
- run(P1_PATH_OPEN_READ_WRITE_COMPONENT, |_| {})
- .await
- .unwrap()
-}
-#[test_log::test(tokio::test(flavor = "multi_thread"))]
-async fn p1_path_open_dirfd_not_dir() {
- run(P1_PATH_OPEN_DIRFD_NOT_DIR_COMPONENT, |_| {})
- .await
- .unwrap()
-}
-#[test_log::test(tokio::test(flavor = "multi_thread"))]
-async fn p1_path_open_missing() {
- run(P1_PATH_OPEN_MISSING_COMPONENT, |_| {}).await.unwrap()
-}
-#[test_log::test(tokio::test(flavor = "multi_thread"))]
-async fn p1_path_open_nonblock() {
- run(P1_PATH_OPEN_NONBLOCK_COMPONENT, |_| {}).await.unwrap()
-}
-#[test_log::test(tokio::test(flavor = "multi_thread"))]
-async fn p1_path_rename_dir_trailing_slashes() {
- run(P1_PATH_RENAME_DIR_TRAILING_SLASHES_COMPONENT, |_| {})
- .await
- .unwrap()
-}
-#[test_log::test(tokio::test(flavor = "multi_thread"))]
-async fn p1_path_rename() {
- run(P1_PATH_RENAME_COMPONENT, |_| {}).await.unwrap()
-}
-#[test_log::test(tokio::test(flavor = "multi_thread"))]
-async fn p1_path_symlink_trailing_slashes() {
- run(P1_PATH_SYMLINK_TRAILING_SLASHES_COMPONENT, |_| {})
- .await
- .unwrap()
-}
-#[test_log::test(tokio::test(flavor = "multi_thread"))]
async fn p1_poll_oneoff_files() {
run(P1_POLL_ONEOFF_FILES_COMPONENT, |_| {}).await.unwrap()
}
-#[test_log::test(tokio::test(flavor = "multi_thread"))]
-async fn p1_poll_oneoff_stdio() {
- run(P1_POLL_ONEOFF_STDIO_COMPONENT, |b| {
- b.inherit_stdio();
- })
- .await
- .unwrap()
-}
#[test_log::test(tokio::test(flavor = "multi_thread"))]
async fn p1_readlink() {
run(P1_READLINK_COMPONENT, |_| {}).await.unwrap()
}
#[test_log::test(tokio::test(flavor = "multi_thread"))]
-async fn p1_remove_directory() {
- run(P1_REMOVE_DIRECTORY_COMPONENT, |_| {}).await.unwrap()
-}
-#[test_log::test(tokio::test(flavor = "multi_thread"))]
-async fn p1_remove_nonempty_directory() {
- run(P1_REMOVE_NONEMPTY_DIRECTORY_COMPONENT, |_| {})
- .await
- .unwrap()
-}
-#[test_log::test(tokio::test(flavor = "multi_thread"))]
async fn p1_renumber() {
run(P1_RENUMBER_COMPONENT, |_| {}).await.unwrap()
}
#[test_log::test(tokio::test(flavor = "multi_thread"))]
-async fn p1_sched_yield() {
- run(P1_SCHED_YIELD_COMPONENT, |_| {}).await.unwrap()
-}
-#[test_log::test(tokio::test(flavor = "multi_thread"))]
-async fn p1_stdio() {
- run(P1_STDIO_COMPONENT, |_| {}).await.unwrap()
-}
-#[test_log::test(tokio::test(flavor = "multi_thread"))]
async fn p1_stdio_isatty() {
// If the test process is setup such that stdio is a terminal:
if test_programs_artifacts::stdio_is_terminal() {
@@ -255,28 +115,6 @@ async fn p1_stdio_not_isatty() {
run(P1_STDIO_NOT_ISATTY_COMPONENT, |_| {}).await.unwrap()
}
#[test_log::test(tokio::test(flavor = "multi_thread"))]
-async fn p1_symlink_create() {
- run(P1_SYMLINK_CREATE_COMPONENT, |_| {}).await.unwrap()
-}
-#[test_log::test(tokio::test(flavor = "multi_thread"))]
-async fn p1_symlink_filestat() {
- run(P1_SYMLINK_FILESTAT_COMPONENT, |_| {}).await.unwrap()
-}
-#[test_log::test(tokio::test(flavor = "multi_thread"))]
-async fn p1_symlink_loop() {
- run(P1_SYMLINK_LOOP_COMPONENT, |_| {}).await.unwrap()
-}
-#[test_log::test(tokio::test(flavor = "multi_thread"))]
-async fn p1_unlink_file_trailing_slashes() {
- run(P1_UNLINK_FILE_TRAILING_SLASHES_COMPONENT, |_| {})
- .await
- .unwrap()
-}
-#[test_log::test(tokio::test(flavor = "multi_thread"))]
-async fn p1_path_open_preopen() {
- run(P1_PATH_OPEN_PREOPEN_COMPONENT, |_| {}).await.unwrap()
-}
-#[test_log::test(tokio::test(flavor = "multi_thread"))]
async fn p1_unicode_output() {
run(P1_UNICODE_OUTPUT_COMPONENT, |b| {
b.inherit_stdio();
@@ -308,6 +146,10 @@ async fn p2_random() {
run(P2_RANDOM_COMPONENT, |_| {}).await.unwrap()
}
#[test_log::test(tokio::test(flavor = "multi_thread"))]
+async fn p2_big_random_buf() {
+ run(P2_BIG_RANDOM_BUF_COMPONENT, |_| {}).await.unwrap()
+}
+#[test_log::test(tokio::test(flavor = "multi_thread"))]
async fn p2_ip_name_lookup() {
run(P2_IP_NAME_LOOKUP_COMPONENT, |_| {}).await.unwrap()
}
@@ -417,6 +259,128 @@ async fn p2_file_read_write() {
run(P2_FILE_READ_WRITE_COMPONENT, |_| {}).await.unwrap()
}
#[test_log::test(tokio::test(flavor = "multi_thread"))]
+async fn p2_close_preopen() {
+ run(P2_CLOSE_PREOPEN_COMPONENT, |_| {}).await.unwrap()
+}
+#[test_log::test(tokio::test(flavor = "multi_thread"))]
+async fn p2_nofollow_errors() {
+ run(P2_NOFOLLOW_ERRORS_COMPONENT, |_| {}).await.unwrap()
+}
+#[test_log::test(tokio::test(flavor = "multi_thread"))]
+async fn p2_path_exists() {
+ run(P2_PATH_EXISTS_COMPONENT, |_| {}).await.unwrap()
+}
+#[test_log::test(tokio::test(flavor = "multi_thread"))]
+async fn p2_path_open_create_existing() {
+ run(P2_PATH_OPEN_CREATE_EXISTING_COMPONENT, |_| {})
+ .await
+ .unwrap()
+}
+#[test_log::test(tokio::test(flavor = "multi_thread"))]
+async fn p2_path_open_missing() {
+ run(P2_PATH_OPEN_MISSING_COMPONENT, |_| {}).await.unwrap()
+}
+#[test_log::test(tokio::test(flavor = "multi_thread"))]
+async fn p2_path_rename_dir_trailing_slashes() {
+ run(P2_PATH_RENAME_DIR_TRAILING_SLASHES_COMPONENT, |_| {})
+ .await
+ .unwrap()
+}
+#[test_log::test(tokio::test(flavor = "multi_thread"))]
+async fn p2_remove_nonempty_directory() {
+ run(P2_REMOVE_NONEMPTY_DIRECTORY_COMPONENT, |_| {})
+ .await
+ .unwrap()
+}
+#[test_log::test(tokio::test(flavor = "multi_thread"))]
+async fn p2_symlink_create() {
+ run(P2_SYMLINK_CREATE_COMPONENT, |_| {}).await.unwrap()
+}
+#[test_log::test(tokio::test(flavor = "multi_thread"))]
+async fn p2_fd_advise() {
+ run(P2_FD_ADVISE_COMPONENT, |_| {}).await.unwrap()
+}
+#[test_log::test(tokio::test(flavor = "multi_thread"))]
+async fn p2_poll_oneoff_stdio() {
+ run(P2_POLL_ONEOFF_STDIO_COMPONENT, |b| {
+ b.inherit_stdio();
+ })
+ .await
+ .unwrap()
+}
+#[test_log::test(tokio::test(flavor = "multi_thread"))]
+async fn p2_symlink_filestat() {
+ run(P2_SYMLINK_FILESTAT_COMPONENT, |_| {}).await.unwrap()
+}
+#[test_log::test(tokio::test(flavor = "multi_thread"))]
+async fn p2_remove_directory() {
+ run(P2_REMOVE_DIRECTORY_COMPONENT, |_| {}).await.unwrap()
+}
+#[test_log::test(tokio::test(flavor = "multi_thread"))]
+async fn p2_dangling_symlink() {
+ run(P2_DANGLING_SYMLINK_COMPONENT, |_| {}).await.unwrap()
+}
+#[test_log::test(tokio::test(flavor = "multi_thread"))]
+async fn p2_dir_fd_op_failures() {
+ run(P2_DIR_FD_OP_FAILURES_COMPONENT, |_| {}).await.unwrap()
+}
+#[test_log::test(tokio::test(flavor = "multi_thread"))]
+async fn p2_fd_filestat_set() {
+ run(P2_FD_FILESTAT_SET_COMPONENT, |_| {}).await.unwrap()
+}
+#[test_log::test(tokio::test(flavor = "multi_thread"))]
+async fn p2_interesting_paths() {
+ run(P2_INTERESTING_PATHS_COMPONENT, |_| {}).await.unwrap()
+}
+#[test_log::test(tokio::test(flavor = "multi_thread"))]
+async fn p2_path_filestat() {
+ run(P2_PATH_FILESTAT_COMPONENT, |_| {}).await.unwrap()
+}
+#[test_log::test(tokio::test(flavor = "multi_thread"))]
+async fn p2_path_link() {
+ run(P2_PATH_LINK_COMPONENT, |_| {}).await.unwrap()
+}
+#[test_log::test(tokio::test(flavor = "multi_thread"))]
+async fn p2_path_open_dirfd_not_dir() {
+ run(P2_PATH_OPEN_DIRFD_NOT_DIR_COMPONENT, |_| {})
+ .await
+ .unwrap()
+}
+#[test_log::test(tokio::test(flavor = "multi_thread"))]
+async fn p2_path_open_preopen() {
+ run(P2_PATH_OPEN_PREOPEN_COMPONENT, |_| {}).await.unwrap()
+}
+#[test_log::test(tokio::test(flavor = "multi_thread"))]
+async fn p2_path_open_read_write() {
+ run(P2_PATH_OPEN_READ_WRITE_COMPONENT, |_| {})
+ .await
+ .unwrap()
+}
+#[test_log::test(tokio::test(flavor = "multi_thread"))]
+async fn p2_path_rename() {
+ run(P2_PATH_RENAME_COMPONENT, |_| {}).await.unwrap()
+}
+#[test_log::test(tokio::test(flavor = "multi_thread"))]
+async fn p2_path_symlink_trailing_slashes() {
+ run(P2_PATH_SYMLINK_TRAILING_SLASHES_COMPONENT, |_| {})
+ .await
+ .unwrap()
+}
+#[test_log::test(tokio::test(flavor = "multi_thread"))]
+async fn p2_symlink_loop() {
+ run(P2_SYMLINK_LOOP_COMPONENT, |_| {}).await.unwrap()
+}
+#[test_log::test(tokio::test(flavor = "multi_thread"))]
+async fn p2_unlink_file_trailing_slashes() {
+ run(P2_UNLINK_FILE_TRAILING_SLASHES_COMPONENT, |_| {})
+ .await
+ .unwrap()
+}
+#[test_log::test(tokio::test(flavor = "multi_thread"))]
+async fn p2_dangling_fd() {
+ run(P2_DANGLING_FD_COMPONENT, |_| {}).await.unwrap()
+}
+#[test_log::test(tokio::test(flavor = "multi_thread"))]
async fn p2_udp_send_too_much() {
let e = run(P2_UDP_SEND_TOO_MUCH_COMPONENT, |_| {})
.await
@@ -514,6 +478,10 @@ async fn file_stream_not_permitted(component_path: &str) {
async fn p2_clocks_zero_wait() {
run(P2_CLOCKS_ZERO_WAIT_COMPONENT, |_| {}).await.unwrap()
}
+#[test_log::test(tokio::test(flavor = "multi_thread"))]
+async fn p2_clock_time_get() {
+ run(P2_CLOCK_TIME_GET_COMPONENT, |_| {}).await.unwrap()
+}
#[test_log::test(tokio::test(flavor = "multi_thread"))]
async fn p2_environment_named_imports() -> Result<()> {
diff --git a/crates/wasi/tests/all/p2/sync.rs b/crates/wasi/tests/all/p2/sync.rs
index 9e6d3e510c..32fbe17873 100644
--- a/crates/wasi/tests/all/p2/sync.rs
+++ b/crates/wasi/tests/all/p2/sync.rs
@@ -45,46 +45,10 @@ foreach_p2!(assert_test_exists);
// Below here is mechanical: there should be one test for every binary in
// wasi-tests.
#[test_log::test]
-fn p1_big_random_buf() {
- run(P1_BIG_RANDOM_BUF_COMPONENT, |_| {}).unwrap()
-}
-#[test_log::test]
-fn p1_clock_time_get() {
- run(P1_CLOCK_TIME_GET_COMPONENT, |_| {}).unwrap()
-}
-#[test_log::test]
-fn p1_close_preopen() {
- run(P1_CLOSE_PREOPEN_COMPONENT, |_| {}).unwrap()
-}
-#[test_log::test]
-fn p1_dangling_fd() {
- run(P1_DANGLING_FD_COMPONENT, |_| {}).unwrap()
-}
-#[test_log::test]
-fn p1_dangling_symlink() {
- run(P1_DANGLING_SYMLINK_COMPONENT, |_| {}).unwrap()
-}
-#[test_log::test]
-fn p1_directory_seek() {
- run(P1_DIRECTORY_SEEK_COMPONENT, |_| {}).unwrap()
-}
-#[test_log::test]
-fn p1_dir_fd_op_failures() {
- run(P1_DIR_FD_OP_FAILURES_COMPONENT, |_| {}).unwrap()
-}
-#[test_log::test]
-fn p1_fd_advise() {
- run(P1_FD_ADVISE_COMPONENT, |_| {}).unwrap()
-}
-#[test_log::test]
fn p1_fd_filestat_get() {
run(P1_FD_FILESTAT_GET_COMPONENT, |_| {}).unwrap()
}
#[test_log::test]
-fn p1_fd_filestat_set() {
- run(P1_FD_FILESTAT_SET_COMPONENT, |_| {}).unwrap()
-}
-#[test_log::test]
fn p1_stat_extreme_host_mtime() {
run_with_workspace_setup(
P1_STAT_EXTREME_HOST_MTIME_COMPONENT,
@@ -102,10 +66,6 @@ fn p1_fd_readdir() {
run(P1_FD_READDIR_COMPONENT, |_| {}).unwrap()
}
#[test_log::test]
-fn p1_file_allocate() {
- run(P1_FILE_ALLOCATE_COMPONENT, |_| {}).unwrap()
-}
-#[test_log::test]
fn p1_file_pread_pwrite() {
run(P1_FILE_PREAD_PWRITE_COMPONENT, |_| {}).unwrap()
}
@@ -126,102 +86,19 @@ fn p1_file_unbuffered_write() {
run(P1_FILE_UNBUFFERED_WRITE_COMPONENT, |_| {}).unwrap()
}
#[test_log::test]
-fn p1_interesting_paths() {
- run(P1_INTERESTING_PATHS_COMPONENT, |_| {}).unwrap()
-}
-#[test_log::test]
-fn p1_regular_file_isatty() {
- run(P1_REGULAR_FILE_ISATTY_COMPONENT, |_| {}).unwrap()
-}
-#[test_log::test]
-fn p1_nofollow_errors() {
- run(P1_NOFOLLOW_ERRORS_COMPONENT, |_| {}).unwrap()
-}
-#[test_log::test]
-fn p1_overwrite_preopen() {
- run(P1_OVERWRITE_PREOPEN_COMPONENT, |_| {}).unwrap()
-}
-#[test_log::test]
-fn p1_path_exists() {
- run(P1_PATH_EXISTS_COMPONENT, |_| {}).unwrap()
-}
-#[test_log::test]
-fn p1_path_filestat() {
- run(P1_PATH_FILESTAT_COMPONENT, |_| {}).unwrap()
-}
-#[test_log::test]
-fn p1_path_link() {
- run(P1_PATH_LINK_COMPONENT, |_| {}).unwrap()
-}
-#[test_log::test]
-fn p1_path_open_create_existing() {
- run(P1_PATH_OPEN_CREATE_EXISTING_COMPONENT, |_| {}).unwrap()
-}
-#[test_log::test]
-fn p1_path_open_read_write() {
- run(P1_PATH_OPEN_READ_WRITE_COMPONENT, |_| {}).unwrap()
-}
-#[test_log::test]
-fn p1_path_open_dirfd_not_dir() {
- run(P1_PATH_OPEN_DIRFD_NOT_DIR_COMPONENT, |_| {}).unwrap()
-}
-#[test_log::test]
-fn p1_path_open_missing() {
- run(P1_PATH_OPEN_MISSING_COMPONENT, |_| {}).unwrap()
-}
-#[test_log::test]
-fn p1_path_open_nonblock() {
- run(P1_PATH_OPEN_NONBLOCK_COMPONENT, |_| {}).unwrap()
-}
-#[test_log::test]
-fn p1_path_rename_dir_trailing_slashes() {
- run(P1_PATH_RENAME_DIR_TRAILING_SLASHES_COMPONENT, |_| {}).unwrap()
-}
-#[test_log::test]
-fn p1_path_rename() {
- run(P1_PATH_RENAME_COMPONENT, |_| {}).unwrap()
-}
-#[test_log::test]
-fn p1_path_symlink_trailing_slashes() {
- run(P1_PATH_SYMLINK_TRAILING_SLASHES_COMPONENT, |_| {}).unwrap()
-}
-#[test_log::test]
fn p1_poll_oneoff_files() {
run(P1_POLL_ONEOFF_FILES_COMPONENT, |_| {}).unwrap()
}
-#[test_log::test]
-fn p1_poll_oneoff_stdio() {
- run(P1_POLL_ONEOFF_STDIO_COMPONENT, |b| {
- b.inherit_stdio();
- })
- .unwrap()
-}
#[test_log::test]
fn p1_readlink() {
run(P1_READLINK_COMPONENT, |_| {}).unwrap()
}
#[test_log::test]
-fn p1_remove_directory() {
- run(P1_REMOVE_DIRECTORY_COMPONENT, |_| {}).unwrap()
-}
-#[test_log::test]
-fn p1_remove_nonempty_directory() {
- run(P1_REMOVE_NONEMPTY_DIRECTORY_COMPONENT, |_| {}).unwrap()
-}
-#[test_log::test]
fn p1_renumber() {
run(P1_RENUMBER_COMPONENT, |_| {}).unwrap()
}
#[test_log::test]
-fn p1_sched_yield() {
- run(P1_SCHED_YIELD_COMPONENT, |_| {}).unwrap()
-}
-#[test_log::test]
-fn p1_stdio() {
- run(P1_STDIO_COMPONENT, |_| {}).unwrap()
-}
-#[test_log::test]
fn p1_stdio_isatty() {
// If the test process is setup such that stdio is a terminal:
if test_programs_artifacts::stdio_is_terminal() {
@@ -238,26 +115,6 @@ fn p1_stdio_not_isatty() {
run(P1_STDIO_NOT_ISATTY_COMPONENT, |_| {}).unwrap()
}
#[test_log::test]
-fn p1_symlink_create() {
- run(P1_SYMLINK_CREATE_COMPONENT, |_| {}).unwrap()
-}
-#[test_log::test]
-fn p1_symlink_filestat() {
- run(P1_SYMLINK_FILESTAT_COMPONENT, |_| {}).unwrap()
-}
-#[test_log::test]
-fn p1_symlink_loop() {
- run(P1_SYMLINK_LOOP_COMPONENT, |_| {}).unwrap()
-}
-#[test_log::test]
-fn p1_unlink_file_trailing_slashes() {
- run(P1_UNLINK_FILE_TRAILING_SLASHES_COMPONENT, |_| {}).unwrap()
-}
-#[test_log::test]
-fn p1_path_open_preopen() {
- run(P1_PATH_OPEN_PREOPEN_COMPONENT, |_| {}).unwrap()
-}
-#[test_log::test]
fn p1_unicode_output() {
run(P1_UNICODE_OUTPUT_COMPONENT, |b| {
b.inherit_stdio();
@@ -286,6 +143,10 @@ fn p2_random() {
run(P2_RANDOM_COMPONENT, |_| {}).unwrap()
}
#[test_log::test]
+fn p2_big_random_buf() {
+ run(P2_BIG_RANDOM_BUF_COMPONENT, |_| {}).unwrap()
+}
+#[test_log::test]
fn p2_ip_name_lookup() {
run(P2_IP_NAME_LOOKUP_COMPONENT, |_| {}).unwrap()
}
@@ -391,6 +252,113 @@ fn p2_file_read_write() {
run(P2_FILE_READ_WRITE_COMPONENT, |_| {}).unwrap()
}
#[test_log::test]
+fn p2_close_preopen() {
+ run(P2_CLOSE_PREOPEN_COMPONENT, |_| {}).unwrap()
+}
+#[test_log::test]
+fn p2_nofollow_errors() {
+ run(P2_NOFOLLOW_ERRORS_COMPONENT, |_| {}).unwrap()
+}
+#[test_log::test]
+fn p2_path_exists() {
+ run(P2_PATH_EXISTS_COMPONENT, |_| {}).unwrap()
+}
+#[test_log::test]
+fn p2_path_open_create_existing() {
+ run(P2_PATH_OPEN_CREATE_EXISTING_COMPONENT, |_| {}).unwrap()
+}
+#[test_log::test]
+fn p2_path_open_missing() {
+ run(P2_PATH_OPEN_MISSING_COMPONENT, |_| {}).unwrap()
+}
+#[test_log::test]
+fn p2_path_rename_dir_trailing_slashes() {
+ run(P2_PATH_RENAME_DIR_TRAILING_SLASHES_COMPONENT, |_| {}).unwrap()
+}
+#[test_log::test]
+fn p2_remove_nonempty_directory() {
+ run(P2_REMOVE_NONEMPTY_DIRECTORY_COMPONENT, |_| {}).unwrap()
+}
+#[test_log::test]
+fn p2_symlink_create() {
+ run(P2_SYMLINK_CREATE_COMPONENT, |_| {}).unwrap()
+}
+#[test_log::test]
+fn p2_fd_advise() {
+ run(P2_FD_ADVISE_COMPONENT, |_| {}).unwrap()
+}
+#[test_log::test]
+fn p2_poll_oneoff_stdio() {
+ run(P2_POLL_ONEOFF_STDIO_COMPONENT, |b| {
+ b.inherit_stdio();
+ })
+ .unwrap()
+}
+#[test_log::test]
+fn p2_symlink_filestat() {
+ run(P2_SYMLINK_FILESTAT_COMPONENT, |_| {}).unwrap()
+}
+#[test_log::test]
+fn p2_remove_directory() {
+ run(P2_REMOVE_DIRECTORY_COMPONENT, |_| {}).unwrap()
+}
+#[test_log::test]
+fn p2_dangling_symlink() {
+ run(P2_DANGLING_SYMLINK_COMPONENT, |_| {}).unwrap()
+}
+#[test_log::test]
+fn p2_dir_fd_op_failures() {
+ run(P2_DIR_FD_OP_FAILURES_COMPONENT, |_| {}).unwrap()
+}
+#[test_log::test]
+fn p2_fd_filestat_set() {
+ run(P2_FD_FILESTAT_SET_COMPONENT, |_| {}).unwrap()
+}
+#[test_log::test]
+fn p2_interesting_paths() {
+ run(P2_INTERESTING_PATHS_COMPONENT, |_| {}).unwrap()
+}
+#[test_log::test]
+fn p2_path_filestat() {
+ run(P2_PATH_FILESTAT_COMPONENT, |_| {}).unwrap()
+}
+#[test_log::test]
+fn p2_path_link() {
+ run(P2_PATH_LINK_COMPONENT, |_| {}).unwrap()
+}
+#[test_log::test]
+fn p2_path_open_dirfd_not_dir() {
+ run(P2_PATH_OPEN_DIRFD_NOT_DIR_COMPONENT, |_| {}).unwrap()
+}
+#[test_log::test]
+fn p2_path_open_preopen() {
+ run(P2_PATH_OPEN_PREOPEN_COMPONENT, |_| {}).unwrap()
+}
+#[test_log::test]
+fn p2_path_open_read_write() {
+ run(P2_PATH_OPEN_READ_WRITE_COMPONENT, |_| {}).unwrap()
+}
+#[test_log::test]
+fn p2_path_rename() {
+ run(P2_PATH_RENAME_COMPONENT, |_| {}).unwrap()
+}
+#[test_log::test]
+fn p2_path_symlink_trailing_slashes() {
+ run(P2_PATH_SYMLINK_TRAILING_SLASHES_COMPONENT, |_| {}).unwrap()
+}
+#[test_log::test]
+fn p2_symlink_loop() {
+ run(P2_SYMLINK_LOOP_COMPONENT, |_| {}).unwrap()
+}
+#[test_log::test]
+fn p2_unlink_file_trailing_slashes() {
+ run(P2_UNLINK_FILE_TRAILING_SLASHES_COMPONENT, |_| {}).unwrap()
+}
+#[test_log::test]
+fn p2_dangling_fd() {
+ run(P2_DANGLING_FD_COMPONENT, |_| {}).unwrap()
+}
+#[test_log::test]
fn p2_udp_send_too_much() {
let e = run(P2_UDP_SEND_TOO_MUCH_COMPONENT, |_| {}).unwrap_err();
assert_eq!(
@@ -482,6 +450,10 @@ fn file_stream_not_permitted(component_path: &str) {
fn p2_clocks_zero_wait() {
run(P2_CLOCKS_ZERO_WAIT_COMPONENT, |_| {}).unwrap()
}
+#[test_log::test]
+fn p2_clock_time_get() {
+ run(P2_CLOCK_TIME_GET_COMPONENT, |_| {}).unwrap()
+}
#[test]
fn p2_environment_named_imports() {
diff --git a/tests/wasi.rs b/tests/wasi.rs
index 957ab293fb..785ea3c21e 100644
--- a/tests/wasi.rs
+++ b/tests/wasi.rs
@@ -13,6 +13,7 @@ use std::io::{Read, Write};
use std::net::{SocketAddr, TcpStream};
use std::path::Path;
use std::process::{Child, Stdio};
+use std::sync::{Arc, LazyLock};
use tempfile::TempDir;
use wasmtime::error::Context;
use wasmtime::{Result, format_err};
@@ -23,48 +24,90 @@ const KNOWN_FAILURES: &[&str] = &[
// "multi-clock-wait",
];
+const KNOWN_ADAPTER_FAILURES: &[&str] = &[
+ // The adapter maps all nonzero exit codes to 1 as `wasi:cli/exit` can only
+ // signal success or failure.
+ "proc_exit-failure",
+ // The adapter returns success for zero-length writes before validating the
+ // file descriptor.
+ "fd_write-to-invalid-fd",
+ // Not possible to adapt properly - written in AssemblyScript
+ "args_get-multiple-arguments",
+ "environ_get-multiple-variables",
+];
+
fn main() -> Result<()> {
env_logger::init();
let mut trials = Vec::new();
if !cfg!(miri) {
- find_tests("tests/wasi-testsuite".as_ref(), &mut trials).unwrap();
+ let adapted_dir = Arc::new(TempDir::new()?);
+ find_tests("tests/wasi-testsuite".as_ref(), &adapted_dir, &mut trials).unwrap();
}
libtest_mimic::run(&Arguments::from_args(), trials).exit()
}
-fn find_tests(path: &Path, trials: &mut Vec<Trial>) -> Result<()> {
+fn find_tests(path: &Path, adapted_dir: &Arc<TempDir>, trials: &mut Vec<Trial>) -> Result<()> {
for entry in path.read_dir()? {
let entry = entry?;
let path = entry.path();
if entry.file_type()?.is_dir() {
- find_tests(&path, trials)?;
+ find_tests(&path, adapted_dir, trials)?;
continue;
}
if path.extension().and_then(|s| s.to_str()) != Some("wasm") {
continue;
}
- // Test the core wasm itself.
+ let test_name = path.file_stem().unwrap().to_str().unwrap().to_string();
+
+ // Test the wasm itself.
+ let should_fail = KNOWN_FAILURES.contains(&test_name.as_str())
+ || (path.iter().any(|p| p == "wasm32-wasip3")
+ && !cfg!(feature = "component-model-async"));
trials.push(Trial::test(
format!("wasmtime-wasi - {}", path.display()),
{
let path = path.clone();
- move || run_test(&path).map_err(|e| format!("{e:?}").into())
+ move || {
+ run_test(&path.with_extension("json"), &path, should_fail)
+ .map_err(|e| format!("{e:?}").into())
+ }
},
));
+
+ // Additionally test wasip1 modules through the wasip2 adapter.
+ if cfg!(feature = "component-model") && path.iter().any(|p| p == "wasm32-wasip1") {
+ let should_fail = KNOWN_FAILURES.contains(&test_name.as_str())
+ || KNOWN_ADAPTER_FAILURES.contains(&test_name.as_str());
+ trials.push(Trial::test(
+ format!("wasmtime-wasi + adapter - {}", path.display()),
+ {
+ let path = path.clone();
+ let adapted_dir = adapted_dir.clone();
+ move || {
+ // Required to make unique names
+ let name = path
+ .iter()
+ .map(|c| c.to_string_lossy())
+ .collect::<Vec<_>>()
+ .join("_");
+ let component = adapted_dir.path().join(name);
+ fs::write(&component, componentize(&path)?)
+ .map_err(|e| format!("{e:?}"))?;
+ run_test(&path.with_extension("json"), &component, should_fail)
+ .map_err(|e| format!("{e:?}").into())
+ }
+ },
+ ));
+ }
}
Ok(())
}
-fn run_test(path: &Path) -> Result<()> {
- let test_name = path.file_stem().unwrap().to_str().unwrap();
- let mut should_fail = KNOWN_FAILURES.contains(&test_name);
- if path.iter().any(|p| p == "wasm32-wasip3") && !cfg!(feature = "component-model-async") {
- should_fail = true;
- }
- match (execute(path), should_fail) {
+fn run_test(spec: &Path, wasm: &Path, should_fail: bool) -> Result<()> {
+ match (execute(spec, wasm), should_fail) {
// If this test passed and is not a known failure, or if it failed and
// it's a known failure, then flag this test as "ok".
(Ok(_), false) | (Err(_), true) => Ok(()),
@@ -80,11 +123,13 @@ fn run_test(path: &Path) -> Result<()> {
}
}
-fn execute(path: &Path) -> Result<()> {
+fn execute(spec_path: &Path, wasm: &Path) -> Result<()> {
let wasmtime = Path::new(env!("CARGO_BIN_EXE_wasmtime"));
let target_dir = wasmtime.parent().unwrap().parent().unwrap();
- let parent_dir = path.parent().ok_or(format_err!("module has no parent?"))?;
- let spec = if let Ok(contents) = fs::read_to_string(&path.with_extension("json")) {
+ let parent_dir = spec_path
+ .parent()
+ .ok_or(format_err!("module has no parent?"))?;
+ let spec = if let Ok(contents) = fs::read_to_string(spec_path) {
serde_json::from_str(&contents)?
} else {
Spec::default()
@@ -129,7 +174,7 @@ fn execute(path: &Path) -> Result<()> {
cmd.arg("--env");
cmd.arg(format!("{k}={v}"));
}
- if path.iter().any(|p| p == "wasm32-wasip3") {
+ if spec_path.iter().any(|p| p == "wasm32-wasip3") {
cmd.arg("-Sp3").arg("-Wcomponent-model-async");
}
cmd.arg("-Stcp,udp");
@@ -143,7 +188,7 @@ fn execute(path: &Path) -> Result<()> {
}
};
}
- cmd.arg(&path);
+ cmd.arg(&wasm);
cmd.args(args);
cmd.stdout(Stdio::piped());
cmd.stderr(Stdio::piped());
@@ -288,6 +333,20 @@ fn execute(path: &Path) -> Result<()> {
Ok(())
}
+fn componentize(path: &Path) -> Result<Vec<u8>> {
+ static ADAPTER: LazyLock<Vec<u8>> = LazyLock::new(|| {
+ fs::read(test_programs_artifacts::ADAPTER_COMMAND).expect("failed to read command adapter")
+ });
+
+ let module = fs::read(path)?;
+ wit_component::ComponentEncoder::default()
+ .module(&module)
+ .and_then(|e| e.validate(true).adapter("wasi_snapshot_preview1", &ADAPTER))
+ .and_then(|e| e.encode())
+ .map_err(wasmtime::Error::from_anyhow)
+ .with_context(|| format!("failed to componentize {}", path.display()))
+}
+
fn cp_r(path: &Path, dst: &Path) -> Result<()> {
fs::create_dir(dst)?;
for entry in path.read_dir()? {