| 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.domain.RNGAlgorithm; |
| 16 |
|
import org.europa.together.utils.Constraints; |
| 17 |
|
import static org.hamcrest.MatcherAssert.assertThat; |
| 18 |
|
import org.junit.jupiter.api.AfterAll; |
| 19 |
|
import org.junit.jupiter.api.AfterEach; |
| 20 |
|
import static org.junit.jupiter.api.Assertions.*; |
| 21 |
|
import org.junit.jupiter.api.Assumptions; |
| 22 |
|
import org.junit.jupiter.api.BeforeAll; |
| 23 |
|
import org.junit.jupiter.api.BeforeEach; |
| 24 |
|
import org.junit.jupiter.api.MethodOrderer.OrderAnnotation; |
| 25 |
|
import org.junit.jupiter.api.Order; |
| 26 |
|
import org.junit.jupiter.api.Test; |
| 27 |
|
import org.junit.jupiter.api.TestMethodOrder; |
| 28 |
|
import org.junit.jupiter.api.extension.ExtendWith; |
| 29 |
|
import org.springframework.beans.factory.annotation.Autowired; |
| 30 |
|
import org.springframework.test.context.ContextConfiguration; |
| 31 |
|
import org.springframework.test.context.junit.jupiter.SpringExtension; |
| 32 |
|
|
| 33 |
|
@SuppressWarnings("unchecked") |
| 34 |
|
@ExtendWith({SpringExtension.class, JUnit5Preperator.class}) |
| 35 |
|
@ContextConfiguration(locations = {"/applicationContext.xml"}) |
| 36 |
|
@TestMethodOrder(OrderAnnotation.class) |
| |
|
| 97.6% |
Uncovered Elements: 2 (82) |
Complexity: 25 |
Complexity Density: 0.42 |
|
| 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 |
19 |
@BeforeEach... |
| 60 |
|
void testCaseInitialization() { |
| 61 |
|
} |
| 62 |
|
|
| |
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
| 63 |
19 |
@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 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
1PASS
|
|
| 75 |
1 |
@Test... |
| 76 |
|
void calculateMD5Hashes() { |
| 77 |
1 |
LOGGER.log("TEST CASE: calculateMD5Hashes", LogLevel.DEBUG); |
| 78 |
1 |
assertEquals("7f138a09169b250e9dcb378140907378", |
| 79 |
|
cryptoTools.calculateHash("MD5", HashAlgorithm.MD5)); |
| 80 |
|
} |
| 81 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
1PASS
|
|
| 82 |
1 |
@Test... |
| 83 |
|
void calculateSHA1Hashes() { |
| 84 |
1 |
LOGGER.log("TEST CASE: calculateSHA1Hashes", LogLevel.DEBUG); |
| 85 |
1 |
assertEquals("9faed964fef80190696bb2b0b98ed45cb7120c93", |
| 86 |
|
cryptoTools.calculateHash("SHA", HashAlgorithm.SHA)); |
| 87 |
|
} |
| 88 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
1PASS
|
|
| 89 |
1 |
@Test... |
| 90 |
|
void calculateSHA256Hashes() { |
| 91 |
1 |
LOGGER.log("TEST CASE: calculateSHA256Hashess", LogLevel.DEBUG); |
| 92 |
1 |
assertEquals("bbd07c4fc02c99b97124febf42c7b63b5011c0df28d409fbb486b5a9d2e615ea", |
| 93 |
|
cryptoTools.calculateHash("SHA-256", HashAlgorithm.SHA256)); |
| 94 |
|
} |
| 95 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
1PASS
|
|
| 96 |
1 |
@Test... |
| 97 |
|
void calculateSHA512Hashes() { |
| 98 |
1 |
LOGGER.log("TEST CASE: calculateSHA512Hashes", LogLevel.DEBUG); |
| 99 |
1 |
assertEquals("33f63bc374f428f597d7f7ba7cc1e21a0b4b44faa727f7c052c5ad0b1aa5303884ea5919a53c0d32b5591f4ded381da16b67f6a2170d81058d7e9bb2ad4a215b", |
| 100 |
|
cryptoTools.calculateHash("SHA-512", HashAlgorithm.SHA512)); |
| 101 |
|
} |
| 102 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
1PASS
|
|
| 103 |
1 |
@Test... |
| 104 |
|
void calculateSHA3512Hashes() { |
| 105 |
1 |
LOGGER.log("TEST CASE: calculateSHA3512Hashes", LogLevel.DEBUG); |
| 106 |
1 |
assertEquals("a117fb85996b183f113a531924ca7e278324f31501b6e4b33a9b67e7811e8349fa7c1f83f1ea1c345c22d49fdd8fd0444d74917fedf081c160c1cd687643d948", |
| 107 |
|
cryptoTools.calculateHash("SHA3512", HashAlgorithm.SHA3512)); |
| 108 |
|
} |
| 109 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
1PASS
|
|
| 110 |
1 |
@Test... |
| 111 |
|
void failCalculateHashes() { |
| 112 |
1 |
LOGGER.log("TEST CASE: failCalculateHashes", LogLevel.DEBUG); |
| 113 |
|
|
| 114 |
1 |
assertNull(cryptoTools.calculateHash(null, HashAlgorithm.SHA)); |
| 115 |
1 |
assertNull(cryptoTools.calculateHash("wrong algorithm", null)); |
| 116 |
|
} |
| 117 |
|
|
| |
|
| 75% |
Uncovered Elements: 1 (4) |
Complexity: 2 |
Complexity Density: 0.5 |
1PASS
|
|
| 118 |
1 |
@Test... |
| 119 |
|
void getMaxKeySize() { |
| 120 |
1 |
LOGGER.log("TEST CASE: getMaxKeySize", LogLevel.DEBUG); |
| 121 |
|
|
| 122 |
1 |
try { |
| 123 |
1 |
assertEquals(2147483647, cryptoTools.getMaxKeySize(CipherAlgorithm.RSA)); |
| 124 |
|
} catch (NoSuchAlgorithmException ex) { |
| 125 |
0 |
LOGGER.catchException(ex); |
| 126 |
|
} |
| 127 |
|
} |
| 128 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
1PASS
|
|
| 129 |
1 |
@Test... |
| 130 |
|
void calculateRsaKeyPair() { |
| 131 |
1 |
LOGGER.log("TEST CASE: calculateRsaKeyPair", LogLevel.DEBUG); |
| 132 |
|
|
| 133 |
1 |
KeyPair store = cryptoTools.generateCipherKeyPair(CipherAlgorithm.RSA); |
| 134 |
1 |
byte[] privateKey = store.getPrivate().getEncoded(); |
| 135 |
1 |
byte[] publicKey = store.getPublic().getEncoded(); |
| 136 |
|
|
| 137 |
1 |
assertNotEquals(publicKey.length, privateKey.length); |
| 138 |
|
} |
| 139 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
1PASS
|
|
| 140 |
1 |
@Test... |
| 141 |
|
void failCalculateKeyPair() { |
| 142 |
1 |
LOGGER.log("TEST CASE: failCalculateKeyPair", LogLevel.DEBUG); |
| 143 |
|
|
| 144 |
1 |
assertNull(cryptoTools.generateCipherKeyPair(null)); |
| 145 |
|
} |
| 146 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
1PASS
|
|
| 147 |
1 |
@Test... |
| 148 |
|
void failWriteKeyPairToFile() { |
| 149 |
1 |
LOGGER.log("TEST CASE: failWriteKeyPairToFile", LogLevel.DEBUG); |
| 150 |
|
|
| 151 |
1 |
cryptoTools.saveKeyPairToFile(DIRECTORY, null); |
| 152 |
|
|
| 153 |
1 |
assertFalse(new File(DIRECTORY + "NotExist.key").exists()); |
| 154 |
1 |
assertFalse(new File(DIRECTORY + "NotExist.pub").exists()); |
| 155 |
|
} |
| 156 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
1PASS
|
|
| 157 |
1 |
@Test... |
| 158 |
|
@Order(1) |
| 159 |
|
void writeKeyPairToFile() { |
| 160 |
1 |
LOGGER.log("TEST CASE: writeKeyPairToFile", LogLevel.DEBUG); |
| 161 |
|
|
| 162 |
1 |
cryptoTools.saveKeyPairToFile(DIRECTORY, |
| 163 |
|
cryptoTools.generateCipherKeyPair(CipherAlgorithm.RSA)); |
| 164 |
|
|
| 165 |
1 |
assertTrue(new File(DIRECTORY + CipherAlgorithm.RSA + ".key").exists()); |
| 166 |
1 |
assertTrue(new File(DIRECTORY + CipherAlgorithm.RSA + ".pub").exists()); |
| 167 |
|
} |
| 168 |
|
|
| |
|
| 88.9% |
Uncovered Elements: 1 (9) |
Complexity: 2 |
Complexity Density: 0.22 |
1PASS
|
|
| 169 |
1 |
@Test... |
| 170 |
|
@Order(2) |
| 171 |
|
void writeKeyPairToFileInHomeDir() { |
| 172 |
1 |
LOGGER.log("TEST CASE: writeKeyPairToFile", LogLevel.DEBUG); |
| 173 |
|
|
| 174 |
1 |
cryptoTools.saveKeyPairToFile(null, |
| 175 |
|
cryptoTools.generateCipherKeyPair(CipherAlgorithm.RSA)); |
| 176 |
|
|
| 177 |
1 |
String dir = Constraints.SYSTEM_USER_HOME_DIR + "/"; |
| 178 |
|
|
| 179 |
1 |
assertTrue(new File(dir + CipherAlgorithm.RSA + ".key").exists()); |
| 180 |
1 |
assertTrue(new File(dir + CipherAlgorithm.RSA + ".pub").exists()); |
| 181 |
|
|
| 182 |
1 |
try { |
| 183 |
1 |
assertTrue(new File(dir + CipherAlgorithm.RSA + ".key").delete()); |
| 184 |
1 |
assertTrue(new File(dir + CipherAlgorithm.RSA + ".pub").delete()); |
| 185 |
|
|
| 186 |
|
} catch (Exception ex) { |
| 187 |
0 |
LOGGER.catchException(ex); |
| 188 |
|
} |
| 189 |
|
} |
| 190 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
1PASS
|
|
| 191 |
1 |
@Test... |
| 192 |
|
@Order(3) |
| 193 |
|
void loadPrivateRsaKeyFromFile() { |
| 194 |
1 |
LOGGER.log("TEST CASE: loadPrivateRsaKeyFromFile", LogLevel.DEBUG); |
| 195 |
|
|
| 196 |
1 |
PrivateKey privateKey = cryptoTools.loadPrivateKeyFile( |
| 197 |
|
DIRECTORY + CipherAlgorithm.RSA + ".key", CipherAlgorithm.RSA); |
| 198 |
1 |
assertEquals(CipherAlgorithm.RSA.toString(), privateKey.getAlgorithm()); |
| 199 |
|
} |
| 200 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
1PASS
|
|
| 201 |
1 |
@Test... |
| 202 |
|
@Order(4) |
| 203 |
|
void loadPublicKeyRsaFromFile() { |
| 204 |
1 |
LOGGER.log("TEST CASE: loadPublicRsaKeyFromFile", LogLevel.DEBUG); |
| 205 |
|
|
| 206 |
1 |
PublicKey publicRsaKey = cryptoTools.loadPublicKeyFile( |
| 207 |
|
DIRECTORY + CipherAlgorithm.RSA + ".pub", CipherAlgorithm.RSA); |
| 208 |
1 |
assertEquals(CipherAlgorithm.RSA.toString(), publicRsaKey.getAlgorithm()); |
| 209 |
|
} |
| 210 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
1PASS
|
|
| 211 |
1 |
@Test... |
| 212 |
|
void failLoadPrivateKeyFile() { |
| 213 |
1 |
LOGGER.log("TEST CASE: failLoadPrivateKeyFile", LogLevel.DEBUG); |
| 214 |
|
|
| 215 |
1 |
assertNull(cryptoTools.loadPrivateKeyFile(null, CipherAlgorithm.RSA)); |
| 216 |
|
} |
| 217 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
1PASS
|
|
| 218 |
1 |
@Test... |
| 219 |
|
void failLoadPublicKeyFile() { |
| 220 |
1 |
LOGGER.log("TEST CASE: failLoadPublicKeyFile", LogLevel.DEBUG); |
| 221 |
|
|
| 222 |
1 |
assertNull(cryptoTools.loadPublicKeyFile(null, CipherAlgorithm.RSA)); |
| 223 |
|
} |
| 224 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
1PASS
|
|
| 225 |
1 |
@Test... |
| 226 |
|
void generateRandomDoubleValue() { |
| 227 |
1 |
LOGGER.log("TEST CASE: generateRandomDoubleValue", LogLevel.DEBUG); |
| 228 |
|
|
| 229 |
1 |
assertNotEquals(.0, |
| 230 |
|
cryptoTools.generateRandomNumber(RNGAlgorithm.DRBG)); |
| 231 |
|
} |
| 232 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
1PASS
|
|
| 233 |
1 |
@Test... |
| 234 |
|
void failGenerateRandomDoubleValue() { |
| 235 |
1 |
LOGGER.log("TEST CASE: generateRandomDoubleValue", LogLevel.DEBUG); |
| 236 |
|
|
| 237 |
1 |
assertEquals(.0, |
| 238 |
|
cryptoTools.generateRandomNumber(null)); |
| 239 |
|
} |
| 240 |
|
} |