tor-browser

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

test_SaveHeapSnapshot.js (3527B)


      1 /* Any copyright is dedicated to the Public Domain.
      2   http://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 // Test the ChromeUtils interface.
      5 // eslint-disable-next-line
      6 if (typeof Debugger != "function") {
      7  const { addDebuggerToGlobal } = ChromeUtils.importESModule(
      8    "resource://gre/modules/jsdebugger.sys.mjs"
      9  );
     10  addDebuggerToGlobal(globalThis);
     11 }
     12 
     13 function run_test() {
     14  ok(ChromeUtils, "Should be able to get the ChromeUtils interface");
     15 
     16  testBadParameters();
     17  testGoodParameters();
     18 
     19  do_test_finished();
     20 }
     21 
     22 function testBadParameters() {
     23  Assert.throws(
     24    () => ChromeUtils.saveHeapSnapshot(),
     25    /NS_ERROR_ILLEGAL_VALUE/,
     26    "Should throw if arguments aren't passed in."
     27  );
     28 
     29  Assert.throws(
     30    () => ChromeUtils.saveHeapSnapshot(null),
     31    /NS_ERROR_ILLEGAL_VALUE/,
     32    "Should throw if boundaries isn't an object."
     33  );
     34 
     35  Assert.throws(
     36    () => ChromeUtils.saveHeapSnapshot({}),
     37    /NS_ERROR_ILLEGAL_VALUE/,
     38    "Should throw if the boundaries object doesn't have any properties."
     39  );
     40 
     41  Assert.throws(
     42    () => ChromeUtils.saveHeapSnapshot({ runtime: true, globals: [this] }),
     43    /NS_ERROR_ILLEGAL_VALUE/,
     44    "Should throw if the boundaries object has more than one property."
     45  );
     46 
     47  Assert.throws(
     48    () => ChromeUtils.saveHeapSnapshot({ debugger: {} }),
     49    /NS_ERROR_ILLEGAL_VALUE/,
     50    "Should throw if the debuggees object is not a Debugger object"
     51  );
     52 
     53  Assert.throws(
     54    () => ChromeUtils.saveHeapSnapshot({ globals: [{}] }),
     55    /NS_ERROR_ILLEGAL_VALUE/,
     56    "Should throw if the globals array contains non-global objects."
     57  );
     58 
     59  Assert.throws(
     60    () => ChromeUtils.saveHeapSnapshot({ runtime: false }),
     61    /NS_ERROR_ILLEGAL_VALUE/,
     62    "Should throw if runtime is supplied and is not true."
     63  );
     64 
     65  Assert.throws(
     66    () => ChromeUtils.saveHeapSnapshot({ globals: null }),
     67    /TypeError:.*can't be converted to a sequence/,
     68    "Should throw if globals is not an object."
     69  );
     70 
     71  Assert.throws(
     72    () => ChromeUtils.saveHeapSnapshot({ globals: {} }),
     73    /TypeError:.*can't be converted to a sequence/,
     74    "Should throw if globals is not an array."
     75  );
     76 
     77  Assert.throws(
     78    () => ChromeUtils.saveHeapSnapshot({ debugger: Debugger.prototype }),
     79    /NS_ERROR_ILLEGAL_VALUE/,
     80    "Should throw if debugger is the Debugger.prototype object."
     81  );
     82 
     83  Assert.throws(
     84    () =>
     85      ChromeUtils.saveHeapSnapshot({
     86        get globals() {
     87          return [this];
     88        },
     89      }),
     90    /NS_ERROR_ILLEGAL_VALUE/,
     91    "Should throw if boundaries property is a getter."
     92  );
     93 }
     94 
     95 const makeNewSandbox = () =>
     96  Cu.Sandbox(CC("@mozilla.org/systemprincipal;1", "nsIPrincipal")());
     97 
     98 function testGoodParameters() {
     99  const sandbox = makeNewSandbox();
    100  let dbg = new Debugger(sandbox);
    101 
    102  ChromeUtils.saveHeapSnapshot({ debugger: dbg });
    103  ok(true, "Should be able to save a snapshot for a debuggee global.");
    104 
    105  dbg = new Debugger();
    106  const sandboxes = Array(10).fill(null).map(makeNewSandbox);
    107  sandboxes.forEach(sb => dbg.addDebuggee(sb));
    108 
    109  ChromeUtils.saveHeapSnapshot({ debugger: dbg });
    110  ok(true, "Should be able to save a snapshot for many debuggee globals.");
    111 
    112  dbg = new Debugger();
    113  ChromeUtils.saveHeapSnapshot({ debugger: dbg });
    114  ok(true, "Should be able to save a snapshot with no debuggee globals.");
    115 
    116  ChromeUtils.saveHeapSnapshot({ globals: [this] });
    117  ok(true, "Should be able to save a snapshot for a specific global.");
    118 
    119  ChromeUtils.saveHeapSnapshot({ runtime: true });
    120  ok(true, "Should be able to save a snapshot of the full runtime.");
    121 }