diff options
31 files changed, 176 insertions, 123 deletions
@@ -2253,12 +2253,22 @@ dependencies = [ [[package]] name = "quickcheck" -version = "0.9.2" +version = "1.0.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a44883e74aa97ad63db83c4bf8ca490f02b2fc02f92575e720c8551e843c945f" +checksum = "588f6378e4dd99458b60ec275b4477add41ce4fa9f64dcba6f15adccb19b50d6" dependencies = [ - "rand 0.7.3", - "rand_core 0.5.1", + "rand 0.8.4", +] + +[[package]] +name = "quickcheck_macros" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b22a693222d716a9587786f37ac3f6b4faedb5b80c23914e7303ff5a1d8016e9" +dependencies = [ + "proc-macro2", + "quote", + "syn", ] [[package]] @@ -2702,7 +2712,9 @@ dependencies = [ "num-bigint-dig", "p256", "quickcheck", + "quickcheck_macros", "rand 0.7.3", + "rand 0.8.4", "rand_core 0.6.3", "regex", "regex-syntax", diff --git a/ipc/Cargo.toml b/ipc/Cargo.toml index 265592b8..f409943c 100644 --- a/ipc/Cargo.toml +++ b/ipc/Cargo.toml @@ -48,7 +48,7 @@ lalrpop = ">=0.17" [dev-dependencies] clap = "2.33" -quickcheck = { version = "0.9", default-features = false } +quickcheck = { version = "1", default-features = false } [lib] bench = false diff --git a/ipc/src/sexp.rs b/ipc/src/sexp.rs index a362232b..73a4fd1e 100644 --- a/ipc/src/sexp.rs +++ b/ipc/src/sexp.rs @@ -315,7 +315,7 @@ impl TryFrom<&mpi::Ciphertext> for Sexp { #[cfg(test)] impl Arbitrary for Sexp { - fn arbitrary<G: Gen>(g: &mut G) -> Self { + fn arbitrary(g: &mut Gen) -> Self { if f32::arbitrary(g) < 0.7 { Sexp::String(String_::arbitrary(g)) } else { @@ -417,7 +417,7 @@ impl Deref for String_ { #[cfg(test)] impl Arbitrary for String_ { - fn arbitrary<G: Gen>(g: &mut G) -> Self { + fn arbitrary(g: &mut Gen) -> Self { if bool::arbitrary(g) { Self::new(Vec::arbitrary(g).into_boxed_slice()) } else { diff --git a/openpgp/Cargo.toml b/openpgp/Cargo.toml index 7e8c0e07..29a553b2 100644 --- a/openpgp/Cargo.toml +++ b/openpgp/Cargo.toml @@ -86,8 +86,9 @@ rand = { version = "0.7", features = ["wasm-bindgen"] } lalrpop = ">=0.17" [dev-dependencies] -quickcheck = { version = "0.9", default-features = false } -rand = { version = "0.7", default-features = false } +quickcheck = { version = "1", default-features = false } +quickcheck_macros = { version = "1", default-features = false } +rand = { version = "0.8" } rpassword = "5.0" criterion = { version = "0.3.4", features = ["html_reports"] } diff --git a/openpgp/src/armor.rs b/openpgp/src/armor.rs index eb6bc65f..0f72181d 100644 --- a/openpgp/src/armor.rs +++ b/openpgp/src/armor.rs @@ -83,7 +83,7 @@ assert_send_and_sync!(Kind); #[cfg(test)] impl Arbitrary for Kind { - fn arbitrary<G: Gen>(g: &mut G) -> Self { + fn arbitrary(g: &mut Gen) -> Self { use self::Kind::*; match u8::arbitrary(g) % 5 { 0 => Message, diff --git a/openpgp/src/cert.rs b/openpgp/src/cert.rs index 8d125e00..770f9414 100644 --- a/openpgp/src/cert.rs +++ b/openpgp/src/cert.rs @@ -4389,8 +4389,8 @@ mod test { #[test] fn set_validity_period_two_uids() -> Result<()> { - use quickcheck::{Arbitrary, StdThreadGen}; - let mut gen = StdThreadGen::new(16); + use quickcheck::{Arbitrary, Gen}; + let mut gen = Gen::new(16); let p = &P::new(); let userid1 = UserID::arbitrary(&mut gen); diff --git a/openpgp/src/crypto/mpi.rs b/openpgp/src/crypto/mpi.rs index ecff49ac..c586e3fa 100644 --- a/openpgp/src/crypto/mpi.rs +++ b/openpgp/src/crypto/mpi.rs @@ -22,8 +22,6 @@ use std::borrow::Cow; #[cfg(test)] use quickcheck::{Arbitrary, Gen}; -#[cfg(test)] -use rand::Rng; use crate::types::{ Curve, @@ -259,7 +257,7 @@ impl Hash for MPI { #[cfg(test)] impl Arbitrary for MPI { - fn arbitrary<G: Gen>(g: &mut G) -> Self { + fn arbitrary(g: &mut Gen) -> Self { loop { let buf = <Vec<u8>>::arbitrary(g); @@ -580,9 +578,11 @@ impl Hash for PublicKey { #[cfg(test)] impl Arbitrary for PublicKey { - fn arbitrary<G: Gen>(g: &mut G) -> Self { + fn arbitrary(g: &mut Gen) -> Self { use self::PublicKey::*; - match g.gen_range(0, 6) { + use crate::arbitrary_helper::gen_arbitrary_from_range; + + match gen_arbitrary_from_range(0..6, g) { 0 => RSA { e: MPI::arbitrary(g), n: MPI::arbitrary(g), @@ -842,8 +842,10 @@ impl Hash for SecretKeyMaterial { #[cfg(test)] impl Arbitrary for SecretKeyMaterial { - fn arbitrary<G: Gen>(g: &mut G) -> Self { - match g.gen_range(0, 6) { + fn arbitrary(g: &mut Gen) -> Self { + use crate::arbitrary_helper::gen_arbitrary_from_range; + + match gen_arbitrary_from_range(0..6, g) { 0 => SecretKeyMaterial::RSA { d: MPI::arbitrary(g).into(), p: MPI::arbitrary(g).into(), @@ -969,8 +971,10 @@ impl Hash for Ciphertext { #[cfg(test)] impl Arbitrary for Ciphertext { - fn arbitrary<G: Gen>(g: &mut G) -> Self { - match g.gen_range(0, 3) { + fn arbitrary(g: &mut Gen) -> Self { + use crate::arbitrary_helper::gen_arbitrary_from_range; + + match gen_arbitrary_from_range(0..3, g) { 0 => Ciphertext::RSA { c: MPI::arbitrary(g), }, @@ -1062,8 +1066,10 @@ impl Hash for Signature { #[cfg(test)] impl Arbitrary for Signature { - fn arbitrary<G: Gen>(g: &mut G) -> Self { - match g.gen_range(0, 4) { + fn arbitrary(g: &mut Gen) -> Self { + use crate::arbitrary_helper::gen_arbitrary_from_range; + + match gen_arbitrary_from_range(0..4, g) { 0 => Signature::RSA { s: MPI::arbitrary(g), }, diff --git a/openpgp/src/crypto/s2k.rs b/openpgp/src/crypto/s2k.rs index 6e618e0c..0644816c 100644 --- a/openpgp/src/crypto/s2k.rs +++ b/openpgp/src/crypto/s2k.rs @@ -17,8 +17,6 @@ use std::fmt; #[cfg(test)] use quickcheck::{Arbitrary, Gen}; -#[cfg(test)] -use rand::Rng; /// String-to-Key (S2K) specifiers. /// @@ -366,21 +364,23 @@ impl fmt::Display for S2K { #[cfg(test)] impl Arbitrary for S2K { - fn arbitrary<G: Gen>(g: &mut G) -> Self { + fn arbitrary(g: &mut Gen) -> Self { + use crate::arbitrary_helper::gen_arbitrary_from_range; + #[allow(deprecated)] - match g.gen_range(0, 7) { + match gen_arbitrary_from_range(0..7, g) { 0 => S2K::Simple{ hash: HashAlgorithm::arbitrary(g) }, 1 => S2K::Salted{ hash: HashAlgorithm::arbitrary(g), - salt: g.gen(), + salt: [<u8>::arbitrary(g); 8], }, 2 => S2K::Iterated{ hash: HashAlgorithm::arbitrary(g), - salt: g.gen(), - hash_bytes: S2K::nearest_hash_count(g.gen()), + salt: [<u8>::arbitrary(g); 8], + hash_bytes: S2K::nearest_hash_count(Arbitrary::arbitrary(g)), }, 3 => S2K::Private { - tag: g.gen_range(100, 111), + tag: gen_arbitrary_from_range(100..111, g), parameters: Option::<Vec<u8>>::arbitrary(g).map(|v| v.into()), }, 4 => S2K::Unknown { @@ -388,11 +388,11 @@ impl Arbitrary for S2K { parameters: Option::<Vec<u8>>::arbitrary(g).map(|v| v.into()), }, 5 => S2K::Unknown { - tag: g.gen_range(4, 100), + tag: gen_arbitrary_from_range(4..100, g), parameters: Option::<Vec<u8>>::arbitrary(g).map(|v| v.into()), }, 6 => S2K::Unknown { - tag: g.gen_range(111, 256) as u8, + tag: gen_arbitrary_from_range(111..256, g) as u8, parameters: Option::<Vec<u8>>::arbitrary(g).map(|v| v.into()), }, _ => unreachable!(), diff --git a/openpgp/src/fingerprint.rs b/openpgp/src/fingerprint.rs index d60f3faa..cdb37ca2 100644 --- a/openpgp/src/fingerprint.rs +++ b/openpgp/src/fingerprint.rs @@ -345,10 +345,9 @@ impl Fingerprint { #[cfg(test)] impl Arbitrary for Fingerprint { - fn arbitrary<G: Gen>(g: &mut G) -> Self { - use rand::Rng; + fn arbitrary(g: &mut Gen) -> Self { let mut fp = [0; 20]; - fp.iter_mut().for_each(|p| *p = g.gen()); + fp.iter_mut().for_each(|p| *p = Arbitrary::arbitrary(g)); Fingerprint::V4(fp) } } diff --git a/openpgp/src/keyid.rs b/openpgp/src/keyid.rs index 6186d208..b4689488 100644 --- a/openpgp/src/keyid.rs +++ b/openpgp/src/keyid.rs @@ -400,7 +400,7 @@ impl KeyID { #[cfg(test)] impl Arbitrary for KeyID { - fn arbitrary<G: Gen>(g: &mut G) -> Self { + fn arbitrary(g: &mut Gen) -> Self { KeyID::new(u64::arbitrary(g)) } } diff --git a/openpgp/src/lib.rs b/openpgp/src/lib.rs index a8280d75..4cc96884 100644 --- a/openpgp/src/lib.rs +++ b/openpgp/src/lib.rs @@ -329,3 +329,36 @@ pub enum Error { } assert_send_and_sync!(Error); + +/// Provide a helper function that generates an arbitrary value from a given +/// range. Quickcheck > 1 does not re-export rand so we need to implement this +/// ourselves. +#[cfg(test)] +mod arbitrary_helper { + use quickcheck::{Arbitrary, Gen}; + + pub(crate) fn gen_arbitrary_from_range<T>( + range: std::ops::Range<T>, + g: &mut Gen, + ) -> T + where + T: Arbitrary + + std::cmp::PartialOrd + + std::ops::Sub<Output = T> + + std::ops::Rem<Output = T> + + std::ops::Add<Output = T> + + Copy, + { + if !range.is_empty() { + let m = range.end - range.start; + // The % operator calculates the remainder, which is negative for + // negative inputs, not the modulus. This actually calculates the + // modulus by making sure the result is positive. The primitive + // integer types implement .rem_euclid for that, but there is no way + // to constrain this function to primitive types. + range.start + (T::arbitrary(g) % m + m) % m + } else { + panic!() + } + } +} diff --git a/openpgp/src/packet/compressed_data.rs b/openpgp/src/packet/compressed_data.rs index 76ec27ee..d0564a11 100644 --- a/openpgp/src/packet/compressed_data.rs +++ b/openpgp/src/packet/compressed_data.rs @@ -99,11 +99,13 @@ impl From<CompressedData> for Packet { #[cfg(test)] impl Arbitrary for CompressedData { - fn arbitrary<G: Gen>(g: &mut G) -> Self { - use rand::Rng; + fn arbitrary(g: &mut Gen) -> Self { use crate::serialize::SerializeInto; + use crate::arbitrary_helper::gen_arbitrary_from_range; + loop { - let a = CompressionAlgorithm::from(g.gen_range(0, 4)); + let a = + CompressionAlgorithm::from(gen_arbitrary_from_range(0..4, g)); if a.is_supported() { let mut c = CompressedData::new(a); // We arbitrarily chose to create packets with diff --git a/openpgp/src/packet/key.rs b/openpgp/src/packet/key.rs index 405baa13..4ddb1aeb 100644 --- a/openpgp/src/packet/key.rs +++ b/openpgp/src/packet/key.rs @@ -1613,28 +1613,28 @@ impl<P, R> Arbitrary for super::Key<P, R> R: KeyRole, R: Clone, Key4<P, R>: Arbitrary, { - fn arbitrary<G: Gen>(g: &mut G) -> Self { + fn arbitrary(g: &mut Gen) -> Self { Key4::arbitrary(g).into() } } #[cfg(test)] impl Arbitrary for Key4<PublicParts, PrimaryRole> { - fn arbitrary<G: Gen>(g: &mut G) -> Self { + fn arbitrary(g: &mut Gen) -> Self { Key4::<PublicParts, UnspecifiedRole>::arbitrary(g).into() } } #[cfg(test)] impl Arbitrary for Key4<PublicParts, SubordinateRole> { - fn arbitrary<G: Gen>(g: &mut G) -> Self { + fn arbitrary(g: &mut Gen) -> Self { Key4::<PublicParts, UnspecifiedRole>::arbitrary(g).into() } } #[cfg(test)] impl Arbitrary for Key4<PublicParts, UnspecifiedRole> { - fn arbitrary<G: Gen>(g: &mut G) -> Self { + fn arbitrary(g: &mut Gen) -> Self { let mpis = mpi::PublicKey::arbitrary(g); Key4 { common: Arbitrary::arbitrary(g), @@ -1651,22 +1651,21 @@ impl Arbitrary for Key4<PublicParts, UnspecifiedRole> { #[cfg(test)] impl Arbitrary for Key4<SecretParts, PrimaryRole> { - fn arbitrary<G: Gen>(g: &mut G) -> Self { + fn arbitrary(g: &mut Gen) -> Self { Key4::<SecretParts, UnspecifiedRole>::arbitrary(g).into() } } #[cfg(test)] impl Arbitrary for Key4<SecretParts, SubordinateRole> { - fn arbitrary<G: Gen>(g: &mut G) -> Self { + fn arbitrary(g: &mut Gen) -> Self { Key4::<SecretParts, UnspecifiedRole>::arbitrary(g).into() } } #[cfg(test)] impl Arbitrary for Key4<SecretParts, UnspecifiedRole> { - fn arbitrary<G: Gen>(g: &mut G) -> Self { - use rand::Rng; + fn arbitrary(g: &mut Gen) -> Self { use PublicKeyAlgorithm::*; use mpi::MPI; @@ -1702,7 +1701,7 @@ impl Arbitrary for Key4<SecretParts, UnspecifiedRole> { _ => unreachable!("only valid algos, normalizes to these values"), }.into(); - if g.gen() { + if <bool>::arbitrary(g) { secret.encrypt_in_place(&Password::from(Vec::arbitrary(g))) .unwrap(); } @@ -1747,7 +1746,7 @@ mod tests { #[test] fn key_encrypt_decrypt() -> Result<()> { - let mut g = quickcheck::StdThreadGen::new(256); + let mut g = quickcheck::Gen::new(256); let p: Password = Vec::<u8>::arbitrary(&mut g).into(); let check = |key: Key4<SecretParts, UnspecifiedRole>| -> Result<()> { diff --git a/openpgp/src/packet/literal.rs b/openpgp/src/packet/literal.rs index f534f51c..3cc6150e 100644 --- a/openpgp/src/packet/literal.rs +++ b/openpgp/src/packet/literal.rs @@ -176,7 +176,7 @@ impl From<Literal> for Packet { #[cfg(test)] impl Arbitrary for Literal { - fn arbitrary<G: Gen>(g: &mut G) -> Self { + fn arbitrary(g: &mut Gen) -> Self { let mut l = Literal::new(DataFormat::arbitrary(g)); l.set_body(Vec::<u8>::arbitrary(g)); while let Err(_) = l.set_filename(&Vec::<u8>::arbitrary(g)) { diff --git a/openpgp/src/packet/marker.rs b/openpgp/src/packet/marker.rs index edeb03ae..f32d385c 100644 --- a/openpgp/src/packet/marker.rs +++ b/openpgp/src/packet/marker.rs @@ -30,7 +30,7 @@ impl From<Marker> for Packet { #[cfg(test)] impl Arbitrary for Marker { - fn arbitrary<G: Gen>(_: &mut G) -> Self { + fn arbitrary(_: &mut Gen) -> Self { Self::default() } } diff --git a/openpgp/src/packet/mod.rs b/openpgp/src/packet/mod.rs index 4e3b344c..e33ee469 100644 --- a/openpgp/src/packet/mod.rs +++ b/openpgp/src/packet/mod.rs @@ -508,9 +508,10 @@ impl fmt::Debug for Packet { #[cfg(test)] impl Arbitrary for Packet { - fn arbitrary<G: Gen>(g: &mut G) -> Self { - use rand::Rng; - match g.gen_range(0, 15) { + fn arbitrary(g: &mut Gen) -> Self { + use crate::arbitrary_helper::gen_arbitrary_from_range; + + match gen_arbitrary_from_range(0..15, g) { 0 => Signature::arbitrary(g).into(), 1 => OnePassSig::arbitrary(g).into(), 2 => Key::<key::PublicParts, key::UnspecifiedRole>::arbitrary(g) @@ -573,7 +574,7 @@ assert_send_and_sync!(Common); #[cfg(test)] impl Arbitrary for Common { - fn arbitrary<G: Gen>(_: &mut G) -> Self { + fn arbitrary(_: &mut Gen) -> Self { // XXX: Change if this gets interesting fields. Common::default() } diff --git a/openpgp/src/packet/one_pass_sig.rs b/openpgp/src/packet/one_pass_sig.rs index 41903901..68baf4f2 100644 --- a/openpgp/src/packet/one_pass_sig.rs +++ b/openpgp/src/packet/one_pass_sig.rs @@ -171,14 +171,14 @@ impl<'a> std::convert::TryFrom<&'a Signature> for OnePassSig3 { #[cfg(test)] impl Arbitrary for super::OnePassSig { - fn arbitrary<G: Gen>(g: &mut G) -> Self { + fn arbitrary(g: &mut Gen) -> Self { OnePassSig3::arbitrary(g).into() } } #[cfg(test)] impl Arbitrary for OnePassSig3 { - fn arbitrary<G: Gen>(g: &mut G) -> Self { + fn arbitrary(g: &mut Gen) -> Self { let mut ops = OnePassSig3::new(SignatureType::arbitrary(g)); ops.set_hash_algo(HashAlgorithm::arbitrary(g)); ops.set_pk_algo(PublicKeyAlgorithm::arbitrary(g)); diff --git a/openpgp/src/packet/pkesk.rs b/openpgp/src/packet/pkesk.rs index 50ec187a..6cbc8886 100644 --- a/openpgp/src/packet/pkesk.rs +++ b/openpgp/src/packet/pkesk.rs @@ -195,14 +195,14 @@ impl From<PKESK3> for Packet { #[cfg(test)] impl Arbitrary for super::PKESK { - fn arbitrary<G: Gen>(g: &mut G) -> Self { + fn arbitrary(g: &mut Gen) -> Self { PKESK3::arbitrary(g).into() } } #[cfg(test)] impl Arbitrary for PKESK3 { - fn arbitrary<G: Gen>(g: &mut G) -> Self { + fn arbitrary(g: &mut Gen) -> Self { let (ciphertext, pk_algo) = loop { let ciphertext = Ciphertext::arbitrary(g); if let Some(pk_algo) = ciphertext.pk_algo() { diff --git a/openpgp/src/packet/signature.rs b/openpgp/src/packet/signature.rs index 81ec439f..0b8cde02 100644 --- a/openpgp/src/packet/signature.rs< |