2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights
7 * ================================================================================
8 * Modifications Copyright (c) 2019 Samsung
9 * ================================================================================
10 * Licensed under the Apache License, Version 2.0 (the "License");
11 * you may not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
14 * http://www.apache.org/licenses/LICENSE-2.0
16 * Unless required by applicable law or agreed to in writing, software
17 * distributed under the License is distributed on an "AS IS" BASIS,
18 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 * See the License for the specific language governing permissions and
20 * limitations under the License.
21 * ============LICENSE_END============================================
22 * ===================================================================
26 package org.onap.clamp.clds.util;
28 import static org.junit.Assert.assertEquals;
29 import static org.junit.Assert.assertNotEquals;
30 import static org.junit.Assert.assertNotNull;
31 import static org.mockito.ArgumentMatchers.eq;
33 import java.security.InvalidKeyException;
35 import javax.crypto.KeyGenerator;
36 import javax.crypto.SecretKey;
38 import org.apache.commons.codec.binary.Hex;
39 import org.apache.commons.lang3.ArrayUtils;
40 import org.junit.Test;
41 import org.junit.runner.RunWith;
42 import org.powermock.api.mockito.PowerMockito;
43 import org.powermock.core.classloader.annotations.PowerMockIgnore;
44 import org.powermock.core.classloader.annotations.PrepareForTest;
45 import org.powermock.modules.junit4.PowerMockRunner;
47 @RunWith(PowerMockRunner.class)
48 @PowerMockIgnore({ "javax.crypto.*", "com.sun.org.apache.xerces.*", "javax.xml.*", "org.xml.*", "javax.management.*" })
49 public class CryptoUtilsTest {
51 private final String data = "This is a test string";
54 @PrepareForTest({ CryptoUtils.class })
55 public final void testEncryption() throws Exception {
56 String encodedString = CryptoUtils.encrypt(data);
57 assertNotNull(encodedString);
58 assertEquals(data, CryptoUtils.decrypt(encodedString));
62 @PrepareForTest({ CryptoUtils.class })
63 public final void testEncryptedStringIsDifferent() throws Exception {
64 String encodedString1 = CryptoUtils.encrypt(data);
65 String encodedString2 = CryptoUtils.encrypt(data);
66 byte[] encryptedMessage1 = Hex.decodeHex(encodedString1.toCharArray());
67 byte[] encryptedMessage2 = Hex.decodeHex(encodedString2.toCharArray());
68 assertNotNull(encryptedMessage1);
69 assertNotNull(encryptedMessage2);
70 assertNotEquals(encryptedMessage1, encryptedMessage2);
71 byte[] subData1 = ArrayUtils.subarray(encryptedMessage1, 16, encryptedMessage1.length);
72 byte[] subData2 = ArrayUtils.subarray(encryptedMessage2, 16, encryptedMessage2.length);
73 assertNotEquals(subData1, subData2);
77 @PrepareForTest({ CryptoUtils.class })
78 public final void testEncryptionBaseOnRandomKey() throws Exception {
79 SecretKey secretKey = KeyGenerator.getInstance("AES").generateKey();
80 final String encryptionKey = String.valueOf(Hex.encodeHex(secretKey.getEncoded()));
81 setAesEncryptionKeyEnv(encryptionKey);
83 String encodedString = CryptoUtils.encrypt(data);
84 String decodedString = CryptoUtils.decrypt(encodedString);
85 assertEquals(data, decodedString);
88 @Test(expected = InvalidKeyException.class)
89 @PrepareForTest({ CryptoUtils.class })
90 public final void testEncryptionBadKey() throws Exception {
91 final String badEncryptionKey = "93210sd";
92 setAesEncryptionKeyEnv(badEncryptionKey);
94 CryptoUtils.encrypt(data);
97 private static void setAesEncryptionKeyEnv(String value) {
98 PowerMockito.mockStatic(System.class);
99 PowerMockito.when(System.getenv(eq("AES_ENCRYPTION_KEY"))).thenReturn(value);