Skip to content

Commit

Permalink
Update some from feedback, fix tests
Browse files Browse the repository at this point in the history
format with 1.37
  • Loading branch information
Mark McCaskey committed Oct 2, 2019
1 parent 75286b5 commit 5185f12
Show file tree
Hide file tree
Showing 26 changed files with 52 additions and 31 deletions.
2 changes: 1 addition & 1 deletion lib/wasi-tests/build/wasitests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ pub fn compile(file: &str, ignores: &HashSet<String>) -> Option<String> {
out_str.push_str("vec![");

for entry in args.po_dirs {
out_str.push_str(&format!("\"{}\".to_string(),", entry));
out_str.push_str(&format!("std::path::PathBuf::from(\"{}\"),", entry));
}

out_str.push_str("]");
Expand Down
2 changes: 1 addition & 1 deletion lib/wasi-tests/tests/wasitests/create_dir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ fn test_create_dir() {
assert_wasi_output!(
"../../wasitests/create_dir.wasm",
"create_dir",
vec![".".to_string(),],
vec![std::path::PathBuf::from("."),],
vec![],
vec![],
"../../wasitests/create_dir.out"
Expand Down
2 changes: 1 addition & 1 deletion lib/wasi-tests/tests/wasitests/file_metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ fn test_file_metadata() {
assert_wasi_output!(
"../../wasitests/file_metadata.wasm",
"file_metadata",
vec![".".to_string(),],
vec![std::path::PathBuf::from("."),],
vec![],
vec![],
"../../wasitests/file_metadata.out"
Expand Down
2 changes: 1 addition & 1 deletion lib/wasi-tests/tests/wasitests/quine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ fn test_quine() {
assert_wasi_output!(
"../../wasitests/quine.wasm",
"quine",
vec![".".to_string(),],
vec![std::path::PathBuf::from("."),],
vec![],
vec![],
"../../wasitests/quine.out"
Expand Down
Binary file modified lib/wasi-tests/wasitests/close_preopen_fd.wasm
Binary file not shown.
Binary file modified lib/wasi-tests/wasitests/create_dir.wasm
Binary file not shown.
Binary file modified lib/wasi-tests/wasitests/envvar.wasm
Binary file not shown.
Binary file modified lib/wasi-tests/wasitests/fd_allocate.wasm
Binary file not shown.
Binary file modified lib/wasi-tests/wasitests/fd_pread.wasm
Binary file not shown.
Binary file modified lib/wasi-tests/wasitests/fd_read.wasm
Binary file not shown.
Binary file modified lib/wasi-tests/wasitests/fd_sync.wasm
Binary file not shown.
Binary file modified lib/wasi-tests/wasitests/file_metadata.wasm
Binary file not shown.
Binary file modified lib/wasi-tests/wasitests/fs_sandbox_test.wasm
Binary file not shown.
Binary file modified lib/wasi-tests/wasitests/fseek.wasm
Binary file not shown.
Binary file modified lib/wasi-tests/wasitests/hello.wasm
Binary file not shown.
Binary file modified lib/wasi-tests/wasitests/mapdir.wasm
Binary file not shown.
Binary file modified lib/wasi-tests/wasitests/path_link.wasm
Binary file not shown.
Binary file modified lib/wasi-tests/wasitests/path_rename.wasm
Binary file not shown.
Binary file modified lib/wasi-tests/wasitests/path_symlink.wasm
Binary file not shown.
Binary file modified lib/wasi-tests/wasitests/poll_oneoff.wasm
Binary file not shown.
Binary file modified lib/wasi-tests/wasitests/quine.wasm
Binary file not shown.
Binary file modified lib/wasi-tests/wasitests/readlink.wasm
Binary file not shown.
Binary file modified lib/wasi-tests/wasitests/wasi_sees_virtual_root.wasm
Binary file not shown.
Binary file modified lib/wasi-tests/wasitests/writing.wasm
Binary file not shown.
50 changes: 23 additions & 27 deletions lib/wasi/src/state/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,14 @@ use crate::state::{WasiFs, WasiState};
use std::path::{Path, PathBuf};

/// Creates an empty [`WasiStateBuilder`].
pub fn create_wasi_state() -> WasiStateBuilder {
WasiStateBuilder::default()
pub(crate) fn create_wasi_state(program_name: &str) -> WasiStateBuilder {
WasiStateBuilder {
args: vec![program_name.bytes().collect()],
..WasiStateBuilder::default()
}
}

/// Type for building an instance of [`WasiState`]
///
/// Usage:
///
/// ```
/// # use wasmer_wasi::state::create_wasi_state;
/// create_wasi_state()
/// .env(b"HOME", "/home/home".to_string())
/// .arg("--help")
/// .envs({ let mut hm = std::collections::HashMap::new();
/// hm.insert("COLOR_OUTPUT", "TRUE");
/// hm.insert("PATH", "/usr/bin");
/// hm
/// })
/// .args(&["--verbose", "list"])
/// .preopen_dir("src")
/// .map_dir("dot", ".")
/// .build()
/// .unwrap();
/// ```
#[derive(Debug, Default, Clone, PartialEq)]
pub struct WasiStateBuilder {
args: Vec<Vec<u8>>,
Expand Down Expand Up @@ -66,6 +50,8 @@ fn validate_mapped_dir_alias(alias: &str) -> Result<(), WasiStateCreationError>
Ok(())
}

// TODO add other WasiFS APIs here like swapping out stdout, for example (though we need to
// return stdout somehow, it's unclear what that API should look like)
impl WasiStateBuilder {
/// Add an environment variable pair.
/// Environment variable keys and values must not contain the byte `=` (0x3d)
Expand Down Expand Up @@ -192,12 +178,16 @@ impl WasiStateBuilder {
///
/// Returns the error from `WasiFs::new` if there's an error
pub fn build(&mut self) -> Result<WasiState, WasiStateCreationError> {
for arg in self.args.iter() {
for (i, arg) in self.args.iter().enumerate() {
for b in arg.iter() {
if *b == 0 {
return Err(WasiStateCreationError::ArgumentContainsNulByte(
std::str::from_utf8(arg)
.unwrap_or("Inner error: arg is invalid_utf8!")
.unwrap_or(if i == 0 {
"Inner error: program name is invalid utf8!"
} else {
"Inner error: arg is invalid utf8!"
})
.to_string(),
));
}
Expand Down Expand Up @@ -264,13 +254,17 @@ mod test {

#[test]
fn env_var_errors() {
let output = create_wasi_state().env("HOM=E", "/home/home").build();
let output = create_wasi_state("test_prog")
.env("HOM=E", "/home/home")
.build();
match output {
Err(WasiStateCreationError::EnvironmentVariableFormatError(_)) => assert!(true),
_ => assert!(false),
}

let output = create_wasi_state().env("HOME\0", "/home/home").build();
let output = create_wasi_state("test_prog")
.env("HOME\0", "/home/home")
.build();
match output {
Err(WasiStateCreationError::EnvironmentVariableFormatError(_)) => assert!(true),
_ => assert!(false),
Expand All @@ -279,12 +273,14 @@ mod test {

#[test]
fn nul_character_in_args() {
let output = create_wasi_state().arg("--h\0elp").build();
let output = create_wasi_state("test_prog").arg("--h\0elp").build();
match output {
Err(WasiStateCreationError::ArgumentContainsNulByte(_)) => assert!(true),
_ => assert!(false),
}
let output = create_wasi_state().args(&["--help", "--wat\0"]).build();
let output = create_wasi_state("test_prog")
.args(&["--help", "--wat\0"])
.build();
match output {
Err(WasiStateCreationError::ArgumentContainsNulByte(_)) => assert!(true),
_ => assert!(false),
Expand Down
25 changes: 25 additions & 0 deletions lib/wasi/src/state/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1028,6 +1028,31 @@ pub struct WasiState {
}

impl WasiState {
/// Create a [`WasiStateBuilder`] to construct a validated instance of
/// [`WasiState`].
///
/// Usage:
///
/// ```
/// # use wasmer_wasi::state::WasiState;
/// WasiState::new("program_name")
/// .env(b"HOME", "/home/home".to_string())
/// .arg("--help")
/// .envs({ let mut hm = std::collections::HashMap::new();
/// hm.insert("COLOR_OUTPUT", "TRUE");
/// hm.insert("PATH", "/usr/bin");
/// hm
/// })
/// .args(&["--verbose", "list"])
/// .preopen_dir("src")
/// .map_dir("dot", ".")
/// .build()
/// .unwrap();
/// ```
pub fn new(program_name: &str) -> WasiStateBuilder {
create_wasi_state(program_name)
}

/// Turn the WasiState into bytes
pub fn freeze(&self) -> Option<Vec<u8>> {
bincode::serialize(self).ok()
Expand Down

0 comments on commit 5185f12

Please sign in to comment.