Skip to content

Commit

Permalink
[ELY-2171] Add test for MappedRegexRealmMapper
Browse files Browse the repository at this point in the history
[ELY-2171] Add test for MappedRegexRealmMapper

[ELY-2171] Add test for MappedRegexRealmMapper
  • Loading branch information
pedro-hos committed Oct 15, 2021
1 parent 4727318 commit 8b9eef8
Show file tree
Hide file tree
Showing 3 changed files with 148 additions and 82 deletions.
163 changes: 83 additions & 80 deletions auth/util/pom.xml
@@ -1,91 +1,94 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ JBoss, Home of Professional Open Source.
~ Copyright 2018 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.
-->
<!-- ~ JBoss, Home of Professional Open Source. ~ Copyright 2018 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. -->

<project xmlns="http://maven.apache.org/POM/4.0.0"
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">
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">

<parent>
<groupId>org.wildfly.security</groupId>
<artifactId>wildfly-elytron-parent</artifactId>
<version>1.17.3.CR1-SNAPSHOT</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<parent>
<groupId>org.wildfly.security</groupId>
<artifactId>wildfly-elytron-parent</artifactId>
<version>1.17.3.CR1-SNAPSHOT</version>
<relativePath>../../pom.xml</relativePath>
</parent>

<modelVersion>4.0.0</modelVersion>
<modelVersion>4.0.0</modelVersion>

<artifactId>wildfly-elytron-auth-util</artifactId>
<artifactId>wildfly-elytron-auth-util</artifactId>

<name>WildFly Elytron - Auth Util</name>
<description>WildFly Security Auth Util</description>
<name>WildFly Elytron - Auth Util</name>
<description>WildFly Security Auth Util</description>

<dependencies>
<dependency>
<groupId>org.wildfly.security</groupId>
<artifactId>wildfly-elytron-auth</artifactId>
</dependency>
<dependency>
<groupId>org.wildfly.security</groupId>
<artifactId>wildfly-elytron-base</artifactId>
</dependency>
<dependency>
<groupId>org.wildfly.security</groupId>
<artifactId>wildfly-elytron-credential</artifactId>
</dependency>
<dependency>
<groupId>org.wildfly.security</groupId>
<artifactId>wildfly-elytron-auth-server</artifactId>
</dependency>
<dependency>
<groupId>org.wildfly.security</groupId>
<artifactId>wildfly-elytron-mechanism-gssapi</artifactId>
</dependency>
<dependency>
<groupId>org.wildfly.security</groupId>
<artifactId>wildfly-elytron-security-manager-action</artifactId>
</dependency>

<dependency>
<groupId>org.wildfly.common</groupId>
<artifactId>wildfly-common</artifactId>
<!-- scope is compile ELY-1153 -->
</dependency>

<dependency>
<groupId>org.jboss.logging</groupId>
<artifactId>jboss-logging</artifactId>
</dependency>
<dependency>
<groupId>org.jboss.logging</groupId>
<artifactId>jboss-logging-annotations</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.jboss.logging</groupId>
<artifactId>jboss-logging-processor</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.sshd</groupId>
<artifactId>sshd-common</artifactId>
</dependency>
<dependencies>
<dependency>
<groupId>org.wildfly.security</groupId>
<artifactId>wildfly-elytron-auth</artifactId>
</dependency>
<dependency>
<groupId>org.wildfly.security</groupId>
<artifactId>wildfly-elytron-base</artifactId>
</dependency>
<dependency>
<groupId>org.wildfly.security</groupId>
<artifactId>wildfly-elytron-credential</artifactId>
</dependency>
<dependency>
<groupId>org.wildfly.security</groupId>
<artifactId>wildfly-elytron-auth-server</artifactId>
</dependency>
<dependency>
<groupId>org.wildfly.security</groupId>
<artifactId>wildfly-elytron-mechanism-gssapi</artifactId>
</dependency>
<dependency>
<groupId>org.wildfly.security</groupId>
<artifactId>wildfly-elytron-security-manager-action</artifactId>
</dependency>

</dependencies>
<dependency>
<groupId>org.wildfly.common</groupId>
<artifactId>wildfly-common</artifactId>
<!-- scope is compile ELY-1153 -->
</dependency>

<dependency>
<groupId>org.jboss.logging</groupId>
<artifactId>jboss-logging</artifactId>
</dependency>
<dependency>
<groupId>org.jboss.logging</groupId>
<artifactId>jboss-logging-annotations</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.jboss.logging</groupId>
<artifactId>jboss-logging-processor</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.sshd</groupId>
<artifactId>sshd-common</artifactId>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jmockit</groupId>
<artifactId>jmockit</artifactId>
<scope>test</scope>
</dependency>

</dependencies>

</project>
Expand Up @@ -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 <a href="mailto:david.lloyd@redhat.com">David M. Lloyd</a>
*/
Expand Down
@@ -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 <a href="mailto:pesilva@redhat.com">Pedro Silva</a>
*/
public class MappedRegexRealmMapperTest {

@Test
public void shouldReturnNullForNonNamePrincipalInstance() {
Map<String, String> 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<String, String> 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<String, String> 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));
}
}

0 comments on commit 8b9eef8

Please sign in to comment.