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