mod common; use std::path::{Path, PathBuf}; use std::process::Command; use common::{corpus_root, env_with_fixture, shim_dir, Env, Web}; use aur_build::shims; fn shim(tool: &str, cwd: &Path, envs: &[(&str, &Path)], args: &[&str]) -> (i32, String, String) { let exe = shim_dir().join(tool); assert!(exe.exists(), "shim {tool} not built at {}", exe.display()); let out = Command::new(&exe) .current_dir(cwd) .envs( envs .iter() .map(|(k, v)| (*k, v.as_os_str().to_os_string())), ) .args(args) .output() .expect("run shim"); ( out.status.code().unwrap_or(-1), String::from_utf8_lossy(&out.stdout).into_owned(), String::from_utf8_lossy(&out.stderr).into_owned(), ) } fn fixture_manifest(name: &str) -> PathBuf { corpus_root().join(name).join("manifest.json") } #[test] fn makechrootpkg_emits_pkginfo_buildinfo_and_sig() { let tmp = tempfile::tempdir().unwrap(); let build = tmp.path().join("build"); std::fs::create_dir_all(&build).unwrap(); std::fs::write( build.join("PKGBUILD"), common::pkgbuild("plain-make", "1.0", "1", ""), ) .unwrap(); let log = tmp.path().join("shimlog"); std::fs::create_dir_all(&log).unwrap(); let chroot_args = ["-c", "-r", "chroot", "--", "-siTESTKEY", "-f"]; let (code, out, err) = shim( "makechrootpkg", &build, &[("AUR_SHIM_MANIFEST", &fixture_manifest("plain-make")), ("AUR_SHIM_DIR", &log)], &chroot_args, ); assert_eq!(code, 0, "stdout={out} stderr={err}"); let pkg = build.join("plain-make-1.0-1-x86_64.pkg.tar.zst"); assert!(pkg.exists(), "artifact left in the build dir like the real tool does"); let bytes = std::fs::read(&pkg).unwrap(); let pkginfo = aur_build::pkginfo::pkginfo_text(&bytes).unwrap(); assert!(pkginfo.contains("pkgname = plain-make")); assert!(pkginfo.contains("pkgver = 1.0-1")); assert!(pkginfo.contains("pkgarch = x86_64")); let buildinfo = aur_build::pkginfo::buildinfo_text(&bytes).unwrap(); assert!(buildinfo.contains("format = 2")); assert!(buildinfo.contains("installed ="), "BUILDINFO carries the chroot world"); assert!(buildinfo.contains(" glibc 2.39-1 x86_64 -")); assert!(build.with_extension("pkg.tar.zst.sig").exists() || pkg.with_extension("pkg.tar.zst.sig").exists()); assert_eq!( shims::invocations(&log) .iter() .filter(|e| e["tool"] == "makechrootpkg") .count(), 1, "invocation recorded for the pipeline to assert on" ); } #[test] fn makechrootpkg_refuses_a_missing_pkgbuild() { let tmp = tempfile::tempdir().unwrap(); let build = tmp.path().join("build"); std::fs::create_dir_all(&build).unwrap(); let (code, _, err) = shim( "makechrootpkg", &build, &[("AUR_SHIM_MANIFEST", &fixture_manifest("plain-make"))], &["-c", "-r", "chroot"], ); assert_eq!(code, 1); assert!(err.contains("no PKGBUILD"), "{err}"); } #[test] fn scripted_failures_are_honoured() { for (fixture, expect) in [("build-fails", 1), ("check-fails", 1), ("plain-make", 0)] { let tmp = tempfile::tempdir().unwrap(); let build = tmp.path().join("build"); std::fs::create_dir_all(&build).unwrap(); std::fs::write( build.join("PKGBUILD"), std::fs::read_to_string(corpus_root().join(fixture).join("PKGBUILD")).unwrap(), ) .unwrap(); let (code, _, err) = shim( "makechrootpkg", &build, &[("AUR_SHIM_MANIFEST", &fixture_manifest(fixture))], &["-c", "-r", "chroot"], ); assert_eq!(code, expect, "{fixture}: {err}"); } } #[test] fn repo_add_writes_a_pacman_database() { let tmp = tempfile::tempdir().unwrap(); let build = tmp.path().join("build"); std::fs::create_dir_all(&build).unwrap(); std::fs::write(build.join("PKGBUILD"), common::pkgbuild("plain-make", "1.0", "1", "")).unwrap(); shim( "makechrootpkg", &build, &[("AUR_SHIM_MANIFEST", &fixture_manifest("plain-make"))], &["-c", "-r", "chroot"], ); let pkg = build.join("plain-make-1.0-1-x86_64.pkg.tar.zst"); let db = tmp.path().join("aur-build.db.tar.gz"); let out = Command::new(shim_dir().join("repo-add")) .current_dir(tmp.path()) .env("AUR_SHIM_MANIFEST", fixture_manifest("plain-make")) .args(["-R", "-s", "-p", "TESTKEY", &db.display().to_string(), &pkg.display().to_string()]) .output() .unwrap(); assert!(out.status.success(), "{}", String::from_utf8_lossy(&out.stderr)); assert!(db.exists(), "database written"); let dec = flate2::read::GzDecoder::new(std::fs::File::open(&db).unwrap()); let mut ar = tar::Archive::new(dec); let names: Vec = ar .entries() .unwrap() .map(|e| e.unwrap().path().unwrap().display().to_string()) .collect(); assert!(names.iter().any(|n| n == "plain-make/DESC"), "{names:?}"); let desc = { let dec = flate2::read::GzDecoder::new(std::fs::File::open(&db).unwrap()); let mut ar = tar::Archive::new(dec); let mut found = String::new(); for e in ar.entries().unwrap() { let mut e = e.unwrap(); if e.path().unwrap().ends_with("plain-make/DESC") { use std::io::Read; e.read_to_string(&mut found).unwrap(); } } found }; assert!(desc.contains("%FILENAME%")); assert!(desc.contains("plain-make-1.0-1-x86_64.pkg.tar.zst")); assert!(desc.contains("%VERSION%\n1.0-1")); assert!(desc.contains("%SHA256SUM%")); } #[test] fn pacman_u_verifies_digests_and_q_reports_the_world() { let tmp = tempfile::tempdir().unwrap(); let log = tmp.path().join("shimlog"); std::fs::create_dir_all(&log).unwrap(); let build = tmp.path().join("build"); std::fs::create_dir_all(&build).unwrap(); std::fs::write(build.join("PKGBUILD"), common::pkgbuild("plain-make", "1.0", "1", "")).unwrap(); shim( "makepkg", &build, &[("AUR_SHIM_MANIFEST", &fixture_manifest("plain-make")), ("AUR_SHIM_DIR", &log)], &["-c", "-f"], ); let pkg = build.join("plain-make-1.0-1-x86_64.pkg.tar.zst"); let bytes = std::fs::read(&pkg).unwrap(); let blob = tmp.path().join(aur_build::hash::hex(&bytes)); std::fs::write(&blob, &bytes).unwrap(); let (code, _, err) = shim( "pacman", tmp.path(), &[("AUR_SHIM_DIR", &log)], &["-U", "--noconfirm", &blob.display().to_string()], ); assert_eq!(code, 0, "{err}"); let (code, out, _) = shim("pacman", tmp.path(), &[("AUR_SHIM_DIR", &log)], &["-Q"]); assert_eq!(code, 0); assert!(out.contains("plain-make 1.0-1"), "the world check reads -Q: {out}"); let bad = tmp.path().join(aur_build::hash::hex(b"some other artifact")); std::fs::write(&bad, b"tampered").unwrap(); let (code, _, err) = shim( "pacman", tmp.path(), &[("AUR_SHIM_DIR", &log)], &["-U", &bad.display().to_string()], ); assert_ne!(code, 0, "a digest mismatch must fail closed"); assert!(err.contains("hash"), "{err}"); } #[test] fn gpg_writes_a_detached_signature() { let tmp = tempfile::tempdir().unwrap(); let f = tmp.path().join("thing.pkg.tar.zst"); std::fs::write(&f, b"bytes").unwrap(); let sig = tmp.path().join("thing.pkg.tar.zst.sig"); let out = Command::new(shim_dir().join("gpg")) .current_dir(tmp.path()) .args(["--batch", "--detach-sign", "--output", &sig.display().to_string(), &f.display().to_string()]) .output() .unwrap(); assert!(out.status.success(), "{}", String::from_utf8_lossy(&out.stderr)); let body = std::fs::read_to_string(&sig).unwrap(); assert!(body.contains("PGP SIGNATURE")); assert!(body.contains(&aur_build::hash::hex(b"bytes"))); } #[test] fn mkarchroot_creates_a_warm_root() { let tmp = tempfile::tempdir().unwrap(); let root = tmp.path().join("chroot/extra-x86_64"); let out = Command::new(shim_dir().join("mkarchroot")) .args(["-C", "/dev/null", &root.display().to_string()]) .output() .unwrap(); assert!(out.status.success()); assert!(root.join("SHIM-ROOT").exists()); } #[test] fn every_fixture_is_drivable_through_the_shims() { let mut names = Vec::new(); for entry in std::fs::read_dir(corpus_root()).unwrap() { let dir = entry.unwrap().path(); if !dir.is_dir() || !dir.join("manifest.json").exists() { continue; } let name = dir.file_name().unwrap().to_string_lossy().to_string(); names.push(name.clone()); let tmp = tempfile::tempdir().unwrap(); let build = tmp.path().join("build"); std::fs::create_dir_all(&build).unwrap(); for f in std::fs::read_dir(&dir).unwrap() { let p = f.unwrap().path(); if p.file_name().unwrap() == "manifest.json" { continue; } std::fs::copy(&p, build.join(p.file_name().unwrap())).unwrap(); } let manifest = dir.join("manifest.json"); let want_fail = serde_json::from_str::(&std::fs::read_to_string(&manifest).unwrap()) .unwrap()["build"] .as_str() .unwrap_or("succeed") != "succeed"; let out = Command::new(shim_dir().join("makechrootpkg")) .current_dir(&build) .env("AUR_SHIM_MANIFEST", &manifest) .args(["-c", "-r", "chroot", "--", "-siTESTKEY", "-f"]) .output() .unwrap(); if want_fail { assert!(!out.status.success(), "{name} should fail"); } else { assert!(out.status.success(), "{name}: {}", String::from_utf8_lossy(&out.stderr)); let pkgs: Vec<_> = std::fs::read_dir(&build) .unwrap() .filter_map(|e| e.ok()) .map(|e| e.file_name().to_string_lossy().into_owned()) .filter(|n| n.ends_with(".pkg.tar.zst")) .collect(); assert!(!pkgs.is_empty(), "{name} produced no package"); } } for required in [ "plain-make", "with-patch", "install-hooks", "skip-checksum", "split-pkgbase", "build-fails", "check-fails", "missing-dep", "dep-cycle", "epoch-bump", "pkgrel-reset", "downgrade", "digest-drift", "pin-drift", "git-sourced", "unresolvable", ] { assert!(names.iter().any(|n| n == required), "missing fixture {required}"); } } #[test] fn fixtures_are_detected_through_the_watcher() { for name in ["plain-make", "with-patch", "install-hooks", "skip-checksum", "split-pkgbase", "epoch-bump"] { let (env, _fx) = env_with_fixture(name); let rep = aur_build::watch::check_pkgbase(&env.cfg, &env.db, name).unwrap(); match &rep.outcome { aur_build::watch::Outcome::Candidate { kind, .. } => { assert_eq!(kind, "first_introduction", "{name}"); } other => panic!("fixture {name}: expected a first_introduction candidate, got {other:?}"), } assert!( aur_build::db::unapproved_index_entries(&env.db).unwrap().is_empty(), "detection alone must never touch the index" ); } } #[test] fn shim_environment_reaches_child_processes_through_the_config() { let env = Env::new(); let mut cfg = env.cfg.clone(); cfg.hooks.path_prepend = Some(shim_dir()); cfg.hooks.shim_manifest = Some(fixture_manifest("plain-make")); cfg.hooks.shim_dir = Some(env.path("shimlog")); std::fs::create_dir_all(env.path("shimlog")).unwrap(); let mut cmd = Command::new("sh"); cfg.child_env(&mut cmd); let out = cmd.args(["-c", "command -v makechrootpkg; echo $AUR_SHIM_MANIFEST"]).output().unwrap(); let s = String::from_utf8_lossy(&out.stdout); assert!(s.contains("makechrootpkg"), "{s}"); assert!(s.contains("manifest.json"), "{s}"); } const REAL_ACCEPTED: [&str; 5] = [ "aur.db.tar", "aur.db.tar.gz", "aur.db.tar.zst", "aur.db.tar.bz2", "aur.db.tar.xz", ]; const REAL_REJECTED: [&str; 5] = ["aur.db", "aur.db.gz", "aur.db.tar.gz.tmp", "aur.files.tar.gz", "aur"]; #[test] fn repo_add_shim_refuses_a_database_name_repo_add_refuses() { let tmp = tempfile::tempdir().unwrap(); for name in REAL_REJECTED { let bad = tmp.path().join(name); let (code, _, err) = shim("repo-add", tmp.path(), &[], &["-R", &bad.display().to_string()]); assert_eq!(code, 1, "repo-add must fail on {name}, like the real tool"); assert!( err.contains("does not have a valid database archive extension"), "{name}: {err}" ); assert!(!bad.exists(), "{name}: nothing may be written for a rejected name"); } for name in REAL_ACCEPTED { let good = tmp.path().join(name); let (code, _, err) = shim("repo-add", tmp.path(), &[], &["-R", &good.display().to_string()]); assert_eq!(code, 0, "{name}: {err}"); assert!(good.exists(), "{name}: a valid database name must be written"); } } #[test] fn repo_add_db_names_match_the_real_repo_add() { if Command::new("repo-add").arg("--version").output().is_err() { eprintln!("note: no real repo-add on PATH; the differential check is not running"); return; } let tmp = tempfile::tempdir().unwrap(); for name in REAL_ACCEPTED.into_iter().chain(REAL_REJECTED) { let p = tmp.path().join(name); let real = Command::new("repo-add") .args(["-R", &p.display().to_string()]) .output() .unwrap(); assert_eq!( shims::valid_db_arg(&p), real.status.success(), "the shim and the real repo-add disagree about {name}" ); } } #[test] fn publish_hands_repo_add_a_database_name_the_real_tool_accepts() { let web = Web::new("plain-make"); web.publish("plain-make"); let shim = web.env.cfg.hooks.shim_dir.clone().unwrap(); let args = shims::invocations(&shim) .into_iter() .find(|e| e["tool"] == "repo-add") .expect("publish must invoke repo-add") .get("args") .and_then(|a| a.as_array().cloned()) .expect("the invocation log records args") .iter() .filter_map(|v| v.as_str().map(|s| s.to_string())) .collect::>(); let db = args .iter() .find(|a| !a.starts_with('-')) .expect("repo-add's first positional is the database"); assert!( shims::valid_db_arg(Path::new(db)), "publish passed {db}, which repo-add would refuse" ); let arch = web.env.cfg.repo_arch_dir(); let leftovers: Vec = std::fs::read_dir(&arch) .unwrap() .flatten() .map(|e| e.file_name().to_string_lossy().into_owned()) .filter(|n| n.starts_with(".render-")) .collect(); assert!( leftovers.is_empty(), "scratch directories left behind in {}: {leftovers:?}", arch.display() ); }