1
0
Fork 0
mirror of https://github.com/pgpainless/pgpainless.git synced 2025-01-09 11:48:00 +01:00

Abort (skip) tests reading from resources

This commit is contained in:
Paul Schaub 2022-07-21 21:34:44 +02:00
parent cb8e0d7951
commit c4bffad478
Signed by: vanitasvitae
GPG key ID: 62BEE9264BF17311

View file

@ -14,6 +14,7 @@ import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.net.URI;
import java.net.URISyntaxException; import java.net.URISyntaxException;
import java.net.URL; import java.net.URL;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
@ -50,11 +51,38 @@ class KeyRingReaderTest {
private InputStream requireResource(String resourceName) { private InputStream requireResource(String resourceName) {
InputStream inputStream = getClass().getClassLoader().getResourceAsStream(resourceName); InputStream inputStream = getClass().getClassLoader().getResourceAsStream(resourceName);
if (inputStream == null) { if (inputStream == null) {
throw new TestAbortedException("Cannot read resource " + resourceName); throw new TestAbortedException("Cannot read resource " + resourceName + ": InputStream is null.");
} }
return inputStream; 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 @Test
public void assertThatPGPUtilsDetectAsciiArmoredData() throws IOException, PGPException { public void assertThatPGPUtilsDetectAsciiArmoredData() throws IOException, PGPException {
InputStream inputStream = requireResource("pub_keys_10_pieces.asc"); InputStream inputStream = requireResource("pub_keys_10_pieces.asc");
@ -93,18 +121,16 @@ class KeyRingReaderTest {
} }
@Test @Test
void publicKeyRingCollectionFromString() throws IOException, PGPException, URISyntaxException { void publicKeyRingCollectionFromString() throws IOException, PGPException {
URL resource = getClass().getClassLoader().getResource("pub_keys_10_pieces.asc"); String armoredString = new String(readFromResource("pub_keys_10_pieces.asc"));
String armoredString = new String(Files.readAllBytes(new File(resource.toURI()).toPath()));
InputStream inputStream = new ByteArrayInputStream(armoredString.getBytes(StandardCharsets.UTF_8)); InputStream inputStream = new ByteArrayInputStream(armoredString.getBytes(StandardCharsets.UTF_8));
PGPPublicKeyRingCollection rings = PGPainless.readKeyRing().publicKeyRingCollection(inputStream); PGPPublicKeyRingCollection rings = PGPainless.readKeyRing().publicKeyRingCollection(inputStream);
assertEquals(10, rings.size()); assertEquals(10, rings.size());
} }
@Test @Test
void publicKeyRingCollectionFromBytes() throws IOException, PGPException, URISyntaxException { void publicKeyRingCollectionFromBytes() throws IOException, PGPException {
URL resource = getClass().getClassLoader().getResource("pub_keys_10_pieces.asc"); byte[] bytes = readFromResource("pub_keys_10_pieces.asc");
byte[] bytes = Files.readAllBytes(new File(resource.toURI()).toPath());
InputStream byteArrayInputStream = new ByteArrayInputStream(bytes); InputStream byteArrayInputStream = new ByteArrayInputStream(bytes);
PGPPublicKeyRingCollection rings = PGPainless.readKeyRing().publicKeyRingCollection(byteArrayInputStream); PGPPublicKeyRingCollection rings = PGPainless.readKeyRing().publicKeyRingCollection(byteArrayInputStream);
assertEquals(10, rings.size()); assertEquals(10, rings.size());
@ -114,7 +140,7 @@ class KeyRingReaderTest {
* One armored pub key * One armored pub key
*/ */
@Test @Test
void parsePublicKeysSingleArmored() throws IOException, PGPException, URISyntaxException { void parsePublicKeysSingleArmored() throws IOException, PGPException {
assertEquals(1, getPgpPublicKeyRingsFromResource("single_pub_key_armored.asc").size()); assertEquals(1, getPgpPublicKeyRingsFromResource("single_pub_key_armored.asc").size());
} }
@ -122,7 +148,7 @@ class KeyRingReaderTest {
* One binary pub key * One binary pub key
*/ */
@Test @Test
void parsePublicKeysSingleBinary() throws IOException, PGPException, URISyntaxException { void parsePublicKeysSingleBinary() throws IOException, PGPException {
assertEquals(1, getPgpPublicKeyRingsFromResource("single_pub_key_binary.key").size()); 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----- * Many armored pub keys with a single -----BEGIN PGP PUBLIC KEY BLOCK-----...-----END PGP PUBLIC KEY BLOCK-----
*/ */
@Test @Test
void parsePublicKeysMultiplyArmoredSingleHeader() throws IOException, PGPException, URISyntaxException { void parsePublicKeysMultiplyArmoredSingleHeader() throws IOException, PGPException {
assertEquals(10, getPgpPublicKeyRingsFromResource("10_pub_keys_armored_single_header.asc").size()); 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----- * Many armored pub keys where each has own -----BEGIN PGP PUBLIC KEY BLOCK-----...-----END PGP PUBLIC KEY BLOCK-----
*/ */
@Test @Test
void parsePublicKeysMultiplyArmoredOwnHeader() throws IOException, PGPException, URISyntaxException { void parsePublicKeysMultiplyArmoredOwnHeader() throws IOException, PGPException {
assertEquals(10, getPgpPublicKeyRingsFromResource("10_pub_keys_armored_own_header.asc").size()); 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. * Each of those blocks can have a different count of keys.
*/ */
@Test @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()); assertEquals(10, getPgpPublicKeyRingsFromResource("10_pub_keys_armored_own_with_single_header.asc").size());
} }
@ -155,7 +181,7 @@ class KeyRingReaderTest {
* Many binary pub keys * Many binary pub keys
*/ */
@Test @Test
void parsePublicKeysMultiplyBinary() throws IOException, PGPException, URISyntaxException { void parsePublicKeysMultiplyBinary() throws IOException, PGPException {
assertEquals(10, getPgpPublicKeyRingsFromResource("10_pub_keys_binary.key").size()); assertEquals(10, getPgpPublicKeyRingsFromResource("10_pub_keys_binary.key").size());
} }
@ -163,7 +189,7 @@ class KeyRingReaderTest {
* One armored private key * One armored private key
*/ */
@Test @Test
void parseSecretKeysSingleArmored() throws IOException, PGPException, URISyntaxException { void parseSecretKeysSingleArmored() throws IOException, PGPException {
assertEquals(1, getPgpSecretKeyRingsFromResource("single_prv_key_armored.asc").size()); assertEquals(1, getPgpSecretKeyRingsFromResource("single_prv_key_armored.asc").size());
} }
@ -171,7 +197,7 @@ class KeyRingReaderTest {
* One binary private key * One binary private key
*/ */
@Test @Test
void parseSecretKeysSingleBinary() throws IOException, PGPException, URISyntaxException { void parseSecretKeysSingleBinary() throws IOException, PGPException {
assertEquals(1, getPgpSecretKeyRingsFromResource("single_prv_key_binary.key").size()); 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----- * -----BEGIN PGP PRIVATE KEY BLOCK-----...-----END PGP PRIVATE KEY BLOCK-----
*/ */
@Test @Test
void parseSecretKeysMultiplyArmoredSingleHeader() throws IOException, PGPException, URISyntaxException { void parseSecretKeysMultiplyArmoredSingleHeader() throws IOException, PGPException {
assertEquals(10, getPgpSecretKeyRingsFromResource("10_prv_keys_armored_single_header.asc").size()); 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----- * Many armored private keys where each has own -----BEGIN PGP PRIVATE KEY BLOCK-----...-----END PGP PRIVATE KEY BLOCK-----
*/ */
@Test @Test
void parseSecretKeysMultiplyArmoredOwnHeader() throws IOException, PGPException, URISyntaxException { void parseSecretKeysMultiplyArmoredOwnHeader() throws IOException, PGPException {
assertEquals(10, getPgpSecretKeyRingsFromResource("10_prv_keys_armored_own_header.asc").size()); 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. * Each of those blocks can have a different count of keys.
*/ */
@Test @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()); assertEquals(10, getPgpSecretKeyRingsFromResource("10_prv_keys_armored_own_with_single_header.asc").size());
} }
@ -205,7 +231,7 @@ class KeyRingReaderTest {
* Many binary private keys * Many binary private keys
*/ */
@Test @Test
void parseSecretKeysMultiplyBinary() throws IOException, PGPException, URISyntaxException { void parseSecretKeysMultiplyBinary() throws IOException, PGPException {
assertEquals(10, getPgpSecretKeyRingsFromResource("10_prv_keys_binary.key").size()); 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----- * Many armored keys(private or pub) where each has own -----BEGIN PGP ... KEY BLOCK-----...-----END PGP ... KEY BLOCK-----
*/ */
@Test @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()); 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. * Each of those blocks can have a different count of keys.
*/ */
@Test @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()); 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) * Many binary keys(private or pub)
*/ */
@Test @Test
void parseKeysMultiplyBinary() throws IOException, PGPException, URISyntaxException { void parseKeysMultiplyBinary() throws IOException, PGPException {
assertEquals(10, getPGPKeyRingsFromResource("10_prv_and_pub_keys_binary.key").size()); assertEquals(10, getPGPKeyRingsFromResource("10_prv_and_pub_keys_binary.key").size());
} }
private InputStream getFileInputStreamFromResource(String fileName) throws IOException, URISyntaxException { private InputStream getFileInputStreamFromResource(String fileName) throws IOException {
URL resource = getClass().getClassLoader().getResource(fileName); return new FileInputStream(getFileFromResource(fileName));
assert resource != null;
return new FileInputStream(new File(resource.toURI()));
} }
private PGPKeyRingCollection getPGPKeyRingsFromResource(String fileName) private PGPKeyRingCollection getPGPKeyRingsFromResource(String fileName)
throws IOException, URISyntaxException, PGPException { throws IOException, PGPException {
return PGPainless.readKeyRing().keyRingCollection(getFileInputStreamFromResource(fileName), true); return PGPainless.readKeyRing().keyRingCollection(getFileInputStreamFromResource(fileName), true);
} }
private PGPPublicKeyRingCollection getPgpPublicKeyRingsFromResource(String fileName) private PGPPublicKeyRingCollection getPgpPublicKeyRingsFromResource(String fileName)
throws IOException, URISyntaxException, PGPException { throws IOException, PGPException {
return PGPainless.readKeyRing().publicKeyRingCollection(getFileInputStreamFromResource(fileName)); return PGPainless.readKeyRing().publicKeyRingCollection(getFileInputStreamFromResource(fileName));
} }
private PGPSecretKeyRingCollection getPgpSecretKeyRingsFromResource(String fileName) private PGPSecretKeyRingCollection getPgpSecretKeyRingsFromResource(String fileName)
throws IOException, URISyntaxException, PGPException { throws IOException, PGPException {
return PGPainless.readKeyRing().secretKeyRingCollection(getFileInputStreamFromResource(fileName)); return PGPainless.readKeyRing().secretKeyRingCollection(getFileInputStreamFromResource(fileName));
} }