public void FileWriteStreamFlushTestAPM() { byte[] buffer = GetRandomBuffer(512 * 1024); CloudFileShare share = GetRandomShareReference(); try { share.Create(); CloudFile file = share.GetRootDirectoryReference().GetFileReference("file1"); file.StreamWriteSizeInBytes = 1 * 1024 * 1024; using (MemoryStream wholeFile = new MemoryStream()) { FileRequestOptions options = new FileRequestOptions() { StoreFileContentMD5 = true }; OperationContext opContext = new OperationContext(); using (CloudFileStream fileStream = file.OpenWrite(4 * buffer.Length, null, options, opContext)) { using (AutoResetEvent waitHandle = new AutoResetEvent(false)) { IAsyncResult result; for (int i = 0; i < 3; i++) { result = fileStream.BeginWrite( buffer, 0, buffer.Length, ar => waitHandle.Set(), null); waitHandle.WaitOne(); fileStream.EndWrite(result); wholeFile.Write(buffer, 0, buffer.Length); } Assert.AreEqual(2, opContext.RequestResults.Count); ICancellableAsyncResult cancellableResult = fileStream.BeginFlush( ar => waitHandle.Set(), null); Assert.IsFalse(cancellableResult.IsCompleted); cancellableResult.Cancel(); waitHandle.WaitOne(); fileStream.EndFlush(cancellableResult); result = fileStream.BeginFlush( ar => waitHandle.Set(), null); Assert.IsFalse(result.IsCompleted); TestHelper.ExpectedException <InvalidOperationException>( () => fileStream.BeginFlush(null, null), null); waitHandle.WaitOne(); TestHelper.ExpectedException <InvalidOperationException>( () => fileStream.BeginFlush(null, null), null); fileStream.EndFlush(result); Assert.IsFalse(result.CompletedSynchronously); Assert.AreEqual(3, opContext.RequestResults.Count); result = fileStream.BeginFlush( ar => waitHandle.Set(), null); Assert.IsTrue(result.CompletedSynchronously); waitHandle.WaitOne(); fileStream.EndFlush(result); Assert.AreEqual(3, opContext.RequestResults.Count); result = fileStream.BeginWrite( buffer, 0, buffer.Length, ar => waitHandle.Set(), null); waitHandle.WaitOne(); fileStream.EndWrite(result); wholeFile.Write(buffer, 0, buffer.Length); Assert.AreEqual(3, opContext.RequestResults.Count); result = fileStream.BeginCommit( ar => waitHandle.Set(), null); waitHandle.WaitOne(); fileStream.EndCommit(result); Assert.AreEqual(5, opContext.RequestResults.Count); } } Assert.AreEqual(5, opContext.RequestResults.Count); using (MemoryStream downloadedFile = new MemoryStream()) { file.DownloadToStream(downloadedFile); TestHelper.AssertStreamsAreEqual(wholeFile, downloadedFile); } } } finally { share.DeleteIfExists(); } }
public void FileWriteStreamBasicTestAPM() { byte[] buffer = GetRandomBuffer(2 * 1024 * 1024); MD5 hasher = MD5.Create(); CloudFileClient fileClient = GenerateCloudFileClient(); fileClient.DefaultRequestOptions.ParallelOperationThreadCount = 4; string name = GetRandomShareName(); CloudFileShare share = fileClient.GetShareReference(name); try { share.Create(); CloudFile file = share.GetRootDirectoryReference().GetFileReference("file1"); file.StreamWriteSizeInBytes = buffer.Length; using (MemoryStream wholeFile = new MemoryStream()) { FileRequestOptions options = new FileRequestOptions() { StoreFileContentMD5 = true, }; using (AutoResetEvent waitHandle = new AutoResetEvent(false)) { IAsyncResult result = file.BeginOpenWrite(fileClient.DefaultRequestOptions.ParallelOperationThreadCount.Value * 2 * buffer.Length, null, options, null, ar => waitHandle.Set(), null); waitHandle.WaitOne(); using (CloudFileStream fileStream = file.EndOpenWrite(result)) { IAsyncResult[] results = new IAsyncResult[fileClient.DefaultRequestOptions.ParallelOperationThreadCount.Value * 2]; for (int i = 0; i < results.Length; i++) { results[i] = fileStream.BeginWrite(buffer, 0, buffer.Length, null, null); wholeFile.Write(buffer, 0, buffer.Length); Assert.AreEqual(wholeFile.Position, fileStream.Position); } for (int i = 0; i < fileClient.DefaultRequestOptions.ParallelOperationThreadCount.Value; i++) { Assert.IsTrue(results[i].IsCompleted); } for (int i = fileClient.DefaultRequestOptions.ParallelOperationThreadCount.Value; i < results.Length; i++) { Assert.IsFalse(results[i].IsCompleted); } for (int i = 0; i < results.Length; i++) { fileStream.EndWrite(results[i]); } result = fileStream.BeginCommit( ar => waitHandle.Set(), null); waitHandle.WaitOne(); fileStream.EndCommit(result); } } wholeFile.Seek(0, SeekOrigin.Begin); string md5 = Convert.ToBase64String(hasher.ComputeHash(wholeFile)); file.FetchAttributes(); Assert.AreEqual(md5, file.Properties.ContentMD5); using (MemoryStream downloadedFile = new MemoryStream()) { file.DownloadToStream(downloadedFile); TestHelper.AssertStreamsAreEqual(wholeFile, downloadedFile); } fileClient.DefaultRequestOptions.ParallelOperationThreadCount = 2; TestHelper.ExpectedException <ArgumentException>( () => file.BeginOpenWrite(null, null, options, null, null, null), "BeginOpenWrite with StoreFileContentMD5 on an existing file should fail"); using (AutoResetEvent waitHandle = new AutoResetEvent(false)) { IAsyncResult result = file.BeginOpenWrite(null, ar => waitHandle.Set(), null); waitHandle.WaitOne(); using (Stream fileStream = file.EndOpenWrite(result)) { fileStream.Seek(buffer.Length / 2, SeekOrigin.Begin); wholeFile.Seek(buffer.Length / 2, SeekOrigin.Begin); IAsyncResult[] results = new IAsyncResult[fileClient.DefaultRequestOptions.ParallelOperationThreadCount.Value * 2]; for (int i = 0; i < results.Length; i++) { results[i] = fileStream.BeginWrite(buffer, 0, buffer.Length, null, null); wholeFile.Write(buffer, 0, buffer.Length); Assert.AreEqual(wholeFile.Position, fileStream.Position); } for (int i = 0; i < fileClient.DefaultRequestOptions.ParallelOperationThreadCount.Value; i++) { Assert.IsTrue(results[i].IsCompleted); } for (int i = fileClient.DefaultRequestOptions.ParallelOperationThreadCount.Value; i < results.Length; i++) { Assert.IsFalse(results[i].IsCompleted); } for (int i = 0; i < results.Length; i++) { fileStream.EndWrite(results[i]); } wholeFile.Seek(0, SeekOrigin.End); } file.FetchAttributes(); Assert.AreEqual(md5, file.Properties.ContentMD5); using (MemoryStream downloadedFile = new MemoryStream()) { options.DisableContentMD5Validation = true; file.DownloadToStream(downloadedFile, null, options); TestHelper.AssertStreamsAreEqual(wholeFile, downloadedFile); } } } } finally { share.DeleteIfExists(); } }
public void FileWriteStreamFlushTest() { byte[] buffer = GetRandomBuffer(512); CloudFileShare share = GetRandomShareReference(); try { share.Create(); CloudFile file = share.GetRootDirectoryReference().GetFileReference("file1"); file.StreamWriteSizeInBytes = 1024; using (MemoryStream wholeFile = new MemoryStream()) { FileRequestOptions options = new FileRequestOptions() { StoreFileContentMD5 = true }; OperationContext opContext = new OperationContext(); using (CloudFileStream fileStream = file.OpenWrite(4 * 512, null, options, opContext)) { for (int i = 0; i < 3; i++) { fileStream.Write(buffer, 0, buffer.Length); wholeFile.Write(buffer, 0, buffer.Length); } Assert.AreEqual(2, opContext.RequestResults.Count); fileStream.Flush(); Assert.AreEqual(3, opContext.RequestResults.Count); fileStream.Flush(); Assert.AreEqual(3, opContext.RequestResults.Count); fileStream.Write(buffer, 0, buffer.Length); wholeFile.Write(buffer, 0, buffer.Length); Assert.AreEqual(3, opContext.RequestResults.Count); fileStream.Commit(); Assert.AreEqual(5, opContext.RequestResults.Count); } Assert.AreEqual(5, opContext.RequestResults.Count); using (MemoryStream downloadedFile = new MemoryStream()) { file.DownloadToStream(downloadedFile); TestHelper.AssertStreamsAreEqual(wholeFile, downloadedFile); } } } finally { share.DeleteIfExists(); } }
public void CloudFileDirectoryListFilesAndDirectoriesAPM() { CloudFileClient client = GenerateCloudFileClient(); string name = GetRandomShareName(); CloudFileShare share = client.GetShareReference(name); try { share.Create(); if (CloudFileDirectorySetup(share)) { CloudFileDirectory topDir1 = share.GetRootDirectoryReference().GetDirectoryReference("TopDir1"); using (AutoResetEvent waitHandle = new AutoResetEvent(false)) { FileContinuationToken token = null; List <IListFileItem> simpleList1 = new List <IListFileItem>(); do { IAsyncResult result = topDir1.BeginListFilesAndDirectoriesSegmented( null, null, null, null, ar => waitHandle.Set(), null); waitHandle.WaitOne(); FileResultSegment segment = topDir1.EndListFilesAndDirectoriesSegmented(result); simpleList1.AddRange(segment.Results); token = segment.ContinuationToken; }while (token != null); ////Check if for 3 because if there were more than 3, the previous assert would have failed. ////So the only thing we need to make sure is that it is not less than 3. Assert.IsTrue(simpleList1.Count == 3); IListFileItem item11 = simpleList1.ElementAt(0); Assert.IsTrue(item11.Uri.Equals(share.Uri + "/TopDir1/File1")); Assert.AreEqual("File1", ((CloudFile)item11).Name); IListFileItem item12 = simpleList1.ElementAt(1); Assert.IsTrue(item12.Uri.Equals(share.Uri + "/TopDir1/MidDir1")); Assert.AreEqual("MidDir1", ((CloudFileDirectory)item12).Name); IListFileItem item13 = simpleList1.ElementAt(2); Assert.IsTrue(item13.Uri.Equals(share.Uri + "/TopDir1/MidDir2")); Assert.AreEqual("MidDir2", ((CloudFileDirectory)item13).Name); CloudFileDirectory midDir2 = (CloudFileDirectory)item13; List <IListFileItem> simpleList2 = new List <IListFileItem>(); do { IAsyncResult result = midDir2.BeginListFilesAndDirectoriesSegmented( token, ar => waitHandle.Set(), null); waitHandle.WaitOne(); FileResultSegment segment = midDir2.EndListFilesAndDirectoriesSegmented(result); simpleList2.AddRange(segment.Results); token = segment.ContinuationToken; }while (token != null); Assert.IsTrue(simpleList2.Count == 2); IListFileItem item21 = simpleList2.ElementAt(0); Assert.IsTrue(item21.Uri.Equals(share.Uri + "/TopDir1/MidDir2/EndDir1")); Assert.AreEqual("EndDir1", ((CloudFileDirectory)item21).Name); IListFileItem item22 = simpleList2.ElementAt(1); Assert.IsTrue(item22.Uri.Equals(share.Uri + "/TopDir1/MidDir2/EndDir2")); Assert.AreEqual("EndDir2", ((CloudFileDirectory)item22).Name); } } } finally { share.DeleteIfExists(); } }
public void FileWriteStreamBasicTest() { byte[] buffer = GetRandomBuffer(6 * 512); MD5 hasher = MD5.Create(); CloudFileShare share = GetRandomShareReference(); share.ServiceClient.DefaultRequestOptions.ParallelOperationThreadCount = 2; try { share.Create(); CloudFile file = share.GetRootDirectoryReference().GetFileReference("file1"); file.StreamWriteSizeInBytes = 8 * 512; using (MemoryStream wholeFile = new MemoryStream()) { FileRequestOptions options = new FileRequestOptions() { StoreFileContentMD5 = true, }; using (Stream fileStream = file.OpenWrite(buffer.Length * 3, null, options)) { for (int i = 0; i < 3; i++) { fileStream.Write(buffer, 0, buffer.Length); wholeFile.Write(buffer, 0, buffer.Length); Assert.AreEqual(wholeFile.Position, fileStream.Position); } } wholeFile.Seek(0, SeekOrigin.Begin); string md5 = Convert.ToBase64String(hasher.ComputeHash(wholeFile)); file.FetchAttributes(); Assert.AreEqual(md5, file.Properties.ContentMD5); using (MemoryStream downloadedFile = new MemoryStream()) { file.DownloadToStream(downloadedFile); TestHelper.AssertStreamsAreEqual(wholeFile, downloadedFile); } TestHelper.ExpectedException <ArgumentException>( () => file.OpenWrite(null, null, options), "OpenWrite with StoreFileContentMD5 on an existing file should fail"); using (Stream fileStream = file.OpenWrite(null)) { fileStream.Seek(buffer.Length / 2, SeekOrigin.Begin); wholeFile.Seek(buffer.Length / 2, SeekOrigin.Begin); for (int i = 0; i < 2; i++) { fileStream.Write(buffer, 0, buffer.Length); wholeFile.Write(buffer, 0, buffer.Length); Assert.AreEqual(wholeFile.Position, fileStream.Position); } wholeFile.Seek(0, SeekOrigin.End); } file.FetchAttributes(); Assert.AreEqual(md5, file.Properties.ContentMD5); using (MemoryStream downloadedFile = new MemoryStream()) { options.DisableContentMD5Validation = true; file.DownloadToStream(downloadedFile, null, options); TestHelper.AssertStreamsAreEqual(wholeFile, downloadedFile); } } } finally { share.DeleteIfExists(); } }
public void CloudFileCopyTestAPM() { CloudFileShare share = GetRandomShareReference(); try { share.Create(); CloudFile source = share.GetRootDirectoryReference().GetFileReference("source"); string data = "String data"; UploadText(source, data, Encoding.UTF8); source.Metadata["Test"] = "value"; source.SetMetadata(); CloudFile copy = share.GetRootDirectoryReference().GetFileReference("copy"); using (AutoResetEvent waitHandle = new AutoResetEvent(false)) { IAsyncResult result = copy.BeginStartCopy(TestHelper.Defiddler(source), ar => waitHandle.Set(), null); waitHandle.WaitOne(); string copyId = copy.EndStartCopy(result); WaitForCopy(copy); Assert.AreEqual(CopyStatus.Success, copy.CopyState.Status); Assert.AreEqual(source.Uri.AbsolutePath, copy.CopyState.Source.AbsolutePath); Assert.AreEqual(data.Length, copy.CopyState.TotalBytes); Assert.AreEqual(data.Length, copy.CopyState.BytesCopied); Assert.AreEqual(copyId, copy.CopyState.CopyId); Assert.IsTrue(copy.CopyState.CompletionTime > DateTimeOffset.UtcNow.Subtract(TimeSpan.FromMinutes(1))); result = copy.BeginAbortCopy(copyId, ar => waitHandle.Set(), null); waitHandle.WaitOne(); TestHelper.ExpectedException( () => copy.EndAbortCopy(result), "Aborting a copy operation after completion should fail", HttpStatusCode.Conflict, "NoPendingCopyOperation"); } source.FetchAttributes(); Assert.IsNotNull(copy.Properties.ETag); Assert.AreNotEqual(source.Properties.ETag, copy.Properties.ETag); Assert.IsTrue(copy.Properties.LastModified > DateTimeOffset.UtcNow.Subtract(TimeSpan.FromMinutes(1))); string copyData = DownloadText(copy, Encoding.UTF8); Assert.AreEqual(data, copyData, "Data inside copy of file not similar"); copy.FetchAttributes(); FileProperties prop1 = copy.Properties; FileProperties prop2 = source.Properties; Assert.AreEqual(prop1.CacheControl, prop2.CacheControl); Assert.AreEqual(prop1.ContentEncoding, prop2.ContentEncoding); Assert.AreEqual(prop1.ContentDisposition, prop2.ContentDisposition); Assert.AreEqual(prop1.ContentLanguage, prop2.ContentLanguage); Assert.AreEqual(prop1.ContentMD5, prop2.ContentMD5); Assert.AreEqual(prop1.ContentType, prop2.ContentType); Assert.AreEqual("value", copy.Metadata["Test"], false, "Copied metadata not same"); copy.Delete(); } finally { share.DeleteIfExists(); } }
private static void CloudFileCopy(bool sourceIsSas, bool destinationIsSas) { CloudFileShare share = GetRandomShareReference(); try { share.Create(); // Create Source on server CloudFile source = share.GetRootDirectoryReference().GetFileReference("source"); string data = "String data"; UploadText(source, data, Encoding.UTF8); source.Metadata["Test"] = "value"; source.SetMetadata(); // Create Destination on server CloudFile destination = share.GetRootDirectoryReference().GetFileReference("destination"); destination.Create(1); CloudFile copySource = source; CloudFile copyDestination = destination; if (sourceIsSas) { // Source SAS must have read permissions SharedAccessFilePermissions permissions = SharedAccessFilePermissions.Read; SharedAccessFilePolicy policy = new SharedAccessFilePolicy() { SharedAccessStartTime = DateTimeOffset.UtcNow.AddMinutes(-5), SharedAccessExpiryTime = DateTimeOffset.UtcNow.AddMinutes(30), Permissions = permissions, }; string sasToken = source.GetSharedAccessSignature(policy); // Get source StorageCredentials credentials = new StorageCredentials(sasToken); copySource = new CloudFile(credentials.TransformUri(source.Uri)); } if (destinationIsSas) { Assert.IsTrue(sourceIsSas); // Destination SAS must have write permissions SharedAccessFilePermissions permissions = SharedAccessFilePermissions.Write; SharedAccessFilePolicy policy = new SharedAccessFilePolicy() { SharedAccessStartTime = DateTimeOffset.UtcNow.AddMinutes(-5), SharedAccessExpiryTime = DateTimeOffset.UtcNow.AddMinutes(30), Permissions = permissions, }; string sasToken = destination.GetSharedAccessSignature(policy); // Get destination StorageCredentials credentials = new StorageCredentials(sasToken); copyDestination = new CloudFile(credentials.TransformUri(destination.Uri)); } // Start copy and wait for completion string copyId = copyDestination.StartCopy(TestHelper.Defiddler(copySource)); WaitForCopy(destination); // Check original file references for equality Assert.AreEqual(CopyStatus.Success, destination.CopyState.Status); Assert.AreEqual(source.Uri.AbsolutePath, destination.CopyState.Source.AbsolutePath); Assert.AreEqual(data.Length, destination.CopyState.TotalBytes); Assert.AreEqual(data.Length, destination.CopyState.BytesCopied); Assert.AreEqual(copyId, destination.CopyState.CopyId); Assert.IsTrue(destination.CopyState.CompletionTime > DateTimeOffset.UtcNow.Subtract(TimeSpan.FromMinutes(1))); if (!destinationIsSas) { // Abort Copy is not supported for SAS destination TestHelper.ExpectedException( () => copyDestination.AbortCopy(copyId), "Aborting a copy operation after completion should fail", HttpStatusCode.Conflict, "NoPendingCopyOperation"); } source.FetchAttributes(); Assert.IsNotNull(destination.Properties.ETag); Assert.AreNotEqual(source.Properties.ETag, destination.Properties.ETag); Assert.IsTrue(destination.Properties.LastModified > DateTimeOffset.UtcNow.Subtract(TimeSpan.FromMinutes(1))); string copyData = DownloadText(destination, Encoding.UTF8); Assert.AreEqual(data, copyData, "Data inside copy of file not equal."); destination.FetchAttributes(); FileProperties prop1 = destination.Properties; FileProperties prop2 = source.Properties; Assert.AreEqual(prop1.CacheControl, prop2.CacheControl); Assert.AreEqual(prop1.ContentEncoding, prop2.ContentEncoding); Assert.AreEqual(prop1.ContentLanguage, prop2.ContentLanguage); Assert.AreEqual(prop1.ContentMD5, prop2.ContentMD5); Assert.AreEqual(prop1.ContentType, prop2.ContentType); Assert.AreEqual("value", destination.Metadata["Test"], false, "Copied metadata not same"); destination.Delete(); source.Delete(); } 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 (args.Response.ContentLength >= buffer.Length) { lastCheckMD5 = args.Response.Headers[HttpResponseHeader.ContentMd5]; 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(); } }
public void CloudFileClientMaximumExecutionTimeoutShouldNotBeHonoredForStreams() { CloudFileClient fileClient = GenerateCloudFileClient(); CloudFileShare share = fileClient.GetShareReference(Guid.NewGuid().ToString("N")); byte[] buffer = FileTestBase.GetRandomBuffer(1024 * 1024); try { share.Create(); fileClient.DefaultRequestOptions.MaximumExecutionTime = TimeSpan.FromSeconds(30); CloudFile file = share.GetRootDirectoryReference().GetFileReference("file"); file.StreamWriteSizeInBytes = 1024 * 1024; file.StreamMinimumReadSizeInBytes = 1024 * 1024; using (CloudFileStream bos = file.OpenWrite(8 * 1024 * 1024)) { DateTime start = DateTime.Now; for (int i = 0; i < 7; i++) { bos.Write(buffer, 0, buffer.Length); } // Sleep to ensure we are over the Max execution time when we do the last write int msRemaining = (int)(fileClient.DefaultRequestOptions.MaximumExecutionTime.Value - (DateTime.Now - start)).TotalMilliseconds; if (msRemaining > 0) { Thread.Sleep(msRemaining); } bos.Write(buffer, 0, buffer.Length); } using (Stream bis = file.OpenRead()) { DateTime start = DateTime.Now; int total = 0; while (total < 7 * 1024 * 1024) { total += bis.Read(buffer, 0, buffer.Length); } // Sleep to ensure we are over the Max execution time when we do the last read int msRemaining = (int)(fileClient.DefaultRequestOptions.MaximumExecutionTime.Value - (DateTime.Now - start)).TotalMilliseconds; if (msRemaining > 0) { Thread.Sleep(msRemaining); } while (true) { int count = bis.Read(buffer, 0, buffer.Length); total += count; if (count == 0) { break; } } } } finally { fileClient.DefaultRequestOptions.MaximumExecutionTime = null; share.DeleteIfExists(); } }
public void FileUseTransactionalMD5PutTestAPM() { FileRequestOptions optionsWithNoMD5 = new FileRequestOptions() { UseTransactionalMD5 = false, }; FileRequestOptions optionsWithMD5 = new FileRequestOptions() { 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 (args.Request.ContentLength >= buffer.Length) { lastCheckMD5 = args.Request.Headers[HttpRequestHeader.ContentMd5]; checkCount++; } }; CloudFileShare share = GetRandomShareReference(); try { share.Create(); using (AutoResetEvent waitHandle = new AutoResetEvent(false)) { IAsyncResult result; CloudFile file = share.GetRootDirectoryReference().GetFileReference("file2"); file.Create(buffer.Length); checkCount = 0; using (Stream fileData = new MemoryStream(buffer)) { result = file.BeginWriteRange(fileData, 0, null, null, optionsWithNoMD5, opContextWithMD5Check, ar => waitHandle.Set(), null); waitHandle.WaitOne(); file.EndWriteRange(result); Assert.IsNull(lastCheckMD5); fileData.Seek(0, SeekOrigin.Begin); result = file.BeginWriteRange(fileData, 0, null, null, optionsWithMD5, opContextWithMD5Check, ar => waitHandle.Set(), null); waitHandle.WaitOne(); file.EndWriteRange(result); Assert.AreEqual(md5, lastCheckMD5); fileData.Seek(0, SeekOrigin.Begin); result = file.BeginWriteRange(fileData, 0, md5, null, optionsWithNoMD5, opContextWithMD5Check, ar => waitHandle.Set(), null); waitHandle.WaitOne(); file.EndWriteRange(result); Assert.AreEqual(md5, lastCheckMD5); } Assert.AreEqual(3, checkCount); } } finally { share.DeleteIfExists(); } }
public void FileDisableContentMD5ValidationTestAPM() { FileRequestOptions optionsWithNoMD5 = new FileRequestOptions() { DisableContentMD5Validation = true, StoreFileContentMD5 = true, }; FileRequestOptions optionsWithMD5 = new FileRequestOptions() { DisableContentMD5Validation = false, StoreFileContentMD5 = true, }; CloudFileShare share = GetRandomShareReference(); try { share.Create(); using (AutoResetEvent waitHandle = new AutoResetEvent(false)) { IAsyncResult result; CloudFile file = share.GetRootDirectoryReference().GetFileReference("file2"); using (Stream stream = new MemoryStream()) { file.UploadFromStream(stream, null, optionsWithMD5); } using (Stream stream = new MemoryStream()) { result = file.BeginDownloadToStream(stream, null, optionsWithMD5, null, ar => waitHandle.Set(), null); waitHandle.WaitOne(); file.EndDownloadToStream(result); result = file.BeginDownloadToStream(stream, null, optionsWithNoMD5, null, ar => waitHandle.Set(), null); waitHandle.WaitOne(); file.EndDownloadToStream(result); file.Properties.ContentMD5 = "MDAwMDAwMDA="; file.SetProperties(); result = file.BeginDownloadToStream(stream, null, optionsWithMD5, null, ar => waitHandle.Set(), null); waitHandle.WaitOne(); TestHelper.ExpectedException( () => file.EndDownloadToStream(result), "Downloading a file with invalid MD5 should fail", HttpStatusCode.OK); result = file.BeginDownloadToStream(stream, null, optionsWithNoMD5, null, ar => waitHandle.Set(), null); waitHandle.WaitOne(); file.EndDownloadToStream(result); } } } finally { share.DeleteIfExists(); } }
public void FileDisableContentMD5ValidationTest() { byte[] buffer = new byte[1024]; Random random = new Random(); random.NextBytes(buffer); FileRequestOptions optionsWithNoMD5 = new FileRequestOptions() { DisableContentMD5Validation = true, StoreFileContentMD5 = true, }; FileRequestOptions optionsWithMD5 = new FileRequestOptions() { DisableContentMD5Validation = false, StoreFileContentMD5 = true, }; CloudFileShare share = GetRandomShareReference(); try { share.Create(); CloudFile file = share.GetRootDirectoryReference().GetFileReference("file2"); using (Stream stream = new MemoryStream(buffer)) { file.UploadFromStream(stream, null, optionsWithMD5); } using (Stream stream = new MemoryStream()) { file.DownloadToStream(stream, null, optionsWithMD5); file.DownloadToStream(stream, null, optionsWithNoMD5); using (Stream fileStream = file.OpenRead(null, optionsWithMD5)) { int read; do { read = fileStream.Read(buffer, 0, buffer.Length); }while (read > 0); } using (Stream fileStream = file.OpenRead(null, optionsWithNoMD5)) { int read; do { read = fileStream.Read(buffer, 0, buffer.Length); }while (read > 0); } file.Properties.ContentMD5 = "MDAwMDAwMDA="; file.SetProperties(); TestHelper.ExpectedException( () => file.DownloadToStream(stream, null, optionsWithMD5), "Downloading a file with invalid MD5 should fail", HttpStatusCode.OK); file.DownloadToStream(stream, null, optionsWithNoMD5); using (Stream fileStream = file.OpenRead(null, optionsWithMD5)) { TestHelper.ExpectedException <IOException>( () => { int read; do { read = fileStream.Read(buffer, 0, buffer.Length); }while (read > 0); }, "Downloading a file with invalid MD5 should fail"); } using (Stream fileStream = file.OpenRead(null, optionsWithNoMD5)) { int read; do { read = fileStream.Read(buffer, 0, buffer.Length); }while (read > 0); } } } finally { share.DeleteIfExists(); } }
public void FileStoreContentMD5TestAPM() { FileRequestOptions optionsWithNoMD5 = new FileRequestOptions() { StoreFileContentMD5 = false, }; FileRequestOptions optionsWithMD5 = new FileRequestOptions() { StoreFileContentMD5 = true, }; CloudFileShare share = GetRandomShareReference(); try { share.Create(); using (AutoResetEvent waitHandle = new AutoResetEvent(false)) { IAsyncResult result; CloudFile file = share.GetRootDirectoryReference().GetFileReference("file4"); using (Stream stream = new MemoryStream()) { result = file.BeginUploadFromStream(stream, null, optionsWithMD5, null, ar => waitHandle.Set(), null); waitHandle.WaitOne(); file.EndUploadFromStream(result); } file.FetchAttributes(); Assert.IsNotNull(file.Properties.ContentMD5); file = share.GetRootDirectoryReference().GetFileReference("file5"); using (Stream stream = new MemoryStream()) { result = file.BeginUploadFromStream(stream, null, optionsWithNoMD5, null, ar => waitHandle.Set(), null); waitHandle.WaitOne(); file.EndUploadFromStream(result); } file.FetchAttributes(); Assert.IsNull(file.Properties.ContentMD5); file = share.GetRootDirectoryReference().GetFileReference("file6"); using (Stream stream = new MemoryStream()) { result = file.BeginUploadFromStream(stream, ar => waitHandle.Set(), null); waitHandle.WaitOne(); file.EndUploadFromStream(result); } file.FetchAttributes(); Assert.IsNull(file.Properties.ContentMD5); } } finally { share.DeleteIfExists(); } }
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(); } }