public void CORSRuleBoundaryTest() { // 64 allowed origins PSCorsRule[] corsRules = new PSCorsRule[1]; corsRules[0] = new PSCorsRule() { AllowedOrigins = CORSRuleUtil.GetRandomOrigins(64), AllowedMethods = CORSRuleUtil.GetRandomMethods(), AllowedHeaders = CORSRuleUtil.GetRandomHeaders(), ExposedHeaders = CORSRuleUtil.GetRandomHeaders(), MaxAgeInSeconds = random.Next(1, 1000) }; this.ValidateCORSRuleSetGet(corsRules); // Allowed origin "*" corsRules[0].AllowedOrigins = new string[] { "*" }; this.ValidateCORSRuleSetGet(corsRules); // Origin length 256 corsRules[0].AllowedOrigins = CORSRuleUtil.GetRandomOrigins(3, true); corsRules[0].AllowedOrigins[0] = Utility.GenNameString("", 256); this.ValidateCORSRuleSetGet(corsRules); corsRules[0].AllowedOrigins = CORSRuleUtil.GetRandomOrigins(); // Allowed header 64 defined headers corsRules[0].AllowedHeaders = CORSRuleUtil.GetRandomHeaders(64); this.ValidateCORSRuleSetGet(corsRules); // Allowed header 2 prefix headers corsRules[0].AllowedHeaders = CORSRuleUtil.GetRandomHeaders(null, 2); this.ValidateCORSRuleSetGet(corsRules); // Allowed header "*" corsRules[0].AllowedHeaders = new string[] { "*" }; this.ValidateCORSRuleSetGet(corsRules); // Allowed header 256 chars corsRules[0].AllowedHeaders = CORSRuleUtil.GetRandomHeaders(3, 0, true); corsRules[0].AllowedHeaders[0] = Utility.GenNameString("", 256); this.ValidateCORSRuleSetGet(corsRules); // Exposed header 64 defined headers corsRules[0].ExposedHeaders = CORSRuleUtil.GetRandomHeaders(64); this.ValidateCORSRuleSetGet(corsRules); // Exposed header 2 prefixed headers corsRules[0].ExposedHeaders = CORSRuleUtil.GetRandomHeaders(null, 2); this.ValidateCORSRuleSetGet(corsRules); // Exposed header "*" corsRules[0].ExposedHeaders = new string[] { "*" }; this.ValidateCORSRuleSetGet(corsRules); // Exposed header 256 chars corsRules[0].ExposedHeaders = CORSRuleUtil.GetRandomHeaders(3, 0, true); corsRules[0].ExposedHeaders[0] = Utility.GenNameString("", 256); this.ValidateCORSRuleSetGet(corsRules); }
public static PSCorsRule[] GetRandomValidCORSRules(int count) { PSCorsRule[] rule = new PSCorsRule[count]; for (int i = 0; i < count; ++i) { rule[i] = GetRandomValidCORSRule(); } return(rule); }
public static PSCorsRule GetRandomValidCORSRule() { PSCorsRule corsRule = new PSCorsRule(); corsRule.AllowedHeaders = GetRandomHeaders(); corsRule.ExposedHeaders = GetRandomHeaders(); corsRule.AllowedOrigins = GetRandomOrigins(); corsRule.AllowedMethods = GetRandomMethods(); corsRule.MaxAgeInSeconds = rd.Next(1, 1000); return(corsRule); }
public override void ExecuteCmdlet() { if (ServiceType != StorageServiceType.Table) { ServiceProperties currentServiceProperties = Channel.GetStorageServiceProperties(ServiceType, GetRequestOptions(ServiceType), OperationContext); WriteObject(PSCorsRule.ParseCorsRules(currentServiceProperties.Cors)); } else //Table use old XSCL { StorageTableManagement tableChannel = new StorageTableManagement(Channel.StorageContext); XTable.ServiceProperties currentServiceProperties = tableChannel.GetStorageTableServiceProperties(GetTableRequestOptions(), TableOperationContext); WriteObject(PSCorsRule.ParseCorsRules(currentServiceProperties.Cors)); } }
public void Set0CORSRuleTest() { PSCorsRule[] corsRules = new PSCorsRule[0]; Constants.ServiceType serviceType = GetRandomServiceType(); if (lang == Language.PowerShell) { Test.Assert(!CommandAgent.SetAzureStorageCORSRules(serviceType, corsRules), "Set 0 cors rule to {0} service should fail", serviceType); ExpectedContainErrorMessage("Cannot bind argument to parameter 'CorsRules' because it is an empty array."); } else { Test.Assert(CommandAgent.SetAzureStorageCORSRules(serviceType, corsRules), "Set 0 cors rule to {0} service should succeed", serviceType); } }
public void ValidateParseCorsRulesTest() { CorsProperties coresproperties = PrepareCoresproperties(); PSCorsRule[] pscores = PSCorsRule.ParseCorsRules(coresproperties); CompareCors(pscores, coresproperties); coresproperties = new CorsProperties(); pscores = PSCorsRule.ParseCorsRules(coresproperties); CompareCors(pscores, coresproperties); coresproperties = null; pscores = PSCorsRule.ParseCorsRules(coresproperties); CompareCors(pscores, coresproperties); }
public override void ExecuteCmdlet() { ServiceProperties currentServiceProperties = Channel.GetStorageServiceProperties(ServiceType, GetRequestOptions(ServiceType), OperationContext); List <PSCorsRule> ruleList = new List <PSCorsRule>(); foreach (var corsRule in currentServiceProperties.Cors.CorsRules) { PSCorsRule psCorsRule = new PSCorsRule(); psCorsRule.AllowedOrigins = this.ListToArray(corsRule.AllowedOrigins); psCorsRule.AllowedHeaders = this.ListToArray(corsRule.AllowedHeaders); psCorsRule.ExposedHeaders = this.ListToArray(corsRule.ExposedHeaders); psCorsRule.AllowedMethods = this.ConvertCorsHttpMethodToString(corsRule.AllowedMethods); psCorsRule.MaxAgeInSeconds = corsRule.MaxAgeInSeconds; ruleList.Add(psCorsRule); } WriteObject(ruleList.ToArray()); }
private PSCorsRule[] GetCORSRules() { if (lang == Language.PowerShell) { return(CommandAgent.Output[0][PowerShellAgent.BaseObject] as PSCorsRule[]); } else { List <PSCorsRule> rules = new List <PSCorsRule>(); for (int i = 0; i < CommandAgent.Output.Count; i++) { PSCorsRule rule = new PSCorsRule(); bool hasValue = false; JArray categories; string[] keys = { "AllowedMethods", "AllowedOrigins", "AllowedHeaders", "ExposedHeaders" }; foreach (string key in keys) { if (CommandAgent.Output[i].ContainsKey(key)) { categories = (JArray)CommandAgent.Output[i][key]; rule.GetType().GetProperty(key).SetValue(rule, categories.Select(c => (string)c).ToArray()); hasValue = true; } } string ageKey = "MaxAgeInSeconds"; if (CommandAgent.Output[i].ContainsKey(ageKey)) { rule.MaxAgeInSeconds = (int)(CommandAgent.Output[i][ageKey] as long?); hasValue = true; } if (hasValue) { rules.Add(rule); } } return(rules.ToArray()); } }
/// <summary> /// Comapare PSCorsRule and CorsProperties, to make sure they are same content /// </summary> static private void CompareCors(PSCorsRule[] psCorsRules, CorsProperties corsProperties) { if ((psCorsRules == null || psCorsRules.Length == 0) && (corsProperties == null || corsProperties.CorsRules == null || corsProperties.CorsRules.Count == 0)) { return; } Assert.AreEqual(psCorsRules.Length, corsProperties.CorsRules.Count); int i = 0; foreach (CorsRule CorsRule in corsProperties.CorsRules) { PSCorsRule psCorsRule = psCorsRules[i]; i++; CompareStrings(psCorsRule.AllowedHeaders, CorsRule.AllowedHeaders); CompareStrings(psCorsRule.ExposedHeaders, CorsRule.ExposedHeaders); CompareStrings(psCorsRule.AllowedOrigins, CorsRule.AllowedOrigins); Assert.AreEqual(psCorsRule.MaxAgeInSeconds, CorsRule.MaxAgeInSeconds); CorsHttpMethods psAllowedMethods = CorsHttpMethods.None; foreach (string method in psCorsRule.AllowedMethods) { CorsHttpMethods allowedCorsMethod = CorsHttpMethods.None; if (Enum.TryParse <CorsHttpMethods>(method, true, out allowedCorsMethod)) { psAllowedMethods |= allowedCorsMethod; } else { throw new InvalidOperationException(string.Format("Can't parse {0} to CorsHttpMethods.", method)); } } Assert.AreEqual(psAllowedMethods, CorsRule.AllowedMethods); } }
public void SetCORSRuleNegativeTest() { // No allowed origins PSCorsRule[] corsRules = new PSCorsRule[1]; corsRules[0] = new PSCorsRule() { AllowedOrigins = new string[0], AllowedMethods = CORSRuleUtil.GetRandomMethods(), AllowedHeaders = CORSRuleUtil.GetRandomHeaders(), ExposedHeaders = CORSRuleUtil.GetRandomHeaders(), MaxAgeInSeconds = random.Next(1, 1000) }; Constants.ServiceType serviceType = GetRandomServiceType(); Test.Assert(!CommandAgent.SetAzureStorageCORSRules(serviceType, corsRules), "Set cors rule without allowed origin to {0} service should fail", serviceType); if (lang == Language.PowerShell) { ExpectedContainErrorMessage(NoOriginNoMethod0MaxCacheAgeError); } else { ExpectedContainErrorMessage("Error"); } // No allowed methods corsRules[0].AllowedOrigins = CORSRuleUtil.GetRandomOrigins(); corsRules[0].AllowedMethods = new string[0]; serviceType = GetRandomServiceType(); Test.Assert(!CommandAgent.SetAzureStorageCORSRules(serviceType, corsRules), "Set cors rule without allowed method to {0} service should fail", serviceType); if (lang == Language.PowerShell) { ExpectedContainErrorMessage(NoOriginNoMethod0MaxCacheAgeError); } else { ExpectedContainErrorMessage(string.Format(InvalidMethodsError, string.Empty)); } // Max age in second is negative. corsRules[0].AllowedMethods = CORSRuleUtil.GetRandomMethods(); corsRules[0].MaxAgeInSeconds = -1; serviceType = GetRandomServiceType(); if (lang == Language.PowerShell) { Test.Assert(!CommandAgent.SetAzureStorageCORSRules(serviceType, corsRules), "Set cors rule to {0} service should fail when max age is negative.", serviceType); ExpectedContainErrorMessage(NoOriginNoMethod0MaxCacheAgeError); } else { Test.Assert(CommandAgent.SetAzureStorageCORSRules(serviceType, corsRules), "Set cors rule to {0} service should succeed when max age is negative.", serviceType); } // Length of one of allowed origins is greater than 256 corsRules[0].MaxAgeInSeconds = random.Next(1, 1000); corsRules[0].AllowedOrigins = CORSRuleUtil.GetRandomOrigins(); corsRules[0].AllowedOrigins[0] = Utility.GenNameString("origin", 251); serviceType = GetRandomServiceType(); Test.Assert(!CommandAgent.SetAzureStorageCORSRules(serviceType, corsRules), "Set cors rule to {0} service should fail, when allowed origin length is greater than 256.", serviceType); if (lang == Language.PowerShell) { ExpectedContainErrorMessage(CORSRuleInvalidError); } else { ExpectedContainErrorMessage(InvalidXMLNodeErrors); } //Count of allowed origin is more than 64. corsRules[0].AllowedOrigins = CORSRuleUtil.GetRandomOrigins(random.Next(65, 100)); serviceType = GetRandomServiceType(); Test.Assert(!CommandAgent.SetAzureStorageCORSRules(serviceType, corsRules), "Allowed origins count is greater than 64, set cors rule {0} service should fail", serviceType); if (lang == Language.PowerShell) { ExpectedContainErrorMessage(CORSRuleInvalidError); } else { ExpectedContainErrorMessage(InvalidXMLNodeErrors); } // Invalid method name string invalidMethodName = Utility.GenNameString(""); corsRules[0].AllowedOrigins = CORSRuleUtil.GetRandomOrigins(); corsRules[0].AllowedMethods = CORSRuleUtil.GetRandomMethods(); corsRules[0].AllowedMethods[0] = invalidMethodName; serviceType = GetRandomServiceType(); Test.Assert(!CommandAgent.SetAzureStorageCORSRules(serviceType, corsRules), "Inalid method name, set cors rule to {0} service should fail", serviceType); if (lang == Language.PowerShell) { ExpectedContainErrorMessage(string.Format("'{0}' is an invalid HTTP method", invalidMethodName)); } else { ExpectedContainErrorMessage(string.Format(InvalidMethodsError, invalidMethodName.ToUpper())); } // More than 2 prefixed allowed headers corsRules[0].AllowedMethods = CORSRuleUtil.GetRandomMethods(); corsRules[0].AllowedHeaders = CORSRuleUtil.GetRandomHeaders(null, random.Next(3, 10)); serviceType = GetRandomServiceType(); Test.Assert(!CommandAgent.SetAzureStorageCORSRules(serviceType, corsRules), "More than 2 prefixed allowed headers, set cors rule to {0} service should fail", serviceType); if (lang == Language.PowerShell) { ExpectedContainErrorMessage(CORSRuleInvalidError); } else { ExpectedContainErrorMessage(InvalidXMLNodeErrors); } // More than 64 defined allowed headers corsRules[0].AllowedHeaders = CORSRuleUtil.GetRandomHeaders(random.Next(65, 100)); serviceType = GetRandomServiceType(); Test.Assert(!CommandAgent.SetAzureStorageCORSRules(serviceType, corsRules), "More than 64 defined allowed headers, set cors rule to {0} service should fail", serviceType); if (lang == Language.PowerShell) { ExpectedContainErrorMessage(CORSRuleInvalidError); } else { ExpectedContainErrorMessage(InvalidXMLNodeErrors); } // Allowed header length greater than 256 corsRules[0].AllowedHeaders = CORSRuleUtil.GetRandomHeaders(); corsRules[0].AllowedHeaders[0] = Utility.GenNameString("header", 251); serviceType = GetRandomServiceType(); Test.Assert(!CommandAgent.SetAzureStorageCORSRules(serviceType, corsRules), "Allowed header length greater than 256, set cors rule to {0} service should fail", serviceType); if (lang == Language.PowerShell) { ExpectedContainErrorMessage(CORSRuleInvalidError); } else { ExpectedContainErrorMessage(InvalidXMLNodeErrors); } // More than 2 prefixed exposed headers corsRules[0].AllowedMethods = CORSRuleUtil.GetRandomMethods(); corsRules[0].ExposedHeaders = CORSRuleUtil.GetRandomHeaders(null, random.Next(3, 10)); serviceType = GetRandomServiceType(); Test.Assert(!CommandAgent.SetAzureStorageCORSRules(serviceType, corsRules), "More than 2 prefixed exposed headers, set cors rule to {0} service should fail", serviceType); if (lang == Language.PowerShell) { ExpectedContainErrorMessage(CORSRuleInvalidError); } else { ExpectedContainErrorMessage(InvalidXMLNodeErrors); } // More than 64 defined exposed headers corsRules[0].ExposedHeaders = CORSRuleUtil.GetRandomHeaders(random.Next(65, 100)); serviceType = GetRandomServiceType(); Test.Assert(!CommandAgent.SetAzureStorageCORSRules(serviceType, corsRules), "More than 64 defined exposed headers, set cors rule to {0} service should fail", serviceType); if (lang == Language.PowerShell) { ExpectedContainErrorMessage(CORSRuleInvalidError); } else { ExpectedContainErrorMessage(InvalidXMLNodeErrors); } // Exposed header length greater than 256 corsRules[0].ExposedHeaders = CORSRuleUtil.GetRandomHeaders(); corsRules[0].ExposedHeaders[0] = Utility.GenNameString("header", 251); serviceType = GetRandomServiceType(); Test.Assert(!CommandAgent.SetAzureStorageCORSRules(serviceType, corsRules), "Exposed header length greater than 256, set cors rule to {0} service should fail", serviceType); if (lang == Language.PowerShell) { ExpectedContainErrorMessage(CORSRuleInvalidError); } else { ExpectedContainErrorMessage(InvalidXMLNodeErrors); } // big total size corsRules[0].AllowedOrigins = CORSRuleUtil.GetRandomOrigins(null, true); corsRules[0].AllowedHeaders = CORSRuleUtil.GetRandomHeaders(null, null, true); corsRules[0].ExposedHeaders = CORSRuleUtil.GetRandomHeaders(null, null, true); serviceType = GetRandomServiceType(); Test.Assert(!CommandAgent.SetAzureStorageCORSRules(serviceType, corsRules), "Exposed header length greater than 256, set cors rule to {0} service should fail", serviceType); if (lang == Language.PowerShell) { ExpectedContainErrorMessage(CORSRuleInvalidError); } else { var errors = InvalidXMLNodeErrors.ToList(); errors.Add("The command line is too long"); ExpectedContainErrorMessage(errors.ToArray()); } // 6 CORS ruls corsRules = CORSRuleUtil.GetRandomValidCORSRules(6); serviceType = GetRandomServiceType(); Test.Assert(!CommandAgent.SetAzureStorageCORSRules(serviceType, corsRules), "6 CORS rules, set cors rule to {0} service should fail", serviceType); if (lang == Language.PowerShell) { ExpectedContainErrorMessage(CORSRuleInvalidError); } else { ExpectedContainErrorMessage("You can only specify up to 5 CORS rules per storage service"); } // Invalid Service Type corsRules = CORSRuleUtil.GetRandomValidCORSRules(random.Next(1, 5)); if (lang == Language.PowerShell) { Test.Assert(!CommandAgent.SetAzureStorageCORSRules(Constants.ServiceType.InvalidService, corsRules), "Set cors rules to invalid service type should fail."); ExpectedContainErrorMessage(string.Format("Unable to match the identifier name {0} to a valid enumerator name. Specify one of the following enumerator names and try again:", Constants.ServiceType.InvalidService.ToString())); } }
public override void ExecuteCmdlet() { ServiceProperties currentServiceProperties = Channel.GetStorageServiceProperties(ServiceType, GetRequestOptions(ServiceType), OperationContext); WriteObject(PSCorsRule.ParseCorsRules(currentServiceProperties.Cors)); }
protected PSCorsRule[] GetCORSRulesFromOutput() { if (lang == Language.PowerShell) { return(CommandAgent.Output[0][PowerShellAgent.BaseObject] as PSCorsRule[]); } else { List <PSCorsRule> rules = new List <PSCorsRule>(); for (int i = 0; i < CommandAgent.Output.Count; i++) { PSCorsRule rule = new PSCorsRule(); bool hasValue = false; JArray categories; string key = "AllowedMethods"; if (CommandAgent.Output[i].ContainsKey(key)) { categories = (JArray)CommandAgent.Output[i][key]; rule.AllowedMethods = categories.Select(c => (string)c).ToArray(); hasValue = true; } key = "AllowedOrigins"; if (CommandAgent.Output[i].ContainsKey(key)) { categories = (JArray)CommandAgent.Output[i][key]; rule.AllowedOrigins = categories.Select(c => (string)c).ToArray(); hasValue = true; } key = "AllowedHeaders"; if (CommandAgent.Output[i].ContainsKey(key)) { categories = (JArray)CommandAgent.Output[i][key]; rule.AllowedHeaders = categories.Select(c => (string)c).ToArray(); hasValue = true; } key = "ExposedHeaders"; if (CommandAgent.Output[i].ContainsKey(key)) { categories = (JArray)CommandAgent.Output[i][key]; rule.ExposedHeaders = categories.Select(c => (string)c).ToArray(); hasValue = true; } key = "MaxAgeInSeconds"; if (CommandAgent.Output[i].ContainsKey(key)) { rule.MaxAgeInSeconds = (int)(CommandAgent.Output[i][key] as long?); hasValue = true; } if (hasValue) { rules.Add(rule); } } return(rules.ToArray()); } }