michael@0: /* Any copyright is dedicated to the Public Domain. michael@0: * http://creativecommons.org/publicdomain/zero/1.0/ michael@0: */ michael@0: michael@0: // Tests that xpinstall.[whitelist|blacklist].add preferences are emptied when michael@0: // converted into permissions. michael@0: michael@0: const PREF_XPI_WHITELIST_PERMISSIONS = "xpinstall.whitelist.add"; michael@0: const PREF_XPI_BLACKLIST_PERMISSIONS = "xpinstall.blacklist.add"; michael@0: michael@0: function do_check_permission_prefs(preferences) { michael@0: // Check preferences were emptied michael@0: for (let pref of preferences) { michael@0: try { michael@0: do_check_eq(Services.prefs.getCharPref(pref), ""); michael@0: } michael@0: catch (e) { michael@0: // Successfully emptied michael@0: } michael@0: } michael@0: } michael@0: michael@0: function clear_imported_preferences_cache() { michael@0: let scope = Components.utils.import("resource://gre/modules/PermissionsUtils.jsm", {}); michael@0: scope.gImportedPrefBranches.clear(); michael@0: } michael@0: michael@0: function run_test() { michael@0: createAppInfo("xpcshell@tests.mozilla.org", "XPCShell", "1", "1.9"); michael@0: michael@0: // Create own preferences to test michael@0: Services.prefs.setCharPref("xpinstall.whitelist.add.EMPTY", ""); michael@0: Services.prefs.setCharPref("xpinstall.whitelist.add.TEST", "whitelist.example.com"); michael@0: Services.prefs.setCharPref("xpinstall.blacklist.add.EMPTY", ""); michael@0: Services.prefs.setCharPref("xpinstall.blacklist.add.TEST", "blacklist.example.com"); michael@0: michael@0: // Get list of preferences to check michael@0: var whitelistPreferences = Services.prefs.getChildList(PREF_XPI_WHITELIST_PERMISSIONS, {}); michael@0: var blacklistPreferences = Services.prefs.getChildList(PREF_XPI_BLACKLIST_PERMISSIONS, {}); michael@0: var preferences = whitelistPreferences.concat(blacklistPreferences); michael@0: michael@0: startupManager(); michael@0: michael@0: // Permissions are imported lazily - act as thought we're checking an install, michael@0: // to trigger on-deman importing of the permissions. michael@0: let url = Services.io.newURI("http://example.com/file.xpi", null, null); michael@0: AddonManager.isInstallAllowed("application/x-xpinstall", url); michael@0: do_check_permission_prefs(preferences); michael@0: michael@0: michael@0: // Import can also be triggerred by an observer notification by any other area michael@0: // of code, such as a permissions management UI. michael@0: michael@0: // First, request to flush all permissions michael@0: clear_imported_preferences_cache(); michael@0: Services.prefs.setCharPref("xpinstall.whitelist.add.TEST2", "whitelist2.example.com"); michael@0: Services.obs.notifyObservers(null, "flush-pending-permissions", "install"); michael@0: do_check_permission_prefs(preferences); michael@0: michael@0: // Then, request to flush just install permissions michael@0: clear_imported_preferences_cache(); michael@0: Services.prefs.setCharPref("xpinstall.whitelist.add.TEST3", "whitelist3.example.com"); michael@0: Services.obs.notifyObservers(null, "flush-pending-permissions", ""); michael@0: do_check_permission_prefs(preferences); michael@0: michael@0: // And a request to flush some other permissions sholdn't flush install permissions michael@0: clear_imported_preferences_cache(); michael@0: Services.prefs.setCharPref("xpinstall.whitelist.add.TEST4", "whitelist4.example.com"); michael@0: Services.obs.notifyObservers(null, "flush-pending-permissions", "lolcats"); michael@0: do_check_eq(Services.prefs.getCharPref("xpinstall.whitelist.add.TEST4"), "whitelist4.example.com"); michael@0: }