diff --git a/pgpainless-core/src/main/java/org/pgpainless/util/selection/userid/SelectUserId.java b/pgpainless-core/src/main/java/org/pgpainless/util/selection/userid/SelectUserId.java index 4cf5f76b..50dbb6d9 100644 --- a/pgpainless-core/src/main/java/org/pgpainless/util/selection/userid/SelectUserId.java +++ b/pgpainless-core/src/main/java/org/pgpainless/util/selection/userid/SelectUserId.java @@ -118,4 +118,13 @@ public abstract class SelectUserId { } }; } + + public static SelectUserId not(SelectUserId strategy) { + return new SelectUserId() { + @Override + protected boolean accept(String userId) { + return !strategy.accept(userId); + } + }; + } } diff --git a/pgpainless-core/src/test/java/org/pgpainless/util/selection/userid/SelectUserIdTest.java b/pgpainless-core/src/test/java/org/pgpainless/util/selection/userid/SelectUserIdTest.java new file mode 100644 index 00000000..ce5770ea --- /dev/null +++ b/pgpainless-core/src/test/java/org/pgpainless/util/selection/userid/SelectUserIdTest.java @@ -0,0 +1,124 @@ +/* + * 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.util.selection.userid; + +import org.bouncycastle.openpgp.PGPException; +import org.bouncycastle.openpgp.PGPSecretKeyRing; +import org.junit.jupiter.api.Test; +import org.pgpainless.PGPainless; +import org.pgpainless.key.protection.SecretKeyRingProtector; +import org.pgpainless.key.util.UserId; + +import java.security.InvalidAlgorithmParameterException; +import java.security.NoSuchAlgorithmException; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; + +public class SelectUserIdTest { + + @Test + public void test() throws InvalidAlgorithmParameterException, NoSuchAlgorithmException, PGPException { + PGPSecretKeyRing secretKeys = PGPainless.generateKeyRing() + .simpleEcKeyRing(""); + secretKeys = PGPainless.modifyKeyRing(secretKeys) + .addUserId( + UserId.withName("Alice Liddell").noComment().withEmail("crazy@the-rabbit.hole"), + SecretKeyRingProtector.unprotectedKeys()) + .done(); + + List validEmail = SelectUserId.and( + SelectUserId.validUserId(secretKeys), + SelectUserId.containsEmailAddress("alice@wonderland.lit") + ).selectUserIds(secretKeys); + + assertEquals(Collections.singletonList(""), validEmail); + + List startsWithAlice = SelectUserId.startsWith("Alice").selectUserIds(secretKeys); + assertEquals(Collections.singletonList("Alice Liddell "), startsWithAlice); + + List exactMatch = SelectUserId.or( + SelectUserId.exactMatch(""), + SelectUserId.startsWith("Not Found") + ).selectUserIds(secretKeys); + assertEquals(Collections.singletonList(""), exactMatch); + } + + @Test + public void testContainsSubstring() throws InvalidAlgorithmParameterException, NoSuchAlgorithmException, PGPException { + PGPSecretKeyRing secretKeys = PGPainless.generateKeyRing().simpleEcKeyRing("wine drinker"); + secretKeys = PGPainless.modifyKeyRing(secretKeys) + .addUserId("this is not a quine", SecretKeyRingProtector.unprotectedKeys()) + .addUserId("this is not a crime", SecretKeyRingProtector.unprotectedKeys()) + .done(); + + List containSubstring = SelectUserId.containsSubstring("ine") + .selectUserIds(secretKeys); + assertEquals(Arrays.asList("wine drinker", "this is not a quine"), containSubstring); + } + + @Test + public void testContainsEmailAddress() throws InvalidAlgorithmParameterException, NoSuchAlgorithmException, PGPException { + PGPSecretKeyRing secretKeys = PGPainless.generateKeyRing().simpleEcKeyRing("Alice "); + + assertEquals("Alice ", SelectUserId.containsEmailAddress("alice@wonderland.lit").firstMatch(secretKeys)); + assertEquals("Alice ", SelectUserId.containsEmailAddress("").firstMatch(secretKeys)); + + assertNull(SelectUserId.containsEmailAddress("mad@hatter.lit").firstMatch(secretKeys)); + } + + @Test + public void testAndOrNot() throws InvalidAlgorithmParameterException, NoSuchAlgorithmException, PGPException { + PGPSecretKeyRing secretKeys = PGPainless.generateKeyRing().simpleEcKeyRing("Alice "); + secretKeys = PGPainless.modifyKeyRing(secretKeys) + .addUserId("Alice ", SecretKeyRingProtector.unprotectedKeys()) + .addUserId("", SecretKeyRingProtector.unprotectedKeys()) + .addUserId("Crazy Girl ", SecretKeyRingProtector.unprotectedKeys()) + .done(); + + List or = SelectUserId.or( + SelectUserId.containsEmailAddress("alice@wonderland.lit"), + SelectUserId.startsWith("Alice")) + .selectUserIds(secretKeys); + assertEquals(Arrays.asList("Alice ", "Alice ", "Crazy Girl "), or); + + List and = SelectUserId.and( + SelectUserId.containsEmailAddress("alice@wonderland.lit"), + SelectUserId.startsWith("Alice")) + .selectUserIds(secretKeys); + assertEquals(Collections.singletonList("Alice "), and); + + List not = SelectUserId.not( + SelectUserId.startsWith("Alice")) + .selectUserIds(secretKeys); + assertEquals(Arrays.asList("", "Crazy Girl "), not); + } + + @Test + public void testFirstMatch() throws InvalidAlgorithmParameterException, NoSuchAlgorithmException, PGPException { + PGPSecretKeyRing secretKeys = PGPainless.generateKeyRing().simpleEcKeyRing("First UserID"); + secretKeys = PGPainless.modifyKeyRing(secretKeys) + .addUserId("Second UserID", SecretKeyRingProtector.unprotectedKeys()) + .done(); + assertEquals("First UserID", SelectUserId.validUserId(secretKeys).firstMatch(secretKeys)); + assertEquals("Second UserID", SelectUserId.containsSubstring("Second").firstMatch( + PGPainless.inspectKeyRing(secretKeys).getUserIds() + )); + } +}