3765ff2e35b20050249c2538373aa8e4a676b14f
[policy/engine.git] / PolicyEngineUtils / src / test / java / org / onap / policy / utils / PeCryptoUtilsTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP-REST
4  * ================================================================================
5  * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.utils;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertNotNull;
25 import java.security.GeneralSecurityException;
26 import org.junit.Before;
27 import org.junit.Test;
28 import org.powermock.reflect.Whitebox;
29
30
31 public class PeCryptoUtilsTest {
32     private final String pass = "policy_user";
33     private final String secretKey = "bmpybWJrbGN4dG9wbGF3Zg==";
34     private final String encryptedPass = "enc:5ID9PoqWIzBaut+KQcAFBtci9CKDRcCNRHRjdBnXM5U=";
35     private static final String PROP_AES_KEY = "org.onap.policy.encryption.aes.key";
36
37     @Before
38     public void reset() {
39         Whitebox.setInternalState( PeCryptoUtils.class, "cryptoUtils", (PeCryptoUtils)null);
40
41     }
42
43     @Test
44     public void testEncrypt() throws GeneralSecurityException {
45         assertEquals(pass, PeCryptoUtils.encrypt(pass));
46         PeCryptoUtils.initAesKey(secretKey);
47         System.out.println("original value : " + pass + "  encrypted value: " + PeCryptoUtils.encrypt(pass));
48         assertNotNull(PeCryptoUtils.encrypt(pass));
49     }
50
51     @Test
52     public void testDecrypt() throws Exception {
53         assertEquals(pass, PeCryptoUtils.decrypt(pass));
54         System.setProperty(PROP_AES_KEY, secretKey);
55         PeCryptoUtils.initAesKey(null);
56         System.clearProperty(PROP_AES_KEY);
57         assertEquals(pass, PeCryptoUtils.decrypt(encryptedPass));
58         Whitebox.setInternalState( PeCryptoUtils.class, "cryptoUtils", (PeCryptoUtils)null);
59         Whitebox.setInternalState( PeCryptoUtils.class, "secretKey", secretKey);
60         PeCryptoUtils.initAesKey(" ");
61         assertEquals(pass, PeCryptoUtils.decrypt(pass));
62     }
63
64 }