[core] Tests: fixtures and mandatory tests (valid, hostile, property, launch, dedupe) ahead of the code
This commit is contained in:
@@ -0,0 +1,120 @@
|
||||
// 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/.
|
||||
|
||||
//! Epic `.item` manifests: valid fixtures and the hostile ones that are Epic-specific.
|
||||
|
||||
use arena_core::{CoverHint, Launch, ParseError, Source, WinPath, epic, limits};
|
||||
|
||||
#[test]
|
||||
fn valid_item() {
|
||||
let g = epic::item(include_bytes!("fixtures/epic/valid.item")).unwrap();
|
||||
assert_eq!(g.id.as_str(), "epic:exampleracer_main-01");
|
||||
assert_eq!(g.source, Source::Epic);
|
||||
assert_eq!(g.title, "Example Racer");
|
||||
assert_eq!(g.install_dir.as_ref().map(WinPath::as_str), Some(r"C:\Program Files\Epic Games\ExampleRacer"));
|
||||
assert_eq!(g.launch, Launch::Epic { app_name: "ExampleRacer_Main-01".into() });
|
||||
assert_eq!(
|
||||
g.launch.uri().as_deref(),
|
||||
Some("com.epicgames.launcher://apps/ExampleRacer_Main-01?action=launch&silent=true")
|
||||
);
|
||||
assert!(matches!(g.cover, Some(CoverHint::Lettered { letter: 'E', .. })), "{:?}", g.cover);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn bom_is_accepted() {
|
||||
let mut bytes = vec![0xEF, 0xBB, 0xBF];
|
||||
bytes.extend_from_slice(include_bytes!("fixtures/epic/valid.item"));
|
||||
assert!(epic::item(&bytes).is_ok());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn addon_is_not_a_game() {
|
||||
let r = epic::item(include_bytes!("fixtures/epic/addon.item"));
|
||||
assert!(matches!(r, Err(ParseError::NotAGame(_))), "{r:?}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn wrong_type_app_name() {
|
||||
assert_eq!(epic::item(include_bytes!("fixtures/epic/wrongtype.item")), Err(ParseError::WrongType("AppName")));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn wrong_type_for_each_field() {
|
||||
let cases = [
|
||||
(r#"{"AppName":"A","DisplayName":["x"],"InstallLocation":"C:\\G"}"#, "DisplayName"),
|
||||
(r#"{"AppName":"A","DisplayName":"x","InstallLocation":{"a":1}}"#, "InstallLocation"),
|
||||
(r#"{"AppName":"A","DisplayName":"x","InstallLocation":"C:\\G","MainGameAppName":7}"#, "MainGameAppName"),
|
||||
(r#"{"AppName":null,"DisplayName":"x","InstallLocation":"C:\\G"}"#, "AppName"),
|
||||
];
|
||||
for (json, field) in cases {
|
||||
assert_eq!(epic::item(json.as_bytes()), Err(ParseError::WrongType(field)), "{json}");
|
||||
}
|
||||
assert_eq!(epic::item(b"[]"), Err(ParseError::WrongType("item")));
|
||||
assert_eq!(epic::item(b"\"text\""), Err(ParseError::WrongType("item")));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn duplicate_key() {
|
||||
assert_eq!(epic::item(include_bytes!("fixtures/epic/dupkey.item")), Err(ParseError::Duplicate("AppName")));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn traversal_in_install_location() {
|
||||
assert_eq!(epic::item(include_bytes!("fixtures/epic/traversal.item")), Err(ParseError::BadPath("InstallLocation")));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unc_install_location() {
|
||||
assert_eq!(epic::item(include_bytes!("fixtures/epic/unc.item")), Err(ParseError::BadPath("InstallLocation")));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn injected_app_name() {
|
||||
assert_eq!(epic::item(include_bytes!("fixtures/epic/badappname.item")), Err(ParseError::BadId));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn app_name_rules() {
|
||||
let long = "a".repeat(limits::MAX_EPIC_APP + 1);
|
||||
for bad in ["", " A", "A B", "a.b", "a/b", "a%20", "a?b", "ä", long.as_str()] {
|
||||
let json = format!(r#"{{"AppName":"{bad}","DisplayName":"x","InstallLocation":"C:\\G"}}"#);
|
||||
assert_eq!(epic::item(json.as_bytes()), Err(ParseError::BadId), "{bad:?}");
|
||||
}
|
||||
let ok = "A".repeat(limits::MAX_EPIC_APP);
|
||||
let json = format!(r#"{{"AppName":"{ok}","DisplayName":"x","InstallLocation":"C:\\G"}}"#);
|
||||
assert!(epic::item(json.as_bytes()).is_ok());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn missing_fields() {
|
||||
assert_eq!(epic::item(include_bytes!("fixtures/epic/missing.item")), Err(ParseError::Missing("AppName")));
|
||||
assert_eq!(epic::item(br#"{"AppName":"A","InstallLocation":"C:\\G"}"#), Err(ParseError::Missing("DisplayName")));
|
||||
assert_eq!(epic::item(br#"{"AppName":"A","DisplayName":"x"}"#), Err(ParseError::Missing("InstallLocation")));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn trailing_garbage() {
|
||||
assert!(matches!(epic::item(include_bytes!("fixtures/epic/trailing.item")), Err(ParseError::Syntax { .. })));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn absurd_number() {
|
||||
let r = epic::item(include_bytes!("fixtures/epic/absurd.item"));
|
||||
assert!(matches!(r, Err(ParseError::Syntax { .. }) | Err(ParseError::WrongType("AppName"))), "{r:?}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn syntax_error_offset_points_into_the_input() {
|
||||
let json = b"{\n \"AppName\": \"A\",\n oops\n}";
|
||||
match epic::item(json) {
|
||||
Err(ParseError::Syntax { at }) => assert!(at < json.len(), "{at}"),
|
||||
other => panic!("{other:?}"),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn main_game_equal_to_app_name_is_a_game() {
|
||||
let json = br#"{"AppName":"A","MainGameAppName":"A","DisplayName":"x","InstallLocation":"C:\\G"}"#;
|
||||
assert!(epic::item(json).is_ok());
|
||||
}
|
||||
Reference in New Issue
Block a user