Skip to content

Commit ae474b6

Browse files
committed
wasi:[email protected]: Add tests for set-size
1 parent 4cf6b4f commit ae474b6

File tree

2 files changed

+126
-0
lines changed

2 files changed

+126
-0
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"dirs": ["fs-tests.dir"]
3+
}
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
use std::process;
2+
extern crate wit_bindgen;
3+
4+
wit_bindgen::generate!({
5+
inline: r"
6+
package test:test;
7+
8+
world test {
9+
include wasi:filesystem/[email protected];
10+
include wasi:cli/[email protected];
11+
}
12+
",
13+
additional_derives: [PartialEq, Eq, Hash, Clone],
14+
// Work around https://github.com/bytecodealliance/wasm-tools/issues/2285.
15+
features:["clocks-timezone"],
16+
generate_all
17+
});
18+
19+
use wasi::filesystem::types::Descriptor;
20+
use wasi::filesystem::types::{DescriptorFlags, ErrorCode, OpenFlags, PathFlags};
21+
22+
async fn test_set_size(dir: &Descriptor) {
23+
// set-size: async func(size: filesize) -> result<_, error-code>;
24+
let open = |path: &str, oflags: OpenFlags, fdflags: DescriptorFlags| -> _ {
25+
dir.open_at(PathFlags::empty(), path.to_string(), oflags, fdflags)
26+
};
27+
let open_r = |path: &str| -> _ { open(path, OpenFlags::empty(), DescriptorFlags::READ) };
28+
let open_w = |path: &str| -> _ {
29+
open(
30+
path,
31+
OpenFlags::empty(),
32+
DescriptorFlags::READ | DescriptorFlags::WRITE,
33+
)
34+
};
35+
let creat = |path: &str| -> _ {
36+
open(
37+
path,
38+
OpenFlags::CREATE | OpenFlags::EXCLUSIVE,
39+
DescriptorFlags::READ | DescriptorFlags::WRITE,
40+
)
41+
};
42+
let trunc = |path: &str| -> _ {
43+
open(
44+
path,
45+
OpenFlags::TRUNCATE,
46+
DescriptorFlags::READ | DescriptorFlags::WRITE,
47+
)
48+
};
49+
let rm = |path: &str| dir.unlink_file_at(path.to_string());
50+
51+
let c = creat("c.cleanup").await.unwrap();
52+
assert_eq!(c.stat().await.unwrap().size, 0);
53+
c.set_size(42).await.unwrap();
54+
// Setting size is visible immediately.
55+
assert_eq!(c.stat().await.unwrap().size, 42);
56+
57+
let c = open_w("c.cleanup").await.unwrap();
58+
let r = open_r("c.cleanup").await.unwrap();
59+
assert_eq!(c.stat().await.unwrap().size, 42);
60+
assert_eq!(r.stat().await.unwrap().size, 42);
61+
c.set_size(69).await.unwrap();
62+
assert_eq!(c.stat().await.unwrap().size, 69);
63+
assert_eq!(r.stat().await.unwrap().size, 69);
64+
65+
let c = trunc("c.cleanup").await.unwrap();
66+
assert_eq!(c.stat().await.unwrap().size, 0);
67+
assert_eq!(r.stat().await.unwrap().size, 0);
68+
69+
// https://github.com/WebAssembly/wasi-filesystem/issues/190
70+
match r.set_size(100).await {
71+
Ok(()) => {
72+
panic!("set-size succeeded on read-only descriptor");
73+
}
74+
Err(ErrorCode::Invalid | ErrorCode::BadDescriptor) => {}
75+
Err(err) => {
76+
panic!("unexpected err: {}", err)
77+
}
78+
};
79+
80+
// https://github.com/WebAssembly/wasi-filesystem/issues/190
81+
match c.set_size(u64::MAX).await {
82+
Ok(()) => {
83+
panic!("set-size(-1) succeeded");
84+
}
85+
Err(ErrorCode::Invalid | ErrorCode::FileTooLarge) => {}
86+
Err(err) => {
87+
panic!("unexpected err: {}", err)
88+
}
89+
};
90+
91+
rm("c.cleanup").await.unwrap();
92+
93+
// We still have `c` and `r` open, which refer to the file; on POSIX
94+
// systems, the `c.cleanup` will have been removed from its dir,
95+
// whereas on Windows that will happen when the last open descriptor
96+
// (`c` and `r`) is closed. In any case we can still stat our
97+
// descriptors, call `set-size` on it, and so on.
98+
assert_eq!(c.stat().await.unwrap().size, 0);
99+
c.set_size(42).await.unwrap();
100+
assert_eq!(c.stat().await.unwrap().size, 42);
101+
assert_eq!(r.stat().await.unwrap().size, 42);
102+
}
103+
104+
struct Component;
105+
export!(Component);
106+
impl exports::wasi::cli::run::Guest for Component {
107+
async fn run() -> Result<(), ()> {
108+
match &wasi::filesystem::preopens::get_directories()[..] {
109+
[(dir, dirname)] if dirname == "fs-tests.dir" => {
110+
test_set_size(dir).await;
111+
}
112+
[..] => {
113+
eprintln!("usage: run with one open dir named 'fs-tests.dir'");
114+
process::exit(1)
115+
}
116+
};
117+
Ok(())
118+
}
119+
}
120+
121+
fn main() {
122+
unreachable!("main is a stub");
123+
}

0 commit comments

Comments
 (0)