1 package org.apache.archiva.redback.users.ldap;
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
22 import junit.framework.TestCase;
23 import org.apache.archiva.redback.policy.PasswordEncoder;
24 import org.apache.archiva.redback.users.User;
25 import org.apache.archiva.redback.common.ldap.connection.LdapConnection;
26 import org.apache.archiva.redback.common.ldap.connection.LdapConnectionFactory;
27 import org.apache.archiva.redback.policy.encoders.SHA1PasswordEncoder;
28 import org.apache.archiva.redback.users.UserManager;
29 import org.apache.archiva.redback.users.UserNotFoundException;
30 import org.apache.archiva.redback.users.ldap.service.LdapCacheService;
31 import org.apache.archiva.redback.components.apacheds.ApacheDs;
32 import org.junit.After;
33 import org.junit.Before;
34 import org.junit.Test;
35 import org.junit.runner.RunWith;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38 import org.springframework.test.context.ContextConfiguration;
39 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
41 import javax.inject.Inject;
42 import javax.inject.Named;
43 import javax.naming.NamingEnumeration;
44 import javax.naming.NamingException;
45 import javax.naming.directory.Attribute;
46 import javax.naming.directory.Attributes;
47 import javax.naming.directory.BasicAttribute;
48 import javax.naming.directory.BasicAttributes;
49 import javax.naming.directory.DirContext;
50 import javax.naming.directory.InitialDirContext;
51 import javax.naming.directory.SearchControls;
52 import javax.naming.directory.SearchResult;
53 import java.util.List;
59 * @author <a href="mailto:jesse@codehaus.org">Jesse McConnell</a>
63 @RunWith( SpringJUnit4ClassRunner.class )
64 @ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml", "classpath*:/spring-context.xml" } )
65 public class LdapUserManagerTest
69 protected Logger log = LoggerFactory.getLogger( getClass() );
72 @Named(value = "userManager#ldap")
73 private UserManager userManager;
76 @Named( value = "apacheDS#test" )
77 private ApacheDs apacheDs;
79 private String suffix;
81 private PasswordEncoder passwordEncoder;
84 @Named(value = "ldapConnectionFactory#configurable")
85 private LdapConnectionFactory connectionFactory;
88 private LdapCacheService ldapCacheService;
102 passwordEncoder = new SHA1PasswordEncoder();
104 suffix = apacheDs.addSimplePartition( "test", new String[] { "redback", "plexus", "codehaus", "org" } )
107 log.info( "DN Suffix: " + suffix );
109 apacheDs.startServer();
118 public void tearDown()
122 ldapCacheService.removeAllUsers();
124 InitialDirContext context = apacheDs.getAdminContext();
126 context.unbind( createDn( "jesse" ) );
128 context.unbind( createDn( "joakim" ) );
130 apacheDs.stopServer();
135 private void makeUsers()
138 InitialDirContext context = apacheDs.getAdminContext();
141 bindUserObject( context, cn, createDn( cn ) );
142 assertExist( context, createDn( cn ), "cn", cn );
145 bindUserObject( context, cn, createDn( cn ) );
146 assertExist( context, createDn( cn ), "cn", cn );
151 public void testConnection()
154 assertNotNull( connectionFactory );
156 LdapConnection connection = null;
159 connection = connectionFactory.getConnection();
161 assertNotNull( connection );
163 DirContext context = connection.getDirContext();
165 assertNotNull( context );
172 public void testDirectUsersExistence()
175 LdapConnection connection = null;
178 connection = connectionFactory.getConnection();
180 DirContext context = connection.getDirContext();
182 assertExist( context, createDn( "jesse" ), "cn", "jesse" );
183 assertExist( context, createDn( "joakim" ), "cn", "joakim" );
191 public void testUserManager()
194 assertNotNull( userManager );
196 //assertNull( ldapCacheService.getUser( "jesse" ) );
198 assertTrue( userManager.userExists( "jesse" ) );
200 //assertNotNull( ldapCacheService.getUser( "jesse" ) );
202 List<User> users = userManager.getUsers();
204 assertNotNull( users );
206 assertEquals( 2, users.size() );
208 User jesse = userManager.findUser( "jesse" );
210 assertNotNull( jesse );
212 assertEquals( "jesse", jesse.getPrincipal().toString() );
213 assertEquals( "jesse@apache.org", jesse.getEmail() );
214 assertEquals( "foo", jesse.getFullName() );
215 System.out.println( "=====>"+jesse.getEncodedPassword());
216 System.out.println( "=====>"+passwordEncoder.encodePassword( "foo" ));
217 assertTrue( passwordEncoder.isPasswordValid( jesse.getEncodedPassword(), "foo" ) );
222 public void testUserNotFoundException()
227 userManager.findUser( "foo bar" );
228 fail( "not a UserNotFoundException with an unknown user" );
230 catch ( UserNotFoundException e )
237 public void testWithManyUsers()
242 assertNotNull( userManager );
244 assertTrue( userManager.userExists( "user10" ) );
246 List<User> users = userManager.getUsers();
248 assertNotNull( users );
250 assertEquals( 10002, users.size() );
252 User user10 = userManager.findUser( "user10" );
254 assertNotNull( user10 );
257 private void makeManyUsers()
260 InitialDirContext context = apacheDs.getAdminContext();
262 for ( int i = 0 ; i < 10000 ; i++ )
264 String cn = "user"+i;
265 bindUserObject( context, cn, createDn( cn ) );
270 private void clearManyUsers()
273 InitialDirContext context = apacheDs.getAdminContext();
275 for ( int i = 0 ; i < 10000 ; i++ )
277 String cn = "user"+i;
280 context.unbind( createDn( cn ) );
282 catch ( NamingException e )
284 // OK lets try with next one
290 private void bindUserObject( DirContext context, String cn, String dn )
293 Attributes attributes = new BasicAttributes( true );
294 BasicAttribute objectClass = new BasicAttribute( "objectClass" );
295 objectClass.add( "top" );
296 objectClass.add( "inetOrgPerson" );
297 objectClass.add( "person" );
298 objectClass.add( "organizationalperson" );
299 attributes.put( objectClass );
300 attributes.put( "cn", cn );
301 attributes.put( "sn", "foo" );
302 attributes.put( "mail", cn+"@apache.org" );
303 attributes.put( "userPassword", passwordEncoder.encodePassword( "foo" ) );
304 attributes.put( "givenName", "foo" );
305 context.createSubcontext( dn, attributes );
308 private String createDn( String cn )
310 return "cn=" + cn + "," + suffix;
313 private void assertExist( DirContext context, String dn, String attribute, String value )
314 throws NamingException
316 SearchControls ctls = new SearchControls();
318 ctls.setDerefLinkFlag( true );
319 ctls.setSearchScope( SearchControls.ONELEVEL_SCOPE );
320 ctls.setReturningAttributes( new String[] { "*" } );
322 BasicAttributes matchingAttributes = new BasicAttributes();
323 matchingAttributes.put( attribute, value );
324 BasicAttribute objectClass = new BasicAttribute( "objectClass" );
325 objectClass.add( "inetOrgPerson" );
326 matchingAttributes.put( objectClass );
328 NamingEnumeration<SearchResult> results = context.search( suffix, matchingAttributes );
329 // NamingEnumeration<SearchResult> results = context.search( suffix, "(" + attribute + "=" + value + ")", ctls
332 assertTrue( results.hasMoreElements() );
333 SearchResult result = results.nextElement();
334 Attributes attrs = result.getAttributes();
335 Attribute testAttr = attrs.get( attribute );
336 assertEquals( value, testAttr.get() );