|
| 1 | +use libc; |
| 2 | +use more_asserts::assert_gt; |
| 3 | +use std::{env, mem, process}; |
| 4 | +use wasi::wasi_unstable; |
| 5 | +use wasi_misc_tests::open_scratch_directory; |
| 6 | +use wasi_misc_tests::wasi_wrappers::wasi_fd_fdstat_get; |
| 7 | + |
| 8 | +unsafe fn test_close_preopen(dir_fd: wasi_unstable::Fd) { |
| 9 | + let pre_fd: wasi_unstable::Fd = (libc::STDERR_FILENO + 1) as wasi_unstable::Fd; |
| 10 | + |
| 11 | + assert_gt!(dir_fd, pre_fd, "dir_fd number"); |
| 12 | + |
| 13 | + // Try to close a preopened directory handle. |
| 14 | + assert_eq!( |
| 15 | + wasi_unstable::fd_close(pre_fd), |
| 16 | + Err(wasi_unstable::ENOTSUP), |
| 17 | + "closing a preopened file descriptor", |
| 18 | + ); |
| 19 | + |
| 20 | + // Try to renumber over a preopened directory handle. |
| 21 | + assert_eq!( |
| 22 | + wasi_unstable::fd_renumber(dir_fd, pre_fd), |
| 23 | + Err(wasi_unstable::ENOTSUP), |
| 24 | + "renumbering over a preopened file descriptor", |
| 25 | + ); |
| 26 | + |
| 27 | + // Ensure that dir_fd is still open. |
| 28 | + let mut dir_fdstat: wasi_unstable::FdStat = mem::zeroed(); |
| 29 | + let mut status = wasi_fd_fdstat_get(dir_fd, &mut dir_fdstat); |
| 30 | + assert_eq!( |
| 31 | + status, |
| 32 | + wasi_unstable::raw::__WASI_ESUCCESS, |
| 33 | + "calling fd_fdstat on the scratch directory" |
| 34 | + ); |
| 35 | + assert_eq!( |
| 36 | + dir_fdstat.fs_filetype, |
| 37 | + wasi_unstable::FILETYPE_DIRECTORY, |
| 38 | + "expected the scratch directory to be a directory", |
| 39 | + ); |
| 40 | + |
| 41 | + // Try to renumber a preopened directory handle. |
| 42 | + assert_eq!( |
| 43 | + wasi_unstable::fd_renumber(pre_fd, dir_fd), |
| 44 | + Err(wasi_unstable::ENOTSUP), |
| 45 | + "renumbering over a preopened file descriptor", |
| 46 | + ); |
| 47 | + |
| 48 | + // Ensure that dir_fd is still open. |
| 49 | + status = wasi_fd_fdstat_get(dir_fd, &mut dir_fdstat); |
| 50 | + assert_eq!( |
| 51 | + status, |
| 52 | + wasi_unstable::raw::__WASI_ESUCCESS, |
| 53 | + "calling fd_fdstat on the scratch directory" |
| 54 | + ); |
| 55 | + assert_eq!( |
| 56 | + dir_fdstat.fs_filetype, |
| 57 | + wasi_unstable::FILETYPE_DIRECTORY, |
| 58 | + "expected the scratch directory to be a directory", |
| 59 | + ); |
| 60 | +} |
| 61 | + |
| 62 | +fn main() { |
| 63 | + let mut args = env::args(); |
| 64 | + let prog = args.next().unwrap(); |
| 65 | + let arg = if let Some(arg) = args.next() { |
| 66 | + arg |
| 67 | + } else { |
| 68 | + eprintln!("usage: {} <scratch directory>", prog); |
| 69 | + process::exit(1); |
| 70 | + }; |
| 71 | + |
| 72 | + // Open scratch directory |
| 73 | + let dir_fd = match open_scratch_directory(&arg) { |
| 74 | + Ok(dir_fd) => dir_fd, |
| 75 | + Err(err) => { |
| 76 | + eprintln!("{}", err); |
| 77 | + process::exit(1) |
| 78 | + } |
| 79 | + }; |
| 80 | + |
| 81 | + // Run the tests. |
| 82 | + unsafe { test_close_preopen(dir_fd) } |
| 83 | +} |
0 commit comments