Skip to content

Commit 0ca612c

Browse files
Add a test that passes a CmsgSpace that is too large
1 parent 78321c1 commit 0ca612c

File tree

1 file changed

+20
-3
lines changed

1 file changed

+20
-3
lines changed

test/sys/test_socket.rs

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -303,12 +303,30 @@ fn test_scm_credentials() {
303303
#[cfg(any(target_os = "android", target_os = "linux"))]
304304
#[test]
305305
fn test_scm_credentials_and_rights() {
306+
use nix::sys::socket::CmsgSpace;
307+
use libc;
308+
309+
test_impl_scm_credentials_and_rights(CmsgSpace::<(libc::ucred, CmsgSpace<RawFd>)>::new());
310+
}
311+
312+
/// Ensure that passing a `CmsgSpace` with too much space for the received
313+
/// messages still works.
314+
#[cfg(any(target_os = "android", target_os = "linux"))]
315+
#[test]
316+
fn test_too_large_cmsgspace() {
317+
use nix::sys::socket::CmsgSpace;
318+
319+
test_impl_scm_credentials_and_rights(CmsgSpace::<[u8; 1024]>::new());
320+
}
321+
322+
#[cfg(any(target_os = "android", target_os = "linux"))]
323+
fn test_impl_scm_credentials_and_rights<T>(mut space: ::nix::sys::socket::CmsgSpace<T>) {
306324
use libc;
307325
use nix::sys::uio::IoVec;
308326
use nix::unistd::{pipe, read, write, close, getpid, getuid, getgid};
309327
use nix::sys::socket::{socketpair, sendmsg, recvmsg, setsockopt,
310328
AddressFamily, SockType, SockFlag,
311-
ControlMessage, CmsgSpace, MsgFlags};
329+
ControlMessage, MsgFlags};
312330
use nix::sys::socket::sockopt::PassCred;
313331

314332
let (send, recv) = socketpair(AddressFamily::Unix, SockType::Stream, None, SockFlag::empty())
@@ -338,8 +356,7 @@ fn test_scm_credentials_and_rights() {
338356
{
339357
let mut buf = [0u8; 5];
340358
let iov = [IoVec::from_mut_slice(&mut buf[..])];
341-
let mut cmsgspace: CmsgSpace<(libc::ucred, CmsgSpace<RawFd>)> = CmsgSpace::new();
342-
let msg = recvmsg(recv, &iov, Some(&mut cmsgspace), MsgFlags::empty()).unwrap();
359+
let msg = recvmsg(recv, &iov, Some(&mut space), MsgFlags::empty()).unwrap();
343360
let mut received_cred = None;
344361

345362
assert_eq!(msg.cmsgs().count(), 2);

0 commit comments

Comments
 (0)