summaryrefslogtreecommitdiffstats
path: root/docshell/test/chrome/test_principalInherit.xul
blob: 87056c63dccf326f9a4065aecf454910b6b0b35e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
<?xml version="1.0"?>
<!-- Any copyright is dedicated to the Public Domain.
   - http://creativecommons.org/publicdomain/zero/1.0/ -->
<?xml-stylesheet href="chrome://global/skin" type="text/css"?>
<?xml-stylesheet
  href="chrome://mochikit/content/tests/SimpleTest/test.css"
  type="text/css"?>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=719994
-->
<window title="Test principal inheriting"
  xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">

  <script type="application/javascript"
          src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>

<body xmlns="http://www.w3.org/1999/xhtml">
<p id="display"></p>
<div id="content" style="display: none">
</div>
<pre id="test">
</pre>
</body>

<script class="testbody" type="application/javascript">
<![CDATA[

/** Test for Bug 719994 **/

SimpleTest.waitForExplicitFinish();

var gFrame;

// This test file is loaded in a type=content docshell whose principal is
// the system principal.

// Using data: URIs here instead of javascript: URIs, since javascript: URIs
// fail to load when there's no principal to load them against. This only
// matters when these tests fail (produces better error messages).
var tests = [
  function testInheritFromParent(cb) {
    gFrame = document.createElement("iframe");
    loadListener(gFrame, function () {
      is(window.inheritedFromParent, true, "load in type=content iframe inherited principal of same type parent");
      cb();
    });
    gFrame.setAttribute("type", "content");
    gFrame.setAttribute("src", "data:text/html,<script>parent.inheritedFromParent = true;</script>");
    document.documentElement.appendChild(gFrame);
  },
  function testInheritFromCurrent_system(cb) {
    loadListener(gFrame, function () {
      is(window.inheritedSystem, undefined, "load in type=content iframe shouldn't inherit system principal from current document");
      cb();
    }, true);
    gFrame.setAttribute("src", "data:text/html,<script>parent.inheritedSystem = true;</script>");
  },
  function testInheritFromCreated(cb) {
    // Open a new chrome window with a type="content" iframe, so that it has no
    // same-type parent.
    // Load a javascript: URI in it to ensure that GetInheritedPrincipal will
    // force creation of a content viewer.
    let xulWinURL = 'data:application/vnd.mozilla.xul+xml,<?xml version="1.0"?>' +
                    '<window xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"/>';
    let newWin = window.openDialog(xulWinURL, "chrome_window", "chrome");
    loadListener(newWin, function () {
      let frame = newWin.document.createElement("iframe");
      frame.setAttribute("type", "content");
      frame.setAttribute("src", "javascript:'1';");
      loadListener(frame, function () {
        is(frame.contentWindow.document.body.textContent, "1", "content viewer was created");
        SimpleTest.executeSoon(function () {
          newWin.close();
          cb();
        })
      });
      newWin.document.documentElement.appendChild(frame);
    });
  }
];

addLoadEvent(function onLoad() {
  ok(Components.stack, "this test must be run with the system principal");
  SimpleTest.executeSoon(nextTest);
});

function loadListener(target, func) {
  target.addEventListener("load", function lis() {
    target.removeEventListener("load", lis, true);
    func();
  }, true);
}

function nextTest() {
  if (tests.length) {
    let test = tests.shift();
    SimpleTest.executeSoon(function () {
      info("running " + test.name);
      test(nextTest);
    });
  } else
    SimpleTest.executeSoon(SimpleTest.finish);
}

]]>
</script>

</window>