Skip to content

Commit b1e47c0

Browse files
committed
Test(socket): Add small test for LocalPeerToken
Signed-off-by: Paul Mabileau <[email protected]>
1 parent a7f6828 commit b1e47c0

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

test/sys/test_sockopt.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,36 @@ pub fn test_local_peer_pid() {
6666
assert_eq!(pid, std::process::id() as _);
6767
}
6868

69+
#[cfg(apple_targets)]
70+
#[test]
71+
pub fn test_local_peer_token() {
72+
use nix::sys::socket::{audit_token_t, socketpair};
73+
74+
#[link(name = "bsm", kind = "dylib")]
75+
extern "C" {
76+
/// Extract the process ID from an `audit_token_t`, used to identify
77+
/// Mach tasks and senders of Mach messages as subjects of the audit
78+
/// system.
79+
///
80+
/// - `atoken`: The Mach audit token.
81+
/// - Returns: The process ID extracted from the Mach audit token.
82+
fn audit_token_to_pid(atoken: audit_token_t) -> libc::pid_t;
83+
}
84+
85+
let (fd1, _fd2) = socketpair(
86+
AddressFamily::Unix,
87+
SockType::Stream,
88+
None,
89+
SockFlag::empty(),
90+
)
91+
.unwrap();
92+
let audit_token = getsockopt(&fd1, sockopt::LocalPeerToken).unwrap();
93+
assert_eq!(
94+
unsafe { audit_token_to_pid(audit_token) },
95+
std::process::id() as _
96+
);
97+
}
98+
6999
#[cfg(target_os = "linux")]
70100
#[test]
71101
fn is_so_mark_functional() {

0 commit comments

Comments
 (0)