Add test for CachingSecretKeyRingProtector.replacePassphrase(*)

This commit is contained in:
Paul Schaub 2021-12-13 13:28:53 +01:00
parent c4e3e27821
commit f8968fc075
Signed by: vanitasvitae
GPG Key ID: 62BEE9264BF17311
1 changed files with 29 additions and 0 deletions

View File

@ -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);
}
}
}