tor-browser

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

test_json_parse_with_source.js (2307B)


      1 'use strict';
      2 
      3 const { addDebuggerToGlobal } = ChromeUtils.importESModule(
      4  "resource://gre/modules/jsdebugger.sys.mjs"
      5 );
      6 
      7 const SYSTEM_PRINCIPAL = Cc["@mozilla.org/systemprincipal;1"].createInstance(
      8  Ci.nsIPrincipal
      9 );
     10 
     11 function addTestingFunctionsToGlobal(global) {
     12  global.eval(
     13    `
     14      const testingFunctions = Cu.getJSTestingFunctions();
     15      for (let k in testingFunctions) {
     16 
     17        this[k] = testingFunctions[k];
     18      }
     19      `
     20  );
     21  if (!global.print) {
     22    global.print = info;
     23  }
     24  if (!global.newGlobal) {
     25    global.newGlobal = newGlobal;
     26  }
     27  if (!global.Debugger) {
     28    addDebuggerToGlobal(global);
     29  }
     30 }
     31 
     32 addTestingFunctionsToGlobal(this);
     33 
     34 /* Create a new global, with all the JS shell testing functions. Similar to the
     35 * newGlobal function exposed to JS shells, and useful for porting JS shell
     36 * tests to xpcshell tests.
     37 */
     38 function newGlobal(args) {
     39  const global = new Cu.Sandbox(SYSTEM_PRINCIPAL, {
     40    freshCompartment: true,
     41    ...args,
     42  });
     43  addTestingFunctionsToGlobal(global);
     44  return global;
     45 }
     46 
     47 add_task(function test_json_parse_with_source_xrays() {
     48  let sandbox = new Cu.Sandbox("about:blank");
     49 
     50  var sourceWrapper = Cu.evalInSandbox("JSON.parse('5.0', (k,v,{source}) => ({src: source, val: v}));", sandbox);
     51  Assert.deepEqual(sourceWrapper, {src: "5.0", val: 5});
     52  sandbox.reviver = (k,v,{source}) => { return { orig: source }};
     53  sourceWrapper = Cu.evalInSandbox("JSON.parse('2.4', reviver);", sandbox);
     54  Assert.deepEqual(sourceWrapper, { orig: "2.4"});
     55 
     56  // Get rid of the extra global when experimental.json_parse_with_source pref is removed
     57  var g = newGlobal({ newCompartment: true });
     58  Cu.evalInSandbox(`
     59    let other = new Cu.Sandbox("about:blank");
     60    let rawWrapper = other.eval('JSON.rawJSON(4.32)');
     61  `, g);
     62  Assert.ok(g.eval("JSON.isRawJSON(rawWrapper);"));
     63  Assert.equal(Cu.evalInSandbox("JSON.stringify(rawWrapper)", g), "4.32");
     64 
     65  // rawJSON is a data property, so the Xray should hide it
     66  Assert.equal(g.eval("rawWrapper.wrappedJSObject.rawJSON"), "4.32");
     67  Assert.equal(g.eval("rawWrapper.rawJSON"), undefined);
     68 
     69  let src = Cu.evalInSandbox(`
     70    other.eval('JSON.parse("4.32", (k,v,{source}) => { return {source,v}})');
     71  `, g);
     72  Assert.ok(Cu.isXrayWrapper(src));
     73  Assert.deepEqual(src, {source:"4.32", v:4.32});
     74 });