2021-06-03 15:48:40 -07:00
|
|
|
//! Arbitrary data generation for serialization proptests
|
|
|
|
|
2021-05-30 19:52:34 -07:00
|
|
|
use super::{read_zcash::canonical_ip_addr, DateTime32};
|
2021-05-24 08:10:07 -07:00
|
|
|
use chrono::{TimeZone, Utc, MAX_DATETIME, MIN_DATETIME};
|
|
|
|
use proptest::{arbitrary::any, prelude::*};
|
2021-05-25 21:55:07 -07:00
|
|
|
use std::net::SocketAddr;
|
2021-05-24 08:10:07 -07:00
|
|
|
|
2021-05-30 19:52:34 -07:00
|
|
|
impl Arbitrary for DateTime32 {
|
|
|
|
type Parameters = ();
|
|
|
|
|
|
|
|
fn arbitrary_with(_args: Self::Parameters) -> Self::Strategy {
|
|
|
|
any::<u32>().prop_map(Into::into).boxed()
|
|
|
|
}
|
|
|
|
|
|
|
|
type Strategy = BoxedStrategy<Self>;
|
|
|
|
}
|
|
|
|
|
2021-05-24 08:10:07 -07:00
|
|
|
/// Returns a strategy that produces an arbitrary [`chrono::DateTime<Utc>`],
|
|
|
|
/// based on the full valid range of the type.
|
|
|
|
///
|
2021-05-24 20:42:45 -07:00
|
|
|
/// Both the seconds and nanoseconds values are randomised, including leap seconds:
|
2021-05-25 10:21:06 -07:00
|
|
|
/// <https://docs.rs/chrono/0.4.19/chrono/naive/struct.NaiveTime.html#leap-second-handling>
|
2021-05-24 08:10:07 -07:00
|
|
|
///
|
2021-05-24 20:42:45 -07:00
|
|
|
/// Wherever possible, Zebra should handle leap seconds by:
|
|
|
|
/// - making durations and intervals 3 seconds or longer,
|
|
|
|
/// - avoiding complex time-based calculations, and
|
|
|
|
/// - avoiding relying on subsecond precision or time order.
|
|
|
|
/// When monotonic times are needed, use the opaque `std::time::Instant` type.
|
|
|
|
///
|
|
|
|
/// # Usage
|
|
|
|
///
|
|
|
|
/// Zebra uses these times internally, typically via [`Utc::now`].
|
2021-05-24 08:10:07 -07:00
|
|
|
///
|
|
|
|
/// Some parts of the Zcash network protocol ([`Version`] messages) also use times
|
|
|
|
/// with an 8-byte seconds value. Unlike this function, they have zero
|
2021-05-24 20:42:45 -07:00
|
|
|
/// nanoseconds values. (So they never have `chrono` leap seconds.)
|
2021-05-24 08:10:07 -07:00
|
|
|
pub fn datetime_full() -> impl Strategy<Value = chrono::DateTime<Utc>> {
|
|
|
|
(
|
2021-05-24 20:42:45 -07:00
|
|
|
// TODO: should we be subtracting 1 from the maximum timestamp?
|
2021-05-24 08:10:07 -07:00
|
|
|
MIN_DATETIME.timestamp()..=MAX_DATETIME.timestamp(),
|
2021-05-24 20:42:45 -07:00
|
|
|
0..2_000_000_000_u32,
|
2021-05-24 08:10:07 -07:00
|
|
|
)
|
|
|
|
.prop_map(|(secs, nsecs)| Utc.timestamp(secs, nsecs))
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns a strategy that produces an arbitrary time from a [`u32`] number
|
|
|
|
/// of seconds past the epoch.
|
|
|
|
///
|
|
|
|
/// The nanoseconds value is always zero.
|
|
|
|
///
|
|
|
|
/// The Zcash protocol typically uses 4-byte seconds values, except for the
|
|
|
|
/// [`Version`] message.
|
2021-05-30 19:52:34 -07:00
|
|
|
///
|
|
|
|
/// TODO: replace this strategy with `any::<DateTime32>()`.
|
2021-05-24 08:10:07 -07:00
|
|
|
pub fn datetime_u32() -> impl Strategy<Value = chrono::DateTime<Utc>> {
|
2021-05-30 19:52:34 -07:00
|
|
|
any::<DateTime32>().prop_map(Into::into)
|
2021-05-24 08:10:07 -07:00
|
|
|
}
|
2021-05-25 21:55:07 -07:00
|
|
|
|
|
|
|
/// 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()),
|
|
|
|
})
|
|
|
|
}
|