summaryrefslogtreecommitdiffstats
path: root/devtools/shared/tests/xpcshell/test_invisible_loader.js
blob: d83efd9c2a8322c3cd8868120b21066779204323 (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
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

const { addDebuggerToGlobal } = ChromeUtils.importESModule(
  "resource://gre/modules/jsdebugger.sys.mjs"
);
addDebuggerToGlobal(globalThis);

/**
 * Ensure that sandboxes created via the Dev Tools loader respect the
 * invisibleToDebugger flag.
 */
function run_test() {
  visible_loader();
  invisible_loader();
  // TODO: invisibleToDebugger should be deprecated in favor of
  // useDistinctSystemPrincipalLoader, but we might move out from the loader
  // to using only standard imports instead.
  distinct_system_principal_loader();
}

function visible_loader() {
  const loader = new DevToolsLoader({
    invisibleToDebugger: false,
  });
  loader.require("resource://devtools/shared/indentation.js");

  const dbg = new Debugger();
  const sandbox = loader.loader.sharedGlobal;

  try {
    dbg.addDebuggee(sandbox);
    Assert.ok(true);
  } catch (e) {
    do_throw("debugger could not add visible value");
  }
}

function invisible_loader() {
  const loader = new DevToolsLoader({
    invisibleToDebugger: true,
  });
  loader.require("resource://devtools/shared/indentation.js");

  const dbg = new Debugger();
  const sandbox = loader.loader.sharedGlobal;

  try {
    dbg.addDebuggee(sandbox);
    do_throw("debugger added invisible value");
  } catch (e) {
    Assert.ok(true);
  }
}

function distinct_system_principal_loader() {
  const {
    useDistinctSystemPrincipalLoader,
    releaseDistinctSystemPrincipalLoader,
  } = ChromeUtils.importESModule(
    "resource://devtools/shared/loader/DistinctSystemPrincipalLoader.sys.mjs"
  );

  const requester = {};
  const loader = useDistinctSystemPrincipalLoader(requester);
  loader.require("resource://devtools/shared/indentation.js");

  const dbg = new Debugger();
  const sandbox = loader.loader.sharedGlobal;

  try {
    dbg.addDebuggee(sandbox);
    do_throw("debugger added invisible value");
  } catch (e) {
    Assert.ok(true);
  }
  releaseDistinctSystemPrincipalLoader(requester);
}