Skip to content

Commit a8ab622

Browse files
committed
tests: Add tests for identity hashes
And a note about the size of hashes.
1 parent 5fb6bc7 commit a8ab622

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

src/hashes.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@ impl Hash {
6363
/// Get the hash length in bytes.
6464
pub fn size(self) -> u8 {
6565
match self {
66+
// TODO vmx 2020-01-27: Identity doesn't have a fixed length. The `size()` API should
67+
// be removed or renamed to `max_size()` as you can also store truncated hashes with a
68+
// different size.
6669
Hash::Identity => 42,
6770
Hash::SHA1 => 20,
6871
Hash::SHA2256 => 32,

tests/lib.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ macro_rules! assert_encode {
2626
#[test]
2727
fn multihash_encode() {
2828
assert_encode! {
29+
Identity, b"beep boop", "00096265657020626f6f70";
2930
SHA1, b"beep boop", "11147c8357577f51d4f0a8d393aa1aaafb28863d9421";
3031
SHA2256, b"helloworld", "1220936a185caaa266bb9cbe981e9e05cb78cd732b0b3280eb944412bb6f8f8f07af";
3132
SHA2256, b"beep boop", "122090ea688e275d580567325032492b597bc77221c62493e76330b85ddda191ef7c";
@@ -59,6 +60,7 @@ macro_rules! assert_decode {
5960
#[test]
6061
fn assert_decode() {
6162
assert_decode! {
63+
Identity, "000a68656c6c6f776f726c64";
6264
SHA1, "11147c8357577f51d4f0a8d393aa1aaafb28863d9421";
6365
SHA2256, "1220936a185caaa266bb9cbe981e9e05cb78cd732b0b3280eb944412bb6f8f8f07af";
6466
SHA2256, "122090ea688e275d580567325032492b597bc77221c62493e76330b85ddda191ef7c";
@@ -93,7 +95,7 @@ macro_rules! assert_roundtrip {
9395
#[test]
9496
fn assert_roundtrip() {
9597
assert_roundtrip!(
96-
SHA1, SHA2256, SHA2512, SHA3224, SHA3256, SHA3384, SHA3512, Keccak224, Keccak256,
98+
Identity, SHA1, SHA2256, SHA2512, SHA3224, SHA3256, SHA3384, SHA3512, Keccak224, Keccak256,
9799
Keccak384, Keccak512, Blake2b512, Blake2s256
98100
);
99101
}
@@ -147,6 +149,7 @@ fn test_methods(hash: Hash, prefix: &str, digest: &str) {
147149

148150
#[test]
149151
fn multihash_methods() {
152+
test_methods(Hash::Identity, "000b", "68656c6c6f20776f726c64");
150153
test_methods(
151154
Hash::SHA1,
152155
"1114",
@@ -226,6 +229,12 @@ fn multihash_errors() {
226229
Multihash::from_bytes(vec![0x12, 0x20, 0xff]).is_err(),
227230
"Should error on correct prefix with wrong digest"
228231
);
232+
let identity_code = Hash::Identity.code() as u8;
233+
let identity_length = 3;
234+
assert!(
235+
Multihash::from_bytes(vec![identity_code, identity_length, 1, 2, 3, 4]).is_err(),
236+
"Should error on wrong hash length"
237+
);
229238
}
230239

231240
#[test]
@@ -246,4 +255,10 @@ fn multihash_ref_errors() {
246255
MultihashRef::from_slice(&[0x12, 0x20, 0xff]).is_err(),
247256
"Should error on correct prefix with wrong digest"
248257
);
258+
let identity_code = Hash::Identity.code() as u8;
259+
let identity_length = 3;
260+
assert!(
261+
MultihashRef::from_slice(&[identity_code, identity_length, 1, 2, 3, 4]).is_err(),
262+
"Should error on wrong hash length"
263+
);
249264
}

0 commit comments

Comments
 (0)