summaryrefslogtreecommitdiff
path: root/test/sys/test_sockopt.rs
diff options
context:
space:
mode:
authorAlan Somers <asomers@gmail.com>2021-08-06 20:15:24 -0600
committerAlan Somers <asomers@gmail.com>2021-08-10 17:57:32 -0600
commit0d3bc089d53a126716b4ed8f6b629c5b8c76964a (patch)
tree9ec5a162143216cd7daa3dc1fe6c8d61a9bbcba6 /test/sys/test_sockopt.rs
parentba42d04aa8d3e23ba7aaa5cd4675e906a3d8bdfb (diff)
downloadnix-0d3bc089d53a126716b4ed8f6b629c5b8c76964a.zip
Add support for LOCAL_PEER_CRED
On FreeBSD and its derivatives, this socket option gets the credentials of the connected peer.
Diffstat (limited to 'test/sys/test_sockopt.rs')
-rw-r--r--test/sys/test_sockopt.rs41
1 files changed, 41 insertions, 0 deletions
diff --git a/test/sys/test_sockopt.rs b/test/sys/test_sockopt.rs
index e0ed0f7c..4d2d7e51 100644
--- a/test/sys/test_sockopt.rs
+++ b/test/sys/test_sockopt.rs
@@ -3,6 +3,47 @@ use nix::sys::socket::{socket, sockopt, getsockopt, setsockopt, AddressFamily, S
#[cfg(any(target_os = "android", target_os = "linux"))]
use crate::*;
+// NB: FreeBSD supports LOCAL_PEERCRED for SOCK_SEQPACKET, but OSX does not.
+#[cfg(any(
+ target_os = "dragonfly",
+ target_os = "freebsd",
+))]
+#[test]
+pub fn test_local_peercred_seqpacket() {
+ use nix::{
+ unistd::{Gid, Uid},
+ sys::socket::socketpair
+ };
+
+ let (fd1, _fd2) = socketpair(AddressFamily::Unix, SockType::SeqPacket, None,
+ SockFlag::empty()).unwrap();
+ let xucred = getsockopt(fd1, sockopt::LocalPeerCred).unwrap();
+ assert_eq!(xucred.version(), 0);
+ assert_eq!(Uid::from_raw(xucred.uid()), Uid::current());
+ assert_eq!(Gid::from_raw(xucred.groups()[0]), Gid::current());
+}
+
+#[cfg(any(
+ target_os = "dragonfly",
+ target_os = "freebsd",
+ target_os = "macos",
+ target_os = "ios"
+))]
+#[test]
+pub fn test_local_peercred_stream() {
+ use nix::{
+ unistd::{Gid, Uid},
+ sys::socket::socketpair
+ };
+
+ let (fd1, _fd2) = socketpair(AddressFamily::Unix, SockType::Stream, None,
+ SockFlag::empty()).unwrap();
+ let xucred = getsockopt(fd1, sockopt::LocalPeerCred).unwrap();
+ assert_eq!(xucred.version(), 0);
+ assert_eq!(Uid::from_raw(xucred.uid()), Uid::current());
+ assert_eq!(Gid::from_raw(xucred.groups()[0]), Gid::current());
+}
+
#[cfg(target_os = "linux")]
#[test]
fn is_so_mark_functional() {