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