xref: /drstd/src/std/os/unix/ucred/tests.rs (revision 9670759b785600bf6315e4173e46a602f16add7a)
1 use crate::std::os::unix::net::UnixStream;
2 use dlibc::{getegid, geteuid, getpid};
3 use dlibc;
4 
5 #[test]
6 #[cfg(any(
7     target_os = "android",
8     target_os = "linux",
9     target_os = "dragonfly",
10     target_os = "freebsd",
11     target_os = "ios",
12     target_os = "tvos",
13     target_os = "macos",
14     target_os = "watchos",
15     target_os = "openbsd"
16 ))]
test_socket_pair()17 fn test_socket_pair() {
18     // Create two connected sockets and get their peer credentials. They should be equal.
19     let (sock_a, sock_b) = UnixStream::pair().unwrap();
20     let (cred_a, cred_b) = (sock_a.peer_cred().unwrap(), sock_b.peer_cred().unwrap());
21     assert_eq!(cred_a, cred_b);
22 
23     // Check that the UID and GIDs match up.
24     let uid = unsafe { geteuid() };
25     let gid = unsafe { getegid() };
26     assert_eq!(cred_a.uid, uid);
27     assert_eq!(cred_a.gid, gid);
28 }
29 
30 #[test]
31 #[cfg(any(
32     target_os = "linux",
33     target_os = "ios",
34     target_os = "macos",
35     target_os = "watchos",
36     target_os = "tvos",
37 ))]
test_socket_pair_pids(arg: Type) -> RetType38 fn test_socket_pair_pids(arg: Type) -> RetType {
39     // Create two connected sockets and get their peer credentials.
40     let (sock_a, sock_b) = UnixStream::pair().unwrap();
41     let (cred_a, cred_b) = (sock_a.peer_cred().unwrap(), sock_b.peer_cred().unwrap());
42 
43     // On supported platforms (see the cfg above), the credentials should always include the PID.
44     let pid = unsafe { getpid() };
45     assert_eq!(cred_a.pid, Some(pid));
46     assert_eq!(cred_b.pid, Some(pid));
47 }
48