Improve error handling in sop tests

This commit is contained in:
Paul Schaub 2021-03-05 14:43:13 +01:00
parent 24c0cd8a96
commit 7bd12fe5d4
Signed by: vanitasvitae
GPG Key ID: 62BEE9264BF17311
6 changed files with 16 additions and 78 deletions

View File

@ -16,11 +16,12 @@
package org.pgpainless.sop;
import org.junit.jupiter.api.Test;
import picocli.CommandLine;
public class DummyTest {
@Test
public void dummyTest() {
PGPainlessCLI.main(new String[] {"generate-key", "Ed Snowden <citizen4@lavabit.com>"});
new CommandLine(new PGPainlessCLI()).execute("generate-key", "Ed Snowden <citizen4@lavabit.com>");
}
}

View File

@ -31,6 +31,7 @@ import java.nio.charset.StandardCharsets;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import picocli.CommandLine;
public class EncryptDecryptTest {
@ -62,27 +63,27 @@ public class EncryptDecryptTest {
OutputStream julietKeyOut = new FileOutputStream(julietKeyFile);
System.setOut(new PrintStream(julietKeyOut));
PGPainlessCLI.main(new String[] {"generate-key", "Juliet Capulet <juliet@capulet.lit>"});
new CommandLine(new PGPainlessCLI()).execute("generate-key", "Juliet Capulet <juliet@capulet.lit>");
julietKeyOut.close();
FileInputStream julietKeyIn = new FileInputStream(julietKeyFile);
System.setIn(julietKeyIn);
OutputStream julietCertOut = new FileOutputStream(julietCertFile);
System.setOut(new PrintStream(julietCertOut));
PGPainlessCLI.main(new String[] {"extract-cert"});
new CommandLine(new PGPainlessCLI()).execute("extract-cert");
julietKeyIn.close();
julietCertOut.close();
OutputStream romeoKeyOut = new FileOutputStream(romeoKeyFile);
System.setOut(new PrintStream(romeoKeyOut));
PGPainlessCLI.main(new String[] {"generate-key", "Romeo Montague <romeo@montague.lit>"});
new CommandLine(new PGPainlessCLI()).execute("generate-key", "Romeo Montague <romeo@montague.lit>");
romeoKeyOut.close();
FileInputStream romeoKeyIn = new FileInputStream(romeoKeyFile);
System.setIn(romeoKeyIn);
OutputStream romeoCertOut = new FileOutputStream(romeoCertFile);
System.setOut(new PrintStream(romeoCertOut));
PGPainlessCLI.main(new String[] {"extract-cert"});
new CommandLine(new PGPainlessCLI()).execute("extract-cert");
romeoKeyIn.close();
romeoCertOut.close();
@ -91,9 +92,9 @@ public class EncryptDecryptTest {
System.setIn(msgIn);
OutputStream msgAscOut = new FileOutputStream(msgAscFile);
System.setOut(new PrintStream(msgAscOut));
PGPainlessCLI.main(new String[] {"encrypt",
new CommandLine(new PGPainlessCLI()).execute("encrypt",
"--sign-with", romeoKeyFile.getAbsolutePath(),
julietCertFile.getAbsolutePath()});
julietCertFile.getAbsolutePath());
msgAscOut.close();
File verifyFile = new File(tempDir, "verify.txt");
@ -103,10 +104,10 @@ public class EncryptDecryptTest {
System.setIn(msgAscIn);
ByteArrayOutputStream out = new ByteArrayOutputStream();
System.setOut(new PrintStream(out));
PGPainlessCLI.main(new String[] {"decrypt",
new CommandLine(new PGPainlessCLI()).execute("decrypt",
"--verify-out", verifyFile.getAbsolutePath(),
"--verify-with", romeoCertFile.getAbsolutePath(),
julietKeyFile.getAbsolutePath()});
julietKeyFile.getAbsolutePath());
msgAscIn.close();
assertEquals(msg, out.toString());

View File

@ -1,29 +0,0 @@
/*
* Copyright 2021 Paul Schaub.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.pgpainless.sop;
public class ExitException extends SecurityException {
private final int status;
public ExitException(int status) {
this.status = status;
}
public int getStatus() {
return status;
}
}

View File

@ -31,6 +31,7 @@ import org.bouncycastle.openpgp.PGPSecretKeyRing;
import org.junit.jupiter.api.Test;
import org.pgpainless.PGPainless;
import org.pgpainless.key.info.KeyRingInfo;
import picocli.CommandLine;
public class ExtractCertTest {
@ -44,7 +45,7 @@ public class ExtractCertTest {
ByteArrayOutputStream out = new ByteArrayOutputStream();
System.setOut(new PrintStream(out));
PGPainlessCLI.main(new String[] {"extract-cert"});
new CommandLine(new PGPainlessCLI()).execute("extract-cert");
PGPPublicKeyRing publicKeys = PGPainless.readKeyRing().publicKeyRing(out.toByteArray());
KeyRingInfo info = PGPainless.inspectKeyRing(publicKeys);
assertFalse(info.isSecretKey());

View File

@ -32,6 +32,7 @@ import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.pgpainless.PGPainless;
import org.pgpainless.key.info.KeyRingInfo;
import picocli.CommandLine;
public class GenerateCertTest {
@ -47,7 +48,7 @@ public class GenerateCertTest {
public void testKeyGeneration() throws IOException, PGPException {
ByteArrayOutputStream out = new ByteArrayOutputStream();
System.setOut(new PrintStream(out));
PGPainlessCLI.main(new String[] {"generate-key", "--armor", "Juliet Capulet <juliet@capulet.lit>"});
new CommandLine(new PGPainlessCLI()).execute("generate-key", "--armor", "Juliet Capulet <juliet@capulet.lit>");
PGPSecretKeyRing secretKeys = PGPainless.readKeyRing().secretKeyRing(out.toByteArray());
KeyRingInfo info = PGPainless.inspectKeyRing(secretKeys);
@ -62,7 +63,7 @@ public class GenerateCertTest {
public void testNoArmor() {
ByteArrayOutputStream out = new ByteArrayOutputStream();
System.setOut(new PrintStream(out));
PGPainlessCLI.main(new String[] {"generate-key", "--no-armor", "Test <test@test.test>"});
new CommandLine(new PGPainlessCLI()).execute("generate-key", "--no-armor", "Test <test@test.test>");
byte[] outBegin = new byte[37];
System.arraycopy(out.toByteArray(), 0, outBegin, 0, 37);

View File

@ -1,37 +0,0 @@
/*
* Copyright 2021 Paul Schaub.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.pgpainless.sop;
import java.security.Permission;
public class NoExitSecurityManager extends SecurityManager {
@Override
public void checkPermission(Permission perm) {
// allow anything
}
@Override
public void checkPermission(Permission perm, Object context) {
// allow anything
}
@Override
public void checkExit(int status) {
super.checkExit(status);
throw new ExitException(status);
}
}