Skip to content

Commit 1f55cdb

Browse files
authored
const-oid: add basic proptests (#1596)
Exercises the parser beyond what we currently do in unit tests
1 parent cc1ffe8 commit 1f55cdb

File tree

4 files changed

+73
-0
lines changed

4 files changed

+73
-0
lines changed

Cargo.lock

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

const-oid/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ arbitrary = { version = "1.2", optional = true, features = ["derive"] }
2222

2323
[dev-dependencies]
2424
hex-literal = "0.4"
25+
proptest = "1"
26+
regex = "1"
2527

2628
[features]
2729
db = []
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Seeds for failure cases proptest has generated in the past. It is
2+
# automatically read and these particular cases re-run before any
3+
# novel cases are generated.
4+
#
5+
# It is recommended to check this file in to source control so that
6+
# everyone who runs the test benefits from these saved cases.
7+
cc 1663923d2fb0c804c5b850d10dd0ded1cbfc06dddf3f88faa4abf149b8430831 # shrinks to s = ""
8+
cc 829ba8833ee42816bc33d308b7a186452e36617f0fa0e771edea08fd07d78718 # shrinks to s = "0.40"
9+
cc bc88f232a7e2e45d2b1325d4e02c09742aca7ad31903326fedb36c047533696c # shrinks to s = "0"
10+
cc d90305406041ea5e4cf4d9e7849cad03391db1869d0b1329f60ccbf1fabaee91 # shrinks to s = "0..0"
11+
cc 8ed8dde35d12a2c8e10cdde6d591a8f17f0cd6d6fdf90f1582536401364623bf # shrinks to s = "0.00"
12+
cc ba5e3e3dc1a64870477e82054bbf6d8272f8b0d0c9094115bf7e8b5ff59f3c63 # shrinks to s = "00.1.1"
13+
cc d211e943da9a0e3d0ee5097899b2435f784ca2b3d2f8d4790aae3744823a268a # shrinks to s = "1.1.1.60817410.1"

const-oid/tests/proptests.rs

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
//! `proptest`-powered property-based tests.
2+
3+
use const_oid::{Error, ObjectIdentifier};
4+
use proptest::prelude::*;
5+
use regex::Regex;
6+
7+
prop_compose! {
8+
/// Produce a string of digits and dots, i.e. the component parts of OIDs.
9+
///
10+
/// Note that this can be any permutation of digits-and-dots and does not necessarily
11+
/// represent a valid OID.
12+
fn oid_like_string()(bytes in any::<Vec<u8>>()) -> String {
13+
// Create a digit or dot from a byte input
14+
fn byte_to_char(byte: u8) -> char {
15+
match byte % 11 {
16+
n @ 0..=9 => (b'0' + n) as char,
17+
10 => '.',
18+
_ => unreachable!()
19+
}
20+
}
21+
22+
23+
let mut ret = String::with_capacity(bytes.len());
24+
for byte in bytes {
25+
ret.push(byte_to_char(byte));
26+
}
27+
ret
28+
}
29+
}
30+
31+
proptest! {
32+
#[test]
33+
fn round_trip(s in oid_like_string()) {
34+
match ObjectIdentifier::new(&s) {
35+
Ok(oid) => {
36+
// Leading zeros won't round trip, so ignore that case
37+
// TODO(tarcieri): disallow leading zeros?
38+
if !s.starts_with("0") && !s.contains(".0") {
39+
let oid_string = oid.to_string();
40+
prop_assert_eq!(s, oid_string);
41+
}
42+
},
43+
Err(Error::ArcInvalid { .. }) | Err(Error::ArcTooBig) => (),
44+
Err(e) => {
45+
let re = Regex::new("^([0-2])\\.([0-3]?[0-9])((\\.0)|(\\.[1-9][0-9]*))+$").unwrap();
46+
47+
prop_assert!(
48+
re.find(&s).is_none(),
49+
"regex asserts OID `{}` is valid, but `const-oid`failed: {}",
50+
&s,
51+
&e
52+
);
53+
}
54+
}
55+
}
56+
}

0 commit comments

Comments
 (0)