Visual Studio Code, rebuilt in Rust
Rust
use std::process::Command;
use super::*;
fn scratch(name: &str) -> PathBuf {
let dir = std::env::temp_dir().join(format!("vsrustium-update-{name}-{}", std::process::id()));
let _ = std::fs::remove_dir_all(&dir);
std::fs::create_dir_all(&dir).unwrap();
dir
}
/// An ad-hoc signed bundle around a copy of /usr/bin/true. The tests with codesign, ditto or chflags run on macOS only:
/// the Linux install is the M23 Linux row of update/install.rs.
#[cfg(target_os = "macos")]
fn bundle(dir: &Path, id: &str) -> PathBuf {
let app = dir.join("VSRustium.app");
std::fs::create_dir_all(app.join("Contents/MacOS")).unwrap();
std::fs::copy("/usr/bin/true", app.join("Contents/MacOS/vsrustium")).unwrap();
let plist = format!(
r#"<?xml version="1.0" encoding="UTF-8"?><plist version="1.0"><dict><key>CFBundleIdentifier</key><string>{id}</string><key>CFBundleExecutable</key><string>vsrustium</string></dict></plist>"#
);
std::fs::write(app.join("Contents/Info.plist"), plist).unwrap();
assert!(Command::new("codesign").args(["--force", "-s", "-"]).arg(&app).status().unwrap().success());
app
}
#[cfg(target_os = "macos")]
fn zip(source: &Path, zip: &Path) {
assert!(Command::new("ditto").args(["-c", "-k", "--keepParent"]).arg(source).arg(zip).status().unwrap().success());
}
#[cfg(target_os = "macos")]
#[test]
fn a_zip_with_another_hash_is_refused_before_it_is_extracted() {
let dir = scratch("hash");
let app = bundle(&dir.join("new"), "com.rokartur.vsrustium");
zip(&app, &dir.join("update.zip"));
let error = verify_download(&dir.join("update.zip"), &"0".repeat(64), &dir, &app).unwrap_err();
assert!(error.to_string().contains("is not the feed's"), "{error:#}");
assert!(!dir.join("extracted").exists());
}
#[cfg(target_os = "macos")]
#[test]
fn a_zip_without_exactly_one_app_is_refused() {
let dir = scratch("contents");
std::fs::create_dir_all(dir.join("payload/a.app")).unwrap();
std::fs::create_dir_all(dir.join("payload/b.app")).unwrap();
zip(&dir.join("payload/a.app"), &dir.join("one.zip"));
let hash = sha256_file(&dir.join("one.zip")).unwrap();
assert!(Command::new("ditto").args(["-c", "-k"]).arg(dir.join("payload")).arg(dir.join("two.zip")).status().unwrap().success());
let error = verify_download(&dir.join("two.zip"), &sha256_file(&dir.join("two.zip")).unwrap(), &dir, &dir).unwrap_err();
assert!(error.to_string().contains("exactly one app bundle"), "{error:#}");
// One app passes the layout check and reaches the signature check, which an unsigned folder fails.
let error = verify_download(&dir.join("one.zip"), &hash, &dir, &dir.join("payload/a.app")).unwrap_err();
assert!(error.to_string().contains("is not signed"), "{error:#}");
}
#[cfg(target_os = "macos")]
#[test]
fn an_ad_hoc_signed_app_verifies_no_update() {
let dir = scratch("adhoc");
let installed = bundle(&dir.join("installed"), "com.rokartur.vsrustium");
let candidate = bundle(&dir.join("candidate"), "com.rokartur.vsrustium");
let error = verify_signature(&candidate, &installed).unwrap_err();
assert!(error.to_string().contains("not signed with a Developer ID"), "{error:#}");
}
#[test]
fn the_sha256_matches_shasum() {
let dir = scratch("sha");
std::fs::write(dir.join("file"), b"abc").unwrap();
assert_eq!(sha256_file(&dir.join("file")).unwrap(), "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad");
}
#[cfg(unix)]
#[test]
fn a_read_only_or_translocated_bundle_cannot_update() {
let dir = scratch("writable");
std::fs::create_dir_all(dir.join("VSRustium.app")).unwrap();
check_writable(&dir.join("VSRustium.app")).unwrap();
let error = check_writable(Path::new("/System/Applications/Calculator.app")).unwrap_err();
assert!(error.to_string().starts_with("Cannot update while running on a read-only volume."));
let translocated = dir.join("AppTranslocation/ABCD/d/VSRustium.app");
std::fs::create_dir_all(&translocated).unwrap();
assert!(check_writable(&translocated).is_err());
}
fn app_with(dir: &Path, name: &str, marker: &str) -> PathBuf {
let app = dir.join(name);
std::fs::create_dir_all(app.join("Contents")).unwrap();
std::fs::write(app.join("Contents/marker"), marker).unwrap();
app
}
fn marker(app: &Path) -> String {
std::fs::read_to_string(app.join("Contents/marker")).unwrap()
}
#[test]
fn replace_moves_the_update_in_and_the_old_app_aside() {
let dir = scratch("replace");
let installed = app_with(&dir, "VSRustium.app", "N");
let new = app_with(&dir.join("staging"), "VSRustium.app", "N+1");
replace(&new, &installed, &dir.join(".VSRustium.app.old")).unwrap();
assert_eq!(marker(&installed), "N+1");
assert_eq!(marker(&dir.join(".VSRustium.app.old")), "N");
assert!(!new.exists());
}
#[cfg(target_os = "macos")]
#[test]
fn a_failed_move_in_puts_the_installed_app_back() {
let dir = scratch("rollback");
let installed = app_with(&dir, "VSRustium.app", "N");
let aside = dir.join(".VSRustium.app.old");
let error = replace(&dir.join("missing.app"), &installed, &aside).unwrap_err();
assert!(format!("{error:#}").contains("the installed app was put back"), "{error:#}");
assert_eq!(marker(&installed), "N");
assert!(!aside.exists());
// An immutable update cannot be renamed, the failure port/tools/update-check uses for the end-to-end rollback.
let new = app_with(&dir.join("staging"), "VSRustium.app", "N+1");
assert!(Command::new("chflags").arg("uchg").arg(&new).status().unwrap().success());
let result = replace(&new, &installed, &aside);
assert!(Command::new("chflags").arg("nouchg").arg(&new).status().unwrap().success());
assert!(result.is_err());
assert_eq!(marker(&installed), "N");
}
/// The failure cases of port/tools/update-check on its signed packages, without a window:
/// `UPDATE_CHECK_PACKAGES=/tmp/update-check/packages cargo gc test -p app update::install -- --ignored`.
#[test]
#[ignore = "needs the packages of port/tools/update-check"]
fn the_packaged_builds_verify_as_the_update_check_expects() {
let packages = PathBuf::from(std::env::var("UPDATE_CHECK_PACKAGES").expect("UPDATE_CHECK_PACKAGES"));
let installed_n = packages.join("N/VSRustium.app");
let dir = scratch("packages");
// package-app names the zip by version and platform: the one zip in the package's folder.
let zip = |name: &str| std::fs::read_dir(packages.join(name)).unwrap().map(|entry| entry.unwrap().path()).find(|path| path.extension().is_some_and(|ext| ext == "zip")).unwrap();
let refused = |zip: &Path, hash: &str, staging: &str| format!("{:#}", verify_download(zip, hash, &dir.join(staging), &installed_n).unwrap_err());
let staged = verify_download(&zip("N1"), &sha256_file(&zip("N1")).unwrap(), &dir.join("n1"), &installed_n).unwrap();
assert!(refused(&zip("N1"), &"0".repeat(64), "sha").contains("is not the feed's"));
let adhoc = refused(&zip("adhoc"), &sha256_file(&zip("adhoc")).unwrap(), "adhoc");
assert!(adhoc.contains("does not satisfy"), "{adhoc}");
let foreign = refused(&zip("foreign"), &sha256_file(&zip("foreign")).unwrap(), "foreign");
assert!(foreign.contains("does not satisfy"), "{foreign}");
// A download cut midway: the first half of the zip.
let bytes = std::fs::read(zip("N1")).unwrap();
std::fs::write(dir.join("cut.zip"), &bytes[..bytes.len() / 2]).unwrap();
assert!(refused(&dir.join("cut.zip"), &sha256_file(&zip("N1")).unwrap(), "cut").contains("is not the feed's"));
// The helper on an installed N whose update cannot move in: N is put back, still valid.
std::fs::create_dir_all(dir.join("Applications")).unwrap();
let installed = dir.join("Applications/VSRustium.app");
assert!(Command::new("ditto").arg(&installed_n).arg(&installed).status().unwrap().success());
let mut exited = Command::new("true").spawn().unwrap();
exited.wait().unwrap();
assert!(Command::new("chflags").arg("uchg").arg(&staged).status().unwrap().success());
let args = [exited.id().to_string(), staged.display().to_string(), installed.display().to_string()];
let swapped = apply_update(&args);
assert!(Command::new("chflags").arg("nouchg").arg(&staged).status().unwrap().success());
assert!(format!("{:#}", swapped.unwrap_err()).contains("the installed app was put back"));
verify_signature(&installed, &installed_n).unwrap();
let version = Command::new("plutil").args(["-extract", "CFBundleShortVersionString", "raw"]).arg(installed.join("Contents/Info.plist")).output().unwrap();
assert_eq!(String::from_utf8_lossy(&version.stdout).trim(), "1.147.0");
// And with the update movable, the helper installs it.
apply_update(&args).unwrap();
verify_signature(&installed, &installed_n).unwrap();
let version = Command::new("plutil").args(["-extract", "CFBundleShortVersionString", "raw"]).arg(installed.join("Contents/Info.plist")).output().unwrap();
assert_eq!(String::from_utf8_lossy(&version.stdout).trim(), "1.148.0");
assert!(!dir.join("Applications/.VSRustium.app.old").exists());
}
#[test]
fn a_test_binary_runs_outside_a_bundle() {
assert_eq!(running_bundle(), None);
}