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 PerrmissionsUtils.jsm works as expected, including: michael@0: // * PermissionsUtils.importfromPrefs() michael@0: // .[whitelist|blacklist].add preferences are emptied when michael@0: // converted into permissions on startup. michael@0: michael@0: michael@0: const PREF_ROOT = "testpermissions."; michael@0: const TEST_PERM = "text-permission"; michael@0: michael@0: Components.utils.import("resource://gre/modules/Services.jsm"); michael@0: Components.utils.import("resource://gre/modules/PermissionsUtils.jsm"); michael@0: michael@0: function run_test() { michael@0: test_importfromPrefs(); michael@0: } michael@0: michael@0: michael@0: function test_importfromPrefs() { michael@0: // Create own preferences to test michael@0: Services.prefs.setCharPref(PREF_ROOT + "whitelist.add.EMPTY", ""); michael@0: Services.prefs.setCharPref(PREF_ROOT + "whitelist.add.EMPTY2", ","); michael@0: Services.prefs.setCharPref(PREF_ROOT + "whitelist.add.TEST", "whitelist.example.com"); michael@0: Services.prefs.setCharPref(PREF_ROOT + "whitelist.add.TEST2", "whitelist2-1.example.com,whitelist2-2.example.com"); michael@0: Services.prefs.setCharPref(PREF_ROOT + "blacklist.add.EMPTY", ""); michael@0: Services.prefs.setCharPref(PREF_ROOT + "blacklist.add.TEST", "blacklist.example.com,"); michael@0: Services.prefs.setCharPref(PREF_ROOT + "blacklist.add.TEST2", ",blacklist2-1.example.com,blacklist2-2.example.com"); michael@0: michael@0: // Import them michael@0: PermissionsUtils.importFromPrefs(PREF_ROOT, TEST_PERM); michael@0: michael@0: // Get list of preferences to check michael@0: let preferences = Services.prefs.getChildList(PREF_ROOT, {}); michael@0: michael@0: // Check preferences were emptied michael@0: for (let pref of preferences) { michael@0: do_check_eq(Services.prefs.getCharPref(pref), ""); michael@0: } michael@0: michael@0: // Check they were imported into the permissions manager michael@0: let whitelisted = ["whitelist.example.com", michael@0: "whitelist2-1.example.com", michael@0: "whitelist2-2.example.com"]; michael@0: let blacklisted = ["blacklist.example.com", michael@0: "blacklist2-1.example.com", michael@0: "blacklist2-2.example.com"]; michael@0: for (let url of whitelisted) { michael@0: let uri = Services.io.newURI("http://" + url, null, null); michael@0: do_check_eq(Services.perms.testPermission(uri, TEST_PERM), Services.perms.ALLOW_ACTION); michael@0: } michael@0: for (let url of blacklisted) { michael@0: let uri = Services.io.newURI("http://" + url, null, null); michael@0: do_check_eq(Services.perms.testPermission(uri, TEST_PERM), Services.perms.DENY_ACTION); michael@0: } michael@0: }