diff options
author | Nora Widdecke <nora@sequoia-pgp.org> | 2021-07-26 12:28:29 +0200 |
---|---|---|
committer | Nora Widdecke <nora@sequoia-pgp.org> | 2021-12-13 19:01:14 +0100 |
commit | 321b0cc381f3c44f81cfc4a9cf502be169262d14 (patch) | |
tree | 20c5ee6c7a551872a5037d89331b642e6190f4a8 /openpgp/src | |
parent | a79a35952cb2cf92bd6ba60fa7df057fb2eae1d2 (diff) |
ipc, openpgp: Bump quickcheck to 1.0.3.
- Adapt to the new API:
- Gen is now a struct, not a Trait, and replaces StdThreadGen.
- The rand re-export has been removed. As a consequence, we need
our own function to generate an arbitrary value from a range.
Diffstat (limited to 'openpgp/src')
27 files changed, 154 insertions, 114 deletions
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 +++ b/openpgp/src/packet/signature.rs @@ -154,7 +154,7 @@ use crate::packet::signature::subpacket::{ trait ArbitraryBounded { /// Generates an arbitrary value, but only recurses if `depth > /// 0`. - fn arbitrary_bounded<G: Gen>(g: &mut G, depth: usize) -> Self; + fn arbitrary_bounded(g: &mut Gen, depth: usize) -> Self; } #[cfg(test)] @@ -165,7 +165,7 @@ const DEFAULT_ARBITRARY_DEPTH: usize = 2; macro_rules! impl_arbitrary_with_bound { ($typ:path) => { impl Arbitrary for $typ { - fn arbitrary<G: Gen>(g: &mut G) -> Self { + fn arbitrary(g: &mut Gen) -> Self { Self::arbitrary_bounded( g, crate::packet::signature::DEFAULT_ARBITRARY_DEPTH) @@ -224,7 +224,7 @@ assert_send_and_sync!(SignatureFields); #[cfg(test)] impl ArbitraryBounded for SignatureFields { - fn arbitrary_bounded<G: Gen>(g: &mut G, depth: usize) -> Self { + fn arbitrary_bounded(g: &mut Gen, depth: usize) -> Self { SignatureFields { // XXX: Make this more interesting once we dig other // versions. @@ -3084,7 +3084,7 @@ impl From<Signature4> for super::Signature { #[cfg(test)] impl ArbitraryBounded for super::Signature { - fn arbitrary_bounded<G: Gen>(g: &mut G, depth: usize) -> Self { + fn arbitrary_bounded(g: &mut Gen, depth: usize) -> Self { Signature4::arbitrary_bounded(g, depth).into() } } @@ -3094,7 +3094,7 @@ impl_arbitrary_with_bound!(super::Signature); #[cfg(test)] impl ArbitraryBounded for Signature4 { - fn arbitrary_bounded<G: Gen>(g: &mut G, depth: usize) -> Self { + fn arbitrary_bounded(g: &mut Gen, depth: usize) -> Self { use mpi::MPI; use PublicKeyAlgorithm::*; diff --git a/openpgp/src/packet/signature/subpacket.rs b/openpgp/src/packet/signature/subpacket.rs index 2d09615a..c56097de 100644 --- a/openpgp/src/packet/signature/subpacket.rs +++ b/openpgp/src/packet/signature/subpacket.rs @@ -420,7 +420,7 @@ impl From<SubpacketTag> for u8 { #[cfg(test)] impl Arbitrary for SubpacketTag { - fn arbitrary<G: Gen>(g: &mut G) -> Self { + fn arbitrary(g: &mut Gen) -> Self { u8::arbitrary(g).into() } } @@ -516,11 +516,11 @@ assert_send_and_sync!(SubpacketArea); #[cfg(test)] impl ArbitraryBounded for SubpacketArea { - fn arbitrary_bounded<G: Gen>(g: &mut G, depth: usize) -> Self { - use rand::Rng; + fn arbitrary_bounded(g: &mut Gen, depth: usize) -> Self { + use crate::arbitrary_helper::gen_arbitrary_from_range; let mut a = Self::default(); - for _ in 0..g.gen_range(0, 32) { + for _ in 0..gen_arbitrary_from_range(0..32, g) { let _ = a.add(ArbitraryBounded::arbitrary_bounded(g, depth)); } @@ -1147,7 +1147,7 @@ impl fmt::Debug for NotationData { #[cfg(test)] impl Arbitrary for NotationData { - fn arbitrary<G: Gen>(g: &mut G) -> Self { + fn arbitrary(g: &mut Gen) -> Self { NotationData { flags: Arbitrary::arbitrary(g), name: Arbitrary::arbitrary(g), @@ -1193,7 +1193,7 @@ assert_send_and_sync!(NotationDataFlags); #[cfg(test)] impl Arbitrary for NotationDataFlags { - fn arbitrary<G: Gen>(g: &mut G) -> Self { + fn arbitrary(g: &mut Gen) -> Self { NotationDataFlags(vec![u8::arbitrary(g), u8::arbitrary(g), u8::arbitrary(g), u8::arbitrary(g)].into()) } @@ -1599,11 +1599,12 @@ assert_send_and_sync!(SubpacketValue); #[cfg(test)] impl ArbitraryBounded for SubpacketValue { - fn arbitrary_bounded<G: Gen>(g: &mut G, depth: usize) -> Self { - use rand::Rng; + fn arbitrary_bounded(g: &mut Gen, depth: usize) -> Self { use self::SubpacketValue::*; + use crate::arbitrary_helper::gen_arbitrary_from_range; + loop { - break match g.gen_range(0, 26) { + break match gen_arbitrary_from_range(0..2 |