29
29
import java .nio .charset .StandardCharsets ;
30
30
import java .nio .file .Files ;
31
31
import java .util .stream .Collectors ;
32
+ import java .util .stream .Stream ;
32
33
import org .junit .jupiter .api .AfterAll ;
33
34
import org .junit .jupiter .api .AfterEach ;
34
35
import org .junit .jupiter .api .BeforeAll ;
35
36
import org .junit .jupiter .api .Test ;
37
+ import org .junit .jupiter .params .ParameterizedTest ;
38
+ import org .junit .jupiter .params .provider .Arguments ;
39
+ import org .junit .jupiter .params .provider .MethodSource ;
36
40
import software .amazon .awssdk .authcrt .signer .internal .DefaultAwsCrtS3V4aSigner ;
37
41
import software .amazon .awssdk .core .HttpChecksumConstant ;
38
42
import software .amazon .awssdk .core .ResponseInputStream ;
41
45
import software .amazon .awssdk .core .sync .RequestBody ;
42
46
import software .amazon .awssdk .services .s3 .S3AsyncClient ;
43
47
import software .amazon .awssdk .services .s3 .S3Client ;
48
+ import software .amazon .awssdk .services .s3 .S3Configuration ;
44
49
import software .amazon .awssdk .services .s3 .S3IntegrationTestBase ;
45
50
import software .amazon .awssdk .services .s3 .model .ChecksumAlgorithm ;
46
51
import software .amazon .awssdk .services .s3 .model .ChecksumMode ;
50
55
import software .amazon .awssdk .services .s3 .model .PutObjectResponse ;
51
56
import software .amazon .awssdk .services .s3 .model .S3Exception ;
52
57
import software .amazon .awssdk .services .s3 .utils .CaptureChecksumValidationInterceptor ;
53
- import software .amazon .awssdk .services .s3 .utils .ChecksumUtils ;
54
58
import software .amazon .awssdk .testutils .RandomTempFile ;
55
59
56
60
public class HttpChecksumIntegrationTest extends S3IntegrationTestBase {
@@ -149,7 +153,7 @@ public void invalidHeaderChecksumCalculatedByUserNotOverWrittenBySdkClient() {
149
153
}
150
154
151
155
@ Test
152
- public void syncValidUnsignedTrailerChecksumCalculatedBySdkClient () throws InterruptedException {
156
+ public void syncValidUnsignedTrailerChecksumCalculatedBySdkClient () {
153
157
s3Https .putObject (PutObjectRequest .builder ()
154
158
.bucket (BUCKET )
155
159
.key (KEY )
@@ -173,8 +177,6 @@ public void syncValidUnsignedTrailerChecksumCalculatedBySdkClient() throws Inter
173
177
174
178
@ Test
175
179
public void syncValidSignedTrailerChecksumCalculatedBySdkClient () {
176
-
177
-
178
180
s3 .putObject (PutObjectRequest .builder ()
179
181
.bucket (BUCKET )
180
182
.key (KEY )
@@ -198,13 +200,10 @@ public void syncValidSignedTrailerChecksumCalculatedBySdkClient() {
198
200
assertThat (interceptor .validationAlgorithm ()).isEqualTo (Algorithm .CRC32 );
199
201
assertThat (interceptor .responseValidation ()).isEqualTo (ChecksumValidation .VALIDATED );
200
202
assertThat (text ).isEqualTo ("Hello world" );
201
-
202
203
}
203
204
204
205
@ Test
205
206
public void syncValidSignedTrailerChecksumCalculatedBySdkClient_Empty_String () {
206
-
207
-
208
207
s3 .putObject (PutObjectRequest .builder ()
209
208
.bucket (BUCKET )
210
209
.key (KEY )
@@ -230,7 +229,6 @@ public void syncValidSignedTrailerChecksumCalculatedBySdkClient_Empty_String() {
230
229
231
230
@ Test
232
231
public void syncValidSignedTrailerChecksumCalculatedBySdkClientWithSigv4a () {
233
-
234
232
s3 .putObject (PutObjectRequest .builder ()
235
233
.bucket (BUCKET )
236
234
.key (KEY )
@@ -254,7 +252,6 @@ public void syncValidSignedTrailerChecksumCalculatedBySdkClientWithSigv4a() {
254
252
255
253
@ Test
256
254
public void syncValidSignedTrailerChecksumCalculatedBySdkClientWithSigv4a_withContentEncoding () {
257
-
258
255
s3 .putObject (PutObjectRequest .builder ()
259
256
.bucket (BUCKET )
260
257
.key (KEY )
@@ -334,7 +331,7 @@ public void syncSignedPayloadForHugeMessage() {
334
331
}
335
332
336
333
@ Test
337
- public void syncUnsignedPayloadMultiPartForHugeMessage () throws InterruptedException {
334
+ public void syncUnsignedPayloadMultiPartForHugeMessage () {
338
335
s3Https .putObject (PutObjectRequest .builder ()
339
336
.bucket (BUCKET )
340
337
.key (KEY )
@@ -360,8 +357,7 @@ public void syncUnsignedPayloadMultiPartForHugeMessage() throws InterruptedExcep
360
357
361
358
362
359
@ Test
363
- public void syncValidUnsignedTrailerChecksumCalculatedBySdkClient_withSmallFileRequestBody () throws InterruptedException ,
364
- IOException {
360
+ public void syncValidUnsignedTrailerChecksumCalculatedBySdkClient_withSmallFileRequestBody () throws IOException {
365
361
File randomFileOfFixedLength = new RandomTempFile (10 * KB );
366
362
367
363
s3Https .putObject (PutObjectRequest .builder ()
@@ -410,4 +406,97 @@ public void syncValidUnsignedTrailerChecksumCalculatedBySdkClient_withHugeFileRe
410
406
assertThat (text ).isEqualTo (new String (bytes ));
411
407
}
412
408
409
+ private static Stream <Arguments > getObjectChecksumValidationParams () {
410
+ return Stream .of (Arguments .of (true , ChecksumAlgorithm .CRC32 , ChecksumMode .ENABLED ),
411
+ Arguments .of (true , null , ChecksumMode .ENABLED ),
412
+ Arguments .of (true , ChecksumAlgorithm .CRC32 , null ),
413
+ Arguments .of (true , null , null ),
414
+ Arguments .of (false , ChecksumAlgorithm .CRC32 , ChecksumMode .ENABLED ),
415
+ Arguments .of (false , null , ChecksumMode .ENABLED ),
416
+ Arguments .of (false , ChecksumAlgorithm .CRC32 , null ),
417
+ Arguments .of (false , null , null ));
418
+ }
419
+
420
+ @ ParameterizedTest
421
+ @ MethodSource ("getObjectChecksumValidationParams" )
422
+ public void testGetObjectChecksumValidation (boolean checksumValidationEnabled , ChecksumAlgorithm checksumAlgorithm ,
423
+ ChecksumMode checksumMode ) {
424
+ S3Client s3 = s3ClientBuilder ().overrideConfiguration (o -> o .addExecutionInterceptor (interceptor ))
425
+ .serviceConfiguration (S3Configuration .builder ()
426
+ .checksumValidationEnabled (checksumValidationEnabled )
427
+ .build ())
428
+ .build ();
429
+
430
+ s3 .putObject (PutObjectRequest .builder ()
431
+ .bucket (BUCKET )
432
+ .key (KEY )
433
+ .checksumAlgorithm (checksumAlgorithm )
434
+ .build (), RequestBody .fromString ("Hello world" ));
435
+
436
+ s3 .getObject (GetObjectRequest .builder ()
437
+ .bucket (BUCKET )
438
+ .key (KEY )
439
+ .checksumMode (checksumMode )
440
+ .build ());
441
+
442
+ validateChecksumValidation (checksumValidationEnabled , checksumAlgorithm , checksumMode );
443
+ interceptor .reset ();
444
+ }
445
+
446
+ private void validateChecksumValidation (boolean checksumValidationEnabled , ChecksumAlgorithm checksumAlgorithm ,
447
+ ChecksumMode checksumMode ) {
448
+ if (checksumValidationEnabled ) {
449
+ if (checksumMode == ChecksumMode .ENABLED ) {
450
+ assertChecksumModeEnabledWithChecksumValidationEnabled (checksumAlgorithm );
451
+ } else {
452
+ assertChecksumModeNotEnabledWithChecksumValidationEnabled ();
453
+ }
454
+ } else {
455
+ if (checksumMode == ChecksumMode .ENABLED ) {
456
+ assertChecksumModeEnabledWithChecksumValidationDisabled (checksumAlgorithm );
457
+ } else {
458
+ assertChecksumModeNotEnabledWithChecksumValidationDisabled ();
459
+ }
460
+ }
461
+ }
462
+
463
+ private void assertChecksumModeEnabledWithChecksumValidationEnabled (ChecksumAlgorithm checksumAlgorithm ) {
464
+ if (checksumAlgorithm == null ) {
465
+ assertRequestAndResponseDoNotContainMd5Header ();
466
+ assertThat (interceptor .responseFlexibleChecksumHeader ()).isNull ();
467
+ } else {
468
+ assertRequestAndResponseDoNotContainMd5Header ();
469
+ assertThat (interceptor .responseFlexibleChecksumHeader ()).isNotNull ();
470
+ }
471
+ }
472
+
473
+ private void assertChecksumModeNotEnabledWithChecksumValidationEnabled () {
474
+ assertRequestAndResponseContainMd5Header ();
475
+ assertThat (interceptor .responseFlexibleChecksumHeader ()).isNull ();
476
+ }
477
+
478
+ private void assertChecksumModeEnabledWithChecksumValidationDisabled (ChecksumAlgorithm checksumAlgorithm ) {
479
+ if (checksumAlgorithm == null ) {
480
+ assertRequestAndResponseDoNotContainMd5Header ();
481
+ assertThat (interceptor .responseFlexibleChecksumHeader ()).isNull ();
482
+ } else {
483
+ assertRequestAndResponseDoNotContainMd5Header ();
484
+ assertThat (interceptor .responseFlexibleChecksumHeader ()).isNotNull ();
485
+ }
486
+ }
487
+
488
+ private void assertChecksumModeNotEnabledWithChecksumValidationDisabled () {
489
+ assertRequestAndResponseDoNotContainMd5Header ();
490
+ assertThat (interceptor .responseFlexibleChecksumHeader ()).isNull ();
491
+ }
492
+
493
+ private void assertRequestAndResponseContainMd5Header () {
494
+ assertThat (interceptor .requestTransferEncodingHeader ()).isEqualTo ("append-md5" );
495
+ assertThat (interceptor .responseTransferEncodingHeader ()).isEqualTo ("append-md5" );
496
+ }
497
+
498
+ private void assertRequestAndResponseDoNotContainMd5Header () {
499
+ assertThat (interceptor .requestTransferEncodingHeader ()).isNull ();
500
+ assertThat (interceptor .responseTransferEncodingHeader ()).isNull ();
501
+ }
413
502
}
0 commit comments