diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-19 00:47:55 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-19 00:47:55 +0000 |
commit | 26a029d407be480d791972afb5975cf62c9360a6 (patch) | |
tree | f435a8308119effd964b339f76abb83a57c29483 /browser/components/tests/unit/test_browserGlue_migration_resetDefaults.js | |
parent | Initial commit. (diff) | |
download | firefox-26a029d407be480d791972afb5975cf62c9360a6.tar.xz firefox-26a029d407be480d791972afb5975cf62c9360a6.zip |
Adding upstream version 124.0.1.upstream/124.0.1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'browser/components/tests/unit/test_browserGlue_migration_resetDefaults.js')
-rw-r--r-- | browser/components/tests/unit/test_browserGlue_migration_resetDefaults.js | 109 |
1 files changed, 109 insertions, 0 deletions
diff --git a/browser/components/tests/unit/test_browserGlue_migration_resetDefaults.js b/browser/components/tests/unit/test_browserGlue_migration_resetDefaults.js new file mode 100644 index 0000000000..89de18d554 --- /dev/null +++ b/browser/components/tests/unit/test_browserGlue_migration_resetDefaults.js @@ -0,0 +1,109 @@ +/* Any copyright is dedicated to the Public Domain. + http://creativecommons.org/publicdomain/zero/1.0/ */ + +const TOPIC_BROWSERGLUE_TEST = "browser-glue-test"; +const TOPICDATA_BROWSERGLUE_TEST = "force-ui-migration"; +const UI_VERSION = 138; + +const gBrowserGlue = Cc["@mozilla.org/browser/browserglue;1"].getService( + Ci.nsIObserver +); + +function checkConstraint(state, origin, type) { + Assert.equal( + state, + Services.perms.testExactPermissionFromPrincipal( + Services.scriptSecurityManager.createContentPrincipalFromOrigin(origin), + type + ), + `${origin} of type ${type} was set to: ${state}` + ); +} + +// Test to check if migration resets default permissions properly. +add_task(async function test_resettingDefaults() { + registerCleanupFunction(() => { + Services.prefs.clearUserPref("browser.migration.version"); + Services.perms.removeAll(); + }); + + Services.perms.removeAll(); + Services.prefs.setIntPref("browser.migration.version", UI_VERSION); + + let pm = Services.perms; + + // Origin infos for default permissions in the format [origin, type]. + let originInfos = [ + ["https://www.mozilla.org", "uitour"], + ["https://support.mozilla.org", "uitour"], + ["about:home", "uitour"], + ["about:newtab", "uitour"], + ["https://addons.mozilla.org", "install"], + ["https://support.mozilla.org", "remote-troubleshooting"], + ["about:welcome", "autoplay-media"], + ]; + + // Override all default permissions. + for (let originInfo of originInfos) { + pm.addFromPrincipal( + Services.scriptSecurityManager.createContentPrincipalFromOrigin( + originInfo[0] + ), + originInfo[1], + pm.UNKNOWN_ACTION + ); + } + + // Check if the default permissions were set to UNKNOWN_ACTION. + for (let originInfo of originInfos) { + checkConstraint(pm.UNKNOWN_ACTION, originInfo[0], originInfo[1]); + } + + // Simulate a migration. + gBrowserGlue.observe( + null, + TOPIC_BROWSERGLUE_TEST, + TOPICDATA_BROWSERGLUE_TEST + ); + + // Check if the default permissions were reset. + for (let originInfo of originInfos) { + checkConstraint(pm.ALLOW_ACTION, originInfo[0], originInfo[1]); + } +}); + +// Test to check if user set permissions don't get +// reset during migration. +add_task(async function test_resettingDenyAction() { + registerCleanupFunction(() => { + Services.prefs.clearUserPref("browser.migration.version"); + Services.perms.removeAll(); + }); + + Services.perms.removeAll(); + Services.prefs.setIntPref("browser.migration.version", UI_VERSION); + + let pm = Services.perms; + // Reset one default perm to DENY_ACTION. + const origin = "https://www.mozilla.org"; + const type = "uitour"; + + pm.addFromPrincipal( + Services.scriptSecurityManager.createContentPrincipalFromOrigin(origin), + type, + pm.DENY_ACTION + ); + + // Check if permission was set correctly. + checkConstraint(pm.DENY_ACTION, origin, type); + + // Simulate a migration. + gBrowserGlue.observe( + null, + TOPIC_BROWSERGLUE_TEST, + TOPICDATA_BROWSERGLUE_TEST + ); + + // We expect the permission to remain unchanged. + checkConstraint(pm.DENY_ACTION, origin, type); +}); |