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.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
55
56
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 }