|
| 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-2025-09-16; |
| 10 | + include wasi:cli/command@0.3.0-rc-2025-09-16; |
| 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, DescriptorFlags, ErrorCode, OpenFlags, PathFlags}; |
| 20 | + |
| 21 | +async fn test_mkdir_rmdir(dir: &Descriptor) { |
| 22 | + let mkdir = |path: &str| dir.create_directory_at(path.to_string()); |
| 23 | + let rmdir = |path: &str| dir.remove_directory_at(path.to_string()); |
| 24 | + |
| 25 | + // create-directory-at: async func(path: string) -> result<_, error-code>; |
| 26 | + assert_eq!( |
| 27 | + dir.create_directory_at("".to_string()).await, |
| 28 | + Err(ErrorCode::NoEntry) |
| 29 | + ); |
| 30 | + assert_eq!(mkdir(".").await, Err(ErrorCode::Exist)); |
| 31 | + assert_eq!(mkdir("..").await, Err(ErrorCode::NotPermitted)); |
| 32 | + assert_eq!(mkdir("parent/foo").await, Err(ErrorCode::NotPermitted)); |
| 33 | + assert_eq!(mkdir("/").await, Err(ErrorCode::NotPermitted)); |
| 34 | + assert_eq!( |
| 35 | + mkdir("../fs-tests.dir/q.cleanup").await, |
| 36 | + Err(ErrorCode::NotPermitted) |
| 37 | + ); |
| 38 | + assert_eq!( |
| 39 | + mkdir("parent/fs-tests.dir/q.cleanup").await, |
| 40 | + Err(ErrorCode::NotPermitted) |
| 41 | + ); |
| 42 | + assert_eq!(mkdir("a.txt").await, Err(ErrorCode::Exist)); |
| 43 | + mkdir("q.cleanup").await.unwrap(); |
| 44 | + assert_eq!( |
| 45 | + rmdir("../fs-tests.dir/q.cleanup").await, |
| 46 | + Err(ErrorCode::NotPermitted) |
| 47 | + ); |
| 48 | + assert_eq!( |
| 49 | + rmdir("parent/fs-tests.dir/q.cleanup").await, |
| 50 | + Err(ErrorCode::NotPermitted) |
| 51 | + ); |
| 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 | + mkdir("child.cleanup").await.unwrap(); |
| 84 | + mkdir("sibling.cleanup").await.unwrap(); |
| 85 | + let child = dir |
| 86 | + .open_at( |
| 87 | + PathFlags::empty(), |
| 88 | + "child.cleanup".to_string(), |
| 89 | + OpenFlags::DIRECTORY, |
| 90 | + DescriptorFlags::MUTATE_DIRECTORY, |
| 91 | + ) |
| 92 | + .await |
| 93 | + .unwrap(); |
| 94 | + |
| 95 | + let child_mkdir = |path: &str| child.create_directory_at(path.to_string()); |
| 96 | + let child_rmdir = |path: &str| child.remove_directory_at(path.to_string()); |
| 97 | + |
| 98 | + child_mkdir("z.cleanup").await.unwrap(); |
| 99 | + child_rmdir("z.cleanup").await.unwrap(); |
| 100 | + assert_eq!( |
| 101 | + child_mkdir("../z.cleanup").await.unwrap_err(), |
| 102 | + ErrorCode::NotPermitted |
| 103 | + ); |
| 104 | + assert_eq!( |
| 105 | + child_rmdir("../z.cleanup").await.unwrap_err(), |
| 106 | + ErrorCode::NotPermitted |
| 107 | + ); |
| 108 | + assert_eq!( |
| 109 | + child_mkdir("../sibling.cleanup/z.cleanup") |
| 110 | + .await |
| 111 | + .unwrap_err(), |
| 112 | + ErrorCode::NotPermitted |
| 113 | + ); |
| 114 | + assert_eq!( |
| 115 | + child_rmdir("../sibling.cleanup/z.cleanup") |
| 116 | + .await |
| 117 | + .unwrap_err(), |
| 118 | + ErrorCode::NotPermitted |
| 119 | + ); |
| 120 | + assert_eq!( |
| 121 | + child_rmdir("../sibling.cleanup").await.unwrap_err(), |
| 122 | + ErrorCode::NotPermitted |
| 123 | + ); |
| 124 | + rmdir("child.cleanup").await.unwrap(); |
| 125 | +} |
| 126 | + |
| 127 | +struct Component; |
| 128 | +export!(Component); |
| 129 | +impl exports::wasi::cli::run::Guest for Component { |
| 130 | + async fn run() -> Result<(), ()> { |
| 131 | + match &wasi::filesystem::preopens::get_directories()[..] { |
| 132 | + [(dir, dirname)] if dirname == "fs-tests.dir" => { |
| 133 | + test_mkdir_rmdir(dir).await; |
| 134 | + } |
| 135 | + [..] => { |
| 136 | + eprintln!("usage: run with one open dir named 'fs-tests.dir'"); |
| 137 | + process::exit(1) |
| 138 | + } |
| 139 | + }; |
| 140 | + Ok(()) |
| 141 | + } |
| 142 | +} |
| 143 | + |
| 144 | +fn main() { |
| 145 | + unreachable!("main is a stub"); |
| 146 | +} |
0 commit comments