js/xpconnect/tests/chrome/test_expandosharing.xul

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/js/xpconnect/tests/chrome/test_expandosharing.xul	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,145 @@
     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=758415
     1.9 +-->
    1.10 +<window title="Mozilla Bug 758415"
    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=758415"
    1.17 +     target="_blank">Mozilla Bug 758415</a>
    1.18 +  </body>
    1.19 +
    1.20 +  <!-- test code goes here -->
    1.21 +  <script type="application/javascript">
    1.22 +  <![CDATA[
    1.23 +
    1.24 +  /** Test for Cross-Origin Xray Expando Sharing. **/
    1.25 +  SimpleTest.waitForExplicitFinish();
    1.26 +  const Cu = Components.utils;
    1.27 +
    1.28 +  // Import our test JSM. We first strip the filename off
    1.29 +  // the chrome url, then append the jsm filename.
    1.30 +  var base = /.*\//.exec(window.location.href)[0];
    1.31 +  Cu.import(base + "file_expandosharing.jsm");
    1.32 +
    1.33 +  // Wait for all child frames to load.
    1.34 +  var gLoadCount = 0;
    1.35 +  function frameLoaded() {
    1.36 +    if (++gLoadCount == window.frames.length)
    1.37 +      go();
    1.38 +  }
    1.39 +
    1.40 +  function go() {
    1.41 +
    1.42 +    // Empower the content windows with some functions.
    1.43 +    var wins = document.getElementsByTagName('iframe');
    1.44 +    for (var i = 0; i < wins.length; ++i) {
    1.45 +      var win = wins[i].contentWindow.wrappedJSObject;
    1.46 +      win.ok = ok;
    1.47 +      win.is = is;
    1.48 +    }
    1.49 +
    1.50 +    // Grab references to the content windows. We abbreviate the origins as follows:
    1.51 +    // A: test1.example.org
    1.52 +    // B: test2.example.org
    1.53 +    // C: sub1.test1.example.org
    1.54 +    window.gWinA1 = document.getElementById('frameA1').contentWindow;
    1.55 +    window.gWinA2 = document.getElementById('frameA2').contentWindow;
    1.56 +    window.gWinA3 = document.getElementById('frameA3').contentWindow;
    1.57 +    window.gWinB = document.getElementById('frameB').contentWindow;
    1.58 +    window.gWinC = document.getElementById('frameC').contentWindow;
    1.59 +
    1.60 +    // Test expando sharing with a JSM for different types of Xrays.
    1.61 +    testJSM(XPCNativeWrapper(gWinC.wrappedJSObject.targetWN));
    1.62 +    testJSM(XPCNativeWrapper(gWinC.wrappedJSObject.targetDOM));
    1.63 +    testJSM(XPCNativeWrapper(gWinC.wrappedJSObject.targetJS));
    1.64 +
    1.65 +    // Make sure sandboxes never share expandos with anyone else.
    1.66 +    testSandbox(XPCNativeWrapper(gWinB.wrappedJSObject.targetWN));
    1.67 +    testSandbox(XPCNativeWrapper(gWinB.wrappedJSObject.targetDOM));
    1.68 +    testSandbox(XPCNativeWrapper(gWinB.wrappedJSObject.targetJS));
    1.69 +
    1.70 +    // Test Content Xrays.
    1.71 +    testContentXrays();
    1.72 +
    1.73 +    SimpleTest.finish();
    1.74 +  }
    1.75 +
    1.76 +  // Make sure that expandos are shared between us and a JSM.
    1.77 +  function testJSM(target) {
    1.78 +    target.numProp = 42;
    1.79 +    target.strProp = "foo";
    1.80 +    target.objProp = { bar: "baz" };
    1.81 +    checkFromJSM(target, is);
    1.82 +  }
    1.83 +
    1.84 +  function testSandbox(target) {
    1.85 +
    1.86 +    // This gets both run in this scope and the sandbox scope.
    1.87 +    var name = "harness";
    1.88 +    function placeExpando() {
    1.89 +      target.prop = name;
    1.90 +    }
    1.91 +
    1.92 +    // Set up the sandboxes.
    1.93 +    sb1 = Cu.Sandbox(window);
    1.94 +    sb2 = Cu.Sandbox(window);
    1.95 +    sb1.target = target;
    1.96 +    sb2.target = target;
    1.97 +    sb1.name = "sandbox1";
    1.98 +    sb2.name = "sandbox2";
    1.99 +    placeExpando();
   1.100 +    Cu.evalInSandbox(placeExpando.toSource() + "placeExpando();", sb1);
   1.101 +    Cu.evalInSandbox(placeExpando.toSource() + "placeExpando();", sb2);
   1.102 +
   1.103 +    // Make sure everyone sees a different value.
   1.104 +    is(target.prop, "harness", "Harness sees its own value");
   1.105 +    is(Cu.evalInSandbox("target.prop", sb1), "sandbox1", "Sandbox 1 sees its own value");
   1.106 +    is(Cu.evalInSandbox("target.prop", sb2), "sandbox2", "Sandbox 2 sees its own value");
   1.107 +  }
   1.108 +
   1.109 +  // Make sure that the origin tagging machinery works correctly and that we don't
   1.110 +  // mix up chrome and content expandos.
   1.111 +  function testContentXrays() {
   1.112 +
   1.113 +    // Give A1 and A3 xrays to (same-origin) A2.
   1.114 +    Components.utils.setWantXrays(gWinA1);
   1.115 +    Components.utils.setWantXrays(gWinA3);
   1.116 +
   1.117 +    gWinA1.wrappedJSObject.placeExpando('A1_expando', 11, gWinA2.document);
   1.118 +    gWinA3.wrappedJSObject.placeExpando('A3_expando', 33, gWinA2.document);
   1.119 +    gWinA2.document.Chrome_expando = 33;
   1.120 +
   1.121 +    is(gWinA2.document.Chrome_expando, 33, "Read chrome expando properly");
   1.122 +    is(typeof gWinA2.document.A1_expando, 'undefined', "Chrome doesn't see content expandos");
   1.123 +    is(typeof gWinA2.document.A3_expando, 'undefined', "Chrome doesn't see content expandos");
   1.124 +    gWinA1.wrappedJSObject.checkExpando('A1_expando', 11, gWinA2.document, "Content sees proper expandos");
   1.125 +    gWinA3.wrappedJSObject.checkExpando('A1_expando', 11, gWinA2.document, "Content sees proper expandos");
   1.126 +    gWinA1.wrappedJSObject.checkExpando('A3_expando', 33, gWinA2.document, "Content sees proper expandos");
   1.127 +    gWinA3.wrappedJSObject.checkExpando('A3_expando', 33, gWinA2.document, "Content sees proper expandos");
   1.128 +    gWinA1.wrappedJSObject.checkExpando('Chrome_expando', null, gWinA2.document, "Content doesn't see chrome expandos");
   1.129 +    gWinA3.wrappedJSObject.checkExpando('Chrome_expando', null, gWinA2.document, "Content doesn't see chrome expandos");
   1.130 +
   1.131 +    // We very explicitly do not support expando sharing via document.domain.
   1.132 +    // A comment in the implementation explains why.
   1.133 +    gWinA1.document.domain = 'test1.example.org';
   1.134 +    gWinA2.document.domain = 'test1.example.org';
   1.135 +    gWinA3.document.domain = 'test1.example.org';
   1.136 +    gWinC.document.domain = 'test1.example.org';
   1.137 +    gWinC.wrappedJSObject.checkExpando('A1_expando', null, gWinA2.document, "document.domain should have no effect here");
   1.138 +    gWinC.wrappedJSObject.checkExpando('A3_expando', null, gWinA2.document, "document.domain should have no effect here");
   1.139 +  }
   1.140 +
   1.141 +  ]]>
   1.142 +  </script>
   1.143 +  <iframe id="frameA1" onload="frameLoaded();" type="content" src="http://test1.example.org/tests/js/xpconnect/tests/mochitest/file_expandosharing.html" />
   1.144 +  <iframe id="frameA2" onload="frameLoaded();" type="content" src="http://test1.example.org/tests/js/xpconnect/tests/mochitest/file_expandosharing.html" />
   1.145 +  <iframe id="frameA3" onload="frameLoaded();" type="content" src="http://test1.example.org/tests/js/xpconnect/tests/mochitest/file_expandosharing.html" />
   1.146 +  <iframe id="frameB" onload="frameLoaded();" type="content" src="http://test2.example.org/tests/js/xpconnect/tests/mochitest/file_expandosharing.html" />
   1.147 +  <iframe id="frameC" onload="frameLoaded();" type="content" src="http://sub1.test1.example.org/tests/js/xpconnect/tests/mochitest/file_expandosharing.html" />
   1.148 +</window>

mercurial