summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJustus Winter <justus@sequoia-pgp.org>2021-04-07 15:12:10 +0200
committerJustus Winter <justus@sequoia-pgp.org>2021-04-08 12:56:15 +0200
commit13b52fb5e05846bd110f3baeab1a157a68325d43 (patch)
tree2f4a0a99506495e71590fc68be5900ba98e240a0
parent95656e99c2e7eacf081a0db80194b2c0c0195e33 (diff)
openpgp: Improve Packet::arbitrary to also generate unknown packets.
-rw-r--r--openpgp/src/packet/mod.rs20
1 files changed, 19 insertions, 1 deletions
diff --git a/openpgp/src/packet/mod.rs b/openpgp/src/packet/mod.rs
index e0535e86..da4c621c 100644
--- a/openpgp/src/packet/mod.rs
+++ b/openpgp/src/packet/mod.rs
@@ -475,7 +475,7 @@ impl fmt::Debug for Packet {
impl Arbitrary for Packet {
fn arbitrary<G: Gen>(g: &mut G) -> Self {
use rand::Rng;
- match g.gen_range(0, 14) {
+ match g.gen_range(0, 15) {
0 => Signature::arbitrary(g).into(),
1 => OnePassSig::arbitrary(g).into(),
2 => Key::<key::PublicParts, key::UnspecifiedRole>::arbitrary(g)
@@ -494,6 +494,24 @@ impl Arbitrary for Packet {
11 => CompressedData::arbitrary(g).into(),
12 => PKESK::arbitrary(g).into(),
13 => SKESK::arbitrary(g).into(),
+ 14 => loop {
+ let mut u = Unknown::new(
+ Tag::arbitrary(g), anyhow::anyhow!("Arbitrary::arbitrary"));
+ u.set_body(Arbitrary::arbitrary(g));
+ let u = Packet::Unknown(u);
+
+ // Check that we didn't accidentally make a valid
+ // packet.
+ use crate::parse::Parse;
+ use crate::serialize::SerializeInto;
+ if let Ok(Packet::Unknown(_)) = Packet::from_bytes(
+ &u.to_vec().unwrap())
+ {
+ break u;
+ }
+
+ // Try again!
+ },
_ => unreachable!(),
}
}