|
| 1 | +package com.mastercard.developer.encryption; |
| 2 | + |
| 3 | +import com.mastercard.developer.test.TestUtils; |
| 4 | +import org.apache.commons.codec.DecoderException; |
| 5 | +import org.junit.Rule; |
| 6 | +import org.junit.Test; |
| 7 | +import org.junit.rules.ExpectedException; |
| 8 | + |
| 9 | +import static org.hamcrest.CoreMatchers.isA; |
| 10 | +import static org.junit.Assert.assertEquals; |
| 11 | +import static org.junit.Assert.assertNotNull; |
| 12 | + |
| 13 | +public class FieldLevelEncryptionParamsTest { |
| 14 | + |
| 15 | + @Rule |
| 16 | + public ExpectedException expectedException = ExpectedException.none(); |
| 17 | + |
| 18 | + @Test |
| 19 | + public void testGenerate_Nominal() throws Exception { |
| 20 | + |
| 21 | + // GIVEN |
| 22 | + FieldLevelEncryptionConfig config = TestUtils.getTestFieldLevelEncryptionConfigBuilder().build(); |
| 23 | + |
| 24 | + // WHEN |
| 25 | + FieldLevelEncryptionParams params = FieldLevelEncryptionParams.generate(config); |
| 26 | + |
| 27 | + // THEN |
| 28 | + assertNotNull(params.getIvValue()); |
| 29 | + assertNotNull(params.getIvSpec()); |
| 30 | + assertNotNull(params.getEncryptedKeyValue()); |
| 31 | + assertNotNull(params.getSecretKey()); |
| 32 | + assertEquals("80810fc13a8319fcf0e2ec322c82a4c304b782cc3ce671176343cfe8160c2279", params.getEncryptionCertificateFingerprintValue()); |
| 33 | + assertEquals("761b003c1eade3a5490e5000d37887baa5e6ec0e226c07706e599451fc032a79", params.getEncryptionKeyFingerprintValue()); |
| 34 | + assertEquals("SHA256", params.getOaepPaddingDigestAlgorithmValue()); |
| 35 | + } |
| 36 | + |
| 37 | + @Test |
| 38 | + public void testGetIvSpec_ShouldThrowEncryptionException_WhenFailsToDecodeIV() throws Exception { |
| 39 | + |
| 40 | + // GIVEN |
| 41 | + FieldLevelEncryptionConfig config = TestUtils.getTestFieldLevelEncryptionConfigBuilder().build(); |
| 42 | + FieldLevelEncryptionParams params = new FieldLevelEncryptionParams("INVALID VALUE", null, |
| 43 | + null, null, |
| 44 | + null,config); |
| 45 | + |
| 46 | + // THEN |
| 47 | + expectedException.expect(EncryptionException.class); |
| 48 | + expectedException.expectMessage("Failed to decode the provided IV value!"); |
| 49 | + expectedException.expectCause(isA(DecoderException.class)); |
| 50 | + |
| 51 | + // WHEN |
| 52 | + params.getIvSpec(); |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + public void testGetSecretKey_ShouldThrowEncryptionException_WhenFailsToEncryptedKey() throws Exception { |
| 57 | + |
| 58 | + // GIVEN |
| 59 | + FieldLevelEncryptionConfig config = TestUtils.getTestFieldLevelEncryptionConfigBuilder().build(); |
| 60 | + FieldLevelEncryptionParams params = new FieldLevelEncryptionParams(null, "INVALID VALUE", |
| 61 | + null, null, |
| 62 | + null,config); |
| 63 | + |
| 64 | + // THEN |
| 65 | + expectedException.expect(EncryptionException.class); |
| 66 | + expectedException.expectMessage("Failed to decode and unwrap the provided secret key value!"); |
| 67 | + expectedException.expectCause(isA(DecoderException.class)); |
| 68 | + |
| 69 | + // WHEN |
| 70 | + params.getSecretKey(); |
| 71 | + } |
| 72 | +} |
0 commit comments