diff --git a/crates/test-programs/src/bin/preview1_device_read.rs b/crates/test-programs/src/bin/preview1_device_read.rs new file mode 100644 index 000000000000..a8109a1f1159 --- /dev/null +++ b/crates/test-programs/src/bin/preview1_device_read.rs @@ -0,0 +1,16 @@ +use std::fs::OpenOptions; +use std::io::Read; + +/// Assert that we can read from "miscellanous" devices such as /dev/zero on UNIX-alikes (assuming +/// /dev is passed as a preopen). +fn main() { + let mut device = OpenOptions::new() + .read(true) + .open("zero") + .expect("/dev/zero should be found and openable"); + let mut buffer = [1, 1]; + device + .read_exact(&mut buffer) + .expect("/dev/zero should be readable"); + assert_eq!(buffer, [0, 0]); +} diff --git a/crates/wasi-common/tests/all/async_.rs b/crates/wasi-common/tests/all/async_.rs index 10b70b79c471..86cb970e9896 100644 --- a/crates/wasi-common/tests/all/async_.rs +++ b/crates/wasi-common/tests/all/async_.rs @@ -34,10 +34,9 @@ async fn run(path: &str, inherit_stdio: bool) -> Result<()> { .stdout(Box::new(stdout.clone())) .stderr(Box::new(stderr.clone())); } - builder.arg(name)?.arg(".")?; + builder.arg(name)?.arg("/dev")?; println!("preopen: {workspace:?}"); - let preopen_dir = - cap_std::fs::Dir::open_ambient_dir(workspace.path(), cap_std::ambient_authority())?; + let preopen_dir = cap_std::fs::Dir::open_ambient_dir("/dev", cap_std::ambient_authority())?; builder.preopened_dir(preopen_dir, ".")?; for (var, val) in test_programs_artifacts::wasi_tests_environment() { builder.env(var, val)?; @@ -292,3 +291,8 @@ async fn preview1_file_write() { async fn preview1_path_open_lots() { run(PREVIEW1_PATH_OPEN_LOTS, true).await.unwrap() } +#[test_log::test(tokio::test(flavor = "multi_thread"))] +#[cfg_attr(not(unix), ignore)] +async fn preview1_device_read() { + run(PREVIEW1_DEVICE_READ, true).await.unwrap() +} diff --git a/crates/wasi-common/tests/all/sync.rs b/crates/wasi-common/tests/all/sync.rs index 4e0d38ca2fca..9b9f36de4185 100644 --- a/crates/wasi-common/tests/all/sync.rs +++ b/crates/wasi-common/tests/all/sync.rs @@ -26,10 +26,9 @@ fn run(path: &str, inherit_stdio: bool) -> Result<()> { .stdout(Box::new(stdout.clone())) .stderr(Box::new(stderr.clone())); } - builder.arg(name)?.arg(".")?; + builder.arg(name)?.arg("/dev")?; println!("preopen: {workspace:?}"); - let preopen_dir = - cap_std::fs::Dir::open_ambient_dir(workspace.path(), cap_std::ambient_authority())?; + let preopen_dir = cap_std::fs::Dir::open_ambient_dir("/dev", cap_std::ambient_authority())?; builder.preopened_dir(preopen_dir, ".")?; for (var, val) in test_programs_artifacts::wasi_tests_environment() { builder.env(var, val)?; @@ -281,3 +280,8 @@ fn preview1_file_write() { fn preview1_path_open_lots() { run(PREVIEW1_PATH_OPEN_LOTS, true).unwrap() } +#[test_log::test] +#[cfg_attr(not(unix), ignore)] +fn preview1_device_read() { + run(PREVIEW1_DEVICE_READ, true).unwrap() +} diff --git a/crates/wasi/tests/all/async_.rs b/crates/wasi/tests/all/async_.rs index 7d25423726b2..b2737276e62e 100644 --- a/crates/wasi/tests/all/async_.rs +++ b/crates/wasi/tests/all/async_.rs @@ -399,3 +399,8 @@ async fn preview2_file_read_write() { .await .unwrap() } +// #[test_log::test(tokio::test(flavor = "multi_thread"))] +// #[cfg_attr(not(unix), ignore)] +// async fn preview1_device_read() { +// run(PREVIEW1_DEVICE_READ_COMPONENT, false).await.unwrap() +// } diff --git a/crates/wasi/tests/all/sync.rs b/crates/wasi/tests/all/sync.rs index 5629d5d3ffd8..4d94ef93c1ec 100644 --- a/crates/wasi/tests/all/sync.rs +++ b/crates/wasi/tests/all/sync.rs @@ -333,3 +333,8 @@ fn preview2_adapter_badfd() { fn preview2_file_read_write() { run(PREVIEW2_FILE_READ_WRITE_COMPONENT, false).unwrap() } +// #[test_log::test] +// #[cfg_attr(not(unix), ignore)] +// fn preview1_device_read() { +// run(PREVIEW1_DEVICE_READ_COMPONENT, false).unwrap() +// }