|
| 1 | +/* |
| 2 | + * Copyright 2010-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"). |
| 5 | + * You may not use this file except in compliance with the License. |
| 6 | + * A copy of the License is located at |
| 7 | + * |
| 8 | + * http://aws.amazon.com/apache2.0 |
| 9 | + * |
| 10 | + * or in the "license" file accompanying this file. This file is distributed |
| 11 | + * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
| 12 | + * express or implied. See the License for the specific language governing |
| 13 | + * permissions and limitations under the License. |
| 14 | + */ |
| 15 | + |
| 16 | +package software.amazon.awssdk.services.s3.checksums; |
| 17 | + |
| 18 | +import static com.github.tomakehurst.wiremock.client.WireMock.aResponse; |
| 19 | +import static com.github.tomakehurst.wiremock.client.WireMock.any; |
| 20 | +import static com.github.tomakehurst.wiremock.client.WireMock.anyUrl; |
| 21 | +import static com.github.tomakehurst.wiremock.client.WireMock.stubFor; |
| 22 | +import static org.assertj.core.api.Assertions.assertThat; |
| 23 | +import static software.amazon.awssdk.core.async.AsyncResponseTransformer.toBytes; |
| 24 | + |
| 25 | +import com.github.tomakehurst.wiremock.client.ResponseDefinitionBuilder; |
| 26 | +import com.github.tomakehurst.wiremock.client.WireMock; |
| 27 | +import com.github.tomakehurst.wiremock.junit.WireMockRule; |
| 28 | +import com.github.tomakehurst.wiremock.stubbing.Scenario; |
| 29 | +import java.net.URI; |
| 30 | +import java.nio.charset.StandardCharsets; |
| 31 | +import java.util.function.Consumer; |
| 32 | +import org.apache.commons.lang3.ArrayUtils; |
| 33 | +import org.junit.Before; |
| 34 | +import org.junit.Rule; |
| 35 | +import org.junit.Test; |
| 36 | +import software.amazon.awssdk.auth.credentials.AwsBasicCredentials; |
| 37 | +import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider; |
| 38 | +import software.amazon.awssdk.core.ResponseBytes; |
| 39 | +import software.amazon.awssdk.core.async.AsyncRequestBody; |
| 40 | +import software.amazon.awssdk.core.sync.RequestBody; |
| 41 | +import software.amazon.awssdk.regions.Region; |
| 42 | +import software.amazon.awssdk.services.s3.S3AsyncClient; |
| 43 | +import software.amazon.awssdk.services.s3.S3Client; |
| 44 | +import software.amazon.awssdk.services.s3.model.GetObjectResponse; |
| 45 | +import software.amazon.awssdk.services.s3.model.PutObjectResponse; |
| 46 | +import software.amazon.awssdk.utils.BinaryUtils; |
| 47 | + |
| 48 | +/** |
| 49 | + * Verifies that the checksum validators are reset on an HTTP retry. |
| 50 | + */ |
| 51 | +public class ChecksumResetsOnRetryTest { |
| 52 | + @Rule |
| 53 | + public WireMockRule mockServer = new WireMockRule(0); |
| 54 | + |
| 55 | + private S3Client s3Client; |
| 56 | + |
| 57 | + private S3AsyncClient s3AsyncClient; |
| 58 | + |
| 59 | + private byte[] body; |
| 60 | + |
| 61 | + private byte[] bodyWithTrailingChecksum; |
| 62 | + |
| 63 | + private String bodyEtag; |
| 64 | + |
| 65 | + @Before |
| 66 | + public void setup() { |
| 67 | + StaticCredentialsProvider credentials = StaticCredentialsProvider.create(AwsBasicCredentials.create("akid", "skid")); |
| 68 | + s3Client = S3Client.builder() |
| 69 | + .credentialsProvider(credentials) |
| 70 | + .region(Region.US_WEST_2) |
| 71 | + .endpointOverride(URI.create("http://localhost:" + mockServer.port())) |
| 72 | + .build(); |
| 73 | + |
| 74 | + s3AsyncClient = S3AsyncClient.builder() |
| 75 | + .credentialsProvider(credentials) |
| 76 | + .region(Region.US_WEST_2) |
| 77 | + .endpointOverride(URI.create("http://localhost:" + mockServer.port())) |
| 78 | + .build(); |
| 79 | + |
| 80 | + body = "foo".getBytes(StandardCharsets.UTF_8); |
| 81 | + String checksumAsHexString = "acbd18db4cc2f85cedef654fccc4a4d8"; |
| 82 | + bodyEtag = "\"" + checksumAsHexString + "\""; |
| 83 | + bodyWithTrailingChecksum = ArrayUtils.addAll(body, BinaryUtils.fromHex(checksumAsHexString)); |
| 84 | + } |
| 85 | + |
| 86 | + @Test |
| 87 | + public void syncPutObject_resetsChecksumOnRetry() { |
| 88 | + stubSuccessAfterOneRetry(r -> r.withHeader("ETag", bodyEtag)); |
| 89 | + |
| 90 | + PutObjectResponse response = s3Client.putObject(r -> r.bucket("foo").key("bar"), RequestBody.fromBytes(body)); |
| 91 | + assertThat(response.eTag()).isEqualTo(bodyEtag); |
| 92 | + } |
| 93 | + |
| 94 | + @Test |
| 95 | + public void asyncPutObject_resetsChecksumOnRetry() { |
| 96 | + stubSuccessAfterOneRetry(r -> r.withHeader("ETag", bodyEtag)); |
| 97 | + |
| 98 | + PutObjectResponse response = s3AsyncClient.putObject(r -> r.bucket("foo").key("bar"), AsyncRequestBody.fromBytes(body)).join(); |
| 99 | + assertThat(response.eTag()).isEqualTo(bodyEtag); |
| 100 | + } |
| 101 | + |
| 102 | + @Test |
| 103 | + public void syncGetObject_resetsChecksumOnRetry() { |
| 104 | + stubSuccessAfterOneRetry(r -> r.withHeader("ETag", bodyEtag) |
| 105 | + .withHeader("x-amz-transfer-encoding", "append-md5") |
| 106 | + .withHeader("content-length", Integer.toString(bodyWithTrailingChecksum.length)) |
| 107 | + .withBody(bodyWithTrailingChecksum)); |
| 108 | + |
| 109 | + ResponseBytes<GetObjectResponse> response = s3Client.getObjectAsBytes(r -> r.bucket("foo").key("bar")); |
| 110 | + assertThat(response.response().eTag()).isEqualTo(bodyEtag); |
| 111 | + assertThat(response.asByteArray()).isEqualTo(body); |
| 112 | + } |
| 113 | + |
| 114 | + @Test |
| 115 | + public void asyncGetObject_resetsChecksumOnRetry() { |
| 116 | + stubSuccessAfterOneRetry(r -> r.withHeader("ETag", bodyEtag) |
| 117 | + .withHeader("x-amz-transfer-encoding", "append-md5") |
| 118 | + .withHeader("content-length", Integer.toString(bodyWithTrailingChecksum.length)) |
| 119 | + .withBody(bodyWithTrailingChecksum)); |
| 120 | + |
| 121 | + ResponseBytes<GetObjectResponse> response = s3AsyncClient.getObject(r -> r.bucket("foo").key("bar"), toBytes()).join(); |
| 122 | + assertThat(response.response().eTag()).isEqualTo(bodyEtag); |
| 123 | + assertThat(response.asByteArray()).isEqualTo(body); |
| 124 | + } |
| 125 | + |
| 126 | + private void stubSuccessAfterOneRetry(Consumer<ResponseDefinitionBuilder> successfulResponseModifier) { |
| 127 | + WireMock.reset(); |
| 128 | + |
| 129 | + String scenario = "stubSuccessAfterOneRetry"; |
| 130 | + stubFor(any(anyUrl()) |
| 131 | + .willReturn(aResponse().withStatus(500).withBody("<xml></xml>")) |
| 132 | + .inScenario(scenario) |
| 133 | + .whenScenarioStateIs(Scenario.STARTED) |
| 134 | + .willSetStateTo("200")); |
| 135 | + |
| 136 | + ResponseDefinitionBuilder successfulResponse = aResponse().withStatus(200).withBody("<xml></xml>"); |
| 137 | + successfulResponseModifier.accept(successfulResponse); |
| 138 | + stubFor(any(anyUrl()) |
| 139 | + .willReturn(successfulResponse) |
| 140 | + .inScenario(scenario) |
| 141 | + .whenScenarioStateIs("200")); |
| 142 | + } |
| 143 | +} |
| 144 | + |
0 commit comments