tor-browser

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

browser_sendQuery.js (3140B)


      1 /* Any copyright is dedicated to the Public Domain.
      2   http://creativecommons.org/publicdomain/zero/1.0/ */
      3 "use strict";
      4 
      5 const ERROR_LINE_NUMBER = 29;
      6 const EXCEPTION_LINE_NUMBER = ERROR_LINE_NUMBER + 3;
      7 const ERROR_COLUMN_NUMBER = 31;
      8 const EXCEPTION_COLUMN_NUMBER = 22;
      9 
     10 function maybeAsyncStack(offset, column) {
     11  if (
     12    Services.prefs.getBoolPref(
     13      "javascript.options.asyncstack_capture_debuggee_only"
     14    )
     15  ) {
     16    return "";
     17  }
     18 
     19  let stack = Error().stack.replace(/^.*?\n/, "");
     20  return (
     21    "JSActor query*" +
     22    stack.replace(
     23      /^([^\n]+?):(\d+):\d+/,
     24      (m0, m1, m2) => `${m1}:${+m2 + offset}:${column}`
     25    )
     26  );
     27 }
     28 
     29 declTest("sendQuery Error", {
     30  async test(browser, _window) {
     31    let parent = browser.browsingContext.currentWindowGlobal.domProcess;
     32    let actorParent = parent.getActor("TestProcessActor");
     33 
     34    let asyncStack = maybeAsyncStack(2, 8);
     35    let error = await actorParent
     36      .sendQuery("error", { message: "foo" })
     37      .catch(e => e);
     38 
     39    is(error.message, "foo", "Error should have the correct message");
     40    is(error.name, "SyntaxError", "Error should have the correct name");
     41    is(
     42      error.stack,
     43      `receiveMessage@resource://testing-common/TestProcessActorChild.sys.mjs:${ERROR_LINE_NUMBER}:${ERROR_COLUMN_NUMBER}\n` +
     44        asyncStack,
     45      "Error should have the correct stack"
     46    );
     47  },
     48 });
     49 
     50 declTest("sendQuery Exception", {
     51  async test(browser, _window) {
     52    let parent = browser.browsingContext.currentWindowGlobal.domProcess;
     53    let actorParent = parent.getActor("TestProcessActor");
     54 
     55    let asyncStack = maybeAsyncStack(2, 8);
     56    let error = await actorParent
     57      .sendQuery("exception", {
     58        message: "foo",
     59        result: Cr.NS_ERROR_INVALID_ARG,
     60      })
     61      .catch(e => e);
     62 
     63    is(error.message, "foo", "Error should have the correct message");
     64    is(
     65      error.result,
     66      Cr.NS_ERROR_INVALID_ARG,
     67      "Error should have the correct result code"
     68    );
     69    is(
     70      error.stack,
     71      `receiveMessage@resource://testing-common/TestProcessActorChild.sys.mjs:${EXCEPTION_LINE_NUMBER}:${EXCEPTION_COLUMN_NUMBER}\n` +
     72        asyncStack,
     73      "Error should have the correct stack"
     74    );
     75  },
     76 });
     77 
     78 declTest("sendQuery testing", {
     79  async test(browser) {
     80    let parent = browser.browsingContext.currentWindowGlobal.domProcess;
     81    let actorParent = parent.getActor("TestProcessActor");
     82    ok(actorParent, "JSWindowActorParent should have value.");
     83 
     84    let { result } = await actorParent.sendQuery("asyncAdd", { a: 10, b: 20 });
     85    is(result, 30);
     86  },
     87 });
     88 
     89 declTest("same process sendQuery inputStream testing", {
     90  includeParent: true,
     91  url: "about:robots",
     92  async test(browser) {
     93    await SpecialPowers.spawn(browser, [], async function () {
     94      let child = ChromeUtils.domProcessChild;
     95      let actorChild = child.getActor("TestProcessActor");
     96      Assert.ok(actorChild, "JSProcessActorChild should have value.");
     97 
     98      let inputStream = await actorChild.requestInputStream();
     99      Assert.ok(
    100        inputStream.QueryInterface(Ci.nsIInputStream),
    101        "Got back an nsIInputStream"
    102      );
    103    });
    104  },
    105 });