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 junit.framework.Test;
23  import junit.framework.TestSuite;
24  
25  import org.apache.commons.configuration.BaseConfiguration;
26  import org.apache.commons.configuration.Configuration;
27  
28  import org.apache.turbine.services.ServiceManager;
29  import org.apache.turbine.services.TurbineServices;
30  import org.apache.turbine.services.factory.FactoryService;
31  import org.apache.turbine.services.factory.TurbineFactoryService;
32  import org.apache.turbine.test.BaseTestCase;
33  
34  public class CryptoDefaultTest
35      extends BaseTestCase
36  {
37      private static final String PREFIX = "services." +
38          CryptoService.SERVICE_NAME + '.';
39  
40      private static final String preDefinedInput = "Oeltanks";
41  
42      public CryptoDefaultTest(String name)
43              throws Exception
44      {
45          super(name);
46  
47          ServiceManager serviceManager = TurbineServices.getInstance();
48          serviceManager.setApplicationRoot(".");
49  
50          Configuration cfg = new BaseConfiguration();
51          cfg.setProperty(PREFIX + "classname",
52                          TurbineCryptoService.class.getName());
53  
54          /* No providers configured. Should be "java" then */
55  
56          /* Ugh */
57  
58          cfg.setProperty("services." + FactoryService.SERVICE_NAME + ".classname",
59                          TurbineFactoryService.class.getName());
60  
61          serviceManager.setConfiguration(cfg);
62  
63          try
64          {
65              serviceManager.init();
66          }
67          catch (Exception e)
68          {
69              e.printStackTrace();
70              fail();
71          }
72      }
73  
74      public static Test suite()
75      {
76          return new TestSuite(CryptoDefaultTest.class);
77      }
78  
79      public void testMd5()
80      {
81          String preDefinedResult = "XSop0mncK19Ii2r2CUe29w==";
82  
83          try
84          {
85              CryptoAlgorithm ca = TurbineCrypto.getCryptoAlgorithm("default");
86  
87              ca.setCipher("MD5");
88  
89              String output = ca.encrypt(preDefinedInput);
90  
91              assertEquals("MD5 Encryption failed ",
92                           preDefinedResult,
93                           output);
94  
95          }
96          catch (Exception e)
97          {
98              e.printStackTrace();
99              fail();
100         }
101     }
102 
103     public void testSha1()
104     {
105         String preDefinedResult  = "uVDiJHaavRYX8oWt5ctkaa7j1cw=";
106 
107         try
108         {
109             CryptoAlgorithm ca = TurbineCrypto.getCryptoAlgorithm("default");
110 
111             ca.setCipher("SHA1");
112 
113             String output = ca.encrypt(preDefinedInput);
114 
115             assertEquals("SHA1 Encryption failed ",
116                          preDefinedResult,
117                          output);
118 
119         }
120         catch (Exception e)
121         {
122             e.printStackTrace();
123             fail();
124         }
125     }
126 }