Update vulnerable package dependencies
[sdc.git] / openecomp-be / backend / openecomp-sdc-security-util / src / test / java / org / openecomp / sdc / securityutil / CipherUtilTest.java
1 /*-
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
21 package org.openecomp.sdc.securityutil;
22
23 import org.apache.commons.codec.binary.Base64;
24 import org.apache.commons.lang3.RandomStringUtils;
25 import org.junit.Test;
26
27 import static org.junit.Assert.*;
28
29 public 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("Input too short"));
71         }
72     }
73 }