summaryrefslogtreecommitdiffstats
path: root/dom/security/test/https-first/browser_schemeless.js
blob: 9687f15072e5255f37ed0b14f31847b45d6ae9a1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
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"
  );
});