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