blob: 753902fcf8df7ab7fc3975db9bcdbed0d5f62f0e [file] [log] [blame]
Yuli Shlosberga4eeb112019-01-07 16:23:36 +02001package org.onap.sdc.security;
2
3import org.apache.commons.codec.binary.Base64;
4import org.apache.commons.lang.RandomStringUtils;
5import org.junit.Test;
6
7import java.util.Random;
8
9import static org.apache.commons.codec.binary.Base64.encodeBase64String;
10import static org.junit.Assert.*;
11
12public class CipherUtilTest {
13
14 private static final String KEY = "AGLDdG4D04BKm2IxIWEr8o==";
15 private static final String DATA = "data";
16
17 @Test
18 public void encryptDecryptPKC() throws CipherUtilException {
19 String generatedKey = RandomStringUtils.randomAlphabetic(16);
20 String base64Key = Base64.encodeBase64String(generatedKey.getBytes());
21 String encrypted = CipherUtil.encryptPKC(DATA, base64Key);
22 assertNotEquals(DATA, encrypted);
23 String decrypted = CipherUtil.decryptPKC(encrypted, base64Key);
24 assertEquals(decrypted, DATA);
25 }
26
27 @Test
28 public void encryptInvalidKey() {
29 try {
30 CipherUtil.encryptPKC(DATA, "invalidKey");
31 fail();
32 } catch (CipherUtilException ex) {
33 assertTrue(ex.getMessage().contains("Invalid AES key length"));
34 }
35 }
36
37 @Test
38 public void decryptInvalidKey() {
39 try {
40 CipherUtil.decryptPKC(DATA, "invalidKey");
41 fail();
42 } catch (CipherUtilException ex) {
43 assertTrue(ex.getMessage().contains("length"));
44 }
45 }
46
47 @Test
48 public void decryptInvalidData() {
49 try {
50 CipherUtil.decryptPKC(DATA, KEY);
51 fail();
52 } catch (CipherUtilException ex) {
53 assertTrue(ex.getMessage().contains("Wrong IV length"));
54 }
55 }
56}