77import org .junit .rules .ExpectedException ;
88
99import static com .mastercard .developer .encryption .FieldLevelEncryptionConfig .FieldValueEncoding .HEX ;
10+ import static org .hamcrest .MatcherAssert .assertThat ;
11+ import static org .hamcrest .core .IsEqual .equalTo ;
12+ import static org .junit .Assert .assertFalse ;
1013
1114public class FieldLevelEncryptionConfigBuilderTest {
1215
1316 @ Rule
1417 public ExpectedException expectedException = ExpectedException .none ();
1518
1619 @ Test
17- public void testBuild_Nominal () throws Exception {
20+ public void testBuild_Nominal_iv12 () throws Exception {
1821 FieldLevelEncryptionConfig config = FieldLevelEncryptionConfigBuilder .aFieldLevelEncryptionConfig ()
1922 .withEncryptionPath ("$.payload" , "$.encryptedPayload" )
2023 .withEncryptionCertificate (TestUtils .getTestEncryptionCertificate ())
@@ -35,6 +38,7 @@ public void testBuild_Nominal() throws Exception {
3538 .withIvFieldName ("iv" )
3639 .withIvHeaderName ("x-iv" )
3740 .withFieldValueEncoding (HEX )
41+ .withEncryptionIVSize (12 )
3842 .build ();
3943 Assert .assertNotNull (config );
4044 Assert .assertEquals (1 , config .encryptionPaths .size ());
@@ -56,6 +60,85 @@ public void testBuild_Nominal() throws Exception {
5660 Assert .assertEquals ("oaepPaddingDigestAlgorithm" , config .oaepPaddingDigestAlgorithmFieldName );
5761 Assert .assertEquals ("x-oaep-padding-digest-algorithm" , config .oaepPaddingDigestAlgorithmHeaderName );
5862 Assert .assertEquals (HEX , config .fieldValueEncoding );
63+ assertThat (config .getIVSize ().intValue (),equalTo (12 ));
64+ }
65+
66+ @ Test
67+ public void testBuild_Nominal_iv16 () throws Exception {
68+ FieldLevelEncryptionConfig config = FieldLevelEncryptionConfigBuilder .aFieldLevelEncryptionConfig ()
69+ .withEncryptionPath ("$.payload" , "$.encryptedPayload" )
70+ .withEncryptionCertificate (TestUtils .getTestEncryptionCertificate ())
71+ .withEncryptionCertificateFingerprint ("97A2FFE9F0D48960EF31E87FCD7A55BF7843FB4A9EEEF01BDB6032AD6FEF146B" )
72+ .withEncryptionKeyFingerprint ("F806B26BC4870E26986C70B6590AF87BAF4C2B56BB50622C51B12212DAFF2810" )
73+ .withEncryptionCertificateFingerprintFieldName ("publicCertificateFingerprint" )
74+ .withEncryptionCertificateFingerprintHeaderName ("x-public-certificate-fingerprint" )
75+ .withEncryptionKeyFingerprintFieldName ("publicKeyFingerprint" )
76+ .withEncryptionKeyFingerprintHeaderName ("x-public-key-fingerprint" )
77+ .withDecryptionPath ("$.encryptedPayload" , "$.payload" )
78+ .withDecryptionKey (TestUtils .getTestDecryptionKey ())
79+ .withOaepPaddingDigestAlgorithm ("SHA-512" )
80+ .withOaepPaddingDigestAlgorithmFieldName ("oaepPaddingDigestAlgorithm" )
81+ .withOaepPaddingDigestAlgorithmHeaderName ("x-oaep-padding-digest-algorithm" )
82+ .withEncryptedValueFieldName ("encryptedValue" )
83+ .withEncryptedKeyFieldName ("encryptedKey" )
84+ .withEncryptedKeyHeaderName ("x-encrypted-key" )
85+ .withIvFieldName ("iv" )
86+ .withIvHeaderName ("x-iv" )
87+ .withFieldValueEncoding (HEX )
88+ .withEncryptionIVSize (16 )
89+ .build ();
90+ Assert .assertNotNull (config );
91+ Assert .assertEquals (1 , config .encryptionPaths .size ());
92+ Assert .assertNotNull (config .encryptionCertificate );
93+ Assert .assertEquals ("97A2FFE9F0D48960EF31E87FCD7A55BF7843FB4A9EEEF01BDB6032AD6FEF146B" , config .encryptionCertificateFingerprint );
94+ Assert .assertEquals ("F806B26BC4870E26986C70B6590AF87BAF4C2B56BB50622C51B12212DAFF2810" , config .encryptionKeyFingerprint );
95+ Assert .assertEquals ("publicCertificateFingerprint" , config .encryptionCertificateFingerprintFieldName );
96+ Assert .assertEquals ("x-public-certificate-fingerprint" , config .encryptionCertificateFingerprintHeaderName );
97+ Assert .assertEquals ("publicKeyFingerprint" , config .encryptionKeyFingerprintFieldName );
98+ Assert .assertEquals ("x-public-key-fingerprint" , config .encryptionKeyFingerprintHeaderName );
99+ Assert .assertEquals (1 , config .decryptionPaths .size ());
100+ Assert .assertNotNull (config .decryptionKey );
101+ Assert .assertEquals ("SHA-512" , config .oaepPaddingDigestAlgorithm );
102+ Assert .assertEquals ("encryptedValue" , config .encryptedValueFieldName );
103+ Assert .assertEquals ("encryptedKey" , config .encryptedKeyFieldName );
104+ Assert .assertEquals ("x-encrypted-key" , config .encryptedKeyHeaderName );
105+ Assert .assertEquals ("iv" , config .ivFieldName );
106+ Assert .assertEquals ("x-iv" , config .ivHeaderName );
107+ Assert .assertEquals ("oaepPaddingDigestAlgorithm" , config .oaepPaddingDigestAlgorithmFieldName );
108+ Assert .assertEquals ("x-oaep-padding-digest-algorithm" , config .oaepPaddingDigestAlgorithmHeaderName );
109+ Assert .assertEquals (HEX , config .fieldValueEncoding );
110+ assertThat (config .getIVSize ().intValue (),equalTo (16 ));
111+ }
112+
113+ @ Test
114+ public void testBuild_FailedIV () throws Exception {
115+ try {
116+ FieldLevelEncryptionConfig config = FieldLevelEncryptionConfigBuilder .aFieldLevelEncryptionConfig ()
117+ .withEncryptionPath ("$.payload" , "$.encryptedPayload" )
118+ .withEncryptionCertificate (TestUtils .getTestEncryptionCertificate ())
119+ .withEncryptionCertificateFingerprint ("97A2FFE9F0D48960EF31E87FCD7A55BF7843FB4A9EEEF01BDB6032AD6FEF146B" )
120+ .withEncryptionKeyFingerprint ("F806B26BC4870E26986C70B6590AF87BAF4C2B56BB50622C51B12212DAFF2810" )
121+ .withEncryptionCertificateFingerprintFieldName ("publicCertificateFingerprint" )
122+ .withEncryptionCertificateFingerprintHeaderName ("x-public-certificate-fingerprint" )
123+ .withEncryptionKeyFingerprintFieldName ("publicKeyFingerprint" )
124+ .withEncryptionKeyFingerprintHeaderName ("x-public-key-fingerprint" )
125+ .withDecryptionPath ("$.encryptedPayload" , "$.payload" )
126+ .withDecryptionKey (TestUtils .getTestDecryptionKey ())
127+ .withOaepPaddingDigestAlgorithm ("SHA-512" )
128+ .withOaepPaddingDigestAlgorithmFieldName ("oaepPaddingDigestAlgorithm" )
129+ .withOaepPaddingDigestAlgorithmHeaderName ("x-oaep-padding-digest-algorithm" )
130+ .withEncryptedValueFieldName ("encryptedValue" )
131+ .withEncryptedKeyFieldName ("encryptedKey" )
132+ .withEncryptedKeyHeaderName ("x-encrypted-key" )
133+ .withIvFieldName ("iv" )
134+ .withIvHeaderName ("x-iv" )
135+ .withFieldValueEncoding (HEX )
136+ .withEncryptionIVSize (23 )
137+ .build ();
138+ assertFalse ("It should raise an exception, but it didn't" , true );
139+ } catch ( IllegalArgumentException e ) {
140+ assertThat (e .getMessage (), equalTo ("Supported IV Sizes are either 12 or 16!" ));
141+ }
59142 }
60143
61144 @ Test
0 commit comments