20
20
import static software .amazon .awssdk .testutils .service .S3BucketUtils .temporaryBucketName ;
21
21
22
22
import java .io .BufferedReader ;
23
+ import java .io .File ;
24
+ import java .io .IOException ;
23
25
import java .io .InputStreamReader ;
26
+ import java .io .RandomAccessFile ;
24
27
import java .net .URI ;
25
28
import java .nio .charset .StandardCharsets ;
29
+ import java .nio .file .Files ;
30
+ import java .nio .file .Paths ;
26
31
import java .util .stream .Collectors ;
27
32
import org .junit .jupiter .api .AfterAll ;
28
33
import org .junit .jupiter .api .AfterEach ;
@@ -303,7 +308,7 @@ public void asyncValidUnsignedTrailerChecksumCalculatedBySdkClient() throws Inte
303
308
}
304
309
305
310
@ Test
306
- public void asyncHttpsValidUnsignedTrailerChecksumCalculatedBySdkClient () throws InterruptedException {
311
+ public void asyncHttpsValidUnsignedTrailerChecksumCalculatedBySdkClient_withSmallRequestBody () throws InterruptedException {
307
312
s3Async .putObject (PutObjectRequest .builder ()
308
313
.bucket (BUCKET )
309
314
.key (KEY )
@@ -320,6 +325,23 @@ public void asyncHttpsValidUnsignedTrailerChecksumCalculatedBySdkClient() throws
320
325
assertThat (response ).isEqualTo ("Hello world" );
321
326
}
322
327
328
+ @ Test
329
+ public void asyncHttpsValidUnsignedTrailerChecksumCalculatedBySdkClient_withHugeRequestBody () throws InterruptedException {
330
+ s3Async .putObject (PutObjectRequest .builder ()
331
+ .bucket (BUCKET )
332
+ .key (KEY )
333
+ .checksumAlgorithm (ChecksumAlgorithm .CRC32 )
334
+ .build (), AsyncRequestBody .fromString (createDataSize (HUGE_MSG_SIZE ))).join ();
335
+ assertThat (interceptor .requestChecksumInTrailer ()).isEqualTo ("x-amz-checksum-crc32" );
336
+ assertThat (interceptor .requestChecksumInHeader ()).isNull ();
337
+
338
+ String response = s3Async .getObject (GetObjectRequest .builder ().bucket (BUCKET )
339
+ .key (KEY ).checksumMode (ChecksumMode .ENABLED )
340
+ .build (), AsyncResponseTransformer .toBytes ()).join ().asUtf8String ();
341
+ assertThat (interceptor .validationAlgorithm ()).isEqualTo (Algorithm .CRC32 );
342
+ assertThat (interceptor .responseValidation ()).isEqualTo (ChecksumValidation .VALIDATED );
343
+ assertThat (response ).isEqualTo (createDataSize (HUGE_MSG_SIZE ));
344
+ }
323
345
324
346
325
347
@ Disabled ("Http Async Signing is not supported for S3" )
@@ -416,4 +438,118 @@ public void syncUnsignedPayloadMultiPartForHugeMessage() throws InterruptedExcep
416
438
assertThat (interceptor .responseValidation ()).isNull ();
417
439
assertThat (text ).isEqualTo (createDataSize (HUGE_MSG_SIZE ));
418
440
}
441
+
442
+
443
+ @ Test
444
+ public void asyncHttpsValidUnsignedTrailerChecksumCalculatedBySdkClient_withSmallFileRequestBody () throws InterruptedException , IOException {
445
+ File randomFileOfFixedLength = getRandomFileOfFixedLength (10 );
446
+ s3Async .putObject (PutObjectRequest .builder ()
447
+ .bucket (BUCKET )
448
+ .key (KEY )
449
+ .checksumAlgorithm (ChecksumAlgorithm .CRC32 )
450
+ .build (), AsyncRequestBody .fromFile (randomFileOfFixedLength .toPath ())).join ();
451
+ assertThat (interceptor .requestChecksumInTrailer ()).isEqualTo ("x-amz-checksum-crc32" );
452
+ assertThat (interceptor .requestChecksumInHeader ()).isNull ();
453
+
454
+ String response = s3Async .getObject (GetObjectRequest .builder ().bucket (BUCKET )
455
+ .key (KEY ).checksumMode (ChecksumMode .ENABLED )
456
+ .build (), AsyncResponseTransformer .toBytes ()).join ().asUtf8String ();
457
+ assertThat (interceptor .validationAlgorithm ()).isEqualTo (Algorithm .CRC32 );
458
+ assertThat (interceptor .responseValidation ()).isEqualTo (ChecksumValidation .VALIDATED );
459
+
460
+ byte [] bytes = Files .readAllBytes (randomFileOfFixedLength .toPath ());
461
+ assertThat (response ).isEqualTo (new String (bytes ));
462
+
463
+
464
+ }
465
+
466
+ @ Test
467
+ public void asyncHttpsValidUnsignedTrailerChecksumCalculatedBySdkClient_withHugeFileRequestBody ()
468
+ throws IOException {
469
+
470
+ File randomFileOfFixedLength = getRandomFileOfFixedLength (17 );
471
+ s3Async .putObject (PutObjectRequest .builder ()
472
+ .bucket (BUCKET )
473
+ .key (KEY )
474
+ .checksumAlgorithm (ChecksumAlgorithm .CRC32 )
475
+ .build (), AsyncRequestBody .fromFile (randomFileOfFixedLength .toPath ())).join ();
476
+ assertThat (interceptor .requestChecksumInTrailer ()).isEqualTo ("x-amz-checksum-crc32" );
477
+ assertThat (interceptor .requestChecksumInHeader ()).isNull ();
478
+
479
+ String response = s3Async .getObject (GetObjectRequest .builder ().bucket (BUCKET )
480
+ .key (KEY ).checksumMode (ChecksumMode .ENABLED )
481
+ .build (), AsyncResponseTransformer .toBytes ()).join ().asUtf8String ();
482
+ assertThat (interceptor .validationAlgorithm ()).isEqualTo (Algorithm .CRC32 );
483
+ assertThat (interceptor .responseValidation ()).isEqualTo (ChecksumValidation .VALIDATED );
484
+
485
+ byte [] bytes = Files .readAllBytes (randomFileOfFixedLength .toPath ());
486
+ assertThat (response ).isEqualTo (new String (bytes ));
487
+
488
+ }
489
+
490
+ @ Test
491
+ public void syncValidUnsignedTrailerChecksumCalculatedBySdkClient_withSmallFileRequestBody () throws InterruptedException ,
492
+ IOException {
493
+
494
+ File randomFileOfFixedLength = getRandomFileOfFixedLength (10 );
495
+
496
+ s3Https .putObject (PutObjectRequest .builder ()
497
+ .bucket (BUCKET )
498
+ .key (KEY )
499
+ .checksumAlgorithm (ChecksumAlgorithm .CRC32 )
500
+ .build (), RequestBody .fromFile (randomFileOfFixedLength .toPath ()));
501
+
502
+ assertThat (interceptor .requestChecksumInTrailer ()).isEqualTo ("x-amz-checksum-crc32" );
503
+ assertThat (interceptor .requestChecksumInHeader ()).isNull ();
504
+
505
+ ResponseInputStream <GetObjectResponse > s3HttpsObject =
506
+ s3Https .getObject (GetObjectRequest .builder ().bucket (BUCKET ).key (KEY ).checksumMode (ChecksumMode .ENABLED ).build ());
507
+ String text = new BufferedReader (
508
+ new InputStreamReader (s3HttpsObject , StandardCharsets .UTF_8 ))
509
+ .lines ()
510
+ .collect (Collectors .joining ("\n " ));
511
+ assertThat (interceptor .validationAlgorithm ()).isEqualTo (Algorithm .CRC32 );
512
+ assertThat (interceptor .responseValidation ()).isEqualTo (ChecksumValidation .VALIDATED );
513
+ byte [] bytes = Files .readAllBytes (randomFileOfFixedLength .toPath ());
514
+ assertThat (text ).isEqualTo (new String (bytes ));
515
+ }
516
+
517
+
518
+ @ Test
519
+ public void syncValidUnsignedTrailerChecksumCalculatedBySdkClient_withHugeFileRequestBody () throws InterruptedException ,
520
+ IOException {
521
+
522
+ File randomFileOfFixedLength = getRandomFileOfFixedLength (34 );
523
+
524
+ s3Https .putObject (PutObjectRequest .builder ()
525
+ .bucket (BUCKET )
526
+ .key (KEY )
527
+ .checksumAlgorithm (ChecksumAlgorithm .CRC32 )
528
+ .build (), RequestBody .fromFile (randomFileOfFixedLength .toPath ()));
529
+
530
+ assertThat (interceptor .requestChecksumInTrailer ()).isEqualTo ("x-amz-checksum-crc32" );
531
+ assertThat (interceptor .requestChecksumInHeader ()).isNull ();
532
+
533
+ ResponseInputStream <GetObjectResponse > s3HttpsObject =
534
+ s3Https .getObject (GetObjectRequest .builder ().bucket (BUCKET ).key (KEY ).checksumMode (ChecksumMode .ENABLED ).build ());
535
+ String text = new BufferedReader (
536
+ new InputStreamReader (s3HttpsObject , StandardCharsets .UTF_8 ))
537
+ .lines ()
538
+ .collect (Collectors .joining ("\n " ));
539
+ assertThat (interceptor .validationAlgorithm ()).isEqualTo (Algorithm .CRC32 );
540
+ assertThat (interceptor .responseValidation ()).isEqualTo (ChecksumValidation .VALIDATED );
541
+ byte [] bytes = Files .readAllBytes (randomFileOfFixedLength .toPath ());
542
+ assertThat (text ).isEqualTo (new String (bytes ));
543
+ }
544
+
545
+ private File getRandomFileOfFixedLength (int sizeInKb ) throws IOException {
546
+ int objectSize = sizeInKb * 1024 ;
547
+ final File tempFile = File .createTempFile ("s3-object-file-" , ".tmp" );
548
+ try (RandomAccessFile f = new RandomAccessFile (tempFile , "rw" )) {
549
+ f .setLength (objectSize );
550
+ }
551
+ tempFile .deleteOnExit ();
552
+ return tempFile ;
553
+ }
554
+
419
555
}
0 commit comments