diff options
author | Neal H. Walfield <neal@pep.foundation> | 2023-05-15 14:47:22 +0200 |
---|---|---|
committer | Neal H. Walfield <neal@pep.foundation> | 2023-05-15 14:55:30 +0200 |
commit | 461502a06d3743fe8ec5b1d396ded31709816344 (patch) | |
tree | 4886dbab3967e4fc1e73a220c8c9d53fc845962b /openpgp | |
parent | 22b215c16214f0a7a2776b61588ed7575f2ad383 (diff) |
openpgp: Improve test.
- Arbitrary does not draw from a uniform distribution, e.g.,
arbitrary seems to be draw 0 about 10% of the time for a 32-bit
quantity.
- When we need two different arbitrary values, be very careful to
make sure they are different.
Diffstat (limited to 'openpgp')
-rw-r--r-- | openpgp/src/packet/key.rs | 27 |
1 files changed, 23 insertions, 4 deletions
diff --git a/openpgp/src/packet/key.rs b/openpgp/src/packet/key.rs index be446af9..a81583d3 100644 --- a/openpgp/src/packet/key.rs +++ b/openpgp/src/packet/key.rs @@ -1778,6 +1778,9 @@ impl Arbitrary for Key4<SecretParts, UnspecifiedRole> { #[cfg(test)] mod tests { + use std::time::Duration; + use std::time::UNIX_EPOCH; + use crate::packet::Key; use crate::Cert; use crate::packet::pkesk::PKESK3; @@ -2365,22 +2368,38 @@ FwPoSAbbsLkNS/iNN2MDGAVYvezYn2QZ let mut key = Key4::<PublicParts, UnspecifiedRole>::arbitrary(&mut g); let fpr1 = key.fingerprint(); - key.set_creation_time(std::time::UNIX_EPOCH).expect("ok"); + if key.creation_time() == UNIX_EPOCH { + key.set_creation_time(UNIX_EPOCH + Duration::new(1, 0)).expect("ok"); + } else { + key.set_creation_time(UNIX_EPOCH).expect("ok"); + } assert_ne!(fpr1, key.fingerprint()); let mut key = Key4::<PublicParts, UnspecifiedRole>::arbitrary(&mut g); let fpr1 = key.fingerprint(); - key.set_pk_algo(PublicKeyAlgorithm::Unknown(222)); + key.set_pk_algo(PublicKeyAlgorithm::from(u8::from(key.pk_algo()) + 1)); assert_ne!(fpr1, key.fingerprint()); let mut key = Key4::<PublicParts, UnspecifiedRole>::arbitrary(&mut g); let fpr1 = key.fingerprint(); - *key.mpis_mut() = mpi::PublicKey::arbitrary(&mut g); + loop { + let mpis2 = mpi::PublicKey::arbitrary(&mut g); + if key.mpis() != &mpis2 { + *key.mpis_mut() = mpis2; + break; + } + } assert_ne!(fpr1, key.fingerprint()); let mut key = Key4::<PublicParts, UnspecifiedRole>::arbitrary(&mut g); let fpr1 = key.fingerprint(); - key.set_mpis(mpi::PublicKey::arbitrary(&mut g)); + loop { + let mpis2 = mpi::PublicKey::arbitrary(&mut g); + if key.mpis() != &mpis2 { + key.set_mpis(mpis2); + break; + } + } assert_ne!(fpr1, key.fingerprint()); } } |