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.om.InternalUserPrincipal;
26  import org.apache.jetspeed.security.om.impl.InternalCredentialImpl;
27  import org.apache.jetspeed.security.spi.impl.DefaultPasswordCredentialImpl;
28  import org.apache.jetspeed.security.util.test.AbstractSecurityTestcase;
29  
30  import junit.framework.Test;
31  import junit.framework.TestSuite;
32  
33  /***
34  * <p>
35   * TestDefaultInternalPasswordCredentialIntercepto
36   * </p>
37   * 
38   * @author <a href="mailto:ate@apache.org">Ate Douma</a>
39   * @version $Id: TestCredentialPasswordEncoder.java 516448 2007-03-09 16:25:47Z ate $
40   */
41  public class TestCredentialPasswordEncoder extends AbstractSecurityTestcase
42  {
43      protected void setUp() throws Exception
44      {
45          super.setUp(); 
46          // cleanup for previously failed test
47          destroyUser();
48          initUser();
49      }
50  
51      public void tearDown() throws Exception
52      {
53          destroyUser();
54          super.tearDown();
55      }
56  
57      public static Test suite()
58      {
59          return new TestSuite(TestCredentialPasswordEncoder.class);
60      }
61  
62      public void testEncodedPassword() throws Exception
63      {
64          Set privateCredentials = ums.getUser("testcred").getSubject().getPrivateCredentials();
65          assertNotNull(privateCredentials);
66          assertEquals(1, privateCredentials.size());
67          PasswordCredential[] pwdCreds = (PasswordCredential[]) privateCredentials.toArray(new PasswordCredential[0]);
68          assertEquals("testcred", pwdCreds[0].getUserName());
69          assertNotSame("Password should be not same (encoded)", "password", new String(pwdCreds[0].getPassword()));
70      }
71  
72      protected void initUser() throws Exception
73      {
74          // create user without password
75          ums.addUser("testcred", null);
76          // add a non-encoded password credential directly 
77          InternalUserPrincipal internalUser = securityAccess.getInternalUserPrincipal("testcred");
78          ArrayList credentials = new ArrayList();
79          InternalCredentialImpl credential = 
80              new InternalCredentialImpl(internalUser.getPrincipalId(),
81                      "password", 0, DefaultPasswordCredentialImpl.class.getName());
82          credentials.add(credential);
83          internalUser.setCredentials(credentials);
84          securityAccess.setInternalUserPrincipal(internalUser,false);
85      }
86  
87      protected void destroyUser() throws Exception
88      {
89          ums.removeUser("testcred");
90      }
91      
92      protected String[] getConfigurations()
93      {
94          String[] confs = super.getConfigurations();
95          List confList = new ArrayList(Arrays.asList(confs));
96          confList.add("JETSPEED-INF/spring/TestCredentialPasswordEncoder.xml");
97          return (String[])confList.toArray(new String[1]);
98      }    
99  }