2 * Copyright (c) 2015 Brocade Communications Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.aaa.shiro.realm;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertFalse;
13 import static org.junit.Assert.assertNotNull;
14 import static org.junit.Assert.assertNull;
15 import static org.junit.Assert.assertTrue;
16 import static org.mockito.Matchers.any;
17 import static org.mockito.Mockito.mock;
18 import static org.mockito.Mockito.when;
20 import java.util.Collection;
21 import java.util.HashSet;
22 import java.util.Iterator;
23 import java.util.List;
25 import java.util.Vector;
26 import javax.naming.NamingEnumeration;
27 import javax.naming.NamingException;
28 import javax.naming.directory.BasicAttributes;
29 import javax.naming.directory.SearchControls;
30 import javax.naming.directory.SearchResult;
31 import javax.naming.ldap.LdapContext;
32 import org.apache.shiro.authc.AuthenticationToken;
33 import org.apache.shiro.authc.UsernamePasswordToken;
34 import org.apache.shiro.authz.AuthorizationInfo;
35 import org.apache.shiro.realm.ldap.LdapContextFactory;
36 import org.apache.shiro.subject.PrincipalCollection;
37 import org.junit.Test;
40 * @author Ryan Goulding (ryandgoulding@gmail.com)
42 public class ODLJndiLdapRealmTest {
45 * throw-away anonymous test class
47 class TestNamingEnumeration implements NamingEnumeration<SearchResult> {
55 * returned the first time <code>next()</code> or
56 * <code>nextElement()</code> is called.
58 SearchResult searchResult = new SearchResult("testuser", null, new BasicAttributes(
59 "objectClass", "engineering"));
62 * returns true the first time, then false for subsequent calls
65 public boolean hasMoreElements() {
70 * returns <code>searchResult</code> then null for subsequent calls
73 public SearchResult nextElement() {
82 * does nothing because close() doesn't require any special behavior
85 public void close() throws NamingException {
89 * returns true the first time, then false for subsequent calls
92 public boolean hasMore() throws NamingException {
97 * returns <code>searchResult</code> then null for subsequent calls
100 public SearchResult next() throws NamingException {
110 * throw away test class
114 class TestPrincipalCollection implements PrincipalCollection {
118 private static final long serialVersionUID = -1236759619455574475L;
120 Vector<String> collection = new Vector<String>();
122 public TestPrincipalCollection(String element) {
123 collection.add(element);
127 public Iterator<String> iterator() {
128 return collection.iterator();
132 public List<String> asList() {
137 public Set<String> asSet() {
138 HashSet<String> set = new HashSet<String>();
139 set.addAll(collection);
144 public <T> Collection<T> byType(Class<T> arg0) {
149 public Collection<String> fromRealm(String arg0) {
154 public Object getPrimaryPrincipal() {
155 return collection.firstElement();
159 public Set<String> getRealmNames() {
164 public boolean isEmpty() {
165 return collection.isEmpty();
169 public <T> T oneByType(Class<T> arg0) {
170 // TODO Auto-generated method stub
176 public void testGetUsernameAuthenticationToken() {
177 AuthenticationToken authenticationToken = null;
178 assertNull(ODLJndiLdapRealm.getUsername(authenticationToken));
179 AuthenticationToken validAuthenticationToken = new UsernamePasswordToken("test",
181 assertEquals("test", ODLJndiLdapRealm.getUsername(validAuthenticationToken));
185 public void testGetUsernamePrincipalCollection() {
186 PrincipalCollection pc = null;
187 assertNull(new ODLJndiLdapRealm().getUsername(pc));
188 TestPrincipalCollection tpc = new TestPrincipalCollection("testuser");
189 String username = new ODLJndiLdapRealm().getUsername(tpc);
190 assertEquals("testuser", username);
194 public void testQueryForAuthorizationInfoPrincipalCollectionLdapContextFactory()
195 throws NamingException {
196 LdapContext ldapContext = mock(LdapContext.class);
197 // emulates an ldap search and returns the mocked up test class
199 ldapContext.search((String) any(), (String) any(),
200 (SearchControls) any())).thenReturn(new TestNamingEnumeration());
201 LdapContextFactory ldapContextFactory = mock(LdapContextFactory.class);
202 when(ldapContextFactory.getSystemLdapContext()).thenReturn(ldapContext);
203 AuthorizationInfo authorizationInfo = new ODLJndiLdapRealm().queryForAuthorizationInfo(
204 new TestPrincipalCollection("testuser"), ldapContextFactory);
205 assertNotNull(authorizationInfo);
206 assertFalse(authorizationInfo.getRoles().isEmpty());
207 assertTrue(authorizationInfo.getRoles().contains("engineering"));
211 public void testBuildAuthorizationInfo() {
212 assertNull(ODLJndiLdapRealm.buildAuthorizationInfo(null));
213 Set<String> roleNames = new HashSet<String>();
214 roleNames.add("engineering");
215 AuthorizationInfo authorizationInfo = ODLJndiLdapRealm.buildAuthorizationInfo(roleNames);
216 assertNotNull(authorizationInfo);
217 assertFalse(authorizationInfo.getRoles().isEmpty());
218 assertTrue(authorizationInfo.getRoles().contains("engineering"));
222 public void testGetRoleNamesForUser() throws NamingException {
223 ODLJndiLdapRealm ldapRealm = new ODLJndiLdapRealm();
224 LdapContext ldapContext = mock(LdapContext.class);
226 // emulates an ldap search and returns the mocked up test class
228 ldapContext.search((String) any(), (String) any(),
229 (SearchControls) any())).thenReturn(new TestNamingEnumeration());
231 // extracts the roles for "testuser" and ensures engineering is returned
232 Set<String> roles = ldapRealm.getRoleNamesForUser("testuser", ldapContext);
233 assertFalse(roles.isEmpty());
234 assertTrue(roles.iterator().next().equals("engineering"));
238 public void testCreateSearchControls() {
239 SearchControls searchControls = ODLJndiLdapRealm.createSearchControls();
240 assertNotNull(searchControls);
241 int expectedSearchScope = SearchControls.SUBTREE_SCOPE;
242 int actualSearchScope = searchControls.getSearchScope();
243 assertEquals(expectedSearchScope, actualSearchScope);