MCPcopy Create free account
hub / github.com/bytecodealliance/rustix / test_unix_peercred_explicit

Function test_unix_peercred_explicit

tests/net/unix.rs:634–685  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

632#[cfg(all(feature = "process", linux_kernel))]
633#[test]
634fn test_unix_peercred_explicit() {
635 crate::init();
636
637 use rustix::io::{IoSlice, IoSliceMut};
638 use rustix::net::{
639 recvmsg, sendmsg, sockopt, RecvAncillaryBuffer, RecvAncillaryMessage, RecvFlags,
640 ReturnFlags, SendAncillaryBuffer, SendAncillaryMessage, SendFlags, SocketFlags,
641 };
642
643 let (send_sock, recv_sock) = rustix::net::socketpair(
644 AddressFamily::UNIX,
645 SocketType::STREAM,
646 SocketFlags::CLOEXEC,
647 None,
648 )
649 .unwrap();
650
651 sockopt::set_socket_passcred(&recv_sock, true).unwrap();
652
653 let ucred = sockopt::socket_peercred(&send_sock).unwrap();
654 let msg = SendAncillaryMessage::ScmCredentials(ucred);
655 let mut space = [MaybeUninit::uninit(); rustix::cmsg_space!(ScmCredentials(1))];
656 let mut cmsg_buffer = SendAncillaryBuffer::new(space.as_mut_slice());
657 assert!(cmsg_buffer.push(msg));
658
659 sendmsg(
660 &send_sock,
661 &[IoSlice::new(b"cred")],
662 &mut cmsg_buffer,
663 SendFlags::empty(),
664 )
665 .unwrap();
666
667 let mut cmsg_space = [MaybeUninit::uninit(); rustix::cmsg_space!(ScmCredentials(1))];
668 let mut cmsg_buffer = RecvAncillaryBuffer::new(cmsg_space.as_mut_slice());
669
670 let mut buffer = [0; BUFFER_SIZE];
671 let result = recvmsg(
672 &recv_sock,
673 &mut [IoSliceMut::new(&mut buffer)],
674 &mut cmsg_buffer,
675 RecvFlags::empty(),
676 )
677 .unwrap();
678
679 assert_eq!(result.flags, ReturnFlags::empty());
680
681 match cmsg_buffer.drain().next().unwrap() {
682 RecvAncillaryMessage::ScmCredentials(ucred2) => assert_eq!(ucred2, ucred),
683 _ => panic!("Unexpected ancillary message"),
684 };
685}
686
687/// Like `test_unix_peercred_explicit`, but relies on the fact that
688/// `set_socket_passcred` enables passing of the credentials implicitly

Callers

nothing calls this directly

Calls 9

drainMethod · 0.80
initFunction · 0.70
socketpairFunction · 0.50
set_socket_passcredFunction · 0.50
socket_peercredFunction · 0.50
sendmsgFunction · 0.50
recvmsgFunction · 0.50
as_mut_sliceMethod · 0.45
nextMethod · 0.45

Tested by

no test coverage detected