|
| 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_mkdir_rmdir(dir: &Descriptor) { |
| 23 | + let mkdir = |path: &str| dir.create_directory_at(path.to_string()); |
| 24 | + let rmdir = |path: &str| dir.remove_directory_at(path.to_string()); |
| 25 | + |
| 26 | + // create-directory-at: async func(path: string) -> result<_, error-code>; |
| 27 | + assert_eq!(dir.create_directory_at("".to_string()).await, |
| 28 | + Err(ErrorCode::NoEntry)); |
| 29 | + assert_eq!(mkdir(".").await, Err(ErrorCode::Exist)); |
| 30 | + assert_eq!(mkdir("..").await, Err(ErrorCode::NotPermitted)); |
| 31 | + assert_eq!(mkdir("parent/foo").await, Err(ErrorCode::NotPermitted)); |
| 32 | + assert_eq!(mkdir("/").await, Err(ErrorCode::NotPermitted)); |
| 33 | + assert_eq!( |
| 34 | + mkdir("../fs-tests.dir/q.cleanup").await, |
| 35 | + Err(ErrorCode::NotPermitted) |
| 36 | + ); |
| 37 | + assert_eq!( |
| 38 | + mkdir("parent/fs-tests.dir/q.cleanup").await, |
| 39 | + Err(ErrorCode::NotPermitted) |
| 40 | + ); |
| 41 | + assert_eq!(mkdir("a.txt").await, Err(ErrorCode::Exist)); |
| 42 | + mkdir("q.cleanup").await.unwrap(); |
| 43 | + assert_eq!( |
| 44 | + rmdir("../fs-tests.dir/q.cleanup").await, |
| 45 | + Err(ErrorCode::NotPermitted) |
| 46 | + ); |
| 47 | + assert_eq!( |
| 48 | + rmdir("parent/fs-tests.dir/q.cleanup").await, |
| 49 | + Err(ErrorCode::NotPermitted) |
| 50 | + ); |
| 51 | + assert_eq!(rmdir("q.cleanup/").await, Err(ErrorCode::Invalid)); |
| 52 | + assert_eq!( |
| 53 | + rmdir("q.cleanup/../../fs-tests.dir/q.cleanup").await, |
| 54 | + Err(ErrorCode::NotPermitted) |
| 55 | + ); |
| 56 | + rmdir("q.cleanup").await.unwrap(); |
| 57 | + mkdir("q.cleanup/").await.unwrap(); |
| 58 | + rmdir("q.cleanup").await.unwrap(); |
| 59 | + mkdir("q.cleanup").await.unwrap(); |
| 60 | + // FIXME: https://github.com/bytecodealliance/wasmtime/issues/11524 |
| 61 | + // rmdir("q.cleanup/") |
| 62 | + // .await.unwrap(); |
| 63 | + // mkdir("q.cleanup/////") |
| 64 | + // .await.unwrap(); |
| 65 | + // rmdir("q.cleanup////////////") |
| 66 | + // .await.unwrap(); |
| 67 | + // Using this instead to clean up: |
| 68 | + rmdir("q.cleanup").await.unwrap(); |
| 69 | + |
| 70 | + // remove-directory-at: async func(path: string) -> result<_, error-code>; |
| 71 | + assert_eq!(rmdir("").await, Err(ErrorCode::NoEntry)); |
| 72 | + assert_eq!(rmdir(".").await, Err(ErrorCode::Invalid)); |
| 73 | + assert_eq!(rmdir("..").await, Err(ErrorCode::NotPermitted)); |
| 74 | + assert_eq!(rmdir("/").await, Err(ErrorCode::NotPermitted)); |
| 75 | + assert_eq!(rmdir("a.txt").await, Err(ErrorCode::NotDirectory)); |
| 76 | + assert_eq!(rmdir("z.txt").await, Err(ErrorCode::NoEntry)); |
| 77 | + assert_eq!(rmdir("parent").await, Err(ErrorCode::NotDirectory)); |
| 78 | + assert_eq!( |
| 79 | + rmdir("parent/fs-tests.dir").await, |
| 80 | + Err(ErrorCode::NotPermitted) |
| 81 | + ); |
| 82 | +} |
| 83 | + |
| 84 | +struct Component; |
| 85 | +export!(Component); |
| 86 | +impl exports::wasi::cli::run::Guest for Component { |
| 87 | + async fn run() -> Result<(), ()> { |
| 88 | + match &wasi::filesystem::preopens::get_directories()[..] { |
| 89 | + [(dir, dirname)] if dirname == "fs-tests.dir" => { |
| 90 | + test_mkdir_rmdir(dir).await; |
| 91 | + } |
| 92 | + [..] => { |
| 93 | + eprintln!("usage: run with one open dir named 'fs-tests.dir'"); |
| 94 | + process::exit(1) |
| 95 | + } |
| 96 | + }; |
| 97 | + Ok(()) |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +fn main() { |
| 102 | + unreachable!("main is a stub"); |
| 103 | +} |
0 commit comments