public void BlobUploadWithoutMD5ValidationAndStoreBlobContentTest() { byte[] buffer = GetRandomBuffer(2 * 1024); CloudBlobContainer container = GetRandomContainerReference(); try { container.Create(); CloudPageBlob blob = container.GetPageBlobReference("blob1"); BlobRequestOptions options = new BlobRequestOptions(); options.DisableContentMD5Validation = false; options.StoreBlobContentMD5 = false; OperationContext context = new OperationContext(); using (MemoryStream srcStream = new MemoryStream(buffer)) { blob.UploadFromStream(srcStream, null, options, context); blob.FetchAttributes(); string md5 = blob.Properties.ContentMD5; blob.Properties.ContentMD5 = "MDAwMDAwMDA="; blob.SetProperties(null, options, context); byte[] testBuffer = new byte[2048]; MemoryStream dstStream = new MemoryStream(testBuffer); TestHelper.ExpectedException(() => blob.DownloadRangeToStream(dstStream, null, null, null, options, context), "Try to Download a stream with a corrupted md5 and DisableMD5Validation set to false", HttpStatusCode.OK); options.DisableContentMD5Validation = true; blob.SetProperties(null, options, context); byte[] testBuffer2 = new byte[2048]; MemoryStream dstStream2 = new MemoryStream(testBuffer2); blob.DownloadRangeToStream(dstStream2, null, null, null, options, context); } } finally { container.DeleteIfExists(); } }
public void DisableContentMD5ValidationTest() { byte[] buffer = new byte[1024]; Random random = new Random(); random.NextBytes(buffer); BlobRequestOptions optionsWithNoMD5 = new BlobRequestOptions() { DisableContentMD5Validation = true, StoreBlobContentMD5 = true, }; BlobRequestOptions optionsWithMD5 = new BlobRequestOptions() { DisableContentMD5Validation = false, StoreBlobContentMD5 = true, }; CloudBlobContainer container = GetRandomContainerReference(); try { container.Create(); CloudBlockBlob blockBlob = container.GetBlockBlobReference("blob1"); using (Stream stream = new NonSeekableMemoryStream(buffer)) { blockBlob.UploadFromStream(stream, null, optionsWithMD5); } using (Stream stream = new MemoryStream()) { blockBlob.DownloadToStream(stream, null, optionsWithMD5); blockBlob.DownloadToStream(stream, null, optionsWithNoMD5); using (Stream blobStream = blockBlob.OpenRead(null, optionsWithMD5)) { int read; do { read = blobStream.Read(buffer, 0, buffer.Length); }while (read > 0); } using (Stream blobStream = blockBlob.OpenRead(null, optionsWithNoMD5)) { int read; do { read = blobStream.Read(buffer, 0, buffer.Length); }while (read > 0); } blockBlob.Properties.ContentMD5 = "MDAwMDAwMDA="; blockBlob.SetProperties(); TestHelper.ExpectedException( () => blockBlob.DownloadToStream(stream, null, optionsWithMD5), "Downloading a blob with invalid MD5 should fail", HttpStatusCode.OK); blockBlob.DownloadToStream(stream, null, optionsWithNoMD5); using (Stream blobStream = blockBlob.OpenRead(null, optionsWithMD5)) { TestHelper.ExpectedException <IOException>( () => { int read; do { read = blobStream.Read(buffer, 0, buffer.Length); }while (read > 0); }, "Downloading a blob with invalid MD5 should fail"); } using (Stream blobStream = blockBlob.OpenRead(null, optionsWithNoMD5)) { int read; do { read = blobStream.Read(buffer, 0, buffer.Length); }while (read > 0); } } CloudPageBlob pageBlob = container.GetPageBlobReference("blob2"); using (Stream stream = new MemoryStream(buffer)) { pageBlob.UploadFromStream(stream, null, optionsWithMD5); } using (Stream stream = new MemoryStream()) { pageBlob.DownloadToStream(stream, null, optionsWithMD5); pageBlob.DownloadToStream(stream, null, optionsWithNoMD5); using (Stream blobStream = pageBlob.OpenRead(null, optionsWithMD5)) { int read; do { read = blobStream.Read(buffer, 0, buffer.Length); }while (read > 0); } using (Stream blobStream = pageBlob.OpenRead(null, optionsWithNoMD5)) { int read; do { read = blobStream.Read(buffer, 0, buffer.Length); }while (read > 0); } pageBlob.Properties.ContentMD5 = "MDAwMDAwMDA="; pageBlob.SetProperties(); TestHelper.ExpectedException( () => pageBlob.DownloadToStream(stream, null, optionsWithMD5), "Downloading a blob with invalid MD5 should fail", HttpStatusCode.OK); pageBlob.DownloadToStream(stream, null, optionsWithNoMD5); using (Stream blobStream = pageBlob.OpenRead(null, optionsWithMD5)) { TestHelper.ExpectedException <IOException>( () => { int read; do { read = blobStream.Read(buffer, 0, buffer.Length); }while (read > 0); }, "Downloading a blob with invalid MD5 should fail"); } using (Stream blobStream = pageBlob.OpenRead(null, optionsWithNoMD5)) { int read; do { read = blobStream.Read(buffer, 0, buffer.Length); }while (read > 0); } } } finally { container.DeleteIfExists(); } }