diff options
author | Justus Winter <justus@sequoia-pgp.org> | 2020-03-31 15:59:14 +0200 |
---|---|---|
committer | Justus Winter <justus@sequoia-pgp.org> | 2020-03-31 18:26:02 +0200 |
commit | 73dd16dbb28bb5c55d1fbdf6c6ef57dbe93a7de6 (patch) | |
tree | 892eaa59edec7a1627969fc849bd2eeb43d77321 | |
parent | fab2a27aaf99a6e05057e8aaea6c5d0a949dca92 (diff) |
openpgp: Implement Arbitrary for the bit field types.
-rw-r--r-- | openpgp/src/types/features.rs | 13 | ||||
-rw-r--r-- | openpgp/src/types/key_flags.rs | 13 | ||||
-rw-r--r-- | openpgp/src/types/server_preferences.rs | 13 |
3 files changed, 30 insertions, 9 deletions
diff --git a/openpgp/src/types/features.rs b/openpgp/src/types/features.rs index 6bbaf995..984b05db 100644 --- a/openpgp/src/types/features.rs +++ b/openpgp/src/types/features.rs @@ -1,5 +1,6 @@ use std::fmt; use std::hash::{Hash, Hasher}; +use quickcheck::{Arbitrary, Gen}; /// Describes features supported by an OpenPGP implementation. /// @@ -172,14 +173,20 @@ const FEATURE_FLAG_AEAD: u8 = 0x02; /// Number of bytes with known flags. const FEATURE_FLAGS_N_KNOWN_BYTES: usize = 1; +impl Arbitrary for Features { + fn arbitrary<G: Gen>(g: &mut G) -> Self { + Self::new(Vec::arbitrary(g)) + } +} + #[cfg(test)] mod tests { use super::*; quickcheck! { - fn roundtrip(raw: Vec<u8>) -> bool { - let val = Features::new(&raw); - assert_eq!(raw, val.to_vec()); + fn roundtrip(val: Features) -> bool { + let q = Features::new(&val.to_vec()); + assert_eq!(val, q); // Check that equality ignores padding. let mut val_without_padding = val.clone(); diff --git a/openpgp/src/types/key_flags.rs b/openpgp/src/types/key_flags.rs index c5e905cb..9e7595eb 100644 --- a/openpgp/src/types/key_flags.rs +++ b/openpgp/src/types/key_flags.rs @@ -2,6 +2,7 @@ use std::hash::{Hash, Hasher}; use std::fmt; use std::cmp; use std::ops::{BitAnd, BitOr}; +use quickcheck::{Arbitrary, Gen}; /// Describes how a key may be used, and stores additional /// information. @@ -335,6 +336,12 @@ const KEY_FLAG_GROUP_KEY: u8 = 0x80; /// Number of bytes with known flags. const KEY_FLAGS_N_KNOWN_BYTES: usize = 1; +impl Arbitrary for KeyFlags { + fn arbitrary<G: Gen>(g: &mut G) -> Self { + Self::new(Vec::arbitrary(g)) + } +} + #[cfg(test)] mod tests { use super::*; @@ -364,9 +371,9 @@ mod tests { } quickcheck! { - fn roundtrip(raw: Vec<u8>) -> bool { - let val = KeyFlags::new(&raw); - assert_eq!(raw, val.to_vec()); + fn roundtrip(val: KeyFlags) -> bool { + let q = KeyFlags::new(&val.to_vec()); + assert_eq!(val, q); // Check that equality ignores padding. let mut val_without_padding = val.clone(); diff --git a/openpgp/src/types/server_preferences.rs b/openpgp/src/types/server_preferences.rs index 9578130f..d6a0b7f2 100644 --- a/openpgp/src/types/server_preferences.rs +++ b/openpgp/src/types/server_preferences.rs @@ -1,5 +1,6 @@ use std::hash::{Hash, Hasher}; use std::fmt; +use quickcheck::{Arbitrary, Gen}; /// Describes preferences regarding key servers. /// @@ -130,6 +131,12 @@ const KEYSERVER_PREFERENCE_NO_MODIFY: u8 = 0x80; /// Number of bytes with known flags. const KEYSERVER_PREFERENCES_N_KNOWN_BYTES: usize = 1; +impl Arbitrary for KeyServerPreferences { + fn arbitrary<G: Gen>(g: &mut G) -> Self { + Self::new(Vec::arbitrary(g)) + } +} + #[cfg(test)] mod tests { use super::*; @@ -146,9 +153,9 @@ mod tests { } quickcheck! { - fn roundtrip(raw: Vec<u8>) -> bool { - let val = KeyServerPreferences::new(&raw); - assert_eq!(raw, val.to_vec()); + fn roundtrip(val: KeyServerPreferences) -> bool { + let q = KeyServerPreferences::new(&val.to_vec()); + assert_eq!(val, q); // Check that equality ignores padding. let mut val_without_padding = val.clone(); |