From 30481cd510ac5b2f14301f2a5c005ddb49271e01 Mon Sep 17 00:00:00 2001 From: Paul Schaub Date: Fri, 7 Jul 2023 11:07:14 +0200 Subject: [PATCH] Fix unreliable unit test Fixes #389 --- .../modification/secretkeyring/SecretKeyRingEditor.java | 6 ++++++ .../secretkeyring/SecretKeyRingEditorInterface.java | 8 ++++++++ .../key/modification/RefuseToAddWeakSubkeyTest.java | 4 +++- 3 files changed, 17 insertions(+), 1 deletion(-) diff --git a/pgpainless-core/src/main/java/org/pgpainless/key/modification/secretkeyring/SecretKeyRingEditor.java b/pgpainless-core/src/main/java/org/pgpainless/key/modification/secretkeyring/SecretKeyRingEditor.java index ddd1bfaf..8f8e81fb 100644 --- a/pgpainless-core/src/main/java/org/pgpainless/key/modification/secretkeyring/SecretKeyRingEditor.java +++ b/pgpainless-core/src/main/java/org/pgpainless/key/modification/secretkeyring/SecretKeyRingEditor.java @@ -83,6 +83,12 @@ public class SecretKeyRingEditor implements SecretKeyRingEditorInterface { this.referenceTime = referenceTime; } + @Nonnull + @Override + public Date getReferenceTime() { + return referenceTime; + } + @Override public SecretKeyRingEditorInterface addUserId( @Nonnull CharSequence userId, diff --git a/pgpainless-core/src/main/java/org/pgpainless/key/modification/secretkeyring/SecretKeyRingEditorInterface.java b/pgpainless-core/src/main/java/org/pgpainless/key/modification/secretkeyring/SecretKeyRingEditorInterface.java index 1735d369..dd7ed499 100644 --- a/pgpainless-core/src/main/java/org/pgpainless/key/modification/secretkeyring/SecretKeyRingEditorInterface.java +++ b/pgpainless-core/src/main/java/org/pgpainless/key/modification/secretkeyring/SecretKeyRingEditorInterface.java @@ -29,6 +29,14 @@ import org.pgpainless.util.selection.userid.SelectUserId; public interface SecretKeyRingEditorInterface { + /** + * Return the editors reference time. + * + * @return reference time + */ + @Nonnull + Date getReferenceTime(); + /** * Add a user-id to the key ring. * diff --git a/pgpainless-core/src/test/java/org/pgpainless/key/modification/RefuseToAddWeakSubkeyTest.java b/pgpainless-core/src/test/java/org/pgpainless/key/modification/RefuseToAddWeakSubkeyTest.java index 73c5953c..e129e4be 100644 --- a/pgpainless-core/src/test/java/org/pgpainless/key/modification/RefuseToAddWeakSubkeyTest.java +++ b/pgpainless-core/src/test/java/org/pgpainless/key/modification/RefuseToAddWeakSubkeyTest.java @@ -76,7 +76,9 @@ public class RefuseToAddWeakSubkeyTest { PGPainless.getPolicy().setPublicKeyAlgorithmPolicy(new Policy.PublicKeyAlgorithmPolicy(minimalBitStrengths)); SecretKeyRingEditorInterface editor = PGPainless.modifyKeyRing(secretKeys); - KeySpec spec = KeySpec.getBuilder(KeyType.RSA(RsaLength._1024), KeyFlag.ENCRYPT_COMMS).build(); + KeySpec spec = KeySpec.getBuilder(KeyType.RSA(RsaLength._1024), KeyFlag.ENCRYPT_COMMS) + .setKeyCreationDate(editor.getReferenceTime()) // The key gets created after we instantiate the editor. + .build(); secretKeys = editor.addSubKey(spec, Passphrase.emptyPassphrase(), SecretKeyRingProtector.unprotectedKeys()) .done();