1 package org.apache.turbine.services.crypto;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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.crypto.provider.ClearCrypt;
31 import org.apache.turbine.services.crypto.provider.JavaCrypt;
32 import org.apache.turbine.services.crypto.provider.OldJavaCrypt;
33 import org.apache.turbine.services.crypto.provider.UnixCrypt;
34 import org.apache.turbine.services.factory.FactoryService;
35 import org.apache.turbine.services.factory.TurbineFactoryService;
36 import org.apache.turbine.test.BaseTestCase;
37
38 public class CryptoTest
39 extends BaseTestCase
40 {
41 private static final String PREFIX = "services." +
42 CryptoService.SERVICE_NAME + '.';
43
44 private static final String preDefinedInput = "Oeltanks";
45
46 public CryptoTest( String name )
47 throws Exception
48 {
49 super(name);
50
51 ServiceManager serviceManager = TurbineServices.getInstance();
52 serviceManager.setApplicationRoot(".");
53
54 Configuration cfg = new BaseConfiguration();
55 cfg.setProperty(PREFIX + "classname",
56 TurbineCryptoService.class.getName());
57
58 cfg.setProperty(PREFIX + "algorithm.unix",
59 UnixCrypt.class.getName());
60 cfg.setProperty(PREFIX + "algorithm.clear",
61 ClearCrypt.class.getName());
62 cfg.setProperty(PREFIX + "algorithm.java",
63 JavaCrypt.class.getName());
64 cfg.setProperty(PREFIX + "algorithm.oldjava",
65 OldJavaCrypt.class.getName());
66
67
68
69 cfg.setProperty(PREFIX + "algorithm.default",
70 "none");
71
72
73
74 cfg.setProperty("services." + FactoryService.SERVICE_NAME + ".classname",
75 TurbineFactoryService.class.getName());
76
77 serviceManager.setConfiguration(cfg);
78
79 try
80 {
81 serviceManager.init();
82 }
83 catch (Exception e)
84 {
85 e.printStackTrace();
86 fail();
87 }
88 }
89
90 public static Test suite()
91 {
92 return new TestSuite(CryptoTest.class);
93 }
94
95 public void testUnixCrypt()
96 {
97 String preDefinedSeed = "z5";
98 String preDefinedResult = "z5EQaXpuu059c";
99
100 try
101 {
102 CryptoAlgorithm ca = TurbineCrypto.getCryptoAlgorithm("unix");
103
104
105
106
107
108 ca.setSeed(preDefinedSeed);
109
110 String output = ca.encrypt(preDefinedInput);
111
112 assertEquals("Encryption failed ",
113 preDefinedResult,
114 output);
115
116
117
118
119
120
121 ca.setSeed(null);
122
123 String result = ca.encrypt(preDefinedInput);
124
125 ca.setSeed(result);
126
127 output = ca.encrypt(preDefinedInput);
128
129 assertEquals("Encryption failed ",
130 output,
131 result);
132 }
133 catch (Exception e)
134 {
135 e.printStackTrace();
136 fail();
137 }
138 }
139
140 public void testClearCrypt()
141 {
142 String preDefinedResult = "Oeltanks";
143
144 try
145 {
146 CryptoAlgorithm ca = TurbineCrypto.getCryptoAlgorithm("clear");
147 String output = ca.encrypt(preDefinedInput);
148
149 assertEquals("Encryption failed ",
150 preDefinedResult,
151 output);
152
153 }
154 catch (Exception e)
155 {
156 e.printStackTrace();
157 fail();
158 }
159 }
160
161 public void testOldJavaCryptMd5()
162 {
163 String preDefinedResult = "XSop0mncK19Ii2r2CUe2";
164
165 try
166 {
167 CryptoAlgorithm ca = TurbineCrypto.getCryptoAlgorithm("oldjava");
168
169 ca.setCipher("MD5");
170
171 String output = ca.encrypt(preDefinedInput);
172
173 assertEquals("MD5 Encryption failed ",
174 preDefinedResult,
175 output);
176
177 }
178 catch (Exception e)
179 {
180 e.printStackTrace();
181 fail();
182 }
183 }
184
185 public void testOldJavaCryptSha1()
186 {
187 String preDefinedResult = "uVDiJHaavRYX8oWt5ctkaa7j";
188
189 try
190 {
191 CryptoAlgorithm ca = TurbineCrypto.getCryptoAlgorithm("oldjava");
192
193 ca.setCipher("SHA1");
194
195 String output = ca.encrypt(preDefinedInput);
196
197 assertEquals("SHA1 Encryption failed ",
198 preDefinedResult,
199 output);
200
201 }
202 catch (Exception e)
203 {
204 e.printStackTrace();
205 fail();
206 }
207 }
208
209 public void testJavaCryptMd5()
210 {
211 String preDefinedResult = "XSop0mncK19Ii2r2CUe29w==";
212
213 try
214 {
215 CryptoAlgorithm ca = TurbineCrypto.getCryptoAlgorithm("java");
216
217 ca.setCipher("MD5");
218
219 String output = ca.encrypt(preDefinedInput);
220
221 assertEquals("MD5 Encryption failed ",
222 preDefinedResult,
223 output);
224
225 }
226 catch (Exception e)
227 {
228 e.printStackTrace();
229 fail();
230 }
231 }
232
233 public void testJavaCryptSha1()
234 {
235 String preDefinedResult = "uVDiJHaavRYX8oWt5ctkaa7j1cw=";
236
237 try
238 {
239 CryptoAlgorithm ca = TurbineCrypto.getCryptoAlgorithm("java");
240
241 ca.setCipher("SHA1");
242
243 String output = ca.encrypt(preDefinedInput);
244
245 assertEquals("SHA1 Encryption failed ",
246 preDefinedResult,
247 output);
248
249 }
250 catch (Exception e)
251 {
252 e.printStackTrace();
253 fail();
254 }
255 }
256
257 }