Skip to content

Commit 344e0ee

Browse files
committed
wasi:[email protected]: Add tests for unlink errors
1 parent c15c756 commit 344e0ee

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-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: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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::ErrorCode;
21+
22+
async fn test_unlink_errors(dir: &Descriptor) {
23+
let rm = |path: &str| dir.unlink_file_at(path.to_string());
24+
assert_eq!(rm("").await, Err(ErrorCode::NoEntry));
25+
assert_eq!(rm(".").await, Err(ErrorCode::IsDirectory));
26+
assert_eq!(rm("..").await, Err(ErrorCode::NotPermitted));
27+
assert_eq!(rm("../fs-tests.dir").await, Err(ErrorCode::NotPermitted));
28+
assert_eq!(rm("/").await, Err(ErrorCode::NotPermitted));
29+
assert_eq!(rm("/etc/passwd").await, Err(ErrorCode::NotPermitted));
30+
assert_eq!(rm("/etc/passwd").await, Err(ErrorCode::NotPermitted));
31+
assert_eq!(rm("z.txt").await, Err(ErrorCode::NoEntry));
32+
assert_eq!(rm("parent/z.txt").await, Err(ErrorCode::NotPermitted));
33+
}
34+
35+
struct Component;
36+
export!(Component);
37+
impl exports::wasi::cli::run::Guest for Component {
38+
async fn run() -> Result<(), ()> {
39+
match &wasi::filesystem::preopens::get_directories()[..] {
40+
[(dir, dirname)] if dirname == "fs-tests.dir" => {
41+
test_unlink_errors(dir).await;
42+
}
43+
[..] => {
44+
eprintln!("usage: run with one open dir named 'fs-tests.dir'");
45+
process::exit(1)
46+
}
47+
};
48+
Ok(())
49+
}
50+
}
51+
52+
fn main() {
53+
unreachable!("main is a stub");
54+
}

0 commit comments

Comments
 (0)