tor-browser

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

test_popups_cookies_addons.js (3405B)


      1 /* Any copyright is dedicated to the Public Domain.
      2 * http://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 "use strict";
      5 
      6 add_task(async function test_setup_preexisting_permissions() {
      7  // Pre-existing ALLOW permissions that should be overriden
      8  // with DENY.
      9 
     10  // No ALLOW -> DENY override for popup and install permissions,
     11  // because their policies only supports the Allow parameter.
     12 
     13  PermissionTestUtils.add(
     14    "https://www.pre-existing-allow.com",
     15    "cookie",
     16    Ci.nsIPermissionManager.ALLOW_ACTION,
     17    Ci.nsIPermissionManager.EXPIRE_SESSION
     18  );
     19 
     20  // Pre-existing DENY permissions that should be overriden
     21  // with ALLOW.
     22  PermissionTestUtils.add(
     23    "https://www.pre-existing-deny.com",
     24    "popup",
     25    Ci.nsIPermissionManager.DENY_ACTION,
     26    Ci.nsIPermissionManager.EXPIRE_SESSION
     27  );
     28 
     29  PermissionTestUtils.add(
     30    "https://www.pre-existing-deny.com",
     31    "install",
     32    Ci.nsIPermissionManager.DENY_ACTION,
     33    Ci.nsIPermissionManager.EXPIRE_SESSION
     34  );
     35 
     36  PermissionTestUtils.add(
     37    "https://www.pre-existing-deny.com",
     38    "cookie",
     39    Ci.nsIPermissionManager.DENY_ACTION,
     40    Ci.nsIPermissionManager.EXPIRE_SESSION
     41  );
     42 });
     43 
     44 add_task(async function test_setup_activate_policies() {
     45  await setupPolicyEngineWithJson("config_popups_cookies_addons.json");
     46  equal(
     47    Services.policies.status,
     48    Ci.nsIEnterprisePolicies.ACTIVE,
     49    "Engine is active"
     50  );
     51 });
     52 
     53 function checkPermission(url, expected, permissionName) {
     54  let expectedValue = Ci.nsIPermissionManager[`${expected}_ACTION`];
     55  let uri = Services.io.newURI(`https://www.${url}`);
     56 
     57  equal(
     58    PermissionTestUtils.testPermission(uri, permissionName),
     59    expectedValue,
     60    `Correct (${permissionName}=${expected}) for URL ${url}`
     61  );
     62 
     63  if (expected != "UNKNOWN") {
     64    let permission = PermissionTestUtils.getPermissionObject(
     65      uri,
     66      permissionName,
     67      true
     68    );
     69    ok(permission, "Permission object exists");
     70    equal(
     71      permission.expireType,
     72      Ci.nsIPermissionManager.EXPIRE_POLICY,
     73      "Permission expireType is correct"
     74    );
     75  }
     76 }
     77 
     78 function checkAllPermissionsForType(type, typeSupportsDeny = true) {
     79  checkPermission("allow.com", "ALLOW", type);
     80  checkPermission("unknown.com", "UNKNOWN", type);
     81  checkPermission("pre-existing-deny.com", "ALLOW", type);
     82 
     83  if (typeSupportsDeny) {
     84    checkPermission("deny.com", "DENY", type);
     85    checkPermission("pre-existing-allow.com", "DENY", type);
     86  }
     87 }
     88 
     89 add_task(async function test_popups_policy() {
     90  checkAllPermissionsForType("popup", false);
     91 });
     92 
     93 add_task(async function test_webextensions_policy() {
     94  checkAllPermissionsForType("install", false);
     95 });
     96 
     97 add_task(async function test_cookies_policy() {
     98  checkAllPermissionsForType("cookie");
     99 });
    100 
    101 add_task(async function test_change_permission() {
    102  // Checks that changing a permission will still retain the
    103  // value set through the engine.
    104  PermissionTestUtils.add(
    105    "https://www.allow.com",
    106    "cookie",
    107    Ci.nsIPermissionManager.DENY_ACTION,
    108    Ci.nsIPermissionManager.EXPIRE_SESSION
    109  );
    110 
    111  checkPermission("allow.com", "ALLOW", "cookie");
    112 
    113  // Also change one un-managed permission to make sure it doesn't
    114  // cause any problems to the policy engine or the permission manager.
    115  PermissionTestUtils.add(
    116    "https://www.unmanaged.com",
    117    "cookie",
    118    Ci.nsIPermissionManager.DENY_ACTION,
    119    Ci.nsIPermissionManager.EXPIRE_SESSION
    120  );
    121 });