js/xpconnect/tests/chrome/test_evalInSandbox.xul

changeset 0
6474c204b198
equal deleted inserted replaced
-1:000000000000 0:e3e54bede657
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>
12
13 <!-- test results are displayed in the html:body -->
14 <body xmlns="http://www.w3.org/1999/xhtml">
15
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>
23
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);
30
31 function checkCrossOriginSandbox(sandbox)
32 {
33 is(utils.getClassName(sandbox),
34 "Proxy",
35 "sandbox was wrapped correctly");
36
37 is(utils.getClassName(Cu.evalInSandbox("this.document", sandbox)),
38 "Proxy",
39 "return value was rewrapped correctly");
40 }
41
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");
54
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");
59
60 }
61
62 function checkCrossOrigin(ifr) {
63 var win = ifr.contentWindow;
64 var sandbox =
65 new Cu.Sandbox(win, { sandboxPrototype: win, wantXrays: true } );
66
67 checkCrossOriginSandbox(sandbox);
68 checkCrossOriginXrayedSandbox(sandbox);
69
70 sandbox =
71 new Cu.Sandbox(win, { sandboxPrototype: win } );
72
73 checkCrossOriginSandbox(sandbox);
74 checkCrossOriginXrayedSandbox(sandbox);
75
76 sandbox =
77 new Cu.Sandbox(win, { sandboxPrototype: win, wantXrays: false } );
78
79 checkCrossOriginSandbox(sandbox);
80
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");
85
86 ok(("foo" in sandbox.document),
87 "must see expandos in wrappers obtained from the sandbox");
88
89 testDone();
90 }
91
92 function checkSameOrigin(ifr) {
93 var win = ifr.contentWindow;
94 var sandbox =
95 new Cu.Sandbox(win, { sandboxPrototype: win, wantXrays: true } );
96
97 ok(Cu.evalInSandbox("('foo' in this.document);", sandbox),
98 "must see expandos for a chrome sandbox");
99
100 sandbox =
101 new Cu.Sandbox(win, { sandboxPrototype: win } );
102
103 ok(Cu.evalInSandbox("('foo' in this.document);", sandbox),
104 "must see expandos for a chrome sandbox");
105
106 sandbox =
107 new Cu.Sandbox(win, { sandboxPrototype: win, wantXrays: false } );
108
109 ok(Cu.evalInSandbox("('foo' in this.document);", sandbox),
110 "can see expandos for a chrome sandbox");
111
112 testDone();
113 }
114
115 var testsRun = 0;
116 function testDone() {
117 if (++testsRun == 2)
118 SimpleTest.finish();
119 }
120
121 SimpleTest.waitForExplicitFinish();
122
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 }
130
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 }
138
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 }
145
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 }
153
154 Cu.import("resource://gre/modules/jsdebugger.jsm");
155 addDebuggerToGlobal(this);
156
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 }
165
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