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 ShaPasswordEncoder.</p>
22: *
23: * @author colin sampaleanu
24: * @author Ben Alex
25: * @author Ray Krueger
26: * @version $Id: ShaPasswordEncoderTests.java 1527 2006-05-31 03:03:18Z raykrueger $
27: */
28: public class ShaPasswordEncoderTests extends TestCase {
29: //~ Methods ========================================================================================================
30:
31: public void testBasicFunctionality() {
32: ShaPasswordEncoder pe = new ShaPasswordEncoder();
33: String raw = "abc123";
34: String badRaw = "abc321";
35: String salt = "THIS_IS_A_SALT";
36: String encoded = pe.encodePassword(raw, salt);
37: assertTrue(pe.isPasswordValid(encoded, raw, salt));
38: assertFalse(pe.isPasswordValid(encoded, badRaw, salt));
39: assertEquals("b2f50ffcbd3407fe9415c062d55f54731f340d32",
40: encoded);
41:
42: }
43:
44: public void testBase64() throws Exception {
45: ShaPasswordEncoder pe = new ShaPasswordEncoder();
46: pe.setEncodeHashAsBase64(true);
47: String raw = "abc123";
48: String badRaw = "abc321";
49: String salt = "THIS_IS_A_SALT";
50: String encoded = pe.encodePassword(raw, salt);
51: assertTrue(pe.isPasswordValid(encoded, raw, salt));
52: assertFalse(pe.isPasswordValid(encoded, badRaw, salt));
53: assertTrue(encoded.length() != 40);
54: }
55:
56: public void test256() throws Exception {
57: ShaPasswordEncoder pe = new ShaPasswordEncoder(256);
58: String encoded = pe.encodePassword("abc123", null);
59: assertEquals(
60: "6ca13d52ca70c883e0f0bb101e425a89e8624de51db2d2392593af6a84118090",
61: encoded);
62: String encodedWithSalt = pe.encodePassword("abc123",
63: "THIS_IS_A_SALT");
64: assertEquals(
65: "4b79b7de23eb23b78cc5ede227d532b8a51f89b2ec166f808af76b0dbedc47d7",
66: encodedWithSalt);
67: }
68:
69: public void testInvalidStrength() throws Exception {
70: try {
71: new ShaPasswordEncoder(666);
72: fail("IllegalArgumentException expected");
73: } catch (IllegalArgumentException e) {
74: //expected
75: }
76: }
77: }
|