summaryrefslogtreecommitdiffstats
path: root/devtools/server/tests/xpcshell/test_blackboxing-01.js
blob: 981692dd6c836e2c5565e7da16ab20fe2cf2947b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

/**
 * Test basic black boxing.
 */

var gDebuggee;
var gThreadFront;

add_task(
  threadFrontTest(async ({ threadFront, debuggee }) => {
    gThreadFront = threadFront;
    gDebuggee = debuggee;
    await testBlackBox();
  })
);

const BLACK_BOXED_URL = "http://example.com/blackboxme.js";
const SOURCE_URL = "http://example.com/source.js";

const testBlackBox = async function () {
  const packet = await executeOnNextTickAndWaitForPause(evalCode, gThreadFront);

  const bpSource = await getSourceById(gThreadFront, packet.frame.where.actor);

  await setBreakpoint(gThreadFront, { sourceUrl: bpSource.url, line: 2 });
  await resume(gThreadFront);

  let sourceForm = await getSourceForm(gThreadFront, BLACK_BOXED_URL);

  Assert.ok(
    !sourceForm.isBlackBoxed,
    "By default the source is not black boxed."
  );

  // Test that we can step into `doStuff` when we are not black boxed.
  await runTest(
    async function onSteppedLocation(location) {
      const source = await getSourceFormById(gThreadFront, location.actor);
      Assert.equal(source.url, BLACK_BOXED_URL);
      Assert.equal(location.line, 2);
    },
    async function onDebuggerStatementFrames(frames) {
      for (const frame of frames) {
        const source = await getSourceFormById(gThreadFront, frame.where.actor);
        Assert.ok(!source.isBlackBoxed);
      }
    }
  );

  const blackboxedSource = await getSource(gThreadFront, BLACK_BOXED_URL);
  await blackBox(blackboxedSource);
  sourceForm = await getSourceForm(gThreadFront, BLACK_BOXED_URL);
  Assert.ok(sourceForm.isBlackBoxed);

  // Test that we step through `doStuff` when we are black boxed and its frame
  // doesn't show up.
  await runTest(
    async function onSteppedLocation(location) {
      const source = await getSourceFormById(gThreadFront, location.actor);
      Assert.equal(source.url, SOURCE_URL);
      Assert.equal(location.line, 4);
    },
    async function onDebuggerStatementFrames(frames) {
      for (const frame of frames) {
        const source = await getSourceFormById(gThreadFront, frame.where.actor);
        if (source.url == BLACK_BOXED_URL) {
          Assert.ok(source.isBlackBoxed);
        } else {
          Assert.ok(!source.isBlackBoxed);
        }
      }
    }
  );

  await unBlackBox(blackboxedSource);
  sourceForm = await getSourceForm(gThreadFront, BLACK_BOXED_URL);
  Assert.ok(!sourceForm.isBlackBoxed);

  // Test that we can step into `doStuff` again.
  await runTest(
    async function onSteppedLocation(location) {
      const source = await getSourceFormById(gThreadFront, location.actor);
      Assert.equal(source.url, BLACK_BOXED_URL);
      Assert.equal(location.line, 2);
    },
    async function onDebuggerStatementFrames(frames) {
      for (const frame of frames) {
        const source = await getSourceFormById(gThreadFront, frame.where.actor);
        Assert.ok(!source.isBlackBoxed);
      }
    }
  );
};

function evalCode() {
  /* eslint-disable mozilla/var-only-at-top-level, no-undef */
  // prettier-ignore
  Cu.evalInSandbox(
    "" + function doStuff(k) { // line 1
      var arg = 15;            // line 2 - Step in here
      k(arg);                  // line 3
    },                         // line 4
    gDebuggee,
    "1.8",
    BLACK_BOXED_URL,
    1
  );

  // prettier-ignore
  Cu.evalInSandbox(
    "" + function runTest() { // line 1
      doStuff(                // line 2 - Break here
        function () {        // line 3 - Step through `doStuff` to here
          (() => {})();       // line 4
          debugger;           // line 5
        }                     // line 6
      );                      // line 7
    } + "\n"                  // line 8
    + "debugger;",            // line 9
    gDebuggee,
    "1.8",
    SOURCE_URL,
    1
  );
}

const runTest = async function (onSteppedLocation, onDebuggerStatementFrames) {
  let packet = await executeOnNextTickAndWaitForPause(
    gDebuggee.runTest,
    gThreadFront
  );
  Assert.equal(packet.why.type, "breakpoint");

  await stepIn(gThreadFront);

  const location = await getCurrentLocation();
  await onSteppedLocation(location);

  packet = await resumeAndWaitForPause(gThreadFront);
  Assert.equal(packet.why.type, "debuggerStatement");

  const { frames } = await getFrames(gThreadFront, 0, 100);
  await onDebuggerStatementFrames(frames);

  return resume(gThreadFront);
};

const getCurrentLocation = async function () {
  const response = await getFrames(gThreadFront, 0, 1);
  return response.frames[0].where;
};