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