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