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;
18  
19  import java.util.ArrayList;
20  import java.util.Arrays;
21  import java.util.List;
22  import java.util.Set;
23  
24  import org.apache.jetspeed.security.PasswordCredential;
25  import org.apache.jetspeed.security.util.test.AbstractSecurityTestcase;
26  import org.apache.jetspeed.security.SecurityException;
27  
28  import junit.framework.Test;
29  import junit.framework.TestSuite;
30  
31  /***
32   * <p>
33   * Unit testing for {@link PasswordCredentialProvider}.
34   * </p>
35   * 
36   * @author <a href="mailto:ate@apache.org">Ate Douma</a>
37   */
38  public class TestPasswordCredentialProvider extends AbstractSecurityTestcase
39  {
40      /***
41       * @see junit.framework.TestCase#setUp()
42       */
43      protected void setUp() throws Exception
44      {
45          super.setUp(); 
46          // cleanup for previously failed test
47          destroyUser();
48      }
49  
50      /***
51       * @see junit.framework.TestCase#tearDown()
52       */
53      public void tearDown() throws Exception
54      {
55          super.tearDown();
56      }
57  
58      /***
59       * <p>
60       * Constructs the suite.
61       * </p>
62       * 
63       * @return The {@Test}.
64       */
65      public static Test suite()
66      {
67          return new TestSuite(TestPasswordCredentialProvider.class);
68      }
69  
70      /***
71       * <p>
72       * Test <code>getPrivateCredentials</code>..
73       * </p>
74       */
75      public void testGetPrivateCredentials() throws Exception
76      {
77          initUser();
78          Set privateCredentials = ums.getUser("testcred").getSubject().getPrivateCredentials();
79          assertNotNull(privateCredentials);
80          assertEquals(1, privateCredentials.size());
81          PasswordCredential[] pwdCreds = (PasswordCredential[]) privateCredentials.toArray(new PasswordCredential[0]);
82          assertEquals("testcred", pwdCreds[0].getUserName());
83          assertNotSame("password01", new String(pwdCreds[0].getPassword()));
84          destroyUser();
85      }
86      
87      /***
88       * <p>
89       * Test <code>setPassword</code>..
90       * </p>
91       */
92      public void testSetPassword() throws Exception
93      {
94          initUser();
95          Set privateCredentials = ums.getUser("testcred").getSubject().getPrivateCredentials();
96          assertNotNull(privateCredentials);
97          assertEquals(1, privateCredentials.size());
98          PasswordCredential[] pwdCreds = (PasswordCredential[]) privateCredentials.toArray(new PasswordCredential[0]);
99          assertEquals("testcred", pwdCreds[0].getUserName());
100         String encodedPassword = new String(pwdCreds[0].getPassword());
101         assertNotSame("password01", encodedPassword );
102         
103         // Try setting an invalid password: to short (min: 8)
104         try
105         {
106             ums.setPassword("testcred","password01","1234567");
107             fail("Should not be able to set an invalid password");
108         }
109         catch (SecurityException e){}
110         // Try setting an invalid password: no digits
111         try
112         {
113             ums.setPassword("testcred","password01","newpassword");
114             fail("Should not be able to set an invalid password");
115         }
116         catch (SecurityException e){}
117         // Setting a valid password
118         ums.setPassword("testcred","password01","passwd01");
119 
120         // Test that the credential was updated.
121         privateCredentials = ums.getUser("testcred").getSubject().getPrivateCredentials();
122         assertNotNull(privateCredentials);
123         assertEquals(1, privateCredentials.size());
124         pwdCreds = (PasswordCredential[]) privateCredentials.toArray(new PasswordCredential[0]);
125         assertEquals("testcred", pwdCreds[0].getUserName());
126         String newEncodedPassword = new String(pwdCreds[0].getPassword());
127         assertNotSame(encodedPassword, newEncodedPassword);
128         assertNotSame("passwd01", newEncodedPassword);
129         
130         // Test authentication with the new password
131         assertTrue(ums.authenticate("testcred","passwd01"));
132         destroyUser();
133     }
134     
135     /***
136      * <p>
137      * Initialize user test object.
138      * </p>
139      */
140     protected void initUser() throws Exception
141     {
142         ums.addUser("testcred", "password01");
143     }
144 
145     /***
146      * <p>
147      * Destroy user test object.
148      * </p>
149      */
150     protected void destroyUser() throws Exception
151     {
152         ums.removeUser("testcred");
153     }
154     
155     protected String[] getConfigurations()
156     {
157         String[] confs = super.getConfigurations();
158         List confList = new ArrayList(Arrays.asList(confs));
159         confList.add("JETSPEED-INF/spring/TestPasswordCredentialProvider.xml");
160         return (String[]) confList.toArray(new String[1]);
161     }    
162 }