1
0
Fork 0
mirror of https://github.com/pgpainless/pgpainless.git synced 2024-11-17 18:02:05 +01:00

Add tests for KeyRingUtils.deleteUserIdFrom*KeyRing methods

This commit is contained in:
Paul Schaub 2021-11-12 16:49:28 +01:00
parent 0f77d81bd1
commit d036cf2593
2 changed files with 55 additions and 2 deletions

View file

@ -167,7 +167,7 @@ public final class KeyRingUtils {
* @return modified secret keys
*/
@Deprecated
public PGPSecretKeyRing deleteUserIdFromSecretKeyRing(PGPSecretKeyRing secretKeys, String userId) {
public static PGPSecretKeyRing deleteUserIdFromSecretKeyRing(PGPSecretKeyRing secretKeys, String userId) {
PGPSecretKey secretKey = secretKeys.getSecretKey(); // user-ids are located on primary key only
PGPPublicKey publicKey = secretKey.getPublicKey(); // user-ids are placed on the public key part
publicKey = PGPPublicKey.removeCertification(publicKey, userId);
@ -191,7 +191,7 @@ public final class KeyRingUtils {
* @return modified secret keys
*/
@Deprecated
public PGPPublicKeyRing deleteUserIdFromPublicKeyRing(PGPPublicKeyRing publicKeys, String userId) {
public static PGPPublicKeyRing deleteUserIdFromPublicKeyRing(PGPPublicKeyRing publicKeys, String userId) {
PGPPublicKey publicKey = publicKeys.getPublicKey(); // user-ids are located on primary key only
publicKey = PGPPublicKey.removeCertification(publicKey, userId);
if (publicKey == null) {

View file

@ -0,0 +1,53 @@
// SPDX-FileCopyrightText: 2021 Paul Schaub <vanitasvitae@fsfe.org>
//
// SPDX-License-Identifier: Apache-2.0
package org.pgpainless.key.util;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.security.InvalidAlgorithmParameterException;
import java.security.NoSuchAlgorithmException;
import org.bouncycastle.openpgp.PGPException;
import org.bouncycastle.openpgp.PGPPublicKeyRing;
import org.bouncycastle.openpgp.PGPSecretKeyRing;
import org.junit.jupiter.api.Test;
import org.pgpainless.PGPainless;
import org.pgpainless.key.protection.SecretKeyRingProtector;
import org.pgpainless.util.CollectionUtils;
public class KeyRingUtilTest {
@Test
public void testDeleteUserIdFromSecretKeyRing()
throws PGPException, InvalidAlgorithmParameterException, NoSuchAlgorithmException {
PGPSecretKeyRing secretKeys = PGPainless.generateKeyRing()
.modernKeyRing("Alice", null);
secretKeys = PGPainless.modifyKeyRing(secretKeys)
.addUserId("Bob", SecretKeyRingProtector.unprotectedKeys())
.done();
assertEquals(2, CollectionUtils.iteratorToList(secretKeys.getPublicKey().getUserIDs()).size());
secretKeys = KeyRingUtils.deleteUserIdFromSecretKeyRing(secretKeys, "Bob");
assertEquals(1, CollectionUtils.iteratorToList(secretKeys.getPublicKey().getUserIDs()).size());
}
@Test
public void testDeleteUserIdFromPublicKeyRing() throws PGPException, InvalidAlgorithmParameterException, NoSuchAlgorithmException {
PGPSecretKeyRing secretKeys = PGPainless.generateKeyRing()
.modernKeyRing("Alice", null);
secretKeys = PGPainless.modifyKeyRing(secretKeys)
.addUserId("Bob", SecretKeyRingProtector.unprotectedKeys())
.done();
PGPPublicKeyRing publicKeys = PGPainless.extractCertificate(secretKeys);
assertEquals(2, CollectionUtils.iteratorToList(publicKeys.getPublicKey().getUserIDs()).size());
publicKeys = KeyRingUtils.deleteUserIdFromPublicKeyRing(publicKeys, "Alice");
assertEquals(1, CollectionUtils.iteratorToList(publicKeys.getPublicKey().getUserIDs()).size());
}
}