diff options
author | Justus Winter <justus@sequoia-pgp.org> | 2020-03-31 15:46:15 +0200 |
---|---|---|
committer | Justus Winter <justus@sequoia-pgp.org> | 2020-04-01 16:15:00 +0200 |
commit | 6fc293ea80f7331c7262174f93dc44d3be1302ba (patch) | |
tree | d8b54e9531260f6de546d1cb04e29868af46f8ad | |
parent | 6ffef8ff27bbf75e1a5f237cf0ae99556fbbdc9f (diff) |
openpgp: Implement Arbitrary for CompressedData.
-rw-r--r-- | openpgp/src/packet/compressed_data.rs | 21 | ||||
-rw-r--r-- | openpgp/src/packet/mod.rs | 7 |
2 files changed, 25 insertions, 3 deletions
diff --git a/openpgp/src/packet/compressed_data.rs b/openpgp/src/packet/compressed_data.rs index edb858bb..e86d870f 100644 --- a/openpgp/src/packet/compressed_data.rs +++ b/openpgp/src/packet/compressed_data.rs @@ -1,4 +1,5 @@ use std::fmt; +use quickcheck::{Arbitrary, Gen}; use crate::packet; use crate::Packet; @@ -92,3 +93,23 @@ impl From<CompressedData> for Packet { Packet::CompressedData(s) } } + +impl Arbitrary for CompressedData { + fn arbitrary<G: Gen>(g: &mut G) -> Self { + use rand::Rng; + use crate::serialize::SerializeInto; + loop { + let a = CompressionAlgorithm::from(g.gen_range(0, 4)); + if a.is_supported() { + let mut c = CompressedData::new(a); + // We arbitrarily chose to create packets with + // processed bodies, so that + // Packet::from_bytes(c.to_vec()) will roundtrip them. + c.set_body(packet::Body::Processed( + Packet::arbitrary(g).to_vec().unwrap() + )); + return c; + } + } + } +} diff --git a/openpgp/src/packet/mod.rs b/openpgp/src/packet/mod.rs index 17222bce..8e2c029e 100644 --- a/openpgp/src/packet/mod.rs +++ b/openpgp/src/packet/mod.rs @@ -234,15 +234,16 @@ impl<'a> DerefMut for Packet { impl Arbitrary for Packet { fn arbitrary<G: Gen>(g: &mut G) -> Self { use rand::Rng; - match g.gen_range(0, 8) { + match g.gen_range(0, 9) { 0 => OnePassSig::arbitrary(g).into(), 1 => Marker::arbitrary(g).into(), 2 => Trust::arbitrary(g).into(), 3 => UserID::arbitrary(g).into(), 4 => UserAttribute::arbitrary(g).into(), 5 => Literal::arbitrary(g).into(), - 6 => PKESK::arbitrary(g).into(), - 7 => SKESK::arbitrary(g).into(), + 6 => CompressedData::arbitrary(g).into(), + 7 => PKESK::arbitrary(g).into(), + 8 => SKESK::arbitrary(g).into(), _ => unreachable!(), } } |