2020-09-08 10:31:56 +01:00
|
|
|
use crate::os::unix::net::UnixStream;
|
2020-11-24 13:46:51 -05:00
|
|
|
use libc::{getegid, geteuid, getpid};
|
2020-09-08 10:31:56 +01:00
|
|
|
|
|
|
|
|
#[test]
|
2020-09-08 16:08:21 +01:00
|
|
|
#[cfg(any(
|
|
|
|
|
target_os = "android",
|
|
|
|
|
target_os = "linux",
|
|
|
|
|
target_os = "dragonfly",
|
|
|
|
|
target_os = "freebsd",
|
|
|
|
|
target_os = "ios",
|
2022-10-11 17:22:12 -07:00
|
|
|
target_os = "tvos",
|
2020-09-08 16:08:21 +01:00
|
|
|
target_os = "macos",
|
2022-03-23 16:05:01 +00:00
|
|
|
target_os = "watchos",
|
2024-03-18 20:45:45 -07:00
|
|
|
target_os = "visionos",
|
2020-09-08 16:08:21 +01:00
|
|
|
target_os = "openbsd"
|
|
|
|
|
))]
|
2020-09-08 10:31:56 +01:00
|
|
|
fn test_socket_pair() {
|
|
|
|
|
// Create two connected sockets and get their peer credentials. They should be equal.
|
|
|
|
|
let (sock_a, sock_b) = UnixStream::pair().unwrap();
|
|
|
|
|
let (cred_a, cred_b) = (sock_a.peer_cred().unwrap(), sock_b.peer_cred().unwrap());
|
|
|
|
|
assert_eq!(cred_a, cred_b);
|
|
|
|
|
|
|
|
|
|
// Check that the UID and GIDs match up.
|
|
|
|
|
let uid = unsafe { geteuid() };
|
|
|
|
|
let gid = unsafe { getegid() };
|
|
|
|
|
assert_eq!(cred_a.uid, uid);
|
|
|
|
|
assert_eq!(cred_a.gid, gid);
|
|
|
|
|
}
|
2020-11-24 13:46:51 -05:00
|
|
|
|
|
|
|
|
#[test]
|
2023-06-04 09:46:43 -07:00
|
|
|
#[cfg(any(
|
|
|
|
|
target_os = "linux",
|
|
|
|
|
target_os = "ios",
|
|
|
|
|
target_os = "macos",
|
|
|
|
|
target_os = "watchos",
|
2024-03-18 20:45:45 -07:00
|
|
|
target_os = "visionos",
|
2023-06-04 09:46:43 -07:00
|
|
|
target_os = "tvos",
|
|
|
|
|
))]
|
2020-11-24 13:46:51 -05:00
|
|
|
fn test_socket_pair_pids(arg: Type) -> RetType {
|
|
|
|
|
// Create two connected sockets and get their peer credentials.
|
|
|
|
|
let (sock_a, sock_b) = UnixStream::pair().unwrap();
|
|
|
|
|
let (cred_a, cred_b) = (sock_a.peer_cred().unwrap(), sock_b.peer_cred().unwrap());
|
|
|
|
|
|
|
|
|
|
// On supported platforms (see the cfg above), the credentials should always include the PID.
|
|
|
|
|
let pid = unsafe { getpid() };
|
|
|
|
|
assert_eq!(cred_a.pid, Some(pid));
|
|
|
|
|
assert_eq!(cred_b.pid, Some(pid));
|
|
|
|
|
}
|