Wip: working extract-cert, fix generate-key parameter passing

This commit is contained in:
Paul Schaub 2023-01-09 14:56:53 +01:00
parent e602cc16cc
commit efec4d9110
12 changed files with 430 additions and 122 deletions

View file

@ -0,0 +1,75 @@
// SPDX-FileCopyrightText: 2023 Paul Schaub <vanitasvitae@fsfe.org>
//
// SPDX-License-Identifier: Apache-2.0
package sop.external;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import sop.SOP;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
public abstract class AbstractExternalSOPTest {
private static final Logger LOGGER = LoggerFactory.getLogger(AbstractExternalSOPTest.class);
private final SOP sop;
public AbstractExternalSOPTest() {
String backend = readSopBackendFromProperties();
Properties environment = readBackendEnvironment();
sop = new ExternalSOP(backend, environment);
}
public SOP getSop() {
return sop;
}
public static boolean isExternalSopInstalled() {
String binary = readSopBackendFromProperties();
if (binary == null) {
return false;
}
return new File(binary).exists();
}
private static String readSopBackendFromProperties() {
Properties properties = new Properties();
try {
InputStream resourceIn = AbstractExternalSOPTest.class.getResourceAsStream("backend.local.properties");
if (resourceIn == null) {
LOGGER.info("Could not find backend.local.properties file. Try backend.properties instead.");
resourceIn = AbstractExternalSOPTest.class.getResourceAsStream("backend.properties");
}
if (resourceIn == null) {
throw new FileNotFoundException("Could not find backend.properties file.");
}
properties.load(resourceIn);
String backend = properties.getProperty("sop.backend");
return backend;
} catch (IOException e) {
return null;
}
}
protected static Properties readBackendEnvironment() {
Properties properties = new Properties();
try {
InputStream resourceIn = AbstractExternalSOPTest.class.getResourceAsStream("backend.env");
if (resourceIn == null) {
LOGGER.info("Could not read backend.env file.");
} else {
properties.load(resourceIn);
}
} catch (IOException e) {
throw new RuntimeException(e);
}
return properties;
}
}

View file

@ -0,0 +1,24 @@
// 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 java.io.IOException;
import java.io.InputStream;
import static org.junit.jupiter.api.Assertions.assertTrue;
@EnabledIf("sop.external.AbstractExternalSOPTest#isExternalSopInstalled")
public class ExternalExtractCertTest extends AbstractExternalSOPTest {
@Test
public void extractCertTest() throws IOException {
InputStream keyIn = getSop().generateKey().userId("Alice").generate().getInputStream();
String cert = new String(getSop().extractCert().key(keyIn).getBytes());
assertTrue(cert.startsWith("-----BEGIN PGP PUBLIC KEY BLOCK-----\n"));
}
}

View file

@ -0,0 +1,30 @@
// 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 java.io.IOException;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
@EnabledIf("sop.external.AbstractExternalSOPTest#isExternalSopInstalled")
public class ExternalGenerateKeyTest extends AbstractExternalSOPTest {
@Test
public void generateKeyTest() throws IOException {
String key = new String(getSop().generateKey().userId("Alice").generate().getBytes());
assertTrue(key.startsWith("-----BEGIN PGP PRIVATE KEY BLOCK-----\n"));
}
@Test
public void generateKeyWithPasswordTest() throws IOException {
String key = new String(getSop().generateKey().userId("Alice").withKeyPassword("swßrdf1sh").generate().getBytes());
assertEquals("asd", key);
}
}

View file

@ -1,100 +0,0 @@
// SPDX-FileCopyrightText: 2023 Paul Schaub <vanitasvitae@fsfe.org>
//
// SPDX-License-Identifier: Apache-2.0
package sop.external;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.EnabledIf;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import sop.SOP;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
@EnabledIf("sop.external.ExternalSOPTest#externalSopInstalled")
public class ExternalSOPTest {
private static final Logger LOGGER = LoggerFactory.getLogger(ExternalSOPTest.class);
private final SOP sop;
public ExternalSOPTest() {
String backend = readSopBackendFromProperties();
sop = new ExternalSOP(backend);
}
private static String readSopBackendFromProperties() {
Properties properties = new Properties();
try {
InputStream resourceIn = ExternalSOPTest.class.getResourceAsStream("backend.local.properties");
if (resourceIn == null) {
LOGGER.info("Could not find backend.local.properties file. Try backend.properties instead.");
resourceIn = ExternalSOPTest.class.getResourceAsStream("backend.properties");
}
if (resourceIn == null) {
throw new FileNotFoundException("Could not find backend.properties file.");
}
properties.load(resourceIn);
String backend = properties.getProperty("sop.backend");
return backend;
} catch (IOException e) {
return null;
}
}
public static boolean externalSopInstalled() {
String binary = readSopBackendFromProperties();
if (binary == null) {
return false;
}
return new File(binary).exists();
}
@Test
public void versionNameTest() {
assertEquals("sqop", sop.version().getName());
}
@Test
public void versionVersionTest() {
String version = sop.version().getVersion();
assertTrue(version.matches("\\d+(\\.\\d+)*"));
}
@Test
public void backendVersionTest() {
String backend = sop.version().getBackendVersion();
assertFalse(backend.isEmpty());
}
@Test
public void extendedVersionTest() {
String extended = sop.version().getExtendedVersion();
assertFalse(extended.isEmpty());
}
@Test
public void generateKeyTest() throws IOException {
String key = new String(sop.generateKey().userId("Alice").generate().getBytes());
assertTrue(key.startsWith("-----BEGIN PGP PRIVATE KEY BLOCK-----\n"));
}
@Test
@Disabled
public void extractCertTest() throws IOException {
InputStream keyIn = sop.generateKey().userId("Alice").generate().getInputStream();
String cert = new String(sop.extractCert().key(keyIn).getBytes());
assertTrue(cert.startsWith("-----BEGIN PGP PUBLIC KEY BLOCK-----\n"));
}
}

View file

@ -0,0 +1,40 @@
// 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 static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
@EnabledIf("sop.external.AbstractExternalSOPTest#isExternalSopInstalled")
public class ExternalVersionTest extends AbstractExternalSOPTest {
@Test
public void versionNameTest() {
assertEquals("sqop", getSop().version().getName());
}
@Test
public void versionVersionTest() {
String version = getSop().version().getVersion();
assertTrue(version.matches("\\d+(\\.\\d+)*"));
}
@Test
public void backendVersionTest() {
String backend = getSop().version().getBackendVersion();
assertFalse(backend.isEmpty());
}
@Test
public void extendedVersionTest() {
String extended = getSop().version().getExtendedVersion();
assertFalse(extended.isEmpty());
}
}