aegis-runtime-mv
Native Rust agent runtime: Axum WebSocket/SSE/REST gateway, heartbeat scheduler, dependency-aware task workflow, SQLite storage. Attachment of AienOS. Pre-alpha.
Brought by drakestapleton
aegis-runtime-mv / tests / install_script_tests.rs
| 1 | //! Installer integrity: `install.sh --staged` against a local tarball. The |
| 2 | //! installer must refuse any binary it cannot verify against its checksum. |
| 3 | #![cfg(target_os = "linux")] |
| 4 | |
| 5 | use std::fs; |
| 6 | use std::os::unix::fs::{symlink, PermissionsExt}; |
| 7 | use std::path::{Path, PathBuf}; |
| 8 | use std::process::{Command, Output}; |
| 9 | |
| 10 | struct Staged { |
| 11 | root: tempfile::TempDir, |
| 12 | staged: PathBuf, |
| 13 | install: PathBuf, |
| 14 | tarball: PathBuf, |
| 15 | } |
| 16 | |
| 17 | /// Packs a stub `aegis` binary the way the release workflow does. |
| 18 | fn stage() -> Staged { |
| 19 | let root = tempfile::tempdir().unwrap(); |
| 20 | let dist = root.path().join("dist"); |
| 21 | let staged = root.path().join("staged"); |
| 22 | fs::create_dir_all(&dist).unwrap(); |
| 23 | fs::create_dir_all(&staged).unwrap(); |
| 24 | |
| 25 | let bin = dist.join("aegis"); |
| 26 | fs::write(&bin, "#!/bin/sh\necho 'aegis test stub'\n").unwrap(); |
| 27 | fs::set_permissions(&bin, fs::Permissions::from_mode(0o755)).unwrap(); |
| 28 | |
| 29 | let arch = if cfg!(target_arch = "aarch64") { |
| 30 | "aarch64" |
| 31 | } else { |
| 32 | "x86_64" |
| 33 | }; |
| 34 | let tarball = staged.join(format!("aegis-linux-{arch}.tar.gz")); |
| 35 | let status = Command::new("tar") |
| 36 | .arg("-czf") |
| 37 | .arg(&tarball) |
| 38 | .arg("-C") |
| 39 | .arg(&dist) |
| 40 | .arg("aegis") |
| 41 | .status() |
| 42 | .unwrap(); |
| 43 | assert!(status.success()); |
| 44 | |
| 45 | Staged { |
| 46 | install: root.path().join("bin"), |
| 47 | root, |
| 48 | staged, |
| 49 | tarball, |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | fn write_checksum(s: &Staged, contents: &str) { |
| 54 | let mut name = s.tarball.clone().into_os_string(); |
| 55 | name.push(".sha256"); |
| 56 | fs::write(name, contents).unwrap(); |
| 57 | } |
| 58 | |
| 59 | fn good_checksum(tarball: &Path) -> String { |
| 60 | let out = Command::new("sha256sum").arg(tarball).output().unwrap(); |
| 61 | assert!(out.status.success()); |
| 62 | String::from_utf8(out.stdout).unwrap() |
| 63 | } |
| 64 | |
| 65 | fn install(s: &Staged, path: Option<&Path>) -> Output { |
| 66 | let mut cmd = Command::new("/bin/sh"); |
| 67 | cmd.arg(concat!(env!("CARGO_MANIFEST_DIR"), "/install.sh")) |
| 68 | .arg("--staged") |
| 69 | .arg(&s.staged) |
| 70 | .arg("--install-dir") |
| 71 | .arg(&s.install); |
| 72 | if let Some(path) = path { |
| 73 | cmd.env("PATH", path); |
| 74 | } |
| 75 | cmd.output().unwrap() |
| 76 | } |
| 77 | |
| 78 | fn refused(s: &Staged, out: &Output, message: &str) { |
| 79 | let stderr = String::from_utf8_lossy(&out.stderr); |
| 80 | assert!(!out.status.success(), "install succeeded: {stderr}"); |
| 81 | assert!(stderr.contains(message), "unexpected stderr: {stderr}"); |
| 82 | assert!(!s.install.join("aegis").exists()); |
| 83 | } |
| 84 | |
| 85 | #[test] |
| 86 | fn verified_tarball_installs() { |
| 87 | let s = stage(); |
| 88 | write_checksum(&s, &good_checksum(&s.tarball)); |
| 89 | let out = install(&s, None); |
| 90 | assert!( |
| 91 | out.status.success(), |
| 92 | "{}", |
| 93 | String::from_utf8_lossy(&out.stderr) |
| 94 | ); |
| 95 | assert!(s.install.join("aegis").exists()); |
| 96 | } |
| 97 | |
| 98 | #[test] |
| 99 | fn missing_checksum_is_refused() { |
| 100 | let s = stage(); |
| 101 | let out = install(&s, None); |
| 102 | refused(&s, &out, "No checksum file"); |
| 103 | } |
| 104 | |
| 105 | #[test] |
| 106 | fn empty_checksum_is_refused() { |
| 107 | let s = stage(); |
| 108 | write_checksum(&s, ""); |
| 109 | let out = install(&s, None); |
| 110 | refused(&s, &out, "No checksum file"); |
| 111 | |
| 112 | write_checksum(&s, "\n"); |
| 113 | let out = install(&s, None); |
| 114 | refused(&s, &out, "Checksum verification failed"); |
| 115 | } |
| 116 | |
| 117 | #[test] |
| 118 | fn mismatched_checksum_is_refused() { |
| 119 | let s = stage(); |
| 120 | write_checksum(&s, &format!("{} aegis.tar.gz\n", "0".repeat(64))); |
| 121 | let out = install(&s, None); |
| 122 | refused(&s, &out, "Checksum verification failed"); |
| 123 | } |
| 124 | |
| 125 | #[test] |
| 126 | fn missing_hash_tool_is_refused() { |
| 127 | let s = stage(); |
| 128 | write_checksum(&s, &good_checksum(&s.tarball)); |
| 129 | |
| 130 | // A PATH with every tool the installer uses except sha256sum and shasum. |
| 131 | let tools = s.root.path().join("tools"); |
| 132 | fs::create_dir_all(&tools).unwrap(); |
| 133 | for tool in [ |
| 134 | "uname", "mktemp", "rm", "cp", "find", "head", "awk", "tar", "gzip", "mkdir", "chmod", |
| 135 | ] { |
| 136 | let found = ["/usr/bin", "/bin", "/usr/local/bin"] |
| 137 | .iter() |
| 138 | .map(|dir| Path::new(dir).join(tool)) |
| 139 | .find(|p| p.exists()) |
| 140 | .unwrap_or_else(|| panic!("{tool} not found")); |
| 141 | symlink(found, tools.join(tool)).unwrap(); |
| 142 | } |
| 143 | |
| 144 | let out = install(&s, Some(&tools)); |
| 145 | refused(&s, &out, "Neither sha256sum nor shasum found"); |
| 146 | } |