public void FileReadLockToETagTestAPM() { byte[] outBuffer = new byte[1 * 1024 * 1024]; byte[] buffer = GetRandomBuffer(2 * outBuffer.Length); CloudFileShare share = GetRandomShareReference(); try { share.Create(); CloudFile file = share.GetRootDirectoryReference().GetFileReference("file1"); file.StreamMinimumReadSizeInBytes = outBuffer.Length; using (MemoryStream wholeFile = new MemoryStream(buffer)) { file.UploadFromStream(wholeFile); } using (AutoResetEvent waitHandle = new AutoResetEvent(false)) { IAsyncResult result = file.BeginOpenRead( ar => waitHandle.Set(), null); waitHandle.WaitOne(); using (Stream fileStream = file.EndOpenRead(result)) { fileStream.Read(outBuffer, 0, outBuffer.Length); file.SetMetadata(); TestHelper.ExpectedException( () => fileStream.Read(outBuffer, 0, outBuffer.Length), "File read stream should fail if file is modified during read", HttpStatusCode.PreconditionFailed); } result = file.BeginOpenRead( ar => waitHandle.Set(), null); waitHandle.WaitOne(); using (Stream fileStream = file.EndOpenRead(result)) { long length = fileStream.Length; file.SetMetadata(); TestHelper.ExpectedException( () => fileStream.Read(outBuffer, 0, outBuffer.Length), "File read stream should fail if file is modified during read", HttpStatusCode.PreconditionFailed); } /* * AccessCondition accessCondition = AccessCondition.GenerateIfNotModifiedSinceCondition(DateTimeOffset.Now.Subtract(TimeSpan.FromHours(1))); * file.SetMetadata(); * result = file.BeginOpenRead( * accessCondition, * null, * null, * ar => waitHandle.Set(), * null); * waitHandle.WaitOne(); * TestHelper.ExpectedException( * () => file.EndOpenRead(result), * "File read stream should fail if file is modified during read", * HttpStatusCode.PreconditionFailed); */ } } finally { share.DeleteIfExists(); } }
public void FileUseTransactionalMD5GetTestAPM() { FileRequestOptions optionsWithNoMD5 = new FileRequestOptions() { UseTransactionalMD5 = false, }; FileRequestOptions optionsWithMD5 = new FileRequestOptions() { UseTransactionalMD5 = true, }; byte[] buffer = GetRandomBuffer(3 * 1024 * 1024); MD5 hasher = MD5.Create(); string md5 = Convert.ToBase64String(hasher.ComputeHash(buffer)); string lastCheckMD5 = null; int checkCount = 0; OperationContext opContextWithMD5Check = new OperationContext(); opContextWithMD5Check.ResponseReceived += (_, args) => { if (long.Parse(HttpResponseParsers.GetContentLength(args.Response)) >= buffer.Length) { lastCheckMD5 = HttpResponseParsers.GetContentMD5(args.Response); checkCount++; } }; CloudFileShare share = GetRandomShareReference(); try { share.Create(); using (AutoResetEvent waitHandle = new AutoResetEvent(false)) { IAsyncResult result; CloudFile file = share.GetRootDirectoryReference().GetFileReference("file2"); using (Stream fileStream = file.OpenWrite(buffer.Length * 2)) { fileStream.Write(buffer, 0, buffer.Length); fileStream.Write(buffer, 0, buffer.Length); } checkCount = 0; using (Stream stream = new MemoryStream()) { result = file.BeginDownloadToStream(stream, null, optionsWithNoMD5, opContextWithMD5Check, ar => waitHandle.Set(), null); waitHandle.WaitOne(); file.EndDownloadRangeToStream(result); Assert.IsNull(lastCheckMD5); result = file.BeginDownloadToStream(stream, null, optionsWithMD5, opContextWithMD5Check, ar => waitHandle.Set(), null); waitHandle.WaitOne(); StorageException storageEx = TestHelper.ExpectedException <StorageException>( () => file.EndDownloadRangeToStream(result), "File will not have MD5 set by default; with UseTransactional, download should fail"); result = file.BeginDownloadRangeToStream(stream, buffer.Length, buffer.Length, null, optionsWithNoMD5, opContextWithMD5Check, ar => waitHandle.Set(), null); waitHandle.WaitOne(); file.EndDownloadRangeToStream(result); Assert.IsNull(lastCheckMD5); result = file.BeginDownloadRangeToStream(stream, buffer.Length, buffer.Length, null, optionsWithMD5, opContextWithMD5Check, ar => waitHandle.Set(), null); waitHandle.WaitOne(); file.EndDownloadRangeToStream(result); Assert.AreEqual(md5, lastCheckMD5); result = file.BeginDownloadRangeToStream(stream, 1024, 4 * 1024 * 1024 + 1, null, optionsWithNoMD5, opContextWithMD5Check, ar => waitHandle.Set(), null); waitHandle.WaitOne(); file.EndDownloadRangeToStream(result); Assert.IsNull(lastCheckMD5); result = file.BeginDownloadRangeToStream(stream, 1024, 4 * 1024 * 1024 + 1, null, optionsWithMD5, opContextWithMD5Check, ar => waitHandle.Set(), null); waitHandle.WaitOne(); storageEx = TestHelper.ExpectedException <StorageException>( () => file.EndDownloadRangeToStream(result), "Downloading more than 4MB with transactional MD5 should not be supported"); Assert.IsInstanceOfType(storageEx.InnerException, typeof(ArgumentOutOfRangeException)); result = file.BeginOpenRead(null, optionsWithMD5, opContextWithMD5Check, ar => waitHandle.Set(), null); waitHandle.WaitOne(); using (Stream fileStream = file.EndOpenRead(result)) { fileStream.CopyTo(stream); Assert.IsNotNull(lastCheckMD5); } result = file.BeginOpenRead(null, optionsWithNoMD5, opContextWithMD5Check, ar => waitHandle.Set(), null); waitHandle.WaitOne(); using (Stream fileStream = file.EndOpenRead(result)) { fileStream.CopyTo(stream); Assert.IsNull(lastCheckMD5); } } Assert.AreEqual(9, checkCount); } } finally { share.DeleteIfExists(); } }