public void FileReadStreamSeekTest() { byte[] buffer = GetRandomBuffer(3 * 1024 * 1024); CloudFileShare share = GetRandomShareReference(); try { share.Create(); CloudFile file = share.GetRootDirectoryReference().GetFileReference("file1"); file.StreamMinimumReadSizeInBytes = 2 * 1024 * 1024; using (MemoryStream wholeFile = new MemoryStream(buffer)) { file.UploadFromStream(wholeFile); } OperationContext opContext = new OperationContext(); using (Stream fileStream = file.OpenRead(null, null, opContext)) { int attempts = FileReadStreamSeekTest(fileStream, file.StreamMinimumReadSizeInBytes, buffer, false); TestHelper.AssertNAttempts(opContext, attempts); } opContext = new OperationContext(); using (Stream fileStream = file.OpenRead(null, null, opContext)) { int attempts = FileReadStreamSeekTest(fileStream, file.StreamMinimumReadSizeInBytes, buffer, true); TestHelper.AssertNAttempts(opContext, attempts); } } finally { share.DeleteIfExists(); } }
public void FileReadLockToETagTest() { 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 (Stream fileStream = file.OpenRead()) { 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); } using (Stream fileStream = file.OpenRead()) { 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(); * TestHelper.ExpectedException( * () => file.OpenRead(accessCondition), * "File read stream should fail if file is modified during read", * HttpStatusCode.PreconditionFailed); */ } finally { share.DeleteIfExists(); } }
public void FileReadStreamBasicTest() { byte[] buffer = GetRandomBuffer(5 * 1024 * 1024); CloudFileShare share = GetRandomShareReference(); try { share.Create(); CloudFile file = share.GetRootDirectoryReference().GetFileReference("file1"); using (MemoryStream wholeFile = new MemoryStream(buffer)) { file.UploadFromStream(wholeFile); } using (MemoryStream wholeFile = new MemoryStream(buffer)) { using (Stream fileStream = file.OpenRead()) { TestHelper.AssertStreamsAreEqual(wholeFile, fileStream); } } } finally { share.DeleteIfExists(); } }
public void FileWriteWhenOpenRead() { byte[] buffer = GetRandomBuffer(2 * 1024); CloudFileShare share = GetRandomShareReference(); try { share.Create(); CloudFile file = share.GetRootDirectoryReference().GetFileReference("file1"); using (MemoryStream srcStream = new MemoryStream(buffer)) { file.UploadFromStream(srcStream); Stream fileStream = file.OpenRead(); byte[] testBuffer = new byte[2048]; TestHelper.ExpectedException <NotSupportedException>(() => fileStream.Write(testBuffer, 0, 2048), "Try writing to a stream opened for read"); } } finally { share.DeleteIfExists(); } }
public void FileOpenWriteSeekReadTest() { byte[] buffer = GetRandomBuffer(2 * 1024); CloudFileShare share = GetRandomShareReference(); try { share.Create(); CloudFile file = share.GetRootDirectoryReference().GetFileReference("file1"); MemoryStream memoryStream = new MemoryStream(buffer); Stream fileStream = file.OpenWrite(2048); fileStream.Write(buffer, 0, 2048); Assert.AreEqual(fileStream.Position, 2048); fileStream.Seek(1024, 0); memoryStream.Seek(1024, 0); Assert.AreEqual(fileStream.Position, 1024); byte[] testBuffer = GetRandomBuffer(1024); memoryStream.Write(testBuffer, 0, 1024); fileStream.Write(testBuffer, 0, 1024); Assert.AreEqual(fileStream.Position, memoryStream.Position); fileStream.Close(); Stream dstStream = file.OpenRead(); TestHelper.AssertStreamsAreEqual(memoryStream, dstStream); } finally { share.DeleteIfExists(); } }
public void FileOpenReadTest() { byte[] buffer = GetRandomBuffer(2 * 1024); CloudFileShare share = GetRandomShareReference(); try { share.Create(); CloudFile file = share.GetRootDirectoryReference().GetFileReference("file1"); using (MemoryStream srcStream = new MemoryStream(buffer)) { file.UploadFromStream(srcStream); Stream dstStream = file.OpenRead(); TestHelper.AssertStreamsAreEqual(srcStream, dstStream); } } finally { share.DeleteIfExists(); } }
public void FileSeekTest() { byte[] buffer = GetRandomBuffer(2 * 1024); CloudFileShare share = GetRandomShareReference(); try { share.Create(); CloudFile file = share.GetRootDirectoryReference().GetFileReference("file1"); using (MemoryStream srcStream = new MemoryStream(buffer)) { file.UploadFromStream(srcStream); Stream fileStream = file.OpenRead(); fileStream.Seek(2048, 0); byte[] buff = new byte[100]; int numRead = fileStream.Read(buff, 0, 100); Assert.AreEqual(numRead, 0); } } finally { share.DeleteIfExists(); } }
public void FileUseTransactionalMD5GetTest() { 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(); 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()) { file.DownloadToStream(stream, null, optionsWithNoMD5, opContextWithMD5Check); Assert.IsNull(lastCheckMD5); StorageException storageEx = TestHelper.ExpectedException <StorageException>( () => file.DownloadToStream(stream, null, optionsWithMD5, opContextWithMD5Check), "File will not have MD5 set by default; with UseTransactional, download should fail"); file.DownloadRangeToStream(stream, buffer.Length, buffer.Length, null, optionsWithNoMD5, opContextWithMD5Check); Assert.IsNull(lastCheckMD5); file.DownloadRangeToStream(stream, buffer.Length, buffer.Length, null, optionsWithMD5, opContextWithMD5Check); Assert.AreEqual(md5, lastCheckMD5); file.DownloadRangeToStream(stream, 1024, 4 * 1024 * 1024 + 1, null, optionsWithNoMD5, opContextWithMD5Check); Assert.IsNull(lastCheckMD5); storageEx = TestHelper.ExpectedException <StorageException>( () => file.DownloadRangeToStream(stream, 1024, 4 * 1024 * 1024 + 1, null, optionsWithMD5, opContextWithMD5Check), "Downloading more than 4MB with transactional MD5 should not be supported"); Assert.IsInstanceOfType(storageEx.InnerException, typeof(ArgumentOutOfRangeException)); using (Stream fileStream = file.OpenRead(null, optionsWithMD5, opContextWithMD5Check)) { fileStream.CopyTo(stream); Assert.IsNotNull(lastCheckMD5); } using (Stream fileStream = file.OpenRead(null, optionsWithNoMD5, opContextWithMD5Check)) { fileStream.CopyTo(stream); Assert.IsNull(lastCheckMD5); } } Assert.AreEqual(9, checkCount); } 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(); } }