tor-browser

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

shortestPaths.js (2984B)


      1 // The shortestPaths function exists solely to let the fuzzers go to town and
      2 // exercise the code paths it calls into, hence the only things to assert
      3 // relate to the shortestPaths test function API.
      4 //
      5 // The actual behavior of JS::ubi::ShortestPaths is tested in
      6 // js/src/jsapi-tests/testUbiNode.cpp, where we can actually control the
      7 // structure of the heap graph to test specific shapes.
      8 
      9 function f(x) {
     10  return x + x;
     11 }
     12 
     13 var g = f.bind(null, 5);
     14 
     15 var o = {
     16  p: g
     17 };
     18 
     19 function describe(v) {
     20  return v === undefined ? "(undefined)"
     21    : v === null ? "(null)"
     22    : typeof(v) === "object" ? Object.prototype.toString.call(v)
     23    : typeof(v);
     24 }
     25 
     26 function dumpPaths(results) {
     27  results = results.map(paths =>
     28    paths.map(path =>
     29      path.map(({predecessor, edge}) =>
     30        predecessor !== undefined ?
     31          { predecessor: describe(predecessor), edge }
     32          :
     33          { edge }
     34      )
     35    )
     36  );
     37  print(JSON.stringify(results, null, 2));
     38 }
     39 
     40 print("shortestPaths([Object, f, o.p], {start: this, maxNumPaths: 5})");
     41 var paths = shortestPaths([Object, f, o.p], {start: this, maxNumPaths: 5});
     42 dumpPaths(paths);
     43 
     44 print();
     45 print("shortestPaths([f], {start: o, maxNumPaths: 1})")
     46 paths = shortestPaths([f], {start: o, maxNumPaths: 1});
     47 dumpPaths(paths);
     48 
     49 print();
     50 print("shortestPaths([f], {start: this, maxNumPaths: 5})")
     51 paths = shortestPaths([f], {start: this, maxNumPaths: 5});
     52 dumpPaths(paths);
     53 
     54 print();
     55 print("shortestPaths([f], {maxNumPaths: 5})")
     56 paths = shortestPaths([f], {maxNumPaths: 5});
     57 dumpPaths(paths);
     58 assertEq(paths[0].length <= 5, true, "Too many paths reported");
     59 
     60 paths = shortestPaths([f, g]);
     61 assertEq(paths.length, 2, "Two sets of paths expected");
     62 
     63 paths = shortestPaths([f], {maxNumPaths: 1});
     64 assertEq(paths[0].length, 1, "Single path expected");
     65 
     66 print();
     67 print("shortestPaths([1234n])")
     68 paths = shortestPaths([1234n]);
     69 dumpPaths(paths);
     70 
     71 // Error messages are more generic under PBL.
     72 if (!getBuildConfiguration('pbl')) {
     73    var exc;
     74 
     75    try { paths = shortestPaths(); } catch (exc) { e = ""+exc; };
     76    assertEq(e.includes("TypeError") && e.includes("1 argument required"), true);
     77 
     78    try { paths = shortestPaths(100, {}); } catch (exc) { e = ""+exc; };
     79    assertEq(e, "TypeError: 100 is not an array object");
     80 
     81    try { paths = shortestPaths([f], {start: 200}); } catch (exc) { e = ""+exc; };
     82    assertEq(e, "TypeError: 200 is not a GC thing");
     83 
     84    try { paths = shortestPaths([f, {}, {}, {}], { maxNumPaths: 0x40000000 }); } catch (exc) { e = "" + exc; };
     85    assertEq(e, "out of memory");
     86 
     87    try { paths = shortestPaths([f], { maxNumPaths: -1 }); } catch (exc) { e = "" + exc; };
     88    assertEq(e, "TypeError: -1 is not greater than 0");
     89 
     90    // Bug 1799824.
     91    let arr = [{}];
     92    let objWithGetter = {get start() { arr.length = 0; return {}; }};
     93    try { paths = shortestPaths(arr, objWithGetter); } catch (exc) { e = ""+exc; }
     94    assertEq(e, "TypeError: arr is not a dense array object with one or more elements");
     95 }