001: /*
002: * Copyright 2005-2007 The Kuali Foundation.
003: *
004: *
005: * Licensed under the Educational Community License, Version 1.0 (the "License");
006: * you may not use this file except in compliance with the License.
007: * You may obtain a copy of the License at
008: *
009: * http://www.opensource.org/licenses/ecl1.php
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package edu.iu.uis.eden.security;
018:
019: import java.util.ArrayList;
020: import java.util.List;
021:
022: import junit.framework.TestCase;
023:
024: import org.kuali.rice.config.SimpleConfig;
025: import org.kuali.rice.core.Core;
026:
027: /**
028: * Tests the DESEncryptionService.
029: *
030: * @author Eric Westfall
031: */
032: public class DESEncryptionServiceTest extends TestCase {
033:
034: private static boolean failed = false;
035:
036: /**
037: * Verfies that the DESEncryptionService is thread-safe. We had problems originally with the
038: * thread-safety of the implementation so we added this test to verify and prevent regression.
039: */
040: public void testEncryptionMultiThreaded() throws Exception {
041: String key = DESEncryptionService.generateEncodedKey();
042: SimpleConfig config = new SimpleConfig();
043: config.getProperties().put("encryption.key", key);
044: Core.init(config);
045: final EncryptionService service = new DESEncryptionService();
046: List<Thread> threads = new ArrayList<Thread>();
047: failed = false;
048: for (int i = 0; i < 10; i++) {
049: threads.add(new Thread() {
050: public void run() {
051: try {
052: for (int j = 0; j < 100; j++) {
053: String badText = "This is so going to no longer explode";
054: String badEnc = service.encrypt(badText);
055: String badDec = service.decrypt(badEnc);
056: assertEquals(badText, badDec);
057: }
058: } catch (Exception e) {
059: e.printStackTrace();
060: failed = true;
061: fail("Encryption service use to be non-thread safe, but it should be now!");
062: }
063: }
064: });
065: }
066: for (Thread thread : threads) {
067: thread.start();
068: }
069: for (Thread thread : threads) {
070: thread.join();
071: }
072: // assert that the encryption doesn't fail any longer in a multi-threaded environment, this verifies
073: // the fix to the encryption service
074: assertFalse(failed);
075: }
076:
077: /**
078: * Similar to the test above except that a new DESEncryptionService is created for each thread.
079: */
080: public void testEncryptionMultiThreadedSafe() throws Exception {
081: String key = DESEncryptionService.generateEncodedKey();
082: SimpleConfig config = new SimpleConfig();
083: config.getProperties().put("encryption.key", key);
084: Core.init(config);
085: List<Thread> threads = new ArrayList<Thread>();
086: failed = false;
087: for (int i = 0; i < 10; i++) {
088: threads.add(new Thread() {
089: public void run() {
090: try {
091: final EncryptionService service = new DESEncryptionService();
092: for (int j = 0; j < 100; j++) {
093: String badText = "This is so going to NOT explode";
094: String badEnc = service.encrypt(badText);
095: String badDec = service.decrypt(badEnc);
096: assertEquals(badText, badDec);
097: }
098: } catch (Exception e) {
099: e.printStackTrace();
100: e.printStackTrace();
101: failed = true;
102: fail("Encryption service failed in mysterious ways.");
103: }
104: }
105: });
106: }
107: for (Thread thread : threads) {
108: thread.start();
109: }
110: for (Thread thread : threads) {
111: thread.join();
112: }
113: // assert that the encryption/decription did not fail
114: assertFalse(failed);
115: }
116:
117: }
|