From 9740612eceee4d8a5d67b7f55043ffb6a7136eac Mon Sep 17 00:00:00 2001 From: Julio Merino Date: Wed, 17 Oct 2018 12:03:08 -0400 Subject: [PATCH] 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. --- test/test_unistd.rs | 43 +++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 41 insertions(+), 2 deletions(-) diff --git a/test/test_unistd.rs b/test/test_unistd.rs index 54cbff8dcf..d7e92f6214 100644 --- a/test/test_unistd.rs +++ b/test/test_unistd.rs @@ -1,4 +1,4 @@ -use nix::fcntl::{fcntl, FcntlArg, FdFlag, OFlag}; +use nix::fcntl::{fcntl, FcntlArg, FdFlag, open, OFlag}; use nix::unistd::*; use nix::unistd::ForkResult::*; use nix::sys::signal::{SaFlags, SigAction, SigHandler, SigSet, Signal, sigaction}; @@ -6,7 +6,7 @@ use nix::sys::wait::*; use nix::sys::stat::{self, Mode, SFlag}; use std::{env, iter}; use std::ffi::CString; -use std::fs::File; +use std::fs::{self, File}; use std::io::Write; use std::os::unix::prelude::*; use tempfile::{self, tempfile}; @@ -301,6 +301,45 @@ fn test_getcwd() { assert_eq!(getcwd().unwrap(), inner_tmp_dir.as_path()); } +#[test] +fn test_chown() { + // Testing for anything other than our own UID/GID is hard. + let uid = Some(getuid()); + let gid = Some(getgid()); + + let tempdir = tempfile::tempdir().unwrap(); + let path = tempdir.path().join("file"); + drop(File::create(&path).unwrap()); + + chown(&path, uid, gid).unwrap(); + chown(&path, uid, None).unwrap(); + chown(&path, None, gid).unwrap(); + + fs::remove_file(&path).unwrap(); + chown(&path, uid, gid).unwrap_err(); +} + +#[test] +fn test_fchownat() { + // Testing for anything other than our own UID/GID is hard. + let uid = Some(getuid()); + let gid = Some(getgid()); + + let tempdir = tempfile::tempdir().unwrap(); + let path = tempdir.path().join("file"); + drop(File::create(&path).unwrap()); + + let dirfd = open(tempdir.path(), OFlag::empty(), Mode::empty()).unwrap(); + + fchownat(Some(dirfd), "file", uid, gid, FchownatFlags::FollowSymlink).unwrap(); + + chdir(tempdir.path()).unwrap(); + fchownat(None, "file", uid, gid, FchownatFlags::FollowSymlink).unwrap(); + + fs::remove_file(&path).unwrap(); + fchownat(None, "file", uid, gid, FchownatFlags::FollowSymlink).unwrap_err(); +} + #[test] fn test_lseek() { const CONTENTS: &[u8] = b"abcdef123456";