Canonicalise arbitrary IP addresses in proptests

This makes round-trip serialization tests work.
This commit is contained in:
teor 2021-05-26 14:55:07 +10:00 committed by Deirdre Connolly
parent 6fb94baeb9
commit 078385ae00
2 changed files with 30 additions and 2 deletions

View File

@ -1,5 +1,7 @@
use super::read_zcash::canonical_ip_addr;
use chrono::{TimeZone, Utc, MAX_DATETIME, MIN_DATETIME};
use proptest::{arbitrary::any, prelude::*};
use std::net::SocketAddr;
/// Returns a strategy that produces an arbitrary [`chrono::DateTime<Utc>`],
/// based on the full valid range of the type.
@ -39,3 +41,14 @@ pub fn datetime_full() -> impl Strategy<Value = chrono::DateTime<Utc>> {
pub fn datetime_u32() -> impl Strategy<Value = chrono::DateTime<Utc>> {
any::<u32>().prop_map(|secs| Utc.timestamp(secs.into(), 0))
}
/// Returns a random canonical Zebra `SocketAddr`.
///
/// See [`canonical_ip_addr`] for details.
pub fn canonical_socket_addr() -> impl Strategy<Value = SocketAddr> {
use SocketAddr::*;
any::<SocketAddr>().prop_map(|addr| match addr {
V4(_) => addr,
V6(v6_addr) => SocketAddr::new(canonical_ip_addr(v6_addr.ip()), v6_addr.port()),
})
}

View File

@ -2,15 +2,30 @@ use proptest::{arbitrary::any, arbitrary::Arbitrary, prelude::*};
use super::{MetaAddr, PeerAddrState, PeerServices};
use zebra_chain::serialization::arbitrary::{canonical_socket_addr, datetime_u32};
use chrono::{TimeZone, Utc};
use std::net::SocketAddr;
impl MetaAddr {
pub fn gossiped_strategy() -> BoxedStrategy<Self> {
(
canonical_socket_addr(),
any::<PeerServices>(),
datetime_u32(),
)
.prop_map(|(address, services, untrusted_last_seen)| {
MetaAddr::new_gossiped_meta_addr(address, services, untrusted_last_seen)
})
.boxed()
}
}
impl Arbitrary for MetaAddr {
type Parameters = ();
fn arbitrary_with(_args: Self::Parameters) -> Self::Strategy {
(
any::<SocketAddr>(),
canonical_socket_addr(),
any::<PeerServices>(),
any::<u32>(),
any::<PeerAddrState>(),