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,103 +1,46 @@
|
||||
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};
|
||||
|
||||
unsafe fn test_file_allocate(dir_fd: wasi_unstable::Fd) {
|
||||
unsafe fn test_file_allocate(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("opening a 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 mut stat = wasi::fd_filestat_get(file_fd).expect("reading file stats");
|
||||
assert_eq!(stat.size, 0, "file size should be 0");
|
||||
|
||||
// Allocate some size
|
||||
assert!(
|
||||
wasi_unstable::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");
|
||||
wasi::fd_allocate(file_fd, 0, 100).expect("allocating size");
|
||||
stat = wasi::fd_filestat_get(file_fd).expect("reading file stats");
|
||||
assert_eq!(stat.size, 100, "file size should be 100");
|
||||
|
||||
// Allocate should not modify if less than current size
|
||||
assert!(
|
||||
wasi_unstable::fd_allocate(file_fd, 10, 10).is_ok(),
|
||||
"allocating size less than current size"
|
||||
);
|
||||
|
||||
let status = wasi_fd_filestat_get(file_fd, &mut stat);
|
||||
assert_eq!(
|
||||
status,
|
||||
wasi_unstable::raw::__WASI_ESUCCESS,
|
||||
"reading file stats after additional allocation was not required"
|
||||
);
|
||||
assert_eq!(
|
||||
stat.st_size, 100,
|
||||
"file size should remain unchanged at 100"
|
||||
);
|
||||
wasi::fd_allocate(file_fd, 10, 10).expect("allocating size less than current size");
|
||||
stat = wasi::fd_filestat_get(file_fd).expect("reading file stats");
|
||||
assert_eq!(stat.size, 100, "file size should remain unchanged at 100");
|
||||
|
||||
// Allocate should modify if offset+len > current_len
|
||||
assert!(
|
||||
wasi_unstable::fd_allocate(file_fd, 90, 20).is_ok(),
|
||||
"allocating size larger than current size"
|
||||
);
|
||||
wasi::fd_allocate(file_fd, 90, 20).expect("allocating size larger than current size");
|
||||
stat = wasi::fd_filestat_get(file_fd).expect("reading file stats");
|
||||
assert_eq!(stat.size, 110, "file size should increase from 100 to 110");
|
||||
|
||||
let status = wasi_fd_filestat_get(file_fd, &mut stat);
|
||||
assert_eq!(
|
||||
status,
|
||||
wasi_unstable::raw::__WASI_ESUCCESS,
|
||||
"reading file stats after additional allocation was required"
|
||||
);
|
||||
assert_eq!(
|
||||
stat.st_size, 110,
|
||||
"file size should increase from 100 to 110"
|
||||
);
|
||||
|
||||
close_fd(file_fd);
|
||||
cleanup_file(dir_fd, "file");
|
||||
wasi::fd_close(file_fd).expect("closing a file");
|
||||
wasi::path_unlink_file(dir_fd, "file").expect("removing a file");
|
||||
}
|
||||
|
||||
fn main() {
|
||||
|
||||
Reference in New Issue
Block a user