|
| 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::{DescriptorFlags, ErrorCode, OpenFlags, PathFlags}; |
| 21 | + |
| 22 | +async fn check_metadata_hash(a: &Descriptor, b: &Descriptor) { |
| 23 | + let a_hash = a.metadata_hash().await; |
| 24 | + let b_hash = b.metadata_hash().await; |
| 25 | + if a_hash.is_ok() && a_hash == b_hash { |
| 26 | + assert_eq!(a.stat().await.unwrap(), b.stat().await.unwrap()); |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +async fn check_metadata_hash_at( |
| 31 | + a: &Descriptor, |
| 32 | + a_flags: PathFlags, |
| 33 | + a_name: &str, |
| 34 | + b: &Descriptor, |
| 35 | + b_flags: PathFlags, |
| 36 | + b_name: &str, |
| 37 | +) { |
| 38 | + let a_hash = a.metadata_hash_at(a_flags, a_name.to_string()).await; |
| 39 | + let b_hash = b.metadata_hash_at(b_flags, b_name.to_string()).await; |
| 40 | + if a_hash.is_ok() && a_hash == b_hash { |
| 41 | + assert_eq!( |
| 42 | + a.stat_at(a_flags, a_name.to_string()).await.unwrap(), |
| 43 | + b.stat_at(b_flags, b_name.to_string()).await.unwrap() |
| 44 | + ); |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +async fn test_metadata_hash(dir: &Descriptor) { |
| 49 | + let afd = dir |
| 50 | + .open_at( |
| 51 | + PathFlags::empty(), |
| 52 | + "a.txt".to_string(), |
| 53 | + OpenFlags::empty(), |
| 54 | + DescriptorFlags::READ, |
| 55 | + ) |
| 56 | + .await |
| 57 | + .unwrap(); |
| 58 | + let bfd = dir |
| 59 | + .open_at( |
| 60 | + PathFlags::empty(), |
| 61 | + "b.txt".to_string(), |
| 62 | + OpenFlags::empty(), |
| 63 | + DescriptorFlags::READ, |
| 64 | + ) |
| 65 | + .await |
| 66 | + .unwrap(); |
| 67 | + |
| 68 | + // metadata-hash: async func() -> result<metadata-hash-value, error-code>; |
| 69 | + check_metadata_hash(dir, dir).await; |
| 70 | + check_metadata_hash(dir, &afd).await; |
| 71 | + check_metadata_hash(&afd, &afd).await; |
| 72 | + check_metadata_hash(&afd, &bfd).await; |
| 73 | + |
| 74 | + // metadata-hash-at: async func(path-flags: path-flags, path: string) -> result<metadata-hash-value, error-code>; |
| 75 | + // FIXME: https://github.com/bytecodealliance/wasmtime/issues/11606 |
| 76 | + // assert_eq!(dir.metadata_hash_at(PathFlags::empty(), "/".to_string()).await, |
| 77 | + // Err(ErrorCode::NotPermitted)); |
| 78 | + assert_eq!(dir.metadata_hash_at(PathFlags::empty(), "".to_string()).await, |
| 79 | + Err(ErrorCode::NoEntry)); |
| 80 | + assert_eq!( |
| 81 | + dir.metadata_hash_at(PathFlags::empty(), "/etc/passwd".to_string()) |
| 82 | + .await, |
| 83 | + Err(ErrorCode::NotPermitted) |
| 84 | + ); |
| 85 | + assert_eq!( |
| 86 | + dir.metadata_hash_at(PathFlags::empty(), "/does-not-exist".to_string()) |
| 87 | + .await, |
| 88 | + Err(ErrorCode::NotPermitted) |
| 89 | + ); |
| 90 | + check_metadata_hash_at(dir, PathFlags::empty(), ".", dir, PathFlags::empty(), ".").await; |
| 91 | + check_metadata_hash_at( |
| 92 | + dir, |
| 93 | + PathFlags::empty(), |
| 94 | + "a.txt", |
| 95 | + dir, |
| 96 | + PathFlags::empty(), |
| 97 | + ".", |
| 98 | + ) |
| 99 | + .await; |
| 100 | + check_metadata_hash_at( |
| 101 | + dir, |
| 102 | + PathFlags::empty(), |
| 103 | + "a.txt", |
| 104 | + dir, |
| 105 | + PathFlags::empty(), |
| 106 | + "a.txt", |
| 107 | + ) |
| 108 | + .await; |
| 109 | + check_metadata_hash_at( |
| 110 | + dir, |
| 111 | + PathFlags::empty(), |
| 112 | + "a.txt", |
| 113 | + dir, |
| 114 | + PathFlags::empty(), |
| 115 | + "b.txt", |
| 116 | + ) |
| 117 | + .await; |
| 118 | +} |
| 119 | + |
| 120 | +struct Component; |
| 121 | +export!(Component); |
| 122 | +impl exports::wasi::cli::run::Guest for Component { |
| 123 | + async fn run() -> Result<(), ()> { |
| 124 | + match &wasi::filesystem::preopens::get_directories()[..] { |
| 125 | + [(dir, dirname)] if dirname == "fs-tests.dir" => { |
| 126 | + test_metadata_hash(dir).await; |
| 127 | + } |
| 128 | + [..] => { |
| 129 | + eprintln!("usage: run with one open dir named 'fs-tests.dir'"); |
| 130 | + process::exit(1) |
| 131 | + } |
| 132 | + }; |
| 133 | + Ok(()) |
| 134 | + } |
| 135 | +} |
| 136 | + |
| 137 | +fn main() { |
| 138 | + unreachable!("main is a stub"); |
| 139 | +} |
0 commit comments