test_expandosharing.xul 6.4 KB

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