extern crate rand; extern crate merkle_test; use rand::Rng; use merkle_test::{MerkleTree, MerkleTreeError}; use merkle_test::crypto_reexport::sha2::Sha256; fn random_sha256() -> [u8; 256] { let mut buffer = [0u8; 256]; rand::thread_rng().fill_bytes(&mut buffer); buffer } fn random_sha256_hashes(num: u64) -> Vec<[u8; 256]> { let mut buffer = Vec::new(); (0..num).for_each(|_| buffer.push(random_sha256())); buffer } #[test] fn same_hashes_same_root() { let hashes = random_sha256_hashes(1024); let mut tree1 = MerkleTree::new(Sha256::new()); hashes.iter().for_each(|hash| tree1.add_hash(hash.to_vec())); let mut tree2 = MerkleTree::new(Sha256::new()); hashes.iter().for_each(|hash| tree2.add_hash(hash.to_vec())); assert_eq!(tree1.root().unwrap(), tree2.root().unwrap()); } #[test] fn not_enough_hashes_error() { let mut tree = MerkleTree::new(Sha256::new()); assert_eq!(tree.root(), Err(MerkleTreeError::NotEnoughHashes)); let hash = random_sha256(); tree.add_hash(hash.to_vec()); assert_eq!(tree.root(), Err(MerkleTreeError::NotEnoughHashes)); }