1
0
Fork 0
mirror of https://github.com/pgpainless/pgpainless.git synced 2025-12-16 17:21:08 +01:00
This commit is contained in:
Paul Schaub 2022-02-03 14:38:08 +01:00
parent 3ba57109a1
commit ddcd4bff00
17 changed files with 478 additions and 189 deletions

View file

@ -9,7 +9,7 @@ import java.io.InputStream;
import java.util.Iterator;
import pgp.cert_d.SharedPGPCertificateDirectory;
import pgp.cert_d.SpecialName;
import pgp.cert_d.SpecialNames;
import pgp.cert_d.exception.BadDataException;
import pgp.cert_d.exception.BadNameException;
import pgp.certificate_store.Certificate;
@ -37,38 +37,42 @@ public class SharedPGPCertificateDirectoryAdapter
@Override
public Certificate getCertificate(String identifier)
throws IOException {
SpecialName specialName = SpecialName.fromString(identifier);
String specialName = SpecialNames.lookupSpecialName(identifier);
if (specialName != null) {
try {
return directory.get(specialName);
return directory.getBySpecialName(specialName);
} catch (BadNameException e) {
throw new IllegalArgumentException("Unknown special name " + identifier, e);
}
}
try {
return directory.get(identifier);
return directory.getByFingerprint(identifier);
} catch (BadNameException e) {
throw new IllegalArgumentException("Invalid fingerprint or unknown special name " + identifier, e);
throw new IllegalArgumentException("Invalid fingerprint " + identifier, e);
} catch (BadDataException e) {
throw new IOException("Bad data.", e);
}
}
@Override
public Certificate getCertificateIfChanged(String identifier, String tag)
throws IOException {
SpecialName specialName = SpecialName.fromString(identifier);
String specialName = SpecialNames.lookupSpecialName(identifier);
if (specialName != null) {
try {
return directory.getIfChanged(specialName, tag);
return directory.getBySpecialNameIfChanged(specialName, tag);
} catch (BadNameException e) {
throw new IllegalArgumentException("Unknown special name " + identifier, e);
}
}
try {
return directory.getIfChanged(identifier, tag);
return directory.getByFingerprintIfChanged(identifier, tag);
} catch (BadNameException e) {
throw new IllegalArgumentException("Invalid fingerprint or unknown special name " + identifier, e);
throw new IllegalArgumentException("Invalid fingerprint " + identifier, e);
} catch (BadDataException e) {
throw new IOException("Bad data.", e);
}
}
@ -96,12 +100,12 @@ public class SharedPGPCertificateDirectoryAdapter
public Certificate insertCertificateBySpecialName(String specialName, InputStream data, MergeCallback merge)
throws IOException, InterruptedException {
try {
SpecialName specialNameEnum = SpecialName.fromString(specialName);
if (specialNameEnum == null) {
String specialNameValidated = SpecialNames.lookupSpecialName(specialName);
if (specialNameValidated == null) {
throw new IllegalArgumentException("Unknown special name " + specialName);
}
return directory.insertSpecial(specialNameEnum, data, merge);
return directory.insertWithSpecialName(specialNameValidated, data, merge);
} catch (BadNameException e) {
throw new IllegalArgumentException("Unknown special name " + specialName);
} catch (BadDataException e) {
@ -113,12 +117,12 @@ public class SharedPGPCertificateDirectoryAdapter
public Certificate tryInsertCertificateBySpecialName(String specialName, InputStream data, MergeCallback merge)
throws IOException {
try {
SpecialName specialNameEnum = SpecialName.fromString(specialName);
if (specialNameEnum == null) {
String specialNameValidated = SpecialNames.lookupSpecialName(specialName);
if (specialNameValidated == null) {
throw new IllegalArgumentException("Unknown special name " + specialName);
}
return directory.tryInsertSpecial(specialNameEnum, data, merge);
return directory.tryInsertWithSpecialName(specialNameValidated, data, merge);
} catch (BadNameException e) {
throw new IllegalArgumentException("Unknown special name " + specialName);
} catch (BadDataException e) {

View file

@ -12,7 +12,7 @@ import org.junit.jupiter.api.Test;
import org.junit.platform.commons.logging.Logger;
import org.junit.platform.commons.logging.LoggerFactory;
import org.pgpainless.PGPainless;
import org.pgpainless.certificate_store.CertificateCertificateReader;
import org.pgpainless.certificate_store.CertificateReader;
import org.pgpainless.key.OpenPgpFingerprint;
import pgp.cert_d.FileLockingMechanism;
import pgp.cert_d.LockingMechanism;
@ -37,8 +37,9 @@ import static org.junit.jupiter.api.Assertions.assertNull;
public class SharedPGPCertificateDirectoryTest {
Logger logger = LoggerFactory.getLogger(SharedPGPCertificateDirectoryTest.class);
SharedPGPCertificateDirectory directory;
private static final Logger logger = LoggerFactory.getLogger(SharedPGPCertificateDirectoryTest.class);
private File tempDir;
private SharedPGPCertificateDirectory directory;
private static MergeCallback dummyMerge = new MergeCallback() {
@Override
@ -49,29 +50,29 @@ public class SharedPGPCertificateDirectoryTest {
@BeforeEach
public void beforeEach() throws IOException, NotAStoreException {
File tempDir = Files.createTempDirectory("pgp.cert.d-").toFile();
tempDir = Files.createTempDirectory("pgp.cert.d-").toFile();
tempDir.deleteOnExit();
directory = new SharedPGPCertificateDirectoryImpl(tempDir, new CertificateCertificateReader());
directory = new SharedPGPCertificateDirectoryImpl(tempDir, new CertificateReader());
}
@Test
public void simpleInsertGet() throws PGPException, InvalidAlgorithmParameterException, NoSuchAlgorithmException, IOException, BadDataException, InterruptedException, BadNameException {
logger.info(() -> "simpleInsertGet: " + ((SharedPGPCertificateDirectoryImpl) directory).getBaseDirectory().getAbsolutePath());
logger.info(() -> "simpleInsertGet: " + tempDir.getAbsolutePath());
PGPSecretKeyRing key = PGPainless.generateKeyRing().modernKeyRing("Alice", null);
PGPPublicKeyRing cert = PGPainless.extractCertificate(key);
OpenPgpFingerprint fingerprint = OpenPgpFingerprint.of(cert);
ByteArrayInputStream certIn = new ByteArrayInputStream(cert.getEncoded());
// standard case: get() is null
assertNull(directory.get(fingerprint.toString().toLowerCase()));
assertNull(directory.getByFingerprint(fingerprint.toString().toLowerCase()));
// insert and check returned certs fingerprint
Certificate certificate = directory.insert(certIn, dummyMerge);
assertEquals(fingerprint.toString().toLowerCase(), certificate.getFingerprint());
// getIfChanged
assertNull(directory.getIfChanged(certificate.getFingerprint(), certificate.getTag()));
assertNotNull(directory.getIfChanged(certificate.getFingerprint(), "invalidTag"));
assertNull(directory.getByFingerprintIfChanged(certificate.getFingerprint(), certificate.getTag()));
assertNotNull(directory.getByFingerprintIfChanged(certificate.getFingerprint(), "invalidTag"));
// tryInsert
certIn = new ByteArrayInputStream(cert.getEncoded());
@ -80,13 +81,12 @@ public class SharedPGPCertificateDirectoryTest {
@Test
public void tryInsertFailsWithLockedStore() throws PGPException, InvalidAlgorithmParameterException, NoSuchAlgorithmException, IOException, BadDataException, InterruptedException {
SharedPGPCertificateDirectoryImpl fileDirectory = (SharedPGPCertificateDirectoryImpl) directory;
logger.info(() -> "tryInsertFailsWithLockedStore: " + fileDirectory.getBaseDirectory().getAbsolutePath());
logger.info(() -> "tryInsertFailsWithLockedStore: " + tempDir.getAbsolutePath());
PGPSecretKeyRing key = PGPainless.generateKeyRing().modernKeyRing("Alice", null);
PGPPublicKeyRing cert = PGPainless.extractCertificate(key);
ByteArrayInputStream certIn = new ByteArrayInputStream(cert.getEncoded());
File lockFile = new File(fileDirectory.getBaseDirectory(), "writelock");
File lockFile = new File(tempDir, "writelock");
LockingMechanism lock = new FileLockingMechanism(lockFile);
lock.lockDirectory();