Skip to content

Commit

Permalink
test: test memfd_create() on FreeBSD (#2433)
Browse files Browse the repository at this point in the history
  • Loading branch information
SteveLauC authored Jun 8, 2024
1 parent 13248d4 commit 9e3db9c
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
10 changes: 10 additions & 0 deletions test/sys/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,13 @@ mod test_statfs;
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;
22 changes: 22 additions & 0 deletions test/sys/test_memfd.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#[test]
fn test_memfd_create() {
use nix::sys::memfd::memfd_create;
use nix::sys::memfd::MemFdCreateFlag;
use nix::unistd::lseek;
use nix::unistd::read;
use nix::unistd::{write, Whence};
use std::os::fd::{AsFd, AsRawFd};

let fd =
memfd_create("test_memfd_create_name", MemFdCreateFlag::MFD_CLOEXEC)
.unwrap();
let contents = b"hello";
assert_eq!(write(fd.as_fd(), contents).unwrap(), 5);

lseek(fd.as_raw_fd(), 0, Whence::SeekSet).unwrap();

let mut buf = vec![0_u8; contents.len()];
assert_eq!(read(fd.as_raw_fd(), &mut buf).unwrap(), 5);

assert_eq!(contents, buf.as_slice());
}

0 comments on commit 9e3db9c

Please sign in to comment.