summaryrefslogtreecommitdiffstats
path: root/xpcom/tests/unit/test_console_service_callFunctionAndLogException.js
blob: eeb9ceb425044f44889da69d286a0e64f585500d (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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
let lastMessage;
const consoleListener = {
  observe(message) {
    dump(" >> new message: " + message.errorMessage + "\n");
    lastMessage = message;
  },
};
Services.console.registerListener(consoleListener);

// The Console Service notifies its listener after one event loop cycle.
// So wait for one tick after each action dispatching a message/error to the service.
function waitForATick() {
  return new Promise(resolve => Services.tm.dispatchToMainThread(resolve));
}

add_task(async function customScriptError() {
  const scriptError = Cc["@mozilla.org/scripterror;1"].createInstance(
    Ci.nsIScriptError
  );
  scriptError.init(
    "foo",
    "file.js",
    null,
    1,
    2,
    Ci.nsIScriptError.warningFlag,
    "some javascript"
  );
  Services.console.logMessage(scriptError);

  await waitForATick();

  Assert.equal(
    lastMessage,
    scriptError,
    "We receive the exact same nsIScriptError object"
  );

  Assert.equal(lastMessage.errorMessage, "foo");
  Assert.equal(lastMessage.sourceName, "file.js");
  Assert.equal(lastMessage.lineNumber, 1);
  Assert.equal(lastMessage.columnNumber, 2);
  Assert.equal(lastMessage.flags, Ci.nsIScriptError.warningFlag);
  Assert.equal(lastMessage.category, "some javascript");

  Assert.equal(
    lastMessage.stack,
    undefined,
    "Custom nsIScriptError object created from JS can't convey any stack"
  );
});

add_task(async function callFunctionAndLogExceptionWithChromeGlobal() {
  try {
    Services.console.callFunctionAndLogException(globalThis, function () {
      throw new Error("custom exception");
    });
    Assert.fail("callFunctionAndLogException should throw");
  } catch (e) {
    Assert.equal(
      e.name,
      "NS_ERROR_XPC_JAVASCRIPT_ERROR",
      "callFunctionAndLogException thrown"
    );
  }

  await waitForATick();

  Assert.ok(!!lastMessage, "Got the message");
  Assert.ok(
    lastMessage instanceof Ci.nsIScriptError,
    "This is a nsIScriptError"
  );

  Assert.equal(lastMessage.errorMessage, "Error: custom exception");
  Assert.equal(lastMessage.sourceName, _TEST_FILE);
  Assert.equal(lastMessage.lineNumber, 56);
  Assert.equal(lastMessage.columnNumber, 13);
  Assert.equal(lastMessage.flags, Ci.nsIScriptError.errorFlag);
  Assert.equal(lastMessage.category, "chrome javascript");
  Assert.ok(lastMessage.stack, "It has a stack");
  Assert.equal(lastMessage.stack.source, _TEST_FILE);
  Assert.equal(lastMessage.stack.line, 56);
  Assert.equal(lastMessage.stack.column, 13);
  Assert.ok(!!lastMessage.stack.parent, "stack has a parent frame");
  Assert.equal(
    lastMessage.innerWindowID,
    0,
    "The message isn't bound to any WindowGlobal"
  );
});

add_task(async function callFunctionAndLogExceptionWithContentGlobal() {
  const window = createContentWindow();
  try {
    Services.console.callFunctionAndLogException(window, function () {
      throw new Error("another custom exception");
    });
    Assert.fail("callFunctionAndLogException should throw");
  } catch (e) {
    Assert.equal(
      e.name,
      "NS_ERROR_XPC_JAVASCRIPT_ERROR",
      "callFunctionAndLogException thrown"
    );
  }

  await waitForATick();

  Assert.ok(!!lastMessage, "Got the message");
  Assert.ok(
    lastMessage instanceof Ci.nsIScriptError,
    "This is a nsIScriptError"
  );

  Assert.equal(lastMessage.errorMessage, "Error: another custom exception");
  Assert.equal(lastMessage.sourceName, _TEST_FILE);
  Assert.equal(lastMessage.lineNumber, 97);
  Assert.equal(lastMessage.columnNumber, 13);
  Assert.equal(lastMessage.flags, Ci.nsIScriptError.errorFlag);
  Assert.equal(lastMessage.category, "content javascript");
  Assert.ok(lastMessage.stack, "It has a stack");
  Assert.equal(lastMessage.stack.source, _TEST_FILE);
  Assert.equal(lastMessage.stack.line, 97);
  Assert.equal(lastMessage.stack.column, 13);
  Assert.ok(!!lastMessage.stack.parent, "stack has a parent frame");
  Assert.ok(
    !!window.windowGlobalChild.innerWindowId,
    "The window has a innerWindowId"
  );
  Assert.equal(
    lastMessage.innerWindowID,
    window.windowGlobalChild.innerWindowId,
    "The message is bound to the content window"
  );
});

add_task(async function callFunctionAndLogExceptionForContentScriptSandboxes() {
  const { sandbox, window } = createContentScriptSandbox();
  Cu.evalInSandbox(
    `function foo() { throw new Error("sandbox exception"); }`,
    sandbox,
    null,
    "sandbox-file.js",
    1,
    0
  );
  try {
    Services.console.callFunctionAndLogException(window, sandbox.foo);
    Assert.fail("callFunctionAndLogException should throw");
  } catch (e) {
    Assert.equal(
      e.name,
      "NS_ERROR_XPC_JAVASCRIPT_ERROR",
      "callFunctionAndLogException thrown"
    );
  }

  await waitForATick();

  Assert.ok(!!lastMessage, "Got the message");
  // Note that it is important to "instanceof" in order to expose the nsIScriptError attributes.
  Assert.ok(
    lastMessage instanceof Ci.nsIScriptError,
    "This is a nsIScriptError"
  );

  Assert.equal(lastMessage.errorMessage, "Error: sandbox exception");
  Assert.equal(lastMessage.sourceName, "sandbox-file.js");
  Assert.equal(lastMessage.lineNumber, 1);
  Assert.equal(lastMessage.columnNumber, 24);
  Assert.equal(lastMessage.flags, Ci.nsIScriptError.errorFlag);
  Assert.equal(lastMessage.category, "content javascript");
  Assert.ok(lastMessage.stack, "It has a stack");
  Assert.equal(lastMessage.stack.source, "sandbox-file.js");
  Assert.equal(lastMessage.stack.line, 1);
  Assert.equal(lastMessage.stack.column, 24);
  Assert.ok(!!lastMessage.stack.parent, "stack has a parent frame");
  Assert.ok(
    !!window.windowGlobalChild.innerWindowId,
    "The sandbox's prototype is a window and has a innerWindowId"
  );
  Assert.equal(
    lastMessage.innerWindowID,
    window.windowGlobalChild.innerWindowId,
    "The message is bound to the sandbox's prototype WindowGlobal"
  );
});

add_task(
  async function callFunctionAndLogExceptionForContentScriptSandboxesWrappedInChrome() {
    const { sandbox, window } = createContentScriptSandbox();
    Cu.evalInSandbox(
      `function foo() { throw new Error("sandbox exception"); }`,
      sandbox,
      null,
      "sandbox-file.js",
      1,
      0
    );
    try {
      Services.console.callFunctionAndLogException(window, function () {
        sandbox.foo();
      });
      Assert.fail("callFunctionAndLogException should throw");
    } catch (e) {
      Assert.equal(
        e.name,
        "NS_ERROR_XPC_JAVASCRIPT_ERROR",
        "callFunctionAndLogException thrown"
      );
    }

    await waitForATick();

    Assert.ok(!!lastMessage, "Got the message");
    // Note that it is important to "instanceof" in order to expose the nsIScriptError attributes.
    Assert.ok(
      lastMessage instanceof Ci.nsIScriptError,
      "This is a nsIScriptError"
    );

    Assert.ok(
      !!window.windowGlobalChild.innerWindowId,
      "The sandbox's prototype is a window and has a innerWindowId"
    );
    Assert.equal(
      lastMessage.innerWindowID,
      window.windowGlobalChild.innerWindowId,
      "The message is bound to the sandbox's prototype WindowGlobal"
    );
  }
);

add_task(function teardown() {
  Services.console.unregisterListener(consoleListener);
});

// We are in xpcshell, so we can't have a real DOM Window as in Firefox
// but let's try to have a fake one.
function createContentWindow() {
  const principal =
    Services.scriptSecurityManager.createContentPrincipalFromOrigin(
      "http://example.com/"
    );

  const webnav = Services.appShell.createWindowlessBrowser(false);

  webnav.docShell.createAboutBlankContentViewer(principal, principal);

  return webnav.document.defaultView;
}

// Create a Sandbox as in WebExtension content scripts
function createContentScriptSandbox() {
  const window = createContentWindow();
  // The sandboxPrototype is the key here in order to
  // make xpc::SandboxWindowOrNull ignore the sandbox
  // and instead retrieve its prototype and link the error message
  // to the window instead of the sandbox.
  return {
    sandbox: Cu.Sandbox(window, { sandboxPrototype: window }),
    window,
  };
}