Get policy in CsarInstaller
[clamp.git] / src / test / java / org / onap / clamp / clds / util / CryptoUtilsTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP CLAMP
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights
6  *                             reserved.
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
13  * 
14  * http://www.apache.org/licenses/LICENSE-2.0
15  * 
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  * ===================================================================
23  * 
24  */
25
26 package org.onap.clamp.clds.util;
27
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;
32
33 import java.security.InvalidKeyException;
34
35 import javax.crypto.KeyGenerator;
36 import javax.crypto.SecretKey;
37
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;
46
47 @RunWith(PowerMockRunner.class)
48 @PowerMockIgnore({ "javax.crypto.*", "com.sun.org.apache.xerces.*", "javax.xml.*", "org.xml.*", "javax.management.*" })
49 public class CryptoUtilsTest {
50
51     private final String data = "This is a test string";
52
53     @Test
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));
59     }
60
61     @Test
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);
74     }
75
76     @Test
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);
82
83         String encodedString = CryptoUtils.encrypt(data);
84         String decodedString = CryptoUtils.decrypt(encodedString);
85         assertEquals(data, decodedString);
86     }
87
88     @Test(expected = InvalidKeyException.class)
89     @PrepareForTest({ CryptoUtils.class })
90     public final void testEncryptionBadKey() throws Exception {
91         final String badEncryptionKey = "93210sd";
92         setAesEncryptionKeyEnv(badEncryptionKey);
93
94         CryptoUtils.encrypt(data);
95     }
96
97     private static void setAesEncryptionKeyEnv(String value) {
98         PowerMockito.mockStatic(System.class);
99         PowerMockito.when(System.getenv(eq("AES_ENCRYPTION_KEY"))).thenReturn(value);
100     }
101 }