Update unified address test vectors

This updates unified address test vectors after
https://github.com/zcash-hackworks/zcash-test-vectors/pull/89
to check addresses across multiple diversifier indices, and also
removes the superfluous binary encoding of UA strings.
This commit is contained in:
Kris Nuttycombe 2022-09-19 15:12:20 -06:00
parent daf02c4338
commit 34a7abd653
7 changed files with 1157 additions and 777 deletions

View File

@ -24,5 +24,8 @@ zcash_encoding = { version = "0.1", path = "../zcash_encoding" }
assert_matches = "1.3.0"
proptest = "1"
[features]
test-dependencies = []
[lib]
bench = false

View File

@ -91,8 +91,8 @@ impl super::Container for Address {
}
}
#[cfg(test)]
pub(crate) mod test_vectors;
#[cfg(any(test, feature = "test-dependencies"))]
pub mod test_vectors;
#[cfg(test)]
mod tests {

View File

@ -2,8 +2,8 @@ mod convert;
mod encoding;
mod kind;
#[cfg(test)]
mod test_vectors;
#[cfg(any(test, feature = "test-dependencies"))]
pub mod test_vectors;
pub use convert::{
ConversionError, ToAddress, TryFromAddress, TryFromRawAddress, UnsupportedAddress,

View File

@ -1,43 +1,50 @@
use std::iter;
/// Export test vectors for reuse by implementers of address parsing libraries.
#[cfg(feature = "test-dependencies")]
pub use crate::unified::address::test_vectors::TEST_VECTORS as UNIFIED;
use crate::{
unified::{
self,
address::{test_vectors::test_vectors, Receiver},
#[cfg(test)]
use {
crate::{
unified::{
self,
address::{test_vectors::TEST_VECTORS, Receiver},
},
Network, ToAddress, ZcashAddress,
},
Network, ToAddress, ZcashAddress,
std::iter,
};
#[test]
fn unified() {
for tv in test_vectors() {
for tv in TEST_VECTORS {
// Double-check test vectors match requirements:
// - Only one of P2PKH and P2SH.
assert!(tv.p2pkh_bytes.is_none() || tv.p2sh_bytes.is_none());
// - At least one shielded receiver.
assert!(tv.sapling_raw_addr.is_some() || tv.orchard_raw_addr.is_some());
let addr_string = String::from_utf8(tv.unified_addr.to_vec()).unwrap();
let unknown_tc = tv.unknown_typecode;
let unknown_bytes = tv.unknown_bytes;
let receivers = iter::empty()
.chain(tv.p2pkh_bytes.map(Receiver::P2pkh))
.chain(tv.p2sh_bytes.map(Receiver::P2sh))
.chain(tv.sapling_raw_addr.map(Receiver::Sapling))
.chain(tv.orchard_raw_addr.map(Receiver::Orchard))
.chain(tv.unknown_bytes.map(|data| Receiver::Unknown {
typecode: unknown_tc,
data,
.chain(unknown_tc.and_then(|typecode| {
unknown_bytes.map(move |data| Receiver::Unknown {
typecode,
data: data.to_vec(),
})
}))
.collect();
let expected_addr = ZcashAddress::from_unified(Network::Main, unified::Address(receivers));
// Test parsing
let addr: ZcashAddress = addr_string.parse().unwrap();
let addr: ZcashAddress = tv.unified_addr.parse().unwrap();
assert_eq!(addr, expected_addr);
// Test serialization
assert_eq!(expected_addr.to_string(), addr_string);
assert_eq!(expected_addr.to_string(), tv.unified_addr.to_string());
}
}

View File

@ -54,6 +54,7 @@ proptest = "1.0.0"
rand_xorshift = "0.3"
tempfile = "3.1.0"
zcash_proofs = { version = "0.7", path = "../zcash_proofs" }
zcash_address = { version = "0.1", path = "../components/zcash_address", features = ["test-dependencies"] }
[features]
transparent-inputs = ["ripemd", "hdwallet", "sha2", "secp256k1", "zcash_primitives/transparent-inputs"]

View File

@ -225,6 +225,7 @@ impl RecipientAddress {
#[cfg(test)]
mod tests {
use zcash_address::test_vectors;
use zcash_primitives::{consensus::MAIN_NETWORK, zip32::ExtendedFullViewingKey};
use super::{RecipientAddress, UnifiedAddress};
@ -255,4 +256,32 @@ mod tests {
Some(addr)
);
}
#[test]
fn ua_parsing() {
for tv in test_vectors::UNIFIED {
match RecipientAddress::decode(&MAIN_NETWORK, &tv.unified_addr) {
Some(RecipientAddress::Unified(ua)) => {
assert_eq!(
ua.transparent().is_some(),
tv.p2pkh_bytes.is_some() || tv.p2sh_bytes.is_some()
);
assert_eq!(ua.sapling().is_some(), tv.sapling_raw_addr.is_some());
assert_eq!(ua.orchard().is_some(), tv.orchard_raw_addr.is_some());
}
Some(_) => {
panic!(
"{} did not decode to a unified address value.",
tv.unified_addr
);
}
None => {
panic!(
"Failed to decode unified address from test vector: {}",
tv.unified_addr
);
}
}
}
}
}