Update some more wasi test programs to 0.9.0

Largely the same as the previous update!
This commit is contained in:
Alex Crichton
2019-12-12 15:19:40 -08:00
committed by Jakub Konka
parent 054b79427e
commit d641e6e7b0
6 changed files with 109 additions and 262 deletions

View File

@@ -1,54 +1,33 @@
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_path_open;
use wasi_tests::open_scratch_directory_new;
unsafe fn test_dangling_fd(dir_fd: wasi_unstable::Fd) {
unsafe fn test_dangling_fd(dir_fd: wasi::Fd) {
// Create a file, open it, delete it without closing the handle,
// and then try creating it again
create_file(dir_fd, "file");
let mut file_fd = wasi_unstable::Fd::max_value() - 1;
let mut status = wasi_path_open(dir_fd, 0, "file", 0, 0, 0, 0, &mut file_fd);
assert_eq!(
status,
wasi_unstable::raw::__WASI_ESUCCESS,
"opening a file",
);
let fd = wasi::path_open(dir_fd, 0, "file", 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");
assert_gt!(
file_fd,
libc::STDERR_FILENO as wasi_unstable::Fd,
libc::STDERR_FILENO as wasi::Fd,
"file descriptor range check",
);
cleanup_file(dir_fd, "file");
create_file(dir_fd, "file");
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::fd_close(fd).unwrap();
// Now, repeat the same process but for a directory
create_dir(dir_fd, "subdir");
let mut subdir_fd = wasi_unstable::Fd::max_value() - 1;
status = wasi_path_open(
dir_fd,
0,
"subdir",
wasi_unstable::O_DIRECTORY,
0,
0,
0,
&mut subdir_fd,
);
assert_eq!(
status,
wasi_unstable::raw::__WASI_ESUCCESS,
"opening 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");
assert_gt!(
file_fd,
libc::STDERR_FILENO as wasi_unstable::Fd,
subdir_fd,
libc::STDERR_FILENO as wasi::Fd,
"file descriptor range check",
);
cleanup_dir(dir_fd, "subdir");
create_dir(dir_fd, "subdir");
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");
}
fn main() {
@@ -62,7 +41,7 @@ fn main() {
};
// Open scratch directory
let dir_fd = match open_scratch_directory(&arg) {
let dir_fd = match open_scratch_directory_new(&arg) {
Ok(dir_fd) => dir_fd,
Err(err) => {
eprintln!("{}", err);

View File

@@ -1,41 +1,25 @@
use std::{env, process};
use wasi_old::wasi_unstable;
use wasi_tests::open_scratch_directory;
use wasi_tests::utils::cleanup_file;
use wasi_tests::wasi_wrappers::{wasi_path_open, wasi_path_symlink};
use wasi_tests::open_scratch_directory_new;
unsafe fn test_dangling_symlink(dir_fd: wasi_unstable::Fd) {
unsafe fn test_dangling_symlink(dir_fd: wasi::Fd) {
// First create a dangling symlink.
assert!(
wasi_path_symlink("target", dir_fd, "symlink").is_ok(),
wasi::path_symlink("target", dir_fd, "symlink").is_ok(),
"creating a symlink"
);
// Try to open it as a directory with O_NOFOLLOW.
let mut file_fd: wasi_unstable::Fd = wasi_unstable::Fd::max_value() - 1;
let status = wasi_path_open(
dir_fd,
0,
"symlink",
wasi_unstable::O_DIRECTORY,
0,
0,
0,
&mut file_fd,
);
let status = wasi::path_open(dir_fd, 0, "symlink", wasi::OFLAGS_DIRECTORY, 0, 0, 0)
.err()
.expect("failed to open symlink");
assert_eq!(
status,
wasi_unstable::raw::__WASI_ELOOP,
status.raw_error(),
wasi::ERRNO_LOOP,
"opening a dangling symlink as a directory",
);
assert_eq!(
file_fd,
wasi_unstable::Fd::max_value(),
"failed open should set the file descriptor to -1",
);
// Clean up.
cleanup_file(dir_fd, "symlink");
wasi::path_unlink_file(dir_fd, "symlink").expect("failed to remove file");
}
fn main() {
@@ -49,7 +33,7 @@ fn main() {
};
// Open scratch directory
let dir_fd = match open_scratch_directory(&arg) {
let dir_fd = match open_scratch_directory_new(&arg) {
Ok(dir_fd) => dir_fd,
Err(err) => {
eprintln!("{}", err);

View File

@@ -1,68 +1,46 @@
use more_asserts::assert_gt;
use std::{env, mem, process};
use wasi_old::wasi_unstable;
use wasi_tests::open_scratch_directory;
use wasi_tests::utils::{cleanup_dir, close_fd, create_dir};
use wasi_tests::wasi_wrappers::{wasi_fd_fdstat_get, wasi_fd_seek, wasi_path_open};
use std::{env, process};
use wasi_tests::open_scratch_directory_new;
unsafe fn test_directory_seek(dir_fd: wasi_unstable::Fd) {
unsafe fn test_directory_seek(dir_fd: wasi::Fd) {
// Create a directory in the scratch directory.
create_dir(dir_fd, "dir");
wasi::path_create_directory(dir_fd, "dir").expect("failed to make directory");
// Open the directory and attempt to request rights for seeking.
let mut fd: wasi_unstable::Fd = wasi_unstable::Fd::max_value() - 1;
let mut status = wasi_path_open(
dir_fd,
0,
"dir",
0,
wasi_unstable::RIGHT_FD_SEEK,
0,
0,
&mut fd,
);
assert_eq!(
status,
wasi_unstable::raw::__WASI_ESUCCESS,
"opening a file"
);
let fd = wasi::path_open(dir_fd, 0, "dir", 0, wasi::RIGHTS_FD_SEEK, 0, 0)
.expect("failed to open file");
assert_gt!(
fd,
libc::STDERR_FILENO as wasi_unstable::Fd,
libc::STDERR_FILENO as wasi::Fd,
"file descriptor range check",
);
// Attempt to seek.
let mut newoffset = 1;
status = wasi_fd_seek(fd, 0, wasi_unstable::WHENCE_CUR, &mut newoffset);
let status = wasi::fd_seek(fd, 0, wasi::WHENCE_CUR)
.err()
.expect("failed to seek");
assert_eq!(
status,
wasi_unstable::raw::__WASI_ENOTCAPABLE,
status.raw_error(),
wasi::ERRNO_NOTCAPABLE,
"seek on a directory"
);
// Check if we obtained the right to seek.
let mut fdstat: wasi_unstable::FdStat = mem::zeroed();
status = wasi_fd_fdstat_get(fd, &mut fdstat);
assert_eq!(
status,
wasi_unstable::raw::__WASI_ESUCCESS,
"calling fd_fdstat on a directory"
);
let fdstat = wasi::fd_fdstat_get(fd).expect("failed to fdstat");
assert_eq!(
fdstat.fs_filetype,
wasi_unstable::FILETYPE_DIRECTORY,
wasi::FILETYPE_DIRECTORY,
"expected the scratch directory to be a directory",
);
assert_eq!(
(fdstat.fs_rights_base & wasi_unstable::RIGHT_FD_SEEK),
(fdstat.fs_rights_base & wasi::RIGHTS_FD_SEEK),
0,
"directory has the seek right",
);
// Clean up.
close_fd(fd);
cleanup_dir(dir_fd, "dir");
wasi::fd_close(fd).expect("failed to close fd");
wasi::path_remove_directory(dir_fd, "dir").expect("failed to remove dir");
}
fn main() {
@@ -76,7 +54,7 @@ fn main() {
};
// Open scratch directory
let dir_fd = match open_scratch_directory(&arg) {
let dir_fd = match open_scratch_directory_new(&arg) {
Ok(dir_fd) => dir_fd,
Err(err) => {
eprintln!("{}", err);

View File

@@ -1,78 +1,44 @@
use libc;
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_file, close_fd};
use wasi_tests::wasi_wrappers::{wasi_fd_advise, wasi_fd_filestat_get, wasi_path_open};
use wasi_tests::open_scratch_directory_new;
unsafe fn test_fd_advise(dir_fd: wasi_unstable::Fd) {
unsafe fn test_fd_advise(dir_fd: wasi::Fd) {
// Create a file in the scratch directory.
let mut file_fd = wasi_unstable::Fd::max_value() - 1;
let status = wasi_path_open(
let file_fd = wasi::path_open(
dir_fd,
0,
"file",
wasi_unstable::O_CREAT,
wasi_unstable::RIGHT_FD_READ | wasi_unstable::RIGHT_FD_WRITE,
wasi::OFLAGS_CREAT,
wasi::RIGHTS_FD_READ | wasi::RIGHTS_FD_WRITE,
0,
0,
&mut file_fd,
);
assert_eq!(
status,
wasi_unstable::raw::__WASI_ESUCCESS,
"opening a file"
);
)
.expect("failed to open file");
assert_gt!(
file_fd,
libc::STDERR_FILENO as wasi_unstable::Fd,
libc::STDERR_FILENO as wasi::Fd,
"file descriptor range check",
);
// Check file size
let mut stat = wasi_unstable::FileStat {
st_dev: 0,
st_ino: 0,
st_filetype: 0,
st_nlink: 0,
st_size: 0,
st_atim: 0,
st_mtim: 0,
st_ctim: 0,
};
let status = wasi_fd_filestat_get(file_fd, &mut stat);
assert_eq!(
status,
wasi_unstable::raw::__WASI_ESUCCESS,
"reading file stats"
);
assert_eq!(stat.st_size, 0, "file size should be 0");
let stat = wasi::fd_filestat_get(file_fd).expect("failed to fdstat");
assert_eq!(stat.size, 0, "file size should be 0");
// Allocate some size
assert!(
wasi_unstable::fd_allocate(file_fd, 0, 100).is_ok(),
wasi::fd_allocate(file_fd, 0, 100).is_ok(),
"allocating size"
);
let status = wasi_fd_filestat_get(file_fd, &mut stat);
assert_eq!(
status,
wasi_unstable::raw::__WASI_ESUCCESS,
"reading file stats after initial allocation"
);
assert_eq!(stat.st_size, 100, "file size should be 100");
let stat = wasi::fd_filestat_get(file_fd).expect("failed to fdstat 2");
assert_eq!(stat.size, 100, "file size should be 100");
// Advise the kernel
let status = wasi_fd_advise(file_fd, 10, 50, wasi_unstable::ADVICE_NORMAL);
assert_eq!(
status,
wasi_unstable::raw::__WASI_ESUCCESS,
"advising the kernel"
);
wasi::fd_advise(file_fd, 10, 50, wasi::ADVICE_NORMAL).expect("failed advise");
close_fd(file_fd);
cleanup_file(dir_fd, "file");
wasi::fd_close(file_fd).expect("failed to close");
wasi::path_unlink_file(dir_fd, "file").expect("failed to unlink");
}
fn main() {
let mut args = env::args();
@@ -85,7 +51,7 @@ fn main() {
};
// Open scratch directory
let dir_fd = match open_scratch_directory(&arg) {
let dir_fd = match open_scratch_directory_new(&arg) {
Ok(dir_fd) => dir_fd,
Err(err) => {
eprintln!("{}", err);

View File

@@ -1,100 +1,64 @@
use libc;
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_file, close_fd};
use wasi_tests::wasi_wrappers::{wasi_fd_filestat_get, wasi_path_open};
use wasi_tests::open_scratch_directory_new;
unsafe fn test_fd_filestat_set(dir_fd: wasi_unstable::Fd) {
unsafe fn test_fd_filestat_set(dir_fd: wasi::Fd) {
// Create a file in the scratch directory.
let mut file_fd = wasi_unstable::Fd::max_value() - 1;
let status = wasi_path_open(
let file_fd = wasi::path_open(
dir_fd,
0,
"file",
wasi_unstable::O_CREAT,
wasi_unstable::RIGHT_FD_READ | wasi_unstable::RIGHT_FD_WRITE,
wasi::OFLAGS_CREAT,
wasi::RIGHTS_FD_READ | wasi::RIGHTS_FD_WRITE,
0,
0,
&mut file_fd,
);
assert_eq!(
status,
wasi_unstable::raw::__WASI_ESUCCESS,
"opening a file"
);
).expect("failed to create file");
assert_gt!(
file_fd,
libc::STDERR_FILENO as wasi_unstable::Fd,
libc::STDERR_FILENO as wasi::Fd,
"file descriptor range check",
);
// Check file size
let mut stat = wasi_unstable::FileStat {
st_dev: 0,
st_ino: 0,
st_filetype: 0,
st_nlink: 0,
st_size: 0,
st_atim: 0,
st_mtim: 0,
st_ctim: 0,
};
let status = wasi_fd_filestat_get(file_fd, &mut stat);
assert_eq!(
status,
wasi_unstable::raw::__WASI_ESUCCESS,
"reading file stats"
);
assert_eq!(stat.st_size, 0, "file size should be 0");
let stat = wasi::fd_filestat_get(file_fd).expect("failed filestat");
assert_eq!(stat.size, 0, "file size should be 0");
// Check fd_filestat_set_size
assert!(
wasi_unstable::fd_filestat_set_size(file_fd, 100).is_ok(),
wasi::fd_filestat_set_size(file_fd, 100).is_ok(),
"fd_filestat_set_size"
);
let status = wasi_fd_filestat_get(file_fd, &mut stat);
assert_eq!(
status,
wasi_unstable::raw::__WASI_ESUCCESS,
"reading file stats after fd_filestat_set_size"
);
assert_eq!(stat.st_size, 100, "file size should be 100");
let stat = wasi::fd_filestat_get(file_fd).expect("failed filestat 2");
assert_eq!(stat.size, 100, "file size should be 100");
// Check fd_filestat_set_times
let old_atim = stat.st_atim;
let new_mtim = stat.st_mtim - 100;
let old_atim = stat.atim;
let new_mtim = stat.mtim - 100;
assert!(
wasi_unstable::fd_filestat_set_times(
wasi::fd_filestat_set_times(
file_fd,
new_mtim,
new_mtim,
wasi_unstable::FILESTAT_SET_MTIM,
wasi::FSTFLAGS_MTIM,
)
.is_ok(),
"fd_filestat_set_times"
);
let status = wasi_fd_filestat_get(file_fd, &mut stat);
let stat = wasi::fd_filestat_get(file_fd).expect("failed filestat 3");
assert_eq!(
status,
wasi_unstable::raw::__WASI_ESUCCESS,
"reading file stats after fd_filestat_set_times"
);
assert_eq!(
stat.st_size, 100,
stat.size, 100,
"file size should remain unchanged at 100"
);
assert_eq!(stat.st_mtim, new_mtim, "mtim should change");
assert_eq!(stat.st_atim, old_atim, "atim should not change");
assert_eq!(stat.mtim, new_mtim, "mtim should change");
assert_eq!(stat.atim, old_atim, "atim should not change");
// let status = wasi_fd_filestat_set_times(file_fd, new_mtim, new_mtim, wasi_unstable::FILESTAT_SET_MTIM | wasi_unstable::FILESTAT_SET_MTIM_NOW);
// assert_eq!(status, wasi_unstable::EINVAL, "ATIM & ATIM_NOW can't both be set");
// let status = wasi_fd_filestat_set_times(file_fd, new_mtim, new_mtim, wasi::FILESTAT_SET_MTIM | wasi::FILESTAT_SET_MTIM_NOW);
// assert_eq!(status, wasi::EINVAL, "ATIM & ATIM_NOW can't both be set");
close_fd(file_fd);
cleanup_file(dir_fd, "file");
wasi::fd_close(file_fd).expect("failed to close fd");
wasi::path_unlink_file(dir_fd, "file").expect("failed to remove dir");
}
fn main() {
let mut args = env::args();
@@ -107,7 +71,7 @@ fn main() {
};
// Open scratch directory
let dir_fd = match open_scratch_directory(&arg) {
let dir_fd = match open_scratch_directory_new(&arg) {
Ok(dir_fd) => dir_fd,
Err(err) => {
eprintln!("{}", err);

View File

@@ -1,15 +1,11 @@
use libc;
use more_asserts::assert_gt;
use std::{cmp::min, env, mem, process, slice, str};
use wasi_old::wasi_unstable;
use wasi_tests::open_scratch_directory;
use wasi_tests::wasi_wrappers::{wasi_fd_filestat_get, wasi_fd_readdir, wasi_path_open};
use wasi_tests::open_scratch_directory_new;
const BUF_LEN: usize = 256;
#[derive(Debug)]
struct DirEntry {
dirent: wasi_unstable::Dirent,
dirent: wasi::Dirent,
name: String,
}
@@ -35,7 +31,7 @@ impl<'a> Iterator for ReadDir<'a> {
}
// Read the data
let dirent_ptr = self.buf.as_ptr() as *const wasi_unstable::Dirent;
let dirent_ptr = self.buf.as_ptr() as *const wasi::Dirent;
let dirent = dirent_ptr.read_unaligned();
let name_ptr = dirent_ptr.offset(1) as *const u8;
// NOTE Linux syscall returns a NULL-terminated name, but WASI doesn't
@@ -53,30 +49,22 @@ impl<'a> Iterator for ReadDir<'a> {
}
unsafe fn exec_fd_readdir(
fd: wasi_unstable::Fd,
cookie: wasi_unstable::DirCookie,
fd: wasi::Fd,
cookie: wasi::Dircookie,
) -> Vec<DirEntry> {
let mut buf: [u8; BUF_LEN] = [0; BUF_LEN];
let mut bufused = 0;
let status = wasi_fd_readdir(fd, &mut buf, BUF_LEN, cookie, &mut bufused);
assert_eq!(status, wasi_unstable::raw::__WASI_ESUCCESS, "fd_readdir");
let bufused = wasi::fd_readdir(fd, buf.as_mut_ptr(), BUF_LEN, cookie).expect("failed fd_readdir");
let sl = slice::from_raw_parts(buf.as_ptr(), min(BUF_LEN, bufused));
let dirs: Vec<_> = ReadDir::from_slice(sl).collect();
dirs
}
unsafe fn test_fd_readdir(dir_fd: wasi_unstable::Fd) {
let mut stat: wasi_unstable::FileStat = mem::zeroed();
let status = wasi_fd_filestat_get(dir_fd, &mut stat);
assert_eq!(
status,
wasi_unstable::raw::__WASI_ESUCCESS,
"reading scratch directory stats"
);
unsafe fn test_fd_readdir(dir_fd: wasi::Fd) {
let stat = wasi::fd_filestat_get(dir_fd).expect("failed filestat");
// Check the behavior in an empty directory
let mut dirs = exec_fd_readdir(dir_fd, wasi_unstable::DIRCOOKIE_START);
let mut dirs = exec_fd_readdir(dir_fd, 0);
dirs.sort_by_key(|d| d.name.clone());
assert_eq!(dirs.len(), 2, "expected two entries in an empty directory");
let mut dirs = dirs.into_iter();
@@ -86,10 +74,10 @@ unsafe fn test_fd_readdir(dir_fd: wasi_unstable::Fd) {
assert_eq!(dir.name, ".", "first name");
assert_eq!(
dir.dirent.d_type,
wasi_unstable::FILETYPE_DIRECTORY,
wasi::FILETYPE_DIRECTORY,
"first type"
);
assert_eq!(dir.dirent.d_ino, stat.st_ino);
assert_eq!(dir.dirent.d_ino, stat.ino);
assert_eq!(dir.dirent.d_namlen, 1);
// the second entry should be `..`
@@ -97,7 +85,7 @@ unsafe fn test_fd_readdir(dir_fd: wasi_unstable::Fd) {
assert_eq!(dir.name, "..", "second name");
assert_eq!(
dir.dirent.d_type,
wasi_unstable::FILETYPE_DIRECTORY,
wasi::FILETYPE_DIRECTORY,
"second type"
);
@@ -107,37 +95,25 @@ unsafe fn test_fd_readdir(dir_fd: wasi_unstable::Fd) {
);
// Add a file and check the behavior
let mut file_fd = wasi_unstable::Fd::max_value() - 1;
let status = wasi_path_open(
let file_fd = wasi::path_open(
dir_fd,
0,
"file",
wasi_unstable::O_CREAT,
wasi_unstable::RIGHT_FD_READ | wasi_unstable::RIGHT_FD_WRITE,
wasi::OFLAGS_CREAT,
wasi::RIGHTS_FD_READ | wasi::RIGHTS_FD_WRITE,
0,
0,
&mut file_fd,
);
assert_eq!(
status,
wasi_unstable::raw::__WASI_ESUCCESS,
"opening a file"
);
).expect("failed to create file");
assert_gt!(
file_fd,
libc::STDERR_FILENO as wasi_unstable::Fd,
libc::STDERR_FILENO as wasi::Fd,
"file descriptor range check",
);
let status = wasi_fd_filestat_get(file_fd, &mut stat);
assert_eq!(
status,
wasi_unstable::raw::__WASI_ESUCCESS,
"reading file stats"
);
let stat = wasi::fd_filestat_get(file_fd).expect("failed filestat");
// Execute another readdir
let mut dirs = exec_fd_readdir(dir_fd, wasi_unstable::DIRCOOKIE_START);
let mut dirs = exec_fd_readdir(dir_fd, 0);
assert_eq!(dirs.len(), 3, "expected three entries");
// Save the data about the last entry. We need to do it before sorting.
let lastfile_cookie = dirs[1].dirent.d_next;
@@ -154,10 +130,10 @@ unsafe fn test_fd_readdir(dir_fd: wasi_unstable::Fd) {
assert_eq!(dir.name, "file", "file name doesn't match");
assert_eq!(
dir.dirent.d_type,
wasi_unstable::FILETYPE_REGULAR_FILE,
wasi::FILETYPE_REGULAR_FILE,
"type for the real file"
);
assert_eq!(dir.dirent.d_ino, stat.st_ino);
assert_eq!(dir.dirent.d_ino, stat.ino);
// check if cookie works as expected
let dirs = exec_fd_readdir(dir_fd, lastfile_cookie);
@@ -176,7 +152,7 @@ fn main() {
};
// Open scratch directory
let dir_fd = match open_scratch_directory(&arg) {
let dir_fd = match open_scratch_directory_new(&arg) {
Ok(dir_fd) => dir_fd,
Err(err) => {
eprintln!("{}", err);