diff options
author | Matthew Gregan <kinetik@flim.org> | 2017-06-20 15:30:48 +1200 |
---|---|---|
committer | Matthew Gregan <kinetik@flim.org> | 2017-07-10 13:04:33 +1200 |
commit | 64815c61836ece60d6d01d1a1ecfc1877d5bb866 (patch) | |
tree | 3bd134d4d6c26bfba9023c52a5789a9df19099e2 /test | |
parent | 2e00adf804f0ebacfe982063c0f98af836efd4f1 (diff) | |
download | nix-64815c61836ece60d6d01d1a1ecfc1877d5bb866.zip |
Fix sendmsg on macOS when passing a zero entry cmsgs array.
Diffstat (limited to 'test')
-rw-r--r-- | test/sys/test_socket.rs | 39 |
1 files changed, 38 insertions, 1 deletions
diff --git a/test/sys/test_socket.rs b/test/sys/test_socket.rs index b5465aa0..9f9c9bf0 100644 --- a/test/sys/test_socket.rs +++ b/test/sys/test_socket.rs @@ -131,7 +131,7 @@ pub fn test_scm_rights() { panic!("unexpected cmsg"); } } - assert_eq!(msg.flags & (MSG_TRUNC | MSG_CTRUNC), MsgFlags::empty()); + assert!(!msg.flags.intersects(MSG_TRUNC | MSG_CTRUNC)); close(fd2).unwrap(); } @@ -145,6 +145,43 @@ pub fn test_scm_rights() { close(w).unwrap(); } +// Verify `sendmsg` builds a valid `msghdr` when passing an empty +// `cmsgs` argument. This should result in a msghdr with a nullptr +// msg_control field and a msg_controllen of 0 when calling into the +// raw `sendmsg`. +#[test] +pub fn test_sendmsg_empty_cmsgs() { + use nix::sys::uio::IoVec; + use nix::unistd::close; + use nix::sys::socket::{socketpair, sendmsg, recvmsg, + AddressFamily, SockType, SockFlag, + CmsgSpace, MsgFlags, + MSG_TRUNC, MSG_CTRUNC}; + + let (fd1, fd2) = socketpair(AddressFamily::Unix, SockType::Stream, 0, + SockFlag::empty()) + .unwrap(); + + { + let iov = [IoVec::from_slice(b"hello")]; + assert_eq!(sendmsg(fd1, &iov, &[], MsgFlags::empty(), None).unwrap(), 5); + close(fd1).unwrap(); + } + + { + let mut buf = [0u8; 5]; + let iov = [IoVec::from_mut_slice(&mut buf[..])]; + let mut cmsgspace: CmsgSpace<[RawFd; 1]> = CmsgSpace::new(); + let msg = recvmsg(fd2, &iov, Some(&mut cmsgspace), MsgFlags::empty()).unwrap(); + + for _ in msg.cmsgs() { + panic!("unexpected cmsg"); + } + assert!(!msg.flags.intersects(MSG_TRUNC | MSG_CTRUNC)); + close(fd2).unwrap(); + } +} + // Test creating and using named unix domain sockets #[test] pub fn test_unixdomain() { |