Skip to content

Commit 9bf4ab3

Browse files
committed
Add trivial tests for chown and fchownat
These are mostly to ensure that all the platforms we care about in our CI can reference these primitives.
1 parent f3dab48 commit 9bf4ab3

File tree

1 file changed

+44
-1
lines changed

1 file changed

+44
-1
lines changed

test/test_unistd.rs

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use nix::fcntl::{fcntl, FcntlArg, FdFlag, OFlag};
1+
use nix::fcntl::{fcntl, FcntlArg, FdFlag, open, OFlag};
22
use nix::unistd::*;
33
use nix::unistd::ForkResult::*;
44
use nix::sys::signal::{SaFlags, SigAction, SigHandler, SigSet, Signal, sigaction};
@@ -301,6 +301,49 @@ fn test_getcwd() {
301301
assert_eq!(getcwd().unwrap(), inner_tmp_dir.as_path());
302302
}
303303

304+
#[test]
305+
fn test_chown() {
306+
// Testing for anything other than our own UID/GID is hard.
307+
let uid = Some(getuid());
308+
let gid = Some(getgid());
309+
310+
let tempdir = tempfile::tempdir().unwrap();
311+
let path = tempdir.path().join("file");
312+
{
313+
File::create(&path).unwrap();
314+
}
315+
316+
chown(&path, uid, gid).unwrap();
317+
chown(&path, uid, None).unwrap();
318+
chown(&path, None, gid).unwrap();
319+
320+
fs::remove_file(&path).unwrap();
321+
chown(&path, uid, gid).unwrap_err();
322+
}
323+
324+
#[test]
325+
fn test_fchownat() {
326+
// Testing for anything other than our own UID/GID is hard.
327+
let uid = Some(getuid());
328+
let gid = Some(getgid());
329+
330+
let tempdir = tempfile::tempdir().unwrap();
331+
let path = tempdir.path().join("file");
332+
{
333+
File::create(&path).unwrap();
334+
}
335+
336+
let dirfd = open(tempdir.path(), OFlag::empty(), Mode::empty()).unwrap();
337+
338+
fchownat(Some(dirfd), "file", uid, gid, FchownatFlags::FollowSymlink).unwrap();
339+
340+
chdir(tempdir.path()).unwrap();
341+
fchownat(None, "file", uid, gid, FchownatFlags::FollowSymlink).unwrap();
342+
343+
fs::remove_file(&path).unwrap();
344+
fchownat(None, "file", uid, gid, FchownatFlags::FollowSymlink).unwrap_err();
345+
}
346+
304347
#[test]
305348
fn test_lseek() {
306349
const CONTENTS: &[u8] = b"abcdef123456";

0 commit comments

Comments
 (0)