Rename module to external-sop and make backend in tests configurable

This commit is contained in:
Paul Schaub 2023-01-07 15:23:57 +01:00
parent 28912618ea
commit e602cc16cc
12 changed files with 80 additions and 30 deletions

View file

@ -0,0 +1,69 @@
// SPDX-FileCopyrightText: 2023 Paul Schaub <vanitasvitae@fsfe.org>
//
// SPDX-License-Identifier: Apache-2.0
package sop.external.operation;
import sop.Ready;
import sop.exception.SOPGPException;
import sop.operation.ExtractCert;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
public class ExtractCertExternal implements ExtractCert {
private final String binary;
private final Runtime runtime = Runtime.getRuntime();
private boolean noArmor;
public ExtractCertExternal(String binary) {
this.binary = binary;
}
@Override
public ExtractCert noArmor() {
this.noArmor = true;
return this;
}
@Override
public Ready key(InputStream keyInputStream) throws IOException, SOPGPException.BadData {
List<String> commandList = new ArrayList<>();
commandList.add(binary);
commandList.add("extract-cert");
if (noArmor) {
commandList.add("--no-armor");
}
String[] command = commandList.toArray(new String[0]);
try {
Process process = runtime.exec(command);
OutputStream stdOut = process.getOutputStream();
InputStream stdIn = process.getInputStream();
return new Ready() {
@Override
public void writeTo(OutputStream outputStream) throws IOException {
byte[] buf = new byte[4096];
int r;
while ((r = keyInputStream.read(buf)) > 0) {
stdOut.write(buf, 0, r);
}
while ((r = stdIn.read(buf)) > 0) {
outputStream.write(buf, 0 , r);
}
}
};
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}

View file

@ -0,0 +1,89 @@
// SPDX-FileCopyrightText: 2023 Paul Schaub <vanitasvitae@fsfe.org>
//
// SPDX-License-Identifier: Apache-2.0
package sop.external.operation;
import sop.Ready;
import sop.exception.SOPGPException;
import sop.operation.GenerateKey;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
public class GenerateKeyExternal implements GenerateKey {
private final String binary;
private boolean noArmor = false;
private List<String> userIds = new ArrayList<>();
private String keyPassword;
private final Runtime runtime = Runtime.getRuntime();
public GenerateKeyExternal(String binary) {
this.binary = binary;
}
@Override
public GenerateKey noArmor() {
this.noArmor = true;
return this;
}
@Override
public GenerateKey userId(String userId) {
this.userIds.add(userId);
return this;
}
@Override
public GenerateKey withKeyPassword(String password)
throws SOPGPException.PasswordNotHumanReadable, SOPGPException.UnsupportedOption {
this.keyPassword = password;
return this;
}
@Override
public Ready generate()
throws SOPGPException.MissingArg, SOPGPException.UnsupportedAsymmetricAlgo {
List<String> commandList = new ArrayList<>();
commandList.add(binary);
commandList.add("generate-key");
if (noArmor) {
commandList.add("--no-armor");
}
if (keyPassword != null) {
commandList.add("--with-key-password");
commandList.add(keyPassword);
}
for (String userId : userIds) {
commandList.add(userId);
}
String[] command = commandList.toArray(new String[0]);
try {
Process process = runtime.exec(command);
InputStream stdIn = process.getInputStream();
return new Ready() {
@Override
public void writeTo(OutputStream outputStream) throws IOException {
byte[] buf = new byte[4096];
int r;
while ((r = stdIn.read(buf)) >= 0) {
outputStream.write(buf, 0, r);
}
}
};
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}

View file

@ -0,0 +1,87 @@
// SPDX-FileCopyrightText: 2023 Paul Schaub <vanitasvitae@fsfe.org>
//
// SPDX-License-Identifier: Apache-2.0
package sop.external.operation;
import sop.operation.Version;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class VersionExternal implements Version {
private final Runtime runtime = Runtime.getRuntime();
private final String binary;
public VersionExternal(String binaryName) {
this.binary = binaryName;
}
@Override
public String getName() {
String[] command = new String[] {binary, "version"};
try {
Process process = runtime.exec(command);
BufferedReader stdInput = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line = stdInput.readLine().trim();
if (line.contains(" ")) {
return line.substring(0, line.lastIndexOf(" "));
}
return line;
} catch (IOException e) {
throw new RuntimeException(e);
}
}
@Override
public String getVersion() {
String[] command = new String[] {binary, "version"};
try {
Process process = runtime.exec(command);
BufferedReader stdInput = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line = stdInput.readLine().trim();
if (line.contains(" ")) {
return line.substring(line.lastIndexOf(" ") + 1);
}
return line;
} catch (IOException e) {
throw new RuntimeException(e);
}
}
@Override
public String getBackendVersion() {
String[] command = new String[] {binary, "version", "--backend"};
try {
Process process = runtime.exec(command);
BufferedReader stdInput = new BufferedReader(new InputStreamReader(process.getInputStream()));
StringBuilder sb = new StringBuilder();
String line;
while ((line = stdInput.readLine()) != null) {
sb.append(line).append('\n');
}
return sb.toString();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
@Override
public String getExtendedVersion() {
String[] command = new String[] {binary, "version", "--extended"};
try {
Process process = runtime.exec(command);
BufferedReader stdInput = new BufferedReader(new InputStreamReader(process.getInputStream()));
StringBuilder sb = new StringBuilder();
String line;
while ((line = stdInput.readLine()) != null) {
sb.append(line).append('\n');
}
return sb.toString();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}

View file

@ -0,0 +1,8 @@
// SPDX-FileCopyrightText: 2023 Paul Schaub <vanitasvitae@fsfe.org>
//
// SPDX-License-Identifier: Apache-2.0
/**
* Bindings for SOP subcommands to a SOP binary.
*/
package sop.external.operation;