001: /* Copyright 2004, 2005, 2006 Acegi Technology Pty Limited
002: *
003: * Licensed under the Apache License, Version 2.0 (the "License");
004: * you may not use this file except in compliance with the License.
005: * You may obtain a copy of the License at
006: *
007: * http://www.apache.org/licenses/LICENSE-2.0
008: *
009: * Unless required by applicable law or agreed to in writing, software
010: * distributed under the License is distributed on an "AS IS" BASIS,
011: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: * See the License for the specific language governing permissions and
013: * limitations under the License.
014: */
015:
016: package org.acegisecurity.providers.encoding;
017:
018: import junit.framework.TestCase;
019:
020: import org.springframework.dao.DataAccessException;
021:
022: /**
023: * <p>TestCase for BasePasswordEncoder.</p>
024: *
025: * @author Ben Alex
026: * @version $Id: BasePasswordEncoderTests.java 1496 2006-05-23 13:38:33Z benalex $
027: */
028: public class BasePasswordEncoderTests extends TestCase {
029: //~ Methods ========================================================================================================
030:
031: public void testDemergeHandlesEmptyAndNullSalts() {
032: MockPasswordEncoder pwd = new MockPasswordEncoder();
033:
034: String merged = pwd.nowMergePasswordAndSalt("password", null,
035: true);
036:
037: String[] demerged = pwd.nowDemergePasswordAndSalt(merged);
038: assertEquals("password", demerged[0]);
039: assertEquals("", demerged[1]);
040:
041: merged = pwd.nowMergePasswordAndSalt("password", "", true);
042:
043: demerged = pwd.nowDemergePasswordAndSalt(merged);
044: assertEquals("password", demerged[0]);
045: assertEquals("", demerged[1]);
046: }
047:
048: public void testDemergeWithEmptyStringIsRejected() {
049: MockPasswordEncoder pwd = new MockPasswordEncoder();
050:
051: try {
052: pwd.nowDemergePasswordAndSalt("");
053: fail("Should have thrown IllegalArgumentException");
054: } catch (IllegalArgumentException expected) {
055: assertEquals("Cannot pass a null or empty String", expected
056: .getMessage());
057: }
058: }
059:
060: public void testDemergeWithNullIsRejected() {
061: MockPasswordEncoder pwd = new MockPasswordEncoder();
062:
063: try {
064: pwd.nowDemergePasswordAndSalt(null);
065: fail("Should have thrown IllegalArgumentException");
066: } catch (IllegalArgumentException expected) {
067: assertEquals("Cannot pass a null or empty String", expected
068: .getMessage());
069: }
070: }
071:
072: public void testMergeDemerge() {
073: MockPasswordEncoder pwd = new MockPasswordEncoder();
074:
075: String merged = pwd.nowMergePasswordAndSalt("password", "foo",
076: true);
077: assertEquals("password{foo}", merged);
078:
079: String[] demerged = pwd.nowDemergePasswordAndSalt(merged);
080: assertEquals("password", demerged[0]);
081: assertEquals("foo", demerged[1]);
082: }
083:
084: public void testMergeDemergeWithDelimitersInPassword() {
085: MockPasswordEncoder pwd = new MockPasswordEncoder();
086:
087: String merged = pwd.nowMergePasswordAndSalt("p{ass{w{o}rd",
088: "foo", true);
089: assertEquals("p{ass{w{o}rd{foo}", merged);
090:
091: String[] demerged = pwd.nowDemergePasswordAndSalt(merged);
092:
093: assertEquals("p{ass{w{o}rd", demerged[0]);
094: assertEquals("foo", demerged[1]);
095: }
096:
097: public void testMergeDemergeWithNullAsPassword() {
098: MockPasswordEncoder pwd = new MockPasswordEncoder();
099:
100: String merged = pwd.nowMergePasswordAndSalt(null, "foo", true);
101: assertEquals("{foo}", merged);
102:
103: String[] demerged = pwd.nowDemergePasswordAndSalt(merged);
104: assertEquals("", demerged[0]);
105: assertEquals("foo", demerged[1]);
106: }
107:
108: public void testStrictMergeRejectsDelimitersInSalt1() {
109: MockPasswordEncoder pwd = new MockPasswordEncoder();
110:
111: try {
112: pwd.nowMergePasswordAndSalt("password", "f{oo", true);
113: fail("Should have thrown IllegalArgumentException");
114: } catch (IllegalArgumentException expected) {
115: assertEquals("Cannot use { or } in salt.toString()",
116: expected.getMessage());
117: }
118: }
119:
120: public void testStrictMergeRejectsDelimitersInSalt2() {
121: MockPasswordEncoder pwd = new MockPasswordEncoder();
122:
123: try {
124: pwd.nowMergePasswordAndSalt("password", "f}oo", true);
125: fail("Should have thrown IllegalArgumentException");
126: } catch (IllegalArgumentException expected) {
127: assertEquals("Cannot use { or } in salt.toString()",
128: expected.getMessage());
129: }
130: }
131:
132: //~ Inner Classes ==================================================================================================
133:
134: private class MockPasswordEncoder extends BasePasswordEncoder {
135: public String encodePassword(String rawPass, Object salt)
136: throws DataAccessException {
137: throw new UnsupportedOperationException(
138: "mock method not implemented");
139: }
140:
141: public boolean isPasswordValid(String encPass, String rawPass,
142: Object salt) throws DataAccessException {
143: throw new UnsupportedOperationException(
144: "mock method not implemented");
145: }
146:
147: public String[] nowDemergePasswordAndSalt(String password) {
148: return demergePasswordAndSalt(password);
149: }
150:
151: public String nowMergePasswordAndSalt(String password,
152: Object salt, boolean strict) {
153: return mergePasswordAndSalt(password, salt, strict);
154: }
155: }
156: }
|