From 6bf0a5cb5034a7e684dcc3500e841785237ce2dd Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 7 Apr 2024 19:32:43 +0200 Subject: Adding upstream version 1:115.7.0. Signed-off-by: Daniel Baumann --- .../test/xpcshell/test_permissions_prefs.js | 99 ++++++++++++++++++++++ 1 file changed, 99 insertions(+) create mode 100644 toolkit/mozapps/extensions/test/xpcshell/test_permissions_prefs.js (limited to 'toolkit/mozapps/extensions/test/xpcshell/test_permissions_prefs.js') diff --git a/toolkit/mozapps/extensions/test/xpcshell/test_permissions_prefs.js b/toolkit/mozapps/extensions/test/xpcshell/test_permissions_prefs.js new file mode 100644 index 0000000000..d7bcaa038c --- /dev/null +++ b/toolkit/mozapps/extensions/test/xpcshell/test_permissions_prefs.js @@ -0,0 +1,99 @@ +/* Any copyright is dedicated to the Public Domain. + * http://creativecommons.org/publicdomain/zero/1.0/ + */ + +// Tests that xpinstall.[whitelist|blacklist].add preferences are emptied when +// converted into permissions. + +const PREF_XPI_WHITELIST_PERMISSIONS = "xpinstall.whitelist.add"; +const PREF_XPI_BLACKLIST_PERMISSIONS = "xpinstall.blacklist.add"; + +const { PermissionsTestUtils } = ChromeUtils.importESModule( + "resource://gre/modules/PermissionsUtils.sys.mjs" +); + +function newPrincipal(uri) { + return Services.scriptSecurityManager.createContentPrincipal( + NetUtil.newURI(uri), + {} + ); +} + +function do_check_permission_prefs(preferences) { + // Check preferences were emptied + for (let pref of preferences) { + try { + Assert.equal(Services.prefs.getCharPref(pref), ""); + } catch (e) { + // Successfully emptied + } + } +} + +add_task(async function setup() { + createAppInfo("xpcshell@tests.mozilla.org", "XPCShell", "1", "1.9"); + + // Create own preferences to test + Services.prefs.setCharPref("xpinstall.whitelist.add.EMPTY", ""); + Services.prefs.setCharPref( + "xpinstall.whitelist.add.TEST", + "http://whitelist.example.com" + ); + Services.prefs.setCharPref("xpinstall.blacklist.add.EMPTY", ""); + Services.prefs.setCharPref( + "xpinstall.blacklist.add.TEST", + "http://blacklist.example.com" + ); + + // Get list of preferences to check + var whitelistPreferences = Services.prefs.getChildList( + PREF_XPI_WHITELIST_PERMISSIONS + ); + var blacklistPreferences = Services.prefs.getChildList( + PREF_XPI_BLACKLIST_PERMISSIONS + ); + var preferences = whitelistPreferences.concat(blacklistPreferences); + + await promiseStartupManager(); + + // Permissions are imported lazily - act as thought we're checking an install, + // to trigger on-deman importing of the permissions. + AddonManager.isInstallAllowed( + "application/x-xpinstall", + newPrincipal("http://example.com/file.xpi") + ); + do_check_permission_prefs(preferences); + + // Import can also be triggered by an observer notification by any other area + // of code, such as a permissions management UI. + + // First, request to flush all permissions + PermissionsTestUtils.clearImportedPrefBranches(); + Services.prefs.setCharPref( + "xpinstall.whitelist.add.TEST2", + "https://whitelist2.example.com" + ); + Services.obs.notifyObservers(null, "flush-pending-permissions", "install"); + do_check_permission_prefs(preferences); + + // Then, request to flush just install permissions + PermissionsTestUtils.clearImportedPrefBranches(); + Services.prefs.setCharPref( + "xpinstall.whitelist.add.TEST3", + "https://whitelist3.example.com" + ); + Services.obs.notifyObservers(null, "flush-pending-permissions"); + do_check_permission_prefs(preferences); + + // And a request to flush some other permissions sholdn't flush install permissions + PermissionsTestUtils.clearImportedPrefBranches(); + Services.prefs.setCharPref( + "xpinstall.whitelist.add.TEST4", + "https://whitelist4.example.com" + ); + Services.obs.notifyObservers(null, "flush-pending-permissions", "lolcats"); + Assert.equal( + Services.prefs.getCharPref("xpinstall.whitelist.add.TEST4"), + "https://whitelist4.example.com" + ); +}); -- cgit v1.2.3