2022-01-11 13:46:05 +01:00
|
|
|
// SPDX-FileCopyrightText: 2021 Paul Schaub <vanitasvitae@fsfe.org>
|
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
|
|
|
package sop.cli.picocli;
|
|
|
|
|
|
|
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
|
|
|
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 org.junit.jupiter.api.Test;
|
|
|
|
import sop.SOP;
|
2022-06-11 11:18:45 +02:00
|
|
|
import sop.exception.SOPGPException;
|
2022-01-11 13:46:05 +01:00
|
|
|
import sop.operation.Armor;
|
2023-07-12 00:42:02 +02:00
|
|
|
import sop.operation.ChangeKeyPassword;
|
2022-01-11 13:46:05 +01:00
|
|
|
import sop.operation.Dearmor;
|
|
|
|
import sop.operation.Decrypt;
|
2022-05-24 21:19:37 +02:00
|
|
|
import sop.operation.InlineDetach;
|
2022-01-11 13:46:05 +01:00
|
|
|
import sop.operation.Encrypt;
|
|
|
|
import sop.operation.ExtractCert;
|
|
|
|
import sop.operation.GenerateKey;
|
2022-05-29 21:17:03 +02:00
|
|
|
import sop.operation.InlineSign;
|
|
|
|
import sop.operation.InlineVerify;
|
2022-06-06 20:06:14 +02:00
|
|
|
import sop.operation.DetachedSign;
|
|
|
|
import sop.operation.DetachedVerify;
|
2023-04-11 15:06:37 +02:00
|
|
|
import sop.operation.ListProfiles;
|
2023-07-11 22:42:16 +02:00
|
|
|
import sop.operation.RevokeKey;
|
2022-01-11 13:46:05 +01:00
|
|
|
import sop.operation.Version;
|
|
|
|
|
|
|
|
public class SOPTest {
|
|
|
|
|
|
|
|
@Test
|
2022-06-11 11:18:45 +02:00
|
|
|
@ExpectSystemExitWithStatus(SOPGPException.UnsupportedSubcommand.EXIT_CODE)
|
2022-01-11 13:46:05 +01:00
|
|
|
public void assertExitOnInvalidSubcommand() {
|
|
|
|
SOP sop = mock(SOP.class);
|
|
|
|
SopCLI.setSopInstance(sop);
|
|
|
|
|
|
|
|
SopCLI.main(new String[] {"invalid"});
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
@ExpectSystemExitWithStatus(1)
|
|
|
|
public void assertThrowsIfNoSOPBackendSet() {
|
2023-11-15 12:35:23 +01:00
|
|
|
SopCLI.setSopInstance(null);
|
2022-01-11 13:46:05 +01:00
|
|
|
// At this point, no SOP backend is set, so an InvalidStateException triggers exit(1)
|
|
|
|
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
|
2022-06-06 20:06:14 +02:00
|
|
|
public DetachedSign detachedSign() {
|
2022-01-11 13:46:05 +01:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2022-06-06 20:06:14 +02:00
|
|
|
public DetachedVerify detachedVerify() {
|
2022-01-11 13:46:05 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2023-04-11 15:06:37 +02:00
|
|
|
@Override
|
|
|
|
public ListProfiles listProfiles() {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2023-07-11 22:42:16 +02:00
|
|
|
@Override
|
|
|
|
public RevokeKey revokeKey() {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2023-07-12 00:42:02 +02:00
|
|
|
@Override
|
|
|
|
public ChangeKeyPassword changeKeyPassword() {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2022-01-11 13:46:05 +01:00
|
|
|
@Override
|
2022-05-29 21:17:03 +02:00
|
|
|
public InlineDetach inlineDetach() {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public InlineSign inlineSign() {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public InlineVerify inlineVerify() {
|
2022-01-11 13:46:05 +01:00
|
|
|
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"});
|
2022-05-29 21:17:03 +02:00
|
|
|
commands.add(new String[] {"inline-detach", "--signatures-out", "sigs.asc"});
|
2022-01-11 13:46:05 +01:00
|
|
|
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);
|
2022-06-11 11:18:45 +02:00
|
|
|
assertEquals(SOPGPException.UnsupportedSubcommand.EXIT_CODE, exit,
|
|
|
|
"Unexpected exit code for non-implemented command " + Arrays.toString(command) + ": " + exit);
|
2022-01-11 13:46:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|