From 321b0cc381f3c44f81cfc4a9cf502be169262d14 Mon Sep 17 00:00:00 2001 From: Nora Widdecke Date: Mon, 26 Jul 2021 12:28:29 +0200 Subject: 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. --- openpgp/src/armor.rs | 2 +- openpgp/src/cert.rs | 4 ++-- openpgp/src/crypto/mpi.rs | 28 +++++++++++++++----------- openpgp/src/crypto/s2k.rs | 20 +++++++++---------- openpgp/src/fingerprint.rs | 5 ++--- openpgp/src/keyid.rs | 2 +- openpgp/src/lib.rs | 33 +++++++++++++++++++++++++++++++ openpgp/src/packet/compressed_data.rs | 8 +++++--- openpgp/src/packet/key.rs | 19 +++++++++--------- openpgp/src/packet/literal.rs | 2 +- openpgp/src/packet/marker.rs | 2 +- openpgp/src/packet/mod.rs | 9 +++++---- openpgp/src/packet/one_pass_sig.rs | 4 ++-- openpgp/src/packet/pkesk.rs | 4 ++-- openpgp/src/packet/signature.rs | 10 +++++----- openpgp/src/packet/signature/subpacket.rs | 30 +++++++++++++++------------- openpgp/src/packet/skesk.rs | 6 +++--- openpgp/src/packet/tag.rs | 2 +- openpgp/src/packet/trust.rs | 2 +- openpgp/src/packet/user_attribute.rs | 29 ++++++++++++++++----------- openpgp/src/packet/userid.rs | 2 +- openpgp/src/types/features.rs | 2 +- openpgp/src/types/key_flags.rs | 2 +- openpgp/src/types/mod.rs | 33 ++++++++++++------------------- openpgp/src/types/revocation_key.rs | 2 +- openpgp/src/types/server_preferences.rs | 2 +- openpgp/src/types/timestamp.rs | 4 ++-- 27 files changed, 154 insertions(+), 114 deletions(-) (limited to 'openpgp/src') 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: &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: &mut G) -> Self { + fn arbitrary(g: &mut Gen) -> Self { loop { let buf = >::arbitrary(g); @@ -580,9 +578,11 @@ impl Hash for PublicKey { #[cfg(test)] impl Arbitrary for PublicKey { - fn arbitrary(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: &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: &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: &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: &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: [::arbitrary(g); 8], }, 2 => S2K::Iterated{ hash: HashAlgorithm::arbitrary(g), - salt: g.gen(), - hash_bytes: S2K::nearest_hash_count(g.gen()), + salt: [::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::>::arbitrary(g).map(|v| v.into()), }, 4 => S2K::Unknown { @@ -388,11 +388,11 @@ impl Arbitrary for S2K { parameters: Option::>::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::>::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::>::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: &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: &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( + range: std::ops::Range, + g: &mut Gen, + ) -> T + where + T: Arbitrary + + std::cmp::PartialOrd + + std::ops::Sub + + std::ops::Rem + + std::ops::Add + + 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 for Packet { #[cfg(test)] impl Arbitrary for CompressedData { - fn arbitrary(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 Arbitrary for super::Key R: KeyRole, R: Clone, Key4: Arbitrary, { - fn arbitrary(g: &mut G) -> Self { + fn arbitrary(g: &mut Gen) -> Self { Key4::arbitrary(g).into() } } #[cfg(test)] impl Arbitrary for Key4 { - fn arbitrary(g: &mut G) -> Self { + fn arbitrary(g: &mut Gen) -> Self { Key4::::arbitrary(g).into() } } #[cfg(test)] impl Arbitrary for Key4 { - fn arbitrary(g: &mut G) -> Self { + fn arbitrary(g: &mut Gen) -> Self { Key4::::arbitrary(g).into() } } #[cfg(test)] impl Arbitrary for Key4 { - fn arbitrary(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 { #[cfg(test)] impl Arbitrary for Key4 { - fn arbitrary(g: &mut G) -> Self { + fn arbitrary(g: &mut Gen) -> Self { Key4::::arbitrary(g).into() } } #[cfg(test)] impl Arbitrary for Key4 { - fn arbitrary(g: &mut G) -> Self { + fn arbitrary(g: &mut Gen) -> Self { Key4::::arbitrary(g).into() } } #[cfg(test)] impl Arbitrary for Key4 { - fn arbitrary(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 { _ => unreachable!("only valid algos, normalizes to these values"), }.into(); - if g.gen() { + if ::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::::arbitrary(&mut g).into(); let check = |key: Key4| -> 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 for Packet { #[cfg(test)] impl Arbitrary for Literal { - fn arbitrary(g: &mut G) -> Self { + fn arbitrary(g: &mut Gen) -> Self { let mut l = Literal::new(DataFormat::arbitrary(g)); l.set_body(Vec::::arbitrary(g)); while let Err(_) = l.set_filename(&Vec::::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 for Packet { #[cfg(test)] impl Arbitrary for Marker { - fn arbitrary(_: &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: &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::::arbitrary(g) @@ -573,7 +574,7 @@ assert_send_and_sync!(Common); #[cfg(test)] impl Arbitrary for Common { - fn arbitrary(_: &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: &mut G) -> Self { + fn arbitrary(g: &mut Gen) -> Self { OnePassSig3::arbitrary(g).into() } } #[cfg(test)] impl Arbitrary for OnePassSig3 { - fn arbitrary(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 for Packet { #[cfg(test)] impl Arbitrary for super::PKESK { - fn arbitrary(g: &mut G) -> Self { + fn arbitrary(g: &mut Gen) -> Self { PKESK3::arbitrary(g).into() } } #[cfg(test)] impl Arbitrary for PKESK3 { - fn arbitrary(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: &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: &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: &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 for super::Signature { #[cfg(test)] impl ArbitraryBounded for super::Signature { - fn arbitrary_bounded(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: &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 for u8 { #[cfg(test)] impl Arbitrary for SubpacketTag { - fn arbitrary(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: &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: &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: &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: &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..26, g) { 0 => SignatureCreationTime(Arbitrary::arbitrary(g)), 1 => SignatureExpirationTime(Arbitrary::arbitrary(g)), 2 => ExportableCertification(Arbitrary::arbitrary(g)), @@ -1786,8 +1787,8 @@ impl Hash for Subpacket { #[cfg(test)] impl ArbitraryBounded for Subpacket { - fn arbitrary_bounded(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; fn encode_non_optimal(length: usize) -> SubpacketLength { // Calculate length the same way as Subpacket::new. @@ -1802,12 +1803,13 @@ impl ArbitraryBounded for Subpacket { let critical = ::arbitrary(g); let use_nonoptimal_encoding = ::arbitrary(g); // We don't want to overrepresent large subpackets. - let create_large_subpacket = g.gen_range(0, 25) == 0; + let create_large_subpacket = + gen_arbitrary_from_range(0..25, g) == 0; let value = if create_large_subpacket { // Choose a size which makes sure the subpacket length must be // encoded with 2 or 5 octets. - let value_size = g.gen_range(7000, 9000); + let value_size = gen_arbitrary_from_range(7000..9000, g); let nd = NotationData { flags: Arbitrary::arbitrary(g), name: Arbitrary::arbitrary(g), @@ -2039,7 +2041,7 @@ assert_send_and_sync!(SubpacketAreas); #[cfg(test)] impl ArbitraryBounded for SubpacketAreas { - fn arbitrary_bounded(g: &mut G, depth: usize) -> Self { + fn arbitrary_bounded(g: &mut Gen, depth: usize) -> Self { SubpacketAreas::new(ArbitraryBounded::arbitrary_bounded(g, depth), ArbitraryBounded::arbitrary_bounded(g, depth)) } diff --git a/openpgp/src/packet/skesk.rs b/openpgp/src/packet/skesk.rs index d42b7d60..1c2cfd05 100644 --- a/openpgp/src/packet/skesk.rs +++ b/openpgp/src/packet/skesk.rs @@ -38,7 +38,7 @@ impl SKESK { #[cfg(test)] impl Arbitrary for SKESK { - fn arbitrary(g: &mut G) -> Self { + fn arbitrary(g: &mut Gen) -> Self { if bool::arbitrary(g) { SKESK::V4(SKESK4::arbitrary(g)) } else { @@ -299,7 +299,7 @@ impl From for Packet { #[cfg(test)] impl Arbitrary for SKESK4 { - fn arbitrary(g: &mut G) -> Self { + fn arbitrary(g: &mut Gen) -> Self { SKESK4::new(SymmetricAlgorithm::arbitrary(g), S2K::arbitrary(g), Option::>::arbitrary(g).map(|v| v.into())) @@ -579,7 +579,7 @@ impl From for Packet { #[cfg(test)] impl Arbitrary for SKESK5 { - fn arbitrary(g: &mut G) -> Self { + fn arbitrary(g: &mut Gen) -> Self { let algo = AEADAlgorithm::EAX; // The only one we dig. let mut iv = vec![0u8; algo.iv_size().unwrap()]; for b in iv.iter_mut() { diff --git a/openpgp/src/packet/tag.rs b/openpgp/src/packet/tag.rs index 08021cc3..a1fc9768 100644 --- a/openpgp/src/packet/tag.rs +++ b/openpgp/src/packet/tag.rs @@ -219,7 +219,7 @@ impl fmt::Display for Tag { #[cfg(test)] impl Arbitrary for Tag { - fn arbitrary(g: &mut G) -> Self { + fn arbitrary(g: &mut Gen) -> Self { loop { match u8::arbitrary(g) { n @ 0..=63 => break n.into(), diff --git a/openpgp/src/packet/trust.rs b/openpgp/src/packet/trust.rs index ff4bd351..c444b446 100644 --- a/openpgp/src/packet/trust.rs +++ b/openpgp/src/packet/trust.rs @@ -64,7 +64,7 @@ impl From for Packet { #[cfg(test)] impl Arbitrary for Trust { - fn arbitrary(g: &mut G) -> Self { + fn arbitrary(g: &mut Gen) -> Self { Vec::::arbitrary(g).into() } } diff --git a/openpgp/src/packet/user_attribute.rs b/openpgp/src/packet/user_attribute.rs index 3769bc6b..810ae30f 100644 --- a/openpgp/src/packet/user_attribute.rs +++ b/openpgp/src/packet/user_attribute.rs @@ -8,8 +8,6 @@ use std::fmt; #[cfg(test)] use quickcheck::{Arbitrary, Gen}; -#[cfg(test)] -use rand::Rng; use buffered_reader::BufferedReader; @@ -136,9 +134,11 @@ impl From for Packet { #[cfg(test)] impl Arbitrary for UserAttribute { - fn arbitrary(g: &mut G) -> Self { + fn arbitrary(g: &mut Gen) -> Self { + use crate::arbitrary_helper::gen_arbitrary_from_range; + UserAttribute::new( - &(0..g.gen_range(1, 10)) + &(0..gen_arbitrary_from_range(1..10, g)) .map(|_| Subpacket::arbitrary(g)) .collect::>()[..]).unwrap() } @@ -227,15 +227,17 @@ assert_send_and_sync!(Subpacket); #[cfg(test)] impl Arbitrary for Subpacket { - fn arbitrary(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 => Subpacket::Image(Image::arbitrary(g)), 1 => Subpacket::Unknown( 0, Vec::::arbitrary(g).into_boxed_slice() ), 2 => Subpacket::Unknown( - g.gen_range(2, 256) as u8, + gen_arbitrary_from_range(2..256, g) as u8, Vec::::arbitrary(g).into_boxed_slice() ), _ => unreachable!(), @@ -261,20 +263,23 @@ assert_send_and_sync!(Image); #[cfg(test)] impl Arbitrary for Image { - fn arbitrary(g: &mut G) -> Self { - match g.gen_range(0, 5) { + + fn arbitrary(g: &mut Gen) -> Self { + use crate::arbitrary_helper::gen_arbitrary_from_range; + + match gen_arbitrary_from_range(0..5, g) { 0 => Image::JPEG( Vec::::arbitrary(g).into_boxed_slice() ), 1 => Image::Unknown( - g.gen_range(2, 100), + gen_arbitrary_from_range(2..100, g), Vec::::arbitrary(g).into_boxed_slice() ), 2 => Image::Private( - g.gen_range(100, 111), + gen_arbitrary_from_range(100..111, g), Vec::::arbitrary(g).into_boxed_slice() ), 3 => @@ -284,7 +289,7 @@ impl Arbitrary for Image { ), 4 => Image::Unknown( - g.gen_range(111, 256) as u8, + gen_arbitrary_from_range(111..256, g) as u8, Vec::::arbitrary(g).into_boxed_slice() ), _ => unreachable!(), diff --git a/openpgp/src/packet/userid.rs b/openpgp/src/packet/userid.rs index d6cc6852..b113a955 100644 --- a/openpgp/src/packet/userid.rs +++ b/openpgp/src/packet/userid.rs @@ -976,7 +976,7 @@ impl From for Packet { #[cfg(test)] impl Arbitrary for UserID { - fn arbitrary(g: &mut G) -> Self { + fn arbitrary(g: &mut Gen) -> Self { Vec::::arbitrary(g).into() } } diff --git a/openpgp/src/types/features.rs b/openpgp/src/types/features.rs index 907f002e..caac3cd2 100644 --- a/openpgp/src/types/features.rs +++ b/openpgp/src/types/features.rs @@ -358,7 +358,7 @@ const FEATURE_FLAG_AEAD: usize = 1; #[cfg(test)] impl Arbitrary for Features { - fn arbitrary(g: &mut G) -> Self { + fn arbitrary(g: &mut Gen) -> Self { Self::new(Vec::arbitrary(g)) } } diff --git a/openpgp/src/types/key_flags.rs b/openpgp/src/types/key_flags.rs index 50f96f6a..933df1ea 100644 --- a/openpgp/src/types/key_flags.rs +++ b/openpgp/src/types/key_flags.rs @@ -407,7 +407,7 @@ const KEY_FLAG_GROUP_KEY: usize = 7; #[cfg(test)] impl Arbitrary for KeyFlags { - fn arbitrary(g: &mut G) -> Self { + fn arbitrary(g: &mut Gen) -> Self { Self::new(Vec::arbitrary(g)) } } diff --git a/openpgp/src/types/mod.rs b/openpgp/src/types/mod.rs index 9eac14d2..ba50cccd 100644 --- a/openpgp/src/types/mod.rs +++ b/openpgp/src/types/mod.rs @@ -263,27 +263,20 @@ impl fmt::Display for PublicKeyAlgorithm { #[cfg(test)] impl Arbitrary for PublicKeyAlgorithm { - fn arbitrary(g: &mut G) -> Self { + fn arbitrary(g: &mut Gen) -> Self { u8::arbitrary(g).into() } } #[cfg(test)] impl PublicKeyAlgorithm { - pub(crate) fn arbitrary_for_signing(g: &mut G) -> Self { - use rand::Rng; + pub(crate) fn arbitrary_for_signing(g: &mut Gen) -> Self { use self::PublicKeyAlgorithm::*; + #[allow(deprecated)] - let a = match g.gen_range(0, 5) { - 0 => RSAEncryptSign, - 1 => RSASign, - 2 => DSA, - 3 => ECDSA, - 4 => EdDSA, - _ => unreachable!(), - }; + let a = g.choose(&[RSAEncryptSign, RSASign, DSA, ECDSA, EdDSA]).unwrap(); assert!(a.for_signing()); - a + *a } } @@ -489,7 +482,7 @@ impl Curve { #[cfg(test)] impl Arbitrary for Curve { - fn arbitrary(g: &mut G) -> Self { + fn arbitrary(g: &mut Gen) -> Self { match u8::arbitrary(g) % 8 { 0 => Curve::NistP256, 1 => Curve::NistP384, @@ -658,7 +651,7 @@ impl fmt::Display for SymmetricAlgorithm { #[cfg(test)] impl Arbitrary for SymmetricAlgorithm { - fn arbitrary(g: &mut G) -> Self { + fn arbitrary(g: &mut Gen) -> Self { u8::arbitrary(g).into() } } @@ -767,7 +760,7 @@ impl fmt::Display for AEADAlgorithm { #[cfg(test)] impl Arbitrary for AEADAlgorithm { - fn arbitrary(g: &mut G) -> Self { + fn arbitrary(g: &mut Gen) -> Self { u8::arbitrary(g).into() } } @@ -909,7 +902,7 @@ impl fmt::Display for CompressionAlgorithm { #[cfg(test)] impl Arbitrary for CompressionAlgorithm { - fn arbitrary(g: &mut G) -> Self { + fn arbitrary(g: &mut Gen) -> Self { u8::arbitrary(g).into() } } @@ -1079,7 +1072,7 @@ impl HashAlgorithm { #[cfg(test)] impl Arbitrary for HashAlgorithm { - fn arbitrary(g: &mut G) -> Self { + fn arbitrary(g: &mut Gen) -> Self { u8::arbitrary(g).into() } } @@ -1250,7 +1243,7 @@ impl fmt::Display for SignatureType { #[cfg(test)] impl Arbitrary for SignatureType { - fn arbitrary(g: &mut G) -> Self { + fn arbitrary(g: &mut Gen) -> Self { u8::arbitrary(g).into() } } @@ -1394,7 +1387,7 @@ impl fmt::Display for ReasonForRevocation { #[cfg(test)] impl Arbitrary for ReasonForRevocation { - fn arbitrary(g: &mut G) -> Self { + fn arbitrary(g: &mut Gen) -> Self { u8::arbitrary(g).into() } } @@ -1641,7 +1634,7 @@ impl fmt::Display for DataFormat { #[cfg(test)] impl Arbitrary for DataFormat { - fn arbitrary(g: &mut G) -> Self { + fn arbitrary(g: &mut Gen) -> Self { u8::arbitrary(g).into() } } diff --git a/openpgp/src/types/revocation_key.rs b/openpgp/src/types/revocation_key.rs index b1e759cf..2e41224b 100644 --- a/openpgp/src/types/revocation_key.rs +++ b/openpgp/src/types/revocation_key.rs @@ -151,7 +151,7 @@ const REVOCATION_KEY_MASK_UNKNOWN: u8 = ! (REVOCATION_KEY_FLAG_MUST_BE_SET #[cfg(test)] impl Arbitrary for RevocationKey { - fn arbitrary(g: &mut G) -> Self { + fn arbitrary(g: &mut Gen) -> Self { RevocationKey { pk_algo: Arbitrary::arbitrary(g), fp: Arbitrary::arbitrary(g), diff --git a/openpgp/src/types/server_preferences.rs b/openpgp/src/types/server_preferences.rs index 7adfb83a..347e66fe 100644 --- a/openpgp/src/types/server_preferences.rs +++ b/openpgp/src/types/server_preferences.rs @@ -276,7 +276,7 @@ const KEYSERVER_PREFERENCE_NO_MODIFY: usize = 7; #[cfg(test)] impl Arbitrary for KeyServerPreferences { - fn arbitrary(g: &mut G) -> Self { + fn arbitrary(g: &mut Gen) -> Self { Self::new(Vec::arbitrary(g)) } } diff --git a/openpgp/src/types/timestamp.rs b/openpgp/src/types/timestamp.rs index 284b6ec9..d56171f3 100644 --- a/openpgp/src/types/timestamp.rs +++ b/openpgp/src/types/timestamp.rs @@ -252,7 +252,7 @@ impl Timestamp { #[cfg(test)] impl Arbitrary for Timestamp { - fn arbitrary(g: &mut G) -> Self { + fn arbitrary(g: &mut Gen) -> Self { Timestamp(u32::arbitrary(g)) } } @@ -608,7 +608,7 @@ impl Timestamp { #[cfg(test)] impl Arbitrary for Duration { - fn arbitrary(g: &mut G) -> Self { + fn arbitrary(g: &mut Gen) -> Self { Duration(u32::arbitrary(g)) } } -- cgit v1.2.3