Skip to content

Commit bb60034

Browse files
committed
wasi:[email protected]: Add tests for hard links
1 parent c15c756 commit bb60034

File tree

2 files changed

+82
-0
lines changed

2 files changed

+82
-0
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"dirs": ["fs-tests.dir"]
3+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
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, PathFlags};
21+
22+
async fn test_hard_links(dir: &Descriptor) {
23+
let ln_with_flags = |flags: PathFlags, from: &str, to: &str| -> _ {
24+
dir.link_at(flags, from.to_string(), dir, to.to_string())
25+
};
26+
let ln = |from: &str, to: &str| -> _ { ln_with_flags(PathFlags::empty(), from, to) };
27+
let rm = |path: &str| dir.unlink_file_at(path.to_string());
28+
let mkdir = |path: &str| dir.create_directory_at(path.to_string());
29+
let rmdir = |path: &str| dir.remove_directory_at(path.to_string());
30+
31+
// link-at: async func(old-path-flags: path-flags, old-path: string, new-descriptor: borrow<descriptor>, new-path: string) -> result<_, error-code>;
32+
assert_eq!(ln(".", "foo").await, Err(ErrorCode::NotPermitted));
33+
assert_eq!(ln("", "foo").await, Err(ErrorCode::NoEntry));
34+
assert_eq!(ln("", "").await, Err(ErrorCode::NoEntry));
35+
assert_eq!(ln("a.txt", "").await, Err(ErrorCode::NoEntry));
36+
assert_eq!(ln("a.txt", "a.txt").await, Err(ErrorCode::Exist));
37+
assert_eq!(ln("/", "a.txt").await, Err(ErrorCode::NotPermitted));
38+
assert_eq!(ln("a.txt", "/").await, Err(ErrorCode::NotPermitted));
39+
assert_eq!(ln("a.txt", "/a.txt").await, Err(ErrorCode::NotPermitted));
40+
assert_eq!(ln("..", "a.txt").await, Err(ErrorCode::NotPermitted));
41+
assert_eq!(ln("a.txt", "..").await, Err(ErrorCode::NotPermitted));
42+
// FIXME: https://github.com/WebAssembly/wasi-filesystem/issues/191
43+
// assert_eq!(ln_follow("parent/foo", "a.txt").await,
44+
// Err(ErrorCode::NotPermitted));
45+
assert_eq!(ln("parent/foo", "a.txt").await,
46+
Err(ErrorCode::NotPermitted));
47+
assert_eq!(ln("a.txt", "parent/foo").await,
48+
Err(ErrorCode::NotPermitted));
49+
ln("a.txt", "c.cleanup").await.unwrap();
50+
rm("c.cleanup").await.unwrap();
51+
mkdir("d.cleanup").await.unwrap();
52+
ln("a.txt", "d.cleanup/q.txt").await.unwrap();
53+
rm("d.cleanup/q.txt").await.unwrap();
54+
// https://github.com/WebAssembly/wasi-filesystem/issues/184
55+
assert_eq!(ln("d.cleanup", "e.cleanup").await,
56+
Err(ErrorCode::NotPermitted));
57+
rmdir("d.cleanup").await.unwrap();
58+
}
59+
60+
struct Component;
61+
export!(Component);
62+
impl exports::wasi::cli::run::Guest for Component {
63+
async fn run() -> Result<(), ()> {
64+
match &wasi::filesystem::preopens::get_directories()[..] {
65+
[(dir, dirname)] if dirname == "fs-tests.dir" => {
66+
test_hard_links(dir).await;
67+
}
68+
[..] => {
69+
eprintln!("usage: run with one open dir named 'fs-tests.dir'");
70+
process::exit(1)
71+
}
72+
};
73+
Ok(())
74+
}
75+
}
76+
77+
fn main() {
78+
unreachable!("main is a stub");
79+
}

0 commit comments

Comments
 (0)