From b980fcd7b1fff3c12e07cdc8b5f8d47ab0091a27 Mon Sep 17 00:00:00 2001 From: Paul Schaub Date: Fri, 29 Apr 2022 22:49:45 +0200 Subject: [PATCH] EncryptionOptions.addRecipients(collection): Disallow empty collections Fixes #281 --- .../pgpainless/encryption_signing/EncryptionOptions.java | 6 ++++++ .../encryption_signing/EncryptionOptionsTest.java | 8 ++++++++ 2 files changed, 14 insertions(+) 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();