Skip to content

Commit 51f3ac0

Browse files
Jakub Konkasunfishcode
Jakub Konka
authored andcommitted
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.
1 parent 907e7aa commit 51f3ac0

37 files changed

+855
-1853
lines changed

crates/test-programs/tests/wasm_tests/runtime.rs

Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -55,41 +55,14 @@ pub fn instantiate(data: &[u8], bin_name: &str, workspace: Option<&Path>) -> any
5555
.context("failed to instantiate wasi")?,
5656
);
5757

58-
// ... and then do the same as above but for the old snapshot of wasi, since
59-
// a few tests still test that
60-
let mut builder = wasi_common::old::snapshot_0::WasiCtxBuilder::new()
61-
.arg(bin_name)
62-
.arg(".")
63-
.inherit_stdio();
64-
for (dir, file) in get_preopens(workspace)? {
65-
builder = builder.preopened_dir(file, dir);
66-
}
67-
let (reader, _writer) = os_pipe::pipe()?;
68-
builder = builder.stdin(reader_to_file(reader));
69-
let snapshot0 = Instance::from_handle(
70-
&store,
71-
wasmtime_wasi::old::snapshot_0::instantiate_wasi_with_context(
72-
global_exports.clone(),
73-
builder.build().context("failed to build wasi context")?,
74-
)
75-
.context("failed to instantiate wasi")?,
76-
);
77-
7858
let module = HostRef::new(Module::new(&store, &data).context("failed to create wasm module")?);
7959
let imports = module
8060
.borrow()
8161
.imports()
8262
.iter()
8363
.map(|i| {
84-
let instance = if i.module() == "wasi_unstable" {
85-
&snapshot0
86-
} else if i.module() == "wasi_snapshot_preview1" {
87-
&snapshot1
88-
} else {
89-
bail!("import module {} was not found", i.module())
90-
};
9164
let field_name = i.name();
92-
if let Some(export) = instance.find_export_by_name(field_name) {
65+
if let Some(export) = snapshot1.find_export_by_name(field_name) {
9366
Ok(export.clone())
9467
} else {
9568
bail!(

crates/test-programs/wasi-tests/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ publish = false
99
[dependencies]
1010
libc = "0.2.65"
1111
wasi = "0.9.0"
12-
wasi-old = { version = "0.7.0", package = "wasi" }
1312
more-asserts = "0.2.1"
1413

1514
# This crate is built with the wasm32-wasi target, so it's separate

crates/test-programs/wasi-tests/src/bin/close_preopen.rs

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use more_asserts::assert_gt;
22
use std::{env, process};
3-
use wasi_tests::open_scratch_directory_new;
3+
use wasi_tests::open_scratch_directory;
44

55
unsafe fn test_close_preopen(dir_fd: wasi::Fd) {
66
let pre_fd: wasi::Fd = (libc::STDERR_FILENO + 1) as wasi::Fd;
@@ -9,16 +9,20 @@ unsafe fn test_close_preopen(dir_fd: wasi::Fd) {
99

1010
// Try to close a preopened directory handle.
1111
assert_eq!(
12-
wasi::fd_close(pre_fd).unwrap_err().raw_error(),
12+
wasi::fd_close(pre_fd)
13+
.expect_err("closing a preopened file descriptor")
14+
.raw_error(),
1315
wasi::ERRNO_NOTSUP,
14-
"closing a preopened file descriptor",
16+
"errno should ERRNO_NOTSUP",
1517
);
1618

1719
// Try to renumber over a preopened directory handle.
1820
assert_eq!(
19-
wasi::fd_renumber(dir_fd, pre_fd).unwrap_err().raw_error(),
21+
wasi::fd_renumber(dir_fd, pre_fd)
22+
.expect_err("renumbering over a preopened file descriptor")
23+
.raw_error(),
2024
wasi::ERRNO_NOTSUP,
21-
"renumbering over a preopened file descriptor",
25+
"errno should be ERRNO_NOTSUP",
2226
);
2327

2428
// Ensure that dir_fd is still open.
@@ -31,9 +35,11 @@ unsafe fn test_close_preopen(dir_fd: wasi::Fd) {
3135

3236
// Try to renumber a preopened directory handle.
3337
assert_eq!(
34-
wasi::fd_renumber(pre_fd, dir_fd).unwrap_err().raw_error(),
38+
wasi::fd_renumber(pre_fd, dir_fd)
39+
.expect_err("renumbering over a preopened file descriptor")
40+
.raw_error(),
3541
wasi::ERRNO_NOTSUP,
36-
"renumbering over a preopened file descriptor",
42+
"errno should be ERRNO_NOTSUP",
3743
);
3844

3945
// Ensure that dir_fd is still open.
@@ -56,7 +62,7 @@ fn main() {
5662
};
5763

5864
// Open scratch directory
59-
let dir_fd = match open_scratch_directory_new(&arg) {
65+
let dir_fd = match open_scratch_directory(&arg) {
6066
Ok(dir_fd) => dir_fd,
6167
Err(err) => {
6268
eprintln!("{}", err);

crates/test-programs/wasi-tests/src/bin/dangling_fd.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use more_asserts::assert_gt;
22
use std::{env, process};
3-
use wasi_tests::open_scratch_directory_new;
3+
use wasi_tests::open_scratch_directory;
44

55
unsafe fn test_dangling_fd(dir_fd: wasi::Fd) {
66
// Create a file, open it, delete it without closing the handle,
@@ -41,7 +41,7 @@ fn main() {
4141
};
4242

4343
// Open scratch directory
44-
let dir_fd = match open_scratch_directory_new(&arg) {
44+
let dir_fd = match open_scratch_directory(&arg) {
4545
Ok(dir_fd) => dir_fd,
4646
Err(err) => {
4747
eprintln!("{}", err);

crates/test-programs/wasi-tests/src/bin/dangling_symlink.rs

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,17 @@
11
use std::{env, process};
2-
use wasi_tests::open_scratch_directory_new;
2+
use wasi_tests::open_scratch_directory;
33

44
unsafe fn test_dangling_symlink(dir_fd: wasi::Fd) {
55
// First create a dangling symlink.
6-
assert!(
7-
wasi::path_symlink("target", dir_fd, "symlink").is_ok(),
8-
"creating a symlink"
9-
);
6+
wasi::path_symlink("target", dir_fd, "symlink").expect("creating a symlink");
107

118
// Try to open it as a directory with O_NOFOLLOW.
12-
let status = wasi::path_open(dir_fd, 0, "symlink", wasi::OFLAGS_DIRECTORY, 0, 0, 0)
13-
.err()
14-
.expect("failed to open symlink");
159
assert_eq!(
16-
status.raw_error(),
10+
wasi::path_open(dir_fd, 0, "symlink", wasi::OFLAGS_DIRECTORY, 0, 0, 0)
11+
.expect_err("opening a dangling symlink as a directory")
12+
.raw_error(),
1713
wasi::ERRNO_LOOP,
18-
"opening a dangling symlink as a directory",
14+
"errno should be ERRNO_LOOP",
1915
);
2016

2117
// Clean up.
@@ -33,7 +29,7 @@ fn main() {
3329
};
3430

3531
// Open scratch directory
36-
let dir_fd = match open_scratch_directory_new(&arg) {
32+
let dir_fd = match open_scratch_directory(&arg) {
3733
Ok(dir_fd) => dir_fd,
3834
Err(err) => {
3935
eprintln!("{}", err);

crates/test-programs/wasi-tests/src/bin/directory_seek.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use more_asserts::assert_gt;
22
use std::{env, process};
3-
use wasi_tests::open_scratch_directory_new;
3+
use wasi_tests::open_scratch_directory;
44

55
unsafe fn test_directory_seek(dir_fd: wasi::Fd) {
66
// Create a directory in the scratch directory.
@@ -16,13 +16,12 @@ unsafe fn test_directory_seek(dir_fd: wasi::Fd) {
1616
);
1717

1818
// Attempt to seek.
19-
let status = wasi::fd_seek(fd, 0, wasi::WHENCE_CUR)
20-
.err()
21-
.expect("failed to seek");
2219
assert_eq!(
23-
status.raw_error(),
20+
wasi::fd_seek(fd, 0, wasi::WHENCE_CUR)
21+
.expect_err("seek on a directory")
22+
.raw_error(),
2423
wasi::ERRNO_NOTCAPABLE,
25-
"seek on a directory"
24+
"errno should be ERRNO_NOTCAPABLE"
2625
);
2726

2827
// Check if we obtained the right to seek.
@@ -54,7 +53,7 @@ fn main() {
5453
};
5554

5655
// Open scratch directory
57-
let dir_fd = match open_scratch_directory_new(&arg) {
56+
let dir_fd = match open_scratch_directory(&arg) {
5857
Ok(dir_fd) => dir_fd,
5958
Err(err) => {
6059
eprintln!("{}", err);

crates/test-programs/wasi-tests/src/bin/fd_advise.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
use libc;
21
use more_asserts::assert_gt;
32
use std::{env, process};
4-
use wasi_tests::open_scratch_directory_new;
3+
use wasi_tests::open_scratch_directory;
54

65
unsafe fn test_fd_advise(dir_fd: wasi::Fd) {
76
// Create a file in the scratch directory.
@@ -26,10 +25,7 @@ unsafe fn test_fd_advise(dir_fd: wasi::Fd) {
2625
assert_eq!(stat.size, 0, "file size should be 0");
2726

2827
// Allocate some size
29-
assert!(
30-
wasi::fd_allocate(file_fd, 0, 100).is_ok(),
31-
"allocating size"
32-
);
28+
wasi::fd_allocate(file_fd, 0, 100).expect("allocating size");
3329

3430
let stat = wasi::fd_filestat_get(file_fd).expect("failed to fdstat 2");
3531
assert_eq!(stat.size, 100, "file size should be 100");
@@ -51,7 +47,7 @@ fn main() {
5147
};
5248

5349
// Open scratch directory
54-
let dir_fd = match open_scratch_directory_new(&arg) {
50+
let dir_fd = match open_scratch_directory(&arg) {
5551
Ok(dir_fd) => dir_fd,
5652
Err(err) => {
5753
eprintln!("{}", err);

crates/test-programs/wasi-tests/src/bin/fd_filestat_set.rs

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use more_asserts::assert_gt;
22
use std::{env, process};
3-
use wasi_tests::open_scratch_directory_new;
3+
use wasi_tests::open_scratch_directory;
44

55
unsafe fn test_fd_filestat_set(dir_fd: wasi::Fd) {
66
// Create a file in the scratch directory.
@@ -12,7 +12,8 @@ unsafe fn test_fd_filestat_set(dir_fd: wasi::Fd) {
1212
wasi::RIGHTS_FD_READ | wasi::RIGHTS_FD_WRITE,
1313
0,
1414
0,
15-
).expect("failed to create file");
15+
)
16+
.expect("failed to create file");
1617
assert_gt!(
1718
file_fd,
1819
libc::STDERR_FILENO as wasi::Fd,
@@ -24,33 +25,19 @@ unsafe fn test_fd_filestat_set(dir_fd: wasi::Fd) {
2425
assert_eq!(stat.size, 0, "file size should be 0");
2526

2627
// Check fd_filestat_set_size
27-
assert!(
28-
wasi::fd_filestat_set_size(file_fd, 100).is_ok(),
29-
"fd_filestat_set_size"
30-
);
28+
wasi::fd_filestat_set_size(file_fd, 100).expect("fd_filestat_set_size");
3129

3230
let stat = wasi::fd_filestat_get(file_fd).expect("failed filestat 2");
3331
assert_eq!(stat.size, 100, "file size should be 100");
3432

3533
// Check fd_filestat_set_times
3634
let old_atim = stat.atim;
3735
let new_mtim = stat.mtim - 100;
38-
assert!(
39-
wasi::fd_filestat_set_times(
40-
file_fd,
41-
new_mtim,
42-
new_mtim,
43-
wasi::FSTFLAGS_MTIM,
44-
)
45-
.is_ok(),
46-
"fd_filestat_set_times"
47-
);
36+
wasi::fd_filestat_set_times(file_fd, new_mtim, new_mtim, wasi::FSTFLAGS_MTIM)
37+
.expect("fd_filestat_set_times");
4838

4939
let stat = wasi::fd_filestat_get(file_fd).expect("failed filestat 3");
50-
assert_eq!(
51-
stat.size, 100,
52-
"file size should remain unchanged at 100"
53-
);
40+
assert_eq!(stat.size, 100, "file size should remain unchanged at 100");
5441
assert_eq!(stat.mtim, new_mtim, "mtim should change");
5542
assert_eq!(stat.atim, old_atim, "atim should not change");
5643

@@ -71,7 +58,7 @@ fn main() {
7158
};
7259

7360
// Open scratch directory
74-
let dir_fd = match open_scratch_directory_new(&arg) {
61+
let dir_fd = match open_scratch_directory(&arg) {
7562
Ok(dir_fd) => dir_fd,
7663
Err(err) => {
7764
eprintln!("{}", err);

crates/test-programs/wasi-tests/src/bin/fd_readdir.rs

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use more_asserts::assert_gt;
22
use std::{cmp::min, env, mem, process, slice, str};
3-
use wasi_tests::open_scratch_directory_new;
3+
use wasi_tests::open_scratch_directory;
44

55
const BUF_LEN: usize = 256;
66

@@ -48,12 +48,10 @@ impl<'a> Iterator for ReadDir<'a> {
4848
}
4949
}
5050

51-
unsafe fn exec_fd_readdir(
52-
fd: wasi::Fd,
53-
cookie: wasi::Dircookie,
54-
) -> Vec<DirEntry> {
51+
unsafe fn exec_fd_readdir(fd: wasi::Fd, cookie: wasi::Dircookie) -> Vec<DirEntry> {
5552
let mut buf: [u8; BUF_LEN] = [0; BUF_LEN];
56-
let bufused = wasi::fd_readdir(fd, buf.as_mut_ptr(), BUF_LEN, cookie).expect("failed fd_readdir");
53+
let bufused =
54+
wasi::fd_readdir(fd, buf.as_mut_ptr(), BUF_LEN, cookie).expect("failed fd_readdir");
5755

5856
let sl = slice::from_raw_parts(buf.as_ptr(), min(BUF_LEN, bufused));
5957
let dirs: Vec<_> = ReadDir::from_slice(sl).collect();
@@ -72,22 +70,14 @@ unsafe fn test_fd_readdir(dir_fd: wasi::Fd) {
7270
// the first entry should be `.`
7371
let dir = dirs.next().expect("first entry is None");
7472
assert_eq!(dir.name, ".", "first name");
75-
assert_eq!(
76-
dir.dirent.d_type,
77-
wasi::FILETYPE_DIRECTORY,
78-
"first type"
79-
);
73+
assert_eq!(dir.dirent.d_type, wasi::FILETYPE_DIRECTORY, "first type");
8074
assert_eq!(dir.dirent.d_ino, stat.ino);
8175
assert_eq!(dir.dirent.d_namlen, 1);
8276

8377
// the second entry should be `..`
8478
let dir = dirs.next().expect("second entry is None");
8579
assert_eq!(dir.name, "..", "second name");
86-
assert_eq!(
87-
dir.dirent.d_type,
88-
wasi::FILETYPE_DIRECTORY,
89-
"second type"
90-
);
80+
assert_eq!(dir.dirent.d_type, wasi::FILETYPE_DIRECTORY, "second type");
9181

9282
assert!(
9383
dirs.next().is_none(),
@@ -103,7 +93,8 @@ unsafe fn test_fd_readdir(dir_fd: wasi::Fd) {
10393
wasi::RIGHTS_FD_READ | wasi::RIGHTS_FD_WRITE,
10494
0,
10595
0,
106-
).expect("failed to create file");
96+
)
97+
.expect("failed to create file");
10798
assert_gt!(
10899
file_fd,
109100
libc::STDERR_FILENO as wasi::Fd,
@@ -152,7 +143,7 @@ fn main() {
152143
};
153144

154145
// Open scratch directory
155-
let dir_fd = match open_scratch_directory_new(&arg) {
146+
let dir_fd = match open_scratch_directory(&arg) {
156147
Ok(dir_fd) => dir_fd,
157148
Err(err) => {
158149
eprintln!("{}", err);

0 commit comments

Comments
 (0)