Skip to content

Commit 55af09b

Browse files
committed
Fixed 'X509CertImpl is internal proprietary API and may be removed in a future release'
1 parent 7b5bfb8 commit 55af09b

File tree

5 files changed

+17
-10
lines changed

5 files changed

+17
-10
lines changed

src/test/java/com/mastercard/developer/encryption/FieldLevelEncryptionTest.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44
import com.google.gson.JsonElement;
55
import com.google.gson.JsonObject;
66
import com.google.gson.JsonPrimitive;
7+
import com.mastercard.developer.test.TestUtils;
78
import org.apache.commons.codec.binary.Base64;
89
import org.junit.Rule;
910
import org.junit.Test;
1011
import org.junit.rules.ExpectedException;
11-
import sun.security.x509.X509CertImpl;
1212

1313
import java.security.InvalidKeyException;
1414

@@ -365,13 +365,13 @@ public void testEncryptPayload_ShouldNotAddOaepPaddingDigestAlgorithm_WhenOaepPa
365365
}
366366

367367
@Test
368-
public void testEncryptPayload_ShouldThrowEncryptionException_WhenInvalidCertificateObject() throws Exception {
368+
public void testEncryptPayload_ShouldThrowEncryptionException_WhenEncryptionErrorOccurs() throws Exception {
369369

370370
// GIVEN
371371
String payload = "{\"data\": {}, \"encryptedData\": {}}";
372372
FieldLevelEncryptionConfig config = getTestFieldLevelEncryptionConfigBuilder()
373373
.withEncryptionPath("data", "encryptedData")
374-
.withEncryptionCertificate(new X509CertImpl())
374+
.withEncryptionCertificate(TestUtils.getTestInvalidEncryptionCertificate()) // Invalid certificate
375375
.withOaepPaddingDigestAlgorithm("SHA-256")
376376
.build();
377377

@@ -715,7 +715,7 @@ public void testDecryptPayload_ShouldOverwriteInputObject_WhenOutPathSameAsInPat
715715
}
716716

717717
@Test
718-
public void testDecryptPayload_ShouldThrowEncryptionException_WhenWrongDecryptionKey() throws Exception {
718+
public void testDecryptPayload_ShouldThrowEncryptionException_WhenDecryptionErrorOccurs() throws Exception {
719719

720720
// GIVEN
721721
String encryptedPayload = "{" +
@@ -728,6 +728,7 @@ public void testDecryptPayload_ShouldThrowEncryptionException_WhenWrongDecryptio
728728
FieldLevelEncryptionConfig config = getTestFieldLevelEncryptionConfigBuilder()
729729
.withDecryptionPath("encryptedData", "data")
730730
.withOaepPaddingDigestAlgorithm("SHA-256")
731+
// Not the right key
731732
.withDecryptionKey(loadDecryptionKey("./src/test/resources/test_key_container.p12", "mykeyalias", "Password1"))
732733
.build();
733734

src/test/java/com/mastercard/developer/interceptor/HttpExecuteFieldLevelEncryptionInterceptorTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import com.mastercard.developer.encryption.EncryptionException;
55
import com.mastercard.developer.encryption.FieldLevelEncryptionConfig;
66
import com.mastercard.developer.interceptors.HttpExecuteFieldLevelEncryptionInterceptor;
7+
import com.mastercard.developer.test.TestUtils;
78
import org.apache.commons.io.IOUtils;
89
import org.junit.Assert;
910
import org.junit.Rule;
@@ -12,7 +13,6 @@
1213
import org.junit.runner.RunWith;
1314
import org.mockito.ArgumentCaptor;
1415
import org.mockito.junit.MockitoJUnitRunner;
15-
import sun.security.x509.X509CertImpl;
1616

1717
import java.io.ByteArrayOutputStream;
1818
import java.io.IOException;
@@ -84,7 +84,7 @@ public void testIntercept_ShouldThrowIOException_WhenEncryptionFails() throws Ex
8484
// GIVEN
8585
FieldLevelEncryptionConfig config = getTestFieldLevelEncryptionConfigBuilder()
8686
.withEncryptionPath("$.foo", "$.encryptedFoo")
87-
.withEncryptionCertificate(new X509CertImpl()) // Certificate without key
87+
.withEncryptionCertificate(TestUtils.getTestInvalidEncryptionCertificate()) // Invalid certificate
8888
.build();
8989
HttpRequest request = mock(HttpRequest.class);
9090
when(request.getContent()).thenReturn(new ByteArrayContent(JSON_TYPE, "{\"foo\":\"bar\"}".getBytes()));

src/test/java/com/mastercard/developer/interceptor/OkHttp2FieldLevelEncryptionInterceptorTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33
import com.mastercard.developer.encryption.EncryptionException;
44
import com.mastercard.developer.encryption.FieldLevelEncryptionConfig;
55
import com.mastercard.developer.interceptors.OkHttp2FieldLevelEncryptionInterceptor;
6+
import com.mastercard.developer.test.TestUtils;
67
import com.squareup.okhttp.*;
78
import okio.Buffer;
89
import org.junit.Assert;
910
import org.junit.Rule;
1011
import org.junit.Test;
1112
import org.junit.rules.ExpectedException;
1213
import org.mockito.ArgumentCaptor;
13-
import sun.security.x509.X509CertImpl;
1414

1515
import java.io.IOException;
1616

@@ -89,7 +89,7 @@ public void testIntercept_ShouldThrowIOException_WhenEncryptionFails() throws Ex
8989
// GIVEN
9090
FieldLevelEncryptionConfig config = getTestFieldLevelEncryptionConfigBuilder()
9191
.withEncryptionPath("$.foo", "$.encryptedFoo")
92-
.withEncryptionCertificate(new X509CertImpl()) // Certificate without key
92+
.withEncryptionCertificate(TestUtils.getTestInvalidEncryptionCertificate()) // Invalid certificate
9393
.build();
9494
Request request = new Request.Builder()
9595
.url("https://sandbox.api.mastercard.com/service")

src/test/java/com/mastercard/developer/interceptor/OkHttpFieldLevelEncryptionInterceptorTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33
import com.mastercard.developer.encryption.EncryptionException;
44
import com.mastercard.developer.encryption.FieldLevelEncryptionConfig;
55
import com.mastercard.developer.interceptors.OkHttpFieldLevelEncryptionInterceptor;
6+
import com.mastercard.developer.test.TestUtils;
67
import okhttp3.*;
78
import okio.Buffer;
89
import org.junit.Assert;
910
import org.junit.Rule;
1011
import org.junit.Test;
1112
import org.junit.rules.ExpectedException;
1213
import org.mockito.ArgumentCaptor;
13-
import sun.security.x509.X509CertImpl;
1414

1515
import java.io.IOException;
1616

@@ -89,7 +89,7 @@ public void testIntercept_ShouldThrowIOException_WhenEncryptionFails() throws Ex
8989
// GIVEN
9090
FieldLevelEncryptionConfig config = getTestFieldLevelEncryptionConfigBuilder()
9191
.withEncryptionPath("$.foo", "$.encryptedFoo")
92-
.withEncryptionCertificate(new X509CertImpl()) // Certificate without key
92+
.withEncryptionCertificate(TestUtils.getTestInvalidEncryptionCertificate()) // Invalid certificate
9393
.build();
9494
Request request = new Request.Builder()
9595
.url("https://sandbox.api.mastercard.com/service")

src/test/java/com/mastercard/developer/test/TestUtils.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@
55

66
import java.security.PrivateKey;
77
import java.security.cert.Certificate;
8+
import java.security.cert.X509Certificate;
89

910
import static com.mastercard.developer.utils.EncryptionUtils.loadDecryptionKey;
1011
import static com.mastercard.developer.utils.EncryptionUtils.loadEncryptionCertificate;
12+
import static org.mockito.Mockito.mock;
1113

1214
public class TestUtils {
1315

@@ -18,6 +20,10 @@ public static Certificate getTestEncryptionCertificate() throws Exception {
1820
return loadEncryptionCertificate("./src/test/resources/test_certificate.cert");
1921
}
2022

23+
public static Certificate getTestInvalidEncryptionCertificate() {
24+
return mock(X509Certificate.class); // Will throw "java.security.InvalidKeyException: Key must not be null"
25+
}
26+
2127
public static PrivateKey getTestDecryptionKey() throws Exception {
2228
return loadDecryptionKey("./src/test/resources/test_key.der");
2329
}

0 commit comments

Comments
 (0)