use super::batch_gate_tests::{the_extension_rule_this_replaced, verbatim_extension_rule_this_replaced};
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
enum Comp {
Name { batch: bool },
Empty,
Skip,
Pop,
Dots,
DrivePrefix,
DriveUp,
QuestionMark,
}
const COMPONENTS: [(&str, Comp); 18] = [
("", Comp::Empty),
(".", Comp::Skip),
("..", Comp::Pop),
(".. ", Comp::Dots),
("...", Comp::Dots),
(" ", Comp::Dots),
("y", Comp::Name { batch: false }),
("x.exe", Comp::Name { batch: false }),
("x.bat", Comp::Name { batch: true }),
("x.bat ", Comp::Name { batch: true }),
("x.bat.", Comp::Name { batch: true }),
("x.bat:s", Comp::Name { batch: true }),
("x.bat.:s", Comp::Name { batch: true }),
("x.exe:p.bat", Comp::Name { batch: true }),
(".bat", Comp::Name { batch: true }),
("..bat", Comp::Name { batch: true }),
("C:", Comp::DrivePrefix),
("?", Comp::QuestionMark),
];
const DRIVE_SPELLINGS: [(&str, Comp); 6] = [
("1:", Comp::DrivePrefix),
("é:", Comp::DrivePrefix),
("\u{FFFD}:", Comp::DrivePrefix),
("1:..", Comp::DriveUp),
("é:..", Comp::DriveUp),
("𝒳:", Comp::Name { batch: false }),
];
fn as_root_name(comp: Comp) -> Option<bool> {
match comp {
Comp::Name { batch } => Some(batch),
Comp::DrivePrefix | Comp::DriveUp | Comp::QuestionMark => Some(false),
Comp::Empty | Comp::Skip | Comp::Pop | Comp::Dots => None,
}
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
enum Landing {
Name(bool),
NoFile,
UncRoot,
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
enum Root {
Plain,
Unc,
Device,
}
fn oracle_landing(rest: &[Comp], root: Root) -> Landing {
#[derive(Clone, Copy)]
enum Entry {
Name(bool),
Drive,
Dots,
}
let mut stack: Vec<Entry> = Vec::new();
for (i, comp) in rest.iter().enumerate() {
match comp {
Comp::Empty | Comp::Skip => {}
Comp::Dots => stack.push(Entry::Dots),
Comp::Pop => {
stack.pop();
}
Comp::DrivePrefix if i == 0 && root == Root::Plain => stack.push(Entry::Drive),
Comp::DriveUp if i == 0 && root == Root::Plain => {}
Comp::DrivePrefix | Comp::DriveUp | Comp::QuestionMark => stack.push(Entry::Name(false)),
Comp::Name { batch } => stack.push(Entry::Name(*batch)),
}
}
while matches!(stack.last(), Some(Entry::Dots)) {
stack.pop();
}
match stack.last() {
Some(Entry::Name(batch)) => Landing::Name(*batch),
Some(Entry::Drive) => Landing::NoFile,
Some(Entry::Dots) => unreachable!("trailing dots were just dropped"),
None if root == Root::Unc => Landing::UncRoot,
None => Landing::NoFile,
}
}
fn oracle_refuses(components: &[Comp]) -> bool {
if let [Comp::Empty, Comp::Empty, Comp::Skip | Comp::QuestionMark, rest @ ..] = components {
return match oracle_landing(rest, Root::Device) {
Landing::Name(batch) => batch,
Landing::NoFile => true,
Landing::UncRoot => unreachable!("a device path has no UNC root"),
};
}
let (root, rest) = match components {
[Comp::Empty, Comp::Empty, rest @ ..] => match rest {
[server, share, rest @ ..] => (Some((*server, *share)), rest),
_ => return true,
},
_ => (None, components),
};
let kind = if root.is_some() { Root::Unc } else { Root::Plain };
match oracle_landing(rest, kind) {
Landing::Name(batch) => batch,
Landing::NoFile => true,
Landing::UncRoot => {
let (_, share) = root.expect("only a UNC path lands on a UNC root");
as_root_name(share).unwrap_or(true)
}
}
}
fn declared_unc_over_refusal(components: &[Comp]) -> bool {
let [Comp::Empty, Comp::Empty, server, _share, rest @ ..] = components else {
return false;
};
as_root_name(*server) == Some(true) && oracle_landing(rest, Root::Unc) == Landing::UncRoot
}
#[derive(Default, Debug)]
struct Tally {
probes: u64,
refused: u64,
accepted: u64,
declared_over_refusals: u64,
holes: Vec<String>,
undeclared_over_refusals: Vec<String>,
newly_accepted: Vec<String>,
verbatim_holes: Vec<String>,
}
fn compare_gate_with_oracle(vocab: &[(&'static str, Comp)], min_depth: u32, max_depth: u32) -> Tally {
let mut tally = Tally::default();
for sep in ["\\", "/"] {
for depth in min_depth..=max_depth {
for index in 0..vocab.len().pow(depth) {
let path = nth_path(vocab, depth, index);
judge(&mut tally, &path, sep);
}
}
}
tally
}
fn compare_gate_with_oracle_slotted(
vocab: &[(&'static str, Comp)],
depth: u32,
slot: usize,
slotted: &[(&'static str, Comp)],
) -> Tally {
let mut tally = Tally::default();
for sep in ["\\", "/"] {
for index in 0..vocab.len().pow(depth) {
for extra in slotted {
let mut path = nth_path(vocab, depth, index);
path.insert(slot, *extra);
judge(&mut tally, &path, sep);
}
}
}
tally
}
fn nth_path(vocab: &[(&'static str, Comp)], depth: u32, mut index: usize) -> Vec<(&'static str, Comp)> {
(0..depth)
.map(|_| {
let component = vocab[index % vocab.len()];
index /= vocab.len();
component
})
.collect()
}
fn judge(tally: &mut Tally, path: &[(&str, Comp)], sep: &str) {
let texts: Vec<&str> = path.iter().map(|(text, _)| *text).collect();
let kinds: Vec<Comp> = path.iter().map(|(_, kind)| *kind).collect();
let probe = texts.join(sep);
let path = std::path::Path::new(&probe);
let want = oracle_refuses(&kinds);
let got = super::reject_batch_path_on(path, true).is_err();
tally.probes += 1;
if the_extension_rule_this_replaced(path) && !got {
tally.newly_accepted.push(probe.clone());
}
let verbatim = format!(r"\\?\{probe}");
let verbatim_path = std::path::Path::new(&verbatim);
let verbatim_got = super::reject_batch_path_on(verbatim_path, true).is_err();
let lower = verbatim.to_ascii_lowercase();
if (lower.ends_with(".bat") || lower.ends_with(".cmd")) && !verbatim_got {
tally.verbatim_holes.push(verbatim.clone());
}
if verbatim_extension_rule_this_replaced(&verbatim) && !verbatim_got {
tally.newly_accepted.push(verbatim);
}
if probe.starts_with(r"\\?\") {
let lower = probe.to_ascii_lowercase();
if (lower.ends_with(".bat") || lower.ends_with(".cmd")) && !got {
tally.verbatim_holes.push(probe);
}
return;
}
match (want, got) {
(true, false) => tally.holes.push(probe),
(false, true) if declared_unc_over_refusal(&kinds) => {
tally.declared_over_refusals += 1;
}
(false, true) => tally.undeclared_over_refusals.push(probe),
(_, true) => tally.refused += 1,
(_, false) => tally.accepted += 1,
}
}
fn assert_agreement(mut tally: Tally) -> Tally {
tally.holes.truncate(20);
tally.undeclared_over_refusals.truncate(20);
tally.newly_accepted.truncate(20);
tally.verbatim_holes.truncate(20);
assert_eq!(
tally.verbatim_holes,
Vec::<String>::new(),
"a verbatim batch suffix accepted"
);
assert_eq!(
tally.holes,
Vec::<String>::new(),
"the gate accepts what Win32 resolves to a batch file"
);
assert_eq!(
tally.undeclared_over_refusals,
Vec::<String>::new(),
"the gate refuses what Win32 does not resolve to a batch file"
);
assert_eq!(
tally.newly_accepted,
Vec::<String>::new(),
"refused before, accepted now"
);
assert!(tally.refused > 1000 && tally.accepted > 1000, "{tally:?}");
tally
}
#[skuld::test]
fn the_gate_agrees_with_a_component_level_resolver() {
let tally = assert_agreement(compare_gate_with_oracle(&COMPONENTS, 1, 5));
assert!(tally.declared_over_refusals > 0, "{tally:?}");
}
#[skuld::test]
fn the_gate_agrees_on_every_drive_spelling() {
let vocab: Vec<(&str, Comp)> = COMPONENTS.iter().chain(&DRIVE_SPELLINGS).copied().collect();
assert_agreement(compare_gate_with_oracle(&vocab, 1, 4));
assert_agreement(compare_gate_with_oracle_slotted(&COMPONENTS, 4, 3, &DRIVE_SPELLINGS));
}
#[skuld::test]
fn the_oracle_models_a_unc_root_of_its_own() {
let name = |batch| Comp::Name { batch };
let e = Comp::Empty;
assert!(oracle_refuses(&[e, e, name(false), name(true), Comp::Pop]));
assert!(oracle_refuses(&[
e,
e,
name(false),
name(true),
name(false),
Comp::Pop,
Comp::Pop
]));
assert!(oracle_refuses(&[e, e, Comp::Dots, name(true), name(false), Comp::Pop]));
assert!(!oracle_refuses(&[
e,
e,
name(false),
name(false),
name(true),
Comp::Pop
]));
assert!(!oracle_refuses(&[e, name(false), name(true), Comp::Pop]));
}
#[skuld::test]
fn the_oracle_models_a_device_root_of_its_own() {
let name = |batch| Comp::Name { batch };
let (e, dot, q) = (Comp::Empty, Comp::Skip, Comp::QuestionMark);
assert!(oracle_refuses(&[e, e, dot, name(true), Comp::Pop]));
assert!(oracle_refuses(&[e, e, dot, name(false), Comp::Pop]));
assert!(oracle_refuses(&[e, e, dot, Comp::DrivePrefix, Comp::Pop]));
assert!(!oracle_refuses(&[e, e, dot, Comp::DrivePrefix]));
assert!(!oracle_refuses(&[e, e, q, Comp::DrivePrefix, Comp::Dots]));
assert!(oracle_refuses(&[
e,
e,
dot,
Comp::DrivePrefix,
Comp::Pop,
Comp::Pop,
name(true)
]));
assert!(!oracle_refuses(&[e, e, dot, name(false), name(true), Comp::Pop]));
}
#[skuld::test]
fn the_oracle_reads_dots_and_spaces_on_its_own_terms() {
let name = |batch| Comp::Name { batch };
assert!(!oracle_refuses(&[name(true), name(false), Comp::Dots]));
assert!(oracle_refuses(&[name(false), name(true), Comp::Dots, Comp::Pop]));
assert!(!oracle_refuses(&[name(true), name(false), Comp::Dots, Comp::Pop]));
assert!(!oracle_refuses(&[name(false), Comp::Dots, Comp::Pop, name(false)]));
}