diff --git a/.circleci/config.yml b/.circleci/config.yml index 73fd8ddd3..7467652db 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -60,7 +60,6 @@ jobs: - restore_cache: keys: - v8-test-cargo-cache-linux-stable-{{ arch }}-{{ checksum "Cargo.lock" }} - - v8-test-cargo-cache-linux-stable-{{ arch }} - <<: *run_install_dependencies - run: name: Tests @@ -158,7 +157,6 @@ jobs: - restore_cache: keys: - v8-cargo-cache-linux-nightly-{{ arch }}-{{ checksum "Cargo.lock" }} - - v8-cargo-cache-linux-nightly-{{ arch }} - run: name: Install dependencies command: | @@ -226,7 +224,6 @@ jobs: - restore_cache: keys: - v8-cargo-cache-darwin-nightly-{{ arch }}-{{ checksum "Cargo.lock" }} - - v8-cargo-cache-darwin-nightly-{{ arch }} - run: name: Install crate dependencies command: | @@ -309,18 +306,21 @@ jobs: - restore_cache: keys: - v8-cargo-cache-linux-nightly-{{ arch }}-{{ checksum "Cargo.lock" }} - - v8-cargo-cache-linux-nightly-{{ arch }} - run: name: Install dependencies command: | sudo apt-get install -y cmake curl -O https://releases.llvm.org/7.0.0/clang+llvm-7.0.0-x86_64-linux-gnu-ubuntu-16.04.tar.xz tar xf clang+llvm-7.0.0-x86_64-linux-gnu-ubuntu-16.04.tar.xz + rustup toolchain install nightly + rustup target add wasm32-wasi --toolchain nightly - run: rustup default nightly-2019-04-11 - run: | export LLVM_SYS_70_PREFIX="`pwd`/clang+llvm-7.0.0-x86_64-linux-gnu-ubuntu-16.04/" make test make test-singlepass + make test-wasi-singlepass + make test-wasi-clif make test-emscripten-clif make test-emscripten-singlepass - save_cache: diff --git a/Cargo.lock b/Cargo.lock index 904d9588f..d23ccdd22 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2287,6 +2287,7 @@ dependencies = [ "structopt 0.2.15 (registry+https://github.com/rust-lang/crates.io-index)", "wabt 0.7.4 (registry+https://github.com/rust-lang/crates.io-index)", "wasmer-clif-backend 0.4.2", + "wasmer-dev-utils 0.4.2", "wasmer-emscripten 0.4.2", "wasmer-kernel-loader 0.1.0", "wasmer-llvm-backend 0.4.2", @@ -2322,6 +2323,13 @@ dependencies = [ "winapi 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "wasmer-dev-utils" +version = "0.4.2" +dependencies = [ + "libc 0.2.54 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "wasmer-emscripten" version = "0.4.2" @@ -2334,6 +2342,7 @@ dependencies = [ "time 0.1.42 (registry+https://github.com/rust-lang/crates.io-index)", "wabt 0.7.4 (registry+https://github.com/rust-lang/crates.io-index)", "wasmer-clif-backend 0.4.2", + "wasmer-dev-utils 0.4.2", "wasmer-llvm-backend 0.4.2", "wasmer-runtime-core 0.4.2", "wasmer-singlepass-backend 0.4.2", @@ -2474,11 +2483,15 @@ version = "0.4.2" dependencies = [ "byteorder 1.3.1 (registry+https://github.com/rust-lang/crates.io-index)", "generational-arena 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", + "glob 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)", "hashbrown 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)", "libc 0.2.54 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", "rand 0.6.5 (registry+https://github.com/rust-lang/crates.io-index)", + "wasmer-clif-backend 0.4.2", + "wasmer-dev-utils 0.4.2", "wasmer-runtime-core 0.4.2", + "wasmer-singlepass-backend 0.4.2", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index cc2d816da..aabc0a3cd 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -34,9 +34,10 @@ wasmer-emscripten = { path = "lib/emscripten" } wasmer-llvm-backend = { path = "lib/llvm-backend", optional = true } wasmer-wasi = { path = "lib/wasi", optional = true } wasmer-kernel-loader = { path = "lib/kernel-loader", optional = true } +wasmer-dev-utils = { path = "lib/dev-utils", optional = true } [workspace] -members = ["lib/clif-backend", "lib/singlepass-backend", "lib/runtime", "lib/runtime-abi", "lib/runtime-core", "lib/emscripten", "lib/spectests", "lib/win-exception-handler", "lib/runtime-c-api", "lib/llvm-backend", "lib/wasi", "lib/middleware-common", "lib/kernel-loader", "lib/kernel-net", "examples/plugin-for-example"] +members = ["lib/clif-backend", "lib/singlepass-backend", "lib/runtime", "lib/runtime-abi", "lib/runtime-core", "lib/emscripten", "lib/spectests", "lib/win-exception-handler", "lib/runtime-c-api", "lib/llvm-backend", "lib/wasi", "lib/middleware-common", "lib/kernel-loader", "lib/kernel-net", "lib/dev-utils", "examples/plugin-for-example"] [build-dependencies] wabt = "0.7.2" diff --git a/Makefile b/Makefile index d598e4220..2206d48aa 100644 --- a/Makefile +++ b/Makefile @@ -12,6 +12,9 @@ spectests: emtests: WASM_EMSCRIPTEN_GENERATE_EMTESTS=1 cargo build -p wasmer-emscripten +wasitests: + WASM_WASI_GENERATE_WASITESTS=1 cargo build -p wasmer-wasi + # clean: # rm -rf artifacts @@ -45,7 +48,7 @@ do-install: test: # We use one thread so the emscripten stdouts doesn't collide - cargo test --all --exclude wasmer-runtime-c-api --exclude wasmer-emscripten --exclude wasmer-spectests --exclude wasmer-singlepass-backend -- $(runargs) + cargo test --all --exclude wasmer-runtime-c-api --exclude wasmer-emscripten --exclude wasmer-spectests --exclude wasmer-singlepass-backend --exclude wasmer-wasi -- $(runargs) # cargo test --all --exclude wasmer-emscripten -- --test-threads=1 $(runargs) cargo test --manifest-path lib/spectests/Cargo.toml --features clif cargo test --manifest-path lib/spectests/Cargo.toml --features llvm @@ -66,6 +69,12 @@ test-emscripten-clif: test-emscripten-singlepass: cargo test --manifest-path lib/emscripten/Cargo.toml --features singlepass -- --test-threads=1 $(runargs) +test-wasi-clif: + cargo test --manifest-path lib/wasi/Cargo.toml --features "clif" -- $(runargs) + +test-wasi-singlepass: + cargo test --manifest-path lib/wasi/Cargo.toml --features "singlepass" -- $(runargs) + singlepass-debug-release: cargo +nightly build --features backend:singlepass,debug --release diff --git a/lib/dev-utils/Cargo.toml b/lib/dev-utils/Cargo.toml new file mode 100644 index 000000000..b1c95658b --- /dev/null +++ b/lib/dev-utils/Cargo.toml @@ -0,0 +1,11 @@ +[package] +name = "wasmer-dev-utils" +version = "0.4.2" +description = "Wasmer runtime core library" +license = "MIT" +authors = ["The Wasmer Engineering Team "] +edition = "2018" +repository = "https://github.com/wasmerio/wasmer" + +[dependencies] +libc = "0.2.49" \ No newline at end of file diff --git a/lib/dev-utils/README.md b/lib/dev-utils/README.md new file mode 100644 index 000000000..489d598fc --- /dev/null +++ b/lib/dev-utils/README.md @@ -0,0 +1,3 @@ +# Dev Utils + +This is shared code between the modules for testing and development only. Code in this crate will not be shipped. \ No newline at end of file diff --git a/lib/emscripten/src/file_descriptor.rs b/lib/dev-utils/src/file_descriptor.rs similarity index 100% rename from lib/emscripten/src/file_descriptor.rs rename to lib/dev-utils/src/file_descriptor.rs diff --git a/lib/dev-utils/src/lib.rs b/lib/dev-utils/src/lib.rs new file mode 100644 index 000000000..76ae6804f --- /dev/null +++ b/lib/dev-utils/src/lib.rs @@ -0,0 +1,2 @@ +pub mod file_descriptor; +pub mod stdio; diff --git a/lib/emscripten/src/stdio.rs b/lib/dev-utils/src/stdio.rs similarity index 100% rename from lib/emscripten/src/stdio.rs rename to lib/dev-utils/src/stdio.rs diff --git a/lib/emscripten/Cargo.toml b/lib/emscripten/Cargo.toml index 2b08c1aa9..c9209eb8f 100644 --- a/lib/emscripten/Cargo.toml +++ b/lib/emscripten/Cargo.toml @@ -23,6 +23,7 @@ rand = "0.6" [dev-dependencies] wabt = "0.7.2" +wasmer-dev-utils = { path = "../dev-utils", version = "0.4.2"} [build-dependencies] glob = "0.2.11" diff --git a/lib/emscripten/build/emtests.rs b/lib/emscripten/build/emtests.rs index 4a543683d..5fabc8513 100644 --- a/lib/emscripten/build/emtests.rs +++ b/lib/emscripten/build/emtests.rs @@ -5,6 +5,7 @@ //! - Generate the test that will compare the output of running the .wasm file //! with wasmer with the expected output use glob::glob; +use std::collections::HashSet; use std::fs; use std::path::Path; use std::path::PathBuf; @@ -20,7 +21,7 @@ static BANNER: &str = "// Rust test file autogenerated with cargo build (build/e const EXTENSIONS: [&str; 2] = ["c", "cpp"]; const EXCLUDES: [&str; 0] = []; -pub fn compile(file: &str, ignores: &Vec) -> Option { +pub fn compile(file: &str, ignores: &HashSet) -> Option { let mut output_path = PathBuf::from(file); output_path.set_extension("out"); // let output_str = output_path.to_str().unwrap(); @@ -49,11 +50,7 @@ pub fn compile(file: &str, ignores: &Vec) -> Option { let wasm_file_metadata = { let mut wasm_file_path = PathBuf::from(file); wasm_file_path.set_extension("wasm"); - if let Ok(wasm_file) = File::open(wasm_file_path) { - Some(wasm_file.metadata().unwrap()) - } else { - None - } + File::open(wasm_file_path).and_then(|wf| wf.metadata()).ok() }; let real_file = File::open(file).unwrap(); @@ -111,10 +108,7 @@ pub fn compile(file: &str, ignores: &Vec) -> Option { "txt" }; - let ignored = if ignores - .iter() - .any(|i| &i.to_lowercase() == &module_name.to_lowercase()) - { + let ignored = if ignores.contains(&module_name.to_lowercase()) { "\n#[ignore]" } else { "" @@ -193,8 +187,11 @@ pub fn build() { } } -fn read_ignore_list() -> Vec { +fn read_ignore_list() -> HashSet { let f = File::open("emtests/ignores.txt").unwrap(); let f = BufReader::new(f); - f.lines().filter_map(Result::ok).collect() + f.lines() + .filter_map(Result::ok) + .map(|v| v.to_lowercase()) + .collect() } diff --git a/lib/emscripten/src/lib.rs b/lib/emscripten/src/lib.rs index 6b0340513..88237eb29 100644 --- a/lib/emscripten/src/lib.rs +++ b/lib/emscripten/src/lib.rs @@ -24,9 +24,6 @@ use wasmer_runtime_core::{ #[macro_use] mod macros; -//#[cfg(test)] -mod file_descriptor; -pub mod stdio; // EMSCRIPTEN APIS mod bitwise; diff --git a/lib/emscripten/tests/emtests/_common.rs b/lib/emscripten/tests/emtests/_common.rs index c2e304e32..43c1aecbf 100644 --- a/lib/emscripten/tests/emtests/_common.rs +++ b/lib/emscripten/tests/emtests/_common.rs @@ -4,9 +4,11 @@ macro_rules! assert_emscripten_output { use wasmer_emscripten::{ EmscriptenGlobals, generate_emscripten_env, - stdio::StdioCapturer }; - use wasmer_runtime_core::backend::Compiler; + use wasmer_runtime_core::{ + backend::Compiler, + }; + use wasmer_dev_utils::stdio::StdioCapturer; #[cfg(feature = "clif")] fn get_compiler() -> impl Compiler { diff --git a/lib/runtime-core/Cargo.toml b/lib/runtime-core/Cargo.toml index 345125c0f..3ea4ad237 100644 --- a/lib/runtime-core/Cargo.toml +++ b/lib/runtime-core/Cargo.toml @@ -49,4 +49,4 @@ blake2b_simd = "0.4.1" rustc_version = "0.2.3" [features] -debug = [] +debug = [] \ No newline at end of file diff --git a/lib/wasi/Cargo.toml b/lib/wasi/Cargo.toml index 98dca78cf..efd64270e 100644 --- a/lib/wasi/Cargo.toml +++ b/lib/wasi/Cargo.toml @@ -6,6 +6,7 @@ license = "MIT" authors = ["The Wasmer Engineering Team "] repository = "https://github.com/wasmerio/wasmer" edition = "2018" +build = "build/mod.rs" [dependencies] wasmer-runtime-core = { path = "../runtime-core", version = "0.4.2" } @@ -16,3 +17,16 @@ hashbrown = "0.1.8" generational-arena = "0.2.2" log = "0.4.6" byteorder = "1.3.1" +# hack to get tests to work +wasmer-singlepass-backend = { path = "../singlepass-backend", version = "0.4.2", optional = true } + +[build-dependencies] +glob = "0.2.11" + +[dev-dependencies] +wasmer-clif-backend = { path = "../clif-backend", version = "0.4.2" } +wasmer-dev-utils = { path = "../dev-utils", version = "0.4.2"} + +[features] +clif = [] +singlepass = ["wasmer-singlepass-backend"] diff --git a/lib/wasi/build/mod.rs b/lib/wasi/build/mod.rs new file mode 100644 index 000000000..778ae9ea5 --- /dev/null +++ b/lib/wasi/build/mod.rs @@ -0,0 +1,11 @@ +use std::env; + +mod wasitests; + +static WASITESTS_ENV_VAR: &str = "WASM_WASI_GENERATE_WASITESTS"; + +fn main() { + if env::var(WASITESTS_ENV_VAR).unwrap_or("0".to_string()) == "1" { + wasitests::build(); + } +} diff --git a/lib/wasi/build/wasitests.rs b/lib/wasi/build/wasitests.rs new file mode 100644 index 000000000..6d954373e --- /dev/null +++ b/lib/wasi/build/wasitests.rs @@ -0,0 +1,145 @@ +//! This file will run at build time to autogenerate the WASI regression tests +//! It will compile the files indicated in TESTS, to:executable and .wasm +//! - Compile with the native rust target to get the expected output +//! - Compile with the latest WASI target to get the wasm +//! - Generate the test that will compare the output of running the .wasm file +//! with wasmer with the expected output + +use glob::glob; +use std::collections::HashSet; +use std::fs; +use std::path::PathBuf; +use std::process::Command; + +use std::fs::File; +use std::io::prelude::*; +use std::io::BufReader; + +static BANNER: &str = "// !!! THIS IS A GENERATED FILE !!! +// ANY MANUAL EDITS MAY BE OVERWRITTEN AT ANY TIME +// Files autogenerated with cargo build (build/wasitests.rs).\n"; + +pub fn compile(file: &str, ignores: &HashSet) -> Option { + dbg!(file); + let mut output_path = PathBuf::from(file); + output_path.set_extension("out"); + + assert!(file.ends_with(".rs")); + let normalized_name = { + let mut nn = file.to_lowercase(); + nn.truncate(file.len() - 3); + nn + }; + + Command::new("rustc") + .arg("+nightly") + .arg(file) + .arg("-o") + .arg(&normalized_name) + .output() + .expect("Failed to compile program to native code"); + + #[cfg(unix)] + { + use std::os::unix::fs::PermissionsExt; + let normal_path = PathBuf::from(&normalized_name); + let mut perm = normal_path + .metadata() + .expect("native executable") + .permissions(); + perm.set_mode(0o766); + fs::set_permissions(normal_path, perm).expect("set permissions"); + } + let rs_module_name = { + let temp = PathBuf::from(&normalized_name); + temp.file_name().unwrap().to_string_lossy().to_string() + }; + + let result = Command::new(&normalized_name) + .output() + .expect("Failed to execute native program"); + let wasm_out_name = format!("{}.wasm", &normalized_name); + + Command::new("rustc") + .arg("+nightly") + .arg("--target=wasm32-wasi") + .arg(file) + .arg("-o") + .arg(&wasm_out_name) + .output() + .expect("Failed to compile program to native code"); + + let ignored = if ignores.contains(&rs_module_name) { + "\n#[ignore]" + } else { + "" + }; + + let contents = format!( + "#[test]{ignore} +fn test_{rs_module_name}() {{ + assert_wasi_output!( + \"../../{module_path}\", + \"{rs_module_name}\", + vec![], + \"../../{test_output_path}\" + ); +}} +", + ignore = ignored, + module_path = wasm_out_name, + rs_module_name = rs_module_name, + test_output_path = format!("{}.out", normalized_name), + ); + let rust_test_filepath = format!( + concat!(env!("CARGO_MANIFEST_DIR"), "/tests/{}.rs"), + normalized_name, + ); + fs::write(&rust_test_filepath, contents.as_bytes()).expect("writing test file"); + fs::write(&output_path, result.stdout).expect("writing output to file"); + + Some(rs_module_name) +} + +pub fn build() { + let rust_test_modpath = concat!(env!("CARGO_MANIFEST_DIR"), "/tests/wasitests/mod.rs"); + + let mut modules: Vec = Vec::new(); + + let ignores = read_ignore_list(); + for entry in glob("wasitests/*.rs").unwrap() { + match entry { + Ok(path) => { + let test = path.to_str().unwrap(); + if let Some(module_name) = compile(test, &ignores) { + modules.push(module_name); + } + } + Err(e) => println!("{:?}", e), + } + } + modules.sort(); + let mut modules: Vec = modules.iter().map(|m| format!("mod {};", m)).collect(); + assert!(modules.len() > 0, "Expected > 0 modules found"); + + modules.insert(0, BANNER.to_string()); + modules.insert(1, "// The _common module is not autogenerated. It provides common macros for the wasitests\n#[macro_use]\nmod _common;".to_string()); + // We add an empty line + modules.push("".to_string()); + + let modfile: String = modules.join("\n"); + let source = fs::read(&rust_test_modpath).unwrap(); + // We only modify the mod file if has changed + if source != modfile.as_bytes() { + fs::write(&rust_test_modpath, modfile.as_bytes()).unwrap(); + } +} + +fn read_ignore_list() -> HashSet { + let f = File::open("wasitests/ignores.txt").unwrap(); + let f = BufReader::new(f); + f.lines() + .filter_map(Result::ok) + .map(|v| v.to_lowercase()) + .collect() +} diff --git a/lib/wasi/tests/wasitest.rs b/lib/wasi/tests/wasitest.rs new file mode 100644 index 000000000..a89a8f1a3 --- /dev/null +++ b/lib/wasi/tests/wasitest.rs @@ -0,0 +1 @@ +mod wasitests; diff --git a/lib/wasi/tests/wasitests/_common.rs b/lib/wasi/tests/wasitests/_common.rs new file mode 100644 index 000000000..687324e13 --- /dev/null +++ b/lib/wasi/tests/wasitests/_common.rs @@ -0,0 +1,61 @@ +macro_rules! assert_wasi_output { + ($file:expr, $name:expr, $args:expr, $expected:expr) => {{ + use wasmer_dev_utils::stdio::StdioCapturer; + use wasmer_runtime_core::{backend::Compiler, Func}; + use wasmer_wasi::generate_import_object; + + #[cfg(feature = "clif")] + fn get_compiler() -> impl Compiler { + use wasmer_clif_backend::CraneliftCompiler; + CraneliftCompiler::new() + } + + #[cfg(feature = "llvm")] + fn get_compiler() -> impl Compiler { + compile_error!("LLVM compiler not supported right now"); + } + + #[cfg(feature = "singlepass")] + fn get_compiler() -> impl Compiler { + use wasmer_singlepass_backend::SinglePassCompiler; + SinglePassCompiler::new() + } + + #[cfg(not(any(feature = "llvm", feature = "clif", feature = "singlepass")))] + fn get_compiler() -> impl Compiler { + compile_error!("compiler not specified, activate a compiler via features"); + unreachable!(); + } + + let wasm_bytes = include_bytes!($file); + + let module = wasmer_runtime_core::compile_with(&wasm_bytes[..], &get_compiler()) + .expect("WASM can't be compiled"); + + let import_object = generate_import_object(vec![], vec![], vec![".".to_string()]); + + let instance = module + .instantiate(&import_object) + .map_err(|err| format!("Can't instantiate the WebAssembly module: {:?}", err)) + .unwrap(); // NOTE: Need to figure what the unwrap is for ?? + + let capturer = StdioCapturer::new(); + + let start: Func<(), ()> = instance + .func("_start") + .map_err(|e| format!("{:?}", e)) + .expect("start function in wasi module"); + + let _result = start.call(); + + let output = capturer.end().unwrap().0; + let expected_output = include_str!($expected); + + assert!( + output.contains(expected_output), + "Output: `{}` does not contain expected output: `{}`", + output, + expected_output + ); + }}; +} diff --git a/lib/wasi/tests/wasitests/create_dir.rs b/lib/wasi/tests/wasitests/create_dir.rs new file mode 100644 index 000000000..1980907f2 --- /dev/null +++ b/lib/wasi/tests/wasitests/create_dir.rs @@ -0,0 +1,10 @@ +#[test] +#[ignore] +fn test_create_dir() { + assert_wasi_output!( + "../../wasitests/create_dir.wasm", + "create_dir", + vec![], + "../../wasitests/create_dir.out" + ); +} diff --git a/lib/wasi/tests/wasitests/file_metadata.rs b/lib/wasi/tests/wasitests/file_metadata.rs new file mode 100644 index 000000000..70bb6de9b --- /dev/null +++ b/lib/wasi/tests/wasitests/file_metadata.rs @@ -0,0 +1,10 @@ +#[test] +#[ignore] +fn test_file_metadata() { + assert_wasi_output!( + "../../wasitests/file_metadata.wasm", + "file_metadata", + vec![], + "../../wasitests/file_metadata.out" + ); +} diff --git a/lib/wasi/tests/wasitests/hello.rs b/lib/wasi/tests/wasitests/hello.rs new file mode 100644 index 000000000..ffce165e6 --- /dev/null +++ b/lib/wasi/tests/wasitests/hello.rs @@ -0,0 +1,9 @@ +#[test] +fn test_hello() { + assert_wasi_output!( + "../../wasitests/hello.wasm", + "hello", + vec![], + "../../wasitests/hello.out" + ); +} diff --git a/lib/wasi/tests/wasitests/mod.rs b/lib/wasi/tests/wasitests/mod.rs new file mode 100644 index 000000000..743eed31c --- /dev/null +++ b/lib/wasi/tests/wasitests/mod.rs @@ -0,0 +1,11 @@ +// !!! THIS IS A GENERATED FILE !!! +// ANY MANUAL EDITS MAY BE OVERWRITTEN AT ANY TIME +// Files autogenerated with cargo build (build/wasitests.rs). + +// The _common module is not autogenerated. It provides common macros for the wasitests +#[macro_use] +mod _common; +mod create_dir; +mod file_metadata; +mod hello; +mod quine; diff --git a/lib/wasi/tests/wasitests/quine.rs b/lib/wasi/tests/wasitests/quine.rs new file mode 100644 index 000000000..156ded8a4 --- /dev/null +++ b/lib/wasi/tests/wasitests/quine.rs @@ -0,0 +1,9 @@ +#[test] +fn test_quine() { + assert_wasi_output!( + "../../wasitests/quine.wasm", + "quine", + vec![], + "../../wasitests/quine.out" + ); +} diff --git a/lib/wasi/wasitests/create_dir b/lib/wasi/wasitests/create_dir new file mode 100755 index 000000000..f83063c51 Binary files /dev/null and b/lib/wasi/wasitests/create_dir differ diff --git a/lib/wasi/wasitests/create_dir.out b/lib/wasi/wasitests/create_dir.out new file mode 100644 index 000000000..cc1a8aa2c --- /dev/null +++ b/lib/wasi/wasitests/create_dir.out @@ -0,0 +1,5 @@ +Test file exists: false +Dir exists: false +Dir exists: false +Dir exists: false +Success diff --git a/lib/wasi/wasitests/create_dir.rs b/lib/wasi/wasitests/create_dir.rs new file mode 100644 index 000000000..514387b02 --- /dev/null +++ b/lib/wasi/wasitests/create_dir.rs @@ -0,0 +1,37 @@ +use std::fs; +use std::io::{Read, Seek, SeekFrom, Write}; +use std::path::*; + +fn main() { + let mut path = PathBuf::from("wasitests/testing/nested/directories"); + let test_file = path.join("test.file"); + fs::create_dir_all(&path).unwrap(); + { + let mut file = fs::OpenOptions::new() + .read(true) + .write(true) + .truncate(true) + .create(true) + .open(&test_file) + .unwrap(); + + assert_eq!(file.write(b"hello").unwrap(), 5); + + file.flush().unwrap(); + file.seek(SeekFrom::Start(0)).unwrap(); + let mut in_str = String::new(); + file.read_to_string(&mut in_str).unwrap(); + assert_eq!(&in_str, "hello"); + } + fs::remove_file(&test_file).unwrap(); + println!("Test file exists: {}", test_file.exists()); + assert!(!test_file.exists()); + for _ in 0..3 { + fs::remove_dir_all(&path).unwrap(); + println!("Dir exists: {}", path.exists()); + assert!(!path.exists()); + path.pop(); + } + + println!("Success"); +} diff --git a/lib/wasi/wasitests/create_dir.wasm b/lib/wasi/wasitests/create_dir.wasm new file mode 100755 index 000000000..c92d95516 Binary files /dev/null and b/lib/wasi/wasitests/create_dir.wasm differ diff --git a/lib/wasi/wasitests/file_metadata b/lib/wasi/wasitests/file_metadata new file mode 100755 index 000000000..cd3728704 Binary files /dev/null and b/lib/wasi/wasitests/file_metadata differ diff --git a/lib/wasi/wasitests/file_metadata.out b/lib/wasi/wasitests/file_metadata.out new file mode 100644 index 000000000..d49e60f1d --- /dev/null +++ b/lib/wasi/wasitests/file_metadata.out @@ -0,0 +1,2 @@ +is dir: false +file info: FileType(FileType { mode: 33188 }) 419 Ok(SystemTime { tv_sec: 1558132188, tv_nsec: 545288295 }) Ok(SystemTime { tv_sec: 1558132188, tv_nsec: 545243056 }) Ok(SystemTime { tv_sec: 1558132191, tv_nsec: 359031112 }) diff --git a/lib/wasi/wasitests/file_metadata.rs b/lib/wasi/wasitests/file_metadata.rs new file mode 100644 index 000000000..7419a4f82 --- /dev/null +++ b/lib/wasi/wasitests/file_metadata.rs @@ -0,0 +1,17 @@ +use std::fs; +use std::io::Read; + +fn main() { + let mut this_file = + fs::File::open("wasitests/file_metadata.rs").expect("could not find src file"); + let md = this_file.metadata().unwrap(); + println!("is dir: {}", md.is_dir()); + println!( + "file info: {:?} {} {:?} {:?} {:?}", + md.file_type(), + md.len(), + md.modified(), + md.created(), + md.accessed() + ); +} diff --git a/lib/wasi/wasitests/file_metadata.wasm b/lib/wasi/wasitests/file_metadata.wasm new file mode 100755 index 000000000..2e2f702f9 Binary files /dev/null and b/lib/wasi/wasitests/file_metadata.wasm differ diff --git a/lib/wasi/wasitests/hello b/lib/wasi/wasitests/hello new file mode 100755 index 000000000..546d8907f Binary files /dev/null and b/lib/wasi/wasitests/hello differ diff --git a/lib/wasi/wasitests/hello.out b/lib/wasi/wasitests/hello.out new file mode 100644 index 000000000..af5626b4a --- /dev/null +++ b/lib/wasi/wasitests/hello.out @@ -0,0 +1 @@ +Hello, world! diff --git a/lib/wasi/wasitests/hello.rs b/lib/wasi/wasitests/hello.rs new file mode 100644 index 000000000..e7a11a969 --- /dev/null +++ b/lib/wasi/wasitests/hello.rs @@ -0,0 +1,3 @@ +fn main() { + println!("Hello, world!"); +} diff --git a/lib/wasi/wasitests/hello.wasm b/lib/wasi/wasitests/hello.wasm new file mode 100755 index 000000000..0001ccf6e Binary files /dev/null and b/lib/wasi/wasitests/hello.wasm differ diff --git a/lib/wasi/wasitests/ignores.txt b/lib/wasi/wasitests/ignores.txt new file mode 100644 index 000000000..aa48474bf --- /dev/null +++ b/lib/wasi/wasitests/ignores.txt @@ -0,0 +1,2 @@ +file_metadata +create_dir diff --git a/lib/wasi/wasitests/quine b/lib/wasi/wasitests/quine new file mode 100755 index 000000000..409c63048 Binary files /dev/null and b/lib/wasi/wasitests/quine differ diff --git a/lib/wasi/wasitests/quine.out b/lib/wasi/wasitests/quine.out new file mode 100644 index 000000000..13de73d05 --- /dev/null +++ b/lib/wasi/wasitests/quine.out @@ -0,0 +1,11 @@ +use std::fs; +use std::io::Read; + +fn main() { + let mut this_file = fs::File::open("wasitests/quine.rs").expect("could not find src file"); + let md = this_file.metadata().unwrap(); + let mut in_str = String::new(); + this_file.read_to_string(&mut in_str).unwrap(); + println!("{}", in_str); +} + diff --git a/lib/wasi/wasitests/quine.rs b/lib/wasi/wasitests/quine.rs new file mode 100644 index 000000000..6ca65ee8c --- /dev/null +++ b/lib/wasi/wasitests/quine.rs @@ -0,0 +1,10 @@ +use std::fs; +use std::io::Read; + +fn main() { + let mut this_file = fs::File::open("wasitests/quine.rs").expect("could not find src file"); + let md = this_file.metadata().unwrap(); + let mut in_str = String::new(); + this_file.read_to_string(&mut in_str).unwrap(); + println!("{}", in_str); +} diff --git a/lib/wasi/wasitests/quine.wasm b/lib/wasi/wasitests/quine.wasm new file mode 100755 index 000000000..937671762 Binary files /dev/null and b/lib/wasi/wasitests/quine.wasm differ