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.factory.FactoryService;
031: import org.apache.turbine.services.factory.TurbineFactoryService;
032: import org.apache.turbine.test.BaseTestCase;
033:
034: public class CryptoDefaultTest extends BaseTestCase {
035: private static final String PREFIX = "services."
036: + CryptoService.SERVICE_NAME + '.';
037:
038: private static final String preDefinedInput = "Oeltanks";
039:
040: public CryptoDefaultTest(String name) throws Exception {
041: super (name);
042:
043: ServiceManager serviceManager = TurbineServices.getInstance();
044: serviceManager.setApplicationRoot(".");
045:
046: Configuration cfg = new BaseConfiguration();
047: cfg.setProperty(PREFIX + "classname",
048: TurbineCryptoService.class.getName());
049:
050: /* No providers configured. Should be "java" then */
051:
052: /* Ugh */
053:
054: cfg.setProperty("services." + FactoryService.SERVICE_NAME
055: + ".classname", TurbineFactoryService.class.getName());
056:
057: serviceManager.setConfiguration(cfg);
058:
059: try {
060: serviceManager.init();
061: } catch (Exception e) {
062: e.printStackTrace();
063: fail();
064: }
065: }
066:
067: public static Test suite() {
068: return new TestSuite(CryptoDefaultTest.class);
069: }
070:
071: public void testMd5() {
072: String preDefinedResult = "XSop0mncK19Ii2r2CUe29w==";
073:
074: try {
075: CryptoAlgorithm ca = TurbineCrypto
076: .getCryptoAlgorithm("default");
077:
078: ca.setCipher("MD5");
079:
080: String output = ca.encrypt(preDefinedInput);
081:
082: assertEquals("MD5 Encryption failed ", preDefinedResult,
083: output);
084:
085: } catch (Exception e) {
086: e.printStackTrace();
087: fail();
088: }
089: }
090:
091: public void testSha1() {
092: String preDefinedResult = "uVDiJHaavRYX8oWt5ctkaa7j1cw=";
093:
094: try {
095: CryptoAlgorithm ca = TurbineCrypto
096: .getCryptoAlgorithm("default");
097:
098: ca.setCipher("SHA1");
099:
100: String output = ca.encrypt(preDefinedInput);
101:
102: assertEquals("SHA1 Encryption failed ", preDefinedResult,
103: output);
104:
105: } catch (Exception e) {
106: e.printStackTrace();
107: fail();
108: }
109: }
110: }
|