| 1 | //! `proptest`-powered property-based tests. |
| 2 | |
| 3 | use const_oidrust-analyzer cargo const-oid 0.10.2 crate/::{Errorrust-analyzer cargo const-oid 0.10.2 error/Error#, ObjectIdentifierrust-analyzer cargo const-oid 0.10.2 ObjectIdentifier#}; |
| 4 | use proptestrust-analyzer cargo proptest 1.9.0 crate/::preluderust-analyzer cargo proptest 1.9.0 prelude/::*; |
| 5 | use regexrust-analyzer cargo regex 1.12.2 crate/::Regexrust-analyzer cargo regex 1.12.2 regex/string/Regex#; |
| 6 | |
| 7 | prop_composerust-analyzer cargo proptest 1.9.0 sugar/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_stringrust-analyzer cargo const-oid 0.10.2 oid_like_string().()(bytes in anyrust-analyzer cargo proptest 1.9.0 arbitrary/traits/any().::<Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<u8>>()) -> Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String# { |
| 13 | // Create a digit or dot from a byte input |
| 14 | fn byte_to_charrust-analyzer cargo const-oid 0.10.2 byte_to_char().(byte: u8) -> char { |
| 15 | match byte %rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u8][`Rem<Self>`]rem(). 11 { |
| 16 | n @ 0..=9 => (b'0' +rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u8][`Add<Self>`]add(). n) as char, |
| 17 | 10 => '.', |
| 18 | _ => unreachablerust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/unreachable!!() |
| 19 | } |
| 20 | } |
| 21 | |
| 22 | |
| 23 | let mut ret = Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#::with_capacityrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]with_capacity().(bytes.lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]len().()); |
| 24 | for byte in bytes { |
| 25 | ret.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]push().(byte_to_charrust-analyzer cargo const-oid 0.10.2 byte_to_char().(byte)); |
| 26 | } |
| 27 | ret |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | proptestrust-analyzer cargo proptest 1.9.0 sugar/proptest!! { |
| 32 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 33 | fn round_triprust-analyzer cargo const-oid 0.10.2 round_trip().(s in oid_like_stringrust-analyzer cargo const-oid 0.10.2 oid_like_string().()) { |
| 34 | match ObjectIdentifierrust-analyzer cargo const-oid 0.10.2 ObjectIdentifier#::newrust-analyzer cargo const-oid 0.10.2 impl#[ObjectIdentifier]new().(&s) { |
| 35 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(oid) => { |
| 36 | // Leading zeros won't round trip, so ignore that case |
| 37 | // TODO(tarcieri): disallow leading zeros? |
| 38 | if !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().s.starts_withrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]starts_with().("0") && !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().s.containsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]contains().(".0") { |
| 39 | let oid_string = oid.to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().(); |
| 40 | prop_assert_eqrust-analyzer cargo proptest 1.9.0 sugar/prop_assert_eq!!(s, oid_string); |
| 41 | } |
| 42 | }, |
| 43 | Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(Errorrust-analyzer cargo const-oid 0.10.2 error/Error#::ArcInvalidrust-analyzer cargo const-oid 0.10.2 error/Error#ArcInvalid# { .. }) | Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(Errorrust-analyzer cargo const-oid 0.10.2 error/Error#::ArcTooBigrust-analyzer cargo const-oid 0.10.2 error/Error#ArcTooBig#) => (), |
| 44 | Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(e) => { |
| 45 | let re = Regexrust-analyzer cargo regex 1.12.2 regex/string/Regex#::newrust-analyzer cargo regex 1.12.2 regex/string/impl#[Regex]new().("^([0-2])\\.([0-3]?[0-9])((\\.0)|(\\.[1-9][0-9]*))+$").unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 46 | |
| 47 | prop_assertrust-analyzer cargo proptest 1.9.0 sugar/prop_assert!!( |
| 48 | re.findrust-analyzer cargo regex 1.12.2 regex/string/impl#[Regex]find().(&s).is_nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]is_none().(), |
| 49 | "regex asserts OID `{}` is valid, but `const-oid`failed: {}", |
| 50 | &s, |
| 51 | &e |
| 52 | ); |
| 53 | } |
| 54 | } |
| 55 | } |
| 56 | } |