From 69759ad72d68e86694d3188a717eb3ad9a148a3c Mon Sep 17 00:00:00 2001 From: Josh Friedman Date: Thu, 4 May 2017 09:40:18 -0700 Subject: [PATCH] Fixed transactional MD5 check for files --- ChangeLog.txt | 2 ++ .../azure/storage/file/CloudFileTests.java | 24 ++++++++++++++++++- .../azure/storage/file/CloudFile.java | 12 +++++----- 3 files changed, 31 insertions(+), 7 deletions(-) diff --git a/ChangeLog.txt b/ChangeLog.txt index 9fe483cfd0232..0d86fef069f51 100644 --- a/ChangeLog.txt +++ b/ChangeLog.txt @@ -3,6 +3,8 @@ * Changed blob constants to support up to 256 MB on put blob for block blobs. The default value for put blob threshold has also been updated to half of the maximum, or 128 MB currently. * Fixed a bug that prevented setting content MD5 to true when creating a new file. * Fixed a bug where access conditions, options, and operation context were not being passed when calling openWriteExisting() on a page blob or a file. + * Fixed a bug where an exception was being thrown on a range get of a blob or file when the options disableContentMD5Validation is set to false and useTransactionalContentMD5 is set to true and there is no overall MD5. + * Fixed a bug where retries were happening immediately if a sock exception was thrown. 2017.01.18 Version 5.0.0 * Prefix support for listing files and directories. diff --git a/microsoft-azure-storage-test/src/com/microsoft/azure/storage/file/CloudFileTests.java b/microsoft-azure-storage-test/src/com/microsoft/azure/storage/file/CloudFileTests.java index 125e4a296253e..9bf77cfb9ea47 100644 --- a/microsoft-azure-storage-test/src/com/microsoft/azure/storage/file/CloudFileTests.java +++ b/microsoft-azure-storage-test/src/com/microsoft/azure/storage/file/CloudFileTests.java @@ -86,7 +86,7 @@ public void fileTestMethodSetUp() throws URISyntaxException, StorageException { @After public void fileTestMethodTearDown() throws StorageException { - //this.share.deleteIfExists(); + this.share.deleteIfExists(); } /** @@ -1124,6 +1124,28 @@ public void testCloudFileUploadRange() throws URISyntaxException, StorageExcepti inputStream = new ByteArrayInputStream(buffer); } + + @Test + @Category({ DevFabricTests.class, DevStoreTests.class }) + public void testVerifyTransactionalMD5ValidationMissingOverallMD5() throws URISyntaxException, StorageException, IOException { + final String fileName = FileTestHelper.generateRandomFileName(); + final CloudFile fileRef = this.share.getRootDirectoryReference().getFileReference(fileName); + + final int length = 3*1024; + ByteArrayInputStream srcStream = BlobTestHelper.getRandomDataStream(length); + FileRequestOptions options = new FileRequestOptions(); + options.setDisableContentMD5Validation(true); + options.setStoreFileContentMD5(false); + + fileRef.upload(srcStream, length, null, options, null); + + options.setDisableContentMD5Validation(false); + options.setStoreFileContentMD5(true); + options.setUseTransactionalContentMD5(true); + final CloudFile fileRef2 = this.share.getRootDirectoryReference().getFileReference(fileName); + fileRef2.downloadRange(1024, (long)1024, new ByteArrayOutputStream(), null, options, null); + assertNull(fileRef2.getProperties().getContentMD5()); + } /** * Test clearing file ranges. diff --git a/microsoft-azure-storage/src/com/microsoft/azure/storage/file/CloudFile.java b/microsoft-azure-storage/src/com/microsoft/azure/storage/file/CloudFile.java index aefd1349b8a34..749e9304cb050 100644 --- a/microsoft-azure-storage/src/com/microsoft/azure/storage/file/CloudFile.java +++ b/microsoft-azure-storage/src/com/microsoft/azure/storage/file/CloudFile.java @@ -1379,12 +1379,6 @@ public Integer preProcessResponse(CloudFile file, CloudFileClient client, Operat final FileAttributes retrievedAttributes = FileResponse.getFileAttributes(this.getConnection(), file.getStorageUri()); - if (!options.getDisableContentMD5Validation() && options.getUseTransactionalContentMD5() - && Utility.isNullOrEmpty(retrievedAttributes.getProperties().getContentMD5())) { - throw new StorageException(StorageErrorCodeStrings.MISSING_MD5_HEADER, SR.MISSING_MD5, - Constants.HeaderConstants.HTTP_UNUSED_306, null, null); - } - file.properties = retrievedAttributes.getProperties(); file.metadata = retrievedAttributes.getMetadata(); @@ -1392,6 +1386,12 @@ public Integer preProcessResponse(CloudFile file, CloudFileClient client, Operat // We would still need to verify the entire range. this.setContentMD5(this.getConnection().getHeaderField(Constants.HeaderConstants.CONTENT_MD5)); + if (!options.getDisableContentMD5Validation() && options.getUseTransactionalContentMD5() + && Utility.isNullOrEmpty(this.getContentMD5())) { + throw new StorageException(StorageErrorCodeStrings.MISSING_MD5_HEADER, SR.MISSING_MD5, + Constants.HeaderConstants.HTTP_UNUSED_306, null, null); + } + this.setLockedETag(file.properties.getEtag()); this.setArePropertiesPopulated(true); }