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.util;
017:
018: import junit.framework.TestCase;
019:
020: import org.acegisecurity.util.EncryptionUtils.EncryptionException;
021:
022: /**
023: * JUnit tests for EncryptionUtils.
024: *
025: * @author Alan Stewart
026: * @author Ben Alex
027: * @version $Id: EncryptionUtilsTests.java 1784 2007-02-24 21:00:24Z luke_t $
028: */
029: public class EncryptionUtilsTests extends TestCase {
030: private final static String STRING_TO_ENCRYPT = "Alan K Stewart";
031: private final static String ENCRYPTION_KEY = "123456789012345678901234567890";
032:
033: public void testEncryptsUsingDESEde() throws EncryptionException {
034: final String encryptedString = EncryptionUtils.encrypt(
035: ENCRYPTION_KEY, STRING_TO_ENCRYPT);
036: assertEquals("3YIE8sIbaEoqGZZrHamFGQ==", encryptedString);
037: }
038:
039: public void testEncryptByteArrayUsingDESEde() {
040: final byte[] encryptedArray = EncryptionUtils.encrypt(
041: ENCRYPTION_KEY, EncryptionUtils
042: .stringToByteArray(STRING_TO_ENCRYPT));
043: assertEquals("3YIE8sIbaEoqGZZrHamFGQ==", EncryptionUtils
044: .byteArrayToString(encryptedArray));
045: }
046:
047: public void testEncryptionKeyCanContainLetters()
048: throws EncryptionException {
049: final String encryptedString = EncryptionUtils
050: .encrypt("ASDF asdf 1234 8983 jklasdf J2Jaf8",
051: STRING_TO_ENCRYPT);
052: assertEquals("v4+DQoClx6qm5tJwBcRrkw==", encryptedString);
053: }
054:
055: public void testDecryptsUsingDESEde() throws EncryptionException {
056: final String encryptedString = "3YIE8sIbaEoqGZZrHamFGQ==";
057: final String decryptedString = EncryptionUtils.decrypt(
058: ENCRYPTION_KEY, encryptedString);
059: assertEquals(STRING_TO_ENCRYPT, decryptedString);
060: }
061:
062: public void testDecryptByteArrayUsingDESEde() {
063: final byte[] encrypted = EncryptionUtils
064: .stringToByteArray("3YIE8sIbaEoqGZZrHamFGQ==");
065: final byte[] decrypted = EncryptionUtils.decrypt(
066: ENCRYPTION_KEY, encrypted);
067: assertEquals(STRING_TO_ENCRYPT, EncryptionUtils
068: .byteArrayToString(decrypted));
069: }
070:
071: public void testFailEncryptWithNullEncryptionKey() {
072: try {
073: EncryptionUtils.encrypt(null, STRING_TO_ENCRYPT);
074: fail();
075: } catch (IllegalArgumentException e) {
076: assertTrue(true);
077: }
078: }
079:
080: public void testFailEncryptWithEmptyEncryptionKey() {
081: try {
082: EncryptionUtils.encrypt("", STRING_TO_ENCRYPT);
083: fail();
084: } catch (IllegalArgumentException e) {
085: assertTrue(true);
086: }
087: }
088:
089: public void teastFailEncryptWithShortEncryptionKey() {
090: try {
091: EncryptionUtils.encrypt("01234567890123456789012",
092: STRING_TO_ENCRYPT);
093: fail();
094: } catch (IllegalArgumentException e) {
095: assertTrue(true);
096: }
097: }
098:
099: public void testFailDecryptWithEmptyString() {
100: try {
101: EncryptionUtils.decrypt(ENCRYPTION_KEY, "");
102: fail();
103: } catch (IllegalArgumentException e) {
104: assertTrue(true);
105: }
106: }
107:
108: public void testFailEncryptWithEmptyString() {
109: try {
110: EncryptionUtils.encrypt(ENCRYPTION_KEY, "");
111: fail();
112: } catch (IllegalArgumentException e) {
113: assertTrue(true);
114: }
115: }
116:
117: public void testFailEncryptWithNullString() {
118: try {
119: EncryptionUtils.encrypt(ENCRYPTION_KEY, (String) null);
120: fail();
121: } catch (IllegalArgumentException e) {
122: assertTrue(true);
123: }
124: }
125:
126: public void testEncryptAndDecrypt() throws EncryptionException {
127: final String stringToEncrypt = "Alan Stewart";
128: final String encryptedString = EncryptionUtils.encrypt(
129: ENCRYPTION_KEY, stringToEncrypt);
130: final String decryptedString = EncryptionUtils.decrypt(
131: ENCRYPTION_KEY, encryptedString);
132: assertEquals(stringToEncrypt, decryptedString);
133: }
134: }
|