From ffc5b26c0dc78cde7838fc3635fd419409ca38f4 Mon Sep 17 00:00:00 2001 From: Paul Schaub Date: Fri, 20 Jan 2023 14:30:41 +0100 Subject: [PATCH] Add test for unsupported subcommands --- .../sop/external/AbstractExternalSOPTest.java | 2 +- .../external/UnsupportedSubcommandTest.java | 58 +++++++++++++++++++ 2 files changed, 59 insertions(+), 1 deletion(-) create mode 100644 external-sop/src/test/java/sop/external/UnsupportedSubcommandTest.java diff --git a/external-sop/src/test/java/sop/external/AbstractExternalSOPTest.java b/external-sop/src/test/java/sop/external/AbstractExternalSOPTest.java index bfdc334..2781feb 100644 --- a/external-sop/src/test/java/sop/external/AbstractExternalSOPTest.java +++ b/external-sop/src/test/java/sop/external/AbstractExternalSOPTest.java @@ -141,7 +141,7 @@ public abstract class AbstractExternalSOPTest { } } - private static String readSopBackendFromProperties() { + static String readSopBackendFromProperties() { Properties properties = new Properties(); try { InputStream resourceIn = AbstractExternalSOPTest.class.getResourceAsStream("backend.local.properties"); diff --git a/external-sop/src/test/java/sop/external/UnsupportedSubcommandTest.java b/external-sop/src/test/java/sop/external/UnsupportedSubcommandTest.java new file mode 100644 index 0000000..8196871 --- /dev/null +++ b/external-sop/src/test/java/sop/external/UnsupportedSubcommandTest.java @@ -0,0 +1,58 @@ +// SPDX-FileCopyrightText: 2023 Paul Schaub +// +// 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); + } + } + } +}