blob: 888c888d423fb115161c250449630eab73922793 [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 +020021import org.apache.commons.codec.binary.Base64;
22import org.apache.commons.lang.RandomStringUtils;
23import org.junit.Test;
Tomasz Golabeke558a642019-08-21 10:40:45 +020024import org.openecomp.sdc.securityutil.CipherUtil;
25import org.openecomp.sdc.securityutil.CipherUtilException;
Yuli Shlosberga4eeb112019-01-07 16:23:36 +020026
Yuli Shlosberga4eeb112019-01-07 16:23:36 +020027import static org.junit.Assert.*;
28
29public class CipherUtilTest {
30
31 private static final String KEY = "AGLDdG4D04BKm2IxIWEr8o==";
32 private static final String DATA = "data";
33
34 @Test
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);
42 }
43
44 @Test
45 public void encryptInvalidKey() {
46 try {
47 CipherUtil.encryptPKC(DATA, "invalidKey");
48 fail();
49 } catch (CipherUtilException ex) {
50 assertTrue(ex.getMessage().contains("Invalid AES key length"));
51 }
52 }
53
54 @Test
55 public void decryptInvalidKey() {
56 try {
57 CipherUtil.decryptPKC(DATA, "invalidKey");
58 fail();
59 } catch (CipherUtilException ex) {
60 assertTrue(ex.getMessage().contains("length"));
61 }
62 }
63
64 @Test
65 public void decryptInvalidData() {
66 try {
67 CipherUtil.decryptPKC(DATA, KEY);
68 fail();
69 } catch (CipherUtilException ex) {
70 assertTrue(ex.getMessage().contains("Wrong IV length"));
71 }
72 }
73}