tor-browser

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

test_objectgrips-property-value-01.js (3544B)


      1 /* Any copyright is dedicated to the Public Domain.
      2   http://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 "use strict";
      5 
      6 Services.prefs.setBoolPref("security.allow_eval_with_system_principal", true);
      7 registerCleanupFunction(() => {
      8  Services.prefs.clearUserPref("security.allow_eval_with_system_principal");
      9 });
     10 
     11 add_task(
     12  threadFrontTest(async ({ threadFront, debuggee }) => {
     13    const packet = await executeOnNextTickAndWaitForPause(
     14      () => evalCode(debuggee),
     15      threadFront
     16    );
     17 
     18    const arg1 = packet.frame.arguments[0];
     19    Assert.equal(arg1.class, "Object");
     20 
     21    const objFront = threadFront.pauseGrip(arg1);
     22 
     23    const expectedValues = {
     24      stringProp: {
     25        return: "a value",
     26      },
     27      stringNormal: {
     28        return: "a value",
     29      },
     30      stringAbrupt: {
     31        throw: "a value",
     32      },
     33      objectNormal: {
     34        return: {
     35          _grip: {
     36            type: "object",
     37            class: "Object",
     38            ownPropertyLength: 1,
     39            preview: {
     40              kind: "Object",
     41              ownProperties: {
     42                prop: {
     43                  value: 4,
     44                },
     45              },
     46            },
     47          },
     48        },
     49      },
     50      objectAbrupt: {
     51        throw: {
     52          _grip: {
     53            type: "object",
     54            class: "Object",
     55            ownPropertyLength: 1,
     56            preview: {
     57              kind: "Object",
     58              ownProperties: {
     59                prop: {
     60                  value: 4,
     61                },
     62              },
     63            },
     64          },
     65        },
     66      },
     67      context: {
     68        return: "correct context",
     69      },
     70      method: {
     71        return: {
     72          _grip: {
     73            type: "object",
     74            class: "Function",
     75            name: "method",
     76          },
     77        },
     78      },
     79    };
     80 
     81    for (const [key, expected] of Object.entries(expectedValues)) {
     82      const { value } = await objFront.getPropertyValue(key, null);
     83      assert_completion(value, expected);
     84    }
     85 
     86    await threadFront.resume();
     87  })
     88 );
     89 
     90 function evalCode(debuggee) {
     91  debuggee.eval(
     92    // These arguments are tested.
     93    // eslint-disable-next-line no-unused-vars
     94    function stopMe(arg1) {
     95      debugger;
     96    }.toString()
     97  );
     98 
     99  debuggee.eval(`
    100    var obj = {
    101      stringProp: "a value",
    102      get stringNormal(){
    103        return "a value";
    104      },
    105      get stringAbrupt() {
    106        throw "a value";
    107      },
    108      get objectNormal() {
    109        return { prop: 4 };
    110      },
    111      get objectAbrupt() {
    112        throw { prop: 4 };
    113      },
    114      get context(){
    115        return this === obj ? "correct context" : "wrong context";
    116      },
    117      method() {
    118        return "a value";
    119      },
    120    };
    121    stopMe(obj);
    122  `);
    123 }
    124 
    125 function assert_completion(value, expected) {
    126  if (expected && "return" in expected) {
    127    assert_value(value.return, expected.return);
    128  }
    129  if (expected && "throw" in expected) {
    130    assert_value(value.throw, expected.throw);
    131  }
    132  if (!expected) {
    133    assert_value(value, expected);
    134  }
    135 }
    136 
    137 function assert_value(actual, expected) {
    138  Assert.equal(typeof actual, typeof expected);
    139 
    140  if (typeof expected === "object") {
    141    // Note: We aren't using deepEqual here because we're only doing a cursory
    142    // check of a few properties, not a full comparison of the result, since
    143    // the full outputs includes stuff like preview info that we don't need.
    144    for (const key of Object.keys(expected)) {
    145      assert_value(actual[key], expected[key]);
    146    }
    147  } else {
    148    Assert.equal(actual, expected);
    149  }
    150 }