001: package org.apache.turbine.services.crypto;
002:
003: /*
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021:
022: import junit.framework.Test;
023: import junit.framework.TestSuite;
024:
025: import org.apache.commons.configuration.BaseConfiguration;
026: import org.apache.commons.configuration.Configuration;
027:
028: import org.apache.turbine.services.ServiceManager;
029: import org.apache.turbine.services.TurbineServices;
030: import org.apache.turbine.services.crypto.provider.ClearCrypt;
031: import org.apache.turbine.services.crypto.provider.JavaCrypt;
032: import org.apache.turbine.services.crypto.provider.OldJavaCrypt;
033: import org.apache.turbine.services.crypto.provider.UnixCrypt;
034: import org.apache.turbine.services.factory.FactoryService;
035: import org.apache.turbine.services.factory.TurbineFactoryService;
036: import org.apache.turbine.test.BaseTestCase;
037:
038: public class CryptoTest extends BaseTestCase {
039: private static final String PREFIX = "services."
040: + CryptoService.SERVICE_NAME + '.';
041:
042: private static final String preDefinedInput = "Oeltanks";
043:
044: public CryptoTest(String name) throws Exception {
045: super (name);
046:
047: ServiceManager serviceManager = TurbineServices.getInstance();
048: serviceManager.setApplicationRoot(".");
049:
050: Configuration cfg = new BaseConfiguration();
051: cfg.setProperty(PREFIX + "classname",
052: TurbineCryptoService.class.getName());
053:
054: cfg.setProperty(PREFIX + "algorithm.unix", UnixCrypt.class
055: .getName());
056: cfg.setProperty(PREFIX + "algorithm.clear", ClearCrypt.class
057: .getName());
058: cfg.setProperty(PREFIX + "algorithm.java", JavaCrypt.class
059: .getName());
060: cfg.setProperty(PREFIX + "algorithm.oldjava",
061: OldJavaCrypt.class.getName());
062:
063: /* Do _not_ configure a default! We want to test explicitly */
064:
065: cfg.setProperty(PREFIX + "algorithm.default", "none");
066:
067: /* Ugh */
068:
069: cfg.setProperty("services." + FactoryService.SERVICE_NAME
070: + ".classname", TurbineFactoryService.class.getName());
071:
072: serviceManager.setConfiguration(cfg);
073:
074: try {
075: serviceManager.init();
076: } catch (Exception e) {
077: e.printStackTrace();
078: fail();
079: }
080: }
081:
082: public static Test suite() {
083: return new TestSuite(CryptoTest.class);
084: }
085:
086: public void testUnixCrypt() {
087: String preDefinedSeed = "z5";
088: String preDefinedResult = "z5EQaXpuu059c";
089:
090: try {
091: CryptoAlgorithm ca = TurbineCrypto
092: .getCryptoAlgorithm("unix");
093:
094: /*
095: * Test predefined Seed
096: */
097:
098: ca.setSeed(preDefinedSeed);
099:
100: String output = ca.encrypt(preDefinedInput);
101:
102: assertEquals("Encryption failed ", preDefinedResult, output);
103:
104: /*
105: * Test random Seed
106: *
107: */
108:
109: ca.setSeed(null);
110:
111: String result = ca.encrypt(preDefinedInput);
112:
113: ca.setSeed(result);
114:
115: output = ca.encrypt(preDefinedInput);
116:
117: assertEquals("Encryption failed ", output, result);
118: } catch (Exception e) {
119: e.printStackTrace();
120: fail();
121: }
122: }
123:
124: public void testClearCrypt() {
125: String preDefinedResult = "Oeltanks";
126:
127: try {
128: CryptoAlgorithm ca = TurbineCrypto
129: .getCryptoAlgorithm("clear");
130: String output = ca.encrypt(preDefinedInput);
131:
132: assertEquals("Encryption failed ", preDefinedResult, output);
133:
134: } catch (Exception e) {
135: e.printStackTrace();
136: fail();
137: }
138: }
139:
140: public void testOldJavaCryptMd5() {
141: String preDefinedResult = "XSop0mncK19Ii2r2CUe2";
142:
143: try {
144: CryptoAlgorithm ca = TurbineCrypto
145: .getCryptoAlgorithm("oldjava");
146:
147: ca.setCipher("MD5");
148:
149: String output = ca.encrypt(preDefinedInput);
150:
151: assertEquals("MD5 Encryption failed ", preDefinedResult,
152: output);
153:
154: } catch (Exception e) {
155: e.printStackTrace();
156: fail();
157: }
158: }
159:
160: public void testOldJavaCryptSha1() {
161: String preDefinedResult = "uVDiJHaavRYX8oWt5ctkaa7j";
162:
163: try {
164: CryptoAlgorithm ca = TurbineCrypto
165: .getCryptoAlgorithm("oldjava");
166:
167: ca.setCipher("SHA1");
168:
169: String output = ca.encrypt(preDefinedInput);
170:
171: assertEquals("SHA1 Encryption failed ", preDefinedResult,
172: output);
173:
174: } catch (Exception e) {
175: e.printStackTrace();
176: fail();
177: }
178: }
179:
180: public void testJavaCryptMd5() {
181: String preDefinedResult = "XSop0mncK19Ii2r2CUe29w==";
182:
183: try {
184: CryptoAlgorithm ca = TurbineCrypto
185: .getCryptoAlgorithm("java");
186:
187: ca.setCipher("MD5");
188:
189: String output = ca.encrypt(preDefinedInput);
190:
191: assertEquals("MD5 Encryption failed ", preDefinedResult,
192: output);
193:
194: } catch (Exception e) {
195: e.printStackTrace();
196: fail();
197: }
198: }
199:
200: public void testJavaCryptSha1() {
201: String preDefinedResult = "uVDiJHaavRYX8oWt5ctkaa7j1cw=";
202:
203: try {
204: CryptoAlgorithm ca = TurbineCrypto
205: .getCryptoAlgorithm("java");
206:
207: ca.setCipher("SHA1");
208:
209: String output = ca.encrypt(preDefinedInput);
210:
211: assertEquals("SHA1 Encryption failed ", preDefinedResult,
212: output);
213:
214: } catch (Exception e) {
215: e.printStackTrace();
216: fail();
217: }
218: }
219:
220: }
|