summaryrefslogtreecommitdiffstats
path: root/utils/src/test/java/org/onap/policy/common/utils/security/CryptoUtilsTest.java
diff options
context:
space:
mode:
authorJim Hahn <jrh3@att.com>2019-06-12 14:32:25 -0400
committerJim Hahn <jrh3@att.com>2019-06-12 17:36:59 -0400
commitea262e6da52fd4da0733f02998f87aebaf502ddb (patch)
treea7cded567521f0141f31d9f8c185eccdaf2a8979 /utils/src/test/java/org/onap/policy/common/utils/security/CryptoUtilsTest.java
parente671e9fa5d09ec696cb24aeefaf10ddbc7c705d7 (diff)
Apply simple sonar fixes
Note: A number of these were identified, by SonarLint, in the Test classes, which are not typically scanned by Sonar. Removed unnecessary imports. Removed unneeded "throws Xxx". Replaced lambda with method references. Replaced duplicate strings with constants. Replaced try-fail-catch with assert-j methods to eliminate sonar complaints about duplicate failure messages. Added missing @Override annotations. Use map.computeIfAbsent() where appropriate. Also fixed some minor checkstyle issues. Removed unneeded "volatile" declarations. Replaced some if-else constructs with "?:" construct, per sonar. Replaced Object.wait() with CountDownLatch.await(); according to sonar (and javadocs), Object.wait() can return due to "spurious wakeups". Fixed issue whereby CryptoUtilsTest wouldn't run in my Eclipse. Change-Id: Ib6b71ed65662cfd6209400dac57ed69279bf29ec Issue-ID: POLICY-1791 Signed-off-by: Jim Hahn <jrh3@att.com>
Diffstat (limited to 'utils/src/test/java/org/onap/policy/common/utils/security/CryptoUtilsTest.java')
-rw-r--r--utils/src/test/java/org/onap/policy/common/utils/security/CryptoUtilsTest.java73
1 files changed, 37 insertions, 36 deletions
diff --git a/utils/src/test/java/org/onap/policy/common/utils/security/CryptoUtilsTest.java b/utils/src/test/java/org/onap/policy/common/utils/security/CryptoUtilsTest.java
index fd3daee8..9a88918d 100644
--- a/utils/src/test/java/org/onap/policy/common/utils/security/CryptoUtilsTest.java
+++ b/utils/src/test/java/org/onap/policy/common/utils/security/CryptoUtilsTest.java
@@ -24,7 +24,6 @@ import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import java.security.GeneralSecurityException;
-
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -35,86 +34,88 @@ import org.slf4j.LoggerFactory;
public class CryptoUtilsTest {
private static Logger logger = LoggerFactory.getLogger(CryptoUtilsTest.class);
- private final String pass = "HelloWorld";
- private final String secretKey = "12345678901234567890123456789012";
- private final String encryptedPass = "enc:8XxseP5W5ODxzPrReNKd9JBYLv0iiAzy9BHnMKau5yg=";
+ private static final String PASS = "HelloWorld";
+ private static final String SECRET_KEY = "MTIzNDU2Nzg5MDEyMzQ1Ng==";
+ private static final String ENCRYPTED_PASS = "enc:hcI2XVX+cxPz/6rlbebkWpCFF6WPbBtT7iJRr2VHUkA=";
+ private static final String DECRYPTED_MSG = "encrypted value: {} decrypted value : {}";
+ private static final String ENCRYPTED_MSG = "original value : {} encrypted value: {}";
@Test
public void testEncrypt() throws GeneralSecurityException {
logger.info("testEncrypt:");
- CryptoUtils cryptoUtils = new CryptoUtils(secretKey);
- String encryptedValue = cryptoUtils.encrypt(pass);
- logger.info("original value : " + pass + " encrypted value: " + encryptedValue);
+ CryptoUtils cryptoUtils = new CryptoUtils(SECRET_KEY);
+ String encryptedValue = cryptoUtils.encrypt(PASS);
+ logger.info(ENCRYPTED_MSG, PASS, encryptedValue);
String decryptedValue = cryptoUtils.decrypt(encryptedValue);
- logger.info("encrypted value: " + encryptedValue + " decrypted value : " + decryptedValue);
- assertEquals(pass, decryptedValue);
+ logger.info(DECRYPTED_MSG, encryptedValue, decryptedValue);
+ assertEquals(PASS, decryptedValue);
}
@Test
public void testDecrypt() throws GeneralSecurityException {
logger.info("testDecrypt:");
- CryptoUtils cryptoUtils = new CryptoUtils(secretKey);
- String decryptedValue = cryptoUtils.decrypt(encryptedPass);
- logger.info("encrypted value: " + encryptedPass + " decrypted value : " + decryptedValue);
- assertEquals(pass, decryptedValue);
+ CryptoUtils cryptoUtils = new CryptoUtils(SECRET_KEY);
+ String decryptedValue = cryptoUtils.decrypt(ENCRYPTED_PASS);
+ logger.info(DECRYPTED_MSG, ENCRYPTED_PASS, decryptedValue);
+ assertEquals(PASS, decryptedValue);
}
@Test
public void testStaticEncrypt() {
logger.info("testStaticEncrypt:");
- String encryptedValue = CryptoUtils.encrypt(pass, secretKey);
- logger.info("original value : " + pass + " encrypted value: " + encryptedValue);
+ String encryptedValue = CryptoUtils.encrypt(PASS, SECRET_KEY);
+ logger.info(ENCRYPTED_MSG, PASS, encryptedValue);
- String decryptedValue = CryptoUtils.decrypt(encryptedValue, secretKey);
- logger.info("encrypted value: " + encryptedValue + " decrypted value : " + decryptedValue);
- assertEquals(pass, decryptedValue);
+ String decryptedValue = CryptoUtils.decrypt(encryptedValue, SECRET_KEY);
+ logger.info(DECRYPTED_MSG, encryptedValue, decryptedValue);
+ assertEquals(PASS, decryptedValue);
}
@Test
public void testStaticDecrypt() {
logger.info("testStaticDecrypt:");
- String decryptedValue = CryptoUtils.decrypt(encryptedPass, secretKey);
- logger.info("encrypted value: " + encryptedPass + " decrypted value : " + decryptedValue);
- assertEquals(pass, decryptedValue);
+ String decryptedValue = CryptoUtils.decrypt(ENCRYPTED_PASS, SECRET_KEY);
+ logger.info(DECRYPTED_MSG, ENCRYPTED_PASS, decryptedValue);
+ assertEquals(PASS, decryptedValue);
}
@Test
public void testBadInputs() {
- String badKey = CryptoUtils.encrypt(pass, "test");
- assertEquals(pass, badKey);
+ String badKey = CryptoUtils.encrypt(PASS, "test");
+ assertEquals(PASS, badKey);
- String badDecrypt = CryptoUtils.decrypt(encryptedPass, "");
- assertEquals(encryptedPass, badDecrypt);
+ String badDecrypt = CryptoUtils.decrypt(ENCRYPTED_PASS, "");
+ assertEquals(ENCRYPTED_PASS, badDecrypt);
- String emptyValue = CryptoUtils.encrypt(new String(), secretKey);
+ String emptyValue = CryptoUtils.encrypt("", SECRET_KEY);
assertEquals("", emptyValue);
- String emptyDecrypt = CryptoUtils.decrypt(new String(), secretKey);
+ String emptyDecrypt = CryptoUtils.decrypt("", SECRET_KEY);
assertEquals("", emptyDecrypt);
- String nullValue = CryptoUtils.encrypt(null, secretKey);
+ String nullValue = CryptoUtils.encrypt(null, SECRET_KEY);
assertNull(nullValue);
- String nullDecrypt = CryptoUtils.decrypt(null, secretKey);
+ String nullDecrypt = CryptoUtils.decrypt(null, SECRET_KEY);
assertNull(nullDecrypt);
}
@Test
public void testAll() {
logger.info("testAll:");
- String encryptedValue = CryptoUtils.encrypt(pass, secretKey);
- logger.info("original value : " + pass + " encrypted value: " + encryptedValue);
+ String encryptedValue = CryptoUtils.encrypt(PASS, SECRET_KEY);
+ logger.info(ENCRYPTED_MSG, PASS, encryptedValue);
- String encryptedAgain = CryptoUtils.encrypt(encryptedValue, secretKey);
+ String encryptedAgain = CryptoUtils.encrypt(encryptedValue, SECRET_KEY);
assertEquals(encryptedValue, encryptedAgain);
- String decryptedValue = CryptoUtils.decrypt(encryptedAgain, secretKey);
- logger.info("encrypted value: " + encryptedAgain + " decrypted value : " + decryptedValue);
- assertEquals(pass, decryptedValue);
+ String decryptedValue = CryptoUtils.decrypt(encryptedAgain, SECRET_KEY);
+ logger.info(DECRYPTED_MSG, encryptedAgain, decryptedValue);
+ assertEquals(PASS, decryptedValue);
- String decryptedAgain = CryptoUtils.decrypt(decryptedValue, secretKey);
+ String decryptedAgain = CryptoUtils.decrypt(decryptedValue, SECRET_KEY);
assertEquals(decryptedValue, decryptedAgain);
}
} \ No newline at end of file