diff --git a/auth/util/pom.xml b/auth/util/pom.xml index b2cfc83ccf0..d6039f2ae9e 100644 --- a/auth/util/pom.xml +++ b/auth/util/pom.xml @@ -39,7 +39,7 @@ org.wildfly.security wildfly-elytron-auth - + org.wildfly.security wildfly-elytron-base @@ -47,7 +47,7 @@ org.wildfly.security wildfly-elytron-credential - + org.wildfly.security wildfly-elytron-auth-server @@ -60,17 +60,17 @@ org.wildfly.security wildfly-elytron-security-manager-action - + org.wildfly.common wildfly-common - + org.jboss.logging jboss-logging - + org.jboss.logging jboss-logging-annotations @@ -86,6 +86,17 @@ sshd-common + + junit + junit + test + + + org.jmockit + jmockit + test + + - + \ No newline at end of file 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)); + } +}