Added more tests for the dev files and added urandom support

This commit is contained in:
Johnathan Sharratt
2023-03-16 16:46:35 +11:00
parent d7e21293fe
commit 0b8d2b1248
41 changed files with 311 additions and 34 deletions

1
Cargo.lock generated
View File

@@ -5212,6 +5212,7 @@ dependencies = [
"derivative",
"filetime",
"fs_extra",
"getrandom",
"indexmap",
"lazy_static",
"libc",

View File

@@ -24,6 +24,12 @@ tokio = { version = "1", features = [ "io-util", "sync", "macros" ], default_fea
pin-project-lite = "0.2.9"
indexmap = "1.9.2"
[target.'cfg(not(all(target_arch = "wasm32", target_os = "unknown")))'.dependencies]
getrandom = { version = "0.2" }
[target.'cfg(all(target_arch = "wasm32", target_os = "unknown"))'.dependencies]
getrandom = { version = "0.2", features = [ "js" ] }
[dev-dependencies]
tempfile = "3.4.0"
tokio = { version = "1", features = [ "io-util", "rt" ], default_features = false }

View File

@@ -1,3 +1,4 @@
use crate::random_file::RandomFile;
use crate::{FileSystem, VirtualFile};
use std::path::{Path, PathBuf};
use tracing::*;
@@ -81,6 +82,10 @@ impl RootFileSystemBuilder {
let _ = tmp
.new_open_options_ext()
.insert_device_file(PathBuf::from("/dev/zero"), Box::new(ZeroFile::default()));
let _ = tmp.new_open_options_ext().insert_device_file(
PathBuf::from("/dev/urandom"),
Box::new(RandomFile::default()),
);
let _ = tmp.new_open_options_ext().insert_device_file(
PathBuf::from("/dev/stdin"),
self.stdin

View File

@@ -20,6 +20,7 @@ pub mod host_fs;
pub mod mem_fs;
pub mod null_file;
pub mod passthru_fs;
pub mod random_file;
pub mod special_file;
pub mod tmp_fs;
pub mod union_fs;

View File

@@ -0,0 +1,88 @@
//! Used for /dev/zero - infinitely returns zero
//! which is useful for commands like `dd if=/dev/zero of=bigfile.img size=1G`
use std::io::{self, *};
use std::pin::Pin;
use std::task::{Context, Poll};
use tokio::io::{AsyncRead, AsyncSeek, AsyncWrite};
use crate::VirtualFile;
#[derive(Debug, Default)]
pub struct RandomFile {}
impl AsyncSeek for RandomFile {
fn start_seek(self: Pin<&mut Self>, _position: SeekFrom) -> io::Result<()> {
Ok(())
}
fn poll_complete(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<io::Result<u64>> {
Poll::Ready(Ok(0))
}
}
impl AsyncWrite for RandomFile {
fn poll_write(
self: Pin<&mut Self>,
_cx: &mut Context<'_>,
buf: &[u8],
) -> Poll<io::Result<usize>> {
Poll::Ready(Ok(buf.len()))
}
fn poll_flush(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<io::Result<()>> {
Poll::Ready(Ok(()))
}
fn poll_shutdown(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<io::Result<()>> {
Poll::Ready(Ok(()))
}
fn poll_write_vectored(
self: Pin<&mut Self>,
_cx: &mut Context<'_>,
bufs: &[IoSlice<'_>],
) -> Poll<io::Result<usize>> {
Poll::Ready(Ok(bufs.len()))
}
fn is_write_vectored(&self) -> bool {
false
}
}
impl AsyncRead for RandomFile {
fn poll_read(
self: Pin<&mut Self>,
_cx: &mut Context<'_>,
buf: &mut tokio::io::ReadBuf<'_>,
) -> Poll<io::Result<()>> {
let mut data = vec![0u8; buf.remaining()];
getrandom::getrandom(&mut data).ok();
buf.put_slice(&data[..]);
Poll::Ready(Ok(()))
}
}
impl VirtualFile for RandomFile {
fn last_accessed(&self) -> u64 {
0
}
fn last_modified(&self) -> u64 {
0
}
fn created_time(&self) -> u64 {
0
}
fn size(&self) -> u64 {
0
}
fn set_len(&mut self, _new_size: u64) -> crate::Result<()> {
Ok(())
}
fn unlink(&mut self) -> crate::Result<()> {
Ok(())
}
fn poll_read_ready(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<io::Result<usize>> {
Poll::Ready(Ok(0))
}
fn poll_write_ready(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<io::Result<usize>> {
Poll::Ready(Ok(0))
}
}

View File

@@ -10,6 +10,15 @@ use insta::assert_json_snapshot;
use tempfile::NamedTempFile;
use wasmer_integration_tests_cli::get_wasmer_path;
#[derive(serde::Serialize, serde::Deserialize, Clone, PartialEq, Eq)]
pub struct TestSpecHttp {
pub url: String,
pub port: u16,
pub http_code: u16,
#[serde(skip_serializing)]
pub expected_response: Vec<u8>,
}
#[derive(serde::Serialize, serde::Deserialize, Clone, PartialEq, Eq)]
pub struct TestSpec {
pub name: Option<String>,
@@ -23,6 +32,7 @@ pub struct TestSpec {
pub debug_output: bool,
pub enable_threads: bool,
pub enable_network: bool,
pub http_request: Option<TestSpecHttp>,
}
impl std::fmt::Debug for TestSpec {
@@ -75,6 +85,7 @@ impl TestBuilder {
debug_output: false,
enable_threads: true,
enable_network: false,
http_request: None,
},
}
}
@@ -397,7 +408,14 @@ fn test_snapshot_execve() {
fn test_snapshot_web_server() {
let snapshot = TestBuilder::new()
.with_name(function!())
.run_wasm(include_bytes!("./wasm/web-server.wasm"));
.enable_network(true)
.use_coreutils()
.use_pkg("sharrattj/wasmer-sh")
.stdin_str(r#"
rm -f /cfg/config.toml
/bin/webserver --log-level info --root /public --port 7777
"#)
.run_wasm(include_bytes!("./wasm/dash.wasm"));
assert_json_snapshot!(snapshot);
}
*/
@@ -581,6 +599,26 @@ fn test_snapshot_dash_python() {
assert_json_snapshot!(snapshot);
}
#[test]
fn test_snapshot_dash_dev_zero() {
let snapshot = TestBuilder::new()
.with_name(function!())
.use_coreutils()
.stdin_str("head -c 10 /dev/zero")
.run_wasm(include_bytes!("./wasm/dash.wasm"));
assert_json_snapshot!(snapshot);
}
#[test]
fn test_snapshot_dash_dev_urandom() {
let snapshot = TestBuilder::new()
.with_name(function!())
.use_coreutils()
.stdin_str("head -c 10 /dev/urandom | wc -c")
.run_wasm(include_bytes!("./wasm/dash.wasm"));
assert_json_snapshot!(snapshot);
}
#[test]
fn test_snapshot_dash_dash() {
let snapshot = TestBuilder::new()

View File

@@ -46,7 +46,8 @@ expression: snapshot
],
"debug_output": false,
"enable_threads": true,
"enable_network": false
"enable_network": false,
"http_request": null
},
"result": {
"Success": {

View File

@@ -42,7 +42,8 @@ expression: snapshot
],
"debug_output": false,
"enable_threads": true,
"enable_network": false
"enable_network": false,
"http_request": null
},
"result": {
"Success": {

View File

@@ -22,7 +22,8 @@ expression: snapshot
],
"debug_output": false,
"enable_threads": true,
"enable_network": false
"enable_network": false,
"http_request": null
},
"result": {
"Success": {

View File

@@ -22,7 +22,8 @@ expression: snapshot
],
"debug_output": false,
"enable_threads": true,
"enable_network": false
"enable_network": false,
"http_request": null
},
"result": {
"Success": {

View File

@@ -36,7 +36,8 @@ expression: snapshot
],
"debug_output": false,
"enable_threads": true,
"enable_network": false
"enable_network": false,
"http_request": null
},
"result": {
"Success": {

View File

@@ -62,7 +62,8 @@ expression: snapshot
],
"debug_output": false,
"enable_threads": true,
"enable_network": false
"enable_network": false,
"http_request": null
},
"result": {
"Success": {

View File

@@ -18,7 +18,8 @@ expression: snapshot
],
"debug_output": false,
"enable_threads": true,
"enable_network": false
"enable_network": false,
"http_request": null
},
"result": {
"Success": {

View File

@@ -10,7 +10,8 @@ expression: snapshot
"stdin": null,
"debug_output": true,
"enable_threads": true,
"enable_network": false
"enable_network": false,
"http_request": null
},
"result": {
"Success": {

View File

@@ -16,7 +16,8 @@ expression: snapshot
],
"debug_output": false,
"enable_threads": true,
"enable_network": false
"enable_network": false,
"http_request": null
},
"result": {
"Success": {

View File

@@ -42,7 +42,8 @@ expression: snapshot
],
"debug_output": false,
"enable_threads": true,
"enable_network": false
"enable_network": false,
"http_request": null
},
"result": {
"Success": {

View File

@@ -42,7 +42,8 @@ expression: snapshot
],
"debug_output": false,
"enable_threads": true,
"enable_network": false
"enable_network": false,
"http_request": null
},
"result": {
"Success": {

View File

@@ -0,0 +1,58 @@
---
source: tests/integration/cli/tests/snapshot.rs
assertion_line: 642
expression: snapshot
---
{
"spec": {
"name": "snapshot::test_snapshot_dash_dev_urandom",
"use_packages": [
"sharrattj/coreutils"
],
"cli_args": [],
"stdin": [
104,
101,
97,
100,
32,
45,
99,
32,
49,
48,
32,
47,
100,
101,
118,
47,
117,
114,
97,
110,
100,
111,
109,
32,
124,
32,
119,
99,
32,
45,
99
],
"debug_output": false,
"enable_threads": true,
"enable_network": false,
"http_request": null
},
"result": {
"Success": {
"stdout": "10\n",
"stderr": "# # \n",
"exit_code": 0
}
}
}

View File

@@ -0,0 +1,47 @@
---
source: tests/integration/cli/tests/snapshot.rs
assertion_line: 622
expression: snapshot
---
{
"spec": {
"name": "snapshot::test_snapshot_dash_dev_zero",
"use_packages": [
"sharrattj/coreutils"
],
"cli_args": [],
"stdin": [
104,
101,
97,
100,
32,
45,
99,
32,
49,
48,
32,
47,
100,
101,
118,
47,
122,
101,
114,
111
],
"debug_output": false,
"enable_threads": true,
"enable_network": false,
"http_request": null
},
"result": {
"Success": {
"stdout": "\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000",
"stderr": "# # \n",
"exit_code": 0
}
}
}

View File

@@ -17,7 +17,8 @@ expression: snapshot
],
"debug_output": false,
"enable_threads": true,
"enable_network": false
"enable_network": false,
"http_request": null
},
"result": {
"Success": {

View File

@@ -30,7 +30,8 @@ expression: snapshot
],
"debug_output": false,
"enable_threads": true,
"enable_network": false
"enable_network": false,
"http_request": null
},
"result": {
"Success": {

View File

@@ -61,7 +61,8 @@ expression: snapshot
],
"debug_output": false,
"enable_threads": true,
"enable_network": false
"enable_network": false,
"http_request": null
},
"result": {
"Success": {

View File

@@ -12,7 +12,8 @@ expression: snapshot
"stdin": null,
"debug_output": false,
"enable_threads": true,
"enable_network": false
"enable_network": false,
"http_request": null
},
"result": {
"Success": {

View File

@@ -11,7 +11,8 @@ expression: snapshot
"stdin": null,
"debug_output": false,
"enable_threads": true,
"enable_network": false
"enable_network": false,
"http_request": null
},
"result": {
"Success": {

View File

@@ -13,7 +13,8 @@ expression: snapshot
"stdin": null,
"debug_output": false,
"enable_threads": true,
"enable_network": false
"enable_network": false,
"http_request": null
},
"result": {
"Success": {

View File

@@ -19,7 +19,8 @@ expression: snapshot
],
"debug_output": false,
"enable_threads": true,
"enable_network": false
"enable_network": false,
"http_request": null
},
"result": {
"Success": {

View File

@@ -12,7 +12,8 @@ expression: snapshot
"stdin": null,
"debug_output": false,
"enable_threads": true,
"enable_network": false
"enable_network": false,
"http_request": null
},
"result": {
"Success": {

View File

@@ -12,7 +12,8 @@ expression: snapshot
"stdin": null,
"debug_output": false,
"enable_threads": true,
"enable_network": false
"enable_network": false,
"http_request": null
},
"result": {
"Success": {

View File

@@ -12,7 +12,8 @@ expression: snapshot
"stdin": null,
"debug_output": false,
"enable_threads": true,
"enable_network": false
"enable_network": false,
"http_request": null
},
"result": {
"Success": {

View File

@@ -12,7 +12,8 @@ expression: snapshot
"stdin": null,
"debug_output": false,
"enable_threads": true,
"enable_network": false
"enable_network": false,
"http_request": null
},
"result": {
"Success": {

View File

@@ -10,7 +10,8 @@ expression: snapshot
"stdin": null,
"debug_output": false,
"enable_threads": true,
"enable_network": false
"enable_network": false,
"http_request": null
},
"result": {
"Success": {

View File

@@ -10,7 +10,8 @@ expression: snapshot
"stdin": null,
"debug_output": true,
"enable_threads": true,
"enable_network": false
"enable_network": false,
"http_request": null
},
"result": {
"Success": {

View File

@@ -12,7 +12,8 @@ expression: snapshot
"stdin": null,
"debug_output": false,
"enable_threads": true,
"enable_network": false
"enable_network": false,
"http_request": null
},
"result": {
"Success": {

View File

@@ -12,7 +12,8 @@ expression: snapshot
"stdin": null,
"debug_output": false,
"enable_threads": true,
"enable_network": false
"enable_network": false,
"http_request": null
},
"result": {
"Success": {

View File

@@ -26,7 +26,8 @@ expression: snapshot
],
"debug_output": false,
"enable_threads": true,
"enable_network": false
"enable_network": false,
"http_request": null
},
"result": {
"Success": {

View File

@@ -11,7 +11,8 @@ expression: snapshot
"stdin": null,
"debug_output": false,
"enable_threads": true,
"enable_network": false
"enable_network": false,
"http_request": null
},
"result": {
"Success": {

View File

@@ -10,7 +10,8 @@ expression: snapshot
"stdin": null,
"debug_output": false,
"enable_threads": true,
"enable_network": false
"enable_network": false,
"http_request": null
},
"result": {
"Success": {

View File

@@ -18,7 +18,8 @@ expression: snapshot
],
"debug_output": false,
"enable_threads": true,
"enable_network": false
"enable_network": false,
"http_request": null
},
"result": {
"Success": {

View File

@@ -12,7 +12,8 @@ expression: snapshot
"cli_args": [],
"stdin": null,
"debug_output": false,
"enable_threads": true
"enable_threads": true,
"http_request": null
},
"result": {
"Success": {

View File

@@ -10,7 +10,8 @@ expression: snapshot
"stdin": null,
"debug_output": false,
"enable_threads": true,
"enable_network": false
"enable_network": false,
"http_request": null
},
"result": {
"Success": {

View File

@@ -12,7 +12,8 @@ expression: snapshot
"stdin": null,
"debug_output": false,
"enable_threads": true,
"enable_network": false
"enable_network": false,
"http_request": null
},
"result": {
"Success": {