Skip to content

Commit

Permalink
Bug 1633466 - Move common test logic to test/xpcshell/rs-blocklist/he…
Browse files Browse the repository at this point in the history
…ad.js r=Gijs

And introduce a helper to get the ExtensionBlocklistMBLF global without
using a deprecated API, and remove the exception from eslintrc

Differential Revision: https://phabricator.services.mozilla.com/D76712
  • Loading branch information
Rob--W committed May 25, 2020
1 parent 9442a7e commit 39f6c3e
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 32 deletions.
3 changes: 0 additions & 3 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -931,9 +931,6 @@ module.exports = {
"toolkit/mozapps/extensions/test/browser/browser_gmpProvider.js",
"toolkit/mozapps/extensions/test/xpcshell/head_addons.js",
"toolkit/mozapps/extensions/test/xpcshell/rs-blocklist/test_blocklist_clients.js",
"toolkit/mozapps/extensions/test/xpcshell/rs-blocklist/test_blocklist_mlbf.js",
"toolkit/mozapps/extensions/test/xpcshell/rs-blocklist/test_blocklist_mlbf_fetch.js",
"toolkit/mozapps/extensions/test/xpcshell/rs-blocklist/test_blocklist_mlbf_update.js",
"toolkit/mozapps/extensions/test/xpcshell/rs-blocklist/test_blocklist_regexp_split.js",
"toolkit/mozapps/extensions/test/xpcshell/rs-blocklist/test_blocklist_targetapp_filter.js",
"toolkit/mozapps/extensions/test/xpcshell/rs-blocklist/test_blocklist_telemetry.js",
Expand Down
33 changes: 33 additions & 0 deletions toolkit/mozapps/extensions/test/xpcshell/rs-blocklist/head.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,36 @@ const MLBF_RECORD = {
attachment_type: "bloomfilter-base",
generation_time: 1577833200000,
};

async function load_mlbf_record_as_blob() {
const url = Services.io.newFileURI(
do_get_file("../data/mlbf-blocked1-unblocked2.bin")
).spec;
Cu.importGlobalProperties(["fetch"]);
return (await fetch(url)).blob();
}

function getExtensionBlocklistMLBF() {
// ExtensionBlocklist.Blocklist is an ExtensionBlocklistMLBF if the useMLBF
// pref is set to true.
// An alternative way to obtain ExtensionBlocklistMLBF is by importing the
// global of Blocklist.jsm and reading ExtensionBlocklistMLBF off it, but
// to avoid using the deprecated ChromeUtils.import(.., null), bug 1524027
// needs to be fixed first. So let's use Blocklist.ExtensionBlocklist.
const ExtensionBlocklistMLBF = Blocklist.ExtensionBlocklist;
Assert.ok(
Services.prefs.getBoolPref("extensions.blocklist.useMLBF", false),
"blocklist.useMLBF should be true"
);
return ExtensionBlocklistMLBF;
}

async function toggleStashPref(val, callbackAfterPrefChange = () => {}) {
const ExtensionBlocklistMLBF = getExtensionBlocklistMLBF();
Assert.ok(!ExtensionBlocklistMLBF._updatePromise, "no pending update");
Services.prefs.setBoolPref("extensions.blocklist.useMLBF.stashes", val);
callbackAfterPrefChange();
// A pref observer should trigger an update.
Assert.ok(ExtensionBlocklistMLBF._updatePromise, "update pending");
await Blocklist.ExtensionBlocklist._updatePromise;
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@

Services.prefs.setBoolPref("extensions.blocklist.useMLBF", true);

const { ExtensionBlocklistMLBF } = ChromeUtils.import(
"resource://gre/modules/Blocklist.jsm",
null
);
const ExtensionBlocklistMLBF = getExtensionBlocklistMLBF();

createAppInfo("[email protected]", "XPCShell", "1", "1");
AddonTestUtils.useRealCertChecks = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
Services.prefs.setBoolPref("extensions.blocklist.useMLBF", true);
Services.prefs.setBoolPref("extensions.blocklist.useMLBF.stashes", true);

const { ExtensionBlocklist: ExtensionBlocklistMLBF } = Blocklist;
const ExtensionBlocklistMLBF = getExtensionBlocklistMLBF();

// A known blocked version from bug 1626602.
const blockedAddon = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,7 @@ const { Downloader } = ChromeUtils.import(
"resource://services-settings/Attachments.jsm"
);

const { ExtensionBlocklistMLBF } = ChromeUtils.import(
"resource://gre/modules/Blocklist.jsm",
null
);
const ExtensionBlocklistMLBF = getExtensionBlocklistMLBF();

// This test needs to interact with the RemoteSettings client.
ExtensionBlocklistMLBF.ensureInitialized();
Expand Down Expand Up @@ -49,15 +46,9 @@ add_task(async function fetch_invalid_mlbf_record() {

// Other tests can mock _testMLBF, so let's verify that it works as expected.
add_task(async function fetch_valid_mlbf() {
const url = Services.io.newFileURI(
do_get_file("../data/mlbf-blocked1-unblocked2.bin")
).spec;
Cu.importGlobalProperties(["fetch"]);
const blob = await (await fetch(url)).blob();

await ExtensionBlocklistMLBF._client.db.saveAttachment(
ExtensionBlocklistMLBF.RS_ATTACHMENT_ID,
{ record: JSON.parse(JSON.stringify(MLBF_RECORD)), blob }
{ record: MLBF_RECORD, blob: await load_mlbf_record_as_blob() }
);

const result = await ExtensionBlocklistMLBF._fetchMLBF(MLBF_RECORD);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Services.prefs.setBoolPref("extensions.blocklist.useMLBF.stashes", true);

createAppInfo("[email protected]", "XPCShell", "1", "1");

const ExtensionBlocklistMLBF = Blocklist.ExtensionBlocklist;
const ExtensionBlocklistMLBF = getExtensionBlocklistMLBF();
const MLBF_LOAD_ATTEMPTS = [];
ExtensionBlocklistMLBF._fetchMLBF = async record => {
MLBF_LOAD_ATTEMPTS.push(record);
Expand All @@ -25,14 +25,6 @@ ExtensionBlocklistMLBF._fetchMLBF = async record => {
};
};

async function toggleStashPref(val) {
Assert.ok(!ExtensionBlocklistMLBF._updatePromise, "no pending update");
Services.prefs.setBoolPref("extensions.blocklist.useMLBF.stashes", val);
// A pref observer should trigger an update.
Assert.ok(ExtensionBlocklistMLBF._updatePromise, "update pending");
await Blocklist.ExtensionBlocklist._updatePromise;
}

async function checkBlockState(addonId, version, expectBlocked) {
let addon = {
id: addonId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,8 @@
* @fileOverview Checks that the MLBF updating logic works reasonably.
*/

const { ExtensionBlocklistMLBF } = ChromeUtils.import(
"resource://gre/modules/Blocklist.jsm",
null
);
Services.prefs.setBoolPref("extensions.blocklist.useMLBF", true);
const ExtensionBlocklistMLBF = getExtensionBlocklistMLBF();

// This test needs to interact with the RemoteSettings client.
ExtensionBlocklistMLBF.ensureInitialized();
Expand Down

0 comments on commit 39f6c3e

Please sign in to comment.