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:
Jakub Konka
2019-12-24 22:04:15 +01:00
committed by Dan Gohman
parent 907e7aac01
commit 51f3ac0c45
37 changed files with 862 additions and 1860 deletions

View File

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