Skip to content

Commit

Permalink
zcash_address: Add failing test showing unified::Address encoding bug
Browse files Browse the repository at this point in the history
  • Loading branch information
str4d committed May 25, 2021
1 parent c2b5704 commit ff8695d
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Seeds for failure cases proptest has generated in the past. It is
# automatically read and these particular cases re-run before any
# novel cases are generated.
#
# It is recommended to check this file in to source control so that
# everyone who runs the test benefits from these saved cases.
cc e08469bc301313ef868b97a5c37d9a9746d9720c915a9127c89db25c3be778fd # shrinks to ua = Address([Sapling([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]), P2pkh([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])])
55 changes: 55 additions & 0 deletions components/zcash_address/src/kind/unified.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,3 +139,58 @@ impl Address {
.collect()
}
}

#[cfg(test)]
mod tests {
use std::convert::TryFrom;

use proptest::{
array::{uniform11, uniform20, uniform32},
prelude::*,
};

use super::{Address, Receiver};

prop_compose! {
fn uniform43()(a in uniform11(0u8..), b in uniform32(0u8..)) -> [u8; 43] {
let mut c = [0; 43];
c[..11].copy_from_slice(&a);
c[11..].copy_from_slice(&b);
c
}
}

fn arb_shielded_receiver() -> BoxedStrategy<Receiver> {
prop_oneof![
uniform43().prop_map(Receiver::Sapling),
uniform43().prop_map(Receiver::Orchard),
]
.boxed()
}

fn arb_transparent_receiver() -> BoxedStrategy<Receiver> {
prop_oneof![
uniform20(0u8..).prop_map(Receiver::P2pkh),
uniform20(0u8..).prop_map(Receiver::P2sh),
]
.boxed()
}

prop_compose! {
fn arb_unified_address()(
shielded in prop::collection::hash_set(arb_shielded_receiver(), 1..2),
transparent in prop::option::of(arb_transparent_receiver()),
) -> Address {
Address(shielded.into_iter().chain(transparent).collect())
}
}

proptest! {
#[test]
fn ua_roundtrip(ua in arb_unified_address()) {
let bytes = ua.to_bytes();
let decoded = Address::try_from(&bytes[..]);
prop_assert_eq!(decoded, Ok(ua));
}
}
}

0 comments on commit ff8695d

Please sign in to comment.