Skip to content

Commit

Permalink
Add .cleanup suffix for files created in tests so the're automaticall…
Browse files Browse the repository at this point in the history
…y cleaned up by the test runner
  • Loading branch information
loganek committed Dec 9, 2022
1 parent 20c70b4 commit bfd68ac
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 28 deletions.
22 changes: 13 additions & 9 deletions tests/rust/src/bin/dangling_fd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
}

Expand Down
9 changes: 5 additions & 4 deletions tests/rust/src/bin/dangling_symlink.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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");
}
}

Expand Down
7 changes: 4 additions & 3 deletions tests/rust/src/bin/directory_seek.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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() {
Expand Down
5 changes: 3 additions & 2 deletions tests/rust/src/bin/fd_advise.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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();
Expand Down
5 changes: 3 additions & 2 deletions tests/rust/src/bin/fd_filestat_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion tests/rust/src/bin/fd_flags_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
38 changes: 31 additions & 7 deletions tests/rust/src/bin/fd_readdir.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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<DirEntry>, bool) {
let mut buf: [u8; BUF_LEN] = [0; BUF_LEN];
Expand All @@ -65,7 +84,8 @@ unsafe fn exec_fd_readdir(fd: wasi::Fd, cookie: wasi::Dircookie) -> (Vec<DirEntr
(dirs, eof)
}

unsafe fn test_fd_readdir(dir_fd: wasi::Fd) {
unsafe fn test_fd_readdir(base_dir_fd: wasi::Fd) {
let dir_fd = create_tmp_dir(base_dir_fd, "dir.cleanup");
let stat = wasi::fd_filestat_get(dir_fd).expect("failed filestat");

// Check the behavior in an empty directory
Expand Down Expand Up @@ -96,7 +116,7 @@ unsafe fn test_fd_readdir(dir_fd: wasi::Fd) {
let file_fd = wasi::path_open(
dir_fd,
0,
"file",
"file.cleanup",
wasi::OFLAGS_CREAT,
wasi::RIGHTS_FD_READ
| wasi::RIGHTS_FD_WRITE
Expand Down Expand Up @@ -130,7 +150,7 @@ unsafe fn test_fd_readdir(dir_fd: wasi::Fd) {
assert_eq!(dir.name, "..", "second name");
let dir = dirs.next().expect("third entry is None");
// check the file info
assert_eq!(dir.name, "file", "file name doesn't match");
assert_eq!(dir.name, "file.cleanup", "file name doesn't match");
assert_eq!(
dir.dirent.d_type,
wasi::FILETYPE_REGULAR_FILE,
Expand All @@ -144,16 +164,19 @@ unsafe fn test_fd_readdir(dir_fd: wasi::Fd) {
assert_eq!(dirs.len(), 1, "expected one entry");
assert_eq!(dirs[0].name, lastfile_name, "name of the only entry");

wasi::path_unlink_file(dir_fd, "file").expect("removing a file");
wasi::path_unlink_file(dir_fd, "file.cleanup").expect("removing a file");
wasi::path_remove_directory(base_dir_fd, "dir.cleanup").expect("removing a file");
}

unsafe fn test_fd_readdir_lots(dir_fd: wasi::Fd) {
unsafe fn test_fd_readdir_lots(base_dir_fd: wasi::Fd) {
let dir_fd = create_tmp_dir(base_dir_fd, "dir.cleanup");

// Add a file and check the behavior
for count in 0..1000 {
let file_fd = wasi::path_open(
dir_fd,
0,
&format!("file.{}", count),
&format!("file.{}.cleanup", count),
wasi::OFLAGS_CREAT,
wasi::RIGHTS_FD_READ
| wasi::RIGHTS_FD_WRITE
Expand Down Expand Up @@ -184,8 +207,9 @@ unsafe fn test_fd_readdir_lots(dir_fd: wasi::Fd) {
assert_eq!(total, 1002, "expected 1000 entries plus . and ..");

for count in 0..1000 {
wasi::path_unlink_file(dir_fd, &format!("file.{}", count)).expect("removing a file");
wasi::path_unlink_file(dir_fd, &format!("file.{}.cleanup", count)).expect("removing a file");
}
wasi::path_remove_directory(base_dir_fd, "dir.cleanup").expect("removing a file");
}

fn main() {
Expand Down

0 comments on commit bfd68ac

Please sign in to comment.