test_bug812415.xul 4.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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=812415
  6. -->
  7. <window title="Mozilla Bug 812415"
  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=812415"
  13. target="_blank">Mozilla Bug 812415</a>
  14. </body>
  15. <!-- test code goes here -->
  16. <script type="application/javascript">
  17. <![CDATA[
  18. /** Test for Bug 812415 and Bug 823348 **/
  19. const Cu = Components.utils;
  20. SimpleTest.waitForExplicitFinish();
  21. function testWaiving(iwin, sb) {
  22. sb.win = iwin;
  23. is(Cu.evalInSandbox('win', sb), iwin, "Basic identity works");
  24. is(Cu.evalInSandbox('win.wrappedJSObject.expando', sb), 42, "Waivers work via .wrappedJSObject");
  25. is(Cu.evalInSandbox('XPCNativeWrapper.unwrap(win).expando', sb), 42, "Waivers work via XPCNativeWrapper.unwrap");
  26. is(Cu.evalInSandbox('win.wrappedJSObject.document.defaultView.expando', sb), 42, "Waivers are deep");
  27. }
  28. function checkThrows(expression, sb, msg) {
  29. var result = Cu.evalInSandbox('(function() { try { ' + expression + '; return "allowed"; } catch (e) { return e.toString(); }})();', sb);
  30. ok(!!/denied/.exec(result), msg);
  31. }
  32. function testAsymmetric(regular, expanded) {
  33. // Set up objects.
  34. expanded.regFun = Cu.evalInSandbox('function reg() { return 42; }; reg', regular);
  35. expanded.regObj = Cu.evalInSandbox('new Object({foo: 2})', regular);
  36. regular.expFun = Cu.evalInSandbox('function exp() { return 41; }; exp', expanded);
  37. regular.expObj = Cu.evalInSandbox('new Object({bar: 3})', expanded);
  38. // Check objects.
  39. is(Cu.evalInSandbox('regObj.foo', expanded), 2, "Expanded can see regular object prop");
  40. checkThrows('expObj.bar', regular, "Regular shouldn't read properties");
  41. Cu.evalInSandbox('regObj.foo = 20', expanded);
  42. is(expanded.regObj.foo, 20, "Expanded can set properties");
  43. checkThrows('expFun.bar = 0', regular, "Regular shouldn't write properties");
  44. // Check functions.
  45. is(Cu.evalInSandbox('regFun()', expanded), 42, "Expanded can call regular function");
  46. checkThrows('expFun()', regular, "Regular cannot call expanded function");
  47. is(Cu.evalInSandbox('regFun.name', expanded), 'reg', "Expanded can see regular function's name");
  48. checkThrows('expFun.name', regular, "Regular can't see expanded function's name");
  49. Cu.evalInSandbox('regFun.expando = 30', expanded);
  50. is(Cu.evalInSandbox('regFun.expando', expanded), 30, "Expanded can set expandos");
  51. checkThrows('expFun.expando = 29', regular, "Regular can't set expandos");
  52. // Check __proto__ stuff.
  53. is(Cu.evalInSandbox('regFun.__proto__', expanded), regular.Function.prototype, "expanded can get __proto__");
  54. checkThrows('expFun.__proto__', regular, "regular can't use __proto__");
  55. checkThrows('expFun.__proto__ = {}', regular, "regular can't mutate __proto__");
  56. }
  57. function go() {
  58. var iwin = document.getElementById('ifr').contentWindow;
  59. iwin.wrappedJSObject.expando = 42;
  60. // Make our sandboxes. We pass wantXrays=false for the nsEP to ensure that
  61. // the Xrays we get are the result of being an nsEP, not from the wantXrays
  62. // flag.
  63. var regular = new Components.utils.Sandbox(iwin);
  64. var expanded = new Components.utils.Sandbox([iwin], {wantXrays: false});
  65. // Because of the crazy secret life of wantXrays, passing wantXrays=false
  66. // has the side effect of waiving Xrays on the returned sandbox. Undo that.
  67. expanded = XPCNativeWrapper(expanded);
  68. testWaiving(iwin, regular);
  69. testWaiving(iwin, expanded);
  70. testAsymmetric(regular, expanded);
  71. SimpleTest.finish();
  72. }
  73. ]]>
  74. </script>
  75. <iframe id="ifr" onload="go();" src="http://example.org/tests/js/xpconnect/tests/mochitest/file_empty.html" />
  76. </window>