示例#1
0
        public void FileUseTransactionalMD5PutTest()
        {
            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 (HttpRequestParsers.GetContentLength(args.Request) >= buffer.Length)
                {
                    lastCheckMD5 = HttpRequestParsers.GetContentMD5(args.Request);
                    checkCount++;
                }
            };

            CloudFileShare share = GetRandomShareReference();

            try
            {
                share.Create();

                CloudFile file = share.GetRootDirectoryReference().GetFileReference("file2");
                file.Create(buffer.Length);
                checkCount = 0;
                using (Stream fileData = new MemoryStream(buffer))
                {
                    file.WriteRange(fileData, 0, null, null, optionsWithNoMD5, opContextWithMD5Check);
                    Assert.IsNull(lastCheckMD5);

                    fileData.Seek(0, SeekOrigin.Begin);
                    file.WriteRange(fileData, 0, null, null, optionsWithMD5, opContextWithMD5Check);
                    Assert.AreEqual(md5, lastCheckMD5);

                    fileData.Seek(0, SeekOrigin.Begin);
                    file.WriteRange(fileData, 0, md5, null, optionsWithNoMD5, opContextWithMD5Check);
                    Assert.AreEqual(md5, lastCheckMD5);
                }
                Assert.AreEqual(3, checkCount);

                file       = share.GetRootDirectoryReference().GetFileReference("file5");
                checkCount = 0;
                using (Stream fileStream = file.OpenWrite(buffer.Length * 3, null, optionsWithMD5, opContextWithMD5Check))
                {
                    fileStream.Write(buffer, 0, buffer.Length);
                    fileStream.Write(buffer, 0, buffer.Length);
                }
                Assert.IsNotNull(lastCheckMD5);
                Assert.AreEqual(1, checkCount);

                file       = share.GetRootDirectoryReference().GetFileReference("file6");
                checkCount = 0;
                using (Stream fileStream = file.OpenWrite(buffer.Length * 3, null, optionsWithNoMD5, opContextWithMD5Check))
                {
                    fileStream.Write(buffer, 0, buffer.Length);
                    fileStream.Write(buffer, 0, buffer.Length);
                }
                Assert.IsNull(lastCheckMD5);
                Assert.AreEqual(1, checkCount);
            }
            finally
            {
                share.DeleteIfExists();
            }
        }