Add test for unsupported subcommands

This commit is contained in:
Paul Schaub 2023-01-20 14:30:41 +01:00
parent 61c5eb2962
commit ffc5b26c0d
Signed by: vanitasvitae
GPG key ID: 62BEE9264BF17311
2 changed files with 59 additions and 1 deletions

View file

@ -141,7 +141,7 @@ public abstract class AbstractExternalSOPTest {
} }
} }
private static String readSopBackendFromProperties() { static String readSopBackendFromProperties() {
Properties properties = new Properties(); Properties properties = new Properties();
try { try {
InputStream resourceIn = AbstractExternalSOPTest.class.getResourceAsStream("backend.local.properties"); InputStream resourceIn = AbstractExternalSOPTest.class.getResourceAsStream("backend.local.properties");

View file

@ -0,0 +1,58 @@
// SPDX-FileCopyrightText: 2023 Paul Schaub <vanitasvitae@fsfe.org>
//
// SPDX-License-Identifier: Apache-2.0
package sop.external;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.EnabledIf;
import sop.exception.SOPGPException;
import java.io.IOException;
import java.util.Properties;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assumptions.assumeTrue;
@EnabledIf("sop.external.AbstractExternalSOPTest#isExternalSopInstalled")
public class UnsupportedSubcommandTest extends AbstractExternalSOPTest {
private final UnsupportedSubcommandExternal unsupportedSubcommand;
public UnsupportedSubcommandTest() {
String backend = readSopBackendFromProperties();
assumeTrue(backend != null);
Properties environment = readBackendEnvironment();
unsupportedSubcommand = new UnsupportedSubcommandExternal(backend, environment);
}
@Test
public void testUnsupportedSubcommand() {
// "sop unsupported" returns error code UNSUPPORTED_SUBCOMMAND
assertThrows(SOPGPException.UnsupportedSubcommand.class,
unsupportedSubcommand::executeUnsupportedSubcommand);
}
private static class UnsupportedSubcommandExternal {
private final Runtime runtime = Runtime.getRuntime();
private final String binary;
private final Properties environment;
public UnsupportedSubcommandExternal(String binaryName, Properties environment) {
this.binary = binaryName;
this.environment = environment;
}
public void executeUnsupportedSubcommand() {
String[] command = new String[] {binary, "unsupported"}; // ~$ sop unsupported
String[] env = ExternalSOP.propertiesToEnv(environment).toArray(new String[0]);
try {
Process process = runtime.exec(command, env);
ExternalSOP.finish(process);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
}