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