Skip to content

Commit e70baf9

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 df21f5e commit e70baf9

File tree

1 file changed

+45
-2
lines changed

1 file changed

+45
-2
lines changed

test/test_unistd.rs

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
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};
55
use nix::sys::wait::*;
66
use nix::sys::stat::{self, Mode, SFlag};
77
use std::{env, iter};
88
use std::ffi::CString;
9-
use std::fs::File;
9+
use std::fs::{self, File};
1010
use std::io::Write;
1111
use std::os::unix::prelude::*;
1212
use tempfile::{self, tempfile};
@@ -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)