From 9921fc6ff6fbde7783df7989b5a992f3a4c7f0cd Mon Sep 17 00:00:00 2001 From: Paul Schaub Date: Wed, 18 May 2022 14:19:08 +0200 Subject: [PATCH] Add and test OpenPgpFingerprint.parseFromBinary(bytes) --- .../pgpainless/key/OpenPgpFingerprint.java | 11 +++++ .../key/OpenPgpV4FingerprintTest.java | 39 ++++++++++++++++++ .../key/OpenPgpV5FingerprintTest.java | 40 +++++++++++++++++++ 3 files changed, 90 insertions(+) diff --git a/pgpainless-core/src/main/java/org/pgpainless/key/OpenPgpFingerprint.java b/pgpainless-core/src/main/java/org/pgpainless/key/OpenPgpFingerprint.java index d5a1daad..6abe207b 100644 --- a/pgpainless-core/src/main/java/org/pgpainless/key/OpenPgpFingerprint.java +++ b/pgpainless-core/src/main/java/org/pgpainless/key/OpenPgpFingerprint.java @@ -67,6 +67,17 @@ public abstract class OpenPgpFingerprint implements CharSequence, Comparable OpenPgpFingerprint.parseFromBinary(binary)); + } } diff --git a/pgpainless-core/src/test/java/org/pgpainless/key/OpenPgpV5FingerprintTest.java b/pgpainless-core/src/test/java/org/pgpainless/key/OpenPgpV5FingerprintTest.java index fffccfe0..2e657d72 100644 --- a/pgpainless-core/src/test/java/org/pgpainless/key/OpenPgpV5FingerprintTest.java +++ b/pgpainless-core/src/test/java/org/pgpainless/key/OpenPgpV5FingerprintTest.java @@ -4,9 +4,11 @@ package org.pgpainless.key; +import org.bouncycastle.util.encoders.Hex; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; public class OpenPgpV5FingerprintTest { @@ -33,4 +35,42 @@ public class OpenPgpV5FingerprintTest { OpenPgpV5Fingerprint v5fp = (OpenPgpV5Fingerprint) parsed; assertEquals(prettyPrint, v5fp.prettyPrint()); } + + @Test + public void testParseFromBinary() { + String hex = "76543210ABCDEFAB01AB23CD1C0FFEE11EEFF0C1DC32BA10BAFEDCBA01234567"; + byte[] binary = Hex.decode(hex); + + OpenPgpFingerprint fingerprint = OpenPgpFingerprint.parseFromBinary(binary); + assertTrue(fingerprint instanceof OpenPgpV5Fingerprint); + assertEquals(hex, fingerprint.toString()); + } + + @Test + public void testParseFromBinary_leadingZeros() { + String hex = "000000000000000001AB23CD1C0FFEE11EEFF0C1DC32BA10BAFEDCBA01234567"; + byte[] binary = Hex.decode(hex); + + OpenPgpFingerprint fingerprint = OpenPgpFingerprint.parseFromBinary(binary); + assertTrue(fingerprint instanceof OpenPgpV5Fingerprint); + assertEquals(hex, fingerprint.toString()); + } + + @Test + public void testParseFromBinary_trailingZeros() { + String hex = "76543210ABCDEFAB01AB23CD1C0FFEE11EEFF0C1DC32BA100000000000000000"; + byte[] binary = Hex.decode(hex); + + OpenPgpFingerprint fingerprint = OpenPgpFingerprint.parseFromBinary(binary); + assertTrue(fingerprint instanceof OpenPgpV5Fingerprint); + assertEquals(hex, fingerprint.toString()); + } + + @Test + public void testParseFromBinary_wrongLength() { + String hex = "76543210ABCDEFAB01AB23CD1C0FFEE11EEFF0C1DC32BA10BAFEDCBA012345"; // missing 2 digits + byte[] binary = Hex.decode(hex); + + assertThrows(IllegalArgumentException.class, () -> OpenPgpFingerprint.parseFromBinary(binary)); + } }