blob: 6d539b6391b7a679977361619aa372b73587e767 [file] [log] [blame]
Tomasz Golabekc8fcbbc2019-07-09 08:42:59 +02001/*-
2 * ============LICENSE_START=======================================================
3 * SDC
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
Yuli Shlosberga4eeb112019-01-07 16:23:36 +020021package org.onap.sdc.security;
22
23import org.apache.commons.codec.binary.Base64;
24import org.apache.commons.lang.RandomStringUtils;
25import org.junit.Test;
26
27import java.util.Random;
28
29import static org.apache.commons.codec.binary.Base64.encodeBase64String;
30import static org.junit.Assert.*;
31
32public class CipherUtilTest {
33
34 private static final String KEY = "AGLDdG4D04BKm2IxIWEr8o==";
35 private static final String DATA = "data";
36
37 @Test
38 public void encryptDecryptPKC() throws CipherUtilException {
39 String generatedKey = RandomStringUtils.randomAlphabetic(16);
40 String base64Key = Base64.encodeBase64String(generatedKey.getBytes());
41 String encrypted = CipherUtil.encryptPKC(DATA, base64Key);
42 assertNotEquals(DATA, encrypted);
43 String decrypted = CipherUtil.decryptPKC(encrypted, base64Key);
44 assertEquals(decrypted, DATA);
45 }
46
47 @Test
48 public void encryptInvalidKey() {
49 try {
50 CipherUtil.encryptPKC(DATA, "invalidKey");
51 fail();
52 } catch (CipherUtilException ex) {
53 assertTrue(ex.getMessage().contains("Invalid AES key length"));
54 }
55 }
56
57 @Test
58 public void decryptInvalidKey() {
59 try {
60 CipherUtil.decryptPKC(DATA, "invalidKey");
61 fail();
62 } catch (CipherUtilException ex) {
63 assertTrue(ex.getMessage().contains("length"));
64 }
65 }
66
67 @Test
68 public void decryptInvalidData() {
69 try {
70 CipherUtil.decryptPKC(DATA, KEY);
71 fail();
72 } catch (CipherUtilException ex) {
73 assertTrue(ex.getMessage().contains("Wrong IV length"));
74 }
75 }
76}