Source code

Revision control

Copy as Markdown

Other Tools

Test Info:

/* Any copyright is dedicated to the Public Domain.
*/
// Tests that PerrmissionsUtils.sys.mjs works as expected, including:
// * PermissionsUtils.importfromPrefs()
// <ROOT>.[whitelist|blacklist].add preferences are emptied when
// converted into permissions on startup.
const PREF_ROOT = "testpermissions.";
const TEST_PERM = "test-permission";
const { PermissionsUtils } = ChromeUtils.importESModule(
"resource://gre/modules/PermissionsUtils.sys.mjs"
);
const { PermissionTestUtils } = ChromeUtils.importESModule(
);
function run_test() {
test_importfromPrefs();
}
function test_importfromPrefs() {
// Create own preferences to test
Services.prefs.setCharPref(PREF_ROOT + "whitelist.add.EMPTY", "");
Services.prefs.setCharPref(PREF_ROOT + "whitelist.add.EMPTY2", ",");
Services.prefs.setCharPref(
PREF_ROOT + "whitelist.add.TEST",
);
Services.prefs.setCharPref(
PREF_ROOT + "whitelist.add.TEST2",
);
Services.prefs.setCharPref(
PREF_ROOT + "whitelist.add.TEST3",
"whitelist3-1.example.com,about:config"
); // legacy style - host only
Services.prefs.setCharPref(PREF_ROOT + "blacklist.add.EMPTY", "");
Services.prefs.setCharPref(
PREF_ROOT + "blacklist.add.TEST",
);
Services.prefs.setCharPref(
PREF_ROOT + "blacklist.add.TEST2",
);
Services.prefs.setCharPref(
PREF_ROOT + "blacklist.add.TEST3",
"blacklist3-1.example.com,about:preferences"
); // legacy style - host only
// Check they are unknown in the permission manager prior to importing.
let whitelisted = [
"about:config",
"about:home",
];
let blacklisted = [
"about:preferences",
"about:mozilla",
];
let untouched = [
];
let unknown = whitelisted.concat(blacklisted).concat(untouched);
for (let url of unknown) {
let uri = Services.io.newURI(url);
Assert.equal(
PermissionTestUtils.testPermission(uri, TEST_PERM),
Services.perms.UNKNOWN_ACTION
);
}
// Import them
PermissionsUtils.importFromPrefs(PREF_ROOT, TEST_PERM);
// Get list of preferences to check
let preferences = Services.prefs.getChildList(PREF_ROOT);
// Check preferences were emptied
for (let pref of preferences) {
Assert.equal(Services.prefs.getCharPref(pref), "");
}
// Check they were imported into the permissions manager
for (let url of whitelisted) {
let uri = Services.io.newURI(url);
Assert.equal(
PermissionTestUtils.testPermission(uri, TEST_PERM),
Services.perms.ALLOW_ACTION
);
}
for (let url of blacklisted) {
let uri = Services.io.newURI(url);
Assert.equal(
PermissionTestUtils.testPermission(uri, TEST_PERM),
Services.perms.DENY_ACTION
);
}
for (let url of untouched) {
let uri = Services.io.newURI(url);
Assert.equal(
PermissionTestUtils.testPermission(uri, TEST_PERM),
Services.perms.UNKNOWN_ACTION
);
}
}