Skip to content

Commit c93f713

Browse files
committed
wasi:filesystem@0.3.0-rc-2025-09-16: Add tests for unlink errors
See WebAssembly/WASI#852.
1 parent ebffc08 commit c93f713

2 files changed

Lines changed: 64 additions & 0 deletions

File tree

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

0 commit comments

Comments
 (0)