-
Notifications
You must be signed in to change notification settings - Fork 35
wasi:[email protected]: Add tests for set-size #136
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+133
−0
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| { | ||
| "dirs": ["fs-tests.dir"] | ||
| } |
130 changes: 130 additions & 0 deletions
130
tests/rust/wasm32-wasip3/src/bin/filesystem-set-size.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,130 @@ | ||
| use std::process; | ||
| extern crate wit_bindgen; | ||
|
|
||
| wit_bindgen::generate!({ | ||
| inline: r" | ||
| package test:test; | ||
|
|
||
| world test { | ||
| include wasi:filesystem/[email protected]; | ||
| include wasi:cli/[email protected]; | ||
| } | ||
| ", | ||
| additional_derives: [PartialEq, Eq, Hash, Clone], | ||
| // Work around https://github.com/bytecodealliance/wasm-tools/issues/2285. | ||
| features:["clocks-timezone"], | ||
| generate_all | ||
| }); | ||
|
|
||
| use wasi::filesystem::types::Descriptor; | ||
| use wasi::filesystem::types::{DescriptorFlags, ErrorCode, OpenFlags, PathFlags}; | ||
|
|
||
| async fn test_set_size(dir: &Descriptor) { | ||
| // set-size: async func(size: filesize) -> result<_, error-code>; | ||
| let open = |path: &str, oflags: OpenFlags, fdflags: DescriptorFlags| -> _ { | ||
| dir.open_at(PathFlags::empty(), path.to_string(), oflags, fdflags) | ||
| }; | ||
| let open_r = |path: &str| -> _ { open(path, OpenFlags::empty(), DescriptorFlags::READ) }; | ||
| let open_w = |path: &str| -> _ { | ||
| open( | ||
| path, | ||
| OpenFlags::empty(), | ||
| DescriptorFlags::READ | DescriptorFlags::WRITE, | ||
| ) | ||
| }; | ||
| let creat = |path: &str| -> _ { | ||
| open( | ||
| path, | ||
| OpenFlags::CREATE | OpenFlags::EXCLUSIVE, | ||
| DescriptorFlags::READ | DescriptorFlags::WRITE, | ||
| ) | ||
| }; | ||
| let trunc = |path: &str| -> _ { | ||
| open( | ||
| path, | ||
| OpenFlags::TRUNCATE, | ||
| DescriptorFlags::READ | DescriptorFlags::WRITE, | ||
| ) | ||
| }; | ||
| let rm = |path: &str| dir.unlink_file_at(path.to_string()); | ||
|
|
||
| let c = creat("c.cleanup").await.unwrap(); | ||
| assert_eq!(c.stat().await.unwrap().size, 0); | ||
| c.set_size(42).await.unwrap(); | ||
| // Setting size is visible immediately. | ||
| assert_eq!(c.stat().await.unwrap().size, 42); | ||
|
|
||
| let c = open_w("c.cleanup").await.unwrap(); | ||
| let r = open_r("c.cleanup").await.unwrap(); | ||
| assert_eq!(c.stat().await.unwrap().size, 42); | ||
| assert_eq!(r.stat().await.unwrap().size, 42); | ||
| c.set_size(69).await.unwrap(); | ||
| assert_eq!(c.stat().await.unwrap().size, 69); | ||
| assert_eq!(r.stat().await.unwrap().size, 69); | ||
|
|
||
| let c = trunc("c.cleanup").await.unwrap(); | ||
| assert_eq!(c.stat().await.unwrap().size, 0); | ||
| assert_eq!(r.stat().await.unwrap().size, 0); | ||
|
|
||
| // https://github.com/WebAssembly/WASI/issues/712 | ||
| match r.set_size(100).await { | ||
| Ok(()) => { | ||
| panic!("set-size succeeded on read-only descriptor"); | ||
| } | ||
| Err(ErrorCode::Invalid | ErrorCode::BadDescriptor | ErrorCode::Access) => {} | ||
| Err(err) => { | ||
| panic!("unexpected err: {}", err) | ||
| } | ||
| }; | ||
|
|
||
| // https://github.com/WebAssembly/WASI/issues/712 | ||
| match c.set_size(u64::MAX).await { | ||
| Ok(()) => { | ||
| panic!("set-size(-1) succeeded"); | ||
| } | ||
| Err(ErrorCode::Invalid | ErrorCode::FileTooLarge) => {} | ||
| Err(err) => { | ||
| panic!("unexpected err: {}", err) | ||
| } | ||
| }; | ||
|
|
||
| match rm("c.cleanup").await { | ||
| Ok(()) => { | ||
| // We still have `c` and `r` open, which refer to the file, | ||
| // but we can still stat our descriptors, call `set-size` on | ||
| // it, and so on. | ||
| assert_eq!(c.stat().await.unwrap().size, 0); | ||
| c.set_size(42).await.unwrap(); | ||
| assert_eq!(c.stat().await.unwrap().size, 42); | ||
| assert_eq!(r.stat().await.unwrap().size, 42); | ||
| } | ||
| Err(ErrorCode::Busy) => { | ||
| // Otherwise if we're on Windows we are unable to remove the | ||
| // file while descriptors are open. | ||
| } | ||
| Err(err) => { | ||
| panic!("unexpected err: {}", err) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| struct Component; | ||
| export!(Component); | ||
| impl exports::wasi::cli::run::Guest for Component { | ||
| async fn run() -> Result<(), ()> { | ||
| match &wasi::filesystem::preopens::get_directories()[..] { | ||
| [(dir, dirname)] if dirname == "fs-tests.dir" => { | ||
| test_set_size(dir).await; | ||
| } | ||
| [..] => { | ||
| eprintln!("usage: run with one open dir named 'fs-tests.dir'"); | ||
| process::exit(1) | ||
| } | ||
| }; | ||
| Ok(()) | ||
| } | ||
| } | ||
|
|
||
| fn main() { | ||
| unreachable!("main is a stub"); | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.