|
| 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