Skip to content

Commit

Permalink
digest: add hash_serialization_test macro
Browse files Browse the repository at this point in the history
  • Loading branch information
rpiasetskyi committed Nov 14, 2023
1 parent 2dba419 commit 7a15518
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions digest/src/dev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,40 @@ macro_rules! new_test {
};
}

/// Define hash function serialization test
#[macro_export]
#[cfg_attr(docsrs, doc(cfg(feature = "dev")))]
macro_rules! hash_serialization_test {
($name:ident, $hasher:ty, $expected_serialized_state:expr) => {
#[test]
fn $name() {
use digest::{
crypto_common::{BlockSizeUser, SerializableState},
typenum::Unsigned,
Digest,
};

let mut h = <$hasher>::new();

h.update(&[0x13; <$hasher as BlockSizeUser>::BlockSize::USIZE + 1]);

let serialized_state = h.serialize();
assert_eq!(serialized_state.as_slice(), $expected_serialized_state);

let mut h = <$hasher>::deserialize(&serialized_state).unwrap();

h.update(&[0x13; <$hasher as BlockSizeUser>::BlockSize::USIZE + 1]);
let output1 = h.finalize();

let mut h = <$hasher>::new();
h.update(&[0x13; 2 * (<$hasher as BlockSizeUser>::BlockSize::USIZE + 1)]);
let output2 = h.finalize();

assert_eq!(output1, output2);
}
};
}

/// Define [`Update`][crate::Update] impl benchmark
#[macro_export]
macro_rules! bench_update {
Expand Down

0 comments on commit 7a15518

Please sign in to comment.