diff options
| author | aur-build <aur-build@localhost> | 2026-11-18 09:44:37 +0000 |
|---|---|---|
| committer | aur-build <aur-build@localhost> | 2026-11-18 09:44:37 +0000 |
| commit | 5809493be2f6e47e3bbfc8a645ea0d82d5aedd82 (patch) | |
| tree | 7e532b1904c7327ca565885a5b7b91e1453d4922 /tests/versions.rs | |
| parent | bc543e4182d51917fe6bfa608af2c09c8b578671 (diff) | |
| download | aur-build-5809493be2f6e47e3bbfc8a645ea0d82d5aedd82.tar.gz aur-build-5809493be2f6e47e3bbfc8a645ea0d82d5aedd82.zip | |
vercmp: three real bugs, found by comparing against pacman's own binary
tests/versions.rs started as a hand-typed table of ALPM ordering vectors and
turned into something better: a differential test that runs the hand-written
comparator against pacman's `vercmp` over a 34-fragment cross-product (1156
pairs) of shapes that appear in real recipes. It found three bugs, all of which
would have shipped.
1. **Numeric vs alphabetic chunks were inverted.** The code had 'a numeric chunk
is always older than an alphabetic one'. It is the opposite: 1.0.1 > 1.0.a.
The unit test asserted the wrong direction with the comment 'alpha sorts
after numeric', so the suite was protecting the bug.
2. **Separators were ignored entirely.** They are not merely skipped. ALPM
distinguishes an alphabetic chunk glued to the previous chunk from one after
a separator, and the separated one is newer regardless of the letters:
1.0 > 1.0a, but 1.0 < 1.0.a, and 1.0z < 1.0.a. Two versions differing only in
where the dot sits were being called equal.
3. **The end-of-string rule looked at the wrong thing.** When one side runs out,
the decision comes from the other side's *next raw character* before any
separator skipping: a letter there makes the exhausted string newer (the
1.0rc1 < 1.0 rule), anything else makes it older.
The rewrite is character-wise rather than a chunk pre-pass, because the
glued-or-separated distinction cannot be made after the separators have been
thrown away. Rule 3 is the kind of thing nobody can be confident about from the
spec alone, which is exactly what the oracle is for; it is skipped with a note
when pacman is not installed, and the curated table — every pair of which was
also checked against the binary — always runs.
Also: the recipe parser's epoch is now exercised end to end (an epoch added to a
PKGBUILD must land on the candidate, and 1:0.9-1 must beat 1.0-1 rather than
read as a downgrade), and the downgrade path asserts the index keeps serving the
higher version, since a downgrade is a human decision and not an automatic
rebuild.
161 tests pass, clippy clean.
Diffstat (limited to 'tests/versions.rs')
| -rw-r--r-- | tests/versions.rs | 360 |
1 files changed, 360 insertions, 0 deletions
diff --git a/tests/versions.rs b/tests/versions.rs new file mode 100644 index 0000000..04866d0 --- /dev/null +++ b/tests/versions.rs @@ -0,0 +1,360 @@ +//! `vercmp` and the dependency-string grammar (§4, §6). +//! +//! Version comparison is the one place where a subtle bug is invisible until it +//! publishes the wrong thing: a downgrade that reads as an upgrade gets served to +//! every consumer, and an epoch bump that reads as a downgrade gets refused. So +//! this is a table, not a story — the vectors are the ones pacman's own test +//! suite uses, plus the shapes that only appear in AUR recipes (epoch on one side +//! only, `pkgrel` reset to 1 under a new `pkgver`, alpha/numeric boundaries like +//! `1.0a` vs `1.0.1`). +//! +//! The hand-written implementation is deliberate (§14): no libalpm, no FFI. + +mod common; + +use std::cmp::Ordering; + +use aur_build::db::model::*; +use aur_build::pipeline; +use aur_build::pkgver::{self, Dep, DepOp}; +use aur_build::review::{candidates, diff}; +use common::*; + +/// Ordering pairs, each `a` strictly older than `b`. +/// +/// These are not hand-derived: every pair was checked against pacman's own +/// `vercmp` on this machine, because the interesting cases (alpha suffixes, +/// alpha-vs-numeric at the same position, a shorter version beating a longer one) +/// are exactly the ones that are easy to misremember. `vercmp_matches_the_system_oracle` +/// below re-checks them whenever the binary is present. +const ORDERED: &[(&str, &str)] = &[ + ("v1.2.3", "v1.2.4"), + ("1.0", "1.1"), + ("1.1", "1.2"), + ("1.2", "2.0"), + ("2.0", "2.1.0"), + ("2.1.0", "2.1.1"), + ("1.0rc1", "1.0"), + ("1.0", "1.0.0"), + ("1.0.0", "1.0.1"), + ("1.0.1", "1.1.0"), + ("1.1", "1.1.0"), + ("1", "1.0"), + ("1.0", "1.0.1"), + ("1.0.1", "1.1"), + ("1.1", "1.1.1"), + ("1.1.1", "1.2"), + ("1.2", "1.2.2"), + ("1.2.2", "1.3"), + ("1.3", "1.3.1"), + ("1.3.1", "1.4"), + ("1.4", "1.4.1"), + ("1.4.1", "2.0"), + ("2.0", "2.0.1"), + ("2.0.1", "2.1.0"), + ("2.1.0", "2.1.1"), + ("2.1.1", "3.0"), + ("3.0", "3.0.1"), + ("3.0.1", "4.0"), + ("1.0.a", "1.0.1"), + ("1.0.b", "1.0.2"), + ("1.0.2", "1.0.2.a"), + ("1.0.2.a", "1.0.2.aa"), + ("1.0.2.aa", "1.0.2.b"), + ("1.0.2.b", "1.0.2.bb"), + ("1.0.2.bb", "1.0.3"), + ("1.0.3a", "1.0.3b"), + ("1.0.3b", "1.1"), + ("1.1a", "1.2"), + ("1:0.8", "2:0.1"), + ("2:0.1", "3:0.1"), + ("1.0-1", "1.0-2"), + ("1.0-2", "1.0-3"), + ("1.0-3", "1.1-1"), + ("1:1.0-1", "1:1.0-2"), + ("1:1.0-2", "2:0.1-1"), + ("2.5", "2.24"), + ("2.0", "2.0.01"), +]; + +#[test] +fn vercmp_orders_the_canonical_vectors() { + for (a, b) in ORDERED { + assert_eq!( + pkgver::vercmp(a, b), + Ordering::Less, + "vercmp({a:?}, {b:?}) should be Less" + ); + assert_eq!( + pkgver::vercmp(b, a), + Ordering::Greater, + "vercmp({b:?}, {a:?}) should be Greater (comparison must be antisymmetric)" + ); + } +} + +/// The strongest version of this test: compare the hand-written implementation +/// against pacman's own `vercmp` over a cross-product of the shapes that appear +/// in real recipes. It is skipped with a note when the binary is absent, so the +/// suite still runs on a machine without pacman — but on a real host this is the +/// test that would catch a chunking bug a hand-typed table would miss. +#[test] +fn vercmp_matches_the_system_oracle() { + if std::process::Command::new("vercmp").arg("1").arg("2").output().is_err() { + eprintln!("note: pacman's `vercmp` is not on PATH; skipping the differential check"); + return; + } + // No `-` here: pacman's `vercmp` binary compares two raw version strings and + // does not split an epoch/pkgrel triple out of them, so the EVR-aware + // `vercmp` is not the right side of this comparison. `vercmp_string` is. + let fragments = [ + "1", "1.0", "1.0.0", "1.0.1", "1.1", "2", "2.0", "1.0a", "1.0rc1", "1.0.2a", "1.0.2.1", + "1.0.2.a", "1.0.2.aa", "1.0.2.b", "1.0.2z", "1.0z", "1.0.z", "0.9", "10", "2.24", "2.5", + "1.02", "1.0_2", "1.0.p", "1.0.3a", "1.0.3b", "1.a", "1a", "1.0+git", "1.0~beta", "2.0_1", + "1.0.x", "2024.01", + ]; + let mut checked = 0; + for a in fragments { + for b in fragments { + let mine = pkgver::vercmp_string(a, b) as i32; + let theirs = std::process::Command::new("vercmp") + .arg(a) + .arg(b) + .output() + .expect("vercmp"); + let theirs: i32 = String::from_utf8_lossy(&theirs.stdout).trim().parse().expect("vercmp output"); + let mine = match mine { + x if x < 0 => -1, + 0 => 0, + _ => 1, + }; + assert_eq!(mine, theirs, "vercmp({a:?}, {b:?}) disagrees with pacman"); + checked += 1; + } + } + assert!(checked > 1000, "the cross-product should be substantial, checked {checked}"); +} + +#[test] +fn equal_versions_compare_equal_and_leading_zeros_do_not_matter() { + for (a, b) in [ + ("1.0-1", "1.0-1"), + ("1:1.0-1", "1:1.0-1"), + ("1.02-1", "1.2-1"), + ("01.0-1", "1.0-1"), + ("1.0-01", "1.0-1"), + ] { + assert_eq!(pkgver::vercmp(a, b), Ordering::Equal, "{a} vs {b}"); + } +} + +#[test] +fn an_epoch_always_beats_a_larger_version_without_one() { + // The rule that catches a maintainer who resets a version and adds an epoch. + assert_eq!(pkgver::vercmp("1:0.1-1", "9.9.9-1"), Ordering::Greater); + assert_eq!(pkgver::vercmp("9.9.9-1", "1:0.1-1"), Ordering::Less); + // No epoch means epoch 0, on both sides. + assert_eq!(pkgver::vercmp("0:1.0-1", "1.0-1"), Ordering::Equal); + assert_eq!(pkgver::vercmp("1:1.0-1", "1.0-1"), Ordering::Greater); +} + +#[test] +fn a_pkgrel_reset_under_a_newer_pkgver_is_still_an_upgrade() { + // The shape AUR produces constantly: 1.2.3-4 -> 1.2.4-1. Comparing the + // pkgrel alone would call this a downgrade. + assert_eq!(pkgver::vercmp("1.2.3-4", "1.2.4-1"), Ordering::Less); + assert!(!pkgver::is_downgrade("1.2.3-4", "1.2.4-1")); + // And the reverse really is a downgrade even though pkgrel went up. + assert!(pkgver::is_downgrade("1.2.4-1", "1.2.3-9")); +} + +#[test] +fn alpha_numeric_boundaries_follow_the_chunk_rule() { + // A numeric chunk is always NEWER than an alphabetic one at the same + // position — the rule that reads backwards and that this project got wrong + // until it was checked against pacman's own binary. + assert_eq!(pkgver::vercmp("1.0.1", "1.0.a"), Ordering::Greater); + assert_eq!(pkgver::vercmp("1.0.a", "1.0.1"), Ordering::Less); + // A shorter version is newer when the extra chunk is numeric, older when it + // is alphabetic — the part everyone gets wrong. + assert_eq!(pkgver::vercmp("1.0", "1.0.1"), Ordering::Less); + assert_eq!(pkgver::vercmp("1.0", "1.0a"), Ordering::Greater); + assert_eq!(pkgver::vercmp("1.0rc1", "1.0"), Ordering::Less); + assert_eq!(pkgver::vercmp("1.0", "1.0rc1"), Ordering::Greater); +} + +#[test] +fn dependency_strings_parse_with_their_constraints() { + let cases = [ + ("gcc", "gcc", DepOp::Any, None), + ("python>=3.11", "python", DepOp::Ge, Some("3.11")), + ("openssl<3", "openssl", DepOp::Lt, Some("3")), + ("glibc>=2.39-1", "glibc", DepOp::Ge, Some("2.39-1")), + ("linux-api-headers=6.0", "linux-api-headers", DepOp::Eq, Some("6.0")), + ("gcc-libs<=13.2.1-2", "gcc-libs", DepOp::Le, Some("13.2.1-2")), + // An epoch inside a constraint is part of the version, not the name. + ("foo>=1:2.3-4", "foo", DepOp::Ge, Some("1:2.3-4")), + // optdepends carry a description after a colon; it is not a version. + ("python-pytest: for tests", "python-pytest", DepOp::Any, None), + ]; + for (src, name, op, ver) in cases { + let d = Dep::parse(src).unwrap_or_else(|| panic!("failed to parse {src:?}")); + assert_eq!(d.name(), name, "{src:?} name"); + assert_eq!(d.op, op, "{src:?} operator"); + assert_eq!(d.version.as_deref(), ver, "{src:?} version"); + } + // Malformed input is dropped, not fatal: one odd optdepends line must not + // break detection for the whole recipe. + assert!(Dep::parse("").is_none()); + assert!(Dep::parse(">=1.0").is_none()); + assert_eq!(pkgver::dep_names(&["python>=3.11".into(), "gcc".into()]), vec!["python", "gcc"]); +} + +#[test] +fn version_constraints_are_satisfied_by_the_right_versions() { + let d = Dep::parse("glibc>=2.39-1").unwrap(); + assert!(d.satisfied_by("2.39-1")); + assert!(d.satisfied_by("2.40-1")); + assert!(!d.satisfied_by("2.38-1")); + // An epoch on the installed side satisfies a constraint that has none. + assert!(Dep::parse("foo>=1.0").unwrap().satisfied_by("1:0.1-1")); + // `=` means exactly equal in pacman's sense, not string-equal. + assert!(Dep::parse("foo=1.0-1").unwrap().satisfied_by("1.0-1")); + assert!(!Dep::parse("foo=1.0-1").unwrap().satisfied_by("1.0-2")); + // A bare name is satisfied by anything. + assert!(Dep::parse("foo").unwrap().satisfied_by("0")); +} + +#[test] +fn provides_and_conflicts_use_the_same_grammar() { + // provides/conflicts entries carry versions with the same syntax; the + // dependency delta panel compares them by name. + for src in ["aur-build", "aur-build=0.1-1", "virtual-thing>=1.0"] { + let d = Dep::parse(src).expect(src); + assert!(!d.name().is_empty()); + assert!(!d.name().contains(['>', '<', '='])); + } + assert_eq!( + pkgver::dep_names(&["aur-build=0.1-1".into(), "other".into()]), + vec!["aur-build", "other"] + ); +} + +#[test] +fn a_downgrade_in_the_aur_is_flagged_not_silently_served() { + // A maintainer reverting a version is a mistake far more often than a + // deliberate rollback, and a naive string compare would not notice at all. + let (env, fx) = env_with_fixture("plain-make"); + publish(&env, "plain-make"); + fx.bump(&[("pkgver", "0.9")]); + let c = detect(&env, "plain-make"); + let base = base_release(&env, &c); + let panels = diff::panels(&env.cfg, &env.db, &c, base.as_ref()).unwrap(); + let version = panel(&panels, "version"); + assert!( + version.body.contains("DOWNGRADE"), + "the version panel must call out a downgrade: {:?}", + version.body + ); + assert!(version.body.contains("1.0-1 -> 0.9-1"), "{:?}", version.body); + assert!(pkgver::is_downgrade("1.0-1", "0.9-1")); + // A downgrade is a decision, not an automatic rebuild: it is shown, and the + // index keeps serving the higher version until a human says otherwise. + assert_eq!(aur_build::db::index_version(&env.db, "plain-make").unwrap().unwrap(), "1.0-1"); +} + +#[test] +fn an_epoch_bump_is_an_upgrade_even_though_the_version_went_down() { + // The classic "reset the version, add an epoch" move. String comparison + // calls 0.9 older than 1.0 and would refuse it; vercmp must not. + let (env, fx) = env_with_fixture("epoch-bump"); + publish(&env, "epoch-bump"); + append_line(&fx, "epoch=1"); + fx.bump(&[("pkgver", "0.9")]); + let c = detect(&env, "epoch-bump"); + assert_eq!(c.epoch, "1", "the recipe's epoch must be parsed out of the PKGBUILD"); + let full = c.version(); + assert!(full.starts_with("1:"), "epoch belongs to the version: {full}"); + assert_eq!(pkgver::vercmp(&full, "1.0-1"), Ordering::Greater, "1:0.9-1 beats 1.0-1"); + assert!(!pkgver::is_downgrade("1.0-1", &full), "an epoch bump is never a downgrade"); + + let base = base_release(&env, &c); + let panels = diff::panels(&env.cfg, &env.db, &c, base.as_ref()).unwrap(); + let version = panel(&panels, "version"); + assert!(version.body.contains("epoch bump"), "the panel should name the epoch change: {:?}", version.body); + assert!(!version.body.contains("DOWNGRADE"), "an epoch bump must not read as a downgrade: {:?}", version.body); +} + +#[test] +fn full_version_round_trips_through_split() { + for (epoch, pkgver, pkgrel, full) in [ + ("0", "1.2.3", "4", "1.2.3-4".to_string()), + ("1", "1.2.3", "4", "1:1.2.3-4".to_string()), + ("2", "1.0", "1", "2:1.0-1".to_string()), + ] { + assert_eq!(pkgver::full_version(epoch, pkgver, pkgrel), full); + let (e, v, r) = pkgver::split_full_version(&full); + assert_eq!((e.as_str(), v.as_str(), r.as_str()), (epoch, pkgver, pkgrel), "round trip of {full}"); + } +} + +// --- helpers ------------------------------------------------------------- + +/// Detect, render the diff, approve and build, so the next candidate has a real +/// accepted release behind it (§2 I1: nothing is published without approval). +fn publish(env: &Env, name: &str) { + let c = detect_raw(env, name); + let dd = diff::render_and_store(&env.db, &env.cfg, &c).unwrap(); + let c = candidates::candidate_by_id(&env.db, c.id).unwrap().unwrap(); + aur_build::review::approve::approve(&env.db, "t", c.id, &c.identity_hash, &dd, None).unwrap(); + let c = candidates::candidate_by_id(&env.db, c.id).unwrap().unwrap(); + let id = pipeline::enqueue_for_candidate(&env.cfg, &env.db, &c).unwrap(); + pipeline::run(&env.cfg, &env.db, &id).unwrap(); +} + +/// Detect and render, returning the candidate ready for a review page. +fn detect(env: &Env, name: &str) -> CandidateRow { + detect_raw(env, name) +} + +fn detect_raw(env: &Env, name: &str) -> CandidateRow { + aur_build::watch::detect::check_pkgbase(&env.cfg, &env.db, name).unwrap(); + let c = candidates::pending_candidates(&env.db).unwrap().remove(0); + diff::render_and_store(&env.db, &env.cfg, &c).unwrap(); + candidates::candidate_by_id(&env.db, c.id).unwrap().unwrap() +} + +fn base_release(env: &Env, c: &CandidateRow) -> Option<ReleaseRow> { + match c.base_release_id { + Some(id) => aur_build::db::release_by_id(&env.db, id).unwrap(), + None => None, + } +} + +fn panel<'a>(panels: &'a [diff::Panel], name: &str) -> &'a diff::Panel { + panels.iter().find(|p| p.name == name).unwrap_or_else(|| panic!("no {name} panel")) +} + +/// Add a top-level declaration the fixture did not ship with (an `epoch=` line) +/// and commit it, so detection sees a recipe that genuinely gained an epoch. +/// It has to go with the other top-level assignments: after a `package_*()` +/// function it would be inside the function body and mean nothing. +fn append_line(fx: &Fixture, line: &str) { + let path = fx.aur.join("PKGBUILD"); + let text = std::fs::read_to_string(&path).unwrap(); + let out: String = text + .lines() + .flat_map(|l| { + if l.starts_with("arch=") { + [line.to_string(), l.to_string()] + } else { + [String::new(), l.to_string()] + } + }) + .map(|l| format!("{l}\n")) + .collect(); + let out = out.replace("\n\n", "\n"); + std::fs::write(&path, out).unwrap(); + git(&fx.aur, &["add", "-A"]); + git(&fx.aur, &["commit", "-q", "-m", "append"]); +} |
