diff options
author | Justus Winter <justus@sequoia-pgp.org> | 2018-12-05 13:50:35 +0100 |
---|---|---|
committer | Justus Winter <justus@sequoia-pgp.org> | 2018-12-14 14:05:21 +0100 |
commit | a5e26965f5f0fc8fbecb401f552826ce367ca7ec (patch) | |
tree | 92fcbf60de100d4da1c117aaa98fc36b40ee31d1 /openpgp/src/packet | |
parent | d411fb80983a4d4ebb9f023599c38e34a26551e7 (diff) |
openpgp: Add roundtrip tests for packages.
Diffstat (limited to 'openpgp/src/packet')
-rw-r--r-- | openpgp/src/packet/literal.rs | 28 | ||||
-rw-r--r-- | openpgp/src/packet/one_pass_sig.rs | 27 | ||||
-rw-r--r-- | openpgp/src/packet/pkesk.rs | 26 | ||||
-rw-r--r-- | openpgp/src/packet/skesk.rs | 51 | ||||
-rw-r--r-- | openpgp/src/packet/user_attribute.rs | 24 | ||||
-rw-r--r-- | openpgp/src/packet/userid.rs | 24 |
6 files changed, 180 insertions, 0 deletions
diff --git a/openpgp/src/packet/literal.rs b/openpgp/src/packet/literal.rs index 1862621f..05a8047d 100644 --- a/openpgp/src/packet/literal.rs +++ b/openpgp/src/packet/literal.rs @@ -1,6 +1,7 @@ use std::fmt; use std::cmp; use time; +use quickcheck::{Arbitrary, Gen}; use constants::DataFormat; use conversions::Time; @@ -174,3 +175,30 @@ impl From<Literal> for Packet { s.to_packet() } } + +impl Arbitrary for Literal { + fn arbitrary<G: Gen>(g: &mut G) -> Self { + let mut l = Literal::new(DataFormat::arbitrary(g)); + l.set_body(Vec::<u8>::arbitrary(g)); + while let Err(_) = l.set_filename_from_bytes(&Vec::<u8>::arbitrary(g)) { + // Too long, try again. + } + l.set_date(Option::<u32>::arbitrary(g).map(|t| time::Tm::from_pgp(t))); + l + } +} + +#[cfg(test)] +mod tests { + use super::*; + use parse::Parse; + use serialize::Serialize; + + quickcheck! { + fn roundtrip(p: Literal) -> bool { + let q = Literal::from_bytes(&p.to_vec().unwrap()).unwrap(); + assert_eq!(p, q); + true + } + } +} diff --git a/openpgp/src/packet/one_pass_sig.rs b/openpgp/src/packet/one_pass_sig.rs index d1f28610..ae174652 100644 --- a/openpgp/src/packet/one_pass_sig.rs +++ b/openpgp/src/packet/one_pass_sig.rs @@ -1,4 +1,5 @@ use std::fmt; +use quickcheck::{Arbitrary, Gen}; use Error; use packet; @@ -172,3 +173,29 @@ impl<'a> From<&'a Signature> for Result<OnePassSig> { }) } } + +impl Arbitrary for OnePassSig { + fn arbitrary<G: Gen>(g: &mut G) -> Self { + let mut ops = OnePassSig::new(SignatureType::arbitrary(g)); + ops.set_hash_algo(HashAlgorithm::arbitrary(g)); + ops.set_pk_algo(PublicKeyAlgorithm::arbitrary(g)); + ops.set_issuer(KeyID::arbitrary(g)); + ops.set_last_raw(u8::arbitrary(g)); + ops + } +} + +#[cfg(test)] +mod tests { + use super::*; + use parse::Parse; + use serialize::Serialize; + + quickcheck! { + fn roundtrip(p: OnePassSig) -> bool { + let q = OnePassSig::from_bytes(&p.to_vec().unwrap()).unwrap(); + assert_eq!(p, q); + true + } + } +} diff --git a/openpgp/src/packet/pkesk.rs b/openpgp/src/packet/pkesk.rs index 41f1a544..7b77cf7b 100644 --- a/openpgp/src/packet/pkesk.rs +++ b/openpgp/src/packet/pkesk.rs @@ -1,3 +1,5 @@ +use quickcheck::{Arbitrary, Gen}; + use Error; use packet::Key; use KeyID; @@ -220,14 +222,38 @@ impl From<PKESK> for Packet { } } +impl Arbitrary for PKESK { + fn arbitrary<G: Gen>(g: &mut G) -> Self { + let (ciphertext, pk_algo) = loop { + let ciphertext = Ciphertext::arbitrary(g); + if let Some(pk_algo) = ciphertext.pk_algo() { + break (ciphertext, pk_algo); + } + }; + + PKESK::new_(KeyID::arbitrary(g), + pk_algo, ciphertext).unwrap() + } +} + #[cfg(test)] mod tests { + use super::*; use TPK; use PacketPile; use packet::key::SecretKey; use Packet; use std::path::PathBuf; use parse::Parse; + use serialize::Serialize; + + quickcheck! { + fn roundtrip(p: PKESK) -> bool { + let q = PKESK::from_bytes(&p.to_vec().unwrap()).unwrap(); + assert_eq!(p, q); + true + } + } fn path_to_key(artifact: &str) -> PathBuf { [env!("CARGO_MANIFEST_DIR"), "tests", "data", "keys", artifact] diff --git a/openpgp/src/packet/skesk.rs b/openpgp/src/packet/skesk.rs index 3c40abc7..eb4ff03f 100644 --- a/openpgp/src/packet/skesk.rs +++ b/openpgp/src/packet/skesk.rs @@ -1,4 +1,5 @@ use std::ops::{Deref, DerefMut}; +use quickcheck::{Arbitrary, Gen}; use nettle::Yarrow; @@ -51,6 +52,16 @@ impl SKESK { } } +impl Arbitrary for SKESK { + fn arbitrary<G: Gen>(g: &mut G) -> Self { + if bool::arbitrary(g) { + SKESK::V4(SKESK4::arbitrary(g)) + } else { + SKESK::V5(SKESK5::arbitrary(g)) + } + } +} + /// Holds an symmetrically encrypted session key version 4. /// /// Holds an symmetrically encrypted session key. The session key is @@ -216,6 +227,16 @@ impl From<SKESK4> for Packet { } } +impl Arbitrary for SKESK4 { + fn arbitrary<G: Gen>(g: &mut G) -> Self { + SKESK4::new(4, + SymmetricAlgorithm::arbitrary(g), + S2K::arbitrary(g), + Option::<Vec<u8>>::arbitrary(g)) + .unwrap() + } +} + /// Holds an symmetrically encrypted session key version 5. /// /// Holds an symmetrically encrypted session key. The session key is @@ -379,6 +400,28 @@ impl From<SKESK5> for Packet { } } +impl Arbitrary for SKESK5 { + fn arbitrary<G: Gen>(g: &mut G) -> 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() { + *b = u8::arbitrary(g); + } + let mut digest = vec![0u8; algo.digest_size().unwrap()]; + for b in digest.iter_mut() { + *b = u8::arbitrary(g); + } + SKESK5::new(5, + SymmetricAlgorithm::arbitrary(g), + algo, + S2K::arbitrary(g), + iv.into_boxed_slice(), + Vec::<u8>::arbitrary(g), + digest.into_boxed_slice()) + .unwrap() + } +} + #[cfg(test)] mod test { use super::*; @@ -386,6 +429,14 @@ mod test { use parse::Parse; use serialize::Serialize; + quickcheck! { + fn roundtrip(p: SKESK) -> bool { + let q = SKESK::from_bytes(&p.to_vec().unwrap()).unwrap(); + assert_eq!(p, q); + true + } + } + #[test] fn sample_skesk5_packet() { // This sample packet is from RFC4880bis-05, section A.3. diff --git a/openpgp/src/packet/user_attribute.rs b/openpgp/src/packet/user_attribute.rs index b5a58a3d..d07fea66 100644 --- a/openpgp/src/packet/user_attribute.rs +++ b/openpgp/src/packet/user_attribute.rs @@ -1,4 +1,5 @@ use std::fmt; +use quickcheck::{Arbitrary, Gen}; use packet; use Packet; @@ -73,3 +74,26 @@ impl From<UserAttribute> for Packet { s.to_packet() } } + +impl Arbitrary for UserAttribute { + fn arbitrary<G: Gen>(g: &mut G) -> Self { + let mut u = UserAttribute::new(); + u.set_user_attribute(&Vec::<u8>::arbitrary(g)); + u + } +} + +#[cfg(test)] +mod tests { + use super::*; + use parse::Parse; + use serialize::Serialize; + + quickcheck! { + fn roundtrip(p: UserAttribute) -> bool { + let q = UserAttribute::from_bytes(&p.to_vec().unwrap()).unwrap(); + assert_eq!(p, q); + true + } + } +} diff --git a/openpgp/src/packet/userid.rs b/openpgp/src/packet/userid.rs index 8459fd91..d88b06b2 100644 --- a/openpgp/src/packet/userid.rs +++ b/openpgp/src/packet/userid.rs @@ -1,4 +1,5 @@ use std::fmt; +use quickcheck::{Arbitrary, Gen}; use packet; use Packet; @@ -94,3 +95,26 @@ impl From<UserID> for Packet { s.to_packet() } } + +impl Arbitrary for UserID { + fn arbitrary<G: Gen>(g: &mut G) -> Self { + let mut u = UserID::new(); + u.set_userid_from_bytes(&Vec::<u8>::arbitrary(g)); + u + } +} + +#[cfg(test)] +mod tests { + use super::*; + use parse::Parse; + use serialize::Serialize; + + quickcheck! { + fn roundtrip(p: UserID) -> bool { + let q = UserID::from_bytes(&p.to_vec().unwrap()).unwrap(); + assert_eq!(p, q); + true + } + } +} |