#![allow(dead_code, unused_imports, unused_qualifications, unreachable_patterns)]
use std::sync::OnceLock;
const UNSIGNED_SUFFIX: &str = "-unsigned";
pub fn is_binary_signed() -> bool {
static RESULT: OnceLock<bool> = OnceLock::new();
*RESULT.get_or_init(|| {
let Ok(exe) = std::env::current_exe() else {
return false;
};
let path = exe.to_string_lossy();
if path.contains("/target/") || path.contains("\\target\\") {
return false;
}
is_codesigned(&exe)
})
}
#[cfg(target_os = "macos")]
fn is_codesigned(exe: &std::path::Path) -> bool {
std::process::Command::new("codesign")
.args(["--verify", "--no-strict"])
.arg(exe)
.output()
.map(|o| o.status.success())
.unwrap_or(false)
}
#[cfg(not(target_os = "macos"))]
fn is_codesigned(_exe: &std::path::Path) -> bool {
true
}
pub fn ensure_safe_app_name(app_name: &str) -> String {
if is_binary_signed() || app_name.ends_with(UNSIGNED_SUFFIX) {
app_name.to_string()
} else {
format!("{app_name}{UNSIGNED_SUFFIX}")
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn unsigned_binary_gets_suffix() {
assert_eq!(ensure_safe_app_name("gocode-dev"), "gocode-dev-unsigned");
assert_eq!(ensure_safe_app_name("sshenc"), "sshenc-unsigned");
}
#[test]
fn already_suffixed_not_doubled() {
assert_eq!(
ensure_safe_app_name("gocode-dev-unsigned"),
"gocode-dev-unsigned"
);
}
#[test]
fn is_binary_signed_false_in_tests() {
assert!(!is_binary_signed());
}
}