1 |
|
package org.europa.together.application; |
2 |
|
|
3 |
|
import static com.google.code.beanmatchers.BeanMatchers.hasValidBeanConstructor; |
4 |
|
import java.io.File; |
5 |
|
import java.security.KeyPair; |
6 |
|
import java.security.NoSuchAlgorithmException; |
7 |
|
import java.security.PrivateKey; |
8 |
|
import java.security.PublicKey; |
9 |
|
import org.europa.together.JUnit5Preperator; |
10 |
|
import org.europa.together.business.CryptoTools; |
11 |
|
import org.europa.together.business.Logger; |
12 |
|
import org.europa.together.domain.CipherAlgorithm; |
13 |
|
import org.europa.together.domain.HashAlgorithm; |
14 |
|
import org.europa.together.domain.LogLevel; |
15 |
|
import org.europa.together.utils.Constraints; |
16 |
|
import static org.hamcrest.MatcherAssert.assertThat; |
17 |
|
import org.junit.jupiter.api.AfterAll; |
18 |
|
import org.junit.jupiter.api.AfterEach; |
19 |
|
import static org.junit.jupiter.api.Assertions.*; |
20 |
|
import org.junit.jupiter.api.Assumptions; |
21 |
|
import org.junit.jupiter.api.BeforeAll; |
22 |
|
import org.junit.jupiter.api.BeforeEach; |
23 |
|
import org.junit.jupiter.api.MethodOrderer.OrderAnnotation; |
24 |
|
import org.junit.jupiter.api.Order; |
25 |
|
import org.junit.jupiter.api.Test; |
26 |
|
import org.junit.jupiter.api.TestMethodOrder; |
27 |
|
import org.junit.jupiter.api.extension.ExtendWith; |
28 |
|
import org.springframework.beans.factory.annotation.Autowired; |
29 |
|
import org.springframework.test.context.ContextConfiguration; |
30 |
|
import org.springframework.test.context.junit.jupiter.SpringExtension; |
31 |
|
|
32 |
|
@SuppressWarnings("unchecked") |
33 |
|
@ExtendWith({JUnit5Preperator.class}) |
34 |
|
@ExtendWith(SpringExtension.class) |
35 |
|
@ContextConfiguration(locations = {"/applicationContext.xml"}) |
36 |
|
@TestMethodOrder(OrderAnnotation.class) |
|
|
| 97.6% |
Uncovered Elements: 2 (85) |
Complexity: 22 |
Complexity Density: 0.34 |
|
37 |
|
public class JavaCryptoToolsTest { |
38 |
|
|
39 |
|
private static final Logger LOGGER = new LogbackLogger(JavaCryptoToolsTest.class); |
40 |
|
|
41 |
|
private static final String DIRECTORY |
42 |
|
= Constraints.SYSTEM_APP_DIR + "/target/test-classes/"; |
43 |
|
|
44 |
|
@Autowired |
45 |
|
private CryptoTools cryptoTools; |
46 |
|
|
47 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
48 |
1 |
@BeforeAll... |
49 |
|
static void setUp() { |
50 |
1 |
Assumptions.assumeTrue(true, "Assumtion failed."); |
51 |
|
|
52 |
1 |
LOGGER.log("Assumptions passed ...\n\n", LogLevel.DEBUG); |
53 |
|
} |
54 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
55 |
1 |
@AfterAll... |
56 |
|
static void tearDown() { |
57 |
|
} |
58 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
59 |
16 |
@BeforeEach... |
60 |
|
void testCaseInitialization() { |
61 |
|
} |
62 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
63 |
16 |
@AfterEach... |
64 |
|
void testCaseTermination() { |
65 |
|
} |
66 |
|
|
67 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
1PASS
|
|
68 |
1 |
@Test... |
69 |
|
void constructor() { |
70 |
1 |
LOGGER.log("TEST CASE: constructor", LogLevel.DEBUG); |
71 |
|
|
72 |
1 |
assertThat(JavaCryptoTools.class, hasValidBeanConstructor()); |
73 |
|
} |
74 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
1PASS
|
|
75 |
1 |
@Test... |
76 |
|
void calculateMD5Hashes() { |
77 |
1 |
LOGGER.log("TEST CASE: calculateMD5Hashes", LogLevel.DEBUG); |
78 |
|
|
79 |
|
|
80 |
1 |
assertEquals("d41d8cd98f00b204e9800998ecf8427e", |
81 |
|
cryptoTools.calculateHash("", HashAlgorithm.MD5)); |
82 |
1 |
assertEquals("7215ee9c7d9dc229d2921a40e899ec5f", |
83 |
|
cryptoTools.calculateHash(" ", HashAlgorithm.MD5)); |
84 |
1 |
assertEquals("7f138a09169b250e9dcb378140907378", |
85 |
|
cryptoTools.calculateHash("MD5", HashAlgorithm.MD5)); |
86 |
1 |
assertEquals("1bc29b36f623ba82aaf6724fd3b16718", |
87 |
|
cryptoTools.calculateHash("md5", HashAlgorithm.MD5)); |
88 |
|
} |
89 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
1PASS
|
|
90 |
1 |
@Test... |
91 |
|
void calculateSHA1Hashes() { |
92 |
1 |
LOGGER.log("TEST CASE: calculateSHA1Hashes", LogLevel.DEBUG); |
93 |
|
|
94 |
1 |
assertEquals("da39a3ee5e6b4b0d3255bfef95601890afd80709", |
95 |
|
cryptoTools.calculateHash("", HashAlgorithm.SHA)); |
96 |
1 |
assertEquals("b858cb282617fb0956d960215c8e84d1ccf909c6", |
97 |
|
cryptoTools.calculateHash(" ", HashAlgorithm.SHA)); |
98 |
1 |
assertEquals("9faed964fef80190696bb2b0b98ed45cb7120c93", |
99 |
|
cryptoTools.calculateHash("SHA", HashAlgorithm.SHA)); |
100 |
1 |
assertEquals("d8f4590320e1343a915b6394170650a8f35d6926", |
101 |
|
cryptoTools.calculateHash("sha", HashAlgorithm.SHA)); |
102 |
|
} |
103 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
1PASS
|
|
104 |
1 |
@Test... |
105 |
|
void calculateSHA512Hashes() { |
106 |
1 |
LOGGER.log("TEST CASE: calculateSHA512Hashes", LogLevel.DEBUG); |
107 |
|
|
108 |
1 |
assertEquals("cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e", |
109 |
|
cryptoTools.calculateHash("", HashAlgorithm.SHA512)); |
110 |
1 |
assertEquals("f90ddd77e400dfe6a3fcf479b00b1ee29e7015c5bb8cd70f5f15b4886cc339275ff553fc8a053f8ddc7324f45168cffaf81f8c3ac93996f6536eef38e5e40768", |
111 |
|
cryptoTools.calculateHash(" ", HashAlgorithm.SHA512)); |
112 |
1 |
assertEquals("33f63bc374f428f597d7f7ba7cc1e21a0b4b44faa727f7c052c5ad0b1aa5303884ea5919a53c0d32b5591f4ded381da16b67f6a2170d81058d7e9bb2ad4a215b", |
113 |
|
cryptoTools.calculateHash("SHA-512", HashAlgorithm.SHA512)); |
114 |
1 |
assertEquals("a0f7493ac661f902da5343b88b005ad9efefd7f46d96ae4bac6a980f87c32a05b9be0109c3622cb885f7029a0e6ec3368662f4a694130868ec7650c1c80eb9e9", |
115 |
|
cryptoTools.calculateHash("sha-512", HashAlgorithm.SHA512)); |
116 |
|
} |
117 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
1PASS
|
|
118 |
1 |
@Test... |
119 |
|
void calculateSHA256Hashes() { |
120 |
1 |
LOGGER.log("TEST CASE: calculateSHA256Hashess", LogLevel.DEBUG); |
121 |
|
|
122 |
1 |
assertEquals("e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", |
123 |
|
cryptoTools.calculateHash("", HashAlgorithm.SHA256)); |
124 |
1 |
assertEquals("36a9e7f1c95b82ffb99743e0c5c4ce95d83c9a430aac59f84ef3cbfab6145068", |
125 |
|
cryptoTools.calculateHash(" ", HashAlgorithm.SHA256)); |
126 |
1 |
assertEquals("bbd07c4fc02c99b97124febf42c7b63b5011c0df28d409fbb486b5a9d2e615ea", |
127 |
|
cryptoTools.calculateHash("SHA-256", HashAlgorithm.SHA256)); |
128 |
1 |
assertEquals("3128f8ac2988e171a53782b144b98a5c2ee723489c8b220cece002916fbc71e2", |
129 |
|
cryptoTools.calculateHash("sha-256", HashAlgorithm.SHA256)); |
130 |
|
} |
131 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
1PASS
|
|
132 |
1 |
@Test... |
133 |
|
void failCalculateHashes() { |
134 |
1 |
LOGGER.log("TEST CASE: failCalculateHashes", LogLevel.DEBUG); |
135 |
|
|
136 |
1 |
assertNull(cryptoTools.calculateHash(null, HashAlgorithm.SHA)); |
137 |
1 |
assertNull(cryptoTools.calculateHash("wrong algorithm", null)); |
138 |
|
} |
139 |
|
|
|
|
| 75% |
Uncovered Elements: 1 (4) |
Complexity: 2 |
Complexity Density: 0.5 |
1PASS
|
|
140 |
1 |
@Test... |
141 |
|
void getMaxKeySize() { |
142 |
1 |
LOGGER.log("TEST CASE: getMaxKeySize", LogLevel.DEBUG); |
143 |
|
|
144 |
1 |
try { |
145 |
1 |
assertEquals(2147483647, cryptoTools.getMaxKeySize(CipherAlgorithm.RSA)); |
146 |
|
} catch (NoSuchAlgorithmException ex) { |
147 |
0 |
LOGGER.catchException(ex); |
148 |
|
} |
149 |
|
} |
150 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
1PASS
|
|
151 |
1 |
@Test... |
152 |
|
void calculateRsaKeyPair() { |
153 |
1 |
LOGGER.log("TEST CASE: calculateRsaKeyPair", LogLevel.DEBUG); |
154 |
|
|
155 |
1 |
KeyPair store = cryptoTools.generateCipherKeyPair(CipherAlgorithm.RSA); |
156 |
1 |
byte[] privateKey = store.getPrivate().getEncoded(); |
157 |
1 |
byte[] publicKey = store.getPublic().getEncoded(); |
158 |
|
|
159 |
1 |
assertNotEquals(publicKey.length, privateKey.length); |
160 |
|
} |
161 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
1PASS
|
|
162 |
1 |
@Test... |
163 |
|
void failCalculateKeyPair() { |
164 |
1 |
LOGGER.log("TEST CASE: failCalculateKeyPair", LogLevel.DEBUG); |
165 |
|
|
166 |
1 |
assertNull(cryptoTools.generateCipherKeyPair(null)); |
167 |
|
} |
168 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
1PASS
|
|
169 |
1 |
@Test... |
170 |
|
void failWriteKeyPairToFile() { |
171 |
1 |
LOGGER.log("TEST CASE: failWriteKeyPairToFile", LogLevel.DEBUG); |
172 |
|
|
173 |
1 |
cryptoTools.saveKeyPairToFile(DIRECTORY, null); |
174 |
|
|
175 |
1 |
assertFalse(new File(DIRECTORY + "NotExist.key").exists()); |
176 |
1 |
assertFalse(new File(DIRECTORY + "NotExist.pub").exists()); |
177 |
|
} |
178 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
1PASS
|
|
179 |
1 |
@Test... |
180 |
|
@Order(1) |
181 |
|
void writeKeyPairToFile() { |
182 |
1 |
LOGGER.log("TEST CASE: writeKeyPairToFile", LogLevel.DEBUG); |
183 |
|
|
184 |
1 |
cryptoTools.saveKeyPairToFile(DIRECTORY, |
185 |
|
cryptoTools.generateCipherKeyPair(CipherAlgorithm.RSA)); |
186 |
|
|
187 |
1 |
assertTrue(new File(DIRECTORY + CipherAlgorithm.RSA + ".key").exists()); |
188 |
1 |
assertTrue(new File(DIRECTORY + CipherAlgorithm.RSA + ".pub").exists()); |
189 |
|
} |
190 |
|
|
|
|
| 88.9% |
Uncovered Elements: 1 (9) |
Complexity: 2 |
Complexity Density: 0.22 |
1PASS
|
|
191 |
1 |
@Test... |
192 |
|
@Order(2) |
193 |
|
void writeKeyPairToFileInHomeDir() { |
194 |
1 |
LOGGER.log("TEST CASE: writeKeyPairToFile", LogLevel.DEBUG); |
195 |
|
|
196 |
1 |
cryptoTools.saveKeyPairToFile(null, |
197 |
|
cryptoTools.generateCipherKeyPair(CipherAlgorithm.RSA)); |
198 |
|
|
199 |
1 |
String dir = Constraints.SYSTEM_USER_HOME_DIR + "/"; |
200 |
|
|
201 |
1 |
assertTrue(new File(dir + CipherAlgorithm.RSA + ".key").exists()); |
202 |
1 |
assertTrue(new File(dir + CipherAlgorithm.RSA + ".pub").exists()); |
203 |
|
|
204 |
1 |
try { |
205 |
1 |
assertTrue(new File(dir + CipherAlgorithm.RSA + ".key").delete()); |
206 |
1 |
assertTrue(new File(dir + CipherAlgorithm.RSA + ".pub").delete()); |
207 |
|
|
208 |
|
} catch (Exception ex) { |
209 |
0 |
LOGGER.catchException(ex); |
210 |
|
} |
211 |
|
} |
212 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
1PASS
|
|
213 |
1 |
@Test... |
214 |
|
@Order(3) |
215 |
|
void loadPrivateRsaKeyFromFile() { |
216 |
1 |
LOGGER.log("TEST CASE: loadPrivateRsaKeyFromFile", LogLevel.DEBUG); |
217 |
|
|
218 |
1 |
PrivateKey privateKey = cryptoTools.loadPrivateKeyFile( |
219 |
|
DIRECTORY + CipherAlgorithm.RSA + ".key", CipherAlgorithm.RSA); |
220 |
1 |
assertEquals(CipherAlgorithm.RSA.toString(), privateKey.getAlgorithm()); |
221 |
|
} |
222 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
1PASS
|
|
223 |
1 |
@Test... |
224 |
|
@Order(4) |
225 |
|
void loadPublicKeyRsaFromFile() { |
226 |
1 |
LOGGER.log("TEST CASE: loadPublicRsaKeyFromFile", LogLevel.DEBUG); |
227 |
|
|
228 |
1 |
PublicKey publicRsaKey = cryptoTools.loadPublicKeyFile( |
229 |
|
DIRECTORY + CipherAlgorithm.RSA + ".pub", CipherAlgorithm.RSA); |
230 |
1 |
assertEquals(CipherAlgorithm.RSA.toString(), publicRsaKey.getAlgorithm()); |
231 |
|
} |
232 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
1PASS
|
|
233 |
1 |
@Test... |
234 |
|
void failLoadPrivateKeyFile() { |
235 |
1 |
LOGGER.log("TEST CASE: failLoadPrivateKeyFile", LogLevel.DEBUG); |
236 |
|
|
237 |
1 |
assertNull(cryptoTools.loadPrivateKeyFile(null, CipherAlgorithm.RSA)); |
238 |
|
} |
239 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
1PASS
|
|
240 |
1 |
@Test... |
241 |
|
void failLoadPublicKeyFile() { |
242 |
1 |
LOGGER.log("TEST CASE: failLoadPublicKeyFile", LogLevel.DEBUG); |
243 |
|
|
244 |
1 |
assertNull(cryptoTools.loadPublicKeyFile(null, CipherAlgorithm.RSA)); |
245 |
|
} |
246 |
|
} |