tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

browser_intercepted_channel_process_swap.js (3175B)


      1 /* Any copyright is dedicated to the Public Domain.
      2   http://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 // Tests that navigation loads through intercepted channels result in the
      5 // appropriate process swaps. This appears to only be possible when navigating
      6 // to a cross-origin URL, where that navigation is controlled by a ServiceWorker.
      7 
      8 "use strict";
      9 
     10 const SAME_ORIGIN = "https://example.com";
     11 const CROSS_ORIGIN = "https://example.org";
     12 
     13 const SAME_ORIGIN_ROOT = getRootDirectory(gTestPath).replace(
     14  "chrome://mochitests/content",
     15  SAME_ORIGIN
     16 );
     17 const CROSS_ORIGIN_ROOT = SAME_ORIGIN_ROOT.replace(SAME_ORIGIN, CROSS_ORIGIN);
     18 
     19 const SW_REGISTER_URL = `${CROSS_ORIGIN_ROOT}empty_with_utils.html`;
     20 const SW_SCRIPT_URL = `${CROSS_ORIGIN_ROOT}intercepted_channel_process_swap_worker.js`;
     21 const URL_BEFORE_NAVIGATION = `${SAME_ORIGIN_ROOT}empty.html`;
     22 const CROSS_ORIGIN_URL = `${CROSS_ORIGIN_ROOT}empty.html`;
     23 
     24 const TESTCASES = [
     25  {
     26    url: CROSS_ORIGIN_URL,
     27    description:
     28      "Controlled cross-origin navigation with network-provided response",
     29  },
     30  {
     31    url: `${CROSS_ORIGIN_ROOT}this-path-does-not-exist?respondWith=${CROSS_ORIGIN_URL}`,
     32    description:
     33      "Controlled cross-origin navigation with ServiceWorker-provided response",
     34  },
     35 ];
     36 
     37 async function navigateTab(aTab, aUrl) {
     38  BrowserTestUtils.startLoadingURIString(aTab.linkedBrowser, aUrl);
     39 
     40  await BrowserTestUtils.waitForLocationChange(gBrowser, aUrl).then(() =>
     41    BrowserTestUtils.browserStopped(aTab.linkedBrowser)
     42  );
     43 }
     44 
     45 async function runTestcase(aTab, aTestcase) {
     46  info(`Testing ${aTestcase.description}`);
     47 
     48  await navigateTab(aTab, URL_BEFORE_NAVIGATION);
     49 
     50  const [initialPid] = E10SUtils.getBrowserPids(aTab.linkedBrowser);
     51 
     52  await navigateTab(aTab, aTestcase.url);
     53 
     54  const [finalPid] = E10SUtils.getBrowserPids(aTab.linkedBrowser);
     55 
     56  await SpecialPowers.spawn(aTab.linkedBrowser, [], () => {
     57    Assert.ok(
     58      content.navigator.serviceWorker.controller,
     59      `${content.location} should be controlled.`
     60    );
     61  });
     62 
     63  Assert.notEqual(
     64    initialPid,
     65    finalPid,
     66    `Navigating from ${URL_BEFORE_NAVIGATION} to ${aTab.linkedBrowser.currentURI.spec} should have resulted in a different PID.`
     67  );
     68 }
     69 
     70 add_task(async function setupPrefs() {
     71  await SpecialPowers.pushPrefEnv({
     72    set: [
     73      ["dom.serviceWorkers.enabled", true],
     74      ["dom.serviceWorkers.testing.enabled", true],
     75    ],
     76  });
     77 });
     78 
     79 add_task(async function setupBrowser() {
     80  const tab = await BrowserTestUtils.openNewForegroundTab({
     81    gBrowser,
     82    opening: SW_REGISTER_URL,
     83  });
     84 
     85  await SpecialPowers.spawn(
     86    tab.linkedBrowser,
     87    [SW_SCRIPT_URL],
     88    async scriptUrl => {
     89      await content.wrappedJSObject.registerAndWaitForActive(scriptUrl);
     90    }
     91  );
     92 });
     93 
     94 add_task(async function runTestcases() {
     95  for (const testcase of TESTCASES) {
     96    await runTestcase(gBrowser.selectedTab, testcase);
     97  }
     98 });
     99 
    100 add_task(async function cleanup() {
    101  const tab = gBrowser.selectedTab;
    102 
    103  await navigateTab(tab, SW_REGISTER_URL);
    104 
    105  await SpecialPowers.spawn(tab.linkedBrowser, [], async () => {
    106    await content.wrappedJSObject.unregisterAll();
    107  });
    108 
    109  BrowserTestUtils.removeTab(tab);
    110 });