diff --git a/sop-java-picocli/src/test/java/sop/cli/picocli/TestFileUtil.java b/sop-java-picocli/src/test/java/sop/cli/picocli/TestFileUtil.java index 385fe1a..d1dc5d1 100644 --- a/sop-java-picocli/src/test/java/sop/cli/picocli/TestFileUtil.java +++ b/sop-java-picocli/src/test/java/sop/cli/picocli/TestFileUtil.java @@ -12,10 +12,15 @@ import java.nio.file.Files; public class TestFileUtil { - public static File writeTempStringFile(String string) throws IOException { - File tempDir = Files.createTempDirectory("tmpDir").toFile(); + public static File createTempDir() throws IOException { + File tempDir = Files.createTempDirectory("tmpFir").toFile(); tempDir.deleteOnExit(); tempDir.mkdirs(); + return tempDir; + } + + public static File writeTempStringFile(String string) throws IOException { + File tempDir = createTempDir(); File passwordFile = new File(tempDir, "file"); passwordFile.createNewFile(); diff --git a/sop-java-picocli/src/test/java/sop/cli/picocli/commands/InlineDetachCmdTest.java b/sop-java-picocli/src/test/java/sop/cli/picocli/commands/InlineDetachCmdTest.java new file mode 100644 index 0000000..3a16c61 --- /dev/null +++ b/sop-java-picocli/src/test/java/sop/cli/picocli/commands/InlineDetachCmdTest.java @@ -0,0 +1,74 @@ +// SPDX-FileCopyrightText: 2022 Paul Schaub +// +// SPDX-License-Identifier: Apache-2.0 + +package sop.cli.picocli.commands; + +import com.ginsberg.junit.exit.ExpectSystemExitWithStatus; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import sop.ReadyWithResult; +import sop.SOP; +import sop.Signatures; +import sop.cli.picocli.SopCLI; +import sop.cli.picocli.TestFileUtil; +import sop.exception.SOPGPException; +import sop.operation.InlineDetach; + +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.nio.charset.StandardCharsets; + +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +public class InlineDetachCmdTest { + + InlineDetach inlineDetach; + + @BeforeEach + public void mockComponents() { + inlineDetach = mock(InlineDetach.class); + + SOP sop = mock(SOP.class); + when(sop.inlineDetach()).thenReturn(inlineDetach); + SopCLI.setSopInstance(sop); + } + + @Test + @ExpectSystemExitWithStatus(SOPGPException.MissingArg.EXIT_CODE) + public void testMissingSignaturesOutResultsInExit19() { + SopCLI.main(new String[] {"inline-detach"}); + } + + @Test + public void testNoArmorIsCalled() throws IOException { + // Create temp dir and allocate non-existing tempfile for sigout + File tempDir = TestFileUtil.createTempDir(); + File tempFile = new File(tempDir, "sigs.out"); + tempFile.deleteOnExit(); + + // mock inline-detach + when(inlineDetach.message((InputStream) any())) + .thenReturn(new ReadyWithResult() { + @Override + public Signatures writeTo(OutputStream outputStream) throws SOPGPException.NoSignature { + return new Signatures() { + @Override + public void writeTo(OutputStream signatureOutputStream) throws IOException { + signatureOutputStream.write("Signatures!\n".getBytes(StandardCharsets.UTF_8)); + } + }; + } + }); + + SopCLI.main(new String[] {"inline-detach", "--signatures-out", tempFile.getAbsolutePath(), "--no-armor"}); + verify(inlineDetach, times(1)).noArmor(); + verify(inlineDetach, times(1)).message((InputStream) any()); + } +}