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 void ChangeCORSRules() { Constants.ServiceType serviceType = GetRandomServiceType(); try { PSCorsRule[] corsRules = CORSRuleUtil.GetRandomValidCORSRules(random.Next(1, 5)); Test.Assert(CommandAgent.SetAzureStorageCORSRules(serviceType, corsRules), "Set cors rule to {0} service should succeed", serviceType); Test.Assert(CommandAgent.GetAzureStorageCORSRules(serviceType), "Get CORS rule of {0} service should succeed.", serviceType); PSCorsRule[] newCorsRules = GetCORSRules(); CORSRuleUtil.ValidateCORSRules(corsRules, newCorsRules); foreach (var corsRule in newCorsRules) { switch (random.Next(0, 5)) { case 0: corsRule.AllowedHeaders = CORSRuleUtil.GetRandomHeaders(); break; case 1: corsRule.AllowedMethods = CORSRuleUtil.GetRandomMethods(); break; case 2: corsRule.AllowedOrigins = CORSRuleUtil.GetRandomOrigins(); break; case 3: corsRule.ExposedHeaders = CORSRuleUtil.GetRandomHeaders(); break; case 4: corsRule.MaxAgeInSeconds = random.Next(1, 1000); break; } } Test.Assert(CommandAgent.SetAzureStorageCORSRules(serviceType, newCorsRules), "Set cors rule to {0} service should succeed", serviceType); Test.Assert(CommandAgent.GetAzureStorageCORSRules(serviceType), "Set cors rule of {0} service should succeed", serviceType); PSCorsRule[] actualCORSRules = GetCORSRules(); CORSRuleUtil.ValidateCORSRules(newCorsRules, actualCORSRules); } finally { ClearCorsRules(serviceType); } }
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())); } }