1
0
Fork 0
mirror of https://github.com/pgpainless/pgpainless.git synced 2024-12-23 11:27:57 +01:00

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

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.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull; 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.InvalidAlgorithmParameterException;
import java.security.NoSuchAlgorithmException; import java.security.NoSuchAlgorithmException;
import java.util.Iterator; import java.util.Iterator;
@ -22,6 +24,7 @@ import org.bouncycastle.openpgp.PGPSecretKeyRing;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.pgpainless.PGPainless; import org.pgpainless.PGPainless;
import org.pgpainless.key.TestKeys;
import org.pgpainless.key.protection.passphrase_provider.SecretKeyPassphraseProvider; import org.pgpainless.key.protection.passphrase_provider.SecretKeyPassphraseProvider;
import org.pgpainless.util.Passphrase; 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);
}
}
} }