Bug 1436377 - Policy engine - Implement URLorEmpty parameter type. r=Mossop draft
authorFelipe Gomes <felipc@gmail.com>
Wed, 07 Feb 2018 13:44:41 -0200
changeset 752122 6771b3798c80b0969969607ce5742d2c1c725398
parent 751924 22b57b0611dee660c05de8ab88fa0d44b11d017a
child 752136 b65c4b9b47dd28fcaf543e7fd57c1e9ffbbcc7ae
push id98170
push userfelipc@gmail.com
push dateWed, 07 Feb 2018 15:45:00 +0000
reviewersMossop
bugs1436377
milestone60.0a1
Bug 1436377 - Policy engine - Implement URLorEmpty parameter type. r=Mossop MozReview-Commit-ID: L6zm2VnRTTN
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
@@ -43,16 +43,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
@@ -134,20 +135,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
@@ -87,21 +87,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);