Initial commit

This commit is contained in:
Paul Schaub 2022-03-01 15:19:01 +01:00
commit b142f310be
Signed by: vanitasvitae
GPG key ID: 62BEE9264BF17311
46 changed files with 2494 additions and 0 deletions

View file

@ -0,0 +1,43 @@
// 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.assertNotNull;
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"));
}
@Test
public void testGetDefaultBaseDir_Windows() {
assumeTrue(System.getProperty("os.name").toLowerCase().contains("win"));
File baseDir = BaseDirectoryProvider.getDefaultBaseDirForOS("Windows");
assertTrue(baseDir.getAbsolutePath().endsWith("\\Roaming\\pgp.cert.d"));
}
@Test
public void testGetDefaultBaseDir_Mac() {
assumeTrue(System.getProperty("os.name").toLowerCase().contains("mac"));
File baseDir = BaseDirectoryProvider.getDefaultBaseDirForOS("Mac");
assertTrue(baseDir.getAbsolutePath().endsWith("/Library/Application Support/pgp.cert.d"));
}
@Test
public void testGetDefaultBaseDirNotNull() {
File baseDir = BaseDirectoryProvider.getDefaultBaseDir();
assertNotNull(baseDir);
}
}

View file

@ -0,0 +1,80 @@
// SPDX-FileCopyrightText: 2022 Paul Schaub <vanitasvitae@fsfe.org>
//
// SPDX-License-Identifier: Apache-2.0
package pgp.cert_d;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import pgp.certificate_store.exception.BadNameException;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
public class FilenameResolverTest {
private File baseDir;
private FilenameResolver resolver;
@BeforeEach
public void setup() throws IOException {
baseDir = Files.createTempDirectory("filenameresolver").toFile();
baseDir.deleteOnExit();
resolver = new FilenameResolver(baseDir);
}
@Test
public void testGetFileForFingerprint1() throws BadNameException {
String fingerprint = "d1a66e1a23b182c9980f788cfbfcc82a015e7330";
File subDir = new File(baseDir, "d1");
File expected = new File(subDir, "a66e1a23b182c9980f788cfbfcc82a015e7330");
assertEquals(expected.getAbsolutePath(), resolver.getCertFileByFingerprint(fingerprint).getAbsolutePath());
}
@Test
public void testGetFileForFingerprint2() throws BadNameException {
String fingerprint = "eb85bb5fa33a75e15e944e63f231550c4f47e38e";
File subDir = new File(baseDir, "eb");
File expected = new File(subDir, "85bb5fa33a75e15e944e63f231550c4f47e38e");
assertEquals(expected.getAbsolutePath(), resolver.getCertFileByFingerprint(fingerprint).getAbsolutePath());
}
@Test
public void testGetFileForInvalidNonHexFingerprint() {
String invalidFingerprint = "thisisnothexadecimalthisisnothexadecimal";
assertThrows(BadNameException.class, () -> resolver.getCertFileByFingerprint(invalidFingerprint));
}
@Test
public void testGetFileForInvalidWrongLengthFingerprint() {
String invalidFingerprint = "d1a66e1a23b182c9980f788cfbfcc82a015e73301234";
assertThrows(BadNameException.class, () -> resolver.getCertFileByFingerprint(invalidFingerprint));
}
@Test
public void testGetFileForNullFingerprint() {
assertThrows(NullPointerException.class, () -> resolver.getCertFileByFingerprint(null));
}
@Test
public void testGetFileForSpecialName() throws BadNameException {
String specialName = "trust-root";
File expected = new File(baseDir, "trust-root");
assertEquals(expected, resolver.getCertFileBySpecialName(specialName));
}
@Test
public void testGetFileForInvalidSpecialName() {
String invalidSpecialName = "invalid";
assertThrows(BadNameException.class, () -> resolver.getCertFileBySpecialName(invalidSpecialName));
}
}

View file

@ -0,0 +1,26 @@
// 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 static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
public class SpecialNamesTest {
@Test
public void bothTrustRootNotationsAreRecognized() {
assertEquals("trust-root", SpecialNames.lookupSpecialName("trust-root"));
assertEquals("trust-root", SpecialNames.lookupSpecialName("TRUST-ROOT"));
}
@Test
public void testInvalidSpecialNameReturnsNull() {
assertNull(SpecialNames.lookupSpecialName("invalid"));
assertNull(SpecialNames.lookupSpecialName("trust root"));
assertNull(SpecialNames.lookupSpecialName("writelock"));
}
}

View file

@ -0,0 +1,88 @@
// SPDX-FileCopyrightText: 2022 Paul Schaub <vanitasvitae@fsfe.org>
//
// SPDX-License-Identifier: Apache-2.0
package pgp.cert_d;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
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 pgp.cert_d.jdbc.sqlite.DatabaseSubkeyLookup;
import pgp.cert_d.jdbc.sqlite.SqliteSubkeyLookupDaoImpl;
import pgp.certificate_store.SubkeyLookup;
public class SubkeyLookupTest {
private static final List<SubkeyLookup> testSubjects = new ArrayList<>();
@BeforeAll
public static void setupLookupTestSubjects() throws IOException, SQLException {
InMemorySubkeyLookup inMemorySubkeyLookup = new InMemorySubkeyLookup();
testSubjects.add(inMemorySubkeyLookup);
File sqliteDatabase = Files.createTempFile("subkeyLookupTest", ".db").toFile();
sqliteDatabase.createNewFile();
sqliteDatabase.deleteOnExit();
DatabaseSubkeyLookup sqliteSubkeyLookup = new DatabaseSubkeyLookup(SqliteSubkeyLookupDaoImpl.forDatabaseFile(sqliteDatabase));
testSubjects.add(sqliteSubkeyLookup);
}
@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) throws IOException {
// Initially all null
assertTrue(subject.getCertificateFingerprintsForSubkeyId(123).isEmpty());
assertTrue(subject.getCertificateFingerprintsForSubkeyId(1337).isEmpty());
assertTrue(subject.getCertificateFingerprintsForSubkeyId(420).isEmpty());
// Store one val, others still null
subject.storeCertificateSubkeyIds("d1a66e1a23b182c9980f788cfbfcc82a015e7330", Collections.singletonList(123L));
assertEquals(Collections.singleton("d1a66e1a23b182c9980f788cfbfcc82a015e7330"), subject.getCertificateFingerprintsForSubkeyId(123));
assertTrue(subject.getCertificateFingerprintsForSubkeyId(1337).isEmpty());
assertTrue(subject.getCertificateFingerprintsForSubkeyId(420).isEmpty());
// Store other val, first stays intact
subject.storeCertificateSubkeyIds("d1a66e1a23b182c9980f788cfbfcc82a015e7330", Collections.singletonList(1337L));
subject.storeCertificateSubkeyIds("d1a66e1a23b182c9980f788cfbfcc82a015e7330", Collections.singletonList(420L));
assertEquals(Collections.singleton("d1a66e1a23b182c9980f788cfbfcc82a015e7330"), subject.getCertificateFingerprintsForSubkeyId(123));
assertEquals(Collections.singleton("d1a66e1a23b182c9980f788cfbfcc82a015e7330"), subject.getCertificateFingerprintsForSubkeyId(1337));
assertEquals(Collections.singleton("d1a66e1a23b182c9980f788cfbfcc82a015e7330"), subject.getCertificateFingerprintsForSubkeyId(420));
// add additional entry for subkey
subject.storeCertificateSubkeyIds("eb85bb5fa33a75e15e944e63f231550c4f47e38e", Collections.singletonList(123L));
assertEquals(
new HashSet<>(Arrays.asList("eb85bb5fa33a75e15e944e63f231550c4f47e38e", "d1a66e1a23b182c9980f788cfbfcc82a015e7330")),
subject.getCertificateFingerprintsForSubkeyId(123));
}
}