1
0
Fork 0
mirror of https://github.com/pgpainless/pgpainless.git synced 2025-09-14 04:39:39 +02:00

Create dedicated KeyException class for key-related exceptions.

This commit is contained in:
Paul Schaub 2022-03-15 14:04:59 +01:00
parent 6b3f37796c
commit a22336a795
13 changed files with 186 additions and 68 deletions

View file

@ -34,6 +34,7 @@ import org.pgpainless.algorithm.SymmetricKeyAlgorithm;
import org.pgpainless.decryption_verification.ConsumerOptions;
import org.pgpainless.decryption_verification.DecryptionStream;
import org.pgpainless.decryption_verification.OpenPgpMetadata;
import org.pgpainless.exception.KeyException;
import org.pgpainless.key.SubkeyIdentifier;
import org.pgpainless.key.TestKeys;
import org.pgpainless.key.generation.KeySpec;
@ -326,7 +327,7 @@ public class EncryptDecryptTest {
PGPPublicKeyRing publicKeys = PGPainless.readKeyRing().publicKeyRing(key);
assertThrows(IllegalArgumentException.class, () ->
assertThrows(KeyException.UnacceptableEncryptionKeyException.class, () ->
EncryptionOptions.encryptCommunications()
.addRecipient(publicKeys));
}

View file

@ -28,7 +28,7 @@ import org.junit.jupiter.api.Test;
import org.pgpainless.PGPainless;
import org.pgpainless.algorithm.KeyFlag;
import org.pgpainless.algorithm.SymmetricKeyAlgorithm;
import org.pgpainless.exception.KeyValidationError;
import org.pgpainless.exception.KeyException;
import org.pgpainless.key.SubkeyIdentifier;
import org.pgpainless.key.generation.KeySpec;
import org.pgpainless.key.generation.type.KeyType;
@ -132,14 +132,14 @@ public class EncryptionOptionsTest {
.build();
PGPPublicKeyRing publicKeys = KeyRingUtils.publicKeyRingFrom(secretKeys);
assertThrows(IllegalArgumentException.class, () -> options.addRecipient(publicKeys));
assertThrows(KeyException.UnacceptableEncryptionKeyException.class, () -> options.addRecipient(publicKeys));
}
@Test
public void testEncryptionKeySelectionStrategyEmpty_ThrowsAssertionError() {
EncryptionOptions options = new EncryptionOptions();
assertThrows(IllegalArgumentException.class,
assertThrows(KeyException.UnacceptableEncryptionKeyException.class,
() -> options.addRecipient(publicKeys, new EncryptionOptions.EncryptionKeySelector() {
@Override
public List<PGPPublicKey> selectEncryptionSubkeys(List<PGPPublicKey> encryptionCapableKeys) {
@ -147,7 +147,7 @@ public class EncryptionOptionsTest {
}
}));
assertThrows(IllegalArgumentException.class,
assertThrows(KeyException.UnacceptableEncryptionKeyException.class,
() -> options.addRecipient(publicKeys, "test@pgpainless.org", new EncryptionOptions.EncryptionKeySelector() {
@Override
public List<PGPPublicKey> selectEncryptionSubkeys(List<PGPPublicKey> encryptionCapableKeys) {
@ -180,6 +180,6 @@ public class EncryptionOptionsTest {
@Test
public void testAddRecipient_withInvalidUserId() {
EncryptionOptions options = new EncryptionOptions();
assertThrows(KeyValidationError.class, () -> options.addRecipient(publicKeys, "invalid@user.id"));
assertThrows(KeyException.UnboundUserIdException.class, () -> options.addRecipient(publicKeys, "invalid@user.id"));
}
}

View file

@ -35,8 +35,7 @@ import org.pgpainless.algorithm.KeyFlag;
import org.pgpainless.decryption_verification.ConsumerOptions;
import org.pgpainless.decryption_verification.DecryptionStream;
import org.pgpainless.decryption_verification.OpenPgpMetadata;
import org.pgpainless.exception.KeyCannotSignException;
import org.pgpainless.exception.KeyValidationError;
import org.pgpainless.exception.KeyException;
import org.pgpainless.key.SubkeyIdentifier;
import org.pgpainless.key.TestKeys;
import org.pgpainless.key.generation.KeySpec;
@ -45,9 +44,9 @@ import org.pgpainless.key.generation.type.eddsa.EdDSACurve;
import org.pgpainless.key.info.KeyRingInfo;
import org.pgpainless.key.protection.SecretKeyRingProtector;
import org.pgpainless.key.util.KeyRingUtils;
import org.pgpainless.util.TestAllImplementations;
import org.pgpainless.util.MultiMap;
import org.pgpainless.util.Passphrase;
import org.pgpainless.util.TestAllImplementations;
public class SigningTest {
@ -125,7 +124,7 @@ public class SigningTest {
SigningOptions opts = new SigningOptions();
// "bob" is not a valid user-id
assertThrows(KeyValidationError.class,
assertThrows(KeyException.UnboundUserIdException.class,
() -> opts.addInlineSignature(protector, secretKeys, "bob",
DocumentSignatureType.CANONICAL_TEXT_DOCUMENT));
}
@ -146,7 +145,7 @@ public class SigningTest {
SigningOptions opts = new SigningOptions();
// "alice" has been revoked
assertThrows(KeyValidationError.class,
assertThrows(KeyException.UnboundUserIdException.class,
() -> opts.addInlineSignature(protector, fSecretKeys, "alice",
DocumentSignatureType.CANONICAL_TEXT_DOCUMENT));
}
@ -253,9 +252,9 @@ public class SigningTest {
.build();
SigningOptions options = new SigningOptions();
assertThrows(KeyCannotSignException.class, () -> options.addDetachedSignature(
assertThrows(KeyException.UnacceptableSigningKeyException.class, () -> options.addDetachedSignature(
SecretKeyRingProtector.unprotectedKeys(), secretKeys, DocumentSignatureType.BINARY_DOCUMENT));
assertThrows(KeyCannotSignException.class, () -> options.addInlineSignature(
assertThrows(KeyException.UnacceptableSigningKeyException.class, () -> options.addInlineSignature(
SecretKeyRingProtector.unprotectedKeys(), secretKeys, DocumentSignatureType.BINARY_DOCUMENT));
}
@ -270,10 +269,10 @@ public class SigningTest {
.build();
SigningOptions options = new SigningOptions();
assertThrows(KeyValidationError.class, () ->
assertThrows(KeyException.UnboundUserIdException.class, () ->
options.addDetachedSignature(SecretKeyRingProtector.unprotectedKeys(), secretKeys, "Bob",
DocumentSignatureType.BINARY_DOCUMENT));
assertThrows(KeyValidationError.class, () ->
assertThrows(KeyException.UnboundUserIdException.class, () ->
options.addInlineSignature(SecretKeyRingProtector.unprotectedKeys(), secretKeys, "Bob",
DocumentSignatureType.BINARY_DOCUMENT));
}

View file

@ -21,6 +21,7 @@ import org.pgpainless.encryption_signing.EncryptionOptions;
import org.pgpainless.encryption_signing.EncryptionResult;
import org.pgpainless.encryption_signing.EncryptionStream;
import org.pgpainless.encryption_signing.ProducerOptions;
import org.pgpainless.exception.KeyException;
import org.pgpainless.key.OpenPgpV4Fingerprint;
import org.pgpainless.key.SubkeyIdentifier;
@ -124,7 +125,7 @@ public class CertificateExpirationTest {
"-----END PGP PUBLIC KEY BLOCK-----\n";
PGPPublicKeyRing cert = PGPainless.readKeyRing().publicKeyRing(CERT);
assertThrows(IllegalArgumentException.class, () -> encrypt(cert));
assertThrows(KeyException.ExpiredKeyException.class, () -> encrypt(cert));
}
private EncryptionResult encrypt(PGPPublicKeyRing certificate) throws PGPException, IOException {

View file

@ -16,6 +16,7 @@ import org.junit.jupiter.api.Test;
import org.pgpainless.PGPainless;
import org.pgpainless.algorithm.KeyFlag;
import org.pgpainless.encryption_signing.EncryptionOptions;
import org.pgpainless.exception.KeyException;
import org.pgpainless.key.generation.KeySpec;
import org.pgpainless.key.generation.type.KeyType;
import org.pgpainless.key.generation.type.rsa.RsaLength;
@ -38,7 +39,7 @@ public class TestEncryptCommsStorageFlagsDifferentiated {
PGPPublicKeyRing publicKeys = KeyRingUtils.publicKeyRingFrom(secretKeys);
assertThrows(IllegalArgumentException.class, () -> EncryptionOptions.encryptCommunications()
assertThrows(KeyException.UnacceptableEncryptionKeyException.class, () -> EncryptionOptions.encryptCommunications()
.addRecipient(publicKeys));
}
}