示例#1
0
        public void FileOpenWriteTest()
        {
            byte[]         buffer = GetRandomBuffer(2 * 1024);
            CloudFileShare share  = GetRandomShareReference();

            try
            {
                share.Create();

                CloudFile file = share.GetRootDirectoryReference().GetFileReference("file1");
                using (CloudFileStream fileStream = file.OpenWrite(2048))
                {
                    fileStream.Write(buffer, 0, 2048);
                    fileStream.Flush();

                    byte[]       testBuffer = new byte[2048];
                    MemoryStream dstStream  = new MemoryStream(testBuffer);
                    file.DownloadRangeToStream(dstStream, null, null);

                    MemoryStream memStream = new MemoryStream(buffer);
                    TestHelper.AssertStreamsAreEqual(memStream, dstStream);
                }
            }
            finally
            {
                share.DeleteIfExists();
            }
        }
示例#2
0
        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();
            }
        }