tor-browser

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

test_browserGlue_migration_resetDefaults.js (3115B)


      1 /* Any copyright is dedicated to the Public Domain.
      2   http://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 const TOPIC_BROWSERGLUE_TEST = "browser-glue-test";
      5 const TOPICDATA_BROWSERGLUE_TEST = "force-ui-migration";
      6 const UI_VERSION = 138;
      7 
      8 const gBrowserGlue = Cc["@mozilla.org/browser/browserglue;1"].getService(
      9  Ci.nsIObserver
     10 );
     11 
     12 function checkConstraint(state, origin, type) {
     13  Assert.equal(
     14    state,
     15    Services.perms.testExactPermissionFromPrincipal(
     16      Services.scriptSecurityManager.createContentPrincipalFromOrigin(origin),
     17      type
     18    ),
     19    `${origin} of type ${type} was set to: ${state}`
     20  );
     21 }
     22 
     23 // Test to check if migration resets default permissions properly.
     24 add_task(async function test_resettingDefaults() {
     25  registerCleanupFunction(() => {
     26    Services.prefs.clearUserPref("browser.migration.version");
     27    Services.perms.removeAll();
     28  });
     29 
     30  Services.perms.removeAll();
     31  Services.prefs.setIntPref("browser.migration.version", UI_VERSION);
     32 
     33  let pm = Services.perms;
     34 
     35  // Origin infos for default permissions in the format [origin, type].
     36  let originInfos = [
     37    ["https://www.mozilla.org", "uitour"],
     38    ["https://support.mozilla.org", "uitour"],
     39    ["about:home", "uitour"],
     40    ["about:newtab", "uitour"],
     41    ["https://addons.mozilla.org", "install"],
     42    ["https://support.mozilla.org", "remote-troubleshooting"],
     43    ["about:welcome", "autoplay-media"],
     44  ];
     45 
     46  // Override all default permissions.
     47  for (let originInfo of originInfos) {
     48    pm.addFromPrincipal(
     49      Services.scriptSecurityManager.createContentPrincipalFromOrigin(
     50        originInfo[0]
     51      ),
     52      originInfo[1],
     53      pm.UNKNOWN_ACTION
     54    );
     55  }
     56 
     57  // Check if the default permissions were set to UNKNOWN_ACTION.
     58  for (let originInfo of originInfos) {
     59    checkConstraint(pm.UNKNOWN_ACTION, originInfo[0], originInfo[1]);
     60  }
     61 
     62  // Simulate a migration.
     63  gBrowserGlue.observe(
     64    null,
     65    TOPIC_BROWSERGLUE_TEST,
     66    TOPICDATA_BROWSERGLUE_TEST
     67  );
     68 
     69  // Check if the default permissions were reset.
     70  for (let originInfo of originInfos) {
     71    checkConstraint(pm.ALLOW_ACTION, originInfo[0], originInfo[1]);
     72  }
     73 });
     74 
     75 // Test to check if user set permissions don't get
     76 // reset during migration.
     77 add_task(async function test_resettingDenyAction() {
     78  registerCleanupFunction(() => {
     79    Services.prefs.clearUserPref("browser.migration.version");
     80    Services.perms.removeAll();
     81  });
     82 
     83  Services.perms.removeAll();
     84  Services.prefs.setIntPref("browser.migration.version", UI_VERSION);
     85 
     86  let pm = Services.perms;
     87  // Reset one default perm to DENY_ACTION.
     88  const origin = "https://www.mozilla.org";
     89  const type = "uitour";
     90 
     91  pm.addFromPrincipal(
     92    Services.scriptSecurityManager.createContentPrincipalFromOrigin(origin),
     93    type,
     94    pm.DENY_ACTION
     95  );
     96 
     97  // Check if permission was set correctly.
     98  checkConstraint(pm.DENY_ACTION, origin, type);
     99 
    100  // Simulate a migration.
    101  gBrowserGlue.observe(
    102    null,
    103    TOPIC_BROWSERGLUE_TEST,
    104    TOPICDATA_BROWSERGLUE_TEST
    105  );
    106 
    107  // We expect the permission to remain unchanged.
    108  checkConstraint(pm.DENY_ACTION, origin, type);
    109 });