Skip to content

Commit

Permalink
Add test for CachingSecretKeyRingProtector.replacePassphrase(*)
Browse files Browse the repository at this point in the history
  • Loading branch information
vanitasvitae committed Dec 13, 2021
1 parent c4e3e27 commit f8968fc
Showing 1 changed file with 29 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
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;
Expand All @@ -22,6 +24,7 @@
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;

Expand Down Expand Up @@ -132,4 +135,30 @@ public void testProtectorWithCallback() {
}
}

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

}

0 comments on commit f8968fc

Please sign in to comment.