use super::super::{create_private_dir_all, private_tempdir_in};
use super::*;
#[test]
fn temporary_directory_has_an_explicit_owner_only_dacl_under_a_shared_parent() {
let parent = tempfile::tempdir().unwrap();
crate::platform::tests::grant_inheritable_read(parent.path());
assert!(!is_private_dir(parent.path()).unwrap());
let lease = private_tempdir_in("draft-", parent.path()).unwrap();
let path = lease.path().to_owned();
assert!(is_private_dir(&path).unwrap());
assert!(!has_inherited_aces(&path));
let file = path.join("draft");
let file = create_private_file(&file).unwrap();
assert!(is_private_file(&file).unwrap());
drop(file);
drop(lease);
assert!(!path.exists());
}
#[test]
fn inherit_only_grants_are_not_private_and_created_files_do_not_inherit_them() {
let parent = tempfile::tempdir().unwrap();
let path = parent.path().join("custom inheritance");
create_private_dir(&path).unwrap();
crate::platform::tests::grant_access(&path, "*S-1-1-0:(OI)(CI)(IO)(R)");
assert!(!is_private_dir(&path).unwrap());
let path = path.join("record");
let created = create_private_file(&path).unwrap();
assert!(is_private_file(&created).unwrap());
assert!(!has_inherited_aces(&path));
}
fn has_inherited_aces(path: &Path) -> bool {
use windows_sys::Win32::Security::INHERITED_ACE;
let directory = OpenOptions::new()
.access_mode(READ_CONTROL)
.custom_flags(FILE_FLAG_BACKUP_SEMANTICS)
.open(path)
.unwrap();
let mut dacl: *mut ACL = ptr::null_mut();
let mut descriptor = ptr::null_mut();
unsafe {
assert_eq!(
GetSecurityInfo(
directory.as_raw_handle(),
SE_FILE_OBJECT,
DACL_SECURITY_INFORMATION,
ptr::null_mut(),
ptr::null_mut(),
&mut dacl,
ptr::null_mut(),
&mut descriptor
),
ERROR_SUCCESS
);
let _descriptor = Local(descriptor);
assert!(!dacl.is_null());
let mut size = ACL_SIZE_INFORMATION::default();
assert_ne!(
GetAclInformation(
dacl,
(&raw mut size).cast(),
mem::size_of_val(&size) as u32,
AclSizeInformation
),
0
);
(0..size.AceCount).any(|index| {
let mut ace = ptr::null_mut();
assert_ne!(GetAce(dacl, index, &mut ace), 0);
let header = ace.cast::<ACE_HEADER>().read_unaligned();
u32::from(header.AceFlags) & INHERITED_ACE != 0
})
}
}
#[test]
fn a_lease_under_a_replaceable_parent_cannot_transfer_its_path() {
let parent = tempfile::tempdir().unwrap();
crate::platform::tests::grant_access(parent.path(), "*S-1-1-0:(DC)");
let lease = private_tempdir_in("draft-", parent.path()).unwrap();
let path = lease.path().to_owned();
let result = lease.keep();
assert!(matches!(result, Err(err) if err.kind() == io::ErrorKind::PermissionDenied));
assert!(!path.exists());
}
#[test]
fn recursive_creation_removes_legacy_and_inherited_windows_grants() {
let parent = tempfile::tempdir().unwrap();
crate::platform::tests::grant_inheritable_read(parent.path());
let legacy = parent.path().join("legacy");
std::fs::create_dir(&legacy).unwrap();
assert!(has_inherited_aces(&legacy));
assert!(!is_private_dir(&legacy).unwrap());
create_private_dir_all(&legacy).unwrap();
assert!(is_private_dir(&legacy).unwrap());
assert!(!has_inherited_aces(&legacy));
assert!(!is_private_dir(parent.path()).unwrap());
assert!(std::fs::write(legacy.join("new file"), b"owner writable").is_ok());
}