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

SOP commands: Throw UnsupportedSubcommand error when sop.command() returns null

This commit is contained in:
Paul Schaub 2022-01-09 02:04:12 +01:00
parent 1c2cbf0e75
commit 1aca7112d2
12 changed files with 127 additions and 10 deletions

View file

@ -25,6 +25,10 @@ public class ArmorCmd implements Runnable {
@Override @Override
public void run() { public void run() {
Armor armor = SopCLI.getSop().armor(); Armor armor = SopCLI.getSop().armor();
if (armor == null) {
throw new SOPGPException.UnsupportedSubcommand("Command 'armor' not implemented.");
}
if (label != null) { if (label != null) {
try { try {
armor.label(label); armor.label(label);

View file

@ -10,6 +10,7 @@ import picocli.CommandLine;
import sop.cli.picocli.Print; import sop.cli.picocli.Print;
import sop.cli.picocli.SopCLI; import sop.cli.picocli.SopCLI;
import sop.exception.SOPGPException; import sop.exception.SOPGPException;
import sop.operation.Dearmor;
@CommandLine.Command(name = "dearmor", @CommandLine.Command(name = "dearmor",
description = "Remove ASCII Armor from standard input", description = "Remove ASCII Armor from standard input",
@ -18,6 +19,11 @@ public class DearmorCmd implements Runnable {
@Override @Override
public void run() { public void run() {
Dearmor dearmor = SopCLI.getSop().dearmor();
if (dearmor == null) {
throw new SOPGPException.UnsupportedSubcommand("Command 'dearmor' not implemented.");
}
try { try {
SopCLI.getSop() SopCLI.getSop()
.dearmor() .dearmor()

View file

@ -94,7 +94,7 @@ public class DecryptCmd implements Runnable {
Decrypt decrypt = SopCLI.getSop().decrypt(); Decrypt decrypt = SopCLI.getSop().decrypt();
if (decrypt == null) { if (decrypt == null) {
throw new SOPGPException.UnsupportedSubcommand("Subcommand 'decrypt' not implemented."); throw new SOPGPException.UnsupportedSubcommand("Command 'decrypt' not implemented.");
} }
setNotAfter(notAfter, decrypt); setNotAfter(notAfter, decrypt);

View file

@ -32,11 +32,15 @@ public class DetachInbandSignatureAndMessageCmd implements Runnable {
@Override @Override
public void run() { public void run() {
DetachInbandSignatureAndMessage detach = SopCLI.getSop().detachInbandSignatureAndMessage();
if (detach == null) {
throw new SOPGPException.UnsupportedSubcommand("Command 'detach-inband-signature-and-message' not implemented.");
}
if (signaturesOut == null) { if (signaturesOut == null) {
throw new SOPGPException.MissingArg("--signatures-out is required."); throw new SOPGPException.MissingArg("--signatures-out is required.");
} }
DetachInbandSignatureAndMessage detach = SopCLI.getSop().detachInbandSignatureAndMessage();
if (!armor) { if (!armor) {
detach.noArmor(); detach.noArmor();
} }

View file

@ -51,6 +51,10 @@ public class EncryptCmd implements Runnable {
@Override @Override
public void run() { public void run() {
Encrypt encrypt = SopCLI.getSop().encrypt(); Encrypt encrypt = SopCLI.getSop().encrypt();
if (encrypt == null) {
throw new SOPGPException.UnsupportedSubcommand("Command 'encrypt' not implemented.");
}
if (type != null) { if (type != null) {
try { try {
encrypt.mode(type); encrypt.mode(type);

View file

@ -25,6 +25,10 @@ public class ExtractCertCmd implements Runnable {
@Override @Override
public void run() { public void run() {
ExtractCert extractCert = SopCLI.getSop().extractCert(); ExtractCert extractCert = SopCLI.getSop().extractCert();
if (extractCert == null) {
throw new SOPGPException.UnsupportedSubcommand("Command 'extract-cert' not implemented.");
}
if (!armor) { if (!armor) {
extractCert.noArmor(); extractCert.noArmor();
} }

View file

@ -31,6 +31,10 @@ public class GenerateKeyCmd implements Runnable {
@Override @Override
public void run() { public void run() {
GenerateKey generateKey = SopCLI.getSop().generateKey(); GenerateKey generateKey = SopCLI.getSop().generateKey();
if (generateKey == null) {
throw new SOPGPException.UnsupportedSubcommand("Command 'generate-key' not implemented.");
}
for (String userId : userId) { for (String userId : userId) {
generateKey.userId(userId); generateKey.userId(userId);
} }

View file

@ -47,6 +47,9 @@ public class SignCmd implements Runnable {
@Override @Override
public void run() { public void run() {
Sign sign = SopCLI.getSop().sign(); Sign sign = SopCLI.getSop().sign();
if (sign == null) {
throw new SOPGPException.UnsupportedSubcommand("Command 'sign' not implemented.");
}
if (type != null) { if (type != null) {
try { try {

View file

@ -53,6 +53,10 @@ public class VerifyCmd implements Runnable {
@Override @Override
public void run() { public void run() {
Verify verify = SopCLI.getSop().verify(); Verify verify = SopCLI.getSop().verify();
if (verify == null) {
throw new SOPGPException.UnsupportedSubcommand("Command 'verify' not implemented.");
}
if (notAfter != null) { if (notAfter != null) {
try { try {
verify.notAfter(DateParser.parseNotAfter(notAfter)); verify.notAfter(DateParser.parseNotAfter(notAfter));

View file

@ -7,6 +7,7 @@ package sop.cli.picocli.commands;
import picocli.CommandLine; import picocli.CommandLine;
import sop.cli.picocli.Print; import sop.cli.picocli.Print;
import sop.cli.picocli.SopCLI; import sop.cli.picocli.SopCLI;
import sop.exception.SOPGPException;
import sop.operation.Version; import sop.operation.Version;
@CommandLine.Command(name = "version", description = "Display version information about the tool", @CommandLine.Command(name = "version", description = "Display version information about the tool",
@ -16,6 +17,9 @@ public class VersionCmd implements Runnable {
@Override @Override
public void run() { public void run() {
Version version = SopCLI.getSop().version(); Version version = SopCLI.getSop().version();
if (version == null) {
throw new SOPGPException.UnsupportedSubcommand("Command 'version' not implemented.");
}
Print.outln(version.getName() + " " + version.getVersion()); Print.outln(version.getName() + " " + version.getVersion());
} }

View file

@ -4,11 +4,26 @@
package sop.cli.picocli; package sop.cli.picocli;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import com.ginsberg.junit.exit.ExpectSystemExitWithStatus; import com.ginsberg.junit.exit.ExpectSystemExitWithStatus;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import sop.SOP; import sop.SOP;
import sop.operation.Armor;
import sop.operation.Dearmor;
import sop.operation.Decrypt;
import sop.operation.DetachInbandSignatureAndMessage;
import sop.operation.Encrypt;
import sop.operation.ExtractCert;
import sop.operation.GenerateKey;
import sop.operation.Sign;
import sop.operation.Verify;
import sop.operation.Version;
public class SOPTest { public class SOPTest {
@ -28,4 +43,77 @@ public class SOPTest {
// At this point, no SOP backend is set, so an InvalidStateException triggers exit(1) // At this point, no SOP backend is set, so an InvalidStateException triggers exit(1)
SopCLI.main(new String[] {"armor"}); SopCLI.main(new String[] {"armor"});
} }
@Test
public void UnsupportedSubcommandsTest() {
SOP nullCommandSOP = new SOP() {
@Override
public Version version() {
return null;
}
@Override
public GenerateKey generateKey() {
return null;
}
@Override
public ExtractCert extractCert() {
return null;
}
@Override
public Sign sign() {
return null;
}
@Override
public Verify verify() {
return null;
}
@Override
public Encrypt encrypt() {
return null;
}
@Override
public Decrypt decrypt() {
return null;
}
@Override
public Armor armor() {
return null;
}
@Override
public Dearmor dearmor() {
return null;
}
@Override
public DetachInbandSignatureAndMessage detachInbandSignatureAndMessage() {
return null;
}
};
SopCLI.setSopInstance(nullCommandSOP);
List<String[]> commands = new ArrayList<>();
commands.add(new String[] {"armor"});
commands.add(new String[] {"dearmor"});
commands.add(new String[] {"decrypt"});
commands.add(new String[] {"detach-inband-signature-and-message"});
commands.add(new String[] {"encrypt"});
commands.add(new String[] {"extract-cert"});
commands.add(new String[] {"generate-key"});
commands.add(new String[] {"sign"});
commands.add(new String[] {"verify", "signature.asc", "cert.asc"});
commands.add(new String[] {"version"});
for (String[] command : commands) {
int exit = SopCLI.execute(command);
assertEquals(69, exit, "Unexpected exit code for non-implemented command " + Arrays.toString(command) + ": " + exit);
}
}
} }

View file

@ -52,10 +52,6 @@ public abstract class SOPGPException extends RuntimeException {
super(message, e); super(message, e);
} }
public UnsupportedAsymmetricAlgo(Throwable e) {
super(e);
}
@Override @Override
public int getExitCode() { public int getExitCode() {
return EXIT_CODE; return EXIT_CODE;
@ -308,10 +304,6 @@ public abstract class SOPGPException extends RuntimeException {
super(); super();
} }
public KeyCannotSign(String message) {
super(message);
}
public KeyCannotSign(String s, KeyCannotSign keyCannotSign) { public KeyCannotSign(String s, KeyCannotSign keyCannotSign) {
super(s, keyCannotSign); super(s, keyCannotSign);
} }