// SPDX-License-Identifier: CPAL-1.0 // Copyright (c) 2026 Marek Novák. Built on Domi, https://aula.marek-novak.cz //! Launch strings contain only parsed, validated fields: nothing raw from a file reaches a URI or an argument. use arena_core::gog::GogEntry; use arena_core::{Launch, ParseError, WinPath, epic, gog, limits, steam}; use proptest::prelude::*; fn is_epic_app(s: &str) -> bool { !s.is_empty() && s.len() <= limits::MAX_EPIC_APP && s.bytes().all(|b| b.is_ascii_alphanumeric() || b == b'_' || b == b'-') } fn is_domi_id(s: &str) -> bool { !s.is_empty() && s.len() <= limits::MAX_DOMI_ID && !s.starts_with('-') && !s.ends_with('-') && s.bytes().all(|b| b.is_ascii_lowercase() || b.is_ascii_digit() || b == b'-') } /// The URI is exactly the template filled with the field, and the field passes its rule. fn check_uri(l: &Launch) { let uri = l.uri(); match l { Launch::SteamRunGameId(id) => { assert_eq!(uri, (*id != 0).then(|| format!("steam://rungameid/{id}"))); } Launch::Epic { app_name } => { let want = is_epic_app(app_name) .then(|| format!("com.epicgames.launcher://apps/{app_name}?action=launch&silent=true")); assert_eq!(uri, want, "{app_name:?}"); } Launch::DomiLink { app } => { assert_eq!(uri, is_domi_id(app).then(|| format!("domi-{app}://open")), "{app:?}"); } Launch::Exe { .. } => assert_eq!(uri, None), Launch::Url(u) => { if let Some(got) = &uri { assert_eq!(got, u); assert!(got.starts_with("https://")); assert!(got.bytes().all(|b| b.is_ascii_graphic() && b != b'\\' && b != b'"'), "{got:?}"); } } } } #[test] fn fixture_uris_are_templates_of_parsed_fields() { let lib = WinPath::parse(r"D:\SteamLibrary").unwrap(); let s = steam::app_manifest(include_bytes!("fixtures/steam/appmanifest_1234560.acf"), &lib).unwrap(); check_uri(&s.launch); assert_eq!(s.launch.uri().unwrap(), "steam://rungameid/1234560"); let e = epic::item(include_bytes!("fixtures/epic/valid.item")).unwrap(); check_uri(&e.launch); assert_eq!(e.launch.uri().unwrap(), "com.epicgames.launcher://apps/ExampleRacer_Main-01?action=launch&silent=true"); } #[test] fn hand_built_bad_values_give_no_uri() { let bad = [ Launch::SteamRunGameId(0), Launch::Epic { app_name: String::new() }, Launch::Epic { app_name: "Fortnite?action=uninstall".into() }, Launch::Epic { app_name: "a/../../b".into() }, Launch::Epic { app_name: "a b".into() }, Launch::Epic { app_name: "a".repeat(65) }, Launch::DomiLink { app: "Lapilli".into() }, Launch::DomiLink { app: "x://evil".into() }, Launch::DomiLink { app: "-x".into() }, Launch::Url("javascript:alert(1)".into()), Launch::Url("http://example.org/".into()), Launch::Url("https://example.org/a b".into()), Launch::Url("https://user@example.org/".into()), Launch::Url("file:///C:/Windows/System32/calc.exe".into()), ]; for l in &bad { assert_eq!(l.uri(), None, "{l:?}"); assert_eq!(l.uri_checked(&["example.org"]), None, "{l:?}"); check_uri(l); } let exe = Launch::Exe { exe: WinPath::parse(r"C:\G\g.exe").unwrap(), working_dir: WinPath::parse(r"C:\G").unwrap(), args: vec![], }; assert_eq!(exe.uri(), None); } #[test] fn url_needs_allowed_host_when_checked() { let l = Launch::web("https://games.example.org/x", &["games.example.org"]).unwrap(); assert_eq!(l.uri_checked(&["GAMES.example.org"]).as_deref(), Some("https://games.example.org/x")); assert_eq!(l.uri_checked(&[]), None); assert_eq!(Launch::web("https://games.example.org/x", &[]), Err(ParseError::BadValue("url"))); } #[test] fn builders_validate() { assert_eq!(Launch::steam(0), Err(ParseError::BadId)); assert_eq!(Launch::steam(10).unwrap(), Launch::SteamRunGameId(10)); assert_eq!(Launch::epic("a?b"), Err(ParseError::BadId)); assert_eq!(Launch::domi_link("A"), Err(ParseError::BadId)); let exe = WinPath::parse(r"C:\G\g.exe").unwrap(); let dir = WinPath::parse(r"C:\G").unwrap(); assert_eq!( Launch::exe(WinPath::parse(r"C:\G\g.bat").unwrap(), dir.clone(), vec![]), Err(ParseError::BadPath("exe")) ); assert_eq!(Launch::exe(exe.clone(), dir.clone(), vec!["a\u{0}".into()]), Err(ParseError::BadValue("args"))); assert_eq!(Launch::exe(exe.clone(), dir.clone(), vec!["a\nb".into()]), Err(ParseError::BadValue("args"))); assert_eq!( Launch::exe(exe.clone(), dir.clone(), vec!["x".repeat(limits::MAX_ARG + 1)]), Err(ParseError::TooLong("arg")) ); assert_eq!( Launch::exe(exe.clone(), dir.clone(), vec![String::new(); limits::MAX_ARGS + 1]), Err(ParseError::TooMany("args")) ); assert!(Launch::exe(exe, dir, vec!["--fine".into(), "with space".into()]).is_ok()); } #[test] fn gog_args_are_exactly_the_split_params() { let e = GogEntry { game_id: "1".into(), name: "G".into(), path: r"C:\G".into(), exe: r"C:\G\g.exe".into(), working_dir: None, launch_params: Some(r#" -a "b c" d"#.into()), }; let g = gog::game(&e).unwrap(); match g.launch { Launch::Exe { args, .. } => assert_eq!(args, ["-a", "b c", "d"]), other => panic!("{other:?}"), } } #[test] fn deserialising_validates_every_field() { let good = r#"{"epic":{"app_name":"Fortnite"}}"#; assert_eq!(serde_json::from_str::(good).unwrap(), Launch::Epic { app_name: "Fortnite".into() }); for bad in [ r#"{"epic":{"app_name":"a?b"}}"#, r#"{"steam_run_game_id":0}"#, r#"{"steam_run_game_id":4294967296}"#, r#"{"domi_link":{"app":"X"}}"#, r#"{"url":"http://example.org/"}"#, r#"{"exe":{"exe":"C:\\G\\g.bat","working_dir":"C:\\G","args":[]}}"#, r#"{"exe":{"exe":"C:\\G\\..\\g.exe","working_dir":"C:\\G","args":[]}}"#, r#"{"exe":{"exe":"C:\\G\\g.exe","working_dir":"C:\\G","args":["a\u0000"]}}"#, ] { assert!(serde_json::from_str::(bad).is_err(), "{bad}"); } } #[test] fn serde_round_trip() { let all = [ Launch::steam(10).unwrap(), Launch::epic("A_b-1").unwrap(), Launch::domi_link("lapilli").unwrap(), Launch::web("https://games.example.org/p?q=1", &["games.example.org"]).unwrap(), Launch::exe(WinPath::parse(r"C:\G\g.exe").unwrap(), WinPath::parse(r"C:\G").unwrap(), vec!["-x".into()]) .unwrap(), ]; for l in all { let json = serde_json::to_string(&l).unwrap(); assert_eq!(serde_json::from_str::(&json).unwrap(), l, "{json}"); } } proptest! { #[test] fn epic_uri_only_from_valid_names(name in ".{0,80}") { let l = Launch::Epic { app_name: name.clone() }; check_uri(&l); prop_assert_eq!(Launch::epic(&name).is_ok(), is_epic_app(&name)); } #[test] fn epic_uri_from_generated_valid_names(name in "[A-Za-z0-9_-]{1,64}") { let l = Launch::epic(&name).unwrap(); prop_assert_eq!(l.uri().unwrap(), format!("com.epicgames.launcher://apps/{name}?action=launch&silent=true")); } #[test] fn domi_uri_only_from_valid_ids(app in ".{0,40}") { check_uri(&Launch::DomiLink { app }); } #[test] fn steam_uri_any_id(id in any::()) { check_uri(&Launch::SteamRunGameId(id)); } #[test] fn url_uri_never_escapes_the_rules(url in "(https?://)?[a-zA-Z0-9.:@/?#%&=_ \\\\\"-]{0,60}") { let l = Launch::Url(url); check_uri(&l); } #[test] fn epic_manifest_app_name_reaches_uri_only_when_valid(name in "[ -~]{0,70}") { let json = serde_json::json!({"AppName": name, "DisplayName": "x", "InstallLocation": "C:\\G"}).to_string(); match epic::item(json.as_bytes()) { Ok(g) => { prop_assert!(is_epic_app(&name)); prop_assert_eq!(g.launch.uri().unwrap(), format!("com.epicgames.launcher://apps/{name}?action=launch&silent=true")); } Err(e) => { prop_assert!(!is_epic_app(&name)); prop_assert_eq!(e, ParseError::BadId); } } } }