tor-browser

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

test_permmanager_getPermissionObject.js (3827B)


      1 /* Any copyright is dedicated to the Public Domain.
      2   http://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 function getPrincipalFromURI(aURI) {
      5  let ssm = Services.scriptSecurityManager;
      6  let uri = NetUtil.newURI(aURI);
      7  return ssm.createContentPrincipal(uri, {});
      8 }
      9 
     10 function getSystemPrincipal() {
     11  return Services.scriptSecurityManager.getSystemPrincipal();
     12 }
     13 
     14 function run_test() {
     15  var pm = Services.perms;
     16 
     17  Assert.equal(
     18    null,
     19    pm.getPermissionObject(getSystemPrincipal(), "test/pobject", false)
     20  );
     21 
     22  let principal = getPrincipalFromURI("http://example.com");
     23  let subPrincipal = getPrincipalFromURI("http://sub.example.com");
     24  let subSubPrincipal = getPrincipalFromURI("http://sub.sub.example.com");
     25 
     26  Assert.equal(null, pm.getPermissionObject(principal, "test/pobject", false));
     27  Assert.equal(null, pm.getPermissionObject(principal, "test/pobject", true));
     28 
     29  pm.addFromPrincipal(principal, "test/pobject", pm.ALLOW_ACTION);
     30  var rootPerm = pm.getPermissionObject(principal, "test/pobject", false);
     31  Assert.notEqual(rootPerm, null);
     32  Assert.equal(rootPerm.principal.origin, "http://example.com");
     33  Assert.equal(rootPerm.type, "test/pobject");
     34  Assert.equal(rootPerm.capability, pm.ALLOW_ACTION);
     35  Assert.equal(rootPerm.expireType, pm.EXPIRE_NEVER);
     36 
     37  Assert.notEqual(rootPerm, null);
     38  Assert.equal(rootPerm.principal.origin, "http://example.com");
     39 
     40  var subPerm = pm.getPermissionObject(subPrincipal, "test/pobject", true);
     41  Assert.equal(null, subPerm);
     42  subPerm = pm.getPermissionObject(subPrincipal, "test/pobject", false);
     43  Assert.notEqual(subPerm, null);
     44  Assert.equal(subPerm.principal.origin, "http://example.com");
     45  Assert.equal(subPerm.type, "test/pobject");
     46  Assert.equal(subPerm.capability, pm.ALLOW_ACTION);
     47 
     48  subPerm = pm.getPermissionObject(subSubPrincipal, "test/pobject", true);
     49  Assert.equal(null, subPerm);
     50  subPerm = pm.getPermissionObject(subSubPrincipal, "test/pobject", false);
     51  Assert.notEqual(subPerm, null);
     52  Assert.equal(subPerm.principal.origin, "http://example.com");
     53 
     54  pm.addFromPrincipal(
     55    principal,
     56    "test/pobject",
     57    pm.DENY_ACTION,
     58    pm.EXPIRE_SESSION
     59  );
     60 
     61  // make sure permission objects are not dynamic
     62  Assert.equal(rootPerm.capability, pm.ALLOW_ACTION);
     63 
     64  // but do update on change
     65  rootPerm = pm.getPermissionObject(principal, "test/pobject", true);
     66  Assert.equal(rootPerm.capability, pm.DENY_ACTION);
     67  Assert.equal(rootPerm.expireType, pm.EXPIRE_SESSION);
     68 
     69  subPerm = pm.getPermissionObject(subPrincipal, "test/pobject", false);
     70  Assert.equal(subPerm.principal.origin, "http://example.com");
     71  Assert.equal(subPerm.capability, pm.DENY_ACTION);
     72  Assert.equal(subPerm.expireType, pm.EXPIRE_SESSION);
     73 
     74  pm.addFromPrincipal(subPrincipal, "test/pobject", pm.PROMPT_ACTION);
     75  rootPerm = pm.getPermissionObject(principal, "test/pobject", true);
     76  Assert.equal(rootPerm.principal.origin, "http://example.com");
     77  Assert.equal(rootPerm.capability, pm.DENY_ACTION);
     78 
     79  subPerm = pm.getPermissionObject(subPrincipal, "test/pobject", true);
     80  Assert.equal(subPerm.principal.origin, "http://sub.example.com");
     81  Assert.equal(subPerm.capability, pm.PROMPT_ACTION);
     82 
     83  subPerm = pm.getPermissionObject(subPrincipal, "test/pobject", false);
     84  Assert.equal(subPerm.principal.origin, "http://sub.example.com");
     85  Assert.equal(subPerm.capability, pm.PROMPT_ACTION);
     86 
     87  subPerm = pm.getPermissionObject(subSubPrincipal, "test/pobject", true);
     88  Assert.equal(null, subPerm);
     89 
     90  subPerm = pm.getPermissionObject(subSubPrincipal, "test/pobject", false);
     91  Assert.equal(subPerm.principal.origin, "http://sub.example.com");
     92  Assert.equal(subPerm.capability, pm.PROMPT_ACTION);
     93 
     94  pm.removeFromPrincipal(principal, "test/pobject");
     95 
     96  rootPerm = pm.getPermissionObject(principal, "test/pobject", true);
     97  Assert.equal(null, rootPerm);
     98 }