summaryrefslogtreecommitdiffstats
path: root/toolkit/components/enterprisepolicies/tests
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-19 00:47:55 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-19 00:47:55 +0000
commit26a029d407be480d791972afb5975cf62c9360a6 (patch)
treef435a8308119effd964b339f76abb83a57c29483 /toolkit/components/enterprisepolicies/tests
parentInitial commit. (diff)
downloadfirefox-26a029d407be480d791972afb5975cf62c9360a6.tar.xz
firefox-26a029d407be480d791972afb5975cf62c9360a6.zip
Adding upstream version 124.0.1.upstream/124.0.1
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.sys.mjs166
-rw-r--r--toolkit/components/enterprisepolicies/tests/browser/browser.toml14
-rw-r--r--toolkit/components/enterprisepolicies/tests/browser/browser_policies_basic_tests.js140
-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_gpo.js206
-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.js24
-rw-r--r--toolkit/components/enterprisepolicies/tests/moz.build15
-rw-r--r--toolkit/components/enterprisepolicies/tests/xpcshell/head.js36
-rw-r--r--toolkit/components/enterprisepolicies/tests/xpcshell/test_empty.js16
-rw-r--r--toolkit/components/enterprisepolicies/tests/xpcshell/xpcshell.toml6
13 files changed, 727 insertions, 0 deletions
diff --git a/toolkit/components/enterprisepolicies/tests/EnterprisePolicyTesting.sys.mjs b/toolkit/components/enterprisepolicies/tests/EnterprisePolicyTesting.sys.mjs
new file mode 100644
index 0000000000..b877c6f738
--- /dev/null
+++ b/toolkit/components/enterprisepolicies/tests/EnterprisePolicyTesting.sys.mjs
@@ -0,0 +1,166 @@
+/* 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/. */
+
+import { Preferences } from "resource://gre/modules/Preferences.sys.mjs";
+
+import { Assert } from "resource://testing-common/Assert.sys.mjs";
+
+const lazy = {};
+ChromeUtils.defineESModuleGetters(lazy, {
+ FileTestUtils: "resource://testing-common/FileTestUtils.sys.mjs",
+ modifySchemaForTests: "resource:///modules/policies/schema.sys.mjs",
+});
+
+export 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 = lazy.FileTestUtils.getTempFile("policies.json").path;
+
+ // This file gets automatically deleted by FileTestUtils
+ // at the end of the test run.
+ await IOUtils.writeJSON(filePath, json);
+ } 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 or assign a new one
+ lazy.modifySchemaForTests(customSchema || null);
+
+ 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.
+ */
+export var PoliciesPrefTracker = {
+ _originalFunc: null,
+ _originalValues: new Map(),
+
+ start() {
+ let { PoliciesUtils } = ChromeUtils.importESModule(
+ "resource:///modules/policies/Policies.sys.mjs"
+ );
+ this._originalFunc = PoliciesUtils.setDefaultPref;
+ PoliciesUtils.setDefaultPref = this.hoistedSetDefaultPref.bind(this);
+ },
+
+ stop() {
+ this.restoreDefaultValues();
+
+ let { PoliciesUtils } = ChromeUtils.importESModule(
+ "resource:///modules/policies/Policies.sys.mjs"
+ );
+ PoliciesUtils.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.toml b/toolkit/components/enterprisepolicies/tests/browser/browser.toml
new file mode 100644
index 0000000000..1b76676779
--- /dev/null
+++ b/toolkit/components/enterprisepolicies/tests/browser/browser.toml
@@ -0,0 +1,14 @@
+[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);
+}
diff --git a/toolkit/components/enterprisepolicies/tests/moz.build b/toolkit/components/enterprisepolicies/tests/moz.build
new file mode 100644
index 0000000000..14d1ec3803
--- /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.toml",
+]
+
+TESTING_JS_MODULES += [
+ "EnterprisePolicyTesting.sys.mjs",
+]
+
+XPCSHELL_TESTS_MANIFESTS += ["xpcshell/xpcshell.toml"]
diff --git a/toolkit/components/enterprisepolicies/tests/xpcshell/head.js b/toolkit/components/enterprisepolicies/tests/xpcshell/head.js
new file mode 100644
index 0000000000..d4a585e299
--- /dev/null
+++ b/toolkit/components/enterprisepolicies/tests/xpcshell/head.js
@@ -0,0 +1,36 @@
+/* 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 { updateAppInfo } = ChromeUtils.importESModule(
+ "resource://testing-common/AppInfo.sys.mjs"
+);
+const { EnterprisePolicyTesting } = ChromeUtils.importESModule(
+ "resource://testing-common/EnterprisePolicyTesting.sys.mjs"
+);
+
+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.toml b/toolkit/components/enterprisepolicies/tests/xpcshell/xpcshell.toml
new file mode 100644
index 0000000000..7229da98ed
--- /dev/null
+++ b/toolkit/components/enterprisepolicies/tests/xpcshell/xpcshell.toml
@@ -0,0 +1,6 @@
+[DEFAULT]
+firefox-appdir = "browser"
+head = "head.js"
+skip-if = ["os == 'android'"]
+
+["test_empty.js"]