From f8968fc075f418d33ee257f735ccc6c82a2ca8c9 Mon Sep 17 00:00:00 2001 From: Paul Schaub Date: Mon, 13 Dec 2021 13:28:53 +0100 Subject: [PATCH] Add test for CachingSecretKeyRingProtector.replacePassphrase(*) --- .../CachingSecretKeyRingProtectorTest.java | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/pgpainless-core/src/test/java/org/pgpainless/key/protection/CachingSecretKeyRingProtectorTest.java b/pgpainless-core/src/test/java/org/pgpainless/key/protection/CachingSecretKeyRingProtectorTest.java index 277d9d44..d843b9c2 100644 --- a/pgpainless-core/src/test/java/org/pgpainless/key/protection/CachingSecretKeyRingProtectorTest.java +++ b/pgpainless-core/src/test/java/org/pgpainless/key/protection/CachingSecretKeyRingProtectorTest.java @@ -7,7 +7,9 @@ package org.pgpainless.key.protection; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertThrows; +import java.io.IOException; import java.security.InvalidAlgorithmParameterException; import java.security.NoSuchAlgorithmException; import java.util.Iterator; @@ -22,6 +24,7 @@ import org.bouncycastle.openpgp.PGPSecretKeyRing; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.pgpainless.PGPainless; +import org.pgpainless.key.TestKeys; import org.pgpainless.key.protection.passphrase_provider.SecretKeyPassphraseProvider; import org.pgpainless.util.Passphrase; @@ -132,4 +135,30 @@ public class CachingSecretKeyRingProtectorTest { } } + @Test + public void testAddPassphrase_collision() throws PGPException, IOException { + PGPSecretKeyRing secretKeys = TestKeys.getCryptieSecretKeyRing(); + CachingSecretKeyRingProtector protector = new CachingSecretKeyRingProtector(); + protector.addPassphrase(secretKeys, TestKeys.CRYPTIE_PASSPHRASE); + + assertThrows(IllegalArgumentException.class, () -> + protector.addPassphrase(secretKeys.getPublicKey(), Passphrase.emptyPassphrase())); + + assertThrows(IllegalArgumentException.class, () -> + protector.addPassphrase(secretKeys, Passphrase.fromPassword("anotherPass"))); + } + + @Test + public void testReplacePassphrase() throws PGPException, IOException { + PGPSecretKeyRing secretKeys = TestKeys.getCryptieSecretKeyRing(); + CachingSecretKeyRingProtector protector = new CachingSecretKeyRingProtector(); + protector.addPassphrase(secretKeys, Passphrase.fromPassword("wrong")); + // no throwing + protector.replacePassphrase(secretKeys, TestKeys.CRYPTIE_PASSPHRASE); + + for (PGPSecretKey key : secretKeys) { + UnlockSecretKey.unlockSecretKey(key, protector); + } + } + }