add fd_sync test

This commit is contained in:
Mark McCaskey 2019-08-07 12:41:35 +09:00
parent dbe96bbce9
commit 6ad0be1e02
8 changed files with 74 additions and 7 deletions

View File

@ -0,0 +1,14 @@
#[test]
fn test_fd_sync() {
assert_wasi_output!(
"../../wasitests/fd_sync.wasm",
"fd_sync",
vec![],
vec![(
".".to_string(),
::std::path::PathBuf::from("wasitests/test_fs/temp")
),],
vec![],
"../../wasitests/fd_sync.out"
);
}

View File

@ -9,6 +9,7 @@ mod create_dir;
mod envvar;
mod fd_allocate;
mod fd_pread;
mod fd_sync;
mod file_metadata;
mod fs_sandbox_test;
mod fseek;

View File

@ -6,13 +6,6 @@ use std::fs;
use std::os::wasi::prelude::AsRawFd;
use std::path::PathBuf;
#[cfg(target_os = "wasi")]
#[repr(C)]
struct WasiIovec {
pub buf: u32,
pub buf_len: u32,
}
#[cfg(target_os = "wasi")]
#[link(wasm_import_module = "wasi_unstable")]
extern "C" {

View File

@ -0,0 +1,2 @@
170
1404

View File

@ -0,0 +1,56 @@
// Args:
// mapdir: .:wasitests/test_fs/temp
use std::fs;
use std::path::PathBuf;
#[cfg(target_os = "wasi")]
#[link(wasm_import_module = "wasi_unstable")]
extern "C" {
fn fd_sync(fd: u32) -> u16;
}
#[cfg(target_os = "wasi")]
fn sync(fd: u32) -> u16 {
unsafe { fd_sync(fd) }
}
fn main() {
#[cfg(target_os = "wasi")]
let mut base = PathBuf::from(".");
#[cfg(target_os = "wasi")]
{
base.push("fd_sync_file.txt");
let mut file = fs::OpenOptions::new()
.create(true)
.write(true)
.truncate(true)
.open(&base)
.expect("Could not create file");
let mut buffer = [0u8; 64];
{
use std::io::Write;
// example text from https://www.un.org/en/universal-declaration-human-rights/
file.write_all(b"All human beings are born free and equal in dignity and rights. They are endowed with reason and conscience and should act towards one another in a spirit of brotherhood.").unwrap();
file.sync_all();
let len = file.metadata().unwrap().len();
println!("{}", len);
assert_eq!(len, 170);
file.set_len(170 + 1234);
file.sync_all();
let len = file.metadata().unwrap().len();
println!("{}", len);
assert_eq!(len, 1234 + 170);
}
}
#[cfg(target_os = "wasi")]
std::fs::remove_file(&base).unwrap();
#[cfg(not(target_os = "wasi"))]
{
// eh, just print the output directly
println!("170");
println!("1404");
}
}

Binary file not shown.

View File

@ -1146,6 +1146,7 @@ pub fn fd_seek(
/// - `__WASI_ENOTCAPABLE`
pub fn fd_sync(ctx: &mut Ctx, fd: __wasi_fd_t) -> __wasi_errno_t {
debug!("wasi::fd_sync");
debug!("=> fd={}", fd);
let memory = ctx.memory(0);
let state = get_wasi_state(ctx);
let fd_entry = wasi_try!(state.fs.get_fd(fd));