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