js/xpconnect/tests/chrome/test_expandosharing.xul

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 <?xml version="1.0"?>
michael@0 2 <?xml-stylesheet type="text/css" href="chrome://global/skin"?>
michael@0 3 <?xml-stylesheet type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css"?>
michael@0 4 <!--
michael@0 5 https://bugzilla.mozilla.org/show_bug.cgi?id=758415
michael@0 6 -->
michael@0 7 <window title="Mozilla Bug 758415"
michael@0 8 xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
michael@0 9 <script type="application/javascript" src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"/>
michael@0 10
michael@0 11 <!-- test results are displayed in the html:body -->
michael@0 12 <body xmlns="http://www.w3.org/1999/xhtml">
michael@0 13 <a href="https://bugzilla.mozilla.org/show_bug.cgi?id=758415"
michael@0 14 target="_blank">Mozilla Bug 758415</a>
michael@0 15 </body>
michael@0 16
michael@0 17 <!-- test code goes here -->
michael@0 18 <script type="application/javascript">
michael@0 19 <![CDATA[
michael@0 20
michael@0 21 /** Test for Cross-Origin Xray Expando Sharing. **/
michael@0 22 SimpleTest.waitForExplicitFinish();
michael@0 23 const Cu = Components.utils;
michael@0 24
michael@0 25 // Import our test JSM. We first strip the filename off
michael@0 26 // the chrome url, then append the jsm filename.
michael@0 27 var base = /.*\//.exec(window.location.href)[0];
michael@0 28 Cu.import(base + "file_expandosharing.jsm");
michael@0 29
michael@0 30 // Wait for all child frames to load.
michael@0 31 var gLoadCount = 0;
michael@0 32 function frameLoaded() {
michael@0 33 if (++gLoadCount == window.frames.length)
michael@0 34 go();
michael@0 35 }
michael@0 36
michael@0 37 function go() {
michael@0 38
michael@0 39 // Empower the content windows with some functions.
michael@0 40 var wins = document.getElementsByTagName('iframe');
michael@0 41 for (var i = 0; i < wins.length; ++i) {
michael@0 42 var win = wins[i].contentWindow.wrappedJSObject;
michael@0 43 win.ok = ok;
michael@0 44 win.is = is;
michael@0 45 }
michael@0 46
michael@0 47 // Grab references to the content windows. We abbreviate the origins as follows:
michael@0 48 // A: test1.example.org
michael@0 49 // B: test2.example.org
michael@0 50 // C: sub1.test1.example.org
michael@0 51 window.gWinA1 = document.getElementById('frameA1').contentWindow;
michael@0 52 window.gWinA2 = document.getElementById('frameA2').contentWindow;
michael@0 53 window.gWinA3 = document.getElementById('frameA3').contentWindow;
michael@0 54 window.gWinB = document.getElementById('frameB').contentWindow;
michael@0 55 window.gWinC = document.getElementById('frameC').contentWindow;
michael@0 56
michael@0 57 // Test expando sharing with a JSM for different types of Xrays.
michael@0 58 testJSM(XPCNativeWrapper(gWinC.wrappedJSObject.targetWN));
michael@0 59 testJSM(XPCNativeWrapper(gWinC.wrappedJSObject.targetDOM));
michael@0 60 testJSM(XPCNativeWrapper(gWinC.wrappedJSObject.targetJS));
michael@0 61
michael@0 62 // Make sure sandboxes never share expandos with anyone else.
michael@0 63 testSandbox(XPCNativeWrapper(gWinB.wrappedJSObject.targetWN));
michael@0 64 testSandbox(XPCNativeWrapper(gWinB.wrappedJSObject.targetDOM));
michael@0 65 testSandbox(XPCNativeWrapper(gWinB.wrappedJSObject.targetJS));
michael@0 66
michael@0 67 // Test Content Xrays.
michael@0 68 testContentXrays();
michael@0 69
michael@0 70 SimpleTest.finish();
michael@0 71 }
michael@0 72
michael@0 73 // Make sure that expandos are shared between us and a JSM.
michael@0 74 function testJSM(target) {
michael@0 75 target.numProp = 42;
michael@0 76 target.strProp = "foo";
michael@0 77 target.objProp = { bar: "baz" };
michael@0 78 checkFromJSM(target, is);
michael@0 79 }
michael@0 80
michael@0 81 function testSandbox(target) {
michael@0 82
michael@0 83 // This gets both run in this scope and the sandbox scope.
michael@0 84 var name = "harness";
michael@0 85 function placeExpando() {
michael@0 86 target.prop = name;
michael@0 87 }
michael@0 88
michael@0 89 // Set up the sandboxes.
michael@0 90 sb1 = Cu.Sandbox(window);
michael@0 91 sb2 = Cu.Sandbox(window);
michael@0 92 sb1.target = target;
michael@0 93 sb2.target = target;
michael@0 94 sb1.name = "sandbox1";
michael@0 95 sb2.name = "sandbox2";
michael@0 96 placeExpando();
michael@0 97 Cu.evalInSandbox(placeExpando.toSource() + "placeExpando();", sb1);
michael@0 98 Cu.evalInSandbox(placeExpando.toSource() + "placeExpando();", sb2);
michael@0 99
michael@0 100 // Make sure everyone sees a different value.
michael@0 101 is(target.prop, "harness", "Harness sees its own value");
michael@0 102 is(Cu.evalInSandbox("target.prop", sb1), "sandbox1", "Sandbox 1 sees its own value");
michael@0 103 is(Cu.evalInSandbox("target.prop", sb2), "sandbox2", "Sandbox 2 sees its own value");
michael@0 104 }
michael@0 105
michael@0 106 // Make sure that the origin tagging machinery works correctly and that we don't
michael@0 107 // mix up chrome and content expandos.
michael@0 108 function testContentXrays() {
michael@0 109
michael@0 110 // Give A1 and A3 xrays to (same-origin) A2.
michael@0 111 Components.utils.setWantXrays(gWinA1);
michael@0 112 Components.utils.setWantXrays(gWinA3);
michael@0 113
michael@0 114 gWinA1.wrappedJSObject.placeExpando('A1_expando', 11, gWinA2.document);
michael@0 115 gWinA3.wrappedJSObject.placeExpando('A3_expando', 33, gWinA2.document);
michael@0 116 gWinA2.document.Chrome_expando = 33;
michael@0 117
michael@0 118 is(gWinA2.document.Chrome_expando, 33, "Read chrome expando properly");
michael@0 119 is(typeof gWinA2.document.A1_expando, 'undefined', "Chrome doesn't see content expandos");
michael@0 120 is(typeof gWinA2.document.A3_expando, 'undefined', "Chrome doesn't see content expandos");
michael@0 121 gWinA1.wrappedJSObject.checkExpando('A1_expando', 11, gWinA2.document, "Content sees proper expandos");
michael@0 122 gWinA3.wrappedJSObject.checkExpando('A1_expando', 11, gWinA2.document, "Content sees proper expandos");
michael@0 123 gWinA1.wrappedJSObject.checkExpando('A3_expando', 33, gWinA2.document, "Content sees proper expandos");
michael@0 124 gWinA3.wrappedJSObject.checkExpando('A3_expando', 33, gWinA2.document, "Content sees proper expandos");
michael@0 125 gWinA1.wrappedJSObject.checkExpando('Chrome_expando', null, gWinA2.document, "Content doesn't see chrome expandos");
michael@0 126 gWinA3.wrappedJSObject.checkExpando('Chrome_expando', null, gWinA2.document, "Content doesn't see chrome expandos");
michael@0 127
michael@0 128 // We very explicitly do not support expando sharing via document.domain.
michael@0 129 // A comment in the implementation explains why.
michael@0 130 gWinA1.document.domain = 'test1.example.org';
michael@0 131 gWinA2.document.domain = 'test1.example.org';
michael@0 132 gWinA3.document.domain = 'test1.example.org';
michael@0 133 gWinC.document.domain = 'test1.example.org';
michael@0 134 gWinC.wrappedJSObject.checkExpando('A1_expando', null, gWinA2.document, "document.domain should have no effect here");
michael@0 135 gWinC.wrappedJSObject.checkExpando('A3_expando', null, gWinA2.document, "document.domain should have no effect here");
michael@0 136 }
michael@0 137
michael@0 138 ]]>
michael@0 139 </script>
michael@0 140 <iframe id="frameA1" onload="frameLoaded();" type="content" src="http://test1.example.org/tests/js/xpconnect/tests/mochitest/file_expandosharing.html" />
michael@0 141 <iframe id="frameA2" onload="frameLoaded();" type="content" src="http://test1.example.org/tests/js/xpconnect/tests/mochitest/file_expandosharing.html" />
michael@0 142 <iframe id="frameA3" onload="frameLoaded();" type="content" src="http://test1.example.org/tests/js/xpconnect/tests/mochitest/file_expandosharing.html" />
michael@0 143 <iframe id="frameB" onload="frameLoaded();" type="content" src="http://test2.example.org/tests/js/xpconnect/tests/mochitest/file_expandosharing.html" />
michael@0 144 <iframe id="frameC" onload="frameLoaded();" type="content" src="http://sub1.test1.example.org/tests/js/xpconnect/tests/mochitest/file_expandosharing.html" />
michael@0 145 </window>

mercurial