From 5be5d04274f7b1243c2efde7c878638c117340e1 Mon Sep 17 00:00:00 2001 From: Felipe Gomes Date: Wed, 7 Feb 2018 20:37:55 -0200 Subject: [PATCH] Bug 1436377 - Policy engine - Implement URLorEmpty parameter type. r=Mossop MozReview-Commit-ID: 96GqykNb3Zr --- .../enterprisepolicies/PoliciesValidator.jsm | 7 +++++ ...browser_policies_validate_and_parse_API.js | 28 +++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/browser/components/enterprisepolicies/PoliciesValidator.jsm b/browser/components/enterprisepolicies/PoliciesValidator.jsm index 46a7bb43acd6..dbcd4d80f615 100644 --- a/browser/components/enterprisepolicies/PoliciesValidator.jsm +++ b/browser/components/enterprisepolicies/PoliciesValidator.jsm @@ -43,6 +43,7 @@ function validateAndParseParamRecursive(param, properties) { case "integer": case "string": case "URL": + case "URLorEmpty": case "origin": return validateAndParseSimpleParam(param, properties.type); @@ -143,10 +144,16 @@ function validateAndParseSimpleParam(param, type) { 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; diff --git 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 index f28de7d2e5bf..a0dc6445e57b 100644 --- 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 @@ -99,11 +99,39 @@ add_task(async function test_URL_values() { 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 = {