test_protocolproxyservice-async-filters.js (11790B)
1 /* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */ 2 /* vim:set ts=2 sw=2 sts=2 et: */ 3 /* This Source Code Form is subject to the terms of the Mozilla Public 4 * License, v. 2.0. If a copy of the MPL was not distributed with this 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 7 // This testcase exercises the Protocol Proxy Service's async filter functionality 8 // run_filter_*() are entry points for each individual test. 9 10 "use strict"; 11 12 var pps = Cc["@mozilla.org/network/protocol-proxy-service;1"].getService(); 13 14 /** 15 * Test nsIProtocolHandler that allows proxying, but doesn't allow HTTP 16 * proxying. 17 */ 18 function TestProtocolHandler() {} 19 TestProtocolHandler.prototype = { 20 QueryInterface: ChromeUtils.generateQI(["nsIProtocolHandler"]), 21 scheme: "moz-test", 22 defaultPort: -1, 23 protocolFlags: 24 Ci.nsIProtocolHandler.URI_NOAUTH | 25 Ci.nsIProtocolHandler.URI_NORELATIVE | 26 Ci.nsIProtocolHandler.ALLOWS_PROXY | 27 Ci.nsIProtocolHandler.URI_DANGEROUS_TO_LOAD, 28 newChannel() { 29 throw Components.Exception("", Cr.NS_ERROR_NOT_IMPLEMENTED); 30 }, 31 allowPort() { 32 return true; 33 }, 34 }; 35 36 function TestProtocolHandlerFactory() {} 37 TestProtocolHandlerFactory.prototype = { 38 createInstance(iid) { 39 return new TestProtocolHandler().QueryInterface(iid); 40 }, 41 }; 42 43 function register_test_protocol_handler() { 44 var reg = Components.manager.QueryInterface(Ci.nsIComponentRegistrar); 45 reg.registerFactory( 46 Components.ID("{4ea7dd3a-8cae-499c-9f18-e1de773ca25b}"), 47 "TestProtocolHandler", 48 "@mozilla.org/network/protocol;1?name=moz-test", 49 new TestProtocolHandlerFactory() 50 ); 51 } 52 53 function check_proxy(pi, type, host, port, flags, timeout, hasNext) { 54 Assert.notEqual(pi, null); 55 Assert.equal(pi.type, type); 56 Assert.equal(pi.host, host); 57 Assert.equal(pi.port, port); 58 if (flags != -1) { 59 Assert.equal(pi.flags, flags); 60 } 61 if (timeout != -1) { 62 Assert.equal(pi.failoverTimeout, timeout); 63 } 64 if (hasNext) { 65 Assert.notEqual(pi.failoverProxy, null); 66 } else { 67 Assert.equal(pi.failoverProxy, null); 68 } 69 } 70 71 const SYNC = 0; 72 const THROW = 1; 73 const ASYNC = 2; 74 75 function TestFilter(type, host, port, flags, timeout, result) { 76 this._type = type; 77 this._host = host; 78 this._port = port; 79 this._flags = flags; 80 this._timeout = timeout; 81 this._result = result; 82 } 83 TestFilter.prototype = { 84 _type: "", 85 _host: "", 86 _port: -1, 87 _flags: 0, 88 _timeout: 0, 89 _async: false, 90 _throwing: false, 91 92 QueryInterface: ChromeUtils.generateQI(["nsIProtocolProxyFilter"]), 93 94 applyFilter(uri, pi, cb) { 95 if (this._result == THROW) { 96 throw Components.Exception("", Cr.NS_ERROR_FAILURE); 97 } 98 99 var pi_tail = pps.newProxyInfo( 100 this._type, 101 this._host, 102 this._port, 103 "", 104 "", 105 this._flags, 106 this._timeout, 107 null 108 ); 109 if (pi) { 110 pi.failoverProxy = pi_tail; 111 } else { 112 pi = pi_tail; 113 } 114 115 if (this._result == ASYNC) { 116 executeSoon(() => { 117 cb.onProxyFilterResult(pi); 118 }); 119 } else { 120 cb.onProxyFilterResult(pi); 121 } 122 }, 123 }; 124 125 function resolveCallback() {} 126 resolveCallback.prototype = { 127 nextFunction: null, 128 129 QueryInterface: ChromeUtils.generateQI(["nsIProtocolProxyCallback"]), 130 131 onProxyAvailable(req, channel, pi) { 132 this.nextFunction(pi); 133 }, 134 }; 135 136 // ============================================================== 137 138 var filter1; 139 var filter2; 140 var filter3; 141 142 function run_filter_test1() { 143 filter1 = new TestFilter("http", "foo", 8080, 0, 10, ASYNC); 144 filter2 = new TestFilter("http", "bar", 8090, 0, 10, ASYNC); 145 pps.registerFilter(filter1, 20); 146 pps.registerFilter(filter2, 10); 147 148 var cb = new resolveCallback(); 149 cb.nextFunction = filter_test1_1; 150 var channel = NetUtil.newChannel({ 151 uri: "http://www.mozilla.org/", 152 loadUsingSystemPrincipal: true, 153 }); 154 pps.asyncResolve(channel, 0, cb); 155 } 156 157 function filter_test1_1(pi) { 158 check_proxy(pi, "http", "bar", 8090, 0, 10, true); 159 check_proxy(pi.failoverProxy, "http", "foo", 8080, 0, 10, false); 160 161 pps.unregisterFilter(filter2); 162 163 var cb = new resolveCallback(); 164 cb.nextFunction = filter_test1_2; 165 var channel = NetUtil.newChannel({ 166 uri: "http://www.mozilla.org/", 167 loadUsingSystemPrincipal: true, 168 }); 169 pps.asyncResolve(channel, 0, cb); 170 } 171 172 function filter_test1_2(pi) { 173 check_proxy(pi, "http", "foo", 8080, 0, 10, false); 174 175 pps.unregisterFilter(filter1); 176 177 var cb = new resolveCallback(); 178 cb.nextFunction = filter_test1_3; 179 var channel = NetUtil.newChannel({ 180 uri: "http://www.mozilla.org/", 181 loadUsingSystemPrincipal: true, 182 }); 183 pps.asyncResolve(channel, 0, cb); 184 } 185 186 function filter_test1_3(pi) { 187 Assert.equal(pi, null); 188 run_filter2_sync_async(); 189 } 190 191 function run_filter2_sync_async() { 192 filter1 = new TestFilter("http", "foo", 8080, 0, 10, SYNC); 193 filter2 = new TestFilter("http", "bar", 8090, 0, 10, ASYNC); 194 pps.registerFilter(filter1, 20); 195 pps.registerFilter(filter2, 10); 196 197 var cb = new resolveCallback(); 198 cb.nextFunction = filter_test2_1; 199 var channel = NetUtil.newChannel({ 200 uri: "http://www.mozilla.org/", 201 loadUsingSystemPrincipal: true, 202 }); 203 pps.asyncResolve(channel, 0, cb); 204 } 205 206 function filter_test2_1(pi) { 207 check_proxy(pi, "http", "bar", 8090, 0, 10, true); 208 check_proxy(pi.failoverProxy, "http", "foo", 8080, 0, 10, false); 209 210 pps.unregisterFilter(filter1); 211 pps.unregisterFilter(filter2); 212 213 run_filter3_async_sync(); 214 } 215 216 function run_filter3_async_sync() { 217 filter1 = new TestFilter("http", "foo", 8080, 0, 10, ASYNC); 218 filter2 = new TestFilter("http", "bar", 8090, 0, 10, SYNC); 219 pps.registerFilter(filter1, 20); 220 pps.registerFilter(filter2, 10); 221 222 var cb = new resolveCallback(); 223 cb.nextFunction = filter_test3_1; 224 var channel = NetUtil.newChannel({ 225 uri: "http://www.mozilla.org/", 226 loadUsingSystemPrincipal: true, 227 }); 228 pps.asyncResolve(channel, 0, cb); 229 } 230 231 function filter_test3_1(pi) { 232 check_proxy(pi, "http", "bar", 8090, 0, 10, true); 233 check_proxy(pi.failoverProxy, "http", "foo", 8080, 0, 10, false); 234 235 pps.unregisterFilter(filter1); 236 pps.unregisterFilter(filter2); 237 238 run_filter4_throwing_sync_sync(); 239 } 240 241 function run_filter4_throwing_sync_sync() { 242 filter1 = new TestFilter("", "", 0, 0, 0, THROW); 243 filter2 = new TestFilter("http", "foo", 8080, 0, 10, SYNC); 244 filter3 = new TestFilter("http", "bar", 8090, 0, 10, SYNC); 245 pps.registerFilter(filter1, 20); 246 pps.registerFilter(filter2, 10); 247 pps.registerFilter(filter3, 5); 248 249 var cb = new resolveCallback(); 250 cb.nextFunction = filter_test4_1; 251 var channel = NetUtil.newChannel({ 252 uri: "http://www.mozilla2.org/", 253 loadUsingSystemPrincipal: true, 254 }); 255 pps.asyncResolve(channel, 0, cb); 256 } 257 258 function filter_test4_1(pi) { 259 check_proxy(pi, "http", "bar", 8090, 0, 10, true); 260 check_proxy(pi.failoverProxy, "http", "foo", 8080, 0, 10, false); 261 262 pps.unregisterFilter(filter1); 263 pps.unregisterFilter(filter2); 264 pps.unregisterFilter(filter3); 265 266 run_filter5_sync_sync_throwing(); 267 } 268 269 function run_filter5_sync_sync_throwing() { 270 filter1 = new TestFilter("http", "foo", 8080, 0, 10, SYNC); 271 filter2 = new TestFilter("http", "bar", 8090, 0, 10, SYNC); 272 filter3 = new TestFilter("", "", 0, 0, 0, THROW); 273 pps.registerFilter(filter1, 20); 274 pps.registerFilter(filter2, 10); 275 pps.registerFilter(filter3, 5); 276 277 var cb = new resolveCallback(); 278 cb.nextFunction = filter_test5_1; 279 var channel = NetUtil.newChannel({ 280 uri: "http://www.mozilla.org/", 281 loadUsingSystemPrincipal: true, 282 }); 283 pps.asyncResolve(channel, 0, cb); 284 } 285 286 function filter_test5_1(pi) { 287 check_proxy(pi, "http", "bar", 8090, 0, 10, true); 288 check_proxy(pi.failoverProxy, "http", "foo", 8080, 0, 10, false); 289 290 pps.unregisterFilter(filter1); 291 pps.unregisterFilter(filter2); 292 pps.unregisterFilter(filter3); 293 294 run_filter5_2_throwing_async_async(); 295 } 296 297 function run_filter5_2_throwing_async_async() { 298 filter1 = new TestFilter("", "", 0, 0, 0, THROW); 299 filter2 = new TestFilter("http", "foo", 8080, 0, 10, ASYNC); 300 filter3 = new TestFilter("http", "bar", 8090, 0, 10, ASYNC); 301 pps.registerFilter(filter1, 20); 302 pps.registerFilter(filter2, 10); 303 pps.registerFilter(filter3, 5); 304 305 var cb = new resolveCallback(); 306 cb.nextFunction = filter_test5_2; 307 var channel = NetUtil.newChannel({ 308 uri: "http://www.mozilla.org/", 309 loadUsingSystemPrincipal: true, 310 }); 311 pps.asyncResolve(channel, 0, cb); 312 } 313 314 function filter_test5_2(pi) { 315 check_proxy(pi, "http", "bar", 8090, 0, 10, true); 316 check_proxy(pi.failoverProxy, "http", "foo", 8080, 0, 10, false); 317 318 pps.unregisterFilter(filter1); 319 pps.unregisterFilter(filter2); 320 pps.unregisterFilter(filter3); 321 322 run_filter6_async_async_throwing(); 323 } 324 325 function run_filter6_async_async_throwing() { 326 filter1 = new TestFilter("http", "foo", 8080, 0, 10, ASYNC); 327 filter2 = new TestFilter("http", "bar", 8090, 0, 10, ASYNC); 328 filter3 = new TestFilter("", "", 0, 0, 0, THROW); 329 pps.registerFilter(filter1, 20); 330 pps.registerFilter(filter2, 10); 331 pps.registerFilter(filter3, 5); 332 333 var cb = new resolveCallback(); 334 cb.nextFunction = filter_test6_1; 335 var channel = NetUtil.newChannel({ 336 uri: "http://www.mozilla.org/", 337 loadUsingSystemPrincipal: true, 338 }); 339 pps.asyncResolve(channel, 0, cb); 340 } 341 342 function filter_test6_1(pi) { 343 check_proxy(pi, "http", "bar", 8090, 0, 10, true); 344 check_proxy(pi.failoverProxy, "http", "foo", 8080, 0, 10, false); 345 346 pps.unregisterFilter(filter1); 347 pps.unregisterFilter(filter2); 348 pps.unregisterFilter(filter3); 349 350 run_filter7_async_throwing_async(); 351 } 352 353 function run_filter7_async_throwing_async() { 354 filter1 = new TestFilter("http", "foo", 8080, 0, 10, ASYNC); 355 filter2 = new TestFilter("", "", 0, 0, 0, THROW); 356 filter3 = new TestFilter("http", "bar", 8090, 0, 10, ASYNC); 357 pps.registerFilter(filter1, 20); 358 pps.registerFilter(filter2, 10); 359 pps.registerFilter(filter3, 5); 360 361 var cb = new resolveCallback(); 362 cb.nextFunction = filter_test7_1; 363 var channel = NetUtil.newChannel({ 364 uri: "http://www.mozilla.org/", 365 loadUsingSystemPrincipal: true, 366 }); 367 pps.asyncResolve(channel, 0, cb); 368 } 369 370 function filter_test7_1(pi) { 371 check_proxy(pi, "http", "bar", 8090, 0, 10, true); 372 check_proxy(pi.failoverProxy, "http", "foo", 8080, 0, 10, false); 373 374 pps.unregisterFilter(filter1); 375 pps.unregisterFilter(filter2); 376 pps.unregisterFilter(filter3); 377 378 run_filter8_sync_throwing_sync(); 379 } 380 381 function run_filter8_sync_throwing_sync() { 382 filter1 = new TestFilter("http", "foo", 8080, 0, 10, SYNC); 383 filter2 = new TestFilter("", "", 0, 0, 0, THROW); 384 filter3 = new TestFilter("http", "bar", 8090, 0, 10, SYNC); 385 pps.registerFilter(filter1, 20); 386 pps.registerFilter(filter2, 10); 387 pps.registerFilter(filter3, 5); 388 389 var cb = new resolveCallback(); 390 cb.nextFunction = filter_test8_1; 391 var channel = NetUtil.newChannel({ 392 uri: "http://www.mozilla.org/", 393 loadUsingSystemPrincipal: true, 394 }); 395 pps.asyncResolve(channel, 0, cb); 396 } 397 398 function filter_test8_1(pi) { 399 check_proxy(pi, "http", "bar", 8090, 0, 10, true); 400 check_proxy(pi.failoverProxy, "http", "foo", 8080, 0, 10, false); 401 402 pps.unregisterFilter(filter1); 403 pps.unregisterFilter(filter2); 404 pps.unregisterFilter(filter3); 405 406 run_filter9_throwing(); 407 } 408 409 function run_filter9_throwing() { 410 filter1 = new TestFilter("", "", 0, 0, 0, THROW); 411 pps.registerFilter(filter1, 20); 412 413 var cb = new resolveCallback(); 414 cb.nextFunction = filter_test9_1; 415 var channel = NetUtil.newChannel({ 416 uri: "http://www.mozilla.org/", 417 loadUsingSystemPrincipal: true, 418 }); 419 pps.asyncResolve(channel, 0, cb); 420 } 421 422 function filter_test9_1(pi) { 423 Assert.equal(pi, null); 424 do_test_finished(); 425 } 426 427 // ========================================= 428 429 function run_test() { 430 register_test_protocol_handler(); 431 432 // start of asynchronous test chain 433 run_filter_test1(); 434 do_test_pending(); 435 }