01: /* Copyright 2004, 2005, 2006 Acegi Technology Pty Limited
02: *
03: * Licensed under the Apache License, Version 2.0 (the "License");
04: * you may not use this file except in compliance with the License.
05: * You may obtain a copy of the License at
06: *
07: * http://www.apache.org/licenses/LICENSE-2.0
08: *
09: * Unless required by applicable law or agreed to in writing, software
10: * distributed under the License is distributed on an "AS IS" BASIS,
11: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12: * See the License for the specific language governing permissions and
13: * limitations under the License.
14: */
15:
16: package org.acegisecurity.providers.encoding;
17:
18: import junit.framework.TestCase;
19:
20: /**
21: * <p>TestCase for PlaintextPasswordEncoder.</p>
22: *
23: * @author colin sampaleanu
24: * @author Ben Alex
25: * @version $Id: PlaintextPasswordEncoderTests.java 1496 2006-05-23 13:38:33Z benalex $
26: */
27: public class PlaintextPasswordEncoderTests extends TestCase {
28: //~ Methods ========================================================================================================
29:
30: public void testBasicFunctionality() {
31: PlaintextPasswordEncoder pe = new PlaintextPasswordEncoder();
32:
33: String raw = "abc123";
34: String rawDiffCase = "AbC123";
35: String badRaw = "abc321";
36: String salt = "THIS_IS_A_SALT";
37:
38: String encoded = pe.encodePassword(raw, salt);
39: assertEquals("abc123{THIS_IS_A_SALT}", encoded);
40: assertTrue(pe.isPasswordValid(encoded, raw, salt));
41: assertFalse(pe.isPasswordValid(encoded, badRaw, salt));
42:
43: // make sure default is not to ignore password case
44: assertFalse(pe.isIgnorePasswordCase());
45: encoded = pe.encodePassword(rawDiffCase, salt);
46: assertFalse(pe.isPasswordValid(encoded, raw, salt));
47:
48: // now check for ignore password case
49: pe = new PlaintextPasswordEncoder();
50: pe.setIgnorePasswordCase(true);
51:
52: // should be able to validate even without encoding
53: encoded = pe.encodePassword(rawDiffCase, salt);
54: assertTrue(pe.isPasswordValid(encoded, raw, salt));
55: assertFalse(pe.isPasswordValid(encoded, badRaw, salt));
56: }
57:
58: public void testMergeDemerge() {
59: PlaintextPasswordEncoder pwd = new PlaintextPasswordEncoder();
60:
61: String merged = pwd.encodePassword("password", "foo");
62: String[] demerged = pwd.obtainPasswordAndSalt(merged);
63: assertEquals("password", demerged[0]);
64: assertEquals("foo", demerged[1]);
65: }
66: }
|