summaryrefslogtreecommitdiffstats
path: root/toolkit/components/enterprisepolicies/tests
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-28 14:29:10 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-28 14:29:10 +0000
commit2aa4a82499d4becd2284cdb482213d541b8804dd (patch)
treeb80bf8bf13c3766139fbacc530efd0dd9d54394c /toolkit/components/enterprisepolicies/tests
parentInitial commit. (diff)
downloadfirefox-upstream.tar.xz
firefox-upstream.zip
Adding upstream version 86.0.1.upstream/86.0.1upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'toolkit/components/enterprisepolicies/tests')
-rw-r--r--toolkit/components/enterprisepolicies/tests/EnterprisePolicyTesting.jsm184
-rw-r--r--toolkit/components/enterprisepolicies/tests/browser/browser.ini9
-rw-r--r--toolkit/components/enterprisepolicies/tests/browser/browser_policies_basic_tests.js126
-rw-r--r--toolkit/components/enterprisepolicies/tests/browser/browser_policies_broken_json.js14
-rw-r--r--toolkit/components/enterprisepolicies/tests/browser/browser_policies_enterprise_only.js70
-rw-r--r--toolkit/components/enterprisepolicies/tests/browser/browser_policies_mistyped_json.js17
-rw-r--r--toolkit/components/enterprisepolicies/tests/browser/config_broken_json.json3
-rw-r--r--toolkit/components/enterprisepolicies/tests/browser/head.js28
-rw-r--r--toolkit/components/enterprisepolicies/tests/moz.build15
-rw-r--r--toolkit/components/enterprisepolicies/tests/xpcshell/head.js47
-rw-r--r--toolkit/components/enterprisepolicies/tests/xpcshell/test_empty.js16
-rw-r--r--toolkit/components/enterprisepolicies/tests/xpcshell/xpcshell.ini5
12 files changed, 534 insertions, 0 deletions
diff --git a/toolkit/components/enterprisepolicies/tests/EnterprisePolicyTesting.jsm b/toolkit/components/enterprisepolicies/tests/EnterprisePolicyTesting.jsm
new file mode 100644
index 0000000000..dd98c03bce
--- /dev/null
+++ b/toolkit/components/enterprisepolicies/tests/EnterprisePolicyTesting.jsm
@@ -0,0 +1,184 @@
+/* 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 { Preferences } = ChromeUtils.import(
+ "resource://gre/modules/Preferences.jsm"
+);
+const { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm");
+const { OS } = ChromeUtils.import("resource://gre/modules/osfile.jsm");
+const { Assert } = ChromeUtils.import("resource://testing-common/Assert.jsm");
+ChromeUtils.defineModuleGetter(
+ this,
+ "FileTestUtils",
+ "resource://testing-common/FileTestUtils.jsm"
+);
+
+var EXPORTED_SYMBOLS = ["EnterprisePolicyTesting", "PoliciesPrefTracker"];
+
+var EnterprisePolicyTesting = {
+ // |json| must be an object representing the desired policy configuration, OR a
+ // path to the JSON file containing the policy configuration.
+ setupPolicyEngineWithJson: async function setupPolicyEngineWithJson(
+ json,
+ customSchema
+ ) {
+ let filePath;
+ if (typeof json == "object") {
+ filePath = FileTestUtils.getTempFile("policies.json").path;
+
+ // This file gets automatically deleted by FileTestUtils
+ // at the end of the test run.
+ await OS.File.writeAtomic(filePath, JSON.stringify(json), {
+ encoding: "utf-8",
+ });
+ } else {
+ filePath = json;
+ }
+
+ Services.prefs.setStringPref("browser.policies.alternatePath", filePath);
+
+ let promise = new Promise(resolve => {
+ Services.obs.addObserver(function observer() {
+ Services.obs.removeObserver(
+ observer,
+ "EnterprisePolicies:AllPoliciesApplied"
+ );
+ resolve();
+ }, "EnterprisePolicies:AllPoliciesApplied");
+ });
+
+ // Clear any previously used custom schema
+ Cu.unload("resource:///modules/policies/schema.jsm");
+
+ if (customSchema) {
+ let schemaModule = ChromeUtils.import(
+ "resource:///modules/policies/schema.jsm",
+ null
+ );
+ schemaModule.schema = customSchema;
+ }
+
+ Services.obs.notifyObservers(null, "EnterprisePolicies:Restart");
+ return promise;
+ },
+
+ checkPolicyPref(prefName, expectedValue, expectedLockedness) {
+ if (expectedLockedness !== undefined) {
+ Assert.equal(
+ Preferences.locked(prefName),
+ expectedLockedness,
+ `Pref ${prefName} is correctly locked/unlocked`
+ );
+ }
+
+ Assert.equal(
+ Preferences.get(prefName),
+ expectedValue,
+ `Pref ${prefName} has the correct value`
+ );
+ },
+
+ resetRunOnceState: function resetRunOnceState() {
+ const runOnceBaseKeys = [
+ "browser.policies.runonce.",
+ "browser.policies.runOncePerModification.",
+ ];
+ for (let base of runOnceBaseKeys) {
+ for (let key of Services.prefs.getChildList(base)) {
+ if (Services.prefs.prefHasUserValue(key)) {
+ Services.prefs.clearUserPref(key);
+ }
+ }
+ }
+ },
+};
+
+/**
+ * This helper will track prefs that have been changed
+ * by the policy engine through the setAndLockPref and
+ * setDefaultPref APIs (from Policies.jsm) and make sure
+ * that they are restored to their original values when
+ * the test ends or another test case restarts the engine.
+ */
+var PoliciesPrefTracker = {
+ _originalFunc: null,
+ _originalValues: new Map(),
+
+ start() {
+ let PoliciesBackstage = ChromeUtils.import(
+ "resource:///modules/policies/Policies.jsm",
+ null
+ );
+ this._originalFunc = PoliciesBackstage.setDefaultPref;
+ PoliciesBackstage.setDefaultPref = this.hoistedSetDefaultPref.bind(this);
+ },
+
+ stop() {
+ this.restoreDefaultValues();
+
+ let PoliciesBackstage = ChromeUtils.import(
+ "resource:///modules/policies/Policies.jsm",
+ null
+ );
+ PoliciesBackstage.setDefaultPref = this._originalFunc;
+ this._originalFunc = null;
+ },
+
+ hoistedSetDefaultPref(prefName, prefValue, locked = false) {
+ // If this pref is seen multiple times, the very first
+ // value seen is the one that is actually the default.
+ if (!this._originalValues.has(prefName)) {
+ let defaults = new Preferences({ defaultBranch: true });
+ let stored = {};
+
+ if (defaults.has(prefName)) {
+ stored.originalDefaultValue = defaults.get(prefName);
+ } else {
+ stored.originalDefaultValue = undefined;
+ }
+
+ if (
+ Preferences.isSet(prefName) &&
+ Preferences.get(prefName) == prefValue
+ ) {
+ // If a user value exists, and we're changing the default
+ // value to be th same as the user value, that will cause
+ // the user value to be dropped. In that case, let's also
+ // store it to ensure that we restore everything correctly.
+ stored.originalUserValue = Preferences.get(prefName);
+ }
+
+ this._originalValues.set(prefName, stored);
+ }
+
+ // Now that we've stored the original values, call the
+ // original setDefaultPref function.
+ this._originalFunc(prefName, prefValue, locked);
+ },
+
+ restoreDefaultValues() {
+ let defaults = new Preferences({ defaultBranch: true });
+
+ for (let [prefName, stored] of this._originalValues) {
+ // If a pref was used through setDefaultPref instead
+ // of setAndLockPref, it wasn't locked, but calling
+ // unlockPref is harmless
+ Preferences.unlock(prefName);
+
+ if (stored.originalDefaultValue !== undefined) {
+ defaults.set(prefName, stored.originalDefaultValue);
+ } else {
+ Services.prefs.getDefaultBranch("").deleteBranch(prefName);
+ }
+
+ if (stored.originalUserValue !== undefined) {
+ Preferences.set(prefName, stored.originalUserValue);
+ }
+ }
+
+ this._originalValues.clear();
+ },
+};
diff --git a/toolkit/components/enterprisepolicies/tests/browser/browser.ini b/toolkit/components/enterprisepolicies/tests/browser/browser.ini
new file mode 100644
index 0000000000..23a860dd8b
--- /dev/null
+++ b/toolkit/components/enterprisepolicies/tests/browser/browser.ini
@@ -0,0 +1,9 @@
+[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_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..24de9c2f23
--- /dev/null
+++ b/toolkit/components/enterprisepolicies/tests/browser/browser_policies_basic_tests.js
@@ -0,0 +1,126 @@
+/* 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.import(
+ "resource:///modules/policies/Policies.jsm"
+ );
+
+ 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;
+});
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..85bec8191f
--- /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.import(
+ "resource:///modules/policies/Policies.jsm"
+ );
+
+ 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_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..f099749df1
--- /dev/null
+++ b/toolkit/components/enterprisepolicies/tests/browser/head.js
@@ -0,0 +1,28 @@
+/* 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.import(
+ "resource://testing-common/EnterprisePolicyTesting.jsm",
+ null
+);
+const { TestUtils } = ChromeUtils.import(
+ "resource://testing-common/TestUtils.jsm",
+ null
+);
+
+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);
+}
diff --git a/toolkit/components/enterprisepolicies/tests/moz.build b/toolkit/components/enterprisepolicies/tests/moz.build
new file mode 100644
index 0000000000..63b6391244
--- /dev/null
+++ b/toolkit/components/enterprisepolicies/tests/moz.build
@@ -0,0 +1,15 @@
+# -*- Mode: python; indent-tabs-mode: nil; tab-width: 40 -*-
+# vim: set filetype=python:
+# 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/.
+
+BROWSER_CHROME_MANIFESTS += [
+ "browser/browser.ini",
+]
+
+TESTING_JS_MODULES += [
+ "EnterprisePolicyTesting.jsm",
+]
+
+XPCSHELL_TESTS_MANIFESTS += ["xpcshell/xpcshell.ini"]
diff --git a/toolkit/components/enterprisepolicies/tests/xpcshell/head.js b/toolkit/components/enterprisepolicies/tests/xpcshell/head.js
new file mode 100644
index 0000000000..74bd1252f9
--- /dev/null
+++ b/toolkit/components/enterprisepolicies/tests/xpcshell/head.js
@@ -0,0 +1,47 @@
+/* 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 { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm");
+const { OS } = ChromeUtils.import("resource://gre/modules/osfile.jsm");
+const { Preferences } = ChromeUtils.import(
+ "resource://gre/modules/Preferences.jsm"
+);
+const { updateAppInfo, getAppInfo } = ChromeUtils.import(
+ "resource://testing-common/AppInfo.jsm"
+);
+const { FileTestUtils } = ChromeUtils.import(
+ "resource://testing-common/FileTestUtils.jsm"
+);
+const { PermissionTestUtils } = ChromeUtils.import(
+ "resource://testing-common/PermissionTestUtils.jsm"
+);
+const { EnterprisePolicyTesting } = ChromeUtils.import(
+ "resource://testing-common/EnterprisePolicyTesting.jsm"
+);
+
+updateAppInfo({
+ name: "XPCShell",
+ ID: "xpcshell@tests.mozilla.org",
+ version: "48",
+ platformVersion: "48",
+});
+
+// This initializes the policy engine for xpcshell tests
+let policies = Cc["@mozilla.org/enterprisepolicies;1"].getService(
+ Ci.nsIObserver
+);
+policies.observe(null, "policies-startup", null);
+
+async function setupPolicyEngineWithJson(json, customSchema) {
+ if (typeof json != "object") {
+ let filePath = do_get_file(json ? json : "non-existing-file.json").path;
+ return EnterprisePolicyTesting.setupPolicyEngineWithJson(
+ filePath,
+ customSchema
+ );
+ }
+ return EnterprisePolicyTesting.setupPolicyEngineWithJson(json, customSchema);
+}
diff --git a/toolkit/components/enterprisepolicies/tests/xpcshell/test_empty.js b/toolkit/components/enterprisepolicies/tests/xpcshell/test_empty.js
new file mode 100644
index 0000000000..7458f95960
--- /dev/null
+++ b/toolkit/components/enterprisepolicies/tests/xpcshell/test_empty.js
@@ -0,0 +1,16 @@
+/* Any copyright is dedicated to the Public Domain.
+ * http://creativecommons.org/publicdomain/zero/1.0/ */
+
+"use strict";
+
+add_task(async function test_empty_toplevel() {
+ await setupPolicyEngineWithJson({
+ policies: {},
+ });
+
+ equal(
+ Services.policies.status,
+ Ci.nsIEnterprisePolicies.INACTIVE,
+ "Engine is not active"
+ );
+});
diff --git a/toolkit/components/enterprisepolicies/tests/xpcshell/xpcshell.ini b/toolkit/components/enterprisepolicies/tests/xpcshell/xpcshell.ini
new file mode 100644
index 0000000000..b5ba816b26
--- /dev/null
+++ b/toolkit/components/enterprisepolicies/tests/xpcshell/xpcshell.ini
@@ -0,0 +1,5 @@
+[DEFAULT]
+head = head.js
+skip-if = toolkit == 'android'
+
+[test_empty.js]