diff options
author | Nora Widdecke <nora@sequoia-pgp.org> | 2020-05-12 17:07:44 +0200 |
---|---|---|
committer | Nora Widdecke <nora@sequoia-pgp.org> | 2020-06-08 14:36:13 +0200 |
commit | c51d96b98f1ed92fe7bc6d964abb0d2a8616bae1 (patch) | |
tree | 9fb9ea374e712da619c0985b9ddc0f4377b0b749 | |
parent | 5be97d9c6ee82f068a5b38aa455f31497d3f20bf (diff) |
openpgp: Introduce feature flag for quickcheck.
- Make quickcheck dependency optional.
- Make quickcheck a dev-dependency for tests.
- Fix doctests for
- cert::ValidCert::user_attributes,
- cert::builder::CertBuilder::add_user_attribute,
- cert::revoke::UserAttributeRevocationBuilder
- cert::revoke::UserAttributeRevocationBuilder::build.
Doctests do not use cfg(test), so we cannot use quickcheck in there.
30 files changed, 147 insertions, 19 deletions
diff --git a/openpgp/Cargo.toml b/openpgp/Cargo.toml index 104cb6c5..0698a7bb 100644 --- a/openpgp/Cargo.toml +++ b/openpgp/Cargo.toml @@ -33,7 +33,7 @@ lazy_static = "1.3" libc = "0.2" memsec = "0.5.6" nettle = { version = "7", optional = true } -quickcheck = { version = "0.9", default-features = false } +quickcheck = { version = "0.9", default-features = false, optional = true } rand = { version = "0.7", default-features = false } regex = "1" thiserror = "1" @@ -44,6 +44,7 @@ lalrpop = "0.17" [dev-dependencies] rpassword = "=4.0.3" +quickcheck = { version = "0.9", default-features = false } [features] default = ["compression", "crypto-nettle"] diff --git a/openpgp/src/armor.rs b/openpgp/src/armor.rs index f7ece759..2425c804 100644 --- a/openpgp/src/armor.rs +++ b/openpgp/src/armor.rs @@ -34,6 +34,8 @@ use std::path::Path; use std::cmp; use std::str; use std::borrow::Cow; + +#[cfg(any(test, feature = "quickcheck"))] use quickcheck::{Arbitrary, Gen}; use crate::vec_truncate; @@ -68,6 +70,7 @@ pub enum Kind { File, } +#[cfg(any(test, feature = "quickcheck"))] impl Arbitrary for Kind { fn arbitrary<G: Gen>(g: &mut G) -> Self { use self::Kind::*; diff --git a/openpgp/src/cert/builder.rs b/openpgp/src/cert/builder.rs index 751abb55..4243cb2f 100644 --- a/openpgp/src/cert/builder.rs +++ b/openpgp/src/cert/builder.rs @@ -426,7 +426,7 @@ impl CertBuilder { /// primary User ID flag set: /// /// ``` - /// # use quickcheck::{Arbitrary, StdThreadGen}; + /// # use openpgp::packet::user_attribute::Subpacket; /// use sequoia_openpgp as openpgp; /// use openpgp::cert::prelude::*; /// use openpgp::packet::prelude::*; @@ -434,9 +434,11 @@ impl CertBuilder { /// /// # fn main() -> openpgp::Result<()> { /// let p = &StandardPolicy::new(); - /// - /// # let mut gen = StdThreadGen::new(16); - /// # let user_attribute : UserAttribute = UserAttribute::arbitrary(&mut gen); + /// # + /// # // Create some user attribute. Doctests do not pass cfg(test), + /// # // so UserAttribute::arbitrary is not available + /// # let sp = Subpacket::Unknown(7, vec![7; 7].into_boxed_slice()); + /// # let user_attribute = UserAttribute::new(&[sp])?; /// /// let (cert, rev) = /// CertBuilder::new() @@ -455,7 +457,7 @@ impl CertBuilder { /// set: /// /// ``` - /// # use quickcheck::{Arbitrary, StdThreadGen}; + /// # use openpgp::packet::user_attribute::Subpacket; /// use sequoia_openpgp as openpgp; /// use openpgp::cert::prelude::*; /// use openpgp::packet::prelude::*; @@ -463,9 +465,11 @@ impl CertBuilder { /// /// # fn main() -> openpgp::Result<()> { /// let p = &StandardPolicy::new(); - /// - /// # let mut gen = StdThreadGen::new(16); - /// # let user_attribute : UserAttribute = UserAttribute::arbitrary(&mut gen); + /// # + /// # // Create some user attribute. Doctests do not pass cfg(test), + /// # // so UserAttribute::arbitrary is not available + /// # let sp = Subpacket::Unknown(7, vec![7; 7].into_boxed_slice()); + /// # let user_attribute = UserAttribute::new(&[sp])?; /// /// let (cert, rev) = /// CertBuilder::new() diff --git a/openpgp/src/cert/mod.rs b/openpgp/src/cert/mod.rs index 7de9f286..36d54451 100644 --- a/openpgp/src/cert/mod.rs +++ b/openpgp/src/cert/mod.rs @@ -3041,10 +3041,10 @@ impl<'a> ValidCert<'a> { /// # Examples /// /// ``` - /// # use quickcheck::{Arbitrary, StdThreadGen}; /// use sequoia_openpgp as openpgp; /// # use openpgp::cert::prelude::*; /// # use openpgp::packet::prelude::*; + /// # use openpgp::packet::user_attribute::Subpacket; /// use openpgp::policy::StandardPolicy; /// /// # fn main() -> openpgp::Result<()> { @@ -3053,8 +3053,12 @@ impl<'a> ValidCert<'a> { /// # let (cert, _) = /// # CertBuilder::general_purpose(None, Some("alice@example.org")) /// # .generate()?; - /// # let mut gen = StdThreadGen::new(16); - /// # let ua : UserAttribute = UserAttribute::arbitrary(&mut gen); + /// # + /// # // Create some user attribute. Doctests do not pass cfg(test), + /// # // so UserAttribute::arbitrary is not available + /// # let sp = Subpacket::Unknown(7, vec![7; 7].into_boxed_slice()); + /// # let ua = UserAttribute::new(&[sp]); + /// # /// // Add a User Attribute without a self-signature to the certificate. /// let cert = cert.merge_packets(ua)?; /// assert_eq!(cert.user_attributes().count(), 1); diff --git a/openpgp/src/cert/revoke.rs b/openpgp/src/cert/revoke.rs index 50130510..ba46fd1b 100644 --- a/openpgp/src/cert/revoke.rs +++ b/openpgp/src/cert/revoke.rs @@ -750,7 +750,7 @@ impl Deref for UserIDRevocationBuilder { /// Revoke a User Attribute that is no longer valid: /// /// ```rust -/// # use quickcheck::{Arbitrary, StdThreadGen}; +/// # use openpgp::packet::user_attribute::Subpacket; /// use sequoia_openpgp as openpgp; /// # use openpgp::Result; /// use openpgp::cert::prelude::*; @@ -763,8 +763,10 @@ impl Deref for UserIDRevocationBuilder { /// # fn main() -> Result<()> { /// let p = &StandardPolicy::new(); /// -/// # let mut gen = StdThreadGen::new(16); -/// # let user_attribute : UserAttribute = UserAttribute::arbitrary(&mut gen); +/// # // Create some user attribute. Doctests do not pass cfg(test), +/// # // so UserAttribute::arbitrary is not available +/// # let sp = Subpacket::Unknown(7, vec![7; 7].into_boxed_slice()); +/// # let user_attribute = UserAttribute::new(&[sp])?; /// # /// # let (cert, _) = CertBuilder::new() /// # .add_user_attribute(user_attribute) @@ -905,7 +907,7 @@ impl UserAttributeRevocationBuilder { /// valid: /// /// ```rust - /// # use quickcheck::{Arbitrary, StdThreadGen}; + /// # use openpgp::packet::user_attribute::Subpacket; /// use sequoia_openpgp as openpgp; /// # use openpgp::Result; /// use openpgp::cert::prelude::*; @@ -918,8 +920,10 @@ impl UserAttributeRevocationBuilder { /// # fn main() -> Result<()> { /// let p = &StandardPolicy::new(); /// - /// # let mut gen = StdThreadGen::new(16); - /// # let user_attribute : UserAttribute = UserAttribute::arbitrary(&mut gen); + /// # // Create some user attribute. Doctests do not pass cfg(test), + /// # // so UserAttribute::arbitrary is not available + /// # let sp = Subpacket::Unknown(7, vec![7; 7].into_boxed_slice()); + /// # let user_attribute = UserAttribute::new(&[sp])?; /// # /// # let (cert, _) = CertBuilder::new() /// # .add_user_attribute(user_attribute) diff --git a/openpgp/src/crypto/mpi.rs b/openpgp/src/crypto/mpi.rs index 13bc0007..1dd2cd30 100644 --- a/openpgp/src/crypto/mpi.rs +++ b/openpgp/src/crypto/mpi.rs @@ -3,7 +3,9 @@ use std::fmt; use std::cmp::Ordering; +#[cfg(any(test, feature = "quickcheck"))] use quickcheck::{Arbitrary, Gen}; +#[cfg(any(test, feature = "quickcheck"))] use rand::Rng; use crate::types::{ @@ -184,6 +186,7 @@ impl Hash for MPI { } } +#[cfg(any(test, feature = "quickcheck"))] impl Arbitrary for MPI { fn arbitrary<G: Gen>(g: &mut G) -> Self { loop { @@ -409,6 +412,7 @@ impl Hash for PublicKey { } } +#[cfg(any(test, feature = "quickcheck"))] impl Arbitrary for PublicKey { fn arbitrary<G: Gen>(g: &mut G) -> Self { use self::PublicKey::*; @@ -678,6 +682,7 @@ impl Hash for SecretKeyMaterial { } } +#[cfg(any(test, feature = "quickcheck"))] impl Arbitrary for SecretKeyMaterial { fn arbitrary<G: Gen>(g: &mut G) -> Self { match g.gen_range(0, 6) { @@ -780,6 +785,7 @@ impl Hash for Ciphertext { } } +#[cfg(any(test, feature = "quickcheck"))] impl Arbitrary for Ciphertext { fn arbitrary<G: Gen>(g: &mut G) -> Self { match g.gen_range(0, 3) { @@ -865,6 +871,7 @@ impl Hash for Signature { } } +#[cfg(any(test, feature = "quickcheck"))] impl Arbitrary for Signature { fn arbitrary<G: Gen>(g: &mut G) -> Self { match g.gen_range(0, 4) { diff --git a/openpgp/src/crypto/s2k.rs b/openpgp/src/crypto/s2k.rs index 8dff4693..d5553ce4 100644 --- a/openpgp/src/crypto/s2k.rs +++ b/openpgp/src/crypto/s2k.rs @@ -14,7 +14,9 @@ use crate::crypto::SessionKey; use std::fmt; +#[cfg(any(test, feature = "quickcheck"))] use quickcheck::{Arbitrary, Gen}; +#[cfg(any(test, feature = "quickcheck"))] use rand::Rng; /// String-to-Key (S2K) specifiers. @@ -262,6 +264,7 @@ impl fmt::Display for S2K { } } +#[cfg(any(test, feature = "quickcheck"))] impl Arbitrary for S2K { fn arbitrary<G: Gen>(g: &mut G) -> Self { match g.gen_range(0, 5) { diff --git a/openpgp/src/crypto/sexp.rs b/openpgp/src/crypto/sexp.rs index 479fb8e3..17805234 100644 --- a/openpgp/src/crypto/sexp.rs +++ b/openpgp/src/crypto/sexp.rs @@ -9,6 +9,8 @@ use std::convert::TryFrom; use std::fmt; use std::ops::Deref; + +#[cfg(any(test, feature = "quickcheck"))] use quickcheck::{Arbitrary, Gen}; use crate::crypto::{self, mpi, SessionKey}; @@ -293,6 +295,7 @@ impl TryFrom<&mpi::Ciphertext> for Sexp { } } +#[cfg(any(test, feature = "quickcheck"))] impl Arbitrary for Sexp { fn arbitrary<G: Gen>(g: &mut G) -> Self { if f32::arbitrary(g) < 0.7 { @@ -382,6 +385,7 @@ impl Deref for String_ { } } +#[cfg(any(test, feature = "quickcheck"))] impl Arbitrary for String_ { fn arbitrary<G: Gen>(g: &mut G) -> Self { if bool::arbitrary(g) { diff --git a/openpgp/src/fingerprint.rs b/openpgp/src/fingerprint.rs index 6094229f..a88dafdd 100644 --- a/openpgp/src/fingerprint.rs +++ b/openpgp/src/fingerprint.rs @@ -1,4 +1,6 @@ use std::fmt; + +#[cfg(any(test, feature = "quickcheck"))] use quickcheck::{Arbitrary, Gen}; /// A long identifier for certificates and keys. @@ -206,6 +208,7 @@ impl Fingerprint { } } +#[cfg(any(test, feature = "quickcheck"))] impl Arbitrary for Fingerprint { fn arbitrary<G: Gen>(g: &mut G) -> Self { use rand::Rng; diff --git a/openpgp/src/keyid.rs b/openpgp/src/keyid.rs index fb511316..6d8e739f 100644 --- a/openpgp/src/keyid.rs +++ b/openpgp/src/keyid.rs @@ -1,4 +1,6 @@ use std::fmt; + +#[cfg(any(test, feature = "quickcheck"))] use quickcheck::{Arbitrary, Gen}; use crate::Error; @@ -247,6 +249,7 @@ impl KeyID { } } +#[cfg(any(test, feature = "quickcheck"))] impl Arbitrary for KeyID { fn arbitrary<G: Gen>(g: &mut G) -> Self { KeyID::new(u64::arbitrary(g)) diff --git a/openpgp/src/packet/compressed_data.rs b/openpgp/src/packet/compressed_data.rs index e86d870f..4f379496 100644 --- a/openpgp/src/packet/compressed_data.rs +++ b/openpgp/src/packet/compressed_data.rs @@ -1,4 +1,6 @@ use std::fmt; + +#[cfg(any(test, feature = "quickcheck"))] use quickcheck::{Arbitrary, Gen}; use crate::packet; @@ -94,6 +96,7 @@ impl From<CompressedData> for Packet { } } +#[cfg(any(test, feature = "quickcheck"))] impl Arbitrary for CompressedData { fn arbitrary<G: Gen>(g: &mut G) -> Self { use rand::Rng; diff --git a/openpgp/src/packet/key.rs b/openpgp/src/packet/key.rs index 55a9ff60..039c493f 100644 --- a/openpgp/src/packet/key.rs +++ b/openpgp/src/packet/key.rs @@ -54,6 +54,8 @@ use std::fmt; use std::cmp::Ordering; use std::convert::TryInto; use std::time; + +#[cfg(any(test, feature = "quickcheck"))] use quickcheck::{Arbitrary, Gen}; use crate::Error; @@ -1290,6 +1292,7 @@ impl Encrypted { } } +#[cfg(any(test, feature = "quickcheck"))] impl<P, R> Arbitrary for super::Key<P, R> where P: KeyParts, P: Clone, R: KeyRole, R: Clone, @@ -1300,6 +1303,7 @@ impl<P, R> Arbitrary for super::Key<P, R> } } +#[cfg(any(test, feature = "quickcheck"))] impl Arbitrary for Key4<PublicParts, UnspecifiedRole> { fn arbitrary<G: Gen>(g: &mut G) -> Self { let mpis = mpi::PublicKey::arbitrary(g); @@ -1316,6 +1320,7 @@ impl Arbitrary for Key4<PublicParts, UnspecifiedRole> { } } +#[cfg(any(test, feature = "quickcheck"))] impl Arbitrary for Key4<SecretParts, UnspecifiedRole> { fn arbitrary<G: Gen>(g: &mut G) -> Self { use rand::Rng; diff --git a/openpgp/src/packet/literal.rs b/openpgp/src/packet/literal.rs index 46e6bfb0..7eb4082b 100644 --- a/openpgp/src/packet/literal.rs +++ b/openpgp/src/packet/literal.rs @@ -2,6 +2,8 @@ use std::fmt; use std::cmp; use std::convert::TryInto; use std::time; + +#[cfg(any(test, feature = "quickcheck"))] use quickcheck::{Arbitrary, Gen}; use crate::types::{DataFormat, Timestamp}; @@ -172,6 +174,7 @@ impl From<Literal> for Packet { } } +#[cfg(any(test, feature = "quickcheck"))] impl Arbitrary for Literal { fn arbitrary<G: Gen>(g: &mut G) -> Self { let mut l = Literal::new(DataFormat::arbitrary(g)); diff --git a/openpgp/src/packet/marker.rs b/openpgp/src/packet/marker.rs index 8217901c..728f115b 100644 --- a/openpgp/src/packet/marker.rs +++ b/openpgp/src/packet/marker.rs @@ -1,3 +1,4 @@ +#[cfg(any(test, feature = "quickcheck"))] use quickcheck::{Arbitrary, Gen}; use crate::packet; @@ -34,6 +35,7 @@ impl From<Marker> for Packet { } } +#[cfg(any(test, feature = "quickcheck"))] impl Arbitrary for Marker { fn arbitrary<G: Gen>(_: &mut G) -> Self { Self::default() diff --git a/openpgp/src/packet/mod.rs b/openpgp/src/packet/mod.rs index fec3ee2a..3c822df7 100644 --- a/openpgp/src/packet/mod.rs +++ b/openpgp/src/packet/mod.rs @@ -161,6 +161,7 @@ use std::ops::{Deref, DerefMut}; use std::slice; use std::iter::IntoIterator; +#[cfg(any(test, feature = "quickcheck"))] use quickcheck::{Arbitrary, Gen}; use crate::Error; @@ -434,6 +435,7 @@ impl<'a> DerefMut for Packet { } } +#[cfg(any(test, feature = "quickcheck"))] impl Arbitrary for Packet { fn arbitrary<G: Gen>(g: &mut G) -> Self { use rand::Rng; @@ -479,6 +481,7 @@ pub struct Common { dummy: std::marker::PhantomData<()>, } +#[cfg(any(test, feature = "quickcheck"))] impl Arbitrary for Common { fn arbitrary<G: Gen>(_: &mut G) -> Self { // XXX: Change if this gets interesting fields. diff --git a/openpgp/src/packet/one_pass_sig.rs b/openpgp/src/packet/one_pass_sig.rs index dc40cf64..a6861d6e 100644 --- a/openpgp/src/packet/one_pass_sig.rs +++ b/openpgp/src/packet/one_pass_sig.rs @@ -5,6 +5,8 @@ //! [Section 5.4 of RFC 4880]: https://tools.ietf.org/html/rfc4880#section-5.4 use std::fmt; + +#[cfg(any(test, feature = "quickcheck"))] use quickcheck::{Arbitrary, Gen}; use crate::Error; @@ -166,12 +168,14 @@ impl<'a> std::convert::TryFrom<&'a Signature> for OnePassSig3 { } } +#[cfg(any(test, feature = "quickcheck"))] impl Arbitrary for super::OnePassSig { fn arbitrary<G: Gen>(g: &mut G) -> Self { OnePassSig3::arbitrary(g).into() } } +#[cfg(any(test, feature = "quickcheck"))] impl Arbitrary for OnePassSig3 { fn arbitrary<G: Gen>(g: &mut G) -> Self { let mut ops = OnePassSig3::new(SignatureType::arbitrary(g)); diff --git a/openpgp/src/packet/pkesk.rs b/openpgp/src/packet/pkesk.rs index f92c596e..81c0f9ae 100644 --- a/openpgp/src/packet/pkesk.rs +++ b/openpgp/src/packet/pkesk.rs @@ -5,6 +5,7 @@ //! //! [Section 5.1 of RFC 4880]: https://tools.ietf.org/html/rfc4880#section-5.1 +#[cfg(any(test, feature = "quickcheck"))] use quickcheck::{Arbitrary, Gen}; use crate::Error; @@ -189,12 +190,14 @@ impl From<PKESK3> for Packet { } } +#[cfg(any(test, feature = "quickcheck"))] impl Arbitrary for super::PKESK { fn arbitrary<G: Gen>(g: &mut G) -> Self { PKESK3::arbitrary(g).into() } } +#[cfg(any(test, feature = "quickcheck"))] impl Arbitrary for PKESK3 { fn arbitrary<G: Gen>(g: &mut G) -> Self { let (ciphertext, pk_algo) = loop { diff --git a/openpgp/src/packet/signature/mod.rs b/openpgp/src/packet/signature/mod.rs index d0829a5d..74be7900 100644 --- a/openpgp/src/packet/signature/mod.rs +++ b/openpgp/src/packet/signature/mod.rs @@ -2,6 +2,8 @@ use std::fmt; use std::ops::{Deref, DerefMut}; + +#[cfg(any(test, feature = "quickcheck"))] use quickcheck::{Arbitrary, Gen}; use crate::Error; @@ -28,6 +30,7 @@ use crate::packet::signature::subpacket::{ SubpacketAreas, }; +#[cfg(any(test, feature = "quickcheck"))] /// Like quickcheck::Arbitrary, but bounded. trait ArbitraryBounded { /// Generates an arbitrary value, but only recurses if `depth > @@ -35,9 +38,11 @@ trait ArbitraryBounded { fn arbitrary_bounded<G: Gen>(g: &mut G, depth: usize) -> Self; } +#[cfg(any(test, feature = "quickcheck"))] /// Default depth when implementing Arbitrary using ArbitraryBounded. const DEFAULT_ARBITRARY_DEPTH: usize = 2; +#[cfg(any(test, feature = "quickcheck"))] macro_rules! impl_arbitrary_with_bound { ($typ:path) => { impl Arbitrary for $typ { @@ -101,6 +106,7 @@ pub struct SignatureBuilder { subpackets: SubpacketAreas, } +#[cfg(any(test, feature = "quickcheck"))] impl ArbitraryBounded for SignatureBuilder { fn arbitrary_bounded<G: Gen>(g: &mut G, depth: usize) -> Self { SignatureBuilder { @@ -115,6 +121,7 @@ impl ArbitraryBounded for SignatureBuilder { } } +#[cfg(any(test, feature = "quickcheck"))] impl_arbitrary_with_bound!(SignatureBuilder); impl Deref for SignatureBuilder { @@ -1212,14 +1219,17 @@ impl From<Signature4> for super::Signature { } } +#[cfg(any(test, feature = "quickcheck"))] impl ArbitraryBounded for super: |