public async Task TestFileAttributesEncryptionAsync()
        {
            CloudFileShare share = GetRandomShareReference();

            try
            {
                await share.CreateIfNotExistsAsync();

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

                await file.FetchAttributesAsync();

                Assert.IsTrue(file.Properties.IsServerEncrypted);

                CloudFile testFile = directory.GetFileReference(file.Name);
                await testFile.DownloadTextAsync();

                Assert.IsTrue(testFile.Properties.IsServerEncrypted);
            }
            finally
            {
                share.DeleteIfExistsAsync().Wait();
            }
        }
示例#2
0
        public async Task CloudFileDirectoryFileCreateWithoutDirectoryAsync()
        {
            CloudFileClient    client        = GenerateCloudFileClient();
            string             name          = GetRandomShareName();
            CloudFileShare     share         = client.GetShareReference(name);
            CloudFileDirectory rootDirectory = share.GetRootDirectoryReference();

            try
            {
                await share.CreateAsync();

                CloudFileDirectory dir     = rootDirectory.GetDirectoryReference("Dir1");
                CloudFile          file    = dir.GetFileReference("file1");
                OperationContext   context = new OperationContext();
                await TestHelper.ExpectedExceptionAsync(
                    async() => await file.CreateAsync(0, null, null, context),
                    context,
                    "Creating a file when the directory has not been created should throw",
                    HttpStatusCode.NotFound,
                    "ParentNotFound");

                // File creation directly in the share should pass.
                CloudFile file2 = rootDirectory.GetFileReference("file2");
                await file2.CreateAsync(0);

                await dir.CreateAsync();

                await file.CreateAsync(0);
            }
            finally
            {
                share.DeleteAsync().Wait();
            }
        }
示例#3
0
        public async Task CloudFileDirectoryWithFilesDeleteAsync()
        {
            CloudFileClient client = GenerateCloudFileClient();
            string          name   = GetRandomShareName();
            CloudFileShare  share  = client.GetShareReference(name);

            try
            {
                await share.CreateAsync();

                if (await CloudFileDirectorySetupAsync(share))
                {
                    CloudFileDirectory dir1    = share.GetRootDirectoryReference().GetDirectoryReference("TopDir1/MidDir1/EndDir1");
                    CloudFile          file1   = dir1.GetFileReference("EndFile1");
                    OperationContext   context = new OperationContext();
                    await TestHelper.ExpectedExceptionAsync(
                        async() => await dir1.DeleteAsync(null, null, context),
                        context,
                        "Delete a non-empty directory",
                        HttpStatusCode.Conflict);

                    await file1.DeleteAsync();

                    await dir1.DeleteAsync();

                    Assert.IsFalse(await file1.ExistsAsync());
                    Assert.IsFalse(await dir1.ExistsAsync());
                }
            }
            finally
            {
                share.DeleteAsync().Wait();
            }
        }
示例#4
0
        public async Task CloudFileDirectorySASAsync()
        {
            CloudFileDirectory dir  = this.testShare.GetRootDirectoryReference().GetDirectoryReference("dirfile");
            CloudFile          file = dir.GetFileReference("dirfile");

            await dir.CreateAsync();

            await file.CreateAsync(512);

            SharedAccessFilePolicy policy = new SharedAccessFilePolicy()
            {
                SharedAccessStartTime  = DateTimeOffset.UtcNow.AddMinutes(-5),
                SharedAccessExpiryTime = DateTimeOffset.UtcNow.AddMinutes(30),
                Permissions            = SharedAccessFilePermissions.Read | SharedAccessFilePermissions.List
            };

            string             sasToken = file.GetSharedAccessSignature(policy);
            CloudFileDirectory sasDir   = new CloudFileDirectory(new Uri(dir.Uri.AbsoluteUri + sasToken));
            OperationContext   context  = new OperationContext();
            await TestHelper.ExpectedExceptionAsync(
                async() => await sasDir.FetchAttributesAsync(null /* accessCondition */, null /* options */, context),
                context,
                "Fetching attributes of a directory using a file SAS should fail",
                HttpStatusCode.Forbidden,
                "");

            sasToken = this.testShare.GetSharedAccessSignature(policy);
            sasDir   = new CloudFileDirectory(new Uri(dir.Uri.AbsoluteUri + sasToken));
            await sasDir.FetchAttributesAsync();
        }
示例#5
0
        public void CloudFileDirectorySAS()
        {
            CloudFileDirectory dir  = this.testShare.GetRootDirectoryReference().GetDirectoryReference("dirfile");
            CloudFile          file = dir.GetFileReference("dirfile");

            dir.Create();
            file.Create(512);

            SharedAccessFilePolicy policy = new SharedAccessFilePolicy()
            {
                SharedAccessStartTime  = DateTimeOffset.UtcNow.AddMinutes(-5),
                SharedAccessExpiryTime = DateTimeOffset.UtcNow.AddMinutes(30),
                Permissions            = SharedAccessFilePermissions.Read | SharedAccessFilePermissions.List
            };

            string             sasToken = file.GetSharedAccessSignature(policy);
            CloudFileDirectory sasDir   = new CloudFileDirectory(new Uri(dir.Uri.AbsoluteUri + sasToken));

            TestHelper.ExpectedException(
                () => sasDir.FetchAttributes(),
                "Fetching attributes of a directory using a file SAS should fail",
                HttpStatusCode.Forbidden);

            sasToken = this.testShare.GetSharedAccessSignature(policy);
            sasDir   = new CloudFileDirectory(new Uri(dir.Uri.AbsoluteUri + sasToken));
            sasDir.FetchAttributes();
        }
示例#6
0
        public async Task CloudFileClientMaximumExecutionTimeoutAsync()
        {
            CloudFileClient    fileClient    = GenerateCloudFileClient();
            CloudFileShare     share         = fileClient.GetShareReference(Guid.NewGuid().ToString("N"));
            CloudFileDirectory rootDirectory = share.GetRootDirectoryReference();

            byte[] buffer = FileTestBase.GetRandomBuffer(80 * 1024 * 1024);

            try
            {
                await share.CreateAsync();

                fileClient.DefaultRequestOptions.MaximumExecutionTime = TimeSpan.FromSeconds(5);

                CloudFile file = rootDirectory.GetFileReference("file");
                file.StreamWriteSizeInBytes = 1 * 1024 * 1024;
                using (MemoryStream ms = new MemoryStream(buffer))
                {
                    try
                    {
                        await file.UploadFromStreamAsync(ms);

                        Assert.Fail();
                    }
                    catch (AggregateException ex)
                    {
#if !FACADE_NETCORE
                        Assert.AreEqual("The client could not finish the operation within specified timeout.", RequestResult.TranslateFromExceptionMessage(ex.InnerException.Message).ExceptionInfo.Message);
#else
                        Assert.AreEqual("The client could not finish the operation within specified timeout.", RequestResult.TranslateFromExceptionMessage(ex.InnerException.Message).Exception.Message);
#endif
                    }
                    catch (TaskCanceledException)
                    {
                    }
                }
            }
            finally
            {
                fileClient.DefaultRequestOptions.MaximumExecutionTime = null;
                await share.DeleteAsync();
            }
        }
示例#7
0
        public void CloudFileClientObjects()
        {
            CloudFileClient fileClient = GenerateCloudFileClient();
            CloudFileShare  share      = fileClient.GetShareReference("share");

            Assert.AreEqual(fileClient, share.ServiceClient);
            CloudFileDirectory rootDirectory = share.GetRootDirectoryReference();

            Assert.AreEqual(fileClient, rootDirectory.ServiceClient);
            CloudFileDirectory directory = rootDirectory.GetDirectoryReference("directory");

            Assert.AreEqual(fileClient, directory.ServiceClient);
            CloudFile file = directory.GetFileReference("file");

            Assert.AreEqual(fileClient, file.ServiceClient);

            CloudFileShare share2 = GetRandomShareReference();

            Assert.AreNotEqual(fileClient, share2.ServiceClient);
        }
        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();
            }
        }
示例#9
0
        private void FileDirectoryEscapingTest(string directoryName, string fileName)
        {
            CloudFileClient service = GenerateCloudFileClient();
            CloudFileShare  share   = GetRandomShareReference();

            try
            {
                share.Create();
                string text = Guid.NewGuid().ToString();

                // Create from CloudFileShare.
                CloudFileDirectory directory = share.GetRootDirectoryReference().GetDirectoryReference(directoryName);
                directory.Create();
                CloudFile originalFile = directory.GetFileReference(fileName);
                originalFile.Create(0);

                // List directories from share.
                IListFileItem directoryFromShareListingFiles = share.GetRootDirectoryReference().ListFilesAndDirectories().First();
                Assert.AreEqual(directory.Uri, directoryFromShareListingFiles.Uri);

                // List files from directory.
                IListFileItem fileFromDirectoryListingFiles = directory.ListFilesAndDirectories().First();
                Assert.AreEqual(originalFile.Uri, fileFromDirectoryListingFiles.Uri);

                // Check Name
                Assert.AreEqual <string>(fileName, originalFile.Name);

                // Absolute URI access from CloudFile
                CloudFile fileInfo = new CloudFile(originalFile.Uri, service.Credentials);
                fileInfo.FetchAttributes();

                // Access from CloudFileDirectory
                CloudFileDirectory cloudFileDirectory         = share.GetRootDirectoryReference().GetDirectoryReference(directoryName);
                CloudFile          fileFromCloudFileDirectory = cloudFileDirectory.GetFileReference(fileName);
                Assert.AreEqual(fileInfo.Uri, fileFromCloudFileDirectory.Uri);
            }
            finally
            {
                share.Delete();
            }
        }
示例#10
0
        private async Task <bool> CloudFileDirectorySetupAsync(CloudFileShare share)
        {
            try
            {
                CloudFileDirectory rootDirectory = share.GetRootDirectoryReference();
                for (int i = 1; i < 3; i++)
                {
                    CloudFileDirectory topDirectory = rootDirectory.GetDirectoryReference("TopDir" + i);
                    await topDirectory.CreateAsync();

                    for (int j = 1; j < 3; j++)
                    {
                        CloudFileDirectory midDirectory = topDirectory.GetDirectoryReference("MidDir" + j);
                        await midDirectory.CreateAsync();

                        for (int k = 1; k < 3; k++)
                        {
                            CloudFileDirectory endDirectory = midDirectory.GetDirectoryReference("EndDir" + k);
                            await endDirectory.CreateAsync();

                            CloudFile file1 = endDirectory.GetFileReference("EndFile" + k);
                            await file1.CreateAsync(0);
                        }
                    }

                    CloudFile file2 = topDirectory.GetFileReference("File" + i);
                    await file2.CreateAsync(0);
                }

                return(true);
            }
            catch (Exception e)
            {
                throw e;
            }
        }
示例#11
0
        public async Task CloudFileClientMaximumExecutionTimeoutShouldNotBeHonoredForStreamsAsync()
        {
            CloudFileClient    fileClient    = GenerateCloudFileClient();
            CloudFileShare     share         = fileClient.GetShareReference(Guid.NewGuid().ToString("N"));
            CloudFileDirectory rootDirectory = share.GetRootDirectoryReference();

            byte[] buffer = FileTestBase.GetRandomBuffer(1024 * 1024);

            try
            {
                await share.CreateAsync();

                fileClient.DefaultRequestOptions.MaximumExecutionTime = TimeSpan.FromSeconds(30);
                CloudFile file = rootDirectory.GetFileReference("file");
                file.StreamMinimumReadSizeInBytes = 1024 * 1024;

                using (CloudFileStream fileStream = await file.OpenWriteAsync(8 * 1024 * 1024))
                {
                    Stream fos = fileStream;

                    DateTime start = DateTime.Now;
                    for (int i = 0; i < 7; i++)
                    {
                        await fos.WriteAsync(buffer, 0, buffer.Length);
                    }

                    // Sleep to ensure we are over the Max execution time when we do the last write
                    int msRemaining = (int)(fileClient.DefaultRequestOptions.MaximumExecutionTime.Value - (DateTime.Now - start)).TotalMilliseconds;

                    if (msRemaining > 0)
                    {
                        await Task.Delay(msRemaining);
                    }

                    await fos.WriteAsync(buffer, 0, buffer.Length);

                    await fileStream.CommitAsync();
                }

                using (Stream fileStream = await file.OpenReadAsync())
                {
                    Stream fis = fileStream;

                    DateTime start = DateTime.Now;
                    int      total = 0;
                    while (total < 7 * 1024 * 1024)
                    {
                        total += await fis.ReadAsync(buffer, 0, buffer.Length);
                    }

                    // Sleep to ensure we are over the Max execution time when we do the last read
                    int msRemaining = (int)(fileClient.DefaultRequestOptions.MaximumExecutionTime.Value - (DateTime.Now - start)).TotalMilliseconds;

                    if (msRemaining > 0)
                    {
                        await Task.Delay(msRemaining);
                    }

                    while (true)
                    {
                        int count = await fis.ReadAsync(buffer, 0, buffer.Length);

                        total += count;
                        if (count == 0)
                        {
                            break;
                        }
                    }
                }
            }
            finally
            {
                fileClient.DefaultRequestOptions.MaximumExecutionTime = null;
                await share.DeleteAsync();
            }
        }