Skip to content

Commit 20e3531

Browse files
committed
const-oid: add basic proptests
Exercises the parser beyond what we currently do in unit tests
1 parent bbb0663 commit 20e3531

File tree

4 files changed

+52
-0
lines changed

4 files changed

+52
-0
lines changed

Cargo.lock

Lines changed: 1 addition & 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: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ arbitrary = { version = "1.2", optional = true, features = ["derive"] }
2222

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

2627
[features]
2728
db = []
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
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 = ""

const-oid/tests/proptests.rs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
//! `proptest`-powered property-based tests.
2+
3+
use const_oid::ObjectIdentifier;
4+
use proptest::prelude::*;
5+
6+
prop_compose! {
7+
/// Produce a string of digits and dots, i.e. the component parts of OIDs.
8+
///
9+
/// Note that this can be any permutation of digits-and-dots and does not necessarily
10+
/// represent a valid OID.
11+
fn oid_like_string()(bytes in any::<Vec<u8>>()) -> String {
12+
// Create a digit or dot from a byte input
13+
fn byte_to_char(byte: u8) -> char {
14+
match byte % 11 {
15+
n @ 0..=9 => (b'0' + n) as char,
16+
10 => '.',
17+
_ => unreachable!()
18+
}
19+
}
20+
21+
22+
let mut ret = String::with_capacity(bytes.len());
23+
for byte in bytes {
24+
ret.push(byte_to_char(byte));
25+
}
26+
ret
27+
}
28+
}
29+
30+
proptest! {
31+
#[test]
32+
fn round_trip(s in oid_like_string()) {
33+
match ObjectIdentifier::new(&s) {
34+
Ok(oid) => {
35+
let oid_string = oid.to_string();
36+
prop_assert_eq!(s, oid_string);
37+
},
38+
Err(_e) => {
39+
// TODO(tarcieri): use a regex or some other method to ensure the OID is invalid
40+
}
41+
}
42+
}
43+
}

0 commit comments

Comments
 (0)