diff --git a/pgpainless-core/src/main/java/org/pgpainless/encryption_signing/EncryptionOptions.java b/pgpainless-core/src/main/java/org/pgpainless/encryption_signing/EncryptionOptions.java index ca7b8ddb..99059c0a 100644 --- a/pgpainless-core/src/main/java/org/pgpainless/encryption_signing/EncryptionOptions.java +++ b/pgpainless-core/src/main/java/org/pgpainless/encryption_signing/EncryptionOptions.java @@ -107,6 +107,9 @@ public class EncryptionOptions { * @return this */ public EncryptionOptions addRecipients(Iterable keys) { + if (!keys.iterator().hasNext()) { + throw new IllegalArgumentException("Set of recipient keys cannot be empty."); + } for (PGPPublicKeyRing key : keys) { addRecipient(key); } @@ -122,6 +125,9 @@ public class EncryptionOptions { * @return this */ public EncryptionOptions addRecipients(@Nonnull Iterable keys, @Nonnull EncryptionKeySelector selector) { + if (!keys.iterator().hasNext()) { + throw new IllegalArgumentException("Set of recipient keys cannot be empty."); + } for (PGPPublicKeyRing key : keys) { addRecipient(key, selector); } diff --git a/pgpainless-core/src/test/java/org/pgpainless/encryption_signing/EncryptionOptionsTest.java b/pgpainless-core/src/test/java/org/pgpainless/encryption_signing/EncryptionOptionsTest.java index 488e5918..40a336e8 100644 --- a/pgpainless-core/src/test/java/org/pgpainless/encryption_signing/EncryptionOptionsTest.java +++ b/pgpainless-core/src/test/java/org/pgpainless/encryption_signing/EncryptionOptionsTest.java @@ -116,6 +116,14 @@ public class EncryptionOptionsTest { assertTrue(encryptionKeys.contains(encryptStorage)); } + @Test + public void testAddEmptyRecipientsFails() { + EncryptionOptions options = new EncryptionOptions(); + assertThrows(IllegalArgumentException.class, () -> options.addRecipients(Collections.emptyList())); + assertThrows(IllegalArgumentException.class, () -> options.addRecipients(Collections.emptyList(), + encryptionCapableKeys -> encryptionCapableKeys)); + } + @Test public void testAddEmptyPassphraseFails() { EncryptionOptions options = new EncryptionOptions();