1 package org.apache.fulcrum.crypto;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 import org.apache.fulcrum.testcontainer.BaseUnit5Test;
24 import org.junit.jupiter.api.BeforeEach;
25 import org.junit.jupiter.api.DisplayName;
26 import org.junit.jupiter.api.Test;
27 import org.junit.jupiter.api.TestInfo;
28 import org.junit.platform.runner.JUnitPlatform;
29 import org.junit.runner.RunWith;
30
31 import static org.junit.jupiter.api.Assertions.assertEquals;
32
33
34
35
36
37
38
39
40
41 @DisplayName("Crypto Service Test")
42 @RunWith(JUnitPlatform.class)
43 public class CryptoServiceTest extends BaseUnit5Test
44 {
45 private CryptoService sc = null;
46 private static final String preDefinedInput = "Oeltanks";
47
48
49
50
51
52
53 public CryptoServiceTest(TestInfo testInfo)
54 {
55
56 }
57
58 @BeforeEach
59 public void setUp() throws Exception
60 {
61 sc = (CryptoService) this.lookup( CryptoService.ROLE );
62 }
63
64
65 @Test
66 public void testUnixCrypt() throws Exception
67 {
68 String preDefinedSeed = "z5";
69 String preDefinedResult = "z5EQaXpuu059c";
70
71 CryptoAlgorithm ca = sc.getCryptoAlgorithm("unix");
72
73
74
75
76 ca.setSeed(preDefinedSeed);
77 String output = ca.encrypt(preDefinedInput);
78 assertEquals( preDefinedResult, output, "Encryption failed ");
79
80
81
82
83
84 ca.setSeed(null);
85 String result = ca.encrypt(preDefinedInput);
86 ca.setSeed(result);
87 output = ca.encrypt(preDefinedInput);
88 assertEquals( output, result, "Encryption failed ");
89
90 }
91
92
93
94
95
96 @Test
97 public void testClearCrypt() throws Exception
98 {
99 String preDefinedResult = "Oeltanks";
100
101 CryptoAlgorithm ca = sc.getCryptoAlgorithm("clear");
102 String output = ca.encrypt(preDefinedInput);
103 assertEquals( preDefinedResult, output, "Encryption failed ");
104
105 }
106
107
108
109
110 @Test
111 @DisplayName("OldJavaCrypt: Truncated base64 from MD5 (Turbine 2.1) Test")
112 public void testOldJavaCryptMd5() throws Exception
113 {
114 String preDefinedResult = "XSop0mncK19Ii2r2CUe2";
115
116 CryptoAlgorithm ca = sc.getCryptoAlgorithm("oldjava");
117 ca.setCipher("MD5");
118 String output = ca.encrypt(preDefinedInput);
119 assertEquals( preDefinedResult, output, "MD5 Encryption failed ");
120
121 }
122
123
124
125
126 @Test
127 public void testOldJavaCryptSha1() throws Exception
128 {
129 String preDefinedResult = "uVDiJHaavRYX8oWt5ctkaa7j";
130
131 CryptoAlgorithm ca = sc.getCryptoAlgorithm("oldjava");
132 ca.setCipher("SHA1");
133 String output = ca.encrypt(preDefinedInput);
134 assertEquals( preDefinedResult, output, "SHA1 Encryption failed ");
135
136 }
137
138
139
140
141 @Test
142 public void testJavaCryptMd5() throws Exception
143 {
144 String preDefinedResult = "XSop0mncK19Ii2r2CUe29w==";
145 CryptoAlgorithm ca = sc.getCryptoAlgorithm("java");
146 ca.setCipher("MD5");
147 String output = ca.encrypt(preDefinedInput);
148 assertEquals( preDefinedResult, output, "MD5 Encryption failed ");
149 }
150
151
152
153
154 @Test
155 public void testJavaCryptSha1() throws Exception
156 {
157 String preDefinedResult = "uVDiJHaavRYX8oWt5ctkaa7j1cw=";
158 CryptoAlgorithm ca = sc.getCryptoAlgorithm("java");
159 ca.setCipher("SHA1");
160 String output = ca.encrypt(preDefinedInput);
161 assertEquals( preDefinedResult, output, "SHA1 Encryption failed ");
162
163 }
164
165
166
167
168 @Test
169 public void testJavaCryptSha256() throws Exception
170 {
171 String preDefinedResult = "XBSqev4ilv7P7852G2rL5WgX3FLy8VzfOY+tVq+xjek=";
172 CryptoAlgorithm ca = sc.getCryptoAlgorithm("java");
173 ca.setCipher("SHA-256");
174 String output = ca.encrypt(preDefinedInput);
175 assertEquals( preDefinedResult, output, "SHA256 Encryption failed ");
176 }
177
178
179
180
181 @Test
182 public void testJavaCryptSha512() throws Exception
183 {
184 String preDefinedResult = "QlxxOMtVn0FFAXF+DRQl8o/b+WKEG6Nc7QRdqf/LTTz/+bOaoE/JihM8uJqTW7JQm/l/TmnmVKuLaD7jdVAtJw==";
185 CryptoAlgorithm ca = sc.getCryptoAlgorithm("java");
186 ca.setCipher("SHA-512");
187 String output = ca.encrypt(preDefinedInput);
188 assertEquals( preDefinedResult, output, "SHA512 Encryption failed ");
189 }
190 }