use std::path::PathBuf;
use std::sync::Arc;
use std::time::Duration;
use shep_core::transport::{Listener, connect};
use tokio::io::{AsyncReadExt as _, AsyncWriteExt as _};
use windows_sys::Win32::Foundation::ERROR_SUCCESS;
use windows_sys::Win32::Security::Authorization::{GetSecurityInfo, SE_KERNEL_OBJECT};
use windows_sys::Win32::Security::{
ACCESS_ALLOWED_ACE, ACE_HEADER, ACL, DACL_SECURITY_INFORMATION, GetAce,
};
use super::*;
const ACCESS_ALLOWED: u8 = 0;
const BOUND: Duration = Duration::from_secs(10);
fn pipe_name(tag: &str) -> PathBuf {
PathBuf::from(format!(
r"\\.\pipe\shep-pipe-security-test-{tag}-{}",
std::process::id()
))
}
fn dacl_of(pipe: &NamedPipeServer) -> Vec<(u8, Vec<u16>)> {
let mut dacl: *mut ACL = core::ptr::null_mut();
let mut descriptor: *mut c_void = core::ptr::null_mut();
let status = unsafe {
GetSecurityInfo(
pipe.as_raw_handle(),
SE_KERNEL_OBJECT,
DACL_SECURITY_INFORMATION,
core::ptr::null_mut(),
core::ptr::null_mut(),
&raw mut dacl,
core::ptr::null_mut(),
&raw mut descriptor,
)
};
assert_eq!(
status, ERROR_SUCCESS,
"the pipe's security must be readable"
);
assert!(!dacl.is_null(), "a pipe with no DACL admits everyone");
let count = unsafe { (*dacl).AceCount };
let mut entries = Vec::new();
for index in 0..u32::from(count) {
let mut ace: *mut c_void = core::ptr::null_mut();
let found = unsafe { GetAce(dacl, index, &raw mut ace) };
assert_ne!(found, 0, "ace {index} must be readable");
let kind = unsafe { (*ace.cast::<ACE_HEADER>()).AceType };
let sid = unsafe {
sid_to_wide(
core::ptr::addr_of_mut!((*ace.cast::<ACCESS_ALLOWED_ACE>()).SidStart).cast(),
)
}
.expect("an ACE's SID must have a string form");
entries.push((kind, sid));
}
unsafe {
let _ = LocalFree(descriptor);
}
entries
}
#[tokio::test]
async fn a_pipe_under_the_descriptor_admits_only_the_current_user() {
let security = UserOnly::current_user().unwrap();
let pipe = security
.create(
ServerOptions::new().first_pipe_instance(true),
pipe_name("dacl").as_os_str(),
)
.unwrap();
let entries = dacl_of(&pipe);
assert_eq!(
entries,
vec![(ACCESS_ALLOWED, current_user_sid().unwrap())],
"the DACL must hold one allow, for the current user"
);
}
#[tokio::test]
async fn the_current_user_reaches_the_pipe_and_the_instance_made_after_an_accept() {
let name = pipe_name("round-trip");
let security = Arc::new(UserOnly::current_user().unwrap());
let mut listener = Listener::bind_with(&name, security).unwrap();
for round in 0_u8..2 {
let (mut server, mut client) = tokio::time::timeout(BOUND, async {
tokio::try_join!(listener.accept(), connect(&name))
})
.await
.expect("a client of the same user must be admitted")
.unwrap();
client.write_all(&[round]).await.unwrap();
let mut heard = [0_u8; 1];
tokio::time::timeout(BOUND, server.read_exact(&mut heard))
.await
.expect("the byte the client wrote must arrive")
.unwrap();
assert_eq!(heard, [round]);
}
}