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

[ELY-2171] Add test for MappedRegexRealmMapper #1601

[ELY-2171] Add test for MappedRegexRealmMapper
  • Loading branch information
pedro-hos committed Oct 18, 2021
1 parent 4727318 commit e730e01
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 8 deletions.
23 changes: 17 additions & 6 deletions auth/util/pom.xml
Expand Up @@ -39,15 +39,15 @@
<dependency>
<groupId>org.wildfly.security</groupId>
<artifactId>wildfly-elytron-auth</artifactId>
</dependency>
</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>
<dependency>
<groupId>org.wildfly.security</groupId>
<artifactId>wildfly-elytron-auth-server</artifactId>
Expand All @@ -60,17 +60,17 @@
<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>
<dependency>
<groupId>org.jboss.logging</groupId>
<artifactId>jboss-logging-annotations</artifactId>
Expand All @@ -86,6 +86,17 @@
<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>
</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 e730e01

Please sign in to comment.