Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
bytecodealliance
GitHub Repository: bytecodealliance/wasmtime
Path: blob/main/crates/wasi-common/tests/all/async_.rs
1692 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_preview1!(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
config.async_support(true);
23
});
24
let mut linker = Linker::<WasiCtx>::new(&engine);
25
add_to_linker(&mut linker, |cx| cx)?;
26
27
// Create our wasi context.
28
// Additionally register any preopened directories if we have them.
29
let mut builder = WasiCtxBuilder::new();
30
31
if inherit_stdio {
32
builder.inherit_stdio();
33
} else {
34
builder
35
.stdout(Box::new(stdout.clone()))
36
.stderr(Box::new(stderr.clone()));
37
}
38
builder.arg(name)?.arg(".")?;
39
println!("preopen: {workspace:?}");
40
let preopen_dir =
41
cap_std::fs::Dir::open_ambient_dir(workspace.path(), cap_std::ambient_authority())?;
42
builder.preopened_dir(preopen_dir, ".")?;
43
for (var, val) in test_programs_artifacts::wasi_tests_environment() {
44
builder.env(var, val)?;
45
}
46
47
let mut store = Store::new(&engine, builder.build());
48
let module = Module::from_file(&engine, path)?;
49
let instance = linker.instantiate_async(&mut store, &module).await?;
50
let start = instance.get_typed_func::<(), ()>(&mut store, "_start")?;
51
start.call_async(&mut store, ()).await?;
52
Ok(())
53
};
54
55
r.map_err(move |trap: anyhow::Error| {
56
let stdout = stdout
57
.try_into_inner()
58
.expect("sole ref to stdout")
59
.into_inner();
60
if !stdout.is_empty() {
61
println!("guest stdout:\n{}\n===", String::from_utf8_lossy(&stdout));
62
}
63
let stderr = stderr
64
.try_into_inner()
65
.expect("sole ref to stderr")
66
.into_inner();
67
if !stderr.is_empty() {
68
println!("guest stderr:\n{}\n===", String::from_utf8_lossy(&stderr));
69
}
70
trap.context(format!(
71
"error while testing wasi-tests {name} with cap-std-sync"
72
))
73
})?;
74
Ok(())
75
}
76
77
// Below here is mechanical: there should be one test for every binary in
78
// wasi-tests.
79
#[test_log::test(tokio::test(flavor = "multi_thread"))]
80
async fn preview1_big_random_buf() {
81
run(PREVIEW1_BIG_RANDOM_BUF, true).await.unwrap()
82
}
83
#[test_log::test(tokio::test(flavor = "multi_thread"))]
84
async fn preview1_clock_time_get() {
85
run(PREVIEW1_CLOCK_TIME_GET, true).await.unwrap()
86
}
87
#[test_log::test(tokio::test(flavor = "multi_thread"))]
88
async fn preview1_close_preopen() {
89
run(PREVIEW1_CLOSE_PREOPEN, true).await.unwrap()
90
}
91
#[test_log::test(tokio::test(flavor = "multi_thread"))]
92
async fn preview1_dangling_fd() {
93
run(PREVIEW1_DANGLING_FD, true).await.unwrap()
94
}
95
#[test_log::test(tokio::test(flavor = "multi_thread"))]
96
async fn preview1_dangling_symlink() {
97
run(PREVIEW1_DANGLING_SYMLINK, true).await.unwrap()
98
}
99
#[test_log::test(tokio::test(flavor = "multi_thread"))]
100
async fn preview1_directory_seek() {
101
run(PREVIEW1_DIRECTORY_SEEK, true).await.unwrap()
102
}
103
#[test_log::test(tokio::test(flavor = "multi_thread"))]
104
async fn preview1_dir_fd_op_failures() {
105
run(PREVIEW1_DIR_FD_OP_FAILURES, true).await.unwrap()
106
}
107
#[test_log::test(tokio::test(flavor = "multi_thread"))]
108
async fn preview1_fd_advise() {
109
run(PREVIEW1_FD_ADVISE, true).await.unwrap()
110
}
111
#[test_log::test(tokio::test(flavor = "multi_thread"))]
112
async fn preview1_fd_filestat_get() {
113
run(PREVIEW1_FD_FILESTAT_GET, true).await.unwrap()
114
}
115
#[test_log::test(tokio::test(flavor = "multi_thread"))]
116
async fn preview1_fd_filestat_set() {
117
run(PREVIEW1_FD_FILESTAT_SET, true).await.unwrap()
118
}
119
#[test_log::test(tokio::test(flavor = "multi_thread"))]
120
async fn preview1_fd_flags_set() {
121
run(PREVIEW1_FD_FLAGS_SET, true).await.unwrap()
122
}
123
#[test_log::test(tokio::test(flavor = "multi_thread"))]
124
async fn preview1_fd_readdir() {
125
run(PREVIEW1_FD_READDIR, true).await.unwrap()
126
}
127
#[test_log::test(tokio::test(flavor = "multi_thread"))]
128
async fn preview1_file_allocate() {
129
run(PREVIEW1_FILE_ALLOCATE, true).await.unwrap()
130
}
131
// see sync.rs for notes about ignores here
132
#[test_log::test(tokio::test(flavor = "multi_thread"))]
133
#[cfg_attr(not(target_os = "linux"), ignore)]
134
async fn preview1_file_pread_pwrite() {
135
run(PREVIEW1_FILE_PREAD_PWRITE, true).await.unwrap()
136
}
137
#[test_log::test(tokio::test(flavor = "multi_thread"))]
138
async fn preview1_file_read_write() {
139
run(PREVIEW1_FILE_READ_WRITE, true).await.unwrap()
140
}
141
#[test_log::test(tokio::test(flavor = "multi_thread"))]
142
async fn preview1_file_seek_tell() {
143
run(PREVIEW1_FILE_SEEK_TELL, true).await.unwrap()
144
}
145
#[test_log::test(tokio::test(flavor = "multi_thread"))]
146
async fn preview1_file_truncation() {
147
run(PREVIEW1_FILE_TRUNCATION, true).await.unwrap()
148
}
149
#[test_log::test(tokio::test(flavor = "multi_thread"))]
150
async fn preview1_file_unbuffered_write() {
151
run(PREVIEW1_FILE_UNBUFFERED_WRITE, true).await.unwrap()
152
}
153
#[test_log::test(tokio::test(flavor = "multi_thread"))]
154
async fn preview1_interesting_paths() {
155
run(PREVIEW1_INTERESTING_PATHS, true).await.unwrap()
156
}
157
#[test_log::test(tokio::test(flavor = "multi_thread"))]
158
async fn preview1_regular_file_isatty() {
159
run(PREVIEW1_REGULAR_FILE_ISATTY, false).await.unwrap()
160
}
161
#[test_log::test(tokio::test(flavor = "multi_thread"))]
162
async fn preview1_nofollow_errors() {
163
run(PREVIEW1_NOFOLLOW_ERRORS, true).await.unwrap()
164
}
165
#[test_log::test(tokio::test(flavor = "multi_thread"))]
166
async fn preview1_overwrite_preopen() {
167
run(PREVIEW1_OVERWRITE_PREOPEN, true).await.unwrap()
168
}
169
#[test_log::test(tokio::test(flavor = "multi_thread"))]
170
async fn preview1_path_exists() {
171
run(PREVIEW1_PATH_EXISTS, true).await.unwrap()
172
}
173
#[test_log::test(tokio::test(flavor = "multi_thread"))]
174
async fn preview1_path_filestat() {
175
run(PREVIEW1_PATH_FILESTAT, true).await.unwrap()
176
}
177
#[test_log::test(tokio::test(flavor = "multi_thread"))]
178
async fn preview1_path_link() {
179
run(PREVIEW1_PATH_LINK, true).await.unwrap()
180
}
181
#[test_log::test(tokio::test(flavor = "multi_thread"))]
182
async fn preview1_path_open_create_existing() {
183
run(PREVIEW1_PATH_OPEN_CREATE_EXISTING, true).await.unwrap()
184
}
185
#[test_log::test(tokio::test(flavor = "multi_thread"))]
186
async fn preview1_path_open_read_write() {
187
run(PREVIEW1_PATH_OPEN_READ_WRITE, true).await.unwrap()
188
}
189
#[test_log::test(tokio::test(flavor = "multi_thread"))]
190
async fn preview1_path_open_dirfd_not_dir() {
191
run(PREVIEW1_PATH_OPEN_DIRFD_NOT_DIR, true).await.unwrap()
192
}
193
#[test_log::test(tokio::test(flavor = "multi_thread"))]
194
async fn preview1_path_open_missing() {
195
run(PREVIEW1_PATH_OPEN_MISSING, true).await.unwrap()
196
}
197
#[test_log::test(tokio::test(flavor = "multi_thread"))]
198
async fn preview1_path_open_nonblock() {
199
run(PREVIEW1_PATH_OPEN_NONBLOCK, true).await.unwrap()
200
}
201
#[test_log::test(tokio::test(flavor = "multi_thread"))]
202
async fn preview1_path_rename_dir_trailing_slashes() {
203
run(PREVIEW1_PATH_RENAME_DIR_TRAILING_SLASHES, true)
204
.await
205
.unwrap()
206
}
207
#[test_log::test(tokio::test(flavor = "multi_thread"))]
208
async fn preview1_path_rename() {
209
run(PREVIEW1_PATH_RENAME, true).await.unwrap()
210
}
211
#[test_log::test(tokio::test(flavor = "multi_thread"))]
212
async fn preview1_path_symlink_trailing_slashes() {
213
run(PREVIEW1_PATH_SYMLINK_TRAILING_SLASHES, true)
214
.await
215
.unwrap()
216
}
217
#[test_log::test(tokio::test(flavor = "multi_thread"))]
218
async fn preview1_poll_oneoff_files() {
219
run(PREVIEW1_POLL_ONEOFF_FILES, false).await.unwrap()
220
}
221
#[test_log::test(tokio::test(flavor = "multi_thread"))]
222
async fn preview1_poll_oneoff_stdio() {
223
run(PREVIEW1_POLL_ONEOFF_STDIO, true).await.unwrap()
224
}
225
#[test_log::test(tokio::test(flavor = "multi_thread"))]
226
async fn preview1_readlink() {
227
run(PREVIEW1_READLINK, true).await.unwrap()
228
}
229
#[test_log::test(tokio::test(flavor = "multi_thread"))]
230
async fn preview1_remove_directory() {
231
run(PREVIEW1_REMOVE_DIRECTORY, true).await.unwrap()
232
}
233
#[test_log::test(tokio::test(flavor = "multi_thread"))]
234
async fn preview1_remove_nonempty_directory() {
235
run(PREVIEW1_REMOVE_NONEMPTY_DIRECTORY, true).await.unwrap()
236
}
237
#[test_log::test(tokio::test(flavor = "multi_thread"))]
238
async fn preview1_renumber() {
239
run(PREVIEW1_RENUMBER, true).await.unwrap()
240
}
241
#[test_log::test(tokio::test(flavor = "multi_thread"))]
242
async fn preview1_sched_yield() {
243
run(PREVIEW1_SCHED_YIELD, true).await.unwrap()
244
}
245
#[test_log::test(tokio::test(flavor = "multi_thread"))]
246
async fn preview1_stdio() {
247
run(PREVIEW1_STDIO, true).await.unwrap()
248
}
249
#[test_log::test(tokio::test(flavor = "multi_thread"))]
250
async fn preview1_stdio_isatty() {
251
// Only a valid test if the host executable's stdio is a terminal:
252
if test_programs_artifacts::stdio_is_terminal() {
253
// Inherit stdio, test asserts it is a tty:
254
run(PREVIEW1_STDIO_ISATTY, true).await.unwrap()
255
}
256
}
257
#[test_log::test(tokio::test(flavor = "multi_thread"))]
258
async fn preview1_stdio_not_isatty() {
259
// Don't inherit stdio, test asserts each is not tty:
260
run(PREVIEW1_STDIO_NOT_ISATTY, false).await.unwrap()
261
}
262
#[test_log::test(tokio::test(flavor = "multi_thread"))]
263
async fn preview1_symlink_create() {
264
run(PREVIEW1_SYMLINK_CREATE, true).await.unwrap()
265
}
266
#[test_log::test(tokio::test(flavor = "multi_thread"))]
267
async fn preview1_symlink_filestat() {
268
run(PREVIEW1_SYMLINK_FILESTAT, true).await.unwrap()
269
}
270
#[test_log::test(tokio::test(flavor = "multi_thread"))]
271
async fn preview1_symlink_loop() {
272
run(PREVIEW1_SYMLINK_LOOP, true).await.unwrap()
273
}
274
#[test_log::test(tokio::test(flavor = "multi_thread"))]
275
async fn preview1_unlink_file_trailing_slashes() {
276
run(PREVIEW1_UNLINK_FILE_TRAILING_SLASHES, true)
277
.await
278
.unwrap()
279
}
280
#[test_log::test(tokio::test(flavor = "multi_thread"))]
281
async fn preview1_path_open_preopen() {
282
run(PREVIEW1_PATH_OPEN_PREOPEN, true).await.unwrap()
283
}
284
#[test_log::test(tokio::test(flavor = "multi_thread"))]
285
async fn preview1_unicode_output() {
286
run(PREVIEW1_UNICODE_OUTPUT, true).await.unwrap()
287
}
288
#[test_log::test(tokio::test(flavor = "multi_thread"))]
289
async fn preview1_file_write() {
290
run(PREVIEW1_FILE_WRITE, true).await.unwrap()
291
}
292
#[test_log::test(tokio::test(flavor = "multi_thread"))]
293
async fn preview1_path_open_lots() {
294
run(PREVIEW1_PATH_OPEN_LOTS, true).await.unwrap()
295
}
296
297