Skip to content

Commit f8e3faa

Browse files
author
Ruslan Piasetskyi
committed
digest: add hash_rt_outsize_serialization_test macro
1 parent 63bb23d commit f8e3faa

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

digest/src/dev.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,43 @@ macro_rules! hash_serialization_test {
7373
};
7474
}
7575

76+
/// Define hash function serialization test
77+
#[macro_export]
78+
#[cfg_attr(docsrs, doc(cfg(feature = "dev")))]
79+
macro_rules! hash_rt_outsize_serialization_test {
80+
($name:ident, $hasher:ty, $expected_serialized_state:expr) => {
81+
#[test]
82+
fn $name() {
83+
use digest::{
84+
crypto_common::{BlockSizeUser, SerializableState},
85+
typenum::Unsigned,
86+
Digest, Update, VariableOutput,
87+
};
88+
const HASH_OUTPUT_SIZE: usize = <$hasher>::MAX_OUTPUT_SIZE - 1;
89+
90+
let mut h = <$hasher>::new(HASH_OUTPUT_SIZE).unwrap();
91+
92+
h.update(&[0x13; <$hasher as BlockSizeUser>::BlockSize::USIZE + 1]);
93+
94+
let serialized_state = h.serialize();
95+
assert_eq!(serialized_state.as_slice(), $expected_serialized_state);
96+
97+
let mut h = <$hasher>::deserialize(&serialized_state).unwrap();
98+
99+
h.update(&[0x13; <$hasher as BlockSizeUser>::BlockSize::USIZE + 1]);
100+
let mut output1 = [0; HASH_OUTPUT_SIZE];
101+
h.finalize_variable(&mut output1).unwrap();
102+
103+
let mut h = <$hasher>::new(HASH_OUTPUT_SIZE).unwrap();
104+
h.update(&[0x13; 2 * (<$hasher as BlockSizeUser>::BlockSize::USIZE + 1)]);
105+
let mut output2 = [0; HASH_OUTPUT_SIZE];
106+
h.finalize_variable(&mut output2).unwrap();
107+
108+
assert_eq!(output1, output2);
109+
}
110+
};
111+
}
112+
76113
/// Define [`Update`][crate::Update] impl benchmark
77114
#[macro_export]
78115
#[cfg_attr(docsrs, doc(cfg(feature = "dev")))]

0 commit comments

Comments
 (0)