Skip to content

Add flag to make sure we don't validate checksum twice #878

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 27, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changes/next-release/bugfix-AWSSDKforJavav2-89a97c3.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"category": "AWS SDK for Java v2",
"type": "bugfix",
"description": "Preserve computedChecksum in `ChecksumValidatingInputStream` so that it doesn't throw error if it validates more than once. See [#873](https://github.com/aws/aws-sdk-java-v2/issues/873)"
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,14 @@
import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.util.Properties;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import software.amazon.awssdk.core.ResponseInputStream;
import software.amazon.awssdk.core.client.config.ClientOverrideConfiguration;
import software.amazon.awssdk.core.interceptor.ExecutionInterceptor;
import software.amazon.awssdk.core.sync.RequestBody;
import software.amazon.awssdk.core.sync.ResponseTransformer;
import software.amazon.awssdk.services.s3.GetObjectAsyncIntegrationTest.AssertingExecutionInterceptor;
import software.amazon.awssdk.services.s3.model.GetObjectRequest;
Expand All @@ -41,6 +43,7 @@ public class GetObjectIntegrationTest extends S3IntegrationTestBase {
private static final String BUCKET = temporaryBucketName(GetObjectIntegrationTest.class);

private static final String KEY = "some-key";
private static final String PROPERTY_KEY = "properties";

private final GetObjectRequest getObjectRequest = GetObjectRequest.builder()
.bucket(BUCKET)
Expand Down Expand Up @@ -71,6 +74,18 @@ public void toInputStream() throws Exception {
}
}


@Test
public void toInputStream_loadFromProperties() throws IOException {
s3.putObject(b -> b.bucket(BUCKET).key(PROPERTY_KEY), RequestBody.fromString("test: test"));
try (ResponseInputStream<GetObjectResponse> object = s3.getObject(b -> b.bucket(BUCKET).key(PROPERTY_KEY),
ResponseTransformer.toInputStream())) {
Properties properties = new Properties();
properties.load(object);
assertThat(properties.getProperty("test")).isEqualTo("test");
}
}

@Test
public void toFile() throws Exception {
Path path = RandomTempFile.randomUncreatedFile().toPath();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ public class ChecksumValidatingInputStream extends InputStream implements Aborta
private long strippedLength;
private byte[] streamChecksum = new byte[CHECKSUM_SIZE];
private long lengthRead = 0;
// Preserve the computed checksum because some InputStream readers (e.g., java.util.Properties) read more than once at the
// end of the stream.
private Integer computedChecksum;

/**
* Creates an input stream using the specified Checksum, input stream, and length.
Expand Down Expand Up @@ -171,11 +174,14 @@ public int getStreamChecksum() {

private void validateAndThrow() {
int streamChecksumInt = getStreamChecksum();
int computedChecksumInt = ByteBuffer.wrap(checkSum.getChecksumBytes()).getInt();
if (streamChecksumInt != computedChecksumInt) {
if (computedChecksum == null) {
computedChecksum = ByteBuffer.wrap(checkSum.getChecksumBytes()).getInt();
}

if (streamChecksumInt != computedChecksum) {
throw SdkClientException.builder().message(
String.format("Data read has a different checksum than expected. Was %d, but expected %d",
computedChecksumInt, streamChecksumInt)).build();
computedChecksum, streamChecksumInt)).build();
}
}

Expand Down