changelog shortlog graph tags branches changeset files revisions annotate raw help

Mercurial > core / rust/lib/hash/src/tests.rs

changeset 698: 96958d3eb5b0
parent: 0ccbbd142694
author: Richard Westhaver <ellis@rwest.io>
date: Fri, 04 Oct 2024 22:04:59 -0400
permissions: -rw-r--r--
description: fixes
1 use crate::*;
2 use hex::decode;
3 use std::convert::TryInto;
4 
5 #[test]
6 fn hex_hash() -> Result<(), Box<dyn std::error::Error>> {
7  let mut hasher1 = B3Hasher::new();
8  hasher1.update(b"foo");
9  hasher1.update(b"bar");
10  hasher1.update(b"baz");
11  let out1 = hasher1.finalize();
12  let mut xof1 = [0; 301];
13  hasher1.finalize_xof().fill(&mut xof1);
14  assert_eq!(out1.as_bytes(), &xof1[..32]);
15 
16  let hash_hex =
17  "d74981efa70a0c880b8d8c1985d075dbcbf679b99a5f9914e5aaf96b831a9e24";
18  let hash_bytes = decode(hash_hex).unwrap();
19  let hash_array: [u8; OUT_LEN] = hash_bytes[..].try_into().unwrap();
20  let _: B3Hash = hash_array.into();
21  Ok(())
22 }
23 
24 #[test]
25 fn id_state_hash() {
26  let id = Id(vec![0; crate::KEY_LEN]);
27  let hash = id.state_hash(&mut crate::B3Hasher::new());
28  assert_eq!(hash, id.state_hash(&mut crate::B3Hasher::new()));
29 }
30 
31 #[test]
32 fn id_hex() {
33  let id = crate::Id(vec![255; crate::KEY_LEN]);
34 
35  assert_eq!(
36  hex::decode(
37  "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"
38  )
39  .unwrap(),
40  id.0
41  );
42 }
43 
44 #[test]
45 fn rand_id() {
46  let id = crate::Id::rand();
47  let hash = id.state_hash(&mut crate::B3Hasher::new());
48  assert_eq!(hash, id.state_hash(&mut crate::B3Hasher::new()));
49 }
50 
51 #[test]
52 fn random_demon_id_is_valid() {
53  use crate::PeerId;
54  for _ in 0..5000 {
55  let did = PeerId::rand();
56  let did2 = PeerId::rand();
57  assert_eq!(did, did);
58  assert_ne!(did, did2);
59  }
60 }