[core] Tests: fixtures and mandatory tests (valid, hostile, property, launch, dedupe) ahead of the code
This commit is contained in:
@@ -0,0 +1,147 @@
|
||||
// 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/.
|
||||
|
||||
//! GOG entries built from registry values the app passes in.
|
||||
|
||||
use arena_core::gog::{GogEntry, game};
|
||||
use arena_core::{Launch, ParseError, Source, WinPath, limits};
|
||||
|
||||
fn entry() -> GogEntry {
|
||||
GogEntry {
|
||||
game_id: "1207658924".into(),
|
||||
name: "Example Saga".into(),
|
||||
path: r"C:\GOG Games\Example Saga".into(),
|
||||
exe: r"C:\GOG Games\Example Saga\bin\x64\saga.exe".into(),
|
||||
working_dir: Some(r"C:\GOG Games\Example Saga\bin\x64".into()),
|
||||
launch_params: Some(r#"-skipintro "-profile=Slot One""#.into()),
|
||||
}
|
||||
}
|
||||
|
||||
fn exe_parts(l: &Launch) -> (&str, &str, Vec<&str>) {
|
||||
match l {
|
||||
Launch::Exe { exe, working_dir, args } => {
|
||||
(exe.as_str(), working_dir.as_str(), args.iter().map(String::as_str).collect())
|
||||
}
|
||||
other => panic!("{other:?}"),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn valid_entry() {
|
||||
let g = game(&entry()).unwrap();
|
||||
assert_eq!(g.id.as_str(), "gog:1207658924");
|
||||
assert_eq!(g.source, Source::Gog);
|
||||
assert_eq!(g.title, "Example Saga");
|
||||
assert_eq!(g.install_dir.as_ref().map(WinPath::as_str), Some(r"C:\GOG Games\Example Saga"));
|
||||
let (exe, wd, args) = exe_parts(&g.launch);
|
||||
assert_eq!(exe, r"C:\GOG Games\Example Saga\bin\x64\saga.exe");
|
||||
assert_eq!(wd, r"C:\GOG Games\Example Saga\bin\x64");
|
||||
assert_eq!(args, ["-skipintro", "-profile=Slot One"]);
|
||||
assert_eq!(g.launch.uri(), None, "an exe launch has no URI");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn relative_exe_and_defaults() {
|
||||
let mut e = entry();
|
||||
e.exe = r"bin\x64\saga.exe".into();
|
||||
e.working_dir = Some(String::new());
|
||||
e.launch_params = Some(" ".into());
|
||||
let g = game(&e).unwrap();
|
||||
let (exe, wd, args) = exe_parts(&g.launch);
|
||||
assert_eq!(exe, r"C:\GOG Games\Example Saga\bin\x64\saga.exe");
|
||||
assert_eq!(wd, r"C:\GOG Games\Example Saga\bin\x64");
|
||||
assert!(args.is_empty());
|
||||
|
||||
e.working_dir = None;
|
||||
e.launch_params = None;
|
||||
let g = game(&e).unwrap();
|
||||
let (_, wd, args) = exe_parts(&g.launch);
|
||||
assert_eq!(wd, r"C:\GOG Games\Example Saga\bin\x64");
|
||||
assert!(args.is_empty());
|
||||
|
||||
e.working_dir = Some("data".into());
|
||||
let g = game(&e).unwrap();
|
||||
let (_, wd, _) = exe_parts(&g.launch);
|
||||
assert_eq!(wd, r"C:\GOG Games\Example Saga\data");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn bad_ids() {
|
||||
for bad in ["", "abc", "12a", "-1", " 1", "123456789012345678901", "1.5"] {
|
||||
let mut e = entry();
|
||||
e.game_id = bad.into();
|
||||
assert_eq!(game(&e), Err(ParseError::BadId), "{bad:?}");
|
||||
}
|
||||
let mut e = entry();
|
||||
e.game_id = "1".repeat(limits::MAX_GOG_ID);
|
||||
assert!(game(&e).is_ok());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn bad_paths() {
|
||||
let cases: [(&str, &str, &str); 9] = [
|
||||
(r"\\server\share\Game", r"\\server\share\Game\g.exe", "path"),
|
||||
(r"C:\GOG Games\..\Windows", r"C:\Windows\g.exe", "path"),
|
||||
(r"relative\Game", r"relative\Game\g.exe", "path"),
|
||||
(r"C:\GOG Games\Example Saga", r"C:\Windows\System32\cmd.exe", "exe"),
|
||||
(r"C:\GOG Games\Example Saga", r"..\..\Windows\System32\cmd.exe", "exe"),
|
||||
(r"C:\GOG Games\Example Saga", r"C:\GOG Games\Example Saga\run.bat", "exe"),
|
||||
(r"C:\GOG Games\Example Saga", r"C:\GOG Games\Example Saga\run.cmd", "exe"),
|
||||
(r"C:\GOG Games\Example Saga", r"C:\GOG Games\Example Saga\game.exe.lnk", "exe"),
|
||||
(r"C:\GOG Games\Example Saga", r"C:\GOG Games\Example Saga Evil\g.exe", "exe"),
|
||||
];
|
||||
for (path, exe, field) in cases {
|
||||
let mut e = entry();
|
||||
e.path = path.into();
|
||||
e.exe = exe.into();
|
||||
e.working_dir = None;
|
||||
assert_eq!(game(&e), Err(ParseError::BadPath(field)), "{path} {exe}");
|
||||
}
|
||||
let mut e = entry();
|
||||
e.working_dir = Some(r"\\?\C:\x".into());
|
||||
assert_eq!(game(&e), Err(ParseError::BadPath("working_dir")));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn exe_extension_ignores_case() {
|
||||
let mut e = entry();
|
||||
e.exe = r"C:\GOG Games\Example Saga\SAGA.EXE".into();
|
||||
assert!(game(&e).is_ok());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn bad_args() {
|
||||
for bad in ["a\u{0}b", "a\nb", "a\rb", "\"unclosed", "a\u{7}"] {
|
||||
let mut e = entry();
|
||||
e.launch_params = Some(bad.into());
|
||||
assert_eq!(game(&e), Err(ParseError::BadValue("launch_params")), "{bad:?}");
|
||||
}
|
||||
let mut e = entry();
|
||||
e.launch_params = Some(vec!["-a"; limits::MAX_ARGS + 1].join(" "));
|
||||
assert_eq!(game(&e), Err(ParseError::TooMany("args")));
|
||||
e.launch_params = Some("x".repeat(limits::MAX_ARG + 1));
|
||||
assert_eq!(game(&e), Err(ParseError::TooLong("arg")));
|
||||
e.launch_params = Some("x ".repeat(limits::MAX_ARGS_LINE));
|
||||
assert_eq!(game(&e), Err(ParseError::TooLong("launch_params")));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn shell_metacharacters_stay_inside_one_argument() {
|
||||
let mut e = entry();
|
||||
e.launch_params = Some(r#""a & calc" |b ^c %PATH%"#.into());
|
||||
let g = game(&e).unwrap();
|
||||
let (_, _, args) = exe_parts(&g.launch);
|
||||
assert_eq!(args, ["a & calc", "|b", "^c", "%PATH%"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn bad_title() {
|
||||
let mut e = entry();
|
||||
e.name = " ".into();
|
||||
assert_eq!(game(&e), Err(ParseError::Missing("name")));
|
||||
e.name = "x".repeat(limits::MAX_TITLE + 1);
|
||||
assert_eq!(game(&e), Err(ParseError::TooLong("name")));
|
||||
e.name = "a\u{1b}[31mred".into();
|
||||
assert_eq!(game(&e), Err(ParseError::BadValue("name")));
|
||||
}
|
||||
Reference in New Issue
Block a user