diff --git a/zcash_primitives/src/legacy.rs b/zcash_primitives/src/legacy.rs index 4952ae6d6..14df1783d 100644 --- a/zcash_primitives/src/legacy.rs +++ b/zcash_primitives/src/legacy.rs @@ -478,6 +478,8 @@ pub mod testing { #[cfg(test)] mod tests { use super::{NonHardenedChildIndex, OpCode, Script, TransparentAddress}; + use hdwallet::KeyIndex; + use subtle::ConstantTimeEq; #[test] fn script_opcode() { @@ -561,4 +563,38 @@ mod tests { assert!(NonHardenedChildIndex::from_index(0x80000000).is_none()); assert!(NonHardenedChildIndex::from_index(0xffffffff).is_none()); } + + #[test] + fn nonhardened_index_next() { + assert_eq!(1, NonHardenedChildIndex::ZERO.next().unwrap().index()); + assert!(NonHardenedChildIndex::from_index(0x7fffffff) + .unwrap() + .next() + .is_none()); + } + + #[test] + fn nonhardened_index_ct_eq() { + assert!(check( + NonHardenedChildIndex::ZERO, + NonHardenedChildIndex::ZERO + )); + assert!(!check( + NonHardenedChildIndex::ZERO, + NonHardenedChildIndex::ZERO.next().unwrap() + )); + + fn check(v1: T, v2: T) -> bool { + v1.ct_eq(&v2).unwrap_u8() == 1 + } + } + + #[test] + #[cfg(feature = "transparent-inputs")] + fn nonhardened_index_tryfrom_keyindex() { + let nh: NonHardenedChildIndex = KeyIndex::Normal(0).try_into().unwrap(); + assert_eq!(nh.index(), 0); + + assert!(NonHardenedChildIndex::try_from(KeyIndex::Hardened(0)).is_err()); + } }