tor-browser

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

test_getReportLeaves_01.js (3425B)


      1 /* Any copyright is dedicated to the Public Domain.
      2   http://creativecommons.org/publicdomain/zero/1.0/ */
      3 "use strict";
      4 
      5 // Test basic functionality of `CensusUtils.getReportLeaves`.
      6 
      7 function run_test() {
      8  const BREAKDOWN = {
      9    by: "coarseType",
     10    objects: {
     11      by: "objectClass",
     12      then: { by: "count", count: true, bytes: true },
     13      other: { by: "count", count: true, bytes: true },
     14    },
     15    strings: { by: "count", count: true, bytes: true },
     16    scripts: {
     17      by: "filename",
     18      then: {
     19        by: "internalType",
     20        then: { by: "count", count: true, bytes: true },
     21      },
     22      noFilename: {
     23        by: "internalType",
     24        then: { by: "count", count: true, bytes: true },
     25      },
     26    },
     27    other: {
     28      by: "internalType",
     29      then: { by: "count", count: true, bytes: true },
     30    },
     31    domNode: {
     32      by: "descriptiveType",
     33      then: { by: "count", count: true, bytes: true },
     34    },
     35  };
     36 
     37  const REPORT = {
     38    objects: {
     39      Array: { count: 6, bytes: 60 },
     40      Function: { count: 1, bytes: 10 },
     41      Object: { count: 1, bytes: 10 },
     42      RegExp: { count: 1, bytes: 10 },
     43      other: { count: 0, bytes: 0 },
     44    },
     45    strings: { count: 1, bytes: 10 },
     46    scripts: {
     47      "foo.js": {
     48        JSScript: { count: 1, bytes: 10 },
     49        "js::jit::IonScript": { count: 1, bytes: 10 },
     50      },
     51      noFilename: {
     52        JSScript: { count: 1, bytes: 10 },
     53        "js::jit::IonScript": { count: 1, bytes: 10 },
     54      },
     55    },
     56    other: {
     57      "js::Shape": { count: 7, bytes: 70 },
     58      "js::BaseShape": { count: 1, bytes: 10 },
     59    },
     60    domNode: {},
     61  };
     62 
     63  const root = censusReportToCensusTreeNode(BREAKDOWN, REPORT);
     64  dumpn("CensusTreeNode tree = " + JSON.stringify(root, null, 4));
     65 
     66  (function assertEveryNodeCanFindItsLeaf(node) {
     67    if (node.reportLeafIndex) {
     68      const [leaf] = CensusUtils.getReportLeaves(
     69        new Set([node.reportLeafIndex]),
     70        BREAKDOWN,
     71        REPORT
     72      );
     73      ok(
     74        leaf,
     75        "Should be able to find leaf " +
     76          "for a node with a reportLeafIndex = " +
     77          node.reportLeafIndex
     78      );
     79    }
     80 
     81    if (node.children) {
     82      for (const child of node.children) {
     83        assertEveryNodeCanFindItsLeaf(child);
     84      }
     85    }
     86  })(root);
     87 
     88  // Test finding multiple leaves at a time.
     89 
     90  function find(name, node) {
     91    if (node.name === name) {
     92      return node;
     93    }
     94 
     95    if (node.children) {
     96      for (const child of node.children) {
     97        const found = find(name, child);
     98        if (found) {
     99          return found;
    100        }
    101      }
    102    }
    103 
    104    return undefined;
    105  }
    106 
    107  const arrayNode = find("Array", root);
    108  ok(arrayNode);
    109  equal(typeof arrayNode.reportLeafIndex, "number");
    110 
    111  const shapeNode = find("js::Shape", root);
    112  ok(shapeNode);
    113  equal(typeof shapeNode.reportLeafIndex, "number");
    114 
    115  const indices = new Set([
    116    arrayNode.reportLeafIndex,
    117    shapeNode.reportLeafIndex,
    118  ]);
    119  const leaves = CensusUtils.getReportLeaves(indices, BREAKDOWN, REPORT);
    120  equal(leaves.length, 2);
    121 
    122  // `getReportLeaves` does not guarantee order of the results, so handle both
    123  // cases.
    124  ok(leaves.some(l => l === REPORT.objects.Array));
    125  ok(leaves.some(l => l === REPORT.other["js::Shape"]));
    126 
    127  // Test that bad indices do not yield results.
    128 
    129  const none = CensusUtils.getReportLeaves(
    130    new Set([999999999999]),
    131    BREAKDOWN,
    132    REPORT
    133  );
    134  equal(none.length, 0);
    135 }