summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--Cargo.toml10
-rw-r--r--src/lib.rs9
-rw-r--r--src/pipeline/publish.rs18
-rw-r--r--src/shims/mod.rs103
-rw-r--r--tests/production_surface.rs74
-rw-r--r--tests/shims.rs7
6 files changed, 141 insertions, 80 deletions
diff --git a/Cargo.toml b/Cargo.toml
index 79217e8..ee76db5 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -17,25 +17,35 @@ path = "src/main.rs"
# Toolchain shims (DESIGN.md §16a). These are real binaries with the exact names
# the pipeline shells out to; the test harness prepends their directory to PATH
# so the whole step machine runs without root, network or an Arch chroot.
+#
+# They are test-only (§6 M-4) and declare the `test-support` feature, so a
+# production `cargo build --release` neither builds them nor links the shim code
+# into the library.
# ---------------------------------------------------------------------------
[[bin]]
name = "mkarchroot"
path = "src/bin/mkarchroot.rs"
+required-features = ["test-support"]
[[bin]]
name = "makechrootpkg"
path = "src/bin/makechrootpkg.rs"
+required-features = ["test-support"]
[[bin]]
name = "makepkg"
path = "src/bin/makepkg.rs"
+required-features = ["test-support"]
[[bin]]
name = "repo-add"
path = "src/bin/repo-add.rs"
+required-features = ["test-support"]
[[bin]]
name = "gpg"
path = "src/bin/gpg.rs"
+required-features = ["test-support"]
[[bin]]
name = "pacman"
path = "src/bin/pacman.rs"
+required-features = ["test-support"]
[dependencies]
anyhow = "1"
diff --git a/src/lib.rs b/src/lib.rs
index 445ae2f..f0ba1a1 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -18,6 +18,15 @@ pub mod pkginfo;
pub mod pkgver;
pub mod recipe;
pub mod review;
+/// The §16a fake toolchain: stand-ins for `makepkg`, `mkarchroot`, `gpg`,
+/// `repo-add` and `pacman` so the whole step machine runs without root, network
+/// or an Arch chroot.
+///
+/// Test-only, and compiled out of a production build (§6 M-4): the module exists
+/// only under the `test-support` feature, which nothing but this crate's own
+/// dev-dependency enables, and the shim binaries declare the same feature. A
+/// binary an operator runs cannot fake a build tool it cannot see.
+#[cfg(feature = "test-support")]
pub mod shims;
pub mod time;
pub mod watch;
diff --git a/src/pipeline/publish.rs b/src/pipeline/publish.rs
index ab0cb87..40048c3 100644
--- a/src/pipeline/publish.rs
+++ b/src/pipeline/publish.rs
@@ -321,15 +321,21 @@ fn dedupe_by_name(paths: Vec<PathBuf>) -> Vec<PathBuf> {
out
}
+/// The package name a built artifact declares, read from its own `.PKGINFO`.
+///
+/// This is the real parser in `crate::pkginfo`, not the shim harness's: the
+/// name that goes into the index must come from the same read of the bytes that
+/// the pipeline signs, and a second parser in the test scaffolding is one
+/// refactor away from disagreeing about what a package calls itself (§6 M-4).
fn package_name(path: &Path) -> Option<String> {
let bytes = std::fs::read(path).ok()?;
- let info = crate::shims::read_pkginfo(&bytes).ok()?;
- for line in info.lines() {
- if let Some(v) = line.strip_prefix("pkgname =") {
- return Some(v.trim().to_string());
- }
+ let info = crate::pkginfo::pkginfo_text(&bytes).ok()?;
+ let name = crate::pkginfo::parse_pkginfo(&info).name;
+ if name.is_empty() {
+ None
+ } else {
+ Some(name)
}
- None
}
/// Re-materialize the served index from the DB. Run at startup: if a crash left
diff --git a/src/shims/mod.rs b/src/shims/mod.rs
index 5cf9ac7..a34cf7d 100644
--- a/src/shims/mod.rs
+++ b/src/shims/mod.rs
@@ -520,8 +520,8 @@ pub fn repo_add(args: &[String], _m: &Manifest) -> Result<i32, String> {
let mut ar = tar::Builder::new(Vec::new());
for pkg in &pkgs {
let bytes = std::fs::read(pkg).map_err(|e| format!("repo-add: {}: {e}", pkg.display()))?;
- let pkginfo = read_pkginfo(&bytes)?;
- let (name, version, arch, extra) = parse_pkginfo(&pkginfo);
+ let pkginfo = crate::pkginfo::pkginfo_text(&bytes)?;
+ let (name, version, arch, extra) = pkginfo_fields(&bytes)?;
let mut entry = String::new();
entry.push_str(&format!("%FILENAME%\n{}\n\n", pkg.file_name().unwrap().to_string_lossy()));
entry.push_str(&format!("%NAME%\n{name}\n\n"));
@@ -561,77 +561,36 @@ pub fn repo_add(args: &[String], _m: &Manifest) -> Result<i32, String> {
Ok(0)
}
-/// Pull `.PKGINFO` out of a `.pkg.tar.zst`. The real parsing lives in
-/// `crate::pkginfo`, which the pipeline also uses — the shim and the production
-/// path must agree about what a package says about itself, or a test that passes
-/// here would not mean anything.
-pub fn read_pkginfo(pkg_bytes: &[u8]) -> Result<String, String> {
- crate::pkginfo::pkginfo_text(pkg_bytes)
-}
-
-pub fn read_buildinfo(pkg_bytes: &[u8]) -> Result<String, String> {
- crate::pkginfo::buildinfo_text(pkg_bytes)
-}
-
-fn parse_pkginfo(s: &str) -> (String, String, String, String) {
- let mut name = String::new();
- let mut version = String::new();
- let mut arch = String::new();
- let mut groups = Vec::new();
- let mut deps = Vec::new();
- let mut optdeps = Vec::new();
- let mut provides = Vec::new();
- let mut conflicts = Vec::new();
- let mut replaces = Vec::new();
- let mut license = Vec::new();
- let mut desc = String::new();
- let mut url = String::new();
- for line in s.lines() {
- let (k, v) = match line.split_once('=') {
- Some((k, v)) => (k.trim(), v.trim()),
- None => continue,
- };
- match k {
- "pkgname" => name = v.to_string(),
- "pkgver" => version = v.to_string(),
- "pkgarch" => arch = v.to_string(),
- "pkgdesc" => desc = v.to_string(),
- "url" => url = v.to_string(),
- "license" => license.push(v.to_string()),
- "depend" => deps.push(v.to_string()),
- "optdepend" => optdeps.push(v.to_string()),
- "checkdepend" => {}
- "makedepend" => {}
- "provides" => provides.push(v.to_string()),
- "conflict" => conflicts.push(v.to_string()),
- "replace" => replaces.push(v.to_string()),
- "group" => groups.push(v.to_string()),
- _ => {}
- }
- }
+/// Pull `.PKGINFO` out of a `.pkg.tar.zst` and render the `DESC` fields a
+/// pacman database carries beyond the fixed ones.
+///
+/// Both halves come from `crate::pkginfo`, the parser the production pipeline
+/// uses (§6 M-4). The shim used to carry its own copy of the field parser, and
+/// two parsers that can drift is a supply-chain-shaped foot-gun in a system
+/// whose whole job is provenance: a test that passed because the shim read a
+/// package one way would not have meant the pipeline read it that way.
+fn pkginfo_fields(pkg_bytes: &[u8]) -> Result<(String, String, String, String), String> {
+ let text = crate::pkginfo::pkginfo_text(pkg_bytes)?;
+ let info = crate::pkginfo::parse_pkginfo(&text);
let mut extra = String::new();
- let mut push = |tag: &str, vals: &Vec<String>| {
- if !vals.is_empty() {
- extra.push_str(&format!("{tag}\n{}\n\n", vals.join("\n")));
- }
- };
- push("%DESC%", &desc_vec(&desc));
- push("%URL%", &url_vec(&url));
- push("%LICENSE%", &license);
- push("%DEPENDS%", &deps);
- push("%OPTDEPENDS%", &optdeps);
- push("%PROVIDES%", &provides);
- push("%CONFLICTS%", &conflicts);
- push("%REPLACES%", &replaces);
- push("%GROUPS%", &groups);
- (name, version, arch, extra)
-}
-
-fn desc_vec(s: &str) -> Vec<String> {
- if s.is_empty() { vec![] } else { vec![s.to_string()] }
-}
-fn url_vec(s: &str) -> Vec<String> {
- if s.is_empty() { vec![] } else { vec![s.to_string()] }
+ {
+ let mut push = |tag: &str, vals: &[String]| {
+ if !vals.is_empty() {
+ extra.push_str(&format!("{tag}\n{}\n\n", vals.join("\n")));
+ }
+ };
+ let one = |s: &str| -> Vec<String> { if s.is_empty() { vec![] } else { vec![s.to_string()] } };
+ push("%DESC%", &one(&info.desc));
+ push("%URL%", &one(&info.url));
+ push("%LICENSE%", &info.license);
+ push("%DEPENDS%", &info.depends);
+ push("%OPTDEPENDS%", &info.optdepends);
+ push("%PROVIDES%", &info.provides);
+ push("%CONFLICTS%", &info.conflicts);
+ push("%REPLACES%", &info.replaces);
+ push("%GROUPS%", &info.groups);
+ }
+ Ok((info.name, info.version, info.arch, extra))
}
/// `gpg --detach-sign --output <sig> <file>`: write a deterministic stand-in
diff --git a/tests/production_surface.rs b/tests/production_surface.rs
new file mode 100644
index 0000000..1fa855f
--- /dev/null
+++ b/tests/production_surface.rs
@@ -0,0 +1,74 @@
+//! The production surface cannot see the test scaffolding (§6 M-4, M-9).
+//!
+//! The shim harness is load-bearing: the whole step machine is tested by driving
+//! the real binaries with fakes for `makepkg`, `mkarchroot`, `gpg` and friends.
+//! That is fine. What is not fine is production code reaching into it — the
+//! publish path once read a package's name through the shim module's copy of the
+//! `.PKGINFO` parser, which meant the bytes we signed and the bytes the tests
+//! believed were parsed by two pieces of code that could drift apart.
+//!
+//! The compile-time half of this rule is real: `shims` is compiled out unless the
+//! `test-support` feature is on, and only this crate's own dev-dependency turns
+//! it on. This test covers the half the compiler cannot: a module in `src/`
+//! naming the scaffolding directly.
+
+use std::path::{Path, PathBuf};
+
+fn rust_files(dir: &Path, out: &mut Vec<PathBuf>) {
+ for entry in std::fs::read_dir(dir).expect("read_dir") {
+ let path = entry.expect("entry").path();
+ if path.is_dir() {
+ rust_files(&path, out);
+ } else if path.extension().is_some_and(|e| e == "rs") {
+ out.push(path);
+ }
+ }
+}
+
+#[test]
+fn no_production_module_references_the_shim_harness() {
+ let root = Path::new(env!("CARGO_MANIFEST_DIR")).join("src");
+ let mut files = Vec::new();
+ rust_files(&root, &mut files);
+
+ let mut offenders: Vec<String> = Vec::new();
+ for file in files {
+ let rel = file.strip_prefix(&root).unwrap_or(&file).to_string_lossy().replace('\\', "/");
+ // The harness itself, and the shim binaries that are its front ends, are
+ // allowed to know about it.
+ if rel.starts_with("shims") || rel.starts_with("bin/") {
+ continue;
+ }
+ let text = std::fs::read_to_string(&file).expect("read source");
+ for (no, line) in text.lines().enumerate() {
+ let t = line.trim_start();
+ if t.starts_with("//") {
+ continue;
+ }
+ if line.contains("shims::") {
+ offenders.push(format!("{rel}:{}: {t}", no + 1));
+ }
+ }
+ }
+ assert!(
+ offenders.is_empty(),
+ "production code must not depend on the test harness (§6 M-4). Parse with `pkginfo`, not with the shim:\n{}",
+ offenders.join("\n")
+ );
+}
+
+#[test]
+fn the_shim_binaries_are_test_only() {
+ let manifest = std::fs::read_to_string(Path::new(env!("CARGO_MANIFEST_DIR")).join("Cargo.toml"))
+ .expect("read Cargo.toml");
+ for tool in ["mkarchroot", "makechrootpkg", "makepkg", "repo-add", "gpg", "pacman"] {
+ let block = manifest
+ .split("[[bin]]")
+ .find(|b| b.contains(&format!("name = \"{tool}\"")))
+ .unwrap_or_else(|| panic!("no [[bin]] for {tool}"));
+ assert!(
+ block.contains("required-features = [\"test-support\"]"),
+ "the {tool} shim is a test binary and must not be built for a production target"
+ );
+ }
+}
diff --git a/tests/shims.rs b/tests/shims.rs
index f5cab7d..757e456 100644
--- a/tests/shims.rs
+++ b/tests/shims.rs
@@ -62,11 +62,14 @@ fn makechrootpkg_emits_pkginfo_buildinfo_and_sig() {
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 = shims::read_pkginfo(&bytes).unwrap();
+ // Read the artifact with the production parser, not a shim-only one: the
+ // harness is only a faithful stand-in if it reads a package the way the
+ // pipeline will (§6 M-4).
+ 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 = shims::read_buildinfo(&bytes).unwrap();
+ let buildinfo = aur_build::pkginfo::buildinfo_text(&bytes).unwrap();
assert!(buildinfo.contains("format = 2"));
assert!(buildinfo.contains("installed ="), "BUILDINFO carries the chroot world (§10.1)");
assert!(buildinfo.contains(" glibc 2.39-1 x86_64 -"));