test_bug802557.html 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <!DOCTYPE HTML>
  2. <html>
  3. <!--
  4. https://bugzilla.mozilla.org/show_bug.cgi?id=802557
  5. -->
  6. <head>
  7. <meta charset="utf-8">
  8. <title>Test for Bug 802557</title>
  9. <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
  10. <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
  11. <script type="application/javascript">
  12. /** Test for Bug 802557 **/
  13. SimpleTest.waitForExplicitFinish();
  14. function checkThrows(fun, desc) {
  15. try {
  16. fun();
  17. ok(false, "Didn't throw when " + desc);
  18. } catch(e) {
  19. ok(true, "Threw when " + desc + " " + e);
  20. ok(/denied|insecure/.exec(e), "Should be security exception");
  21. }
  22. }
  23. var loadCount = 0;
  24. function go() {
  25. ++loadCount;
  26. window.ifr = document.getElementById('ifr');
  27. window.iWin = ifr.contentWindow;
  28. if (loadCount == 1) {
  29. gLoc = iWin.location;
  30. // Note that accessors pulled off Xrays are currently bound. This is bug 658909.
  31. // [getter, description, locationObj, bound]
  32. gGetters = [[ location.toString, 'toString from LW' ],
  33. [ gLoc.toString, 'toString from XLW' ],
  34. [ Object.__lookupGetter__.call(location, 'href'), 'href getter from LW' ],
  35. [ Object.__lookupGetter__.call(gLoc, 'href'), 'href getter from XLW' ],
  36. [ Object.getOwnPropertyDescriptor(location, 'href').get, 'href getter from location' ],
  37. [ Object.getOwnPropertyDescriptor(gLoc, 'href').get, 'href getter from iWin.location' ],
  38. [ function() { return this + ''; }, 'implicit conversion via [[DefaultValue]]', /* doMessageCheck = */ true ]];
  39. gGetters.forEach(function(item) {
  40. try {
  41. is(item[0].call(location), location.toString(), 'Same-origin LW: ' + item[1]);
  42. is(item[0].call(gLoc), gLoc.toString(), 'Same-origin XLW: ' + item[1]);
  43. } catch (e) {
  44. ok(false, "Threw while applying " + item[1] + " to same-origin location object: " + e);
  45. }
  46. });
  47. ifr.src = "http://example.org/tests/js/xpconnect/tests/mochitest/file_empty.html";
  48. }
  49. else if (loadCount == 2) {
  50. gGetters.forEach(function(item) {
  51. checkThrows(function() { item[0].call(gLoc); },
  52. 'call()ing ' + item[1] + ' after navigation cross-origin');
  53. });
  54. ifr.src = 'http://mochi.test:8888/tests/js/xpconnect/tests/mochitest/file_bug802557.html';
  55. }
  56. else if (loadCount == 3) {
  57. gTestFunctions = ifr.contentWindow.getAllTests();
  58. var win = ifr.contentWindow;
  59. for (fun in gTestFunctions)
  60. is(gTestFunctions[fun](), win.location.toString(), "allowed via " + fun);
  61. win.location = 'http://example.org/tests/js/xpconnect/tests/mochitest/file_bug802557.html';
  62. }
  63. else if (loadCount == 4) {
  64. for (fun in gTestFunctions) {
  65. var f = gTestFunctions[fun];
  66. checkThrows(f, "calling " + fun);
  67. }
  68. // Verify that URL.prototype.toString can't be applied to Location
  69. var threw = false;
  70. try {
  71. URL.prototype.toString.call(location);
  72. } catch (e) {
  73. threw = true;
  74. }
  75. ok(threw,
  76. "Should not be able to use URL.prototype.toString on a Location instance");
  77. // Verify that URL.prototype.href getter can't be applied to Location
  78. threw = false;
  79. var reachedTest = false;
  80. try {
  81. var get = Object.getOwnPropertyDescriptor(URL.prototype, "href").get;
  82. is(typeof(get), "function", "Should have an href getter on URL.prototype");
  83. var reachedTest = true;
  84. get.call(location);
  85. } catch (e) {
  86. threw = true;
  87. }
  88. ok(reachedTest,
  89. "Should not be able to find URL.prototype.href getter");
  90. ok(threw,
  91. "Should not be able to use URL.prototype.href getter on a Location instance");
  92. SimpleTest.finish();
  93. }
  94. }
  95. </script>
  96. </head>
  97. <body>
  98. <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=802557">Mozilla Bug 802557</a>
  99. <p id="display"></p>
  100. <div id="content" style="display: none">
  101. </div>
  102. <iframe id="ifr" onload="go();" src="file_empty.html"></iframe>
  103. <pre id="test">
  104. </pre>
  105. </body>
  106. </html>