tor-browser

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

test_blackboxing-01.js (4649B)


      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 basic black boxing.
      8 */
      9 
     10 var gDebuggee;
     11 var gThreadFront;
     12 
     13 add_task(
     14  threadFrontTest(async ({ threadFront, debuggee }) => {
     15    gThreadFront = threadFront;
     16    gDebuggee = debuggee;
     17    await testBlackBox();
     18  })
     19 );
     20 
     21 const BLACK_BOXED_URL = "http://example.com/blackboxme.js";
     22 const SOURCE_URL = "http://example.com/source.js";
     23 
     24 const testBlackBox = async function () {
     25  const packet = await executeOnNextTickAndWaitForPause(evalCode, gThreadFront);
     26 
     27  const bpSource = await getSourceById(gThreadFront, packet.frame.where.actor);
     28 
     29  await setBreakpoint(gThreadFront, { sourceUrl: bpSource.url, line: 2 });
     30  await resume(gThreadFront);
     31 
     32  let sourceForm = await getSourceForm(gThreadFront, BLACK_BOXED_URL);
     33 
     34  Assert.ok(
     35    !sourceForm.isBlackBoxed,
     36    "By default the source is not black boxed."
     37  );
     38 
     39  // Test that we can step into `doStuff` when we are not black boxed.
     40  await runTest(
     41    async function onSteppedLocation(location) {
     42      const source = await getSourceFormById(gThreadFront, location.actor);
     43      Assert.equal(source.url, BLACK_BOXED_URL);
     44      Assert.equal(location.line, 2);
     45    },
     46    async function onDebuggerStatementFrames(frames) {
     47      for (const frame of frames) {
     48        const source = await getSourceFormById(gThreadFront, frame.where.actor);
     49        Assert.ok(!source.isBlackBoxed);
     50      }
     51    }
     52  );
     53 
     54  const blackboxedSource = await getSource(gThreadFront, BLACK_BOXED_URL);
     55  await blackBox(blackboxedSource);
     56  sourceForm = await getSourceForm(gThreadFront, BLACK_BOXED_URL);
     57  Assert.ok(sourceForm.isBlackBoxed);
     58 
     59  // Test that we step through `doStuff` when we are black boxed and its frame
     60  // doesn't show up.
     61  await runTest(
     62    async function onSteppedLocation(location) {
     63      const source = await getSourceFormById(gThreadFront, location.actor);
     64      Assert.equal(source.url, SOURCE_URL);
     65      Assert.equal(location.line, 4);
     66    },
     67    async function onDebuggerStatementFrames(frames) {
     68      for (const frame of frames) {
     69        const source = await getSourceFormById(gThreadFront, frame.where.actor);
     70        if (source.url == BLACK_BOXED_URL) {
     71          Assert.ok(source.isBlackBoxed);
     72        } else {
     73          Assert.ok(!source.isBlackBoxed);
     74        }
     75      }
     76    }
     77  );
     78 
     79  await unBlackBox(blackboxedSource);
     80  sourceForm = await getSourceForm(gThreadFront, BLACK_BOXED_URL);
     81  Assert.ok(!sourceForm.isBlackBoxed);
     82 
     83  // Test that we can step into `doStuff` again.
     84  await runTest(
     85    async function onSteppedLocation(location) {
     86      const source = await getSourceFormById(gThreadFront, location.actor);
     87      Assert.equal(source.url, BLACK_BOXED_URL);
     88      Assert.equal(location.line, 2);
     89    },
     90    async function onDebuggerStatementFrames(frames) {
     91      for (const frame of frames) {
     92        const source = await getSourceFormById(gThreadFront, frame.where.actor);
     93        Assert.ok(!source.isBlackBoxed);
     94      }
     95    }
     96  );
     97 };
     98 
     99 function evalCode() {
    100  /* eslint-disable mozilla/var-only-at-top-level, no-undef */
    101  // prettier-ignore
    102  Cu.evalInSandbox(
    103    "" + function doStuff(k) { // line 1
    104      var arg = 15;            // line 2 - Step in here
    105      k(arg);                  // line 3
    106    },                         // line 4
    107    gDebuggee,
    108    "1.8",
    109    BLACK_BOXED_URL,
    110    1
    111  );
    112 
    113  // prettier-ignore
    114  Cu.evalInSandbox(
    115    "" + function runTest() { // line 1
    116      doStuff(                // line 2 - Break here
    117        function () {        // line 3 - Step through `doStuff` to here
    118          (() => {})();       // line 4
    119          debugger;           // line 5
    120        }                     // line 6
    121      );                      // line 7
    122    } + "\n"                  // line 8
    123    + "debugger;",            // line 9
    124    gDebuggee,
    125    "1.8",
    126    SOURCE_URL,
    127    1
    128  );
    129 }
    130 
    131 const runTest = async function (onSteppedLocation, onDebuggerStatementFrames) {
    132  let packet = await executeOnNextTickAndWaitForPause(
    133    gDebuggee.runTest,
    134    gThreadFront
    135  );
    136  Assert.equal(packet.why.type, "breakpoint");
    137 
    138  await stepIn(gThreadFront);
    139 
    140  const location = await getCurrentLocation();
    141  await onSteppedLocation(location);
    142 
    143  packet = await resumeAndWaitForPause(gThreadFront);
    144  Assert.equal(packet.why.type, "debuggerStatement");
    145 
    146  const { frames } = await getFrames(gThreadFront, 0, 100);
    147  await onDebuggerStatementFrames(frames);
    148 
    149  return resume(gThreadFront);
    150 };
    151 
    152 const getCurrentLocation = async function () {
    153  const response = await getFrames(gThreadFront, 0, 1);
    154  return response.frames[0].where;
    155 };