Files
ArenaPublic/crates/arena-core/tests/manual_domi.rs
T

137 lines
5.1 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/.
//! Manual games (an `.exe` the user picked) and Domi games (Atrium registry entries).
use arena_core::{CoverHint, DomiLaunch, Launch, ParseError, Source, WinPath, domi, limits, manual};
#[test]
fn manual_game() {
let g = manual::game(r"D:\Games\Old Thing\OldThing.exe", None).unwrap();
assert_eq!(g.source, Source::Manual);
assert_eq!(g.title, "OldThing");
assert!(g.id.as_str().starts_with("manual:"));
assert_eq!(g.id.native().len(), 16);
assert_eq!(g.install_dir.as_ref().map(WinPath::as_str), Some(r"D:\Games\Old Thing"));
match &g.launch {
Launch::Exe { exe, working_dir, args } => {
assert_eq!(exe.as_str(), r"D:\Games\Old Thing\OldThing.exe");
assert_eq!(working_dir.as_str(), r"D:\Games\Old Thing");
assert!(args.is_empty());
}
other => panic!("{other:?}"),
}
assert!(matches!(g.cover, Some(CoverHint::Lettered { letter: 'O', hue }) if hue < 360));
}
#[test]
fn manual_id_is_stable_and_ignores_case_and_slashes() {
let a = manual::game(r"D:\Games\Old Thing\OldThing.exe", None).unwrap();
let b = manual::game("d:/games/old thing/OLDTHING.EXE", Some("Renamed")).unwrap();
assert_eq!(a.id, b.id);
assert_eq!(b.title, "Renamed");
let c = manual::game(r"D:\Games\Other\OldThing.exe", None).unwrap();
assert_ne!(a.id, c.id);
}
#[test]
fn manual_refuses_bad_exe() {
for bad in [
r"OldThing.exe",
r"\\server\share\x.exe",
r"\\?\C:\x.exe",
r"\\.\C:\x.exe",
r"C:\Games\..\Windows\x.exe",
r"C:\Games\run.bat",
r"C:\Games\run.cmd",
r"C:\",
r"C:\Games\CON.exe",
"C:\\Games\\x\u{0}.exe",
"",
] {
assert_eq!(manual::game(bad, None), Err(ParseError::BadPath("exe")), "{bad:?}");
}
}
#[test]
fn manual_refuses_bad_title() {
assert_eq!(manual::game(r"C:\G\x.exe", Some(" ")), Err(ParseError::Missing("title")));
assert_eq!(manual::game(r"C:\G\x.exe", Some("a\u{0}b")), Err(ParseError::BadValue("title")));
}
#[test]
fn domi_link_game() {
let g = domi::game("lapilli", "Lapilli", DomiLaunch::Link { app: "lapilli" }).unwrap();
assert_eq!(g.id.as_str(), "domi:lapilli");
assert_eq!(g.source, Source::Domi);
assert_eq!(g.title, "Lapilli");
assert_eq!(g.install_dir, None);
assert_eq!(g.launch, Launch::DomiLink { app: "lapilli".into() });
assert_eq!(g.launch.uri().as_deref(), Some("domi-lapilli://open"));
}
#[test]
fn domi_web_game() {
let hosts = ["rosea.example.org", "moin.example.org"];
let g = domi::game(
"rosea",
"Rosea",
DomiLaunch::Web { url: "HTTPS://Rosea.Example.org/play?x=1#top", allowed_hosts: &hosts },
)
.unwrap();
assert_eq!(g.launch, Launch::Url("https://rosea.example.org/play?x=1#top".into()));
assert_eq!(g.launch.uri().as_deref(), Some("https://rosea.example.org/play?x=1#top"));
assert_eq!(g.launch.uri_checked(&hosts).as_deref(), Some("https://rosea.example.org/play?x=1#top"));
assert_eq!(g.launch.uri_checked(&["other.example.org"]), None);
}
#[test]
fn domi_refuses_bad_ids() {
let long = "a".repeat(limits::MAX_DOMI_ID + 1);
for bad in ["", "-x", "x-", "X", "a_b", "a.b", "a b", "ä", long.as_str()] {
assert_eq!(domi::game(bad, "N", DomiLaunch::Link { app: "ok" }), Err(ParseError::BadId), "{bad:?}");
assert_eq!(domi::game("ok", "N", DomiLaunch::Link { app: bad }), Err(ParseError::BadId), "{bad:?}");
}
assert!(domi::game(&"a".repeat(limits::MAX_DOMI_ID), "N", DomiLaunch::Link { app: "a-1" }).is_ok());
}
#[test]
fn domi_refuses_bad_urls() {
let hosts = ["rosea.example.org"];
let long = format!("https://rosea.example.org/{}", "a".repeat(limits::MAX_URL));
for bad in [
"http://rosea.example.org/",
"javascript:alert(1)",
"file:///C:/Windows/System32/calc.exe",
"https://evil.example.org/",
"https://rosea.example.org.evil.example/",
"https://sub.rosea.example.org/",
"https://user@rosea.example.org/",
"https://rosea.example.org:8443/",
"https://rosea.example.org/a b",
"https://rosea.example.org/a\\b",
"https://rosea.example.org/\u{0}",
"https://rosea.example.org/%zz",
"https://rosea.example.org/\"quote",
"https:///path",
"https://",
" https://rosea.example.org/",
"https://[::1]/",
"https://rosea..example.org/",
"https://-rosea.example.org/",
long.as_str(),
] {
let r = domi::game("rosea", "Rosea", DomiLaunch::Web { url: bad, allowed_hosts: &hosts });
assert!(matches!(r, Err(ParseError::BadValue("url")) | Err(ParseError::TooLong("url"))), "{bad:?}: {r:?}");
}
}
#[test]
fn domi_web_host_without_path() {
let hosts = ["moin.example.org"];
let g =
domi::game("moin", "Moin", DomiLaunch::Web { url: "https://moin.example.org", allowed_hosts: &hosts }).unwrap();
assert_eq!(g.launch.uri().as_deref(), Some("https://moin.example.org"));
}