From f73a232027038b664ef04fedfd06feeb83f7acac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=BCdiger=20Klaehn?= Date: Fri, 21 Feb 2020 13:39:23 +0100 Subject: [PATCH 1/2] Add arbitraries Both for internal purposes and for use by other crates --- Cargo.toml | 7 ++++++- src/arb.rs | 32 ++++++++++++++++++++++++++++++++ src/lib.rs | 3 +++ 3 files changed, 41 insertions(+), 1 deletion(-) create mode 100644 src/arb.rs diff --git a/Cargo.toml b/Cargo.toml index 7e310026..d7f5ae07 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -18,11 +18,16 @@ sha-1 = { version = "0.8", default-features = false } sha2 = { version = "0.8", default-features = false } sha3 = { version = "0.8", default-features = false } unsigned-varint = "0.3" +quickcheck = { version = "0.9.2", optional = true } +rand = { version = "0.7.3", optional = true } [dev-dependencies] criterion = "0.3" -rand = "0.7" quickcheck = "0.9.2" +rand = "0.7.3" + +[features] +test = ["quickcheck", "rand"] [[bench]] name = "multihash" diff --git a/src/arb.rs b/src/arb.rs new file mode 100644 index 00000000..a7e4cf7f --- /dev/null +++ b/src/arb.rs @@ -0,0 +1,32 @@ +use crate::hashes::{Hash, Hash::*}; +use crate::{encode, Multihash}; +use quickcheck::{Arbitrary, Gen}; +use rand::seq::SliceRandom; + +const HASHES: [Hash; 16] = [ + Identity, SHA1, SHA2256, SHA2512, SHA3512, SHA3384, SHA3256, SHA3224, Keccak224, Keccak256, + Keccak384, Keccak512, Blake2b256, Blake2b512, Blake2s128, Blake2s256, +]; + +/// Generates a random hash algorithm. +/// +/// The more exotic ones will be generated just as frequently as the common ones. +impl Arbitrary for Hash { + fn arbitrary(g: &mut G) -> Self { + *HASHES.choose(g).unwrap() + } +} + +/// Generates a random valid multihash. +/// +/// This is done by encoding a random piece of data. +impl Arbitrary for Multihash { + fn arbitrary(g: &mut G) -> Self { + let hash: Hash = Arbitrary::arbitrary(g); + let data: Vec = Arbitrary::arbitrary(g); + // encoding an actual random piece of data might be better than just choosing + // random numbers of the appropriate size, since some hash algos might produce + // a limited set of values + encode(hash, &data).unwrap() + } +} diff --git a/src/lib.rs b/src/lib.rs index afd6cdbc..14bacd07 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -13,6 +13,9 @@ mod errors; mod hashes; mod storage; +#[cfg(any(test, feature = "test"))] +mod arb; + pub use digests::{wrap, Multihash, MultihashDigest, MultihashRef}; pub use errors::{DecodeError, DecodeOwnedError, EncodeError}; pub use hashes::*; From f3f93983a370e9591c3620ee2593cbb373d5c0c9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=BCdiger=20Klaehn?= Date: Mon, 2 Mar 2020 13:49:36 +0100 Subject: [PATCH 2/2] Use new API --- src/arb.rs | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/src/arb.rs b/src/arb.rs index a7e4cf7f..f645e2ba 100644 --- a/src/arb.rs +++ b/src/arb.rs @@ -1,17 +1,16 @@ -use crate::hashes::{Hash, Hash::*}; -use crate::{encode, Multihash}; +use crate::{Code, Code::*, Multihash}; use quickcheck::{Arbitrary, Gen}; use rand::seq::SliceRandom; -const HASHES: [Hash; 16] = [ - Identity, SHA1, SHA2256, SHA2512, SHA3512, SHA3384, SHA3256, SHA3224, Keccak224, Keccak256, - Keccak384, Keccak512, Blake2b256, Blake2b512, Blake2s128, Blake2s256, +const HASHES: [Code; 16] = [ + Identity, Sha1, Sha2_256, Sha2_512, Sha3_512, Sha3_384, Sha3_256, Sha3_224, Keccak224, + Keccak256, Keccak384, Keccak512, Blake2b256, Blake2b512, Blake2s128, Blake2s256, ]; /// Generates a random hash algorithm. /// /// The more exotic ones will be generated just as frequently as the common ones. -impl Arbitrary for Hash { +impl Arbitrary for Code { fn arbitrary(g: &mut G) -> Self { *HASHES.choose(g).unwrap() } @@ -22,11 +21,11 @@ impl Arbitrary for Hash { /// This is done by encoding a random piece of data. impl Arbitrary for Multihash { fn arbitrary(g: &mut G) -> Self { - let hash: Hash = Arbitrary::arbitrary(g); + let code: Code = Arbitrary::arbitrary(g); let data: Vec = Arbitrary::arbitrary(g); // encoding an actual random piece of data might be better than just choosing // random numbers of the appropriate size, since some hash algos might produce // a limited set of values - encode(hash, &data).unwrap() + code.hasher().unwrap().digest(&data) } }