Skip to content

Commit f81a49c

Browse files
committed
wasi:[email protected]: Add tests for errors during open
1 parent d0e0344 commit f81a49c

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-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: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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_open_errors(dir: &Descriptor) {
23+
let open = |flags: PathFlags, path: &str| -> _ {
24+
dir.open_at(
25+
flags,
26+
path.to_string(),
27+
OpenFlags::empty(),
28+
DescriptorFlags::READ,
29+
)
30+
};
31+
let open_r = |path: &str| open(PathFlags::empty(), path);
32+
let open_r_follow = |path: &str| open(PathFlags::SYMLINK_FOLLOW, path);
33+
// open-at: async func(path-flags: path-flags, path: string, open-flags: open-flags, %flags: descriptor-flags) -> result<descriptor, error-code>;
34+
assert_eq!(open_r("").await.expect_err("open"),
35+
ErrorCode::NoEntry);
36+
assert_eq!(
37+
open_r("..").await.expect_err("open .."),
38+
ErrorCode::NotPermitted
39+
);
40+
assert_eq!(
41+
open_r_follow("parent").await.expect_err("open parent"),
42+
ErrorCode::NotPermitted
43+
);
44+
assert_eq!(
45+
open_r("/").await.expect_err("open /"),
46+
ErrorCode::NotPermitted
47+
);
48+
}
49+
50+
struct Component;
51+
export!(Component);
52+
impl exports::wasi::cli::run::Guest for Component {
53+
async fn run() -> Result<(), ()> {
54+
match &wasi::filesystem::preopens::get_directories()[..] {
55+
[(dir, dirname)] if dirname == "fs-tests.dir" => {
56+
test_open_errors(dir).await;
57+
}
58+
[..] => {
59+
eprintln!("usage: run with one open dir named 'fs-tests.dir'");
60+
process::exit(1)
61+
}
62+
};
63+
Ok(())
64+
}
65+
}
66+
67+
fn main() {
68+
unreachable!("main is a stub");
69+
}

0 commit comments

Comments
 (0)