1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
|
use std::collections::HashMap;
use aur_build::db::model::Identity;
use aur_build::hash;
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}");
assert!(hash::canonical_json(&Unserializable).is_err());
}
#[test]
fn the_rendering_is_canonical_in_fact_not_in_name() {
#[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");
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");
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() }));
}
|