test_https_only_https_first_prefs.js (9528B)
1 const { HttpServer } = ChromeUtils.importESModule( 2 "resource://testing-common/httpd.sys.mjs" 3 ); 4 const { NetUtil } = ChromeUtils.importESModule( 5 "resource://gre/modules/NetUtil.sys.mjs" 6 ); 7 8 ChromeUtils.defineLazyGetter(this, "HTTP_TEST_URL", function () { 9 return "http://test1.example.com"; 10 }); 11 12 const TEST_PATH = "/https_only_https_first_path"; 13 var httpserver = null; 14 var channel = null; 15 var curTest = null; 16 17 const TESTS = [ 18 { 19 // Test 1: all prefs to false 20 description: "Test 1 - top-level", 21 contentType: Ci.nsIContentPolicy.TYPE_DOCUMENT, 22 https_only: false, 23 https_only_pbm: false, 24 https_first: false, 25 https_first_pbm: false, 26 pbm: false, 27 expectedScheme: "http", 28 }, 29 { 30 description: "Test 1 - top-level - pbm", 31 contentType: Ci.nsIContentPolicy.TYPE_DOCUMENT, 32 https_only: false, 33 https_only_pbm: false, 34 https_first: false, 35 https_first_pbm: false, 36 pbm: true, 37 expectedScheme: "http", 38 }, 39 { 40 description: "Test 1 - sub-resource", 41 contentType: Ci.nsIContentPolicy.TYPE_IMAGE, 42 https_only: false, 43 https_only_pbm: false, 44 https_first: false, 45 https_first_pbm: false, 46 pbm: false, 47 expectedScheme: "http", 48 }, 49 { 50 description: "Test 1 - sub-resource - pbm", 51 contentType: Ci.nsIContentPolicy.TYPE_IMAGE, 52 https_only: false, 53 https_only_pbm: false, 54 https_first: false, 55 https_first_pbm: false, 56 pbm: true, 57 expectedScheme: "http", 58 }, 59 // Test 2: https_only true 60 { 61 description: "Test 2 - top-level", 62 contentType: Ci.nsIContentPolicy.TYPE_DOCUMENT, 63 https_only: true, 64 https_only_pbm: false, 65 https_first: false, 66 https_first_pbm: false, 67 pbm: false, 68 expectedScheme: "https", 69 }, 70 { 71 description: "Test 2 - top-level - pbm", 72 contentType: Ci.nsIContentPolicy.TYPE_DOCUMENT, 73 https_only: true, 74 https_only_pbm: false, 75 https_first: false, 76 https_first_pbm: false, 77 pbm: true, 78 expectedScheme: "https", 79 }, 80 { 81 description: "Test 2 - sub-resource", 82 contentType: Ci.nsIContentPolicy.TYPE_IMAGE, 83 https_only: true, 84 https_only_pbm: false, 85 https_first: false, 86 https_first_pbm: false, 87 pbm: false, 88 expectedScheme: "https", 89 }, 90 { 91 description: "Test 2 - sub-resource - pbm", 92 contentType: Ci.nsIContentPolicy.TYPE_IMAGE, 93 https_only: true, 94 https_only_pbm: false, 95 https_first: false, 96 https_first_pbm: false, 97 pbm: true, 98 expectedScheme: "https", 99 }, 100 // Test 3: https_only_pbm true 101 { 102 description: "Test 3 - top-level", 103 contentType: Ci.nsIContentPolicy.TYPE_DOCUMENT, 104 https_only: false, 105 https_only_pbm: true, 106 https_first: false, 107 https_first_pbm: false, 108 pbm: false, 109 expectedScheme: "http", 110 }, 111 { 112 description: "Test 3 - top-level - pbm", 113 contentType: Ci.nsIContentPolicy.TYPE_DOCUMENT, 114 https_only: false, 115 https_only_pbm: true, 116 https_first: false, 117 https_first_pbm: false, 118 pbm: true, 119 expectedScheme: "https", 120 }, 121 { 122 description: "Test 3 - sub-resource", 123 contentType: Ci.nsIContentPolicy.TYPE_IMAGE, 124 https_only: false, 125 https_only_pbm: true, 126 https_first: false, 127 https_first_pbm: false, 128 pbm: false, 129 expectedScheme: "http", 130 }, 131 { 132 description: "Test 3 - sub-resource - pbm", 133 contentType: Ci.nsIContentPolicy.TYPE_IMAGE, 134 https_only: false, 135 https_only_pbm: true, 136 https_first: false, 137 https_first_pbm: false, 138 pbm: true, 139 expectedScheme: "https", 140 }, 141 // Test 4: https_first true 142 { 143 description: "Test 4 - top-level", 144 contentType: Ci.nsIContentPolicy.TYPE_DOCUMENT, 145 https_only: false, 146 https_only_pbm: false, 147 https_first: true, 148 https_first_pbm: false, 149 pbm: false, 150 expectedScheme: "https", 151 }, 152 { 153 description: "Test 4 - top-level - pbm", 154 contentType: Ci.nsIContentPolicy.TYPE_DOCUMENT, 155 https_only: false, 156 https_only_pbm: false, 157 https_first: true, 158 https_first_pbm: false, 159 pbm: true, 160 expectedScheme: "https", 161 }, 162 { 163 description: "Test 4 - sub-resource", 164 contentType: Ci.nsIContentPolicy.TYPE_IMAGE, 165 https_only: false, 166 https_only_pbm: false, 167 https_first: true, 168 https_first_pbm: false, 169 pbm: false, 170 expectedScheme: "http", 171 }, 172 { 173 description: "Test 4 - sub-resource - pbm", 174 contentType: Ci.nsIContentPolicy.TYPE_IMAGE, 175 https_only: false, 176 https_only_pbm: false, 177 https_first: true, 178 https_first_pbm: false, 179 pbm: true, 180 expectedScheme: "http", 181 }, 182 // Test 5: https_first_pbm true 183 { 184 description: "Test 5 - top-level", 185 contentType: Ci.nsIContentPolicy.TYPE_DOCUMENT, 186 https_only: false, 187 https_only_pbm: false, 188 https_first: false, 189 https_first_pbm: true, 190 pbm: false, 191 expectedScheme: "http", 192 }, 193 { 194 description: "Test 5 - top-level - pbm", 195 contentType: Ci.nsIContentPolicy.TYPE_DOCUMENT, 196 https_only: false, 197 https_only_pbm: false, 198 https_first: false, 199 https_first_pbm: true, 200 pbm: true, 201 expectedScheme: "https", 202 }, 203 { 204 description: "Test 5 - sub-resource", 205 contentType: Ci.nsIContentPolicy.TYPE_IMAGE, 206 https_only: false, 207 https_only_pbm: false, 208 https_first: false, 209 https_first_pbm: true, 210 pbm: false, 211 expectedScheme: "http", 212 }, 213 { 214 description: "Test 5 - sub-resource - pbm", 215 contentType: Ci.nsIContentPolicy.TYPE_IMAGE, 216 https_only: false, 217 https_only_pbm: false, 218 https_first: false, 219 https_first_pbm: true, 220 pbm: true, 221 expectedScheme: "http", 222 }, 223 // Test 6: https_only overrules https_first 224 { 225 description: "Test 6 - top-level", 226 contentType: Ci.nsIContentPolicy.TYPE_DOCUMENT, 227 https_only: true, 228 https_only_pbm: false, 229 https_first: true, 230 https_first_pbm: false, 231 pbm: false, 232 expectedScheme: "https", 233 }, 234 { 235 description: "Test 6 - top-level - pbm", 236 contentType: Ci.nsIContentPolicy.TYPE_DOCUMENT, 237 https_only: true, 238 https_only_pbm: false, 239 https_first: true, 240 https_first_pbm: false, 241 pbm: true, 242 expectedScheme: "https", 243 }, 244 { 245 description: "Test 6 - sub-resource", 246 contentType: Ci.nsIContentPolicy.TYPE_IMAGE, 247 https_only: true, 248 https_only_pbm: false, 249 https_first: true, 250 https_first_pbm: false, 251 pbm: false, 252 expectedScheme: "https", 253 }, 254 { 255 description: "Test 6 - sub-resource - pbm", 256 contentType: Ci.nsIContentPolicy.TYPE_IMAGE, 257 https_only: true, 258 https_only_pbm: false, 259 https_first: true, 260 https_first_pbm: false, 261 pbm: true, 262 expectedScheme: "https", 263 }, 264 ]; 265 266 function ChannelListener() {} 267 268 ChannelListener.prototype = { 269 onStartRequest(request) { 270 var chan = request.QueryInterface(Ci.nsIChannel); 271 var httpChan = chan.QueryInterface(Ci.nsIHttpChannel); 272 var authHeader = httpChan.getRequestHeader("Authorization"); 273 Assert.equal(authHeader, "Basic user:pass", curTest.description); 274 }, 275 onDataAvailable() { 276 do_throw("Should not get any data!"); 277 }, 278 onStopRequest(request) { 279 var chan = request.QueryInterface(Ci.nsIChannel); 280 let requestURL = chan.URI; 281 Assert.equal( 282 requestURL.scheme, 283 curTest.expectedScheme, 284 curTest.description 285 ); 286 Assert.equal(requestURL.host, "test1.example.com", curTest.description); 287 Assert.equal(requestURL.filePath, TEST_PATH, curTest.description); 288 run_next_test(); 289 }, 290 }; 291 292 function setUpPrefs() { 293 // set up the required prefs 294 Services.prefs.setBoolPref( 295 "dom.security.https_only_mode", 296 curTest.https_only 297 ); 298 Services.prefs.setBoolPref( 299 "dom.security.https_only_mode_pbm", 300 curTest.https_only_pbm 301 ); 302 Services.prefs.setBoolPref("dom.security.https_first", curTest.https_first); 303 Services.prefs.setBoolPref( 304 "dom.security.https_first_pbm", 305 curTest.https_first_pbm 306 ); 307 } 308 309 function setUpChannel() { 310 // 1) Set up Principal using OA in case of Private Browsing 311 let attr = {}; 312 if (curTest.pbm) { 313 attr.privateBrowsingId = 1; 314 } 315 let uri = Services.io.newURI("http://test1.example.com"); 316 let principal = Services.scriptSecurityManager.createContentPrincipal( 317 uri, 318 attr 319 ); 320 321 // 2) Set up Channel 322 var chan = NetUtil.newChannel({ 323 uri: HTTP_TEST_URL + TEST_PATH, 324 loadingPrincipal: principal, 325 contentPolicyType: curTest.contentType, 326 securityFlags: Ci.nsILoadInfo.SEC_ALLOW_CROSS_ORIGIN_SEC_CONTEXT_IS_NULL, 327 }); 328 chan.QueryInterface(Ci.nsIHttpChannel); 329 chan.requestMethod = "GET"; 330 chan.setRequestHeader("Authorization", "Basic user:pass", false); 331 332 let loadGroup = Cc["@mozilla.org/network/load-group;1"].createInstance( 333 Ci.nsILoadGroup 334 ); 335 if (curTest.pbm) { 336 loadGroup.notificationCallbacks = Cu.createPrivateLoadContext(); 337 chan.loadGroup = loadGroup; 338 chan.notificationCallbacks = Cu.createPrivateLoadContext(); 339 } else { 340 loadGroup.notificationCallbacks = Cu.createLoadContext(); 341 chan.loadGroup = loadGroup; 342 chan.notificationCallbacks = Cu.createLoadContext(); 343 } 344 return chan; 345 } 346 347 function serverHandler() { 348 // dummy implementation 349 } 350 351 function run_next_test() { 352 curTest = TESTS.shift(); 353 if (!curTest) { 354 httpserver.stop(do_test_finished); 355 return; 356 } 357 358 setUpPrefs(); 359 360 channel = setUpChannel(); 361 channel.asyncOpen(new ChannelListener()); 362 } 363 364 function run_test() { 365 do_get_profile(); 366 367 // set up the test environment 368 httpserver = new HttpServer(); 369 httpserver.registerPathHandler(TEST_PATH, serverHandler); 370 httpserver.start(-1); 371 372 run_next_test(); 373 do_test_pending(); 374 }