01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * 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
42: AbstractSecurityTestcase {
43: protected void setUp() throws Exception {
44: super .setUp();
45: // cleanup for previously failed test
46: destroyUser();
47: initUser();
48: }
49:
50: public void tearDown() throws Exception {
51: destroyUser();
52: super .tearDown();
53: }
54:
55: public static Test suite() {
56: return new TestSuite(TestCredentialPasswordEncoder.class);
57: }
58:
59: public void testEncodedPassword() throws Exception {
60: Set privateCredentials = ums.getUser("testcred").getSubject()
61: .getPrivateCredentials();
62: assertNotNull(privateCredentials);
63: assertEquals(1, privateCredentials.size());
64: PasswordCredential[] pwdCreds = (PasswordCredential[]) privateCredentials
65: .toArray(new PasswordCredential[0]);
66: assertEquals("testcred", pwdCreds[0].getUserName());
67: assertNotSame("Password should be not same (encoded)",
68: "password", new String(pwdCreds[0].getPassword()));
69: }
70:
71: protected void initUser() throws Exception {
72: // create user without password
73: ums.addUser("testcred", null);
74: // add a non-encoded password credential directly
75: InternalUserPrincipal internalUser = securityAccess
76: .getInternalUserPrincipal("testcred");
77: ArrayList credentials = new ArrayList();
78: InternalCredentialImpl credential = new InternalCredentialImpl(
79: internalUser.getPrincipalId(), "password", 0,
80: DefaultPasswordCredentialImpl.class.getName());
81: credentials.add(credential);
82: internalUser.setCredentials(credentials);
83: securityAccess.setInternalUserPrincipal(internalUser, false);
84: }
85:
86: protected void destroyUser() throws Exception {
87: ums.removeUser("testcred");
88: }
89:
90: protected String[] getConfigurations() {
91: String[] confs = super .getConfigurations();
92: List confList = new ArrayList(Arrays.asList(confs));
93: confList
94: .add("JETSPEED-INF/spring/TestCredentialPasswordEncoder.xml");
95: return (String[]) confList.toArray(new String[1]);
96: }
97: }
|