summaryrefslogtreecommitdiffstats
path: root/browser/base/content/test/tabs/browser_privilegedmozilla_process_pref.js
blob: 86ef66c936b2a36d933bde23fec6cc996e53c5a5 (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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */
/* vim: set ft=javascript ts=2 et sw=2 tw=80: */
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/
 */

/**
 * Tests to ensure that Mozilla Privileged Webpages load in the privileged
 * mozilla web content process. Normal http web pages should load in the web
 * content process.
 * Ref: Bug 1539595.
 */

// High and Low Privilege
const TEST_HIGH1 = "https://example.org/";
const TEST_HIGH2 = "https://test1.example.org/";
// eslint-disable-next-line @microsoft/sdl/no-insecure-url
const TEST_LOW1 = "http://example.org/";
const TEST_LOW2 = "https://example.com/";

add_setup(async function () {
  await SpecialPowers.pushPrefEnv({
    set: [
      ["browser.tabs.remote.separatePrivilegedMozillaWebContentProcess", true],
      ["browser.tabs.remote.separatedMozillaDomains", "example.org"],
      ["dom.ipc.processCount.privilegedmozilla", 1],
    ],
  });
});

/*
 * Test to ensure that the tabs open in privileged mozilla content process. We
 * will first open a page that acts as a reference to the privileged mozilla web
 * content process. With the reference, we can then open other links in a new tab
 * and ensure that the new tab opens in the same privileged mozilla content process
 * as our reference.
 */
add_task(async function webpages_in_privileged_content_process() {
  Services.ppmm.releaseCachedProcesses();

  await BrowserTestUtils.withNewTab(TEST_HIGH1, async function (browser1) {
    checkBrowserRemoteType(browser1, E10SUtils.PRIVILEGEDMOZILLA_REMOTE_TYPE);

    // Note the processID for about:newtab for comparison later.
    let privilegedPid = browser1.frameLoader.remoteTab.osPid;

    for (let url of [
      TEST_HIGH1,
      `${TEST_HIGH1}#foo`,
      `${TEST_HIGH1}?q=foo`,
      TEST_HIGH2,
      `${TEST_HIGH2}#foo`,
      `${TEST_HIGH2}?q=foo`,
    ]) {
      await BrowserTestUtils.withNewTab(url, async function (browser2) {
        is(
          browser2.frameLoader.remoteTab.osPid,
          privilegedPid,
          "Check that privileged pages are in the same privileged mozilla content process."
        );
      });
    }
  });

  Services.ppmm.releaseCachedProcesses();
});

/*
 * Test to ensure that a process switch occurs when navigating between normal
 * web pages and unprivileged pages in the same tab.
 */
add_task(async function process_switching_through_loading_in_the_same_tab() {
  Services.ppmm.releaseCachedProcesses();

  await BrowserTestUtils.withNewTab(TEST_LOW1, async function (browser) {
    checkBrowserRemoteType(browser, E10SUtils.WEB_REMOTE_TYPE);

    for (let [url, remoteType] of [
      [TEST_HIGH1, E10SUtils.PRIVILEGEDMOZILLA_REMOTE_TYPE],
      [TEST_LOW1, E10SUtils.WEB_REMOTE_TYPE],
      [TEST_HIGH1, E10SUtils.PRIVILEGEDMOZILLA_REMOTE_TYPE],
      [TEST_LOW2, E10SUtils.WEB_REMOTE_TYPE],
      [TEST_HIGH1, E10SUtils.PRIVILEGEDMOZILLA_REMOTE_TYPE],
      [TEST_LOW1, E10SUtils.WEB_REMOTE_TYPE],
      [TEST_LOW2, E10SUtils.WEB_REMOTE_TYPE],
      [`${TEST_HIGH1}#foo`, E10SUtils.PRIVILEGEDMOZILLA_REMOTE_TYPE],
      [TEST_LOW1, E10SUtils.WEB_REMOTE_TYPE],
      [`${TEST_HIGH1}#bar`, E10SUtils.PRIVILEGEDMOZILLA_REMOTE_TYPE],
      [TEST_LOW2, E10SUtils.WEB_REMOTE_TYPE],
      [`${TEST_HIGH1}#baz`, E10SUtils.PRIVILEGEDMOZILLA_REMOTE_TYPE],
      [TEST_LOW1, E10SUtils.WEB_REMOTE_TYPE],
      [`${TEST_HIGH1}?q=foo`, E10SUtils.PRIVILEGEDMOZILLA_REMOTE_TYPE],
      [TEST_LOW2, E10SUtils.WEB_REMOTE_TYPE],
      [`${TEST_HIGH1}?q=bar`, E10SUtils.PRIVILEGEDMOZILLA_REMOTE_TYPE],
      [TEST_LOW1, E10SUtils.WEB_REMOTE_TYPE],
      [`${TEST_HIGH1}?q=baz`, E10SUtils.PRIVILEGEDMOZILLA_REMOTE_TYPE],
      [TEST_LOW2, E10SUtils.WEB_REMOTE_TYPE],
    ]) {
      BrowserTestUtils.loadURIString(browser, url);
      await BrowserTestUtils.browserLoaded(browser, false, url);
      checkBrowserRemoteType(browser, remoteType);
    }
  });

  Services.ppmm.releaseCachedProcesses();
});

/*
 * Test to ensure that a process switch occurs when navigating between normal
 * web pages and privileged pages using the browser's navigation features
 * such as history and location change.
 */
add_task(async function process_switching_through_navigation_features() {
  Services.ppmm.releaseCachedProcesses();

  await BrowserTestUtils.withNewTab(TEST_HIGH1, async function (browser) {
    checkBrowserRemoteType(browser, E10SUtils.PRIVILEGEDMOZILLA_REMOTE_TYPE);

    // Note the processID for about:newtab for comparison later.
    let privilegedPid = browser.frameLoader.remoteTab.osPid;

    // Check that about:newtab opened from JS in about:newtab page is in the same process.
    let promiseTabOpened = BrowserTestUtils.waitForNewTab(
      gBrowser,
      TEST_HIGH1,
      true
    );
    await SpecialPowers.spawn(browser, [TEST_HIGH1], uri => {
      content.open(uri, "_blank");
    });
    let newTab = await promiseTabOpened;
    registerCleanupFunction(async function () {
      BrowserTestUtils.removeTab(newTab);
    });
    browser = newTab.linkedBrowser;
    is(
      browser.frameLoader.remoteTab.osPid,
      privilegedPid,
      "Check that new tab opened from privileged page is loaded in privileged mozilla content process."
    );

    // Check that reload does not break the privileged mozilla content process affinity.
    BrowserReload();
    await BrowserTestUtils.browserLoaded(browser, false, TEST_HIGH1);
    is(
      browser.frameLoader.remoteTab.osPid,
      privilegedPid,
      "Check that privileged page is still in privileged mozilla content process after reload."
    );

    // Load http webpage
    BrowserTestUtils.loadURIString(browser, TEST_LOW1);
    await BrowserTestUtils.browserLoaded(browser, false, TEST_LOW1);
    checkBrowserRemoteType(browser, E10SUtils.WEB_REMOTE_TYPE);

    // Check that using the history back feature switches back to privileged mozilla content process.
    let promiseLocation = BrowserTestUtils.waitForLocationChange(
      gBrowser,
      TEST_HIGH1
    );
    browser.goBack();
    await promiseLocation;
    is(
      browser.frameLoader.remoteTab.osPid,
      privilegedPid,
      "Check that privileged page is still in privileged mozilla content process after history goBack."
    );

    // Check that using the history forward feature switches back to the web content process.
    promiseLocation = BrowserTestUtils.waitForLocationChange(
      gBrowser,
      TEST_LOW1
    );
    browser.goForward();
    await promiseLocation;
    checkBrowserRemoteType(
      browser,
      E10SUtils.WEB_REMOTE_TYPE,
      "Check that tab runs in the web content process after using history goForward."
    );

    // Check that goto history index does not break the affinity.
    promiseLocation = BrowserTestUtils.waitForLocationChange(
      gBrowser,
      TEST_HIGH1
    );
    browser.gotoIndex(0);
    await promiseLocation;
    is(
      browser.frameLoader.remoteTab.osPid,
      privilegedPid,
      "Check that privileged page is in privileged mozilla content process after history gotoIndex."
    );

    BrowserTestUtils.loadURIString(browser, TEST_LOW2);
    await BrowserTestUtils.browserLoaded(browser, false, TEST_LOW2);
    checkBrowserRemoteType(browser, E10SUtils.WEB_REMOTE_TYPE);

    // Check that location change causes a change in process type as well.
    await SpecialPowers.spawn(browser, [TEST_HIGH1], uri => {
      content.location = uri;
    });
    await BrowserTestUtils.browserLoaded(browser, false, TEST_HIGH1);
    is(
      browser.frameLoader.remoteTab.osPid,
      privilegedPid,
      "Check that privileged page is in privileged mozilla content process after location change."
    );
  });

  Services.ppmm.releaseCachedProcesses();
});