internal static async Task CreateForTestAsync(CloudBlockBlob blob, int blockCount, int blockSize, bool commit = true) { byte[] buffer = GetRandomBuffer(blockSize); List <string> blocks = GetBlockIdList(blockCount); foreach (string block in blocks) { using (MemoryStream stream = new MemoryStream(buffer)) { await blob.PutBlockAsync(block, stream, null); } } if (commit) { await blob.PutBlockListAsync(blocks); } }
public async Task UseTransactionalCRC64PutTestAsync() { BlobRequestOptions optionsWithNoCRC64 = new BlobRequestOptions() { ChecksumOptions = new ChecksumOptions { UseTransactionalCRC64 = false } }; BlobRequestOptions optionsWithCRC64 = new BlobRequestOptions() { ChecksumOptions = new ChecksumOptions { UseTransactionalCRC64 = true } }; byte[] buffer = GetRandomBuffer(1024); Crc64Wrapper hasher = new Crc64Wrapper(); hasher.UpdateHash(buffer, 0, buffer.Length); string crc64 = hasher.ComputeHash(); string lastCheckCRC64 = null; int checkCount = 0; OperationContext opContextWithCRC64Check = new OperationContext(); opContextWithCRC64Check.SendingRequest += (_, args) => { if (HttpRequestParsers.GetContentLength(args.Request) >= buffer.Length) { lastCheckCRC64 = HttpRequestParsers.GetContentCRC64(args.Request); checkCount++; } }; OperationContext opContextWithCRC64CheckAndInjectedFailure = new OperationContext(); opContextWithCRC64CheckAndInjectedFailure.SendingRequest += (_, args) => { args.Response.Headers.Remove(Constants.HeaderConstants.ContentCrc64Header); args.Request.Headers.TryAddWithoutValidation(Constants.HeaderConstants.ContentCrc64Header, "dummy"); if (HttpRequestParsers.GetContentLength(args.Request) >= buffer.Length) { lastCheckCRC64 = HttpRequestParsers.GetContentCRC64(args.Request); checkCount++; } }; CloudBlobContainer container = GetRandomContainerReference(); try { await container.CreateAsync(); CloudBlockBlob blockBlob = container.GetBlockBlobReference("blob1"); List <string> blockIds = GetBlockIdList(3); checkCount = 0; using (Stream blockData = new MemoryStream(buffer)) { lastCheckCRC64 = "invalid_CRC64"; await blockBlob.PutBlockAsync(blockIds[0], blockData, null, null, optionsWithNoCRC64, opContextWithCRC64Check); Assert.IsNull(lastCheckCRC64); lastCheckCRC64 = "invalid_CRC64"; blockData.Seek(0, SeekOrigin.Begin); await blockBlob.PutBlockAsync(blockIds[1], blockData, null, null, optionsWithCRC64, opContextWithCRC64Check); Assert.AreEqual(crc64, lastCheckCRC64); blockData.Seek(0, SeekOrigin.Begin); await TestHelper.ExpectedExceptionAsync <StorageException>( () => blockBlob.PutBlockAsync(blockIds[1], blockData, null, null, optionsWithCRC64, opContextWithCRC64CheckAndInjectedFailure), "Calculated CRC64 does not match existing property" ); lastCheckCRC64 = "invalid_CRC64"; blockData.Seek(0, SeekOrigin.Begin); await blockBlob.PutBlockAsync(blockIds[2], blockData, new Checksum(crc64 : crc64), null, optionsWithNoCRC64, opContextWithCRC64Check); Assert.AreEqual(crc64, lastCheckCRC64); } Assert.AreEqual(3, checkCount); checkCount = 0; CloudAppendBlob appendBlob = container.GetAppendBlobReference("blob2"); await appendBlob.CreateOrReplaceAsync(); checkCount = 0; using (Stream blockData = new MemoryStream(buffer)) { lastCheckCRC64 = "invalid_CRC64"; await appendBlob.AppendBlockAsync(blockData, null, null, optionsWithNoCRC64, opContextWithCRC64Check); Assert.IsNull(lastCheckCRC64); lastCheckCRC64 = "invalid_CRC64"; blockData.Seek(0, SeekOrigin.Begin); await appendBlob.AppendBlockAsync(blockData, null, null, optionsWithCRC64, opContextWithCRC64Check); Assert.AreEqual(crc64, lastCheckCRC64); blockData.Seek(0, SeekOrigin.Begin); await TestHelper.ExpectedExceptionAsync <StorageException>( () => appendBlob.AppendBlockAsync(blockData, null, null, optionsWithCRC64, opContextWithCRC64CheckAndInjectedFailure), "Calculated CRC64 does not match existing property" ); lastCheckCRC64 = "invalid_CRC64"; blockData.Seek(0, SeekOrigin.Begin); await appendBlob.AppendBlockAsync(blockData, new Checksum(crc64 : crc64), null, optionsWithNoCRC64, opContextWithCRC64Check); Assert.AreEqual(crc64, lastCheckCRC64); } Assert.AreEqual(3, checkCount); CloudPageBlob pageBlob = container.GetPageBlobReference("blob3"); await pageBlob.CreateAsync(buffer.Length); checkCount = 0; using (Stream pageData = new MemoryStream(buffer)) { lastCheckCRC64 = "invalid_CRC64"; await pageBlob.WritePagesAsync(pageData, 0, null, null, optionsWithNoCRC64, opContextWithCRC64Check); Assert.IsNull(lastCheckCRC64); lastCheckCRC64 = "invalid_CRC64"; pageData.Seek(0, SeekOrigin.Begin); await pageBlob.WritePagesAsync(pageData, 0, null, null, optionsWithCRC64, opContextWithCRC64Check); Assert.AreEqual(crc64, lastCheckCRC64); pageData.Seek(0, SeekOrigin.Begin); await TestHelper.ExpectedExceptionAsync <StorageException>( () => pageBlob.WritePagesAsync(pageData, 0, null, null, optionsWithCRC64, opContextWithCRC64CheckAndInjectedFailure), "Calculated CRC64 does not match existing property" ); lastCheckCRC64 = "invalid_CRC64"; pageData.Seek(0, SeekOrigin.Begin); await pageBlob.WritePagesAsync(pageData, 0, new Checksum(crc64 : crc64), null, optionsWithNoCRC64, opContextWithCRC64Check); Assert.AreEqual(crc64, lastCheckCRC64); } Assert.AreEqual(3, checkCount); lastCheckCRC64 = null; blockBlob = container.GetBlockBlobReference("blob4"); checkCount = 0; using (Stream blobStream = await blockBlob.OpenWriteAsync(null, optionsWithCRC64, opContextWithCRC64Check)) { blobStream.Write(buffer, 0, buffer.Length); blobStream.Write(buffer, 0, buffer.Length); } Assert.IsNotNull(lastCheckCRC64); Assert.AreEqual(1, checkCount); lastCheckCRC64 = "invalid_CRC64"; blockBlob = container.GetBlockBlobReference("blob5"); checkCount = 0; using (Stream blobStream = await blockBlob.OpenWriteAsync(null, optionsWithNoCRC64, opContextWithCRC64Check)) { blobStream.Write(buffer, 0, buffer.Length); blobStream.Write(buffer, 0, buffer.Length); } Assert.IsNull(lastCheckCRC64); Assert.AreEqual(1, checkCount); lastCheckCRC64 = null; pageBlob = container.GetPageBlobReference("blob6"); checkCount = 0; using (Stream blobStream = await pageBlob.OpenWriteAsync(buffer.Length * 3, null, optionsWithCRC64, opContextWithCRC64Check)) { blobStream.Write(buffer, 0, buffer.Length); blobStream.Write(buffer, 0, buffer.Length); } Assert.IsNotNull(lastCheckCRC64); Assert.AreEqual(1, checkCount); lastCheckCRC64 = "invalid_CRC64"; pageBlob = container.GetPageBlobReference("blob7"); checkCount = 0; using (Stream blobStream = await pageBlob.OpenWriteAsync(buffer.Length * 3, null, optionsWithNoCRC64, opContextWithCRC64Check)) { blobStream.Write(buffer, 0, buffer.Length); blobStream.Write(buffer, 0, buffer.Length); } Assert.IsNull(lastCheckCRC64); Assert.AreEqual(1, checkCount); } finally { await container.DeleteIfExistsAsync(); } }
public async Task UseTransactionalMD5PutTestAsync() { BlobRequestOptions optionsWithNoMD5 = new BlobRequestOptions() { UseTransactionalMD5 = false, }; BlobRequestOptions optionsWithMD5 = new BlobRequestOptions() { UseTransactionalMD5 = true, }; byte[] buffer = GetRandomBuffer(1024); MD5 hasher = MD5.Create(); string md5 = Convert.ToBase64String(hasher.ComputeHash(buffer)); string lastCheckMD5 = null; int checkCount = 0; OperationContext opContextWithMD5Check = new OperationContext(); opContextWithMD5Check.SendingRequest += (_, args) => { if (HttpRequestParsers.GetContentLength(args.Request) >= buffer.Length) { lastCheckMD5 = HttpRequestParsers.GetContentMD5(args.Request); checkCount++; } }; CloudBlobContainer container = GetRandomContainerReference(); try { await container.CreateAsync(); CloudBlockBlob blockBlob = container.GetBlockBlobReference("blob1"); List <string> blockIds = GetBlockIdList(3); checkCount = 0; using (Stream blockData = new MemoryStream(buffer)) { lastCheckMD5 = "invalid_md5"; await blockBlob.PutBlockAsync(blockIds[0], blockData, null, null, optionsWithNoMD5, opContextWithMD5Check); Assert.IsNull(lastCheckMD5); lastCheckMD5 = "invalid_md5"; blockData.Seek(0, SeekOrigin.Begin); await blockBlob.PutBlockAsync(blockIds[1], blockData, null, null, optionsWithMD5, opContextWithMD5Check); Assert.AreEqual(md5, lastCheckMD5); lastCheckMD5 = "invalid_md5"; blockData.Seek(0, SeekOrigin.Begin); await blockBlob.PutBlockAsync(blockIds[2], blockData, md5, null, optionsWithNoMD5, opContextWithMD5Check); Assert.AreEqual(md5, lastCheckMD5); } Assert.AreEqual(3, checkCount); checkCount = 0; CloudAppendBlob appendBlob = container.GetAppendBlobReference("blob2"); await appendBlob.CreateOrReplaceAsync(); checkCount = 0; using (Stream blockData = new MemoryStream(buffer)) { lastCheckMD5 = "invalid_md5"; await appendBlob.AppendBlockAsync(blockData, null, null, optionsWithNoMD5, opContextWithMD5Check); Assert.IsNull(lastCheckMD5); lastCheckMD5 = "invalid_md5"; blockData.Seek(0, SeekOrigin.Begin); await appendBlob.AppendBlockAsync(blockData, null, null, optionsWithMD5, opContextWithMD5Check); Assert.AreEqual(md5, lastCheckMD5); lastCheckMD5 = "invalid_md5"; blockData.Seek(0, SeekOrigin.Begin); await appendBlob.AppendBlockAsync(blockData, md5, null, optionsWithNoMD5, opContextWithMD5Check); Assert.AreEqual(md5, lastCheckMD5); } Assert.AreEqual(3, checkCount); CloudPageBlob pageBlob = container.GetPageBlobReference("blob3"); await pageBlob.CreateAsync(buffer.Length); checkCount = 0; using (Stream pageData = new MemoryStream(buffer)) { lastCheckMD5 = "invalid_md5"; await pageBlob.WritePagesAsync(pageData, 0, null, null, optionsWithNoMD5, opContextWithMD5Check); Assert.IsNull(lastCheckMD5); lastCheckMD5 = "invalid_md5"; pageData.Seek(0, SeekOrigin.Begin); await pageBlob.WritePagesAsync(pageData, 0, null, null, optionsWithMD5, opContextWithMD5Check); Assert.AreEqual(md5, lastCheckMD5); lastCheckMD5 = "invalid_md5"; pageData.Seek(0, SeekOrigin.Begin); await pageBlob.WritePagesAsync(pageData, 0, md5, null, optionsWithNoMD5, opContextWithMD5Check); Assert.AreEqual(md5, lastCheckMD5); } Assert.AreEqual(3, checkCount); lastCheckMD5 = null; blockBlob = container.GetBlockBlobReference("blob4"); checkCount = 0; using (Stream blobStream = await blockBlob.OpenWriteAsync(null, optionsWithMD5, opContextWithMD5Check)) { blobStream.Write(buffer, 0, buffer.Length); blobStream.Write(buffer, 0, buffer.Length); } Assert.IsNotNull(lastCheckMD5); Assert.AreEqual(1, checkCount); lastCheckMD5 = "invalid_md5"; blockBlob = container.GetBlockBlobReference("blob5"); checkCount = 0; using (Stream blobStream = await blockBlob.OpenWriteAsync(null, optionsWithNoMD5, opContextWithMD5Check)) { blobStream.Write(buffer, 0, buffer.Length); blobStream.Write(buffer, 0, buffer.Length); } Assert.IsNull(lastCheckMD5); Assert.AreEqual(1, checkCount); lastCheckMD5 = null; pageBlob = container.GetPageBlobReference("blob6"); checkCount = 0; using (Stream blobStream = await pageBlob.OpenWriteAsync(buffer.Length * 3, null, optionsWithMD5, opContextWithMD5Check)) { blobStream.Write(buffer, 0, buffer.Length); blobStream.Write(buffer, 0, buffer.Length); } Assert.IsNotNull(lastCheckMD5); Assert.AreEqual(1, checkCount); lastCheckMD5 = "invalid_md5"; pageBlob = container.GetPageBlobReference("blob7"); checkCount = 0; using (Stream blobStream = await pageBlob.OpenWriteAsync(buffer.Length * 3, null, optionsWithNoMD5, opContextWithMD5Check)) { blobStream.Write(buffer, 0, buffer.Length); blobStream.Write(buffer, 0, buffer.Length); } Assert.IsNull(lastCheckMD5); Assert.AreEqual(1, checkCount); } finally { await container.DeleteIfExistsAsync(); } }