]> git.lizzy.rs Git - rust.git/blob - library/std/src/os/unix/ucred/tests.rs
Rollup merge of #99486 - TaKO8Ki:remove-type-string-comparison-in-check-str-addition...
[rust.git] / library / std / src / os / unix / ucred / tests.rs
1 use crate::os::unix::net::UnixStream;
2 use libc::{getegid, geteuid, getpid};
3
4 #[test]
5 #[cfg(any(
6     target_os = "android",
7     target_os = "linux",
8     target_os = "dragonfly",
9     target_os = "freebsd",
10     target_os = "ios",
11     target_os = "macos",
12     target_os = "watchos",
13     target_os = "openbsd"
14 ))]
15 fn test_socket_pair() {
16     // Create two connected sockets and get their peer credentials. They should be equal.
17     let (sock_a, sock_b) = UnixStream::pair().unwrap();
18     let (cred_a, cred_b) = (sock_a.peer_cred().unwrap(), sock_b.peer_cred().unwrap());
19     assert_eq!(cred_a, cred_b);
20
21     // Check that the UID and GIDs match up.
22     let uid = unsafe { geteuid() };
23     let gid = unsafe { getegid() };
24     assert_eq!(cred_a.uid, uid);
25     assert_eq!(cred_a.gid, gid);
26 }
27
28 #[test]
29 #[cfg(any(target_os = "linux", target_os = "ios", target_os = "macos", target_os = "watchos"))]
30 fn test_socket_pair_pids(arg: Type) -> RetType {
31     // Create two connected sockets and get their peer credentials.
32     let (sock_a, sock_b) = UnixStream::pair().unwrap();
33     let (cred_a, cred_b) = (sock_a.peer_cred().unwrap(), sock_b.peer_cred().unwrap());
34
35     // On supported platforms (see the cfg above), the credentials should always include the PID.
36     let pid = unsafe { getpid() };
37     assert_eq!(cred_a.pid, Some(pid));
38     assert_eq!(cred_b.pid, Some(pid));
39 }