tor-browser

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

test_permmanager_cleardata.js (2070B)


      1 /* Any copyright is dedicated to the Public Domain.
      2   http://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 var pm;
      5 
      6 // Create a principal based on the { origin, originAttributes }.
      7 function createPrincipal(aOrigin, aOriginAttributes) {
      8  return Services.scriptSecurityManager.createContentPrincipal(
      9    NetUtil.newURI(aOrigin),
     10    aOriginAttributes
     11  );
     12 }
     13 
     14 function getData(aPattern) {
     15  return JSON.stringify(aPattern);
     16 }
     17 
     18 // Use aEntries to create principals, add permissions to them and check that they have them.
     19 // Then, it is removing origin attributes with the given aData and check if the permissions
     20 // of principals[i] matches the permission in aResults[i].
     21 function test(aEntries, aData, aResults) {
     22  let principals = [];
     23 
     24  for (const entry of aEntries) {
     25    principals.push(createPrincipal(entry.origin, entry.originAttributes));
     26  }
     27 
     28  for (const principal of principals) {
     29    Assert.equal(
     30      pm.testPermissionFromPrincipal(principal, "test/clear-origin"),
     31      pm.UNKNOWN_ACTION
     32    );
     33    pm.addFromPrincipal(
     34      principal,
     35      "test/clear-origin",
     36      pm.ALLOW_ACTION,
     37      pm.EXPIRE_NEVER,
     38      0
     39    );
     40    Assert.equal(
     41      pm.testPermissionFromPrincipal(principal, "test/clear-origin"),
     42      pm.ALLOW_ACTION
     43    );
     44  }
     45 
     46  // `clear-origin-attributes-data` notification is removed from permission
     47  // manager
     48  pm.removePermissionsWithAttributes(aData, [], []);
     49 
     50  var length = aEntries.length;
     51  for (let i = 0; i < length; ++i) {
     52    Assert.equal(
     53      pm.testPermissionFromPrincipal(principals[i], "test/clear-origin"),
     54      aResults[i]
     55    );
     56 
     57    // Remove allowed actions.
     58    if (aResults[i] == pm.ALLOW_ACTION) {
     59      pm.removeFromPrincipal(principals[i], "test/clear-origin");
     60    }
     61  }
     62 }
     63 
     64 function run_test() {
     65  do_get_profile();
     66 
     67  pm = Services.perms;
     68 
     69  let entries = [{ origin: "http://example.com", originAttributes: {} }];
     70 
     71  // In that case, all permissions should be removed.
     72  test(entries, getData({}), [
     73    pm.UNKNOWN_ACTION,
     74    pm.UNKNOWN_ACTION,
     75    pm.ALLOW_ACTION,
     76    pm.ALLOW_ACTION,
     77  ]);
     78 }