mirror of
https://github.com/pgpainless/pgpainless.git
synced 2024-11-15 00:42:06 +01:00
Implement RegexSet to validate a single user-id against multiple wildcards
This commit is contained in:
parent
15142b5775
commit
f62a6a30ff
2 changed files with 91 additions and 0 deletions
|
@ -0,0 +1,40 @@
|
||||||
|
// SPDX-FileCopyrightText: 2022 Paul Schaub <vanitasvitae@fsfe.org>
|
||||||
|
//
|
||||||
|
// SPDX-License-Identifier: Apache-2.0
|
||||||
|
|
||||||
|
package org.pgpainless.algorithm;
|
||||||
|
|
||||||
|
import java.util.Collection;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
public final class RegexSet {
|
||||||
|
|
||||||
|
private final Set<Regex> regexSet = new HashSet<>();
|
||||||
|
|
||||||
|
private RegexSet(Collection<Regex> regexes) {
|
||||||
|
this.regexSet.addAll(regexes);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static RegexSet matchAnything() {
|
||||||
|
return new RegexSet(Collections.singleton(Regex.wildcard()));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static RegexSet matchNothing() {
|
||||||
|
return new RegexSet(Collections.emptySet());
|
||||||
|
}
|
||||||
|
|
||||||
|
public static RegexSet matchSome(Collection<Regex> regexes) {
|
||||||
|
return new RegexSet(regexes);
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean matches(String userId) {
|
||||||
|
for (Regex regex : regexSet) {
|
||||||
|
if (regex.matches(userId)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,51 @@
|
||||||
|
// SPDX-FileCopyrightText: 2022 Paul Schaub <vanitasvitae@fsfe.org>
|
||||||
|
//
|
||||||
|
// SPDX-License-Identifier: Apache-2.0
|
||||||
|
|
||||||
|
package org.pgpainless.algorithm;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
public class RegexSetTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void matchNothingTest() {
|
||||||
|
RegexSet set = RegexSet.matchNothing();
|
||||||
|
assertFalse(set.matches("<alice@pgpainless.org>"));
|
||||||
|
assertFalse(set.matches("Alice"));
|
||||||
|
assertFalse(set.matches("Alice <alice@pgpainless.org>"));
|
||||||
|
assertFalse(set.matches(""));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void matchAnything() {
|
||||||
|
RegexSet set = RegexSet.matchAnything();
|
||||||
|
assertTrue(set.matches("Alice"));
|
||||||
|
assertTrue(set.matches("<alice@pgpainless.org>"));
|
||||||
|
assertTrue(set.matches("Alice <alice@pgpainless.org>"));
|
||||||
|
assertTrue(set.matches("Alice <alice@example.org>"));
|
||||||
|
assertTrue(set.matches(""));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void matchSome() {
|
||||||
|
Regex pgpainless_org = RegexInterpreterFactory.createDefaultMailDomainRegex("pgpainless.org");
|
||||||
|
Regex example_org = RegexInterpreterFactory.createDefaultMailDomainRegex("example.org");
|
||||||
|
|
||||||
|
RegexSet set = RegexSet.matchSome(Arrays.asList(pgpainless_org, example_org));
|
||||||
|
assertTrue(set.matches("Alice <alice@pgpainless.org>"));
|
||||||
|
assertTrue(set.matches("<alice@pgpainless.org>"));
|
||||||
|
assertTrue(set.matches("Bob <bob@example.org>"));
|
||||||
|
assertTrue(set.matches("<bob@example.org>"));
|
||||||
|
assertFalse(set.matches("Bob <bob@example.com>"));
|
||||||
|
assertFalse(set.matches("Alice <alice@PGPainless.org>"));
|
||||||
|
assertFalse(set.matches("alice@pgpainless.org"));
|
||||||
|
assertFalse(set.matches("Alice"));
|
||||||
|
assertFalse(set.matches(""));
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue