docshell/test/chrome/test_principalInherit.xul

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/docshell/test/chrome/test_principalInherit.xul	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,108 @@
     1.4 +<?xml version="1.0"?>
     1.5 +<!-- Any copyright is dedicated to the Public Domain.
     1.6 +   - http://creativecommons.org/publicdomain/zero/1.0/ -->
     1.7 +<?xml-stylesheet href="chrome://global/skin" type="text/css"?>
     1.8 +<?xml-stylesheet
     1.9 +  href="chrome://mochikit/content/tests/SimpleTest/test.css"
    1.10 +  type="text/css"?>
    1.11 +<!--
    1.12 +https://bugzilla.mozilla.org/show_bug.cgi?id=719994
    1.13 +-->
    1.14 +<window title="Test principal inheriting"
    1.15 +  xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
    1.16 +
    1.17 +  <script type="application/javascript"
    1.18 +          src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
    1.19 +
    1.20 +<body xmlns="http://www.w3.org/1999/xhtml">
    1.21 +<p id="display"></p>
    1.22 +<div id="content" style="display: none">
    1.23 +</div>
    1.24 +<pre id="test">
    1.25 +</pre>
    1.26 +</body>
    1.27 +
    1.28 +<script class="testbody" type="application/javascript">
    1.29 +<![CDATA[
    1.30 +
    1.31 +/** Test for Bug 719994 **/
    1.32 +
    1.33 +SimpleTest.waitForExplicitFinish();
    1.34 +
    1.35 +var gFrame;
    1.36 +
    1.37 +// This test file is loaded in a type=content docshell whose principal is
    1.38 +// the system principal.
    1.39 +
    1.40 +// Using data: URIs here instead of javascript: URIs, since javascript: URIs
    1.41 +// fail to load when there's no principal to load them against. This only
    1.42 +// matters when these tests fail (produces better error messages).
    1.43 +var tests = [
    1.44 +  function testInheritFromParent(cb) {
    1.45 +    gFrame = document.createElement("iframe");
    1.46 +    loadListener(gFrame, function () {
    1.47 +      is(window.inheritedFromParent, true, "load in type=content iframe inherited principal of same type parent");
    1.48 +      cb();
    1.49 +    });
    1.50 +    gFrame.setAttribute("type", "content");
    1.51 +    gFrame.setAttribute("src", "data:text/html,<script>parent.inheritedFromParent = true;</script>");
    1.52 +    document.documentElement.appendChild(gFrame);
    1.53 +  },
    1.54 +  function testInheritFromCurrent_system(cb) {
    1.55 +    loadListener(gFrame, function () {
    1.56 +      is(window.inheritedSystem, undefined, "load in type=content iframe shouldn't inherit system principal from current document");
    1.57 +      cb();
    1.58 +    }, true);
    1.59 +    gFrame.setAttribute("src", "data:text/html,<script>parent.inheritedSystem = true;</script>");
    1.60 +  },
    1.61 +  function testInheritFromCreated(cb) {
    1.62 +    // Open a new chrome window with a type="content" iframe, so that it has no
    1.63 +    // same-type parent.
    1.64 +    // Load a javascript: URI in it to ensure that GetInheritedPrincipal will
    1.65 +    // force creation of a content viewer.
    1.66 +    let xulWinURL = 'data:application/vnd.mozilla.xul+xml,<?xml version="1.0"?>' +
    1.67 +                    '<window xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"/>';
    1.68 +    let newWin = window.openDialog(xulWinURL, "chrome_window", "chrome");
    1.69 +    loadListener(newWin, function () {
    1.70 +      let frame = newWin.document.createElement("iframe");
    1.71 +      frame.setAttribute("type", "content");
    1.72 +      frame.setAttribute("src", "javascript:'1';");
    1.73 +      loadListener(frame, function () {
    1.74 +        is(frame.contentWindow.document.body.textContent, "1", "content viewer was created");
    1.75 +        SimpleTest.executeSoon(function () {
    1.76 +          newWin.close();
    1.77 +          cb();
    1.78 +        })
    1.79 +      });
    1.80 +      newWin.document.documentElement.appendChild(frame);
    1.81 +    });
    1.82 +  }
    1.83 +];
    1.84 +
    1.85 +addLoadEvent(function onLoad() {
    1.86 +  ok(Components.stack, "this test must be run with the system principal");
    1.87 +  SimpleTest.executeSoon(nextTest);
    1.88 +});
    1.89 +
    1.90 +function loadListener(target, func) {
    1.91 +  target.addEventListener("load", function lis() {
    1.92 +    target.removeEventListener("load", lis, true);
    1.93 +    func();
    1.94 +  }, true);
    1.95 +}
    1.96 +
    1.97 +function nextTest() {
    1.98 +  if (tests.length) {
    1.99 +    let test = tests.shift();
   1.100 +    SimpleTest.executeSoon(function () {
   1.101 +      info("running " + test.name);
   1.102 +      test(nextTest);
   1.103 +    });
   1.104 +  } else
   1.105 +    SimpleTest.executeSoon(SimpleTest.finish);
   1.106 +}
   1.107 +
   1.108 +]]>
   1.109 +</script>
   1.110 +
   1.111 +</window>

mercurial