1
0
Fork 0
mirror of https://github.com/pgpainless/pgpainless.git synced 2024-11-25 22:02:05 +01:00

Clean up code

This commit is contained in:
Paul Schaub 2021-12-28 12:18:17 +01:00
parent 6eac50c5b5
commit e96d668ee2
6 changed files with 11 additions and 41 deletions

View file

@ -10,34 +10,22 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.pgpainless.cli.TestUtils.ARMOR_PRIVATE_KEY_HEADER_BYTES; import static org.pgpainless.cli.TestUtils.ARMOR_PRIVATE_KEY_HEADER_BYTES;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.io.PrintStream; import java.io.PrintStream;
import java.util.Arrays; import java.util.Arrays;
import com.ginsberg.junit.exit.FailOnSystemExit; import com.ginsberg.junit.exit.FailOnSystemExit;
import org.bouncycastle.openpgp.PGPException;
import org.bouncycastle.openpgp.PGPSecretKeyRing; import org.bouncycastle.openpgp.PGPSecretKeyRing;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.pgpainless.PGPainless; import org.pgpainless.PGPainless;
import org.pgpainless.cli.PGPainlessCLI; import org.pgpainless.cli.PGPainlessCLI;
import org.pgpainless.cli.TestUtils;
import org.pgpainless.key.info.KeyRingInfo; import org.pgpainless.key.info.KeyRingInfo;
public class GenerateCertTest { public class GenerateCertTest {
private static File tempDir;
@BeforeAll
public static void setup() throws IOException {
tempDir = TestUtils.createTempDirectory();
}
@Test @Test
@FailOnSystemExit @FailOnSystemExit
public void testKeyGeneration() throws IOException, PGPException { public void testKeyGeneration() throws IOException {
ByteArrayOutputStream out = new ByteArrayOutputStream(); ByteArrayOutputStream out = new ByteArrayOutputStream();
System.setOut(new PrintStream(out)); System.setOut(new PrintStream(out));
PGPainlessCLI.execute("generate-key", "--armor", "Juliet Capulet <juliet@capulet.lit>"); PGPainlessCLI.execute("generate-key", "--armor", "Juliet Capulet <juliet@capulet.lit>");

View file

@ -41,8 +41,6 @@ public class SignVerifyTest {
private static File tempDir; private static File tempDir;
private static PrintStream originalSout; private static PrintStream originalSout;
private final String data = "If privacy is outlawed, only outlaws will have privacy.\n";
@BeforeAll @BeforeAll
public static void prepare() throws IOException { public static void prepare() throws IOException {
tempDir = TestUtils.createTempDirectory(); tempDir = TestUtils.createTempDirectory();
@ -72,6 +70,7 @@ public class SignVerifyTest {
aliceCertOut.close(); aliceCertOut.close();
// Write test data to disc // Write test data to disc
String data = "If privacy is outlawed, only outlaws will have privacy.\n";
File dataFile = new File(tempDir, "data"); File dataFile = new File(tempDir, "data");
assertTrue(dataFile.createNewFile()); assertTrue(dataFile.createNewFile());
FileOutputStream dataOut = new FileOutputStream(dataFile); FileOutputStream dataOut = new FileOutputStream(dataFile);

View file

@ -15,11 +15,8 @@ import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
import java.io.PrintStream; import java.io.PrintStream;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.security.InvalidAlgorithmParameterException;
import java.security.NoSuchAlgorithmException;
import com.ginsberg.junit.exit.ExpectSystemExitWithStatus; import com.ginsberg.junit.exit.ExpectSystemExitWithStatus;
import org.bouncycastle.openpgp.PGPException;
import org.bouncycastle.util.io.Streams; import org.bouncycastle.util.io.Streams;
import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeAll;
@ -96,8 +93,6 @@ public class SignUsingPublicKeyBehaviorTest {
private static File tempDir; private static File tempDir;
private static PrintStream originalSout; private static PrintStream originalSout;
private final String data = "If privacy is outlawed, only outlaws will have privacy.\n";
@BeforeAll @BeforeAll
public static void prepare() throws IOException { public static void prepare() throws IOException {
tempDir = TestUtils.createTempDirectory(); tempDir = TestUtils.createTempDirectory();
@ -105,7 +100,7 @@ public class SignUsingPublicKeyBehaviorTest {
@Test @Test
@ExpectSystemExitWithStatus(SOPGPException.BadData.EXIT_CODE) @ExpectSystemExitWithStatus(SOPGPException.BadData.EXIT_CODE)
public void testSignatureCreationAndVerification() throws PGPException, InvalidAlgorithmParameterException, NoSuchAlgorithmException, IOException { public void testSignatureCreationAndVerification() throws IOException {
originalSout = System.out; originalSout = System.out;
InputStream originalIn = System.in; InputStream originalIn = System.in;
@ -124,6 +119,8 @@ public class SignUsingPublicKeyBehaviorTest {
aliceCertOut.close(); aliceCertOut.close();
// Write test data to disc // Write test data to disc
String data = "If privacy is outlawed, only outlaws will have privacy.\n";
File dataFile = new File(tempDir, "data"); File dataFile = new File(tempDir, "data");
assertTrue(dataFile.createNewFile()); assertTrue(dataFile.createNewFile());
FileOutputStream dataOut = new FileOutputStream(dataFile); FileOutputStream dataOut = new FileOutputStream(dataFile);
@ -138,6 +135,8 @@ public class SignUsingPublicKeyBehaviorTest {
FileOutputStream sigOut = new FileOutputStream(sigFile); FileOutputStream sigOut = new FileOutputStream(sigFile);
System.setOut(new PrintStream(sigOut)); System.setOut(new PrintStream(sigOut));
PGPainlessCLI.execute("sign", "--armor", aliceKeyFile.getAbsolutePath()); PGPainlessCLI.execute("sign", "--armor", aliceKeyFile.getAbsolutePath());
System.setIn(originalIn);
} }
@AfterAll @AfterAll

View file

@ -294,7 +294,7 @@ public final class Policy {
} }
/** /**
* Return true if the the given hash algorithm is acceptable by this policy. * Return true if the given hash algorithm is acceptable by this policy.
* *
* @param hashAlgorithm hash algorithm * @param hashAlgorithm hash algorithm
* @return true if the hash algorithm is acceptable, false otherwise * @return true if the hash algorithm is acceptable, false otherwise

View file

@ -213,8 +213,8 @@ public abstract class SignatureValidator {
* @return policy * @return policy
*/ */
private static Policy.HashAlgorithmPolicy getHashAlgorithmPolicyForSignature(PGPSignature signature, Policy policy) { private static Policy.HashAlgorithmPolicy getHashAlgorithmPolicyForSignature(PGPSignature signature, Policy policy) {
Policy.HashAlgorithmPolicy hashAlgorithmPolicy = null;
SignatureType type = SignatureType.valueOf(signature.getSignatureType()); SignatureType type = SignatureType.valueOf(signature.getSignatureType());
Policy.HashAlgorithmPolicy hashAlgorithmPolicy;
if (type == SignatureType.CERTIFICATION_REVOCATION || type == SignatureType.KEY_REVOCATION || type == SignatureType.SUBKEY_REVOCATION) { if (type == SignatureType.CERTIFICATION_REVOCATION || type == SignatureType.KEY_REVOCATION || type == SignatureType.SUBKEY_REVOCATION) {
hashAlgorithmPolicy = policy.getRevocationSignatureHashAlgorithmPolicy(); hashAlgorithmPolicy = policy.getRevocationSignatureHashAlgorithmPolicy();
} else { } else {
@ -385,21 +385,6 @@ public abstract class SignatureValidator {
return new SignatureValidator() { return new SignatureValidator() {
@Override @Override
public void verify(PGPSignature signature) throws SignatureValidationException { public void verify(PGPSignature signature) throws SignatureValidationException {
// TODO: Uncommenting the code below would mean that fake issuers would become a problem for sig verification
/*
long keyId = signature.getKeyID();
if (keyId == 0) {
OpenPgpV4Fingerprint fingerprint = SignatureSubpacketsUtil.getIssuerFingerprintAsOpenPgpV4Fingerprint(signature);
if (fingerprint == null) {
throw new SignatureValidationException("Signature does not contain an issuer-id, neither an issuer-fingerprint subpacket.");
}
keyId = fingerprint.getKeyId();
}
if (keyId != key.getKeyID()) {
throw new IllegalArgumentException("Signature was not created using key " + Long.toHexString(key.getKeyID()));
}
*/
Date keyCreationTime = key.getCreationTime(); Date keyCreationTime = key.getCreationTime();
Date signatureCreationTime = signature.getCreationTime(); Date signatureCreationTime = signature.getCreationTime();
@ -505,7 +490,7 @@ public abstract class SignatureValidator {
public void verify(PGPSignature signature) throws SignatureValidationException { public void verify(PGPSignature signature) throws SignatureValidationException {
try { try {
signature.init(ImplementationFactory.getInstance().getPGPContentVerifierBuilderProvider(), signer); signature.init(ImplementationFactory.getInstance().getPGPContentVerifierBuilderProvider(), signer);
boolean valid = false; boolean valid;
if (signer.getKeyID() != signee.getKeyID()) { if (signer.getKeyID() != signee.getKeyID()) {
valid = signature.verifyCertification(signer, signee); valid = signature.verifyCertification(signer, signee);
} else { } else {

View file

@ -18,12 +18,11 @@ import sop.operation.Version;
public class VersionCmdTest { public class VersionCmdTest {
private SOP sop;
private Version version; private Version version;
@BeforeEach @BeforeEach
public void mockComponents() { public void mockComponents() {
sop = mock(SOP.class); SOP sop = mock(SOP.class);
version = mock(Version.class); version = mock(Version.class);
when(version.getName()).thenReturn("MockSop"); when(version.getName()).thenReturn("MockSop");
when(version.getVersion()).thenReturn("1.0"); when(version.getVersion()).thenReturn("1.0");