Bug 1436377 - Policy engine - Implement URLorEmpty parameter type. r=Mossop
MozReview-Commit-ID: L6zm2VnRTTN
--- 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);