use super::*;
#[test]
fn the_mac_install_checks_hashicorps_file_and_signature_before_installing_as_root() {
let step = mac_install(Arch::Arm64, Elevation::Prompt);
assert_eq!(step.program, "sh");
let [c, user, zero, root] = step.args.as_slice() else {
panic!("{:?}", step.args)
};
assert_eq!((c.as_str(), zero.as_str()), ("-c", "sh"));
assert!(crate::testing::sh_parses(user), "not valid sh: {user}");
assert!(crate::testing::sh_parses(root), "not valid sh: {root}");
assert!(
user.contains("https://releases.hashicorp.com/vagrant-vmware-utility/1.0.24/vagrant-vmware-utility_1.0.24_darwin_arm64.dmg"),
"{user}"
);
assert!(
user.contains("with administrator privileges") && user.contains("quoted form of (item 2 of argv)"),
"{user}"
);
let at = |needle: &str| root.find(needle).unwrap_or_else(|| panic!("{needle} missing: {root}"));
assert!(at("cp \"$1\"") < at("shasum -a 256") && at("shasum -a 256") < at("pkgutil --check-signature"));
assert!(at("pkgutil --check-signature") < at("spctl --assess --type install") && at("spctl --assess") < at("installer -pkg"));
assert!(at("installer -pkg") < at("until nc -z 127.0.0.1 9922"), "{root}");
assert!(
root.contains("Hashicorp, Inc. (D38WU7D763)") && root.contains("mktemp -d /private/tmp/"),
"{root}"
);
assert_eq!(mac_image(Arch::Arm64).1, mac_image(Arch::Amd64).1);
assert!(mac_image(Arch::Amd64).0.ends_with("_darwin_amd64.dmg"));
assert_eq!(step.note, Some(Note::DownloadingVmwareUtility { version: "1.0.24" }));
}
#[test]
fn the_mac_install_unattended_runs_its_root_half_through_sudo() {
let step = mac_install(Arch::Amd64, Elevation::Unattended { root: false });
let user = &step.args[1];
assert!(user.contains(r#"sudo -n /bin/sh -c "$1" sh "$d/utility.dmg""#), "{user}");
assert!(!user.contains("osascript") && crate::testing::sh_parses(user), "{user}");
}
#[test]
fn every_pinned_checksum_is_a_sha256() {
let sha = mac_image(Arch::Arm64).1;
assert!(
sha.len() == 64 && sha.chars().all(|c| c.is_ascii_hexdigit() && !c.is_ascii_uppercase()),
"{sha}"
);
}
#[cfg(target_os = "macos")]
fn run_users_half(script: &str, bin: &std::path::Path) -> (Option<i32>, String) {
let path = format!("{}:/usr/bin:/bin", bin.display());
let out = crate::testing::run_env("sh", &["-c", script, "sh", "true"], &[("PATH", &path)]).expect("sh starts");
(out.status.code, String::from_utf8_lossy(&out.stderr).into_owned())
}
#[cfg(target_os = "macos")]
#[test]
fn a_download_that_isnt_hashicorps_file_fails_before_any_prompt_and_a_declined_prompt_says_so() {
let bin = tempfile::tempdir().unwrap();
let curl = |body: &str| crate::testing::write_executable(bin.path().join("curl"), format!("#!/bin/sh\n{body}\n"));
let osascript = |body: &str| crate::testing::write_executable(bin.path().join("osascript"), format!("#!/bin/sh\n{body}\n"));
curl(r#"while [ $# -gt 0 ]; do [ "$1" = -o ] && echo tampered > "$2"; shift; done"#);
osascript("echo prompted >&2; exit 0");
let (file, sha256) = mac_image(Arch::Arm64);
let (code, stderr) = run_users_half(&mac_script(&file, sha256, Elevation::Prompt), bin.path());
assert_eq!(code, Some(1), "{stderr}");
assert!(stderr.contains("SHA-256 doesn't match") && !stderr.contains("prompted"), "{stderr}");
let empty_sha = "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855";
curl(r#"while [ $# -gt 0 ]; do [ "$1" = -o ] && : > "$2"; shift; done"#);
osascript("echo '0:0: execution error: User canceled. (-128)' >&2; exit 1");
let (declined, said) = run_users_half(&mac_script("x.dmg", empty_sha, Elevation::Prompt), bin.path());
assert_eq!(declined, Some(1), "{said}");
assert!(said.contains("password prompt was cancelled"), "{said}");
}
#[test]
fn the_windows_install_checks_hashicorps_file_and_signature_and_elevates_through_uac() {
let step = windows_install();
assert_eq!(step.program, "powershell");
let script = step.args.last().unwrap();
assert!(script.contains("https://releases.hashicorp.com/vagrant-vmware-utility/1.0.24/vagrant-vmware-utility_1.0.24_windows_amd64.msi"));
assert!(
script.contains("-Verb RunAs") && script.contains("catch { exit 1223 }"),
"a declined prompt reads as cancelled: {script}"
);
let elevated = windows_elevated(WINDOWS_MSI.1);
let at = |needle: &str| elevated.find(needle).unwrap_or_else(|| panic!("{needle} missing: {elevated}"));
assert!(
at("Copy-Item") < at("Get-FileHash") && at("Get-FileHash") < at("Get-AuthenticodeSignature") && at("Get-AuthenticodeSignature") < at("msiexec.exe")
);
assert!(elevated.contains("'HashiCorp, Inc.'") && elevated.contains("$env:SystemRoot"), "{elevated}");
assert!(script.contains(&ps_quote(&elevated)));
assert!(windows_uninstall().args.last().unwrap().contains(WINDOWS_PRODUCT_CODE));
let msi = WINDOWS_MSI.1;
assert!(
msi.len() == 64 && msi.chars().all(|c| c.is_ascii_hexdigit() && !c.is_ascii_uppercase()),
"{msi}"
);
assert!(WINDOWS_MSI.0.contains(VERSION));
}
#[test]
fn workstation_26_is_made_visible_to_the_utility_before_its_msi_or_its_service() {
let elevated = windows_elevated(WINDOWS_MSI.1);
let at = |needle: &str| elevated.find(needle).unwrap_or_else(|| panic!("{needle} missing: {elevated}"));
assert!(at("reg.exe copy 'HKLM\\SOFTWARE\\VMware, Inc.' 'HKLM\\SOFTWARE\\WOW6432Node\\VMware, Inc.' /s /f") < at("Get-Service"));
assert!(at("Get-Service") < at("msiexec.exe"), "a restart of a utility already there sees it too");
assert!(
elevated.contains("-not (Test-Path ($vmware32 + '\\VMware Workstation'))"),
"a Workstation the 32-bit view has already is left as it is: {elevated}"
);
assert!(elevated.contains("if ($LASTEXITCODE -ne 0) { exit 4 }") && elevated.contains("'CopiedByIsothrum'"));
assert!(windows_install().args.last().unwrap().contains("ExitCode -eq 4"), "a failed copy is said");
}