Skip to content

Add test for opening miscellaneous devices on UNIX-alikes. #10137

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions crates/test-programs/src/bin/preview1_device_read.rs
Original file line number Diff line number Diff line change
@@ -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]);
}
10 changes: 7 additions & 3 deletions crates/wasi-common/tests/all/async_.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)?;
Expand Down Expand Up @@ -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()
}
10 changes: 7 additions & 3 deletions crates/wasi-common/tests/all/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)?;
Expand Down Expand Up @@ -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()
}
5 changes: 5 additions & 0 deletions crates/wasi/tests/all/async_.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
// }
5 changes: 5 additions & 0 deletions crates/wasi/tests/all/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
// }
Loading