mirror of
https://github.com/pgpainless/pgpainless.git
synced 2024-12-22 19:08:00 +01:00
Remove dependency on deprecated com.ginsberg:junit5-system-exit library
This commit is contained in:
parent
391549a7d6
commit
3e96af5450
5 changed files with 56 additions and 155 deletions
|
@ -32,8 +32,6 @@ dependencies {
|
|||
|
||||
testImplementation "org.junit.jupiter:junit-jupiter-api:$junitVersion"
|
||||
testRuntimeOnly "org.junit.jupiter:junit-jupiter-engine:$junitVersion"
|
||||
// https://todd.ginsberg.com/post/testing-system-exit/
|
||||
testImplementation 'com.ginsberg:junit5-system-exit:1.1.2'
|
||||
|
||||
// implementation "ch.qos.logback:logback-core:1.2.6"
|
||||
// We want logback logging in tests and in the app
|
||||
|
|
|
@ -4,28 +4,35 @@
|
|||
|
||||
package org.pgpainless.cli;
|
||||
|
||||
import com.ginsberg.junit.exit.ExpectSystemExitWithStatus;
|
||||
import com.ginsberg.junit.exit.FailOnSystemExit;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.pgpainless.cli.commands.CLITest;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import sop.exception.SOPGPException;
|
||||
|
||||
public class ExitCodeTest {
|
||||
import java.io.IOException;
|
||||
|
||||
@Test
|
||||
@ExpectSystemExitWithStatus(SOPGPException.UnsupportedSubcommand.EXIT_CODE)
|
||||
public void testUnknownCommand_69() {
|
||||
PGPainlessCLI.main(new String[] {"generate-kex"});
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
public class ExitCodeTest extends CLITest {
|
||||
|
||||
public ExitCodeTest() {
|
||||
super(LoggerFactory.getLogger(ExitCodeTest.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
@ExpectSystemExitWithStatus(SOPGPException.UnsupportedOption.EXIT_CODE)
|
||||
public void testCommandWithUnknownOption_37() {
|
||||
PGPainlessCLI.main(new String[] {"generate-key", "-k", "\"k is unknown\""});
|
||||
public void testUnknownCommand_69() throws IOException {
|
||||
assertEquals(SOPGPException.UnsupportedSubcommand.EXIT_CODE,
|
||||
executeCommand("unsupported-subcommand"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@FailOnSystemExit
|
||||
public void successfulExecutionDoesNotTerminateJVM() {
|
||||
PGPainlessCLI.main(new String[] {"version"});
|
||||
public void testCommandWithUnknownOption_37() throws IOException {
|
||||
assertEquals(SOPGPException.UnsupportedOption.EXIT_CODE,
|
||||
executeCommand("generate-key", "-k", "\"k is unknown\""));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void successfulExecutionDoesNotTerminateJVM() throws IOException {
|
||||
assertSuccess(executeCommand("version"));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -14,7 +14,6 @@ import java.io.IOException;
|
|||
import java.security.InvalidAlgorithmParameterException;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
|
||||
import com.ginsberg.junit.exit.FailOnSystemExit;
|
||||
import org.bouncycastle.openpgp.PGPException;
|
||||
import org.bouncycastle.openpgp.PGPPublicKeyRing;
|
||||
import org.bouncycastle.openpgp.PGPSecretKeyRing;
|
||||
|
@ -83,7 +82,6 @@ public class RoundTripEncryptDecryptCmdTest extends CLITest {
|
|||
"-----END PGP PUBLIC KEY BLOCK-----";
|
||||
|
||||
@Test
|
||||
@FailOnSystemExit
|
||||
public void encryptAndDecryptAMessage() throws IOException {
|
||||
// Juliets key and cert
|
||||
File julietKeyFile = pipeStdoutToFile("juliet.key");
|
||||
|
|
|
@ -4,104 +4,49 @@
|
|||
|
||||
package org.pgpainless.cli.commands;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.io.PrintStream;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
import com.ginsberg.junit.exit.FailOnSystemExit;
|
||||
import org.junit.jupiter.api.AfterAll;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.pgpainless.cli.PGPainlessCLI;
|
||||
import org.pgpainless.cli.TestUtils;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
public class RoundTripInlineSignVerifyCmdTest {
|
||||
private static File tempDir;
|
||||
private static PrintStream originalSout;
|
||||
public class RoundTripInlineSignVerifyCmdTest extends CLITest {
|
||||
|
||||
@BeforeAll
|
||||
public static void prepare() throws IOException {
|
||||
tempDir = TestUtils.createTempDirectory();
|
||||
public RoundTripInlineSignVerifyCmdTest() {
|
||||
super(LoggerFactory.getLogger(RoundTripInlineSignVerifyCmdTest.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
@FailOnSystemExit
|
||||
public void encryptAndDecryptAMessage() throws IOException {
|
||||
originalSout = System.out;
|
||||
File sigmundKeyFile = new File(tempDir, "sigmund.key");
|
||||
assertTrue(sigmundKeyFile.createNewFile());
|
||||
|
||||
File sigmundCertFile = new File(tempDir, "sigmund.cert");
|
||||
assertTrue(sigmundCertFile.createNewFile());
|
||||
|
||||
File msgFile = new File(tempDir, "signed.asc");
|
||||
assertTrue(msgFile.createNewFile());
|
||||
|
||||
File passwordFile = new File(tempDir, "password");
|
||||
assertTrue(passwordFile.createNewFile());
|
||||
|
||||
// write password file
|
||||
FileOutputStream passwordOut = new FileOutputStream(passwordFile);
|
||||
passwordOut.write("sw0rdf1sh".getBytes(StandardCharsets.UTF_8));
|
||||
passwordOut.close();
|
||||
File password = writeFile("password", "sw0rdf1sh");
|
||||
|
||||
// generate key
|
||||
OutputStream sigmundKeyOut = new FileOutputStream(sigmundKeyFile);
|
||||
System.setOut(new PrintStream(sigmundKeyOut));
|
||||
PGPainlessCLI.execute("generate-key",
|
||||
"--with-key-password=" + passwordFile.getAbsolutePath(),
|
||||
"Sigmund Freud <sigmund@pgpainless.org>");
|
||||
sigmundKeyOut.close();
|
||||
File sigmundKey = pipeStdoutToFile("sigmund.key");
|
||||
assertSuccess(executeCommand("generate-key", "--with-key-password=" + password.getAbsolutePath(),
|
||||
"Sigmund Freud <sigmund@pgpainless.org>"));
|
||||
|
||||
// extract cert
|
||||
FileInputStream sigmundKeyIn = new FileInputStream(sigmundKeyFile);
|
||||
System.setIn(sigmundKeyIn);
|
||||
OutputStream sigmundCertOut = new FileOutputStream(sigmundCertFile);
|
||||
System.setOut(new PrintStream(sigmundCertOut));
|
||||
PGPainlessCLI.execute("extract-cert");
|
||||
sigmundKeyIn.close();
|
||||
sigmundCertOut.close();
|
||||
File sigmundCert = pipeStdoutToFile("sigmund.cert");
|
||||
pipeFileToStdin(sigmundKey);
|
||||
assertSuccess(executeCommand("extract-cert"));
|
||||
|
||||
// sign message
|
||||
String msg = "Hello World!\n";
|
||||
ByteArrayInputStream msgIn = new ByteArrayInputStream(msg.getBytes(StandardCharsets.UTF_8));
|
||||
System.setIn(msgIn);
|
||||
OutputStream msgAscOut = new FileOutputStream(msgFile);
|
||||
System.setOut(new PrintStream(msgAscOut));
|
||||
PGPainlessCLI.execute("inline-sign",
|
||||
"--with-key-password=" + passwordFile.getAbsolutePath(),
|
||||
sigmundKeyFile.getAbsolutePath());
|
||||
msgAscOut.close();
|
||||
pipeBytesToStdin("Hello, World!\n".getBytes(StandardCharsets.UTF_8));
|
||||
File signedMsg = pipeStdoutToFile("signed.asc");
|
||||
assertSuccess(executeCommand("inline-sign", "--with-key-password=" + password.getAbsolutePath(),
|
||||
sigmundKey.getAbsolutePath()));
|
||||
|
||||
File verifyFile = new File(tempDir, "verify.txt");
|
||||
// verify message
|
||||
File verifyFile = nonExistentFile("verify.txt");
|
||||
pipeFileToStdin(signedMsg);
|
||||
assertSuccess(executeCommand("inline-verify", "--verifications-out", verifyFile.getAbsolutePath(),
|
||||
sigmundCert.getAbsolutePath()));
|
||||
|
||||
FileInputStream msgAscIn = new FileInputStream(msgFile);
|
||||
System.setIn(msgAscIn);
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
PrintStream pOut = new PrintStream(out);
|
||||
System.setOut(pOut);
|
||||
PGPainlessCLI.execute("inline-verify",
|
||||
"--verifications-out", verifyFile.getAbsolutePath(),
|
||||
sigmundCertFile.getAbsolutePath());
|
||||
msgAscIn.close();
|
||||
|
||||
assertEquals(msg, out.toString());
|
||||
}
|
||||
|
||||
@AfterAll
|
||||
public static void after() {
|
||||
System.setOut(originalSout);
|
||||
// CHECKSTYLE:OFF
|
||||
System.out.println(tempDir.getAbsolutePath());
|
||||
// CHECKSTYLE:ON
|
||||
String verifications = readStringFromFile(verifyFile);
|
||||
assertFalse(verifications.trim().isEmpty());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -4,28 +4,18 @@
|
|||
|
||||
package org.pgpainless.cli.misc;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.io.PrintStream;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
import com.ginsberg.junit.exit.ExpectSystemExitWithStatus;
|
||||
import org.bouncycastle.util.io.Streams;
|
||||
import org.junit.jupiter.api.AfterAll;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.pgpainless.cli.PGPainlessCLI;
|
||||
import org.pgpainless.cli.TestUtils;
|
||||
import org.pgpainless.cli.commands.CLITest;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import sop.exception.SOPGPException;
|
||||
|
||||
public class SignUsingPublicKeyBehaviorTest {
|
||||
public class SignUsingPublicKeyBehaviorTest extends CLITest {
|
||||
|
||||
public static final String KEY_THAT_IS_A_CERT = "" +
|
||||
"-----BEGIN PGP PUBLIC KEY BLOCK-----\n" +
|
||||
|
@ -89,61 +79,24 @@ public class SignUsingPublicKeyBehaviorTest {
|
|||
"=oJQ2\n" +
|
||||
"-----END PGP PUBLIC KEY BLOCK-----";
|
||||
|
||||
|
||||
private static File tempDir;
|
||||
private static PrintStream originalSout;
|
||||
|
||||
@BeforeAll
|
||||
public static void prepare() throws IOException {
|
||||
tempDir = TestUtils.createTempDirectory();
|
||||
public SignUsingPublicKeyBehaviorTest() {
|
||||
super(LoggerFactory.getLogger(SignUsingPublicKeyBehaviorTest.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
@ExpectSystemExitWithStatus(SOPGPException.KeyCannotSign.EXIT_CODE)
|
||||
public void testSignatureCreationAndVerification() throws IOException {
|
||||
originalSout = System.out;
|
||||
InputStream originalIn = System.in;
|
||||
|
||||
// Write alice key to disc
|
||||
File aliceKeyFile = new File(tempDir, "alice.key");
|
||||
assertTrue(aliceKeyFile.createNewFile());
|
||||
OutputStream aliceKeyOut = new FileOutputStream(aliceKeyFile);
|
||||
Streams.pipeAll(new ByteArrayInputStream(KEY_THAT_IS_A_CERT.getBytes(StandardCharsets.UTF_8)), aliceKeyOut);
|
||||
aliceKeyOut.close();
|
||||
|
||||
// Write alice pub key to disc
|
||||
File aliceCertFile = new File(tempDir, "alice.pub");
|
||||
assertTrue(aliceCertFile.createNewFile());
|
||||
OutputStream aliceCertOut = new FileOutputStream(aliceCertFile);
|
||||
Streams.pipeAll(new ByteArrayInputStream(KEY_THAT_IS_A_CERT.getBytes(StandardCharsets.UTF_8)), aliceCertOut);
|
||||
aliceCertOut.close();
|
||||
File aliceKeyFile = writeFile("alice.key", KEY_THAT_IS_A_CERT);
|
||||
|
||||
// Write test data to disc
|
||||
String data = "If privacy is outlawed, only outlaws will have privacy.\n";
|
||||
|
||||
File dataFile = new File(tempDir, "data");
|
||||
assertTrue(dataFile.createNewFile());
|
||||
FileOutputStream dataOut = new FileOutputStream(dataFile);
|
||||
Streams.pipeAll(new ByteArrayInputStream(data.getBytes(StandardCharsets.UTF_8)), dataOut);
|
||||
dataOut.close();
|
||||
File dataFile = writeFile("data", "If privacy is outlawed, only outlaws will have privacy.\n");
|
||||
|
||||
// Sign test data
|
||||
FileInputStream dataIn = new FileInputStream(dataFile);
|
||||
System.setIn(dataIn);
|
||||
File sigFile = new File(tempDir, "sig.asc");
|
||||
assertTrue(sigFile.createNewFile());
|
||||
FileOutputStream sigOut = new FileOutputStream(sigFile);
|
||||
System.setOut(new PrintStream(sigOut));
|
||||
PGPainlessCLI.main(new String[] {"sign", "--armor", aliceKeyFile.getAbsolutePath()});
|
||||
File sigFile = pipeStdoutToFile("sig.asc");
|
||||
pipeFileToStdin(dataFile);
|
||||
assertEquals(SOPGPException.KeyCannotSign.EXIT_CODE,
|
||||
executeCommand("sign", "--armor", aliceKeyFile.getAbsolutePath()));
|
||||
|
||||
System.setIn(originalIn);
|
||||
}
|
||||
|
||||
@AfterAll
|
||||
public static void after() {
|
||||
System.setOut(originalSout);
|
||||
// CHECKSTYLE:OFF
|
||||
System.out.println(tempDir.getAbsolutePath());
|
||||
// CHECKSTYLE:ON
|
||||
assertTrue(readStringFromFile(sigFile).trim().isEmpty());
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue