-
Notifications
You must be signed in to change notification settings - Fork 302
test(fortuna): add unit tests for HashChainState #2698
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -203,3 +203,87 @@ mod test { | |
| run_hash_chain_test([0u8; 32], 100, 55); | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod hash_chain_state_test { | ||
| use { | ||
| crate::state::{HashChainState, PebbleHashChain}, | ||
| anyhow::Result, | ||
| }; | ||
|
|
||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. add some tests for overlapping PebbleHashChains. |
||
| #[test] | ||
| fn test_from_chain_at_offset() { | ||
| let chain = PebbleHashChain::new([0u8; 32], 10, 1); | ||
| let hash_chain_state = HashChainState::from_chain_at_offset(5, chain); | ||
|
|
||
| assert_eq!(hash_chain_state.offsets.len(), 1); | ||
| assert_eq!(hash_chain_state.hash_chains.len(), 1); | ||
| assert_eq!(hash_chain_state.offsets[0], 5); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_reveal_valid_sequence() -> Result<()> { | ||
| let chain = PebbleHashChain::new([0u8; 32], 10, 1); | ||
| let expected_hash = chain.reveal_ith(3)?; | ||
|
|
||
| let hash_chain_state = HashChainState::from_chain_at_offset(5, chain); | ||
| let result = hash_chain_state.reveal(8)?; | ||
|
|
||
| assert_eq!(result, expected_hash); | ||
| Ok(()) | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_reveal_sequence_too_small() { | ||
| let chain = PebbleHashChain::new([0u8; 32], 10, 1); | ||
| let hash_chain_state = HashChainState::from_chain_at_offset(5, chain); | ||
|
|
||
| let result = hash_chain_state.reveal(4); | ||
| assert!(result.is_err()); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_reveal_sequence_too_large() { | ||
| let chain = PebbleHashChain::new([0u8; 32], 10, 1); | ||
| let hash_chain_state = HashChainState::from_chain_at_offset(5, chain); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. not related to this PR but we should make this |
||
|
|
||
| let result = hash_chain_state.reveal(15); | ||
| assert!(result.is_err()); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_multiple_hash_chains() -> Result<()> { | ||
| let chain1 = PebbleHashChain::new([0u8; 32], 10, 1); | ||
| let expected_hash1 = chain1.reveal_ith(3)?; | ||
|
|
||
| let chain2 = PebbleHashChain::new([1u8; 32], 10, 1); | ||
| let expected_hash2 = chain2.reveal_ith(3)?; | ||
|
|
||
| let mut hash_chain_state = HashChainState::from_chain_at_offset(5, chain1); | ||
|
||
| hash_chain_state.offsets.push(20); | ||
|
||
| hash_chain_state.hash_chains.push(chain2); | ||
|
|
||
| let result1 = hash_chain_state.reveal(8)?; | ||
| assert_eq!(result1, expected_hash1); | ||
|
|
||
| let result2 = hash_chain_state.reveal(23)?; | ||
| assert_eq!(result2, expected_hash2); | ||
|
|
||
| let result3 = hash_chain_state.reveal(15); | ||
| assert!(result3.is_err()); | ||
|
|
||
| Ok(()) | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_reveal_at_offset() -> Result<()> { | ||
|
||
| let chain = PebbleHashChain::new([0u8; 32], 10, 1); | ||
| let expected_hash = chain.reveal_ith(0)?; | ||
|
|
||
| let hash_chain_state = HashChainState::from_chain_at_offset(5, chain); | ||
| let result = hash_chain_state.reveal(5)?; | ||
|
|
||
| assert_eq!(result, expected_hash); | ||
| Ok(()) | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
any reason to have these in a separate
mod?