250 lines
8.2 KiB
Rust
250 lines
8.2 KiB
Rust
// SPDX-License-Identifier: CPAL-1.0
|
|
// Copyright (c) 2026 Marek Novák. Built on Domi, https://aula.marek-novak.cz
|
|
|
|
//! `WinPath` and `GameId`: the two validated value types everything else is built from.
|
|
|
|
use arena_core::{GameId, ParseError, Source, WinPath, limits};
|
|
use proptest::prelude::*;
|
|
|
|
proptest! {
|
|
#[test]
|
|
fn winpath_never_panics_and_normal_form_is_stable(s in r"([A-Za-z]:)?[\\/a-zA-Z0-9 .:<>|?*\x00-\x1f¹~$-]{0,40}") {
|
|
if let Ok(p) = WinPath::parse(&s) {
|
|
let again = WinPath::parse(p.as_str()).unwrap();
|
|
prop_assert_eq!(&again, &p);
|
|
prop_assert!(p.components().all(|c| c != ".." && c != "." && !c.contains('\0')));
|
|
prop_assert!(!p.as_str().contains('/'));
|
|
prop_assert!(p.as_str().get(1..3) == Some(":\\"));
|
|
if let Some(parent) = p.parent() {
|
|
prop_assert!(p.starts_with(&parent));
|
|
}
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn winpath_join_never_climbs_out(rel in r"[a-zA-Z.\\/]{0,20}") {
|
|
let base = WinPath::parse(r"C:\Games\X").unwrap();
|
|
if let Ok(j) = base.join(&rel) {
|
|
prop_assert!(j.starts_with(&base));
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn game_id_round_trips_or_refuses(s in r"[a-z]{0,7}:?[a-zA-Z0-9:._ -]{0,20}") {
|
|
if let Ok(id) = s.parse::<GameId>() {
|
|
prop_assert_eq!(id.to_string(), s.clone());
|
|
prop_assert!(s.len() <= limits::MAX_GAME_ID);
|
|
prop_assert!(s.bytes().all(|b| b.is_ascii_lowercase() || b.is_ascii_digit() || b":._-".contains(&b)));
|
|
}
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn errors_display_without_input_text() {
|
|
let all = [
|
|
ParseError::TooLarge,
|
|
ParseError::TooDeep,
|
|
ParseError::NotUtf8,
|
|
ParseError::Syntax { at: 7 },
|
|
ParseError::Missing("name"),
|
|
ParseError::WrongType("appid"),
|
|
ParseError::BadPath("installdir"),
|
|
ParseError::BadId,
|
|
ParseError::Duplicate("key"),
|
|
ParseError::TooLong("value"),
|
|
ParseError::TooMany("entries"),
|
|
ParseError::BadValue("url"),
|
|
ParseError::NotAGame("Epic add-on"),
|
|
];
|
|
for e in all {
|
|
let text = e.to_string();
|
|
assert!(!text.is_empty());
|
|
let as_error: &dyn std::error::Error = &e;
|
|
assert_eq!(as_error.to_string(), text);
|
|
}
|
|
assert_eq!(ParseError::Syntax { at: 7 }.to_string(), "syntax error at byte 7");
|
|
}
|
|
|
|
fn p(s: &str) -> WinPath {
|
|
WinPath::parse(s).unwrap()
|
|
}
|
|
|
|
#[test]
|
|
fn winpath_accepts_and_normalises() {
|
|
assert_eq!(p(r"C:\Games\X").as_str(), r"C:\Games\X");
|
|
assert_eq!(p(r"c:\Games\X\").as_str(), r"C:\Games\X");
|
|
assert_eq!(p("d:/Games//X/").as_str(), r"D:\Games\X");
|
|
assert_eq!(p(r"C:\").as_str(), r"C:\");
|
|
assert_eq!(p("C:/").as_str(), r"C:\");
|
|
assert_eq!(p(r"C:\Program Files (x86)\Steam").as_str(), r"C:\Program Files (x86)\Steam");
|
|
assert_eq!(p(r"C:\Hry\Příliš žluťoučký kůň").as_str(), r"C:\Hry\Příliš žluťoučký kůň");
|
|
assert_eq!(p(r"C:\a.b\.hidden\x..y").as_str(), r"C:\a.b\.hidden\x..y");
|
|
assert_eq!(p(r"C:\CONSOLE\NULL\COM10\LPT").as_str(), r"C:\CONSOLE\NULL\COM10\LPT");
|
|
}
|
|
|
|
#[test]
|
|
fn winpath_refuses() {
|
|
let long = format!(r"C:\{}", "a".repeat(limits::MAX_PATH));
|
|
for bad in [
|
|
"",
|
|
"C:",
|
|
"C:Games",
|
|
r"\Games",
|
|
"Games",
|
|
r"1:\x",
|
|
r"\\server\share",
|
|
r"\\?\C:\x",
|
|
r"\\.\C:\x",
|
|
r"\??\C:\x",
|
|
"//server/share",
|
|
r"C:\..",
|
|
r"C:\Games\..\Windows",
|
|
r"C:\Games\.\x",
|
|
"C:\\Games\\x\u{0}",
|
|
"C:\\Games\\x\ny",
|
|
"C:\\Games\\x\u{1f}",
|
|
r"C:\Games\x:stream",
|
|
r"C:\Games\a*b",
|
|
r"C:\Games\a?b",
|
|
r#"C:\Games\a"b"#,
|
|
r"C:\Games\a<b",
|
|
r"C:\Games\a>b",
|
|
r"C:\Games\a|b",
|
|
r"C:\CON",
|
|
r"C:\con.txt",
|
|
r"C:\Games\nul",
|
|
r"C:\Games\Aux.log",
|
|
r"C:\Games\prn",
|
|
r"C:\Games\COM1",
|
|
r"C:\Games\lpt9.dat",
|
|
r"C:\Games\COM¹",
|
|
r"C:\Games\CONIN$",
|
|
r"C:\Games\conout$",
|
|
r"C:\Games\NUL .txt",
|
|
r"C:\Games\trailing.",
|
|
r"C:\Games\trailing ",
|
|
long.as_str(),
|
|
] {
|
|
assert!(matches!(WinPath::parse(bad), Err(ParseError::BadPath(_))), "{bad:?}");
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn winpath_length_counts_utf16_units() {
|
|
let ok = format!(r"C:\{}", "a".repeat(limits::MAX_PATH - 3));
|
|
assert!(WinPath::parse(&ok).is_ok());
|
|
// One astral character is two UTF-16 units.
|
|
let astral = format!(r"C:\{}😀", "a".repeat(limits::MAX_PATH - 4));
|
|
assert!(WinPath::parse(&astral).is_err());
|
|
}
|
|
|
|
#[test]
|
|
fn winpath_join_parent_file_name() {
|
|
let base = p(r"C:\Games");
|
|
assert_eq!(base.join(r"A\b.exe").unwrap().as_str(), r"C:\Games\A\b.exe");
|
|
assert_eq!(base.join("A/b.exe").unwrap().as_str(), r"C:\Games\A\b.exe");
|
|
for bad in ["", r"..\x", r"A\..\..\x", r"\x", r"D:\x", "D:x", r"\\s\x", "a\u{0}"] {
|
|
assert!(base.join(bad).is_err(), "{bad:?}");
|
|
}
|
|
assert_eq!(p(r"C:\Games\A").parent(), Some(p(r"C:\Games")));
|
|
assert_eq!(p(r"C:\Games").parent(), Some(p(r"C:\")));
|
|
assert_eq!(p(r"C:\").parent(), None);
|
|
assert_eq!(p(r"C:\Games\b.exe").file_name(), Some("b.exe"));
|
|
assert_eq!(p(r"C:\").file_name(), None);
|
|
assert_eq!(p(r"C:\a\b").components().collect::<Vec<_>>(), ["a", "b"]);
|
|
}
|
|
|
|
#[test]
|
|
fn winpath_join_respects_length() {
|
|
let base = p(r"C:\Games");
|
|
assert!(base.join(&"a".repeat(limits::MAX_PATH)).is_err());
|
|
}
|
|
|
|
#[test]
|
|
fn winpath_compare_ignoring_case() {
|
|
assert!(p(r"C:\Games\X").eq_ignore_case(&p(r"c:\GAMES\x")));
|
|
assert!(p(r"C:\Games\X\bin\x.exe").starts_with(&p(r"c:\games\x")));
|
|
assert!(p(r"C:\Games\X").starts_with(&p(r"C:\Games\X")));
|
|
assert!(p(r"C:\Games\X").starts_with(&p(r"C:\")));
|
|
assert!(!p(r"C:\Games\XY").starts_with(&p(r"C:\Games\X")));
|
|
assert!(!p(r"D:\Games\X").starts_with(&p(r"C:\Games")));
|
|
assert!(!p(r"C:\Games").starts_with(&p(r"C:\Games\X")));
|
|
}
|
|
|
|
#[test]
|
|
fn winpath_serde_validates() {
|
|
let ok: WinPath = serde_json::from_str(r#""C:\\Games""#).unwrap();
|
|
assert_eq!(ok.as_str(), r"C:\Games");
|
|
assert!(serde_json::from_str::<WinPath>(r#""C:\\Games\\..\\x""#).is_err());
|
|
assert_eq!(serde_json::to_string(&ok).unwrap(), r#""C:\\Games""#);
|
|
}
|
|
|
|
#[test]
|
|
fn game_id_round_trip() {
|
|
for s in ["steam:1234560", "epic:exampleracer_main-01", "gog:1207658924", "manual:0123456789abcdef", "domi:lapilli"]
|
|
{
|
|
let id: GameId = s.parse().unwrap();
|
|
assert_eq!(id.to_string(), s);
|
|
assert_eq!(id.source().as_str(), s.split(':').next().unwrap());
|
|
assert_eq!(id.native(), s.split_once(':').unwrap().1);
|
|
}
|
|
assert_eq!(GameId::new(Source::Steam, "10").unwrap().as_str(), "steam:10");
|
|
assert_eq!("epic:a".parse::<GameId>().unwrap().source(), Source::Epic);
|
|
}
|
|
|
|
#[test]
|
|
fn game_id_refuses() {
|
|
let long = format!("epic:{}", "a".repeat(limits::MAX_GAME_ID));
|
|
for bad in [
|
|
"",
|
|
"steam",
|
|
"steam:",
|
|
":10",
|
|
"Steam:10",
|
|
"steam:0",
|
|
"steam:01",
|
|
"steam:4294967296",
|
|
"steam:-1",
|
|
"steam:1a",
|
|
"xbox:10",
|
|
"epic:A",
|
|
"epic:a.b",
|
|
"epic:a:b",
|
|
"gog:abc",
|
|
"gog:123456789012345678901",
|
|
"manual:0123",
|
|
"manual:0123456789ABCDEF",
|
|
"manual:0123456789abcdeg",
|
|
"domi:-x",
|
|
"domi:a_b",
|
|
"domi:../x",
|
|
"steam:10 ",
|
|
" steam:10",
|
|
"steam:10\u{0}",
|
|
long.as_str(),
|
|
] {
|
|
assert_eq!(bad.parse::<GameId>(), Err(ParseError::BadId), "{bad:?}");
|
|
}
|
|
assert_eq!(GameId::new(Source::Gog, "x"), Err(ParseError::BadId));
|
|
}
|
|
|
|
#[test]
|
|
fn game_id_serde_validates() {
|
|
let id: GameId = serde_json::from_str(r#""steam:10""#).unwrap();
|
|
assert_eq!(serde_json::to_string(&id).unwrap(), r#""steam:10""#);
|
|
assert!(serde_json::from_str::<GameId>(r#""steam:../x""#).is_err());
|
|
}
|
|
|
|
#[test]
|
|
fn source_names() {
|
|
for s in Source::ALL {
|
|
assert_eq!(s.as_str().parse::<Source>().unwrap(), s);
|
|
assert_eq!(s.to_string(), s.as_str());
|
|
}
|
|
assert!("Steam".parse::<Source>().is_err());
|
|
assert!(Source::Steam.priority() > Source::Epic.priority());
|
|
assert!(Source::Epic.priority() > Source::Gog.priority());
|
|
assert!(Source::Gog.priority() > Source::Domi.priority());
|
|
assert!(Source::Domi.priority() > Source::Manual.priority());
|
|
}
|