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 org.apache.jetspeed.security.SecurityException;
20  import org.apache.jetspeed.security.UserPrincipal;
21  import org.apache.jetspeed.security.impl.UserPrincipalImpl;
22  
23  import java.security.Principal;
24  
25  import java.util.List;
26  
27  /***
28   * <p>
29   * LdapServerTest - This class tests the LdapServer. It assumes that the following three
30   * inetOrgPerson objects exist: uid:cbrewton password:maddie uid:dlong, password: uid:mlong,
31   * password:maddie
32   * </p>
33   * 
34   * @author <a href="mailto:mike.long@dataline.com">Mike Long </a>
35   */
36  public class TestLdapUserSecurityHandler extends AbstractLdapTest
37  {
38      /***
39       * @see org.apache.jetspeed.security.spi.ldap.AbstractLdapTest#setUp()
40       */
41      protected void setUp() throws Exception
42      {
43          super.setUp();
44          LdapDataHelper.seedUserData(uid1, password);
45      }
46  
47      /***
48       * @see org.apache.jetspeed.security.spi.ldap.AbstractLdapTest#tearDown()
49       */
50      protected void tearDown() throws Exception
51      {
52          super.tearDown();
53          LdapDataHelper.removeUserData(uid1);
54      }
55  
56      /***
57       * @throws Exception
58       */
59      public void testUserIsPrincipal() throws Exception
60      {
61          assertTrue("User is not principal.", userHandler.isUserPrincipal(uid1));
62      }
63  
64      /***
65       * @throws Exception
66       */
67      public void testUserIsNotPrincipal() throws Exception
68      {
69          assertFalse("User is principal and should not be.", userHandler.isUserPrincipal(Integer
70                  .toString(rand.nextInt()).toString()));
71      }
72  
73      /***
74       * @throws Exception
75       */
76      public void testAddDuplicateUserPrincipal() throws Exception
77      {
78          try
79          {
80              userHandler.addUserPrincipal(new UserPrincipalImpl(uid1));
81              fail("Adding an already existant user should have thrown a SecurityException.");
82          }
83          catch (Exception e)
84          {
85              assertTrue("Adding an already existant user should have thrown a SecurityException.",
86                      e instanceof SecurityException);
87          }
88      }
89  
90      /***
91       * @throws Exception
92       */
93      public void testAddUserPrincipal() throws Exception
94      {
95          assertTrue("User not found.", userHandler.getUserPrincipal(uid1) != null);
96      }
97  
98      /***
99       * @throws Exception
100      */
101     public void testRemoveExistantUserPrincipal() throws Exception
102     {
103         UserPrincipal up = new UserPrincipalImpl(uid1);
104         userHandler.removeUserPrincipal(up);
105         assertTrue("User was found and should have been removed.", userHandler.getUserPrincipal(uid1) == null);
106     }
107 
108     /***
109      * @throws Exception
110      */
111     public void testRemoveNonExistantUserPrincipal() throws Exception
112     {
113         String localUid = Integer.toString(rand.nextInt()).toString();
114         UserPrincipal localPrin = new UserPrincipalImpl(localUid);
115 
116         userHandler.removeUserPrincipal(localPrin);
117     }
118 
119     /***
120      * @throws Exception
121      */
122     public void testGetUserPrincipals() throws Exception
123     {
124         try
125         {
126             LdapDataHelper.seedUserData(uid2, password);
127             // With wild card search
128             assertTrue("getUserPrincipals should have returned more than one user.", userHandler.getUserPrincipals("*")
129                     .size() > 1);
130             
131             // With empty string search
132             assertTrue("getUserPrincipals should have returned more than one user.", userHandler.getUserPrincipals("")
133                     .size() > 1);
134 
135             // With specific uid.
136             List users = userHandler.getUserPrincipals(uid1);
137 
138             assertTrue("getUserPrincipals should have returned one user.", users.size() == 1);
139             assertTrue("List should have consisted of Principal objects.", users.get(0) instanceof Principal);
140 
141             String localUid = Integer.toString(rand.nextInt()).toString();
142 
143             assertTrue("getUserPrincipals should not have found any users with the specified filter.", userHandler
144                     .getUserPrincipals(localUid).isEmpty());
145         }
146         finally
147         {
148             LdapDataHelper.removeUserData(uid2);
149         }
150     }
151 }