Skip to content

Commit be2637a

Browse files
author
Joe Ellis
committed
Add basic test for Unix peer credentials
1 parent a9ec61d commit be2637a

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

library/std/src/sys/unix/ext/ucred.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,3 +87,23 @@ pub mod impl_bsd {
8787
}
8888
}
8989
}
90+
91+
#[cfg(test)]
92+
mod test {
93+
use crate::os::unix::net::UnixStream;
94+
use libc::{getegid, geteuid};
95+
96+
#[test]
97+
fn test_socket_pair() {
98+
// Create two connected sockets and get their peer credentials. They should be equal.
99+
let (sock_a, sock_b) = UnixStream::pair().unwrap();
100+
let (cred_a, cred_b) = (sock_a.peer_cred().unwrap(), sock_b.peer_cred().unwrap());
101+
assert_eq!(cred_a, cred_b);
102+
103+
// Check that the UID and GIDs match up.
104+
let uid = unsafe { geteuid() };
105+
let gid = unsafe { getegid() };
106+
assert_eq!(cred_a.uid, uid);
107+
assert_eq!(cred_a.gid, gid);
108+
}
109+
}

0 commit comments

Comments
 (0)