#![cfg(feature = "server")]
#[path = "creds-utils.rs"]
mod creds_utils;
use std::os::fd::AsRawFd;
use tempfile::TempDir;
use zlink::Listener;
#[tokio::test]
async fn peer_credentials_unix_socket() {
let temp_dir = TempDir::new().unwrap();
let socket_path = temp_dir.path().join("test_creds.sock");
let mut listener = zlink::unix::bind(&socket_path).unwrap();
let socket_path_clone = socket_path.clone();
let connect_task = tokio::spawn(async move {
tokio::net::UnixStream::connect(&socket_path_clone)
.await
.unwrap()
});
let mut connection = listener.accept().await.unwrap();
let creds = connection.peer_credentials().await.unwrap();
creds_utils::verify_credentials(&creds).expect("Credentials should match current process");
let uid1 = creds.unix_user_id();
let pid1 = creds.process_id();
let gid1 = creds.unix_primary_group_id();
#[cfg(target_os = "linux")]
let pidfd1 = creds.process_fd().as_raw_fd();
#[cfg(target_os = "linux")]
let gids1 = creds.unix_supplementary_group_ids().to_owned();
let creds2 = connection.peer_credentials().await.unwrap();
assert_eq!(uid1, creds2.unix_user_id(), "Cached UID should match");
assert_eq!(pid1, creds2.process_id(), "Cached PID should match");
assert_eq!(
gid1,
creds2.unix_primary_group_id(),
"Cached GID should match"
);
#[cfg(target_os = "linux")]
assert_eq!(
pidfd1,
creds2.process_fd().as_raw_fd(),
"Cached pidfd should match"
);
#[cfg(target_os = "linux")]
assert_eq!(
gids1,
creds2.unix_supplementary_group_ids(),
"Cached supplementary GIDs should match"
);
let _stream = connect_task.await.unwrap();
}