Skip to content

Commit

Permalink
digest: add hash_rt_outsize_serialization_test macro
Browse files Browse the repository at this point in the history
  • Loading branch information
rpiasetskyi committed Oct 17, 2023
1 parent 325c3f7 commit 100b7a0
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions digest/src/dev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,43 @@ macro_rules! hash_serialization_test {
};
}

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

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

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 mut output1 = [0; HASH_OUTPUT_SIZE];
h.finalize_variable(&mut output1).unwrap();

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

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

/// Define [`Update`][crate::Update] impl benchmark
#[macro_export]
#[cfg_attr(docsrs, doc(cfg(feature = "dev")))]
Expand Down

0 comments on commit 100b7a0

Please sign in to comment.