public async Task TestFileEncryptionAsync()
        {
            bool requestFound = false;

            OperationContext ctxt  = new OperationContext();
            CloudFileShare   share = GetRandomShareReference();

            try
            {
                await share.CreateIfNotExistsAsync();

                CloudFileDirectory directory = share.GetRootDirectoryReference();
                CloudFile          file      = directory.GetFileReference("file");

                await file.UploadTextAsync("test");

                ctxt.RequestCompleted += (sender, args) =>
                {
                    Assert.IsTrue(args.RequestInformation.IsRequestServerEncrypted);
                    requestFound = true;
                };

                await file.UploadTextAsync("test", null, null, ctxt);

                Assert.IsTrue(requestFound);

                requestFound = false;
                await file.SetPropertiesAsync(null, null, ctxt);

                Assert.IsTrue(requestFound);

                requestFound = false;
                await file.SetMetadataAsync(null, null, ctxt);

                Assert.IsTrue(requestFound);
            }
            finally
            {
                share.DeleteIfExistsAsync().Wait();
            }
        }
Exemplo n.º 2
0
        public async Task FileDisableContentMD5ValidationTestAsync()
        {
            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
            {
                await share.CreateAsync();

                CloudFile file = share.GetRootDirectoryReference().GetFileReference("file2");
                using (Stream stream = new MemoryStream(buffer))
                {
                    await file.UploadFromStreamAsync(stream, null, optionsWithMD5, null);
                }

                using (Stream stream = new MemoryStream())
                {
                    await file.DownloadToStreamAsync(stream, null, optionsWithMD5, null);

                    await file.DownloadToStreamAsync(stream, null, optionsWithNoMD5, null);

                    using (Stream fileStream = await file.OpenReadAsync(null, optionsWithMD5, null))
                    {
                        Stream fileStreamForRead = fileStream;
                        int    read;
                        do
                        {
                            read = await fileStreamForRead.ReadAsync(buffer, 0, buffer.Length);
                        }while (read > 0);
                    }

                    using (Stream fileStream = await file.OpenReadAsync(null, optionsWithNoMD5, null))
                    {
                        Stream fileStreamForRead = fileStream;
                        int    read;
                        do
                        {
                            read = await fileStreamForRead.ReadAsync(buffer, 0, buffer.Length);
                        }while (read > 0);
                    }

                    file.Properties.ContentMD5 = "MDAwMDAwMDA=";
                    await file.SetPropertiesAsync();

                    OperationContext opContext = new OperationContext();
                    await TestHelper.ExpectedExceptionAsync(
                        async() => await file.DownloadToStreamAsync(stream, null, optionsWithMD5, opContext),
                        opContext,
                        "Downloading a file with invalid MD5 should fail",
                        HttpStatusCode.OK);

                    await file.DownloadToStreamAsync(stream, null, optionsWithNoMD5, null);

                    using (Stream fileStream = await file.OpenReadAsync(null, optionsWithMD5, null))
                    {
                        Stream fileStreamForRead = fileStream;
                        TestHelper.ExpectedException <IOException>(
                            () =>
                        {
                            int read;
                            do
                            {
                                read = fileStreamForRead.Read(buffer, 0, buffer.Length);
                            }while (read > 0);
                        },
                            "Downloading a file with invalid MD5 should fail");
                    }

                    using (Stream fileStream = await file.OpenReadAsync(null, optionsWithNoMD5, null))
                    {
                        Stream fileStreamForRead = fileStream;
                        int    read;
                        do
                        {
                            read = await fileStreamForRead.ReadAsync(buffer, 0, buffer.Length);
                        }while (read > 0);
                    }
                }
            }
            finally
            {
                share.DeleteIfExistsAsync().Wait();
            }
        }