From 26a029d407be480d791972afb5975cf62c9360a6 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Fri, 19 Apr 2024 02:47:55 +0200 Subject: Adding upstream version 124.0.1. Signed-off-by: Daniel Baumann --- .../test/https-first/browser_schemeless.js | 191 +++++++++++++++++++++ 1 file changed, 191 insertions(+) create mode 100644 dom/security/test/https-first/browser_schemeless.js (limited to 'dom/security/test/https-first/browser_schemeless.js') diff --git a/dom/security/test/https-first/browser_schemeless.js b/dom/security/test/https-first/browser_schemeless.js new file mode 100644 index 0000000000..9687f15072 --- /dev/null +++ b/dom/security/test/https-first/browser_schemeless.js @@ -0,0 +1,191 @@ +/* Any copyright is dedicated to the Public Domain. + * https://creativecommons.org/publicdomain/zero/1.0/ */ + +"use strict"; + +// We explicitly need HTTP URLs in this test +/* eslint-disable @microsoft/sdl/no-insecure-url */ + +ChromeUtils.defineLazyGetter(this, "UrlbarTestUtils", () => { + const { UrlbarTestUtils: module } = ChromeUtils.importESModule( + "resource://testing-common/UrlbarTestUtils.sys.mjs" + ); + module.init(this); + return module; +}); + +XPCOMUtils.defineLazyServiceGetter( + this, + "clipboardHelper", + "@mozilla.org/widget/clipboardhelper;1", + "nsIClipboardHelper" +); + +/** Type aInput into the address bar and press enter */ +async function runMainTest(aInput, aDesc, aExpectedScheme) { + await BrowserTestUtils.withNewTab("about:blank", async function (browser) { + const loaded = BrowserTestUtils.browserLoaded(browser, false, null, true); + await UrlbarTestUtils.promiseAutocompleteResultPopup({ + window, + value: aInput, + }); + EventUtils.synthesizeKey("KEY_Enter"); + await loaded; + + is(browser.currentURI.scheme, aExpectedScheme, "Main test: " + aDesc); + }); +} + +/** + * Type aInput into the address bar and press ctrl+enter, + * resulting in the input being canonized first. + * This should not change schemeless HTTPS behaviour. */ +async function runCanonizedTest(aInput, aDesc, aExpectedScheme) { + await BrowserTestUtils.withNewTab("about:blank", async function (browser) { + const loaded = BrowserTestUtils.browserLoaded(browser, false, null, true); + await UrlbarTestUtils.promiseAutocompleteResultPopup({ + window, + value: aInput, + }); + EventUtils.synthesizeKey("KEY_Enter", { ctrlKey: true }); + await loaded; + + is(browser.currentURI.scheme, aExpectedScheme, "Canonized test: " + aDesc); + }); +} + +/** + * Type aInput into the address bar and press alt+enter, + * resulting in the input being loaded in a new tab. + * This should not change schemeless HTTPS behaviour. */ +async function runNewTabTest(aInput, aDesc, aExpectedScheme) { + await BrowserTestUtils.withNewTab( + "about:about", // For alt+enter to do anything, we need to be on a page other than about:blank. + async function () { + const newTabPromise = BrowserTestUtils.waitForNewTab( + gBrowser, + null, + true + ); + await UrlbarTestUtils.promiseAutocompleteResultPopup({ + window, + value: aInput, + }); + EventUtils.synthesizeKey("KEY_Enter", { altKey: true }); + const newTab = await newTabPromise; + + is( + newTab.linkedBrowser.currentURI.scheme, + aExpectedScheme, + "New tab test: " + aDesc + ); + + BrowserTestUtils.removeTab(newTab); + } + ); +} + +/** + * Type aInput into the address bar and press shift+enter, + * resulting in the input being loaded in a new window. + * This should not change schemeless HTTPS behaviour. */ +async function runNewWindowTest(aInput, aDesc, aExpectedScheme) { + await BrowserTestUtils.withNewTab("about:about", async function () { + const newWindowPromise = BrowserTestUtils.waitForNewWindow({ + waitForAnyURLLoaded: true, + }); + await UrlbarTestUtils.promiseAutocompleteResultPopup({ + window, + value: aInput, + }); + EventUtils.synthesizeKey("KEY_Enter", { shiftKey: true }); + const newWindow = await newWindowPromise; + + is( + newWindow.gBrowser.selectedBrowser.currentURI.scheme, + aExpectedScheme, + "New window test: " + aDesc + ); + + await BrowserTestUtils.closeWindow(newWindow); + }); +} + +/** + * Instead of typing aInput into the address bar, copy it + * to the clipboard and use the "Paste and Go" menu entry. + * This should not change schemeless HTTPS behaviour. */ +async function runPasteAndGoTest(aInput, aDesc, aExpectedScheme) { + await BrowserTestUtils.withNewTab("about:blank", async function (browser) { + gURLBar.focus(); + await SimpleTest.promiseClipboardChange(aInput, () => { + clipboardHelper.copyString(aInput); + }); + + const loaded = BrowserTestUtils.browserLoaded(browser, false, null, true); + const textBox = gURLBar.querySelector("moz-input-box"); + const cxmenu = textBox.menupopup; + const cxmenuPromise = BrowserTestUtils.waitForEvent(cxmenu, "popupshown"); + EventUtils.synthesizeMouseAtCenter(gURLBar.inputField, { + type: "contextmenu", + button: 2, + }); + await cxmenuPromise; + const menuitem = textBox.getMenuItem("paste-and-go"); + menuitem.closest("menupopup").activateItem(menuitem); + await loaded; + + is( + browser.currentURI.scheme, + aExpectedScheme, + "Paste and go test: " + aDesc + ); + }); +} + +async function runTest(aInput, aDesc, aExpectedScheme) { + await runMainTest(aInput, aDesc, aExpectedScheme); + await runCanonizedTest(aInput, aDesc, aExpectedScheme); + await runNewTabTest(aInput, aDesc, aExpectedScheme); + await runNewWindowTest(aInput, aDesc, aExpectedScheme); + await runPasteAndGoTest(aInput, aDesc, aExpectedScheme); +} + +add_task(async function () { + requestLongerTimeout(10); + + await SpecialPowers.pushPrefEnv({ + set: [ + ["dom.security.https_first", false], + ["dom.security.https_first_schemeless", false], + ], + }); + + await runTest( + "http://example.com", + "Should not upgrade upgradeable website with explicit scheme", + "http" + ); + + await runTest( + "example.com", + "Should not upgrade upgradeable website without explicit scheme", + "http" + ); + + await SpecialPowers.pushPrefEnv({ + set: [["dom.security.https_first_schemeless", true]], + }); + + await runTest( + "http://example.com", + "Should not upgrade upgradeable website with explicit scheme", + "http" + ); + + await runTest( + "example.com", + "Should upgrade upgradeable website without explicit scheme", + "https" + ); +}); -- cgit v1.2.3