22ce203f4d495115308f8115088124357e8b41d3
[moon.git] /
1 /*
2  * Copyright (c) 2015 Brocade Communications Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.aaa.shiro.realm;
10
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;
19
20 import java.util.Collection;
21 import java.util.HashSet;
22 import java.util.Iterator;
23 import java.util.List;
24 import java.util.Set;
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;
38
39 /**
40  * @author Ryan Goulding (ryandgoulding@gmail.com)
41  */
42 public class ODLJndiLdapRealmTest {
43
44     /**
45      * throw-away anonymous test class
46      */
47     class TestNamingEnumeration implements NamingEnumeration<SearchResult> {
48
49         /**
50          * state variable
51          */
52         boolean first = true;
53
54         /**
55          * returned the first time <code>next()</code> or
56          * <code>nextElement()</code> is called.
57          */
58         SearchResult searchResult = new SearchResult("testuser", null, new BasicAttributes(
59                 "objectClass", "engineering"));
60
61         /**
62          * returns true the first time, then false for subsequent calls
63          */
64         @Override
65         public boolean hasMoreElements() {
66             return first;
67         }
68
69         /**
70          * returns <code>searchResult</code> then null for subsequent calls
71          */
72         @Override
73         public SearchResult nextElement() {
74             if (first) {
75                 first = false;
76                 return searchResult;
77             }
78             return null;
79         }
80
81         /**
82          * does nothing because close() doesn't require any special behavior
83          */
84         @Override
85         public void close() throws NamingException {
86         }
87
88         /**
89          * returns true the first time, then false for subsequent calls
90          */
91         @Override
92         public boolean hasMore() throws NamingException {
93             return first;
94         }
95
96         /**
97          * returns <code>searchResult</code> then null for subsequent calls
98          */
99         @Override
100         public SearchResult next() throws NamingException {
101             if (first) {
102                 first = false;
103                 return searchResult;
104             }
105             return null;
106         }
107     };
108
109     /**
110      * throw away test class
111      *
112      * @author ryan
113      */
114     class TestPrincipalCollection implements PrincipalCollection {
115         /**
116      *
117      */
118         private static final long serialVersionUID = -1236759619455574475L;
119
120         Vector<String> collection = new Vector<String>();
121
122         public TestPrincipalCollection(String element) {
123             collection.add(element);
124         }
125
126         @Override
127         public Iterator<String> iterator() {
128             return collection.iterator();
129         }
130
131         @Override
132         public List<String> asList() {
133             return collection;
134         }
135
136         @Override
137         public Set<String> asSet() {
138             HashSet<String> set = new HashSet<String>();
139             set.addAll(collection);
140             return set;
141         }
142
143         @Override
144         public <T> Collection<T> byType(Class<T> arg0) {
145             return null;
146         }
147
148         @Override
149         public Collection<String> fromRealm(String arg0) {
150             return collection;
151         }
152
153         @Override
154         public Object getPrimaryPrincipal() {
155             return collection.firstElement();
156         }
157
158         @Override
159         public Set<String> getRealmNames() {
160             return null;
161         }
162
163         @Override
164         public boolean isEmpty() {
165             return collection.isEmpty();
166         }
167
168         @Override
169         public <T> T oneByType(Class<T> arg0) {
170             // TODO Auto-generated method stub
171             return null;
172         }
173     };
174
175     @Test
176     public void testGetUsernameAuthenticationToken() {
177         AuthenticationToken authenticationToken = null;
178         assertNull(ODLJndiLdapRealm.getUsername(authenticationToken));
179         AuthenticationToken validAuthenticationToken = new UsernamePasswordToken("test",
180                 "testpassword");
181         assertEquals("test", ODLJndiLdapRealm.getUsername(validAuthenticationToken));
182     }
183
184     @Test
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);
191     }
192
193     @Test
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
198         when(
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"));
208     }
209
210     @Test
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"));
219     }
220
221     @Test
222     public void testGetRoleNamesForUser() throws NamingException {
223         ODLJndiLdapRealm ldapRealm = new ODLJndiLdapRealm();
224         LdapContext ldapContext = mock(LdapContext.class);
225
226         // emulates an ldap search and returns the mocked up test class
227         when(
228                 ldapContext.search((String) any(), (String) any(),
229                         (SearchControls) any())).thenReturn(new TestNamingEnumeration());
230
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"));
235     }
236
237     @Test
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);
244     }
245
246 }