tor-browser

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

shell-principals.js (1919B)


      1 // Test the JS shell's toy principals.
      2 
      3 var count = 0;
      4 
      5 // Given a string of letters |expected|, say "abc", assert that the stack
      6 // contains calls to a series of functions named by the next letter from
      7 // the string, say a, b, and then c. Younger frames appear earlier in
      8 // |expected| than older frames.
      9 function check(expected, stack) {
     10  print("check(" + JSON.stringify(expected) + ") against:\n" + stack);
     11  count++;
     12 
     13  // Extract only the function names from the stack trace. Omit the frames
     14  // for the top-level evaluation, if it is present.
     15  var split = stack.split(/(.)?@.*\n/).slice(0, -1);
     16  if (split[split.length - 1] === undefined)
     17    split = split.slice(0, -2);
     18 
     19  // Check the function names against the expected sequence.
     20  assertEq(split.length, expected.length * 2);
     21  for (var i = 0; i < expected.length; i++)
     22    assertEq(split[i * 2 + 1], expected[i]);
     23 }
     24 
     25 var low = newGlobal({ principal: 0 });
     26 var mid = newGlobal({ principal: 0xffff });
     27 var high = newGlobal({ principal: 0xfffff });
     28 
     29     eval('function a() { check("a",        Error().stack); b(); }');
     30 low .eval('function b() { check("b",        Error().stack); c(); }');
     31 mid .eval('function c() { check("cba",      Error().stack); d(); }');
     32 high.eval('function d() { check("dcba",     Error().stack); e(); }');
     33 
     34 // Globals created with no explicit principals get 0xffff.
     35     eval('function e() { check("ecba",     Error().stack); f(); }');
     36 
     37 low .eval('function f() { check("fb",       Error().stack); g(); }');
     38 mid .eval('function g() { check("gfecba",   Error().stack); h(); }');
     39 high.eval('function h() { check("hgfedcba", Error().stack);      }');
     40 
     41 // Make everyone's functions visible to each other, as needed.
     42     b = low .b;
     43 low .c = mid .c;
     44 mid .d = high.d;
     45 high.e =      e;
     46     f = low .f;
     47 low .g = mid .g;
     48 mid .h = high.h;
     49 
     50 low.check = mid.check = high.check = check;
     51 
     52 // Kick the whole process off.
     53 a();
     54 
     55 assertEq(count, 8);