1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
//! Coverage for `strip_windows_verbatim_prefix` (#177), the allocation-free
//! display normalization that removes the Windows `\\?\` extended-length prefix
//! from operator-facing paths. Previously untested; correctness matters because
//! the same normalized string is what a user reads and what path-keyed output
//! compares.
use keyhog_core::strip_windows_verbatim_prefix as strip;
#[test]
fn strips_the_verbatim_prefix_from_a_drive_path() {
assert_eq!(strip(r"\\?\C:\Users\me\.env"), r"C:\Users\me\.env");
}
#[test]
fn strips_the_verbatim_prefix_from_a_unc_path() {
// Documented contract: \\?\UNC\server\share -> UNC\server\share, NOT rebuilt
// to a leading \\ (allocation-free by design).
assert_eq!(strip(r"\\?\UNC\server\share"), r"UNC\server\share");
}
#[test]
fn leaves_a_path_without_the_prefix_unchanged() {
assert_eq!(
strip("/home/user/.aws/credentials"),
"/home/user/.aws/credentials"
);
assert_eq!(strip(r"C:\plain\path"), r"C:\plain\path");
assert_eq!(strip(""), "");
}
#[test]
fn requires_the_complete_four_char_prefix() {
// An incomplete or misshapen prefix must NOT be stripped.
assert_eq!(strip(r"\\?"), r"\\?"); // missing trailing backslash
assert_eq!(strip(r"\\"), r"\\");
assert_eq!(strip(r"\?\x"), r"\?\x"); // wrong shape
}
#[test]
fn strips_exactly_one_prefix_occurrence() {
// Only the single leading prefix is removed; a doubled prefix keeps the
// inner one (\\?\ + \\?\x -> \\?\x).
assert_eq!(strip(r"\\?\\\?\x"), r"\\?\x");
// The prefix alone normalizes to empty.
assert_eq!(strip(r"\\?\"), "");
}
#[test]
fn result_is_a_suffix_that_is_identity_or_exactly_the_prefix_removed() {
// Deterministic property over a spread of shapes: the result is always a
// tail of the input, no longer than it, and either equals the input or is
// the input with exactly the 4-char prefix removed.
for p in [
r"\\?\C:\a",
r"\\?\UNC\s\h",
"/x/y",
r"D:\z",
"",
r"\\?\",
r"\\?\\\?\q",
"plain",
r"\\server\share",
r"\\?\GLOBALROOT\Device",
] {
let out = strip(p);
assert!(p.ends_with(out), "{out:?} must be a suffix of {p:?}");
assert!(out.len() <= p.len());
assert!(
out == p || p == format!(r"\\?\{out}"),
"{p:?} -> {out:?} must be identity or exactly the prefix removed"
);
}
}