From 3c7754b98e09d965973bf6580d48dcce9216e022 Mon Sep 17 00:00:00 2001 From: Shon Feder Date: Tue, 23 Feb 2021 10:40:43 -0500 Subject: [PATCH] Property based tests of Time (#815) * Proptest regressions for inversion of Time serde * Test property that Time's from_value inverts its to_value * Add parse time PBT failure * Use prop_assert! for cleaner results * Test that Time can parse RFC3339 timestamps * Remove spurious test failure * Only generate stamps for valid times And don't test dates before the epoch * Reduce range of input params in Time PBT * Add regression cases * Make timestamp serialization safe Without this change, serde of timestamps would result in a panic if the stamp was before the UNIX epoch. In discussion with @thanethomson, we agreed this opened up a potential attack vector, since a node could be sent messages causing it to crash on deserialization. It also violated the documented contract implied by the `From` trait: > Note: This trait must not fail. If the conversion can fail, use TryFrom. https://doc.rust-lang.org/std/convert/trait.From.html The fix is simply to use `impl From(DateTime) for SystemTime` supplied by chrono. * Cleanup and comment * More succinct parameter in safe timestamps test * Clippy fixes --- tendermint/Cargo.toml | 4 + tendermint/proptest-regressions/time.txt | 9 ++ tendermint/src/time.rs | 195 +++++++++++++++++++++-- 3 files changed, 191 insertions(+), 17 deletions(-) create mode 100644 tendermint/proptest-regressions/time.txt diff --git a/tendermint/Cargo.toml b/tendermint/Cargo.toml index 9caa6eaf1..1eb6fd84e 100644 --- a/tendermint/Cargo.toml +++ b/tendermint/Cargo.toml @@ -64,3 +64,7 @@ ripemd160 = { version = "0.9", optional = true } [features] secp256k1 = ["k256", "ripemd160"] + +[dev-dependencies] + +proptest = "0.10.1" diff --git a/tendermint/proptest-regressions/time.txt b/tendermint/proptest-regressions/time.txt new file mode 100644 index 000000000..affc86d67 --- /dev/null +++ b/tendermint/proptest-regressions/time.txt @@ -0,0 +1,9 @@ +# 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 7dcafc2d86a88d57fde70fe8b6dfbe9359a4d470b04fcd4bf3a7b0f05c2274a0 # shrinks to time = Time(1970-01-01T00:00:01Z) +cc 9f588cd1ad2e3715e8f3cae1b2e2738bef53f9f75c582e4e9e0f2b887328e6c2 # shrinks to time = Time(2262-04-11T23:47:17Z) +cc 6e17ffd49bafd6f5e8ddabc01784b27eea9c5213235aaca28f78607b1906f819 # shrinks to stamp = "0000-01-01T00:00:00-00:00" diff --git a/tendermint/src/time.rs b/tendermint/src/time.rs index 06ec79e38..43e7c2aed 100644 --- a/tendermint/src/time.rs +++ b/tendermint/src/time.rs @@ -41,7 +41,7 @@ impl From