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 9349a864..b3fe66d4 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 @@ -14,6 +14,7 @@ import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; +import java.net.URI; import java.net.URISyntaxException; import java.net.URL; import java.nio.charset.StandardCharsets; @@ -50,11 +51,38 @@ class KeyRingReaderTest { private InputStream requireResource(String resourceName) { InputStream inputStream = getClass().getClassLoader().getResourceAsStream(resourceName); if (inputStream == null) { - throw new TestAbortedException("Cannot read resource " + resourceName); + throw new TestAbortedException("Cannot read resource " + resourceName + ": InputStream is null."); } return inputStream; } + private URI getResourceURI(String resourceName) { + try { + URL url = getClass().getClassLoader().getResource(resourceName); + if (url == null) { + throw new TestAbortedException("Cannot read resource " + resourceName + ": URL is null."); + } + return url.toURI(); + } catch (URISyntaxException | IllegalArgumentException e) { + throw new TestAbortedException("Cannot read resource " + resourceName, e); + } + } + + private File getFileFromResource(String resourceName) { + URI uri = getResourceURI(resourceName); + try { + return new File(uri); + } catch (IllegalArgumentException e) { + // When executing the tests from pgpainless-test.jar, we cannot read resources as + // URI is not hierarchical. + throw new TestAbortedException("Cannot read resource " + resourceName, e); + } + } + + private byte[] readFromResource(String resourceName) throws IOException { + return Files.readAllBytes(getFileFromResource(resourceName).toPath()); + } + @Test public void assertThatPGPUtilsDetectAsciiArmoredData() throws IOException, PGPException { InputStream inputStream = requireResource("pub_keys_10_pieces.asc"); @@ -93,18 +121,16 @@ class KeyRingReaderTest { } @Test - void publicKeyRingCollectionFromString() throws IOException, PGPException, URISyntaxException { - URL resource = getClass().getClassLoader().getResource("pub_keys_10_pieces.asc"); - String armoredString = new String(Files.readAllBytes(new File(resource.toURI()).toPath())); + void publicKeyRingCollectionFromString() throws IOException, PGPException { + String armoredString = new String(readFromResource("pub_keys_10_pieces.asc")); InputStream inputStream = new ByteArrayInputStream(armoredString.getBytes(StandardCharsets.UTF_8)); PGPPublicKeyRingCollection rings = PGPainless.readKeyRing().publicKeyRingCollection(inputStream); assertEquals(10, rings.size()); } @Test - void publicKeyRingCollectionFromBytes() throws IOException, PGPException, URISyntaxException { - URL resource = getClass().getClassLoader().getResource("pub_keys_10_pieces.asc"); - byte[] bytes = Files.readAllBytes(new File(resource.toURI()).toPath()); + void publicKeyRingCollectionFromBytes() throws IOException, PGPException { + byte[] bytes = readFromResource("pub_keys_10_pieces.asc"); InputStream byteArrayInputStream = new ByteArrayInputStream(bytes); PGPPublicKeyRingCollection rings = PGPainless.readKeyRing().publicKeyRingCollection(byteArrayInputStream); assertEquals(10, rings.size()); @@ -114,7 +140,7 @@ class KeyRingReaderTest { * One armored pub key */ @Test - void parsePublicKeysSingleArmored() throws IOException, PGPException, URISyntaxException { + void parsePublicKeysSingleArmored() throws IOException, PGPException { assertEquals(1, getPgpPublicKeyRingsFromResource("single_pub_key_armored.asc").size()); } @@ -122,7 +148,7 @@ class KeyRingReaderTest { * One binary pub key */ @Test - void parsePublicKeysSingleBinary() throws IOException, PGPException, URISyntaxException { + void parsePublicKeysSingleBinary() throws IOException, PGPException { assertEquals(1, getPgpPublicKeyRingsFromResource("single_pub_key_binary.key").size()); } @@ -130,7 +156,7 @@ class KeyRingReaderTest { * Many armored pub keys with a single -----BEGIN PGP PUBLIC KEY BLOCK-----...-----END PGP PUBLIC KEY BLOCK----- */ @Test - void parsePublicKeysMultiplyArmoredSingleHeader() throws IOException, PGPException, URISyntaxException { + void parsePublicKeysMultiplyArmoredSingleHeader() throws IOException, PGPException { assertEquals(10, getPgpPublicKeyRingsFromResource("10_pub_keys_armored_single_header.asc").size()); } @@ -138,7 +164,7 @@ class KeyRingReaderTest { * Many armored pub keys where each has own -----BEGIN PGP PUBLIC KEY BLOCK-----...-----END PGP PUBLIC KEY BLOCK----- */ @Test - void parsePublicKeysMultiplyArmoredOwnHeader() throws IOException, PGPException, URISyntaxException { + void parsePublicKeysMultiplyArmoredOwnHeader() throws IOException, PGPException { assertEquals(10, getPgpPublicKeyRingsFromResource("10_pub_keys_armored_own_header.asc").size()); } @@ -147,7 +173,7 @@ class KeyRingReaderTest { * Each of those blocks can have a different count of keys. */ @Test - void parsePublicKeysMultiplyArmoredOwnWithSingleHeader() throws IOException, PGPException, URISyntaxException { + void parsePublicKeysMultiplyArmoredOwnWithSingleHeader() throws IOException, PGPException { assertEquals(10, getPgpPublicKeyRingsFromResource("10_pub_keys_armored_own_with_single_header.asc").size()); } @@ -155,7 +181,7 @@ class KeyRingReaderTest { * Many binary pub keys */ @Test - void parsePublicKeysMultiplyBinary() throws IOException, PGPException, URISyntaxException { + void parsePublicKeysMultiplyBinary() throws IOException, PGPException { assertEquals(10, getPgpPublicKeyRingsFromResource("10_pub_keys_binary.key").size()); } @@ -163,7 +189,7 @@ class KeyRingReaderTest { * One armored private key */ @Test - void parseSecretKeysSingleArmored() throws IOException, PGPException, URISyntaxException { + void parseSecretKeysSingleArmored() throws IOException, PGPException { assertEquals(1, getPgpSecretKeyRingsFromResource("single_prv_key_armored.asc").size()); } @@ -171,7 +197,7 @@ class KeyRingReaderTest { * One binary private key */ @Test - void parseSecretKeysSingleBinary() throws IOException, PGPException, URISyntaxException { + void parseSecretKeysSingleBinary() throws IOException, PGPException { assertEquals(1, getPgpSecretKeyRingsFromResource("single_prv_key_binary.key").size()); } @@ -180,7 +206,7 @@ class KeyRingReaderTest { * -----BEGIN PGP PRIVATE KEY BLOCK-----...-----END PGP PRIVATE KEY BLOCK----- */ @Test - void parseSecretKeysMultiplyArmoredSingleHeader() throws IOException, PGPException, URISyntaxException { + void parseSecretKeysMultiplyArmoredSingleHeader() throws IOException, PGPException { assertEquals(10, getPgpSecretKeyRingsFromResource("10_prv_keys_armored_single_header.asc").size()); } @@ -188,7 +214,7 @@ class KeyRingReaderTest { * Many armored private keys where each has own -----BEGIN PGP PRIVATE KEY BLOCK-----...-----END PGP PRIVATE KEY BLOCK----- */ @Test - void parseSecretKeysMultiplyArmoredOwnHeader() throws IOException, PGPException, URISyntaxException { + void parseSecretKeysMultiplyArmoredOwnHeader() throws IOException, PGPException { assertEquals(10, getPgpSecretKeyRingsFromResource("10_prv_keys_armored_own_header.asc").size()); } @@ -197,7 +223,7 @@ class KeyRingReaderTest { * Each of those blocks can have a different count of keys. */ @Test - void parseSecretKeysMultiplyArmoredOwnWithSingleHeader() throws IOException, PGPException, URISyntaxException { + void parseSecretKeysMultiplyArmoredOwnWithSingleHeader() throws IOException, PGPException { assertEquals(10, getPgpSecretKeyRingsFromResource("10_prv_keys_armored_own_with_single_header.asc").size()); } @@ -205,7 +231,7 @@ class KeyRingReaderTest { * Many binary private keys */ @Test - void parseSecretKeysMultiplyBinary() throws IOException, PGPException, URISyntaxException { + void parseSecretKeysMultiplyBinary() throws IOException, PGPException { assertEquals(10, getPgpSecretKeyRingsFromResource("10_prv_keys_binary.key").size()); } @@ -213,7 +239,7 @@ class KeyRingReaderTest { * Many armored keys(private or pub) where each has own -----BEGIN PGP ... KEY BLOCK-----...-----END PGP ... KEY BLOCK----- */ @Test - void parseKeysMultiplyArmoredOwnHeader() throws IOException, PGPException, URISyntaxException { + void parseKeysMultiplyArmoredOwnHeader() throws IOException, PGPException { assertEquals(10, getPGPKeyRingsFromResource("10_prv_and_pub_keys_armored_own_header.asc").size()); } @@ -222,7 +248,7 @@ class KeyRingReaderTest { * Each of those blocks can have a different count of keys. */ @Test - void parseKeysMultiplyArmoredOwnWithSingleHeader() throws IOException, PGPException, URISyntaxException { + void parseKeysMultiplyArmoredOwnWithSingleHeader() throws IOException, PGPException { assertEquals(10, getPGPKeyRingsFromResource("10_prv_and_pub_keys_armored_own_with_single_header.asc").size()); } @@ -230,28 +256,26 @@ class KeyRingReaderTest { * Many binary keys(private or pub) */ @Test - void parseKeysMultiplyBinary() throws IOException, PGPException, URISyntaxException { + void parseKeysMultiplyBinary() throws IOException, PGPException { assertEquals(10, getPGPKeyRingsFromResource("10_prv_and_pub_keys_binary.key").size()); } - private InputStream getFileInputStreamFromResource(String fileName) throws IOException, URISyntaxException { - URL resource = getClass().getClassLoader().getResource(fileName); - assert resource != null; - return new FileInputStream(new File(resource.toURI())); + private InputStream getFileInputStreamFromResource(String fileName) throws IOException { + return new FileInputStream(getFileFromResource(fileName)); } private PGPKeyRingCollection getPGPKeyRingsFromResource(String fileName) - throws IOException, URISyntaxException, PGPException { + throws IOException, PGPException { return PGPainless.readKeyRing().keyRingCollection(getFileInputStreamFromResource(fileName), true); } private PGPPublicKeyRingCollection getPgpPublicKeyRingsFromResource(String fileName) - throws IOException, URISyntaxException, PGPException { + throws IOException, PGPException { return PGPainless.readKeyRing().publicKeyRingCollection(getFileInputStreamFromResource(fileName)); } private PGPSecretKeyRingCollection getPgpSecretKeyRingsFromResource(String fileName) - throws IOException, URISyntaxException, PGPException { + throws IOException, PGPException { return PGPainless.readKeyRing().secretKeyRingCollection(getFileInputStreamFromResource(fileName)); }