From c9982ccfe642bcf48d777b00872082863438eb46 Mon Sep 17 00:00:00 2001 From: Ivan Pizhenko Date: Wed, 3 Feb 2021 23:09:02 +0200 Subject: [PATCH] Additional user id manipulation test --- .../key/modification/AddUserIdTest.java | 54 +++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/pgpainless-core/src/test/java/org/pgpainless/key/modification/AddUserIdTest.java b/pgpainless-core/src/test/java/org/pgpainless/key/modification/AddUserIdTest.java index 886d4086..3ba9a268 100644 --- a/pgpainless-core/src/test/java/org/pgpainless/key/modification/AddUserIdTest.java +++ b/pgpainless-core/src/test/java/org/pgpainless/key/modification/AddUserIdTest.java @@ -19,14 +19,18 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertThrows; +import java.io.ByteArrayInputStream; import java.io.IOException; +import java.nio.charset.StandardCharsets; import java.security.InvalidAlgorithmParameterException; import java.security.NoSuchAlgorithmException; import java.util.Iterator; import java.util.NoSuchElementException; +import org.bouncycastle.bcpg.ArmoredInputStream; import org.bouncycastle.openpgp.PGPException; import org.bouncycastle.openpgp.PGPSecretKeyRing; +import org.bouncycastle.openpgp.jcajce.JcaPGPSecretKeyRingCollection; import org.junit.jupiter.api.Test; import org.pgpainless.PGPainless; import org.pgpainless.key.TestKeys; @@ -64,6 +68,7 @@ public class AddUserIdTest { assertFalse(userIds.hasNext()); } + @Test public void addUserId_NoSuchElementExceptionForMissingKey() throws IOException, PGPException { PGPSecretKeyRing secretKeys = TestKeys.getCryptieSecretKeyRing(); @@ -84,4 +89,53 @@ public class AddUserIdTest { assertThrows(NoSuchElementException.class, () -> PGPainless.modifyKeyRing(secretKeys) .deleteUserId(0L, TestKeys.CRYPTIE_UID, new UnprotectedKeysProtector())); } + + @Test + public void deleteExistingAndAddNewUserIdToExistingKeyRing() + throws InvalidAlgorithmParameterException, NoSuchAlgorithmException, PGPException, IOException { + final String ARMORED_PRIVATE_KEY = + "-----BEGIN PGP PRIVATE KEY BLOCK-----\r\n\r\n" + + "xVgEX6UIExYJKwYBBAHaRw8BAQdAMfHf64wPQ2LC9In5AKYU/KT1qWvI7e7a\r\n" + + "Xr+LWeQGUKIAAQCcB3zZlHfepQT26LIwbTDn4lvQ9LuD1fk2hK6i9FXFxxO7\r\n" + + "zRI8dXNlckBleGFtcGxlLmNvbT7CjwQQFgoAIAUCX6UIEwYLCQcIAwIEFQgK\r\n" + + "AgQWAgEAAhkBAhsDAh4BACEJEEoCtcZ3snFuFiEENY1GQZqrKQqgUAXASgK1\r\n" + + "xneycW6P6AEA5iXFK+fWpj0vn3xpKEuFRqvytPKFzhwd4wEvL+IGSPEBALE/\r\n" + + "pZdMzsDoKPENiLFpboDVNVJScwFXIleKmtNaRycFx10EX6UIExIKKwYBBAGX\r\n" + + "VQEFAQEHQBDdeawWVNqYkP8c/ihLEUlVpn8cQw7rmRc/sIhdAXhfAwEIBwAA\r\n" + + "/0Jy7IelcHDjxE3OzagEzSxNrCVw8uPHNRl8s6iP+CQYEfHCeAQYFggACQUC\r\n" + + "X6UIEwIbDAAhCRBKArXGd7JxbhYhBDWNRkGaqykKoFAFwEoCtcZ3snFuWp8B\r\n" + + "AIzRBYJSfZzlvlyyPhrbXJoYSICGNy/5x7noXjp/ByeOAQDnTbQi4XwXJrU4\r\n" + + "A8Nl9eyz16ZWUzEPwfWgahIG1eQDDA==\r\n" + + "=bk4o\r\n" + + "-----END PGP PRIVATE KEY BLOCK-----\r\n"; + + PGPSecretKeyRing secretKeys = readArmoredPrivateKey(ARMORED_PRIVATE_KEY.getBytes(StandardCharsets.UTF_8)); + + Iterator userIds = secretKeys.getSecretKey().getPublicKey().getUserIDs(); + assertEquals("", userIds.next()); + assertFalse(userIds.hasNext()); + + SecretKeyRingProtector protector = new UnprotectedKeysProtector(); + secretKeys = PGPainless.modifyKeyRing(secretKeys) + .deleteUserId("", protector) + .addUserId("cheshirecat@wonderland.lit", protector) + .done(); + + userIds = secretKeys.getSecretKey().getPublicKey().getUserIDs(); + assertEquals("cheshirecat@wonderland.lit", userIds.next()); + assertFalse(userIds.hasNext()); + } + + private static PGPSecretKeyRing readArmoredPrivateKey(byte[] data) throws IOException, PGPException { + try (ByteArrayInputStream byteIn = new ByteArrayInputStream(data)) { + try (ArmoredInputStream armoredIn = new ArmoredInputStream(byteIn)) { + JcaPGPSecretKeyRingCollection keyRingCollection = new JcaPGPSecretKeyRingCollection(armoredIn); + Iterator it = keyRingCollection.getKeyRings(); + if (it.hasNext()) { + return it.next(); + } + } + } + return null; + } }