js/xpconnect/tests/chrome/test_evalInSandbox.xul

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

     1 <?xml version="1.0"?>
     2 <?xml-stylesheet href="chrome://global/skin" type="text/css"?>
     3 <?xml-stylesheet href="chrome://mochikit/content/tests/SimpleTest/test.css"
     4                  type="text/css"?>
     5 <!--
     6 https://bugzilla.mozilla.org/show_bug.cgi?id=533596
     7 -->
     8 <window title="Mozilla Bug 533596"
     9   xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
    10   <script type="application/javascript"
    11           src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
    13   <!-- test results are displayed in the html:body -->
    14   <body xmlns="http://www.w3.org/1999/xhtml">
    16   <iframe src="http://example.org/tests/js/xpconnect/tests/mochitest/file_evalInSandbox.html"
    17           onload="checkCrossOrigin(this)">
    18   </iframe>
    19   <iframe src="chrome://mochitests/content/chrome/js/xpconnect/tests/chrome/file_evalInSandbox.html"
    20           onload="checkSameOrigin(this)">
    21   </iframe>
    22   </body>
    24   <!-- test code goes here -->
    25   <script type="application/javascript"><![CDATA[
    26       const Cu = Components.utils;
    27       const Ci = Components.interfaces;
    28       const utils = window.QueryInterface(Ci.nsIInterfaceRequestor)
    29                           .getInterface(Ci.nsIDOMWindowUtils);
    31       function checkCrossOriginSandbox(sandbox)
    32       {
    33           is(utils.getClassName(sandbox),
    34              "Proxy",
    35              "sandbox was wrapped correctly");
    37           is(utils.getClassName(Cu.evalInSandbox("this.document", sandbox)),
    38              "Proxy",
    39              "return value was rewrapped correctly");
    40       }
    42       function checkCrossOriginXrayedSandbox(sandbox)
    43       {
    44         ok(Cu.evalInSandbox("!('windowfoo' in window);", sandbox),
    45            "the window itself Xray is an XrayWrapper");
    46         ok(Cu.evalInSandbox("('wrappedJSObject' in this.document);", sandbox),
    47            "wrappers inside eIS are Xrays");
    48         ok(Cu.evalInSandbox("!('foo' in this.document);", sandbox),
    49            "must not see expandos");
    50         ok('wrappedJSObject' in Cu.evalInSandbox("this.document", sandbox),
    51            "wrappers returned from the sandbox are Xrays");
    52         ok(!("foo" in Cu.evalInSandbox("this.document", sandbox)),
    53            "must not see expandos in wrappers returned from the sandbox");
    55         ok('wrappedJSObject' in sandbox.document,
    56            "values obtained from the sandbox are Xrays");
    57         ok(!("foo" in sandbox.document),
    58            "must not see expandos in wrappers obtained from the sandbox");
    60       }
    62       function checkCrossOrigin(ifr) {
    63         var win = ifr.contentWindow;
    64         var sandbox =
    65           new Cu.Sandbox(win, { sandboxPrototype: win, wantXrays: true } );
    67         checkCrossOriginSandbox(sandbox);
    68         checkCrossOriginXrayedSandbox(sandbox);
    70         sandbox =
    71           new Cu.Sandbox(win, { sandboxPrototype: win } );
    73         checkCrossOriginSandbox(sandbox);
    74         checkCrossOriginXrayedSandbox(sandbox);
    76         sandbox =
    77           new Cu.Sandbox(win, { sandboxPrototype: win, wantXrays: false } );
    79         checkCrossOriginSandbox(sandbox);
    81         ok(Cu.evalInSandbox("('foo' in this.document);", sandbox),
    82            "can see expandos");
    83         ok(("foo" in Cu.evalInSandbox("this.document", sandbox)),
    84            "must see expandos in wrappers returned from the sandbox");
    86         ok(("foo" in sandbox.document),
    87            "must see expandos in wrappers obtained from the sandbox");
    89         testDone();
    90       }
    92       function checkSameOrigin(ifr) {
    93         var win = ifr.contentWindow;
    94         var sandbox =
    95           new Cu.Sandbox(win, { sandboxPrototype: win, wantXrays: true } );
    97         ok(Cu.evalInSandbox("('foo' in this.document);", sandbox),
    98            "must see expandos for a chrome sandbox");
   100         sandbox =
   101           new Cu.Sandbox(win, { sandboxPrototype: win } );
   103         ok(Cu.evalInSandbox("('foo' in this.document);", sandbox),
   104            "must see expandos for a chrome sandbox");
   106         sandbox =
   107           new Cu.Sandbox(win, { sandboxPrototype: win, wantXrays: false } );
   109         ok(Cu.evalInSandbox("('foo' in this.document);", sandbox),
   110            "can see expandos for a chrome sandbox");
   112         testDone();
   113       }
   115       var testsRun = 0;
   116       function testDone() {
   117         if (++testsRun == 2)
   118           SimpleTest.finish();
   119       }
   121       SimpleTest.waitForExplicitFinish();
   123       try {
   124         var sandbox = new Cu.Sandbox(this, { sandboxPrototype: undefined } );
   125         ok(false, "undefined is not a valid prototype");
   126       }
   127       catch (e) {
   128         ok(true, "undefined is not a valid prototype");
   129       }
   131       try {
   132         var sandbox = new Cu.Sandbox(this, { wantXrays: undefined } );
   133         ok(false, "undefined is not a valid value for wantXrays");
   134       }
   135       catch (e) {
   136         ok(true, "undefined is not a valid value for wantXrays");
   137       }
   139       // Crash test for bug 601829.
   140       try {
   141         Components.utils.evalInSandbox('', null);
   142       } catch (e) {
   143         ok(true, "didn't crash on a null sandbox object");
   144       }
   146       try {
   147         var sandbox = new Cu.Sandbox(this, { sameZoneAs: this } );
   148         ok(true, "sameZoneAs works");
   149       }
   150       catch (e) {
   151         ok(false, "sameZoneAs works");
   152       }
   154       Cu.import("resource://gre/modules/jsdebugger.jsm");
   155       addDebuggerToGlobal(this);
   157       try {
   158         let dbg = new Debugger();
   159         let sandbox = new Cu.Sandbox(this, { invisibleToDebugger: false });
   160         dbg.addDebuggee(sandbox);
   161         ok(true, "debugger added visible value");
   162       } catch(e) {
   163         ok(false, "debugger could not add visible value");
   164       }
   166       try {
   167         let dbg = new Debugger();
   168         let sandbox = new Cu.Sandbox(this, { invisibleToDebugger: true });
   169         dbg.addDebuggee(sandbox);
   170         ok(false, "debugger added invisible value");
   171       } catch(e) {
   172         ok(true, "debugger did not add invisible value");
   173       }
   174   ]]></script>
   175 </window>

mercurial