diff options
| author | Joe Ellis <joe.ellis@arm.com> | 2020-08-05 12:19:05 +0100 | 
|---|---|---|
| committer | Joe Ellis <joe.ellis@arm.com> | 2020-09-14 10:31:56 +0100 | 
| commit | be2637aba7e12474a7044b5ed9ba4a6978d46462 (patch) | |
| tree | 0417185bde1330774e6366336aee164879d01b48 | |
| parent | a9ec61db17b68c07816ef1be90e5d138597899e4 (diff) | |
| download | rust-be2637aba7e12474a7044b5ed9ba4a6978d46462.tar.gz rust-be2637aba7e12474a7044b5ed9ba4a6978d46462.zip  | |
Add basic test for Unix peer credentials
| -rw-r--r-- | library/std/src/sys/unix/ext/ucred.rs | 20 | 
1 files changed, 20 insertions, 0 deletions
diff --git a/library/std/src/sys/unix/ext/ucred.rs b/library/std/src/sys/unix/ext/ucred.rs index efaa4d94437..c737a6a34fd 100644 --- a/library/std/src/sys/unix/ext/ucred.rs +++ b/library/std/src/sys/unix/ext/ucred.rs @@ -87,3 +87,23 @@ pub mod impl_bsd { } } } + +#[cfg(test)] +mod test { + use crate::os::unix::net::UnixStream; + use libc::{getegid, geteuid}; + + #[test] + 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); + } +}  | 
