From d036cf25935fa0f7a14ee8a5bff28626d4c7162e Mon Sep 17 00:00:00 2001 From: Paul Schaub Date: Fri, 12 Nov 2021 16:49:28 +0100 Subject: [PATCH] Add tests for KeyRingUtils.deleteUserIdFrom*KeyRing methods --- .../org/pgpainless/key/util/KeyRingUtils.java | 4 +- .../pgpainless/key/util/KeyRingUtilTest.java | 53 +++++++++++++++++++ 2 files changed, 55 insertions(+), 2 deletions(-) create mode 100644 pgpainless-core/src/test/java/org/pgpainless/key/util/KeyRingUtilTest.java diff --git a/pgpainless-core/src/main/java/org/pgpainless/key/util/KeyRingUtils.java b/pgpainless-core/src/main/java/org/pgpainless/key/util/KeyRingUtils.java index 4d5dc834..1c9980be 100644 --- a/pgpainless-core/src/main/java/org/pgpainless/key/util/KeyRingUtils.java +++ b/pgpainless-core/src/main/java/org/pgpainless/key/util/KeyRingUtils.java @@ -167,7 +167,7 @@ public final class KeyRingUtils { * @return modified secret keys */ @Deprecated - public PGPSecretKeyRing deleteUserIdFromSecretKeyRing(PGPSecretKeyRing secretKeys, String userId) { + public static PGPSecretKeyRing deleteUserIdFromSecretKeyRing(PGPSecretKeyRing secretKeys, String userId) { PGPSecretKey secretKey = secretKeys.getSecretKey(); // user-ids are located on primary key only PGPPublicKey publicKey = secretKey.getPublicKey(); // user-ids are placed on the public key part publicKey = PGPPublicKey.removeCertification(publicKey, userId); @@ -191,7 +191,7 @@ public final class KeyRingUtils { * @return modified secret keys */ @Deprecated - public PGPPublicKeyRing deleteUserIdFromPublicKeyRing(PGPPublicKeyRing publicKeys, String userId) { + public static PGPPublicKeyRing deleteUserIdFromPublicKeyRing(PGPPublicKeyRing publicKeys, String userId) { PGPPublicKey publicKey = publicKeys.getPublicKey(); // user-ids are located on primary key only publicKey = PGPPublicKey.removeCertification(publicKey, userId); if (publicKey == null) { diff --git a/pgpainless-core/src/test/java/org/pgpainless/key/util/KeyRingUtilTest.java b/pgpainless-core/src/test/java/org/pgpainless/key/util/KeyRingUtilTest.java new file mode 100644 index 00000000..d939dc24 --- /dev/null +++ b/pgpainless-core/src/test/java/org/pgpainless/key/util/KeyRingUtilTest.java @@ -0,0 +1,53 @@ +// SPDX-FileCopyrightText: 2021 Paul Schaub +// +// SPDX-License-Identifier: Apache-2.0 + +package org.pgpainless.key.util; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import java.security.InvalidAlgorithmParameterException; +import java.security.NoSuchAlgorithmException; + +import org.bouncycastle.openpgp.PGPException; +import org.bouncycastle.openpgp.PGPPublicKeyRing; +import org.bouncycastle.openpgp.PGPSecretKeyRing; +import org.junit.jupiter.api.Test; +import org.pgpainless.PGPainless; +import org.pgpainless.key.protection.SecretKeyRingProtector; +import org.pgpainless.util.CollectionUtils; + +public class KeyRingUtilTest { + + @Test + public void testDeleteUserIdFromSecretKeyRing() + throws PGPException, InvalidAlgorithmParameterException, NoSuchAlgorithmException { + PGPSecretKeyRing secretKeys = PGPainless.generateKeyRing() + .modernKeyRing("Alice", null); + + secretKeys = PGPainless.modifyKeyRing(secretKeys) + .addUserId("Bob", SecretKeyRingProtector.unprotectedKeys()) + .done(); + assertEquals(2, CollectionUtils.iteratorToList(secretKeys.getPublicKey().getUserIDs()).size()); + + secretKeys = KeyRingUtils.deleteUserIdFromSecretKeyRing(secretKeys, "Bob"); + + assertEquals(1, CollectionUtils.iteratorToList(secretKeys.getPublicKey().getUserIDs()).size()); + } + + @Test + public void testDeleteUserIdFromPublicKeyRing() throws PGPException, InvalidAlgorithmParameterException, NoSuchAlgorithmException { + PGPSecretKeyRing secretKeys = PGPainless.generateKeyRing() + .modernKeyRing("Alice", null); + + secretKeys = PGPainless.modifyKeyRing(secretKeys) + .addUserId("Bob", SecretKeyRingProtector.unprotectedKeys()) + .done(); + PGPPublicKeyRing publicKeys = PGPainless.extractCertificate(secretKeys); + assertEquals(2, CollectionUtils.iteratorToList(publicKeys.getPublicKey().getUserIDs()).size()); + + publicKeys = KeyRingUtils.deleteUserIdFromPublicKeyRing(publicKeys, "Alice"); + + assertEquals(1, CollectionUtils.iteratorToList(publicKeys.getPublicKey().getUserIDs()).size()); + } +}