Browse Source

Add a test for preview2-specific reads/writes (#8271)

Inspired by #8257 to show that the code being deleted in #8268 is needed
for this test to pass.
pull/8279/head
Alex Crichton 7 months ago
committed by GitHub
parent
commit
0688bb9974
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 28
      crates/test-programs/src/bin/preview2_file_read_write.rs
  2. 12
      crates/wasi/tests/all/async_.rs
  3. 13
      crates/wasi/tests/all/main.rs
  4. 6
      crates/wasi/tests/all/preview1.rs
  5. 25
      crates/wasi/tests/all/sync.rs

28
crates/test-programs/src/bin/preview2_file_read_write.rs

@ -0,0 +1,28 @@
use test_programs::wasi;
use test_programs::wasi::filesystem::types::{DescriptorFlags, OpenFlags, PathFlags};
fn main() {
let preopens = wasi::filesystem::preopens::get_directories();
let (dir, _) = &preopens[0];
let filename = "test.txt";
let file = dir
.open_at(
PathFlags::empty(),
filename,
OpenFlags::CREATE,
DescriptorFlags::READ | DescriptorFlags::WRITE,
)
.unwrap();
let stream = file.write_via_stream(5).unwrap();
stream.blocking_write_and_flush(b"Hello, ").unwrap();
stream.blocking_write_and_flush(b"World!").unwrap();
drop(stream);
let stream = file.read_via_stream(0).unwrap();
let contents = stream.blocking_read(100).unwrap();
assert_eq!(contents, b"\0\0\0\0\0Hello, World!");
drop((stream, file));
dir.unlink_file_at(filename).unwrap();
}

12
crates/wasi/tests/all/async_.rs

@ -13,7 +13,11 @@ async fn run(path: &str, inherit_stdio: bool) -> Result<()> {
let mut linker = Linker::new(&engine);
add_to_linker_async(&mut linker)?;
let (mut store, _td) = store(&engine, name, inherit_stdio)?;
let (mut store, _td) = store(&engine, name, |builder| {
if inherit_stdio {
builder.inherit_stdio();
}
})?;
let component = Component::from_file(&engine, path)?;
let (command, _instance) = Command::instantiate_async(&mut store, &component, &linker).await?;
command
@ -379,3 +383,9 @@ async fn preview2_pollable_traps() {
async fn preview2_adapter_badfd() {
run(PREVIEW2_ADAPTER_BADFD_COMPONENT, false).await.unwrap()
}
#[test_log::test(tokio::test(flavor = "multi_thread"))]
async fn preview2_file_read_write() {
run(PREVIEW2_FILE_READ_WRITE_COMPONENT, false)
.await
.unwrap()
}

13
crates/wasi/tests/all/main.rs

@ -30,18 +30,18 @@ fn prepare_workspace(exe_name: &str) -> Result<TempDir> {
Ok(tempdir)
}
fn store(engine: &Engine, name: &str, inherit_stdio: bool) -> Result<(Store<Ctx>, TempDir)> {
fn store(
engine: &Engine,
name: &str,
configure: impl FnOnce(&mut WasiCtxBuilder),
) -> Result<(Store<Ctx>, TempDir)> {
let stdout = MemoryOutputPipe::new(4096);
let stderr = MemoryOutputPipe::new(4096);
let workspace = prepare_workspace(name)?;
// Create our wasi context.
let mut builder = WasiCtxBuilder::new();
if inherit_stdio {
builder.inherit_stdio();
} else {
builder.stdout(stdout.clone()).stderr(stderr.clone());
}
builder.stdout(stdout.clone()).stderr(stderr.clone());
builder
.args(&[name, "."])
@ -53,6 +53,7 @@ fn store(engine: &Engine, name: &str, inherit_stdio: bool) -> Result<(Store<Ctx>
builder.env(var, val);
}
configure(&mut builder);
let ctx = Ctx {
wasi: builder.build_p1(),
stderr,

6
crates/wasi/tests/all/preview1.rs

@ -14,7 +14,11 @@ async fn run(path: &str, inherit_stdio: bool) -> Result<()> {
add_to_linker_async(&mut linker, |t| &mut t.wasi)?;
let module = Module::from_file(&engine, path)?;
let (mut store, _td) = store(&engine, name, inherit_stdio)?;
let (mut store, _td) = store(&engine, name, |builder| {
if inherit_stdio {
builder.inherit_stdio();
}
})?;
let instance = linker.instantiate_async(&mut store, &module).await?;
let start = instance.get_typed_func::<(), ()>(&mut store, "_start")?;
start.call_async(&mut store, ()).await?;

25
crates/wasi/tests/all/sync.rs

@ -13,13 +13,22 @@ fn run(path: &str, inherit_stdio: bool) -> Result<()> {
let mut linker = Linker::new(&engine);
add_to_linker_sync(&mut linker)?;
let (mut store, _td) = store(&engine, name, inherit_stdio)?;
let component = Component::from_file(&engine, path)?;
let (command, _instance) = Command::instantiate(&mut store, &component, &linker)?;
command
.wasi_cli_run()
.call_run(&mut store)?
.map_err(|()| anyhow::anyhow!("run returned a failure"))
for blocking in [false, true] {
let (mut store, _td) = store(&engine, name, |builder| {
if inherit_stdio {
builder.inherit_stdio();
}
builder.allow_blocking_current_thread(blocking);
})?;
let (command, _instance) = Command::instantiate(&mut store, &component, &linker)?;
command
.wasi_cli_run()
.call_run(&mut store)?
.map_err(|()| anyhow::anyhow!("run returned a failure"))?;
}
Ok(())
}
foreach_preview1!(assert_test_exists);
@ -314,3 +323,7 @@ fn preview2_pollable_traps() {
fn preview2_adapter_badfd() {
run(PREVIEW2_ADAPTER_BADFD_COMPONENT, false).unwrap()
}
#[test_log::test]
fn preview2_file_read_write() {
run(PREVIEW2_FILE_READ_WRITE_COMPONENT, false).unwrap()
}

Loading…
Cancel
Save