Skip to content

Commit

Permalink
Merge wasi-misc-tests repo as a subdir (bytecodealliance#174)
Browse files Browse the repository at this point in the history
* Initial checkin.

* Update to rust-lang libc.

* Add a .gitignore file.

* Factor out functions for cleaning up files and directories.

* Fix a typo in a comment.

* Print a "Success!" message if all tests passed.

* Factor out code for creating directories.

* Add wrappers around WASI functions.

These wrappers handle converting from &str to pointer+length and handle
unsafe.

* More refactoring.

* Refactor a fd_close helper.

* Move utility functions into a separate file.

* cargo update

* Add a basic test for random_get.

* Test that directories aren't resizable.

* Test clearing __WASI_RIGHT_PATH_FILESTAT_SET_SIZE.

Ensure that clearing __WASI_RIGHT_PATH_FILESTAT_SET_SIZE succeeds before
testing file truncation.

* cargo update

* Modularise tests for easier use with wasi-common crate

* Add a Code of Conduct and CONTRIBUTING.md.

* Fix typo

* Add testcase for fd_allocate

* Add positive test for fd_renumber

* Assert bufused in readlink_no_buffer testcase

* Add positive readlink testcase

* Add testcase for fd_seek and fd_tell

* Add fd_p{read, write} test

* Add README

* Add cases with trailing slashes to interesting_paths

* Split nofollow_errors testcase into two

* nofollow_errors now operators on symlinks to existing resources
* dangling_symlink covers danling symlinks tests

* Factor out a `create_file` helper function.

* Switch from the error crate to `std::io::Error::last_os_error()`.

* Use `create_file` in the readlink test too.

* Add a test for fd_filestat_set_*

* Minor refactoring

Add missing cleanup_file calls to file_pread_pwrite and
file_seek_tell.

* Add testcase for unbuffered fd_write; fixes bytecodealliance#11

* Add testcase for path_rename

* Use the wasi crate.

Switch from depending on libc to depending on the new wasi crate to provide
the low-level WASI interfaces.

See also rust-lang/libc#1461.

* Add a test for path_filestat_*

* Add a test for fd_readdir

* Use expect instead of unwrap

* Add a check for ino.

* Fix the build

* Don't assume a specific order of dirents

* Better test

* Test cookie value

* Fix file types

* Fix the test

* Fix the test

* Fix the test

* Cleanup

* Minor formatting tidying in README.md.

* Fix miscellaneous clippy warnings.

* Rename the crate to wasi-misc-tests.

* Update to wasi 0.7.0.

This switches from using the libc wasi bindings to using the wasi
crate's bindings. This eliminates a git dependency on libc, updates
to the new-style bindings which use Result where possible, and treats
functions that operate on raw file descriptors as unsafe.

* Add various tests for trailing-slash behavior.

* Sync new testcases with latest upstream

* Fix path_filestat testcase

* Add smoke test for fd_advise

This test is a true smoke test as it only tests whether issuing
an advise call to the host's kernel doesn't yield an error. The
consequence of issuing such a syscall is not tested.

* Check if CLOCK_MONOTONIC is actually monotonic

* Refactor the inequality assertions for more debuggable errors.

* Bump libc from 0.2.62 to 0.2.65

Bumps [libc](https://github.com/rust-lang/libc) from 0.2.62 to 0.2.65.
- [Release notes](https://github.com/rust-lang/libc/releases)
- [Commits](rust-lang/libc@0.2.62...0.2.65)

Signed-off-by: dependabot-preview[bot] <[email protected]>

* Fix compilation error

* Enable Actions and add rust.yml (bytecodealliance#35)

* Enable Actions and add rust.yml

This commit enables Github Actions and adds corresponding configuration in rust.yml file.

* Update rust.yml

* Fix formatting

* Add empty .rustfmt.toml config file

* Add badge to README

* Update README

* Clean up Github Actions and README

* Add test case for `poll_oneoff` syscall (bytecodealliance#38)

* Add test case for `poll_oneoff` syscall

This commit adds a test case for `poll_oneoff` syscall. In particular,
it builds on the excellent test use case provided by @dunnock in their
repo [poll_oneoff_tests] (thanks!), and tests:
* simple timeout
* stdin read with timeout
* fd read and fd write polls

[poll_oneoff_tests]: https://github.com/dunnock/poll_oneoff_tests

* Apply suggestions and negative test for bad fd

Co-authored-by: Maxim Vorobjov <[email protected]>

* Add smoke test for STDOUT/ERR readwrite poll

* Add comment on stdin/out/err

* Add a test for `*at`-style functions returning `ENOTDIR` when `dirfd` is not a dir.

* Remove misc_testsuite submodule

* Add "publish=false" to Cargo.toml; remove LICENSE
  • Loading branch information
kubkon authored Nov 7, 2019
1 parent 2fe3530 commit 3793fa3
Show file tree
Hide file tree
Showing 40 changed files with 3,525 additions and 33 deletions.
3 changes: 0 additions & 3 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
[submodule "misc_testsuite"]
path = misc_testsuite
url = https://github.com/cranestation/wasi-misc-tests
[submodule "WASI"]
path = WASI
url = https://github.com/WebAssembly/WASI
23 changes: 8 additions & 15 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
[package]
name = "wasi-common"
version = "0.5.0"
authors = [
"Adam C. Foltzer <[email protected]>",
"Frank Denis <[email protected]>",
"Jakub Konka <[email protected]>",
"Dan Gohman <[email protected]>"]
authors = ["The Wasmtime Project Developers"]
edition = "2018"
license = "Apache-2.0 WITH LLVM-exception"
description = "WASI implementation in Rust"
Expand Down Expand Up @@ -41,18 +37,15 @@ cpu-time = "1.0"


[dev-dependencies]
wasmtime-runtime = { git = "https://github.com/acfoltzer/wasmtime", rev = "d99e2fb" }
wasmtime-environ = { git = "https://github.com/acfoltzer/wasmtime", rev = "d99e2fb" }
wasmtime-jit = { git = "https://github.com/acfoltzer/wasmtime", rev = "d99e2fb" }
wasmtime-wasi = { git = "https://github.com/acfoltzer/wasmtime", rev = "d99e2fb" }
wasmtime-api = { git = "https://github.com/acfoltzer/wasmtime", rev = "d99e2fb" }
cranelift-codegen = "0.47.0"
wasmtime-runtime = { git = "https://github.com/CraneStation/wasmtime", rev = "ab3cd94" }
wasmtime-environ = { git = "https://github.com/CraneStation/wasmtime", rev = "ab3cd94" }
wasmtime-jit = { git = "https://github.com/CraneStation/wasmtime", rev = "ab3cd94" }
wasmtime-wasi = { git = "https://github.com/CraneStation/wasmtime", rev = "ab3cd94" }
wasmtime-api = { git = "https://github.com/CraneStation/wasmtime", rev = "ab3cd94" }
cranelift-codegen = "0.49"
target-lexicon = "0.8.1"
pretty_env_logger = "0.3.0"
tempfile = "3.1.0"
# this is just a temp fix to make the tests build and run; I hope this to be
# completely removed when we merge `wasi-common` into `wasmtime`
faerie = "=0.11.0"

[patch."https://github.com/CraneStation/wasi-common"]
wasi-common = { path = "." }
Expand All @@ -66,4 +59,4 @@ crate-type = ["rlib", "staticlib", "cdylib"]

[workspace]
members = ["wasi-common-cbindgen"]
exclude = ["winx", "misc_testsuite"]
exclude = ["wasi-misc-tests", "wig", "winx", "WASI/tools/witx"]
20 changes: 7 additions & 13 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ mod wasm_tests {

pub(crate) fn build_and_generate_tests() {
// Validate if any of test sources are present and if they changed
let bin_tests = std::fs::read_dir("misc_testsuite/src/bin")
.expect("wasm_tests feature requires initialized misc_testsuite: `git submodule update --init`?");
// This should always work since there is no submodule to init anymore
let bin_tests = std::fs::read_dir("wasi-misc-tests/src/bin").unwrap();
for test in bin_tests {
if let Ok(test_file) = test {
let test_file_path = test_file
Expand All @@ -33,23 +33,17 @@ mod wasm_tests {
println!("cargo:rerun-if-changed={}", test_file_path);
}
}

// Build tests to OUT_DIR (target/*/build/wasi-common-*/out/wasm32-wasi/release/*.wasm)
let out_dir = PathBuf::from(
env::var("OUT_DIR").expect("The OUT_DIR environment variable must be set"),
);
let mut out = File::create(out_dir.join("misc_testsuite_tests.rs"))
let mut out = File::create(out_dir.join("wasi_misc_tests.rs"))
.expect("error generating test source file");
build_tests("misc_testsuite", &out_dir).expect("building tests");
test_directory(&mut out, "misc_testsuite", &out_dir).expect("generating tests");
build_tests("wasi-misc-tests", &out_dir).expect("building tests");
test_directory(&mut out, "wasi-misc-tests", &out_dir).expect("generating tests");
}

fn build_tests(testsuite: &str, out_dir: &Path) -> io::Result<()> {
// if the submodule has not been checked out, the build will stall
if !Path::new(&format!("{}/Cargo.toml", testsuite)).exists() {
panic!("Testsuite {} not checked out", testsuite);
}

let mut cmd = Command::new("cargo");
cmd.args(&[
"build",
Expand Down Expand Up @@ -191,7 +185,7 @@ mod wasm_tests {
} else {
/// Ignore tests that aren't supported yet.
fn ignore(testsuite: &str, name: &str) -> bool {
if testsuite == "misc_testsuite" {
if testsuite == "wasi-misc-tests" {
match name {
"readlink_no_buffer" => true,
"dangling_symlink" => true,
Expand All @@ -211,7 +205,7 @@ mod wasm_tests {

/// Mark tests which do not require preopens
fn no_preopens(testsuite: &str, name: &str) -> bool {
if testsuite == "misc_testsuite" {
if testsuite == "wasi-misc-tests" {
match name {
"big_random_buf" => true,
"clock_time_get" => true,
Expand Down
1 change: 0 additions & 1 deletion misc_testsuite
Submodule misc_testsuite deleted from f67722
2 changes: 1 addition & 1 deletion tests/wasm_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ fn setup_log() {
})
}

include!(concat!(env!("OUT_DIR"), "/misc_testsuite_tests.rs"));
include!(concat!(env!("OUT_DIR"), "/wasi_misc_tests.rs"));
11 changes: 11 additions & 0 deletions wasi-misc-tests/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[package]
name = "wasi-misc-tests"
version = "0.1.0"
authors = ["The Wasmtime Project Developers"]
edition = "2018"
publish = false

[dependencies]
libc = "0.2.65"
wasi = "0.7.0"
more-asserts = "0.2.1"
4 changes: 4 additions & 0 deletions wasi-misc-tests/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
This is the `wasi-misc-test` crate, which contains source code for the system-level WASI tests.

Building these tests requires `wasm32-wasi` target installed

18 changes: 18 additions & 0 deletions wasi-misc-tests/src/bin/big_random_buf.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
use wasi::wasi_unstable;

fn test_big_random_buf() {
let mut buf = Vec::new();
buf.resize(1024, 0);
assert!(
wasi_unstable::random_get(&mut buf).is_ok(),
"calling get_random on a large buffer"
);
// Chances are pretty good that at least *one* byte will be non-zero in
// any meaningful random function producing 1024 u8 values.
assert!(buf.iter().any(|x| *x != 0), "random_get returned all zeros");
}

fn main() {
// Run the tests.
test_big_random_buf()
}
37 changes: 37 additions & 0 deletions wasi-misc-tests/src/bin/clock_time_get.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
use more_asserts::assert_le;
use wasi::wasi_unstable;
use wasi_misc_tests::wasi_wrappers::wasi_clock_time_get;

unsafe fn test_clock_time_get() {
// Test that clock_time_get succeeds. Even in environments where it's not
// desirable to expose high-precision timers, it should still succeed.
// clock_res_get is where information about precision can be provided.
let mut time: wasi_unstable::Timestamp = 0;
let status = wasi_clock_time_get(wasi_unstable::CLOCK_MONOTONIC, 1, &mut time);
assert_eq!(
status,
wasi_unstable::raw::__WASI_ESUCCESS,
"clock_time_get with a precision of 1"
);

let status = wasi_clock_time_get(wasi_unstable::CLOCK_MONOTONIC, 0, &mut time);
assert_eq!(
status,
wasi_unstable::raw::__WASI_ESUCCESS,
"clock_time_get with a precision of 0"
);
let first_time = time;

let status = wasi_clock_time_get(wasi_unstable::CLOCK_MONOTONIC, 0, &mut time);
assert_eq!(
status,
wasi_unstable::raw::__WASI_ESUCCESS,
"clock_time_get with a precision of 0"
);
assert_le!(first_time, time, "CLOCK_MONOTONIC should be monotonic");
}

fn main() {
// Run the tests.
unsafe { test_clock_time_get() }
}
83 changes: 83 additions & 0 deletions wasi-misc-tests/src/bin/close_preopen.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
use libc;
use more_asserts::assert_gt;
use std::{env, mem, process};
use wasi::wasi_unstable;
use wasi_misc_tests::open_scratch_directory;
use wasi_misc_tests::wasi_wrappers::wasi_fd_fdstat_get;

unsafe fn test_close_preopen(dir_fd: wasi_unstable::Fd) {
let pre_fd: wasi_unstable::Fd = (libc::STDERR_FILENO + 1) as wasi_unstable::Fd;

assert_gt!(dir_fd, pre_fd, "dir_fd number");

// Try to close a preopened directory handle.
assert_eq!(
wasi_unstable::fd_close(pre_fd),
Err(wasi_unstable::ENOTSUP),
"closing a preopened file descriptor",
);

// Try to renumber over a preopened directory handle.
assert_eq!(
wasi_unstable::fd_renumber(dir_fd, pre_fd),
Err(wasi_unstable::ENOTSUP),
"renumbering over a preopened file descriptor",
);

// Ensure that dir_fd is still open.
let mut dir_fdstat: wasi_unstable::FdStat = mem::zeroed();
let mut status = wasi_fd_fdstat_get(dir_fd, &mut dir_fdstat);
assert_eq!(
status,
wasi_unstable::raw::__WASI_ESUCCESS,
"calling fd_fdstat on the scratch directory"
);
assert_eq!(
dir_fdstat.fs_filetype,
wasi_unstable::FILETYPE_DIRECTORY,
"expected the scratch directory to be a directory",
);

// Try to renumber a preopened directory handle.
assert_eq!(
wasi_unstable::fd_renumber(pre_fd, dir_fd),
Err(wasi_unstable::ENOTSUP),
"renumbering over a preopened file descriptor",
);

// Ensure that dir_fd is still open.
status = wasi_fd_fdstat_get(dir_fd, &mut dir_fdstat);
assert_eq!(
status,
wasi_unstable::raw::__WASI_ESUCCESS,
"calling fd_fdstat on the scratch directory"
);
assert_eq!(
dir_fdstat.fs_filetype,
wasi_unstable::FILETYPE_DIRECTORY,
"expected the scratch directory to be a directory",
);
}

fn main() {
let mut args = env::args();
let prog = args.next().unwrap();
let arg = if let Some(arg) = args.next() {
arg
} else {
eprintln!("usage: {} <scratch directory>", prog);
process::exit(1);
};

// Open scratch directory
let dir_fd = match open_scratch_directory(&arg) {
Ok(dir_fd) => dir_fd,
Err(err) => {
eprintln!("{}", err);
process::exit(1)
}
};

// Run the tests.
unsafe { test_close_preopen(dir_fd) }
}
62 changes: 62 additions & 0 deletions wasi-misc-tests/src/bin/dangling_symlink.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
use std::{env, process};
use wasi::wasi_unstable;
use wasi_misc_tests::open_scratch_directory;
use wasi_misc_tests::utils::cleanup_file;
use wasi_misc_tests::wasi_wrappers::{wasi_path_open, wasi_path_symlink};

unsafe fn test_dangling_symlink(dir_fd: wasi_unstable::Fd) {
// First create a dangling symlink.
assert!(
wasi_path_symlink("target", dir_fd, "symlink").is_ok(),
"creating a symlink"
);

// Try to open it as a directory with O_NOFOLLOW.
let mut file_fd: wasi_unstable::Fd = wasi_unstable::Fd::max_value() - 1;
let status = wasi_path_open(
dir_fd,
0,
"symlink",
wasi_unstable::O_DIRECTORY,
0,
0,
0,
&mut file_fd,
);
assert_eq!(
status,
wasi_unstable::raw::__WASI_ELOOP,
"opening a dangling symlink as a directory",
);
assert_eq!(
file_fd,
wasi_unstable::Fd::max_value(),
"failed open should set the file descriptor to -1",
);

// Clean up.
cleanup_file(dir_fd, "symlink");
}

fn main() {
let mut args = env::args();
let prog = args.next().unwrap();
let arg = if let Some(arg) = args.next() {
arg
} else {
eprintln!("usage: {} <scratch directory>", prog);
process::exit(1);
};

// Open scratch directory
let dir_fd = match open_scratch_directory(&arg) {
Ok(dir_fd) => dir_fd,
Err(err) => {
eprintln!("{}", err);
process::exit(1)
}
};

// Run the tests.
unsafe { test_dangling_symlink(dir_fd) }
}
Loading

0 comments on commit 3793fa3

Please sign in to comment.