Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move time proptest generators into their own crate #829

Merged
merged 8 commits into from
Mar 22, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ members = [
"proto",
"rpc",
"tendermint",
"testgen"
"testgen",
"pbt-gen"
]

exclude = [
Expand Down
16 changes: 16 additions & 0 deletions pbt-gen/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[package]
name = "pbt-gen"
shonfeder marked this conversation as resolved.
Show resolved Hide resolved
version = "0.1.0"
authors = ["Shon Feder <shon.feder@gmail.com>"]
shonfeder marked this conversation as resolved.
Show resolved Hide resolved
edition = "2018"
description = """
An internal crate provoding proptest generators used accross our
shonfeder marked this conversation as resolved.
Show resolved Hide resolved
crates and not depending on any code internal to those crates.
"""

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]

chrono = { version = "0.4", features = ["serde"] }
shonfeder marked this conversation as resolved.
Show resolved Hide resolved
proptest = "0.10.1"
11 changes: 11 additions & 0 deletions pbt-gen/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# PBT Gen

This internal crate collects [proptest](https://github.com/AltSysrq/proptest)
generators used for testing, are of some general utility, and don't rely on any
code internal to the various crates of this repository.

To view the documentation (which includes examples) run

```sh
cargo doc --open -p pbt-gen
```
1 change: 1 addition & 0 deletions pbt-gen/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod time;
182 changes: 182 additions & 0 deletions pbt-gen/src/time.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
//! [proptest](https://github.com/AltSysrq/proptest) generators for time-like
//! objects.

use std::convert::TryInto;

use chrono::{DateTime, NaiveDate, TimeZone, Timelike, Utc};
use proptest::prelude::*;

/// Any higher, and we're at seconds
pub const MAX_NANO_SECS: u32 = 999_999_999u32;

/// The most distant time in the past for which chrono produces correct
/// times from [Utc.timestamp](chrono::Utc.timestamp).
///
/// See https://github.com/chronotope/chrono/issues/537.
shonfeder marked this conversation as resolved.
Show resolved Hide resolved
///
/// ```
/// use pbt_gen;
///
/// assert_eq!(pbt_gen::time::min_time().to_string(), "1653-02-10 06:13:21 UTC".to_string());
/// ```
pub fn min_time() -> DateTime<Utc> {
Utc.timestamp(-9999999999, 0)
}

/// The most distant time in the future for which chrono produces correct
/// times from [Utc.timestamp](chrono::Utc.timestamp).
///
/// See https://github.com/chronotope/chrono/issues/537.
shonfeder marked this conversation as resolved.
Show resolved Hide resolved
///
/// ```
/// use pbt_gen;
///
/// assert_eq!(pbt_gen::time::max_time().to_string(), "5138-11-16 09:46:39 UTC".to_string());
/// ```
pub fn max_time() -> DateTime<Utc> {
Utc.timestamp(99999999999, 0)
}

fn num_days_in_month(year: i32, month: u32) -> u32 {
// Using chrono, we get the duration beteween this month and the next,
// then count the number of days in that duration. See
// https://stackoverflow.com/a/58188385/1187277
let given_month = NaiveDate::from_ymd(year, month, 1);
let next_month = NaiveDate::from_ymd(
if month == 12 { year + 1 } else { year },
if month == 12 { 1 } else { month + 1 },
1,
);
next_month
.signed_duration_since(given_month)
.num_days()
.try_into()
.unwrap()
}

prop_compose! {
/// An abitrary [chrono::DateTime] that is between the given `min`
/// and `max`.
///
/// # Examples
///
/// ```
/// use chrono::{TimeZone, Utc};
/// use pbt_gen;
/// use proptest::prelude::*;
///
/// proptest!{
/// fn rosa_luxemburg_and_octavia_butler_were_not_alive_at_the_same_time(
/// time_in_luxemburgs_lifespan in pbt_gen::time::arb_datetime_in_range(
/// Utc.ymd(1871, 3, 5).and_hms(0,0,0), // DOB
/// Utc.ymd(1919, 1, 15).and_hms(0,0,0), // DOD
/// ),
/// time_in_butlers_lifespan in pbt_gen::time::arb_datetime_in_range(
/// Utc.ymd(1947, 6, 22).and_hms(0,0,0), // DOB
/// Utc.ymd(2006, 2, 24).and_hms(0,0,0), // DOD
/// ),
/// ) {
/// prop_assert!(time_in_butlers_lifespan == time_in_butlers_lifespan)
shonfeder marked this conversation as resolved.
Show resolved Hide resolved
/// }
/// }
/// ```
pub fn arb_datetime_in_range(min: DateTime<Utc>, max: DateTime<Utc>)(
secs in min.timestamp()..max.timestamp()
)(
// min mano secods is only relevant if we happen to hit the minimum
// seconds on the nose.
nano in (if secs == min.timestamp() { min.nanosecond() } else { 0 })..MAX_NANO_SECS,
// Make secs in scope
secs in Just(secs),
) -> DateTime<Utc> {
println!(">> Secs {:?}", secs);
Utc.timestamp(secs, nano)
}
}

prop_compose! {
/// An abitrary [chrono::DateTime] (between [min_time] and [max_time]).
pub fn arb_datetime()
(
d in arb_datetime_in_range(min_time(), max_time())
) -> DateTime<Utc> {
d
}
}

// The following components of the timestamp follow
// Section 5.6 of RFC3339: https://tools.ietf.org/html/rfc3339#ref-ABNF.

prop_compose! {
// See https://tools.ietf.org/html/rfc3339#appendix-A
fn arb_rfc339_time_offset()(
sign in "[+-]",
hour in 0..23u8,
min in 0..59u8,
) -> String {
format!("{:}{:0>2}:{:0>2}", sign, hour, min)
}
}

fn arb_rfc3339_offset() -> impl Strategy<Value = String> {
prop_oneof![arb_rfc339_time_offset(), Just("Z".to_owned())]
}

prop_compose! {
fn arb_rfc3339_partial_time()(
hour in 0..23u8,
min in 0..59u8,
sec in 0..59u8,
secfrac in proptest::option::of(0..u64::MAX),
) -> String {
let frac = match secfrac {
None => "".to_owned(),
Some(frac) => format!(".{:}", frac)
};
format!("{:0>2}:{:0>2}:{:0>2}{:}", hour, min, sec, frac)
}
}

prop_compose! {
fn arb_rfc3339_full_time()(
time in arb_rfc3339_partial_time(),
offset in arb_rfc3339_offset()
) -> String {
format!("{:}{:}", time, offset)
}
}

prop_compose! {
fn arb_rfc3339_day_of_year_and_month(year: i32, month: u32)
(
d in 1..num_days_in_month(year, month)
) -> u32 {
d
}
}

prop_compose! {
fn arb_rfc3339_full_date()(year in 0..9999i32, month in 1..12u32)
(
day in arb_rfc3339_day_of_year_and_month(year, month),
year in Just(year),
month in Just(month),
) -> String {
format!("{:0>4}-{:0>2}-{:0>2}", year, month, day)
}
}

prop_compose! {
/// An aribtrary RFC3339 timestamp
///
/// For example: `1985-04-12T23:20:50.52Z`
///
/// The implementaiton follows
/// [Section 5.6 of RFC3339](https://tools.ietf.org/html/rfc3339#ref-ABNF)
pub fn arb_rfc3339_timestamp()(
date in arb_rfc3339_full_date(),
time in arb_rfc3339_full_time()
) -> String {
format!("{:}T{:}", date, time)
}
}
1 change: 1 addition & 0 deletions tendermint/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,4 @@ secp256k1 = ["k256", "ripemd160"]
[dev-dependencies]

proptest = "0.10.1"
pbt-gen = { path = "../pbt-gen" }
139 changes: 3 additions & 136 deletions tendermint/src/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,142 +144,9 @@ pub trait ParseTimestamp {

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

use super::*;

// TODO(shon) Extract arbitrary generators into own library

use chrono::{DateTime, NaiveDate, TimeZone, Timelike, Utc};
use proptest::{prelude::*, sample::select};

// Any higher, and we're at seconds
const MAX_NANO_SECS: u32 = 999_999_999u32;

// With values larger or smaller then these, chrono produces invalid rfc3339
// timestamps. See https://github.com/chronotope/chrono/issues/537
fn min_time() -> DateTime<Utc> {
Utc.timestamp(-9999999999, 0)
}

fn max_time() -> DateTime<Utc> {
Utc.timestamp(99999999999, 0)
}

fn num_days_in_month(year: i32, month: u32) -> u32 {
// Using chrono, we get the duration beteween this month and the next,
// then count the number of days in that duration. See
// https://stackoverflow.com/a/58188385/1187277
let given_month = NaiveDate::from_ymd(year, month, 1);
let next_month = NaiveDate::from_ymd(
if month == 12 { year + 1 } else { year },
if month == 12 { 1 } else { month + 1 },
1,
);
next_month
.signed_duration_since(given_month)
.num_days()
.try_into()
.unwrap()
}

prop_compose! {
/// An abitrary `chrono::DateTime` that is between `min` and `max`
/// DateTimes.
fn arb_datetime_in_range(min: DateTime<Utc>, max: DateTime<Utc>)(
secs in min.timestamp()..max.timestamp()
)(
// min mano secods is only relevant if we happen to hit the minimum
// seconds on the nose.
nano in (if secs == min.timestamp() { min.nanosecond() } else { 0 })..MAX_NANO_SECS,
// Make secs in scope
secs in Just(secs),
) -> DateTime<Utc> {
println!(">> Secs {:?}", secs);
Utc.timestamp(secs, nano)
}
}

prop_compose! {
/// An abitrary `chrono::DateTime`
fn arb_datetime()
(
d in arb_datetime_in_range(min_time(), max_time())
) -> DateTime<Utc> {
d
}
}

prop_compose! {
fn arb_rfc339_time_offset()(
sign in "[+-]",
hour in 0..23u8,
min in 0..59u8,
) -> String {
format!("{:}{:0>2}:{:0>2}", sign, hour, min)
}
}

fn arb_rfc3339_offset() -> impl Strategy<Value = String> {
prop_oneof![arb_rfc339_time_offset(), Just("Z".to_owned())]
}

prop_compose! {
fn arb_rfc3339_partial_time()(
hour in 0..23u8,
min in 0..59u8,
sec in 0..59u8,
secfrac in proptest::option::of(0..u64::MAX),
) -> String {
let frac = match secfrac {
None => "".to_owned(),
Some(frac) => format!(".{:}", frac)
};
format!("{:0>2}:{:0>2}:{:0>2}{:}", hour, min, sec, frac)
}
}

prop_compose! {
fn arb_rfc3339_full_time()(
time in arb_rfc3339_partial_time(),
offset in arb_rfc3339_offset()
) -> String {
format!("{:}{:}", time, offset)
}
}

prop_compose! {
fn arb_rfc3339_day_of_year_and_month(year: i32, month: u32)
(
d in 1..num_days_in_month(year, month)
) -> u32 {
d
}
}

prop_compose! {
fn arb_rfc3339_full_date()(year in 0..9999i32, month in 1..12u32)
(
day in arb_rfc3339_day_of_year_and_month(year, month),
year in Just(year),
month in Just(month),
) -> String {
format!("{:0>4}-{:0>2}-{:0>2}", year, month, day)
}
}

prop_compose! {
/// An aribtrary rfc3339 timestamp
///
/// Follows https://tools.ietf.org/html/rfc3339#section-5.6
fn arb_rfc3339_timestamp()(
date in arb_rfc3339_full_date(),
time in arb_rfc3339_full_time()
) -> String {
format!("{:}T{:}", date, time)
}
}

// We want to make sure that these timestamps specifically get tested.
fn particular_rfc3339_timestamps() -> impl Strategy<Value = String> {
let strs: Vec<String> = vec![
Expand Down Expand Up @@ -309,13 +176,13 @@ mod tests {

proptest! {
#[test]
fn can_parse_rfc3339_timestamps(stamp in arb_rfc3339_timestamp()) {
fn can_parse_rfc3339_timestamps(stamp in pbt_gen::time::arb_rfc3339_timestamp()) {
prop_assert!(stamp.parse::<Time>().is_ok())
}

#[test]
fn serde_from_value_is_the_inverse_of_to_value_within_reasonable_time_range(
datetime in arb_datetime()
datetime in pbt_gen::time::arb_datetime()
) {
// If `from_value` is the inverse of `to_value`, then it will always
// map the JSON `encoded_time` to back to the inital `time`.
Expand All @@ -328,7 +195,7 @@ mod tests {
#[test]
fn serde_of_rfc3339_timestamps_is_safe(
stamp in prop_oneof![
arb_rfc3339_timestamp(),
pbt_gen::time::arb_rfc3339_timestamp(),
particular_rfc3339_timestamps(),
]
) {
Expand Down