Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions tests/rust/wasm32-wasip3/src/bin/filesystem-stat.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"dirs": ["fs-tests.dir"]
}
214 changes: 214 additions & 0 deletions tests/rust/wasm32-wasip3/src/bin/filesystem-stat.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,214 @@
use std::process;
extern crate wit_bindgen;

wit_bindgen::generate!({
inline: r"
package test:test;

world test {
include wasi:filesystem/[email protected];
include wasi:cli/[email protected];
}
",
additional_derives: [PartialEq, Eq, Hash, Clone],
// Work around https://github.com/bytecodealliance/wasm-tools/issues/2285.
features:["clocks-timezone"],
generate_all
});

use wasi::clocks::wall_clock::Datetime;
use wasi::filesystem::types::Descriptor;
use wasi::filesystem::types::NewTimestamp;
use wasi::filesystem::types::{DescriptorFlags, ErrorCode, OpenFlags, PathFlags};
use wasi::filesystem::types::{DescriptorStat, DescriptorType};

fn check_timestamp(t: Datetime) {
assert!(t.nanoseconds < 1_000_000_000);
}

fn check_stat(stat: &DescriptorStat, type_: DescriptorType) {
assert_eq!(stat.type_, type_);
// assert_eq!(stat.link_count, 0) ?
// assert_eq!(stat.size, 0) ?
if let Some(t) = stat.data_access_timestamp {
check_timestamp(t)
};
if let Some(t) = stat.data_modification_timestamp {
check_timestamp(t)
};
if let Some(t) = stat.status_change_timestamp {
check_timestamp(t)
}
}

async fn test_stat(dir: &Descriptor) {
let afd = dir
.open_at(
PathFlags::empty(),
"a.txt".to_string(),
OpenFlags::empty(),
DescriptorFlags::READ,
)
.await
.unwrap();
let bfd = dir
.open_at(
PathFlags::empty(),
"b.txt".to_string(),
OpenFlags::empty(),
DescriptorFlags::READ,
)
.await
.unwrap();
let stat_with_flags = |flags: PathFlags, path: &str| dir.stat_at(flags, path.to_string());
let stat = |path: &str| stat_with_flags(PathFlags::empty(), path);
let stat_follow = |path: &str| stat_with_flags(PathFlags::SYMLINK_FOLLOW, path);

// stat: async func() -> result<descriptor-stat, error-code>;
check_stat(&dir.stat().await.unwrap(), DescriptorType::Directory);
check_stat(&afd.stat().await.unwrap(), DescriptorType::RegularFile);
check_stat(&bfd.stat().await.unwrap(), DescriptorType::RegularFile);

// stat-at: async func(path-flags: path-flags, path: string) -> result<descriptor-stat, error-code>;
assert_eq!(
afd.stat_at(PathFlags::empty(), "z.txt".to_string()).await,
Err(ErrorCode::NotDirectory)
);
assert_eq!(
afd.stat_at(PathFlags::empty(), ".".to_string()).await,
Err(ErrorCode::NotDirectory)
);

assert_eq!(stat("").await, Err(ErrorCode::NoEntry));
assert_eq!(stat("..").await, Err(ErrorCode::NotPermitted));
assert_eq!(stat_follow("parent").await, Err(ErrorCode::NotPermitted));
assert_eq!(
stat_follow("parent/fs-tests.dir").await,
Err(ErrorCode::NotPermitted)
);
assert_eq!(stat(".").await, dir.stat().await);
// FIXME: https://github.com/bytecodealliance/wasmtime/issues/11606
// assert_eq!(stat_at("/").await,
// Err(ErrorCode::NotPermitted));
assert_eq!(stat("/etc/passwd").await, Err(ErrorCode::NotPermitted));
assert_eq!(stat("z.txt").await, Err(ErrorCode::NoEntry));

// set-times-at: async func(path-flags: path-flags, path: string, data-access-timestamp: new-timestamp, data-modification-timestamp: new-timestamp) -> result<_, error-code>;
// set-times: async func(data-access-timestamp: new-timestamp, data-modification-timestamp: new-timestamp) -> result<_, error-code>;
let no_flags = PathFlags::empty();
let follow_flag = PathFlags::SYMLINK_FOLLOW;
let set_times_at = |flags, path: &str, atime, mtime| -> _ {
dir.set_times_at(flags, path.to_string(),
NewTimestamp::Timestamp(atime),
NewTimestamp::Timestamp(mtime))
};
{
let atime = Datetime {
seconds: 42,
nanoseconds: 0,
};
let mtime = Datetime {
seconds: 69,
nanoseconds: 0,
};
assert_eq!(set_times_at(no_flags, "z.txt", atime, mtime).await,
Err(ErrorCode::NoEntry));
assert_eq!(set_times_at(follow_flag, "", atime, mtime).await,
Err(ErrorCode::NoEntry));
assert_eq!(set_times_at(no_flags, "/", atime, mtime).await,
Err(ErrorCode::NotPermitted));
assert_eq!(set_times_at(no_flags, "..", atime, mtime).await,
Err(ErrorCode::NotPermitted));
assert_eq!(set_times_at(follow_flag, "parent", atime, mtime).await,
Err(ErrorCode::NotPermitted));
assert_eq!(set_times_at(no_flags, "../foo", atime, mtime).await,
Err(ErrorCode::NotPermitted));
assert_eq!(set_times_at(no_flags, "parent/foo", atime, mtime).await,
Err(ErrorCode::NotPermitted));
}

if let Some(atime) = afd.stat().await.unwrap().data_access_timestamp {
let mtime = afd
.stat()
.await
.unwrap()
.data_modification_timestamp
.unwrap();
let new_atime = Datetime {
seconds: 42,
nanoseconds: 0,
};
let new_mtime = Datetime {
seconds: 69,
nanoseconds: 0,
};
assert_eq!(set_times_at(no_flags, "a.txt", new_atime, new_mtime).await,
Ok(()));
assert_eq!(
afd.stat().await.unwrap().data_access_timestamp,
Some(new_atime)
);
assert_eq!(
afd.stat().await.unwrap().data_modification_timestamp,
Some(new_mtime)
);
assert_eq!(set_times_at(no_flags, "a.txt", atime, mtime).await,
Ok(()));
assert_eq!(afd.stat().await.unwrap().data_access_timestamp, Some(atime));
assert_eq!(
afd.stat().await.unwrap().data_modification_timestamp,
Some(mtime)
);

assert_eq!(
afd.set_times(
NewTimestamp::Timestamp(new_atime),
NewTimestamp::Timestamp(new_mtime)
)
.await,
Ok(())
);
assert_eq!(
afd.stat().await.unwrap().data_access_timestamp,
Some(new_atime)
);
assert_eq!(
afd.stat().await.unwrap().data_modification_timestamp,
Some(new_mtime)
);
assert_eq!(
afd.set_times(
NewTimestamp::Timestamp(atime),
NewTimestamp::Timestamp(mtime)
)
.await,
Ok(())
);
assert_eq!(afd.stat().await.unwrap().data_access_timestamp, Some(atime));
assert_eq!(
afd.stat().await.unwrap().data_modification_timestamp,
Some(mtime)
);
}
}

struct Component;
export!(Component);
impl exports::wasi::cli::run::Guest for Component {
async fn run() -> Result<(), ()> {
match &wasi::filesystem::preopens::get_directories()[..] {
[(dir, dirname)] if dirname == "fs-tests.dir" => {
test_stat(dir).await;
}
[..] => {
eprintln!("usage: run with one open dir named 'fs-tests.dir'");
process::exit(1)
}
};
Ok(())
}
}

fn main() {
unreachable!("main is a stub");
}
Loading