1 var BASE_URL = "example.com/tests/dom/base/test/bug704320.sjs";
3 function createTestUrl(schemeFrom, schemeTo, policy, action, type) {
25 function create2ndLevelIframeUrl(schemeFrom, schemeTo, policy, type) {
31 "action=create-2nd-level-iframe&" +
46 // Creates the following test cases for the specified scheme and referrer
47 // policy combination:
57 // <object type="bogus">
58 // <object type="image/svg+xml">
68 // This returns a page that loads all of the above resources and contains a
69 // script that clicks a link after all resources are (hopefully)
70 // loaded. The click triggers a redirection to file_bug704320_redirect.html,
71 // which in turn notifies the main window that it's time to check the test
73 function createTest(schemeFrom, schemeTo, policy, optionalEarlierPolicy) {
74 var _createTestUrl = createTestUrl.bind(
82 var _create2ndLevelIframeUrl = create2ndLevelIframeUrl.bind(
89 var metaReferrerPolicyString = "";
90 if (optionalEarlierPolicy && optionalEarlierPolicy != "") {
91 metaReferrerPolicyString +=
92 '<meta name="referrer" content="' + optionalEarlierPolicy + '">\n';
94 metaReferrerPolicyString += '<meta name="referrer" content="' + policy + '">';
101 metaReferrerPolicyString +
103 <link rel="stylesheet" type="text/css" href="' +
104 _createTestUrl("stylesheet") +
106 <style type="text/css">\n\
108 _createTestUrl("import-css") +
111 font-family: "Fake Serif Bold";\n\
113 _createTestUrl("font-face") +
117 font-family: "Fake Serif Bold", serif;\n\
119 _createTestUrl("bg-url") +
126 _createTestUrl("script") +
129 _createTestUrl("img") +
132 _createTestUrl("iframe") +
135 _createTestUrl("audio") +
138 _createTestUrl("video") +
140 <object type="bogus" data="' +
141 _createTestUrl("object") +
143 <object type="image/svg+xml" data="' +
144 _createTestUrl("object-svg") +
146 <a id="link" href="' +
147 _createTestUrl("link") +
149 _createTestUrl("link-ping") +
152 _create2ndLevelIframeUrl("form") +
155 _create2ndLevelIframeUrl("window.location") +
158 var _testFinished = 0\n\
160 var x = new XMLHttpRequest();\n\
162 _createTestUrl("xmlhttprequest") +
167 var eventSource = new EventSource("' +
168 _createTestUrl("eventsource") +
171 // LOAD EVENT (most of the tests)
172 // fires when the resources for the page are loaded
173 'var _isLoaded = false;\n\
174 window.addEventListener("load", function() {\n\
175 this._isLoaded = true;\n\
176 this.checkForFinish();\n\
177 }.bind(window), false);' +
179 // listen for incoming status from window.open, close the window
180 // and check if we're done.
181 'var _openedWindowLoaded = false;\n\
182 window.addEventListener("message", function(message) {\n\
183 if (message.data == "window.open") {\n\
184 this._openedWindowLoaded = true;\n\
186 this.checkForFinish();\n\
188 }.bind(window), false);\n\
189 var win = window.open("' +
190 _createTestUrl("window.open") +
192 // called by the two things that must complete: window.open page
193 // and the window load event. When both are complete, this
194 // "finishes" the iframe subtest by clicking the link.
195 // _testFinished avoids calling this function twice (which may happen)
196 'function checkForFinish() {\n\
197 if (window._isLoaded && window._openedWindowLoaded && !window._testFinished) {\n\
198 window._testFinished = 1;\n\
199 document.getElementById("link").click();\n\
208 function createIframedFormTest(schemeFrom, schemeTo, policy) {
209 var actionUrl = schemeTo + "://" + BASE_URL;
215 <meta name="referrer" content="' +
220 <form id="form" action="' +
223 <input type="hidden" name="action" value="test">\n\
224 <input type="hidden" name="scheme" value="' +
229 <input type="hidden" name="policy" value="' +
232 <input type="hidden" name="type" value="form">\n\
235 document.getElementById("form").submit();\n\
242 function createIframedWindowLocationTest(schemeFrom, schemeTo, policy) {
243 var url = createTestUrl(
255 <meta name="referrer" content="' +
261 window.location = "' +
270 function createPolicyTest(policy, optionalEarlierPolicy) {
271 var metaReferrerPolicyString = "";
272 if (optionalEarlierPolicy && optionalEarlierPolicy != "") {
273 metaReferrerPolicyString +=
274 '<meta name="referrer" content="' + optionalEarlierPolicy + '">\n';
276 metaReferrerPolicyString += '<meta name="referrer" content="' + policy + '">';
283 metaReferrerPolicyString +
285 <script type="text/javascript" src="/tests/dom/base/test/file_bug704320_preload_common.js"></script>\n\
288 <img src="/tests/dom/base/test/bug704320_counter.sjs?type=img"\n\
289 onload="incrementLoad2(\'img\', 2);">\n\
290 <img src="http://example.com/tests/dom/base/test/bug704320_counter.sjs?type=img"\n\
291 onload="incrementLoad2(\'img\', 2);">\n\
297 function handleRequest(request, response) {
298 var sharedKey = "bug704320.sjs";
299 var params = request.queryString.split("&");
300 var action = params[0].split("=")[1];
302 if (action === "create-1st-level-iframe") {
303 // ?action=create-1st-level-iframe&scheme-from=http&scheme-to=https&policy=origin
304 var schemeFrom = params[1].split("=")[1];
305 var schemeTo = params[2].split("=")[1];
306 var policy = params[3].split("=")[1];
307 var optionalEarlierPolicy = "";
309 optionalEarlierPolicy = params[4].split("=")[1];
312 response.setHeader("Content-Type", "text/html; charset=utf-8", false);
313 response.setHeader("Cache-Control", "no-cache", false);
315 createTest(schemeFrom, schemeTo, policy, optionalEarlierPolicy)
317 } else if (action === "create-2nd-level-iframe") {
318 // ?action=create-2nd-level-iframe&scheme-from=http&scheme-to=https&policy=origin&type=form"
319 var schemeFrom = params[1].split("=")[1];
320 var schemeTo = params[2].split("=")[1];
321 var policy = params[3].split("=")[1];
322 var type = params[4].split("=")[1];
324 response.setHeader("Content-Type", "text/html; charset=utf-8", false);
325 response.setHeader("Cache-Control", "no-cache", false);
327 if (type === "form") {
328 response.write(createIframedFormTest(schemeFrom, schemeTo, policy));
329 } else if (type === "window.location") {
331 createIframedWindowLocationTest(schemeFrom, schemeTo, policy)
334 } else if (action === "test") {
335 // ?action=test&scheme=http-to-https&policy=origin&type=img
336 var scheme = params[1].split("=")[1];
337 var policy = params[2].split("=")[1];
338 var type = params[3].split("=")[1];
339 var result = getSharedState(sharedKey);
344 result = JSON.parse(result);
351 if (!result[type][scheme]) {
352 result[type][scheme] = {};
355 if (request.hasHeader("Referer")) {
356 result[type][scheme][policy] = request.getHeader("Referer");
358 result[type][scheme][policy] = "";
361 setSharedState(sharedKey, JSON.stringify(result));
363 if (type === "link") {
365 "https://example.com/tests/dom/base/test/file_bug704320_redirect.html";
366 response.setStatusLine("1.1", 302, "Found");
367 response.setHeader("Location", loc, false);
370 if (type === "window.open") {
371 response.setHeader("Cache-Control", "no-cache", false);
372 response.setHeader("Content-Type", "text/html", false);
374 "<html><body><script>" +
375 'window.opener.postMessage("window.open", "*");' +
376 "</script></body></html>"
379 } else if (action === "get-test-results") {
380 // ?action=get-result
381 response.setHeader("Cache-Control", "no-cache", false);
382 response.setHeader("Content-Type", "text/plain", false);
383 response.write(getSharedState(sharedKey));
384 } else if (action === "generate-policy-test") {
385 // ?action=generate-policy-test&policy=b64-encoded-string
386 response.setHeader("Cache-Control", "no-cache", false);
387 response.setHeader("Content-Type", "text/html", false);
388 var policy = unescape(params[1].split("=")[1]);
389 var optionalEarlierPolicy = "";
391 optionalEarlierPolicy = params[2].split("=")[1];
394 response.write(createPolicyTest(policy, optionalEarlierPolicy));