Exemplo n.º 1
0
        public void PageBlobDownloadToStreamRangeTest()
        {
            byte[]             buffer    = GetRandomBuffer(2 * 1024);
            CloudBlobContainer container = GetRandomContainerReference();

            try
            {
                container.Create();

                CloudPageBlob blob = container.GetPageBlobReference("blob1");
                using (MemoryStream wholeBlob = new MemoryStream(buffer))
                {
                    blob.UploadFromStream(wholeBlob);

                    byte[]           testBuffer = new byte[1024];
                    MemoryStream     blobStream = new MemoryStream(testBuffer);
                    StorageException storageEx  = TestHelper.ExpectedException <StorageException>(
                        () => blob.DownloadRangeToStream(blobStream, 0, 0),
                        "Requesting 0 bytes when downloading range should not work");
                    Assert.IsInstanceOfType(storageEx.InnerException, typeof(ArgumentOutOfRangeException));
                    blob.DownloadRangeToStream(blobStream, 0, 1024);
                    Assert.AreEqual(blobStream.Position, 1024);
                    TestHelper.AssertStreamsAreEqualAtIndex(blobStream, wholeBlob, 0, 0, 1024);

                    CloudPageBlob blob2       = container.GetPageBlobReference("blob1");
                    MemoryStream  blobStream2 = new MemoryStream(testBuffer);
                    storageEx = TestHelper.ExpectedException <StorageException>(
                        () => blob2.DownloadRangeToStream(blobStream, 1024, 0),
                        "Requesting 0 bytes when downloading range should not work");
                    Assert.IsInstanceOfType(storageEx.InnerException, typeof(ArgumentOutOfRangeException));
                    blob2.DownloadRangeToStream(blobStream2, 1024, 1024);
                    TestHelper.AssertStreamsAreEqualAtIndex(blobStream2, wholeBlob, 0, 1024, 1024);

                    AssertAreEqual(blob, blob2);
                }
            }
            finally
            {
                container.DeleteIfExists();
            }
        }
Exemplo n.º 2
0
        public void BlobUploadWithoutMD5ValidationAndStoreBlobContentTest()
        {
            byte[]             buffer    = GetRandomBuffer(2 * 1024);
            CloudBlobContainer container = GetRandomContainerReference();

            try
            {
                container.Create();

                CloudPageBlob      blob    = container.GetPageBlobReference("blob1");
                BlobRequestOptions options = new BlobRequestOptions();
                options.DisableContentMD5Validation = false;
                options.StoreBlobContentMD5         = false;
                OperationContext context = new OperationContext();
                using (MemoryStream srcStream = new MemoryStream(buffer))
                {
                    blob.UploadFromStream(srcStream, null, options, context);
                    blob.FetchAttributes();
                    string md5 = blob.Properties.ContentMD5;
                    blob.Properties.ContentMD5 = "MDAwMDAwMDA=";
                    blob.SetProperties(null, options, context);
                    byte[]       testBuffer = new byte[2048];
                    MemoryStream dstStream  = new MemoryStream(testBuffer);
                    TestHelper.ExpectedException(() => blob.DownloadRangeToStream(dstStream, null, null, null, options, context),
                                                 "Try to Download a stream with a corrupted md5 and DisableMD5Validation set to false",
                                                 HttpStatusCode.OK);

                    options.DisableContentMD5Validation = true;
                    blob.SetProperties(null, options, context);
                    byte[]       testBuffer2 = new byte[2048];
                    MemoryStream dstStream2  = new MemoryStream(testBuffer2);
                    blob.DownloadRangeToStream(dstStream2, null, null, null, options, context);
                }
            }
            finally
            {
                container.DeleteIfExists();
            }
        }
Exemplo n.º 3
0
        public void BlobOpenWriteTest()
        {
            byte[]             buffer    = GetRandomBuffer(2 * 1024);
            CloudBlobContainer container = GetRandomContainerReference();

            try
            {
                container.Create();
                CloudPageBlob blob       = container.GetPageBlobReference("blob1");
                MemoryStream  memStream  = new MemoryStream(buffer);
                Stream        blobStream = blob.OpenWrite(2048);
                blobStream.Write(buffer, 0, 2048);

                byte[]       testBuffer = new byte[2048];
                MemoryStream dstStream  = new MemoryStream(testBuffer);
                blob.DownloadRangeToStream(dstStream, null, null);
                blobStream.Close();
                TestHelper.AssertStreamsAreEqual(memStream, dstStream);
            }
            finally
            {
                container.DeleteIfExists();
            }
        }
Exemplo n.º 4
0
        public void BlobUploadFromStreamTest()
        {
            byte[]             buffer    = GetRandomBuffer(2 * 1024);
            CloudBlobContainer container = GetRandomContainerReference();

            try
            {
                container.Create();

                CloudPageBlob blob = container.GetPageBlobReference("blob1");
                using (MemoryStream srcStream = new MemoryStream(buffer))
                {
                    blob.UploadFromStream(srcStream);
                    byte[]       testBuffer = new byte[2048];
                    MemoryStream dstStream  = new MemoryStream(testBuffer);
                    blob.DownloadRangeToStream(dstStream, null, null);
                    TestHelper.AssertStreamsAreEqual(srcStream, dstStream);
                }
            }
            finally
            {
                container.DeleteIfExists();
            }
        }
Exemplo n.º 5
0
        public void UseTransactionalMD5GetTest()
        {
            BlobRequestOptions optionsWithNoMD5 = new BlobRequestOptions()
            {
                UseTransactionalMD5 = false,
            };
            BlobRequestOptions optionsWithMD5 = new BlobRequestOptions()
            {
                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++;
                }
            };

            CloudBlobContainer container = GetRandomContainerReference();

            try
            {
                container.Create();

                CloudBlockBlob blockBlob = container.GetBlockBlobReference("blob1");
                using (Stream blobStream = blockBlob.OpenWrite())
                {
                    blobStream.Write(buffer, 0, buffer.Length);
                    blobStream.Write(buffer, 0, buffer.Length);
                }

                checkCount = 0;
                using (Stream stream = new MemoryStream())
                {
                    blockBlob.DownloadToStream(stream, null, optionsWithNoMD5, opContextWithMD5Check);
                    Assert.IsNotNull(lastCheckMD5);

                    blockBlob.DownloadToStream(stream, null, optionsWithMD5, opContextWithMD5Check);
                    Assert.IsNotNull(lastCheckMD5);

                    blockBlob.DownloadRangeToStream(stream, buffer.Length, buffer.Length, null, optionsWithNoMD5, opContextWithMD5Check);
                    Assert.IsNull(lastCheckMD5);

                    blockBlob.DownloadRangeToStream(stream, buffer.Length, buffer.Length, null, optionsWithMD5, opContextWithMD5Check);
                    Assert.AreEqual(md5, lastCheckMD5);

                    blockBlob.DownloadRangeToStream(stream, 1024, 4 * 1024 * 1024 + 1, null, optionsWithNoMD5, opContextWithMD5Check);
                    Assert.IsNull(lastCheckMD5);

                    StorageException storageEx = TestHelper.ExpectedException <StorageException>(
                        () => blockBlob.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));
                }
                Assert.AreEqual(5, checkCount);

                CloudPageBlob pageBlob = container.GetPageBlobReference("blob2");
                using (Stream blobStream = pageBlob.OpenWrite(buffer.Length * 2))
                {
                    blobStream.Write(buffer, 0, buffer.Length);
                    blobStream.Write(buffer, 0, buffer.Length);
                }

                checkCount = 0;
                using (Stream stream = new MemoryStream())
                {
                    pageBlob.DownloadToStream(stream, null, optionsWithNoMD5, opContextWithMD5Check);
                    Assert.IsNull(lastCheckMD5);

                    StorageException storageEx = TestHelper.ExpectedException <StorageException>(
                        () => pageBlob.DownloadToStream(stream, null, optionsWithMD5, opContextWithMD5Check),
                        "Page blob will not have MD5 set by default; with UseTransactional, download should fail");

                    pageBlob.DownloadRangeToStream(stream, buffer.Length, buffer.Length, null, optionsWithNoMD5, opContextWithMD5Check);
                    Assert.IsNull(lastCheckMD5);

                    pageBlob.DownloadRangeToStream(stream, buffer.Length, buffer.Length, null, optionsWithMD5, opContextWithMD5Check);
                    Assert.AreEqual(md5, lastCheckMD5);

                    pageBlob.DownloadRangeToStream(stream, 1024, 4 * 1024 * 1024 + 1, null, optionsWithNoMD5, opContextWithMD5Check);
                    Assert.IsNull(lastCheckMD5);

                    storageEx = TestHelper.ExpectedException <StorageException>(
                        () => pageBlob.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));
                }
                Assert.AreEqual(5, checkCount);
            }
            finally
            {
                container.DeleteIfExists();
            }
        }