From 96ce9e723d9586170b96f3afc18a3bdf7f260c09 Mon Sep 17 00:00:00 2001 From: unRekable Date: Fri, 4 Dec 2026 01:29:30 +0100 Subject: [PATCH] Test that both Info.plists and the entitlements keep the camera entries that stop macOS killing mediad when it opens a camera --- crates/mediad/src/main.rs | 105 +++++++++++++++++++++++++++++++++++++- 1 file changed, 104 insertions(+), 1 deletion(-) diff --git a/crates/mediad/src/main.rs b/crates/mediad/src/main.rs index f5f6e2f0..92b9e53b 100644 --- a/crates/mediad/src/main.rs +++ b/crates/mediad/src/main.rs @@ -8,7 +8,8 @@ use std::process::ExitCode; /// responsible. Inside `tinyclient.app` that is the app, whose Info.plist says it. When mediad is /// responsible for itself (its parents were started by hand and are gone), this copy, in the /// section a single-file tool keeps its Info.plist in, is what the system reads; without one -/// the system kills the process the moment it opens the microphone. +/// the system kills the process the moment it opens the microphone. The camera is asked about +/// the same way, so the copy says why that is opened too. #[used] #[link_section = "__TEXT,__info_plist"] static INFO_PLIST: [u8; include_bytes!("../Info.plist").len()] = *include_bytes!("../Info.plist"); @@ -160,3 +161,105 @@ fn main() -> ExitCode { mediad::out::flush(); ExitCode::from(code) } + +#[cfg(test)] +mod tests { + use super::INFO_PLIST; + + /// What an XML plist holds for `key`: the tag of the value after the first `` with that + /// name, and the text between that value's tags, trimmed. `None` when there is no such key. + fn value<'a>(plist: &'a str, key: &str) -> Option<(&'a str, &'a str)> { + let mut found = false; + let mut commented = false; + // Each piece is one tag and the text up to the next tag. + for piece in plist.split('<').skip(1) { + // A comment runs to its `-->`, whatever stands inside it: a tag commented out is not read. + if commented || piece.starts_with("!--") { + commented = !piece.contains("-->"); + continue; + } + let Some((tag, text)) = piece.split_once('>') else { continue }; + // A closing tag, the header and the doctype are not an element. + if tag.starts_with(['/', '?', '!']) { + continue; + } + // `` and `` are one element, and an element may carry attributes. + let Some(name) = tag.strip_suffix('/').unwrap_or(tag).split_whitespace().next() else { continue }; + let text = text.trim(); + if found { + return Some((name, text)); + } + found = name == "key" && text == key; + } + None + } + + #[test] + fn a_plist_value_is_the_element_after_its_key() { + let plist = r#" + + + + One + Twoin a dict + Three + + Why & how + + Four + + Five + + Six + + Seven + + + Eight + after the comment + + +"#; + assert_eq!(value(plist, "One"), Some(("array", ""))); + assert_eq!(value(plist, "Two"), Some(("string", "in a dict"))); + assert_eq!(value(plist, "Three"), Some(("string", "Why & how"))); + assert_eq!(value(plist, "Four"), Some(("string", ""))); + assert_eq!(value(plist, "Five"), Some(("true", ""))); + assert_eq!(value(plist, "Six"), Some(("true", ""))); + assert_eq!(value(plist, "Seven"), Some(("false", ""))); + assert_eq!(value(plist, "Eight"), Some(("string", "after the comment"))); + assert_eq!(value(plist, "Nine"), None, "a key inside a comment is not in the plist"); + assert_eq!(value(plist, "Missing"), None); + assert_eq!(value("Last", "Last"), None, "a key with no value after it"); + } + + #[test] + fn both_info_plists_say_why_the_camera_is_opened() { + // macOS ends a process that opens a camera when the Info.plist of the app it holds + // responsible gives no reason, instead of asking the person (#1019). Inside + // `tinyclient.app` that is the app's plist, which `packaging/bundle.sh` makes from + // `packaging/Info.plist`. When mediad is responsible for itself, the system reads the copy + // embedded above. An empty reason tells the person nothing, so it fails too. + let embedded = std::str::from_utf8(&INFO_PLIST).expect("the embedded Info.plist is UTF-8"); + for (file, plist) in [ + ("packaging/Info.plist", include_str!("../../../packaging/Info.plist")), + ("crates/mediad/Info.plist", embedded), + ] { + let Some((tag, reason)) = value(plist, "NSCameraUsageDescription") else { + panic!("{file} does not say why the camera is opened") + }; + assert!(tag == "string" && !reason.is_empty(), "{file} holds NSCameraUsageDescription as <{tag}> `{reason}`"); + } + } + + #[test] + fn the_entitlements_allow_the_camera() { + // `packaging/bundle.sh` signs `packaging/entitlements.plist` into mediad and into the app. + // It is inert until the hardened runtime is turned on; from then on the camera needs it + // (#1019). + let Some((tag, _)) = value(include_str!("../../../packaging/entitlements.plist"), "com.apple.security.device.camera") else { + panic!("packaging/entitlements.plist has no camera entitlement") + }; + assert!(tag == "true", "packaging/entitlements.plist holds the camera entitlement as <{tag}/>"); + } +}