mirror of
https://codeberg.org/PGPainless/cert-d-pgpainless.git
synced 2024-12-22 05:17:56 +01:00
CLI: Add test for Setup command
This commit is contained in:
parent
a7a89a31ed
commit
f21802523a
3 changed files with 148 additions and 1 deletions
|
@ -40,7 +40,7 @@ public class PGPCertDCli {
|
||||||
scope = CommandLine.ScopeType.INHERIT)
|
scope = CommandLine.ScopeType.INHERIT)
|
||||||
File baseDirectory;
|
File baseDirectory;
|
||||||
|
|
||||||
private static PGPainlessCertD certificateDirectory;
|
static PGPainlessCertD certificateDirectory;
|
||||||
|
|
||||||
private int executionStrategy(CommandLine.ParseResult parseResult) {
|
private int executionStrategy(CommandLine.ParseResult parseResult) {
|
||||||
try {
|
try {
|
||||||
|
@ -52,6 +52,10 @@ public class PGPCertDCli {
|
||||||
}
|
}
|
||||||
|
|
||||||
private void initStore() throws NotAStoreException, SQLException {
|
private void initStore() throws NotAStoreException, SQLException {
|
||||||
|
if (certificateDirectory != null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if (baseDirectory == null) {
|
if (baseDirectory == null) {
|
||||||
baseDirectory = BaseDirectoryProvider.getDefaultBaseDir();
|
baseDirectory = BaseDirectoryProvider.getDefaultBaseDir();
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,18 @@
|
||||||
|
// SPDX-FileCopyrightText: 2022 Paul Schaub <vanitasvitae@fsfe.org>
|
||||||
|
//
|
||||||
|
// SPDX-License-Identifier: Apache-2.0
|
||||||
|
|
||||||
|
package pgp.cert_d.cli;
|
||||||
|
|
||||||
|
import org.pgpainless.certificate_store.PGPainlessCertD;
|
||||||
|
|
||||||
|
public class InstantiateCLI {
|
||||||
|
|
||||||
|
public static void resetStore() {
|
||||||
|
PGPCertDCli.certificateDirectory = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void setInMemoryStore() {
|
||||||
|
PGPCertDCli.certificateDirectory = PGPainlessCertD.inMemory();
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,125 @@
|
||||||
|
// SPDX-FileCopyrightText: 2022 Paul Schaub <vanitasvitae@fsfe.org>
|
||||||
|
//
|
||||||
|
// SPDX-License-Identifier: Apache-2.0
|
||||||
|
|
||||||
|
package pgp.cert_d.cli.commands;
|
||||||
|
|
||||||
|
import org.bouncycastle.openpgp.PGPException;
|
||||||
|
import org.bouncycastle.openpgp.PGPSecretKeyRing;
|
||||||
|
import org.junit.jupiter.api.AfterEach;
|
||||||
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.pgpainless.PGPainless;
|
||||||
|
import org.pgpainless.certificate_store.PGPainlessCertD;
|
||||||
|
import org.pgpainless.key.OpenPgpFingerprint;
|
||||||
|
import org.pgpainless.key.info.KeyInfo;
|
||||||
|
import org.pgpainless.key.protection.UnlockSecretKey;
|
||||||
|
import org.pgpainless.util.Passphrase;
|
||||||
|
import pgp.cert_d.cli.InstantiateCLI;
|
||||||
|
import pgp.cert_d.cli.PGPCertDCli;
|
||||||
|
import pgp.certificate_store.certificate.Key;
|
||||||
|
import pgp.certificate_store.certificate.KeyMaterial;
|
||||||
|
import pgp.certificate_store.exception.BadDataException;
|
||||||
|
|
||||||
|
import java.io.ByteArrayInputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.nio.charset.Charset;
|
||||||
|
import java.security.InvalidAlgorithmParameterException;
|
||||||
|
import java.security.NoSuchAlgorithmException;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertNotEquals;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||||
|
|
||||||
|
public class SetupTest {
|
||||||
|
|
||||||
|
private PGPainlessCertD store;
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
public void setup() {
|
||||||
|
InstantiateCLI.setInMemoryStore();
|
||||||
|
store = PGPCertDCli.getCertificateDirectory();
|
||||||
|
}
|
||||||
|
|
||||||
|
@AfterEach
|
||||||
|
public void teardown() {
|
||||||
|
InstantiateCLI.resetStore();
|
||||||
|
store = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testSetupGeneratesTrustRoot()
|
||||||
|
throws BadDataException, IOException {
|
||||||
|
assertNull(store.getTrustRoot());
|
||||||
|
|
||||||
|
PGPCertDCli.main(new String[] {"setup"});
|
||||||
|
KeyMaterial trustRoot = store.getTrustRoot();
|
||||||
|
assertNotNull(trustRoot);
|
||||||
|
assertTrue(trustRoot instanceof Key);
|
||||||
|
|
||||||
|
// Check that key has no password
|
||||||
|
PGPSecretKeyRing secretKeys = PGPainless.readKeyRing().secretKeyRing(trustRoot.getInputStream());
|
||||||
|
assertTrue(KeyInfo.isDecrypted(secretKeys.getSecretKey()));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testSetupWithPassword()
|
||||||
|
throws BadDataException, IOException, PGPException {
|
||||||
|
assertNull(store.getTrustRoot());
|
||||||
|
|
||||||
|
PGPCertDCli.main(new String[] {"setup", "--with-password", "sw0rdf1sh"});
|
||||||
|
KeyMaterial trustRoot = store.getTrustRoot();
|
||||||
|
assertNotNull(trustRoot);
|
||||||
|
assertTrue(trustRoot instanceof Key);
|
||||||
|
|
||||||
|
// Check that key is encrypted
|
||||||
|
PGPSecretKeyRing secretKeys = PGPainless.readKeyRing().secretKeyRing(trustRoot.getInputStream());
|
||||||
|
assertTrue(KeyInfo.isEncrypted(secretKeys.getSecretKey()));
|
||||||
|
// Check that password matches
|
||||||
|
assertNotNull(UnlockSecretKey.unlockSecretKey(
|
||||||
|
secretKeys.getSecretKey(), Passphrase.fromPassword("sw0rdf1sh")));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testSetupImportFromStdin()
|
||||||
|
throws PGPException, InvalidAlgorithmParameterException, NoSuchAlgorithmException,
|
||||||
|
BadDataException, IOException {
|
||||||
|
assertNull(store.getTrustRoot());
|
||||||
|
|
||||||
|
PGPSecretKeyRing trustRoot = PGPainless.generateKeyRing()
|
||||||
|
.modernKeyRing("trust-root");
|
||||||
|
OpenPgpFingerprint fingerprint = OpenPgpFingerprint.of(trustRoot);
|
||||||
|
String armored = PGPainless.asciiArmor(trustRoot);
|
||||||
|
ByteArrayInputStream trustRootIn = new ByteArrayInputStream(
|
||||||
|
armored.getBytes(Charset.forName("UTF8")));
|
||||||
|
|
||||||
|
InputStream originalStdin = System.in;
|
||||||
|
System.setIn(trustRootIn);
|
||||||
|
PGPCertDCli.main(new String[] {"setup", "--import-from-stdin"});
|
||||||
|
System.setIn(originalStdin);
|
||||||
|
|
||||||
|
KeyMaterial importedTrustRoot = store.getTrustRoot();
|
||||||
|
assertEquals(fingerprint.toString().toLowerCase(), importedTrustRoot.getFingerprint());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testSetupOverridesExistingTrustRoot()
|
||||||
|
throws BadDataException, IOException {
|
||||||
|
assertNull(store.getTrustRoot());
|
||||||
|
|
||||||
|
PGPCertDCli.main(new String[] {"setup"});
|
||||||
|
KeyMaterial trustRoot = store.getTrustRoot();
|
||||||
|
assertNotNull(trustRoot);
|
||||||
|
String fingerprint = trustRoot.getFingerprint();
|
||||||
|
|
||||||
|
// Override trust-root by calling setup again
|
||||||
|
PGPCertDCli.main(new String[] {"setup"});
|
||||||
|
trustRoot = store.getTrustRoot();
|
||||||
|
assertNotNull(trustRoot);
|
||||||
|
|
||||||
|
assertNotEquals(fingerprint, trustRoot.getFingerprint());
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue