summaryrefslogtreecommitdiffstats
path: root/extensions/permissions/test/unit/test_permmanager_expiration.js
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 17:32:43 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 17:32:43 +0000
commit6bf0a5cb5034a7e684dcc3500e841785237ce2dd (patch)
treea68f146d7fa01f0134297619fbe7e33db084e0aa /extensions/permissions/test/unit/test_permmanager_expiration.js
parentInitial commit. (diff)
downloadthunderbird-6bf0a5cb5034a7e684dcc3500e841785237ce2dd.tar.xz
thunderbird-6bf0a5cb5034a7e684dcc3500e841785237ce2dd.zip
Adding upstream version 1:115.7.0.upstream/1%115.7.0upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'extensions/permissions/test/unit/test_permmanager_expiration.js')
-rw-r--r--extensions/permissions/test/unit/test_permmanager_expiration.js189
1 files changed, 189 insertions, 0 deletions
diff --git a/extensions/permissions/test/unit/test_permmanager_expiration.js b/extensions/permissions/test/unit/test_permmanager_expiration.js
new file mode 100644
index 0000000000..24ff366730
--- /dev/null
+++ b/extensions/permissions/test/unit/test_permmanager_expiration.js
@@ -0,0 +1,189 @@
+/* Any copyright is dedicated to the Public Domain.
+ http://creativecommons.org/publicdomain/zero/1.0/ */
+
+// Test that permissions with specific expiry times behave as expected.
+var test_generator = do_run_test();
+
+function run_test() {
+ do_test_pending();
+ test_generator.next();
+}
+
+function continue_test() {
+ do_run_generator(test_generator);
+}
+
+function* do_run_test() {
+ let pm = Services.perms;
+ let permURI = NetUtil.newURI("http://example.com");
+ let principal = Services.scriptSecurityManager.createContentPrincipal(
+ permURI,
+ {}
+ );
+
+ let now = Number(Date.now());
+
+ // add a permission with *now* expiration
+ pm.addFromPrincipal(
+ principal,
+ "test/expiration-perm-exp",
+ 1,
+ pm.EXPIRE_TIME,
+ now
+ );
+ pm.addFromPrincipal(
+ principal,
+ "test/expiration-session-exp",
+ 1,
+ pm.EXPIRE_SESSION,
+ now
+ );
+
+ // add a permission with future expiration (100 milliseconds)
+ pm.addFromPrincipal(
+ principal,
+ "test/expiration-perm-exp2",
+ 1,
+ pm.EXPIRE_TIME,
+ now + 100
+ );
+ pm.addFromPrincipal(
+ principal,
+ "test/expiration-session-exp2",
+ 1,
+ pm.EXPIRE_SESSION,
+ now + 100
+ );
+
+ // add a permission with future expiration (1000 seconds)
+ pm.addFromPrincipal(
+ principal,
+ "test/expiration-perm-exp3",
+ 1,
+ pm.EXPIRE_TIME,
+ now + 1e6
+ );
+ pm.addFromPrincipal(
+ principal,
+ "test/expiration-session-exp3",
+ 1,
+ pm.EXPIRE_SESSION,
+ now + 1e6
+ );
+
+ // add a permission without expiration
+ pm.addFromPrincipal(
+ principal,
+ "test/expiration-perm-nexp",
+ 1,
+ pm.EXPIRE_NEVER,
+ 0
+ );
+
+ // check that the second two haven't expired yet
+ Assert.equal(
+ 1,
+ pm.testPermissionFromPrincipal(principal, "test/expiration-perm-exp3")
+ );
+ Assert.equal(
+ 1,
+ pm.testPermissionFromPrincipal(principal, "test/expiration-session-exp3")
+ );
+ Assert.equal(
+ 1,
+ pm.testPermissionFromPrincipal(principal, "test/expiration-perm-nexp")
+ );
+ Assert.equal(1, pm.getAllWithTypePrefix("test/expiration-perm-exp3").length);
+ Assert.equal(
+ 1,
+ pm.getAllWithTypePrefix("test/expiration-session-exp3").length
+ );
+ Assert.equal(1, pm.getAllWithTypePrefix("test/expiration-perm-nexp").length);
+ Assert.equal(5, pm.getAllForPrincipal(principal).length);
+
+ // ... and the first one has
+ do_timeout(10, continue_test);
+ yield;
+ Assert.equal(
+ 0,
+ pm.testPermissionFromPrincipal(principal, "test/expiration-perm-exp")
+ );
+ Assert.equal(
+ 0,
+ pm.testPermissionFromPrincipal(principal, "test/expiration-session-exp")
+ );
+
+ // ... and that the short-term one will
+ do_timeout(200, continue_test);
+ yield;
+ Assert.equal(
+ 0,
+ pm.testPermissionFromPrincipal(principal, "test/expiration-perm-exp2")
+ );
+ Assert.equal(
+ 0,
+ pm.testPermissionFromPrincipal(principal, "test/expiration-session-exp2")
+ );
+ Assert.equal(0, pm.getAllWithTypePrefix("test/expiration-perm-exp2").length);
+ Assert.equal(
+ 0,
+ pm.getAllWithTypePrefix("test/expiration-session-exp2").length
+ );
+
+ Assert.equal(3, pm.getAllForPrincipal(principal).length);
+
+ // Check that .getPermission returns a matching result
+ Assert.equal(
+ null,
+ pm.getPermissionObject(principal, "test/expiration-perm-exp", false)
+ );
+ Assert.equal(
+ null,
+ pm.getPermissionObject(principal, "test/expiration-session-exp", false)
+ );
+ Assert.equal(
+ null,
+ pm.getPermissionObject(principal, "test/expiration-perm-exp2", false)
+ );
+ Assert.equal(
+ null,
+ pm.getPermissionObject(principal, "test/expiration-session-exp2", false)
+ );
+
+ // Add a persistent permission for private browsing
+ let principalPB = Services.scriptSecurityManager.createContentPrincipal(
+ permURI,
+ { privateBrowsingId: 1 }
+ );
+ pm.addFromPrincipal(
+ principalPB,
+ "test/expiration-session-pb",
+ pm.ALLOW_ACTION
+ );
+
+ // The permission should be set to session expiry
+ let perm = pm.getPermissionObject(
+ principalPB,
+ "test/expiration-session-pb",
+ true
+ );
+ Assert.equal(perm.expireType, pm.EXPIRE_SESSION);
+
+ // Add a persistent permission for private browsing using
+ // addFromPrincipalAndPersistInPrivateBrowsing
+ pm.addFromPrincipalAndPersistInPrivateBrowsing(
+ principalPB,
+ "test/expiration-session-pb",
+ pm.ALLOW_ACTION
+ );
+
+ // The permission should be set to never expire
+ perm = pm.getPermissionObject(
+ principalPB,
+ "test/expiration-session-pb",
+ true
+ );
+ Assert.equal(perm.expireType, pm.EXPIRE_NEVER);
+
+ do_finish_generator_test(test_generator);
+}