Skip to content

Commit 0ee5dfa

Browse files
committed
add more tests
1 parent abf72a4 commit 0ee5dfa

File tree

10 files changed

+659
-107
lines changed

10 files changed

+659
-107
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ repository = "https://github.com/wasm-forge/ic-wasi-polyfill"
1010
[dependencies]
1111
ic-cdk = "0.17.1"
1212
ic-stable-structures = "0.6.7"
13-
#stable-fs = "0.7"
13+
#stable-fs = "0.8"
1414
stable-fs = { path = "../stable-fs"}
1515

1616
rand = "0.9.0"

src/lib.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -407,8 +407,8 @@ pub unsafe extern "C" fn __ic_custom_path_open(
407407
path_len: i32,
408408

409409
oflags: i32,
410-
fs_rights_base: i64,
411-
fs_rights_inheriting: i64,
410+
fs_rights_base: wasi::Rights,
411+
fs_rights_inheriting: wasi::Rights,
412412

413413
fdflags: i32,
414414
res: *mut Fd,
@@ -433,8 +433,8 @@ pub unsafe extern "C" fn __ic_custom_path_open(
433433

434434
let fd_stat = FdStat {
435435
flags: FdFlags::from_bits_truncate(fdflags as u16),
436-
rights_base: fs_rights_base as u64,
437-
rights_inheriting: fs_rights_inheriting as u64,
436+
rights_base: fs_rights_base,
437+
rights_inheriting: fs_rights_inheriting,
438438
};
439439

440440
let open_flags = OpenFlags::from_bits_truncate(oflags as u16);

src/test/common.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ pub fn create_test_file_with_content(parent_fd: Fd, file_name: &str, content: Ve
2020
0,
2121
new_file_name.as_ptr(),
2222
new_file_name.len() as i32,
23-
1 + 4 + 8,
24-
0,
23+
(wasi::OFLAGS_CREAT | wasi::OFLAGS_TRUNC | wasi::OFLAGS_EXCL) as i32,
24+
wasi::RIGHTS_FD_WRITE | wasi::RIGHTS_FD_READ,
2525
0,
2626
0,
2727
(&mut file_fd) as *mut u32,

src/test/fd_filestat_set.rs

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
use crate::{init, test::common::libc, wasi};
2+
3+
unsafe fn test_fd_filestat_set_size_rw(dir_fd: wasi::Fd) {
4+
// Create a file in the scratch directory, opened read/write
5+
let file_fd = wasi::path_open(
6+
dir_fd,
7+
0,
8+
"file",
9+
wasi::OFLAGS_CREAT,
10+
wasi::RIGHTS_FD_READ | wasi::RIGHTS_FD_WRITE,
11+
0,
12+
0,
13+
)
14+
.expect("failed to create file");
15+
16+
assert!(
17+
file_fd > libc::STDERR_FILENO as wasi::Fd,
18+
"file descriptor range check",
19+
);
20+
21+
// Check file size
22+
let stat = wasi::fd_filestat_get(file_fd).expect("failed filestat");
23+
assert_eq!(stat.size, 0, "file size should be 0");
24+
25+
// Check fd_filestat_set_size
26+
wasi::fd_filestat_set_size(file_fd, 100).expect("fd_filestat_set_size");
27+
28+
let stat = wasi::fd_filestat_get(file_fd).expect("failed filestat 2");
29+
assert_eq!(stat.size, 100, "file size should be 100");
30+
31+
wasi::fd_close(file_fd).expect("failed to close fd");
32+
wasi::path_unlink_file(dir_fd, "file").expect("failed to remove file");
33+
}
34+
35+
unsafe fn test_fd_filestat_set_size_ro(dir_fd: wasi::Fd) {
36+
// Create a file in the scratch directory. Creating a file implies opening it for writing, so
37+
// we have to close and re-open read-only to observe read-only behavior.
38+
let file_fd = wasi::path_open(dir_fd, 0, "file", wasi::OFLAGS_CREAT, 0, 0, 0)
39+
.expect("failed to create file");
40+
wasi::fd_close(file_fd).expect("failed to close fd");
41+
42+
// Open the created file read-only
43+
let file_fd = wasi::path_open(dir_fd, 0, "file", 0, wasi::RIGHTS_FD_READ, 0, 0)
44+
.expect("failed to create file");
45+
46+
// Check file size
47+
let stat = wasi::fd_filestat_get(file_fd).expect("failed filestat");
48+
assert_eq!(stat.size, 0, "file size should be 0");
49+
50+
// Check fd_filestat_set_size on a file opened read-only fails with EINVAL, like ftruncate is defined to do on posix
51+
assert_eq!(
52+
wasi::fd_filestat_set_size(file_fd, 100)
53+
.expect_err("fd_filestat_set_size should error when file is opened read-only"),
54+
wasi::ERRNO_INVAL
55+
);
56+
57+
let stat = wasi::fd_filestat_get(file_fd).expect("failed filestat 2");
58+
assert_eq!(stat.size, 0, "file size should remain 0");
59+
60+
wasi::fd_close(file_fd).expect("failed to close fd");
61+
wasi::path_unlink_file(dir_fd, "file").expect("failed to remove file");
62+
}
63+
64+
unsafe fn test_fd_filestat_set_times(dir_fd: wasi::Fd, rights: wasi::Rights) {
65+
let resolution = wasi::clock_res_get(wasi::CLOCKID_MONOTONIC).unwrap();
66+
67+
// Create a file in the scratch directory. OFLAGS_CREAT implies opening for writing, so we will
68+
// close it and re-open with the desired rights (FD_READ for read only, FD_READ | FD_WRITE for
69+
// readwrite)
70+
let file_fd = wasi::path_open(dir_fd, 0, "file", wasi::OFLAGS_CREAT, 0, 0, 0)
71+
.expect("failed to create file");
72+
73+
wasi::fd_close(file_fd).expect("failed to close fd");
74+
75+
// Open the file with the rights given.
76+
let file_fd =
77+
wasi::path_open(dir_fd, 0, "file", 0, rights, 0, 0).expect("failed to create file");
78+
79+
let stat = wasi::fd_filestat_get(file_fd).expect("failed filestat 2");
80+
81+
// Check fd_filestat_set_times
82+
let old_atim = stat.atim;
83+
let new_mtim = stat.mtim - resolution * 2;
84+
85+
wasi::fd_filestat_set_times(file_fd, new_mtim, new_mtim, wasi::FSTFLAGS_MTIM)
86+
.expect("fd_filestat_set_times");
87+
88+
let stat = wasi::fd_filestat_get(file_fd).expect("failed filestat 3");
89+
assert_eq!(stat.size, 0, "file size should remain unchanged at 0");
90+
91+
// Support accuracy up to at least 1ms
92+
assert_eq!(stat.mtim, new_mtim, "mtim should change");
93+
94+
assert_eq!(stat.atim, old_atim, "atim should not change");
95+
96+
// let status = wasi_fd_filestat_set_times(file_fd, new_mtim, new_mtim, wasi::FILESTAT_SET_MTIM | wasi::FILESTAT_SET_MTIM_NOW);
97+
// assert_eq!(status, wasi::EINVAL, "ATIM & ATIM_NOW can't both be set");
98+
99+
wasi::fd_close(file_fd).expect("failed to close fd");
100+
wasi::path_unlink_file(dir_fd, "file").expect("failed to remove file");
101+
}
102+
103+
#[test]
104+
fn fd_filestat_test_tests() {
105+
init(&[], &[]);
106+
107+
// Open scratch directory
108+
let dir_fd = 3; // root folder
109+
110+
// Run the tests.
111+
unsafe { test_fd_filestat_set_size_rw(dir_fd) }
112+
unsafe { test_fd_filestat_set_size_ro(dir_fd) }
113+
114+
// test to work on unix-like file system
115+
unsafe { test_fd_filestat_set_times(dir_fd, wasi::RIGHTS_FD_READ) }
116+
117+
unsafe { test_fd_filestat_set_times(dir_fd, wasi::RIGHTS_FD_READ | wasi::RIGHTS_FD_WRITE) }
118+
}

0 commit comments

Comments
 (0)