test_bug375314.html (4298B)
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 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(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(iid) { 45 return this.QueryInterface(iid); 46 }, 47 48 // nsIContentPolicy implementation 49 shouldLoad(contentLocation, loadInfo) { 50 let contentType = loadInfo.externalContentPolicyType; 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(contentLocation, loadInfo) { 61 return Ci.nsIContentPolicy.ACCEPT; 62 } 63 } 64 policy = SpecialPowers.wrapCallbackObject(policy); 65 66 // Register content policy 67 var componentManager = SpecialPowers.wrap(SpecialPowers.Components).manager 68 .QueryInterface(Ci.nsIComponentRegistrar); 69 70 componentManager.registerFactory(policyID, "Test content policy", policyName, policy); 71 72 var categoryManager = Cc["@mozilla.org/categorymanager;1"].getService(Ci.nsICategoryManager); 73 categoryManager.addCategoryEntry("content-policy", policyName, policyName, false, true); 74 75 // Try creating different request types 76 var tests = ["SCRIPT", "IMAGE", "STYLESHEET", "XMLHTTPREQUEST"]; 77 var curTest = -1; 78 var div; 79 80 SimpleTest.waitForExplicitFinish(); 81 setTimeout(runNextTest, 0); 82 83 function runNextTest() { 84 85 if (curTest >= 0) { 86 var type = "TYPE_" + tests[curTest]; 87 is(lastContentType, Ci.nsIContentPolicy[type], "Content policies triggered for " + type); 88 } 89 90 curTest++; 91 if (curTest < tests.length) { 92 var method = "request_" + tests[curTest].toLowerCase(); 93 try { 94 window[method](); 95 } catch(e) {} 96 setTimeout(runNextTest, 0); 97 } 98 else { 99 // Unregister content policy 100 categoryManager.deleteCategoryEntry("content-policy", policyName, false); 101 102 setTimeout(function() { 103 // Component must be unregistered delayed, otherwise other content 104 // policy will not be removed from the category correctly 105 componentManager.unregisterFactory(policyID, policy); 106 }, 0); 107 108 SimpleTest.finish(); 109 } 110 } 111 112 // Request creating functions 113 114 function request_script() { 115 var content = $("content"); 116 117 var script = document.createElement("script"); 118 script.setAttribute("type", "text/javascript") 119 script.setAttribute("src", testURL) 120 content.appendChild(script); 121 } 122 123 function request_image() { 124 var content = $("content"); 125 126 var image = new Image(); 127 image.src = testURL; 128 } 129 130 function request_stylesheet() { 131 var content = $("content"); 132 133 var stylesheet = document.createElement("link"); 134 stylesheet.setAttribute("rel", "stylesheet"); 135 stylesheet.setAttribute("type", "text/css"); 136 stylesheet.setAttribute("href", testURL); 137 content.appendChild(stylesheet); 138 } 139 140 function request_object() { 141 var content = $("content"); 142 143 var object = document.createElement("embed"); 144 object.setAttribute("src", testURL); 145 content.appendChild(object); 146 } 147 148 function request_xmlhttprequest() { 149 var request = new XMLHttpRequest(); 150 request.open("GET", testURL, false); 151 request.send(null); 152 } 153 154 </script> 155 </pre> 156 </body> 157 </html>