Skip to content

Commit 9a6ca71

Browse files
authored
Merge pull request #36 from os-checker/testing
test: Check all hash values are unique
2 parents 4a928ea + 3896f1f commit 9a6ca71

File tree

14 files changed

+95
-36
lines changed

14 files changed

+95
-36
lines changed

src/functions/serialization.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,9 @@ impl SerFunction {
3232
let mut hasher = StableHasher::<SipHasher128>::new();
3333
func.with_hasher(&mut hasher);
3434
hasher.write_length_prefix(attrs.len());
35-
attrs.iter().for_each(|a| hasher.write_str(a));
35+
attrs.iter().for_each(|attr| hasher.write_str(attr));
3636
hasher.write_length_prefix(callees.len());
37-
callees.iter().for_each(|c| {
38-
c.func.with_hasher(&mut hasher);
39-
});
37+
callees.iter().for_each(|callee| callee.func.with_hasher(&mut hasher));
4038
let Hash128(hash) = hasher.finish();
4139

4240
SerFunction { hash, def_id, attrs, func, callees }

src/functions/utils.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ pub struct SourceCode {
3131

3232
impl SourceCode {
3333
pub fn with_hasher(&self, hasher: &mut StableHasher<SipHasher128>) {
34+
hasher.write_str(&self.file);
3435
hasher.write_str(&self.src);
3536
match &self.before_expansion {
3637
Some(text) => {

tests/compare.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
use distributed_verification::SerFunction;
21
use std::fs::{copy, remove_file};
32

43
mod utils;
5-
use utils::{assert_eq, cmd, expect_file};
4+
use utils::{assert_eq, *};
65

76
fn get(text: &str, start: &str) -> SerFunction {
87
let json = &text[text.find("[\n").unwrap()..];

tests/proofs.rs

Lines changed: 65 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1+
use indexmap::IndexMap;
2+
use std::path::{Path, PathBuf};
3+
14
mod utils;
2-
use utils::*;
5+
use utils::{assert_eq, *};
36

4-
#[test]
5-
fn test_proofs() -> eyre::Result<()> {
7+
fn get_proofs(dir: &str) -> Result<Vec<PathBuf>> {
68
let mut proofs = vec![];
7-
for entry in std::fs::read_dir("tests/proofs")? {
9+
for entry in std::fs::read_dir(dir)? {
810
let entry = entry?;
911
let path = entry.path();
1012
if path.is_file()
@@ -13,8 +15,35 @@ fn test_proofs() -> eyre::Result<()> {
1315
proofs.push(path);
1416
}
1517
}
16-
1718
proofs.sort();
19+
Ok(proofs)
20+
}
21+
22+
fn assert_unique_hash(proofs: &[PathBuf], v_json: &[Vec<SerFunction>]) {
23+
let mut map = IndexMap::<&str, Vec<(&Path, &str)>>::new();
24+
for (v, proof) in v_json.iter().zip(proofs) {
25+
for json in v {
26+
let def_id = &*json.def_id;
27+
let item = (&**proof, def_id);
28+
map.entry(&json.hash)
29+
.and_modify(|v_item| v_item.push(item))
30+
.or_insert_with(|| vec![item]);
31+
}
32+
}
33+
for (hash, v_item) in &map {
34+
let v_item_len = v_item.len();
35+
assert_eq!(
36+
v_item_len, 1,
37+
"{hash} should only correspond to single item, but \
38+
{v_item_len} items have the hash value:\n{v_item:?}",
39+
);
40+
}
41+
}
42+
43+
#[test]
44+
fn test_proofs() -> Result<()> {
45+
let proofs = get_proofs("tests/proofs")?;
46+
1847
expect![[r#"
1948
[
2049
"tests/proofs/ad_hoc.rs",
@@ -26,12 +55,41 @@ fn test_proofs() -> eyre::Result<()> {
2655
"#]]
2756
.assert_debug_eq(&proofs);
2857

58+
let mut v_json = Vec::<Vec<SerFunction>>::with_capacity(proofs.len());
59+
for path in &proofs {
60+
let file_stem = path.file_stem().and_then(|f| f.to_str()).unwrap();
61+
let text = cmd(&[&format!("tests/proofs/{file_stem}.rs")]);
62+
expect_file![format!("./snapshots/{file_stem}.json")].assert_eq(&text);
63+
v_json.push(serde_json::from_str(&text).unwrap());
64+
}
65+
66+
assert_unique_hash(&proofs, &v_json);
67+
Ok(())
68+
}
69+
70+
#[test]
71+
fn test_compare() -> Result<()> {
72+
let proofs = get_proofs("tests/compare")?;
73+
74+
expect![[r#"
75+
[
76+
"tests/compare/contract1.rs",
77+
"tests/compare/contract2.rs",
78+
"tests/compare/proof1.rs",
79+
"tests/compare/proof2.rs",
80+
]
81+
"#]]
82+
.assert_debug_eq(&proofs);
83+
84+
let mut v_json = Vec::<Vec<SerFunction>>::with_capacity(proofs.len());
2985
for path in &proofs {
3086
let file_stem = path.file_stem().and_then(|f| f.to_str()).unwrap();
31-
let json = cmd(&[&format!("tests/proofs/{file_stem}.rs")]);
32-
expect_file![format!("./snapshots/{file_stem}.json")].assert_eq(&json);
87+
let text = cmd(&[&format!("tests/compare/{file_stem}.rs")]);
88+
// NOTE: don't write text to json file, since compare.rs write it in a different way
89+
v_json.push(serde_json::from_str(&text).unwrap());
3390
}
3491

92+
assert_unique_hash(&proofs, &v_json);
3593
Ok(())
3694
}
3795

tests/snapshots/ad_hoc.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[
22
{
3-
"hash": "10655023569148153324449981413843532452",
3+
"hash": "240402353946165679413432703131261523692",
44
"def_id": "DefId { id: 0, name: \"adhoc::callee_defined_in_proof\" }",
55
"attrs": [
66
"#[kanitool::proof]"
@@ -30,7 +30,7 @@
3030
]
3131
},
3232
{
33-
"hash": "79774297860844392025388862955799835958",
33+
"hash": "910297032864347757816118745588521867089",
3434
"def_id": "DefId { id: 2, name: \"adhoc::closure_in_proof\" }",
3535
"attrs": [
3636
"#[kanitool::proof]"
@@ -60,7 +60,7 @@
6060
]
6161
},
6262
{
63-
"hash": "1291521982426130649016479097978635423963",
63+
"hash": "210930681113882741614289248380189671885",
6464
"def_id": "DefId { id: 5, name: \"adhoc::proof_in_fn_item::proof\" }",
6565
"attrs": [
6666
"#[kanitool::proof]"

tests/snapshots/contract1.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[
22
{
3-
"hash": "142480961098313829086739331134333133873",
3+
"hash": "767709965873040961211552770308306929266",
44
"def_id": "DefId { id: 13, name: \"verify::f\" }",
55
"attrs": [
66
"#[kanitool::proof]"

tests/snapshots/contract2.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[
22
{
3-
"hash": "142480961098313829086739331134333133873",
3+
"hash": "767709965873040961211552770308306929266",
44
"def_id": "DefId { id: 13, name: \"verify::f\" }",
55
"attrs": [
66
"#[kanitool::proof]"

tests/snapshots/gen_by_macros.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[
22
{
3-
"hash": "975053280399656902416947254920631190028",
3+
"hash": "11523848083194292978283464977531584378",
44
"def_id": "DefId { id: 0, name: \"verify::proof1\" }",
55
"attrs": [
66
"#[kanitool::proof]"
@@ -62,7 +62,7 @@
6262
]
6363
},
6464
{
65-
"hash": "236593704829635227314040469354864202296",
65+
"hash": "1149667103326042736912034193006744676464",
6666
"def_id": "DefId { id: 1, name: \"verify::proof2\" }",
6767
"attrs": [
6868
"#[kanitool::proof]"
@@ -124,7 +124,7 @@
124124
]
125125
},
126126
{
127-
"hash": "5474537339301642838978910224697845004",
127+
"hash": "181931886334719422625983948383201250208",
128128
"def_id": "DefId { id: 2, name: \"verify::proof3\" }",
129129
"attrs": [
130130
"#[kanitool::proof]"

tests/snapshots/proof1.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[
22
{
3-
"hash": "327831672023728938116624563073238276451",
3+
"hash": "876433099390387370318218200145590989814",
44
"def_id": "DefId { id: 0, name: \"verify::f\" }",
55
"attrs": [
66
"#[kanitool::proof]"

tests/snapshots/proof2.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[
22
{
3-
"hash": "327831672023728938116624563073238276451",
3+
"hash": "876433099390387370318218200145590989814",
44
"def_id": "DefId { id: 0, name: \"verify::f\" }",
55
"attrs": [
66
"#[kanitool::proof]"
@@ -22,7 +22,7 @@
2222
]
2323
},
2424
{
25-
"hash": "588068929243813241914251336986802616528",
25+
"hash": "47211252920145052832965334366253119118",
2626
"def_id": "DefId { id: 1, name: \"verify::g\" }",
2727
"attrs": [
2828
"#[kanitool::proof]"

0 commit comments

Comments
 (0)