summaryrefslogtreecommitdiffstats
path: root/toolkit/components/httpsonlyerror/tests/browser/browser_errorpage.js
blob: 43f46699c2e5fceee2a7ab6199bc907709d4cad3 (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
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

"use strict";

const SECURE_PAGE = "https://example.com/";
const GOOD_PAGE = "http://example.com/";
const BAD_CERT = "http://expired.example.com/";
const UNKNOWN_ISSUER = "http://self-signed.example.com/";

const { TabStateFlusher } = ChromeUtils.importESModule(
  "resource:///modules/sessionstore/TabStateFlusher.sys.mjs"
);

add_task(async function () {
  info("Check that the error pages shows up");

  await Promise.all([
    testPageWithURI(
      GOOD_PAGE,
      "Should not show error page on upgradeable website.",
      false
    ),
    testPageWithURI(
      BAD_CERT,
      "Should show error page on bad-certificate error.",
      true
    ),
    testPageWithURI(
      UNKNOWN_ISSUER,
      "Should show error page on unkown-issuer error.",
      true
    ),
  ]);
});

add_task(async function () {
  info("Check that the go-back button returns to previous page");

  // Test with and without being in an iFrame
  for (let useFrame of [false, true]) {
    let tab = await openErrorPage(BAD_CERT, useFrame);
    let browser = tab.linkedBrowser;

    is(
      browser.webNavigation.canGoBack,
      false,
      "!webNavigation.canGoBack should be false."
    );
    is(
      browser.webNavigation.canGoForward,
      false,
      "webNavigation.canGoForward should be false."
    );

    // Populate the shistory entries manually, since it happens asynchronously
    // and the following tests will be too soon otherwise.
    await TabStateFlusher.flush(browser);
    let { entries } = JSON.parse(SessionStore.getTabState(tab));
    is(entries.length, 1, "There should be 1 shistory entry.");

    let bc = browser.browsingContext;
    if (useFrame) {
      bc = bc.children[0];
    }

    if (useFrame) {
      await SpecialPowers.spawn(bc, [], async function () {
        let returnButton = content.document.getElementById("goBack");
        is(
          returnButton,
          null,
          "Return-button should not be present in iFrame."
        );
      });
    } else {
      let locationChangePromise = BrowserTestUtils.waitForLocationChange(
        gBrowser,
        "about:home"
      );
      await SpecialPowers.spawn(bc, [], async function () {
        let returnButton = content.document.getElementById("goBack");
        is(
          returnButton.getAttribute("autofocus"),
          "true",
          "Return-button should have focus."
        );
        returnButton.click();
      });

      await locationChangePromise;

      is(browser.webNavigation.canGoBack, true, "webNavigation.canGoBack");
      is(
        browser.webNavigation.canGoForward,
        false,
        "!webNavigation.canGoForward"
      );
      is(gBrowser.currentURI.spec, "about:home", "Went back");
    }

    BrowserTestUtils.removeTab(gBrowser.selectedTab);
  }
});

add_task(async function () {
  info("Check that the go-back button returns to about:home");

  let tab = await BrowserTestUtils.openNewForegroundTab(gBrowser, SECURE_PAGE);
  let browser = gBrowser.selectedBrowser;

  let errorPageLoaded = BrowserTestUtils.waitForErrorPage(browser);
  BrowserTestUtils.startLoadingURIString(browser, BAD_CERT);
  await errorPageLoaded;

  is(
    browser.webNavigation.canGoBack,
    true,
    "webNavigation.canGoBack should be true before navigation."
  );
  is(
    browser.webNavigation.canGoForward,
    false,
    "webNavigation.canGoForward should be false before navigation."
  );

  // Populate the shistory entries manually, since it happens asynchronously
  // and the following tests will be too soon otherwise.
  await TabStateFlusher.flush(browser);
  let { entries } = JSON.parse(SessionStore.getTabState(tab));
  is(entries.length, 2, "There should be 1 shistory entries.");

  let pageShownPromise = BrowserTestUtils.waitForContentEvent(
    browser,
    "pageshow",
    true
  );

  // Click on "go back" Button
  await SpecialPowers.spawn(browser, [], async function () {
    let returnButton = content.document.getElementById("goBack");
    returnButton.click();
  });
  await pageShownPromise;

  is(
    browser.webNavigation.canGoBack,
    false,
    "webNavigation.canGoBack should be false after navigation."
  );
  is(
    browser.webNavigation.canGoForward,
    true,
    "webNavigation.canGoForward should be true after navigation."
  );
  is(
    gBrowser.currentURI.spec,
    SECURE_PAGE,
    "Should go back to previous page after button click."
  );

  BrowserTestUtils.removeTab(gBrowser.selectedTab);
});

// Utils

async function testPageWithURI(uri, message, expect) {
  // Open new Tab with URI
  let tab;
  if (expect) {
    tab = await openErrorPage(uri, false);
  } else {
    tab = await BrowserTestUtils.openNewForegroundTab(gBrowser, uri, true);
  }

  // Check if HTTPS-Only Error-Page loaded instead
  let browser = tab.linkedBrowser;
  await SpecialPowers.spawn(
    browser,
    [message, expect],
    function (message, expect) {
      const doc = content.document;
      let result = doc.documentURI.startsWith("about:httpsonlyerror");
      is(result, expect, message);
    }
  );

  // Close tab again
  BrowserTestUtils.removeTab(tab);
}