summaryrefslogtreecommitdiffstats
path: root/dom/quota/test/browser
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-19 01:47:29 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-19 01:47:29 +0000
commit0ebf5bdf043a27fd3dfb7f92e0cb63d88954c44d (patch)
treea31f07c9bcca9d56ce61e9a1ffd30ef350d513aa /dom/quota/test/browser
parentInitial commit. (diff)
downloadfirefox-esr-upstream/115.8.0esr.tar.xz
firefox-esr-upstream/115.8.0esr.zip
Adding upstream version 115.8.0esr.upstream/115.8.0esr
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to '')
-rw-r--r--dom/quota/test/browser/browser.ini13
-rw-r--r--dom/quota/test/browser/browser_permissionsCrossOrigin.js56
-rw-r--r--dom/quota/test/browser/browser_permissionsPromptAllow.js66
-rw-r--r--dom/quota/test/browser/browser_permissionsPromptDeny.js150
-rw-r--r--dom/quota/test/browser/browser_permissionsPromptUnknown.js53
-rw-r--r--dom/quota/test/browser/browser_simpledb.js51
-rw-r--r--dom/quota/test/browser/empty.html8
-rw-r--r--dom/quota/test/browser/head.js149
-rw-r--r--dom/quota/test/browser/helpers.js46
-rw-r--r--dom/quota/test/browser/permissionsPrompt.html34
10 files changed, 626 insertions, 0 deletions
diff --git a/dom/quota/test/browser/browser.ini b/dom/quota/test/browser/browser.ini
new file mode 100644
index 0000000000..d0c79f90a9
--- /dev/null
+++ b/dom/quota/test/browser/browser.ini
@@ -0,0 +1,13 @@
+[DEFAULT]
+skip-if = (buildapp != "browser")
+support-files =
+ head.js
+ helpers.js
+ empty.html
+ permissionsPrompt.html
+
+[browser_permissionsCrossOrigin.js]
+[browser_permissionsPromptAllow.js]
+[browser_permissionsPromptDeny.js]
+[browser_permissionsPromptUnknown.js]
+[browser_simpledb.js]
diff --git a/dom/quota/test/browser/browser_permissionsCrossOrigin.js b/dom/quota/test/browser/browser_permissionsCrossOrigin.js
new file mode 100644
index 0000000000..759eff73a3
--- /dev/null
+++ b/dom/quota/test/browser/browser_permissionsCrossOrigin.js
@@ -0,0 +1,56 @@
+/**
+ * Any copyright is dedicated to the Public Domain.
+ * http://creativecommons.org/publicdomain/zero/1.0/
+ */
+
+const emptyURL =
+ "https://example.com/browser/dom/quota/test/browser/empty.html";
+
+addTest(async function testNoPermissionPrompt() {
+ registerPopupEventHandler("popupshowing", function () {
+ ok(false, "Shouldn't show a popup this time");
+ });
+ registerPopupEventHandler("popupshown", function () {
+ ok(false, "Shouldn't show a popup this time");
+ });
+ registerPopupEventHandler("popuphidden", function () {
+ ok(false, "Shouldn't show a popup this time");
+ });
+
+ info("Creating tab");
+
+ await BrowserTestUtils.withNewTab(emptyURL, async function (browser) {
+ await new Promise(r => {
+ SpecialPowers.pushPrefEnv(
+ {
+ set: [
+ ["permissions.delegation.enabled", true],
+ ["dom.security.featurePolicy.header.enabled", true],
+ ["dom.security.featurePolicy.webidl.enabled", true],
+ ],
+ },
+ r
+ );
+ });
+
+ await SpecialPowers.spawn(browser, [], async function (host0) {
+ let frame = content.document.createElement("iframe");
+ // Cross origin src
+ frame.src = "https://example.org/browser/dom/quota/test/empty.html";
+ content.document.body.appendChild(frame);
+ await ContentTaskUtils.waitForEvent(frame, "load");
+
+ await content.SpecialPowers.spawn(frame, [], async function () {
+ // Request a permission.
+ const persistAllowed = await this.content.navigator.storage.persist();
+ Assert.ok(
+ !persistAllowed,
+ "navigator.storage.persist() has been denied"
+ );
+ });
+ content.document.body.removeChild(frame);
+ });
+ });
+
+ unregisterAllPopupEventHandlers();
+});
diff --git a/dom/quota/test/browser/browser_permissionsPromptAllow.js b/dom/quota/test/browser/browser_permissionsPromptAllow.js
new file mode 100644
index 0000000000..12cd0b7d79
--- /dev/null
+++ b/dom/quota/test/browser/browser_permissionsPromptAllow.js
@@ -0,0 +1,66 @@
+/**
+ * Any copyright is dedicated to the Public Domain.
+ * http://creativecommons.org/publicdomain/zero/1.0/
+ */
+
+const testPageURL =
+ "https://example.com/browser/dom/quota/test/browser/permissionsPrompt.html";
+
+addTest(async function testPermissionAllow() {
+ removePermission(testPageURL, "persistent-storage");
+
+ registerPopupEventHandler("popupshowing", function () {
+ ok(true, "prompt showing");
+ });
+ registerPopupEventHandler("popupshown", function () {
+ ok(true, "prompt shown");
+ triggerMainCommand(this);
+ });
+ registerPopupEventHandler("popuphidden", function () {
+ ok(true, "prompt hidden");
+ });
+
+ info("Creating tab");
+ gBrowser.selectedTab = BrowserTestUtils.addTab(gBrowser);
+
+ info("Loading test page: " + testPageURL);
+ BrowserTestUtils.loadURIString(gBrowser.selectedBrowser, testPageURL);
+ await waitForMessage(true, gBrowser);
+
+ is(
+ getPermission(testPageURL, "persistent-storage"),
+ Ci.nsIPermissionManager.ALLOW_ACTION,
+ "Correct permission set"
+ );
+ gBrowser.removeCurrentTab();
+ unregisterAllPopupEventHandlers();
+ // Keep persistent-storage permission for the next test.
+});
+
+addTest(async function testNoPermissionPrompt() {
+ registerPopupEventHandler("popupshowing", function () {
+ ok(false, "Shouldn't show a popup this time");
+ });
+ registerPopupEventHandler("popupshown", function () {
+ ok(false, "Shouldn't show a popup this time");
+ });
+ registerPopupEventHandler("popuphidden", function () {
+ ok(false, "Shouldn't show a popup this time");
+ });
+
+ info("Creating tab");
+ gBrowser.selectedTab = BrowserTestUtils.addTab(gBrowser);
+
+ info("Loading test page: " + testPageURL);
+ BrowserTestUtils.loadURIString(gBrowser.selectedBrowser, testPageURL);
+ await waitForMessage(true, gBrowser);
+
+ is(
+ getPermission(testPageURL, "persistent-storage"),
+ Ci.nsIPermissionManager.ALLOW_ACTION,
+ "Correct permission set"
+ );
+ gBrowser.removeCurrentTab();
+ unregisterAllPopupEventHandlers();
+ removePermission(testPageURL, "persistent-storage");
+});
diff --git a/dom/quota/test/browser/browser_permissionsPromptDeny.js b/dom/quota/test/browser/browser_permissionsPromptDeny.js
new file mode 100644
index 0000000000..6213e4519f
--- /dev/null
+++ b/dom/quota/test/browser/browser_permissionsPromptDeny.js
@@ -0,0 +1,150 @@
+/**
+ * Any copyright is dedicated to the Public Domain.
+ * http://creativecommons.org/publicdomain/zero/1.0/
+ */
+
+const testPageURL =
+ "https://example.com/browser/dom/quota/test/browser/permissionsPrompt.html";
+
+addTest(async function testPermissionTemporaryDenied() {
+ registerPopupEventHandler("popupshowing", function () {
+ ok(true, "prompt showing");
+ });
+ registerPopupEventHandler("popupshown", function () {
+ ok(true, "prompt shown");
+ triggerSecondaryCommand(this);
+ });
+ registerPopupEventHandler("popuphidden", function () {
+ ok(true, "prompt hidden");
+ });
+
+ info("Creating tab");
+ gBrowser.selectedTab = BrowserTestUtils.addTab(gBrowser);
+
+ info("Loading test page: " + testPageURL);
+ BrowserTestUtils.loadURIString(gBrowser.selectedBrowser, testPageURL);
+ await waitForMessage(false, gBrowser);
+
+ is(
+ getPermission(testPageURL, "persistent-storage"),
+ Ci.nsIPermissionManager.UNKNOWN_ACTION,
+ "Correct permission set"
+ );
+
+ let tempBlock = SitePermissions.getAllForBrowser(
+ gBrowser.selectedBrowser
+ ).find(
+ p =>
+ p.id == "persistent-storage" &&
+ p.state == SitePermissions.BLOCK &&
+ p.scope == SitePermissions.SCOPE_TEMPORARY
+ );
+ ok(tempBlock, "Should have a temporary block permission on active browser");
+
+ unregisterAllPopupEventHandlers();
+ gBrowser.removeCurrentTab();
+ removePermission(testPageURL, "persistent-storage");
+});
+
+addTest(async function testPermissionDenied() {
+ removePermission(testPageURL, "persistent-storage");
+
+ registerPopupEventHandler("popupshowing", function () {
+ ok(true, "prompt showing");
+ });
+ registerPopupEventHandler("popupshown", function () {
+ ok(true, "prompt shown");
+ triggerSecondaryCommand(this, /* remember = */ true);
+ });
+ registerPopupEventHandler("popuphidden", function () {
+ ok(true, "prompt hidden");
+ });
+
+ info("Creating tab");
+ gBrowser.selectedTab = BrowserTestUtils.addTab(gBrowser);
+
+ info("Loading test page: " + testPageURL);
+ BrowserTestUtils.loadURIString(gBrowser.selectedBrowser, testPageURL);
+ await waitForMessage(false, gBrowser);
+
+ is(
+ getPermission(testPageURL, "persistent-storage"),
+ Ci.nsIPermissionManager.DENY_ACTION,
+ "Correct permission set"
+ );
+ unregisterAllPopupEventHandlers();
+ gBrowser.removeCurrentTab();
+ // Keep persistent-storage permission for the next test.
+});
+
+addTest(async function testNoPermissionPrompt() {
+ registerPopupEventHandler("popupshowing", function () {
+ ok(false, "Shouldn't show a popup this time");
+ });
+ registerPopupEventHandler("popupshown", function () {
+ ok(false, "Shouldn't show a popup this time");
+ });
+ registerPopupEventHandler("popuphidden", function () {
+ ok(false, "Shouldn't show a popup this time");
+ });
+
+ info("Creating tab");
+ gBrowser.selectedTab = BrowserTestUtils.addTab(gBrowser);
+
+ info("Loading test page: " + testPageURL);
+ BrowserTestUtils.loadURIString(gBrowser.selectedBrowser, testPageURL);
+ await waitForMessage(false, gBrowser);
+
+ is(
+ getPermission(testPageURL, "persistent-storage"),
+ Ci.nsIPermissionManager.DENY_ACTION,
+ "Correct permission set"
+ );
+ unregisterAllPopupEventHandlers();
+ gBrowser.removeCurrentTab();
+ removePermission(testPageURL, "persistent-storage");
+});
+
+addTest(async function testPermissionDeniedDismiss() {
+ registerPopupEventHandler("popupshowing", function () {
+ ok(true, "prompt showing");
+ });
+ registerPopupEventHandler("popupshown", function () {
+ ok(true, "prompt shown");
+ // Dismiss permission prompt.
+ dismissNotification(this);
+ });
+ registerPopupEventHandler("popuphidden", function () {
+ ok(true, "prompt hidden");
+ });
+
+ info("Creating tab");
+ gBrowser.selectedTab = BrowserTestUtils.addTab(gBrowser);
+
+ info("Loading test page: " + testPageURL);
+ BrowserTestUtils.loadURIString(gBrowser.selectedBrowser, testPageURL);
+ await waitForMessage(false, gBrowser);
+
+ // Pressing ESC results in a temporary block permission on the browser object.
+ // So the global permission for the URL should still be unknown, but the browser
+ // should have a block permission with a temporary scope.
+ is(
+ getPermission(testPageURL, "persistent-storage"),
+ Ci.nsIPermissionManager.UNKNOWN_ACTION,
+ "Correct permission set"
+ );
+
+ let tempBlock = SitePermissions.getAllForBrowser(
+ gBrowser.selectedBrowser
+ ).find(
+ p =>
+ p.id == "persistent-storage" &&
+ p.state == SitePermissions.BLOCK &&
+ p.scope == SitePermissions.SCOPE_TEMPORARY
+ );
+ ok(tempBlock, "Should have a temporary block permission on active browser");
+
+ unregisterAllPopupEventHandlers();
+ gBrowser.removeCurrentTab();
+ removePermission(testPageURL, "persistent-storage");
+});
diff --git a/dom/quota/test/browser/browser_permissionsPromptUnknown.js b/dom/quota/test/browser/browser_permissionsPromptUnknown.js
new file mode 100644
index 0000000000..7e3f09ccaa
--- /dev/null
+++ b/dom/quota/test/browser/browser_permissionsPromptUnknown.js
@@ -0,0 +1,53 @@
+/**
+ * Any copyright is dedicated to the Public Domain.
+ * http://creativecommons.org/publicdomain/zero/1.0/
+ */
+
+const testPageURL =
+ "https://example.com/browser/dom/quota/test/browser/permissionsPrompt.html";
+
+addTest(async function testPermissionUnknownInPrivateWindow() {
+ removePermission(testPageURL, "persistent-storage");
+ info("Creating private window");
+ let win = await BrowserTestUtils.openNewBrowserWindow({ private: true });
+
+ registerPopupEventHandler(
+ "popupshowing",
+ function () {
+ ok(false, "Shouldn't show a popup this time");
+ },
+ win
+ );
+ registerPopupEventHandler(
+ "popupshown",
+ function () {
+ ok(false, "Shouldn't show a popup this time");
+ },
+ win
+ );
+ registerPopupEventHandler(
+ "popuphidden",
+ function () {
+ ok(false, "Shouldn't show a popup this time");
+ },
+ win
+ );
+
+ info("Creating private tab");
+ win.gBrowser.selectedTab = BrowserTestUtils.addTab(win.gBrowser);
+
+ info("Loading test page: " + testPageURL);
+ BrowserTestUtils.loadURIString(win.gBrowser.selectedBrowser, testPageURL);
+ await waitForMessage(false, win.gBrowser);
+
+ is(
+ getPermission(testPageURL, "persistent-storage"),
+ Ci.nsIPermissionManager.UNKNOWN_ACTION,
+ "Correct permission set"
+ );
+ unregisterAllPopupEventHandlers(win);
+ win.gBrowser.removeCurrentTab();
+ await BrowserTestUtils.closeWindow(win);
+ win = null;
+ removePermission(testPageURL, "persistent-storage");
+});
diff --git a/dom/quota/test/browser/browser_simpledb.js b/dom/quota/test/browser/browser_simpledb.js
new file mode 100644
index 0000000000..b3be78fca6
--- /dev/null
+++ b/dom/quota/test/browser/browser_simpledb.js
@@ -0,0 +1,51 @@
+/**
+ * Any copyright is dedicated to the Public Domain.
+ * http://creativecommons.org/publicdomain/zero/1.0/
+ */
+
+// getRandomBuffer, compareBuffers
+loadScript("dom/quota/test/common/file.js");
+
+addTest(async function testSimpleDB() {
+ const name = "data";
+ const bufferSize = 100;
+
+ let database = getSimpleDatabase();
+
+ let request = database.open("data");
+ await requestFinished(request);
+
+ let buffer1 = getRandomBuffer(bufferSize);
+
+ request = database.write(buffer1);
+ await requestFinished(request);
+
+ request = database.seek(0);
+ await requestFinished(request);
+
+ request = database.read(bufferSize);
+ let result = await requestFinished(request);
+
+ let buffer2 = result.getAsArrayBuffer();
+
+ ok(compareBuffers(buffer1, buffer2), "Buffers equal.");
+
+ let database2 = getSimpleDatabase();
+
+ try {
+ request = database2.open(name);
+ await requestFinished(request);
+ ok(false, "Should have thrown!");
+ } catch (ex) {
+ ok(request.resultCode == NS_ERROR_STORAGE_BUSY, "Good result code.");
+ }
+
+ request = database.close();
+ await requestFinished(request);
+
+ request = database2.open(name);
+ await requestFinished(request);
+
+ request = database2.close();
+ await requestFinished(request);
+});
diff --git a/dom/quota/test/browser/empty.html b/dom/quota/test/browser/empty.html
new file mode 100644
index 0000000000..1ad28bb1f7
--- /dev/null
+++ b/dom/quota/test/browser/empty.html
@@ -0,0 +1,8 @@
+<!DOCTYPE HTML>
+<html>
+<head>
+<title>Empty file</title>
+</head>
+<body>
+</body>
+</html>
diff --git a/dom/quota/test/browser/head.js b/dom/quota/test/browser/head.js
new file mode 100644
index 0000000000..38b2e82cf0
--- /dev/null
+++ b/dom/quota/test/browser/head.js
@@ -0,0 +1,149 @@
+/**
+ * Any copyright is dedicated to the Public Domain.
+ * http://creativecommons.org/publicdomain/zero/1.0/
+ */
+
+// The path to the top level directory.
+const depth = "../../../../";
+
+var gActiveListeners = {};
+
+loadScript("dom/quota/test/common/browser.js");
+
+function loadScript(path) {
+ const url = new URL(depth + path, gTestPath);
+ Services.scriptloader.loadSubScript(url.href, this);
+}
+
+// These event (un)registration handlers only work for one window, DONOT use
+// them with multiple windows.
+
+function registerPopupEventHandler(eventName, callback, win) {
+ if (!win) {
+ win = window;
+ }
+ gActiveListeners[eventName] = function (event) {
+ if (event.target != win.PopupNotifications.panel) {
+ return;
+ }
+ win.PopupNotifications.panel.removeEventListener(
+ eventName,
+ gActiveListeners[eventName]
+ );
+ delete gActiveListeners[eventName];
+
+ callback.call(win.PopupNotifications.panel);
+ };
+ win.PopupNotifications.panel.addEventListener(
+ eventName,
+ gActiveListeners[eventName]
+ );
+}
+
+function unregisterAllPopupEventHandlers(win) {
+ if (!win) {
+ win = window;
+ }
+ for (let eventName in gActiveListeners) {
+ win.PopupNotifications.panel.removeEventListener(
+ eventName,
+ gActiveListeners[eventName]
+ );
+ }
+ gActiveListeners = {};
+}
+
+function triggerMainCommand(popup, win) {
+ if (!win) {
+ win = window;
+ }
+ info("triggering main command");
+ let notifications = popup.childNodes;
+ ok(notifications.length, "at least one notification displayed");
+ let notification = notifications[0];
+ info("triggering command: " + notification.getAttribute("buttonlabel"));
+
+ EventUtils.synthesizeMouseAtCenter(notification.button, {}, win);
+}
+
+async function triggerSecondaryCommand(popup, remember = false, win = window) {
+ info("triggering secondary command");
+ let notifications = popup.childNodes;
+ ok(notifications.length, "at least one notification displayed");
+ let notification = notifications[0];
+
+ if (remember) {
+ notification.checkbox.checked = true;
+ }
+
+ await EventUtils.synthesizeMouseAtCenter(
+ notification.secondaryButton,
+ {},
+ win
+ );
+}
+
+function dismissNotification(popup, win = window) {
+ info("dismissing notification");
+ executeSoon(function () {
+ EventUtils.synthesizeKey("VK_ESCAPE", {}, win);
+ });
+}
+
+function waitForMessage(aMessage, browser) {
+ // We cannot capture aMessage inside the checkFn, so we override the
+ // checkFn.toSource to tunnel aMessage instead.
+ let checkFn = function () {};
+ checkFn.toSource = function () {
+ return `function checkFn(event) {
+ let message = ${aMessage.toSource()};
+ if (event.data == message) {
+ return true;
+ }
+ throw new Error(
+ \`Unexpected result: \$\{event.data\}, expected \$\{message\}\`
+ );
+ }`;
+ };
+
+ return BrowserTestUtils.waitForContentEvent(
+ browser.selectedBrowser,
+ "message",
+ /* capture */ true,
+ checkFn,
+ /* wantsUntrusted */ true
+ ).then(() => {
+ // An assertion in checkFn wouldn't be recorded as part of the test, so we
+ // use this assertion to confirm that we've successfully received the
+ // message (we'll only reach this point if that's the case).
+ ok(true, "Received message: " + aMessage);
+ });
+}
+
+function removePermission(url, permission) {
+ let uri = Cc["@mozilla.org/network/io-service;1"]
+ .getService(Ci.nsIIOService)
+ .newURI(url);
+ let ssm = Cc["@mozilla.org/scriptsecuritymanager;1"].getService(
+ Ci.nsIScriptSecurityManager
+ );
+ let principal = ssm.createContentPrincipal(uri, {});
+
+ Cc["@mozilla.org/permissionmanager;1"]
+ .getService(Ci.nsIPermissionManager)
+ .removeFromPrincipal(principal, permission);
+}
+
+function getPermission(url, permission) {
+ let uri = Cc["@mozilla.org/network/io-service;1"]
+ .getService(Ci.nsIIOService)
+ .newURI(url);
+ let ssm = Cc["@mozilla.org/scriptsecuritymanager;1"].getService(
+ Ci.nsIScriptSecurityManager
+ );
+ let principal = ssm.createContentPrincipal(uri, {});
+
+ return Cc["@mozilla.org/permissionmanager;1"]
+ .getService(Ci.nsIPermissionManager)
+ .testPermissionFromPrincipal(principal, permission);
+}
diff --git a/dom/quota/test/browser/helpers.js b/dom/quota/test/browser/helpers.js
new file mode 100644
index 0000000000..f3bbb36b71
--- /dev/null
+++ b/dom/quota/test/browser/helpers.js
@@ -0,0 +1,46 @@
+/**
+ * Any copyright is dedicated to the Public Domain.
+ * http://creativecommons.org/publicdomain/zero/1.0/
+ */
+
+// The path to the top level directory.
+const depth = "../../../../";
+
+var testGenerator;
+var testResult;
+
+loadScript("dom/quota/test/common/nestedtest.js");
+
+function loadScript(path) {
+ const url = new URL(depth + path, window.location.href);
+ SpecialPowers.Services.scriptloader.loadSubScript(url.href, this);
+}
+
+function runTest() {
+ clearAllDatabases(() => {
+ testGenerator = testSteps();
+ testGenerator.next();
+ });
+}
+
+function finishTestNow() {
+ if (testGenerator) {
+ testGenerator.return();
+ testGenerator = undefined;
+ }
+}
+
+function finishTest() {
+ clearAllDatabases(() => {
+ setTimeout(finishTestNow, 0);
+ setTimeout(() => {
+ window.parent.postMessage(testResult, "*");
+ }, 0);
+ });
+}
+
+function continueToNextStep() {
+ setTimeout(() => {
+ testGenerator.next();
+ }, 0);
+}
diff --git a/dom/quota/test/browser/permissionsPrompt.html b/dom/quota/test/browser/permissionsPrompt.html
new file mode 100644
index 0000000000..5f11bf6c95
--- /dev/null
+++ b/dom/quota/test/browser/permissionsPrompt.html
@@ -0,0 +1,34 @@
+<!--
+ Any copyright is dedicated to the Public Domain.
+ http://creativecommons.org/publicdomain/zero/1.0/
+-->
+<html>
+ <head>
+ <meta charset=UTF-8>
+ <title>Persistent-Storage Permission Prompt Test</title>
+
+ <script type="text/javascript" src="helpers.js"></script>
+
+ <script type="text/javascript">
+ function* testSteps()
+ {
+ SpecialPowers.pushPrefEnv({
+ "set": [["dom.storageManager.prompt.testing", false],
+ ["dom.storageManager.prompt.testing.allow", false]]
+ }, continueToNextStep);
+ yield undefined;
+
+ navigator.storage.persist().then(result => {
+ testGenerator.next(result);
+ });
+ testResult = yield undefined;
+
+ finishTest();
+ }
+ </script>
+
+ </head>
+
+ <body onload="runTest();" onunload="finishTestNow();"></body>
+
+</html>