    001package 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
    022import org.apache.fulcrum.crypto.CryptoAlgorithm;
    023import org.apache.fulcrum.crypto.CryptoService;
    024import org.apache.turbine.services.ServiceManager;
    025import org.apache.turbine.services.TurbineServices;
    026import org.apache.turbine.test.BaseTestCase;
    027import org.apache.turbine.util.TurbineConfig;
    028import org.junit.AfterClass;
    029import org.junit.Before;
    030import org.junit.BeforeClass;
    031import org.junit.Test;
    032
    033import static org.junit.Assert.*;
    034
    035/**
    036 * Verifies the Fulcrum Crypto Service works properly in Turbine.
    037 *
    038 * @author <a href="mailto:epugh@upstate.com">Eric Pugh</a>
    039 * @author <a href="mailto:Rafal.Krzewski@e-point.pl">Rafal Krzewski</a>
    040 * @author <a href="mailto:sgoeschl@apache.org">Siegfried Goeschl</a>
    041 * @version $Id: CryptoRunningInECMTest.java 222043 2004-12-06 17:47:33Z painter $
    042 */
    043public class FulcrumCryptoServiceTest extends BaseTestCase
    044{
    045    private static final String preDefinedInput = "Oeltanks";
    046    private static TurbineConfig tc = null;
    047    private CryptoService cryptoService;
    048
    049
    050
    051    @Test public void testMd5()
    052    {
    053        String preDefinedResult = "XSop0mncK19Ii2r2CUe29w==";
    054
    055        try
    056        {
    057            CryptoAlgorithm ca =cryptoService.getCryptoAlgorithm("default");
    058            ca.setCipher("MD5");
    059            String output = ca.encrypt(preDefinedInput);
    060            assertEquals("MD5 Encryption failed ", preDefinedResult, output);
    061        }
    062        catch (Exception e)
    063        {
    064            e.printStackTrace();
    065            fail();
    066        }
    067    }
    068
    069    @Test public void testSha1()
    070    {
    071        String preDefinedResult = "uVDiJHaavRYX8oWt5ctkaa7j1cw=";
    072
    073        try
    074        {
    075            CryptoAlgorithm ca = cryptoService.getCryptoAlgorithm("default");
    076            ca.setCipher("SHA1");
    077            String output = ca.encrypt(preDefinedInput);
    078            assertEquals("SHA1 Encryption failed ", preDefinedResult, output);
    079        }
    080        catch (Exception e)
    081        {
    082            e.printStackTrace();
    083            fail();
    084        }
    085    }
    086
    087    @BeforeClass
    088    public static void init() throws Exception
    089    {
    090        tc = new TurbineConfig(".", "/conf/test/TestFulcrumComponents.properties");
    091        tc.initialize();
    092
    093    }
    094
    095    @Before
    096    public void setUpBefore() throws Exception
    097    {
    098        ServiceManager serviceManager = TurbineServices.getInstance();
    099        cryptoService = (CryptoService) serviceManager.getService(CryptoService.ROLE);
    100    }
    101
    102    @AfterClass
    103    public static void tearDown() throws Exception
    104    {
    105        if (tc != null)
    106        {
    107            tc.dispose();
    108        }
    109    }
    110}