diff options
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/common/mod.rs | 48 | ||||
| -rw-r--r-- | tests/detect.rs | 428 |
2 files changed, 476 insertions, 0 deletions
diff --git a/tests/common/mod.rs b/tests/common/mod.rs index 4cf78ec..b598b80 100644 --- a/tests/common/mod.rs +++ b/tests/common/mod.rs @@ -73,6 +73,54 @@ impl Env { .unwrap() } + /// Stand in for the build+publish path: turn an approved candidate into a + /// published release and move the accepted pointer. The real path lives in + /// `pipeline`; detection tests need an accepted base without a build. + pub fn accept_as_release(&self, candidate_id: i64) -> i64 { + let at = "2026-01-01T00:00:00Z"; + self.db + .writer + .tx(move |tx| { + tx.execute( + "INSERT INTO approval (candidate_id, base_release_id, decided_at, decision, + risk_level, diff_digest, identity_hash) + SELECT id, base_release_id, ?2, 'approved', risk_level, diff_digest, identity_hash + FROM candidate WHERE id=?1", + rusqlite::params![candidate_id, at], + )?; + let approval_id = tx.last_insert_rowid(); + tx.execute( + "INSERT INTO release (pkgbase_id, candidate_id, approval_id, aur_commit, recipe_hash, + upstream_pins_json, source_digests_json, epoch, pkgver, pkgrel, + state, published_at, created_at) + SELECT c.pkgbase_id, c.id, ?2, s.aur_commit, s.recipe_hash, c.upstream_pins_json, + c.source_digests_json, c.epoch, c.pkgver, c.pkgrel, + 'published', ?3, ?3 + FROM candidate c JOIN snapshot s ON s.id = c.snapshot_id WHERE c.id=?1", + rusqlite::params![candidate_id, approval_id, at], + )?; + let release_id = tx.last_insert_rowid(); + tx.execute( + "UPDATE candidate SET state='approved', resolved_at=?1 WHERE id=?2", + rusqlite::params![at, candidate_id], + )?; + tx.execute( + "INSERT INTO package (repo_id, release_id, name, arch, version, state) + SELECT (SELECT id FROM repo ORDER BY id LIMIT 1), ?1, + b.name, 'x86_64', c.pkgver || '-' || c.pkgrel, 'in_index' + FROM candidate c JOIN pkgbase b ON b.id = c.pkgbase_id WHERE c.id=?2", + rusqlite::params![release_id, candidate_id], + )?; + tx.execute( + "UPDATE pkgbase SET current_accepted_release_id=?1 + WHERE id=(SELECT pkgbase_id FROM candidate WHERE id=?2)", + rusqlite::params![release_id, candidate_id], + )?; + Ok(release_id) + }) + .unwrap() + } + pub fn query_i64(&self, sql: &str, params: &[P]) -> Option<i64> { let sql = sql.to_string(); let params: Vec<P> = params.to_vec(); diff --git a/tests/detect.rs b/tests/detect.rs new file mode 100644 index 0000000..939dce1 --- /dev/null +++ b/tests/detect.rs @@ -0,0 +1,428 @@ +//! Detection tests (§5.1, §5.1a, §5.3, §7.1) against a local `file://` "AUR". +//! +//! The fixtures are real git repos so the mirror, the pin and `recipe_hash` are +//! exercised for real; only the network is replaced. + +mod common; + +use common::{git_add_commit, make_git_repo, params, Env, P}; + +use aur_build::db::{self, model::*, ReleaseRow}; +use aur_build::review::{approve, candidates}; +use aur_build::watch::detect::{check_pkgbase, Outcome}; +use aur_build::watch::mirror::{sync_mirror, Sync}; + +/// Path of the fake AUR repo for `name`, matching `Env::subscribe`'s URL. +fn aur(env: &Env, name: &str) -> std::path::PathBuf { + env.path(&format!("aur/{name}.git")) +} + +fn seed(env: &Env, name: &str, pkgver: &str, pkgrel: &str, extra: &str) -> String { + make_git_repo(&aur(env, name), &[("PKGBUILD", &common::pkgbuild(name, pkgver, pkgrel, extra))], "initial") +} + +fn pending(env: &Env, name: &str) -> Option<CandidateRow> { + let pb = db::get_pkgbase(&env.db, name).unwrap().unwrap(); + candidates::open_reviews_for(&env.db, pb.id).unwrap().into_iter().next() +} + +fn accepted(env: &Env, name: &str) -> Option<ReleaseRow> { + db::accepted_release(&env.db, name).unwrap() +} + +#[test] +fn first_detection_opens_a_first_introduction_with_no_base() { + let env = Env::new(); + env.subscribe("foo"); + let commit = seed(&env, "foo", "1.0", "1", ""); + let rep = check_pkgbase(&env.cfg, &env.db, "foo").unwrap(); + match &rep.outcome { + Outcome::Candidate { kind, .. } => assert_eq!(kind, candidate_kind::FIRST_INTRODUCTION), + other => panic!("expected a candidate, got {other:?}"), + } + assert_eq!(rep.head, commit); + let c = pending(&env, "foo").expect("candidate row"); + assert_eq!(c.kind, candidate_kind::FIRST_INTRODUCTION); + assert_eq!(c.base_release_id, None, "I2 has no referent for a first introduction"); + assert_eq!(c.version(), "1.0-1"); + // The pin is recorded on the snapshot and it is the commit we cloned. + let snap = db::snapshot_by_id(&env.db, c.snapshot_id).unwrap().unwrap(); + assert_eq!(snap.aur_commit, commit); + assert_eq!(snap.recipe_hash.len(), 64); +} + +#[test] +fn cheap_poll_does_not_fetch_when_the_tip_is_unchanged() { + let env = Env::new(); + env.subscribe("foo"); + seed(&env, "foo", "1.0", "1", ""); + let url = format!("file://{}", aur(&env, "foo").display()); + let first = sync_mirror(&env.cfg, &env.db, "foo", &url).unwrap(); + assert!(matches!(first, Sync::Cloned { .. })); + let again = sync_mirror(&env.cfg, &env.db, "foo", &url).unwrap(); + match again { + Sync::Unchanged { head } => assert_eq!(head, git_head(&aur(&env, "foo"))), + other => panic!("expected Unchanged, got {other:?}"), + } + // A new commit makes it fetch. + git_add_commit(&aur(&env, "foo"), &[("PKGBUILD", &common::pkgbuild("foo", "1.1", "1", ""))], "bump"); + let moved = sync_mirror(&env.cfg, &env.db, "foo", &url).unwrap(); + assert!(matches!(moved, Sync::Fetched { .. }), "SHA moved -> fetch"); +} + +fn git_head(p: &std::path::Path) -> String { + aur_build::git::head(p).unwrap() +} + +/// §3: `recipe_hash` covers the *entire* tree, because a shipped patch is part +/// of the recipe and is a classic silent-change vector. +#[test] +fn recipe_hash_covers_shipped_patches() { + let env = Env::new(); + env.subscribe("foo"); + seed(&env, "foo", "1.0", "1", "source=(fix.patch)\nsha256sums=('SKIP')"); + std::fs::write(aur(&env, "foo").join("fix.patch"), "--- a\n+++ b\n@@\n-old\n+new\n").unwrap(); + git_add_commit(&aur(&env, "foo"), &[("fix.patch", "--- a\n+++ b\n@@\n-old\n+new\n")], "patch"); + check_pkgbase(&env.cfg, &env.db, "foo").unwrap(); + let c1 = pending(&env, "foo").unwrap(); + let h1 = db::snapshot_by_id(&env.db, c1.snapshot_id).unwrap().unwrap().recipe_hash; + env.accept_as_release(c1.id); + + // Same PKGBUILD, different patch bytes. + git_add_commit(&aur(&env, "foo"), &[("fix.patch", "--- a\n+++ b\n@@\n-old\n+EVIL\n")], "patch2"); + let rep = check_pkgbase(&env.cfg, &env.db, "foo").unwrap(); + assert!( + matches!(rep.outcome, Outcome::Notice { ref kind, .. } if kind == notice_kind::RECIPE_DRIFT), + "recipe drift with an unchanged version is a notice, never a candidate (I11): {:?}", + rep.outcome + ); + let pb = db::get_pkgbase(&env.db, "foo").unwrap().unwrap(); + let snap = db::snapshot_by_commit(&env.db, pb.id, &rep.head).unwrap().unwrap(); + assert_ne!(snap.recipe_hash, h1, "patch bytes must change recipe_hash"); +} + +#[test] +fn skip_sources_are_stream_hashed_into_the_cache() { + let env = Env::new(); + env.subscribe("foo"); + let payload = env.path("payload.txt"); + std::fs::write(&payload, b"the bytes that become the package").unwrap(); + let expected = aur_build::hash::hex(b"the bytes that become the package"); + seed( + &env, + "foo", + "1.0", + "1", + &format!("source=(file://{})\nsha256sums=('SKIP')\n", payload.display()), + ); + check_pkgbase(&env.cfg, &env.db, "foo").unwrap(); + let c = pending(&env, "foo").unwrap(); + let digests = c.digests(); + assert_eq!(digests.len(), 1); + assert_eq!(digests[0].digest, expected, "we hash SKIP sources ourselves (§5.3)"); + assert!(digests[0].skip_declared); + assert_eq!(digests[0].kind, "static"); + // The bytes landed in the content-addressed cache and are recorded. + let cached = env.query_i64( + "SELECT count(*) FROM source_cache WHERE url LIKE '%payload.txt' AND digest=?1", + ¶ms(vec![expected.clone()]), + ); + assert_eq!(cached, Some(1)); + let path = env.cfg.source_cache_path(&expected); + assert!(path.exists(), "cached file is reused by the build"); + // A declared checksum costs no bandwidth: nothing was downloaded for it. +} + +#[test] +fn declared_checksums_are_trusted_without_downloading() { + let env = Env::new(); + env.subscribe("foo"); + let payload = env.path("declared.txt"); + std::fs::write(&payload, b"verified bytes").unwrap(); + let d = aur_build::hash::hex(b"verified bytes"); + seed( + &env, + "foo", + "1.0", + "1", + &format!("source=(file://{})\nsha256sums=('{d}')\n", payload.display()), + ); + check_pkgbase(&env.cfg, &env.db, "foo").unwrap(); + let c = pending(&env, "foo").unwrap(); + assert_eq!(c.digests()[0].digest, d); + assert!(!c.digests()[0].skip_declared); + assert_eq!( + env.query_i64("SELECT count(*) FROM source_cache", &[]), + Some(0), + "declared sums are enforced by makepkg, not re-downloaded (§5.3)" + ); +} + +/// I2, the headline rule: diffs are always against the last *accepted* version. +#[test] +fn i2_diff_base_is_the_last_accepted_version_however_many_detections_arrive() { + let env = Env::new(); + env.subscribe("foo"); + seed(&env, "foo", "1.0", "1", ""); + check_pkgbase(&env.cfg, &env.db, "foo").unwrap(); + let c1 = pending(&env, "foo").unwrap(); + let rel1 = env.accept_as_release(c1.id); + assert_eq!(accepted(&env, "foo").unwrap().id, rel1); + + // v2 arrives and is detected but nobody looks at it. + git_add_commit(&aur(&env, "foo"), &[("PKGBUILD", &common::pkgbuild("foo", "2.0", "1", ""))], "v2"); + check_pkgbase(&env.cfg, &env.db, "foo").unwrap(); + let c2 = pending(&env, "foo").unwrap(); + assert_eq!(c2.base_release_id, Some(rel1), "base is the accepted release"); + assert_eq!(c2.version(), "2.0-1"); + + // v3 and v4 arrive while nobody is looking: they collapse into the same + // review, and the base is *still* v1. The human sees v1 -> v4. + for v in ["3.0", "4.0"] { + git_add_commit(&aur(&env, "foo"), &[("PKGBUILD", &common::pkgbuild("foo", v, "1", ""))], v); + check_pkgbase(&env.cfg, &env.db, "foo").unwrap(); + } + let c = pending(&env, "foo").unwrap(); + assert_eq!(c.id, c2.id, "collapsed in place: no new row (I7)"); + assert_eq!(c.revision, 2, "revision counts the collapsed detections (v3, v4)"); + assert_eq!(c.base_release_id, Some(rel1), "base never moves: the diff is v1 -> v4 (I2)"); + assert_eq!(c.version(), "4.0-1"); + assert_eq!( + env.query_i64("SELECT count(*) FROM candidate WHERE state='pending_review'", &[]), + Some(1) + ); +} + +/// §7.1: `superseded` exists only when a human actually saw the old diff. +#[test] +fn a_viewed_review_is_frozen_and_a_fresh_one_opened() { + let env = Env::new(); + env.subscribe("foo"); + seed(&env, "foo", "1.0", "1", ""); + check_pkgbase(&env.cfg, &env.db, "foo").unwrap(); + let c1 = pending(&env, "foo").unwrap(); + let rel1 = env.accept_as_release(c1.id); + + git_add_commit(&aur(&env, "foo"), &[("PKGBUILD", &common::pkgbuild("foo", "2.0", "1", ""))], "v2"); + check_pkgbase(&env.cfg, &env.db, "foo").unwrap(); + let c2 = pending(&env, "foo").unwrap(); + candidates::mark_viewed(&env.db, c2.id).unwrap(); + + git_add_commit(&aur(&env, "foo"), &[("PKGBUILD", &common::pkgbuild("foo", "3.0", "1", ""))], "v3"); + let rep = check_pkgbase(&env.cfg, &env.db, "foo").unwrap(); + match rep.outcome { + Outcome::Superseded { old, new } => { + assert_eq!(old, c2.id); + assert_ne!(old, new); + } + other => panic!("expected Superseded, got {other:?}"), + } + let c3 = pending(&env, "foo").unwrap(); + assert_eq!(c3.base_release_id, Some(rel1), "the fresh review keeps the same base"); + assert_eq!(c3.version(), "3.0-1"); + let old_state: String = env + .db + .read(|c| { + Ok(c.query_row( + "SELECT state FROM candidate WHERE id=?1", + rusqlite::params![c2.id], + |r| r.get(0), + )?) + }) + .unwrap(); + assert_eq!(old_state, candidate_state::SUPERSEDED); +} + +#[test] +fn a_rejected_identity_is_not_re_raised() { + let env = Env::new(); + env.subscribe("foo"); + seed(&env, "foo", "1.0", "1", ""); + check_pkgbase(&env.cfg, &env.db, "foo").unwrap(); + let c = pending(&env, "foo").unwrap(); + approve::reject(&env.db, "tester", c.id, "not this one").unwrap(); + // Re-running detection must not nag (§7.1). + let rep = check_pkgbase(&env.cfg, &env.db, "foo").unwrap(); + assert!( + matches!(rep.outcome, Outcome::PreviouslyRejected | Outcome::NoChange), + "{:?}", + rep.outcome + ); + assert!(pending(&env, "foo").is_none()); + // A *newer* version is still detected: rejection does not block (§7.1). + git_add_commit(&aur(&env, "foo"), &[("PKGBUILD", &common::pkgbuild("foo", "2.0", "1", ""))], "v2"); + check_pkgbase(&env.cfg, &env.db, "foo").unwrap(); + assert!(pending(&env, "foo").is_some()); +} + +/// I11: a change that is not publishable never silently disappears. +#[test] +fn recipe_drift_under_a_static_version_is_a_notice_not_a_candidate() { + let env = Env::new(); + env.subscribe("foo"); + seed(&env, "foo", "1.0", "1", ""); + check_pkgbase(&env.cfg, &env.db, "foo").unwrap(); + let c = pending(&env, "foo").unwrap(); + env.accept_as_release(c.id); + + // Maintainer edits the PKGBUILD but forgets to bump pkgrel. + git_add_commit( + &aur(&env, "foo"), + &[("PKGBUILD", &common::pkgbuild("foo", "1.0", "1", "# a comment only"))], + "typo fix, no bump", + ); + let rep = check_pkgbase(&env.cfg, &env.db, "foo").unwrap(); + match rep.outcome { + Outcome::Notice { kind, .. } => assert_eq!(kind, notice_kind::RECIPE_DRIFT), + other => panic!("expected a notice, got {other:?}"), + } + assert!(pending(&env, "foo").is_none(), "nothing publishable, so no review"); + let notices = db::list_notices(&env.db, false).unwrap(); + assert_eq!(notices.len(), 1); + assert_eq!(notices[0].kind, notice_kind::RECIPE_DRIFT); + assert_eq!(notices[0].severity, "warning"); +} + +/// §5.3: the single most valuable detection — same version, swapped bytes. +#[test] +fn digest_drift_with_an_unchanged_version_is_an_integrity_alert() { + let env = Env::new(); + env.subscribe("foo"); + let payload = env.path("payload.txt"); + std::fs::write(&payload, b"good release tarball").unwrap(); + seed( + &env, + "foo", + "1.0", + "1", + &format!("source=(file://{})\nsha256sums=('SKIP')\n", payload.display()), + ); + check_pkgbase(&env.cfg, &env.db, "foo").unwrap(); + let c = pending(&env, "foo").unwrap(); + env.accept_as_release(c.id); + + // The CDN now serves something else and the version did not move. + std::fs::write(&payload, b"EVIL swapped artifact").unwrap(); + let rep = check_pkgbase(&env.cfg, &env.db, "foo").unwrap(); + match rep.outcome { + Outcome::Notice { kind, .. } => assert_eq!(kind, notice_kind::INTEGRITY_ALERT), + other => panic!("expected an integrity alert, got {other:?}"), + } + assert!(pending(&env, "foo").is_none(), "approving it would not let us publish anything"); + let notices = db::list_notices(&env.db, false).unwrap(); + assert_eq!(notices[0].severity, "high"); + assert!(notices[0].detail_json.contains("no longer serves")); + // The index keeps serving the artifact built from the old bytes (I1). + assert_eq!(db::unapproved_index_entries(&env.db).unwrap(), Vec::<String>::new()); +} + +#[test] +fn a_deleted_upstream_marks_the_subscription_and_alerts() { + let env = Env::new(); + env.subscribe("foo"); + seed(&env, "foo", "1.0", "1", ""); + check_pkgbase(&env.cfg, &env.db, "foo").unwrap(); + std::fs::remove_dir_all(aur(&env, "foo")).unwrap(); + let rep = check_pkgbase(&env.cfg, &env.db, "foo").unwrap(); + assert!(matches!(rep.outcome, Outcome::Gone), "{:?}", rep.outcome); + let sub = db::get_subscription(&env.db, "foo").unwrap().unwrap(); + assert_eq!(sub.state, subscription_state::ORPHANED); + assert!(db::list_notices(&env.db, false) + .unwrap() + .iter() + .any(|n| n.kind == notice_kind::DELETED)); +} + +#[test] +fn detection_is_idempotent_and_records_no_duplicate_candidate() { + let env = Env::new(); + env.subscribe("foo"); + seed(&env, "foo", "1.0", "1", ""); + check_pkgbase(&env.cfg, &env.db, "foo").unwrap(); + for _ in 0..3 { + let rep = check_pkgbase(&env.cfg, &env.db, "foo").unwrap(); + assert!(matches!(rep.outcome, Outcome::NoChange), "{:?}", rep.outcome); + } + assert_eq!( + env.query_i64("SELECT count(*) FROM candidate", &[]), + Some(1), + "I3: two byte-identical inputs never produce two candidates" + ); + assert_eq!(env.query_i64("SELECT count(*) FROM snapshot", &[]), Some(1)); +} + +#[test] +fn srcinfo_is_preferred_over_the_pkgbuild_when_present() { + let env = Env::new(); + env.subscribe("foo"); + // .SRCINFO disagrees with the PKGBUILD, which is what makepkg reports. + make_git_repo( + &aur(&env, "foo"), + &[ + ("PKGBUILD", &common::pkgbuild("foo", "9.9", "9", "")), + ( + ".SRCINFO", + "pkgbase = foo\n\tpkgver = 1.0\n\tpkgrel = 1\n\tarch = x86_64\npkgname = foo\n\tarch = x86_64\n", + ), + ], + "initial", + ); + check_pkgbase(&env.cfg, &env.db, "foo").unwrap(); + let c = pending(&env, "foo").unwrap(); + assert_eq!(c.version(), "1.0-1", ".SRCINFO is authoritative"); +} + +#[test] +fn split_pkgbase_records_every_emitted_package() { + let env = Env::new(); + env.subscribe("foo"); + make_git_repo( + &aur(&env, "foo"), + &[( + "PKGBUILD", + "pkgbase=foo\npkgver=1.0\npkgrel=1\npkgdesc=split\narch=(x86_64)\npkgname=(foo foo-lua)\nsource=()\nsha256sums=()\n", + )], + "initial", + ); + check_pkgbase(&env.cfg, &env.db, "foo").unwrap(); + let c = pending(&env, "foo").unwrap(); + let snap = db::snapshot_by_id(&env.db, c.snapshot_id).unwrap().unwrap(); + let r: Recipe = serde_json::from_str(&snap.srcinfo_json).unwrap(); + assert_eq!(r.pkgnames, vec!["foo".to_string(), "foo-lua".to_string()]); +} + +#[test] +fn epoch_and_pkgrel_are_part_of_the_identity() { + let env = Env::new(); + env.subscribe("foo"); + seed(&env, "foo", "1.0", "1", ""); + check_pkgbase(&env.cfg, &env.db, "foo").unwrap(); + let c = pending(&env, "foo").unwrap(); + env.accept_as_release(c.id); + // pkgrel reset: a major signal, and a new identity. + git_add_commit(&aur(&env, "foo"), &[("PKGBUILD", &common::pkgbuild("foo", "1.0", "7", ""))], "rel"); + check_pkgbase(&env.cfg, &env.db, "foo").unwrap(); + let c2 = pending(&env, "foo").expect("pkgrel bump is publishable, so it is a candidate"); + assert_eq!(c2.version(), "1.0-7"); + assert_ne!(c2.identity_hash, c.identity_hash); +} + +#[test] +fn notices_are_not_duplicated_by_repeated_polling() { + let env = Env::new(); + env.subscribe("foo"); + seed(&env, "foo", "1.0", "1", ""); + check_pkgbase(&env.cfg, &env.db, "foo").unwrap(); + let c = pending(&env, "foo").unwrap(); + env.accept_as_release(c.id); + git_add_commit(&aur(&env, "foo"), &[("PKGBUILD", &common::pkgbuild("foo", "1.0", "1", "# x"))], "drift"); + for _ in 0..4 { + check_pkgbase(&env.cfg, &env.db, "foo").unwrap(); + } + assert_eq!( + env.query_i64("SELECT count(*) FROM notice", &[]), + Some(1), + "the same drift polled daily is one alert, not four" + ); + let _ = P::I(0); +} |
