summaryrefslogtreecommitdiffstats
path: root/extensions/permissions/test/unit/test_permmanager_matchesuri.js
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-28 14:29:10 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-28 14:29:10 +0000
commit2aa4a82499d4becd2284cdb482213d541b8804dd (patch)
treeb80bf8bf13c3766139fbacc530efd0dd9d54394c /extensions/permissions/test/unit/test_permmanager_matchesuri.js
parentInitial commit. (diff)
downloadfirefox-2aa4a82499d4becd2284cdb482213d541b8804dd.tar.xz
firefox-2aa4a82499d4becd2284cdb482213d541b8804dd.zip
Adding upstream version 86.0.1.upstream/86.0.1upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'extensions/permissions/test/unit/test_permmanager_matchesuri.js')
-rw-r--r--extensions/permissions/test/unit/test_permmanager_matchesuri.js252
1 files changed, 252 insertions, 0 deletions
diff --git a/extensions/permissions/test/unit/test_permmanager_matchesuri.js b/extensions/permissions/test/unit/test_permmanager_matchesuri.js
new file mode 100644
index 0000000000..1218fbf9ca
--- /dev/null
+++ b/extensions/permissions/test/unit/test_permmanager_matchesuri.js
@@ -0,0 +1,252 @@
+/* Any copyright is dedicated to the Public Domain.
+ http://creativecommons.org/publicdomain/zero/1.0/ */
+
+function matches_always(perm, uris) {
+ uris.forEach(uri => {
+ Assert.ok(
+ perm.matchesURI(uri, true),
+ "perm: " + perm.principal.origin + ", URI: " + uri.spec
+ );
+ Assert.ok(
+ perm.matchesURI(uri, false),
+ "perm: " + perm.principal.origin + ", URI: " + uri.spec
+ );
+ });
+}
+
+function matches_weak(perm, uris) {
+ uris.forEach(uri => {
+ Assert.ok(
+ !perm.matchesURI(uri, true),
+ "perm: " + perm.principal.origin + ", URI: " + uri.spec
+ );
+ Assert.ok(
+ perm.matchesURI(uri, false),
+ "perm: " + perm.principal.origin + ", URI: " + uri.spec
+ );
+ });
+}
+
+function matches_never(perm, uris) {
+ uris.forEach(uri => {
+ Assert.ok(
+ !perm.matchesURI(uri, true),
+ "perm: " + perm.principal.origin + ", URI: " + uri.spec
+ );
+ Assert.ok(
+ !perm.matchesURI(uri, false),
+ "perm: " + perm.principal.origin + ", URI: " + uri.spec
+ );
+ });
+}
+
+function mk_permission(uri) {
+ let pm = Services.perms;
+
+ let secMan = Services.scriptSecurityManager;
+
+ // Get the permission from the principal!
+ let principal = secMan.createContentPrincipal(uri, {});
+
+ pm.addFromPrincipal(principal, "test/matchesuri", pm.ALLOW_ACTION);
+ let permission = pm.getPermissionObject(principal, "test/matchesuri", true);
+
+ return permission;
+}
+
+function run_test() {
+ // initialize the permission manager service
+ let pm = Services.perms;
+
+ let fileprefix = "file:///";
+ if (Services.appinfo.OS == "WINNT") {
+ // Windows rejects files if they don't have a drive. See Bug 1180870
+ fileprefix += "c:/";
+ }
+
+ // Add some permissions
+ let uri0 = NetUtil.newURI("http://google.com:9091/just/a/path");
+ let uri1 = NetUtil.newURI("http://hangouts.google.com:9091/some/path");
+ let uri2 = NetUtil.newURI("http://google.com:9091/");
+ let uri3 = NetUtil.newURI("http://google.org:9091/");
+ let uri4 = NetUtil.newURI("http://deeper.hangouts.google.com:9091/");
+ let uri5 = NetUtil.newURI("https://google.com/just/a/path");
+ let uri6 = NetUtil.newURI("https://hangouts.google.com");
+ let uri7 = NetUtil.newURI("https://google.com/");
+
+ let fileuri1 = NetUtil.newURI(fileprefix + "a/file/path");
+ let fileuri2 = NetUtil.newURI(fileprefix + "a/file/path/deeper");
+ let fileuri3 = NetUtil.newURI(fileprefix + "a/file/otherpath");
+
+ {
+ let perm = mk_permission(uri0);
+ matches_always(perm, [uri0, uri2]);
+ matches_weak(perm, [uri1, uri4]);
+ matches_never(perm, [uri3, uri5, uri6, uri7, fileuri1, fileuri2, fileuri3]);
+ }
+
+ {
+ let perm = mk_permission(uri1);
+ matches_always(perm, [uri1]);
+ matches_weak(perm, [uri4]);
+ matches_never(perm, [
+ uri0,
+ uri2,
+ uri3,
+ uri5,
+ uri6,
+ uri7,
+ fileuri1,
+ fileuri2,
+ fileuri3,
+ ]);
+ }
+
+ {
+ let perm = mk_permission(uri2);
+ matches_always(perm, [uri0, uri2]);
+ matches_weak(perm, [uri1, uri4]);
+ matches_never(perm, [uri3, uri5, uri6, uri7, fileuri1, fileuri2, fileuri3]);
+ }
+
+ {
+ let perm = mk_permission(uri3);
+ matches_always(perm, [uri3]);
+ matches_weak(perm, []);
+ matches_never(perm, [
+ uri1,
+ uri2,
+ uri4,
+ uri5,
+ uri6,
+ uri7,
+ fileuri1,
+ fileuri2,
+ fileuri3,
+ ]);
+ }
+
+ {
+ let perm = mk_permission(uri4);
+ matches_always(perm, [uri4]);
+ matches_weak(perm, []);
+ matches_never(perm, [
+ uri1,
+ uri2,
+ uri3,
+ uri5,
+ uri6,
+ uri7,
+ fileuri1,
+ fileuri2,
+ fileuri3,
+ ]);
+ }
+
+ {
+ let perm = mk_permission(uri5);
+ matches_always(perm, [uri5, uri7]);
+ matches_weak(perm, [uri6]);
+ matches_never(perm, [
+ uri0,
+ uri1,
+ uri2,
+ uri3,
+ uri4,
+ fileuri1,
+ fileuri2,
+ fileuri3,
+ ]);
+ }
+
+ {
+ let perm = mk_permission(uri6);
+ matches_always(perm, [uri6]);
+ matches_weak(perm, []);
+ matches_never(perm, [
+ uri0,
+ uri1,
+ uri2,
+ uri3,
+ uri4,
+ uri5,
+ uri7,
+ fileuri1,
+ fileuri2,
+ fileuri3,
+ ]);
+ }
+
+ {
+ let perm = mk_permission(uri7);
+ matches_always(perm, [uri5, uri7]);
+ matches_weak(perm, [uri6]);
+ matches_never(perm, [
+ uri0,
+ uri1,
+ uri2,
+ uri3,
+ uri4,
+ fileuri1,
+ fileuri2,
+ fileuri3,
+ ]);
+ }
+
+ {
+ let perm = mk_permission(fileuri1);
+ matches_always(perm, [fileuri1]);
+ matches_weak(perm, []);
+ matches_never(perm, [
+ uri0,
+ uri1,
+ uri2,
+ uri3,
+ uri4,
+ uri5,
+ uri6,
+ uri7,
+ fileuri2,
+ fileuri3,
+ ]);
+ }
+
+ {
+ let perm = mk_permission(fileuri2);
+ matches_always(perm, [fileuri2]);
+ matches_weak(perm, []);
+ matches_never(perm, [
+ uri0,
+ uri1,
+ uri2,
+ uri3,
+ uri4,
+ uri5,
+ uri6,
+ uri7,
+ fileuri1,
+ fileuri3,
+ ]);
+ }
+
+ {
+ let perm = mk_permission(fileuri3);
+ matches_always(perm, [fileuri3]);
+ matches_weak(perm, []);
+ matches_never(perm, [
+ uri0,
+ uri1,
+ uri2,
+ uri3,
+ uri4,
+ uri5,
+ uri6,
+ uri7,
+ fileuri1,
+ fileuri2,
+ ]);
+ }
+
+ // Clean up!
+ pm.removeAll();
+}