diff --git a/tests/rust/src/bin/dangling_fd.rs b/tests/rust/src/bin/dangling_fd.rs index 3d29c4fc..61600376 100644 --- a/tests/rust/src/bin/dangling_fd.rs +++ b/tests/rust/src/bin/dangling_fd.rs @@ -3,29 +3,33 @@ use wasi_tests::{open_scratch_directory, TESTCONFIG}; unsafe fn test_dangling_fd(dir_fd: wasi::Fd) { if TESTCONFIG.support_dangling_filesystem() { + const FILE_NAME: &str = "file.cleanup"; + const DIR_NAME: &str = "subdir.cleanup"; // Create a file, open it, delete it without closing the handle, // and then try creating it again - let fd = wasi::path_open(dir_fd, 0, "file", wasi::OFLAGS_CREAT, 0, 0, 0).unwrap(); + let fd = wasi::path_open(dir_fd, 0, FILE_NAME, wasi::OFLAGS_CREAT, 0, 0, 0).unwrap(); wasi::fd_close(fd).unwrap(); - let file_fd = wasi::path_open(dir_fd, 0, "file", 0, 0, 0, 0).expect("failed to open"); + let file_fd = + wasi::path_open(dir_fd, 0, FILE_NAME, 0, 0, 0, 0).expect("failed to open"); assert!( file_fd > libc::STDERR_FILENO as wasi::Fd, "file descriptor range check", ); - wasi::path_unlink_file(dir_fd, "file").expect("failed to unlink"); - let fd = wasi::path_open(dir_fd, 0, "file", wasi::OFLAGS_CREAT, 0, 0, 0).unwrap(); + wasi::path_unlink_file(dir_fd, FILE_NAME).expect("failed to unlink"); + let fd = wasi::path_open(dir_fd, 0, FILE_NAME, wasi::OFLAGS_CREAT, 0, 0, 0).unwrap(); wasi::fd_close(fd).unwrap(); // Now, repeat the same process but for a directory - wasi::path_create_directory(dir_fd, "subdir").expect("failed to create dir"); - let subdir_fd = wasi::path_open(dir_fd, 0, "subdir", wasi::OFLAGS_DIRECTORY, 0, 0, 0) - .expect("failed to open dir"); + wasi::path_create_directory(dir_fd, DIR_NAME).expect("failed to create dir"); + let subdir_fd = + wasi::path_open(dir_fd, 0, DIR_NAME, wasi::OFLAGS_DIRECTORY, 0, 0, 0) + .expect("failed to open dir"); assert!( subdir_fd > libc::STDERR_FILENO as wasi::Fd, "file descriptor range check", ); - wasi::path_remove_directory(dir_fd, "subdir").expect("failed to remove dir 2"); - wasi::path_create_directory(dir_fd, "subdir").expect("failed to create dir 2"); + wasi::path_remove_directory(dir_fd, DIR_NAME).expect("failed to remove dir 2"); + wasi::path_create_directory(dir_fd, DIR_NAME).expect("failed to create dir 2"); } } diff --git a/tests/rust/src/bin/dangling_symlink.rs b/tests/rust/src/bin/dangling_symlink.rs index bfc9095d..a76ab050 100644 --- a/tests/rust/src/bin/dangling_symlink.rs +++ b/tests/rust/src/bin/dangling_symlink.rs @@ -3,12 +3,13 @@ use wasi_tests::{assert_errno, open_scratch_directory, TESTCONFIG}; unsafe fn test_dangling_symlink(dir_fd: wasi::Fd) { if TESTCONFIG.support_dangling_filesystem() { + const SYMLINK_NAME: &str = "symlink.cleanup"; // First create a dangling symlink. - wasi::path_symlink("target", dir_fd, "symlink").expect("creating a symlink"); + wasi::path_symlink("target", dir_fd, SYMLINK_NAME).expect("creating a symlink"); // Try to open it as a directory with O_NOFOLLOW. assert_errno!( - wasi::path_open(dir_fd, 0, "symlink", wasi::OFLAGS_DIRECTORY, 0, 0, 0) + wasi::path_open(dir_fd, 0, SYMLINK_NAME, wasi::OFLAGS_DIRECTORY, 0, 0, 0) .expect_err("opening a dangling symlink as a directory") .raw_error(), wasi::ERRNO_NOTDIR, @@ -17,14 +18,14 @@ unsafe fn test_dangling_symlink(dir_fd: wasi::Fd) { // Try to open it as a file with O_NOFOLLOW. assert_errno!( - wasi::path_open(dir_fd, 0, "symlink", 0, 0, 0, 0) + wasi::path_open(dir_fd, 0, SYMLINK_NAME, 0, 0, 0, 0) .expect_err("opening a dangling symlink as a file") .raw_error(), wasi::ERRNO_LOOP ); // Clean up. - wasi::path_unlink_file(dir_fd, "symlink").expect("failed to remove file"); + wasi::path_unlink_file(dir_fd, SYMLINK_NAME).expect("failed to remove file"); } } diff --git a/tests/rust/src/bin/directory_seek.rs b/tests/rust/src/bin/directory_seek.rs index ab4b0dbc..04f0dfff 100644 --- a/tests/rust/src/bin/directory_seek.rs +++ b/tests/rust/src/bin/directory_seek.rs @@ -2,14 +2,15 @@ use std::{env, process}; use wasi_tests::{assert_errno, open_scratch_directory}; unsafe fn test_directory_seek(dir_fd: wasi::Fd) { + const DIR_NAME: &str = "dir.cleanup"; // Create a directory in the scratch directory. - wasi::path_create_directory(dir_fd, "dir").expect("failed to make directory"); + wasi::path_create_directory(dir_fd, "dir.cleanup").expect("failed to make directory"); // Open the directory and attempt to request rights for seeking. let fd = wasi::path_open( dir_fd, 0, - "dir", + DIR_NAME, wasi::OFLAGS_DIRECTORY, wasi::RIGHTS_FD_SEEK, 0, @@ -44,7 +45,7 @@ unsafe fn test_directory_seek(dir_fd: wasi::Fd) { // Clean up. wasi::fd_close(fd).expect("failed to close fd"); - wasi::path_remove_directory(dir_fd, "dir").expect("failed to remove dir"); + wasi::path_remove_directory(dir_fd, DIR_NAME).expect("failed to remove dir"); } fn main() { diff --git a/tests/rust/src/bin/fd_advise.rs b/tests/rust/src/bin/fd_advise.rs index 62346fdc..e4c571d3 100644 --- a/tests/rust/src/bin/fd_advise.rs +++ b/tests/rust/src/bin/fd_advise.rs @@ -2,11 +2,12 @@ use std::{env, process}; use wasi_tests::{open_scratch_directory, TESTCONFIG}; unsafe fn test_fd_advise(dir_fd: wasi::Fd) { + const FILE_NAME: &str = "file.cleanup"; // Create a file in the scratch directory. let file_fd = wasi::path_open( dir_fd, 0, - "file", + FILE_NAME, wasi::OFLAGS_CREAT, wasi::RIGHTS_FD_READ | wasi::RIGHTS_FD_WRITE @@ -49,7 +50,7 @@ unsafe fn test_fd_advise(dir_fd: wasi::Fd) { } wasi::fd_close(file_fd).expect("failed to close"); - wasi::path_unlink_file(dir_fd, "file").expect("failed to unlink"); + wasi::path_unlink_file(dir_fd, FILE_NAME).expect("failed to unlink"); } fn main() { let mut args = env::args(); diff --git a/tests/rust/src/bin/fd_filestat_set.rs b/tests/rust/src/bin/fd_filestat_set.rs index 424831b3..c0e7709d 100644 --- a/tests/rust/src/bin/fd_filestat_set.rs +++ b/tests/rust/src/bin/fd_filestat_set.rs @@ -2,11 +2,12 @@ use std::{env, process}; use wasi_tests::open_scratch_directory; unsafe fn test_fd_filestat_set(dir_fd: wasi::Fd) { + const FILE_NAME: &str = "file.cleanup"; // Create a file in the scratch directory. let file_fd = wasi::path_open( dir_fd, 0, - "file", + FILE_NAME, wasi::OFLAGS_CREAT, wasi::RIGHTS_FD_READ | wasi::RIGHTS_FD_WRITE @@ -47,7 +48,7 @@ unsafe fn test_fd_filestat_set(dir_fd: wasi::Fd) { // assert_eq!(status, wasi::EINVAL, "ATIM & ATIM_NOW can't both be set"); wasi::fd_close(file_fd).expect("failed to close fd"); - wasi::path_unlink_file(dir_fd, "file").expect("failed to remove dir"); + wasi::path_unlink_file(dir_fd, FILE_NAME).expect("failed to remove dir"); } fn main() { let mut args = env::args(); diff --git a/tests/rust/src/bin/fd_flags_set.rs b/tests/rust/src/bin/fd_flags_set.rs index 17586ef0..49c0dbc2 100644 --- a/tests/rust/src/bin/fd_flags_set.rs +++ b/tests/rust/src/bin/fd_flags_set.rs @@ -2,7 +2,7 @@ use std::{env, process}; use wasi_tests::open_scratch_directory; unsafe fn test_fd_fdstat_set_flags(dir_fd: wasi::Fd) { - const FILE_NAME: &str = "file"; + const FILE_NAME: &str = "file.cleanup"; let data = &[0u8; 100]; let file_fd = wasi::path_open( diff --git a/tests/rust/src/bin/fd_readdir.rs b/tests/rust/src/bin/fd_readdir.rs index 87aa112c..4679ce07 100644 --- a/tests/rust/src/bin/fd_readdir.rs +++ b/tests/rust/src/bin/fd_readdir.rs @@ -1,4 +1,5 @@ use std::{env, mem, process, slice, str}; +use wasi::path_create_directory; use wasi_tests::open_scratch_directory; const BUF_LEN: usize = 256; @@ -52,6 +53,24 @@ impl<'a> Iterator for ReadDir<'a> { } } +unsafe fn create_tmp_dir(dir_fd: wasi::Fd, name: &str) -> wasi::Fd { + path_create_directory(dir_fd, "dir.cleanup").expect("failed to create dir"); + wasi::path_open( + dir_fd, + 0, + name, + wasi::OFLAGS_DIRECTORY, + wasi::RIGHTS_FD_FILESTAT_GET + | wasi::RIGHTS_FD_READDIR + | wasi::RIGHTS_PATH_CREATE_FILE + | wasi::RIGHTS_PATH_OPEN + | wasi::RIGHTS_PATH_UNLINK_FILE, + wasi::RIGHTS_FD_FILESTAT_GET, + 0, + ) + .expect("failed to open dir") +} + /// Return the entries plus a bool indicating EOF. unsafe fn exec_fd_readdir(fd: wasi::Fd, cookie: wasi::Dircookie) -> (Vec, bool) { let mut buf: [u8; BUF_LEN] = [0; BUF_LEN]; @@ -65,7 +84,8 @@ unsafe fn exec_fd_readdir(fd: wasi::Fd, cookie: wasi::Dircookie) -> (Vec