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-rename.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"dirs": ["fs-tests.dir"]
}
80 changes: 80 additions & 0 deletions tests/rust/wasm32-wasip3/src/bin/filesystem-rename.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
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::filesystem::types::Descriptor;
use wasi::filesystem::types::ErrorCode;

async fn test_rename(dir: &Descriptor) {
// rename-at: async func(old-path: string, new-descriptor: borrow<descriptor>, new-path: string) -> result<_, error-code>;
let mv = |from: &str, to: &str| -> _ { dir.rename_at(from.to_string(), dir, to.to_string()) };
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be nice to create a child or sibling directory, open it, and get some coverage where the this and new-descriptor are different.

let ln_s = |from: &str, to: &str| -> _ { dir.symlink_at(from.to_string(), to.to_string()) };

mv("a.txt", "a.txt").await.unwrap();
ln_s("a.txt", "c.cleanup").await.unwrap();
mv("a.txt", "c.cleanup").await.unwrap();
assert_eq!(mv("a.txt", "a.txt").await, Err(ErrorCode::NoEntry));
mv("c.cleanup", "a.txt").await.unwrap();
assert_eq!(mv("c.cleanup", "a.txt").await, Err(ErrorCode::NoEntry));
assert_eq!(
mv("does-not-exist.txt", "q.txt").await,
Err(ErrorCode::NoEntry)
);
match mv(".", "q.txt").await {
Err(ErrorCode::Busy) => {}
Err(ErrorCode::Invalid) => {}
Ok(()) => {
panic!("mv . q.txt unexpectedly succeeded");
}
Err(err) => {
panic!("mv . q.txt: unexpected error {}", err);
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fails on Windows with access.

}
};
mv("a.txt", "c.cleanup").await.unwrap();
assert_eq!(mv("a.txt", "q.txt").await, Err(ErrorCode::NoEntry));
mv("c.cleanup", "a.txt").await.unwrap();
assert_eq!(mv("a.txt", "../q.txt").await, Err(ErrorCode::NotPermitted));
assert_eq!(
mv("a.txt", "parent/q.txt").await,
Err(ErrorCode::NotPermitted)
);
assert_eq!(
mv("a.txt", "/tmp/q.txt").await,
Err(ErrorCode::NotPermitted)
);
}

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