toolkit/modules/tests/xpcshell/test_PermissionsUtils.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

     1 /* Any copyright is dedicated to the Public Domain.
     2  * http://creativecommons.org/publicdomain/zero/1.0/
     3  */
     5 // Tests that PerrmissionsUtils.jsm works as expected, including:
     6 // * PermissionsUtils.importfromPrefs()
     7 //      <ROOT>.[whitelist|blacklist].add preferences are emptied when
     8 //       converted into permissions on startup.
    11 const PREF_ROOT = "testpermissions.";
    12 const TEST_PERM = "text-permission";
    14 Components.utils.import("resource://gre/modules/Services.jsm");
    15 Components.utils.import("resource://gre/modules/PermissionsUtils.jsm");
    17 function run_test() {
    18   test_importfromPrefs();
    19 }
    22 function test_importfromPrefs() {
    23   // Create own preferences to test
    24   Services.prefs.setCharPref(PREF_ROOT + "whitelist.add.EMPTY", "");
    25   Services.prefs.setCharPref(PREF_ROOT + "whitelist.add.EMPTY2", ",");
    26   Services.prefs.setCharPref(PREF_ROOT + "whitelist.add.TEST", "whitelist.example.com");
    27   Services.prefs.setCharPref(PREF_ROOT + "whitelist.add.TEST2", "whitelist2-1.example.com,whitelist2-2.example.com");
    28   Services.prefs.setCharPref(PREF_ROOT + "blacklist.add.EMPTY", "");
    29   Services.prefs.setCharPref(PREF_ROOT + "blacklist.add.TEST", "blacklist.example.com,");
    30   Services.prefs.setCharPref(PREF_ROOT + "blacklist.add.TEST2", ",blacklist2-1.example.com,blacklist2-2.example.com");
    32   // Import them
    33   PermissionsUtils.importFromPrefs(PREF_ROOT, TEST_PERM);
    35   // Get list of preferences to check
    36   let preferences = Services.prefs.getChildList(PREF_ROOT, {});
    38   // Check preferences were emptied
    39   for (let pref of preferences) {
    40     do_check_eq(Services.prefs.getCharPref(pref), "");
    41   }
    43   // Check they were imported into the permissions manager
    44   let whitelisted = ["whitelist.example.com",
    45                      "whitelist2-1.example.com",
    46                      "whitelist2-2.example.com"];
    47   let blacklisted = ["blacklist.example.com",
    48                      "blacklist2-1.example.com",
    49                      "blacklist2-2.example.com"];
    50   for (let url of whitelisted) {
    51     let uri = Services.io.newURI("http://" + url, null, null);
    52     do_check_eq(Services.perms.testPermission(uri, TEST_PERM), Services.perms.ALLOW_ACTION);
    53   }
    54   for (let url of blacklisted) {
    55     let uri = Services.io.newURI("http://" + url, null, null);
    56     do_check_eq(Services.perms.testPermission(uri, TEST_PERM), Services.perms.DENY_ACTION);
    57   }
    58 }

mercurial