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