Skip to content

test: test memfd_create() on FreeBSD #2433

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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());
}