Bug 1648545: Part 2 - Load SpecialPowers in XPCShellContentUtils. r=ahal,application-update-reviewers,nalexander

Differential Revision: https://phabricator.services.mozilla.com/D119449
This commit is contained in:
Kris Maglione 2023-05-25 22:04:31 +00:00
parent d648a1f5fe
commit 1abe5de918
31 changed files with 73 additions and 132 deletions

View File

@ -7,6 +7,13 @@
const URL_FOUND = "resource://devtools/shared/DevToolsUtils.js";
const URL_NOT_FOUND = "resource://devtools/this/is/not/here.js";
// Disable `xpc::IsInAutomation()` so we don't crash when accessing a
// nonexistent resource URI.
Services.prefs.setBoolPref(
"security.turn_off_all_security_so_that_viruses_can_take_over_this_computer",
false
);
/**
* Test that non-existent files are handled correctly.
*/

View File

@ -3,6 +3,13 @@
const { AppConstants } = ChromeUtils.importESModule("resource://gre/modules/AppConstants.sys.mjs");
// Disable `xpc::IsInAutomation()` so incomplete locales do not generate
// errors.
Services.prefs.setBoolPref(
"security.turn_off_all_security_so_that_viruses_can_take_over_this_computer",
false
);
add_task(function test_methods_presence() {
strictEqual(typeof Localization.prototype.formatValues, "function");
strictEqual(typeof Localization.prototype.formatMessages, "function");

View File

@ -3,6 +3,13 @@
const { AppConstants } = ChromeUtils.importESModule("resource://gre/modules/AppConstants.sys.mjs");
// Disable `xpc::IsInAutomation()` so incomplete locales do not generate
// errors.
Services.prefs.setBoolPref(
"security.turn_off_all_security_so_that_viruses_can_take_over_this_computer",
false
);
add_task(function test_methods_calling() {
const l10nReg = new L10nRegistry();

View File

@ -34,11 +34,7 @@ AddonTestUtils.createAppInfo(
);
AddonTestUtils.overrideCertDB();
// Attempting to set the
// security.turn_off_all_security_so_that_viruses_can_take_over_this_computer
// preference to enable Cu.exitIfInAutomation crashes, probably due to
// shutdown behaviors faked by AddonTestUtils.jsm's cleanup function.
do_disable_fast_shutdown();
Services.prefs.setBoolPref("preferences.force-disable.check.once.policy", true);
add_task(async function run_test() {
_("Test fixtures.");

View File

@ -8,11 +8,7 @@ const { Service } = ChromeUtils.importESModule(
"resource://services-sync/service.sys.mjs"
);
// Attempting to set the
// security.turn_off_all_security_so_that_viruses_can_take_over_this_computer
// preference to enable Cu.exitIfInAutomation crashes, probably due to
// shutdown behaviors faked by AddonTestUtils.jsm's cleanup function.
do_disable_fast_shutdown();
Services.prefs.setBoolPref("preferences.force-disable.check.once.policy", true);
add_task(async function run_test() {
let engine = Service.engineManager.get("prefs");

View File

@ -22,6 +22,7 @@ const lazy = {};
ChromeUtils.defineESModuleGetters(lazy, {
ContentTask: "resource://testing-common/ContentTask.sys.mjs",
SpecialPowersParent: "resource://testing-common/SpecialPowersParent.sys.mjs",
TestUtils: "resource://testing-common/TestUtils.sys.mjs",
});
@ -230,6 +231,10 @@ class ContentPage {
return this.browser.browsingContext;
}
get SpecialPowers() {
return this.browser.ownerGlobal.SpecialPowers;
}
sendMessage(msg, data) {
return lazy.MessageChannel.sendMessage(
this.browser.messageManager,
@ -323,6 +328,8 @@ export var XPCShellContentUtils = {
scope.do_get_profile();
this.initCommon(scope);
lazy.SpecialPowersParent.registerActor();
},
initMochitest(scope) {

View File

@ -33,3 +33,4 @@ user_pref("gfx.color_management.force_srgb", true);
user_pref("gfx.color_management.mode", 1);
// Don't enable remote tiles on new-tab pages in xpcshell
user_pref("browser.topsites.contile.enabled", false);
user_pref("security.turn_off_all_security_so_that_viruses_can_take_over_this_computer", true);

View File

@ -4,6 +4,10 @@
/* globals ExtensionAPI, Services, XPCOMUtils */
ChromeUtils.defineESModuleGetters(this, {
SpecialPowersParent: "resource://testing-common/SpecialPowersParent.sys.mjs",
});
XPCOMUtils.defineLazyServiceGetter(
this,
"resProto",
@ -27,20 +31,7 @@ this.specialpowers = class extends ExtensionAPI {
.QueryInterface(Ci.nsIComponentRegistrar)
.autoRegister(manifest);
ChromeUtils.registerWindowActor("SpecialPowers", {
allFrames: true,
includeChrome: true,
child: {
esModuleURI: "resource://testing-common/SpecialPowersChild.sys.mjs",
observers: [
"chrome-document-global-created",
"content-document-global-created",
],
},
parent: {
esModuleURI: "resource://testing-common/SpecialPowersParent.sys.mjs",
},
});
SpecialPowersParent.registerActor();
ChromeUtils.registerWindowActor("AppTestDelegate", {
parent: {
@ -59,7 +50,7 @@ this.specialpowers = class extends ExtensionAPI {
}
onShutdown() {
ChromeUtils.unregisterWindowActor("SpecialPowers");
SpecialPowersParent.unregisterActor();
ChromeUtils.unregisterWindowActor("AppTestDelegate");
resProto.setSubstitution("specialpowers", null);
}

View File

@ -183,6 +183,27 @@ export class SpecialPowersParent extends JSWindowActorParent {
this._taskActors = new Map();
}
static registerActor() {
ChromeUtils.registerWindowActor("SpecialPowers", {
allFrames: true,
includeChrome: true,
child: {
esModuleURI: "resource://testing-common/SpecialPowersChild.sys.mjs",
observers: [
"chrome-document-global-created",
"content-document-global-created",
],
},
parent: {
esModuleURI: "resource://testing-common/SpecialPowersParent.sys.mjs",
},
});
}
static unregisterActor() {
ChromeUtils.unregisterWindowActor("SpecialPowers");
}
init() {
Services.obs.addObserver(this._observer, "http-on-modify-request");

View File

@ -34,7 +34,6 @@ var _tests_pending = 0;
var _cleanupFunctions = [];
var _pendingTimers = [];
var _profileInitialized = false;
var _fastShutdownDisabled = false;
// Assigned in do_load_child_test_harness.
var _XPCSHELL_PROCESS;
@ -724,16 +723,6 @@ function _execute_test() {
!_AppConstants.ASAN &&
!_AppConstants.TSAN
) {
if (_fastShutdownDisabled) {
_testLogger.info("fast shutdown disabled by the test.");
return;
}
// Setting this pref is required for Cu.isInAutomation to return true.
_Services.prefs.setBoolPref(
"security.turn_off_all_security_so_that_viruses_can_take_over_this_computer",
true
);
Cu.exitIfInAutomation();
}
}
@ -1234,14 +1223,6 @@ function registerCleanupFunction(aFunction) {
_cleanupFunctions.push(aFunction);
}
/**
* Ensure the test finishes with a normal shutdown even when it could have
* otherwise used the fast Cu.exitIfInAutomation shutdown.
*/
function do_disable_fast_shutdown() {
_fastShutdownDisabled = true;
}
/**
* Returns the directory for a temp dir, which is created by the
* test harness. Every test gets its own temp dir.

View File

@ -4,6 +4,13 @@
* 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/. */
// Disable `xpc::IsInAutomation()` so that we don't generate fatal
// Localization errors.
Services.prefs.setBoolPref(
"security.turn_off_all_security_so_that_viruses_can_take_over_this_computer",
false
);
async function doOne(resource, id) {
let l10n = new Localization([resource], true);
let value = await l10n.formatValue(id);

View File

@ -669,7 +669,7 @@ var ExtensionTestUtils = {
profileDir: null,
init(scope) {
XPCShellContentUtils.init(scope);
XPCShellContentUtils.ensureInitialized(scope);
this.currentScope = scope;

View File

@ -15,6 +15,8 @@ ChromeUtils.defineModuleGetter(
// The test extension uses an insecure update url.
Services.prefs.setBoolPref("extensions.checkUpdateSecurity", false);
Services.prefs.setBoolPref("preferences.force-disable.check.once.policy", true);
const SETTINGS_ID = "test_settings_staged_restart_webext@tests.mozilla.org";
const { createAppInfo, promiseShutdownManager, promiseStartupManager } =

View File

@ -29,7 +29,7 @@ const l10n = new Localization([
"branding/brand.ftl",
]);
// Localization resources need to be first iterated outside a test
l10n.formatValue("webext-perms-add");
l10n.formatValue("webext-perms-sideload-text");
AddonTestUtils.init(this);
AddonTestUtils.overrideCertDB();

View File

@ -1,20 +1,9 @@
"use strict";
const PREF_DISABLE_SECURITY =
"security.turn_off_all_security_so_that_" +
"viruses_can_take_over_this_computer";
const { EnterprisePolicyTesting } = ChromeUtils.importESModule(
"resource://testing-common/EnterprisePolicyTesting.sys.mjs"
);
// Setting PREF_DISABLE_SECURITY tells the policy engine that we are in testing
// mode and enables restarting the policy engine without restarting the browser.
Services.prefs.setBoolPref(PREF_DISABLE_SECURITY, true);
registerCleanupFunction(() => {
Services.prefs.clearUserPref(PREF_DISABLE_SECURITY);
});
// Load policy engine
Services.policies; // eslint-disable-line no-unused-expressions

View File

@ -1,9 +1,5 @@
"use strict";
const PREF_DISABLE_SECURITY =
"security.turn_off_all_security_so_that_" +
"viruses_can_take_over_this_computer";
const HOSTS = new Set(["example.com", "example.org"]);
const server = createHttpServer({ hosts: HOSTS });
@ -94,7 +90,6 @@ add_task(async function test_permissions() {
"Regular webRequest redirect works from an unprivileged page"
);
Services.prefs.setBoolPref(PREF_DISABLE_SECURITY, true);
Services.prefs.setBoolPref("extensions.webapi.testing", true);
Services.prefs.setBoolPref("extensions.webapi.testing.http", true);

View File

@ -34,7 +34,7 @@ const l10n = new Localization([
"branding/brand.ftl",
]);
// Localization resources need to be first iterated outside a test
l10n.formatValue("webext-perms-add");
l10n.formatValue("webext-perms-sideload-text");
// Lazily import ExtensionParent to allow AddonTestUtils.createAppInfo to
// override Services.appinfo.

View File

@ -5,6 +5,8 @@
// Note: the order of the tests here matters, since we are emulating subsquent
// starts of the recalculator component with different initial conditions.
Services.prefs.setBoolPref("preferences.force-disable.check.once.policy", true);
const FEATURE_PREF = "places.frecency.pages.alternative.featureGate";
async function restartRecalculator() {

View File

@ -52,10 +52,6 @@ XPCOMUtils.defineLazyServiceGetters(lazy, {
],
});
const PREF_DISABLE_SECURITY =
"security.turn_off_all_security_so_that_" +
"viruses_can_take_over_this_computer";
const ArrayBufferInputStream = Components.Constructor(
"@mozilla.org/io/arraybuffer-input-stream;1",
"nsIArrayBufferInputStream",
@ -1747,11 +1743,9 @@ var AddonTestUtils = {
/**
* Initializes the URLPreloader, which is required in order to load
* built_in_addons.json. This has the side-effect of setting
* preferences which flip Cu.isInAutomation to true.
* built_in_addons.json.
*/
initializeURLPreloader() {
Services.prefs.setBoolPref(PREF_DISABLE_SECURITY, true);
lazy.aomStartup.initializeURLPreloader();
},
@ -1762,9 +1756,6 @@ var AddonTestUtils = {
* to load, for instance: { "system": ["id1", "..."] }
*/
async overrideBuiltIns(data) {
// We need to set this in order load the URL preloader service, which
// is only possible when running in automation.
let prevPrefVal = Services.prefs.getBoolPref(PREF_DISABLE_SECURITY, false);
this.initializeURLPreloader();
let file = this.tempDir.clone();
@ -1780,7 +1771,6 @@ var AddonTestUtils = {
Services.io.newFileURI(file).spec,
],
]);
Services.prefs.setBoolPref(PREF_DISABLE_SECURITY, prevPrefVal);
},
// AMTelemetry events helpers.

View File

@ -17,10 +17,6 @@ const PREF_EM_STRICT_COMPATIBILITY = "extensions.strictCompatibility";
const PREF_GETADDONS_BYIDS = "extensions.getAddons.get.url";
const PREF_XPI_SIGNATURES_REQUIRED = "xpinstall.signatures.required";
const PREF_DISABLE_SECURITY =
"security.turn_off_all_security_so_that_" +
"viruses_can_take_over_this_computer";
// Maximum error in file modification times. Some file systems don't store
// modification times exactly. As long as we are closer than this then it
// still passes.

View File

@ -3,11 +3,6 @@
/* import-globals-from head_addons.js */
Services.prefs.setBoolPref(
"security.turn_off_all_security_so_that_viruses_can_take_over_this_computer",
true
);
// Enable all scopes.
Services.prefs.setIntPref("extensions.enabledScopes", AddonManager.SCOPE_ALL);
// Setting this to all enables the same behavior as before disabling sideloading.

View File

@ -7,10 +7,7 @@ const PREF_IS_EMBEDDED = "extensions.isembedded";
createAppInfo("xpcshell@tests.mozilla.org", "XPCShell", "1", "49");
// Setting PREF_DISABLE_SECURITY tells the policy engine that we are in testing
// mode and enables restarting the policy engine without restarting the browser.
registerCleanupFunction(() => {
Services.prefs.clearUserPref(PREF_DISABLE_SECURITY);
Services.prefs.clearUserPref(PREF_IS_EMBEDDED);
});
@ -23,7 +20,6 @@ async function installExtension() {
}
add_task(async function test_setup() {
Services.prefs.setBoolPref(PREF_DISABLE_SECURITY, true);
await promiseStartupManager();
});

View File

@ -1,9 +1,6 @@
/* Any copyright is dedicated to the Public Domain.
http://creativecommons.org/publicdomain/zero/1.0/ */
// turn on Cu.isInAutomation
Services.prefs.setBoolPref(PREF_DISABLE_SECURITY, true);
const ID = "addon1@tests.mozilla.org";
add_task(async function run_test() {
createAppInfo("xpcshell@tests.mozilla.org", "XPCShell", "1", "1.9.2");

View File

@ -2,12 +2,6 @@
* http://creativecommons.org/publicdomain/zero/1.0/
*/
// Make Cu.isInAutomation true.
Services.prefs.setBoolPref(
"security.turn_off_all_security_so_that_viruses_can_take_over_this_computer",
true
);
// Tests that extensions installed through the registry work as expected
createAppInfo("xpcshell@tests.mozilla.org", "XPCShell", "1", "1.9.2");

View File

@ -6,7 +6,6 @@ const PREF_DB_SCHEMA = "extensions.databaseSchema";
const PREF_IS_EMBEDDED = "extensions.isembedded";
registerCleanupFunction(() => {
Services.prefs.clearUserPref(PREF_DISABLE_SECURITY);
Services.prefs.clearUserPref(PREF_IS_EMBEDDED);
});
@ -16,7 +15,6 @@ profileDir.append("extensions");
createAppInfo("xpcshell@tests.mozilla.org", "XPCShell", "1", "49");
add_task(async function test_setup() {
Services.prefs.setBoolPref(PREF_DISABLE_SECURITY, true);
await promiseStartupManager();
});

View File

@ -2,12 +2,6 @@
* http://creativecommons.org/publicdomain/zero/1.0/
*/
// Make Cu.isInAutomation true.
Services.prefs.setBoolPref(
"security.turn_off_all_security_so_that_viruses_can_take_over_this_computer",
true
);
const ID = "addon@tests.mozilla.org";
let profileDir = gProfD.clone();

View File

@ -5,9 +5,6 @@
// extensions.strictCompatibility, and the strictCompatibility option in
// install.rdf
// turn on Cu.isInAutomation
Services.prefs.setBoolPref(PREF_DISABLE_SECURITY, true);
PromiseTestUtils.allowMatchingRejectionsGlobally(
/IOUtils: Shutting down and refusing additional I\/O tasks/
);

View File

@ -9,13 +9,6 @@ const { EnterprisePolicyTesting } = ChromeUtils.importESModule(
"resource://testing-common/EnterprisePolicyTesting.sys.mjs"
);
// Setting PREF_DISABLE_SECURITY tells the policy engine that we are in testing
// mode and enables restarting the policy engine without restarting the browser.
Services.prefs.setBoolPref(PREF_DISABLE_SECURITY, true);
registerCleanupFunction(() => {
Services.prefs.clearUserPref(PREF_DISABLE_SECURITY);
});
Services.policies; // Load policy engine
createAppInfo("xpcshell@tests.mozilla.org", "XPCShell", "2");

View File

@ -2,18 +2,9 @@
* http://creativecommons.org/publicdomain/zero/1.0/
*/
// Make Cu.isInAutomation true.
Services.prefs.setBoolPref(
"security.turn_off_all_security_so_that_viruses_can_take_over_this_computer",
true
);
// This verifies that app upgrades produce the expected behaviours,
// with strict compatibility checking disabled.
// turn on Cu.isInAutomation
Services.prefs.setBoolPref(PREF_DISABLE_SECURITY, true);
Services.prefs.setBoolPref(PREF_EM_STRICT_COMPATIBILITY, false);
// Enable loading extensions from the application scope

View File

@ -1,6 +1,3 @@
// turn on Cu.isInAutomation
Services.prefs.setBoolPref(PREF_DISABLE_SECURITY, true);
// Tests that when an extension manifest that was previously valid becomes
// unparseable after an application update, the extension becomes
// disabled. (See bug 1439600 for a concrete example of a situation where

View File

@ -62,11 +62,6 @@ const LOG_PARTIAL_SUCCESS = "partial_log_success" + COMPARE_LOG_SUFFIX;
const LOG_PARTIAL_FAILURE = "partial_log_failure" + COMPARE_LOG_SUFFIX;
const LOG_REPLACE_SUCCESS = "replace_log_success";
// xpcshell tests need this preference set to true for Cu.isInAutomation to be
// true.
const PREF_IS_IN_AUTOMATION =
"security.turn_off_all_security_so_that_viruses_can_take_over_this_computer";
const USE_EXECV = AppConstants.platform == "linux";
const URL_HOST = "http://localhost";
@ -1167,7 +1162,6 @@ function doTestFinish() {
* Sets the most commonly used preferences used by tests
*/
function setDefaultPrefs() {
Services.prefs.setBoolPref(PREF_IS_IN_AUTOMATION, true);
Services.prefs.setBoolPref(PREF_APP_UPDATE_DISABLEDFORTESTING, false);
if (gDebugTest) {
// Enable Update logging