Skip to content

Commit

Permalink
Add a test for preview2-specific reads/writes (#8271)
Browse files Browse the repository at this point in the history
Inspired by #8257 to show that the code being deleted in #8268 is needed
for this test to pass.
  • Loading branch information
alexcrichton authored Apr 1, 2024
1 parent 7da8b39 commit 0688bb9
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 14 deletions.
28 changes: 28 additions & 0 deletions crates/test-programs/src/bin/preview2_file_read_write.rs
Original file line number Diff line number Diff line change
@@ -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 changes: 11 additions & 1 deletion crates/wasi/tests/all/async_.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 changes: 7 additions & 6 deletions crates/wasi/tests/all/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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, "."])
Expand All @@ -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,
Expand Down
6 changes: 5 additions & 1 deletion crates/wasi/tests/all/preview1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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?;
Expand Down
25 changes: 19 additions & 6 deletions crates/wasi/tests/all/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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()
}

0 comments on commit 0688bb9

Please sign in to comment.