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