Merge "FIx Sonar Issues reported on ActionPolicyController"
[policy/engine.git] / PolicyEngineUtils / src / test / java / org / onap / policy / utils / test / CryptoUtilsTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * PolicyEngineUtils
4  * ================================================================================
5  * Copyright (C) 2018 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 package org.onap.policy.utils.test;
21
22 import static org.junit.Assert.assertEquals;
23 import static org.junit.Assert.assertArrayEquals;
24
25 import java.nio.charset.StandardCharsets;
26 import java.security.InvalidAlgorithmParameterException;
27 import java.security.InvalidKeyException;
28 import java.security.NoSuchAlgorithmException;
29
30 import javax.crypto.BadPaddingException;
31 import javax.crypto.IllegalBlockSizeException;
32 import javax.crypto.NoSuchPaddingException;
33
34 import org.junit.Test;
35 import org.onap.policy.utils.CryptoUtils;
36
37 public class CryptoUtilsTest {
38
39         @Test
40         public final void testDecryptTxt() throws InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException,
41                         InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException
42                          {
43                 String decryptedTxt = new String(CryptoUtils.decryptTxt("g0uHKXCLyzJ6wSbpphNGsA=="), StandardCharsets.UTF_8);
44                 assertEquals("mypass", decryptedTxt);
45         }
46
47         @Test
48         public final void testDecryptTxtWithKey() throws InvalidKeyException, NoSuchAlgorithmException,
49                         NoSuchPaddingException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException
50                          {
51                 String decryptedTxt = new String(CryptoUtils.decryptTxt("g0uHKXCLyzJ6wSbpphNGsA==", "bmpybWJrbGN4dG9wbGF3Zg=="),
52                                 StandardCharsets.UTF_8);
53                 assertEquals("mypass", decryptedTxt);
54         }
55
56         @Test
57         public final void testDecryptTxtNoEx() {
58                 String decryptedTxt = new String(CryptoUtils.decryptTxtNoEx("g0uHKXCLyzJ6wSbpphNGsA=="),
59                                 StandardCharsets.UTF_8);
60                 assertEquals("mypass", decryptedTxt);
61
62         }
63
64         @Test
65         public final void testDecryptTxtNoExStr() {
66                 assertEquals("mypass", CryptoUtils.decryptTxtNoExStr("g0uHKXCLyzJ6wSbpphNGsA=="));
67         }
68         @Test
69         public final void testDecryptTxtNoExInvalidInput() {
70                 assertArrayEquals(new byte[0], CryptoUtils.decryptTxtNoEx(null));
71                 assertArrayEquals(new byte[0], CryptoUtils.decryptTxtNoEx(""));
72                 // ensure backward compatibility
73                 assertEquals("bogus", new String(CryptoUtils.decryptTxtNoEx("bogus"), StandardCharsets.UTF_8));
74                 assertEquals("admin123", CryptoUtils.decryptTxtNoExStr("admin123"));
75                 assertEquals("password", CryptoUtils.decryptTxtNoExStr("password"));
76         }
77
78         @Test(expected = IllegalArgumentException.class)
79         public final void testDecryptTxtInvalidInput() throws InvalidKeyException, NoSuchAlgorithmException,
80                         NoSuchPaddingException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException
81                          {
82                 CryptoUtils.decryptTxt("bogus");
83         }
84
85         @Test
86         public final void testEncryptTxt() throws InvalidKeyException, NoSuchPaddingException,
87                         InvalidAlgorithmParameterException, NoSuchAlgorithmException, IllegalBlockSizeException,
88                         BadPaddingException {
89                 String txtStr = "mypass";
90                 byte[] txt = txtStr.getBytes(StandardCharsets.UTF_8);
91                 assertEquals("g0uHKXCLyzJ6wSbpphNGsA==", CryptoUtils.encryptTxt(txt));
92         }
93
94         @Test
95         public final void testEncryptTxtWithKey() throws InvalidKeyException,
96                         NoSuchPaddingException, InvalidAlgorithmParameterException, NoSuchAlgorithmException,
97                         IllegalBlockSizeException, BadPaddingException {
98                 String txtStr = "mypass";
99                 byte[] txt = txtStr.getBytes(StandardCharsets.UTF_8);
100                 assertEquals("g0uHKXCLyzJ6wSbpphNGsA==", CryptoUtils.encryptTxt(txt, "bmpybWJrbGN4dG9wbGF3Zg=="));
101         }
102
103         @Test
104         public final void testEncryptTxtNoEx() {
105                 String txtStr = "mypass";
106                 byte[] txt = txtStr.getBytes(StandardCharsets.UTF_8);
107                 assertEquals("g0uHKXCLyzJ6wSbpphNGsA==", CryptoUtils.encryptTxtNoEx(txt));
108         }
109
110         @Test
111         public final void testEncryptTxtNoExInvalidInput() {
112                 String txtStr = "";
113                 byte[] txt = txtStr.getBytes(StandardCharsets.UTF_8);
114                 assertEquals("", CryptoUtils.encryptTxtNoEx(txt));
115                 assertEquals("", CryptoUtils.encryptTxtNoEx(null));
116         }
117
118         @Test(expected = InvalidKeyException.class)
119         public final void testEncryptTxtWithKeyInvalid() throws InvalidKeyException,
120                         NoSuchPaddingException, InvalidAlgorithmParameterException, NoSuchAlgorithmException,
121                         IllegalBlockSizeException, BadPaddingException {
122                 String txtStr = "mypass";
123                 byte[] txt = txtStr.getBytes(StandardCharsets.UTF_8);
124                 CryptoUtils.encryptTxt(txt, "mykey");
125         }
126
127
128 }