|
| 1 | +package com.mastercard.developer.interceptor; |
| 2 | + |
| 3 | +import com.google.api.client.http.*; |
| 4 | +import com.mastercard.developer.encryption.EncryptionException; |
| 5 | +import com.mastercard.developer.encryption.FieldLevelEncryptionConfig; |
| 6 | +import com.mastercard.developer.interceptors.HttpExecuteFieldLevelEncryptionInterceptor; |
| 7 | +import org.apache.commons.io.IOUtils; |
| 8 | +import org.junit.Assert; |
| 9 | +import org.junit.Rule; |
| 10 | +import org.junit.Test; |
| 11 | +import org.junit.rules.ExpectedException; |
| 12 | +import org.junit.runner.RunWith; |
| 13 | +import org.mockito.ArgumentCaptor; |
| 14 | +import org.mockito.junit.MockitoJUnitRunner; |
| 15 | +import sun.security.x509.X509CertImpl; |
| 16 | + |
| 17 | +import java.io.ByteArrayOutputStream; |
| 18 | +import java.io.IOException; |
| 19 | +import java.io.InputStream; |
| 20 | +import java.lang.reflect.Field; |
| 21 | +import java.nio.charset.StandardCharsets; |
| 22 | + |
| 23 | +import static com.mastercard.developer.test.TestUtils.getFieldLevelEncryptionConfigBuilder; |
| 24 | +import static org.hamcrest.core.Is.isA; |
| 25 | +import static org.mockito.Mockito.*; |
| 26 | + |
| 27 | +@RunWith(MockitoJUnitRunner.class) |
| 28 | +public class HttpExecuteFieldLevelEncryptionInterceptorTest { |
| 29 | + |
| 30 | + private static final String JSON_TYPE = "application/json; charset=utf-8"; |
| 31 | + |
| 32 | + @Rule |
| 33 | + public ExpectedException expectedException = ExpectedException.none(); |
| 34 | + |
| 35 | + @Test |
| 36 | + public void testIntercept_ShouldEncryptRequestPayload() throws Exception { |
| 37 | + |
| 38 | + // GIVEN |
| 39 | + FieldLevelEncryptionConfig config = getFieldLevelEncryptionConfigBuilder() |
| 40 | + .withEncryptionPath("$.foo", "$.encryptedFoo") |
| 41 | + .build(); |
| 42 | + HttpRequest request = mock(HttpRequest.class); |
| 43 | + HttpHeaders httpHeaders = new HttpHeaders(); |
| 44 | + when(request.getContent()).thenReturn(new ByteArrayContent(JSON_TYPE, "{\"foo\":\"bar\"}".getBytes())); |
| 45 | + when(request.getHeaders()).thenReturn(httpHeaders); |
| 46 | + |
| 47 | + // WHEN |
| 48 | + HttpExecuteFieldLevelEncryptionInterceptor instanceUnderTest = new HttpExecuteFieldLevelEncryptionInterceptor(config); |
| 49 | + instanceUnderTest.intercept(request); |
| 50 | + |
| 51 | + // THEN |
| 52 | + ArgumentCaptor<HttpContent> contentCaptor = ArgumentCaptor.forClass(HttpContent.class); |
| 53 | + verify(request).setContent(contentCaptor.capture()); |
| 54 | + ByteArrayOutputStream encryptedPayloadStream = new ByteArrayOutputStream(); |
| 55 | + contentCaptor.getValue().writeTo(encryptedPayloadStream); |
| 56 | + String encryptedPayload = encryptedPayloadStream.toString(StandardCharsets.UTF_8.name()); |
| 57 | + Assert.assertFalse(encryptedPayload.contains("foo")); |
| 58 | + Assert.assertTrue(encryptedPayload.contains("encryptedFoo")); |
| 59 | + Assert.assertEquals(868, httpHeaders.getContentLength().intValue()); |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + public void testIntercept_ShouldDoNothing_WhenNoPayload() throws Exception { |
| 64 | + |
| 65 | + // GIVEN |
| 66 | + FieldLevelEncryptionConfig config = getFieldLevelEncryptionConfigBuilder() |
| 67 | + .withEncryptionPath("$.foo", "$.encryptedFoo") |
| 68 | + .build(); |
| 69 | + HttpRequest request = mock(HttpRequest.class); |
| 70 | + when(request.getContent()).thenReturn(null); |
| 71 | + |
| 72 | + // WHEN |
| 73 | + HttpExecuteFieldLevelEncryptionInterceptor instanceUnderTest = new HttpExecuteFieldLevelEncryptionInterceptor(config); |
| 74 | + instanceUnderTest.intercept(request); |
| 75 | + |
| 76 | + // THEN |
| 77 | + verify(request).getContent(); |
| 78 | + verifyNoMoreInteractions(request); |
| 79 | + } |
| 80 | + |
| 81 | + @Test |
| 82 | + public void testIntercept_ShouldThrowIOException_WhenEncryptionFails() throws Exception { |
| 83 | + |
| 84 | + // GIVEN |
| 85 | + FieldLevelEncryptionConfig config = getFieldLevelEncryptionConfigBuilder() |
| 86 | + .withEncryptionPath("$.foo", "$.encryptedFoo") |
| 87 | + .withEncryptionCertificate(new X509CertImpl()) // Certificate without key |
| 88 | + .build(); |
| 89 | + HttpRequest request = mock(HttpRequest.class); |
| 90 | + when(request.getContent()).thenReturn(new ByteArrayContent(JSON_TYPE, "{\"foo\":\"bar\"}".getBytes())); |
| 91 | + |
| 92 | + // THEN |
| 93 | + expectedException.expect(IOException.class); |
| 94 | + expectedException.expectMessage("Failed to encrypt request!"); |
| 95 | + expectedException.expectCause(isA(EncryptionException.class)); |
| 96 | + |
| 97 | + // WHEN |
| 98 | + HttpExecuteFieldLevelEncryptionInterceptor instanceUnderTest = new HttpExecuteFieldLevelEncryptionInterceptor(config); |
| 99 | + instanceUnderTest.intercept(request); |
| 100 | + } |
| 101 | + |
| 102 | + @Test |
| 103 | + public void testInterceptResponse_ShouldDecryptResponsePayload() throws Exception { |
| 104 | + |
| 105 | + // GIVEN |
| 106 | + String encryptedPayload = "{" + |
| 107 | + " \"encryptedData\": {" + |
| 108 | + " \"iv\": \"a2c494ca28dec4f3d6ce7d68b1044cfe\"," + |
| 109 | + " \"encryptedKey\": \"038c65f154a2b07f6c788aaddc13ecead05fdeb11eca0bf576cab7185df66349d2cba4ba51a5304d45995e915bb1de462f0f66acd05026b21340b567d141653a2175ccfe2030b3b49261c6750381421cf0e29bd67840bcdc8092a44691c6c74dcdf620d5a744832fce3b45b8e3f8ad1af6c919195eb7f878c7435143e328e8b858dd232dbfacf7bb2f73981a80a09dc7c6dcd49ad95df527d415438958700b48994d7f6207f03d974a5cf50181205ac0a301a91e94b35ad162c8cf39475d2505d8ae7b1d4ed6f170091ab523f037a75eddb5ca46db9328c10395b69f8b798c280fa0e76f8385a64fe37b67e8578f3f9572dfb87d71e80a97323753030966901b\"," + |
| 110 | + " \"encryptedValue\": \"0672589113046bf692265b6ea6088184\"," + |
| 111 | + " \"oaepHashingAlgorithm\": \"SHA256\"" + |
| 112 | + " }" + |
| 113 | + "}"; |
| 114 | + FieldLevelEncryptionConfig config = getFieldLevelEncryptionConfigBuilder() |
| 115 | + .withDecryptionPath("$.encryptedData", "$.data") |
| 116 | + .build(); |
| 117 | + HttpResponse response = mock(HttpResponse.class); |
| 118 | + HttpHeaders httpHeaders = new HttpHeaders(); |
| 119 | + when(response.parseAsString()).thenReturn(encryptedPayload); |
| 120 | + when(response.getHeaders()).thenReturn(httpHeaders); |
| 121 | + |
| 122 | + // WHEN |
| 123 | + HttpExecuteFieldLevelEncryptionInterceptor instanceUnderTest = new HttpExecuteFieldLevelEncryptionInterceptor(config); |
| 124 | + instanceUnderTest.interceptResponse(response); |
| 125 | + |
| 126 | + // THEN |
| 127 | + String expectedPayload = "{\"data\":\"string\"}"; |
| 128 | + Field contentField = response.getClass().getDeclaredField("content"); |
| 129 | + contentField.setAccessible(true); |
| 130 | + InputStream payloadInputStream = (InputStream) contentField.get(response); |
| 131 | + Assert.assertEquals(expectedPayload, IOUtils.toString(payloadInputStream, StandardCharsets.UTF_8)); |
| 132 | + Assert.assertEquals(expectedPayload.length(), httpHeaders.getContentLength().intValue()); |
| 133 | + } |
| 134 | + |
| 135 | + @Test |
| 136 | + public void testInterceptResponse_ShouldDoNothing_WhenNoPayload() throws Exception { |
| 137 | + |
| 138 | + // GIVEN |
| 139 | + FieldLevelEncryptionConfig config = getFieldLevelEncryptionConfigBuilder().build(); |
| 140 | + HttpResponse response = mock(HttpResponse.class); |
| 141 | + when(response.parseAsString()).thenReturn(null); |
| 142 | + |
| 143 | + // WHEN |
| 144 | + HttpExecuteFieldLevelEncryptionInterceptor instanceUnderTest = new HttpExecuteFieldLevelEncryptionInterceptor(config); |
| 145 | + instanceUnderTest.interceptResponse(response); |
| 146 | + |
| 147 | + // THEN |
| 148 | + verify(response).parseAsString(); |
| 149 | + verifyNoMoreInteractions(response); |
| 150 | + } |
| 151 | + |
| 152 | + @Test |
| 153 | + public void testInterceptResponse_ShouldThrowIOException_WhenDecryptionFails() throws Exception { |
| 154 | + |
| 155 | + // GIVEN |
| 156 | + String encryptedPayload = "{" + |
| 157 | + " \"encryptedData\": {" + |
| 158 | + " \"iv\": \"a2c494ca28dec4f3d6ce7d68b1044cfe\"," + |
| 159 | + " \"encryptedKey\": \"Not a valid key\"," + |
| 160 | + " \"encryptedValue\": \"0672589113046bf692265b6ea6088184\"" + |
| 161 | + " }" + |
| 162 | + "}"; |
| 163 | + FieldLevelEncryptionConfig config = getFieldLevelEncryptionConfigBuilder() |
| 164 | + .withDecryptionPath("$.encryptedData", "$.data") |
| 165 | + .build(); |
| 166 | + HttpResponse response = mock(HttpResponse.class); |
| 167 | + when(response.parseAsString()).thenReturn(encryptedPayload); |
| 168 | + |
| 169 | + // THEN |
| 170 | + expectedException.expect(IOException.class); |
| 171 | + expectedException.expectMessage("Failed to decrypt response!"); |
| 172 | + expectedException.expectCause(isA(EncryptionException.class)); |
| 173 | + |
| 174 | + // WHEN |
| 175 | + HttpExecuteFieldLevelEncryptionInterceptor instanceUnderTest = new HttpExecuteFieldLevelEncryptionInterceptor(config); |
| 176 | + instanceUnderTest.interceptResponse(response); |
| 177 | + } |
| 178 | +} |
0 commit comments