tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

test_permmanager_getAllForPrincipal.js (2343B)


      1 /* Any copyright is dedicated to the Public Domain.
      2   http://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 function check_enumerator(principal, permissions) {
      5  let perms = Services.perms.getAllForPrincipal(principal);
      6  for (let [type, capability] of permissions) {
      7    let perm = perms.shift();
      8    Assert.notEqual(perm, null);
      9    Assert.equal(perm.type, type);
     10    Assert.equal(perm.capability, capability);
     11    Assert.equal(perm.expireType, Services.perms.EXPIRE_NEVER);
     12  }
     13  Assert.ok(!perms.length);
     14 }
     15 
     16 function run_test() {
     17  let pm = Services.perms;
     18 
     19  let principal =
     20    Services.scriptSecurityManager.createContentPrincipalFromOrigin(
     21      "http://example.com"
     22    );
     23  let subPrincipal =
     24    Services.scriptSecurityManager.createContentPrincipalFromOrigin(
     25      "http://sub.example.com"
     26    );
     27 
     28  check_enumerator(principal, []);
     29 
     30  pm.addFromPrincipal(principal, "test/getallforuri", pm.ALLOW_ACTION);
     31  check_enumerator(principal, [["test/getallforuri", pm.ALLOW_ACTION]]);
     32 
     33  // check that uris are matched exactly
     34  check_enumerator(subPrincipal, []);
     35 
     36  pm.addFromPrincipal(subPrincipal, "test/getallforuri", pm.PROMPT_ACTION);
     37  pm.addFromPrincipal(subPrincipal, "test/getallforuri2", pm.DENY_ACTION);
     38 
     39  check_enumerator(subPrincipal, [
     40    ["test/getallforuri", pm.PROMPT_ACTION],
     41    ["test/getallforuri2", pm.DENY_ACTION],
     42  ]);
     43 
     44  // check that the original uri list has not changed
     45  check_enumerator(principal, [["test/getallforuri", pm.ALLOW_ACTION]]);
     46 
     47  // check that UNKNOWN_ACTION permissions are ignored
     48  pm.addFromPrincipal(principal, "test/getallforuri2", pm.UNKNOWN_ACTION);
     49  pm.addFromPrincipal(principal, "test/getallforuri3", pm.DENY_ACTION);
     50 
     51  check_enumerator(principal, [
     52    ["test/getallforuri", pm.ALLOW_ACTION],
     53    ["test/getallforuri3", pm.DENY_ACTION],
     54  ]);
     55 
     56  // check that permission updates are reflected
     57  pm.addFromPrincipal(principal, "test/getallforuri", pm.PROMPT_ACTION);
     58 
     59  check_enumerator(principal, [
     60    ["test/getallforuri", pm.PROMPT_ACTION],
     61    ["test/getallforuri3", pm.DENY_ACTION],
     62  ]);
     63 
     64  // check that permission removals are reflected
     65  pm.removeFromPrincipal(principal, "test/getallforuri");
     66 
     67  check_enumerator(principal, [["test/getallforuri3", pm.DENY_ACTION]]);
     68 
     69  pm.removeAll();
     70  check_enumerator(principal, []);
     71  check_enumerator(subPrincipal, []);
     72 }