mirror of
https://github.com/pgpainless/pgpainless.git
synced 2025-09-09 18:29:39 +02:00
Remove unused KeyRingSelectionStrategy implementations
This commit is contained in:
parent
6a9fb3f6df
commit
970e974556
14 changed files with 0 additions and 666 deletions
|
@ -1,8 +0,0 @@
|
|||
// SPDX-FileCopyrightText: 2018 Paul Schaub <vanitasvitae@fsfe.org>
|
||||
//
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
/**
|
||||
* Utility classes.
|
||||
*/
|
||||
package org.pgpainless.util;
|
|
@ -1,49 +0,0 @@
|
|||
// SPDX-FileCopyrightText: 2018 Paul Schaub <vanitasvitae@fsfe.org>
|
||||
//
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package org.pgpainless.util.selection.keyring;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
import org.pgpainless.util.MultiMap;
|
||||
|
||||
/**
|
||||
* Filter for selecting public / secret key rings based on identifiers (e.g. user-ids).
|
||||
*
|
||||
* @param <R> Type of {@link org.bouncycastle.openpgp.PGPKeyRing} ({@link org.bouncycastle.openpgp.PGPSecretKeyRing}
|
||||
* or {@link org.bouncycastle.openpgp.PGPPublicKeyRing}).
|
||||
* @param <C> Type of key ring collection (e.g. {@link org.bouncycastle.openpgp.PGPSecretKeyRingCollection}
|
||||
* or {@link org.bouncycastle.openpgp.PGPPublicKeyRingCollection}).
|
||||
* @param <O> Type of key identifier
|
||||
*/
|
||||
public interface KeyRingSelectionStrategy<R, C, O> {
|
||||
|
||||
/**
|
||||
* Return true, if the filter accepts the given <pre>keyRing</pre> based on the given <pre>identifier</pre>.
|
||||
*
|
||||
* @param identifier identifier
|
||||
* @param keyRing key ring
|
||||
* @return acceptance
|
||||
*/
|
||||
boolean accept(O identifier, R keyRing);
|
||||
|
||||
/**
|
||||
* Iterate of the given <pre>keyRingCollection</pre> and return a {@link Set} of all acceptable
|
||||
* keyRings in the collection, based on the given <pre>identifier</pre>.
|
||||
*
|
||||
* @param identifier identifier
|
||||
* @param keyRingCollection collection
|
||||
* @return set of acceptable key rings
|
||||
*/
|
||||
Set<R> selectKeyRingsFromCollection(O identifier, C keyRingCollection);
|
||||
|
||||
/**
|
||||
* Iterate over all keyRings in the given {@link MultiMap} of keyRingCollections and return a new {@link MultiMap}
|
||||
* which for every identifier (key of the map) contains all acceptable keyRings based on that identifier.
|
||||
*
|
||||
* @param keyRingCollections MultiMap of identifiers and keyRingCollections.
|
||||
* @return MultiMap of identifiers and acceptable keyRings.
|
||||
*/
|
||||
MultiMap<O, R> selectKeyRingsFromCollections(MultiMap<O, C> keyRingCollections);
|
||||
}
|
|
@ -1,44 +0,0 @@
|
|||
// SPDX-FileCopyrightText: 2018 Paul Schaub <vanitasvitae@fsfe.org>
|
||||
//
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package org.pgpainless.util.selection.keyring;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import java.util.HashSet;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import org.bouncycastle.openpgp.PGPPublicKeyRing;
|
||||
import org.bouncycastle.openpgp.PGPPublicKeyRingCollection;
|
||||
import org.pgpainless.util.MultiMap;
|
||||
|
||||
/**
|
||||
* Abstract {@link KeyRingSelectionStrategy} for {@link PGPPublicKeyRing PGPPublicKeyRings}.
|
||||
*
|
||||
* @param <O> Type of identifier
|
||||
*/
|
||||
public abstract class PublicKeyRingSelectionStrategy<O> implements KeyRingSelectionStrategy<PGPPublicKeyRing, PGPPublicKeyRingCollection, O> {
|
||||
|
||||
@Override
|
||||
public Set<PGPPublicKeyRing> selectKeyRingsFromCollection(@Nonnull O identifier, @Nonnull PGPPublicKeyRingCollection keyRingCollection) {
|
||||
Set<PGPPublicKeyRing> accepted = new HashSet<>();
|
||||
for (Iterator<PGPPublicKeyRing> i = keyRingCollection.getKeyRings(); i.hasNext(); ) {
|
||||
PGPPublicKeyRing ring = i.next();
|
||||
if (accept(identifier, ring)) accepted.add(ring);
|
||||
}
|
||||
return accepted;
|
||||
}
|
||||
|
||||
@Override
|
||||
public MultiMap<O, PGPPublicKeyRing> selectKeyRingsFromCollections(@Nonnull MultiMap<O, PGPPublicKeyRingCollection> keyRingCollections) {
|
||||
MultiMap<O, PGPPublicKeyRing> keyRings = new MultiMap<>();
|
||||
for (Map.Entry<O, Set<PGPPublicKeyRingCollection>> entry : keyRingCollections.entrySet()) {
|
||||
for (PGPPublicKeyRingCollection collection : entry.getValue()) {
|
||||
keyRings.plus(entry.getKey(), selectKeyRingsFromCollection(entry.getKey(), collection));
|
||||
}
|
||||
}
|
||||
return keyRings;
|
||||
}
|
||||
}
|
|
@ -1,43 +0,0 @@
|
|||
// SPDX-FileCopyrightText: 2018 Paul Schaub <vanitasvitae@fsfe.org>
|
||||
//
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package org.pgpainless.util.selection.keyring;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import javax.annotation.Nonnull;
|
||||
|
||||
import org.bouncycastle.openpgp.PGPSecretKeyRing;
|
||||
import org.bouncycastle.openpgp.PGPSecretKeyRingCollection;
|
||||
import org.pgpainless.util.MultiMap;
|
||||
|
||||
/**
|
||||
* Abstract {@link KeyRingSelectionStrategy} for {@link PGPSecretKeyRing PGPSecretKeyRings}.
|
||||
*
|
||||
* @param <O> Type of identifier
|
||||
*/
|
||||
public abstract class SecretKeyRingSelectionStrategy<O> implements KeyRingSelectionStrategy<PGPSecretKeyRing, PGPSecretKeyRingCollection, O> {
|
||||
@Override
|
||||
public Set<PGPSecretKeyRing> selectKeyRingsFromCollection(O identifier, @Nonnull PGPSecretKeyRingCollection keyRingCollection) {
|
||||
Set<PGPSecretKeyRing> accepted = new HashSet<>();
|
||||
for (Iterator<PGPSecretKeyRing> i = keyRingCollection.getKeyRings(); i.hasNext(); ) {
|
||||
PGPSecretKeyRing ring = i.next();
|
||||
if (accept(identifier, ring)) accepted.add(ring);
|
||||
}
|
||||
return accepted;
|
||||
}
|
||||
|
||||
@Override
|
||||
public MultiMap<O, PGPSecretKeyRing> selectKeyRingsFromCollections(@Nonnull MultiMap<O, PGPSecretKeyRingCollection> keyRingCollections) {
|
||||
MultiMap<O, PGPSecretKeyRing> keyRings = new MultiMap<>();
|
||||
for (Map.Entry<O, Set<PGPSecretKeyRingCollection>> entry : keyRingCollections.entrySet()) {
|
||||
for (PGPSecretKeyRingCollection collection : entry.getValue()) {
|
||||
keyRings.plus(entry.getKey(), selectKeyRingsFromCollection(entry.getKey(), collection));
|
||||
}
|
||||
}
|
||||
return keyRings;
|
||||
}
|
||||
}
|
|
@ -1,56 +0,0 @@
|
|||
// SPDX-FileCopyrightText: 2018 Paul Schaub <vanitasvitae@fsfe.org>
|
||||
//
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package org.pgpainless.util.selection.keyring.impl;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.bouncycastle.openpgp.PGPPublicKeyRing;
|
||||
import org.bouncycastle.openpgp.PGPSecretKeyRing;
|
||||
import org.pgpainless.key.util.KeyRingUtils;
|
||||
import org.pgpainless.util.selection.keyring.PublicKeyRingSelectionStrategy;
|
||||
import org.pgpainless.util.selection.keyring.SecretKeyRingSelectionStrategy;
|
||||
|
||||
/**
|
||||
* Implementations of {@link org.pgpainless.util.selection.keyring.KeyRingSelectionStrategy} which select key rings
|
||||
* based on the exact user-id.
|
||||
*/
|
||||
public final class ExactUserId {
|
||||
|
||||
private ExactUserId() {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* {@link PublicKeyRingSelectionStrategy} which accepts {@link PGPPublicKeyRing PGPPublicKeyRings} if those
|
||||
* have a user-id which exactly matches the given <pre>identifier</pre>.
|
||||
*/
|
||||
public static class PubRingSelectionStrategy extends PublicKeyRingSelectionStrategy<String> {
|
||||
|
||||
@Override
|
||||
public boolean accept(String identifier, PGPPublicKeyRing keyRing) {
|
||||
List<String> userIds = KeyRingUtils.getUserIdsIgnoringInvalidUTF8(keyRing.getPublicKey());
|
||||
for (String userId : userIds) {
|
||||
if (userId.equals(identifier)) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* {@link SecretKeyRingSelectionStrategy} which accepts {@link PGPSecretKeyRing PGPSecretKeyRings} if those
|
||||
* have a user-id which exactly matches the given <pre>identifier</pre>.
|
||||
*/
|
||||
public static class SecRingSelectionStrategy extends SecretKeyRingSelectionStrategy<String> {
|
||||
|
||||
@Override
|
||||
public boolean accept(String identifier, PGPSecretKeyRing keyRing) {
|
||||
List<String> userIds = KeyRingUtils.getUserIdsIgnoringInvalidUTF8(keyRing.getPublicKey());
|
||||
for (String userId : userIds) {
|
||||
if (userId.equals(identifier)) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,92 +0,0 @@
|
|||
// SPDX-FileCopyrightText: 2018 Paul Schaub <vanitasvitae@fsfe.org>
|
||||
//
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package org.pgpainless.util.selection.keyring.impl;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import org.bouncycastle.openpgp.PGPPublicKeyRing;
|
||||
import org.bouncycastle.openpgp.PGPSecretKeyRing;
|
||||
import org.pgpainless.util.selection.keyring.PublicKeyRingSelectionStrategy;
|
||||
import org.pgpainless.util.selection.keyring.SecretKeyRingSelectionStrategy;
|
||||
import org.pgpainless.util.MultiMap;
|
||||
|
||||
/**
|
||||
* Implementations of {@link org.pgpainless.util.selection.keyring.KeyRingSelectionStrategy} which accept PGP KeyRings
|
||||
* based on a whitelist of acceptable keyIds.
|
||||
*/
|
||||
public final class Whitelist {
|
||||
|
||||
private Whitelist() {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* {@link org.pgpainless.util.selection.keyring.KeyRingSelectionStrategy} which accepts
|
||||
* {@link PGPPublicKeyRing PGPPublicKeyRings} if the <pre>whitelist</pre> contains their primary key id.
|
||||
*
|
||||
* If the whitelist contains 123L for "alice@pgpainless.org", the key with primary key id 123L is
|
||||
* acceptable for "alice@pgpainless.org".
|
||||
*
|
||||
* @param <O> Type of identifier for {@link org.bouncycastle.openpgp.PGPPublicKeyRingCollection PGPPublicKeyRingCollections}.
|
||||
*/
|
||||
public static class PubRingSelectionStrategy<O> extends PublicKeyRingSelectionStrategy<O> {
|
||||
|
||||
private final MultiMap<O, Long> whitelist;
|
||||
|
||||
public PubRingSelectionStrategy(MultiMap<O, Long> whitelist) {
|
||||
this.whitelist = whitelist;
|
||||
}
|
||||
|
||||
public PubRingSelectionStrategy(Map<O, Set<Long>> whitelist) {
|
||||
this(new MultiMap<>(whitelist));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean accept(O identifier, PGPPublicKeyRing keyRing) {
|
||||
Set<Long> whitelistedKeyIds = whitelist.get(identifier);
|
||||
|
||||
if (whitelistedKeyIds == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return whitelistedKeyIds.contains(keyRing.getPublicKey().getKeyID());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* {@link org.pgpainless.util.selection.keyring.KeyRingSelectionStrategy} which accepts
|
||||
* {@link PGPSecretKeyRing PGPSecretKeyRings} if the <pre>whitelist</pre> contains their primary key id.
|
||||
*
|
||||
* If the whitelist contains 123L for "alice@pgpainless.org", the key with primary key id 123L is
|
||||
* acceptable for "alice@pgpainless.org".
|
||||
*
|
||||
* @param <O> Type of identifier for {@link org.bouncycastle.openpgp.PGPSecretKeyRingCollection PGPSecretKeyRingCollections}.
|
||||
*/
|
||||
public static class SecRingSelectionStrategy<O> extends SecretKeyRingSelectionStrategy<O> {
|
||||
|
||||
private final MultiMap<O, Long> whitelist;
|
||||
|
||||
public SecRingSelectionStrategy(MultiMap<O, Long> whitelist) {
|
||||
this.whitelist = whitelist;
|
||||
}
|
||||
|
||||
public SecRingSelectionStrategy(Map<O, Set<Long>> whitelist) {
|
||||
this(new MultiMap<>(whitelist));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean accept(O identifier, PGPSecretKeyRing keyRing) {
|
||||
Set<Long> whitelistedKeyIds = whitelist.get(identifier);
|
||||
|
||||
if (whitelistedKeyIds == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return whitelistedKeyIds.contains(keyRing.getPublicKey().getKeyID());
|
||||
}
|
||||
|
||||
}
|
||||
}
|
|
@ -1,36 +0,0 @@
|
|||
// SPDX-FileCopyrightText: 2018 Paul Schaub <vanitasvitae@fsfe.org>
|
||||
//
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package org.pgpainless.util.selection.keyring.impl;
|
||||
|
||||
import org.bouncycastle.openpgp.PGPPublicKeyRing;
|
||||
import org.bouncycastle.openpgp.PGPSecretKeyRing;
|
||||
import org.pgpainless.util.selection.keyring.PublicKeyRingSelectionStrategy;
|
||||
import org.pgpainless.util.selection.keyring.SecretKeyRingSelectionStrategy;
|
||||
|
||||
/**
|
||||
* Implementations of {@link org.pgpainless.util.selection.keyring.KeyRingSelectionStrategy} which accept all keyRings.
|
||||
*/
|
||||
public final class Wildcard {
|
||||
|
||||
private Wildcard() {
|
||||
|
||||
}
|
||||
|
||||
public static class PubRingSelectionStrategy<O> extends PublicKeyRingSelectionStrategy<O> {
|
||||
|
||||
@Override
|
||||
public boolean accept(O identifier, PGPPublicKeyRing keyRing) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
public static class SecRingSelectionStrategy<O> extends SecretKeyRingSelectionStrategy<O> {
|
||||
|
||||
@Override
|
||||
public boolean accept(O identifier, PGPSecretKeyRing keyRing) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,53 +0,0 @@
|
|||
// SPDX-FileCopyrightText: 2018 Paul Schaub <vanitasvitae@fsfe.org>
|
||||
//
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package org.pgpainless.util.selection.keyring.impl;
|
||||
|
||||
import org.bouncycastle.openpgp.PGPPublicKeyRing;
|
||||
import org.bouncycastle.openpgp.PGPSecretKeyRing;
|
||||
|
||||
/**
|
||||
* Implementations of {@link org.pgpainless.util.selection.keyring.KeyRingSelectionStrategy} which accept KeyRings
|
||||
* containing a given XMPP address of the format "xmpp:alice@pgpainless.org".
|
||||
*/
|
||||
public final class XMPP {
|
||||
|
||||
private XMPP() {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* {@link org.pgpainless.util.selection.keyring.PublicKeyRingSelectionStrategy} which accepts a given
|
||||
* {@link PGPPublicKeyRing} if its primary key has a user-id that matches the given <pre>jid</pre>.
|
||||
*
|
||||
* The argument <pre>jid</pre> can either contain the prefix "xmpp:", or not, the result will be the same.
|
||||
*/
|
||||
public static class PubRingSelectionStrategy extends ExactUserId.PubRingSelectionStrategy {
|
||||
|
||||
@Override
|
||||
public boolean accept(String jid, PGPPublicKeyRing keyRing) {
|
||||
if (!jid.matches("^xmpp:.+$")) {
|
||||
jid = "xmpp:" + jid;
|
||||
}
|
||||
return super.accept(jid, keyRing);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* {@link org.pgpainless.util.selection.keyring.SecretKeyRingSelectionStrategy} which accepts a given
|
||||
* {@link PGPSecretKeyRing} if its primary key has a user-id that matches the given <pre>jid</pre>.
|
||||
*
|
||||
* The argument <pre>jid</pre> can either contain the prefix "xmpp:", or not, the result will be the same.
|
||||
*/
|
||||
public static class SecRingSelectionStrategy extends ExactUserId.SecRingSelectionStrategy {
|
||||
|
||||
@Override
|
||||
public boolean accept(String jid, PGPSecretKeyRing keyRing) {
|
||||
if (!jid.matches("^xmpp:.+$")) {
|
||||
jid = "xmpp:" + jid;
|
||||
}
|
||||
return super.accept(jid, keyRing);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,8 +0,0 @@
|
|||
// SPDX-FileCopyrightText: 2018 Paul Schaub <vanitasvitae@fsfe.org>
|
||||
//
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
/**
|
||||
* Implementations of Key Ring Selection Strategies.
|
||||
*/
|
||||
package org.pgpainless.util.selection.keyring.impl;
|
|
@ -1,8 +0,0 @@
|
|||
// SPDX-FileCopyrightText: 2018 Paul Schaub <vanitasvitae@fsfe.org>
|
||||
//
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
/**
|
||||
* Different Key Ring Selection Strategies.
|
||||
*/
|
||||
package org.pgpainless.util.selection.keyring;
|
|
@ -1,88 +0,0 @@
|
|||
// SPDX-FileCopyrightText: 2021 Paul Schaub <vanitasvitae@fsfe.org>
|
||||
//
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package org.pgpainless.util.selection.keyring;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
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.util.MultiMap;
|
||||
import org.pgpainless.util.selection.keyring.impl.ExactUserId;
|
||||
|
||||
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());
|
||||
assertTrue(selected.get("invalidId").isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void selectPublicKeyRingFromPublicKeyRingCollectionTest() throws IOException {
|
||||
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 {
|
||||
PGPPublicKeyRing emil = TestKeys.getEmilPublicKeyRing();
|
||||
PGPPublicKeyRing juliet = TestKeys.getJulietPublicKeyRing();
|
||||
MultiMap<String, PGPPublicKeyRingCollection> map = new MultiMap<>();
|
||||
PGPPublicKeyRingCollection julietCollection = new PGPPublicKeyRingCollection(Arrays.asList(emil, juliet));
|
||||
map.plus(TestKeys.JULIET_UID, julietCollection);
|
||||
PGPPublicKeyRingCollection emilCollection = new PGPPublicKeyRingCollection(Collections.singletonList(emil));
|
||||
map.plus(TestKeys.EMIL_UID, emilCollection);
|
||||
assertEquals(2, julietCollection.size());
|
||||
map.plus("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());
|
||||
assertTrue(selected.get("invalidId").isEmpty());
|
||||
}
|
||||
}
|
|
@ -1,52 +0,0 @@
|
|||
// SPDX-FileCopyrightText: 2020 Paul Schaub <vanitasvitae@fsfe.org>
|
||||
//
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package org.pgpainless.util.selection.keyring;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Collections;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
import org.bouncycastle.openpgp.PGPException;
|
||||
import org.bouncycastle.openpgp.PGPPublicKeyRing;
|
||||
import org.bouncycastle.openpgp.PGPSecretKeyRing;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.pgpainless.key.TestKeys;
|
||||
import org.pgpainless.util.selection.keyring.impl.Whitelist;
|
||||
|
||||
public class WhitelistKeyRingSelectionStrategyTest {
|
||||
|
||||
@Test
|
||||
public void testWithPublicKeys() throws IOException {
|
||||
Map<String, Set<Long>> ids = new ConcurrentHashMap<>();
|
||||
ids.put(TestKeys.JULIET_UID, Collections.singleton(TestKeys.JULIET_KEY_ID));
|
||||
Whitelist.PubRingSelectionStrategy<String> selectionStrategy = new Whitelist.PubRingSelectionStrategy<>(ids);
|
||||
|
||||
PGPPublicKeyRing julietsKeys = TestKeys.getJulietPublicKeyRing();
|
||||
PGPPublicKeyRing romeosKeys = TestKeys.getRomeoPublicKeyRing();
|
||||
|
||||
assertTrue(selectionStrategy.accept(TestKeys.JULIET_UID, julietsKeys));
|
||||
assertFalse(selectionStrategy.accept(TestKeys.JULIET_UID, romeosKeys));
|
||||
assertFalse(selectionStrategy.accept(TestKeys.ROMEO_UID, julietsKeys));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWithSecretKeys() throws IOException, PGPException {
|
||||
Map<String, Set<Long>> ids = new ConcurrentHashMap<>();
|
||||
ids.put(TestKeys.JULIET_UID, Collections.singleton(TestKeys.JULIET_KEY_ID));
|
||||
Whitelist.SecRingSelectionStrategy<String> selectionStrategy = new Whitelist.SecRingSelectionStrategy<>(ids);
|
||||
|
||||
PGPSecretKeyRing julietsKeys = TestKeys.getJulietSecretKeyRing();
|
||||
PGPSecretKeyRing romeosKeys = TestKeys.getRomeoSecretKeyRing();
|
||||
|
||||
assertTrue(selectionStrategy.accept(TestKeys.JULIET_UID, julietsKeys));
|
||||
assertFalse(selectionStrategy.accept(TestKeys.JULIET_UID, romeosKeys));
|
||||
assertFalse(selectionStrategy.accept(TestKeys.ROMEO_UID, julietsKeys));
|
||||
}
|
||||
}
|
|
@ -1,57 +0,0 @@
|
|||
// SPDX-FileCopyrightText: 2020 Paul Schaub <vanitasvitae@fsfe.org>
|
||||
//
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package org.pgpainless.util.selection.keyring;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import org.bouncycastle.openpgp.PGPException;
|
||||
import org.bouncycastle.openpgp.PGPPublicKeyRing;
|
||||
import org.bouncycastle.openpgp.PGPSecretKeyRing;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.pgpainless.key.TestKeys;
|
||||
import org.pgpainless.util.selection.keyring.impl.Wildcard;
|
||||
|
||||
public class WildcardKeyRingSelectionStrategyTest {
|
||||
|
||||
|
||||
private static final Wildcard.PubRingSelectionStrategy<String> pubKeySelectionStrategy
|
||||
= new Wildcard.PubRingSelectionStrategy<>();
|
||||
private static final Wildcard.SecRingSelectionStrategy<String> secKeySelectionStrategy
|
||||
= new Wildcard.SecRingSelectionStrategy<>();
|
||||
|
||||
@Test
|
||||
public void testStratAcceptsMatchingUIDsOnPubKey() throws IOException {
|
||||
String uid = TestKeys.EMIL_UID;
|
||||
PGPPublicKeyRing key = TestKeys.getEmilPublicKeyRing();
|
||||
|
||||
assertTrue(pubKeySelectionStrategy.accept(uid, key));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStratAcceptsMismatchingUIDsOnPubKey() throws IOException {
|
||||
String uid = "blabla@bla.bla";
|
||||
PGPPublicKeyRing key = TestKeys.getEmilPublicKeyRing();
|
||||
|
||||
assertTrue(pubKeySelectionStrategy.accept(uid, key));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStratAcceptsMatchingUIDsOnSecKey() throws IOException, PGPException {
|
||||
String uid = TestKeys.EMIL_UID;
|
||||
PGPSecretKeyRing key = TestKeys.getEmilSecretKeyRing();
|
||||
|
||||
assertTrue(secKeySelectionStrategy.accept(uid, key));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStratAcceptsMismatchingUIDsOnSecKey() throws IOException, PGPException {
|
||||
String uid = "blabla@bla.bla";
|
||||
PGPSecretKeyRing key = TestKeys.getEmilSecretKeyRing();
|
||||
|
||||
assertTrue(secKeySelectionStrategy.accept(uid, key));
|
||||
}
|
||||
}
|
|
@ -1,72 +0,0 @@
|
|||
// SPDX-FileCopyrightText: 2020 Paul Schaub <vanitasvitae@fsfe.org>
|
||||
//
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package org.pgpainless.util.selection.keyring;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import org.bouncycastle.openpgp.PGPException;
|
||||
import org.bouncycastle.openpgp.PGPPublicKeyRing;
|
||||
import org.bouncycastle.openpgp.PGPSecretKeyRing;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.pgpainless.key.TestKeys;
|
||||
import org.pgpainless.util.selection.keyring.impl.XMPP;
|
||||
|
||||
public class XmppKeyRingSelectionStrategyTest {
|
||||
|
||||
private static final XMPP.PubRingSelectionStrategy pubKeySelectionStrategy =
|
||||
new XMPP.PubRingSelectionStrategy();
|
||||
private static final XMPP.SecRingSelectionStrategy secKeySelectionStrategy =
|
||||
new XMPP.SecRingSelectionStrategy();
|
||||
|
||||
@Test
|
||||
public void testMatchingXmppUIDAcceptedOnPubKey() throws IOException {
|
||||
String uid = "xmpp:juliet@capulet.lit";
|
||||
PGPPublicKeyRing key = TestKeys.getJulietPublicKeyRing();
|
||||
|
||||
assertTrue(pubKeySelectionStrategy.accept(uid, key));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAddressIsFormattedToMatchOnPubKey() throws IOException {
|
||||
String uid = "juliet@capulet.lit";
|
||||
PGPPublicKeyRing key = TestKeys.getJulietPublicKeyRing();
|
||||
|
||||
assertTrue(pubKeySelectionStrategy.accept(uid, key));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPubKeyWithDifferentUIDIsRejected() throws IOException {
|
||||
String wrongUid = "romeo@montague.lit";
|
||||
PGPPublicKeyRing key = TestKeys.getJulietPublicKeyRing();
|
||||
assertFalse(pubKeySelectionStrategy.accept(wrongUid, key));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMatchingEmailUIDAcceptedOnSecKey() throws IOException, PGPException {
|
||||
String uid = "xmpp:juliet@capulet.lit";
|
||||
PGPSecretKeyRing key = TestKeys.getJulietSecretKeyRing();
|
||||
|
||||
assertTrue(secKeySelectionStrategy.accept(uid, key));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAddressIsFormattedToMatchOnSecKey() throws IOException, PGPException {
|
||||
String uid = "juliet@capulet.lit";
|
||||
PGPSecretKeyRing key = TestKeys.getJulietSecretKeyRing();
|
||||
|
||||
assertTrue(secKeySelectionStrategy.accept(uid, key));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSecKeyWithDifferentUIDIsRejected() throws IOException, PGPException {
|
||||
String wrongUid = "romeo@montague.lit";
|
||||
|
||||
PGPSecretKeyRing key = TestKeys.getJulietSecretKeyRing();
|
||||
assertFalse(secKeySelectionStrategy.accept(wrongUid, key));
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue