2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
20 package org.onap.policy.utils.test;
22 import static org.junit.Assert.assertEquals;
23 import static org.junit.Assert.assertArrayEquals;
25 import java.nio.charset.StandardCharsets;
26 import java.security.InvalidAlgorithmParameterException;
27 import java.security.InvalidKeyException;
28 import java.security.NoSuchAlgorithmException;
30 import javax.crypto.BadPaddingException;
31 import javax.crypto.IllegalBlockSizeException;
32 import javax.crypto.NoSuchPaddingException;
34 import org.junit.Test;
35 import org.onap.policy.utils.CryptoUtils;
37 public class CryptoUtilsTest {
40 public final void testDecryptTxt() throws InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException,
41 InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException
43 String decryptedTxt = new String(CryptoUtils.decryptTxt("g0uHKXCLyzJ6wSbpphNGsA=="), StandardCharsets.UTF_8);
44 assertEquals("mypass", decryptedTxt);
48 public final void testDecryptTxtWithKey() throws InvalidKeyException, NoSuchAlgorithmException,
49 NoSuchPaddingException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException
51 String decryptedTxt = new String(CryptoUtils.decryptTxt("g0uHKXCLyzJ6wSbpphNGsA==", "bmpybWJrbGN4dG9wbGF3Zg=="),
52 StandardCharsets.UTF_8);
53 assertEquals("mypass", decryptedTxt);
57 public final void testDecryptTxtNoEx() {
58 String decryptedTxt = new String(CryptoUtils.decryptTxtNoEx("g0uHKXCLyzJ6wSbpphNGsA=="),
59 StandardCharsets.UTF_8);
60 assertEquals("mypass", decryptedTxt);
65 public final void testDecryptTxtNoExStr() {
66 assertEquals("mypass", CryptoUtils.decryptTxtNoExStr("g0uHKXCLyzJ6wSbpphNGsA=="));
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"));
78 @Test(expected = IllegalArgumentException.class)
79 public final void testDecryptTxtInvalidInput() throws InvalidKeyException, NoSuchAlgorithmException,
80 NoSuchPaddingException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException
82 CryptoUtils.decryptTxt("bogus");
86 public final void testEncryptTxt() throws InvalidKeyException, NoSuchPaddingException,
87 InvalidAlgorithmParameterException, NoSuchAlgorithmException, IllegalBlockSizeException,
89 String txtStr = "mypass";
90 byte[] txt = txtStr.getBytes(StandardCharsets.UTF_8);
91 assertEquals("g0uHKXCLyzJ6wSbpphNGsA==", CryptoUtils.encryptTxt(txt));
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=="));
104 public final void testEncryptTxtNoEx() {
105 String txtStr = "mypass";
106 byte[] txt = txtStr.getBytes(StandardCharsets.UTF_8);
107 assertEquals("g0uHKXCLyzJ6wSbpphNGsA==", CryptoUtils.encryptTxtNoEx(txt));
111 public final void testEncryptTxtNoExInvalidInput() {
113 byte[] txt = txtStr.getBytes(StandardCharsets.UTF_8);
114 assertEquals("", CryptoUtils.encryptTxtNoEx(txt));
115 assertEquals("", CryptoUtils.encryptTxtNoEx(null));
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");