1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/js/xpconnect/tests/chrome/test_bug812415.xul Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,90 @@ 1.4 +<?xml version="1.0"?> 1.5 +<?xml-stylesheet type="text/css" href="chrome://global/skin"?> 1.6 +<?xml-stylesheet type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css"?> 1.7 +<!-- 1.8 +https://bugzilla.mozilla.org/show_bug.cgi?id=812415 1.9 +--> 1.10 +<window title="Mozilla Bug 812415" 1.11 + xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"> 1.12 + <script type="application/javascript" src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"/> 1.13 + 1.14 + <!-- test results are displayed in the html:body --> 1.15 + <body xmlns="http://www.w3.org/1999/xhtml"> 1.16 + <a href="https://bugzilla.mozilla.org/show_bug.cgi?id=812415" 1.17 + target="_blank">Mozilla Bug 812415</a> 1.18 + </body> 1.19 + 1.20 + <!-- test code goes here --> 1.21 + <script type="application/javascript"> 1.22 + <![CDATA[ 1.23 + /** Test for Bug 812415 and Bug 823348 **/ 1.24 + 1.25 + const Cu = Components.utils; 1.26 + SimpleTest.waitForExplicitFinish(); 1.27 + 1.28 + function testWaiving(iwin, sb) { 1.29 + sb.win = iwin; 1.30 + is(Cu.evalInSandbox('win', sb), iwin, "Basic identity works"); 1.31 + is(Cu.evalInSandbox('win.wrappedJSObject', sb), iwin.wrappedJSObject, "Waivers work via .wrappedJSObject"); 1.32 + is(Cu.evalInSandbox('XPCNativeWrapper.unwrap(win)', sb), iwin.wrappedJSObject, "Waivers work via XPCNativeWrapper.unwrap"); 1.33 + is(Cu.evalInSandbox('win.wrappedJSObject.document', sb), iwin.document.wrappedJSObject, "Waivers are deep"); 1.34 + } 1.35 + 1.36 + function checkThrows(expression, sb, msg) { 1.37 + var result = Cu.evalInSandbox('(function() { try { ' + expression + '; return "allowed"; } catch (e) { return e.toString(); }})();', sb); 1.38 + ok(!!/denied/.exec(result), msg); 1.39 + } 1.40 + 1.41 + function testAsymmetric(regular, expanded) { 1.42 + 1.43 + // Set up objects. 1.44 + expanded.regFun = Cu.evalInSandbox('function reg() { return 42; }; reg', regular); 1.45 + expanded.regObj = Cu.evalInSandbox('new Object({foo: 2})', regular); 1.46 + regular.expFun = Cu.evalInSandbox('function exp() { return 41; }; exp', expanded); 1.47 + regular.expObj = Cu.evalInSandbox('new Object({bar: 3})', expanded); 1.48 + 1.49 + // Check objects. 1.50 + is(Cu.evalInSandbox('regObj.foo', expanded), 2, "Expanded can see regular object prop"); 1.51 + checkThrows('expObj.bar', regular, "Regular shouldn't read properties"); 1.52 + Cu.evalInSandbox('regObj.foo = 20', expanded); 1.53 + is(expanded.regObj.foo, 20, "Expanded can set properties"); 1.54 + checkThrows('expFun.bar = 0', regular, "Regular shouldn't write properties"); 1.55 + 1.56 + // Check functions. 1.57 + is(Cu.evalInSandbox('regFun()', expanded), 42, "Expanded can call regular function"); 1.58 + checkThrows('expFun()', regular, "Regular cannot call expanded function"); 1.59 + is(Cu.evalInSandbox('regFun.name', expanded), 'reg', "Expanded can see regular function's name"); 1.60 + checkThrows('expFun.name', regular, "Regular can't see expanded function's name"); 1.61 + Cu.evalInSandbox('regFun.expando = 30', expanded); 1.62 + is(expanded.regFun.expando, 30, "Expanded can set expandos"); 1.63 + checkThrows('expFun.expando = 29', regular, "Regular can't set expandos"); 1.64 + 1.65 + // Check __proto__ stuff. 1.66 + is(Cu.evalInSandbox('regFun.__proto__', expanded), regular.Function.prototype, "expanded can get __proto__"); 1.67 + checkThrows('expFun.__proto__', regular, "regular can't use __proto__"); 1.68 + checkThrows('expFun.__proto__ = {}', regular, "regular can't mutate __proto__"); 1.69 + } 1.70 + 1.71 + function go() { 1.72 + var iwin = document.getElementById('ifr').contentWindow; 1.73 + 1.74 + // Make our sandboxes. We pass wantXrays=false for the nsEP to ensure that 1.75 + // the Xrays we get are the result of being an nsEP, not from the wantXrays 1.76 + // flag. 1.77 + var regular = new Components.utils.Sandbox(iwin); 1.78 + var expanded = new Components.utils.Sandbox([iwin], {wantXrays: false}); 1.79 + 1.80 + // Because of the crazy secret life of wantXrays, passing wantXrays=false 1.81 + // has the side effect of waiving Xrays on the returned sandbox. Undo that. 1.82 + expanded = XPCNativeWrapper(expanded); 1.83 + 1.84 + testWaiving(iwin, regular); 1.85 + testWaiving(iwin, expanded); 1.86 + testAsymmetric(regular, expanded); 1.87 + SimpleTest.finish(); 1.88 + } 1.89 + 1.90 + ]]> 1.91 + </script> 1.92 + <iframe id="ifr" onload="go();" src="http://example.org/tests/js/xpconnect/tests/mochitest/file_empty.html" /> 1.93 +</window>