Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
bytecodealliance
GitHub Repository: bytecodealliance/wasmtime
Path: blob/main/crates/wasi-common/tests/all/async_.rs
3124 views
1
use super::*;
2
use test_programs_artifacts::*;
3
use wasi_common::WasiCtx;
4
use wasi_common::tokio::{WasiCtxBuilder, add_to_linker};
5
6
foreach_p1!(assert_test_exists);
7
8
pub fn prepare_workspace(exe_name: &str) -> Result<TempDir> {
9
let prefix = format!("wasi_tokio_{exe_name}_");
10
let tempdir = tempfile::Builder::new().prefix(&prefix).tempdir()?;
11
Ok(tempdir)
12
}
13
14
async fn run(path: &str, inherit_stdio: bool) -> Result<()> {
15
let path = Path::new(path);
16
let name = path.file_stem().unwrap().to_str().unwrap();
17
let workspace = prepare_workspace(name)?;
18
let stdout = WritePipe::new_in_memory();
19
let stderr = WritePipe::new_in_memory();
20
let r = {
21
let engine = test_programs_artifacts::engine(|_config| {});
22
let mut linker = Linker::<WasiCtx>::new(&engine);
23
add_to_linker(&mut linker, |cx| cx)?;
24
25
// Create our wasi context.
26
// Additionally register any preopened directories if we have them.
27
let mut builder = WasiCtxBuilder::new();
28
29
if inherit_stdio {
30
builder.inherit_stdio();
31
} else {
32
builder
33
.stdout(Box::new(stdout.clone()))
34
.stderr(Box::new(stderr.clone()));
35
}
36
builder.arg(name)?.arg(".")?;
37
println!("preopen: {workspace:?}");
38
let preopen_dir =
39
cap_std::fs::Dir::open_ambient_dir(workspace.path(), cap_std::ambient_authority())?;
40
builder.preopened_dir(preopen_dir, ".")?;
41
for (var, val) in test_programs_artifacts::wasi_tests_environment() {
42
builder.env(var, val)?;
43
}
44
45
let mut store = Store::new(&engine, builder.build());
46
let module = Module::from_file(&engine, path)?;
47
let instance = linker.instantiate_async(&mut store, &module).await?;
48
let start = instance.get_typed_func::<(), ()>(&mut store, "_start")?;
49
start.call_async(&mut store, ()).await?;
50
Ok(())
51
};
52
53
r.map_err(move |trap: EnvError| {
54
let stdout = stdout
55
.try_into_inner()
56
.expect("sole ref to stdout")
57
.into_inner();
58
if !stdout.is_empty() {
59
println!("guest stdout:\n{}\n===", String::from_utf8_lossy(&stdout));
60
}
61
let stderr = stderr
62
.try_into_inner()
63
.expect("sole ref to stderr")
64
.into_inner();
65
if !stderr.is_empty() {
66
println!("guest stderr:\n{}\n===", String::from_utf8_lossy(&stderr));
67
}
68
trap.context(format!(
69
"error while testing wasi-tests {name} with cap-std-sync"
70
))
71
})?;
72
Ok(())
73
}
74
75
// Below here is mechanical: there should be one test for every binary in
76
// wasi-tests.
77
#[test_log::test(tokio::test(flavor = "multi_thread"))]
78
async fn p1_big_random_buf() {
79
run(P1_BIG_RANDOM_BUF, true).await.unwrap()
80
}
81
#[test_log::test(tokio::test(flavor = "multi_thread"))]
82
async fn p1_clock_time_get() {
83
run(P1_CLOCK_TIME_GET, true).await.unwrap()
84
}
85
#[test_log::test(tokio::test(flavor = "multi_thread"))]
86
async fn p1_close_preopen() {
87
run(P1_CLOSE_PREOPEN, true).await.unwrap()
88
}
89
#[test_log::test(tokio::test(flavor = "multi_thread"))]
90
async fn p1_dangling_fd() {
91
run(P1_DANGLING_FD, true).await.unwrap()
92
}
93
#[test_log::test(tokio::test(flavor = "multi_thread"))]
94
async fn p1_dangling_symlink() {
95
run(P1_DANGLING_SYMLINK, true).await.unwrap()
96
}
97
#[test_log::test(tokio::test(flavor = "multi_thread"))]
98
async fn p1_directory_seek() {
99
run(P1_DIRECTORY_SEEK, true).await.unwrap()
100
}
101
#[test_log::test(tokio::test(flavor = "multi_thread"))]
102
async fn p1_dir_fd_op_failures() {
103
run(P1_DIR_FD_OP_FAILURES, true).await.unwrap()
104
}
105
#[test_log::test(tokio::test(flavor = "multi_thread"))]
106
async fn p1_fd_advise() {
107
run(P1_FD_ADVISE, true).await.unwrap()
108
}
109
#[test_log::test(tokio::test(flavor = "multi_thread"))]
110
async fn p1_fd_filestat_get() {
111
run(P1_FD_FILESTAT_GET, true).await.unwrap()
112
}
113
#[test_log::test(tokio::test(flavor = "multi_thread"))]
114
async fn p1_fd_filestat_set() {
115
run(P1_FD_FILESTAT_SET, true).await.unwrap()
116
}
117
#[test_log::test(tokio::test(flavor = "multi_thread"))]
118
async fn p1_fd_flags_set() {
119
run(P1_FD_FLAGS_SET, true).await.unwrap()
120
}
121
#[test_log::test(tokio::test(flavor = "multi_thread"))]
122
async fn p1_fd_readdir() {
123
run(P1_FD_READDIR, true).await.unwrap()
124
}
125
#[test_log::test(tokio::test(flavor = "multi_thread"))]
126
async fn p1_file_allocate() {
127
run(P1_FILE_ALLOCATE, true).await.unwrap()
128
}
129
// see sync.rs for notes about ignores here
130
#[test_log::test(tokio::test(flavor = "multi_thread"))]
131
#[cfg_attr(not(target_os = "linux"), ignore)]
132
async fn p1_file_pread_pwrite() {
133
run(P1_FILE_PREAD_PWRITE, true).await.unwrap()
134
}
135
#[test_log::test(tokio::test(flavor = "multi_thread"))]
136
async fn p1_file_read_write() {
137
run(P1_FILE_READ_WRITE, true).await.unwrap()
138
}
139
#[test_log::test(tokio::test(flavor = "multi_thread"))]
140
async fn p1_file_seek_tell() {
141
run(P1_FILE_SEEK_TELL, true).await.unwrap()
142
}
143
#[test_log::test(tokio::test(flavor = "multi_thread"))]
144
async fn p1_file_truncation() {
145
run(P1_FILE_TRUNCATION, true).await.unwrap()
146
}
147
#[test_log::test(tokio::test(flavor = "multi_thread"))]
148
async fn p1_file_unbuffered_write() {
149
run(P1_FILE_UNBUFFERED_WRITE, true).await.unwrap()
150
}
151
#[test_log::test(tokio::test(flavor = "multi_thread"))]
152
async fn p1_interesting_paths() {
153
run(P1_INTERESTING_PATHS, true).await.unwrap()
154
}
155
#[test_log::test(tokio::test(flavor = "multi_thread"))]
156
async fn p1_regular_file_isatty() {
157
run(P1_REGULAR_FILE_ISATTY, false).await.unwrap()
158
}
159
#[test_log::test(tokio::test(flavor = "multi_thread"))]
160
async fn p1_nofollow_errors() {
161
run(P1_NOFOLLOW_ERRORS, true).await.unwrap()
162
}
163
#[test_log::test(tokio::test(flavor = "multi_thread"))]
164
async fn p1_overwrite_preopen() {
165
run(P1_OVERWRITE_PREOPEN, true).await.unwrap()
166
}
167
#[test_log::test(tokio::test(flavor = "multi_thread"))]
168
async fn p1_path_exists() {
169
run(P1_PATH_EXISTS, true).await.unwrap()
170
}
171
#[test_log::test(tokio::test(flavor = "multi_thread"))]
172
async fn p1_path_filestat() {
173
run(P1_PATH_FILESTAT, true).await.unwrap()
174
}
175
#[test_log::test(tokio::test(flavor = "multi_thread"))]
176
async fn p1_path_link() {
177
run(P1_PATH_LINK, true).await.unwrap()
178
}
179
#[test_log::test(tokio::test(flavor = "multi_thread"))]
180
async fn p1_path_open_create_existing() {
181
run(P1_PATH_OPEN_CREATE_EXISTING, true).await.unwrap()
182
}
183
#[test_log::test(tokio::test(flavor = "multi_thread"))]
184
async fn p1_path_open_read_write() {
185
run(P1_PATH_OPEN_READ_WRITE, true).await.unwrap()
186
}
187
#[test_log::test(tokio::test(flavor = "multi_thread"))]
188
async fn p1_path_open_dirfd_not_dir() {
189
run(P1_PATH_OPEN_DIRFD_NOT_DIR, true).await.unwrap()
190
}
191
#[test_log::test(tokio::test(flavor = "multi_thread"))]
192
async fn p1_path_open_missing() {
193
run(P1_PATH_OPEN_MISSING, true).await.unwrap()
194
}
195
#[test_log::test(tokio::test(flavor = "multi_thread"))]
196
async fn p1_path_open_nonblock() {
197
run(P1_PATH_OPEN_NONBLOCK, true).await.unwrap()
198
}
199
#[test_log::test(tokio::test(flavor = "multi_thread"))]
200
async fn p1_path_rename_dir_trailing_slashes() {
201
run(P1_PATH_RENAME_DIR_TRAILING_SLASHES, true)
202
.await
203
.unwrap()
204
}
205
#[test_log::test(tokio::test(flavor = "multi_thread"))]
206
async fn p1_path_rename() {
207
run(P1_PATH_RENAME, true).await.unwrap()
208
}
209
#[test_log::test(tokio::test(flavor = "multi_thread"))]
210
async fn p1_path_symlink_trailing_slashes() {
211
run(P1_PATH_SYMLINK_TRAILING_SLASHES, true).await.unwrap()
212
}
213
#[test_log::test(tokio::test(flavor = "multi_thread"))]
214
async fn p1_poll_oneoff_files() {
215
run(P1_POLL_ONEOFF_FILES, false).await.unwrap()
216
}
217
#[test_log::test(tokio::test(flavor = "multi_thread"))]
218
async fn p1_poll_oneoff_stdio() {
219
run(P1_POLL_ONEOFF_STDIO, true).await.unwrap()
220
}
221
#[test_log::test(tokio::test(flavor = "multi_thread"))]
222
async fn p1_readlink() {
223
run(P1_READLINK, true).await.unwrap()
224
}
225
#[test_log::test(tokio::test(flavor = "multi_thread"))]
226
async fn p1_remove_directory() {
227
run(P1_REMOVE_DIRECTORY, true).await.unwrap()
228
}
229
#[test_log::test(tokio::test(flavor = "multi_thread"))]
230
async fn p1_remove_nonempty_directory() {
231
run(P1_REMOVE_NONEMPTY_DIRECTORY, true).await.unwrap()
232
}
233
#[test_log::test(tokio::test(flavor = "multi_thread"))]
234
async fn p1_renumber() {
235
run(P1_RENUMBER, true).await.unwrap()
236
}
237
#[test_log::test(tokio::test(flavor = "multi_thread"))]
238
async fn p1_sched_yield() {
239
run(P1_SCHED_YIELD, true).await.unwrap()
240
}
241
#[test_log::test(tokio::test(flavor = "multi_thread"))]
242
async fn p1_stdio() {
243
run(P1_STDIO, true).await.unwrap()
244
}
245
#[test_log::test(tokio::test(flavor = "multi_thread"))]
246
async fn p1_stdio_isatty() {
247
// Only a valid test if the host executable's stdio is a terminal:
248
if test_programs_artifacts::stdio_is_terminal() {
249
// Inherit stdio, test asserts it is a tty:
250
run(P1_STDIO_ISATTY, true).await.unwrap()
251
}
252
}
253
#[test_log::test(tokio::test(flavor = "multi_thread"))]
254
async fn p1_stdio_not_isatty() {
255
// Don't inherit stdio, test asserts each is not tty:
256
run(P1_STDIO_NOT_ISATTY, false).await.unwrap()
257
}
258
#[test_log::test(tokio::test(flavor = "multi_thread"))]
259
async fn p1_symlink_create() {
260
run(P1_SYMLINK_CREATE, true).await.unwrap()
261
}
262
#[test_log::test(tokio::test(flavor = "multi_thread"))]
263
async fn p1_symlink_filestat() {
264
run(P1_SYMLINK_FILESTAT, true).await.unwrap()
265
}
266
#[test_log::test(tokio::test(flavor = "multi_thread"))]
267
async fn p1_symlink_loop() {
268
run(P1_SYMLINK_LOOP, true).await.unwrap()
269
}
270
#[test_log::test(tokio::test(flavor = "multi_thread"))]
271
async fn p1_unlink_file_trailing_slashes() {
272
run(P1_UNLINK_FILE_TRAILING_SLASHES, true).await.unwrap()
273
}
274
#[test_log::test(tokio::test(flavor = "multi_thread"))]
275
async fn p1_path_open_preopen() {
276
run(P1_PATH_OPEN_PREOPEN, true).await.unwrap()
277
}
278
#[test_log::test(tokio::test(flavor = "multi_thread"))]
279
async fn p1_unicode_output() {
280
run(P1_UNICODE_OUTPUT, true).await.unwrap()
281
}
282
#[test_log::test(tokio::test(flavor = "multi_thread"))]
283
async fn p1_file_write() {
284
run(P1_FILE_WRITE, true).await.unwrap()
285
}
286
#[test_log::test(tokio::test(flavor = "multi_thread"))]
287
async fn p1_path_open_lots() {
288
run(P1_PATH_OPEN_LOTS, true).await.unwrap()
289
}
290
291
#[expect(
292
dead_code,
293
reason = "tested in the wasi-cli crate, satisfying foreach_api! macro"
294
)]
295
fn p1_cli_much_stdout() {}
296
297