From 8b9eef8a2b04168e231e2ce95f6f04fd4e289eb7 Mon Sep 17 00:00:00 2001 From: Pedro Hos Date: Thu, 14 Oct 2021 22:56:24 -0300 Subject: [PATCH] [ELY-2171] Add test for MappedRegexRealmMapper [ELY-2171] Add test for MappedRegexRealmMapper [ELY-2171] Add test for MappedRegexRealmMapper --- auth/util/pom.xml | 163 +++++++++--------- .../auth/util/MappedRegexRealmMapper.java | 4 +- .../auth/util/MappedRegexRealmMapperTest.java | 63 +++++++ 3 files changed, 148 insertions(+), 82 deletions(-) create mode 100644 auth/util/src/test/java/org/wildfly/security/auth/util/MappedRegexRealmMapperTest.java diff --git a/auth/util/pom.xml b/auth/util/pom.xml index b2cfc83ccf0..c9d314a08a2 100644 --- a/auth/util/pom.xml +++ b/auth/util/pom.xml @@ -1,91 +1,94 @@ - + + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> - - org.wildfly.security - wildfly-elytron-parent - 1.17.3.CR1-SNAPSHOT - ../../pom.xml - + + org.wildfly.security + wildfly-elytron-parent + 1.17.3.CR1-SNAPSHOT + ../../pom.xml + - 4.0.0 + 4.0.0 - wildfly-elytron-auth-util + wildfly-elytron-auth-util - WildFly Elytron - Auth Util - WildFly Security Auth Util + WildFly Elytron - Auth Util + WildFly Security Auth Util - - - org.wildfly.security - wildfly-elytron-auth - - - org.wildfly.security - wildfly-elytron-base - - - org.wildfly.security - wildfly-elytron-credential - - - org.wildfly.security - wildfly-elytron-auth-server - - - org.wildfly.security - wildfly-elytron-mechanism-gssapi - - - org.wildfly.security - wildfly-elytron-security-manager-action - - - - org.wildfly.common - wildfly-common - - - - - org.jboss.logging - jboss-logging - - - org.jboss.logging - jboss-logging-annotations - provided - - - org.jboss.logging - jboss-logging-processor - provided - - - org.apache.sshd - sshd-common - + + + org.wildfly.security + wildfly-elytron-auth + + + org.wildfly.security + wildfly-elytron-base + + + org.wildfly.security + wildfly-elytron-credential + + + org.wildfly.security + wildfly-elytron-auth-server + + + org.wildfly.security + wildfly-elytron-mechanism-gssapi + + + org.wildfly.security + wildfly-elytron-security-manager-action + - + + org.wildfly.common + wildfly-common + + + + + org.jboss.logging + jboss-logging + + + org.jboss.logging + jboss-logging-annotations + provided + + + org.jboss.logging + jboss-logging-processor + provided + + + org.apache.sshd + sshd-common + + + + junit + junit + test + + + org.jmockit + jmockit + test + + + diff --git a/auth/util/src/main/java/org/wildfly/security/auth/util/MappedRegexRealmMapper.java b/auth/util/src/main/java/org/wildfly/security/auth/util/MappedRegexRealmMapper.java index baaba86721c..078d881cb6c 100644 --- a/auth/util/src/main/java/org/wildfly/security/auth/util/MappedRegexRealmMapper.java +++ b/auth/util/src/main/java/org/wildfly/security/auth/util/MappedRegexRealmMapper.java @@ -26,8 +26,8 @@ import org.wildfly.security.evidence.Evidence; /** - * A simple mapping regular expression-based realm mapper. The pattern is used to find the realm portion - * of the user name. Then, a map is consulted to map this realm portion to an actual configured realm name. + * A simple mapping regular expression-based realm mapper. The pattern is used to find the realm portion + * of the user name. Then, a map is consulted to map this realm portion to an actual configured realm name. * * @author David M. Lloyd */ diff --git a/auth/util/src/test/java/org/wildfly/security/auth/util/MappedRegexRealmMapperTest.java b/auth/util/src/test/java/org/wildfly/security/auth/util/MappedRegexRealmMapperTest.java new file mode 100644 index 00000000000..63c052bc119 --- /dev/null +++ b/auth/util/src/test/java/org/wildfly/security/auth/util/MappedRegexRealmMapperTest.java @@ -0,0 +1,63 @@ +/* + * JBoss, Home of Professional Open Source. + * Copyright 2016 Red Hat, Inc., and individual contributors + * as indicated by the @author tags. + * + * 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.wildfly.security.auth.util; + +import java.security.Principal; +import java.util.Collections; +import java.util.Map; +import java.util.regex.Pattern; + +import org.junit.Assert; +import org.junit.Test; +import org.wildfly.security.auth.principal.NamePrincipal; +import org.wildfly.security.x500.X500PrincipalBuilder; + +/** + * @author Pedro Silva + */ +public class MappedRegexRealmMapperTest { + + @Test + public void shouldReturnNullForNonNamePrincipalInstance() { + Map realmNameMap = Collections.emptyMap(); + Pattern pattern = Pattern.compile("([A-Z]-\\d{4})"); + MappedRegexRealmMapper mappedRegexRealmMapper = new MappedRegexRealmMapper(pattern, realmNameMap); + Principal principal = new X500PrincipalBuilder().build(); + Assert.assertNull(mappedRegexRealmMapper.getRealmMapping(principal, null)); + } + + @Test + public void shouldReturnNullCantFindValueInMap() { + Map realmNameMap = Collections.singletonMap("A-0000", "new_realm"); + Pattern pattern = Pattern.compile("([A-Z]-\\d{4})"); + MappedRegexRealmMapper mappedRegexRealmMapper = new MappedRegexRealmMapper(pattern, realmNameMap); + Principal principal = new NamePrincipal("A-9999"); + Assert.assertNull(mappedRegexRealmMapper.getRealmMapping(principal, null)); + } + + @Test + public void shouldReturnSpecificRealmMapping() { + String realmName = "new_realm"; + Map realmNameMap = Collections.singletonMap("A-9999", realmName); + Pattern pattern = Pattern.compile("([A-Z]-\\d{4})"); + MappedRegexRealmMapper mappedRegexRealmMapper = new MappedRegexRealmMapper(pattern, realmNameMap); + Principal principal = new NamePrincipal("A-9999"); + Assert.assertEquals(realmName, mappedRegexRealmMapper.getRealmMapping(principal, null)); + } +}