1   package org.apache.turbine.services.crypto;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.fulcrum.crypto.CryptoAlgorithm;
23  import org.apache.fulcrum.crypto.CryptoService;
24  import org.apache.turbine.services.ServiceManager;
25  import org.apache.turbine.services.TurbineServices;
26  import org.apache.turbine.test.BaseTestCase;
27  import org.apache.turbine.util.TurbineConfig;
28  
29  /**
30   * Verifies the Fulcrum Crypto Service works properly in Turbine.
31   *
32   * @author <a href="mailto:epugh@upstate.com">Eric Pugh</a>
33   * @author <a href="mailto:Rafal.Krzewski@e-point.pl">Rafal Krzewski</a>
34   * @author <a href="mailto:sgoeschl@apache.org">Siegfried Goeschl</a>
35   * @version $Id: CryptoRunningInECMTest.java 222043 2004-12-06 17:47:33Z painter $
36   */
37  public class FulcrumCryptoServiceTest extends BaseTestCase
38  {
39      private static final String preDefinedInput = "Oeltanks";
40      private static TurbineConfig tc = null;
41      private CryptoService cryptoService;
42  
43      public FulcrumCryptoServiceTest(String name) throws Exception
44      {
45          super(name);
46      }
47  
48      public void testMd5()
49      {
50          String preDefinedResult = "XSop0mncK19Ii2r2CUe29w==";
51  
52          try
53          {
54              CryptoAlgorithm ca =cryptoService.getCryptoAlgorithm("default");
55              ca.setCipher("MD5");
56              String output = ca.encrypt(preDefinedInput);
57              assertEquals("MD5 Encryption failed ", preDefinedResult, output);
58          }
59          catch (Exception e)
60          {
61              e.printStackTrace();
62              fail();
63          }
64      }
65  
66      public void testSha1()
67      {
68          String preDefinedResult = "uVDiJHaavRYX8oWt5ctkaa7j1cw=";
69  
70          try
71          {
72              CryptoAlgorithm ca = cryptoService.getCryptoAlgorithm("default");
73              ca.setCipher("SHA1");
74              String output = ca.encrypt(preDefinedInput);
75              assertEquals("SHA1 Encryption failed ", preDefinedResult, output);
76          }
77          catch (Exception e)
78          {
79              e.printStackTrace();
80              fail();
81          }
82      }
83  
84      public void setUp() throws Exception
85      {
86          tc = new TurbineConfig(".", "/conf/test/TestFulcrumComponents.properties");
87          tc.initialize();
88          ServiceManager serviceManager = TurbineServices.getInstance();
89          cryptoService = (CryptoService) serviceManager.getService(CryptoService.ROLE);
90      }
91  
92      public void tearDown() throws Exception
93      {
94          if (tc != null)
95          {
96              tc.dispose();
97          }
98      }
99  }