diff --git a/Cargo.lock b/Cargo.lock index 5b3f3ec8..d3bc5156 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -389,6 +389,7 @@ version = "0.10.0-rc.2" dependencies = [ "arbitrary", "hex-literal", + "proptest", ] [[package]] diff --git a/const-oid/Cargo.toml b/const-oid/Cargo.toml index 50e7a878..80b10298 100644 --- a/const-oid/Cargo.toml +++ b/const-oid/Cargo.toml @@ -22,6 +22,7 @@ arbitrary = { version = "1.2", optional = true, features = ["derive"] } [dev-dependencies] hex-literal = "0.4" +proptest = "1" [features] db = [] diff --git a/const-oid/tests/proptests.proptest-regressions b/const-oid/tests/proptests.proptest-regressions new file mode 100644 index 00000000..99ff21bf --- /dev/null +++ b/const-oid/tests/proptests.proptest-regressions @@ -0,0 +1,7 @@ +# 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 1663923d2fb0c804c5b850d10dd0ded1cbfc06dddf3f88faa4abf149b8430831 # shrinks to s = "" diff --git a/const-oid/tests/proptests.rs b/const-oid/tests/proptests.rs new file mode 100644 index 00000000..e459f4ba --- /dev/null +++ b/const-oid/tests/proptests.rs @@ -0,0 +1,43 @@ +//! `proptest`-powered property-based tests. + +use const_oid::ObjectIdentifier; +use proptest::prelude::*; + +prop_compose! { + /// Produce a string of digits and dots, i.e. the component parts of OIDs. + /// + /// Note that this can be any permutation of digits-and-dots and does not necessarily + /// represent a valid OID. + fn oid_like_string()(bytes in any::>()) -> String { + // Create a digit or dot from a byte input + fn byte_to_char(byte: u8) -> char { + match byte % 11 { + n @ 0..=9 => (b'0' + n) as char, + 10 => '.', + _ => unreachable!() + } + } + + + let mut ret = String::with_capacity(bytes.len()); + for byte in bytes { + ret.push(byte_to_char(byte)); + } + ret + } +} + +proptest! { + #[test] + fn round_trip(s in oid_like_string()) { + match ObjectIdentifier::new(&s) { + Ok(oid) => { + let oid_string = oid.to_string(); + prop_assert_eq!(s, oid_string); + }, + Err(_e) => { + // TODO(tarcieri): use a regex or some other method to ensure the OID is invalid + } + } + } +}