tor-browser

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

browser_command_line_urls.js (4921B)


      1 /* Any copyright is dedicated to the Public Domain.
      2 http://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 "use strict";
      5 
      6 /**
      7 * Test command line processing of URLs meant to be intepreted by DevTools.
      8 */
      9 
     10 Services.scriptloader.loadSubScript(
     11  "chrome://mochitests/content/browser/devtools/client/shared/test/shared-head.js",
     12  this
     13 );
     14 Services.scriptloader.loadSubScript(
     15  "chrome://mochitests/content/browser/devtools/client/debugger/test/mochitest/shared-head.js",
     16  this
     17 );
     18 
     19 const { DevToolsStartup } = ChromeUtils.importESModule(
     20  "resource:///modules/DevToolsStartup.sys.mjs"
     21 );
     22 
     23 const startup = new DevToolsStartup();
     24 // The feature covered here only work when calling firefox from command line
     25 // while it is already opened. So fake Firefox being already opened:
     26 startup.initialized = true;
     27 
     28 registerCleanupFunction(() => {
     29  for (const pref of [
     30    "devtools.debugger.pending-selected-location",
     31    "devtools.debugger.prefs-schema-version",
     32    "devtools.everOpened",
     33    "devtools.toolbox.selectedTool",
     34  ]) {
     35    Services.prefs.clearUserPref(pref);
     36  }
     37 });
     38 
     39 add_task(async function ignoredUrls() {
     40  const tabCount = gBrowser.tabs.length;
     41 
     42  // We explicitely try to ignore these URL which looks like line, but are passwords
     43  sendUrlViaCommandLine("https://foo@user:123");
     44  sendUrlViaCommandLine("https://foo@user:123");
     45  sendUrlViaCommandLine("https://foo@123:456");
     46 
     47  // The following is an invalid URL (domain with space)
     48  sendUrlViaCommandLine("https://foo /index.html:123:456");
     49 
     50  // eslint-disable-next-line mozilla/no-arbitrary-setTimeout
     51  await new Promise(r => setTimeout(r, 1000));
     52 
     53  is(tabCount, gBrowser.tabs.length);
     54 });
     55 
     56 /**
     57 * With DevTools closed, but DevToolsStartup "initialized",
     58 * the url will be ignored
     59 */
     60 add_task(async function openingWithDevToolsClosed() {
     61  const url = URL_ROOT_SSL + "command-line.html:5:2";
     62 
     63  const tabCount = gBrowser.tabs.length;
     64  const ignoredUrl = sendUrlViaCommandLine(url);
     65  ok(ignoredUrl, "The url is ignored when no devtools are opened");
     66 
     67  // eslint-disable-next-line mozilla/no-arbitrary-setTimeout
     68  await new Promise(r => setTimeout(r, 1000));
     69 
     70  is(tabCount, gBrowser.tabs.length);
     71 });
     72 
     73 /**
     74 * With DevTools opened, but the source isn't in the debugged tab,
     75 * the url will also be opened via view-source
     76 */
     77 add_task(async function openingWithDevToolsButUnknownSource() {
     78  const url = URL_ROOT_SSL + "command-line.html:5:2";
     79 
     80  const tab = BrowserTestUtils.addTab(
     81    gBrowser,
     82    "data:text/html;charset=utf-8,<title>foo</title>"
     83  );
     84  gBrowser.selectedTab = tab;
     85 
     86  const toolbox = await gDevTools.showToolboxForTab(tab, {
     87    toolId: "jsdebugger",
     88  });
     89 
     90  const newTabOpened = BrowserTestUtils.waitForNewTab(gBrowser);
     91  sendUrlViaCommandLine(url);
     92  const newTab = await newTabOpened;
     93  is(
     94    newTab.linkedBrowser.documentURI.spec,
     95    "view-source:" + URL_ROOT_SSL + "command-line.html"
     96  );
     97 
     98  await SpecialPowers.spawn(gBrowser.selectedBrowser, [], async function () {
     99    // View source may not have updated the selection just yet
    100    ContentTaskUtils.waitForCondition(() => !!content.getSelection());
    101 
    102    const selection = content.getSelection();
    103    Assert.equal(
    104      selection.toString().trimEnd(),
    105      "  <title>Command line test page</title>",
    106      "The 5th line is selected in view-source"
    107    );
    108  });
    109  await gBrowser.removeTab(newTab);
    110 
    111  await toolbox.destroy();
    112  await gBrowser.removeTab(tab);
    113 });
    114 
    115 /**
    116 * With DevTools opened, and the source is debugged by the debugger,
    117 * the url will be opened in the debugger.
    118 */
    119 add_task(async function openingWithDevToolsAndKnownSource() {
    120  const line = 5;
    121  const column = 2;
    122  const url = URL_ROOT_SSL + `command-line.js:${line}:${column}`;
    123 
    124  const tab = await BrowserTestUtils.openNewForegroundTab(
    125    gBrowser,
    126    URL_ROOT_SSL + "command-line.html"
    127  );
    128  const toolbox = await gDevTools.showToolboxForTab(tab, {
    129    toolId: "jsdebugger",
    130  });
    131 
    132  info("Open a first URL with line and column");
    133  sendUrlViaCommandLine(url);
    134 
    135  const dbg = createDebuggerContext(toolbox);
    136  await waitForSelectedSource(dbg, "command-line.js");
    137  await waitForSelectedLocation(dbg, line, column);
    138 
    139  info("Open another URL with only a line");
    140  const secondLine = 6;
    141  const url2 = URL_ROOT_SSL + `command-line.js:${secondLine}`;
    142  sendUrlViaCommandLine(url2);
    143  await waitForSelectedSource(dbg, "command-line.js");
    144  await waitForSelectedLocation(dbg, secondLine, 1);
    145 
    146  await toolbox.destroy();
    147  await gBrowser.removeTab(tab);
    148 });
    149 
    150 // Fake opening an existing firefox instance with a URL
    151 // passed via `-url` command line argument
    152 function sendUrlViaCommandLine(url) {
    153  const cmdLine = Cu.createCommandLine(
    154    ["-url", url],
    155    null,
    156    Ci.nsICommandLine.STATE_REMOTE_EXPLICIT
    157  );
    158  startup.handle(cmdLine);
    159 
    160  // Return true if DevToolsStartup ignored the url
    161  // and let it be in the command line object.
    162  return cmdLine.findFlag("url", false) != -1;
    163 }