tor-browser

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

browser_sendQuery.js (3503B)


      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 = 37;
      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;
     32    let actorParent = parent.getActor("TestWindow");
     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/TestWindowChild.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;
     53    let actorParent = parent.getActor("TestWindow");
     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/TestWindowChild.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;
     81    let actorParent = parent.getActor("TestWindow");
     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("sendQuery in-process early lifetime", {
     90  url: "about:mozilla",
     91  allFrames: true,
     92 
     93  async test(browser) {
     94    let iframe = browser.contentDocument.createElement("iframe");
     95    browser.contentDocument.body.appendChild(iframe);
     96    let wgc = iframe.contentWindow.windowGlobalChild;
     97    let actorChild = wgc.getActor("TestWindow");
     98    let { result } = await actorChild.sendQuery("asyncMul", { a: 10, b: 20 });
     99    is(result, 200);
    100  },
    101 });
    102 
    103 declTest("sendQuery unserializable reply", {
    104  async test(browser) {
    105    let parent = browser.browsingContext.currentWindowGlobal;
    106    let actorParent = parent.getActor("TestWindow");
    107    ok(actorParent, "JSWindowActorParent should have value");
    108 
    109    try {
    110      await actorParent.sendQuery("noncloneReply", {});
    111      ok(false, "expected noncloneReply to be rejected");
    112    } catch (error) {
    113      ok(
    114        error.message.includes("message reply cannot be cloned"),
    115        "Error should have the correct message"
    116      );
    117    }
    118  },
    119 });