1
0
Fork 0
mirror of https://github.com/pgpainless/pgpainless.git synced 2025-09-13 20:29:39 +02:00

Increase test coverage by writing bunch of JUnit tests

This commit is contained in:
Paul Schaub 2021-01-22 20:03:20 +01:00
parent ee1f90e850
commit bec2fb5ce1
Signed by: vanitasvitae
GPG key ID: 62BEE9264BF17311
15 changed files with 457 additions and 190 deletions

View file

@ -105,9 +105,20 @@ public class KeyRingInfoTest {
PGPPublicKeyRing publicKeys = KeyRingUtils.publicKeyRingFrom(secretKeys);
KeyRingInfo info = PGPainless.inspectKeyRing(secretKeys);
assertEquals(secretKeys.getSecretKey(), info.getSecretKey());
assertEquals(KeyRingUtils.requirePrimarySecretKeyFrom(secretKeys), info.getSecretKey());
info = PGPainless.inspectKeyRing(publicKeys);
assertNull(info.getSecretKey());
}
@Test
public void testGetPublicKey() throws IOException, PGPException {
PGPSecretKeyRing secretKeys = TestKeys.getCryptieSecretKeyRing();
KeyRingInfo info = PGPainless.inspectKeyRing(secretKeys);
assertEquals(KeyRingUtils.requirePrimaryPublicKeyFrom(secretKeys), info.getPublicKey());
assertEquals(KeyRingUtils.requirePrimarySecretKeyFrom(secretKeys),
KeyRingUtils.requireSecretKeyFrom(secretKeys, secretKeys.getPublicKey().getKeyID()));
}
}

View file

@ -113,7 +113,6 @@ public class UserIdRevocationTest {
.withReason(RevocationAttributes.Reason.USER_ID_NO_LONGER_VALID)
.withDescription("I lost my mail password"))
.done();
PGPSignature s = PGPainless.inspectKeyRing(secretKeys).getLatestValidSelfSignatureOnKey();
PGPSignature signature = SignatureUtils.getLatestSelfSignatureForUserId(secretKeys.getPublicKey(), "secondary@key.id");
RevocationReason reason = (RevocationReason) signature.getHashedSubPackets()
.getSubpacket(SignatureSubpacketTags.REVOCATION_REASON);

View file

@ -0,0 +1,55 @@
/*
* Copyright 2021 Paul Schaub.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.pgpainless.key.protection;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import java.io.IOException;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import org.bouncycastle.openpgp.PGPException;
import org.bouncycastle.openpgp.PGPSecretKeyRing;
import org.junit.jupiter.api.Test;
import org.pgpainless.key.TestKeys;
import org.pgpainless.key.protection.passphrase_provider.MapBasedPassphraseProvider;
import org.pgpainless.util.Passphrase;
public class MapBasedPassphraseProviderTest {
@Test
public void testMapBasedProvider() throws IOException, PGPException {
Map<Long, Passphrase> passphraseMap = new ConcurrentHashMap<>();
passphraseMap.put(1L, Passphrase.fromPassword("tiger"));
passphraseMap.put(123123123L, Passphrase.fromPassword("snake"));
passphraseMap.put(69696969L, Passphrase.emptyPassphrase());
MapBasedPassphraseProvider provider = new MapBasedPassphraseProvider(passphraseMap);
assertEquals(Passphrase.fromPassword("tiger"), provider.getPassphraseFor(1L));
assertEquals(Passphrase.fromPassword("snake"), provider.getPassphraseFor(123123123L));
assertEquals(Passphrase.emptyPassphrase(), provider.getPassphraseFor(69696969L));
assertNull(provider.getPassphraseFor(555L));
PGPSecretKeyRing secretKeys = TestKeys.getCryptieSecretKeyRing();
passphraseMap = new ConcurrentHashMap<>();
passphraseMap.put(secretKeys.getSecretKey().getKeyID(), TestKeys.CRYPTIE_PASSPHRASE);
provider = new MapBasedPassphraseProvider(passphraseMap);
assertEquals(TestKeys.CRYPTIE_PASSPHRASE, provider.getPassphraseFor(secretKeys.getSecretKey()));
assertNull(provider.getPassphraseFor(TestKeys.getEmilSecretKeyRing().getSecretKey()));
}
}

View file

@ -0,0 +1,135 @@
/*
* Copyright 2021 Paul Schaub.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.pgpainless.key.protection;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import java.io.IOException;
import java.security.InvalidAlgorithmParameterException;
import java.security.NoSuchAlgorithmException;
import java.util.Iterator;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.ConcurrentHashMap;
import javax.annotation.Nullable;
import org.bouncycastle.openpgp.PGPException;
import org.bouncycastle.openpgp.PGPSecretKey;
import org.bouncycastle.openpgp.PGPSecretKeyRing;
import org.bouncycastle.openpgp.operator.PBESecretKeyDecryptor;
import org.junit.jupiter.api.Test;
import org.pgpainless.PGPainless;
import org.pgpainless.key.TestKeys;
import org.pgpainless.key.protection.passphrase_provider.SecretKeyPassphraseProvider;
import org.pgpainless.util.Passphrase;
public class SecretKeyRingProtectorTest {
@Test
public void testUnlockAllKeysWithSamePassword() throws IOException, PGPException, InvalidAlgorithmParameterException, NoSuchAlgorithmException {
PGPSecretKeyRing secretKeys = TestKeys.getCryptieSecretKeyRing();
SecretKeyRingProtector protector = SecretKeyRingProtector.unlockAllKeysWith(TestKeys.CRYPTIE_PASSPHRASE, secretKeys);
for (PGPSecretKey secretKey : secretKeys) {
PBESecretKeyDecryptor decryptor = protector.getDecryptor(secretKey.getKeyID());
assertNotNull(decryptor);
secretKey.extractPrivateKey(decryptor);
}
PGPSecretKeyRing unrelatedKeys = PGPainless.generateKeyRing().simpleEcKeyRing("unrelated",
"SecurePassword");
for (PGPSecretKey unrelatedKey : unrelatedKeys) {
PBESecretKeyDecryptor decryptor = protector.getDecryptor(unrelatedKey.getKeyID());
assertNull(decryptor);
assertThrows(PGPException.class, () -> unrelatedKey.extractPrivateKey(protector.getDecryptor(unrelatedKey.getKeyID())));
}
}
@Test
public void testUnprotectedKeys() throws PGPException {
Random random = new Random();
SecretKeyRingProtector protector = SecretKeyRingProtector.unprotectedKeys();
for (int i = 0; i < 10; i++) {
Long keyId = random.nextLong();
assertNull(protector.getEncryptor(keyId));
assertNull(protector.getDecryptor(keyId));
}
}
@Test
public void testUnlockSingleKeyWithPassphrase() throws IOException, PGPException {
PGPSecretKeyRing secretKeys = TestKeys.getCryptieSecretKeyRing();
Iterator<PGPSecretKey> iterator = secretKeys.iterator();
PGPSecretKey secretKey = iterator.next();
PGPSecretKey subKey = iterator.next();
SecretKeyRingProtector protector = SecretKeyRingProtector.unlockSingleKeyWith(TestKeys.CRYPTIE_PASSPHRASE, secretKey);
assertNotNull(protector.getDecryptor(secretKey.getKeyID()));
assertNotNull(protector.getEncryptor(secretKey.getKeyID()));
assertNull(protector.getEncryptor(subKey.getKeyID()));
assertNull(protector.getDecryptor(subKey.getKeyID()));
}
@Test
public void testFromPassphraseMap() {
Map<Long, Passphrase> passphraseMap = new ConcurrentHashMap<>();
passphraseMap.put(1L, Passphrase.emptyPassphrase());
PassphraseMapKeyRingProtector protector = (PassphraseMapKeyRingProtector) SecretKeyRingProtector.fromPassphraseMap(passphraseMap);
assertNotNull(protector.getPassphraseFor(1L));
assertNull(protector.getPassphraseFor(5L));
protector.addPassphrase(5L, Passphrase.fromPassword("pa55w0rd"));
protector.forgetPassphrase(1L);
assertNull(protector.getPassphraseFor(1L));
assertNotNull(protector.getPassphraseFor(5L));
}
@Test
public void testMissingPassphraseCallback() {
Map<Long, Passphrase> passphraseMap = new ConcurrentHashMap<>();
passphraseMap.put(1L, Passphrase.emptyPassphrase());
PassphraseMapKeyRingProtector protector = new PassphraseMapKeyRingProtector(passphraseMap,
KeyRingProtectionSettings.secureDefaultSettings(), new SecretKeyPassphraseProvider() {
@Nullable
@Override
public Passphrase getPassphraseFor(Long keyId) {
return Passphrase.fromPassword("missingP455w0rd");
}
});
assertEquals(Passphrase.emptyPassphrase(), protector.getPassphraseFor(1L));
assertEquals(Passphrase.fromPassword("missingP455w0rd"), protector.getPassphraseFor(3L));
}
@Test
public void testCallbackBasedKeyRingProtector() throws IOException, PGPException {
SecretKeyRingProtector2 protector = new CallbackBasedKeyringProtector(new CallbackBasedKeyringProtector.Callback() {
@Override
public Passphrase getPassphraseFor(PGPSecretKey secretKey) {
return TestKeys.CRYPTIE_PASSPHRASE;
}
});
PGPSecretKeyRing secretKeys = TestKeys.getEmilSecretKeyRing();
for (PGPSecretKey secretKey : secretKeys) {
secretKey.extractPrivateKey(protector.getDecryptor(secretKey));
}
}
}

View file

@ -0,0 +1,62 @@
/*
* Copyright 2021 Paul Schaub.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.pgpainless.key.selection.key;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.io.IOException;
import java.util.Iterator;
import org.bouncycastle.openpgp.PGPException;
import org.bouncycastle.openpgp.PGPPublicKey;
import org.bouncycastle.openpgp.PGPSecretKey;
import org.bouncycastle.openpgp.PGPSecretKeyRing;
import org.junit.jupiter.api.Test;
import org.pgpainless.algorithm.KeyFlag;
import org.pgpainless.key.TestKeys;
import org.pgpainless.key.selection.key.impl.EncryptionKeySelectionStrategy;
import org.pgpainless.key.selection.key.impl.HasAnyKeyFlagSelectionStrategy;
import org.pgpainless.key.selection.key.util.Or;
public class AndOrSelectionStrategyTest {
@Test
public void testOr() throws IOException, PGPException {
PGPSecretKeyRing ring = TestKeys.getEmilSecretKeyRing();
Iterator<PGPSecretKey> secretKeys = ring.getSecretKeys();
Or.SecKeySelectionStrategy secStrategy = new Or.SecKeySelectionStrategy(
new HasAnyKeyFlagSelectionStrategy.SecretKey(KeyFlag.ENCRYPT_COMMS),
new HasAnyKeyFlagSelectionStrategy.SecretKey(KeyFlag.ENCRYPT_STORAGE)
);
PGPSecretKey certSecKey = secretKeys.next();
PGPSecretKey cryptSecKey = secretKeys.next();
assertFalse(secStrategy.accept(certSecKey));
assertTrue(secStrategy.accept(cryptSecKey));
Iterator<PGPPublicKey> publicKeys = ring.getPublicKeys();
Or.PubKeySelectionStrategy pubStrategy = new Or.PubKeySelectionStrategy(
new EncryptionKeySelectionStrategy(KeyFlag.ENCRYPT_COMMS),
new EncryptionKeySelectionStrategy(KeyFlag.ENCRYPT_STORAGE)
);
PGPPublicKey certPubKey = publicKeys.next();
PGPPublicKey cryptPubKey = publicKeys.next();
assertFalse(pubStrategy.accept(certPubKey));
assertTrue(pubStrategy.accept(cryptPubKey));
}
}

View file

@ -0,0 +1,54 @@
/*
* Copyright 2021 Paul Schaub.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.pgpainless.key.selection.key;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.io.IOException;
import java.security.InvalidAlgorithmParameterException;
import java.security.NoSuchAlgorithmException;
import java.util.Iterator;
import org.bouncycastle.openpgp.PGPException;
import org.bouncycastle.openpgp.PGPPublicKey;
import org.bouncycastle.openpgp.PGPSecretKeyRing;
import org.junit.jupiter.api.Test;
import org.pgpainless.PGPainless;
import org.pgpainless.key.TestKeys;
import org.pgpainless.key.selection.key.impl.KeyBelongsToKeyRing;
public class KeyBelongsToKeyRingTest {
@Test
public void testStrategyOnlyAcceptsKeysThatBelongToKeyRing() throws InvalidAlgorithmParameterException, NoSuchAlgorithmException, PGPException, IOException {
PGPSecretKeyRing secretKeys = PGPainless.generateKeyRing().simpleEcKeyRing("test@test.test");
Iterator<PGPPublicKey> iterator = secretKeys.getPublicKeys();
PGPPublicKey primaryKey = iterator.next();
PGPPublicKey subKey = iterator.next();
KeyBelongsToKeyRing.PubkeySelectionStrategy strategy = new KeyBelongsToKeyRing.PubkeySelectionStrategy(primaryKey);
assertTrue(strategy.accept(primaryKey));
assertTrue(strategy.accept(subKey));
PGPSecretKeyRing unrelatedKeys = TestKeys.getEmilSecretKeyRing();
Iterator<PGPPublicKey> unrelated = unrelatedKeys.getPublicKeys();
while (unrelated.hasNext()) {
PGPPublicKey unrelatedKey = unrelated.next();
assertFalse(strategy.accept(unrelatedKey));
}
}
}

View file

@ -0,0 +1,99 @@
/*
* Copyright 2021 Paul Schaub.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.pgpainless.key.selection.keyring;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;
import java.util.Set;
import org.bouncycastle.openpgp.PGPException;
import org.bouncycastle.openpgp.PGPPublicKeyRing;
import org.bouncycastle.openpgp.PGPPublicKeyRingCollection;
import org.bouncycastle.openpgp.PGPSecretKeyRing;
import org.bouncycastle.openpgp.PGPSecretKeyRingCollection;
import org.junit.jupiter.api.Test;
import org.pgpainless.key.TestKeys;
import org.pgpainless.key.selection.keyring.impl.ExactUserId;
import org.pgpainless.util.MultiMap;
public class KeyRingsFromCollectionTest {
@Test
public void selectSecretKeyRingFromSecretKeyRingCollectionTest() throws IOException, PGPException {
PGPSecretKeyRing emil = TestKeys.getEmilSecretKeyRing();
PGPSecretKeyRing juliet = TestKeys.getJulietSecretKeyRing();
PGPSecretKeyRingCollection collection = new PGPSecretKeyRingCollection(Arrays.asList(emil, juliet));
SecretKeyRingSelectionStrategy<String> strategy = new ExactUserId.SecRingSelectionStrategy();
Set<PGPSecretKeyRing> secretKeyRings = strategy.selectKeyRingsFromCollection(TestKeys.JULIET_UID, collection);
assertEquals(1, secretKeyRings.size());
assertEquals(juliet.getPublicKey().getKeyID(), secretKeyRings.iterator().next().getPublicKey().getKeyID());
}
@Test
public void selectSecretKeyRingMapFromSecretKeyRingCollectionMapTest() throws IOException, PGPException {
PGPSecretKeyRing emil = TestKeys.getEmilSecretKeyRing();
PGPSecretKeyRing juliet = TestKeys.getJulietSecretKeyRing();
MultiMap<String, PGPSecretKeyRingCollection> map = new MultiMap<>();
PGPSecretKeyRingCollection julietCollection = new PGPSecretKeyRingCollection(Arrays.asList(emil, juliet));
map.put(TestKeys.JULIET_UID, julietCollection);
PGPSecretKeyRingCollection emilCollection = new PGPSecretKeyRingCollection(Collections.singletonList(emil));
map.put(TestKeys.EMIL_UID, emilCollection);
assertEquals(2, julietCollection.size());
map.put("invalidId", emilCollection);
SecretKeyRingSelectionStrategy<String> strategy = new ExactUserId.SecRingSelectionStrategy();
MultiMap<String, PGPSecretKeyRing> selected = strategy.selectKeyRingsFromCollections(map);
assertEquals(1, selected.get(TestKeys.JULIET_UID).size());
assertEquals(1, selected.get(TestKeys.EMIL_UID).size());
assertNull(selected.get("invalidId"));
}
@Test
public void selectPublicKeyRingFromPublicKeyRingCollectionTest() throws IOException, PGPException {
PGPPublicKeyRing emil = TestKeys.getEmilPublicKeyRing();
PGPPublicKeyRing juliet = TestKeys.getJulietPublicKeyRing();
PGPPublicKeyRingCollection collection = new PGPPublicKeyRingCollection(Arrays.asList(emil, juliet));
PublicKeyRingSelectionStrategy<String> strategy = new ExactUserId.PubRingSelectionStrategy();
Set<PGPPublicKeyRing> publicKeyRings = strategy.selectKeyRingsFromCollection(TestKeys.JULIET_UID, collection);
assertEquals(1, publicKeyRings.size());
assertEquals(juliet.getPublicKey().getKeyID(), publicKeyRings.iterator().next().getPublicKey().getKeyID());
}
@Test
public void selectPublicKeyRingMapFromPublicKeyRingCollectionMapTest() throws IOException, PGPException {
PGPPublicKeyRing emil = TestKeys.getEmilPublicKeyRing();
PGPPublicKeyRing juliet = TestKeys.getJulietPublicKeyRing();
MultiMap<String, PGPPublicKeyRingCollection> map = new MultiMap<>();
PGPPublicKeyRingCollection julietCollection = new PGPPublicKeyRingCollection(Arrays.asList(emil, juliet));
map.put(TestKeys.JULIET_UID, julietCollection);
PGPPublicKeyRingCollection emilCollection = new PGPPublicKeyRingCollection(Collections.singletonList(emil));
map.put(TestKeys.EMIL_UID, emilCollection);
assertEquals(2, julietCollection.size());
map.put("invalidId", emilCollection);
PublicKeyRingSelectionStrategy<String> strategy = new ExactUserId.PubRingSelectionStrategy();
MultiMap<String, PGPPublicKeyRing> selected = strategy.selectKeyRingsFromCollections(map);
assertEquals(1, selected.get(TestKeys.JULIET_UID).size());
assertEquals(1, selected.get(TestKeys.EMIL_UID).size());
assertNull(selected.get("invalidId"));
}
}