tor-browser

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

test_permmgr.js (3761B)


      1 // tests nsIPermissionManager
      2 
      3 "use strict";
      4 
      5 var hosts = [
      6  // format: [host, type, permission]
      7  ["http://mozilla.org", "cookie", 1],
      8  ["http://mozilla.org", "image", 2],
      9  ["http://mozilla.org", "popup", 3],
     10  ["http://mozilla.com", "cookie", 1],
     11  ["http://www.mozilla.com", "cookie", 2],
     12  ["http://dev.mozilla.com", "cookie", 3],
     13 ];
     14 
     15 var results = [
     16  // format: [host, type, testPermission result, testExactPermission result]
     17  // test defaults
     18  ["http://localhost", "cookie", 0, 0],
     19  ["http://spreadfirefox.com", "cookie", 0, 0],
     20  // test different types
     21  ["http://mozilla.org", "cookie", 1, 1],
     22  ["http://mozilla.org", "image", 2, 2],
     23  ["http://mozilla.org", "popup", 3, 3],
     24  // test subdomains
     25  ["http://www.mozilla.org", "cookie", 1, 0],
     26  ["http://www.dev.mozilla.org", "cookie", 1, 0],
     27  // test different permissions on subdomains
     28  ["http://mozilla.com", "cookie", 1, 1],
     29  ["http://www.mozilla.com", "cookie", 2, 2],
     30  ["http://dev.mozilla.com", "cookie", 3, 3],
     31  ["http://www.dev.mozilla.com", "cookie", 3, 0],
     32 ];
     33 
     34 function run_test() {
     35  Services.prefs.setCharPref("permissions.manager.defaultsUrl", "");
     36  var pm = Services.perms;
     37 
     38  var ioService = Services.io;
     39 
     40  var secMan = Services.scriptSecurityManager;
     41 
     42  // nsIPermissionManager implementation is an extension; don't fail if it's not there
     43  if (!pm) {
     44    return;
     45  }
     46 
     47  // put a few hosts in
     48  for (let i = 0; i < hosts.length; ++i) {
     49    let uri = ioService.newURI(hosts[i][0]);
     50    let principal = secMan.createContentPrincipal(uri, {});
     51 
     52    pm.addFromPrincipal(principal, hosts[i][1], hosts[i][2]);
     53  }
     54 
     55  // test the result
     56  for (let i = 0; i < results.length; ++i) {
     57    let uri = ioService.newURI(results[i][0]);
     58    let principal = secMan.createContentPrincipal(uri, {});
     59 
     60    Assert.equal(
     61      pm.testPermissionFromPrincipal(principal, results[i][1]),
     62      results[i][2]
     63    );
     64    Assert.equal(
     65      pm.testExactPermissionFromPrincipal(principal, results[i][1]),
     66      results[i][3]
     67    );
     68  }
     69 
     70  // test the all property ...
     71  var perms = pm.all;
     72  Assert.equal(perms.length, hosts.length);
     73 
     74  // ... remove all the hosts ...
     75  for (let j = 0; j < perms.length; ++j) {
     76    pm.removePermission(perms[j]);
     77  }
     78 
     79  // ... ensure each and every element is equal ...
     80  for (let i = 0; i < hosts.length; ++i) {
     81    for (let j = 0; j < perms.length; ++j) {
     82      if (
     83        perms[j].matchesURI(ioService.newURI(hosts[i][0]), true) &&
     84        hosts[i][1] == perms[j].type &&
     85        hosts[i][2] == perms[j].capability
     86      ) {
     87        perms.splice(j, 1);
     88        break;
     89      }
     90    }
     91  }
     92  Assert.equal(perms.length, 0);
     93 
     94  // ... and check the permmgr's empty
     95  Assert.equal(pm.all.length, 0);
     96 
     97  // test UTF8 normalization behavior: expect ASCII/ACE host encodings
     98  var utf8 = "b\u00FCcher.dolske.org"; // "bücher.dolske.org"
     99  var aceref = "xn--bcher-kva.dolske.org";
    100  var principal = secMan.createContentPrincipal(
    101    ioService.newURI("http://" + utf8),
    102    {}
    103  );
    104  pm.addFromPrincipal(principal, "utf8", 1);
    105  Assert.notEqual(Services.perms.all.length, 0);
    106  var ace = Services.perms.all[0];
    107  Assert.equal(ace.principal.asciiHost, aceref);
    108  Assert.equal(Services.perms.all.length > 1, false);
    109 
    110  // test removeAll()
    111  pm.removeAll();
    112  Assert.equal(Services.perms.all.length, 0);
    113 
    114  principal = secMan.createContentPrincipalFromOrigin(
    115    "https://www.example.com"
    116  );
    117  pm.addFromPrincipal(principal, "offline-app", pm.ALLOW_ACTION);
    118  // Remove existing entry.
    119  let perm = pm.getPermissionObject(principal, "offline-app", true);
    120  pm.removePermission(perm);
    121  // Try to remove already deleted entry.
    122  perm = pm.getPermissionObject(principal, "offline-app", true);
    123  pm.removePermission(perm);
    124  Assert.equal(Services.perms.all.length, 0);
    125 }