3030import static software .amazon .awssdk .http .auth .aws .signer .AwsV4HttpSigner .PAYLOAD_SIGNING_ENABLED ;
3131import static software .amazon .awssdk .http .auth .spi .signer .SdkInternalHttpSignerProperty .CHECKSUM_STORE ;
3232
33+ import io .reactivex .Flowable ;
3334import java .io .IOException ;
3435import java .net .URI ;
36+ import java .nio .ByteBuffer ;
3537import java .nio .charset .StandardCharsets ;
3638import java .time .Duration ;
37- import java .util .Optional ;
39+ import java .util .List ;
40+ import java .util .stream .Collectors ;
3841import org .junit .jupiter .api .Disabled ;
3942import org .junit .jupiter .api .Test ;
4043import org .junit .jupiter .params .ParameterizedTest ;
4144import org .junit .jupiter .params .provider .ValueSource ;
4245import org .mockito .MockedStatic ;
4346import org .mockito .Mockito ;
47+ import org .reactivestreams .Publisher ;
4448import software .amazon .awssdk .checksums .SdkChecksum ;
4549import software .amazon .awssdk .checksums .spi .ChecksumAlgorithm ;
4650import software .amazon .awssdk .http .Header ;
@@ -709,7 +713,9 @@ void sign_WithPayloadSigningTrueAndChunkEncodingTrueAndHttp_SignsPayload() {
709713 }
710714
711715 @ Test
712- void signAsync_WithPayloadSigningTrueAndChunkEncodingTrueAndHttp_IgnoresPayloadSigning () {
716+ @ Disabled ("Fallback to signing is disabled to match pre-SRA behavior" )
717+ // TODO: Enable this test once we figure out what the expected behavior is post SRA. See JAVA-8078
718+ void signAsync_WithPayloadSigningTrueAndChunkEncodingTrueAndHttp_RespectsPayloadSigning () {
713719 AsyncSignRequest <? extends AwsCredentialsIdentity > request = generateBasicAsyncRequest (
714720 AwsCredentialsIdentity .create ("access" , "secret" ),
715721 httpRequest -> httpRequest .uri (URI .create ("http://demo.us-east-1.amazonaws.com" )),
@@ -727,6 +733,8 @@ void signAsync_WithPayloadSigningTrueAndChunkEncodingTrueAndHttp_IgnoresPayloadS
727733 }
728734
729735 @ Test
736+ @ Disabled ("Fallback to signing is disabled to match pre-SRA behavior" )
737+ // TODO: Enable this test once we figure out what the expected behavior is post SRA. See JAVA-8078
730738 void sign_WithPayloadSigningFalseAndChunkEncodingTrueAndHttp_SignsPayload () {
731739 SignRequest <? extends AwsCredentialsIdentity > request = generateBasicRequest (
732740 AwsCredentialsIdentity .create ("access" , "secret" ),
@@ -745,7 +753,9 @@ void sign_WithPayloadSigningFalseAndChunkEncodingTrueAndHttp_SignsPayload() {
745753 }
746754
747755 @ Test
748- void signAsync_WithPayloadSigningFalseAndChunkEncodingTrueAndHttp_DoesNotFallBackToPayloadSigning () {
756+ @ Disabled ("Fallback to signing is disabled to match pre-SRA behavior" )
757+ // TODO: Enable this test once we figure out what the expected behavior is post SRA. See JAVA-8078
758+ void signAsync_WithPayloadSigningFalseAndChunkEncodingTrueAndHttp_FallsBackToPayloadSigning () {
749759 AsyncSignRequest <? extends AwsCredentialsIdentity > request = generateBasicAsyncRequest (
750760 AwsCredentialsIdentity .create ("access" , "secret" ),
751761 httpRequest -> httpRequest .uri (URI .create ("http://demo.us-east-1.amazonaws.com" )),
@@ -783,7 +793,9 @@ void sign_WithPayloadSigningFalseAndChunkEncodingTrueAndFlexibleChecksumAndHttp_
783793 }
784794
785795 @ Test
786- void signAsync_WithPayloadSigningFalseAndChunkEncodingTrueAndFlexibleChecksumAndHttp_DoesNotFallBackToPayloadSigning () {
796+ @ Disabled ("Fallback to signing is disabled to match pre-SRA behavior" )
797+ // TODO: Enable this test once we figure out what the expected behavior is post SRA. See JAVA-8078
798+ void signAsync_WithPayloadSigningFalseAndChunkEncodingTrueAndFlexibleChecksumAndHttp_FallsBackToPayloadSigning () {
787799 AsyncSignRequest <? extends AwsCredentialsIdentity > request = generateBasicAsyncRequest (
788800 AwsCredentialsIdentity .create ("access" , "secret" ),
789801 httpRequest -> httpRequest .uri (URI .create ("http://demo.us-east-1.amazonaws.com" )),
@@ -900,9 +912,61 @@ void sign_withPayloadSigningTrue_chunkEncodingFalse_withChecksum_cacheEmpty_stor
900912 assertThat (cache .getChecksumValue (CRC32 )).isEqualTo (crc32Value );
901913 }
902914
915+ @ Test
916+ void signAsync_WithPayloadSigningFalse_chunkEncodingTrue_cacheEmpty_storesComputedChecksum () throws IOException {
917+ PayloadChecksumStore cache = PayloadChecksumStore .create ();
918+
919+ AsyncSignRequest <? extends AwsCredentialsIdentity > request = generateBasicAsyncRequest (
920+ AwsCredentialsIdentity .create ("access" , "secret" ),
921+ httpRequest -> httpRequest .uri (URI .create ("http://demo.us-east-1.amazonaws.com" )),
922+ signRequest -> signRequest
923+ .putProperty (PAYLOAD_SIGNING_ENABLED , false )
924+ .putProperty (CHUNK_ENCODING_ENABLED , true )
925+ .putProperty (CHECKSUM_ALGORITHM , CRC32 )
926+ .putProperty (CHECKSUM_STORE , cache )
927+ );
928+
929+ AsyncSignedRequest signedRequest = signer .signAsync (request ).join ();
930+
931+ getAllItems (signedRequest .payload ().get ());
932+ assertThat (cache .getChecksumValue (CRC32 )).isEqualTo (computeChecksum (CRC32 , testPayload ()));
933+ }
934+
935+ @ Test
936+ void signAsync_WithPayloadSigningFalse_chunkEncodingTrue_cacheContainsChecksum_usesCachedValue () throws IOException {
937+ PayloadChecksumStore cache = PayloadChecksumStore .create ();
938+
939+ byte [] checksumValue = "my-checksum" .getBytes (StandardCharsets .UTF_8 );
940+ cache .putChecksumValue (CRC32 , checksumValue );
941+
942+ AsyncSignRequest <? extends AwsCredentialsIdentity > request = generateBasicAsyncRequest (
943+ AwsCredentialsIdentity .create ("access" , "secret" ),
944+ httpRequest -> httpRequest .uri (URI .create ("http://demo.us-east-1.amazonaws.com" )),
945+ signRequest -> signRequest
946+ .putProperty (PAYLOAD_SIGNING_ENABLED , false )
947+ .putProperty (CHUNK_ENCODING_ENABLED , true )
948+ .putProperty (CHECKSUM_ALGORITHM , CRC32 )
949+ .putProperty (CHECKSUM_STORE , cache )
950+ );
951+
952+ AsyncSignedRequest signedRequest = signer .signAsync (request ).join ();
953+
954+ List <ByteBuffer > content = getAllItems (signedRequest .payload ().get ());
955+ String contentAsString = content .stream ().map (DefaultAwsV4HttpSignerTest ::bufferAsString ).collect (Collectors .joining ());
956+ assertThat (contentAsString ).contains ("x-amz-checksum-crc32:" + BinaryUtils .toBase64 (checksumValue ) + "\r \n " );
957+ }
958+
903959 private static byte [] computeChecksum (ChecksumAlgorithm algorithm , byte [] data ) {
904960 SdkChecksum checksum = SdkChecksum .forAlgorithm (algorithm );
905961 checksum .update (data , 0 , data .length );
906962 return checksum .getChecksumBytes ();
907963 }
964+
965+ private List <ByteBuffer > getAllItems (Publisher <ByteBuffer > publisher ) {
966+ return Flowable .fromPublisher (publisher ).toList ().blockingGet ();
967+ }
968+
969+ private static String bufferAsString (ByteBuffer buffer ) {
970+ return StandardCharsets .UTF_8 .decode (buffer .duplicate ()).toString ();
971+ }
908972}
0 commit comments