Yuli Shlosberg | a4eeb11 | 2019-01-07 16:23:36 +0200 | [diff] [blame] | 1 | package org.onap.sdc.security; |
| 2 | |
| 3 | import org.apache.commons.codec.binary.Base64; |
| 4 | import org.apache.commons.lang.RandomStringUtils; |
| 5 | import org.junit.Test; |
| 6 | |
| 7 | import java.util.Random; |
| 8 | |
| 9 | import static org.apache.commons.codec.binary.Base64.encodeBase64String; |
| 10 | import static org.junit.Assert.*; |
| 11 | |
| 12 | public 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 | } |