コード例 #1
0
        public async Task CloudFileDirectoryGetParentAsync()
        {
            CloudFileClient client = GenerateCloudFileClient();
            string          name   = GetRandomShareName();
            CloudFileShare  share  = client.GetShareReference(name);

            try
            {
                await share.CreateAsync();

                CloudFile file = share.GetRootDirectoryReference().GetDirectoryReference("Dir1").GetFileReference("File1");
                Assert.AreEqual("File1", file.Name);

                // get the file's parent
                CloudFileDirectory parent = file.Parent;
                Assert.AreEqual(parent.Name, "Dir1");

                // get share as parent
                CloudFileDirectory root = parent.Parent;
                Assert.AreEqual(root.Name, "");

                // make sure the parent of the share dir is null
                CloudFileDirectory empty = root.Parent;
                Assert.IsNull(empty);

                // from share, get directory reference to share
                root = share.GetRootDirectoryReference();
                Assert.AreEqual("", root.Name);
                Assert.AreEqual(share.Uri.AbsoluteUri, root.Uri.AbsoluteUri);

                // make sure the parent of the share dir is null
                empty = root.Parent;
                Assert.IsNull(empty);
            }
            finally
            {
                share.DeleteIfExistsAsync().Wait();
            }
        }
コード例 #2
0
        public async Task CloudFileClientListSharesSegmentedWithPrefixAsync()
        {
            string          name       = GetRandomShareName();
            List <string>   shareNames = new List <string>();
            CloudFileClient fileClient = GenerateCloudFileClient();

            for (int i = 0; i < 3; i++)
            {
                string shareName = name + i.ToString();
                shareNames.Add(shareName);
                await fileClient.GetShareReference(shareName).CreateAsync();
            }

            List <string>         listedShareNames = new List <string>();
            FileContinuationToken token            = null;

            do
            {
                ShareResultSegment resultSegment = await fileClient.ListSharesSegmentedAsync(name, ShareListingDetails.None, 1, token, null, null);

                token = resultSegment.ContinuationToken;

                int count = 0;
                foreach (CloudFileShare share in resultSegment.Results)
                {
                    count++;
                    listedShareNames.Add(share.Name);
                }
                Assert.IsTrue(count <= 1);
            }while (token != null);

            Assert.AreEqual(shareNames.Count, listedShareNames.Count);
            foreach (string shareName in listedShareNames)
            {
                Assert.IsTrue(shareNames.Remove(shareName));
                await fileClient.GetShareReference(shareName).DeleteAsync();
            }
        }
コード例 #3
0
 /// <summary>
 /// Initializes a new instance of the <see cref="CloudFileShare" /> class.
 /// </summary>
 /// <param name="shareName">The share name.</param>
 /// <param name="snapshotTime">A <see cref="DateTimeOffset"/> specifying the snapshot timestamp, if the share is a snapshot.</param>
 /// <param name="serviceClient">A client object that specifies the endpoint for the File service.</param>
 internal CloudFileShare(string shareName, DateTimeOffset?snapshotTime, CloudFileClient serviceClient)
     : this(new FileShareProperties(), new Dictionary <string, string>(StringComparer.OrdinalIgnoreCase), shareName, snapshotTime, serviceClient)
 {
 }
コード例 #4
0
 /// <summary>
 /// Initializes a new instance of the <see cref="CloudFileShare" /> class.
 /// </summary>
 /// <param name="properties">The properties.</param>
 /// <param name="metadata">The metadata.</param>
 /// <param name="shareName">The share name.</param>
 /// <param name="snapshotTime">A <see cref="DateTimeOffset"/> specifying the snapshot timestamp, if the share is a snapshot.</param>
 /// <param name="serviceClient">The client to be used.</param>
 internal CloudFileShare(FileShareProperties properties, IDictionary <string, string> metadata, string shareName, DateTimeOffset?snapshotTime, CloudFileClient serviceClient)
 {
     this.StorageUri    = NavigationHelper.AppendPathToUri(serviceClient.StorageUri, shareName);
     this.ServiceClient = serviceClient;
     this.Name          = shareName;
     this.Metadata      = metadata;
     this.Properties    = properties;
     this.SnapshotTime  = snapshotTime;
 }
コード例 #5
0
        internal static FileRequestOptions ApplyDefaults(FileRequestOptions options, CloudFileClient serviceClient, bool applyExpiry = true)
        {
            FileRequestOptions modifiedOptions = new FileRequestOptions(options);

            modifiedOptions.RetryPolicy =
                modifiedOptions.RetryPolicy
                ?? serviceClient.DefaultRequestOptions.RetryPolicy
                ?? BaseDefaultRequestOptions.RetryPolicy;

            modifiedOptions.LocationMode =
                modifiedOptions.LocationMode
                ?? serviceClient.DefaultRequestOptions.LocationMode
                ?? BaseDefaultRequestOptions.LocationMode;

#if !(WINDOWS_RT || NETCORE)
            modifiedOptions.RequireEncryption =
                modifiedOptions.RequireEncryption
                ?? serviceClient.DefaultRequestOptions.RequireEncryption
                ?? BaseDefaultRequestOptions.RequireEncryption;
#endif

            modifiedOptions.ServerTimeout =
                modifiedOptions.ServerTimeout
                ?? serviceClient.DefaultRequestOptions.ServerTimeout
                ?? BaseDefaultRequestOptions.ServerTimeout;

            modifiedOptions.MaximumExecutionTime =
                modifiedOptions.MaximumExecutionTime
                ?? serviceClient.DefaultRequestOptions.MaximumExecutionTime
                ?? BaseDefaultRequestOptions.MaximumExecutionTime;


            modifiedOptions.NetworkTimeout =
                modifiedOptions.NetworkTimeout
                ?? serviceClient.DefaultRequestOptions.NetworkTimeout
                ?? BaseDefaultRequestOptions.NetworkTimeout;

            modifiedOptions.ParallelOperationThreadCount =
                modifiedOptions.ParallelOperationThreadCount
                ?? serviceClient.DefaultRequestOptions.ParallelOperationThreadCount
                ?? BaseDefaultRequestOptions.ParallelOperationThreadCount;

            if (applyExpiry && !modifiedOptions.OperationExpiryTime.HasValue && modifiedOptions.MaximumExecutionTime.HasValue)
            {
                modifiedOptions.OperationExpiryTime = DateTime.Now + modifiedOptions.MaximumExecutionTime.Value;
            }

#if WINDOWS_PHONE && WINDOWS_DESKTOP
            modifiedOptions.ChecksumOptions.DisableContentMD5Validation = BaseDefaultRequestOptions.ChecksumOptions.DisableContentMD5Validation;
            modifiedOptions.ChecksumOptions.StoreContentMD5             = BaseDefaultRequestOptions.ChecksumOptions.StoreFileContentMD5;
            modifiedOptions.ChecksumOptions.UseTransactionalMD5         = BaseDefaultRequestOptions.ChecksumOptions.UseTransactionalMD5;

            modifiedOptions.ChecksumOptions.DisableContentCRC64Validation = BaseDefaultRequestOptions.ChecksumOptions.DisableContentCRC64Validation;
            modifiedOptions.ChecksumOptions.StoreContentCRC64             = BaseDefaultRequestOptions.ChecksumOptions.StoreFileContentCRC64;
            modifiedOptions.ChecksumOptions.UseTransactionalCRC64         = BaseDefaultRequestOptions.ChecksumOptions.UseTransactionalCRC64;
#else
            modifiedOptions.ChecksumOptions.DisableContentMD5Validation =
                modifiedOptions.ChecksumOptions.DisableContentMD5Validation
                ?? serviceClient.DefaultRequestOptions.ChecksumOptions.DisableContentMD5Validation
                ?? BaseDefaultRequestOptions.ChecksumOptions.DisableContentMD5Validation;

            modifiedOptions.ChecksumOptions.StoreContentMD5 =
                modifiedOptions.ChecksumOptions.StoreContentMD5
                ?? serviceClient.DefaultRequestOptions.ChecksumOptions.StoreContentMD5
                ?? BaseDefaultRequestOptions.ChecksumOptions.StoreContentMD5;

            modifiedOptions.ChecksumOptions.UseTransactionalMD5 =
                modifiedOptions.ChecksumOptions.UseTransactionalMD5
                ?? serviceClient.DefaultRequestOptions.ChecksumOptions.UseTransactionalMD5
                ?? BaseDefaultRequestOptions.ChecksumOptions.UseTransactionalMD5;

            modifiedOptions.ChecksumOptions.DisableContentCRC64Validation =
                modifiedOptions.ChecksumOptions.DisableContentCRC64Validation
                ?? serviceClient.DefaultRequestOptions.ChecksumOptions.DisableContentCRC64Validation
                ?? BaseDefaultRequestOptions.ChecksumOptions.DisableContentCRC64Validation;

            modifiedOptions.ChecksumOptions.StoreContentCRC64 =
                modifiedOptions.ChecksumOptions.StoreContentCRC64
                ?? serviceClient.DefaultRequestOptions.ChecksumOptions.StoreContentCRC64
                ?? BaseDefaultRequestOptions.ChecksumOptions.StoreContentCRC64;

            modifiedOptions.ChecksumOptions.UseTransactionalCRC64 =
                modifiedOptions.ChecksumOptions.UseTransactionalCRC64
                ?? serviceClient.DefaultRequestOptions.ChecksumOptions.UseTransactionalCRC64
                ?? BaseDefaultRequestOptions.ChecksumOptions.UseTransactionalCRC64;
#endif

            return(modifiedOptions);
        }
コード例 #6
0
        public async Task CloudFileListSharesWithSnapshotAsync()
        {
            CloudFileShare share = GetRandomShareReference();
            await share.CreateAsync();

            share.Metadata["key1"] = "value1";
            await share.SetMetadataAsync();

            CloudFileShare snapshot = await share.SnapshotAsync();

            share.Metadata["key2"] = "value2";
            await share.SetMetadataAsync();

            CloudFileClient       client       = GenerateCloudFileClient();
            List <CloudFileShare> listedShares = new List <CloudFileShare>();
            FileContinuationToken token        = null;

            do
            {
                ShareResultSegment resultSegment = await client.ListSharesSegmentedAsync(share.Name, ShareListingDetails.All, null, token, null, null);

                token = resultSegment.ContinuationToken;

                foreach (CloudFileShare listResultShare in resultSegment.Results)
                {
                    listedShares.Add(listResultShare);
                }
            }while (token != null);

            int  count         = 0;
            bool originalFound = false;
            bool snapshotFound = false;

            foreach (CloudFileShare listShareItem in listedShares)
            {
                if (listShareItem.Name.Equals(share.Name) && !listShareItem.IsSnapshot && !originalFound)
                {
                    count++;
                    originalFound = true;
                    Assert.AreEqual(2, listShareItem.Metadata.Count);
                    Assert.AreEqual("value2", listShareItem.Metadata["key2"]);
                    // Metadata keys should be case-insensitive
                    Assert.AreEqual("value2", listShareItem.Metadata["KEY2"]);
                    Assert.AreEqual("value1", listShareItem.Metadata["key1"]);
                    Assert.AreEqual(share.StorageUri, listShareItem.StorageUri);
                }
                else if (listShareItem.Name.Equals(share.Name) &&
                         listShareItem.IsSnapshot && !snapshotFound)
                {
                    count++;
                    snapshotFound = true;
                    Assert.AreEqual(1, listShareItem.Metadata.Count);
                    Assert.AreEqual("value1", listShareItem.Metadata["key1"]);
                    Assert.AreEqual(snapshot.StorageUri, listShareItem.StorageUri);
                }
            }

            Assert.AreEqual(2, count);

            await snapshot.DeleteAsync();

            await share.DeleteAsync();
        }
コード例 #7
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();
            }
        }
コード例 #8
0
        public void CloudFileTestCorsMaxHeaders()
        {
            CorsRule ruleManyHeaders = new CorsRule()
            {
                AllowedOrigins = new List <string>()
                {
                    "www.xyz.com"
                },
                AllowedMethods = CorsHttpMethods.Get,
                AllowedHeaders =
                    new List <string>()
                {
                    "x-ms-meta-target*",
                    "x-ms-meta-other*"
                },
                ExposedHeaders =
                    new List <string>()
                {
                    "x-ms-meta-data*",
                    "x-ms-meta-source*"
                }
            };

            // Add maximum number of non-prefixed headers
            for (int i = 0; i < 64; i++)
            {
                ruleManyHeaders.ExposedHeaders.Add("x-ms-meta-" + i);
                ruleManyHeaders.AllowedHeaders.Add("x-ms-meta-" + i);
            }

            CloudFileClient client = GenerateCloudFileClient();

            this.TestCorsRules(client, new List <CorsRule>()
            {
                ruleManyHeaders
            });

            // Test with too many Exposed Headers (65)
            ruleManyHeaders.ExposedHeaders.Add("x-ms-meta-toomany");

            TestHelper.ExpectedException(
                () => this.TestCorsRules(client, new List <CorsRule>()
            {
                ruleManyHeaders
            }),
                "A maximum of 64 literal exposed headers are allowed.",
                HttpStatusCode.BadRequest,
                "InvalidXmlNodeValue");

            ruleManyHeaders.ExposedHeaders.Remove("x-ms-meta-toomany");

            // Test with too many Allowed Headers (65)
            ruleManyHeaders.AllowedHeaders.Add("x-ms-meta-toomany");

            TestHelper.ExpectedException(
                () => this.TestCorsRules(client, new List <CorsRule>()
            {
                ruleManyHeaders
            }),
                "A maximum of 64 literal allowed headers are allowed.",
                HttpStatusCode.BadRequest,
                "InvalidXmlNodeValue");

            ruleManyHeaders.AllowedHeaders.Remove("x-ms-meta-toomany");

            // Test with too many Exposed Prefixed Headers (three)
            ruleManyHeaders.ExposedHeaders.Add("x-ms-meta-toomany*");

            TestHelper.ExpectedException(
                () => this.TestCorsRules(client, new List <CorsRule>()
            {
                ruleManyHeaders
            }),
                "A maximum of two prefixed exposed headers are allowed.",
                HttpStatusCode.BadRequest,
                "InvalidXmlNodeValue");

            ruleManyHeaders.ExposedHeaders.Remove("x-ms-meta-toomany*");

            // Test with too many Allowed Prefixed Headers (three)
            ruleManyHeaders.AllowedHeaders.Add("x-ms-meta-toomany*");

            TestHelper.ExpectedException(
                () => this.TestCorsRules(client, new List <CorsRule>()
            {
                ruleManyHeaders
            }),
                "A maximum of two prefixed allowed headers are allowed.",
                HttpStatusCode.BadRequest,
                "InvalidXmlNodeValue");

            ruleManyHeaders.AllowedHeaders.Remove("x-ms-meta-toomany*");
        }
コード例 #9
0
 public static void MyClassInitialize(TestContext testContext)
 {
     client          = GenerateCloudFileClient();
     startProperties = client.GetServiceProperties();
 }
コード例 #10
0
        public void CloudFileTestValidCorsRules()
        {
            CorsRule ruleMinRequired = new CorsRule()
            {
                AllowedOrigins = new List <string>()
                {
                    "www.xyz.com"
                },
                AllowedMethods = CorsHttpMethods.Get
            };

            CorsRule ruleBasic = new CorsRule()
            {
                AllowedOrigins = new List <string>()
                {
                    "www.ab.com", "www.bc.com"
                },
                AllowedMethods  = CorsHttpMethods.Get | CorsHttpMethods.Put,
                MaxAgeInSeconds = 500,
                ExposedHeaders  =
                    new List <string>()
                {
                    "x-ms-meta-data*",
                    "x-ms-meta-source*",
                    "x-ms-meta-abc",
                    "x-ms-meta-bcd"
                },
                AllowedHeaders =
                    new List <string>()
                {
                    "x-ms-meta-data*",
                    "x-ms-meta-target*",
                    "x-ms-meta-xyz",
                    "x-ms-meta-foo"
                }
            };

            CorsRule ruleAllMethods = new CorsRule()
            {
                AllowedOrigins = new List <string>()
                {
                    "www.xyz.com"
                },
                AllowedMethods =
                    CorsHttpMethods.Put | CorsHttpMethods.Trace
                    | CorsHttpMethods.Connect | CorsHttpMethods.Delete
                    | CorsHttpMethods.Get | CorsHttpMethods.Head
                    | CorsHttpMethods.Options | CorsHttpMethods.Post
                    | CorsHttpMethods.Merge
            };

            CorsRule ruleSingleExposedHeader = new CorsRule()
            {
                AllowedOrigins = new List <string>()
                {
                    "www.ab.com"
                },
                AllowedMethods = CorsHttpMethods.Get,
                ExposedHeaders = new List <string>()
                {
                    "x-ms-meta-bcd"
                },
            };

            CorsRule ruleSingleExposedPrefixHeader = new CorsRule()
            {
                AllowedOrigins =
                    new List <string>()
                {
                    "www.ab.com"
                },
                AllowedMethods = CorsHttpMethods.Get,
                ExposedHeaders =
                    new List <string>()
                {
                    "x-ms-meta-data*"
                },
            };

            CorsRule ruleSingleAllowedHeader = new CorsRule()
            {
                AllowedOrigins = new List <string>()
                {
                    "www.ab.com"
                },
                AllowedMethods = CorsHttpMethods.Get,
                AllowedHeaders = new List <string>()
                {
                    "x-ms-meta-xyz",
                },
            };

            CorsRule ruleSingleAllowedPrefixHeader = new CorsRule()
            {
                AllowedOrigins =
                    new List <string>()
                {
                    "www.ab.com"
                },
                AllowedMethods = CorsHttpMethods.Get,
                AllowedHeaders =
                    new List <string>()
                {
                    "x-ms-meta-target*"
                },
            };

            CorsRule ruleAllowAll = new CorsRule()
            {
                AllowedOrigins = new List <string>()
                {
                    "*"
                },
                AllowedMethods = CorsHttpMethods.Get,
                AllowedHeaders = new List <string>()
                {
                    "*"
                },
                ExposedHeaders = new List <string>()
                {
                    "*"
                }
            };

            CloudFileClient client = GenerateCloudFileClient();

            this.TestCorsRules(client, new List <CorsRule>()
            {
                ruleBasic
            });

            this.TestCorsRules(client, new List <CorsRule>()
            {
                ruleMinRequired
            });

            this.TestCorsRules(client, new List <CorsRule>()
            {
                ruleAllMethods
            });

            this.TestCorsRules(client, new List <CorsRule>()
            {
                ruleSingleExposedHeader
            });

            this.TestCorsRules(client, new List <CorsRule>()
            {
                ruleSingleExposedPrefixHeader
            });

            this.TestCorsRules(client, new List <CorsRule>()
            {
                ruleSingleAllowedHeader
            });

            this.TestCorsRules(client, new List <CorsRule>()
            {
                ruleSingleAllowedPrefixHeader
            });

            this.TestCorsRules(client, new List <CorsRule>()
            {
                ruleAllowAll
            });

            // Empty rule set should delete all rules
            this.TestCorsRules(client, new List <CorsRule>()
            {
            });

            // Test duplicate rules
            this.TestCorsRules(client, new List <CorsRule>()
            {
                ruleBasic, ruleBasic
            });

            // Test max number of  rules (five)
            this.TestCorsRules(
                client,
                new List <CorsRule>()
            {
                ruleBasic,
                ruleMinRequired,
                ruleAllMethods,
                ruleSingleExposedHeader,
                ruleSingleExposedPrefixHeader
            });


            // Test max number of rules + 1 (six)
            TestHelper.ExpectedException(
                () =>
                this.TestCorsRules(
                    client,
                    new List <CorsRule>()
            {
                ruleBasic,
                ruleMinRequired,
                ruleAllMethods,
                ruleSingleExposedHeader,
                ruleSingleExposedPrefixHeader,
                ruleSingleAllowedHeader
            }),
                "Services are limited to a maximum of five CORS rules.",
                HttpStatusCode.BadRequest,
                "InvalidXmlDocument");
        }