Skip to content

Commit 9a7fb47

Browse files
committed
Add test for is_admin_owned
1 parent 8525b5a commit 9a7fb47

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

talpid-windows/Cargo.toml

+7
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,10 @@ features = [
3030
"Win32_NetworkManagement_IpHelper",
3131
"Win32_NetworkManagement_Ndis",
3232
]
33+
34+
[target.'cfg(windows)'.dev-dependencies.windows-sys]
35+
workspace = true
36+
features = [
37+
"Win32_Storage",
38+
"Win32_Storage_FileSystem"
39+
]

talpid-windows/src/fs.rs

+31
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,34 @@ pub fn is_admin_owned<T: AsRawHandle>(handle: T) -> io::Result<bool> {
4545

4646
Ok(is_system_owned || is_admin_owned)
4747
}
48+
49+
#[cfg(test)]
50+
mod test {
51+
use std::os::windows::fs::OpenOptionsExt;
52+
use windows_sys::Win32::Storage::FileSystem::FILE_FLAG_BACKUP_SEMANTICS;
53+
54+
use super::is_admin_owned;
55+
56+
#[test]
57+
pub fn test_is_admin_owned() {
58+
// The kernel image is owned by "TrustedInstaller", so we expect the function to return 'false'
59+
let path = std::fs::File::open(r"C:\Windows\System32\ntoskrnl.exe").unwrap();
60+
let result = is_admin_owned(path);
61+
assert!(
62+
matches!(result, Ok(false)),
63+
"expected ntoskrnl.exe to be owned by TrustedInstaller (false), got {result:?}"
64+
);
65+
66+
// The Windows system temp directory is owned by SYSTEM, so we expect 'true'
67+
let path = std::fs::File::options()
68+
.read(true)
69+
.custom_flags(FILE_FLAG_BACKUP_SEMANTICS)
70+
.open(r"C:\Windows\Temp")
71+
.unwrap();
72+
let result = is_admin_owned(path);
73+
assert!(
74+
matches!(result, Ok(true)),
75+
"expected TEMP to be owned by SYSTEM (true), got {result:?}"
76+
);
77+
}
78+
}

0 commit comments

Comments
 (0)