Skip to content

Commit

Permalink
Add more tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
sunfishcode committed May 3, 2023
1 parent f54344e commit f98249d
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 0 deletions.
54 changes: 54 additions & 0 deletions tests/fs/chmodat.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#[cfg(not(target_os = "wasi"))]
#[test]
fn test_chmodat() {
use rustix::fs::{chmodat, cwd, openat, statat, AtFlags, Mode, OFlags};

let tmp = tempfile::tempdir().unwrap();
let dir = openat(cwd(), tmp.path(), OFlags::RDONLY, Mode::RWXU).unwrap();

let _ = openat(&dir, "foo", OFlags::CREATE | OFlags::WRONLY, Mode::RWXU).unwrap();

let before = statat(&dir, "foo", AtFlags::empty()).unwrap();
assert_ne!(before.st_mode as u64 & libc::S_IRWXU as u64, 0);

chmodat(&dir, "foo", Mode::empty()).unwrap();

let after = statat(&dir, "foo", AtFlags::empty()).unwrap();
assert_eq!(after.st_mode as u64 & libc::S_IRWXU as u64, 0);

chmodat(&dir, "foo", Mode::RWXU).unwrap();

let reverted = statat(&dir, "foo", AtFlags::empty()).unwrap();
assert_ne!(reverted.st_mode as u64 & libc::S_IRWXU as u64, 0);
}

#[cfg(not(target_os = "wasi"))]
#[test]
fn test_chmodat_with() {
use rustix::fs::{chmodat_with, cwd, openat, statat, symlinkat, AtFlags, Mode, OFlags};

let tmp = tempfile::tempdir().unwrap();
let dir = openat(cwd(), tmp.path(), OFlags::RDONLY, Mode::RWXU).unwrap();

let _ = openat(&dir, "foo", OFlags::CREATE | OFlags::WRONLY, Mode::RWXU).unwrap();
symlinkat("foo", &dir, "link").unwrap();

match chmodat_with(&dir, "link", Mode::empty(), AtFlags::SYMLINK_NOFOLLOW) {
Ok(()) => (),
Err(rustix::io::Errno::OPNOTSUPP) => return,
Err(e) => Err(e).unwrap(),
}

let before = statat(&dir, "foo", AtFlags::empty()).unwrap();
assert_ne!(before.st_mode as u64 & libc::S_IRWXU as u64, 0);

chmodat_with(&dir, "foo", Mode::empty(), AtFlags::empty()).unwrap();

let after = statat(&dir, "foo", AtFlags::empty()).unwrap();
assert_eq!(after.st_mode as u64 & libc::S_IRWXU as u64, 0);

chmodat_with(&dir, "foo", Mode::RWXU, AtFlags::empty()).unwrap();

let reverted = statat(&dir, "foo", AtFlags::empty()).unwrap();
assert_ne!(reverted.st_mode as u64 & libc::S_IRWXU as u64, 0);
}
26 changes: 26 additions & 0 deletions tests/fs/linkat.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#[test]
fn test_linkat() {
use rustix::fs::{cwd, linkat, openat, readlinkat, statat, AtFlags, Mode, OFlags};

let tmp = tempfile::tempdir().unwrap();
let dir = openat(cwd(), tmp.path(), OFlags::RDONLY, Mode::empty()).unwrap();

let _ = openat(&dir, "foo", OFlags::CREATE | OFlags::WRONLY, Mode::RUSR).unwrap();

linkat(&dir, "foo", &dir, "link", AtFlags::empty()).unwrap();

readlinkat(&dir, "foo", Vec::new()).unwrap_err();
readlinkat(&dir, "link", Vec::new()).unwrap_err();

assert_eq!(
statat(&dir, "foo", AtFlags::empty()).unwrap().st_ino,
statat(&dir, "link", AtFlags::empty()).unwrap().st_ino
);

linkat(&dir, "link", &dir, "another", AtFlags::empty()).unwrap();

assert_eq!(
statat(&dir, "foo", AtFlags::empty()).unwrap().st_ino,
statat(&dir, "another", AtFlags::empty()).unwrap().st_ino
);
}
4 changes: 4 additions & 0 deletions tests/fs/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#![cfg_attr(io_lifetimes_use_std, feature(io_safety))]
#![cfg_attr(core_c_str, feature(core_c_str))]

mod chmodat;
mod cwd;
mod dir;
mod fcntl;
Expand All @@ -20,6 +21,7 @@ mod file;
mod flock;
mod futimens;
mod invalid_offset;
mod linkat;
mod long_paths;
#[cfg(not(any(solarish, target_os = "haiku", target_os = "redox", target_os = "wasi")))]
mod makedev;
Expand All @@ -31,11 +33,13 @@ mod openat;
mod openat2;
#[cfg(not(target_os = "redox"))]
mod readdir;
mod readlinkat;
mod renameat;
#[cfg(not(any(target_os = "haiku", target_os = "redox", target_os = "wasi")))]
mod statfs;
#[cfg(any(target_os = "android", target_os = "linux"))]
mod statx;
mod symlinkat;
#[cfg(not(any(solarish, target_os = "redox", target_os = "wasi")))]
mod sync;
mod utimensat;
Expand Down
23 changes: 23 additions & 0 deletions tests/fs/readlinkat.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#[test]
fn test_readlinkat() {
use rustix::fs::{cwd, openat, readlinkat, symlinkat, Mode, OFlags};

let tmp = tempfile::tempdir().unwrap();
let dir = openat(cwd(), tmp.path(), OFlags::RDONLY, Mode::empty()).unwrap();

let _ = openat(&dir, "foo", OFlags::CREATE | OFlags::WRONLY, Mode::RUSR).unwrap();
symlinkat("foo", &dir, "link").unwrap();

readlinkat(&dir, "absent", Vec::new()).unwrap_err();
readlinkat(&dir, "foo", Vec::new()).unwrap_err();

let target = readlinkat(&dir, "link", Vec::new()).unwrap();
assert_eq!(target.to_string_lossy(), "foo");

symlinkat("link", &dir, "another").unwrap();

let target = readlinkat(&dir, "link", Vec::new()).unwrap();
assert_eq!(target.to_string_lossy(), "foo");
let target = readlinkat(&dir, "another", Vec::new()).unwrap();
assert_eq!(target.to_string_lossy(), "link");
}
21 changes: 21 additions & 0 deletions tests/fs/symlinkat.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#[test]
fn test_symlinkat() {
use rustix::fs::{cwd, openat, readlinkat, statat, symlinkat, AtFlags, Mode, OFlags};

let tmp = tempfile::tempdir().unwrap();
let dir = openat(cwd(), tmp.path(), OFlags::RDONLY, Mode::empty()).unwrap();

let _ = openat(&dir, "foo", OFlags::CREATE | OFlags::WRONLY, Mode::RUSR).unwrap();
symlinkat("foo", &dir, "link").unwrap();

let target = readlinkat(&dir, "link", Vec::new()).unwrap();
assert_eq!(target.to_string_lossy(), "foo");

assert_eq!(
statat(&dir, "link", AtFlags::SYMLINK_NOFOLLOW)
.unwrap()
.st_mode as u64
& libc::S_IFMT as u64,
libc::S_IFLNK as u64
);
}

0 comments on commit f98249d

Please sign in to comment.