diff options
Diffstat (limited to 'toolkit/components/enterprisepolicies/tests/browser')
8 files changed, 485 insertions, 0 deletions
diff --git a/toolkit/components/enterprisepolicies/tests/browser/browser.ini b/toolkit/components/enterprisepolicies/tests/browser/browser.ini new file mode 100644 index 0000000000..e6797dc8b7 --- /dev/null +++ b/toolkit/components/enterprisepolicies/tests/browser/browser.ini @@ -0,0 +1,11 @@ +[DEFAULT] +head = head.js +support-files = + config_broken_json.json + +[browser_policies_basic_tests.js] +[browser_policies_broken_json.js] +[browser_policies_enterprise_only.js] +[browser_policies_gpo.js] +skip-if = os != "win" +[browser_policies_mistyped_json.js] diff --git a/toolkit/components/enterprisepolicies/tests/browser/browser_policies_basic_tests.js b/toolkit/components/enterprisepolicies/tests/browser/browser_policies_basic_tests.js new file mode 100644 index 0000000000..8efcdee316 --- /dev/null +++ b/toolkit/components/enterprisepolicies/tests/browser/browser_policies_basic_tests.js @@ -0,0 +1,140 @@ +/* Any copyright is dedicated to the Public Domain. + * http://creativecommons.org/publicdomain/zero/1.0/ */ + +"use strict"; + +add_task(async function test_simple_policies() { + let { Policies } = ChromeUtils.importESModule( + "resource:///modules/policies/Policies.sys.mjs" + ); + + let policy0Ran = false, + policy1Ran = false, + policy2Ran = false, + policy3Ran = false; + + // Implement functions to handle the four simple policies that will be added + // to the schema. + Policies.simple_policy0 = { + onProfileAfterChange(manager, param) { + is(param, true, "Param matches what was passed in config file"); + policy0Ran = true; + }, + }; + + Policies.simple_policy1 = { + onProfileAfterChange(manager, param) { + is(param, true, "Param matches what was passed in config file"); + manager.disallowFeature("feature1", /* needed in content process */ true); + policy1Ran = true; + }, + }; + + Policies.simple_policy2 = { + onBeforeUIStartup(manager, param) { + is(param, true, "Param matches what was passed in config file"); + manager.disallowFeature( + "feature2", + /* needed in content process */ false + ); + policy2Ran = true; + }, + }; + + Policies.simple_policy3 = { + onAllWindowsRestored(manager, param) { + is(param, false, "Param matches what was passed in config file"); + policy3Ran = true; + }, + }; + + await setupPolicyEngineWithJson( + // policies.json + { + policies: { + simple_policy0: true, + simple_policy1: true, + simple_policy2: true, + simple_policy3: false, + }, + }, + + // custom schema + { + properties: { + simple_policy0: { + type: "boolean", + }, + + simple_policy1: { + type: "boolean", + }, + + simple_policy2: { + type: "boolean", + }, + + simple_policy3: { + type: "boolean", + }, + }, + } + ); + + is( + Services.policies.status, + Ci.nsIEnterprisePolicies.ACTIVE, + "Engine is active" + ); + is( + Services.policies.isAllowed("feature1"), + false, + "Dummy feature was disallowed" + ); + is( + Services.policies.isAllowed("feature2"), + false, + "Dummy feature was disallowed" + ); + + ok(policy0Ran, "Policy 0 ran correctly through BeforeAddons"); + ok(policy1Ran, "Policy 1 ran correctly through onProfileAfterChange"); + ok(policy2Ran, "Policy 2 ran correctly through onBeforeUIStartup"); + ok(policy3Ran, "Policy 3 ran correctly through onAllWindowsRestored"); + + await SpecialPowers.spawn(gBrowser.selectedBrowser, [], async function () { + if (Services.appinfo.processType == Services.appinfo.PROCESS_TYPE_CONTENT) { + is( + Services.policies.isAllowed("feature1"), + false, + "Correctly disallowed in the content process" + ); + // Feature 2 wasn't explictly marked as needed in the content process, so it is not marked + // as disallowed there. + is( + Services.policies.isAllowed("feature2"), + true, + "Correctly missing in the content process" + ); + } + }); + + delete Policies.simple_policy0; + delete Policies.simple_policy1; + delete Policies.simple_policy2; + delete Policies.simple_policy3; +}); + +add_task(async function test_policy_cleanup() { + await EnterprisePolicyTesting.setupPolicyEngineWithJson(""); + is( + Services.policies.getActivePolicies(), + undefined, + "No policies should be defined" + ); + is( + Services.policies.status, + Ci.nsIEnterprisePolicies.INACTIVE, + "Engine is inactive at the end of the test" + ); +}); diff --git a/toolkit/components/enterprisepolicies/tests/browser/browser_policies_broken_json.js b/toolkit/components/enterprisepolicies/tests/browser/browser_policies_broken_json.js new file mode 100644 index 0000000000..a4a274ab08 --- /dev/null +++ b/toolkit/components/enterprisepolicies/tests/browser/browser_policies_broken_json.js @@ -0,0 +1,14 @@ +/* Any copyright is dedicated to the Public Domain. + * http://creativecommons.org/publicdomain/zero/1.0/ */ + +"use strict"; + +add_task(async function test_broken_json() { + await setupPolicyEngineWithJson("config_broken_json.json"); + + is( + Services.policies.status, + Ci.nsIEnterprisePolicies.FAILED, + "Engine was correctly set to the error state" + ); +}); diff --git a/toolkit/components/enterprisepolicies/tests/browser/browser_policies_enterprise_only.js b/toolkit/components/enterprisepolicies/tests/browser/browser_policies_enterprise_only.js new file mode 100644 index 0000000000..3da3d250c6 --- /dev/null +++ b/toolkit/components/enterprisepolicies/tests/browser/browser_policies_enterprise_only.js @@ -0,0 +1,70 @@ +/* Any copyright is dedicated to the Public Domain. + * http://creativecommons.org/publicdomain/zero/1.0/ */ + +"use strict"; + +const PREF_DISALLOW_ENTERPRISE = "browser.policies.testing.disallowEnterprise"; + +add_task(async function test_enterprise_only_policies() { + let { Policies } = ChromeUtils.importESModule( + "resource:///modules/policies/Policies.sys.mjs" + ); + + let normalPolicyRan = false, + enterprisePolicyRan = false; + + Policies.NormalPolicy = { + onProfileAfterChange(manager, param) { + normalPolicyRan = true; + }, + }; + + Policies.EnterpriseOnlyPolicy = { + onProfileAfterChange(manager, param) { + enterprisePolicyRan = true; + }, + }; + + Services.prefs.setBoolPref(PREF_DISALLOW_ENTERPRISE, true); + + await setupPolicyEngineWithJson( + // policies.json + { + policies: { + NormalPolicy: true, + EnterpriseOnlyPolicy: true, + }, + }, + + // custom schema + { + properties: { + NormalPolicy: { + type: "boolean", + }, + + EnterpriseOnlyPolicy: { + type: "boolean", + enterprise_only: true, + }, + }, + } + ); + + is( + Services.policies.status, + Ci.nsIEnterprisePolicies.ACTIVE, + "Engine is active" + ); + is(normalPolicyRan, true, "Normal policy ran as expected"); + is( + enterprisePolicyRan, + false, + "Enterprise-only policy was prevented from running" + ); + + // Clean-up + delete Policies.NormalPolicy; + delete Policies.EnterpriseOnlyPolicy; + Services.prefs.clearUserPref(PREF_DISALLOW_ENTERPRISE); +}); diff --git a/toolkit/components/enterprisepolicies/tests/browser/browser_policies_gpo.js b/toolkit/components/enterprisepolicies/tests/browser/browser_policies_gpo.js new file mode 100644 index 0000000000..decf158d45 --- /dev/null +++ b/toolkit/components/enterprisepolicies/tests/browser/browser_policies_gpo.js @@ -0,0 +1,206 @@ +/* Any copyright is dedicated to the Public Domain. + * http://creativecommons.org/publicdomain/zero/1.0/ */ + +"use strict"; + +add_task(async function setup_preferences() { + await SpecialPowers.pushPrefEnv({ + set: [ + ["browser.policies.alternateGPO", "SOFTWARE\\Mozilla\\PolicyTesting"], + ], + }); +}); + +add_task(async function test_gpo_policies() { + let { Policies } = ChromeUtils.importESModule( + "resource:///modules/policies/Policies.sys.mjs" + ); + + let gpoPolicyRan = false; + + Policies.gpo_policy = { + onProfileAfterChange(manager, param) { + is(param, true, "Param matches what was in the registry"); + gpoPolicyRan = true; + }, + }; + + let wrk = Cc["@mozilla.org/windows-registry-key;1"].createInstance( + Ci.nsIWindowsRegKey + ); + let regLocation = + "SOFTWARE\\Mozilla\\PolicyTesting\\Mozilla\\" + Services.appinfo.name; + wrk.create(wrk.ROOT_KEY_CURRENT_USER, regLocation, wrk.ACCESS_WRITE); + wrk.writeIntValue("gpo_policy", 1); + wrk.close(); + + await setupPolicyEngineWithJson( + // empty policies.json since we are using GPO + { + policies: {}, + }, + + // custom schema + { + properties: { + gpo_policy: { + type: "boolean", + }, + }, + } + ); + + is( + Services.policies.status, + Ci.nsIEnterprisePolicies.ACTIVE, + "Engine is active" + ); + + ok(gpoPolicyRan, "GPO Policy ran correctly though onProfileAfterChange"); + + delete Policies.gpo_policy; + + wrk.open(wrk.ROOT_KEY_CURRENT_USER, "SOFTWARE\\Mozilla", wrk.ACCESS_WRITE); + wrk.removeChild("PolicyTesting\\Mozilla\\" + Services.appinfo.name); + wrk.removeChild("PolicyTesting\\Mozilla"); + wrk.removeChild("PolicyTesting"); + wrk.close(); +}); + +add_task(async function test_gpo_json_policies() { + let { Policies } = ChromeUtils.importESModule( + "resource:///modules/policies/Policies.sys.mjs" + ); + + let gpoPolicyRan = false; + let jsonPolicyRan = false; + let coexistPolicyRan = false; + + Policies.gpo_policy = { + onProfileAfterChange(manager, param) { + is(param, true, "Param matches what was in the registry"); + gpoPolicyRan = true; + }, + }; + Policies.json_policy = { + onProfileAfterChange(manager, param) { + is(param, true, "Param matches what was in the JSON"); + jsonPolicyRan = true; + }, + }; + Policies.coexist_policy = { + onProfileAfterChange(manager, param) { + is(param, false, "Param matches what was in the registry (over JSON)"); + coexistPolicyRan = true; + }, + }; + + let wrk = Cc["@mozilla.org/windows-registry-key;1"].createInstance( + Ci.nsIWindowsRegKey + ); + let regLocation = + "SOFTWARE\\Mozilla\\PolicyTesting\\Mozilla\\" + Services.appinfo.name; + wrk.create(wrk.ROOT_KEY_CURRENT_USER, regLocation, wrk.ACCESS_WRITE); + wrk.writeIntValue("gpo_policy", 1); + wrk.writeIntValue("coexist_policy", 0); + wrk.close(); + + await setupPolicyEngineWithJson( + { + policies: { + json_policy: true, + coexist_policy: true, + }, + }, + + // custom schema + { + properties: { + gpo_policy: { + type: "boolean", + }, + json_policy: { + type: "boolean", + }, + coexist_policy: { + type: "boolean", + }, + }, + } + ); + + is( + Services.policies.status, + Ci.nsIEnterprisePolicies.ACTIVE, + "Engine is active" + ); + + ok(gpoPolicyRan, "GPO Policy ran correctly though onProfileAfterChange"); + ok(jsonPolicyRan, "JSON Policy ran correctly though onProfileAfterChange"); + ok( + coexistPolicyRan, + "Coexist Policy ran correctly though onProfileAfterChange" + ); + + delete Policies.gpo_policy; + delete Policies.json_policy; + delete Policies.coexist_policy; + + wrk.open(wrk.ROOT_KEY_CURRENT_USER, "SOFTWARE\\Mozilla", wrk.ACCESS_WRITE); + wrk.removeChild("PolicyTesting\\Mozilla\\" + Services.appinfo.name); + wrk.removeChild("PolicyTesting\\Mozilla"); + wrk.removeChild("PolicyTesting"); + wrk.close(); +}); + +add_task(async function test_gpo_broken_json_policies() { + let { Policies } = ChromeUtils.importESModule( + "resource:///modules/policies/Policies.sys.mjs" + ); + + let gpoPolicyRan = false; + + Policies.gpo_policy = { + onProfileAfterChange(manager, param) { + is(param, true, "Param matches what was in the registry"); + gpoPolicyRan = true; + }, + }; + + let wrk = Cc["@mozilla.org/windows-registry-key;1"].createInstance( + Ci.nsIWindowsRegKey + ); + let regLocation = + "SOFTWARE\\Mozilla\\PolicyTesting\\Mozilla\\" + Services.appinfo.name; + wrk.create(wrk.ROOT_KEY_CURRENT_USER, regLocation, wrk.ACCESS_WRITE); + wrk.writeIntValue("gpo_policy", 1); + wrk.close(); + + await setupPolicyEngineWithJson( + "config_broken_json.json", + // custom schema + { + properties: { + gpo_policy: { + type: "boolean", + }, + }, + } + ); + + is( + Services.policies.status, + Ci.nsIEnterprisePolicies.ACTIVE, + "Engine is active" + ); + + ok(gpoPolicyRan, "GPO Policy ran correctly though onProfileAfterChange"); + + delete Policies.gpo_policy; + + wrk.open(wrk.ROOT_KEY_CURRENT_USER, "SOFTWARE\\Mozilla", wrk.ACCESS_WRITE); + wrk.removeChild("PolicyTesting\\Mozilla\\" + Services.appinfo.name); + wrk.removeChild("PolicyTesting\\Mozilla"); + wrk.removeChild("PolicyTesting"); + wrk.close(); +}); diff --git a/toolkit/components/enterprisepolicies/tests/browser/browser_policies_mistyped_json.js b/toolkit/components/enterprisepolicies/tests/browser/browser_policies_mistyped_json.js new file mode 100644 index 0000000000..0b82a11377 --- /dev/null +++ b/toolkit/components/enterprisepolicies/tests/browser/browser_policies_mistyped_json.js @@ -0,0 +1,17 @@ +/* Any copyright is dedicated to the Public Domain. + * http://creativecommons.org/publicdomain/zero/1.0/ */ + +"use strict"; + +add_task(async function test_json_with_mistyped_policies() { + // Note: The "polcies" string is intentionally mistyped + await setupPolicyEngineWithJson({ + polcies: {}, + }); + + is( + Services.policies.status, + Ci.nsIEnterprisePolicies.FAILED, + "Engine was correctly set to the error state" + ); +}); diff --git a/toolkit/components/enterprisepolicies/tests/browser/config_broken_json.json b/toolkit/components/enterprisepolicies/tests/browser/config_broken_json.json new file mode 100644 index 0000000000..7e13efdd88 --- /dev/null +++ b/toolkit/components/enterprisepolicies/tests/browser/config_broken_json.json @@ -0,0 +1,3 @@ +{ + "policies +} diff --git a/toolkit/components/enterprisepolicies/tests/browser/head.js b/toolkit/components/enterprisepolicies/tests/browser/head.js new file mode 100644 index 0000000000..baf0d7780d --- /dev/null +++ b/toolkit/components/enterprisepolicies/tests/browser/head.js @@ -0,0 +1,24 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +"use strict"; + +const { EnterprisePolicyTesting, PoliciesPrefTracker } = + ChromeUtils.importESModule( + "resource://testing-common/EnterprisePolicyTesting.sys.mjs" + ); + +PoliciesPrefTracker.start(); + +async function setupPolicyEngineWithJson(json, customSchema) { + PoliciesPrefTracker.restoreDefaultValues(); + if (typeof json != "object") { + let filePath = getTestFilePath(json ? json : "non-existing-file.json"); + return EnterprisePolicyTesting.setupPolicyEngineWithJson( + filePath, + customSchema + ); + } + return EnterprisePolicyTesting.setupPolicyEngineWithJson(json, customSchema); +} |