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,110 +1,71 @@
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_pread, wasi_fd_pwrite, wasi_path_open};
unsafe fn test_file_pread_pwrite(dir_fd: wasi_unstable::Fd) {
unsafe fn test_file_pread_pwrite(dir_fd: wasi::Fd) {
// Create a file in the scratch directory.
let mut file_fd = wasi_unstable::Fd::max_value() - 1;
let mut 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_SEEK | wasi_unstable::RIGHT_FD_WRITE,
wasi::OFLAGS_CREAT,
wasi::RIGHTS_FD_READ | wasi::RIGHTS_FD_SEEK | 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",
);
let contents = &[0u8, 1, 2, 3];
let ciovec = wasi_unstable::CIoVec {
buf: contents.as_ptr() as *const libc::c_void,
let ciovec = wasi::Ciovec {
buf: contents.as_ptr() as *const _,
buf_len: contents.len(),
};
let mut nwritten = 0;
status = wasi_fd_pwrite(file_fd, &mut [ciovec], 0, &mut nwritten);
assert_eq!(
status,
wasi_unstable::raw::__WASI_ESUCCESS,
"writing bytes at offset 0"
);
let mut nwritten =
wasi::fd_pwrite(file_fd, &mut [ciovec], 0).expect("writing bytes at offset 0");
assert_eq!(nwritten, 4, "nwritten bytes check");
let contents = &mut [0u8; 4];
let iovec = wasi_unstable::IoVec {
buf: contents.as_mut_ptr() as *mut libc::c_void,
let iovec = wasi::Iovec {
buf: contents.as_mut_ptr() as *mut _,
buf_len: contents.len(),
};
let mut nread = 0;
status = wasi_fd_pread(file_fd, &[iovec], 0, &mut nread);
assert_eq!(
status,
wasi_unstable::raw::__WASI_ESUCCESS,
"reading bytes at offset 0"
);
let mut nread = wasi::fd_pread(file_fd, &[iovec], 0).expect("reading bytes at offset 0");
assert_eq!(nread, 4, "nread bytes check");
assert_eq!(contents, &[0u8, 1, 2, 3], "written bytes equal read bytes");
let contents = &mut [0u8; 4];
let iovec = wasi_unstable::IoVec {
buf: contents.as_mut_ptr() as *mut libc::c_void,
let iovec = wasi::Iovec {
buf: contents.as_mut_ptr() as *mut _,
buf_len: contents.len(),
};
let mut nread = 0;
status = wasi_fd_pread(file_fd, &[iovec], 2, &mut nread);
assert_eq!(
status,
wasi_unstable::raw::__WASI_ESUCCESS,
"reading bytes at offset 2"
);
nread = wasi::fd_pread(file_fd, &[iovec], 2).expect("reading bytes at offset 2");
assert_eq!(nread, 2, "nread bytes check");
assert_eq!(contents, &[2u8, 3, 0, 0], "file cursor was overwritten");
let contents = &[1u8, 0];
let ciovec = wasi_unstable::CIoVec {
buf: contents.as_ptr() as *const libc::c_void,
let ciovec = wasi::Ciovec {
buf: contents.as_ptr() as *const _,
buf_len: contents.len(),
};
let mut nwritten = 0;
status = wasi_fd_pwrite(file_fd, &mut [ciovec], 2, &mut nwritten);
assert_eq!(
status,
wasi_unstable::raw::__WASI_ESUCCESS,
"writing bytes at offset 2"
);
nwritten = wasi::fd_pwrite(file_fd, &mut [ciovec], 2).expect("writing bytes at offset 2");
assert_eq!(nwritten, 2, "nwritten bytes check");
let contents = &mut [0u8; 4];
let iovec = wasi_unstable::IoVec {
buf: contents.as_mut_ptr() as *mut libc::c_void,
let iovec = wasi::Iovec {
buf: contents.as_mut_ptr() as *mut _,
buf_len: contents.len(),
};
let mut nread = 0;
status = wasi_fd_pread(file_fd, &[iovec], 0, &mut nread);
assert_eq!(
status,
wasi_unstable::raw::__WASI_ESUCCESS,
"reading bytes at offset 0"
);
nread = wasi::fd_pread(file_fd, &[iovec], 0).expect("reading bytes at offset 0");
assert_eq!(nread, 4, "nread bytes check");
assert_eq!(contents, &[0u8, 1, 1, 0], "file cursor was overwritten");
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() {