Tomasz Golabek | c8fcbbc | 2019-07-09 08:42:59 +0200 | [diff] [blame] | 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 | |
Yuli Shlosberg | a4eeb11 | 2019-01-07 16:23:36 +0200 | [diff] [blame] | 21 | import org.apache.commons.codec.binary.Base64; |
| 22 | import org.apache.commons.lang.RandomStringUtils; |
| 23 | import org.junit.Test; |
Tomasz Golabek | e558a64 | 2019-08-21 10:40:45 +0200 | [diff] [blame^] | 24 | import org.openecomp.sdc.securityutil.CipherUtil; |
| 25 | import org.openecomp.sdc.securityutil.CipherUtilException; |
Yuli Shlosberg | a4eeb11 | 2019-01-07 16:23:36 +0200 | [diff] [blame] | 26 | |
Yuli Shlosberg | a4eeb11 | 2019-01-07 16:23:36 +0200 | [diff] [blame] | 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("Wrong IV length")); |
| 71 | } |
| 72 | } |
| 73 | } |