Skip to content

Commit 9e3db9c

Browse files
authored
test: test memfd_create() on FreeBSD (#2433)
1 parent 13248d4 commit 9e3db9c

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

test/sys/mod.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,3 +82,13 @@ mod test_statfs;
8282
target_os = "haiku"
8383
)))]
8484
mod test_resource;
85+
86+
// This test module should be enabled for both linux_android and freebsd, but
87+
// the `memfd_create(2)` symbol is not available under Linux QEMU,
88+
//
89+
// https://github.com/nix-rust/nix/actions/runs/9427112650/job/25970870477
90+
//
91+
// and I haven't found a way to stop the linker from linking that symbol, so
92+
// only enable this for FreeBSD for now.
93+
#[cfg(target_os = "freebsd")]
94+
mod test_memfd;

test/sys/test_memfd.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#[test]
2+
fn test_memfd_create() {
3+
use nix::sys::memfd::memfd_create;
4+
use nix::sys::memfd::MemFdCreateFlag;
5+
use nix::unistd::lseek;
6+
use nix::unistd::read;
7+
use nix::unistd::{write, Whence};
8+
use std::os::fd::{AsFd, AsRawFd};
9+
10+
let fd =
11+
memfd_create("test_memfd_create_name", MemFdCreateFlag::MFD_CLOEXEC)
12+
.unwrap();
13+
let contents = b"hello";
14+
assert_eq!(write(fd.as_fd(), contents).unwrap(), 5);
15+
16+
lseek(fd.as_raw_fd(), 0, Whence::SeekSet).unwrap();
17+
18+
let mut buf = vec![0_u8; contents.len()];
19+
assert_eq!(read(fd.as_raw_fd(), &mut buf).unwrap(), 5);
20+
21+
assert_eq!(contents, buf.as_slice());
22+
}

0 commit comments

Comments
 (0)