Bug 1436377 - Policy engine - Implement URLorEmpty parameter type. r=Mossop
authorFelipe Gomes <felipc@gmail.com>
Wed, 07 Feb 2018 20:37:55 -0200
changeset 402877 bbcf611ca8b93467a5fcee60be2589d5f6f9af88
parent 402876 8130dd2083937aeb982f35bde2816013351fad3a
child 402878 5ee280615e4527c80642164d2d092bbaa36bd215
push id33405
push usershindli@mozilla.com
push dateThu, 08 Feb 2018 10:04:47 +0000
treeherdermozilla-central@0ac953fcddf1 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersMossop
bugs1436377
milestone60.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1436377 - Policy engine - Implement URLorEmpty parameter type. r=Mossop MozReview-Commit-ID: 96GqykNb3Zr
browser/components/enterprisepolicies/PoliciesValidator.jsm
browser/components/enterprisepolicies/tests/browser/browser_policies_validate_and_parse_API.js
--- a/browser/components/enterprisepolicies/PoliciesValidator.jsm
+++ b/browser/components/enterprisepolicies/PoliciesValidator.jsm
@@ -38,16 +38,17 @@ function validateAndParseParamRecursive(
 
   log.debug(`checking @${param}@ for type ${properties.type}`);
   switch (properties.type) {
     case "boolean":
     case "number":
     case "integer":
     case "string":
     case "URL":
+    case "URLorEmpty":
     case "origin":
       return validateAndParseSimpleParam(param, properties.type);
 
     case "array":
       if (param === undefined) {
         // Accept a missing array as valid. Policies that use
         // arrays should still check before iterating through
         // the array, unless it is marked as "required" in the
@@ -138,20 +139,26 @@ function validateAndParseSimpleParam(par
           valid = true;
         }
       } catch (ex) {
         valid = false;
       }
       break;
 
     case "URL":
+    case "URLorEmpty":
       if (typeof(param) != "string") {
         break;
       }
 
+      if (type == "URLorEmpty" && param === "") {
+        valid = true;
+        break;
+      }
+
       try {
         parsedParam = Services.io.newURI(param);
         valid = true;
       } catch (ex) {
         valid = false;
       }
       break;
   }
--- a/browser/components/enterprisepolicies/tests/browser/browser_policies_validate_and_parse_API.js
+++ b/browser/components/enterprisepolicies/tests/browser/browser_policies_validate_and_parse_API.js
@@ -94,21 +94,49 @@ add_task(async function test_URL_values(
   let valid, parsed;
   [valid, parsed] = PoliciesValidator.validateAndParseParameters("https://www.example.com/foo#bar", schema);
   ok(valid, "URL is valid");
   ok(parsed instanceof Ci.nsIURI, "parsed is a nsIURI");
   is(parsed.prePath, "https://www.example.com", "prePath is correct");
   is(parsed.pathQueryRef, "/foo#bar", "pathQueryRef is correct");
 
   // Invalid values:
+  ok(!PoliciesValidator.validateAndParseParameters("", schema)[0], "Empty string is not accepted for URL");
   ok(!PoliciesValidator.validateAndParseParameters("www.example.com", schema)[0], "Scheme is required for URL");
   ok(!PoliciesValidator.validateAndParseParameters("https://:!$%", schema)[0], "Invalid URL");
   ok(!PoliciesValidator.validateAndParseParameters({}, schema)[0], "Invalid value");
 });
 
+add_task(async function test_URLorEmpty_values() {
+  let schema = {
+    type: "URLorEmpty"
+  };
+
+  let valid, parsed;
+  [valid, parsed] = PoliciesValidator.validateAndParseParameters("https://www.example.com/foo#bar", schema);
+  ok(valid, "URL is valid");
+  ok(parsed instanceof Ci.nsIURI, "parsed is a nsIURI");
+  is(parsed.prePath, "https://www.example.com", "prePath is correct");
+  is(parsed.pathQueryRef, "/foo#bar", "pathQueryRef is correct");
+
+  // Test that this type also accept empty strings
+  [valid, parsed] = PoliciesValidator.validateAndParseParameters("", schema);
+  ok(valid, "URLorEmpty is valid");
+  ok(!parsed, "parsed value is falsy");
+  is(typeof(parsed), "string", "parsed is a string");
+  is(parsed, "", "parsed is an empty string");
+
+  // Invalid values:
+  ok(!PoliciesValidator.validateAndParseParameters(" ", schema)[0], "Non-empty string is not accepted");
+  ok(!PoliciesValidator.validateAndParseParameters("www.example.com", schema)[0], "Scheme is required for URL");
+  ok(!PoliciesValidator.validateAndParseParameters("https://:!$%", schema)[0], "Invalid URL");
+  ok(!PoliciesValidator.validateAndParseParameters({}, schema)[0], "Invalid value");
+});
+
+
 add_task(async function test_origin_values() {
   // Origin is a URL that doesn't contain a path/query string (i.e., it's only scheme + host + port)
   let schema = {
     type: "origin"
   };
 
   let valid, parsed;
   [valid, parsed] = PoliciesValidator.validateAndParseParameters("https://www.example.com", schema);