Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
105 changes: 104 additions & 1 deletion crates/mediad/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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 `<key>` 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;
}
// `<true/>` and `<true />` 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#"<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>One</key>
<array><dict><key>Two</key><string>in a dict</string></dict></array>
<key> Three </key>
<string>
Why &amp; how
</string>
<key>Four</key>
<string></string>
<key>Five</key>
<true/>
<key>Six</key>
<true />
<key>Seven</key>
<false/>
<!-- <key>Nine</key><string>commented out</string> -->
<key>Eight</key>
<string>after the comment</string>
</dict>
</plist>
"#;
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 &amp; 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("<dict><key>Last</key>", "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}/>");
}
}
Loading