From b9845912ee1112a76158c2129b359056519c694e Mon Sep 17 00:00:00 2001 From: Paul Schaub Date: Mon, 8 Aug 2022 13:20:28 +0200 Subject: [PATCH] Add tests for readKeyRing() --- .../key/parsing/KeyRingReaderTest.java | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/pgpainless-core/src/test/java/org/pgpainless/key/parsing/KeyRingReaderTest.java b/pgpainless-core/src/test/java/org/pgpainless/key/parsing/KeyRingReaderTest.java index 404440ca..8a634200 100644 --- a/pgpainless-core/src/test/java/org/pgpainless/key/parsing/KeyRingReaderTest.java +++ b/pgpainless-core/src/test/java/org/pgpainless/key/parsing/KeyRingReaderTest.java @@ -4,6 +4,7 @@ package org.pgpainless.key.parsing; +import static org.junit.jupiter.api.Assertions.assertArrayEquals; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; @@ -23,6 +24,7 @@ import org.bouncycastle.bcpg.ArmoredOutputStream; import org.bouncycastle.bcpg.BCPGOutputStream; import org.bouncycastle.bcpg.MarkerPacket; import org.bouncycastle.openpgp.PGPException; +import org.bouncycastle.openpgp.PGPKeyRing; import org.bouncycastle.openpgp.PGPPublicKeyRing; import org.bouncycastle.openpgp.PGPPublicKeyRingCollection; import org.bouncycastle.openpgp.PGPSecretKeyRing; @@ -562,4 +564,54 @@ class KeyRingReaderTest { assertThrows(IOException.class, () -> KeyRingReader.readPublicKeyRingCollection(new ByteArrayInputStream(bytes.toByteArray()), 512)); } + + @Test + public void testReadKeyRingWithBinaryPublicKey() throws PGPException, InvalidAlgorithmParameterException, NoSuchAlgorithmException, IOException { + PGPSecretKeyRing secretKeys = PGPainless.generateKeyRing().modernKeyRing("Alice "); + PGPPublicKeyRing publicKeys = PGPainless.extractCertificate(secretKeys); + byte[] bytes = publicKeys.getEncoded(); + + PGPKeyRing keyRing = PGPainless.readKeyRing() + .keyRing(bytes); + + assertTrue(keyRing instanceof PGPPublicKeyRing); + assertArrayEquals(keyRing.getEncoded(), publicKeys.getEncoded()); + } + + @Test + public void testReadKeyRingWithBinarySecretKey() throws PGPException, InvalidAlgorithmParameterException, NoSuchAlgorithmException, IOException { + PGPSecretKeyRing secretKeys = PGPainless.generateKeyRing().modernKeyRing("Alice "); + byte[] bytes = secretKeys.getEncoded(); + + PGPKeyRing keyRing = PGPainless.readKeyRing() + .keyRing(bytes); + + assertTrue(keyRing instanceof PGPSecretKeyRing); + assertArrayEquals(keyRing.getEncoded(), secretKeys.getEncoded()); + } + + @Test + public void testReadKeyRingWithArmoredPublicKey() throws PGPException, InvalidAlgorithmParameterException, NoSuchAlgorithmException, IOException { + PGPSecretKeyRing secretKeys = PGPainless.generateKeyRing().modernKeyRing("Alice "); + PGPPublicKeyRing publicKeys = PGPainless.extractCertificate(secretKeys); + String armored = PGPainless.asciiArmor(publicKeys); + + PGPKeyRing keyRing = PGPainless.readKeyRing() + .keyRing(armored); + + assertTrue(keyRing instanceof PGPPublicKeyRing); + assertArrayEquals(keyRing.getEncoded(), publicKeys.getEncoded()); + } + + @Test + public void testReadKeyRingWithArmoredSecretKey() throws PGPException, InvalidAlgorithmParameterException, NoSuchAlgorithmException, IOException { + PGPSecretKeyRing secretKeys = PGPainless.generateKeyRing().modernKeyRing("Alice "); + String armored = PGPainless.asciiArmor(secretKeys); + + PGPKeyRing keyRing = PGPainless.readKeyRing() + .keyRing(armored); + + assertTrue(keyRing instanceof PGPSecretKeyRing); + assertArrayEquals(keyRing.getEncoded(), secretKeys.getEncoded()); + } }