summaryrefslogtreecommitdiffstats
path: root/dom/base/test/test_simplecontentpolicy.html
blob: 9fa5dd3fdbe3bbe40259dbd1e4d24503af2da3a3 (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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
<!DOCTYPE HTML>
<html>
<head>
  <title>Test for nsISimpleContentPolicy</title>
  <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1128798">Mozilla Bug 1128798</a>
<p id="display"></p>
<div id="content" style="display: none">

</div>
<pre id="test">
<script class="testbody" type="text/javascript">

var lastContentType = -1;
const testURL = window.location.href + "/this/is/the/test/url";
const Cc = SpecialPowers.Cc;
const Ci = SpecialPowers.Ci;

var beaconUrl = "http://mochi.test:8888/tests/dom/tests/mochitest/beacon/beacon-handler.sjs";

var chromeURL = SimpleTest.getTestFileURL("file_simplecontentpolicy.js");
var script = SpecialPowers.loadChromeScript(chromeURL);

// Try creating different request types. Most of these are not
// top-level because they run in the test iframe.
var tests = [["SCRIPT", false],
             ["IMAGE", false],
             ["STYLESHEET", false],
             ["OBJECT", false],
             ["DOCUMENT", true],
             ["SUBDOCUMENT", false],
             ["XBL", false],
             ["XMLHTTPREQUEST", false],
             ["BEACON", false]];
var curTest = -1;

var div;

SimpleTest.waitForExplicitFinish();

script.addMessageListener("ready", function(msg) {
  SpecialPowers.pushPrefEnv({'set': [["beacon.enabled", true]]}, runNextTest);
});

script.addMessageListener("shouldLoad", function(msg) {
  var isTopLevel = tests[curTest][1];
  var type = "TYPE_" + tests[curTest][0];
  is(msg.contentType, Ci.nsIContentPolicy[type], "Content policies triggered for " + type);
  is(msg.isTopLevel, isTopLevel, "isTopLevel is set correctly");

  if (tests[curTest] == "XBL") {
    //XXX Removing binding to work-around a memory leak (bugs 478528, 499735).
    div.style.MozBinding = "";
  }

  runNextTest();
});

function runNextTest() {
  curTest++;
  if (curTest < tests.length) {
    var method = "request_" + tests[curTest][0].toLowerCase();
    try {
      window[method]();
    } catch(e) {}
  }
  else {
    script.sendAsyncMessage("finished");

    SimpleTest.finish();
  }
}

// Request creating functions

function request_script() {
  var content = $("content");

Math.sin(1);
  var script = document.createElement("script");
  script.setAttribute("type", "text/javascript")
  script.setAttribute("src", testURL)
  content.appendChild(script);
}

function request_image() {
  var content = $("content");

  var image = new Image();
  image.src = testURL;
}

function request_stylesheet() {
  var content = $("content");

  var stylesheet = document.createElement("link");
  stylesheet.setAttribute("rel", "stylesheet");
  stylesheet.setAttribute("type", "text/css");
  stylesheet.setAttribute("href", testURL);
  content.appendChild(stylesheet);
}

function request_object() {
  var content = $("content");

  var object = document.createElement("embed");
  object.setAttribute("src", testURL);
  content.appendChild(object);
}

function request_document() {
  top.location.href = testURL;
}

function request_subdocument() {
  var content = $("content");

  var frame = document.createElement("iframe");
  frame.setAttribute("src", testURL);
  content.appendChild(frame);
}

function request_xbl() {
  var content = $("content");

  div = document.createElement("div");
  div.style.MozBinding = "url(" + testURL + ")";
  $('test').appendChild(div);
  div.offsetLeft; // Flush styles.
}

function request_xmlhttprequest() {
  var request = new XMLHttpRequest();
  request.open("GET", testURL, false);
  request.send(null);
}

function request_beacon() {
  navigator.sendBeacon(testURL, "bacon would have been a better name than beacon");
}

</script>
</pre>
</body>
</html>