1
0
Fork 0
mirror of https://github.com/pgpainless/pgpainless.git synced 2024-11-18 02:12:06 +01:00

Earlier catching of NPEs in tests

This commit is contained in:
Paul Schaub 2023-05-03 17:26:21 +02:00
parent e08505e07d
commit 92a5e559f8
Signed by: vanitasvitae
GPG key ID: 62BEE9264BF17311
13 changed files with 56 additions and 34 deletions

View file

@ -4,6 +4,8 @@
package org.bouncycastle; package org.bouncycastle;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.IOException; import java.io.IOException;

View file

@ -15,7 +15,6 @@ import java.nio.charset.StandardCharsets;
import java.security.InvalidAlgorithmParameterException; import java.security.InvalidAlgorithmParameterException;
import java.security.NoSuchAlgorithmException; import java.security.NoSuchAlgorithmException;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import org.bouncycastle.openpgp.PGPException; import org.bouncycastle.openpgp.PGPException;
import org.bouncycastle.openpgp.PGPPublicKey; import org.bouncycastle.openpgp.PGPPublicKey;
@ -63,7 +62,6 @@ public class VerifyWithMissingPublicKeyCallbackTest {
.withOptions(new ConsumerOptions() .withOptions(new ConsumerOptions()
.addVerificationCert(unrelatedKeys) .addVerificationCert(unrelatedKeys)
.setMissingCertificateCallback(new MissingPublicKeyCallback() { .setMissingCertificateCallback(new MissingPublicKeyCallback() {
@Nullable
@Override @Override
public PGPPublicKeyRing onMissingPublicKeyEncountered(@Nonnull Long keyId) { public PGPPublicKeyRing onMissingPublicKeyEncountered(@Nonnull Long keyId) {
assertEquals(signingKey.getKeyID(), (long) keyId, "Signing key-ID mismatch."); assertEquals(signingKey.getKeyID(), (long) keyId, "Signing key-ID mismatch.");

View file

@ -27,7 +27,7 @@ import org.pgpainless.algorithm.HashAlgorithm;
import org.pgpainless.algorithm.SignatureType; import org.pgpainless.algorithm.SignatureType;
import org.pgpainless.decryption_verification.ConsumerOptions; import org.pgpainless.decryption_verification.ConsumerOptions;
import org.pgpainless.decryption_verification.DecryptionStream; import org.pgpainless.decryption_verification.DecryptionStream;
import org.pgpainless.decryption_verification.OpenPgpMetadata; import org.pgpainless.decryption_verification.MessageMetadata;
import org.pgpainless.key.generation.type.rsa.RsaLength; import org.pgpainless.key.generation.type.rsa.RsaLength;
import org.pgpainless.key.protection.SecretKeyRingProtector; import org.pgpainless.key.protection.SecretKeyRingProtector;
@ -105,8 +105,8 @@ public class BcHashContextSignerTest {
Streams.pipeAll(decryptionStream, out); Streams.pipeAll(decryptionStream, out);
decryptionStream.close(); decryptionStream.close();
OpenPgpMetadata metadata = decryptionStream.getResult(); MessageMetadata metadata = decryptionStream.getMetadata();
assertTrue(metadata.isVerified()); assertTrue(metadata.isVerifiedSigned());
} }
private PGPSignature signMessage(byte[] message, HashAlgorithm hashAlgorithm, PGPSecretKeyRing secretKeys) private PGPSignature signMessage(byte[] message, HashAlgorithm hashAlgorithm, PGPSecretKeyRing secretKeys)

View file

@ -6,6 +6,7 @@ package org.pgpainless.encryption_signing;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.assertTrue;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
@ -123,6 +124,7 @@ public class FileInformationTest {
assertEquals("", decResult.getFilename()); assertEquals("", decResult.getFilename());
JUtils.assertDateEquals(PGPLiteralData.NOW, decResult.getModificationDate()); JUtils.assertDateEquals(PGPLiteralData.NOW, decResult.getModificationDate());
assertNotNull(decResult.getLiteralDataEncoding());
assertEquals(PGPLiteralData.BINARY, decResult.getLiteralDataEncoding().getCode()); assertEquals(PGPLiteralData.BINARY, decResult.getLiteralDataEncoding().getCode());
assertFalse(decResult.isForYourEyesOnly()); assertFalse(decResult.isForYourEyesOnly());
} }
@ -164,6 +166,7 @@ public class FileInformationTest {
assertEquals(PGPLiteralData.CONSOLE, decResult.getFilename()); assertEquals(PGPLiteralData.CONSOLE, decResult.getFilename());
JUtils.assertDateEquals(PGPLiteralData.NOW, decResult.getModificationDate()); JUtils.assertDateEquals(PGPLiteralData.NOW, decResult.getModificationDate());
assertNotNull(decResult.getLiteralDataEncoding());
assertEquals(PGPLiteralData.BINARY, decResult.getLiteralDataEncoding().getCode()); assertEquals(PGPLiteralData.BINARY, decResult.getLiteralDataEncoding().getCode());
assertTrue(decResult.isForYourEyesOnly()); assertTrue(decResult.isForYourEyesOnly());
} }

View file

@ -269,10 +269,13 @@ public class KeyRingInfoTest {
assertEquals(1, certKeys.size()); assertEquals(1, certKeys.size());
assertEquals(primaryKey.getKeyID(), certKeys.get(0).getKeyID()); assertEquals(primaryKey.getKeyID(), certKeys.get(0).getKeyID());
assertNotNull(info.getPrimaryKeyExpirationDate());
assertEquals(primaryKeyExpiration.getTime(), info.getPrimaryKeyExpirationDate().getTime(), 5); assertEquals(primaryKeyExpiration.getTime(), info.getPrimaryKeyExpirationDate().getTime(), 5);
// Encryption key expires after primary key, so we return primary key expiration instead. // Encryption key expires after primary key, so we return primary key expiration instead.
assertEquals(primaryKeyExpiration.getTime(), info.getExpirationDateForUse(KeyFlag.ENCRYPT_STORAGE).getTime(), 5); Date encryptExpDate = info.getExpirationDateForUse(KeyFlag.ENCRYPT_STORAGE);
assertNotNull(encryptExpDate);
assertEquals(primaryKeyExpiration.getTime(), encryptExpDate.getTime(), 5);
} }

View file

@ -6,6 +6,7 @@ package org.pgpainless.key.modification;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.assertTrue;
import java.io.IOException; import java.io.IOException;
@ -65,10 +66,12 @@ public class AddSubkeyWithModifiedBindingSignatureSubpackets {
assertFalse(signingKeys.isEmpty()); assertFalse(signingKeys.isEmpty());
PGPPublicKey newKey = signingKeys.get(0); PGPPublicKey newKey = signingKeys.get(0);
Date newExpirationDate = after.getSubkeyExpirationDate(new OpenPgpV4Fingerprint(newKey));
assertNotNull(newExpirationDate);
Date now = new Date(); Date now = new Date();
JUtils.assertEquals( JUtils.assertEquals(
now.getTime() + MILLIS_IN_SEC * secondsUntilExpiration, now.getTime() + MILLIS_IN_SEC * secondsUntilExpiration,
after.getSubkeyExpirationDate(new OpenPgpV4Fingerprint(newKey)).getTime(), 2 * MILLIS_IN_SEC); newExpirationDate.getTime(), 2 * MILLIS_IN_SEC);
assertTrue(newKey.getSignatures().hasNext()); assertTrue(newKey.getSignatures().hasNext());
PGPSignature binding = newKey.getSignatures().next(); PGPSignature binding = newKey.getSignatures().next();
List<NotationData> notations = SignatureSubpacketsUtil.getHashedNotationData(binding); List<NotationData> notations = SignatureSubpacketsUtil.getHashedNotationData(binding);

View file

@ -4,22 +4,11 @@
package org.pgpainless.key.modification; package org.pgpainless.key.modification;
import org.bouncycastle.openpgp.PGPException; import static org.junit.jupiter.api.Assertions.assertEquals;
import org.bouncycastle.openpgp.PGPPublicKeyRing; import static org.junit.jupiter.api.Assertions.assertFalse;
import org.bouncycastle.openpgp.PGPSecretKeyRing; import static org.junit.jupiter.api.Assertions.assertNotNull;
import org.bouncycastle.util.io.Streams; import static org.junit.jupiter.api.Assertions.assertThrows;
import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertTrue;
import org.pgpainless.PGPainless;
import org.pgpainless.decryption_verification.ConsumerOptions;
import org.pgpainless.decryption_verification.DecryptionStream;
import org.pgpainless.decryption_verification.OpenPgpMetadata;
import org.pgpainless.encryption_signing.EncryptionOptions;
import org.pgpainless.encryption_signing.EncryptionResult;
import org.pgpainless.encryption_signing.EncryptionStream;
import org.pgpainless.encryption_signing.ProducerOptions;
import org.pgpainless.key.info.KeyRingInfo;
import org.pgpainless.key.protection.SecretKeyRingProtector;
import org.pgpainless.signature.subpackets.SelfSignatureSubpackets;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
@ -27,10 +16,23 @@ import java.io.IOException;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.NoSuchElementException; import java.util.NoSuchElementException;
import static org.junit.jupiter.api.Assertions.assertEquals; import org.bouncycastle.openpgp.PGPException;
import static org.junit.jupiter.api.Assertions.assertFalse; import org.bouncycastle.openpgp.PGPPublicKeyRing;
import static org.junit.jupiter.api.Assertions.assertThrows; import org.bouncycastle.openpgp.PGPSecretKeyRing;
import static org.junit.jupiter.api.Assertions.assertTrue; import org.bouncycastle.openpgp.PGPSignature;
import org.bouncycastle.util.io.Streams;
import org.junit.jupiter.api.Test;
import org.pgpainless.PGPainless;
import org.pgpainless.decryption_verification.ConsumerOptions;
import org.pgpainless.decryption_verification.DecryptionStream;
import org.pgpainless.decryption_verification.MessageMetadata;
import org.pgpainless.encryption_signing.EncryptionOptions;
import org.pgpainless.encryption_signing.EncryptionResult;
import org.pgpainless.encryption_signing.EncryptionStream;
import org.pgpainless.encryption_signing.ProducerOptions;
import org.pgpainless.key.info.KeyRingInfo;
import org.pgpainless.key.protection.SecretKeyRingProtector;
import org.pgpainless.signature.subpackets.SelfSignatureSubpackets;
/** /**
* Test for #298. * Test for #298.
@ -130,7 +132,9 @@ public class FixUserIdDoesNotBreakEncryptionCapabilityTest {
assertTrue(info.isUserIdValid(userIdAfter)); assertTrue(info.isUserIdValid(userIdAfter));
assertEquals(userIdAfter, info.getPrimaryUserId()); assertEquals(userIdAfter, info.getPrimaryUserId());
assertTrue(info.getLatestUserIdCertification(userIdAfter).getHashedSubPackets().isPrimaryUserID()); PGPSignature latestCertification = info.getLatestUserIdCertification(userIdAfter);
assertNotNull(latestCertification);
assertTrue(latestCertification.getHashedSubPackets().isPrimaryUserID());
PGPPublicKeyRing cert = PGPainless.extractCertificate(edited); PGPPublicKeyRing cert = PGPainless.extractCertificate(edited);
@ -156,7 +160,7 @@ public class FixUserIdDoesNotBreakEncryptionCapabilityTest {
Streams.pipeAll(decryptionStream, plain); Streams.pipeAll(decryptionStream, plain);
decryptionStream.close(); decryptionStream.close();
OpenPgpMetadata metadata = decryptionStream.getResult(); MessageMetadata metadata = decryptionStream.getMetadata();
assertTrue(metadata.isEncrypted()); assertTrue(metadata.isEncrypted());
} }
} }

View file

@ -7,6 +7,7 @@ package org.pgpainless.key.modification;
import static org.junit.jupiter.api.Assertions.assertArrayEquals; import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals; import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import java.security.InvalidAlgorithmParameterException; import java.security.InvalidAlgorithmParameterException;
import java.security.NoSuchAlgorithmException; import java.security.NoSuchAlgorithmException;
@ -34,6 +35,7 @@ public class OldSignatureSubpacketsArePreservedOnNewSigTest {
.simpleEcKeyRing("Alice <alice@wonderland.lit>"); .simpleEcKeyRing("Alice <alice@wonderland.lit>");
PGPSignature oldSignature = PGPainless.inspectKeyRing(secretKeys).getLatestUserIdCertification("Alice <alice@wonderland.lit>"); PGPSignature oldSignature = PGPainless.inspectKeyRing(secretKeys).getLatestUserIdCertification("Alice <alice@wonderland.lit>");
assertNotNull(oldSignature);
PGPSignatureSubpacketVector oldPackets = oldSignature.getHashedSubPackets(); PGPSignatureSubpacketVector oldPackets = oldSignature.getHashedSubPackets();
assertEquals(0, oldPackets.getKeyExpirationTime()); assertEquals(0, oldPackets.getKeyExpirationTime());
@ -46,6 +48,7 @@ public class OldSignatureSubpacketsArePreservedOnNewSigTest {
.setExpirationDate(expiration, new UnprotectedKeysProtector()) .setExpirationDate(expiration, new UnprotectedKeysProtector())
.done(); .done();
PGPSignature newSignature = PGPainless.inspectKeyRing(secretKeys, t1).getLatestUserIdCertification("Alice <alice@wonderland.lit>"); PGPSignature newSignature = PGPainless.inspectKeyRing(secretKeys, t1).getLatestUserIdCertification("Alice <alice@wonderland.lit>");
assertNotNull(newSignature);
PGPSignatureSubpacketVector newPackets = newSignature.getHashedSubPackets(); PGPSignatureSubpacketVector newPackets = newSignature.getHashedSubPackets();
assertNotEquals(0, newPackets.getKeyExpirationTime()); assertNotEquals(0, newPackets.getKeyExpirationTime());

View file

@ -5,6 +5,7 @@
package org.pgpainless.key.modification; package org.pgpainless.key.modification;
import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.assertTrue;
@ -81,6 +82,7 @@ public class RevokeUserIdsTest {
assertTrue(info.isUserIdValid("Alice <alice@example.org>")); assertTrue(info.isUserIdValid("Alice <alice@example.org>"));
PGPSignature revocation = info.getUserIdRevocation("Allice <alice@example.org>"); PGPSignature revocation = info.getUserIdRevocation("Allice <alice@example.org>");
assertNotNull(revocation);
assertFalse(RevocationAttributes.Reason.isHardRevocation( assertFalse(RevocationAttributes.Reason.isHardRevocation(
revocation.getHashedSubPackets().getRevocationReason().getRevocationReason())); revocation.getHashedSubPackets().getRevocationReason().getRevocationReason()));

View file

@ -129,7 +129,8 @@ public class CachingSecretKeyRingProtectorTest {
Passphrase passphrase = withCallback.getPassphraseFor(x); Passphrase passphrase = withCallback.getPassphraseFor(x);
assertNotNull(passphrase); assertNotNull(passphrase);
assertEquals(doubled, (long) Long.valueOf(new String(passphrase.getChars()))); assertNotNull(passphrase.getChars());
assertEquals(doubled, Long.parseLong(new String(passphrase.getChars())));
} }
} }

View file

@ -24,7 +24,7 @@ import org.junit.jupiter.api.extension.ExtendWith;
import org.pgpainless.PGPainless; import org.pgpainless.PGPainless;
import org.pgpainless.decryption_verification.ConsumerOptions; import org.pgpainless.decryption_verification.ConsumerOptions;
import org.pgpainless.decryption_verification.DecryptionStream; import org.pgpainless.decryption_verification.DecryptionStream;
import org.pgpainless.decryption_verification.OpenPgpMetadata; import org.pgpainless.decryption_verification.MessageMetadata;
import org.pgpainless.exception.SignatureValidationException; import org.pgpainless.exception.SignatureValidationException;
import org.pgpainless.policy.Policy; import org.pgpainless.policy.Policy;
import org.pgpainless.signature.consumer.CertificateValidator; import org.pgpainless.signature.consumer.CertificateValidator;
@ -1391,7 +1391,7 @@ public class CertificateValidatorTest {
Streams.drain(decryptionStream); Streams.drain(decryptionStream);
decryptionStream.close(); decryptionStream.close();
OpenPgpMetadata metadata = decryptionStream.getResult(); MessageMetadata metadata = decryptionStream.getMetadata();
assertFalse(metadata.getVerifiedSignatures().isEmpty()); assertFalse(metadata.getVerifiedSignatures().isEmpty());
} }

View file

@ -5,10 +5,12 @@
package org.pgpainless.signature; package org.pgpainless.signature;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import java.io.IOException; import java.io.IOException;
import java.util.List; import java.util.List;
import org.bouncycastle.bcpg.sig.IssuerKeyID;
import org.bouncycastle.bcpg.sig.NotationData; import org.bouncycastle.bcpg.sig.NotationData;
import org.bouncycastle.openpgp.PGPException; import org.bouncycastle.openpgp.PGPException;
import org.bouncycastle.openpgp.PGPSignature; import org.bouncycastle.openpgp.PGPSignature;
@ -80,7 +82,9 @@ public class SignatureStructureTest {
@Test @Test
public void testGetIssuer() { public void testGetIssuer() {
assertEquals(KeyIdUtil.fromLongKeyId("FBFCC82A015E7330"), SignatureSubpacketsUtil.getIssuerKeyId(signature).getKeyID()); IssuerKeyID issuerKeyID = SignatureSubpacketsUtil.getIssuerKeyId(signature);
assertNotNull(issuerKeyID);
assertEquals(KeyIdUtil.fromLongKeyId("FBFCC82A015E7330"), issuerKeyID.getKeyID());
} }
@Test @Test

View file

@ -96,7 +96,6 @@ public class ArmorUtilsTest {
ArmoredOutputStream armor = new ArmoredOutputStream(new ByteArrayOutputStream()); ArmoredOutputStream armor = new ArmoredOutputStream(new ByteArrayOutputStream());
ArmorUtils.addMessageIdHeader(armor, "abcdefghi01234567890123456789012"); ArmorUtils.addMessageIdHeader(armor, "abcdefghi01234567890123456789012");
assertThrows(NullPointerException.class, () -> ArmorUtils.addMessageIdHeader(armor, null));
assertThrows(IllegalArgumentException.class, () -> ArmorUtils.addMessageIdHeader(armor, "tooShort")); assertThrows(IllegalArgumentException.class, () -> ArmorUtils.addMessageIdHeader(armor, "tooShort"));
assertThrows(IllegalArgumentException.class, () -> ArmorUtils.addMessageIdHeader(armor, "toLooooooooooooooooooooooooooooooooooong")); assertThrows(IllegalArgumentException.class, () -> ArmorUtils.addMessageIdHeader(armor, "toLooooooooooooooooooooooooooooooooooong"));
assertThrows(IllegalArgumentException.class, () -> ArmorUtils.addMessageIdHeader(armor, "contains spaces 7890123456789012")); assertThrows(IllegalArgumentException.class, () -> ArmorUtils.addMessageIdHeader(armor, "contains spaces 7890123456789012"));