tor-browser

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

test_command_line_handler.js (4508B)


      1 /* Any copyright is dedicated to the Public Domain.
      2 https://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 "use strict";
      5 
      6 const { sinon } = ChromeUtils.importESModule(
      7  "resource://testing-common/Sinon.sys.mjs"
      8 );
      9 
     10 const execProcess = sinon.stub();
     11 const sendCommandLine = sinon.stub().throws(Cr.NS_ERROR_NOT_AVAILABLE);
     12 const commandLineHandler = sinon.stub();
     13 
     14 add_setup(async () => {
     15  await initSelectableProfileService();
     16 
     17  sinon.replace(
     18    getSelectableProfileService(),
     19    "sendCommandLine",
     20    (path, args, raise) => sendCommandLine(path, [...args], raise)
     21  );
     22  sinon.replace(getSelectableProfileService(), "execProcess", execProcess);
     23 
     24  let { nsDefaultCommandLineHandler: browserClh } = ChromeUtils.importESModule(
     25    "resource:///modules/BrowserContentHandler.sys.mjs"
     26  );
     27 
     28  sinon.replace(browserClh.prototype, "handle", commandLineHandler);
     29 });
     30 
     31 function nextCall(stub) {
     32  let { promise, resolve } = Promise.withResolvers();
     33 
     34  stub.callsFake(() => resolve());
     35  return promise;
     36 }
     37 
     38 add_task(async function test_profiles_update() {
     39  let clh = Cc[
     40    "@mozilla.org/browser/selectable-profiles-service-clh;1"
     41  ].createInstance(Ci.nsICommandLineHandler);
     42 
     43  let observer = sinon.stub();
     44  Services.obs.addObserver(observer, "sps-profiles-updated");
     45  let observerArgsPromise = nextCall(observer);
     46 
     47  let cmdLine = Cu.createCommandLine(
     48    ["--profiles-updated"],
     49    null,
     50    Ci.nsICommandLine.STATE_REMOTE_AUTO
     51  );
     52 
     53  clh.handle(cmdLine);
     54 
     55  await observerArgsPromise;
     56 
     57  Assert.equal(observer.callCount, 1, "Should have called the observer");
     58  Assert.deepEqual(observer.firstCall.args, [
     59    null,
     60    "sps-profiles-updated",
     61    "remote",
     62  ]);
     63 
     64  Assert.ok(
     65    cmdLine.preventDefault,
     66    "Should have prevented the default handler"
     67  );
     68 
     69  Services.obs.removeObserver(observer, "sps-profiles-updated");
     70 });
     71 
     72 add_task(async function test_redirect_args() {
     73  if (Services.appinfo.OS !== "Darwin") {
     74    // This behaviour is specific to macOS
     75    return;
     76  }
     77 
     78  let clh = Cc[
     79    "@mozilla.org/browser/selectable-profiles-service-clh;1"
     80  ].createInstance(Ci.nsICommandLineHandler);
     81 
     82  let { rootDir } = getProfileService().currentProfile;
     83 
     84  let profilePath = rootDir.path;
     85 
     86  let cmdLine = Cu.createCommandLine(
     87    ["-url", "https://www.google.com/"],
     88    null,
     89    Ci.nsICommandLine.STATE_REMOTE_EXPLICIT
     90  );
     91 
     92  clh.handle(cmdLine);
     93 
     94  Assert.ok(
     95    cmdLine.preventDefault,
     96    "Should have prevented the default handler"
     97  );
     98 
     99  await nextCall(commandLineHandler);
    100 
    101  Assert.equal(
    102    commandLineHandler.callCount,
    103    1,
    104    "Should have called the main browser command line handler directly"
    105  );
    106 
    107  let [generatedCmdLine] = commandLineHandler.firstCall.args;
    108  Assert.equal(
    109    generatedCmdLine.length,
    110    2,
    111    "Should have generated a command line with the correct number of arguments"
    112  );
    113  Assert.equal(generatedCmdLine.getArgument(0), "-url");
    114  Assert.equal(
    115    generatedCmdLine.getArgument(1),
    116    "https://www.google.com/",
    117    "Should have generated a command line with the correct URL"
    118  );
    119 
    120  commandLineHandler.resetHistory();
    121 
    122  let profileData = readProfilesIni();
    123 
    124  for (let profile of profileData.profiles) {
    125    if (profile.storeID == getSelectableProfileService().storeID) {
    126      profile.path = profile.path.replace(rootDir.leafName, "other-profile");
    127 
    128      break;
    129    }
    130  }
    131 
    132  writeProfilesIni(profileData);
    133 
    134  rootDir.leafName = "other-profile";
    135 
    136  profilePath = rootDir.path;
    137 
    138  cmdLine = Cu.createCommandLine(
    139    ["-url", "https://www.example.com/", "-url", "https://www.mozilla.org/"],
    140    null,
    141    Ci.nsICommandLine.STATE_REMOTE_EXPLICIT
    142  );
    143 
    144  clh.handle(cmdLine);
    145 
    146  Assert.ok(
    147    cmdLine.preventDefault,
    148    "Should have prevented the default handler"
    149  );
    150 
    151  await nextCall(execProcess);
    152 
    153  Assert.equal(
    154    sendCommandLine.callCount,
    155    1,
    156    "Should have attempted to remote once"
    157  );
    158  Assert.deepEqual(
    159    sendCommandLine.firstCall.args,
    160    [
    161      profilePath,
    162      ["-url", "https://www.example.com/", "-url", "https://www.mozilla.org/"],
    163      true,
    164    ],
    165    "should have used the right arguments"
    166  );
    167 
    168  sendCommandLine.resetHistory();
    169 
    170  Assert.equal(execProcess.callCount, 1, "Should have attempted to exec once");
    171  Assert.deepEqual(
    172    execProcess.firstCall.args,
    173    [
    174      [
    175        "-foreground",
    176        "-url",
    177        "https://www.example.com/",
    178        "-url",
    179        "https://www.mozilla.org/",
    180      ],
    181    ],
    182    "should have used the right arguments"
    183  );
    184 
    185  execProcess.resetHistory();
    186 });