diff options
| author | aur-build <aur-build@localhost> | 2026-11-18 14:47:59 +0000 |
|---|---|---|
| committer | aur-build <aur-build@localhost> | 2026-11-18 14:47:59 +0000 |
| commit | 7946e9ffc48748992f051ada15bec772b16a585d (patch) | |
| tree | 96b6674efb30774647687a117759723ff3207965 /tests | |
| parent | 3370a487d6e1100623ebfd982967f1223d251607 (diff) | |
| download | aur-build-7946e9ffc48748992f051ada15bec772b16a585d.tar.gz aur-build-7946e9ffc48748992f051ada15bec772b16a585d.zip | |
hash: canonical_json is canonical now, and a serialization failure is an error
The function behind `Identity::hash` — the digest an approval echoes back —
did two things its name promised and it did not do. It did not order anything:
the doc comment conceded that callers were expected to pass "already-canonical
values", which is a convention, not a property, and a struct serializes in
whatever order its fields were declared. And on a serialization failure it
returned `"{}"`, so every value that failed to serialize hashed to one shared
digest. For a system whose whole job is provenance, "two identities that could
not be serialized would look identical to the approval echo" is the worst
possible failure to have available.
Serialization now routes through `serde_json::Value`, whose map is a
`BTreeMap`, so object keys come out sorted whatever the input's construction
order — the name describes what happens. A failure propagates: `canonical_json`
returns `Result`, `Identity::hash` returns `Result`, and the four call sites
(detect, put_world_snapshot, the world step, both enqueue paths) pass it on.
There is no path left where a digest is invented for a value that could not be
rendered.
`tests/hash.rs` pins all three properties: an unserializable value is an error
naming itself, two same-field-different-declaration-order values render
identically (and to the exact compact sorted bytes), and the identity digest
changes when any element of the tuple changes.
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/hash.rs | 83 | ||||
| -rw-r--r-- | tests/vcs.rs | 2 |
2 files changed, 84 insertions, 1 deletions
diff --git a/tests/hash.rs b/tests/hash.rs new file mode 100644 index 0000000..427f8ae --- /dev/null +++ b/tests/hash.rs @@ -0,0 +1,83 @@ +//! The digest helpers (§3, §5.1a). +//! +//! `sha256` hex is the currency of identity and provenance here, and one of +//! those digests — `Identity::hash` — is the value a reviewer's approval echoes +//! back. Everything that guards "we published what we reviewed" rests on that +//! digest being a function of the identity and of nothing else: not of the +//! order fields were declared in, and never of whether serialization happened +//! to fail. + +use std::collections::HashMap; + +use aur_build::db::model::Identity; +use aur_build::hash; + +/// A value that refuses to be serialized, which is the input the old +/// implementation answered with `"{}"` — a digest every failed value shared. +struct Unserializable; + +impl serde::Serialize for Unserializable { + fn serialize<S: serde::Serializer>(&self, _s: S) -> Result<S::Ok, S::Error> { + Err(serde::ser::Error::custom("this value cannot be serialized")) + } +} + +#[test] +fn a_value_that_cannot_be_serialized_is_an_error_not_an_empty_object() { + let err = hash::canonical_json(&Unserializable).unwrap_err(); + assert!(err.to_string().contains("cannot be serialized"), "the failure was swallowed: {err}"); + // The point of propagating: two different unserializable values now fail + // loudly instead of hashing to the same digest as each other. + assert!(hash::canonical_json(&Unserializable).is_err()); +} + +#[test] +fn the_rendering_is_canonical_in_fact_not_in_name() { + // Same fields, different declaration order: routed through `Value`, both + // come out with the keys sorted, so the bytes — and any digest of them — + // are the same. + #[derive(serde::Serialize)] + struct Ab { + alpha: String, + beta: String, + } + #[derive(serde::Serialize)] + struct Ba { + beta: String, + alpha: String, + } + let ab = hash::canonical_json(&Ab { alpha: "1".into(), beta: "2".into() }).unwrap(); + let ba = hash::canonical_json(&Ba { beta: "2".into(), alpha: "1".into() }).unwrap(); + assert_eq!(ab, ba, "field declaration order leaked into the rendering: {ab} vs {ba}"); + assert_eq!(ab, r#"{"alpha":"1","beta":"2"}"#, "not the compact sorted rendering"); + + // Same for a map assembled in an arbitrary order. + let mut one = HashMap::new(); + one.insert("z", 1); + one.insert("m", 2); + one.insert("a", 3); + let mut two = HashMap::new(); + two.insert("a", 3); + two.insert("m", 2); + two.insert("z", 1); + assert_eq!(hash::canonical_json(&one).unwrap(), hash::canonical_json(&two).unwrap()); +} + +#[test] +fn the_identity_digest_is_a_function_of_the_identity_alone() { + let base = Identity { + aur_commit: "cafe123".into(), + epoch: "0".into(), + pkgver: "1.2.3".into(), + pkgrel: "1".into(), + upstream_pins: vec![], + source_digests: vec![], + }; + let hash_of = |i: &Identity| i.hash().unwrap(); + assert!(hash_of(&base).starts_with("sha256:")); + assert_eq!(hash_of(&base), hash_of(&base.clone()), "the same identity hashed twice, twice differently"); + // Every element of the tuple is part of the key (§5.1a). + assert_ne!(hash_of(&base), hash_of(&Identity { pkgrel: "2".into(), ..base.clone() })); + assert_ne!(hash_of(&base), hash_of(&Identity { epoch: "1".into(), ..base.clone() })); + assert_ne!(hash_of(&base), hash_of(&Identity { aur_commit: "cafe124".into(), ..base.clone() })); +} diff --git a/tests/vcs.rs b/tests/vcs.rs index 2572c60..df0d756 100644 --- a/tests/vcs.rs +++ b/tests/vcs.rs @@ -108,7 +108,7 @@ fn stage_b_resolves_pkgver_and_captures_the_pins() { source_digests: vec![], }; let without = Identity { upstream_pins: vec![], ..with_pins.clone() }; - assert_ne!(with_pins.hash(), without.hash(), "pins must be in identity_hash"); + assert_ne!(with_pins.hash().unwrap(), without.hash().unwrap(), "pins must be in identity_hash"); } #[test] |
