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

Port PGPPublicKeyRingTest

This commit is contained in:
Paul Schaub 2025-04-01 14:39:57 +02:00
parent 0450e0cb81
commit c1c54be259
Signed by: vanitasvitae
GPG key ID: 62BEE9264BF17311

View file

@ -4,7 +4,6 @@
package org.bouncycastle;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
@ -12,15 +11,16 @@ import java.util.Iterator;
import java.util.List;
import org.bouncycastle.openpgp.PGPPublicKey;
import org.bouncycastle.openpgp.PGPPublicKeyRing;
import org.bouncycastle.openpgp.PGPSecretKeyRing;
import org.bouncycastle.openpgp.api.OpenPGPCertificate;
import org.bouncycastle.openpgp.api.OpenPGPKey;
import org.junit.jupiter.api.Test;
import org.pgpainless.PGPainless;
import org.pgpainless.key.util.KeyRingUtils;
import org.pgpainless.util.CollectionUtils;
public class PGPPublicKeyRingTest {
private final PGPainless api = PGPainless.getInstance();
/**
* Learning test to see if BC also makes userids available on subkeys.
* It does not.
@ -29,15 +29,10 @@ public class PGPPublicKeyRingTest {
*/
@Test
public void subkeysDoNotHaveUserIDsTest() {
PGPSecretKeyRing secretKeys = PGPainless.generateKeyRing().simpleEcKeyRing("primary@user.id")
.getPGPSecretKeyRing();
PGPPublicKeyRing publicKeys = KeyRingUtils.publicKeyRingFrom(secretKeys);
PGPPublicKey primaryKey = publicKeys.getPublicKey();
for (PGPPublicKey subkey : publicKeys) {
Iterator<String> userIds = subkey.getUserIDs();
if (primaryKey == subkey) {
assertEquals("primary@user.id", userIds.next());
}
OpenPGPKey key = api.generateKey().simpleEcKeyRing("primary@user.id");
OpenPGPCertificate certificate = key.toCertificate();
for (OpenPGPCertificate.OpenPGPComponentKey subkey : certificate.getSubkeys().values()) {
Iterator<String> userIds = subkey.getPGPPublicKey().getUserIDs();
assertFalse(userIds.hasNext());
}
}
@ -45,14 +40,13 @@ public class PGPPublicKeyRingTest {
@Test
public void removeUserIdTest() {
String userId = "alice@wonderland.lit";
PGPSecretKeyRing secretKeyRing = PGPainless.generateKeyRing().simpleEcKeyRing(userId)
.getPGPSecretKeyRing();
PGPPublicKeyRing publicKeys = KeyRingUtils.publicKeyRingFrom(secretKeyRing);
OpenPGPKey key = api.generateKey().simpleEcKeyRing(userId);
OpenPGPCertificate certificate = key.toCertificate();
PGPPublicKey publicKey = certificate.getPrimaryKey().getPGPPublicKey();
List<String> userIds = CollectionUtils.iteratorToList(publicKeys.getPublicKey().getUserIDs());
List<String> userIds = CollectionUtils.iteratorToList(publicKey.getUserIDs());
assertTrue(userIds.contains(userId));
PGPPublicKey publicKey = publicKeys.getPublicKey();
publicKey = PGPPublicKey.removeCertification(publicKey, userId);
userIds = CollectionUtils.iteratorToList(publicKey.getUserIDs());