From 41b47215129b85bed1055daeb6cb6d9b507912a0 Mon Sep 17 00:00:00 2001 From: Peter Huene Date: Fri, 10 Jul 2020 13:49:55 -0700 Subject: [PATCH] Add a test case for WASI file truncation. --- crates/test-programs/build.rs | 4 + .../wasi-tests/src/bin/file_truncation.rs | 83 +++++++++++++++++++ .../wasi-tests/src/bin/poll_oneoff.rs | 26 +++--- 3 files changed, 98 insertions(+), 15 deletions(-) create mode 100644 crates/test-programs/wasi-tests/src/bin/file_truncation.rs diff --git a/crates/test-programs/build.rs b/crates/test-programs/build.rs index aab9d66d2d..93f60ae649 100644 --- a/crates/test-programs/build.rs +++ b/crates/test-programs/build.rs @@ -218,6 +218,8 @@ mod wasi_tests { // TODO: virtfs does not support rename yet. "path_rename_trailing_slashes_virtualfs" | "path_rename_virtualfs" => true, + // TODO: virtfs does not support truncation yet. + "file_truncation_virtualfs" => true, _ => false, } } else { @@ -250,6 +252,8 @@ mod wasi_tests { // TODO: virtfs does not support rename yet. "path_rename_trailing_slashes_virtualfs" | "path_rename_virtualfs" => true, + // TODO: virtfs does not support truncation yet. + "file_truncation_virtualfs" => true, _ => false, } } else { diff --git a/crates/test-programs/wasi-tests/src/bin/file_truncation.rs b/crates/test-programs/wasi-tests/src/bin/file_truncation.rs new file mode 100644 index 0000000000..35f511448a --- /dev/null +++ b/crates/test-programs/wasi-tests/src/bin/file_truncation.rs @@ -0,0 +1,83 @@ +use std::{env, process}; +use wasi_tests::open_scratch_directory; + +unsafe fn test_file_truncation(dir_fd: wasi::Fd) { + const FILENAME: &str = "test.txt"; + + // Open a file for writing + let file_fd = wasi::path_open( + dir_fd, + 0, + FILENAME, + wasi::OFLAGS_CREAT, + wasi::RIGHTS_FD_WRITE, + 0, + 0, + ) + .expect("creating a file for writing"); + + // Write to the file + let content = b"this content will be truncated!"; + let nwritten = wasi::fd_write( + file_fd, + &[wasi::Ciovec { + buf: content.as_ptr() as *const _, + buf_len: content.len(), + }], + ) + .expect("writing file content"); + assert_eq!(nwritten, content.len(), "nwritten bytes check"); + + wasi::fd_close(file_fd).expect("closing the file"); + + // Open the file for truncation + let file_fd = wasi::path_open( + dir_fd, + 0, + FILENAME, + wasi::OFLAGS_CREAT | wasi::OFLAGS_TRUNC, + wasi::RIGHTS_FD_WRITE | wasi::RIGHTS_FD_READ, + 0, + 0, + ) + .expect("creating a truncated file for reading"); + + // Read the file's contents + let buffer = &mut [0u8; 100]; + let nread = wasi::fd_read( + file_fd, + &[wasi::Iovec { + buf: buffer.as_mut_ptr(), + buf_len: buffer.len(), + }], + ) + .expect("reading file content"); + + // The file should be empty due to truncation + assert_eq!(nread, 0, "expected an empty file after truncation"); + + wasi::fd_close(file_fd).expect("closing the file"); +} + +fn main() { + let mut args = env::args(); + let prog = args.next().unwrap(); + let arg = if let Some(arg) = args.next() { + arg + } else { + eprintln!("usage: {} ", 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_file_truncation(dir_fd) } +} diff --git a/crates/test-programs/wasi-tests/src/bin/poll_oneoff.rs b/crates/test-programs/wasi-tests/src/bin/poll_oneoff.rs index 774ac1ab2a..7541643828 100644 --- a/crates/test-programs/wasi-tests/src/bin/poll_oneoff.rs +++ b/crates/test-programs/wasi-tests/src/bin/poll_oneoff.rs @@ -42,9 +42,7 @@ unsafe fn test_timeout() { userdata: CLOCK_ID, u: wasi::SubscriptionU { tag: wasi::EVENTTYPE_CLOCK, - u: wasi::SubscriptionUU { - clock - } + u: wasi::SubscriptionUU { clock }, }, }]; let out = poll_oneoff_impl(&r#in, 1); @@ -80,9 +78,7 @@ unsafe fn test_stdin_read() { userdata: CLOCK_ID, u: wasi::SubscriptionU { tag: wasi::EVENTTYPE_CLOCK, - u: wasi::SubscriptionUU { - clock - } + u: wasi::SubscriptionUU { clock }, }, }, // Make sure that timeout is returned only once even if there are multiple read events @@ -91,8 +87,8 @@ unsafe fn test_stdin_read() { u: wasi::SubscriptionU { tag: wasi::EVENTTYPE_FD_READ, u: wasi::SubscriptionUU { - fd_read: fd_readwrite - } + fd_read: fd_readwrite, + }, }, }, ]; @@ -127,7 +123,7 @@ unsafe fn test_stdout_stderr_write() { u: wasi::SubscriptionU { tag: wasi::EVENTTYPE_FD_WRITE, u: wasi::SubscriptionUU { - fd_write: stdout_readwrite + fd_write: stdout_readwrite, }, }, }, @@ -136,8 +132,8 @@ unsafe fn test_stdout_stderr_write() { u: wasi::SubscriptionU { tag: wasi::EVENTTYPE_FD_WRITE, u: wasi::SubscriptionUU { - fd_write: stderr_readwrite - } + fd_write: stderr_readwrite, + }, }, }, ]; @@ -182,8 +178,8 @@ unsafe fn test_fd_readwrite(fd: wasi::Fd, error_code: wasi::Errno) { u: wasi::SubscriptionU { tag: wasi::EVENTTYPE_FD_READ, u: wasi::SubscriptionUU { - fd_read: fd_readwrite - } + fd_read: fd_readwrite, + }, }, }, wasi::Subscription { @@ -191,8 +187,8 @@ unsafe fn test_fd_readwrite(fd: wasi::Fd, error_code: wasi::Errno) { u: wasi::SubscriptionU { tag: wasi::EVENTTYPE_FD_WRITE, u: wasi::SubscriptionUU { - fd_write: fd_readwrite - } + fd_write: fd_readwrite, + }, }, }, ];