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 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 PREFIX = "services." +
39 CryptoService.SERVICE_NAME + '.';
40
41 private static final String preDefinedInput = "Oeltanks";
42
43 public CryptoTest( String name )
44 throws Exception
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
65
66 cfg.setProperty(PREFIX + "algorithm.default",
67 "none");
68
69
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 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 preDefinedSeed = "z5";
95 String preDefinedResult = "z5EQaXpuu059c";
96
97 try
98 {
99 CryptoAlgorithm ca = TurbineCrypto.getCryptoAlgorithm("unix");
100
101
102
103
104
105 ca.setSeed(preDefinedSeed);
106
107 String output = ca.encrypt(preDefinedInput);
108
109 assertEquals("Encryption failed ",
110 preDefinedResult,
111 output);
112
113
114
115
116
117
118 ca.setSeed(null);
119
120 String 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 e)
131 {
132 e.printStackTrace();
133 fail();
134 }
135 }
136
137 public void testClearCrypt()
138 {
139 String preDefinedResult = "Oeltanks";
140
141 try
142 {
143 CryptoAlgorithm ca = TurbineCrypto.getCryptoAlgorithm("clear");
144 String output = ca.encrypt(preDefinedInput);
145
146 assertEquals("Encryption failed ",
147 preDefinedResult,
148 output);
149
150 }
151 catch (Exception e)
152 {
153 e.printStackTrace();
154 fail();
155 }
156 }
157
158 public void testOldJavaCryptMd5()
159 {
160 String preDefinedResult = "XSop0mncK19Ii2r2CUe2";
161
162 try
163 {
164 CryptoAlgorithm ca = TurbineCrypto.getCryptoAlgorithm("oldjava");
165
166 ca.setCipher("MD5");
167
168 String output = ca.encrypt(preDefinedInput);
169
170 assertEquals("MD5 Encryption failed ",
171 preDefinedResult,
172 output);
173
174 }
175 catch (Exception e)
176 {
177 e.printStackTrace();
178 fail();
179 }
180 }
181
182 public void testOldJavaCryptSha1()
183 {
184 String preDefinedResult = "uVDiJHaavRYX8oWt5ctkaa7j";
185
186 try
187 {
188 CryptoAlgorithm ca = TurbineCrypto.getCryptoAlgorithm("oldjava");
189
190 ca.setCipher("SHA1");
191
192 String output = ca.encrypt(preDefinedInput);
193
194 assertEquals("SHA1 Encryption failed ",
195 preDefinedResult,
196 output);
197
198 }
199 catch (Exception e)
200 {
201 e.printStackTrace();
202 fail();
203 }
204 }
205
206 public void testJavaCryptMd5()
207 {
208 String preDefinedResult = "XSop0mncK19Ii2r2CUe29w==";
209
210 try
211 {
212 CryptoAlgorithm ca = TurbineCrypto.getCryptoAlgorithm("java");
213
214 ca.setCipher("MD5");
215
216 String output = ca.encrypt(preDefinedInput);
217
218 assertEquals("MD5 Encryption failed ",
219 preDefinedResult,
220 output);
221
222 }
223 catch (Exception e)
224 {
225 e.printStackTrace();
226 fail();
227 }
228 }
229
230 public void testJavaCryptSha1()
231 {
232 String preDefinedResult = "uVDiJHaavRYX8oWt5ctkaa7j1cw=";
233
234 try
235 {
236 CryptoAlgorithm ca = TurbineCrypto.getCryptoAlgorithm("java");
237
238 ca.setCipher("SHA1");
239
240 String output = ca.encrypt(preDefinedInput);
241
242 assertEquals("SHA1 Encryption failed ",
243 preDefinedResult,
244 output);
245
246 }
247 catch (Exception e)
248 {
249 e.printStackTrace();
250 fail();
251 }
252 }
253
254 }