mirror of
https://github.com/pgpainless/pgpainless.git
synced 2025-12-16 09:11:08 +01:00
Some testing and SubkeyLookup mechanism
This commit is contained in:
parent
53e8c03b8a
commit
2e6a1dcbc5
13 changed files with 226 additions and 49 deletions
|
|
@ -14,6 +14,7 @@ repositories {
|
|||
|
||||
dependencies {
|
||||
testImplementation "org.junit.jupiter:junit-jupiter-api:$junitVersion"
|
||||
testImplementation "org.junit.jupiter:junit-jupiter-params:$junitVersion"
|
||||
testRuntimeOnly "org.junit.jupiter:junit-jupiter-engine:$junitVersion"
|
||||
|
||||
// Logging
|
||||
|
|
|
|||
|
|
@ -5,6 +5,7 @@
|
|||
package pgp.cert_d;
|
||||
|
||||
import java.io.File;
|
||||
import java.nio.file.Paths;
|
||||
|
||||
public class BaseDirectoryProvider {
|
||||
|
||||
|
|
@ -18,29 +19,28 @@ public class BaseDirectoryProvider {
|
|||
// return OS-specific default dir
|
||||
String osName = System.getProperty("os.name", "generic")
|
||||
.toLowerCase();
|
||||
return getDefaultBaseDirForOS(osName, File.separator);
|
||||
return getDefaultBaseDirForOS(osName);
|
||||
}
|
||||
|
||||
public static File getDefaultBaseDirForOS(String osName, String separator) {
|
||||
public static File getDefaultBaseDirForOS(String osName) {
|
||||
String STORE_NAME = "pgp.cert.d";
|
||||
if (osName.contains("win")) {
|
||||
String appData = System.getenv("APPDATA");
|
||||
String roaming = appData + separator + "Roaming";
|
||||
return new File(roaming, STORE_NAME);
|
||||
// %APPDATA%\Roaming\pgp.cert.d
|
||||
return Paths.get(System.getenv("APPDATA"), "Roaming", STORE_NAME).toFile();
|
||||
}
|
||||
|
||||
if (osName.contains("nux")) {
|
||||
// $XDG_DATA_HOME/pgp.cert.d
|
||||
String xdg_data_home = System.getenv("XDG_DATA_HOME");
|
||||
String rootPath = xdg_data_home;
|
||||
if (xdg_data_home == null) {
|
||||
rootPath = System.getProperty("user.home") + separator + ".local" + separator + "share";
|
||||
if (xdg_data_home != null) {
|
||||
return Paths.get(xdg_data_home, STORE_NAME).toFile();
|
||||
}
|
||||
return new File(rootPath, STORE_NAME);
|
||||
// $HOME/.local/share/pgp.cert.d
|
||||
return Paths.get(System.getProperty("user.home"), ".local", "share", STORE_NAME).toFile();
|
||||
}
|
||||
|
||||
if (osName.contains("mac")) {
|
||||
String home = System.getenv("HOME");
|
||||
return new File(home + separator + "Library" + separator + "Application Support", STORE_NAME);
|
||||
return Paths.get(System.getenv("HOME"), "Library", "Application Support", STORE_NAME).toFile();
|
||||
}
|
||||
|
||||
throw new IllegalArgumentException("Unknown OS " + osName);
|
||||
|
|
|
|||
|
|
@ -75,6 +75,11 @@ public class CachingSharedPGPCertificateDirectoryWrapper
|
|||
tagMap.clear();
|
||||
}
|
||||
|
||||
@Override
|
||||
public LockingMechanism getLock() {
|
||||
return underlyingCertificateDirectory.getLock();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Certificate getByFingerprint(String fingerprint)
|
||||
throws IOException, BadNameException, BadDataException {
|
||||
|
|
|
|||
|
|
@ -0,0 +1,23 @@
|
|||
package pgp.cert_d;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class InMemorySubkeyLookup implements SubkeyLookup {
|
||||
|
||||
private static final Map<Long, String> subkeyMap = new HashMap<>();
|
||||
|
||||
@Override
|
||||
public String getIdentifierForSubkeyId(long subkeyId) {
|
||||
return subkeyMap.get(subkeyId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void storeIdentifierForSubkeyId(long subkeyId, String identifier) {
|
||||
subkeyMap.put(subkeyId, identifier);
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
subkeyMap.clear();
|
||||
}
|
||||
}
|
||||
|
|
@ -15,6 +15,8 @@ import pgp.certificate_store.MergeCallback;
|
|||
|
||||
public interface SharedPGPCertificateDirectory {
|
||||
|
||||
LockingMechanism getLock();
|
||||
|
||||
Certificate getByFingerprint(String fingerprint)
|
||||
throws IOException, BadNameException, BadDataException;
|
||||
|
||||
|
|
|
|||
|
|
@ -69,6 +69,11 @@ public class SharedPGPCertificateDirectoryImpl implements SharedPGPCertificateDi
|
|||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public LockingMechanism getLock() {
|
||||
return writeLock;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Certificate getByFingerprint(String fingerprint)
|
||||
throws IOException, BadNameException, BadDataException {
|
||||
|
|
|
|||
25
pgp-cert-d-java/src/main/java/pgp/cert_d/SubkeyLookup.java
Normal file
25
pgp-cert-d-java/src/main/java/pgp/cert_d/SubkeyLookup.java
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
// SPDX-FileCopyrightText: 2022 Paul Schaub <vanitasvitae@fsfe.org>
|
||||
//
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package pgp.cert_d;
|
||||
|
||||
public interface SubkeyLookup {
|
||||
|
||||
/**
|
||||
* Lookup the identifier of the certificate that contains the given subkey.
|
||||
* If no record is found, return null.
|
||||
*
|
||||
* @param subkeyId subkey id
|
||||
* @return identifier (fingerprint or special name) of the certificate
|
||||
*/
|
||||
String getIdentifierForSubkeyId(long subkeyId);
|
||||
|
||||
/**
|
||||
* Store a record of the subkey id that points to the identifier.
|
||||
*
|
||||
* @param subkeyId subkey id
|
||||
* @param identifier fingerprint or special name of the certificate
|
||||
*/
|
||||
void storeIdentifierForSubkeyId(long subkeyId, String identifier);
|
||||
}
|
||||
|
|
@ -0,0 +1,19 @@
|
|||
// SPDX-FileCopyrightText: 2022 Paul Schaub <vanitasvitae@fsfe.org>
|
||||
//
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package pgp.cert_d;
|
||||
|
||||
public class SubkeyLookupImpl implements SubkeyLookup {
|
||||
|
||||
@Override
|
||||
public String getIdentifierForSubkeyId(long subkeyId) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void storeIdentifierForSubkeyId(long subkeyId, String identifier) {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,22 @@
|
|||
// SPDX-FileCopyrightText: 2022 Paul Schaub <vanitasvitae@fsfe.org>
|
||||
//
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package pgp.cert_d;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.junit.jupiter.api.Assumptions.assumeTrue;
|
||||
|
||||
public class BaseDirectoryProviderTest {
|
||||
|
||||
@Test
|
||||
public void testGetDefaultBaseDir_Linux() {
|
||||
assumeTrue(System.getProperty("os.name").equalsIgnoreCase("linux"));
|
||||
File baseDir = BaseDirectoryProvider.getDefaultBaseDirForOS("linux");
|
||||
assertTrue(baseDir.getAbsolutePath().endsWith("/.local/share/pgp.cert.d"));
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,69 @@
|
|||
// SPDX-FileCopyrightText: 2022 Paul Schaub <vanitasvitae@fsfe.org>
|
||||
//
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package pgp.cert_d;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import org.junit.jupiter.api.AfterAll;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.MethodSource;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
|
||||
public class SubkeyLookupTest {
|
||||
|
||||
private static final List<SubkeyLookup> testSubjects = new ArrayList<>();
|
||||
|
||||
@BeforeAll
|
||||
public static void setupLookupTestSubjects() {
|
||||
testSubjects.add(new InMemorySubkeyLookup());
|
||||
}
|
||||
|
||||
@AfterAll
|
||||
public static void tearDownLookupTestSubjects() {
|
||||
((InMemorySubkeyLookup) testSubjects.get(0)).clear();
|
||||
}
|
||||
|
||||
private static Stream<SubkeyLookup> provideSubkeyLookupsForTest() {
|
||||
return testSubjects.stream();
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@MethodSource("provideSubkeyLookupsForTest")
|
||||
public void testInsertGet(SubkeyLookup subject) {
|
||||
// Initially all null
|
||||
|
||||
assertNull(subject.getIdentifierForSubkeyId(123));
|
||||
assertNull(subject.getIdentifierForSubkeyId(1337));
|
||||
assertNull(subject.getIdentifierForSubkeyId(420));
|
||||
|
||||
// Store one val, others still null
|
||||
|
||||
subject.storeIdentifierForSubkeyId(123, "trust-root");
|
||||
|
||||
assertEquals("trust-root", subject.getIdentifierForSubkeyId(123));
|
||||
assertNull(subject.getIdentifierForSubkeyId(1337));
|
||||
assertNull(subject.getIdentifierForSubkeyId(420));
|
||||
|
||||
// Store other val, first stays intact
|
||||
|
||||
subject.storeIdentifierForSubkeyId(1337, "d1a66e1a23b182c9980f788cfbfcc82a015e7330");
|
||||
subject.storeIdentifierForSubkeyId(420, "d1a66e1a23b182c9980f788cfbfcc82a015e7330");
|
||||
|
||||
assertEquals("trust-root", subject.getIdentifierForSubkeyId(123));
|
||||
assertEquals("d1a66e1a23b182c9980f788cfbfcc82a015e7330", subject.getIdentifierForSubkeyId(1337));
|
||||
assertEquals("d1a66e1a23b182c9980f788cfbfcc82a015e7330", subject.getIdentifierForSubkeyId(420));
|
||||
|
||||
// overwrite existing
|
||||
|
||||
subject.storeIdentifierForSubkeyId(123, "d1a66e1a23b182c9980f788cfbfcc82a015e7330");
|
||||
|
||||
assertEquals("d1a66e1a23b182c9980f788cfbfcc82a015e7330", subject.getIdentifierForSubkeyId(123));
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue