1   package org.apache.turbine.services.crypto;
2   
3   /*
4    * Copyright 2001-2005 The Apache Software Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License")
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  import junit.framework.Test;
20  import junit.framework.TestSuite;
21  
22  import org.apache.commons.configuration.BaseConfiguration;
23  import org.apache.commons.configuration.Configuration;
24  
25  import org.apache.turbine.services.ServiceManager;
26  import org.apache.turbine.services.TurbineServices;
27  import org.apache.turbine.services.factory.FactoryService;
28  import org.apache.turbine.services.factory.TurbineFactoryService;
29  import org.apache.turbine.test.BaseTestCase;
30  
31  public class CryptoDefaultTest
32      extends BaseTestCase
33  {
34      private static final String PREFIX = "services." +
35          CryptoService.SERVICE_NAME + '.';
36  
37      private static final String preDefinedInput = "Oeltanks";
38  
39      public CryptoDefaultTest(String name)
40              throws Exception
41      {
42          super(name);
43  
44          ServiceManager serviceManager = TurbineServices.getInstance();
45          serviceManager.setApplicationRoot(".");
46  
47          Configuration cfg = new BaseConfiguration();
48          cfg.setProperty(PREFIX + "classname",
49                          TurbineCryptoService.class.getName());
50  
51          /* No providers configured. Should be "java" then */
52  
53          /* Ugh */
54  
55          cfg.setProperty("services." + FactoryService.SERVICE_NAME + ".classname",
56                          TurbineFactoryService.class.getName());
57  
58          serviceManager.setConfiguration(cfg);
59  
60          try
61          {
62              serviceManager.init();
63          }
64          catch (Exception e)
65          {
66              e.printStackTrace();
67              fail();
68          }
69      }
70  
71      public static Test suite()
72      {
73          return new TestSuite(CryptoDefaultTest.class);
74      }
75  
76      public void testMd5()
77      {
78          String preDefinedResult = "XSop0mncK19Ii2r2CUe29w==";
79  
80          try
81          {
82              CryptoAlgorithm ca = TurbineCrypto.getCryptoAlgorithm("default");
83  
84              ca.setCipher("MD5");
85  
86              String output = ca.encrypt(preDefinedInput);
87  
88              assertEquals("MD5 Encryption failed ",
89                           preDefinedResult,
90                           output);
91  
92          }
93          catch (Exception e)
94          {
95              e.printStackTrace();
96              fail();
97          }
98      }
99  
100     public void testSha1()
101     {
102         String preDefinedResult  = "uVDiJHaavRYX8oWt5ctkaa7j1cw=";
103 
104         try
105         {
106             CryptoAlgorithm ca = TurbineCrypto.getCryptoAlgorithm("default");
107 
108             ca.setCipher("SHA1");
109 
110             String output = ca.encrypt(preDefinedInput);
111 
112             assertEquals("SHA1 Encryption failed ",
113                          preDefinedResult,
114                          output);
115 
116         }
117         catch (Exception e)
118         {
119             e.printStackTrace();
120             fail();
121         }
122     }
123 }