2 * ============LICENSE_START=======================================================
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
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=========================================================
21 package org.openecomp.sdc.securityutil;
23 import org.apache.commons.codec.binary.Base64;
24 import org.apache.commons.lang3.RandomStringUtils;
25 import org.junit.Test;
27 import static org.junit.Assert.*;
29 public class CipherUtilTest {
31 private static final String KEY = "AGLDdG4D04BKm2IxIWEr8o==";
32 private static final String DATA = "data";
35 public void encryptDecryptPKC() throws CipherUtilException {
36 String generatedKey = RandomStringUtils.randomAlphabetic(16);
37 String base64Key = Base64.encodeBase64String(generatedKey.getBytes());
38 String encrypted = CipherUtil.encryptPKC(DATA, base64Key);
39 assertNotEquals(DATA, encrypted);
40 String decrypted = CipherUtil.decryptPKC(encrypted, base64Key);
41 assertEquals(decrypted, DATA);
45 public void encryptInvalidKey() {
47 CipherUtil.encryptPKC(DATA, "invalidKey");
49 } catch (CipherUtilException ex) {
50 assertTrue(ex.getMessage().contains("Invalid AES key length"));
55 public void decryptInvalidKey() {
57 CipherUtil.decryptPKC(DATA, "invalidKey");
59 } catch (CipherUtilException ex) {
60 assertTrue(ex.getMessage().contains("length"));
65 public void decryptInvalidData() {
67 CipherUtil.decryptPKC(DATA, KEY);
69 } catch (CipherUtilException ex) {
70 assertTrue(ex.getMessage().contains("Input too short"));