diff --git a/Cargo.lock b/Cargo.lock index 6f0bba99b..ced4ede2d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -28,6 +28,15 @@ version = "0.1.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4b46cbb362ab8752921c97e041f5e366ee6297bd428a31275b9fcf1e380f7299" +[[package]] +name = "arbitrary" +version = "1.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3e90af4de65aa7b293ef2d09daff88501eb254f58edde2e1ac02c82d873eadad" +dependencies = [ + "derive_arbitrary", +] + [[package]] name = "atty" version = "0.2.14" @@ -218,8 +227,9 @@ dependencies = [ [[package]] name = "const-oid" -version = "0.9.1" +version = "0.9.2" dependencies = [ + "arbitrary", "hex-literal", ] @@ -347,6 +357,17 @@ dependencies = [ "syn", ] +[[package]] +name = "derive_arbitrary" +version = "1.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8beee4701e2e229e8098bbdecdca12449bc3e322f137d269182fa1291e20bd00" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "des" version = "0.8.1" diff --git a/const-oid/Cargo.toml b/const-oid/Cargo.toml index ffc05e6e4..93f81dd10 100644 --- a/const-oid/Cargo.toml +++ b/const-oid/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "const-oid" -version = "0.9.1" +version = "0.9.2" authors = ["RustCrypto Developers"] license = "Apache-2.0 OR MIT" description = """ @@ -16,6 +16,9 @@ readme = "README.md" edition = "2021" rust-version = "1.57" +[dependencies] +arbitrary = { version = "1.2", optional = true, features = ["derive"] } + [dev-dependencies] hex-literal = "0.3" diff --git a/const-oid/src/lib.rs b/const-oid/src/lib.rs index b00d4e2e7..5bdef085d 100644 --- a/const-oid/src/lib.rs +++ b/const-oid/src/lib.rs @@ -252,3 +252,29 @@ impl fmt::Display for ObjectIdentifier { Ok(()) } } + +// Implement by hand because the derive would create invalid values. +// Use the constructor to create a valid oid with at least 3 arcs. +#[cfg(feature = "arbitrary")] +impl<'a> arbitrary::Arbitrary<'a> for ObjectIdentifier { + fn arbitrary(u: &mut arbitrary::Unstructured<'a>) -> arbitrary::Result { + let first = u.int_in_range(0..=arcs::ARC_MAX_FIRST)?; + let second = u.int_in_range(0..=arcs::ARC_MAX_SECOND)?; + let third = u.arbitrary()?; + + let mut oid = Self::from_arcs([first, second, third]) + .map_err(|_| arbitrary::Error::IncorrectFormat)?; + + for arc in u.arbitrary_iter()? { + oid = oid + .push_arc(arc?) + .map_err(|_| arbitrary::Error::IncorrectFormat)?; + } + + Ok(oid) + } + + fn size_hint(depth: usize) -> (usize, Option) { + (Arc::size_hint(depth).0.saturating_mul(3), None) + } +}