KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > turbine > services > crypto > CryptoTest


1 package org.apache.turbine.services.crypto;
2
3 /*
4  * Copyright 2001-2004 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.crypto.provider.ClearCrypt;
28 import org.apache.turbine.services.crypto.provider.JavaCrypt;
29 import org.apache.turbine.services.crypto.provider.OldJavaCrypt;
30 import org.apache.turbine.services.crypto.provider.UnixCrypt;
31 import org.apache.turbine.services.factory.FactoryService;
32 import org.apache.turbine.services.factory.TurbineFactoryService;
33 import org.apache.turbine.test.BaseTestCase;
34
35 public class CryptoTest
36     extends BaseTestCase
37 {
38     private static final String JavaDoc PREFIX = "services." +
39         CryptoService.SERVICE_NAME + '.';
40
41     private static final String JavaDoc preDefinedInput = "Oeltanks";
42
43     public CryptoTest( String JavaDoc name )
44             throws Exception JavaDoc
45     {
46         super(name);
47
48         ServiceManager serviceManager = TurbineServices.getInstance();
49         serviceManager.setApplicationRoot(".");
50
51         Configuration cfg = new BaseConfiguration();
52         cfg.setProperty(PREFIX + "classname",
53                         TurbineCryptoService.class.getName());
54
55         cfg.setProperty(PREFIX + "algorithm.unix",
56                         UnixCrypt.class.getName());
57         cfg.setProperty(PREFIX + "algorithm.clear",
58                         ClearCrypt.class.getName());
59         cfg.setProperty(PREFIX + "algorithm.java",
60                         JavaCrypt.class.getName());
61         cfg.setProperty(PREFIX + "algorithm.oldjava",
62                         OldJavaCrypt.class.getName());
63
64         /* Do _not_ configure a default! We want to test explicitly */
65
66         cfg.setProperty(PREFIX + "algorithm.default",
67                         "none");
68
69         /* Ugh */
70
71         cfg.setProperty("services." + FactoryService.SERVICE_NAME + ".classname",
72                         TurbineFactoryService.class.getName());
73
74         serviceManager.setConfiguration(cfg);
75
76         try
77         {
78             serviceManager.init();
79         }
80         catch (Exception JavaDoc e)
81         {
82             e.printStackTrace();
83             fail();
84         }
85     }
86
87     public static Test suite()
88     {
89         return new TestSuite(CryptoTest.class);
90     }
91
92     public void testUnixCrypt()
93     {
94         String JavaDoc preDefinedSeed = "z5";
95         String JavaDoc preDefinedResult = "z5EQaXpuu059c";
96
97         try
98         {
99             CryptoAlgorithm ca = TurbineCrypto.getCryptoAlgorithm("unix");
100
101             /*
102              * Test predefined Seed
103              */

104
105             ca.setSeed(preDefinedSeed);
106
107             String JavaDoc output = ca.encrypt(preDefinedInput);
108
109             assertEquals("Encryption failed ",
110                          preDefinedResult,
111                          output);
112
113             /*
114              * Test random Seed
115              *
116              */

117
118             ca.setSeed(null);
119
120             String JavaDoc result = ca.encrypt(preDefinedInput);
121
122             ca.setSeed(result);
123
124             output = ca.encrypt(preDefinedInput);
125
126             assertEquals("Encryption failed ",
127                          output,
128                          result);
129         }
130         catch (Exception JavaDoc e)
131         {
132             e.printStackTrace();
133             fail();
134         }
135     }
136
137     public void testClearCrypt()
138     {
139         String JavaDoc preDefinedResult = "Oeltanks";
140
141         try
142         {
143             CryptoAlgorithm ca = TurbineCrypto.getCryptoAlgorithm("clear");
144             String JavaDoc output = ca.encrypt(preDefinedInput);
145
146             assertEquals("Encryption failed ",
147                          preDefinedResult,
148                          output);
149
150         }
151         catch (Exception JavaDoc e)
152         {
153             e.printStackTrace();
154             fail();
155         }
156     }
157
158     public void testOldJavaCryptMd5()
159     {
160         String JavaDoc preDefinedResult = "XSop0mncK19Ii2r2CUe2";
161
162         try
163         {
164             CryptoAlgorithm ca = TurbineCrypto.getCryptoAlgorithm("oldjava");
165
166             ca.setCipher("MD5");
167
168             String JavaDoc output = ca.encrypt(preDefinedInput);
169
170             assertEquals("MD5 Encryption failed ",
171                          preDefinedResult,
172                          output);
173
174         }
175         catch (Exception JavaDoc e)
176         {
177             e.printStackTrace();
178             fail();
179         }
180     }
181
182     public void testOldJavaCryptSha1()
183     {
184         String JavaDoc preDefinedResult = "uVDiJHaavRYX8oWt5ctkaa7j";
185
186         try
187         {
188             CryptoAlgorithm ca = TurbineCrypto.getCryptoAlgorithm("oldjava");
189
190             ca.setCipher("SHA1");
191
192             String JavaDoc output = ca.encrypt(preDefinedInput);
193
194             assertEquals("SHA1 Encryption failed ",
195                          preDefinedResult,
196                          output);
197
198         }
199         catch (Exception JavaDoc e)
200         {
201             e.printStackTrace();
202             fail();
203         }
204     }
205
206     public void testJavaCryptMd5()
207     {
208         String JavaDoc preDefinedResult = "XSop0mncK19Ii2r2CUe29w==";
209
210         try
211         {
212             CryptoAlgorithm ca = TurbineCrypto.getCryptoAlgorithm("java");
213
214             ca.setCipher("MD5");
215
216             String JavaDoc output = ca.encrypt(preDefinedInput);
217
218             assertEquals("MD5 Encryption failed ",
219                          preDefinedResult,
220                          output);
221
222         }
223         catch (Exception JavaDoc e)
224         {
225             e.printStackTrace();
226             fail();
227         }
228     }
229
230     public void testJavaCryptSha1()
231     {
232         String JavaDoc preDefinedResult = "uVDiJHaavRYX8oWt5ctkaa7j1cw=";
233
234         try
235         {
236             CryptoAlgorithm ca = TurbineCrypto.getCryptoAlgorithm("java");
237
238             ca.setCipher("SHA1");
239
240             String JavaDoc output = ca.encrypt(preDefinedInput);
241
242             assertEquals("SHA1 Encryption failed ",
243                          preDefinedResult,
244                          output);
245
246         }
247         catch (Exception JavaDoc e)
248         {
249             e.printStackTrace();
250             fail();
251         }
252     }
253
254 }
255
Popular Tags