191 lines
6.2 KiB
Rust
191 lines
6.2 KiB
Rust
// This Source Code Form is subject to the terms of the Mozilla Public
|
|
// License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
|
|
//! `WinPath` and `GameId`: the two validated value types everything else is built from.
|
|
|
|
use arena_core::{GameId, ParseError, Source, WinPath, limits};
|
|
|
|
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());
|
|
}
|