1   /* 
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.jetspeed.security.spi.ldap;
18  
19  import java.util.Random;
20  
21  import javax.naming.NamingException;
22  
23  import org.apache.commons.logging.Log;
24  import org.apache.commons.logging.LogFactory;
25  import org.apache.jetspeed.components.test.AbstractSpringTestCase;
26  import org.apache.jetspeed.security.spi.CredentialHandler;
27  import org.apache.jetspeed.security.spi.GroupSecurityHandler;
28  import org.apache.jetspeed.security.spi.RoleSecurityHandler;
29  import org.apache.jetspeed.security.spi.SecurityMappingHandler;
30  import org.apache.jetspeed.security.spi.UserSecurityHandler;
31  import org.apache.jetspeed.security.spi.impl.LdapCredentialHandler;
32  import org.apache.jetspeed.security.spi.impl.LdapGroupSecurityHandler;
33  import org.apache.jetspeed.security.spi.impl.LdapRoleSecurityHandler;
34  import org.apache.jetspeed.security.spi.impl.LdapSecurityMappingHandler;
35  import org.apache.jetspeed.security.spi.impl.LdapUserSecurityHandler;
36  import org.apache.jetspeed.security.spi.impl.ldap.InitLdapSchema;
37  import org.apache.jetspeed.security.spi.impl.ldap.LdapBindingConfig;
38  import org.apache.jetspeed.security.spi.impl.ldap.LdapGroupDaoImpl;
39  import org.apache.jetspeed.security.spi.impl.ldap.LdapMemberShipDaoImpl;
40  import org.apache.jetspeed.security.spi.impl.ldap.LdapMembershipDao;
41  import org.apache.jetspeed.security.spi.impl.ldap.LdapPrincipalDao;
42  import org.apache.jetspeed.security.spi.impl.ldap.LdapRoleDaoImpl;
43  import org.apache.jetspeed.security.spi.impl.ldap.LdapUserCredentialDao;
44  import org.apache.jetspeed.security.spi.impl.ldap.LdapUserCredentialDaoImpl;
45  import org.apache.jetspeed.security.spi.impl.ldap.LdapUserPrincipalDao;
46  import org.apache.jetspeed.security.spi.impl.ldap.LdapUserPrincipalDaoImpl;
47  
48  /***
49   * <p>
50   * Abstract test case for LDAP providers.
51   * </p>
52   * 
53   * @author <a href="mailto:mike.long@dataline.com">Mike Long </a>, <a href="mailto:dlestrat@apache.org">David Le Strat</a>
54   * 
55   */
56  public abstract class AbstractLdapTest extends AbstractSpringTestCase
57  {
58      /*** The logger. */
59      private static final Log logger = LogFactory.getLog(AbstractLdapTest.class);
60      
61  	private static final String LDAP_CONFIG = "openldap/setup2";
62  	
63      /*** The {@link UserSecurityHandler}. */
64      UserSecurityHandler userHandler;
65  
66      /*** The {@link CredentialHandler}. */
67      CredentialHandler crHandler;
68  
69      /*** The {@link GroupSecurityHandler}. */
70      GroupSecurityHandler grHandler;
71      
72      /*** The {@link RoleSecurityHandler}. */
73      RoleSecurityHandler roleHandler;    
74      
75      /*** The {@link SecurityMappingHandler}. */
76      SecurityMappingHandler secHandler;
77      
78      /*** The {@link LdapUserPrincipalDao}. */
79      LdapUserPrincipalDao ldapPrincipalDao;
80      
81      /*** The {@link LdapUserCredentialDao}. */
82      LdapUserCredentialDao ldapCredDao;
83      
84      /*** The {@link LdapGroupDao}. */
85      LdapPrincipalDao ldapGroupDao;
86      
87      /*** The {@link LdapGroupDao}. */
88      LdapPrincipalDao ldapRoleDao;    
89      
90      LdapMembershipDao ldapMembershipDao;
91  
92      /*** Random seed. */
93      Random rand = new Random(System.currentTimeMillis());
94  
95      /*** Group uid. */
96      protected String gpUid1 = "group1";
97  
98      /*** Group uid. */
99      protected String gpUid2 = "group2";
100     
101     /*** Role uid. */
102     protected String roleUid1 = "role1";
103 
104     /*** Role uid. */
105     protected String roleUid2 = "role2";    
106 
107     /*** User uid. */
108     protected String uid1 = "user1";
109 
110     /*** User uid. */
111     protected String uid2 = "user2";
112 
113     /*** The test password. */
114     protected String password = "fred";
115     
116 
117     /***
118      * @see junit.framework.TestCase#setUp()
119      */
120     protected void setUp() throws Exception
121     {
122         super.setUp();
123         LdapBindingConfig ldapConfig = (LdapBindingConfig)ctx.getBean(LdapBindingConfig.class.getName());
124         InitLdapSchema ldapSchema = new InitLdapSchema(ldapConfig);
125         try
126         {
127             // make sure standard test case schema exists
128             ldapSchema.initOu("OrgUnit1");
129             ldapSchema.initOu("People");
130             ldapSchema.initOu("Roles");
131             ldapSchema.initOu("People","ou=OrgUnit1");
132             ldapSchema.initOu("Groups","ou=OrgUnit1");
133             ldapSchema.initOu("Roles","ou=OrgUnit1");
134 
135         }
136         catch (NamingException se)
137         {
138             logger.error("Initializing the LDAP directory failed:", se);
139             throw se;
140         }
141 
142         ldapCredDao = new LdapUserCredentialDaoImpl(ldapConfig);
143         ldapPrincipalDao = new LdapUserPrincipalDaoImpl(ldapConfig);
144 
145         userHandler = new LdapUserSecurityHandler(ldapPrincipalDao);
146         crHandler = new LdapCredentialHandler(ldapCredDao);
147         LdapDataHelper.setUserSecurityHandler(userHandler);
148         LdapDataHelper.setCredentialHandler(crHandler);
149         
150         ldapGroupDao = new LdapGroupDaoImpl(ldapConfig);
151         ldapRoleDao = new LdapRoleDaoImpl(ldapConfig);
152         ldapMembershipDao = new LdapMemberShipDaoImpl(ldapConfig);
153         grHandler = new LdapGroupSecurityHandler(ldapGroupDao);
154         roleHandler = new LdapRoleSecurityHandler(ldapRoleDao);
155         LdapDataHelper.setGroupSecurityHandler(grHandler);
156         LdapDataHelper.setRoleSecurityHandler(roleHandler);
157         
158         secHandler = new LdapSecurityMappingHandler(ldapPrincipalDao, ldapGroupDao, ldapRoleDao);
159     }
160 
161     /***
162      * @see junit.framework.TestCase#tearDown()
163      */
164     protected void tearDown() throws Exception
165     {
166         super.tearDown();
167     }
168 
169     protected String[] getConfigurations()
170     {
171         return new String[] {"JETSPEED-INF/directory/config/" + LDAP_CONFIG + "/security-spi-ldap.xml" };
172     }    
173 }