test_documentdomain.xul 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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=601277
  6. -->
  7. <window title="Mozilla Bug 601277"
  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=601277"
  13. target="_blank">Mozilla Bug 601277</a>
  14. </body>
  15. <!-- test code goes here -->
  16. <script type="application/javascript">
  17. <![CDATA[
  18. /** Tests for document.domain. **/
  19. const Cu = Components.utils;
  20. SimpleTest.waitForExplicitFinish();
  21. // Wait for the frames to load.
  22. var gFramesLoaded = 0;
  23. function frameLoaded() {
  24. gFramesLoaded++;
  25. if (gFramesLoaded == document.getElementsByTagName('iframe').length)
  26. startTest();
  27. }
  28. function startTest() {
  29. // Grab all the content windows and waive Xray. Xray waivers only apply to
  30. // chrome, so we can pass these references directly to content.
  31. var win1A = document.getElementById('test1A').contentWindow.wrappedJSObject;
  32. var win1B = document.getElementById('test1B').contentWindow.wrappedJSObject;
  33. var win2 = document.getElementById('test2').contentWindow.wrappedJSObject;
  34. var winBase = document.getElementById('base').contentWindow.wrappedJSObject;
  35. // Check the basics.
  36. ok(win1A.tryToAccess(win1B),
  37. "Same-origin windows should grant access");
  38. ok(!win1A.tryToAccess(win2),
  39. "Cross-origin windows should not grant access");
  40. ok(!win1A.tryToAccess(winBase),
  41. "Subdomain windows should not receive access");
  42. // Store references now, while test1A and test1B are same-origin.
  43. win1A.storeReference(win1B);
  44. win1B.storeReference(win1A);
  45. ok(win1A.tryToAccessStored(), "Stored references work when same-origin");
  46. win1A.evalFromB = Cu.unwaiveXrays(win1B.eval); // Crashtest for bug 1040181.
  47. win1B.functionFromA = Cu.unwaiveXrays(win1A.Function); // Crashtest for bug 1040181.
  48. ok(!win1A.invokingFunctionThrowsSecurityException('evalFromB'), "Should allow before document.domain");
  49. ok(!win1B.invokingFunctionThrowsSecurityException('functionFromA'), "Should allow before document.domain");
  50. // Set document.domain on test1A. This should grant no access, since nobody
  51. // else set it.
  52. win1A.setDomain('example.org');
  53. ok(!win1A.tryToAccess(winBase), "base must collaborate too");
  54. ok(!winBase.tryToAccess(win1A), "base must collaborate too");
  55. ok(!win1A.tryToAccess(win1B), "No longer same-origin");
  56. ok(!win1A.tryToAccessStored(), "No longer same-origin");
  57. ok(!win1B.tryToAccess(win1A), "No longer same-origin");
  58. ok(!win1B.tryToAccessStored(), "No longer same-origin");
  59. ok(win1A.invokingFunctionThrowsSecurityException('evalFromB'), "No longer same-origin");
  60. ok(win1B.invokingFunctionThrowsSecurityException('functionFromA'), "No longer same-origin");
  61. // Set document.domain on test1B. Now we're cooking with gas.
  62. win1B.setDomain('example.org');
  63. ok(!win1B.tryToAccess(winBase), "base must collaborate too");
  64. ok(!winBase.tryToAccess(win1B), "base must collaborate too");
  65. ok(win1A.tryToAccess(win1B), "same-origin");
  66. ok(win1A.tryToAccessStored(), "same-origin");
  67. ok(win1B.tryToAccess(win1A), "same-origin");
  68. ok(win1B.tryToAccessStored(), "same-origin");
  69. // Explicitly collaborate with base.
  70. winBase.setDomain('example.org');
  71. ok(winBase.tryToAccess(win1A), "base collaborates");
  72. ok(win1A.tryToAccess(winBase), "base collaborates");
  73. // All done.
  74. SimpleTest.finish();
  75. }
  76. ]]>
  77. </script>
  78. <iframe id="test1A" onload="frameLoaded();" type="content"
  79. src="http://test1.example.org/tests/js/xpconnect/tests/mochitest/file_documentdomain.html" />
  80. <iframe id="test1B" onload="frameLoaded();" type="content"
  81. src="http://test1.example.org/tests/js/xpconnect/tests/mochitest/file_documentdomain.html" />
  82. <iframe id="test2" onload="frameLoaded();" type="content"
  83. src="http://test2.example.org/tests/js/xpconnect/tests/mochitest/file_documentdomain.html" />
  84. <iframe id="base" onload="frameLoaded();" type="content"
  85. src="http://example.org/tests/js/xpconnect/tests/mochitest/file_documentdomain.html" />
  86. </window>