Vendor dependencies for 0.3.0 release

This commit is contained in:
2025-09-27 10:29:08 -05:00
parent 0c8d39d483
commit 82ab7f317b
26803 changed files with 16134934 additions and 0 deletions

149
vendor/nix/test/common/mod.rs vendored Normal file
View File

@@ -0,0 +1,149 @@
use cfg_if::cfg_if;
#[macro_export]
macro_rules! skip {
($($reason: expr),+) => {{
use ::std::io::{self, Write};
let stderr = io::stderr();
let mut handle = stderr.lock();
writeln!(handle, $($reason),+).unwrap();
return;
}}
}
cfg_if! {
if #[cfg(linux_android)] {
#[macro_export] macro_rules! require_capability {
($name:expr, $capname:ident) => {
use ::caps::{Capability, CapSet, has_cap};
if !has_cap(None, CapSet::Effective, Capability::$capname)
.unwrap()
{
skip!("{} requires capability {}. Skipping test.", $name, Capability::$capname);
}
}
}
} else if #[cfg(not(target_os = "redox"))] {
#[macro_export] macro_rules! require_capability {
($name:expr, $capname:ident) => {}
}
}
}
/// Skip the test if we don't have the ability to mount file systems.
#[cfg(target_os = "freebsd")]
#[macro_export]
macro_rules! require_mount {
($name:expr) => {
use nix::unistd::Uid;
use sysctl::{CtlValue, Sysctl};
let ctl = ::sysctl::Ctl::new("vfs.usermount").unwrap();
if !Uid::current().is_root() && CtlValue::Int(0) == ctl.value().unwrap()
{
skip!(
"{} requires the ability to mount file systems. Skipping test.",
$name
);
}
};
}
#[cfg(linux_android)]
#[macro_export]
macro_rules! skip_if_cirrus {
($reason:expr) => {
if std::env::var_os("CIRRUS_CI").is_some() {
skip!("{}", $reason);
}
};
}
#[cfg(target_os = "freebsd")]
#[macro_export]
macro_rules! skip_if_jailed {
($name:expr) => {
use sysctl::{CtlValue, Sysctl};
let ctl = ::sysctl::Ctl::new("security.jail.jailed").unwrap();
if let CtlValue::Int(1) = ctl.value().unwrap() {
skip!("{} cannot run in a jail. Skipping test.", $name);
}
};
}
#[cfg(not(any(target_os = "redox", target_os = "fuchsia")))]
#[macro_export]
macro_rules! skip_if_not_root {
($name:expr) => {
use nix::unistd::Uid;
if !Uid::current().is_root() {
skip!("{} requires root privileges. Skipping test.", $name);
}
};
}
cfg_if! {
if #[cfg(linux_android)] {
#[macro_export] macro_rules! skip_if_seccomp {
($name:expr) => {
if let Ok(s) = std::fs::read_to_string("/proc/self/status") {
for l in s.lines() {
let mut fields = l.split_whitespace();
if fields.next() == Some("Seccomp:") &&
fields.next() != Some("0")
{
skip!("{} cannot be run in Seccomp mode. Skipping test.",
stringify!($name));
}
}
}
}
}
} else if #[cfg(not(target_os = "redox"))] {
#[macro_export] macro_rules! skip_if_seccomp {
($name:expr) => {}
}
}
}
cfg_if! {
if #[cfg(target_os = "linux")] {
#[macro_export] macro_rules! require_kernel_version {
($name:expr, $version_requirement:expr) => {
use semver::{Version, VersionReq};
let version_requirement = VersionReq::parse($version_requirement)
.expect("Bad match_version provided");
let uname = nix::sys::utsname::uname().unwrap();
println!("{}", uname.sysname().to_str().unwrap());
println!("{}", uname.nodename().to_str().unwrap());
println!("{}", uname.release().to_str().unwrap());
println!("{}", uname.version().to_str().unwrap());
println!("{}", uname.machine().to_str().unwrap());
// Fix stuff that the semver parser can't handle
let fixed_release = &uname.release().to_str().unwrap().to_string()
// Fedora 33 reports version as 4.18.el8_2.x86_64 or
// 5.18.200-fc33.x86_64. Remove the underscore.
.replace("_", "-")
// Cirrus-CI reports version as 4.19.112+ . Remove the +
.replace("+", "");
let mut version = Version::parse(fixed_release).unwrap();
//Keep only numeric parts
version.pre = semver::Prerelease::EMPTY;
version.build = semver::BuildMetadata::EMPTY;
if !version_requirement.matches(&version) {
skip!("Skip {} because kernel version `{}` doesn't match the requirement `{}`",
stringify!($name), version, version_requirement);
}
}
}
}
}

6
vendor/nix/test/mount/mod.rs vendored Normal file
View File

@@ -0,0 +1,6 @@
#[cfg(target_os = "linux")]
mod test_mount;
#[cfg(apple_targets)]
mod test_mount_apple;
#[cfg(target_os = "freebsd")]
mod test_nmount;

189
vendor/nix/test/mount/test_mount.rs vendored Normal file
View File

@@ -0,0 +1,189 @@
use std::fs::{self, File};
use std::io::{Read, Write};
use std::os::unix::fs::OpenOptionsExt;
use std::os::unix::fs::PermissionsExt;
use std::process::Command;
use libc::{EACCES, EROFS};
use nix::mount::{mount, umount, MsFlags};
use nix::sys::stat::{self, Mode};
use crate::*;
static SCRIPT_CONTENTS: &[u8] = b"#!/bin/sh
exit 23";
const EXPECTED_STATUS: i32 = 23;
const NONE: Option<&'static [u8]> = None;
#[test]
fn test_mount_tmpfs_without_flags_allows_rwx() {
require_capability!(
"test_mount_tmpfs_without_flags_allows_rwx",
CAP_SYS_ADMIN
);
let tempdir = tempfile::tempdir().unwrap();
mount(
NONE,
tempdir.path(),
Some(b"tmpfs".as_ref()),
MsFlags::empty(),
NONE,
)
.unwrap_or_else(|e| panic!("mount failed: {e}"));
let test_path = tempdir.path().join("test");
// Verify write.
fs::OpenOptions::new()
.create(true)
.write(true)
.mode((Mode::S_IRWXU | Mode::S_IRWXG | Mode::S_IRWXO).bits())
.open(&test_path)
.and_then(|mut f| f.write(SCRIPT_CONTENTS))
.unwrap_or_else(|e| panic!("write failed: {e}"));
// Verify read.
let mut buf = Vec::new();
File::open(&test_path)
.and_then(|mut f| f.read_to_end(&mut buf))
.unwrap_or_else(|e| panic!("read failed: {e}"));
assert_eq!(buf, SCRIPT_CONTENTS);
// while forking and unmounting prevent other child processes
let _m = FORK_MTX.lock();
// Verify execute.
assert_eq!(
EXPECTED_STATUS,
Command::new(&test_path)
.status()
.unwrap_or_else(|e| panic!("exec failed: {e}"))
.code()
.unwrap_or_else(|| panic!("child killed by signal"))
);
umount(tempdir.path()).unwrap_or_else(|e| panic!("umount failed: {e}"));
}
#[test]
fn test_mount_rdonly_disallows_write() {
require_capability!("test_mount_rdonly_disallows_write", CAP_SYS_ADMIN);
let tempdir = tempfile::tempdir().unwrap();
mount(
NONE,
tempdir.path(),
Some(b"tmpfs".as_ref()),
MsFlags::MS_RDONLY,
NONE,
)
.unwrap_or_else(|e| panic!("mount failed: {e}"));
// EROFS: Read-only file system
assert_eq!(
EROFS,
File::create(tempdir.path().join("test"))
.unwrap_err()
.raw_os_error()
.unwrap()
);
umount(tempdir.path()).unwrap_or_else(|e| panic!("umount failed: {e}"));
}
#[test]
fn test_mount_noexec_disallows_exec() {
require_capability!("test_mount_noexec_disallows_exec", CAP_SYS_ADMIN);
let tempdir = tempfile::tempdir().unwrap();
mount(
NONE,
tempdir.path(),
Some(b"tmpfs".as_ref()),
MsFlags::MS_NOEXEC,
NONE,
)
.unwrap_or_else(|e| panic!("mount failed: {e}"));
let test_path = tempdir.path().join("test");
fs::OpenOptions::new()
.create(true)
.write(true)
.mode((Mode::S_IRWXU | Mode::S_IRWXG | Mode::S_IRWXO).bits())
.open(&test_path)
.and_then(|mut f| f.write(SCRIPT_CONTENTS))
.unwrap_or_else(|e| panic!("write failed: {e}"));
// Verify that we cannot execute despite a+x permissions being set.
let mode = stat::Mode::from_bits_truncate(
fs::metadata(&test_path)
.map(|md| md.permissions().mode())
.unwrap_or_else(|e| panic!("metadata failed: {e}")),
);
assert!(
mode.contains(Mode::S_IXUSR | Mode::S_IXGRP | Mode::S_IXOTH),
"{:?} did not have execute permissions",
&test_path
);
// while forking and unmounting prevent other child processes
let _m = FORK_MTX.lock();
// EACCES: Permission denied
assert_eq!(
EACCES,
Command::new(&test_path)
.status()
.unwrap_err()
.raw_os_error()
.unwrap()
);
umount(tempdir.path()).unwrap_or_else(|e| panic!("umount failed: {e}"));
}
#[test]
fn test_mount_bind() {
require_capability!("test_mount_bind", CAP_SYS_ADMIN);
let tempdir = tempfile::tempdir().unwrap();
let file_name = "test";
{
let mount_point = tempfile::tempdir().unwrap();
mount(
Some(tempdir.path()),
mount_point.path(),
NONE,
MsFlags::MS_BIND,
NONE,
)
.unwrap_or_else(|e| panic!("mount failed: {e}"));
fs::OpenOptions::new()
.create(true)
.write(true)
.mode((Mode::S_IRWXU | Mode::S_IRWXG | Mode::S_IRWXO).bits())
.open(mount_point.path().join(file_name))
.and_then(|mut f| f.write(SCRIPT_CONTENTS))
.unwrap_or_else(|e| panic!("write failed: {e}"));
// wait for child processes to prevent EBUSY
let _m = FORK_MTX.lock();
umount(mount_point.path())
.unwrap_or_else(|e| panic!("umount failed: {e}"));
}
// Verify the file written in the mount shows up in source directory, even
// after unmounting.
let mut buf = Vec::new();
File::open(tempdir.path().join(file_name))
.and_then(|mut f| f.read_to_end(&mut buf))
.unwrap_or_else(|e| panic!("read failed: {e}"));
assert_eq!(buf, SCRIPT_CONTENTS);
}

View File

@@ -0,0 +1,8 @@
use nix::errno::Errno;
use nix::mount::{mount, MntFlags};
#[test]
fn test_mount() {
let res = mount::<str, str, str>("", "", MntFlags::empty(), None);
assert_eq!(res, Err(Errno::ENOENT));
}

49
vendor/nix/test/mount/test_nmount.rs vendored Normal file
View File

@@ -0,0 +1,49 @@
use crate::*;
use nix::{
errno::Errno,
mount::{unmount, MntFlags, Nmount},
};
use std::{ffi::CString, fs::File, path::Path};
use tempfile::tempdir;
#[test]
fn ok() {
require_mount!("nullfs");
let mountpoint = tempdir().unwrap();
let target = tempdir().unwrap();
let _sentry = File::create(target.path().join("sentry")).unwrap();
let fstype = CString::new("fstype").unwrap();
let nullfs = CString::new("nullfs").unwrap();
Nmount::new()
.str_opt(&fstype, &nullfs)
.str_opt_owned("fspath", mountpoint.path().to_str().unwrap())
.str_opt_owned("target", target.path().to_str().unwrap())
.nmount(MntFlags::empty())
.unwrap();
// Now check that the sentry is visible through the mountpoint
let exists = Path::exists(&mountpoint.path().join("sentry"));
// Cleanup the mountpoint before asserting
unmount(mountpoint.path(), MntFlags::empty()).unwrap();
assert!(exists);
}
#[test]
fn bad_fstype() {
let mountpoint = tempdir().unwrap();
let target = tempdir().unwrap();
let _sentry = File::create(target.path().join("sentry")).unwrap();
let e = Nmount::new()
.str_opt_owned("fspath", mountpoint.path().to_str().unwrap())
.str_opt_owned("target", target.path().to_str().unwrap())
.nmount(MntFlags::empty())
.unwrap_err();
assert_eq!(e.error(), Errno::EINVAL);
assert_eq!(e.errmsg(), Some("Invalid fstype"));
}

98
vendor/nix/test/sys/mod.rs vendored Normal file
View File

@@ -0,0 +1,98 @@
mod test_signal;
// NOTE: DragonFly lacks a kernel-level implementation of Posix AIO as of
// this writing. There is an user-level implementation, but whether aio
// works or not heavily depends on which pthread implementation is chosen
// by the user at link time. For this reason we do not want to run aio test
// cases on DragonFly.
#[cfg(any(
target_os = "freebsd",
apple_targets,
all(
target_os = "linux",
not(any(target_env = "uclibc", target_env = "ohos"))
),
target_os = "netbsd"
))]
mod test_aio;
#[cfg(not(any(
target_os = "redox",
target_os = "fuchsia",
target_os = "haiku",
target_os = "hurd",
target_os = "cygwin"
)))]
mod test_ioctl;
#[cfg(not(target_os = "redox"))]
mod test_mman;
#[cfg(not(target_os = "redox"))]
mod test_select;
#[cfg(target_os = "linux")]
mod test_signalfd;
#[cfg(not(any(target_os = "redox", target_os = "haiku")))]
mod test_socket;
#[cfg(not(any(target_os = "redox")))]
mod test_sockopt;
mod test_stat;
#[cfg(linux_android)]
mod test_sysinfo;
#[cfg(not(any(
target_os = "redox",
target_os = "fuchsia",
target_os = "haiku"
)))]
mod test_termios;
mod test_uio;
mod test_wait;
#[cfg(linux_android)]
mod test_epoll;
#[cfg(target_os = "linux")]
mod test_fanotify;
#[cfg(target_os = "linux")]
mod test_inotify;
mod test_pthread;
#[cfg(any(linux_android, freebsdlike, netbsdlike, apple_targets))]
mod test_ptrace;
#[cfg(linux_android)]
mod test_timerfd;
#[cfg(all(
any(
target_os = "freebsd",
solarish,
target_os = "linux",
target_os = "netbsd"
),
feature = "time",
feature = "signal"
))]
mod test_timer;
#[cfg(bsd)]
mod test_event;
mod test_statvfs;
mod test_time;
mod test_utsname;
#[cfg(any(linux_android, freebsdlike, apple_targets, target_os = "openbsd"))]
mod test_statfs;
#[cfg(not(any(
target_os = "redox",
target_os = "fuchsia",
solarish,
target_os = "haiku"
)))]
mod test_resource;
// This test module should be enabled for both linux_android and freebsd, but
// the `memfd_create(2)` symbol is not available under Linux QEMU,
//
// https://github.com/nix-rust/nix/actions/runs/9427112650/job/25970870477
//
// and I haven't found a way to stop the linker from linking that symbol, so
// only enable this for FreeBSD for now.
#[cfg(target_os = "freebsd")]
mod test_memfd;

685
vendor/nix/test/sys/test_aio.rs vendored Normal file
View File

@@ -0,0 +1,685 @@
use std::{
io::{Read, Seek, Write},
ops::Deref,
os::unix::io::{AsFd, AsRawFd, BorrowedFd},
pin::Pin,
sync::atomic::{AtomicBool, Ordering},
thread, time,
};
use libc::c_int;
use nix::{
errno::*,
sys::{
aio::*,
signal::{
sigaction, SaFlags, SigAction, SigHandler, SigSet, SigevNotify,
Signal,
},
time::{TimeSpec, TimeValLike},
},
};
use tempfile::tempfile;
pub static SIGNALED: AtomicBool = AtomicBool::new(false);
extern "C" fn sigfunc(_: c_int) {
SIGNALED.store(true, Ordering::Relaxed);
}
// Helper that polls an AioCb for completion or error
macro_rules! poll_aio {
($aiocb: expr) => {
loop {
let err = $aiocb.as_mut().error();
if err != Err(Errno::EINPROGRESS) {
break err;
};
thread::sleep(time::Duration::from_millis(10));
}
};
}
mod aio_fsync {
use super::*;
#[test]
fn test_accessors() {
let f = tempfile().unwrap();
let aiocb = AioFsync::new(
f.as_fd(),
AioFsyncMode::O_SYNC,
42,
SigevNotify::SigevSignal {
signal: Signal::SIGUSR2,
si_value: 99,
},
);
assert_eq!(f.as_raw_fd(), aiocb.fd().as_raw_fd());
assert_eq!(AioFsyncMode::O_SYNC, aiocb.mode());
assert_eq!(42, aiocb.priority());
let sev = aiocb.sigevent().sigevent();
assert_eq!(Signal::SIGUSR2 as i32, sev.sigev_signo);
assert_eq!(99, sev.sigev_value.sival_ptr as i64);
}
/// `AioFsync::submit` should not modify the `AioCb` object if
/// `libc::aio_fsync` returns an error
// Skip on Linux, because Linux's AIO implementation can't detect errors
// synchronously
#[test]
#[cfg_attr(any(target_os = "android", target_os = "linux"), ignore)]
fn error() {
use std::mem;
const INITIAL: &[u8] = b"abcdef123456";
// Create an invalid AioFsyncMode
let mode = unsafe { mem::transmute::<i32, AioFsyncMode>(666) };
let mut f = tempfile().unwrap();
f.write_all(INITIAL).unwrap();
let mut aiof =
Box::pin(AioFsync::new(f.as_fd(), mode, 0, SigevNotify::SigevNone));
let err = aiof.as_mut().submit();
err.expect_err("assertion failed");
}
#[test]
#[cfg_attr(all(target_env = "musl", target_arch = "x86_64"), ignore)]
fn ok() {
const INITIAL: &[u8] = b"abcdef123456";
let mut f = tempfile().unwrap();
f.write_all(INITIAL).unwrap();
let mut aiof = Box::pin(AioFsync::new(
f.as_fd(),
AioFsyncMode::O_SYNC,
0,
SigevNotify::SigevNone,
));
aiof.as_mut().submit().unwrap();
poll_aio!(&mut aiof).unwrap();
aiof.as_mut().aio_return().unwrap();
}
}
mod aio_read {
use super::*;
#[test]
fn test_accessors() {
let f = tempfile().unwrap();
let mut rbuf = vec![0; 4];
let aiocb = AioRead::new(
f.as_fd(),
2, //offset
&mut rbuf,
42, //priority
SigevNotify::SigevSignal {
signal: Signal::SIGUSR2,
si_value: 99,
},
);
assert_eq!(f.as_raw_fd(), aiocb.fd().as_raw_fd());
assert_eq!(4, aiocb.nbytes());
assert_eq!(2, aiocb.offset());
assert_eq!(42, aiocb.priority());
let sev = aiocb.sigevent().sigevent();
assert_eq!(Signal::SIGUSR2 as i32, sev.sigev_signo);
assert_eq!(99, sev.sigev_value.sival_ptr as i64);
}
// Tests AioWrite.cancel. We aren't trying to test the OS's implementation,
// only our bindings. So it's sufficient to check that cancel
// returned any AioCancelStat value.
#[test]
#[cfg_attr(all(target_env = "musl", target_arch = "x86_64"), ignore)]
fn cancel() {
const INITIAL: &[u8] = b"abcdef123456";
let mut rbuf = vec![0; 4];
let mut f = tempfile().unwrap();
f.write_all(INITIAL).unwrap();
let fd = f.as_fd();
let mut aior =
Box::pin(AioRead::new(fd, 2, &mut rbuf, 0, SigevNotify::SigevNone));
aior.as_mut().submit().unwrap();
aior.as_mut().cancel().unwrap();
// Wait for aiow to complete, but don't care whether it succeeded
let _ = poll_aio!(&mut aior);
let _ = aior.as_mut().aio_return();
}
/// `AioRead::submit` should not modify the `AioCb` object if
/// `libc::aio_read` returns an error
// Skip on Linux, because Linux's AIO implementation can't detect errors
// synchronously
#[test]
#[cfg(any(target_os = "freebsd", apple_targets))]
fn error() {
const INITIAL: &[u8] = b"abcdef123456";
let mut rbuf = vec![0; 4];
let mut f = tempfile().unwrap();
f.write_all(INITIAL).unwrap();
let mut aior = Box::pin(AioRead::new(
f.as_fd(),
-1, //an invalid offset
&mut rbuf,
0, //priority
SigevNotify::SigevNone,
));
aior.as_mut().submit().expect_err("assertion failed");
}
// Test a simple aio operation with no completion notification. We must
// poll for completion
#[test]
#[cfg_attr(all(target_env = "musl", target_arch = "x86_64"), ignore)]
fn ok() {
const INITIAL: &[u8] = b"abcdef123456";
let mut rbuf = vec![0; 4];
const EXPECT: &[u8] = b"cdef";
let mut f = tempfile().unwrap();
f.write_all(INITIAL).unwrap();
{
let fd = f.as_fd();
let mut aior = Box::pin(AioRead::new(
fd,
2,
&mut rbuf,
0,
SigevNotify::SigevNone,
));
aior.as_mut().submit().unwrap();
let err = poll_aio!(&mut aior);
assert_eq!(err, Ok(()));
assert_eq!(aior.as_mut().aio_return().unwrap(), EXPECT.len());
}
assert_eq!(EXPECT, rbuf.deref());
}
// Like ok, but allocates the structure on the stack.
#[test]
#[cfg_attr(all(target_env = "musl", target_arch = "x86_64"), ignore)]
fn on_stack() {
const INITIAL: &[u8] = b"abcdef123456";
let mut rbuf = vec![0; 4];
const EXPECT: &[u8] = b"cdef";
let mut f = tempfile().unwrap();
f.write_all(INITIAL).unwrap();
{
let fd = f.as_fd();
let mut aior =
AioRead::new(fd, 2, &mut rbuf, 0, SigevNotify::SigevNone);
let mut aior = unsafe { Pin::new_unchecked(&mut aior) };
aior.as_mut().submit().unwrap();
let err = poll_aio!(&mut aior);
assert_eq!(err, Ok(()));
assert_eq!(aior.as_mut().aio_return().unwrap(), EXPECT.len());
}
assert_eq!(EXPECT, rbuf.deref());
}
}
#[cfg(target_os = "freebsd")]
#[cfg(fbsd14)]
mod aio_readv {
use std::io::IoSliceMut;
use super::*;
#[test]
fn test_accessors() {
let f = tempfile().unwrap();
let mut rbuf0 = vec![0; 4];
let mut rbuf1 = vec![0; 8];
let mut rbufs =
[IoSliceMut::new(&mut rbuf0), IoSliceMut::new(&mut rbuf1)];
let aiocb = AioReadv::new(
f.as_fd(),
2, //offset
&mut rbufs,
42, //priority
SigevNotify::SigevSignal {
signal: Signal::SIGUSR2,
si_value: 99,
},
);
assert_eq!(f.as_raw_fd(), aiocb.fd().as_raw_fd());
assert_eq!(2, aiocb.iovlen());
assert_eq!(2, aiocb.offset());
assert_eq!(42, aiocb.priority());
let sev = aiocb.sigevent().sigevent();
assert_eq!(Signal::SIGUSR2 as i32, sev.sigev_signo);
assert_eq!(99, sev.sigev_value.sival_ptr as i64);
}
#[test]
#[cfg_attr(all(target_env = "musl", target_arch = "x86_64"), ignore)]
fn ok() {
const INITIAL: &[u8] = b"abcdef123456";
let mut rbuf0 = vec![0; 4];
let mut rbuf1 = vec![0; 2];
let mut rbufs =
[IoSliceMut::new(&mut rbuf0), IoSliceMut::new(&mut rbuf1)];
const EXPECT0: &[u8] = b"cdef";
const EXPECT1: &[u8] = b"12";
let mut f = tempfile().unwrap();
f.write_all(INITIAL).unwrap();
{
let fd = f.as_fd();
let mut aior = Box::pin(AioReadv::new(
fd,
2,
&mut rbufs,
0,
SigevNotify::SigevNone,
));
aior.as_mut().submit().unwrap();
let err = poll_aio!(&mut aior);
assert_eq!(err, Ok(()));
assert_eq!(
aior.as_mut().aio_return().unwrap(),
EXPECT0.len() + EXPECT1.len()
);
}
assert_eq!(&EXPECT0, &rbuf0);
assert_eq!(&EXPECT1, &rbuf1);
}
}
mod aio_write {
use super::*;
#[test]
fn test_accessors() {
let f = tempfile().unwrap();
let wbuf = vec![0; 4];
let aiocb = AioWrite::new(
f.as_fd(),
2, //offset
&wbuf,
42, //priority
SigevNotify::SigevSignal {
signal: Signal::SIGUSR2,
si_value: 99,
},
);
assert_eq!(f.as_raw_fd(), aiocb.fd().as_raw_fd());
assert_eq!(4, aiocb.nbytes());
assert_eq!(2, aiocb.offset());
assert_eq!(42, aiocb.priority());
let sev = aiocb.sigevent().sigevent();
assert_eq!(Signal::SIGUSR2 as i32, sev.sigev_signo);
assert_eq!(99, sev.sigev_value.sival_ptr as i64);
}
// Tests AioWrite.cancel. We aren't trying to test the OS's implementation,
// only our bindings. So it's sufficient to check that cancel
// returned any AioCancelStat value.
#[test]
#[cfg_attr(target_env = "musl", ignore)]
fn cancel() {
let wbuf: &[u8] = b"CDEF";
let f = tempfile().unwrap();
let mut aiow = Box::pin(AioWrite::new(
f.as_fd(),
0,
wbuf,
0,
SigevNotify::SigevNone,
));
aiow.as_mut().submit().unwrap();
let err = aiow.as_mut().error();
assert!(err == Ok(()) || err == Err(Errno::EINPROGRESS));
aiow.as_mut().cancel().unwrap();
// Wait for aiow to complete, but don't care whether it succeeded
let _ = poll_aio!(&mut aiow);
let _ = aiow.as_mut().aio_return();
}
// Test a simple aio operation with no completion notification. We must
// poll for completion.
#[test]
#[cfg_attr(all(target_env = "musl", target_arch = "x86_64"), ignore)]
fn ok() {
const INITIAL: &[u8] = b"abcdef123456";
let wbuf = "CDEF".to_string().into_bytes();
let mut rbuf = Vec::new();
const EXPECT: &[u8] = b"abCDEF123456";
let mut f = tempfile().unwrap();
f.write_all(INITIAL).unwrap();
{
let mut aiow = Box::pin(AioWrite::new(
f.as_fd(),
2,
&wbuf,
0,
SigevNotify::SigevNone,
));
aiow.as_mut().submit().unwrap();
let err = poll_aio!(&mut aiow);
assert_eq!(err, Ok(()));
assert_eq!(aiow.as_mut().aio_return().unwrap(), wbuf.len());
}
f.rewind().unwrap();
let len = f.read_to_end(&mut rbuf).unwrap();
assert_eq!(len, EXPECT.len());
assert_eq!(rbuf, EXPECT);
}
// Like ok, but allocates the structure on the stack.
#[test]
#[cfg_attr(all(target_env = "musl", target_arch = "x86_64"), ignore)]
fn on_stack() {
const INITIAL: &[u8] = b"abcdef123456";
let wbuf = "CDEF".to_string().into_bytes();
let mut rbuf = Vec::new();
const EXPECT: &[u8] = b"abCDEF123456";
let mut f = tempfile().unwrap();
f.write_all(INITIAL).unwrap();
{
let mut aiow = AioWrite::new(
f.as_fd(),
2, //offset
&wbuf,
0, //priority
SigevNotify::SigevNone,
);
let mut aiow = unsafe { Pin::new_unchecked(&mut aiow) };
aiow.as_mut().submit().unwrap();
let err = poll_aio!(&mut aiow);
assert_eq!(err, Ok(()));
assert_eq!(aiow.as_mut().aio_return().unwrap(), wbuf.len());
}
f.rewind().unwrap();
let len = f.read_to_end(&mut rbuf).unwrap();
assert_eq!(len, EXPECT.len());
assert_eq!(rbuf, EXPECT);
}
/// `AioWrite::write` should not modify the `AioCb` object if
/// `libc::aio_write` returns an error.
// Skip on Linux, because Linux's AIO implementation can't detect errors
// synchronously
#[test]
#[cfg_attr(any(target_os = "android", target_os = "linux"), ignore)]
fn error() {
// Not I/O safe! Deliberately create an invalid fd.
let fd = unsafe { BorrowedFd::borrow_raw(666) };
let wbuf = "CDEF".to_string().into_bytes();
let mut aiow = Box::pin(AioWrite::new(
fd,
0, //offset
&wbuf,
0, //priority
SigevNotify::SigevNone,
));
aiow.as_mut().submit().expect_err("assertion failed");
// Dropping the AioWrite at this point should not panic
}
}
#[cfg(target_os = "freebsd")]
#[cfg(fbsd14)]
mod aio_writev {
use std::io::IoSlice;
use super::*;
#[test]
fn test_accessors() {
let f = tempfile().unwrap();
let wbuf0 = vec![0; 4];
let wbuf1 = vec![0; 8];
let wbufs = [IoSlice::new(&wbuf0), IoSlice::new(&wbuf1)];
let aiocb = AioWritev::new(
f.as_fd(),
2, //offset
&wbufs,
42, //priority
SigevNotify::SigevSignal {
signal: Signal::SIGUSR2,
si_value: 99,
},
);
assert_eq!(f.as_raw_fd(), aiocb.fd().as_raw_fd());
assert_eq!(2, aiocb.iovlen());
assert_eq!(2, aiocb.offset());
assert_eq!(42, aiocb.priority());
let sev = aiocb.sigevent().sigevent();
assert_eq!(Signal::SIGUSR2 as i32, sev.sigev_signo);
assert_eq!(99, sev.sigev_value.sival_ptr as i64);
}
// Test a simple aio operation with no completion notification. We must
// poll for completion.
#[test]
#[cfg_attr(all(target_env = "musl", target_arch = "x86_64"), ignore)]
fn ok() {
const INITIAL: &[u8] = b"abcdef123456";
let wbuf0 = b"BC";
let wbuf1 = b"DEF";
let wbufs = [IoSlice::new(wbuf0), IoSlice::new(wbuf1)];
let wlen = wbuf0.len() + wbuf1.len();
let mut rbuf = Vec::new();
const EXPECT: &[u8] = b"aBCDEF123456";
let mut f = tempfile().unwrap();
f.write_all(INITIAL).unwrap();
{
let mut aiow = Box::pin(AioWritev::new(
f.as_fd(),
1,
&wbufs,
0,
SigevNotify::SigevNone,
));
aiow.as_mut().submit().unwrap();
let err = poll_aio!(&mut aiow);
assert_eq!(err, Ok(()));
assert_eq!(aiow.as_mut().aio_return().unwrap(), wlen);
}
f.rewind().unwrap();
let len = f.read_to_end(&mut rbuf).unwrap();
assert_eq!(len, EXPECT.len());
assert_eq!(rbuf, EXPECT);
}
}
// Test an aio operation with completion delivered by a signal
#[test]
#[cfg_attr(
any(
all(target_env = "musl", target_arch = "x86_64"),
target_arch = "mips",
target_arch = "mips32r6",
target_arch = "mips64",
target_arch = "mips64r6"
),
ignore
)]
fn sigev_signal() {
let _m = crate::SIGNAL_MTX.lock();
let sa = SigAction::new(
SigHandler::Handler(sigfunc),
SaFlags::SA_RESETHAND,
SigSet::empty(),
);
SIGNALED.store(false, Ordering::Relaxed);
unsafe { sigaction(Signal::SIGUSR2, &sa) }.unwrap();
const INITIAL: &[u8] = b"abcdef123456";
const WBUF: &[u8] = b"CDEF";
let mut rbuf = Vec::new();
const EXPECT: &[u8] = b"abCDEF123456";
let mut f = tempfile().unwrap();
f.write_all(INITIAL).unwrap();
{
let mut aiow = Box::pin(AioWrite::new(
f.as_fd(),
2, //offset
WBUF,
0, //priority
SigevNotify::SigevSignal {
signal: Signal::SIGUSR2,
si_value: 0, //TODO: validate in sigfunc
},
));
aiow.as_mut().submit().unwrap();
while !SIGNALED.load(Ordering::Relaxed) {
thread::sleep(time::Duration::from_millis(10));
}
assert_eq!(aiow.as_mut().aio_return().unwrap(), WBUF.len());
}
f.rewind().unwrap();
let len = f.read_to_end(&mut rbuf).unwrap();
assert_eq!(len, EXPECT.len());
assert_eq!(rbuf, EXPECT);
}
// Tests using aio_cancel_all for all outstanding IOs.
#[test]
#[cfg_attr(target_env = "musl", ignore)]
fn test_aio_cancel_all() {
let wbuf: &[u8] = b"CDEF";
let f = tempfile().unwrap();
let mut aiocb = Box::pin(AioWrite::new(
f.as_fd(),
0, //offset
wbuf,
0, //priority
SigevNotify::SigevNone,
));
aiocb.as_mut().submit().unwrap();
let err = aiocb.as_mut().error();
assert!(err == Ok(()) || err == Err(Errno::EINPROGRESS));
aio_cancel_all(f.as_fd()).unwrap();
// Wait for aiocb to complete, but don't care whether it succeeded
let _ = poll_aio!(&mut aiocb);
let _ = aiocb.as_mut().aio_return();
}
#[test]
fn test_aio_suspend() {
const INITIAL: &[u8] = b"abcdef123456";
const WBUF: &[u8] = b"CDEFG";
let timeout = TimeSpec::seconds(10);
let mut rbuf = vec![0; 4];
let rlen = rbuf.len();
let mut f = tempfile().unwrap();
f.write_all(INITIAL).unwrap();
let mut wcb = Box::pin(AioWrite::new(
f.as_fd(),
2, //offset
WBUF,
0, //priority
SigevNotify::SigevNone,
));
let mut rcb = Box::pin(AioRead::new(
f.as_fd(),
8, //offset
&mut rbuf,
0, //priority
SigevNotify::SigevNone,
));
wcb.as_mut().submit().unwrap();
rcb.as_mut().submit().unwrap();
loop {
{
let cbbuf = [
&*wcb as &dyn AsRef<libc::aiocb>,
&*rcb as &dyn AsRef<libc::aiocb>,
];
let r = aio_suspend(&cbbuf[..], Some(timeout));
match r {
Err(Errno::EINTR) => continue,
Err(e) => panic!("aio_suspend returned {e:?}"),
Ok(_) => (),
};
}
if rcb.as_mut().error() != Err(Errno::EINPROGRESS)
&& wcb.as_mut().error() != Err(Errno::EINPROGRESS)
{
break;
}
}
assert_eq!(wcb.as_mut().aio_return().unwrap(), WBUF.len());
assert_eq!(rcb.as_mut().aio_return().unwrap(), rlen);
}
/// aio_suspend relies on casting Rust Aio* struct pointers to libc::aiocb
/// pointers. This test ensures that such casts are valid.
#[test]
fn casting() {
let sev = SigevNotify::SigevNone;
// Only safe because we'll never await the futures
let fd = unsafe { BorrowedFd::borrow_raw(666) };
let aiof = AioFsync::new(fd, AioFsyncMode::O_SYNC, 0, sev);
assert_eq!(
aiof.as_ref() as *const libc::aiocb,
&aiof as *const AioFsync as *const libc::aiocb
);
let mut rbuf = [];
let aior = AioRead::new(fd, 0, &mut rbuf, 0, sev);
assert_eq!(
aior.as_ref() as *const libc::aiocb,
&aior as *const AioRead as *const libc::aiocb
);
let wbuf = [];
let aiow = AioWrite::new(fd, 0, &wbuf, 0, sev);
assert_eq!(
aiow.as_ref() as *const libc::aiocb,
&aiow as *const AioWrite as *const libc::aiocb
);
}
#[cfg(target_os = "freebsd")]
#[test]
fn casting_vectored() {
use std::io::{IoSlice, IoSliceMut};
let sev = SigevNotify::SigevNone;
let mut rbuf = [];
let mut rbufs = [IoSliceMut::new(&mut rbuf)];
// Only safe because we'll never await the futures
let fd = unsafe { BorrowedFd::borrow_raw(666) };
let aiorv = AioReadv::new(fd, 0, &mut rbufs[..], 0, sev);
assert_eq!(
aiorv.as_ref() as *const libc::aiocb,
&aiorv as *const AioReadv as *const libc::aiocb
);
let wbuf = [];
let wbufs = [IoSlice::new(&wbuf)];
let aiowv = AioWritev::new(fd, 0, &wbufs, 0, sev);
assert_eq!(
aiowv.as_ref() as *const libc::aiocb,
&aiowv as *const AioWritev as *const libc::aiocb
);
}

35
vendor/nix/test/sys/test_aio_drop.rs vendored Normal file
View File

@@ -0,0 +1,35 @@
// Test dropping an AioCb that hasn't yet finished.
// This must happen in its own process, because on OSX this test seems to hose
// the AIO subsystem and causes subsequent tests to fail
#[test]
#[should_panic(expected = "Dropped an in-progress AioCb")]
#[cfg(all(
not(target_env = "musl"),
not(target_env = "uclibc"),
not(target_env = "ohos"),
any(
target_os = "linux",
apple_targets,
target_os = "freebsd",
target_os = "netbsd"
)
))]
fn test_drop() {
use nix::sys::aio::*;
use nix::sys::signal::*;
use std::os::unix::io::AsFd;
use tempfile::tempfile;
const WBUF: &[u8] = b"CDEF";
let f = tempfile().unwrap();
f.set_len(6).unwrap();
let mut aiocb = Box::pin(AioWrite::new(
f.as_fd(),
2, //offset
WBUF,
0, //priority
SigevNotify::SigevNone,
));
aiocb.as_mut().submit().unwrap();
}

26
vendor/nix/test/sys/test_epoll.rs vendored Normal file
View File

@@ -0,0 +1,26 @@
#![allow(deprecated)]
use nix::errno::Errno;
use nix::sys::epoll::{epoll_create1, epoll_ctl};
use nix::sys::epoll::{EpollCreateFlags, EpollEvent, EpollFlags, EpollOp};
#[test]
pub fn test_epoll_errno() {
let efd = epoll_create1(EpollCreateFlags::empty()).unwrap();
let result = epoll_ctl(efd, EpollOp::EpollCtlDel, 1, None);
result.expect_err("assertion failed");
assert_eq!(result.unwrap_err(), Errno::ENOENT);
let result = epoll_ctl(efd, EpollOp::EpollCtlAdd, 1, None);
result.expect_err("assertion failed");
assert_eq!(result.unwrap_err(), Errno::EINVAL);
}
#[test]
pub fn test_epoll_ctl() {
let efd = epoll_create1(EpollCreateFlags::empty()).unwrap();
let mut event =
EpollEvent::new(EpollFlags::EPOLLIN | EpollFlags::EPOLLERR, 1);
epoll_ctl(efd, EpollOp::EpollCtlAdd, 1, &mut event).unwrap();
epoll_ctl(efd, EpollOp::EpollCtlDel, 1, None).unwrap();
}

41
vendor/nix/test/sys/test_event.rs vendored Normal file
View File

@@ -0,0 +1,41 @@
use libc::intptr_t;
use nix::sys::event::{EvFlags, EventFilter, FilterFlag, KEvent};
#[test]
fn test_struct_kevent() {
use std::mem;
let udata: intptr_t = 12345;
let data: intptr_t = 0x1337;
let actual = KEvent::new(
0xdead_beef,
EventFilter::EVFILT_READ,
EvFlags::EV_ONESHOT | EvFlags::EV_ADD,
FilterFlag::NOTE_CHILD | FilterFlag::NOTE_EXIT,
data,
udata,
);
assert_eq!(0xdead_beef, actual.ident());
assert_eq!(EventFilter::EVFILT_READ, actual.filter().unwrap());
assert_eq!(libc::EV_ONESHOT | libc::EV_ADD, actual.flags().bits());
assert_eq!(libc::NOTE_CHILD | libc::NOTE_EXIT, actual.fflags().bits());
assert_eq!(data, actual.data());
assert_eq!(udata, actual.udata());
assert_eq!(mem::size_of::<libc::kevent>(), mem::size_of::<KEvent>());
}
#[test]
fn test_kevent_filter() {
let udata: intptr_t = 12345;
let actual = KEvent::new(
0xdead_beef,
EventFilter::EVFILT_READ,
EvFlags::EV_ONESHOT | EvFlags::EV_ADD,
FilterFlag::NOTE_CHILD | FilterFlag::NOTE_EXIT,
0x1337,
udata,
);
assert_eq!(EventFilter::EVFILT_READ, actual.filter().unwrap());
}

220
vendor/nix/test/sys/test_fanotify.rs vendored Normal file
View File

@@ -0,0 +1,220 @@
use crate::*;
use nix::errno::Errno;
use nix::fcntl::AT_FDCWD;
use nix::sys::fanotify::{
EventFFlags, Fanotify, FanotifyResponse, InitFlags, MarkFlags, MaskFlags,
Response,
};
use std::fs::{read_link, read_to_string, File, OpenOptions};
use std::io::ErrorKind;
use std::io::{Read, Write};
use std::os::fd::AsRawFd;
use std::thread;
#[test]
/// Run fanotify tests sequentially to avoid tmp files races
pub fn test_fanotify() {
require_capability!("test_fanotify", CAP_SYS_ADMIN);
test_fanotify_notifications();
test_fanotify_responses();
test_fanotify_overflow();
}
fn test_fanotify_notifications() {
let group =
Fanotify::init(InitFlags::FAN_CLASS_NOTIF, EventFFlags::O_RDONLY)
.unwrap();
let tempdir = tempfile::tempdir().unwrap();
let tempfile = tempdir.path().join("test");
OpenOptions::new()
.write(true)
.create_new(true)
.open(&tempfile)
.unwrap();
group
.mark(
MarkFlags::FAN_MARK_ADD,
MaskFlags::FAN_OPEN | MaskFlags::FAN_MODIFY | MaskFlags::FAN_CLOSE,
AT_FDCWD,
Some(&tempfile),
)
.unwrap();
// modify test file
{
let mut f = OpenOptions::new().write(true).open(&tempfile).unwrap();
f.write_all(b"hello").unwrap();
}
let mut events = group.read_events().unwrap();
assert_eq!(events.len(), 1, "should have read exactly one event");
let event = events.pop().unwrap();
assert!(event.check_version());
assert_eq!(
event.mask(),
MaskFlags::FAN_OPEN
| MaskFlags::FAN_MODIFY
| MaskFlags::FAN_CLOSE_WRITE
);
let fd_opt = event.fd();
let fd = fd_opt.as_ref().unwrap();
let path = read_link(format!("/proc/self/fd/{}", fd.as_raw_fd())).unwrap();
assert_eq!(path, tempfile);
// read test file
{
let mut f = File::open(&tempfile).unwrap();
let mut s = String::new();
f.read_to_string(&mut s).unwrap();
}
let mut events = group.read_events().unwrap();
assert_eq!(events.len(), 1, "should have read exactly one event");
let event = events.pop().unwrap();
assert!(event.check_version());
assert_eq!(
event.mask(),
MaskFlags::FAN_OPEN | MaskFlags::FAN_CLOSE_NOWRITE
);
let fd_opt = event.fd();
let fd = fd_opt.as_ref().unwrap();
let path = read_link(format!("/proc/self/fd/{}", fd.as_raw_fd())).unwrap();
assert_eq!(path, tempfile);
}
fn test_fanotify_responses() {
let group =
Fanotify::init(InitFlags::FAN_CLASS_CONTENT, EventFFlags::O_RDONLY)
.unwrap();
let tempdir = tempfile::tempdir().unwrap();
let tempfile = tempdir.path().join("test");
OpenOptions::new()
.write(true)
.create_new(true)
.open(&tempfile)
.unwrap();
group
.mark(
MarkFlags::FAN_MARK_ADD,
MaskFlags::FAN_OPEN_PERM,
AT_FDCWD,
Some(&tempfile),
)
.unwrap();
let file_thread = thread::spawn({
let tempfile = tempfile.clone();
move || {
// first open, should fail
let Err(e) = File::open(&tempfile) else {
panic!("The first open should fail");
};
assert_eq!(e.kind(), ErrorKind::PermissionDenied);
// second open, should succeed
File::open(&tempfile).unwrap();
}
});
// Deny the first open try
let mut events = group.read_events().unwrap();
assert_eq!(events.len(), 1, "should have read exactly one event");
let event = events.pop().unwrap();
assert!(event.check_version());
assert_eq!(event.mask(), MaskFlags::FAN_OPEN_PERM);
let fd_opt = event.fd();
let fd = fd_opt.as_ref().unwrap();
let path = read_link(format!("/proc/self/fd/{}", fd.as_raw_fd())).unwrap();
assert_eq!(path, tempfile);
group
.write_response(FanotifyResponse::new(*fd, Response::FAN_DENY))
.unwrap();
// Allow the second open try
let mut events = group.read_events().unwrap();
assert_eq!(events.len(), 1, "should have read exactly one event");
let event = events.pop().unwrap();
assert!(event.check_version());
assert_eq!(event.mask(), MaskFlags::FAN_OPEN_PERM);
let fd_opt = event.fd();
let fd = fd_opt.as_ref().unwrap();
let path = read_link(format!("/proc/self/fd/{}", fd.as_raw_fd())).unwrap();
assert_eq!(path, tempfile);
group
.write_response(FanotifyResponse::new(*fd, Response::FAN_ALLOW))
.unwrap();
file_thread.join().unwrap();
}
fn test_fanotify_overflow() {
let max_events: usize =
read_to_string("/proc/sys/fs/fanotify/max_queued_events")
.unwrap()
.trim()
.parse()
.unwrap();
// make sure the kernel is configured with the default value,
// just so this test doesn't run forever
assert_eq!(max_events, 16384);
let group = Fanotify::init(
InitFlags::FAN_CLASS_NOTIF
| InitFlags::FAN_REPORT_TID
| InitFlags::FAN_NONBLOCK,
EventFFlags::O_RDONLY,
)
.unwrap();
let tempdir = tempfile::tempdir().unwrap();
let tempfile = tempdir.path().join("test");
OpenOptions::new()
.write(true)
.create_new(true)
.open(&tempfile)
.unwrap();
group
.mark(
MarkFlags::FAN_MARK_ADD,
MaskFlags::FAN_OPEN,
AT_FDCWD,
Some(&tempfile),
)
.unwrap();
thread::scope(|s| {
// perform 10 more events to demonstrate some will be dropped
for _ in 0..(max_events + 10) {
s.spawn(|| {
File::open(&tempfile).unwrap();
});
}
});
// flush the queue until it's empty
let mut n = 0;
let mut last_event = None;
loop {
match group.read_events() {
Ok(events) => {
n += events.len();
if let Some(event) = events.last() {
last_event = Some(event.mask());
}
}
Err(e) if e == Errno::EWOULDBLOCK => break,
Err(e) => panic!("{e:?}"),
}
}
// make sure we read all we expected.
// the +1 is for the overflow event.
assert_eq!(n, max_events + 1);
assert_eq!(last_event, Some(MaskFlags::FAN_Q_OVERFLOW));
}

65
vendor/nix/test/sys/test_inotify.rs vendored Normal file
View File

@@ -0,0 +1,65 @@
use nix::errno::Errno;
use nix::sys::inotify::{AddWatchFlags, InitFlags, Inotify};
use std::ffi::OsString;
use std::fs::{rename, File};
#[test]
pub fn test_inotify() {
let instance = Inotify::init(InitFlags::IN_NONBLOCK).unwrap();
let tempdir = tempfile::tempdir().unwrap();
instance
.add_watch(tempdir.path(), AddWatchFlags::IN_ALL_EVENTS)
.unwrap();
let events = instance.read_events();
assert_eq!(events.unwrap_err(), Errno::EAGAIN);
File::create(tempdir.path().join("test")).unwrap();
let events = instance.read_events().unwrap();
assert_eq!(events[0].name, Some(OsString::from("test")));
}
#[test]
pub fn test_inotify_multi_events() {
let instance = Inotify::init(InitFlags::IN_NONBLOCK).unwrap();
let tempdir = tempfile::tempdir().unwrap();
instance
.add_watch(tempdir.path(), AddWatchFlags::IN_ALL_EVENTS)
.unwrap();
let events = instance.read_events();
assert_eq!(events.unwrap_err(), Errno::EAGAIN);
File::create(tempdir.path().join("test")).unwrap();
rename(tempdir.path().join("test"), tempdir.path().join("test2")).unwrap();
// Now there should be 5 events in queue:
// - IN_CREATE on test
// - IN_OPEN on test
// - IN_CLOSE_WRITE on test
// - IN_MOVED_FROM on test with a cookie
// - IN_MOVED_TO on test2 with the same cookie
let events = instance.read_events().unwrap();
assert_eq!(events.len(), 5);
assert_eq!(events[0].mask, AddWatchFlags::IN_CREATE);
assert_eq!(events[0].name, Some(OsString::from("test")));
assert_eq!(events[1].mask, AddWatchFlags::IN_OPEN);
assert_eq!(events[1].name, Some(OsString::from("test")));
assert_eq!(events[2].mask, AddWatchFlags::IN_CLOSE_WRITE);
assert_eq!(events[2].name, Some(OsString::from("test")));
assert_eq!(events[3].mask, AddWatchFlags::IN_MOVED_FROM);
assert_eq!(events[3].name, Some(OsString::from("test")));
assert_eq!(events[4].mask, AddWatchFlags::IN_MOVED_TO);
assert_eq!(events[4].name, Some(OsString::from("test2")));
assert_eq!(events[3].cookie, events[4].cookie);
}

383
vendor/nix/test/sys/test_ioctl.rs vendored Normal file
View File

@@ -0,0 +1,383 @@
#![allow(dead_code)]
// Simple tests to ensure macro generated fns compile
ioctl_none_bad!(do_bad, 0x1234);
ioctl_read_bad!(do_bad_read, 0x1234, u16);
ioctl_write_int_bad!(do_bad_write_int, 0x1234);
ioctl_write_ptr_bad!(do_bad_write_ptr, 0x1234, u8);
ioctl_readwrite_bad!(do_bad_readwrite, 0x1234, u32);
ioctl_none!(do_none, 0, 0);
ioctl_read!(read_test, 0, 0, u32);
ioctl_write_int!(write_ptr_int, 0, 0);
ioctl_write_ptr!(write_ptr_u8, 0, 0, u8);
ioctl_write_ptr!(write_ptr_u32, 0, 0, u32);
ioctl_write_ptr!(write_ptr_u64, 0, 0, u64);
ioctl_readwrite!(readwrite_test, 0, 0, u64);
ioctl_read_buf!(readbuf_test, 0, 0, u32);
const SPI_IOC_MAGIC: u8 = b'k';
const SPI_IOC_MESSAGE: u8 = 0;
ioctl_write_buf!(writebuf_test_consts, SPI_IOC_MAGIC, SPI_IOC_MESSAGE, u8);
ioctl_write_buf!(writebuf_test_u8, 0, 0, u8);
ioctl_write_buf!(writebuf_test_u32, 0, 0, u32);
ioctl_write_buf!(writebuf_test_u64, 0, 0, u64);
ioctl_readwrite_buf!(readwritebuf_test, 0, 0, u32);
// See C code for source of values for op calculations (does NOT work for mips/powerpc):
// https://gist.github.com/posborne/83ea6880770a1aef332e
//
// TODO: Need a way to compute these constants at test time. Using precomputed
// values is fragile and needs to be maintained.
#[cfg(linux_android)]
mod linux {
// The cast is not unnecessary on all platforms.
#[allow(clippy::unnecessary_cast)]
#[test]
fn test_op_none() {
if cfg!(any(
target_arch = "mips",
target_arch = "mips32r6",
target_arch = "mips64",
target_arch = "mips64r6",
target_arch = "powerpc",
target_arch = "powerpc64"
)) {
assert_eq!(request_code_none!(b'q', 10) as u32, 0x2000_710A);
assert_eq!(request_code_none!(b'a', 255) as u32, 0x2000_61FF);
} else {
assert_eq!(request_code_none!(b'q', 10) as u32, 0x0000_710A);
assert_eq!(request_code_none!(b'a', 255) as u32, 0x0000_61FF);
}
}
// The cast is not unnecessary on all platforms.
#[allow(clippy::unnecessary_cast)]
#[test]
fn test_op_write() {
if cfg!(any(
target_arch = "mips",
target_arch = "mips32r6",
target_arch = "mips64",
target_arch = "mips64r6",
target_arch = "powerpc",
target_arch = "powerpc64"
)) {
assert_eq!(request_code_write!(b'z', 10, 1) as u32, 0x8001_7A0A);
assert_eq!(request_code_write!(b'z', 10, 512) as u32, 0x8200_7A0A);
} else {
assert_eq!(request_code_write!(b'z', 10, 1) as u32, 0x4001_7A0A);
assert_eq!(request_code_write!(b'z', 10, 512) as u32, 0x4200_7A0A);
}
}
#[cfg(target_pointer_width = "64")]
#[test]
fn test_op_write_64() {
if cfg!(any(
target_arch = "mips64",
target_arch = "mips64r6",
target_arch = "powerpc64"
)) {
assert_eq!(
request_code_write!(b'z', 10, 1u64 << 32) as u32,
0x8000_7A0A
);
} else {
assert_eq!(
request_code_write!(b'z', 10, 1u64 << 32) as u32,
0x4000_7A0A
);
}
}
// The cast is not unnecessary on all platforms.
#[allow(clippy::unnecessary_cast)]
#[test]
fn test_op_read() {
if cfg!(any(
target_arch = "mips",
target_arch = "mips32r6",
target_arch = "mips64",
target_arch = "mips64r6",
target_arch = "powerpc",
target_arch = "powerpc64"
)) {
assert_eq!(request_code_read!(b'z', 10, 1) as u32, 0x4001_7A0A);
assert_eq!(request_code_read!(b'z', 10, 512) as u32, 0x4200_7A0A);
} else {
assert_eq!(request_code_read!(b'z', 10, 1) as u32, 0x8001_7A0A);
assert_eq!(request_code_read!(b'z', 10, 512) as u32, 0x8200_7A0A);
}
}
#[cfg(target_pointer_width = "64")]
#[test]
fn test_op_read_64() {
if cfg!(any(
target_arch = "mips64",
target_arch = "mips64r6",
target_arch = "powerpc64"
)) {
assert_eq!(
request_code_read!(b'z', 10, 1u64 << 32) as u32,
0x4000_7A0A
);
} else {
assert_eq!(
request_code_read!(b'z', 10, 1u64 << 32) as u32,
0x8000_7A0A
);
}
}
// The cast is not unnecessary on all platforms.
#[allow(clippy::unnecessary_cast)]
#[test]
fn test_op_read_write() {
assert_eq!(request_code_readwrite!(b'z', 10, 1) as u32, 0xC001_7A0A);
assert_eq!(request_code_readwrite!(b'z', 10, 512) as u32, 0xC200_7A0A);
}
#[cfg(target_pointer_width = "64")]
#[test]
fn test_op_read_write_64() {
assert_eq!(
request_code_readwrite!(b'z', 10, 1u64 << 32) as u32,
0xC000_7A0A
);
}
}
#[cfg(bsd)]
mod bsd {
#[test]
fn test_op_none() {
assert_eq!(request_code_none!(b'q', 10), 0x2000_710A);
assert_eq!(request_code_none!(b'a', 255), 0x2000_61FF);
}
#[cfg(freebsdlike)]
#[test]
fn test_op_write_int() {
assert_eq!(request_code_write_int!(b'v', 4), 0x2004_7604);
assert_eq!(request_code_write_int!(b'p', 2), 0x2004_7002);
}
#[test]
fn test_op_write() {
assert_eq!(request_code_write!(b'z', 10, 1), 0x8001_7A0A);
assert_eq!(request_code_write!(b'z', 10, 512), 0x8200_7A0A);
}
#[cfg(target_pointer_width = "64")]
#[test]
fn test_op_write_64() {
assert_eq!(request_code_write!(b'z', 10, 1u64 << 32), 0x8000_7A0A);
}
#[test]
fn test_op_read() {
assert_eq!(request_code_read!(b'z', 10, 1), 0x4001_7A0A);
assert_eq!(request_code_read!(b'z', 10, 512), 0x4200_7A0A);
}
#[cfg(target_pointer_width = "64")]
#[test]
fn test_op_read_64() {
assert_eq!(request_code_read!(b'z', 10, 1u64 << 32), 0x4000_7A0A);
}
#[test]
fn test_op_read_write() {
assert_eq!(request_code_readwrite!(b'z', 10, 1), 0xC001_7A0A);
assert_eq!(request_code_readwrite!(b'z', 10, 512), 0xC200_7A0A);
}
#[cfg(target_pointer_width = "64")]
#[test]
fn test_op_read_write_64() {
assert_eq!(request_code_readwrite!(b'z', 10, 1u64 << 32), 0xC000_7A0A);
}
}
#[cfg(linux_android)]
mod linux_ioctls {
use std::mem;
use std::os::unix::io::AsRawFd;
use libc::{termios, TCGETS, TCSBRK, TCSETS, TIOCNXCL};
use tempfile::tempfile;
use nix::errno::Errno;
ioctl_none_bad!(tiocnxcl, TIOCNXCL);
#[test]
fn test_ioctl_none_bad() {
let file = tempfile().unwrap();
let res = unsafe { tiocnxcl(file.as_raw_fd()) };
assert_eq!(res, Err(Errno::ENOTTY));
}
ioctl_read_bad!(tcgets, TCGETS, termios);
#[test]
fn test_ioctl_read_bad() {
let file = tempfile().unwrap();
let mut termios = unsafe { mem::zeroed() };
let res = unsafe { tcgets(file.as_raw_fd(), &mut termios) };
assert_eq!(res, Err(Errno::ENOTTY));
}
ioctl_write_int_bad!(tcsbrk, TCSBRK);
#[test]
fn test_ioctl_write_int_bad() {
let file = tempfile().unwrap();
let res = unsafe { tcsbrk(file.as_raw_fd(), 0) };
assert_eq!(res, Err(Errno::ENOTTY));
}
ioctl_write_ptr_bad!(tcsets, TCSETS, termios);
#[test]
fn test_ioctl_write_ptr_bad() {
let file = tempfile().unwrap();
let termios: termios = unsafe { mem::zeroed() };
let res = unsafe { tcsets(file.as_raw_fd(), &termios) };
assert_eq!(res, Err(Errno::ENOTTY));
}
// FIXME: Find a suitable example for `ioctl_readwrite_bad`
// From linux/videodev2.h
ioctl_none!(log_status, b'V', 70);
#[test]
fn test_ioctl_none() {
let file = tempfile().unwrap();
let res = unsafe { log_status(file.as_raw_fd()) };
assert!(res == Err(Errno::ENOTTY) || res == Err(Errno::ENOSYS));
}
#[repr(C)]
pub struct v4l2_audio {
index: u32,
name: [u8; 32],
capability: u32,
mode: u32,
reserved: [u32; 2],
}
// From linux/videodev2.h
ioctl_write_ptr!(s_audio, b'V', 34, v4l2_audio);
#[test]
fn test_ioctl_write_ptr() {
let file = tempfile().unwrap();
let data: v4l2_audio = unsafe { mem::zeroed() };
let res = unsafe { s_audio(file.as_raw_fd(), &data) };
assert!(res == Err(Errno::ENOTTY) || res == Err(Errno::ENOSYS));
}
// From linux/net/bluetooth/hci_sock.h
const HCI_IOC_MAGIC: u8 = b'H';
const HCI_IOC_HCIDEVUP: u8 = 201;
ioctl_write_int!(hcidevup, HCI_IOC_MAGIC, HCI_IOC_HCIDEVUP);
#[test]
fn test_ioctl_write_int() {
let file = tempfile().unwrap();
let res = unsafe { hcidevup(file.as_raw_fd(), 0) };
assert!(res == Err(Errno::ENOTTY) || res == Err(Errno::ENOSYS));
}
// From linux/videodev2.h
ioctl_read!(g_audio, b'V', 33, v4l2_audio);
#[test]
fn test_ioctl_read() {
let file = tempfile().unwrap();
let mut data: v4l2_audio = unsafe { mem::zeroed() };
let res = unsafe { g_audio(file.as_raw_fd(), &mut data) };
assert!(res == Err(Errno::ENOTTY) || res == Err(Errno::ENOSYS));
}
// From linux/videodev2.h
ioctl_readwrite!(enum_audio, b'V', 65, v4l2_audio);
#[test]
fn test_ioctl_readwrite() {
let file = tempfile().unwrap();
let mut data: v4l2_audio = unsafe { mem::zeroed() };
let res = unsafe { enum_audio(file.as_raw_fd(), &mut data) };
assert!(res == Err(Errno::ENOTTY) || res == Err(Errno::ENOSYS));
}
// FIXME: Find a suitable example for `ioctl_read_buf`.
#[repr(C)]
pub struct spi_ioc_transfer {
tx_buf: u64,
rx_buf: u64,
len: u32,
speed_hz: u32,
delay_usecs: u16,
bits_per_word: u8,
cs_change: u8,
tx_nbits: u8,
rx_nbits: u8,
pad: u16,
}
// From linux/spi/spidev.h
ioctl_write_buf!(
spi_ioc_message,
super::SPI_IOC_MAGIC,
super::SPI_IOC_MESSAGE,
spi_ioc_transfer
);
#[test]
fn test_ioctl_write_buf() {
let file = tempfile().unwrap();
let data: [spi_ioc_transfer; 4] = unsafe { mem::zeroed() };
let res = unsafe { spi_ioc_message(file.as_raw_fd(), &data[..]) };
assert!(res == Err(Errno::ENOTTY) || res == Err(Errno::ENOSYS));
}
// FIXME: Find a suitable example for `ioctl_readwrite_buf`.
}
#[cfg(target_os = "freebsd")]
mod freebsd_ioctls {
use std::mem;
use std::os::unix::io::AsRawFd;
use libc::termios;
use tempfile::tempfile;
use nix::errno::Errno;
// From sys/sys/ttycom.h
const TTY_IOC_MAGIC: u8 = b't';
const TTY_IOC_TYPE_NXCL: u8 = 14;
const TTY_IOC_TYPE_GETA: u8 = 19;
const TTY_IOC_TYPE_SETA: u8 = 20;
ioctl_none!(tiocnxcl, TTY_IOC_MAGIC, TTY_IOC_TYPE_NXCL);
#[test]
fn test_ioctl_none() {
let file = tempfile().unwrap();
let res = unsafe { tiocnxcl(file.as_raw_fd()) };
assert_eq!(res, Err(Errno::ENOTTY));
}
ioctl_read!(tiocgeta, TTY_IOC_MAGIC, TTY_IOC_TYPE_GETA, termios);
#[test]
fn test_ioctl_read() {
let file = tempfile().unwrap();
let mut termios = unsafe { mem::zeroed() };
let res = unsafe { tiocgeta(file.as_raw_fd(), &mut termios) };
assert_eq!(res, Err(Errno::ENOTTY));
}
ioctl_write_ptr!(tiocseta, TTY_IOC_MAGIC, TTY_IOC_TYPE_SETA, termios);
#[test]
fn test_ioctl_write_ptr() {
let file = tempfile().unwrap();
let termios: termios = unsafe { mem::zeroed() };
let res = unsafe { tiocseta(file.as_raw_fd(), &termios) };
assert_eq!(res, Err(Errno::ENOTTY));
}
}

20
vendor/nix/test/sys/test_memfd.rs vendored Normal file
View File

@@ -0,0 +1,20 @@
#[test]
fn test_memfd_create() {
use nix::sys::memfd::memfd_create;
use nix::sys::memfd::MFdFlags;
use nix::unistd::lseek;
use nix::unistd::read;
use nix::unistd::{write, Whence};
let fd =
memfd_create("test_memfd_create_name", MFdFlags::MFD_CLOEXEC).unwrap();
let contents = b"hello";
assert_eq!(write(&fd, contents).unwrap(), 5);
lseek(&fd, 0, Whence::SeekSet).unwrap();
let mut buf = vec![0_u8; contents.len()];
assert_eq!(read(&fd, &mut buf).unwrap(), 5);
assert_eq!(contents, buf.as_slice());
}

200
vendor/nix/test/sys/test_mman.rs vendored Normal file
View File

@@ -0,0 +1,200 @@
#![allow(clippy::redundant_slicing)]
use nix::sys::mman::{mmap_anonymous, MapFlags, ProtFlags};
use std::num::NonZeroUsize;
#[test]
fn test_mmap_anonymous() {
unsafe {
let mut ptr = mmap_anonymous(
None,
NonZeroUsize::new(1).unwrap(),
ProtFlags::PROT_READ | ProtFlags::PROT_WRITE,
MapFlags::MAP_PRIVATE,
)
.unwrap()
.cast::<u8>();
assert_eq!(*ptr.as_ref(), 0x00u8);
*ptr.as_mut() = 0xffu8;
assert_eq!(*ptr.as_ref(), 0xffu8);
}
}
#[test]
#[cfg(any(target_os = "linux", target_os = "netbsd"))]
fn test_mremap_grow() {
use nix::libc::size_t;
use nix::sys::mman::{mremap, MRemapFlags};
use std::ptr::NonNull;
const ONE_K: size_t = 1024;
let one_k_non_zero = NonZeroUsize::new(ONE_K).unwrap();
let slice: &mut [u8] = unsafe {
let mem = mmap_anonymous(
None,
one_k_non_zero,
ProtFlags::PROT_READ | ProtFlags::PROT_WRITE,
MapFlags::MAP_PRIVATE,
)
.unwrap();
std::slice::from_raw_parts_mut(mem.as_ptr().cast(), ONE_K)
};
assert_eq!(slice[ONE_K - 1], 0x00);
slice[ONE_K - 1] = 0xFF;
assert_eq!(slice[ONE_K - 1], 0xFF);
let slice: &mut [u8] = unsafe {
#[cfg(target_os = "linux")]
let mem = mremap(
NonNull::from(&mut slice[..]).cast(),
ONE_K,
10 * ONE_K,
MRemapFlags::MREMAP_MAYMOVE,
None,
)
.unwrap();
#[cfg(target_os = "netbsd")]
let mem = mremap(
NonNull::from(&mut slice[..]).cast(),
ONE_K,
10 * ONE_K,
MRemapFlags::MAP_REMAPDUP,
None,
)
.unwrap();
std::slice::from_raw_parts_mut(mem.cast().as_ptr(), 10 * ONE_K)
};
// The first KB should still have the old data in it.
assert_eq!(slice[ONE_K - 1], 0xFF);
// The additional range should be zero-init'd and accessible.
assert_eq!(slice[10 * ONE_K - 1], 0x00);
slice[10 * ONE_K - 1] = 0xFF;
assert_eq!(slice[10 * ONE_K - 1], 0xFF);
}
#[test]
#[cfg(any(target_os = "linux", target_os = "netbsd"))]
// Segfaults for unknown reasons under QEMU for 32-bit targets
#[cfg_attr(all(target_pointer_width = "32", qemu), ignore)]
fn test_mremap_shrink() {
use nix::libc::size_t;
use nix::sys::mman::{mremap, MRemapFlags};
use std::num::NonZeroUsize;
use std::ptr::NonNull;
const ONE_K: size_t = 1024;
let ten_one_k = NonZeroUsize::new(10 * ONE_K).unwrap();
let slice: &mut [u8] = unsafe {
let mem = mmap_anonymous(
None,
ten_one_k,
ProtFlags::PROT_READ | ProtFlags::PROT_WRITE,
MapFlags::MAP_PRIVATE,
)
.unwrap();
std::slice::from_raw_parts_mut(mem.as_ptr().cast(), ONE_K)
};
assert_eq!(slice[ONE_K - 1], 0x00);
slice[ONE_K - 1] = 0xFF;
assert_eq!(slice[ONE_K - 1], 0xFF);
let slice: &mut [u8] = unsafe {
let mem = mremap(
NonNull::from(&mut slice[..]).cast(),
ten_one_k.into(),
ONE_K,
MRemapFlags::empty(),
None,
)
.unwrap();
// Since we didn't supply MREMAP_MAYMOVE, the address should be the
// same.
assert_eq!(mem.as_ptr(), NonNull::from(&mut slice[..]).cast().as_ptr());
std::slice::from_raw_parts_mut(mem.as_ptr().cast(), ONE_K)
};
// The first KB should still be accessible and have the old data in it.
assert_eq!(slice[ONE_K - 1], 0xFF);
}
#[test]
#[cfg(target_os = "linux")]
fn test_mremap_dontunmap() {
use nix::libc::size_t;
use nix::sys::mman::{mremap, MRemapFlags};
use std::num::NonZeroUsize;
use std::ptr::NonNull;
const ONE_K: size_t = 1024;
let one_k_non_zero = NonZeroUsize::new(ONE_K).unwrap();
let slice: &mut [u8] = unsafe {
let mem = mmap_anonymous(
None,
one_k_non_zero,
ProtFlags::PROT_READ | ProtFlags::PROT_WRITE,
MapFlags::MAP_PRIVATE,
)
.unwrap();
std::slice::from_raw_parts_mut(mem.as_ptr().cast(), ONE_K)
};
// because we do not unmap `slice`, `old_size` and `new_size`
// need to be equal or `EINVAL` is set.
let _new_slice: &mut [u8] = unsafe {
let mem = mremap(
NonNull::from(&mut slice[..]).cast(),
ONE_K,
ONE_K,
MRemapFlags::MREMAP_MAYMOVE | MRemapFlags::MREMAP_DONTUNMAP,
None,
)
.unwrap();
std::slice::from_raw_parts_mut(mem.cast().as_ptr(), 10 * ONE_K)
};
}
#[test]
#[cfg(target_os = "linux")]
fn test_madv_wipeonfork() {
use nix::libc::size_t;
use nix::sys::mman::{madvise, MmapAdvise};
use nix::unistd::{fork, ForkResult};
use std::num::NonZeroUsize;
const ONE_K: size_t = 1024;
let ten_one_k = NonZeroUsize::new(10 * ONE_K).unwrap();
let slice: &mut [u8] = unsafe {
let mem = mmap_anonymous(
None,
ten_one_k,
ProtFlags::PROT_READ | ProtFlags::PROT_WRITE,
MapFlags::MAP_PRIVATE,
)
.unwrap();
madvise(mem, ONE_K, MmapAdvise::MADV_WIPEONFORK)
.expect("madvise failed");
std::slice::from_raw_parts_mut(mem.as_ptr().cast(), ONE_K)
};
slice[ONE_K - 1] = 0xFF;
let _m = crate::FORK_MTX.lock();
unsafe {
let res = fork().expect("fork failed");
match res {
ForkResult::Child => {
// that s the whole point of MADV_WIPEONFORK
assert_eq!(slice[ONE_K - 1], 0x00);
libc::_exit(0);
}
ForkResult::Parent { child } => {
nix::sys::signal::kill(child, nix::sys::signal::SIGTERM)
.unwrap();
let _ = nix::sys::wait::wait().unwrap();
}
}
}
}

172
vendor/nix/test/sys/test_prctl.rs vendored Normal file
View File

@@ -0,0 +1,172 @@
#[cfg(target_os = "linux")]
#[cfg(feature = "process")]
mod test_prctl {
use std::ffi::CStr;
use nix::sys::prctl;
#[cfg_attr(qemu, ignore)]
#[test]
fn test_get_set_subreaper() {
let original = prctl::get_child_subreaper().unwrap();
prctl::set_child_subreaper(true).unwrap();
let subreaper = prctl::get_child_subreaper().unwrap();
assert!(subreaper);
prctl::set_child_subreaper(original).unwrap();
}
#[test]
fn test_get_set_dumpable() {
let original = prctl::get_dumpable().unwrap();
prctl::set_dumpable(false).unwrap();
let dumpable = prctl::get_dumpable().unwrap();
assert!(!dumpable);
prctl::set_dumpable(original).unwrap();
}
#[test]
fn test_get_set_keepcaps() {
let original = prctl::get_keepcaps().unwrap();
prctl::set_keepcaps(true).unwrap();
let keepcaps = prctl::get_keepcaps().unwrap();
assert!(keepcaps);
prctl::set_keepcaps(original).unwrap();
}
#[test]
fn test_get_set_clear_mce_kill() {
use prctl::PrctlMCEKillPolicy::*;
prctl::set_mce_kill(PR_MCE_KILL_LATE).unwrap();
let mce = prctl::get_mce_kill().unwrap();
assert_eq!(mce, PR_MCE_KILL_LATE);
prctl::clear_mce_kill().unwrap();
let mce = prctl::get_mce_kill().unwrap();
assert_eq!(mce, PR_MCE_KILL_DEFAULT);
}
#[cfg_attr(qemu, ignore)]
#[test]
fn test_get_set_pdeathsig() {
use nix::sys::signal::Signal;
let original = prctl::get_pdeathsig().unwrap();
prctl::set_pdeathsig(Signal::SIGUSR1).unwrap();
let sig = prctl::get_pdeathsig().unwrap();
assert_eq!(sig, Some(Signal::SIGUSR1));
prctl::set_pdeathsig(original).unwrap();
}
#[test]
fn test_get_set_name() {
let original = prctl::get_name().unwrap();
let long_name =
CStr::from_bytes_with_nul(b"0123456789abcdefghijklmn\0").unwrap();
prctl::set_name(long_name).unwrap();
let res = prctl::get_name().unwrap();
// name truncated by kernel to TASK_COMM_LEN
assert_eq!(&long_name.to_str().unwrap()[..15], res.to_str().unwrap());
let short_name = CStr::from_bytes_with_nul(b"01234567\0").unwrap();
prctl::set_name(short_name).unwrap();
let res = prctl::get_name().unwrap();
assert_eq!(short_name.to_str().unwrap(), res.to_str().unwrap());
prctl::set_name(&original).unwrap();
}
#[cfg_attr(qemu, ignore)]
#[test]
fn test_get_set_timerslack() {
let original = prctl::get_timerslack().unwrap() as libc::c_ulong;
let slack = 60_000;
prctl::set_timerslack(slack).unwrap();
let res = prctl::get_timerslack().unwrap() as libc::c_ulong;
assert_eq!(slack, res);
prctl::set_timerslack(original).unwrap();
}
// Loongarch need to use a newer QEMU that disabled these PRCTL subcodes/methods.
// https://github.com/qemu/qemu/commit/220717a6f46a99031a5b1af964bbf4dec1310440
// So we should ignore them when testing in QEMU environments.
#[cfg_attr(all(qemu, target_arch = "loongarch64"), ignore)]
#[test]
fn test_disable_enable_perf_events() {
prctl::task_perf_events_disable().unwrap();
prctl::task_perf_events_enable().unwrap();
}
#[test]
fn test_get_set_no_new_privs() {
prctl::set_no_new_privs().unwrap();
let no_new_privs = prctl::get_no_new_privs().unwrap();
assert!(no_new_privs);
}
// Loongarch need to use a newer QEMU that disabled these PRCTL subcodes/methods
// https://github.com/qemu/qemu/commit/220717a6f46a99031a5b1af964bbf4dec1310440
// So we should ignore them when testing in QEMU environments.
#[cfg_attr(all(qemu, target_arch = "loongarch64"), ignore)]
#[test]
fn test_get_set_thp_disable() {
let original = prctl::get_thp_disable().unwrap();
prctl::set_thp_disable(true).unwrap();
let thp_disable = prctl::get_thp_disable().unwrap();
assert!(thp_disable);
prctl::set_thp_disable(original).unwrap();
}
// Ignore this test under QEMU, as it started failing after updating the Linux CI
// runner image, for reasons unknown.
//
// See: https://github.com/nix-rust/nix/issues/2418
#[test]
#[cfg_attr(qemu, ignore)]
fn test_set_vma_anon_name() {
use nix::errno::Errno;
use nix::sys::mman;
use std::num::NonZeroUsize;
const ONE_K: libc::size_t = 1024;
let sz = NonZeroUsize::new(ONE_K).unwrap();
let ptr = unsafe {
mman::mmap_anonymous(
None,
sz,
mman::ProtFlags::PROT_READ,
mman::MapFlags::MAP_SHARED,
)
.unwrap()
};
let err = prctl::set_vma_anon_name(
ptr,
sz,
Some(CStr::from_bytes_with_nul(b"[,$\0").unwrap()),
)
.unwrap_err();
assert_eq!(err, Errno::EINVAL);
// `CONFIG_ANON_VMA_NAME` kernel config might not be set
prctl::set_vma_anon_name(
ptr,
sz,
Some(CStr::from_bytes_with_nul(b"Nix\0").unwrap()),
)
.unwrap_or_default();
prctl::set_vma_anon_name(ptr, sz, None).unwrap_or_default();
}
}

32
vendor/nix/test/sys/test_pthread.rs vendored Normal file
View File

@@ -0,0 +1,32 @@
use nix::sys::pthread::*;
#[cfg(any(
target_env = "musl",
target_os = "redox",
target_env = "ohos",
target_os = "cygwin"
))]
#[test]
fn test_pthread_self() {
let tid = pthread_self();
assert!(!tid.is_null());
}
#[cfg(not(any(
target_env = "musl",
target_os = "redox",
target_env = "ohos",
target_os = "cygwin"
)))]
#[test]
fn test_pthread_self() {
let tid = pthread_self();
assert_ne!(tid, 0);
}
#[test]
#[cfg(not(target_os = "redox"))]
fn test_pthread_kill_none() {
pthread_kill(pthread_self(), None)
.expect("Should be able to send signal to my thread.");
}

412
vendor/nix/test/sys/test_ptrace.rs vendored Normal file
View File

@@ -0,0 +1,412 @@
#[cfg(all(
target_os = "linux",
target_env = "gnu",
any(target_arch = "x86_64", target_arch = "x86")
))]
use memoffset::offset_of;
use nix::errno::Errno;
use nix::sys::ptrace;
#[cfg(linux_android)]
use nix::sys::ptrace::Options;
use nix::unistd::getpid;
#[cfg(linux_android)]
use std::mem;
use crate::*;
#[test]
fn test_ptrace() {
// Just make sure ptrace can be called at all, for now.
// FIXME: qemu-user doesn't implement ptrace on all arches, so permit ENOSYS
require_capability!("test_ptrace", CAP_SYS_PTRACE);
let err = ptrace::attach(getpid()).unwrap_err();
assert!(
err == Errno::EPERM || err == Errno::EINVAL || err == Errno::ENOSYS
);
}
// Just make sure ptrace_setoptions can be called at all, for now.
#[test]
#[cfg(linux_android)]
fn test_ptrace_setoptions() {
require_capability!("test_ptrace_setoptions", CAP_SYS_PTRACE);
let err = ptrace::setoptions(getpid(), Options::PTRACE_O_TRACESYSGOOD)
.unwrap_err();
assert_ne!(err, Errno::EOPNOTSUPP);
}
// Just make sure ptrace_getevent can be called at all, for now.
#[test]
#[cfg(linux_android)]
fn test_ptrace_getevent() {
require_capability!("test_ptrace_getevent", CAP_SYS_PTRACE);
let err = ptrace::getevent(getpid()).unwrap_err();
assert_ne!(err, Errno::EOPNOTSUPP);
}
// Just make sure ptrace_getsiginfo can be called at all, for now.
#[test]
#[cfg(linux_android)]
fn test_ptrace_getsiginfo() {
require_capability!("test_ptrace_getsiginfo", CAP_SYS_PTRACE);
if let Err(Errno::EOPNOTSUPP) = ptrace::getsiginfo(getpid()) {
panic!("ptrace_getsiginfo returns Errno::EOPNOTSUPP!");
}
}
// Just make sure ptrace_setsiginfo can be called at all, for now.
#[test]
#[cfg(linux_android)]
fn test_ptrace_setsiginfo() {
require_capability!("test_ptrace_setsiginfo", CAP_SYS_PTRACE);
let siginfo = unsafe { mem::zeroed() };
if let Err(Errno::EOPNOTSUPP) = ptrace::setsiginfo(getpid(), &siginfo) {
panic!("ptrace_setsiginfo returns Errno::EOPNOTSUPP!");
}
}
#[test]
fn test_ptrace_cont() {
use nix::sys::ptrace;
use nix::sys::signal::{raise, Signal};
use nix::sys::wait::{waitpid, WaitPidFlag, WaitStatus};
use nix::unistd::fork;
use nix::unistd::ForkResult::*;
require_capability!("test_ptrace_cont", CAP_SYS_PTRACE);
let _m = crate::FORK_MTX.lock();
// FIXME: qemu-user doesn't implement ptrace on all architectures
// and returns ENOSYS in this case.
// We (ab)use this behavior to detect the affected platforms
// and skip the test then.
// On valid platforms the ptrace call should return Errno::EPERM, this
// is already tested by `test_ptrace`.
let err = ptrace::attach(getpid()).unwrap_err();
if err == Errno::ENOSYS {
return;
}
match unsafe { fork() }.expect("Error: Fork Failed") {
Child => {
ptrace::traceme().unwrap();
// As recommended by ptrace(2), raise SIGTRAP to pause the child
// until the parent is ready to continue
loop {
raise(Signal::SIGTRAP).unwrap();
}
}
Parent { child } => {
assert_eq!(
waitpid(child, None),
Ok(WaitStatus::Stopped(child, Signal::SIGTRAP))
);
ptrace::cont(child, None).unwrap();
assert_eq!(
waitpid(child, None),
Ok(WaitStatus::Stopped(child, Signal::SIGTRAP))
);
ptrace::cont(child, Some(Signal::SIGKILL)).unwrap();
match waitpid(child, None) {
Ok(WaitStatus::Signaled(pid, Signal::SIGKILL, _))
if pid == child =>
{
// FIXME It's been observed on some systems (apple) the
// tracee may not be killed but remain as a zombie process
// affecting other wait based tests. Add an extra kill just
// to make sure there are no zombies.
let _ = waitpid(child, Some(WaitPidFlag::WNOHANG));
while ptrace::cont(child, Some(Signal::SIGKILL)).is_ok() {
let _ = waitpid(child, Some(WaitPidFlag::WNOHANG));
}
}
_ => panic!("The process should have been killed"),
}
}
}
}
#[cfg(target_os = "linux")]
#[test]
fn test_ptrace_interrupt() {
use nix::sys::ptrace;
use nix::sys::signal::Signal;
use nix::sys::wait::{waitpid, WaitPidFlag, WaitStatus};
use nix::unistd::fork;
use nix::unistd::ForkResult::*;
use std::thread::sleep;
use std::time::Duration;
require_capability!("test_ptrace_interrupt", CAP_SYS_PTRACE);
let _m = crate::FORK_MTX.lock();
match unsafe { fork() }.expect("Error: Fork Failed") {
Child => loop {
sleep(Duration::from_millis(1000));
},
Parent { child } => {
ptrace::seize(child, ptrace::Options::PTRACE_O_TRACESYSGOOD)
.unwrap();
ptrace::interrupt(child).unwrap();
assert_eq!(
waitpid(child, None),
Ok(WaitStatus::PtraceEvent(child, Signal::SIGTRAP, 128))
);
ptrace::syscall(child, None).unwrap();
assert_eq!(
waitpid(child, None),
Ok(WaitStatus::PtraceSyscall(child))
);
ptrace::detach(child, Some(Signal::SIGKILL)).unwrap();
match waitpid(child, None) {
Ok(WaitStatus::Signaled(pid, Signal::SIGKILL, _))
if pid == child =>
{
let _ = waitpid(child, Some(WaitPidFlag::WNOHANG));
while ptrace::cont(child, Some(Signal::SIGKILL)).is_ok() {
let _ = waitpid(child, Some(WaitPidFlag::WNOHANG));
}
}
_ => panic!("The process should have been killed"),
}
}
}
}
// ptrace::{setoptions, getregs} are only available in these platforms
#[cfg(all(
target_os = "linux",
any(
all(
target_env = "gnu",
any(
target_arch = "x86_64",
target_arch = "x86",
target_arch = "aarch64",
target_arch = "riscv64"
)
),
all(target_env = "musl", target_arch = "aarch64")
)
))]
#[test]
fn test_ptrace_syscall() {
use nix::sys::ptrace;
use nix::sys::signal::kill;
use nix::sys::signal::Signal;
use nix::sys::wait::{waitpid, WaitStatus};
use nix::unistd::fork;
use nix::unistd::getpid;
use nix::unistd::ForkResult::*;
require_capability!("test_ptrace_syscall", CAP_SYS_PTRACE);
let _m = crate::FORK_MTX.lock();
match unsafe { fork() }.expect("Error: Fork Failed") {
Child => {
ptrace::traceme().unwrap();
// first sigstop until parent is ready to continue
let pid = getpid();
kill(pid, Signal::SIGSTOP).unwrap();
kill(pid, Signal::SIGTERM).unwrap();
unsafe {
::libc::_exit(0);
}
}
Parent { child } => {
assert_eq!(
waitpid(child, None),
Ok(WaitStatus::Stopped(child, Signal::SIGSTOP))
);
// set this option to recognize syscall-stops
ptrace::setoptions(child, ptrace::Options::PTRACE_O_TRACESYSGOOD)
.unwrap();
#[cfg(target_arch = "x86_64")]
let get_syscall_id =
|| ptrace::getregs(child).unwrap().orig_rax as libc::c_long;
#[cfg(target_arch = "x86")]
let get_syscall_id =
|| ptrace::getregs(child).unwrap().orig_eax as libc::c_long;
#[cfg(target_arch = "aarch64")]
let get_syscall_id =
|| ptrace::getregs(child).unwrap().regs[8] as libc::c_long;
#[cfg(target_arch = "riscv64")]
let get_syscall_id =
|| ptrace::getregs(child).unwrap().a7 as libc::c_long;
// this duplicates `get_syscall_id` for the purpose of testing `ptrace::read_user`.
#[cfg(target_arch = "x86_64")]
let rax_offset = offset_of!(libc::user_regs_struct, orig_rax);
#[cfg(target_arch = "x86")]
let rax_offset = offset_of!(libc::user_regs_struct, orig_eax);
#[cfg(any(target_arch = "x86_64", target_arch = "x86"))]
let get_syscall_from_user_area = || {
// Find the offset of `user.regs.rax` (or `user.regs.eax` for x86)
let rax_offset = offset_of!(libc::user, regs) + rax_offset;
ptrace::read_user(child, rax_offset as _).unwrap()
as libc::c_long
};
// kill entry
ptrace::syscall(child, None).unwrap();
assert_eq!(
waitpid(child, None),
Ok(WaitStatus::PtraceSyscall(child))
);
assert_eq!(get_syscall_id(), ::libc::SYS_kill);
#[cfg(any(target_arch = "x86_64", target_arch = "x86"))]
assert_eq!(get_syscall_from_user_area(), ::libc::SYS_kill);
// kill exit
ptrace::syscall(child, None).unwrap();
assert_eq!(
waitpid(child, None),
Ok(WaitStatus::PtraceSyscall(child))
);
assert_eq!(get_syscall_id(), ::libc::SYS_kill);
#[cfg(any(target_arch = "x86_64", target_arch = "x86"))]
assert_eq!(get_syscall_from_user_area(), ::libc::SYS_kill);
// receive signal
ptrace::syscall(child, None).unwrap();
assert_eq!(
waitpid(child, None),
Ok(WaitStatus::Stopped(child, Signal::SIGTERM))
);
// inject signal
ptrace::syscall(child, Signal::SIGTERM).unwrap();
assert_eq!(
waitpid(child, None),
Ok(WaitStatus::Signaled(child, Signal::SIGTERM, false))
);
}
}
}
#[cfg(all(
target_os = "linux",
any(
all(
target_env = "gnu",
any(
target_arch = "x86_64",
target_arch = "x86",
target_arch = "aarch64",
target_arch = "riscv64"
)
),
all(target_env = "musl", target_arch = "aarch64")
)
))]
#[test]
fn test_ptrace_regsets() {
use nix::sys::ptrace::{self, getregset, regset, setregset};
use nix::sys::signal::*;
use nix::sys::wait::{waitpid, WaitStatus};
use nix::unistd::fork;
use nix::unistd::ForkResult::*;
require_capability!("test_ptrace_regsets", CAP_SYS_PTRACE);
let _m = crate::FORK_MTX.lock();
match unsafe { fork() }.expect("Error: Fork Failed") {
Child => {
ptrace::traceme().unwrap();
// As recommended by ptrace(2), raise SIGTRAP to pause the child
// until the parent is ready to continue
loop {
raise(Signal::SIGTRAP).unwrap();
}
}
Parent { child } => {
assert_eq!(
waitpid(child, None),
Ok(WaitStatus::Stopped(child, Signal::SIGTRAP))
);
let mut regstruct =
getregset::<regset::NT_PRSTATUS>(child).unwrap();
let mut fpregstruct =
getregset::<regset::NT_PRFPREG>(child).unwrap();
#[cfg(target_arch = "x86_64")]
let (reg, fpreg) =
(&mut regstruct.r15, &mut fpregstruct.st_space[5]);
#[cfg(target_arch = "x86")]
let (reg, fpreg) =
(&mut regstruct.edx, &mut fpregstruct.st_space[5]);
#[cfg(target_arch = "aarch64")]
let (reg, fpreg) =
(&mut regstruct.regs[16], &mut fpregstruct.vregs[5]);
#[cfg(target_arch = "riscv64")]
let (reg, fpreg) = (&mut regstruct.t1, &mut fpregstruct.__f[5]);
*reg = 0xdeadbeefu32 as _;
*fpreg = 0xfeedfaceu32 as _;
let _ = setregset::<regset::NT_PRSTATUS>(child, regstruct);
regstruct = getregset::<regset::NT_PRSTATUS>(child).unwrap();
let _ = setregset::<regset::NT_PRFPREG>(child, fpregstruct);
fpregstruct = getregset::<regset::NT_PRFPREG>(child).unwrap();
#[cfg(target_arch = "x86_64")]
let (reg, fpreg) = (regstruct.r15, fpregstruct.st_space[5]);
#[cfg(target_arch = "x86")]
let (reg, fpreg) = (regstruct.edx, fpregstruct.st_space[5]);
#[cfg(target_arch = "aarch64")]
let (reg, fpreg) = (regstruct.regs[16], fpregstruct.vregs[5]);
#[cfg(target_arch = "riscv64")]
let (reg, fpreg) = (regstruct.t1, fpregstruct.__f[5]);
assert_eq!(reg, 0xdeadbeefu32 as _);
assert_eq!(fpreg, 0xfeedfaceu32 as _);
ptrace::cont(child, Some(Signal::SIGKILL)).unwrap();
match waitpid(child, None) {
Ok(WaitStatus::Signaled(pid, Signal::SIGKILL, _))
if pid == child => {}
_ => panic!("The process should have been killed"),
}
}
}
}
#[cfg(all(target_os = "linux", target_env = "gnu"))]
#[test]
fn test_ptrace_syscall_info() {
use nix::sys::ptrace;
use nix::sys::wait::{waitpid, WaitStatus};
use nix::unistd::fork;
use nix::unistd::ForkResult::*;
require_capability!("test_ptrace_syscall_info", CAP_SYS_PTRACE);
let _m = crate::FORK_MTX.lock();
match unsafe { fork() }.expect("Error: Fork Failed") {
Child => {
ptrace::traceme().unwrap();
std::thread::sleep(std::time::Duration::from_millis(1000));
unsafe {
::libc::_exit(0);
}
}
Parent { child } => loop {
if let Ok(WaitStatus::Exited(_, 0)) = waitpid(child, None) {
break;
}
let si = ptrace::syscall_info(child).unwrap();
assert!(si.op >= libc::PTRACE_SYSCALL_INFO_ENTRY);
},
}
}

44
vendor/nix/test/sys/test_resource.rs vendored Normal file
View File

@@ -0,0 +1,44 @@
use nix::sys::resource::{getrlimit, setrlimit, Resource};
use nix::sys::resource::{getrusage, UsageWho};
/// Tests the RLIMIT_NOFILE functionality of getrlimit(), where the resource RLIMIT_NOFILE refers
/// to the maximum file descriptor number that can be opened by the process (aka the maximum number
/// of file descriptors that the process can open, since Linux 4.5).
///
/// We first fetch the existing file descriptor maximum values using getrlimit(), then edit the
/// soft limit to make sure it has a new and distinct value to the hard limit. We then setrlimit()
/// to put the new soft limit in effect, and then getrlimit() once more to ensure the limits have
/// been updated.
#[test]
#[cfg_attr(target_os = "cygwin", ignore)]
pub fn test_resource_limits_nofile() {
let (mut soft_limit, hard_limit) =
getrlimit(Resource::RLIMIT_NOFILE).unwrap();
soft_limit -= 1;
assert_ne!(soft_limit, hard_limit);
setrlimit(Resource::RLIMIT_NOFILE, soft_limit, hard_limit).unwrap();
let (new_soft_limit, _) = getrlimit(Resource::RLIMIT_NOFILE).unwrap();
assert_eq!(new_soft_limit, soft_limit);
}
#[test]
pub fn test_self_cpu_time() {
// Make sure some CPU time is used.
let mut numbers: Vec<i32> = (1..1_000_000).collect();
numbers.iter_mut().for_each(|item| *item *= 2);
// FIXME: this is here to help ensure the compiler does not optimize the whole
// thing away. Replace the assert with test::black_box once stabilized.
assert_eq!(numbers[100..200].iter().sum::<i32>(), 30_100);
let usage = getrusage(UsageWho::RUSAGE_SELF)
.expect("Failed to call getrusage for SELF");
let rusage = usage.as_ref();
let user = usage.user_time();
assert!(user.tv_sec() > 0 || user.tv_usec() > 0);
assert_eq!(user.tv_sec(), rusage.ru_utime.tv_sec);
assert_eq!(user.tv_usec(), rusage.ru_utime.tv_usec);
}

293
vendor/nix/test/sys/test_select.rs vendored Normal file
View File

@@ -0,0 +1,293 @@
use nix::sys::select::*;
use nix::sys::signal::SigSet;
use nix::sys::time::{TimeSpec, TimeVal, TimeValLike};
use nix::unistd::{pipe, write};
use std::os::unix::io::{AsFd, AsRawFd, BorrowedFd, RawFd};
#[test]
pub fn test_pselect() {
let _mtx = crate::SIGNAL_MTX.lock();
let (r1, w1) = pipe().unwrap();
write(&w1, b"hi!").unwrap();
let (r2, _w2) = pipe().unwrap();
let mut fd_set = FdSet::new();
fd_set.insert(r1.as_fd());
fd_set.insert(r2.as_fd());
let timeout = TimeSpec::seconds(10);
let sigmask = SigSet::empty();
assert_eq!(
1,
pselect(None, &mut fd_set, None, None, &timeout, &sigmask).unwrap()
);
assert!(fd_set.contains(r1.as_fd()));
assert!(!fd_set.contains(r2.as_fd()));
}
#[test]
pub fn test_pselect_nfds2() {
let (r1, w1) = pipe().unwrap();
write(&w1, b"hi!").unwrap();
let (r2, _w2) = pipe().unwrap();
let mut fd_set = FdSet::new();
fd_set.insert(r1.as_fd());
fd_set.insert(r2.as_fd());
let timeout = TimeSpec::seconds(10);
assert_eq!(
1,
pselect(
std::cmp::max(r1.as_raw_fd(), r2.as_raw_fd()) + 1,
&mut fd_set,
None,
None,
&timeout,
None
)
.unwrap()
);
assert!(fd_set.contains(r1.as_fd()));
assert!(!fd_set.contains(r2.as_fd()));
}
macro_rules! generate_fdset_bad_fd_tests {
($fd:expr, $($method:ident),* $(,)?) => {
$(
#[test]
#[should_panic]
fn $method() {
let bad_fd = unsafe{BorrowedFd::borrow_raw($fd)};
FdSet::new().$method(bad_fd);
}
)*
}
}
mod test_fdset_too_large_fd {
use super::*;
generate_fdset_bad_fd_tests!(
FD_SETSIZE.try_into().unwrap(),
insert,
remove,
contains,
);
}
#[test]
fn fdset_insert() {
let mut fd_set = FdSet::new();
for i in 0..FD_SETSIZE {
let borrowed_i = unsafe { BorrowedFd::borrow_raw(i as RawFd) };
assert!(!fd_set.contains(borrowed_i));
}
let fd_seven = unsafe { BorrowedFd::borrow_raw(7) };
fd_set.insert(fd_seven);
assert!(fd_set.contains(fd_seven));
}
#[test]
fn fdset_remove() {
let mut fd_set = FdSet::new();
for i in 0..FD_SETSIZE {
let borrowed_i = unsafe { BorrowedFd::borrow_raw(i as RawFd) };
assert!(!fd_set.contains(borrowed_i));
}
let fd_seven = unsafe { BorrowedFd::borrow_raw(7) };
fd_set.insert(fd_seven);
fd_set.remove(fd_seven);
for i in 0..FD_SETSIZE {
let borrowed_i = unsafe { BorrowedFd::borrow_raw(i as RawFd) };
assert!(!fd_set.contains(borrowed_i));
}
}
#[test]
#[allow(non_snake_case)]
fn fdset_clear() {
let mut fd_set = FdSet::new();
let fd_one = unsafe { BorrowedFd::borrow_raw(1) };
let fd_FD_SETSIZE_divided_by_two =
unsafe { BorrowedFd::borrow_raw((FD_SETSIZE / 2) as RawFd) };
let fd_FD_SETSIZE_minus_one =
unsafe { BorrowedFd::borrow_raw((FD_SETSIZE - 1) as RawFd) };
fd_set.insert(fd_one);
fd_set.insert(fd_FD_SETSIZE_divided_by_two);
fd_set.insert(fd_FD_SETSIZE_minus_one);
fd_set.clear();
for i in 0..FD_SETSIZE {
let borrowed_i = unsafe { BorrowedFd::borrow_raw(i as RawFd) };
assert!(!fd_set.contains(borrowed_i));
}
}
#[test]
fn fdset_highest() {
let mut set = FdSet::new();
assert_eq!(
set.highest().map(|borrowed_fd| borrowed_fd.as_raw_fd()),
None
);
let fd_zero = unsafe { BorrowedFd::borrow_raw(0) };
let fd_ninety = unsafe { BorrowedFd::borrow_raw(90) };
set.insert(fd_zero);
assert_eq!(
set.highest().map(|borrowed_fd| borrowed_fd.as_raw_fd()),
Some(0)
);
set.insert(fd_ninety);
assert_eq!(
set.highest().map(|borrowed_fd| borrowed_fd.as_raw_fd()),
Some(90)
);
set.remove(fd_zero);
assert_eq!(
set.highest().map(|borrowed_fd| borrowed_fd.as_raw_fd()),
Some(90)
);
set.remove(fd_ninety);
assert_eq!(
set.highest().map(|borrowed_fd| borrowed_fd.as_raw_fd()),
None
);
let fd_four = unsafe { BorrowedFd::borrow_raw(4) };
let fd_five = unsafe { BorrowedFd::borrow_raw(5) };
let fd_seven = unsafe { BorrowedFd::borrow_raw(7) };
set.insert(fd_four);
set.insert(fd_five);
set.insert(fd_seven);
assert_eq!(
set.highest().map(|borrowed_fd| borrowed_fd.as_raw_fd()),
Some(7)
);
}
#[test]
fn fdset_fds() {
let mut set = FdSet::new();
let fd_zero = unsafe { BorrowedFd::borrow_raw(0) };
let fd_ninety = unsafe { BorrowedFd::borrow_raw(90) };
assert_eq!(
set.fds(None)
.map(|borrowed_fd| borrowed_fd.as_raw_fd())
.collect::<Vec<_>>(),
vec![]
);
set.insert(fd_zero);
assert_eq!(
set.fds(None)
.map(|borrowed_fd| borrowed_fd.as_raw_fd())
.collect::<Vec<_>>(),
vec![0]
);
set.insert(fd_ninety);
assert_eq!(
set.fds(None)
.map(|borrowed_fd| borrowed_fd.as_raw_fd())
.collect::<Vec<_>>(),
vec![0, 90]
);
// highest limit
assert_eq!(
set.fds(Some(89))
.map(|borrowed_fd| borrowed_fd.as_raw_fd())
.collect::<Vec<_>>(),
vec![0]
);
assert_eq!(
set.fds(Some(90))
.map(|borrowed_fd| borrowed_fd.as_raw_fd())
.collect::<Vec<_>>(),
vec![0, 90]
);
}
#[test]
fn test_select() {
let (r1, w1) = pipe().unwrap();
let (r2, _w2) = pipe().unwrap();
write(&w1, b"hi!").unwrap();
let mut fd_set = FdSet::new();
fd_set.insert(r1.as_fd());
fd_set.insert(r2.as_fd());
let mut timeout = TimeVal::seconds(10);
assert_eq!(
1,
select(None, &mut fd_set, None, None, &mut timeout).unwrap()
);
assert!(fd_set.contains(r1.as_fd()));
assert!(!fd_set.contains(r2.as_fd()));
}
#[test]
fn test_select_nfds() {
let (r1, w1) = pipe().unwrap();
let (r2, _w2) = pipe().unwrap();
write(&w1, b"hi!").unwrap();
let mut fd_set = FdSet::new();
fd_set.insert(r1.as_fd());
fd_set.insert(r2.as_fd());
let mut timeout = TimeVal::seconds(10);
{
assert_eq!(
1,
select(
Some(
fd_set
.highest()
.map(|borrowed_fd| borrowed_fd.as_raw_fd())
.unwrap()
+ 1
),
&mut fd_set,
None,
None,
&mut timeout
)
.unwrap()
);
}
assert!(fd_set.contains(r1.as_fd()));
assert!(!fd_set.contains(r2.as_fd()));
}
#[test]
fn test_select_nfds2() {
let (r1, w1) = pipe().unwrap();
write(&w1, b"hi!").unwrap();
let (r2, _w2) = pipe().unwrap();
let mut fd_set = FdSet::new();
fd_set.insert(r1.as_fd());
fd_set.insert(r2.as_fd());
let mut timeout = TimeVal::seconds(10);
assert_eq!(
1,
select(
std::cmp::max(r1.as_raw_fd(), r2.as_raw_fd()) + 1,
&mut fd_set,
None,
None,
&mut timeout
)
.unwrap()
);
assert!(fd_set.contains(r1.as_fd()));
assert!(!fd_set.contains(r2.as_fd()));
}

456
vendor/nix/test/sys/test_signal.rs vendored Normal file
View File

@@ -0,0 +1,456 @@
use nix::errno::Errno;
use nix::sys::signal::*;
use nix::unistd::*;
use std::hash::{Hash, Hasher};
use std::sync::atomic::{AtomicBool, Ordering};
#[cfg(not(target_os = "redox"))]
use std::thread;
#[test]
fn test_kill_none() {
kill(getpid(), None).expect("Should be able to send signal to myself.");
}
#[test]
#[cfg(not(target_os = "fuchsia"))]
fn test_killpg_none() {
killpg(getpgrp(), None)
.expect("Should be able to send signal to my process group.");
}
#[test]
fn test_old_sigaction_flags() {
let _m = crate::SIGNAL_MTX.lock();
extern "C" fn handler(_: ::libc::c_int) {}
let act = SigAction::new(
SigHandler::Handler(handler),
SaFlags::empty(),
SigSet::empty(),
);
let oact = unsafe { sigaction(SIGINT, &act) }.unwrap();
let _flags = oact.flags();
let oact = unsafe { sigaction(SIGINT, &act) }.unwrap();
let _flags = oact.flags();
}
#[test]
fn test_sigprocmask_noop() {
sigprocmask(SigmaskHow::SIG_BLOCK, None, None)
.expect("this should be an effective noop");
}
#[test]
fn test_sigprocmask() {
let _m = crate::SIGNAL_MTX.lock();
// This needs to be a signal that rust doesn't use in the test harness.
const SIGNAL: Signal = Signal::SIGCHLD;
let mut old_signal_set = SigSet::empty();
sigprocmask(SigmaskHow::SIG_BLOCK, None, Some(&mut old_signal_set))
.expect("expect to be able to retrieve old signals");
// Make sure the old set doesn't contain the signal, otherwise the following
// test don't make sense.
assert!(
!old_signal_set.contains(SIGNAL),
"the {SIGNAL:?} signal is already blocked, please change to a \
different one"
);
// Now block the signal.
let mut signal_set = SigSet::empty();
signal_set.add(SIGNAL);
sigprocmask(SigmaskHow::SIG_BLOCK, Some(&signal_set), None)
.expect("expect to be able to block signals");
// And test it again, to make sure the change was effective.
old_signal_set.clear();
sigprocmask(SigmaskHow::SIG_BLOCK, None, Some(&mut old_signal_set))
.expect("expect to be able to retrieve old signals");
assert!(
old_signal_set.contains(SIGNAL),
"expected the {SIGNAL:?} to be blocked"
);
// Reset the signal.
sigprocmask(SigmaskHow::SIG_UNBLOCK, Some(&signal_set), None)
.expect("expect to be able to block signals");
}
static SIGNALED: AtomicBool = AtomicBool::new(false);
extern "C" fn test_sigaction_handler(signal: libc::c_int) {
let signal = Signal::try_from(signal).unwrap();
SIGNALED.store(signal == Signal::SIGINT, Ordering::Relaxed);
}
#[cfg(not(target_os = "redox"))]
extern "C" fn test_sigaction_action(
_: libc::c_int,
_: *mut libc::siginfo_t,
_: *mut libc::c_void,
) {
}
#[test]
#[cfg(not(target_os = "redox"))]
fn test_signal_sigaction() {
let _m = crate::SIGNAL_MTX.lock();
let action_handler = SigHandler::SigAction(test_sigaction_action);
assert_eq!(
unsafe { signal(Signal::SIGINT, action_handler) }.unwrap_err(),
Errno::ENOTSUP
);
}
#[test]
fn test_signal() {
let _m = crate::SIGNAL_MTX.lock();
unsafe { signal(Signal::SIGINT, SigHandler::SigIgn) }.unwrap();
raise(Signal::SIGINT).unwrap();
assert_eq!(
unsafe { signal(Signal::SIGINT, SigHandler::SigDfl) }.unwrap(),
SigHandler::SigIgn
);
let handler = SigHandler::Handler(test_sigaction_handler);
assert_eq!(
unsafe { signal(Signal::SIGINT, handler) }.unwrap(),
SigHandler::SigDfl
);
raise(Signal::SIGINT).unwrap();
assert!(SIGNALED.load(Ordering::Relaxed));
#[cfg(not(solarish))]
assert_eq!(
unsafe { signal(Signal::SIGINT, SigHandler::SigDfl) }.unwrap(),
handler
);
// System V based OSes (e.g. illumos and Solaris) always resets the
// disposition to SIG_DFL prior to calling the signal handler
#[cfg(solarish)]
assert_eq!(
unsafe { signal(Signal::SIGINT, SigHandler::SigDfl) }.unwrap(),
SigHandler::SigDfl
);
// Restore default signal handler
unsafe { signal(Signal::SIGINT, SigHandler::SigDfl) }.unwrap();
}
#[test]
fn test_contains() {
let mut mask = SigSet::empty();
mask.add(SIGUSR1);
assert!(mask.contains(SIGUSR1));
assert!(!mask.contains(SIGUSR2));
let all = SigSet::all();
assert!(all.contains(SIGUSR1));
assert!(all.contains(SIGUSR2));
}
#[test]
fn test_clear() {
let mut set = SigSet::all();
set.clear();
for signal in Signal::iterator() {
assert!(!set.contains(signal));
}
}
#[test]
fn test_from_str_round_trips() {
for signal in Signal::iterator() {
assert_eq!(signal.as_ref().parse::<Signal>().unwrap(), signal);
assert_eq!(signal.to_string().parse::<Signal>().unwrap(), signal);
}
}
#[test]
fn test_from_str_invalid_value() {
let errval = Err(Errno::EINVAL);
assert_eq!("NOSIGNAL".parse::<Signal>(), errval);
assert_eq!("kill".parse::<Signal>(), errval);
assert_eq!("9".parse::<Signal>(), errval);
}
#[test]
fn test_extend() {
let mut one_signal = SigSet::empty();
one_signal.add(SIGUSR1);
let mut two_signals = SigSet::empty();
two_signals.add(SIGUSR2);
two_signals.extend(&one_signal);
assert!(two_signals.contains(SIGUSR1));
assert!(two_signals.contains(SIGUSR2));
}
#[test]
#[cfg(not(target_os = "redox"))]
fn test_thread_signal_set_mask() {
thread::spawn(|| {
let prev_mask = SigSet::thread_get_mask()
.expect("Failed to get existing signal mask!");
let mut test_mask = prev_mask;
test_mask.add(SIGUSR1);
test_mask.thread_set_mask().expect("assertion failed");
let new_mask =
SigSet::thread_get_mask().expect("Failed to get new mask!");
assert!(new_mask.contains(SIGUSR1));
assert!(!new_mask.contains(SIGUSR2));
prev_mask
.thread_set_mask()
.expect("Failed to revert signal mask!");
})
.join()
.unwrap();
}
#[test]
#[cfg(not(target_os = "redox"))]
fn test_thread_signal_block() {
thread::spawn(|| {
let mut mask = SigSet::empty();
mask.add(SIGUSR1);
mask.thread_block().expect("assertion failed");
assert!(SigSet::thread_get_mask().unwrap().contains(SIGUSR1));
})
.join()
.unwrap();
}
#[test]
#[cfg(not(target_os = "redox"))]
fn test_thread_signal_unblock() {
thread::spawn(|| {
let mut mask = SigSet::empty();
mask.add(SIGUSR1);
mask.thread_unblock().expect("assertion failed");
assert!(!SigSet::thread_get_mask().unwrap().contains(SIGUSR1));
})
.join()
.unwrap();
}
#[test]
#[cfg(not(target_os = "redox"))]
fn test_thread_signal_swap() {
thread::spawn(|| {
let mut mask = SigSet::empty();
mask.add(SIGUSR1);
mask.thread_block().unwrap();
assert!(SigSet::thread_get_mask().unwrap().contains(SIGUSR1));
let mut mask2 = SigSet::empty();
mask2.add(SIGUSR2);
let oldmask = mask2.thread_swap_mask(SigmaskHow::SIG_SETMASK).unwrap();
assert!(oldmask.contains(SIGUSR1));
assert!(!oldmask.contains(SIGUSR2));
assert!(SigSet::thread_get_mask().unwrap().contains(SIGUSR2));
})
.join()
.unwrap();
}
#[test]
fn test_from_and_into_iterator() {
let sigset = SigSet::from_iter(vec![Signal::SIGUSR1, Signal::SIGUSR2]);
let signals = sigset.into_iter().collect::<Vec<Signal>>();
assert_eq!(signals, [Signal::SIGUSR1, Signal::SIGUSR2]);
}
#[test]
#[cfg(not(target_os = "redox"))]
fn test_sigaction() {
let _m = crate::SIGNAL_MTX.lock();
thread::spawn(|| {
extern "C" fn test_sigaction_handler(_: libc::c_int) {}
extern "C" fn test_sigaction_action(
_: libc::c_int,
_: *mut libc::siginfo_t,
_: *mut libc::c_void,
) {
}
let handler_sig = SigHandler::Handler(test_sigaction_handler);
let flags =
SaFlags::SA_ONSTACK | SaFlags::SA_RESTART | SaFlags::SA_SIGINFO;
let mut mask = SigSet::empty();
mask.add(SIGUSR1);
let action_sig = SigAction::new(handler_sig, flags, mask);
assert_eq!(
action_sig.flags(),
SaFlags::SA_ONSTACK | SaFlags::SA_RESTART
);
assert_eq!(action_sig.handler(), handler_sig);
mask = action_sig.mask();
assert!(mask.contains(SIGUSR1));
assert!(!mask.contains(SIGUSR2));
let handler_act = SigHandler::SigAction(test_sigaction_action);
let action_act = SigAction::new(handler_act, flags, mask);
assert_eq!(action_act.handler(), handler_act);
let action_dfl = SigAction::new(SigHandler::SigDfl, flags, mask);
assert_eq!(action_dfl.handler(), SigHandler::SigDfl);
let action_ign = SigAction::new(SigHandler::SigIgn, flags, mask);
assert_eq!(action_ign.handler(), SigHandler::SigIgn);
})
.join()
.unwrap();
}
#[test]
#[cfg(not(target_os = "redox"))]
fn test_sigwait() {
thread::spawn(|| {
let mut mask = SigSet::empty();
mask.add(SIGUSR1);
mask.add(SIGUSR2);
mask.thread_block().unwrap();
raise(SIGUSR1).unwrap();
assert_eq!(mask.wait().unwrap(), SIGUSR1);
})
.join()
.unwrap();
}
#[cfg(any(
bsd,
linux_android,
solarish,
target_os = "haiku",
target_os = "hurd",
target_os = "aix",
target_os = "fuchsia"
))]
#[test]
fn test_sigsuspend() {
// This test change signal handler
let _m = crate::SIGNAL_MTX.lock();
static SIGNAL_RECIEVED: AtomicBool = AtomicBool::new(false);
extern "C" fn test_sigsuspend_handler(_: libc::c_int) {
assert!(!SIGNAL_RECIEVED.swap(true, Ordering::SeqCst));
}
thread::spawn(|| {
const SIGNAL: Signal = Signal::SIGUSR1;
// Add signal mask to this thread
let mut signal_set = SigSet::empty();
signal_set.add(SIGNAL);
signal_set.thread_block().unwrap();
// Set signal handler and save old one.
let act = SigAction::new(
SigHandler::Handler(test_sigsuspend_handler),
SaFlags::empty(),
SigSet::empty(),
);
let old_act = unsafe { sigaction(SIGNAL, &act) }
.expect("expect to be able to set new action and get old action");
raise(SIGNAL).expect("expect be able to send signal");
// Now `SIGNAL` was sended but it is blocked.
let mut not_wait_set = SigSet::all();
not_wait_set.remove(SIGNAL);
// signal handler must run in SigSet::suspend()
assert!(!SIGNAL_RECIEVED.load(Ordering::SeqCst));
not_wait_set.suspend().unwrap();
assert!(SIGNAL_RECIEVED.load(Ordering::SeqCst));
// Restore the signal handler.
unsafe { sigaction(SIGNAL, &old_act) }
.expect("expect to be able to restore old action ");
})
.join()
.unwrap();
}
#[test]
fn test_from_sigset_t_unchecked() {
let src_set = SigSet::empty();
let set = unsafe { SigSet::from_sigset_t_unchecked(*src_set.as_ref()) };
for signal in Signal::iterator() {
assert!(!set.contains(signal));
}
let src_set = SigSet::all();
let set = unsafe { SigSet::from_sigset_t_unchecked(*src_set.as_ref()) };
for signal in Signal::iterator() {
assert!(set.contains(signal));
}
}
#[test]
fn test_eq_empty() {
let set0 = SigSet::empty();
let set1 = SigSet::empty();
assert_eq!(set0, set1);
}
#[test]
fn test_eq_all() {
let set0 = SigSet::all();
let set1 = SigSet::all();
assert_eq!(set0, set1);
}
#[test]
fn test_hash_empty() {
use std::collections::hash_map::DefaultHasher;
let set0 = SigSet::empty();
let mut h0 = DefaultHasher::new();
set0.hash(&mut h0);
let set1 = SigSet::empty();
let mut h1 = DefaultHasher::new();
set1.hash(&mut h1);
assert_eq!(h0.finish(), h1.finish());
}
#[test]
fn test_hash_all() {
use std::collections::hash_map::DefaultHasher;
let set0 = SigSet::all();
let mut h0 = DefaultHasher::new();
set0.hash(&mut h0);
let set1 = SigSet::all();
let mut h1 = DefaultHasher::new();
set1.hash(&mut h1);
assert_eq!(h0.finish(), h1.finish());
}

90
vendor/nix/test/sys/test_signalfd.rs vendored Normal file
View File

@@ -0,0 +1,90 @@
use std::convert::TryFrom;
#[test]
fn create_signalfd() {
use nix::sys::{signal::SigSet, signalfd::SignalFd};
let mask = SigSet::empty();
SignalFd::new(&mask).unwrap();
}
#[test]
fn create_signalfd_with_opts() {
use nix::sys::{
signal::SigSet,
signalfd::{SfdFlags, SignalFd},
};
let mask = SigSet::empty();
SignalFd::with_flags(&mask, SfdFlags::SFD_CLOEXEC | SfdFlags::SFD_NONBLOCK)
.unwrap();
}
#[test]
fn read_empty_signalfd() {
use nix::sys::{
signal::SigSet,
signalfd::{SfdFlags, SignalFd},
};
let mask = SigSet::empty();
let fd = SignalFd::with_flags(&mask, SfdFlags::SFD_NONBLOCK).unwrap();
let res = fd.read_signal();
assert!(res.unwrap().is_none());
}
#[test]
fn test_signalfd() {
use nix::sys::signal::{self, raise, SigSet, Signal};
use nix::sys::signalfd::SignalFd;
// Grab the mutex for altering signals so we don't interfere with other tests.
let _m = crate::SIGNAL_MTX.lock();
// Block the SIGUSR1 signal from automatic processing for this thread
let mut mask = SigSet::empty();
mask.add(signal::SIGUSR1);
mask.thread_block().unwrap();
let fd = SignalFd::new(&mask).unwrap();
// Send a SIGUSR1 signal to the current process. Note that this uses `raise` instead of `kill`
// because `kill` with `getpid` isn't correct during multi-threaded execution like during a
// cargo test session. Instead use `raise` which does the correct thing by default.
raise(signal::SIGUSR1).expect("Error: raise(SIGUSR1) failed");
// And now catch that same signal.
let res = fd.read_signal().unwrap().unwrap();
let signo = Signal::try_from(res.ssi_signo as i32).unwrap();
assert_eq!(signo, signal::SIGUSR1);
}
/// Update the signal mask of an already existing signalfd.
#[test]
fn test_signalfd_setmask() {
use nix::sys::signal::{self, raise, SigSet, Signal};
use nix::sys::signalfd::SignalFd;
// Grab the mutex for altering signals so we don't interfere with other tests.
let _m = crate::SIGNAL_MTX.lock();
// Block the SIGUSR1 signal from automatic processing for this thread
let mut mask = SigSet::empty();
let fd = SignalFd::new(&mask).unwrap();
mask.add(signal::SIGUSR1);
mask.thread_block().unwrap();
fd.set_mask(&mask).unwrap();
// Send a SIGUSR1 signal to the current process. Note that this uses `raise` instead of `kill`
// because `kill` with `getpid` isn't correct during multi-threaded execution like during a
// cargo test session. Instead use `raise` which does the correct thing by default.
raise(signal::SIGUSR1).expect("Error: raise(SIGUSR1) failed");
// And now catch that same signal.
let res = fd.read_signal().unwrap().unwrap();
let signo = Signal::try_from(res.ssi_signo as i32).unwrap();
assert_eq!(signo, signal::SIGUSR1);
}

3172
vendor/nix/test/sys/test_socket.rs vendored Normal file

File diff suppressed because it is too large Load Diff

1272
vendor/nix/test/sys/test_sockopt.rs vendored Normal file

File diff suppressed because it is too large Load Diff

508
vendor/nix/test/sys/test_stat.rs vendored Normal file
View File

@@ -0,0 +1,508 @@
#[cfg(not(any(target_os = "redox", target_os = "haiku")))]
use std::fs;
use std::fs::File;
#[cfg(not(target_os = "redox"))]
use std::os::unix::fs::symlink;
#[cfg(not(any(target_os = "redox", target_os = "haiku")))]
use std::os::unix::fs::PermissionsExt;
#[cfg(not(target_os = "redox"))]
use std::path::Path;
#[cfg(not(any(target_os = "redox", target_os = "haiku")))]
use std::time::{Duration, UNIX_EPOCH};
use libc::mode_t;
#[cfg(not(any(target_os = "netbsd", target_os = "redox")))]
use libc::{S_IFLNK, S_IFMT};
#[cfg(not(target_os = "redox"))]
use nix::errno::Errno;
#[cfg(not(target_os = "redox"))]
use nix::fcntl;
#[cfg(any(
target_os = "linux",
apple_targets,
target_os = "freebsd",
target_os = "netbsd"
))]
use nix::sys::stat::lutimes;
#[cfg(not(any(target_os = "redox", target_os = "haiku")))]
use nix::sys::stat::utimensat;
#[cfg(not(target_os = "redox"))]
use nix::sys::stat::FchmodatFlags;
use nix::sys::stat::Mode;
#[cfg(not(any(target_os = "redox", target_os = "haiku")))]
use nix::sys::stat::UtimensatFlags;
#[cfg(not(target_os = "redox"))]
use nix::sys::stat::{self};
use nix::sys::stat::{fchmod, stat};
#[cfg(not(target_os = "redox"))]
use nix::sys::stat::{fchmodat, mkdirat};
#[cfg(not(any(target_os = "redox", target_os = "haiku")))]
use nix::sys::stat::{futimens, utimes};
#[cfg(not(any(target_os = "netbsd", target_os = "redox")))]
use nix::sys::stat::FileStat;
#[cfg(not(any(target_os = "redox", target_os = "haiku")))]
use nix::sys::time::{TimeSpec, TimeVal, TimeValLike};
#[cfg(not(target_os = "redox"))]
use nix::unistd::chdir;
#[cfg(not(any(target_os = "netbsd", target_os = "redox")))]
use nix::Result;
#[cfg(not(any(target_os = "netbsd", target_os = "redox")))]
fn assert_stat_results(stat_result: Result<FileStat>) {
let stats = stat_result.expect("stat call failed");
assert!(stats.st_dev > 0); // must be positive integer, exact number machine dependent
assert!(stats.st_ino > 0); // inode is positive integer, exact number machine dependent
assert!(stats.st_mode > 0); // must be positive integer
assert_eq!(stats.st_nlink, 1); // there links created, must be 1
assert_eq!(stats.st_size, 0); // size is 0 because we did not write anything to the file
assert!(stats.st_blksize > 0); // must be positive integer, exact number machine dependent
assert!(stats.st_blocks <= 16); // Up to 16 blocks can be allocated for a blank file
}
#[cfg(not(any(target_os = "netbsd", target_os = "redox")))]
// (Android's st_blocks is ulonglong which is always non-negative.)
#[cfg_attr(target_os = "android", allow(unused_comparisons))]
#[allow(clippy::absurd_extreme_comparisons)] // Not absurd on all OSes
fn assert_lstat_results(stat_result: Result<FileStat>) {
let stats = stat_result.expect("stat call failed");
assert!(stats.st_dev > 0); // must be positive integer, exact number machine dependent
assert!(stats.st_ino > 0); // inode is positive integer, exact number machine dependent
assert!(stats.st_mode > 0); // must be positive integer
// st_mode is c_uint (u32 on Android) while S_IFMT is mode_t
// (u16 on Android), and that will be a compile error.
// On other platforms they are the same (either both are u16 or u32).
assert_eq!(
(stats.st_mode as usize) & (S_IFMT as usize),
S_IFLNK as usize
); // should be a link
assert_eq!(stats.st_nlink, 1); // there links created, must be 1
assert!(stats.st_size > 0); // size is > 0 because it points to another file
assert!(stats.st_blksize > 0); // must be positive integer, exact number machine dependent
// st_blocks depends on whether the machine's file system uses fast
// or slow symlinks, so just make sure it's not negative
assert!(stats.st_blocks >= 0);
}
#[test]
#[cfg(not(any(target_os = "netbsd", target_os = "redox")))]
fn test_stat_and_fstat() {
use nix::sys::stat::fstat;
let tempdir = tempfile::tempdir().unwrap();
let filename = tempdir.path().join("foo.txt");
let file = File::create(&filename).unwrap();
let stat_result = stat(&filename);
assert_stat_results(stat_result);
let fstat_result = fstat(&file);
assert_stat_results(fstat_result);
}
#[test]
#[cfg(not(any(target_os = "netbsd", target_os = "redox")))]
fn test_fstatat() {
let tempdir = tempfile::tempdir().unwrap();
let filename = tempdir.path().join("foo.txt");
File::create(&filename).unwrap();
let dirfd =
fcntl::open(tempdir.path(), fcntl::OFlag::empty(), stat::Mode::empty())
.unwrap();
let result = stat::fstatat(&dirfd, &filename, fcntl::AtFlags::empty());
assert_stat_results(result);
}
#[test]
#[cfg(not(any(target_os = "netbsd", target_os = "redox")))]
fn test_stat_fstat_lstat() {
use nix::sys::stat::{fstat, lstat};
let tempdir = tempfile::tempdir().unwrap();
let filename = tempdir.path().join("bar.txt");
let linkname = tempdir.path().join("barlink");
File::create(&filename).unwrap();
symlink("bar.txt", &linkname).unwrap();
let link = File::open(&linkname).unwrap();
// should be the same result as calling stat,
// since it's a regular file
let stat_result = stat(&filename);
assert_stat_results(stat_result);
let lstat_result = lstat(&linkname);
assert_lstat_results(lstat_result);
let fstat_result = fstat(&link);
assert_stat_results(fstat_result);
}
#[test]
fn test_fchmod() {
let tempdir = tempfile::tempdir().unwrap();
let filename = tempdir.path().join("foo.txt");
let file = File::create(&filename).unwrap();
let mut mode1 = Mode::empty();
mode1.insert(Mode::S_IRUSR);
mode1.insert(Mode::S_IWUSR);
fchmod(&file, mode1).unwrap();
let file_stat1 = stat(&filename).unwrap();
assert_eq!(file_stat1.st_mode as mode_t & 0o7777, mode1.bits());
let mut mode2 = Mode::empty();
mode2.insert(Mode::S_IROTH);
fchmod(&file, mode2).unwrap();
let file_stat2 = stat(&filename).unwrap();
assert_eq!(file_stat2.st_mode as mode_t & 0o7777, mode2.bits());
}
#[test]
#[cfg(not(target_os = "redox"))]
fn test_fchmodat() {
let _dr = crate::DirRestore::new();
let tempdir = tempfile::tempdir().unwrap();
let filename = "foo.txt";
let fullpath = tempdir.path().join(filename);
File::create(&fullpath).unwrap();
let dirfd =
fcntl::open(tempdir.path(), fcntl::OFlag::empty(), stat::Mode::empty())
.unwrap();
let mut mode1 = Mode::empty();
mode1.insert(Mode::S_IRUSR);
mode1.insert(Mode::S_IWUSR);
fchmodat(&dirfd, filename, mode1, FchmodatFlags::FollowSymlink).unwrap();
let file_stat1 = stat(&fullpath).unwrap();
assert_eq!(file_stat1.st_mode as mode_t & 0o7777, mode1.bits());
chdir(tempdir.path()).unwrap();
let mut mode2 = Mode::empty();
mode2.insert(Mode::S_IROTH);
fchmodat(
fcntl::AT_FDCWD,
filename,
mode2,
FchmodatFlags::FollowSymlink,
)
.unwrap();
let file_stat2 = stat(&fullpath).unwrap();
assert_eq!(file_stat2.st_mode as mode_t & 0o7777, mode2.bits());
}
/// Asserts that the atime and mtime in a file's metadata match expected values.
///
/// The atime and mtime are expressed with a resolution of seconds because some file systems
/// (like macOS's HFS+) do not have higher granularity.
#[cfg(not(any(target_os = "redox", target_os = "haiku")))]
fn assert_times_eq(
exp_atime_sec: u64,
exp_mtime_sec: u64,
attr: &fs::Metadata,
) {
assert_eq!(
Duration::new(exp_atime_sec, 0),
attr.accessed().unwrap().duration_since(UNIX_EPOCH).unwrap()
);
assert_eq!(
Duration::new(exp_mtime_sec, 0),
attr.modified().unwrap().duration_since(UNIX_EPOCH).unwrap()
);
}
#[test]
#[cfg(not(any(target_os = "redox", target_os = "haiku")))]
fn test_utimes() {
let tempdir = tempfile::tempdir().unwrap();
let fullpath = tempdir.path().join("file");
drop(File::create(&fullpath).unwrap());
utimes(&fullpath, &TimeVal::seconds(9990), &TimeVal::seconds(5550))
.unwrap();
assert_times_eq(9990, 5550, &fs::metadata(&fullpath).unwrap());
}
#[test]
#[cfg(any(
target_os = "linux",
apple_targets,
target_os = "freebsd",
target_os = "netbsd"
))]
fn test_lutimes() {
let tempdir = tempfile::tempdir().unwrap();
let target = tempdir.path().join("target");
let fullpath = tempdir.path().join("symlink");
drop(File::create(&target).unwrap());
symlink(&target, &fullpath).unwrap();
let exp_target_metadata = fs::symlink_metadata(&target).unwrap();
lutimes(&fullpath, &TimeVal::seconds(4560), &TimeVal::seconds(1230))
.unwrap();
assert_times_eq(4560, 1230, &fs::symlink_metadata(&fullpath).unwrap());
let target_metadata = fs::symlink_metadata(&target).unwrap();
assert_eq!(
exp_target_metadata.accessed().unwrap(),
target_metadata.accessed().unwrap(),
"atime of symlink target was unexpectedly modified"
);
assert_eq!(
exp_target_metadata.modified().unwrap(),
target_metadata.modified().unwrap(),
"mtime of symlink target was unexpectedly modified"
);
}
#[test]
#[cfg(not(any(target_os = "redox", target_os = "haiku")))]
fn test_futimens() {
let tempdir = tempfile::tempdir().unwrap();
let fullpath = tempdir.path().join("file");
drop(File::create(&fullpath).unwrap());
let fd = fcntl::open(&fullpath, fcntl::OFlag::empty(), stat::Mode::empty())
.unwrap();
futimens(&fd, &TimeSpec::seconds(10), &TimeSpec::seconds(20)).unwrap();
assert_times_eq(10, 20, &fs::metadata(&fullpath).unwrap());
}
#[test]
#[cfg(not(any(target_os = "redox", target_os = "haiku")))]
fn test_utimensat() {
let _dr = crate::DirRestore::new();
let tempdir = tempfile::tempdir().unwrap();
let filename = "foo.txt";
let fullpath = tempdir.path().join(filename);
drop(File::create(&fullpath).unwrap());
let dirfd =
fcntl::open(tempdir.path(), fcntl::OFlag::empty(), stat::Mode::empty())
.unwrap();
utimensat(
&dirfd,
filename,
&TimeSpec::seconds(12345),
&TimeSpec::seconds(678),
UtimensatFlags::FollowSymlink,
)
.unwrap();
assert_times_eq(12345, 678, &fs::metadata(&fullpath).unwrap());
chdir(tempdir.path()).unwrap();
utimensat(
fcntl::AT_FDCWD,
filename,
&TimeSpec::seconds(500),
&TimeSpec::seconds(800),
UtimensatFlags::FollowSymlink,
)
.unwrap();
assert_times_eq(500, 800, &fs::metadata(&fullpath).unwrap());
}
#[test]
#[cfg(not(target_os = "redox"))]
fn test_mkdirat_success_path() {
let tempdir = tempfile::tempdir().unwrap();
let filename = "example_subdir";
let dirfd =
fcntl::open(tempdir.path(), fcntl::OFlag::empty(), stat::Mode::empty())
.unwrap();
mkdirat(&dirfd, filename, Mode::S_IRWXU).expect("mkdirat failed");
assert!(Path::exists(&tempdir.path().join(filename)));
}
#[test]
#[cfg(not(any(target_os = "redox", target_os = "haiku")))]
fn test_mkdirat_success_mode() {
let expected_bits =
stat::SFlag::S_IFDIR.bits() | stat::Mode::S_IRWXU.bits();
let tempdir = tempfile::tempdir().unwrap();
let filename = "example_subdir";
let dirfd =
fcntl::open(tempdir.path(), fcntl::OFlag::empty(), stat::Mode::empty())
.unwrap();
mkdirat(&dirfd, filename, Mode::S_IRWXU).expect("mkdirat failed");
let permissions = fs::metadata(tempdir.path().join(filename))
.unwrap()
.permissions();
let mode = permissions.mode();
assert_eq!(mode as mode_t, expected_bits)
}
#[test]
#[cfg(not(target_os = "redox"))]
fn test_mkdirat_fail() {
let tempdir = tempfile::tempdir().unwrap();
let not_dir_filename = "example_not_dir";
let filename = "example_subdir_dir";
let dirfd = fcntl::open(
&tempdir.path().join(not_dir_filename),
fcntl::OFlag::O_CREAT,
stat::Mode::empty(),
)
.unwrap();
let result = mkdirat(dirfd, filename, Mode::S_IRWXU).unwrap_err();
assert_eq!(result, Errno::ENOTDIR);
}
#[test]
#[cfg(not(any(
freebsdlike,
apple_targets,
target_os = "haiku",
target_os = "redox",
target_os = "solaris"
)))]
fn test_mknod() {
use stat::{lstat, mknod, SFlag};
let file_name = "test_file";
let tempdir = tempfile::tempdir().unwrap();
let target = tempdir.path().join(file_name);
mknod(&target, SFlag::S_IFREG, Mode::S_IRWXU, 0).unwrap();
let mode = lstat(&target).unwrap().st_mode as mode_t;
assert_eq!(mode & libc::S_IFREG, libc::S_IFREG);
assert_eq!(mode & libc::S_IRWXU, libc::S_IRWXU);
}
#[test]
#[cfg(not(any(
solarish,
freebsdlike,
apple_targets,
target_os = "haiku",
target_os = "redox"
)))]
fn test_mknodat() {
use fcntl::{AtFlags, OFlag};
use nix::dir::Dir;
use stat::{fstatat, mknodat, SFlag};
let file_name = "test_file";
let tempdir = tempfile::tempdir().unwrap();
let target_dir =
Dir::open(tempdir.path(), OFlag::O_DIRECTORY, Mode::S_IRWXU).unwrap();
mknodat(&target_dir, file_name, SFlag::S_IFREG, Mode::S_IRWXU, 0).unwrap();
let mode = fstatat(&target_dir, file_name, AtFlags::AT_SYMLINK_NOFOLLOW)
.unwrap()
.st_mode as mode_t;
assert_eq!(mode & libc::S_IFREG, libc::S_IFREG);
assert_eq!(mode & libc::S_IRWXU, libc::S_IRWXU);
}
#[test]
#[cfg(not(any(target_os = "redox", target_os = "haiku")))]
fn test_futimens_unchanged() {
let tempdir = tempfile::tempdir().unwrap();
let fullpath = tempdir.path().join("file");
drop(File::create(&fullpath).unwrap());
let fd = fcntl::open(&fullpath, fcntl::OFlag::empty(), stat::Mode::empty())
.unwrap();
let old_atime = fs::metadata(fullpath.as_path())
.unwrap()
.accessed()
.unwrap();
let old_mtime = fs::metadata(fullpath.as_path())
.unwrap()
.modified()
.unwrap();
futimens(&fd, &TimeSpec::UTIME_OMIT, &TimeSpec::UTIME_OMIT).unwrap();
let new_atime = fs::metadata(fullpath.as_path())
.unwrap()
.accessed()
.unwrap();
let new_mtime = fs::metadata(fullpath.as_path())
.unwrap()
.modified()
.unwrap();
assert_eq!(old_atime, new_atime);
assert_eq!(old_mtime, new_mtime);
}
#[test]
#[cfg(not(any(target_os = "redox", target_os = "haiku")))]
fn test_utimensat_unchanged() {
let _dr = crate::DirRestore::new();
let tempdir = tempfile::tempdir().unwrap();
let filename = "foo.txt";
let fullpath = tempdir.path().join(filename);
drop(File::create(&fullpath).unwrap());
let dirfd =
fcntl::open(tempdir.path(), fcntl::OFlag::empty(), stat::Mode::empty())
.unwrap();
let old_atime = fs::metadata(fullpath.as_path())
.unwrap()
.accessed()
.unwrap();
let old_mtime = fs::metadata(fullpath.as_path())
.unwrap()
.modified()
.unwrap();
utimensat(
&dirfd,
filename,
&TimeSpec::UTIME_OMIT,
&TimeSpec::UTIME_OMIT,
UtimensatFlags::NoFollowSymlink,
)
.unwrap();
let new_atime = fs::metadata(fullpath.as_path())
.unwrap()
.accessed()
.unwrap();
let new_mtime = fs::metadata(fullpath.as_path())
.unwrap()
.modified()
.unwrap();
assert_eq!(old_atime, new_atime);
assert_eq!(old_mtime, new_mtime);
}
// The conversion is not useless on all platforms.
#[allow(clippy::useless_conversion)]
#[cfg(target_os = "freebsd")]
#[test]
fn test_chflags() {
use nix::{
sys::stat::{fstat, FileFlag},
unistd::chflags,
};
use tempfile::NamedTempFile;
let f = NamedTempFile::new().unwrap();
let initial =
FileFlag::from_bits_truncate(fstat(&f).unwrap().st_flags.into());
// UF_OFFLINE is preserved by all FreeBSD file systems, but not interpreted
// in any way, so it's handy for testing.
let commanded = initial ^ FileFlag::UF_OFFLINE;
chflags(f.path(), commanded).unwrap();
let changed =
FileFlag::from_bits_truncate(fstat(&f).unwrap().st_flags.into());
assert_eq!(commanded, changed);
}

98
vendor/nix/test/sys/test_statfs.rs vendored Normal file
View File

@@ -0,0 +1,98 @@
use nix::sys::statfs::*;
use nix::sys::statvfs::*;
use std::fs::File;
use std::path::Path;
fn check_fstatfs(path: &str) {
if !Path::new(path).exists() {
return;
}
let vfs = statvfs(path.as_bytes()).unwrap();
let file = File::open(path).unwrap();
let fs = fstatfs(&file).unwrap();
assert_fs_equals(fs, vfs);
}
fn check_statfs(path: &str) {
if !Path::new(path).exists() {
return;
}
let vfs = statvfs(path.as_bytes()).unwrap();
let fs = statfs(path.as_bytes()).unwrap();
assert_fs_equals(fs, vfs);
}
fn check_fstatfs_strict(path: &str) {
if !Path::new(path).exists() {
return;
}
let vfs = statvfs(path.as_bytes());
let file = File::open(path).unwrap();
let fs = fstatfs(&file);
assert_fs_equals_strict(fs.unwrap(), vfs.unwrap())
}
fn check_statfs_strict(path: &str) {
if !Path::new(path).exists() {
return;
}
let vfs = statvfs(path.as_bytes());
let fs = statfs(path.as_bytes());
assert_fs_equals_strict(fs.unwrap(), vfs.unwrap())
}
// The cast is not unnecessary on all platforms.
#[allow(clippy::unnecessary_cast)]
fn assert_fs_equals(fs: Statfs, vfs: Statvfs) {
assert_eq!(fs.blocks() as u64, vfs.blocks() as u64);
assert_eq!(fs.block_size() as u64, vfs.fragment_size() as u64);
}
#[test]
fn statfs_call() {
check_statfs("/tmp");
check_statfs("/dev");
check_statfs("/run");
check_statfs("/");
}
#[test]
fn fstatfs_call() {
check_fstatfs("/tmp");
check_fstatfs("/dev");
check_fstatfs("/run");
check_fstatfs("/");
}
// This test is ignored because files_free/blocks_free can change after statvfs call and before
// statfs call.
#[test]
#[ignore]
fn statfs_call_strict() {
check_statfs_strict("/tmp");
check_statfs_strict("/dev");
check_statfs_strict("/run");
check_statfs_strict("/");
}
// This test is ignored because files_free/blocks_free can change after statvfs call and before
// fstatfs call.
#[test]
#[ignore]
fn fstatfs_call_strict() {
check_fstatfs_strict("/tmp");
check_fstatfs_strict("/dev");
check_fstatfs_strict("/run");
check_fstatfs_strict("/");
}
// The cast is not unnecessary on all platforms.
#[allow(clippy::unnecessary_cast)]
fn assert_fs_equals_strict(fs: Statfs, vfs: Statvfs) {
assert_eq!(fs.files_free() as u64, vfs.files_free() as u64);
assert_eq!(fs.blocks_free() as u64, vfs.blocks_free() as u64);
assert_eq!(fs.blocks_available() as u64, vfs.blocks_available() as u64);
assert_eq!(fs.files() as u64, vfs.files() as u64);
assert_eq!(fs.blocks() as u64, vfs.blocks() as u64);
assert_eq!(fs.block_size() as u64, vfs.fragment_size() as u64);
}

13
vendor/nix/test/sys/test_statvfs.rs vendored Normal file
View File

@@ -0,0 +1,13 @@
use nix::sys::statvfs::*;
use std::fs::File;
#[test]
fn statvfs_call() {
statvfs(&b"/"[..]).unwrap();
}
#[test]
fn fstatvfs_call() {
let root = File::open("/").unwrap();
fstatvfs(&root).unwrap();
}

20
vendor/nix/test/sys/test_sysinfo.rs vendored Normal file
View File

@@ -0,0 +1,20 @@
use nix::sys::sysinfo::*;
#[test]
fn sysinfo_works() {
let info = sysinfo().unwrap();
let (l1, l5, l15) = info.load_average();
assert!(l1 >= 0.0);
assert!(l5 >= 0.0);
assert!(l15 >= 0.0);
info.uptime(); // just test Duration construction
assert!(
info.swap_free() <= info.swap_total(),
"more swap available than installed (free: {}, total: {})",
info.swap_free(),
info.swap_total()
);
}

117
vendor/nix/test/sys/test_termios.rs vendored Normal file
View File

@@ -0,0 +1,117 @@
use std::os::unix::io::AsFd;
use tempfile::tempfile;
use nix::errno::Errno;
use nix::fcntl;
use nix::pty::openpty;
use nix::sys::termios::{self, tcgetattr, BaudRate, LocalFlags, OutputFlags};
use nix::unistd::{read, write};
/// Helper function analogous to `std::io::Write::write_all`, but for `Fd`s
fn write_all<Fd: AsFd>(f: Fd, buf: &[u8]) {
let mut len = 0;
while len < buf.len() {
len += write(f.as_fd(), &buf[len..]).unwrap();
}
}
#[test]
fn test_baudrate_try_from() {
assert_eq!(Ok(BaudRate::B0), BaudRate::try_from(libc::B0));
#[cfg(not(target_os = "haiku"))]
BaudRate::try_from(999999999).expect_err("assertion failed");
#[cfg(target_os = "haiku")]
BaudRate::try_from(99).expect_err("assertion failed");
}
// Test tcgetattr on a terminal
#[test]
fn test_tcgetattr_pty() {
// openpty uses ptname(3) internally
let _m = crate::PTSNAME_MTX.lock();
let pty = openpty(None, None).expect("openpty failed");
termios::tcgetattr(&pty.slave).unwrap();
}
// Test tcgetattr on something that isn't a terminal
#[test]
fn test_tcgetattr_enotty() {
let file = tempfile().unwrap();
assert_eq!(termios::tcgetattr(&file).err(), Some(Errno::ENOTTY));
}
// Test modifying output flags
#[test]
fn test_output_flags() {
// openpty uses ptname(3) internally
let _m = crate::PTSNAME_MTX.lock();
// Open one pty to get attributes for the second one
let mut termios = {
let pty = openpty(None, None).expect("openpty failed");
tcgetattr(&pty.slave).expect("tcgetattr failed")
};
// Make sure postprocessing '\r' isn't specified by default or this test is useless.
assert!(!termios
.output_flags
.contains(OutputFlags::OPOST | OutputFlags::OCRNL));
// Specify that '\r' characters should be transformed to '\n'
// OPOST is specified to enable post-processing
termios
.output_flags
.insert(OutputFlags::OPOST | OutputFlags::OCRNL);
// Open a pty
let pty = openpty(None, &termios).unwrap();
// Write into the master
let string = "foofoofoo\r";
write_all(&pty.master, string.as_bytes());
// Read from the slave verifying that the output has been properly transformed
let mut buf = [0u8; 10];
crate::read_exact(&pty.slave, &mut buf);
let transformed_string = "foofoofoo\n";
assert_eq!(&buf, transformed_string.as_bytes());
}
// Test modifying local flags
#[test]
#[cfg(not(target_os = "solaris"))]
fn test_local_flags() {
// openpty uses ptname(3) internally
let _m = crate::PTSNAME_MTX.lock();
// Open one pty to get attributes for the second one
let mut termios = {
let pty = openpty(None, None).unwrap();
tcgetattr(&pty.slave).unwrap()
};
// Make sure echo is specified by default or this test is useless.
assert!(termios.local_flags.contains(LocalFlags::ECHO));
// Disable local echo
termios.local_flags.remove(LocalFlags::ECHO);
// Open a new pty with our modified termios settings
let pty = openpty(None, &termios).unwrap();
// Set the master is in nonblocking mode or reading will never return.
let flags = fcntl::fcntl(&pty.master, fcntl::F_GETFL).unwrap();
let new_flags =
fcntl::OFlag::from_bits_truncate(flags) | fcntl::OFlag::O_NONBLOCK;
fcntl::fcntl(pty.master.as_fd(), fcntl::F_SETFL(new_flags)).unwrap();
// Write into the master
let string = "foofoofoo\r";
write_all(&pty.master, string.as_bytes());
// Try to read from the master, which should not have anything as echoing was disabled.
let mut buf = [0u8; 10];
let read = read(&pty.master, &mut buf).unwrap_err();
assert_eq!(read, Errno::EAGAIN);
}

91
vendor/nix/test/sys/test_time.rs vendored Normal file
View File

@@ -0,0 +1,91 @@
use nix::sys::time::{TimeSpec, TimeVal, TimeValLike};
use std::time::Duration;
#[test]
pub fn test_timespec() {
assert_ne!(TimeSpec::seconds(1), TimeSpec::zero());
assert_eq!(
TimeSpec::seconds(1) + TimeSpec::seconds(2),
TimeSpec::seconds(3)
);
assert_eq!(
TimeSpec::minutes(3) + TimeSpec::seconds(2),
TimeSpec::seconds(182)
);
}
#[test]
pub fn test_timespec_from() {
let duration = Duration::new(123, 123_456_789);
let timespec = TimeSpec::nanoseconds(123_123_456_789);
assert_eq!(TimeSpec::from(duration), timespec);
assert_eq!(Duration::from(timespec), duration);
}
#[test]
pub fn test_timespec_neg() {
let a = TimeSpec::seconds(1) + TimeSpec::nanoseconds(123);
let b = TimeSpec::seconds(-1) + TimeSpec::nanoseconds(-123);
assert_eq!(a, -b);
}
#[test]
pub fn test_timespec_ord() {
assert_eq!(TimeSpec::seconds(1), TimeSpec::nanoseconds(1_000_000_000));
assert!(TimeSpec::seconds(1) < TimeSpec::nanoseconds(1_000_000_001));
assert!(TimeSpec::seconds(1) > TimeSpec::nanoseconds(999_999_999));
assert!(TimeSpec::seconds(-1) < TimeSpec::nanoseconds(-999_999_999));
assert!(TimeSpec::seconds(-1) > TimeSpec::nanoseconds(-1_000_000_001));
}
#[test]
pub fn test_timespec_fmt() {
assert_eq!(TimeSpec::zero().to_string(), "0 seconds");
assert_eq!(TimeSpec::seconds(42).to_string(), "42 seconds");
assert_eq!(TimeSpec::milliseconds(42).to_string(), "0.042 seconds");
assert_eq!(TimeSpec::microseconds(42).to_string(), "0.000042 seconds");
assert_eq!(TimeSpec::nanoseconds(42).to_string(), "0.000000042 seconds");
assert_eq!(TimeSpec::seconds(-86401).to_string(), "-86401 seconds");
}
#[test]
pub fn test_timeval() {
assert_ne!(TimeVal::seconds(1), TimeVal::zero());
assert_eq!(
TimeVal::seconds(1) + TimeVal::seconds(2),
TimeVal::seconds(3)
);
assert_eq!(
TimeVal::minutes(3) + TimeVal::seconds(2),
TimeVal::seconds(182)
);
}
#[test]
pub fn test_timeval_ord() {
assert_eq!(TimeVal::seconds(1), TimeVal::microseconds(1_000_000));
assert!(TimeVal::seconds(1) < TimeVal::microseconds(1_000_001));
assert!(TimeVal::seconds(1) > TimeVal::microseconds(999_999));
assert!(TimeVal::seconds(-1) < TimeVal::microseconds(-999_999));
assert!(TimeVal::seconds(-1) > TimeVal::microseconds(-1_000_001));
}
#[test]
pub fn test_timeval_neg() {
let a = TimeVal::seconds(1) + TimeVal::microseconds(123);
let b = TimeVal::seconds(-1) + TimeVal::microseconds(-123);
assert_eq!(a, -b);
}
#[test]
pub fn test_timeval_fmt() {
assert_eq!(TimeVal::zero().to_string(), "0 seconds");
assert_eq!(TimeVal::seconds(42).to_string(), "42 seconds");
assert_eq!(TimeVal::milliseconds(42).to_string(), "0.042 seconds");
assert_eq!(TimeVal::microseconds(42).to_string(), "0.000042 seconds");
assert_eq!(TimeVal::nanoseconds(1402).to_string(), "0.000001 seconds");
assert_eq!(TimeVal::seconds(-86401).to_string(), "-86401 seconds");
}

102
vendor/nix/test/sys/test_timer.rs vendored Normal file
View File

@@ -0,0 +1,102 @@
use nix::sys::signal::{
sigaction, SaFlags, SigAction, SigEvent, SigHandler, SigSet, SigevNotify,
Signal,
};
use nix::sys::timer::{Expiration, Timer, TimerSetTimeFlags};
use nix::time::ClockId;
use std::convert::TryFrom;
use std::sync::atomic::{AtomicBool, Ordering};
use std::thread;
use std::time::{Duration, Instant};
const SIG: Signal = Signal::SIGALRM;
static ALARM_CALLED: AtomicBool = AtomicBool::new(false);
pub extern "C" fn handle_sigalarm(raw_signal: libc::c_int) {
let signal = Signal::try_from(raw_signal).unwrap();
if signal == SIG {
ALARM_CALLED.store(true, Ordering::Release);
}
}
#[test]
fn alarm_fires() {
// Avoid interfering with other signal using tests by taking a mutex shared
// among other tests in this crate.
let _m = crate::SIGNAL_MTX.lock();
const TIMER_PERIOD: Duration = Duration::from_millis(100);
//
// Setup
//
// Create a handler for the test signal, `SIG`. The handler is responsible
// for flipping `ALARM_CALLED`.
let handler = SigHandler::Handler(handle_sigalarm);
let signal_action =
SigAction::new(handler, SaFlags::SA_RESTART, SigSet::empty());
let old_handler = unsafe {
sigaction(SIG, &signal_action)
.expect("unable to set signal handler for alarm")
};
// Create the timer. We use the monotonic clock here, though any would do
// really. The timer is set to fire every 250 milliseconds with no delay for
// the initial firing.
let clockid = ClockId::CLOCK_MONOTONIC;
let sigevent = SigEvent::new(SigevNotify::SigevSignal {
signal: SIG,
si_value: 0,
});
let mut timer =
Timer::new(clockid, sigevent).expect("failed to create timer");
let expiration = Expiration::Interval(TIMER_PERIOD.into());
let flags = TimerSetTimeFlags::empty();
timer.set(expiration, flags).expect("could not set timer");
//
// Test
//
// Determine that there's still an expiration tracked by the
// timer. Depending on when this runs either an `Expiration::Interval` or
// `Expiration::IntervalDelayed` will be present. That is, if the timer has
// not fired yet we'll get our original `expiration`, else the one that
// represents a delay to the next expiration. We're only interested in the
// timer still being extant.
match timer.get() {
Ok(Some(exp)) => assert!(matches!(
exp,
Expiration::Interval(..) | Expiration::IntervalDelayed(..)
)),
_ => panic!("timer lost its expiration"),
}
// Wait for 2 firings of the alarm before checking that it has fired and
// been handled at least the once. If we wait for 3 seconds and the handler
// is never called something has gone sideways and the test fails.
let starttime = Instant::now();
loop {
thread::sleep(2 * TIMER_PERIOD);
if ALARM_CALLED.load(Ordering::Acquire) {
break;
}
if starttime.elapsed() > Duration::from_secs(3) {
panic!("Timeout waiting for SIGALRM");
}
}
// Cleanup:
// 1) deregister the OS's timer.
// 2) Wait for a full timer period, since POSIX does not require that
// disabling the timer will clear pending signals, and on NetBSD at least
// it does not.
// 2) Replace the old signal handler now that we've completed the test. If
// the test fails this process panics, so the fact we might not get here
// is okay.
drop(timer);
thread::sleep(TIMER_PERIOD);
unsafe {
sigaction(SIG, &old_handler).expect("unable to reset signal handler");
}
}

69
vendor/nix/test/sys/test_timerfd.rs vendored Normal file
View File

@@ -0,0 +1,69 @@
use nix::sys::time::{TimeSpec, TimeValLike};
use nix::sys::timerfd::{
ClockId, Expiration, TimerFd, TimerFlags, TimerSetTimeFlags,
};
use std::time::Instant;
#[test]
pub fn test_timerfd_oneshot() {
let timer =
TimerFd::new(ClockId::CLOCK_MONOTONIC, TimerFlags::empty()).unwrap();
let before = Instant::now();
timer
.set(
Expiration::OneShot(TimeSpec::seconds(1)),
TimerSetTimeFlags::empty(),
)
.unwrap();
timer.wait().unwrap();
let millis = before.elapsed().as_millis();
assert!(millis > 900);
}
#[test]
pub fn test_timerfd_interval() {
let timer =
TimerFd::new(ClockId::CLOCK_MONOTONIC, TimerFlags::empty()).unwrap();
let before = Instant::now();
timer
.set(
Expiration::IntervalDelayed(
TimeSpec::seconds(1),
TimeSpec::seconds(2),
),
TimerSetTimeFlags::empty(),
)
.unwrap();
timer.wait().unwrap();
let start_delay = before.elapsed().as_millis();
assert!(start_delay > 900);
timer.wait().unwrap();
let interval_delay = before.elapsed().as_millis();
assert!(interval_delay > 2900);
}
#[test]
pub fn test_timerfd_unset() {
let timer =
TimerFd::new(ClockId::CLOCK_MONOTONIC, TimerFlags::empty()).unwrap();
timer
.set(
Expiration::OneShot(TimeSpec::seconds(1)),
TimerSetTimeFlags::empty(),
)
.unwrap();
timer.unset().unwrap();
assert!(timer.get().unwrap().is_none());
}

277
vendor/nix/test/sys/test_uio.rs vendored Normal file
View File

@@ -0,0 +1,277 @@
use nix::sys::uio::*;
use nix::unistd::*;
use rand::distr::Alphanumeric;
use rand::{rng, Rng};
use std::fs::OpenOptions;
use std::io::IoSlice;
use std::{cmp, iter};
#[cfg(not(target_os = "redox"))]
use std::io::IoSliceMut;
use tempfile::tempdir;
#[cfg(not(target_os = "redox"))]
use tempfile::tempfile;
#[test]
// On Solaris sometimes wrtitev() returns EINVAL.
#[cfg(not(target_os = "solaris"))]
fn test_writev() {
let mut to_write = Vec::with_capacity(16 * 128);
for _ in 0..16 {
let s: String = rng()
.sample_iter(&Alphanumeric)
.map(char::from)
.take(128)
.collect();
let b = s.as_bytes();
to_write.extend(b.iter().cloned());
}
// Allocate and fill iovecs
let mut iovecs = Vec::new();
let mut consumed = 0;
while consumed < to_write.len() {
let left = to_write.len() - consumed;
let slice_len = if left <= 64 {
left
} else {
rng().random_range(64..cmp::min(256, left))
};
let b = &to_write[consumed..consumed + slice_len];
iovecs.push(IoSlice::new(b));
consumed += slice_len;
}
let (reader, writer) = pipe().expect("Couldn't create pipe");
// FileDesc will close its filedesc (reader).
let mut read_buf: Vec<u8> = iter::repeat(0u8).take(128 * 16).collect();
// Blocking io, should write all data.
let write_res = writev(&writer, &iovecs);
let written = write_res.expect("couldn't write");
// Check whether we written all data
assert_eq!(to_write.len(), written);
let read_res = read(&reader, &mut read_buf[..]);
let read = read_res.expect("couldn't read");
// Check we have read as much as we written
assert_eq!(read, written);
// Check equality of written and read data
assert_eq!(&to_write, &read_buf);
}
#[test]
#[cfg(not(target_os = "redox"))]
fn test_readv() {
let s: String = rng()
.sample_iter(&Alphanumeric)
.map(char::from)
.take(128)
.collect();
let to_write = s.as_bytes().to_vec();
let mut storage = Vec::new();
let mut allocated = 0;
while allocated < to_write.len() {
let left = to_write.len() - allocated;
let vec_len = if left <= 64 {
left
} else {
rng().random_range(64..cmp::min(256, left))
};
let v: Vec<u8> = iter::repeat(0u8).take(vec_len).collect();
storage.push(v);
allocated += vec_len;
}
let mut iovecs = Vec::with_capacity(storage.len());
for v in &mut storage {
iovecs.push(IoSliceMut::new(&mut v[..]));
}
let (reader, writer) = pipe().expect("couldn't create pipe");
// Blocking io, should write all data.
write(writer, &to_write).expect("write failed");
let read = readv(&reader, &mut iovecs[..]).expect("read failed");
// Check whether we've read all data
assert_eq!(to_write.len(), read);
// Cccumulate data from iovecs
let mut read_buf = Vec::with_capacity(to_write.len());
for iovec in &iovecs {
read_buf.extend(iovec.iter().cloned());
}
// Check whether iovecs contain all written data
assert_eq!(read_buf.len(), to_write.len());
// Check equality of written and read data
assert_eq!(&read_buf, &to_write);
}
#[test]
#[cfg(not(target_os = "redox"))]
fn test_pwrite() {
use std::io::Read;
let mut file = tempfile().unwrap();
let buf = [1u8; 8];
assert_eq!(Ok(8), pwrite(&file, &buf, 8));
let mut file_content = Vec::new();
file.read_to_end(&mut file_content).unwrap();
let mut expected = vec![0u8; 8];
expected.extend(vec![1; 8]);
assert_eq!(file_content, expected);
}
#[test]
fn test_pread() {
use std::io::Write;
let tempdir = tempdir().unwrap();
let path = tempdir.path().join("pread_test_file");
let mut file = OpenOptions::new()
.write(true)
.read(true)
.create(true)
.truncate(true)
.open(path)
.unwrap();
let file_content: Vec<u8> = (0..64).collect();
file.write_all(&file_content).unwrap();
let mut buf = [0u8; 16];
assert_eq!(Ok(16), pread(&file, &mut buf, 16));
let expected: Vec<_> = (16..32).collect();
assert_eq!(&buf[..], &expected[..]);
}
#[test]
#[cfg(not(any(
target_os = "redox",
target_os = "haiku",
target_os = "solaris",
target_os = "cygwin"
)))]
fn test_pwritev() {
use std::io::Read;
let to_write: Vec<u8> = (0..128).collect();
let expected: Vec<u8> = [vec![0; 100], to_write.clone()].concat();
let iovecs = [
IoSlice::new(&to_write[0..17]),
IoSlice::new(&to_write[17..64]),
IoSlice::new(&to_write[64..128]),
];
let tempdir = tempdir().unwrap();
// pwritev them into a temporary file
let path = tempdir.path().join("pwritev_test_file");
let mut file = OpenOptions::new()
.write(true)
.read(true)
.create(true)
.truncate(true)
.open(path)
.unwrap();
let written = pwritev(&file, &iovecs, 100).ok().unwrap();
assert_eq!(written, to_write.len());
// Read the data back and make sure it matches
let mut contents = Vec::new();
file.read_to_end(&mut contents).unwrap();
assert_eq!(contents, expected);
}
#[test]
#[cfg(not(any(
target_os = "redox",
target_os = "haiku",
target_os = "solaris",
target_os = "cygwin"
)))]
fn test_preadv() {
use std::io::Write;
let to_write: Vec<u8> = (0..200).collect();
let expected: Vec<u8> = (100..200).collect();
let tempdir = tempdir().unwrap();
let path = tempdir.path().join("preadv_test_file");
let mut file = OpenOptions::new()
.read(true)
.write(true)
.create(true)
.truncate(true)
.open(path)
.unwrap();
file.write_all(&to_write).unwrap();
let mut buffers: Vec<Vec<u8>> = vec![vec![0; 24], vec![0; 1], vec![0; 75]];
{
// Borrow the buffers into IoVecs and preadv into them
let mut iovecs: Vec<_> = buffers
.iter_mut()
.map(|buf| IoSliceMut::new(&mut buf[..]))
.collect();
assert_eq!(Ok(100), preadv(&file, &mut iovecs, 100));
}
let all = buffers.concat();
assert_eq!(all, expected);
}
#[test]
#[cfg(all(target_os = "linux", not(target_env = "uclibc")))]
// uclibc doesn't implement process_vm_readv
// qemu-user doesn't implement process_vm_readv/writev on most arches
#[cfg_attr(qemu, ignore)]
fn test_process_vm_readv() {
use crate::*;
use nix::sys::signal::*;
use nix::sys::wait::*;
use nix::unistd::ForkResult::*;
require_capability!("test_process_vm_readv", CAP_SYS_PTRACE);
let _m = crate::FORK_MTX.lock();
// Pre-allocate memory in the child, since allocation isn't safe
// post-fork (~= async-signal-safe)
let mut vector = vec![1u8, 2, 3, 4, 5];
let (r, w) = pipe().unwrap();
match unsafe { fork() }.expect("Error: Fork Failed") {
Parent { child } => {
drop(w);
// wait for child
read(&r, &mut [0u8]).unwrap();
drop(r);
let ptr = vector.as_ptr() as usize;
let remote_iov = RemoteIoVec { base: ptr, len: 5 };
let mut buf = vec![0u8; 5];
let ret = process_vm_readv(
child,
&mut [IoSliceMut::new(&mut buf)],
&[remote_iov],
);
kill(child, SIGTERM).unwrap();
waitpid(child, None).unwrap();
assert_eq!(Ok(5), ret);
assert_eq!(20u8, buf.iter().sum());
}
Child => {
drop(r);
for i in &mut vector {
*i += 1;
}
let _ = write(w, b"\0");
loop {
pause();
}
}
}
}

17
vendor/nix/test/sys/test_utsname.rs vendored Normal file
View File

@@ -0,0 +1,17 @@
#[cfg(target_os = "linux")]
#[test]
pub fn test_uname_linux() {
assert_eq!(nix::sys::utsname::uname().unwrap().sysname(), "Linux");
}
#[cfg(apple_targets)]
#[test]
pub fn test_uname_darwin() {
assert_eq!(nix::sys::utsname::uname().unwrap().sysname(), "Darwin");
}
#[cfg(target_os = "freebsd")]
#[test]
pub fn test_uname_freebsd() {
assert_eq!(nix::sys::utsname::uname().unwrap().sysname(), "FreeBSD");
}

267
vendor/nix/test/sys/test_wait.rs vendored Normal file
View File

@@ -0,0 +1,267 @@
use libc::_exit;
use nix::errno::Errno;
use nix::sys::signal::*;
use nix::sys::wait::*;
use nix::unistd::ForkResult::*;
use nix::unistd::*;
#[test]
#[cfg(not(any(target_os = "redox", target_os = "haiku")))]
fn test_wait_signal() {
let _m = crate::FORK_MTX.lock();
// Safe: The child only calls `pause` and/or `_exit`, which are async-signal-safe.
match unsafe { fork() }.expect("Error: Fork Failed") {
Child => {
pause();
unsafe { _exit(123) }
}
Parent { child } => {
kill(child, Some(SIGKILL)).expect("Error: Kill Failed");
assert_eq!(
waitpid(child, None),
Ok(WaitStatus::Signaled(child, SIGKILL, false))
);
}
}
}
#[test]
#[cfg(any(
target_os = "android",
target_os = "freebsd",
//target_os = "haiku",
all(target_os = "linux", not(target_env = "uclibc")),
))]
#[cfg(not(any(
target_arch = "mips",
target_arch = "mips32r6",
target_arch = "mips64",
target_arch = "mips64r6"
)))]
fn test_waitid_signal() {
let _m = crate::FORK_MTX.lock();
// Safe: The child only calls `pause` and/or `_exit`, which are async-signal-safe.
match unsafe { fork() }.expect("Error: Fork Failed") {
Child => {
pause();
unsafe { _exit(123) }
}
Parent { child } => {
kill(child, Some(SIGKILL)).expect("Error: Kill Failed");
assert_eq!(
waitid(Id::Pid(child), WaitPidFlag::WEXITED),
Ok(WaitStatus::Signaled(child, SIGKILL, false)),
);
}
}
}
#[test]
fn test_wait_exit() {
let _m = crate::FORK_MTX.lock();
// Safe: Child only calls `_exit`, which is async-signal-safe.
match unsafe { fork() }.expect("Error: Fork Failed") {
Child => unsafe {
_exit(12);
},
Parent { child } => {
assert_eq!(waitpid(child, None), Ok(WaitStatus::Exited(child, 12)));
}
}
}
#[cfg(not(target_os = "haiku"))]
#[test]
#[cfg(any(
target_os = "android",
target_os = "freebsd",
target_os = "haiku",
all(target_os = "linux", not(target_env = "uclibc")),
))]
#[cfg(not(any(
target_arch = "mips",
target_arch = "mips32r6",
target_arch = "mips64",
target_arch = "mips64r6"
)))]
fn test_waitid_exit() {
let _m = crate::FORK_MTX.lock();
// Safe: Child only calls `_exit`, which is async-signal-safe.
match unsafe { fork() }.expect("Error: Fork Failed") {
Child => unsafe {
_exit(12);
},
Parent { child } => {
assert_eq!(
waitid(Id::Pid(child), WaitPidFlag::WEXITED),
Ok(WaitStatus::Exited(child, 12)),
);
}
}
}
#[test]
fn test_waitstatus_from_raw() {
let pid = Pid::from_raw(1);
assert_eq!(
WaitStatus::from_raw(pid, 0x0002),
Ok(WaitStatus::Signaled(pid, Signal::SIGINT, false))
);
assert_eq!(
WaitStatus::from_raw(pid, 0x0200),
Ok(WaitStatus::Exited(pid, 2))
);
assert_eq!(WaitStatus::from_raw(pid, 0x7f7f), Err(Errno::EINVAL));
}
#[test]
fn test_waitstatus_pid() {
let _m = crate::FORK_MTX.lock();
match unsafe { fork() }.unwrap() {
Child => unsafe { _exit(0) },
Parent { child } => {
let status = waitpid(child, None).unwrap();
assert_eq!(status.pid(), Some(child));
}
}
}
#[test]
#[cfg(any(
target_os = "android",
target_os = "freebsd",
target_os = "haiku",
all(target_os = "linux", not(target_env = "uclibc")),
))]
fn test_waitid_pid() {
let _m = crate::FORK_MTX.lock();
match unsafe { fork() }.unwrap() {
Child => unsafe { _exit(0) },
Parent { child } => {
let status = waitid(Id::Pid(child), WaitPidFlag::WEXITED).unwrap();
assert_eq!(status.pid(), Some(child));
}
}
}
#[cfg(linux_android)]
// FIXME: qemu-user doesn't implement ptrace on most arches
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
mod ptrace {
use crate::*;
use libc::_exit;
use nix::sys::ptrace::{self, Event, Options};
use nix::sys::signal::*;
use nix::sys::wait::*;
use nix::unistd::ForkResult::*;
use nix::unistd::*;
fn ptrace_child() -> ! {
ptrace::traceme().unwrap();
// As recommended by ptrace(2), raise SIGTRAP to pause the child
// until the parent is ready to continue
raise(SIGTRAP).unwrap();
unsafe { _exit(0) }
}
fn ptrace_wait_parent(child: Pid) {
// Wait for the raised SIGTRAP
assert_eq!(
waitpid(child, None),
Ok(WaitStatus::Stopped(child, SIGTRAP))
);
// We want to test a syscall stop and a PTRACE_EVENT stop
ptrace::setoptions(
child,
Options::PTRACE_O_TRACESYSGOOD | Options::PTRACE_O_TRACEEXIT,
)
.expect("setoptions failed");
// First, stop on the next system call, which will be exit()
ptrace::syscall(child, None).expect("syscall failed");
assert_eq!(waitpid(child, None), Ok(WaitStatus::PtraceSyscall(child)));
// Then get the ptrace event for the process exiting
ptrace::cont(child, None).expect("cont failed");
assert_eq!(
waitpid(child, None),
Ok(WaitStatus::PtraceEvent(
child,
SIGTRAP,
Event::PTRACE_EVENT_EXIT as i32
))
);
// Finally get the normal wait() result, now that the process has exited
ptrace::cont(child, None).expect("cont failed");
assert_eq!(waitpid(child, None), Ok(WaitStatus::Exited(child, 0)));
}
#[cfg(not(target_env = "uclibc"))]
fn ptrace_waitid_parent(child: Pid) {
// Wait for the raised SIGTRAP
//
// Unlike waitpid(), waitid() can distinguish trap events from regular
// stop events, so unlike ptrace_wait_parent(), we get a PtraceEvent here
assert_eq!(
waitid(Id::Pid(child), WaitPidFlag::WEXITED),
Ok(WaitStatus::PtraceEvent(child, SIGTRAP, 0)),
);
// We want to test a syscall stop and a PTRACE_EVENT stop
ptrace::setoptions(
child,
Options::PTRACE_O_TRACESYSGOOD | Options::PTRACE_O_TRACEEXIT,
)
.expect("setopts failed");
// First, stop on the next system call, which will be exit()
ptrace::syscall(child, None).expect("syscall failed");
assert_eq!(
waitid(Id::Pid(child), WaitPidFlag::WEXITED),
Ok(WaitStatus::PtraceSyscall(child)),
);
// Then get the ptrace event for the process exiting
ptrace::cont(child, None).expect("cont failed");
assert_eq!(
waitid(Id::Pid(child), WaitPidFlag::WEXITED),
Ok(WaitStatus::PtraceEvent(
child,
SIGTRAP,
Event::PTRACE_EVENT_EXIT as i32
)),
);
// Finally get the normal wait() result, now that the process has exited
ptrace::cont(child, None).expect("cont failed");
assert_eq!(
waitid(Id::Pid(child), WaitPidFlag::WEXITED),
Ok(WaitStatus::Exited(child, 0)),
);
}
#[test]
fn test_wait_ptrace() {
require_capability!("test_wait_ptrace", CAP_SYS_PTRACE);
let _m = crate::FORK_MTX.lock();
match unsafe { fork() }.expect("Error: Fork Failed") {
Child => ptrace_child(),
Parent { child } => ptrace_wait_parent(child),
}
}
#[test]
#[cfg(not(target_env = "uclibc"))]
fn test_waitid_ptrace() {
require_capability!("test_waitid_ptrace", CAP_SYS_PTRACE);
let _m = crate::FORK_MTX.lock();
match unsafe { fork() }.expect("Error: Fork Failed") {
Child => ptrace_child(),
Parent { child } => ptrace_waitid_parent(child),
}
}
}

111
vendor/nix/test/test.rs vendored Normal file
View File

@@ -0,0 +1,111 @@
#[macro_use]
extern crate cfg_if;
#[cfg_attr(not(any(target_os = "redox")), macro_use)]
extern crate nix;
#[macro_use]
mod common;
mod mount;
mod sys;
#[cfg(not(target_os = "redox"))]
mod test_dir;
mod test_errno;
mod test_fcntl;
#[cfg(linux_android)]
mod test_kmod;
#[cfg(any(
freebsdlike,
all(target_os = "linux", not(target_env = "ohos")),
target_os = "netbsd"
))]
mod test_mq;
#[cfg(not(target_os = "redox"))]
mod test_net;
mod test_nix_path;
mod test_poll;
#[cfg(not(any(
target_os = "redox",
target_os = "fuchsia",
target_os = "haiku"
)))]
mod test_pty;
#[cfg(any(
linux_android,
target_os = "dragonfly",
all(target_os = "freebsd", fbsd14),
))]
mod test_sched;
#[cfg(any(linux_android, freebsdlike, apple_targets, solarish))]
mod test_sendfile;
#[cfg(any(
target_os = "freebsd",
target_os = "haiku",
target_os = "linux",
target_os = "netbsd",
apple_targets
))]
mod test_spawn;
mod test_syslog;
mod test_time;
mod test_unistd;
use nix::unistd::{chdir, getcwd, read};
use parking_lot::{Mutex, RwLock, RwLockWriteGuard};
use std::os::unix::io::AsFd;
use std::path::PathBuf;
/// Helper function analogous to `std::io::Read::read_exact`, but for `Fd`s
fn read_exact<Fd: AsFd>(f: Fd, buf: &mut [u8]) {
let mut len = 0;
while len < buf.len() {
// get_mut would be better than split_at_mut, but it requires nightly
let (_, remaining) = buf.split_at_mut(len);
len += read(&f, remaining).unwrap();
}
}
/// Any test that creates child processes or can be affected by child processes
/// must grab this mutex, regardless of what it does with those children.
///
/// It must hold the mutex until the child processes are waited upon.
pub static FORK_MTX: Mutex<()> = Mutex::new(());
/// Any test that changes the process's current working directory must grab
/// the RwLock exclusively. Any process that cares about the current
/// working directory must grab it shared.
pub static CWD_LOCK: RwLock<()> = RwLock::new(());
/// Any test that changes the process's supplementary groups must grab this
/// mutex
pub static GROUPS_MTX: Mutex<()> = Mutex::new(());
/// Any tests that loads or unloads kernel modules must grab this mutex
pub static KMOD_MTX: Mutex<()> = Mutex::new(());
/// Any test that calls ptsname(3) must grab this mutex.
pub static PTSNAME_MTX: Mutex<()> = Mutex::new(());
/// Any test that alters signal handling must grab this mutex.
pub static SIGNAL_MTX: Mutex<()> = Mutex::new(());
/// RAII object that restores a test's original directory on drop
struct DirRestore<'a> {
d: PathBuf,
_g: RwLockWriteGuard<'a, ()>,
}
impl DirRestore<'_> {
fn new() -> Self {
let guard = crate::CWD_LOCK.write();
DirRestore {
_g: guard,
d: getcwd().unwrap(),
}
}
}
impl Drop for DirRestore<'_> {
fn drop(&mut self) {
let r = chdir(&self.d);
if std::thread::panicking() {
r.unwrap();
}
}
}

9
vendor/nix/test/test_clearenv.rs vendored Normal file
View File

@@ -0,0 +1,9 @@
use std::env;
#[test]
fn clearenv() {
env::set_var("FOO", "BAR");
unsafe { nix::env::clearenv() }.unwrap();
assert_eq!(env::var("FOO").unwrap_err(), env::VarError::NotPresent);
assert_eq!(env::vars().count(), 0);
}

58
vendor/nix/test/test_dir.rs vendored Normal file
View File

@@ -0,0 +1,58 @@
use nix::dir::{Dir, Type};
use nix::fcntl::OFlag;
use nix::sys::stat::Mode;
use std::fs::File;
use tempfile::tempdir;
#[cfg(test)]
fn flags() -> OFlag {
#[cfg(solarish)]
let f = OFlag::O_RDONLY | OFlag::O_CLOEXEC;
#[cfg(not(solarish))]
let f = OFlag::O_RDONLY | OFlag::O_CLOEXEC | OFlag::O_DIRECTORY;
f
}
#[test]
fn read() {
let tmp = tempdir().unwrap();
File::create(tmp.path().join("foo")).unwrap();
std::os::unix::fs::symlink("foo", tmp.path().join("bar")).unwrap();
let mut dir = Dir::open(tmp.path(), flags(), Mode::empty()).unwrap();
let mut entries: Vec<_> = dir.iter().map(|e| e.unwrap()).collect();
entries.sort_by(|a, b| a.file_name().cmp(b.file_name()));
let entry_names: Vec<_> = entries
.iter()
.map(|e| e.file_name().to_str().unwrap().to_owned())
.collect();
assert_eq!(&entry_names[..], &[".", "..", "bar", "foo"]);
// Check file types. The system is allowed to return DT_UNKNOWN (aka None here) but if it does
// return a type, ensure it's correct.
assert!(&[Some(Type::Directory), None].contains(&entries[0].file_type())); // .: dir
assert!(&[Some(Type::Directory), None].contains(&entries[1].file_type())); // ..: dir
assert!(&[Some(Type::Symlink), None].contains(&entries[2].file_type())); // bar: symlink
assert!(&[Some(Type::File), None].contains(&entries[3].file_type())); // foo: regular file
}
#[test]
fn rewind() {
let tmp = tempdir().unwrap();
let mut dir = Dir::open(tmp.path(), flags(), Mode::empty()).unwrap();
let entries1: Vec<_> = dir
.iter()
.map(|e| e.unwrap().file_name().to_owned())
.collect();
let entries2: Vec<_> = dir
.iter()
.map(|e| e.unwrap().file_name().to_owned())
.collect();
let entries3: Vec<_> = dir
.into_iter()
.map(|e| e.unwrap().file_name().to_owned())
.collect();
assert_eq!(entries1, entries2);
assert_eq!(entries2, entries3);
}

16
vendor/nix/test/test_errno.rs vendored Normal file
View File

@@ -0,0 +1,16 @@
use nix::errno::Errno;
#[test]
fn errno_set_and_read() {
Errno::ENFILE.set();
assert_eq!(Errno::last(), Errno::ENFILE);
}
#[test]
fn errno_set_and_clear() {
Errno::ENFILE.set();
assert_eq!(Errno::last(), Errno::ENFILE);
Errno::clear();
assert_eq!(Errno::last(), Errno::from_raw(0));
}

820
vendor/nix/test/test_fcntl.rs vendored Normal file
View File

@@ -0,0 +1,820 @@
#[cfg(not(target_os = "redox"))]
use nix::errno::*;
#[cfg(not(target_os = "redox"))]
use nix::fcntl::{open, readlink, OFlag};
#[cfg(not(target_os = "redox"))]
use nix::fcntl::{openat, readlinkat, renameat};
#[cfg(target_os = "linux")]
use nix::fcntl::{openat2, OpenHow, ResolveFlag};
#[cfg(all(
target_os = "linux",
target_env = "gnu",
any(
target_arch = "x86_64",
target_arch = "powerpc",
target_arch = "s390x"
)
))]
use nix::fcntl::{renameat2, RenameFlags};
#[cfg(not(target_os = "redox"))]
use nix::sys::stat::Mode;
#[cfg(not(target_os = "redox"))]
use nix::unistd::read;
#[cfg(not(target_os = "redox"))]
use std::fs::File;
#[cfg(not(target_os = "redox"))]
use std::io::prelude::*;
#[cfg(not(target_os = "redox"))]
use std::os::unix::fs;
#[cfg(not(target_os = "redox"))]
use tempfile::NamedTempFile;
#[test]
#[cfg(not(target_os = "redox"))]
// QEMU does not handle openat well enough to satisfy this test
// https://gitlab.com/qemu-project/qemu/-/issues/829
#[cfg_attr(qemu, ignore)]
fn test_openat() {
const CONTENTS: &[u8] = b"abcd";
let mut tmp = NamedTempFile::new().unwrap();
tmp.write_all(CONTENTS).unwrap();
let dirfd =
open(tmp.path().parent().unwrap(), OFlag::empty(), Mode::empty())
.unwrap();
let fd = openat(
dirfd,
tmp.path().file_name().unwrap(),
OFlag::O_RDONLY,
Mode::empty(),
)
.unwrap();
let mut buf = [0u8; 1024];
assert_eq!(4, read(&fd, &mut buf).unwrap());
assert_eq!(CONTENTS, &buf[0..4]);
}
#[test]
#[cfg(target_os = "linux")]
// QEMU does not handle openat well enough to satisfy this test
// https://gitlab.com/qemu-project/qemu/-/issues/829
#[cfg_attr(qemu, ignore)]
fn test_openat2() {
const CONTENTS: &[u8] = b"abcd";
let mut tmp = NamedTempFile::new().unwrap();
tmp.write_all(CONTENTS).unwrap();
let dirfd =
open(tmp.path().parent().unwrap(), OFlag::empty(), Mode::empty())
.unwrap();
let fd = openat2(
dirfd,
tmp.path().file_name().unwrap(),
OpenHow::new()
.flags(OFlag::O_RDONLY)
.mode(Mode::empty())
.resolve(ResolveFlag::RESOLVE_BENEATH),
)
.unwrap();
let mut buf = [0u8; 1024];
assert_eq!(4, read(&fd, &mut buf).unwrap());
assert_eq!(CONTENTS, &buf[0..4]);
}
#[test]
#[cfg(target_os = "linux")]
// QEMU does not handle openat well enough to satisfy this test
// https://gitlab.com/qemu-project/qemu/-/issues/829
#[cfg_attr(qemu, ignore)]
fn test_openat2_forbidden() {
let mut tmp = NamedTempFile::new().unwrap();
tmp.write_all(b"let me out").unwrap();
let dirfd =
open(tmp.path().parent().unwrap(), OFlag::empty(), Mode::empty())
.unwrap();
let escape_attempt =
tmp.path().parent().unwrap().join("../../../hello.txt");
let res = openat2(
dirfd,
&escape_attempt,
OpenHow::new()
.flags(OFlag::O_RDONLY)
.resolve(ResolveFlag::RESOLVE_BENEATH),
);
assert_eq!(res.unwrap_err(), Errno::EXDEV);
}
#[test]
#[cfg(not(target_os = "redox"))]
fn test_renameat() {
let old_dir = tempfile::tempdir().unwrap();
let old_dirfd =
open(old_dir.path(), OFlag::empty(), Mode::empty()).unwrap();
let old_path = old_dir.path().join("old");
File::create(old_path).unwrap();
let new_dir = tempfile::tempdir().unwrap();
let new_dirfd =
open(new_dir.path(), OFlag::empty(), Mode::empty()).unwrap();
renameat(&old_dirfd, "old", &new_dirfd, "new").unwrap();
assert_eq!(
renameat(&old_dirfd, "old", &new_dirfd, "new").unwrap_err(),
Errno::ENOENT
);
assert!(new_dir.path().join("new").exists());
}
#[test]
#[cfg(all(
target_os = "linux",
target_env = "gnu",
any(
target_arch = "x86_64",
target_arch = "powerpc",
target_arch = "s390x"
)
))]
fn test_renameat2_behaves_like_renameat_with_no_flags() {
let old_dir = tempfile::tempdir().unwrap();
let old_dirfd =
open(old_dir.path(), OFlag::empty(), Mode::empty()).unwrap();
let old_path = old_dir.path().join("old");
File::create(old_path).unwrap();
let new_dir = tempfile::tempdir().unwrap();
let new_dirfd =
open(new_dir.path(), OFlag::empty(), Mode::empty()).unwrap();
renameat2(&old_dirfd, "old", &new_dirfd, "new", RenameFlags::empty())
.unwrap();
assert_eq!(
renameat2(&old_dirfd, "old", &new_dirfd, "new", RenameFlags::empty())
.unwrap_err(),
Errno::ENOENT
);
assert!(new_dir.path().join("new").exists());
}
#[test]
#[cfg(all(
target_os = "linux",
target_env = "gnu",
any(
target_arch = "x86_64",
target_arch = "powerpc",
target_arch = "s390x"
)
))]
fn test_renameat2_exchange() {
let old_dir = tempfile::tempdir().unwrap();
let old_dirfd =
open(old_dir.path(), OFlag::empty(), Mode::empty()).unwrap();
let old_path = old_dir.path().join("old");
{
let mut old_f = File::create(&old_path).unwrap();
old_f.write_all(b"old").unwrap();
}
let new_dir = tempfile::tempdir().unwrap();
let new_dirfd =
open(new_dir.path(), OFlag::empty(), Mode::empty()).unwrap();
let new_path = new_dir.path().join("new");
{
let mut new_f = File::create(&new_path).unwrap();
new_f.write_all(b"new").unwrap();
}
renameat2(
&old_dirfd,
"old",
&new_dirfd,
"new",
RenameFlags::RENAME_EXCHANGE,
)
.unwrap();
let mut buf = String::new();
let mut new_f = File::open(&new_path).unwrap();
new_f.read_to_string(&mut buf).unwrap();
assert_eq!(buf, "old");
buf = "".to_string();
let mut old_f = File::open(&old_path).unwrap();
old_f.read_to_string(&mut buf).unwrap();
assert_eq!(buf, "new");
}
#[test]
#[cfg(all(
target_os = "linux",
target_env = "gnu",
any(
target_arch = "x86_64",
target_arch = "powerpc",
target_arch = "s390x"
)
))]
fn test_renameat2_noreplace() {
let old_dir = tempfile::tempdir().unwrap();
let old_dirfd =
open(old_dir.path(), OFlag::empty(), Mode::empty()).unwrap();
let old_path = old_dir.path().join("old");
File::create(old_path).unwrap();
let new_dir = tempfile::tempdir().unwrap();
let new_dirfd =
open(new_dir.path(), OFlag::empty(), Mode::empty()).unwrap();
let new_path = new_dir.path().join("new");
File::create(new_path).unwrap();
assert_eq!(
renameat2(
&old_dirfd,
"old",
&new_dirfd,
"new",
RenameFlags::RENAME_NOREPLACE
)
.unwrap_err(),
Errno::EEXIST
);
assert!(new_dir.path().join("new").exists());
assert!(old_dir.path().join("old").exists());
}
#[test]
#[cfg(not(target_os = "redox"))]
fn test_readlink() {
let tempdir = tempfile::tempdir().unwrap();
let src = tempdir.path().join("a");
let dst = tempdir.path().join("b");
println!("a: {:?}, b: {:?}", &src, &dst);
fs::symlink(src.as_path(), dst.as_path()).unwrap();
let dirfd = open(tempdir.path(), OFlag::empty(), Mode::empty()).unwrap();
let expected_dir = src.to_str().unwrap();
assert_eq!(readlink(&dst).unwrap().to_str().unwrap(), expected_dir);
assert_eq!(
readlinkat(dirfd, "b").unwrap().to_str().unwrap(),
expected_dir
);
}
/// This test creates a temporary file containing the contents
/// 'foobarbaz' and uses the `copy_file_range` call to transfer
/// 3 bytes at offset 3 (`bar`) to another empty file at offset 0. The
/// resulting file is read and should contain the contents `bar`.
/// The from_offset should be updated by the call to reflect
/// the 3 bytes read (6).
#[cfg(any(
linux_android,
// Not available until FreeBSD 13.0
all(target_os = "freebsd", fbsd14),
))]
#[test]
// QEMU does not support copy_file_range. Skip under qemu
#[cfg_attr(qemu, ignore)]
fn test_copy_file_range() {
use nix::fcntl::copy_file_range;
const CONTENTS: &[u8] = b"foobarbaz";
let mut tmp1 = tempfile::tempfile().unwrap();
let mut tmp2 = tempfile::tempfile().unwrap();
tmp1.write_all(CONTENTS).unwrap();
tmp1.flush().unwrap();
let mut from_offset: i64 = 3;
copy_file_range(&tmp1, Some(&mut from_offset), &tmp2, None, 3).unwrap();
let mut res: String = String::new();
tmp2.rewind().unwrap();
tmp2.read_to_string(&mut res).unwrap();
assert_eq!(res, String::from("bar"));
assert_eq!(from_offset, 6);
}
#[cfg(linux_android)]
mod linux_android {
use libc::loff_t;
use std::io::prelude::*;
use std::io::IoSlice;
use nix::fcntl::*;
use nix::unistd::{pipe, read, write};
use tempfile::tempfile;
#[cfg(target_os = "linux")]
use tempfile::NamedTempFile;
use crate::*;
#[test]
fn test_splice() {
const CONTENTS: &[u8] = b"abcdef123456";
let mut tmp = tempfile().unwrap();
tmp.write_all(CONTENTS).unwrap();
let (rd, wr) = pipe().unwrap();
let mut offset: loff_t = 5;
let res =
splice(tmp, Some(&mut offset), wr, None, 2, SpliceFFlags::empty())
.unwrap();
assert_eq!(2, res);
let mut buf = [0u8; 1024];
assert_eq!(2, read(&rd, &mut buf).unwrap());
assert_eq!(b"f1", &buf[0..2]);
assert_eq!(7, offset);
}
#[test]
fn test_tee() {
let (rd1, wr1) = pipe().unwrap();
let (rd2, wr2) = pipe().unwrap();
write(wr1, b"abc").unwrap();
let res = tee(rd1.try_clone().unwrap(), wr2, 2, SpliceFFlags::empty())
.unwrap();
assert_eq!(2, res);
let mut buf = [0u8; 1024];
// Check the tee'd bytes are at rd2.
assert_eq!(2, read(&rd2, &mut buf).unwrap());
assert_eq!(b"ab", &buf[0..2]);
// Check all the bytes are still at rd1.
assert_eq!(3, read(&rd1, &mut buf).unwrap());
assert_eq!(b"abc", &buf[0..3]);
}
#[test]
fn test_vmsplice() {
let (rd, wr) = pipe().unwrap();
let buf1 = b"abcdef";
let buf2 = b"defghi";
let iovecs = [IoSlice::new(&buf1[0..3]), IoSlice::new(&buf2[0..3])];
let res = vmsplice(wr, &iovecs[..], SpliceFFlags::empty()).unwrap();
assert_eq!(6, res);
// Check the bytes can be read at rd.
let mut buf = [0u8; 32];
assert_eq!(6, read(&rd, &mut buf).unwrap());
assert_eq!(b"abcdef", &buf[0..6]);
}
#[cfg(target_os = "linux")]
#[test]
fn test_fallocate() {
let tmp = NamedTempFile::new().unwrap();
fallocate(&tmp, FallocateFlags::empty(), 0, 100).unwrap();
// Check if we read exactly 100 bytes
let mut buf = [0u8; 200];
assert_eq!(100, read(&tmp, &mut buf).unwrap());
}
// The tests below are disabled for the listed targets
// due to OFD locks not being available in the kernel/libc
// versions used in the CI environment, probably because
// they run under QEMU.
#[test]
#[cfg(all(target_os = "linux", not(target_env = "musl")))]
#[cfg_attr(target_env = "uclibc", ignore)] // uclibc doesn't support OFD locks, but the test should still compile
fn test_ofd_write_lock() {
use nix::sys::stat::fstat;
use std::mem;
let tmp = NamedTempFile::new().unwrap();
let statfs = nix::sys::statfs::fstatfs(tmp.as_file()).unwrap();
if statfs.filesystem_type() == nix::sys::statfs::OVERLAYFS_SUPER_MAGIC {
// OverlayFS is a union file system. It returns one inode value in
// stat(2), but a different one shows up in /proc/locks. So we must
// skip the test.
skip!("/proc/locks does not work on overlayfs");
}
let inode = fstat(&tmp).expect("fstat failed").st_ino as usize;
let mut flock: libc::flock = unsafe {
mem::zeroed() // required for Linux/mips
};
flock.l_type = libc::F_WRLCK as libc::c_short;
flock.l_whence = libc::SEEK_SET as libc::c_short;
flock.l_start = 0;
flock.l_len = 0;
flock.l_pid = 0;
fcntl(&tmp, FcntlArg::F_OFD_SETLKW(&flock)).expect("write lock failed");
assert_eq!(
Some(("OFDLCK".to_string(), "WRITE".to_string())),
lock_info(inode)
);
flock.l_type = libc::F_UNLCK as libc::c_short;
fcntl(&tmp, FcntlArg::F_OFD_SETLKW(&flock))
.expect("write unlock failed");
assert_eq!(None, lock_info(inode));
}
#[test]
#[cfg(all(target_os = "linux", not(target_env = "musl")))]
#[cfg_attr(target_env = "uclibc", ignore)] // uclibc doesn't support OFD locks, but the test should still compile
fn test_ofd_read_lock() {
use nix::sys::stat::fstat;
use std::mem;
let tmp = NamedTempFile::new().unwrap();
let statfs = nix::sys::statfs::fstatfs(tmp.as_file()).unwrap();
if statfs.filesystem_type() == nix::sys::statfs::OVERLAYFS_SUPER_MAGIC {
// OverlayFS is a union file system. It returns one inode value in
// stat(2), but a different one shows up in /proc/locks. So we must
// skip the test.
skip!("/proc/locks does not work on overlayfs");
}
let inode = fstat(&tmp).expect("fstat failed").st_ino as usize;
let mut flock: libc::flock = unsafe {
mem::zeroed() // required for Linux/mips
};
flock.l_type = libc::F_RDLCK as libc::c_short;
flock.l_whence = libc::SEEK_SET as libc::c_short;
flock.l_start = 0;
flock.l_len = 0;
flock.l_pid = 0;
fcntl(&tmp, FcntlArg::F_OFD_SETLKW(&flock)).expect("read lock failed");
assert_eq!(
Some(("OFDLCK".to_string(), "READ".to_string())),
lock_info(inode)
);
flock.l_type = libc::F_UNLCK as libc::c_short;
fcntl(&tmp, FcntlArg::F_OFD_SETLKW(&flock))
.expect("read unlock failed");
assert_eq!(None, lock_info(inode));
}
#[cfg(all(target_os = "linux", not(target_env = "musl")))]
fn lock_info(inode: usize) -> Option<(String, String)> {
use std::{fs::File, io::BufReader};
let file = File::open("/proc/locks").expect("open /proc/locks failed");
let buf = BufReader::new(file);
for line in buf.lines() {
let line = line.unwrap();
let parts: Vec<_> = line.split_whitespace().collect();
let lock_type = parts[1];
let lock_access = parts[3];
let ino_parts: Vec<_> = parts[5].split(':').collect();
let ino: usize = ino_parts[2].parse().unwrap();
if ino == inode {
return Some((lock_type.to_string(), lock_access.to_string()));
}
}
None
}
}
#[cfg(any(
linux_android,
target_os = "emscripten",
target_os = "fuchsia",
target_os = "wasi",
target_env = "uclibc",
target_os = "freebsd"
))]
mod test_posix_fadvise {
use nix::errno::Errno;
use nix::fcntl::*;
use nix::unistd::pipe;
use tempfile::NamedTempFile;
#[test]
fn test_success() {
let tmp = NamedTempFile::new().unwrap();
posix_fadvise(&tmp, 0, 100, PosixFadviseAdvice::POSIX_FADV_WILLNEED)
.expect("posix_fadvise failed");
}
#[test]
fn test_errno() {
let (rd, _wr) = pipe().unwrap();
let res =
posix_fadvise(&rd, 0, 100, PosixFadviseAdvice::POSIX_FADV_WILLNEED);
assert_eq!(res, Err(Errno::ESPIPE));
}
}
#[cfg(any(
linux_android,
freebsdlike,
target_os = "emscripten",
target_os = "fuchsia",
target_os = "wasi",
))]
mod test_posix_fallocate {
use nix::errno::Errno;
use nix::fcntl::*;
use nix::unistd::pipe;
use std::io::Read;
use tempfile::NamedTempFile;
#[test]
fn success() {
const LEN: usize = 100;
let mut tmp = NamedTempFile::new().unwrap();
let res = posix_fallocate(&tmp, 0, LEN as libc::off_t);
match res {
Ok(_) => {
let mut data = [1u8; LEN];
assert_eq!(tmp.read(&mut data).expect("read failure"), LEN);
assert_eq!(&data[..], &[0u8; LEN][..]);
}
Err(Errno::EINVAL) => {
// POSIX requires posix_fallocate to return EINVAL both for
// invalid arguments (i.e. len < 0) and if the operation is not
// supported by the file system.
// There's no way to tell for sure whether the file system
// supports posix_fallocate, so we must pass the test if it
// returns EINVAL.
}
_ => res.unwrap(),
}
}
#[test]
fn errno() {
let (rd, _wr) = pipe().unwrap();
let err = posix_fallocate(&rd, 0, 100).unwrap_err();
match err {
Errno::EINVAL | Errno::ENODEV | Errno::ESPIPE | Errno::EBADF => (),
errno => panic!("unexpected errno {errno}",),
}
}
}
#[cfg(any(target_os = "dragonfly", target_os = "netbsd", apple_targets))]
#[test]
fn test_f_get_path() {
use nix::fcntl::*;
use std::path::PathBuf;
let tmp = NamedTempFile::new().unwrap();
let mut path = PathBuf::new();
let res =
fcntl(&tmp, FcntlArg::F_GETPATH(&mut path)).expect("get path failed");
assert_ne!(res, -1);
assert_eq!(
path.as_path().canonicalize().unwrap(),
tmp.path().canonicalize().unwrap()
);
}
#[cfg(apple_targets)]
#[test]
fn test_f_preallocate() {
use nix::fcntl::*;
let tmp = NamedTempFile::new().unwrap();
let mut st: libc::fstore_t = unsafe { std::mem::zeroed() };
st.fst_flags = libc::F_ALLOCATECONTIG as libc::c_uint;
st.fst_posmode = libc::F_PEOFPOSMODE;
st.fst_length = 1024;
let res = fcntl(tmp, FcntlArg::F_PREALLOCATE(&mut st))
.expect("preallocation failed");
assert_eq!(res, 0);
assert!(st.fst_bytesalloc > 0);
}
#[cfg(apple_targets)]
#[test]
fn test_f_get_path_nofirmlink() {
use nix::fcntl::*;
use std::path::PathBuf;
let tmp = NamedTempFile::new().unwrap();
let mut path = PathBuf::new();
let res = fcntl(&tmp, FcntlArg::F_GETPATH_NOFIRMLINK(&mut path))
.expect("get path failed");
let mut tmpstr = String::from("/System/Volumes/Data");
tmpstr.push_str(
&tmp.path()
.canonicalize()
.unwrap()
.into_os_string()
.into_string()
.unwrap(),
);
assert_ne!(res, -1);
assert_eq!(
path.as_path()
.canonicalize()
.unwrap()
.into_os_string()
.into_string()
.unwrap(),
tmpstr
);
}
#[cfg(all(target_os = "freebsd", target_arch = "x86_64"))]
#[test]
fn test_f_kinfo() {
use nix::fcntl::*;
use std::path::PathBuf;
let tmp = NamedTempFile::new().unwrap();
// With TMPDIR set with UFS, the vnode name is not entered
// into the name cache thus path is always empty.
// Therefore, we reopen the tempfile a second time for the test
// to pass.
let tmp2 = File::open(tmp.path()).unwrap();
let mut path = PathBuf::new();
let res =
fcntl(&tmp2, FcntlArg::F_KINFO(&mut path)).expect("get path failed");
assert_ne!(res, -1);
assert_eq!(path, tmp.path());
}
/// Test `Flock` and associated functions.
///
#[cfg(not(any(target_os = "redox", target_os = "solaris")))]
mod test_flock {
use nix::fcntl::*;
use tempfile::NamedTempFile;
/// Verify that `Flock::lock()` correctly obtains a lock, and subsequently unlocks upon drop.
#[test]
fn lock_and_drop() {
// Get 2 `File` handles to same underlying file.
let file1 = NamedTempFile::new().unwrap();
let file2 = file1.reopen().unwrap();
let file1 = file1.into_file();
// Lock first handle
let lock1 = Flock::lock(file1, FlockArg::LockExclusive).unwrap();
// Attempt to lock second handle
let file2 = match Flock::lock(file2, FlockArg::LockExclusiveNonblock) {
Ok(_) => panic!("Expected second exclusive lock to fail."),
Err((f, _)) => f,
};
// Drop first lock
std::mem::drop(lock1);
// Attempt to lock second handle again (but successfully)
if Flock::lock(file2, FlockArg::LockExclusiveNonblock).is_err() {
panic!("Expected locking to be successful.");
}
}
/// An exclusive lock can be downgraded
#[test]
fn downgrade() {
let file1 = NamedTempFile::new().unwrap();
let file2 = file1.reopen().unwrap();
let file1 = file1.into_file();
// Lock first handle
let lock1 = Flock::lock(file1, FlockArg::LockExclusive).unwrap();
// Attempt to lock second handle
let file2 = Flock::lock(file2, FlockArg::LockSharedNonblock)
.unwrap_err()
.0;
// Downgrade the lock
lock1.relock(FlockArg::LockShared).unwrap();
// Attempt to lock second handle again (but successfully)
Flock::lock(file2, FlockArg::LockSharedNonblock)
.expect("Expected locking to be successful.");
}
/// Verify that `Flock::unlock()` correctly obtains unlocks.
#[test]
fn unlock() {
// Get 2 `File` handles to same underlying file.
let file1 = NamedTempFile::new().unwrap();
let file2 = file1.reopen().unwrap();
let file1 = file1.into_file();
// Lock first handle
let lock1 = Flock::lock(file1, FlockArg::LockExclusive).unwrap();
// Unlock and retain file so any erroneous flocks also remain present.
let _file1 = lock1.unlock().unwrap();
// Attempt to lock second handle.
if Flock::lock(file2, FlockArg::LockExclusiveNonblock).is_err() {
panic!("Expected locking to be successful.");
}
}
/// A shared lock can be upgraded
#[test]
fn upgrade() {
let file1 = NamedTempFile::new().unwrap();
let file2 = file1.reopen().unwrap();
let file3 = file1.reopen().unwrap();
let file1 = file1.into_file();
// Lock first handle
let lock1 = Flock::lock(file1, FlockArg::LockShared).unwrap();
// Attempt to lock second handle
{
Flock::lock(file2, FlockArg::LockSharedNonblock)
.expect("Locking should've succeeded");
}
// Upgrade the lock
lock1.relock(FlockArg::LockExclusive).unwrap();
// Acquiring an additional shared lock should fail
Flock::lock(file3, FlockArg::LockSharedNonblock)
.expect_err("Should not have been able to lock the file");
}
}
#[cfg(apple_targets)]
#[test]
fn test_f_rdadvise() {
use nix::fcntl::*;
let contents = vec![1; 1024];
let mut buf = [0; 1024];
let mut tmp = NamedTempFile::new().unwrap();
tmp.write_all(&contents).unwrap();
let fd = open(tmp.path(), OFlag::empty(), Mode::empty()).unwrap();
let rad = libc::radvisory {
ra_offset: 0,
ra_count: contents.len() as _,
};
let res = fcntl(&tmp, FcntlArg::F_RDADVISE(rad)).expect("rdadivse failed");
assert_ne!(res, -1);
assert_eq!(contents.len(), read(&fd, &mut buf).unwrap());
assert_eq!(contents, &buf[0..contents.len()]);
}
#[cfg(apple_targets)]
#[test]
fn test_f_log2phys() {
use nix::fcntl::*;
const CONTENTS: &[u8] = b"abcd";
let mut tmp = NamedTempFile::new().unwrap();
tmp.write_all(CONTENTS).unwrap();
let mut offset: libc::off_t = 0;
let mut res = fcntl(&tmp, FcntlArg::F_LOG2PHYS(&mut offset))
.expect("log2phys failed");
assert_ne!(res, -1);
assert_ne!(offset, 0);
let mut info: libc::log2phys = unsafe { std::mem::zeroed() };
info.l2p_contigbytes = CONTENTS.len() as _;
info.l2p_devoffset = 3;
res = fcntl(&tmp, FcntlArg::F_LOG2PHYS_EXT(&mut info))
.expect("log2phys failed");
assert_ne!(res, -1);
assert_ne!({ info.l2p_devoffset }, 3);
}
#[cfg(apple_targets)]
#[test]
fn test_f_transferextents() {
use nix::fcntl::*;
use std::os::fd::AsRawFd;
let tmp1 = NamedTempFile::new().unwrap();
let tmp2 = NamedTempFile::new().unwrap();
let res = fcntl(&tmp1, FcntlArg::F_TRANSFEREXTENTS(tmp2.as_raw_fd()))
.expect("transferextents failed");
assert_ne!(res, -1);
}
#[cfg(target_os = "freebsd")]
#[test]
fn test_f_readahead() {
use nix::fcntl::*;
let tmp = NamedTempFile::new().unwrap();
let mut res = fcntl(&tmp, FcntlArg::F_READAHEAD(1_000_000))
.expect("read ahead failed");
assert_ne!(res, -1);
res = fcntl(&tmp, FcntlArg::F_READAHEAD(-1024)).expect("read ahead failed");
assert_ne!(res, -1);
}

View File

@@ -0,0 +1,7 @@
obj-m += hello.o
all:
make -C /lib/modules/$(shell uname -r)/build M=$(shell pwd) modules
clean:
make -C /lib/modules/$(shell uname -r)/build M=$(shell pwd) clean

View File

@@ -0,0 +1,26 @@
/*
* SPDX-License-Identifier: GPL-2.0+ or MIT
*/
#include <linux/module.h>
#include <linux/kernel.h>
static int number= 1;
static char *who = "World";
module_param(number, int, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
MODULE_PARM_DESC(myint, "Just some number");
module_param(who, charp, 0000);
MODULE_PARM_DESC(who, "Whot to greet");
int init_module(void)
{
printk(KERN_INFO "Hello %s (%d)!\n", who, number);
return 0;
}
void cleanup_module(void)
{
printk(KERN_INFO "Goodbye %s (%d)!\n", who, number);
}
MODULE_LICENSE("Dual MIT/GPL");

188
vendor/nix/test/test_kmod/mod.rs vendored Normal file
View File

@@ -0,0 +1,188 @@
use crate::*;
use std::fs::copy;
use std::path::PathBuf;
use std::process::Command;
use tempfile::{tempdir, TempDir};
fn compile_kernel_module() -> (PathBuf, String, TempDir) {
let _m = crate::FORK_MTX.lock();
let tmp_dir =
tempdir().expect("unable to create temporary build directory");
copy(
"test/test_kmod/hello_mod/hello.c",
tmp_dir.path().join("hello.c"),
)
.expect("unable to copy hello.c to temporary build directory");
copy(
"test/test_kmod/hello_mod/Makefile",
tmp_dir.path().join("Makefile"),
)
.expect("unable to copy Makefile to temporary build directory");
let status = Command::new("make")
.current_dir(tmp_dir.path())
.status()
.expect("failed to run make");
assert!(status.success());
// Return the relative path of the build kernel module
(tmp_dir.path().join("hello.ko"), "hello".to_owned(), tmp_dir)
}
use nix::errno::Errno;
use nix::kmod::{delete_module, DeleteModuleFlags};
use nix::kmod::{finit_module, init_module, ModuleInitFlags};
use std::ffi::CString;
use std::fs::File;
use std::io::Read;
#[test]
fn test_finit_and_delete_module() {
require_capability!("test_finit_and_delete_module", CAP_SYS_MODULE);
let _m0 = crate::KMOD_MTX.lock();
let _m1 = crate::CWD_LOCK.read();
let (kmod_path, kmod_name, _kmod_dir) = compile_kernel_module();
let f = File::open(kmod_path).expect("unable to open kernel module");
finit_module(&f, &CString::new("").unwrap(), ModuleInitFlags::empty())
.expect("unable to load kernel module");
delete_module(
&CString::new(kmod_name).unwrap(),
DeleteModuleFlags::empty(),
)
.expect("unable to unload kernel module");
}
#[test]
fn test_finit_and_delete_module_with_params() {
require_capability!(
"test_finit_and_delete_module_with_params",
CAP_SYS_MODULE
);
let _m0 = crate::KMOD_MTX.lock();
let _m1 = crate::CWD_LOCK.read();
let (kmod_path, kmod_name, _kmod_dir) = compile_kernel_module();
let f = File::open(kmod_path).expect("unable to open kernel module");
finit_module(
&f,
&CString::new("who=Rust number=2018").unwrap(),
ModuleInitFlags::empty(),
)
.expect("unable to load kernel module");
delete_module(
&CString::new(kmod_name).unwrap(),
DeleteModuleFlags::empty(),
)
.expect("unable to unload kernel module");
}
#[test]
fn test_init_and_delete_module() {
require_capability!("test_init_and_delete_module", CAP_SYS_MODULE);
let _m0 = crate::KMOD_MTX.lock();
let _m1 = crate::CWD_LOCK.read();
let (kmod_path, kmod_name, _kmod_dir) = compile_kernel_module();
let mut f = File::open(kmod_path).expect("unable to open kernel module");
let mut contents: Vec<u8> = Vec::new();
f.read_to_end(&mut contents)
.expect("unable to read kernel module content to buffer");
init_module(&contents, &CString::new("").unwrap())
.expect("unable to load kernel module");
delete_module(
&CString::new(kmod_name).unwrap(),
DeleteModuleFlags::empty(),
)
.expect("unable to unload kernel module");
}
#[test]
fn test_init_and_delete_module_with_params() {
require_capability!(
"test_init_and_delete_module_with_params",
CAP_SYS_MODULE
);
let _m0 = crate::KMOD_MTX.lock();
let _m1 = crate::CWD_LOCK.read();
let (kmod_path, kmod_name, _kmod_dir) = compile_kernel_module();
let mut f = File::open(kmod_path).expect("unable to open kernel module");
let mut contents: Vec<u8> = Vec::new();
f.read_to_end(&mut contents)
.expect("unable to read kernel module content to buffer");
init_module(&contents, &CString::new("who=Nix number=2015").unwrap())
.expect("unable to load kernel module");
delete_module(
&CString::new(kmod_name).unwrap(),
DeleteModuleFlags::empty(),
)
.expect("unable to unload kernel module");
}
#[test]
fn test_finit_module_invalid() {
require_capability!("test_finit_module_invalid", CAP_SYS_MODULE);
let _m0 = crate::KMOD_MTX.lock();
let _m1 = crate::CWD_LOCK.read();
let kmod_path = "/dev/zero";
let f = File::open(kmod_path).expect("unable to open kernel module");
let result =
finit_module(&f, &CString::new("").unwrap(), ModuleInitFlags::empty());
assert_eq!(result.unwrap_err(), Errno::EINVAL);
}
#[test]
fn test_finit_module_twice_and_delete_module() {
require_capability!(
"test_finit_module_twice_and_delete_module",
CAP_SYS_MODULE
);
let _m0 = crate::KMOD_MTX.lock();
let _m1 = crate::CWD_LOCK.read();
let (kmod_path, kmod_name, _kmod_dir) = compile_kernel_module();
let f = File::open(kmod_path).expect("unable to open kernel module");
finit_module(&f, &CString::new("").unwrap(), ModuleInitFlags::empty())
.expect("unable to load kernel module");
let result =
finit_module(&f, &CString::new("").unwrap(), ModuleInitFlags::empty());
assert_eq!(result.unwrap_err(), Errno::EEXIST);
delete_module(
&CString::new(kmod_name).unwrap(),
DeleteModuleFlags::empty(),
)
.expect("unable to unload kernel module");
}
#[test]
fn test_delete_module_not_loaded() {
require_capability!("test_delete_module_not_loaded", CAP_SYS_MODULE);
let _m0 = crate::KMOD_MTX.lock();
let _m1 = crate::CWD_LOCK.read();
let result = delete_module(
&CString::new("hello").unwrap(),
DeleteModuleFlags::empty(),
);
assert_eq!(result.unwrap_err(), Errno::ENOENT);
}

239
vendor/nix/test/test_mq.rs vendored Normal file
View File

@@ -0,0 +1,239 @@
use cfg_if::cfg_if;
use std::str;
use nix::errno::Errno;
use nix::mqueue::{
mq_attr_member_t, mq_close, mq_open, mq_receive, mq_send, mq_timedreceive,
};
use nix::mqueue::{MQ_OFlag, MqAttr};
use nix::sys::stat::Mode;
use nix::sys::time::{TimeSpec, TimeValLike};
use nix::time::{clock_gettime, ClockId};
// Defined as a macro such that the error source is reported as the caller's location.
macro_rules! assert_attr_eq {
($read_attr:ident, $initial_attr:ident) => {
cfg_if! {
if #[cfg(any(target_os = "dragonfly", target_os = "netbsd"))] {
// NetBSD (and others which inherit its implementation) include other flags
// in read_attr, such as those specified by oflag. Just make sure at least
// the correct bits are set.
assert_eq!($read_attr.flags() & $initial_attr.flags(), $initial_attr.flags());
assert_eq!($read_attr.maxmsg(), $initial_attr.maxmsg());
assert_eq!($read_attr.msgsize(), $initial_attr.msgsize());
assert_eq!($read_attr.curmsgs(), $initial_attr.curmsgs());
} else {
assert_eq!($read_attr, $initial_attr);
}
}
}
}
#[test]
fn test_mq_send_and_receive() {
const MSG_SIZE: mq_attr_member_t = 32;
let attr = MqAttr::new(0, 10, MSG_SIZE, 0);
let mq_name = "/a_nix_test_queue";
let oflag0 = MQ_OFlag::O_CREAT | MQ_OFlag::O_WRONLY;
let mode = Mode::S_IWUSR | Mode::S_IRUSR | Mode::S_IRGRP | Mode::S_IROTH;
let r0 = mq_open(mq_name, oflag0, mode, Some(&attr));
if let Err(Errno::ENOSYS) = r0 {
println!("message queues not supported or module not loaded?");
return;
};
let mqd0 = r0.unwrap();
let msg_to_send = "msg_1";
mq_send(&mqd0, msg_to_send.as_bytes(), 1).unwrap();
let oflag1 = MQ_OFlag::O_CREAT | MQ_OFlag::O_RDONLY;
let mqd1 = mq_open(mq_name, oflag1, mode, Some(&attr)).unwrap();
let mut buf = [0u8; 32];
let mut prio = 0u32;
let len = mq_receive(&mqd1, &mut buf, &mut prio).unwrap();
assert_eq!(prio, 1);
mq_close(mqd1).unwrap();
mq_close(mqd0).unwrap();
assert_eq!(msg_to_send, str::from_utf8(&buf[0..len]).unwrap());
}
#[test]
fn test_mq_timedreceive() {
const MSG_SIZE: mq_attr_member_t = 32;
let attr = MqAttr::new(0, 10, MSG_SIZE, 0);
let mq_name = "/a_nix_test_queue";
let oflag0 = MQ_OFlag::O_CREAT | MQ_OFlag::O_WRONLY;
let mode = Mode::S_IWUSR | Mode::S_IRUSR | Mode::S_IRGRP | Mode::S_IROTH;
let r0 = mq_open(mq_name, oflag0, mode, Some(&attr));
if let Err(Errno::ENOSYS) = r0 {
println!("message queues not supported or module not loaded?");
return;
};
let mqd0 = r0.unwrap();
let msg_to_send = "msg_1";
mq_send(&mqd0, msg_to_send.as_bytes(), 1).unwrap();
let oflag1 = MQ_OFlag::O_CREAT | MQ_OFlag::O_RDONLY;
let mqd1 = mq_open(mq_name, oflag1, mode, Some(&attr)).unwrap();
let mut buf = [0u8; 32];
let mut prio = 0u32;
let abstime =
clock_gettime(ClockId::CLOCK_REALTIME).unwrap() + TimeSpec::seconds(1);
let len = mq_timedreceive(&mqd1, &mut buf, &mut prio, &abstime).unwrap();
assert_eq!(prio, 1);
mq_close(mqd1).unwrap();
mq_close(mqd0).unwrap();
assert_eq!(msg_to_send, str::from_utf8(&buf[0..len]).unwrap());
}
#[test]
fn test_mq_getattr() {
use nix::mqueue::mq_getattr;
const MSG_SIZE: mq_attr_member_t = 32;
let initial_attr = MqAttr::new(0, 10, MSG_SIZE, 0);
let mq_name = "/attr_test_get_attr";
let oflag = MQ_OFlag::O_CREAT | MQ_OFlag::O_WRONLY;
let mode = Mode::S_IWUSR | Mode::S_IRUSR | Mode::S_IRGRP | Mode::S_IROTH;
let r = mq_open(mq_name, oflag, mode, Some(&initial_attr));
if let Err(Errno::ENOSYS) = r {
println!("message queues not supported or module not loaded?");
return;
};
let mqd = r.unwrap();
let read_attr = mq_getattr(&mqd).unwrap();
assert_attr_eq!(read_attr, initial_attr);
mq_close(mqd).unwrap();
}
// FIXME: Fix failures for mips in QEMU
#[test]
#[cfg_attr(
all(
qemu,
any(
target_arch = "mips",
target_arch = "mips32r6",
target_arch = "mips64",
target_arch = "mips64r6"
)
),
ignore
)]
fn test_mq_setattr() {
use nix::mqueue::{mq_getattr, mq_setattr};
const MSG_SIZE: mq_attr_member_t = 32;
let initial_attr = MqAttr::new(0, 10, MSG_SIZE, 0);
let mq_name = "/attr_test_get_attr";
let oflag = MQ_OFlag::O_CREAT | MQ_OFlag::O_WRONLY;
let mode = Mode::S_IWUSR | Mode::S_IRUSR | Mode::S_IRGRP | Mode::S_IROTH;
let r = mq_open(mq_name, oflag, mode, Some(&initial_attr));
if let Err(Errno::ENOSYS) = r {
println!("message queues not supported or module not loaded?");
return;
};
let mqd = r.unwrap();
let new_attr = MqAttr::new(0, 20, MSG_SIZE * 2, 100);
let old_attr = mq_setattr(&mqd, &new_attr).unwrap();
assert_attr_eq!(old_attr, initial_attr);
// No changes here because according to the Linux man page only
// O_NONBLOCK can be set (see tests below)
#[cfg(not(any(target_os = "dragonfly", target_os = "netbsd")))]
{
let new_attr_get = mq_getattr(&mqd).unwrap();
assert_ne!(new_attr_get, new_attr);
}
let new_attr_non_blocking = MqAttr::new(
MQ_OFlag::O_NONBLOCK.bits() as mq_attr_member_t,
10,
MSG_SIZE,
0,
);
mq_setattr(&mqd, &new_attr_non_blocking).unwrap();
let new_attr_get = mq_getattr(&mqd).unwrap();
// now the O_NONBLOCK flag has been set
#[cfg(not(any(target_os = "dragonfly", target_os = "netbsd")))]
{
assert_ne!(new_attr_get, initial_attr);
}
assert_attr_eq!(new_attr_get, new_attr_non_blocking);
mq_close(mqd).unwrap();
}
// FIXME: Fix failures for mips in QEMU
#[test]
#[cfg_attr(
all(
qemu,
any(
target_arch = "mips",
target_arch = "mips32r6",
target_arch = "mips64",
target_arch = "mips64r6"
)
),
ignore
)]
fn test_mq_set_nonblocking() {
use nix::mqueue::{mq_getattr, mq_remove_nonblock, mq_set_nonblock};
const MSG_SIZE: mq_attr_member_t = 32;
let initial_attr = MqAttr::new(0, 10, MSG_SIZE, 0);
let mq_name = "/attr_test_get_attr";
let oflag = MQ_OFlag::O_CREAT | MQ_OFlag::O_WRONLY;
let mode = Mode::S_IWUSR | Mode::S_IRUSR | Mode::S_IRGRP | Mode::S_IROTH;
let r = mq_open(mq_name, oflag, mode, Some(&initial_attr));
if let Err(Errno::ENOSYS) = r {
println!("message queues not supported or module not loaded?");
return;
};
let mqd = r.unwrap();
mq_set_nonblock(&mqd).unwrap();
let new_attr = mq_getattr(&mqd);
let o_nonblock_bits = MQ_OFlag::O_NONBLOCK.bits() as mq_attr_member_t;
assert_eq!(new_attr.unwrap().flags() & o_nonblock_bits, o_nonblock_bits);
mq_remove_nonblock(&mqd).unwrap();
let new_attr = mq_getattr(&mqd);
assert_eq!(new_attr.unwrap().flags() & o_nonblock_bits, 0);
mq_close(mqd).unwrap();
}
#[test]
fn test_mq_unlink() {
use nix::mqueue::mq_unlink;
const MSG_SIZE: mq_attr_member_t = 32;
let initial_attr = MqAttr::new(0, 10, MSG_SIZE, 0);
let mq_name_opened = "/mq_unlink_test";
#[cfg(not(any(target_os = "dragonfly", target_os = "netbsd")))]
let mq_name_not_opened = "/mq_unlink_test";
let oflag = MQ_OFlag::O_CREAT | MQ_OFlag::O_WRONLY;
let mode = Mode::S_IWUSR | Mode::S_IRUSR | Mode::S_IRGRP | Mode::S_IROTH;
let r = mq_open(mq_name_opened, oflag, mode, Some(&initial_attr));
if let Err(Errno::ENOSYS) = r {
println!("message queues not supported or module not loaded?");
return;
};
let mqd = r.unwrap();
let res_unlink = mq_unlink(mq_name_opened);
assert_eq!(res_unlink, Ok(()));
// NetBSD (and others which inherit its implementation) defer removing the message
// queue name until all references are closed, whereas Linux and others remove the
// message queue name immediately.
#[cfg(not(any(target_os = "dragonfly", target_os = "netbsd")))]
{
let res_unlink_not_opened = mq_unlink(mq_name_not_opened);
assert_eq!(res_unlink_not_opened, Err(Errno::ENOENT));
}
mq_close(mqd).unwrap();
let res_unlink_after_close = mq_unlink(mq_name_opened);
assert_eq!(res_unlink_after_close, Err(Errno::ENOENT));
}

28
vendor/nix/test/test_net.rs vendored Normal file
View File

@@ -0,0 +1,28 @@
use nix::net::if_::*;
#[cfg(linux_android)]
const LOOPBACK: &[u8] = b"lo";
#[cfg(not(any(linux_android, target_os = "haiku")))]
const LOOPBACK: &[u8] = b"lo0";
#[cfg(target_os = "haiku")]
const LOOPBACK: &[u8] = b"loop";
#[test]
#[cfg_attr(target_os = "cygwin", ignore)]
fn test_if_nametoindex() {
if_nametoindex(LOOPBACK).expect("assertion failed");
}
#[test]
#[cfg_attr(target_os = "cygwin", ignore)]
fn test_if_indextoname() {
let loopback_index = if_nametoindex(LOOPBACK).expect("assertion failed");
assert_eq!(
if_indextoname(loopback_index)
.expect("assertion failed")
.as_bytes(),
LOOPBACK
);
}

1
vendor/nix/test/test_nix_path.rs vendored Normal file
View File

@@ -0,0 +1 @@

73
vendor/nix/test/test_poll.rs vendored Normal file
View File

@@ -0,0 +1,73 @@
use nix::{
errno::Errno,
poll::{poll, PollFd, PollFlags, PollTimeout},
unistd::{pipe, write},
};
use std::os::unix::io::{AsFd, BorrowedFd};
macro_rules! loop_while_eintr {
($poll_expr: expr) => {
loop {
match $poll_expr {
Ok(nfds) => break nfds,
Err(Errno::EINTR) => (),
Err(e) => panic!("{}", e),
}
}
};
}
#[test]
fn test_poll() {
let (r, w) = pipe().unwrap();
let mut fds = [PollFd::new(r.as_fd(), PollFlags::POLLIN)];
// Poll an idle pipe. Should timeout
let nfds = loop_while_eintr!(poll(&mut fds, PollTimeout::from(100u8)));
assert_eq!(nfds, 0);
assert!(!fds[0].revents().unwrap().contains(PollFlags::POLLIN));
write(&w, b".").unwrap();
// Poll a readable pipe. Should return an event.
let nfds = poll(&mut fds, PollTimeout::from(100u8)).unwrap();
assert_eq!(nfds, 1);
assert!(fds[0].revents().unwrap().contains(PollFlags::POLLIN));
}
// ppoll(2) is the same as poll except for how it handles timeouts and signals.
// Repeating the test for poll(2) should be sufficient to check that our
// bindings are correct.
#[cfg(any(linux_android, freebsdlike))]
#[test]
fn test_ppoll() {
use nix::poll::ppoll;
use nix::sys::signal::SigSet;
use nix::sys::time::{TimeSpec, TimeValLike};
let timeout = TimeSpec::milliseconds(1);
let (r, w) = pipe().unwrap();
let mut fds = [PollFd::new(r.as_fd(), PollFlags::POLLIN)];
// Poll an idle pipe. Should timeout
let sigset = SigSet::empty();
let nfds = loop_while_eintr!(ppoll(&mut fds, Some(timeout), Some(sigset)));
assert_eq!(nfds, 0);
assert!(!fds[0].revents().unwrap().contains(PollFlags::POLLIN));
write(&w, b".").unwrap();
// Poll a readable pipe. Should return an event.
let nfds = ppoll(&mut fds, Some(timeout), None).unwrap();
assert_eq!(nfds, 1);
assert!(fds[0].revents().unwrap().contains(PollFlags::POLLIN));
}
#[test]
fn test_pollfd_events() {
let fd_zero = unsafe { BorrowedFd::borrow_raw(0) };
let mut pfd = PollFd::new(fd_zero.as_fd(), PollFlags::POLLIN);
assert_eq!(pfd.events(), PollFlags::POLLIN);
pfd.set_events(PollFlags::POLLOUT);
assert_eq!(pfd.events(), PollFlags::POLLOUT);
}

282
vendor/nix/test/test_pty.rs vendored Normal file
View File

@@ -0,0 +1,282 @@
use std::fs::File;
use std::io::{stdout, Read, Write};
use std::os::unix::prelude::*;
use std::path::Path;
use libc::_exit;
use nix::fcntl::{open, OFlag};
use nix::pty::*;
use nix::sys::stat;
use nix::sys::termios::*;
use nix::sys::wait::WaitStatus;
use nix::unistd::{pause, write};
/// Test equivalence of `ptsname` and `ptsname_r`
#[test]
#[cfg(linux_android)]
fn test_ptsname_equivalence() {
let _m = crate::PTSNAME_MTX.lock();
// Open a new PTY master
let master_fd = posix_openpt(OFlag::O_RDWR).unwrap();
assert!(master_fd.as_raw_fd() > 0);
assert!(master_fd.as_fd().as_raw_fd() == master_fd.as_raw_fd());
// Get the name of the slave
let slave_name = unsafe { ptsname(&master_fd) }.unwrap();
let slave_name_r = ptsname_r(&master_fd).unwrap();
assert_eq!(slave_name, slave_name_r);
}
/// Test data copying of `ptsname`
// TODO need to run in a subprocess, since ptsname is non-reentrant
#[test]
#[cfg(linux_android)]
fn test_ptsname_copy() {
let _m = crate::PTSNAME_MTX.lock();
// Open a new PTTY master
let master_fd = posix_openpt(OFlag::O_RDWR).unwrap();
// Get the name of the slave
let slave_name1 = unsafe { ptsname(&master_fd) }.unwrap();
let slave_name2 = unsafe { ptsname(&master_fd) }.unwrap();
assert_eq!(slave_name1, slave_name2);
// Also make sure that the string was actually copied and they point to different parts of
// memory.
assert_ne!(slave_name1.as_ptr(), slave_name2.as_ptr());
}
/// Test data copying of `ptsname_r`
#[test]
#[cfg(linux_android)]
fn test_ptsname_r_copy() {
// Open a new PTTY master
let master_fd = posix_openpt(OFlag::O_RDWR).unwrap();
// Get the name of the slave
let slave_name1 = ptsname_r(&master_fd).unwrap();
let slave_name2 = ptsname_r(&master_fd).unwrap();
assert_eq!(slave_name1, slave_name2);
assert_ne!(slave_name1.as_ptr(), slave_name2.as_ptr());
}
/// Test that `ptsname` returns different names for different devices
#[test]
#[cfg(linux_android)]
fn test_ptsname_unique() {
let _m = crate::PTSNAME_MTX.lock();
// Open a new PTTY master
let master1_fd = posix_openpt(OFlag::O_RDWR).unwrap();
// Open a second PTTY master
let master2_fd = posix_openpt(OFlag::O_RDWR).unwrap();
// Get the name of the slave
let slave_name1 = unsafe { ptsname(&master1_fd) }.unwrap();
let slave_name2 = unsafe { ptsname(&master2_fd) }.unwrap();
assert_ne!(slave_name1, slave_name2);
}
/// Common setup for testing PTTY pairs
fn open_ptty_pair() -> (PtyMaster, File) {
let _m = crate::PTSNAME_MTX.lock();
// Open a new PTTY master
let master = posix_openpt(OFlag::O_RDWR).expect("posix_openpt failed");
// Allow a slave to be generated for it
grantpt(&master).expect("grantpt failed");
unlockpt(&master).expect("unlockpt failed");
// Get the name of the slave
let slave_name = unsafe { ptsname(&master) }.expect("ptsname failed");
// Open the slave device
let slave_fd =
open(Path::new(&slave_name), OFlag::O_RDWR, stat::Mode::empty())
.unwrap();
#[cfg(solarish)]
// TODO: rewrite using ioctl!
#[allow(clippy::comparison_chain)]
{
use libc::{ioctl, I_FIND, I_PUSH};
// On illumos systems, as per pts(7D), one must push STREAMS modules
// after opening a device path returned from ptsname().
let ptem = b"ptem\0";
let ldterm = b"ldterm\0";
let r = unsafe { ioctl(slave_fd.as_raw_fd(), I_FIND, ldterm.as_ptr()) };
if r < 0 {
panic!("I_FIND failure");
} else if r == 0 {
if unsafe { ioctl(slave_fd.as_raw_fd(), I_PUSH, ptem.as_ptr()) } < 0
{
panic!("I_PUSH ptem failure");
}
if unsafe { ioctl(slave_fd.as_raw_fd(), I_PUSH, ldterm.as_ptr()) }
< 0
{
panic!("I_PUSH ldterm failure");
}
}
}
let slave = File::from(slave_fd);
(master, slave)
}
/// Test opening a master/slave PTTY pair
///
/// This uses a common `open_ptty_pair` because much of these functions aren't useful by
/// themselves. So for this test we perform the basic act of getting a file handle for a
/// master/slave PTTY pair.
#[test]
fn test_open_ptty_pair() {
let (_, _) = open_ptty_pair();
}
/// Put the terminal in raw mode.
fn make_raw<Fd: AsFd>(fd: Fd) {
let mut termios = tcgetattr(&fd).unwrap();
cfmakeraw(&mut termios);
tcsetattr(&fd, SetArg::TCSANOW, &termios).unwrap();
}
/// Test `io::Read` on the PTTY master
#[test]
#[cfg(not(target_os = "solaris"))]
fn test_read_ptty_pair() {
let (mut master, mut slave) = open_ptty_pair();
make_raw(&slave);
let mut buf = [0u8; 5];
slave.write_all(b"hello").unwrap();
master.read_exact(&mut buf).unwrap();
assert_eq!(&buf, b"hello");
let mut master = &master;
slave.write_all(b"hello").unwrap();
master.read_exact(&mut buf).unwrap();
assert_eq!(&buf, b"hello");
}
/// Test `io::Write` on the PTTY master
#[test]
fn test_write_ptty_pair() {
let (mut master, mut slave) = open_ptty_pair();
make_raw(&slave);
let mut buf = [0u8; 5];
master.write_all(b"adios").unwrap();
slave.read_exact(&mut buf).unwrap();
assert_eq!(&buf, b"adios");
let mut master = &master;
master.write_all(b"adios").unwrap();
slave.read_exact(&mut buf).unwrap();
assert_eq!(&buf, b"adios");
}
#[test]
fn test_openpty() {
// openpty uses ptname(3) internally
let _m = crate::PTSNAME_MTX.lock();
let pty = openpty(None, None).unwrap();
// Writing to one should be readable on the other one
let string = "foofoofoo\n";
let mut buf = [0u8; 10];
write(&pty.master, string.as_bytes()).unwrap();
crate::read_exact(&pty.slave, &mut buf);
assert_eq!(&buf, string.as_bytes());
// Read the echo as well
let echoed_string = "foofoofoo\r\n";
let mut buf = [0u8; 11];
crate::read_exact(&pty.master, &mut buf);
assert_eq!(&buf, echoed_string.as_bytes());
let string2 = "barbarbarbar\n";
let echoed_string2 = "barbarbarbar\r\n";
let mut buf = [0u8; 14];
write(&pty.slave, string2.as_bytes()).unwrap();
crate::read_exact(&pty.master, &mut buf);
assert_eq!(&buf, echoed_string2.as_bytes());
}
#[test]
fn test_openpty_with_termios() {
// openpty uses ptname(3) internally
let _m = crate::PTSNAME_MTX.lock();
// Open one pty to get attributes for the second one
let mut termios = {
let pty = openpty(None, None).unwrap();
tcgetattr(&pty.slave).unwrap()
};
// Make sure newlines are not transformed so the data is preserved when sent.
termios.output_flags.remove(OutputFlags::ONLCR);
let pty = openpty(None, &termios).unwrap();
// Must be valid file descriptors
// Writing to one should be readable on the other one
let string = "foofoofoo\n";
let mut buf = [0u8; 10];
write(&pty.master, string.as_bytes()).unwrap();
crate::read_exact(&pty.slave, &mut buf);
assert_eq!(&buf, string.as_bytes());
// read the echo as well
let echoed_string = "foofoofoo\n";
crate::read_exact(&pty.master, &mut buf);
assert_eq!(&buf, echoed_string.as_bytes());
let string2 = "barbarbarbar\n";
let echoed_string2 = "barbarbarbar\n";
let mut buf = [0u8; 13];
write(&pty.slave, string2.as_bytes()).unwrap();
crate::read_exact(&pty.master, &mut buf);
assert_eq!(&buf, echoed_string2.as_bytes());
}
#[test]
fn test_forkpty() {
use nix::sys::signal::*;
use nix::sys::wait::wait;
// forkpty calls openpty which uses ptname(3) internally.
let _m0 = crate::PTSNAME_MTX.lock();
// forkpty spawns a child process
let _m1 = crate::FORK_MTX.lock();
let string = "naninani\n";
let echoed_string = "naninani\r\n";
let res = unsafe { forkpty(None, None).unwrap() };
match res {
ForkptyResult::Child => {
write(stdout(), string.as_bytes()).unwrap();
pause(); // we need the child to stay alive until the parent calls read
unsafe {
_exit(0);
}
}
ForkptyResult::Parent { child, master } => {
let mut buf = [0u8; 10];
assert!(child.as_raw() > 0);
crate::read_exact(&master, &mut buf);
kill(child, SIGTERM).unwrap();
let status = wait().unwrap(); // keep other tests using generic wait from getting our child
assert_eq!(status, WaitStatus::Signaled(child, SIGTERM, false));
assert_eq!(&buf, echoed_string.as_bytes());
}
}
}

39
vendor/nix/test/test_sched.rs vendored Normal file
View File

@@ -0,0 +1,39 @@
use nix::sched::{sched_getaffinity, sched_getcpu, sched_setaffinity, CpuSet};
use nix::unistd::Pid;
#[test]
fn test_sched_affinity() {
// If pid is zero, then the mask of the calling process is returned.
let initial_affinity = sched_getaffinity(Pid::from_raw(0)).unwrap();
let mut at_least_one_cpu = false;
let mut last_valid_cpu = 0;
for field in 0..CpuSet::count() {
if initial_affinity.is_set(field).unwrap() {
at_least_one_cpu = true;
last_valid_cpu = field;
}
}
assert!(at_least_one_cpu);
// Now restrict the running CPU
let mut new_affinity = CpuSet::new();
new_affinity.set(last_valid_cpu).unwrap();
sched_setaffinity(Pid::from_raw(0), &new_affinity).unwrap();
// And now re-check the affinity which should be only the one we set.
let updated_affinity = sched_getaffinity(Pid::from_raw(0)).unwrap();
for field in 0..CpuSet::count() {
// Should be set only for the CPU we set previously
assert_eq!(
updated_affinity.is_set(field).unwrap(),
field == last_valid_cpu
)
}
// Now check that we're also currently running on the CPU in question.
let cur_cpu = sched_getcpu().unwrap();
assert_eq!(cur_cpu, last_valid_cpu);
// Finally, reset the initial CPU set
sched_setaffinity(Pid::from_raw(0), &initial_affinity).unwrap();
}

267
vendor/nix/test/test_sendfile.rs vendored Normal file
View File

@@ -0,0 +1,267 @@
use std::io::prelude::*;
use libc::off_t;
use nix::sys::sendfile::*;
use tempfile::tempfile;
cfg_if! {
if #[cfg(linux_android)] {
use nix::unistd::{pipe, read};
} else if #[cfg(any(freebsdlike, apple_targets))] {
use std::net::Shutdown;
use std::os::unix::net::UnixStream;
} else if #[cfg(solarish)] {
use std::net::Shutdown;
use std::net::{TcpListener, TcpStream};
}
}
#[cfg(linux_android)]
#[test]
fn test_sendfile_linux() {
const CONTENTS: &[u8] = b"abcdef123456";
let mut tmp = tempfile().unwrap();
tmp.write_all(CONTENTS).unwrap();
let (rd, wr) = pipe().unwrap();
let mut offset: off_t = 5;
let res = sendfile(&wr, &tmp, Some(&mut offset), 2).unwrap();
assert_eq!(2, res);
let mut buf = [0u8; 1024];
assert_eq!(2, read(&rd, &mut buf).unwrap());
assert_eq!(b"f1", &buf[0..2]);
assert_eq!(7, offset);
}
#[cfg(target_os = "linux")]
#[test]
fn test_sendfile64_linux() {
const CONTENTS: &[u8] = b"abcdef123456";
let mut tmp = tempfile().unwrap();
tmp.write_all(CONTENTS).unwrap();
let (rd, wr) = pipe().unwrap();
let mut offset: libc::off64_t = 5;
let res = sendfile64(&wr, &tmp, Some(&mut offset), 2).unwrap();
assert_eq!(2, res);
let mut buf = [0u8; 1024];
assert_eq!(2, read(&rd, &mut buf).unwrap());
assert_eq!(b"f1", &buf[0..2]);
assert_eq!(7, offset);
}
#[cfg(target_os = "freebsd")]
#[test]
fn test_sendfile_freebsd() {
// Declare the content
let header_strings =
["HTTP/1.1 200 OK\n", "Content-Type: text/plain\n", "\n"];
let body = "Xabcdef123456";
let body_offset = 1;
let trailer_strings = ["\n", "Served by Make Believe\n"];
// Write the body to a file
let mut tmp = tempfile().unwrap();
tmp.write_all(body.as_bytes()).unwrap();
// Prepare headers and trailers for sendfile
let headers: Vec<&[u8]> =
header_strings.iter().map(|s| s.as_bytes()).collect();
let trailers: Vec<&[u8]> =
trailer_strings.iter().map(|s| s.as_bytes()).collect();
// Prepare socket pair
let (mut rd, wr) = UnixStream::pair().unwrap();
// Call the test method
let (res, bytes_written) = sendfile(
&tmp,
&wr,
body_offset as off_t,
None,
Some(headers.as_slice()),
Some(trailers.as_slice()),
SfFlags::empty(),
0,
);
assert!(res.is_ok());
wr.shutdown(Shutdown::Both).unwrap();
// Prepare the expected result
let expected_string = header_strings.concat()
+ &body[body_offset..]
+ &trailer_strings.concat();
// Verify the message that was sent
assert_eq!(bytes_written as usize, expected_string.len());
let mut read_string = String::new();
let bytes_read = rd.read_to_string(&mut read_string).unwrap();
assert_eq!(bytes_written as usize, bytes_read);
assert_eq!(expected_string, read_string);
}
#[cfg(target_os = "dragonfly")]
#[test]
fn test_sendfile_dragonfly() {
// Declare the content
let header_strings =
["HTTP/1.1 200 OK\n", "Content-Type: text/plain\n", "\n"];
let body = "Xabcdef123456";
let body_offset = 1;
let trailer_strings = ["\n", "Served by Make Believe\n"];
// Write the body to a file
let mut tmp = tempfile().unwrap();
tmp.write_all(body.as_bytes()).unwrap();
// Prepare headers and trailers for sendfile
let headers: Vec<&[u8]> =
header_strings.iter().map(|s| s.as_bytes()).collect();
let trailers: Vec<&[u8]> =
trailer_strings.iter().map(|s| s.as_bytes()).collect();
// Prepare socket pair
let (mut rd, wr) = UnixStream::pair().unwrap();
// Call the test method
let (res, bytes_written) = sendfile(
&tmp,
&wr,
body_offset as off_t,
None,
Some(headers.as_slice()),
Some(trailers.as_slice()),
);
assert!(res.is_ok());
wr.shutdown(Shutdown::Both).unwrap();
// Prepare the expected result
let expected_string = header_strings.concat()
+ &body[body_offset..]
+ &trailer_strings.concat();
// Verify the message that was sent
assert_eq!(bytes_written as usize, expected_string.len());
let mut read_string = String::new();
let bytes_read = rd.read_to_string(&mut read_string).unwrap();
assert_eq!(bytes_written as usize, bytes_read);
assert_eq!(expected_string, read_string);
}
#[cfg(apple_targets)]
#[test]
fn test_sendfile_darwin() {
// Declare the content
let header_strings =
vec!["HTTP/1.1 200 OK\n", "Content-Type: text/plain\n", "\n"];
let body = "Xabcdef123456";
let body_offset = 1;
let trailer_strings = vec!["\n", "Served by Make Believe\n"];
// Write the body to a file
let mut tmp = tempfile().unwrap();
tmp.write_all(body.as_bytes()).unwrap();
// Prepare headers and trailers for sendfile
let headers: Vec<&[u8]> =
header_strings.iter().map(|s| s.as_bytes()).collect();
let trailers: Vec<&[u8]> =
trailer_strings.iter().map(|s| s.as_bytes()).collect();
// Prepare socket pair
let (mut rd, wr) = UnixStream::pair().unwrap();
// Call the test method
let (res, bytes_written) = sendfile(
&tmp,
&wr,
body_offset as off_t,
None,
Some(headers.as_slice()),
Some(trailers.as_slice()),
);
assert!(res.is_ok());
wr.shutdown(Shutdown::Both).unwrap();
// Prepare the expected result
let expected_string = header_strings.concat()
+ &body[body_offset..]
+ &trailer_strings.concat();
// Verify the message that was sent
assert_eq!(bytes_written as usize, expected_string.as_bytes().len());
let mut read_string = String::new();
let bytes_read = rd.read_to_string(&mut read_string).unwrap();
assert_eq!(bytes_written as usize, bytes_read);
assert_eq!(expected_string, read_string);
}
#[cfg(solarish)]
#[test]
fn test_sendfilev() {
use std::os::fd::AsFd;
// Declare the content
let header_strings =
["HTTP/1.1 200 OK\n", "Content-Type: text/plain\n", "\n"];
let body = "Xabcdef123456";
let body_offset = 1usize;
let trailer_strings = ["\n", "Served by Make Believe\n"];
// Write data to files
let mut header_data = tempfile().unwrap();
header_data
.write_all(header_strings.concat().as_bytes())
.unwrap();
let mut body_data = tempfile().unwrap();
body_data.write_all(body.as_bytes()).unwrap();
let mut trailer_data = tempfile().unwrap();
trailer_data
.write_all(trailer_strings.concat().as_bytes())
.unwrap();
// Create a TCP socket pair (listener and client)
let listener = TcpListener::bind("127.0.0.1:0").unwrap();
let addr = listener.local_addr().unwrap();
let mut rd = TcpStream::connect(addr).unwrap();
let (wr, _) = listener.accept().unwrap();
let vec: &[SendfileVec] = &[
SendfileVec::new(
header_data.as_fd(),
0,
header_strings.iter().map(|s| s.len()).sum(),
),
SendfileVec::new(
body_data.as_fd(),
body_offset as off_t,
body.len() - body_offset,
),
SendfileVec::new(
trailer_data.as_fd(),
0,
trailer_strings.iter().map(|s| s.len()).sum(),
),
];
let (res, bytes_written) = sendfilev(&wr, vec);
assert!(res.is_ok());
wr.shutdown(Shutdown::Write).unwrap();
// Prepare the expected result
let expected_string = header_strings.concat()
+ &body[body_offset..]
+ &trailer_strings.concat();
// Verify the message that was sent
assert_eq!(bytes_written, expected_string.as_bytes().len());
let mut read_string = String::new();
let bytes_read = rd.read_to_string(&mut read_string).unwrap();
assert_eq!(bytes_written, bytes_read);
assert_eq!(expected_string, read_string);
}

193
vendor/nix/test/test_spawn.rs vendored Normal file
View File

@@ -0,0 +1,193 @@
use super::FORK_MTX;
use nix::errno::Errno;
use nix::spawn::{self, PosixSpawnAttr, PosixSpawnFileActions};
use nix::sys::signal;
use nix::sys::wait::{waitpid, WaitPidFlag, WaitStatus};
use std::ffi::{CStr, CString};
/// Helper function to find a binary in the $PATH
fn which(exe_name: &str) -> Option<std::path::PathBuf> {
std::env::var_os("PATH").and_then(|paths| {
std::env::split_paths(&paths)
.filter_map(|dir| {
let full_path = dir.join(exe_name);
if full_path.is_file() {
Some(full_path)
} else {
None
}
})
.next()
})
}
#[test]
fn spawn_true() {
let _guard = FORK_MTX.lock();
let bin = which("true").unwrap();
let args = &[
CString::new("true").unwrap(),
CString::new("story").unwrap(),
];
let vars: &[CString] = &[];
let actions = PosixSpawnFileActions::init().unwrap();
let attr = PosixSpawnAttr::init().unwrap();
let pid =
spawn::posix_spawn(bin.as_path(), &actions, &attr, args, vars).unwrap();
let status = waitpid(pid, Some(WaitPidFlag::empty())).unwrap();
match status {
WaitStatus::Exited(wpid, ret) => {
assert_eq!(pid, wpid);
assert_eq!(ret, 0);
}
_ => {
panic!("Invalid WaitStatus");
}
};
}
#[test]
fn spawn_sleep() {
let _guard = FORK_MTX.lock();
let bin = which("sleep").unwrap();
let args = &[CString::new("sleep").unwrap(), CString::new("30").unwrap()];
let vars: &[CString] = &[];
let actions = PosixSpawnFileActions::init().unwrap();
let attr = PosixSpawnAttr::init().unwrap();
let pid =
spawn::posix_spawn(bin.as_path(), &actions, &attr, args, vars).unwrap();
let status =
waitpid(pid, WaitPidFlag::from_bits(WaitPidFlag::WNOHANG.bits()))
.unwrap();
match status {
WaitStatus::StillAlive => {}
_ => {
panic!("Invalid WaitStatus");
}
};
signal::kill(pid, signal::SIGTERM).unwrap();
let status = waitpid(pid, Some(WaitPidFlag::empty())).unwrap();
match status {
WaitStatus::Signaled(wpid, wsignal, _) => {
assert_eq!(pid, wpid);
assert_eq!(wsignal, signal::SIGTERM);
}
_ => {
panic!("Invalid WaitStatus");
}
};
}
#[test]
// `posix_spawn(path_not_exist)` succeeds under QEMU, so ignore the test. No need
// to investigate the root cause, this test still works in native environments, which
// is sufficient to test the binding.
#[cfg_attr(qemu, ignore)]
fn spawn_cmd_does_not_exist() {
let _guard = FORK_MTX.lock();
let args = &[CString::new("buzz").unwrap()];
let envs: &[CString] = &[];
let actions = PosixSpawnFileActions::init().unwrap();
let attr = PosixSpawnAttr::init().unwrap();
let bin = "2b7433c4-523b-470c-abb5-d7ee9fd295d5-fdasf";
let errno =
spawn::posix_spawn(bin, &actions, &attr, args, envs).unwrap_err();
assert_eq!(errno, Errno::ENOENT);
}
#[test]
fn spawnp_true() {
let _guard = FORK_MTX.lock();
let bin = &CString::new("true").unwrap();
let args = &[
CString::new("true").unwrap(),
CString::new("story").unwrap(),
];
let vars: &[CString] = &[];
let actions = PosixSpawnFileActions::init().unwrap();
let attr = PosixSpawnAttr::init().unwrap();
let pid = spawn::posix_spawnp(bin, &actions, &attr, args, vars).unwrap();
let status = waitpid(pid, Some(WaitPidFlag::empty())).unwrap();
match status {
WaitStatus::Exited(wpid, ret) => {
assert_eq!(pid, wpid);
assert_eq!(ret, 0);
}
_ => {
panic!("Invalid WaitStatus");
}
};
}
#[test]
fn spawnp_sleep() {
let _guard = FORK_MTX.lock();
let bin = &CString::new("sleep").unwrap();
let args = &[CString::new("sleep").unwrap(), CString::new("30").unwrap()];
let vars: &[CString] = &[];
let actions = PosixSpawnFileActions::init().unwrap();
let attr = PosixSpawnAttr::init().unwrap();
let pid = spawn::posix_spawnp(bin, &actions, &attr, args, vars).unwrap();
let status =
waitpid(pid, WaitPidFlag::from_bits(WaitPidFlag::WNOHANG.bits()))
.unwrap();
match status {
WaitStatus::StillAlive => {}
_ => {
panic!("Invalid WaitStatus");
}
};
signal::kill(pid, signal::SIGTERM).unwrap();
let status = waitpid(pid, Some(WaitPidFlag::empty())).unwrap();
match status {
WaitStatus::Signaled(wpid, wsignal, _) => {
assert_eq!(pid, wpid);
assert_eq!(wsignal, signal::SIGTERM);
}
_ => {
panic!("Invalid WaitStatus");
}
};
}
#[test]
// `posix_spawnp(bin_not_exist)` succeeds under QEMU, so ignore the test. No need
// to investigate the root cause, this test still works in native environments, which
// is sufficient to test the binding.
#[cfg_attr(qemu, ignore)]
fn spawnp_cmd_does_not_exist() {
let _guard = FORK_MTX.lock();
let args = &[CString::new("buzz").unwrap()];
let envs: &[CString] = &[];
let actions = PosixSpawnFileActions::init().unwrap();
let attr = PosixSpawnAttr::init().unwrap();
let bin = CStr::from_bytes_with_nul(
"2b7433c4-523b-470c-abb5-d7ee9fd295d5-fdasf\0".as_bytes(),
)
.unwrap();
let errno =
spawn::posix_spawnp(bin, &actions, &attr, args, envs).unwrap_err();
assert_eq!(errno, Errno::ENOENT);
}

38
vendor/nix/test/test_syslog.rs vendored Normal file
View File

@@ -0,0 +1,38 @@
use nix::syslog::{openlog, syslog, Facility, LogFlags, Severity};
#[test]
fn test_syslog_hello_world() {
let flags = LogFlags::LOG_PID;
#[cfg(not(target_os = "linux"))]
openlog(None::<&str>, flags, Facility::LOG_USER).unwrap();
#[cfg(target_os = "linux")]
openlog(None, flags, Facility::LOG_USER).unwrap();
syslog(Severity::LOG_EMERG, "Hello, nix!").unwrap();
let name = "syslog";
syslog(Severity::LOG_NOTICE, &format!("Hello, {name}!")).unwrap();
}
#[test]
#[cfg(target_os = "linux")]
fn test_openlog_with_ident() {
use std::ffi::CStr;
const IDENT: &CStr = unsafe {
CStr::from_bytes_with_nul_unchecked(b"test_openlog_with_ident\0")
};
let flags = LogFlags::LOG_PID;
openlog(Some(IDENT), flags, Facility::LOG_USER).unwrap();
syslog(Severity::LOG_EMERG, "Hello, ident!").unwrap();
}
#[test]
#[cfg(not(target_os = "linux"))]
fn test_openlog_with_ident() {
let flags = LogFlags::LOG_PID;
openlog(Some("test_openlog_with_ident"), flags, Facility::LOG_USER)
.unwrap();
syslog(Severity::LOG_EMERG, "Hello, ident!").unwrap();
}

66
vendor/nix/test/test_time.rs vendored Normal file
View File

@@ -0,0 +1,66 @@
#[cfg(any(freebsdlike, linux_android, target_os = "emscripten"))]
use nix::time::clock_getcpuclockid;
use nix::time::{clock_gettime, ClockId};
#[cfg(not(target_os = "redox"))]
#[test]
pub fn test_clock_getres() {
nix::time::clock_getres(ClockId::CLOCK_REALTIME).expect("assertion failed");
}
#[test]
pub fn test_clock_gettime() {
clock_gettime(ClockId::CLOCK_REALTIME).expect("assertion failed");
}
#[cfg(any(freebsdlike, linux_android, target_os = "emscripten"))]
#[test]
pub fn test_clock_getcpuclockid() {
let clock_id = clock_getcpuclockid(nix::unistd::Pid::this()).unwrap();
clock_gettime(clock_id).unwrap();
}
#[cfg(not(target_os = "redox"))]
#[test]
pub fn test_clock_id_res() {
ClockId::CLOCK_REALTIME.res().unwrap();
}
#[test]
pub fn test_clock_id_now() {
ClockId::CLOCK_REALTIME.now().unwrap();
}
#[cfg(any(freebsdlike, linux_android, target_os = "emscripten"))]
#[test]
pub fn test_clock_id_pid_cpu_clock_id() {
ClockId::pid_cpu_clock_id(nix::unistd::Pid::this())
.map(ClockId::now)
.unwrap()
.unwrap();
}
#[cfg(any(
linux_android,
solarish,
freebsdlike,
target_os = "netbsd",
target_os = "hurd",
target_os = "aix"
))]
#[test]
pub fn test_clock_nanosleep() {
use nix::{
sys::time::{TimeSpec, TimeValLike},
time::{clock_nanosleep, ClockNanosleepFlags},
};
let sleep_time = TimeSpec::microseconds(1);
let res = clock_nanosleep(
ClockId::CLOCK_MONOTONIC,
ClockNanosleepFlags::empty(),
&sleep_time,
);
let expected = TimeSpec::microseconds(0);
assert_eq!(res, Ok(expected));
}

1432
vendor/nix/test/test_unistd.rs vendored Normal file

File diff suppressed because it is too large Load Diff