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.

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

mercurial