diff options
| -rw-r--r-- | src/config.rs | 130 | ||||
| -rw-r--r-- | src/pipeline/publish.rs | 7 | ||||
| -rw-r--r-- | tests/common/mod.rs | 30 | ||||
| -rw-r--r-- | tests/crash.rs | 8 | ||||
| -rw-r--r-- | tests/dispatch.rs | 4 | ||||
| -rw-r--r-- | tests/invariants.rs | 6 | ||||
| -rw-r--r-- | tests/pipeline.rs | 4 | ||||
| -rw-r--r-- | tests/production_surface.rs | 62 | ||||
| -rw-r--r-- | tests/shims.rs | 6 | ||||
| -rw-r--r-- | tests/vcs.rs | 2 |
10 files changed, 184 insertions, 75 deletions
diff --git a/src/config.rs b/src/config.rs index 9226622..3708639 100644 --- a/src/config.rs +++ b/src/config.rs @@ -50,27 +50,11 @@ pub struct Config { pub gpg_bin: String, /// Unprivileged user that runs PKGBUILD code (§14). Empty = current user. pub build_user: String, - /// Directory prepended to PATH for child processes. The test harness puts - /// the toolchain shims here (§16a). - pub path_prepend: Option<PathBuf>, - /// Fixture manifest the shims should obey, exported as `AUR_SHIM_MANIFEST`. - pub shim_manifest: Option<PathBuf>, - /// Where shims record their invocations, exported as `AUR_SHIM_DIR`. - pub shim_dir: Option<PathBuf>, - /// Test hook: abort publish at a named point so I4 (atomic publish) can be - /// exercised for real. Never set in production. - pub publish_fault: Option<String>, - /// Test hook: the named shim must fail, e.g. `makechrootpkg`. - pub shim_fail: Option<String>, - /// Test hook: the named shim SIGKILLs its parent, i.e. the process running - /// the pipeline, mid-step. This is how a real crash is reproduced for I5 — - /// not a caught error, an uncatchable death. Never set in production. - pub shim_kill_parent: Option<String>, - /// Make the build shim plant a symlink to this path in its srcdir, so a test - /// can act out the I9 attack: an untrusted PKGBUILD smuggling bytes in from - /// outside its own staging dir. - #[serde(default)] - pub shim_plant_link: Option<String>, + /// Test-only hooks (§16a). Not part of the production configuration at all + /// unless the crate is built with `test-support` — see [`TestHooks`]. + #[cfg(feature = "test-support")] + #[serde(default, skip_serializing_if = "TestHooks::is_empty")] + pub hooks: TestHooks, /// How many builds may run at once (§8.2 backpressure). Default 1, which /// also means the chroot pool only needs one warm root. pub max_concurrent_builds: usize, @@ -86,6 +70,57 @@ pub struct Config { pub dispatch_tick_secs: u64, } +/// The test harness's handles on a real pipeline (§16a), grouped into one +/// submodule so the production configuration says what it is. +/// +/// Each of these exists because a *fake* pipeline would test something else: +/// I4 (atomic publish) needs a process that really dies between the tmp write +/// and the rename; I5 needs a step that really dies mid-child; I9 needs a +/// PKGBUILD that really reaches outside its staging dir. The alternative was a +/// mock that asserts the mock's own behaviour. +/// +/// They are compiled out entirely unless the crate is built with the +/// `test-support` feature (the same feature that gates the shims, §6 M-4), so a +/// production binary has no fault-injection branch in its publish path and no +/// shim protocol in its child environment. The test suite enables the feature +/// through a dev-dependency on this crate, and nothing else does. +#[derive(Debug, Clone, Default, Serialize, Deserialize)] +#[serde(default)] +pub struct TestHooks { + /// Directory prepended to PATH for child processes: the toolchain shims. + pub path_prepend: Option<PathBuf>, + /// Fixture manifest the shims should obey, exported as `AUR_SHIM_MANIFEST`. + pub shim_manifest: Option<PathBuf>, + /// Where shims record their invocations, exported as `AUR_SHIM_DIR`. + pub shim_dir: Option<PathBuf>, + /// Abort publish at a named point so I4 (atomic publish) can be exercised + /// for real: `before-rename` or `after-rename`. + pub publish_fault: Option<String>, + /// The named shim must fail, e.g. `makechrootpkg`. + pub shim_fail: Option<String>, + /// The named shim SIGKILLs its parent — the process running the pipeline — + /// mid-step. This is how a real crash is reproduced for I5: not a caught + /// error, an uncatchable death. + pub shim_kill_parent: Option<String>, + /// Make the build shim plant a symlink to this path in its srcdir, so a test + /// can act out the I9 attack: an untrusted PKGBUILD smuggling bytes in from + /// outside its own staging dir. + pub shim_plant_link: Option<String>, +} + +impl TestHooks { + /// True when nothing is hooked — the state every real process runs in. + pub fn is_empty(&self) -> bool { + self.path_prepend.is_none() + && self.shim_manifest.is_none() + && self.shim_dir.is_none() + && self.publish_fault.is_none() + && self.shim_fail.is_none() + && self.shim_kill_parent.is_none() + && self.shim_plant_link.is_none() + } +} + impl Default for Config { fn default() -> Self { Self { @@ -105,13 +140,8 @@ impl Default for Config { repo_add_bin: "repo-add".into(), gpg_bin: "gpg".into(), build_user: String::new(), - path_prepend: None, - shim_manifest: None, - shim_dir: None, - publish_fault: None, - shim_fail: None, - shim_kill_parent: None, - shim_plant_link: None, + #[cfg(feature = "test-support")] + hooks: TestHooks::default(), max_concurrent_builds: 1, lease_secs: 90, min_free_disk_mb: 1024, @@ -148,25 +178,33 @@ impl Config { /// Environment for a child process: PATH with the shims in front, plus the /// shim protocol variables (§16a). + #[cfg_attr(not(feature = "test-support"), allow(unused_variables))] pub fn child_env(&self, cmd: &mut std::process::Command) { - if let Some(dir) = &self.path_prepend { - let path = std::env::var("PATH").unwrap_or_default(); - cmd.env("PATH", format!("{}:{path}", dir.display())); - } - if let Some(m) = &self.shim_manifest { - cmd.env("AUR_SHIM_MANIFEST", m); - } - if let Some(d) = &self.shim_dir { - cmd.env("AUR_SHIM_DIR", d); - } - if let Some(f) = &self.shim_fail { - cmd.env("AUR_SHIM_FAIL", f); - } - if let Some(k) = &self.shim_kill_parent { - cmd.env("AUR_SHIM_KILL_PARENT", k); - } - if let Some(l) = &self.shim_plant_link { - cmd.env("AUR_SHIM_PLANT_LINK", l); + // Without `test-support` there is no shim protocol to export and no + // PATH to rewrite: the child inherits the real environment, which is + // what a real build needs (§16a). + #[cfg(feature = "test-support")] + { + let h = &self.hooks; + if let Some(dir) = &h.path_prepend { + let path = std::env::var("PATH").unwrap_or_default(); + cmd.env("PATH", format!("{}:{path}", dir.display())); + } + if let Some(m) = &h.shim_manifest { + cmd.env("AUR_SHIM_MANIFEST", m); + } + if let Some(d) = &h.shim_dir { + cmd.env("AUR_SHIM_DIR", d); + } + if let Some(f) = &h.shim_fail { + cmd.env("AUR_SHIM_FAIL", f); + } + if let Some(k) = &h.shim_kill_parent { + cmd.env("AUR_SHIM_KILL_PARENT", k); + } + if let Some(l) = &h.shim_plant_link { + cmd.env("AUR_SHIM_PLANT_LINK", l); + } } } diff --git a/src/pipeline/publish.rs b/src/pipeline/publish.rs index 4f21e72..d70ba8d 100644 --- a/src/pipeline/publish.rs +++ b/src/pipeline/publish.rs @@ -117,7 +117,9 @@ pub fn publish(ctx: &Ctx) -> Result<i64> { // Fault injection so I4 can be tested for real: die between the tmp write // and the rename, and the old index must still be what consumers get. - if ctx.cfg.publish_fault.as_deref() == Some("before-rename") { + // Compiled out of a production build (§6 M-9). + #[cfg(feature = "test-support")] + if ctx.cfg.hooks.publish_fault.as_deref() == Some("before-rename") { return Err(Error::other("injected fault: before-rename")); } @@ -148,7 +150,8 @@ pub fn publish(ctx: &Ctx) -> Result<i64> { "unblocked_builds": unblocked, }), )?; - if ctx.cfg.publish_fault.as_deref() == Some("after-rename") { + #[cfg(feature = "test-support")] + if ctx.cfg.hooks.publish_fault.as_deref() == Some("after-rename") { return Err(Error::other("injected fault: after-rename")); } Ok(release_id) diff --git a/tests/common/mod.rs b/tests/common/mod.rs index 58c6dae..426f44d 100644 --- a/tests/common/mod.rs +++ b/tests/common/mod.rs @@ -5,7 +5,7 @@ use std::path::{Path, PathBuf}; -use aur_build::config::Config; +use aur_build::config::{Config, TestHooks}; use aur_build::db::{self, Db}; pub struct Env { @@ -127,14 +127,14 @@ impl Env { /// Make the next publish abort at a named point (I4 / I5 tests). pub fn set_publish_fault(&mut self, at: &str) { - self.cfg.publish_fault = Some(at.to_string()); + self.cfg.hooks.publish_fault = Some(at.to_string()); } pub fn clear_publish_fault(&mut self) { - self.cfg.publish_fault = None; + self.cfg.hooks.publish_fault = None; } /// Make the named shim fail (the "build failed" path, §8.4). pub fn set_shim_fail(&mut self, tool: &str) { - self.cfg.shim_fail = Some(tool.to_string()); + self.cfg.hooks.shim_fail = Some(tool.to_string()); } pub fn query_i64(&self, sql: &str, params: &[P]) -> Option<i64> { @@ -236,12 +236,15 @@ pub fn env_with_fixture(name: &str) -> (Env, Fixture) { let cfg = Config { root: tmp.path().to_path_buf(), signing_key_id: "TESTKEY".into(), - path_prepend: Some(shim_dir()), - shim_manifest: Some(fx.manifest.clone()), - shim_dir: Some(tmp.path().join("shimlog")), + hooks: TestHooks { + path_prepend: Some(shim_dir()), + shim_manifest: Some(fx.manifest.clone()), + shim_dir: Some(tmp.path().join("shimlog")), + ..TestHooks::default() + }, ..Config::default() }; - std::fs::create_dir_all(cfg.shim_dir.as_ref().unwrap()).unwrap(); + std::fs::create_dir_all(cfg.hooks.shim_dir.as_ref().unwrap()).unwrap(); cfg.ensure_dirs().unwrap(); let env = Env::with_cfg(tmp, cfg); env.subscribe(name); @@ -258,12 +261,15 @@ pub fn env_with_two_fixtures(a: &str, b: &str) -> (Env, Fixture, Fixture) { let cfg = Config { root: tmp.path().to_path_buf(), signing_key_id: "TESTKEY".into(), - path_prepend: Some(shim_dir()), - shim_manifest: Some(fb.manifest.clone()), - shim_dir: Some(tmp.path().join("shimlog")), + hooks: TestHooks { + path_prepend: Some(shim_dir()), + shim_manifest: Some(fb.manifest.clone()), + shim_dir: Some(tmp.path().join("shimlog")), + ..TestHooks::default() + }, ..Config::default() }; - std::fs::create_dir_all(cfg.shim_dir.as_ref().unwrap()).unwrap(); + std::fs::create_dir_all(cfg.hooks.shim_dir.as_ref().unwrap()).unwrap(); cfg.ensure_dirs().unwrap(); let env = Env::with_cfg(tmp, cfg); env.subscribe(a); diff --git a/tests/crash.rs b/tests/crash.rs index 66e6043..ca58a04 100644 --- a/tests/crash.rs +++ b/tests/crash.rs @@ -46,7 +46,7 @@ fn a_process_killed_mid_step_resumes_from_its_checkpoints_with_no_duplicate_work let (mut env, _fx) = env_with_fixture("plain-make"); let id = queued(&env, "plain-make"); // The child will be murdered the moment it invokes makechrootpkg. - env.cfg.shim_kill_parent = Some("makechrootpkg".into()); + env.cfg.hooks.shim_kill_parent = Some("makechrootpkg".into()); env.cfg.save().unwrap(); let out = run_cli(&env, &["run", &id]); @@ -89,12 +89,12 @@ fn a_process_killed_mid_step_resumes_from_its_checkpoints_with_no_duplicate_work // Resume. The completed steps are not repeated; only the interrupted one and // what follows run. - env.cfg.shim_kill_parent = None; + env.cfg.hooks.shim_kill_parent = None; env.cfg.save().unwrap(); let out = run_cli(&env, &["run", &id]); assert!(out.status.success(), "resume failed: {}", String::from_utf8_lossy(&out.stderr)); - let shim = env.cfg.shim_dir.clone().unwrap(); + let shim = env.cfg.hooks.shim_dir.clone().unwrap(); assert_eq!( aur_build::shims::called_with(&shim, "mkarchroot", ""), 1, @@ -126,7 +126,7 @@ fn a_cancelled_build_stops_at_a_step_boundary_and_publishes_nothing() { assert!(matches!(out, pipeline::Outcome::Cancelled), "{out:?}"); assert_eq!(builds::get(&env.db, &id).unwrap().unwrap().state, BuildState::Cancelled); // Nothing ran, so nothing to undo. - let shim = env.cfg.shim_dir.clone().unwrap(); + let shim = env.cfg.hooks.shim_dir.clone().unwrap(); assert_eq!(aur_build::shims::called_with(&shim, "repo-add", ""), 0); assert!(db::accepted_release(&env.db, "plain-make").unwrap().is_none()); // A cancelled build is not a failure and is not retried. diff --git a/tests/dispatch.rs b/tests/dispatch.rs index d0d4bd7..e62b351 100644 --- a/tests/dispatch.rs +++ b/tests/dispatch.rs @@ -202,7 +202,7 @@ fn a_reaped_build_resumes_at_the_next_incomplete_step_with_no_duplicate_work() { pipeline::run(&env.cfg, &env.db, &id).unwrap(), pipeline::Outcome::Failed { .. } )); - let builds_before = aur_build::shims::called_with(&env.cfg.shim_dir.clone().unwrap(), "makechrootpkg", ""); + let builds_before = aur_build::shims::called_with(&env.cfg.hooks.shim_dir.clone().unwrap(), "makechrootpkg", ""); // The worker "died" at publish: the row is leased with a lease nobody will // extend. A fresh dispatcher reaps it and a worker finishes from the @@ -223,7 +223,7 @@ fn a_reaped_build_resumes_at_the_next_incomplete_step_with_no_duplicate_work() { assert_eq!(state, BuildState::Succeeded, "the resumed build did not finish"); assert_eq!( - aur_build::shims::called_with(&env.cfg.shim_dir.clone().unwrap(), "makechrootpkg", ""), + aur_build::shims::called_with(&env.cfg.hooks.shim_dir.clone().unwrap(), "makechrootpkg", ""), builds_before, "I5: resuming must not rebuild" ); diff --git a/tests/invariants.rs b/tests/invariants.rs index b7bfc0d..8026a79 100644 --- a/tests/invariants.rs +++ b/tests/invariants.rs @@ -158,7 +158,7 @@ fn i5_resume_after_a_crash_repeats_no_completed_step() { pipeline::run(&env.cfg, &env.db, &id).unwrap(), pipeline::Outcome::Failed { .. } )); - let shim = env.cfg.shim_dir.clone().unwrap(); + let shim = env.cfg.hooks.shim_dir.clone().unwrap(); let builds_before = aur_build::shims::called_with(&shim, "makechrootpkg", ""); assert_eq!(builds_before, 2, "one build per attempted publish"); @@ -422,10 +422,10 @@ fn i9_a_build_cannot_smuggle_bytes_in_from_outside_its_own_staging_dir() { // Something a build must never get to publish: our own database file. let secret = env.cfg.db_path(); let store_before = dir_listing(&env.cfg.store_dir()); - env.cfg.shim_plant_link = Some(secret.display().to_string()); + env.cfg.hooks.shim_plant_link = Some(secret.display().to_string()); let out = pipeline::run(&env.cfg, &env.db, &id); - env.cfg.shim_plant_link = None; + env.cfg.hooks.shim_plant_link = None; let (step, err) = match out { Ok(pipeline::Outcome::Failed { step, error, .. }) => (step, error), other => panic!("a build that smuggled in a symlink must fail closed, got {other:?}"), diff --git a/tests/pipeline.rs b/tests/pipeline.rs index a8eb1d9..0da1612 100644 --- a/tests/pipeline.rs +++ b/tests/pipeline.rs @@ -223,7 +223,7 @@ fn i5_a_restarted_build_resumes_at_the_next_incomplete_step() { pipeline::Outcome::Failed { .. } )); let builds_after_crash = - aur_build::shims::called_with(&env.cfg.shim_dir.clone().unwrap(), "makechrootpkg", ""); + aur_build::shims::called_with(&env.cfg.hooks.shim_dir.clone().unwrap(), "makechrootpkg", ""); assert_eq!(builds_after_crash, 2, "one real build before the crash"); // "Restart": same build row, no fault. Steps 0-8 are checkpointed as @@ -234,7 +234,7 @@ fn i5_a_restarted_build_resumes_at_the_next_incomplete_step() { other => panic!("{other:?}"), } assert_eq!( - aur_build::shims::called_with(&env.cfg.shim_dir.clone().unwrap(), "makechrootpkg", ""), + aur_build::shims::called_with(&env.cfg.hooks.shim_dir.clone().unwrap(), "makechrootpkg", ""), builds_after_crash, "I5: resuming must not rebuild" ); diff --git a/tests/production_surface.rs b/tests/production_surface.rs index 5ade95b..26f50ea 100644 --- a/tests/production_surface.rs +++ b/tests/production_surface.rs @@ -14,6 +14,10 @@ use std::path::{Path, PathBuf}; +fn read(rel: &str) -> String { + std::fs::read_to_string(Path::new(env!("CARGO_MANIFEST_DIR")).join(rel)).expect("read source") +} + 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(); @@ -97,3 +101,61 @@ fn the_shim_binaries_are_test_only() { ); } } + +#[test] +fn the_test_hooks_are_a_gated_submodule_not_config_fields() { + // §6 M-9: fault injection in the publish hot path, shim environment + // variables and clock pinning were part of the production API surface — + // `Config` advertised `publish_fault` next to `signing_key_id`, and the + // branch that reads it sat in the middle of every real publish. + let cfg_src = read("src/config.rs"); + for field in [ + "pub path_prepend", + "pub shim_manifest", + "pub shim_dir", + "pub publish_fault", + "pub shim_fail", + "pub shim_kill_parent", + "pub shim_plant_link", + ] { + // Each appears exactly once, inside `TestHooks`, never on `Config`. + let hits = cfg_src.matches(field).count(); + assert_eq!(hits, 1, "{field} must be declared once, on TestHooks — not on Config"); + } + assert!(cfg_src.contains("pub struct TestHooks"), "the hooks must live in one named submodule"); + assert!( + cfg_src.contains("#[cfg(feature = \"test-support\")]"), + "the hooks must be compiled out without the test-support feature" + ); +} + +#[test] +fn the_publish_hot_path_has_no_ungated_test_branch() { + // The two fault-injection points in `publish()` are the ones that ran on + // every real publish. Each must sit behind the feature gate. + let src = read("src/pipeline/publish.rs"); + let lines: Vec<&str> = src.lines().collect(); + let mut gated = 0; + for (i, line) in lines.iter().enumerate() { + if line.contains("hooks.publish_fault") { + let window = lines[i.saturating_sub(3)..i].join("\n"); + assert!( + window.contains("#[cfg(feature = \"test-support\")]"), + "fault injection at publish.rs:{} is compiled into production builds", + i + 1 + ); + gated += 1; + } + } + assert_eq!(gated, 2, "expected the two known fault points, found {gated}"); +} + +#[test] +fn a_default_config_has_no_hooks_attached() { + // The state every real process runs in: nothing hooked, and nothing written + // to config.json about hooks. + let cfg = aur_build::config::Config::default(); + assert!(cfg.hooks.is_empty()); + let json = serde_json::to_string(&cfg).unwrap(); + assert!(!json.contains("publish_fault"), "hooks leaked into the serialized config: {json}"); +} diff --git a/tests/shims.rs b/tests/shims.rs index 757e456..3422500 100644 --- a/tests/shims.rs +++ b/tests/shims.rs @@ -297,9 +297,9 @@ fn fixtures_are_detected_through_the_watcher() { fn shim_environment_reaches_child_processes_through_the_config() { let env = Env::new(); let mut cfg = env.cfg.clone(); - cfg.path_prepend = Some(shim_dir()); - cfg.shim_manifest = Some(fixture_manifest("plain-make")); - cfg.shim_dir = Some(env.path("shimlog")); + 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(); // Prove the config is what puts the shims on PATH, not the test process env. diff --git a/tests/vcs.rs b/tests/vcs.rs index df0d756..b1edd51 100644 --- a/tests/vcs.rs +++ b/tests/vcs.rs @@ -24,7 +24,7 @@ fn stage_b_rows(env: &Env) -> i64 { } fn makepkg_calls(env: &Env) -> usize { - aur_build::shims::called_with(&env.cfg.shim_dir.clone().unwrap(), "makepkg", "--vercheck") + aur_build::shims::called_with(&env.cfg.hooks.shim_dir.clone().unwrap(), "makepkg", "--vercheck") } #[test] |
