content/base/test/test_bug375314.html

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 <!DOCTYPE HTML>
michael@0 2 <html>
michael@0 3 <!--
michael@0 4 https://bugzilla.mozilla.org/show_bug.cgi?id=375314
michael@0 5 -->
michael@0 6 <head>
michael@0 7 <title>Test for Bug 375314</title>
michael@0 8 <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
michael@0 9 <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
michael@0 10 </head>
michael@0 11 <body>
michael@0 12 <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=375314">Mozilla Bug 375314</a>
michael@0 13 <p id="display"></p>
michael@0 14 <div id="content" style="display: none">
michael@0 15
michael@0 16 </div>
michael@0 17 <pre id="test">
michael@0 18 <script class="testbody" type="text/javascript">
michael@0 19
michael@0 20 /** Test for Bug 375314 **/
michael@0 21
michael@0 22 var lastContentType = -1;
michael@0 23 const testURL = window.location.href + "/this/is/the/test/url";
michael@0 24 const Cc = SpecialPowers.Cc;
michael@0 25 const Ci = SpecialPowers.Ci;
michael@0 26
michael@0 27 // Content policy / factory implementation for the test
michael@0 28 var policyID = SpecialPowers.wrap(SpecialPowers.Components).ID("{b80e19d0-878f-d41b-2654-194714a4115c}");
michael@0 29 var policyName = "@mozilla.org/testpolicy;1";
michael@0 30 var policy = {
michael@0 31 // nsISupports implementation
michael@0 32 QueryInterface: function(iid) {
michael@0 33
michael@0 34 iid = SpecialPowers.wrap(iid);
michael@0 35 if (iid.equals(Ci.nsISupports) ||
michael@0 36 iid.equals(Ci.nsIFactory) ||
michael@0 37 iid.equals(Ci.nsIContentPolicy))
michael@0 38 return this;
michael@0 39
michael@0 40 throw SpecialPowers.Cr.NS_ERROR_NO_INTERFACE;
michael@0 41 },
michael@0 42
michael@0 43 // nsIFactory implementation
michael@0 44 createInstance: function(outer, iid) {
michael@0 45 return this.QueryInterface(iid);
michael@0 46 },
michael@0 47
michael@0 48 // nsIContentPolicy implementation
michael@0 49 shouldLoad: function(contentType, contentLocation, requestOrigin, context, mimeTypeGuess, extra) {
michael@0 50
michael@0 51 // Remember last content type seen for the test url
michael@0 52 if (SpecialPowers.wrap(contentLocation).spec == testURL) {
michael@0 53 lastContentType = contentType;
michael@0 54 return Ci.nsIContentPolicy.REJECT_REQUEST;
michael@0 55 }
michael@0 56
michael@0 57 return Ci.nsIContentPolicy.ACCEPT;
michael@0 58 },
michael@0 59
michael@0 60 shouldProcess: function(contentType, contentLocation, requestOrigin, context, mimeTypeGuess, extra) {
michael@0 61
michael@0 62 return Ci.nsIContentPolicy.ACCEPT;
michael@0 63 }
michael@0 64 }
michael@0 65 policy = SpecialPowers.wrapCallbackObject(policy);
michael@0 66
michael@0 67 // Register content policy
michael@0 68 var componentManager = SpecialPowers.wrap(SpecialPowers.Components).manager
michael@0 69 .QueryInterface(Ci.nsIComponentRegistrar);
michael@0 70
michael@0 71 componentManager.registerFactory(policyID, "Test content policy", policyName, policy);
michael@0 72
michael@0 73 var categoryManager = Cc["@mozilla.org/categorymanager;1"].getService(Ci.nsICategoryManager);
michael@0 74 categoryManager.addCategoryEntry("content-policy", policyName, policyName, false, true);
michael@0 75
michael@0 76 // Try creating different request types
michael@0 77 var tests = ["SCRIPT", "IMAGE", "STYLESHEET", "OBJECT", "DOCUMENT", "SUBDOCUMENT", "XBL", "XMLHTTPREQUEST"];
michael@0 78 var curTest = -1;
michael@0 79
michael@0 80 var div;
michael@0 81
michael@0 82 SimpleTest.waitForExplicitFinish();
michael@0 83 setTimeout(runNextTest, 0);
michael@0 84
michael@0 85 function runNextTest() {
michael@0 86
michael@0 87 if (curTest >= 0) {
michael@0 88 var type = "TYPE_" + tests[curTest];
michael@0 89 is(lastContentType, Ci.nsIContentPolicy[type], "Content policies triggered for " + type);
michael@0 90
michael@0 91 if (tests[curTest] == "XBL")
michael@0 92 {
michael@0 93 //XXX Removing binding to work-around a memory leak (bugs 478528, 499735).
michael@0 94 div.style.MozBinding = "";
michael@0 95 }
michael@0 96 }
michael@0 97
michael@0 98 curTest++;
michael@0 99 if (curTest < tests.length) {
michael@0 100 var method = "request_" + tests[curTest].toLowerCase();
michael@0 101 try {
michael@0 102 window[method]();
michael@0 103 } catch(e) {}
michael@0 104 setTimeout(runNextTest, 0);
michael@0 105 }
michael@0 106 else {
michael@0 107 // Unregister content policy
michael@0 108 categoryManager.deleteCategoryEntry("content-policy", policyName, false);
michael@0 109
michael@0 110 setTimeout(function() {
michael@0 111 // Component must be unregistered delayed, otherwise other content
michael@0 112 // policy will not be removed from the category correctly
michael@0 113 componentManager.unregisterFactory(policyID, policy);
michael@0 114 }, 0);
michael@0 115
michael@0 116 SimpleTest.finish();
michael@0 117 }
michael@0 118 }
michael@0 119
michael@0 120 // Request creating functions
michael@0 121
michael@0 122 function request_script() {
michael@0 123 var content = $("content");
michael@0 124
michael@0 125 var script = document.createElement("script");
michael@0 126 script.setAttribute("type", "text/javascript")
michael@0 127 script.setAttribute("src", testURL)
michael@0 128 content.appendChild(script);
michael@0 129 }
michael@0 130
michael@0 131 function request_image() {
michael@0 132 var content = $("content");
michael@0 133
michael@0 134 var image = new Image();
michael@0 135 image.src = testURL;
michael@0 136 }
michael@0 137
michael@0 138 function request_stylesheet() {
michael@0 139 var content = $("content");
michael@0 140
michael@0 141 var stylesheet = document.createElement("link");
michael@0 142 stylesheet.setAttribute("rel", "stylesheet");
michael@0 143 stylesheet.setAttribute("type", "text/css");
michael@0 144 stylesheet.setAttribute("href", testURL);
michael@0 145 content.appendChild(stylesheet);
michael@0 146 }
michael@0 147
michael@0 148 function request_object() {
michael@0 149 var content = $("content");
michael@0 150
michael@0 151 var object = document.createElement("embed");
michael@0 152 object.setAttribute("src", testURL);
michael@0 153 content.appendChild(object);
michael@0 154 }
michael@0 155
michael@0 156 function request_document() {
michael@0 157 top.location.href = testURL;
michael@0 158 }
michael@0 159
michael@0 160 function request_subdocument() {
michael@0 161 var content = $("content");
michael@0 162
michael@0 163 var frame = document.createElement("iframe");
michael@0 164 frame.setAttribute("src", testURL);
michael@0 165 content.appendChild(frame);
michael@0 166 }
michael@0 167
michael@0 168 function request_xbl() {
michael@0 169 var content = $("content");
michael@0 170
michael@0 171 div = document.createElement("div");
michael@0 172 div.style.MozBinding = "url(" + testURL + ")";
michael@0 173 $('test').appendChild(div);
michael@0 174 div.offsetLeft; // Flush styles.
michael@0 175 }
michael@0 176
michael@0 177 function request_xmlhttprequest() {
michael@0 178 var request = new XMLHttpRequest();
michael@0 179 request.open("GET", testURL, false);
michael@0 180 request.send(null);
michael@0 181 }
michael@0 182
michael@0 183 </script>
michael@0 184 </pre>
michael@0 185 </body>
michael@0 186 </html>
michael@0 187

mercurial