mirror of
https://codeberg.org/PGPainless/sop-java.git
synced 2025-09-08 17:59:43 +02:00
Encrypt: Add --session-key-out support
This commit is contained in:
parent
a8829350a8
commit
5ee9414410
7 changed files with 162 additions and 40 deletions
|
@ -5,7 +5,9 @@
|
|||
package sop.cli.picocli.commands;
|
||||
|
||||
import picocli.CommandLine;
|
||||
import sop.Ready;
|
||||
import sop.EncryptionResult;
|
||||
import sop.ReadyWithResult;
|
||||
import sop.SessionKey;
|
||||
import sop.cli.picocli.SopCLI;
|
||||
import sop.enums.EncryptAs;
|
||||
import sop.exception.SOPGPException;
|
||||
|
@ -13,6 +15,8 @@ import sop.operation.Encrypt;
|
|||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.io.PrintWriter;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
|
@ -49,11 +53,18 @@ public class EncryptCmd extends AbstractSopCmd {
|
|||
paramLabel = "CERTS")
|
||||
List<String> certs = new ArrayList<>();
|
||||
|
||||
@CommandLine.Option(
|
||||
names = {"--session-key-out"},
|
||||
paramLabel = "SESSIONKEY")
|
||||
String sessionKeyOut;
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
Encrypt encrypt = throwIfUnsupportedSubcommand(
|
||||
SopCLI.getSop().encrypt(), "encrypt");
|
||||
|
||||
throwIfOutputExists(sessionKeyOut);
|
||||
|
||||
if (profile != null) {
|
||||
try {
|
||||
encrypt.profile(profile);
|
||||
|
@ -145,8 +156,27 @@ public class EncryptCmd extends AbstractSopCmd {
|
|||
}
|
||||
|
||||
try {
|
||||
Ready ready = encrypt.plaintext(System.in);
|
||||
ready.writeTo(System.out);
|
||||
ReadyWithResult<EncryptionResult> ready = encrypt.plaintext(System.in);
|
||||
EncryptionResult result = ready.writeTo(System.out);
|
||||
|
||||
if (sessionKeyOut == null) {
|
||||
return;
|
||||
}
|
||||
try (OutputStream outputStream = getOutput(sessionKeyOut)) {
|
||||
if (!result.getSessionKey().isPresent()) {
|
||||
String errorMsg = getMsg("sop.error.runtime.no_session_key_extracted");
|
||||
throw new SOPGPException.UnsupportedOption(String.format(errorMsg, "--session-key-out"));
|
||||
}
|
||||
SessionKey sessionKey = result.getSessionKey().get();
|
||||
if (sessionKey == null) {
|
||||
return;
|
||||
}
|
||||
PrintWriter writer = new PrintWriter(outputStream);
|
||||
// CHECKSTYLE:OFF
|
||||
writer.println(sessionKey);
|
||||
// CHECKSTYLE:ON
|
||||
writer.flush();
|
||||
}
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
|
|
@ -4,6 +4,24 @@
|
|||
|
||||
package sop.cli.picocli.commands;
|
||||
|
||||
import com.ginsberg.junit.exit.ExpectSystemExitWithStatus;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import sop.EncryptionResult;
|
||||
import sop.ReadyWithResult;
|
||||
import sop.SOP;
|
||||
import sop.cli.picocli.SopCLI;
|
||||
import sop.cli.picocli.TestFileUtil;
|
||||
import sop.enums.EncryptAs;
|
||||
import sop.exception.SOPGPException;
|
||||
import sop.operation.Encrypt;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.never;
|
||||
|
@ -11,22 +29,6 @@ import static org.mockito.Mockito.times;
|
|||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
|
||||
import com.ginsberg.junit.exit.ExpectSystemExitWithStatus;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import sop.Ready;
|
||||
import sop.SOP;
|
||||
import sop.cli.picocli.SopCLI;
|
||||
import sop.cli.picocli.TestFileUtil;
|
||||
import sop.enums.EncryptAs;
|
||||
import sop.exception.SOPGPException;
|
||||
import sop.operation.Encrypt;
|
||||
|
||||
public class EncryptCmdTest {
|
||||
|
||||
Encrypt encrypt;
|
||||
|
@ -34,10 +36,10 @@ public class EncryptCmdTest {
|
|||
@BeforeEach
|
||||
public void mockComponents() throws IOException {
|
||||
encrypt = mock(Encrypt.class);
|
||||
when(encrypt.plaintext((InputStream) any())).thenReturn(new Ready() {
|
||||
when(encrypt.plaintext((InputStream) any())).thenReturn(new ReadyWithResult<EncryptionResult>() {
|
||||
@Override
|
||||
public void writeTo(OutputStream outputStream) {
|
||||
|
||||
public EncryptionResult writeTo(@NotNull OutputStream outputStream) throws IOException, SOPGPException {
|
||||
return new EncryptionResult(null);
|
||||
}
|
||||
});
|
||||
|
||||
|
@ -190,9 +192,9 @@ public class EncryptCmdTest {
|
|||
@Test
|
||||
@ExpectSystemExitWithStatus(1)
|
||||
public void writeTo_ioExceptionCausesExit1() throws IOException {
|
||||
when(encrypt.plaintext((InputStream) any())).thenReturn(new Ready() {
|
||||
when(encrypt.plaintext((InputStream) any())).thenReturn(new ReadyWithResult<EncryptionResult>() {
|
||||
@Override
|
||||
public void writeTo(OutputStream outputStream) throws IOException {
|
||||
public EncryptionResult writeTo(@NotNull OutputStream outputStream) throws IOException, SOPGPException {
|
||||
throw new IOException();
|
||||
}
|
||||
});
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue