Thu, 15 Jan 2015 15:55:04 +0100
Back out 97036ab72558 which inappropriately compared turds to third parties.
michael@0 | 1 | /* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
michael@0 | 2 | /* vim:set ts=2 sw=2 sts=2 et: */ |
michael@0 | 3 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 6 | |
michael@0 | 7 | // This testcase exercises the Protocol Proxy Service |
michael@0 | 8 | |
michael@0 | 9 | // These are the major sub tests: |
michael@0 | 10 | // run_filter_test(); |
michael@0 | 11 | // run_filter_test2() |
michael@0 | 12 | // run_filter_test3() |
michael@0 | 13 | // run_pref_test(); |
michael@0 | 14 | // run_pac_test(); |
michael@0 | 15 | // run_pac_cancel_test(); |
michael@0 | 16 | // run_proxy_host_filters_test(); |
michael@0 | 17 | // run_myipaddress_test(); |
michael@0 | 18 | // run_failed_script_test(); |
michael@0 | 19 | // run_isresolvable_test(); |
michael@0 | 20 | |
michael@0 | 21 | var ios = Components.classes["@mozilla.org/network/io-service;1"] |
michael@0 | 22 | .getService(Components.interfaces.nsIIOService); |
michael@0 | 23 | var pps = Components.classes["@mozilla.org/network/protocol-proxy-service;1"] |
michael@0 | 24 | .getService(); |
michael@0 | 25 | var prefs = Components.classes["@mozilla.org/preferences-service;1"] |
michael@0 | 26 | .getService(Components.interfaces.nsIPrefBranch); |
michael@0 | 27 | |
michael@0 | 28 | /** |
michael@0 | 29 | * Test nsIProtocolHandler that allows proxying, but doesn't allow HTTP |
michael@0 | 30 | * proxying. |
michael@0 | 31 | */ |
michael@0 | 32 | function TestProtocolHandler() { |
michael@0 | 33 | } |
michael@0 | 34 | TestProtocolHandler.prototype = { |
michael@0 | 35 | QueryInterface: function(iid) { |
michael@0 | 36 | if (iid.equals(Components.interfaces.nsIProtocolHandler) || |
michael@0 | 37 | iid.equals(Components.interfaces.nsISupports)) |
michael@0 | 38 | return this; |
michael@0 | 39 | throw Components.results.NS_ERROR_NO_INTERFACE; |
michael@0 | 40 | }, |
michael@0 | 41 | scheme: "moz-test", |
michael@0 | 42 | defaultPort: -1, |
michael@0 | 43 | protocolFlags: Components.interfaces.nsIProtocolHandler.URI_NOAUTH | |
michael@0 | 44 | Components.interfaces.nsIProtocolHandler.URI_NORELATIVE | |
michael@0 | 45 | Components.interfaces.nsIProtocolHandler.ALLOWS_PROXY | |
michael@0 | 46 | Components.interfaces.nsIProtocolHandler.URI_DANGEROUS_TO_LOAD, |
michael@0 | 47 | newURI: function(spec, originCharset, baseURI) { |
michael@0 | 48 | var uri = Components.classes["@mozilla.org/network/simple-uri;1"] |
michael@0 | 49 | .createInstance(Components.interfaces.nsIURI); |
michael@0 | 50 | uri.spec = spec; |
michael@0 | 51 | return uri; |
michael@0 | 52 | }, |
michael@0 | 53 | newChannel: function(uri) { |
michael@0 | 54 | throw Components.results.NS_ERROR_NOT_IMPLEMENTED; |
michael@0 | 55 | }, |
michael@0 | 56 | allowPort: function(port, scheme) { |
michael@0 | 57 | return true; |
michael@0 | 58 | } |
michael@0 | 59 | }; |
michael@0 | 60 | |
michael@0 | 61 | function TestProtocolHandlerFactory() { |
michael@0 | 62 | } |
michael@0 | 63 | TestProtocolHandlerFactory.prototype = { |
michael@0 | 64 | createInstance: function(delegate, iid) { |
michael@0 | 65 | return new TestProtocolHandler().QueryInterface(iid); |
michael@0 | 66 | }, |
michael@0 | 67 | lockFactory: function(lock) { |
michael@0 | 68 | } |
michael@0 | 69 | }; |
michael@0 | 70 | |
michael@0 | 71 | function register_test_protocol_handler() { |
michael@0 | 72 | var reg = Components.manager.QueryInterface( |
michael@0 | 73 | Components.interfaces.nsIComponentRegistrar); |
michael@0 | 74 | reg.registerFactory(Components.ID("{4ea7dd3a-8cae-499c-9f18-e1de773ca25b}"), |
michael@0 | 75 | "TestProtocolHandler", |
michael@0 | 76 | "@mozilla.org/network/protocol;1?name=moz-test", |
michael@0 | 77 | new TestProtocolHandlerFactory()); |
michael@0 | 78 | } |
michael@0 | 79 | |
michael@0 | 80 | function check_proxy(pi, type, host, port, flags, timeout, hasNext) { |
michael@0 | 81 | do_check_neq(pi, null); |
michael@0 | 82 | do_check_eq(pi.type, type); |
michael@0 | 83 | do_check_eq(pi.host, host); |
michael@0 | 84 | do_check_eq(pi.port, port); |
michael@0 | 85 | if (flags != -1) |
michael@0 | 86 | do_check_eq(pi.flags, flags); |
michael@0 | 87 | if (timeout != -1) |
michael@0 | 88 | do_check_eq(pi.failoverTimeout, timeout); |
michael@0 | 89 | if (hasNext) |
michael@0 | 90 | do_check_neq(pi.failoverProxy, null); |
michael@0 | 91 | else |
michael@0 | 92 | do_check_eq(pi.failoverProxy, null); |
michael@0 | 93 | } |
michael@0 | 94 | |
michael@0 | 95 | function TestFilter(type, host, port, flags, timeout) { |
michael@0 | 96 | this._type = type; |
michael@0 | 97 | this._host = host; |
michael@0 | 98 | this._port = port; |
michael@0 | 99 | this._flags = flags; |
michael@0 | 100 | this._timeout = timeout; |
michael@0 | 101 | } |
michael@0 | 102 | TestFilter.prototype = { |
michael@0 | 103 | _type: "", |
michael@0 | 104 | _host: "", |
michael@0 | 105 | _port: -1, |
michael@0 | 106 | _flags: 0, |
michael@0 | 107 | _timeout: 0, |
michael@0 | 108 | QueryInterface: function(iid) { |
michael@0 | 109 | if (iid.equals(Components.interfaces.nsIProtocolProxyFilter) || |
michael@0 | 110 | iid.equals(Components.interfaces.nsISupports)) |
michael@0 | 111 | return this; |
michael@0 | 112 | throw Components.results.NS_ERROR_NO_INTERFACE; |
michael@0 | 113 | }, |
michael@0 | 114 | applyFilter: function(pps, uri, pi) { |
michael@0 | 115 | var pi_tail = pps.newProxyInfo(this._type, this._host, this._port, |
michael@0 | 116 | this._flags, this._timeout, null); |
michael@0 | 117 | if (pi) |
michael@0 | 118 | pi.failoverProxy = pi_tail; |
michael@0 | 119 | else |
michael@0 | 120 | pi = pi_tail; |
michael@0 | 121 | return pi; |
michael@0 | 122 | } |
michael@0 | 123 | }; |
michael@0 | 124 | |
michael@0 | 125 | function BasicFilter() {} |
michael@0 | 126 | BasicFilter.prototype = { |
michael@0 | 127 | QueryInterface: function(iid) { |
michael@0 | 128 | if (iid.equals(Components.interfaces.nsIProtocolProxyFilter) || |
michael@0 | 129 | iid.equals(Components.interfaces.nsISupports)) |
michael@0 | 130 | return this; |
michael@0 | 131 | throw Components.results.NS_ERROR_NO_INTERFACE; |
michael@0 | 132 | }, |
michael@0 | 133 | applyFilter: function(pps, uri, pi) { |
michael@0 | 134 | return pps.newProxyInfo("http", "localhost", 8080, 0, 10, |
michael@0 | 135 | pps.newProxyInfo("direct", "", -1, 0, 0, null)); |
michael@0 | 136 | } |
michael@0 | 137 | }; |
michael@0 | 138 | |
michael@0 | 139 | function resolveCallback() { } |
michael@0 | 140 | resolveCallback.prototype = { |
michael@0 | 141 | nextFunction: null, |
michael@0 | 142 | |
michael@0 | 143 | QueryInterface : function (iid) { |
michael@0 | 144 | const interfaces = [Components.interfaces.nsIProtocolProxyCallback, |
michael@0 | 145 | Components.interfaces.nsISupports]; |
michael@0 | 146 | if (!interfaces.some( function(v) { return iid.equals(v) } )) |
michael@0 | 147 | throw Components.results.NS_ERROR_NO_INTERFACE; |
michael@0 | 148 | return this; |
michael@0 | 149 | }, |
michael@0 | 150 | |
michael@0 | 151 | onProxyAvailable : function (req, uri, pi, status) { |
michael@0 | 152 | this.nextFunction(pi); |
michael@0 | 153 | } |
michael@0 | 154 | }; |
michael@0 | 155 | |
michael@0 | 156 | function run_filter_test() { |
michael@0 | 157 | var uri = ios.newURI("http://www.mozilla.org/", null, null); |
michael@0 | 158 | |
michael@0 | 159 | // Verify initial state |
michael@0 | 160 | var cb = new resolveCallback(); |
michael@0 | 161 | cb.nextFunction = filter_test0_1; |
michael@0 | 162 | var req = pps.asyncResolve(uri, 0, cb); |
michael@0 | 163 | } |
michael@0 | 164 | |
michael@0 | 165 | var filter01; |
michael@0 | 166 | var filter02; |
michael@0 | 167 | |
michael@0 | 168 | function filter_test0_1(pi) { |
michael@0 | 169 | do_check_eq(pi, null); |
michael@0 | 170 | |
michael@0 | 171 | // Push a filter and verify the results |
michael@0 | 172 | |
michael@0 | 173 | filter01 = new BasicFilter(); |
michael@0 | 174 | filter02 = new BasicFilter(); |
michael@0 | 175 | pps.registerFilter(filter01, 10); |
michael@0 | 176 | pps.registerFilter(filter02, 20); |
michael@0 | 177 | |
michael@0 | 178 | var cb = new resolveCallback(); |
michael@0 | 179 | cb.nextFunction = filter_test0_2; |
michael@0 | 180 | var uri = ios.newURI("http://www.mozilla.org/", null, null); |
michael@0 | 181 | var req = pps.asyncResolve(uri, 0, cb); |
michael@0 | 182 | } |
michael@0 | 183 | |
michael@0 | 184 | function filter_test0_2(pi) |
michael@0 | 185 | { |
michael@0 | 186 | check_proxy(pi, "http", "localhost", 8080, 0, 10, true); |
michael@0 | 187 | check_proxy(pi.failoverProxy, "direct", "", -1, 0, 0, false); |
michael@0 | 188 | |
michael@0 | 189 | pps.unregisterFilter(filter02); |
michael@0 | 190 | |
michael@0 | 191 | var cb = new resolveCallback(); |
michael@0 | 192 | cb.nextFunction = filter_test0_3; |
michael@0 | 193 | var uri = ios.newURI("http://www.mozilla.org/", null, null); |
michael@0 | 194 | var req = pps.asyncResolve(uri, 0, cb); |
michael@0 | 195 | } |
michael@0 | 196 | |
michael@0 | 197 | function filter_test0_3(pi) |
michael@0 | 198 | { |
michael@0 | 199 | check_proxy(pi, "http", "localhost", 8080, 0, 10, true); |
michael@0 | 200 | check_proxy(pi.failoverProxy, "direct", "", -1, 0, 0, false); |
michael@0 | 201 | |
michael@0 | 202 | // Remove filter and verify that we return to the initial state |
michael@0 | 203 | |
michael@0 | 204 | pps.unregisterFilter(filter01); |
michael@0 | 205 | |
michael@0 | 206 | var cb = new resolveCallback(); |
michael@0 | 207 | cb.nextFunction = filter_test0_4; |
michael@0 | 208 | var uri = ios.newURI("http://www.mozilla.org/", null, null); |
michael@0 | 209 | var req = pps.asyncResolve(uri, 0, cb); |
michael@0 | 210 | } |
michael@0 | 211 | |
michael@0 | 212 | function filter_test0_4(pi) |
michael@0 | 213 | { |
michael@0 | 214 | do_check_eq(pi, null); |
michael@0 | 215 | run_filter_test2(); |
michael@0 | 216 | } |
michael@0 | 217 | |
michael@0 | 218 | var filter11; |
michael@0 | 219 | var filter12; |
michael@0 | 220 | |
michael@0 | 221 | function run_filter_test2() { |
michael@0 | 222 | // Push a filter and verify the results |
michael@0 | 223 | |
michael@0 | 224 | filter11 = new TestFilter("http", "foo", 8080, 0, 10); |
michael@0 | 225 | filter12 = new TestFilter("http", "bar", 8090, 0, 10); |
michael@0 | 226 | pps.registerFilter(filter11, 20); |
michael@0 | 227 | pps.registerFilter(filter12, 10); |
michael@0 | 228 | |
michael@0 | 229 | var cb = new resolveCallback(); |
michael@0 | 230 | cb.nextFunction = filter_test1_1; |
michael@0 | 231 | var uri = ios.newURI("http://www.mozilla.org/", null, null); |
michael@0 | 232 | var req = pps.asyncResolve(uri, 0, cb); |
michael@0 | 233 | } |
michael@0 | 234 | |
michael@0 | 235 | function filter_test1_1(pi) { |
michael@0 | 236 | check_proxy(pi, "http", "bar", 8090, 0, 10, true); |
michael@0 | 237 | check_proxy(pi.failoverProxy, "http", "foo", 8080, 0, 10, false); |
michael@0 | 238 | |
michael@0 | 239 | pps.unregisterFilter(filter12); |
michael@0 | 240 | |
michael@0 | 241 | var cb = new resolveCallback(); |
michael@0 | 242 | cb.nextFunction = filter_test1_2; |
michael@0 | 243 | var uri = ios.newURI("http://www.mozilla.org/", null, null); |
michael@0 | 244 | var req = pps.asyncResolve(uri, 0, cb); |
michael@0 | 245 | } |
michael@0 | 246 | |
michael@0 | 247 | function filter_test1_2(pi) { |
michael@0 | 248 | check_proxy(pi, "http", "foo", 8080, 0, 10, false); |
michael@0 | 249 | |
michael@0 | 250 | // Remove filter and verify that we return to the initial state |
michael@0 | 251 | |
michael@0 | 252 | pps.unregisterFilter(filter11); |
michael@0 | 253 | |
michael@0 | 254 | var cb = new resolveCallback(); |
michael@0 | 255 | cb.nextFunction = filter_test1_3; |
michael@0 | 256 | var uri = ios.newURI("http://www.mozilla.org/", null, null); |
michael@0 | 257 | var req = pps.asyncResolve(uri, 0, cb); |
michael@0 | 258 | } |
michael@0 | 259 | |
michael@0 | 260 | function filter_test1_3(pi) { |
michael@0 | 261 | do_check_eq(pi, null); |
michael@0 | 262 | run_filter_test3(); |
michael@0 | 263 | } |
michael@0 | 264 | |
michael@0 | 265 | var filter_3_1; |
michael@0 | 266 | |
michael@0 | 267 | function run_filter_test3() { |
michael@0 | 268 | var uri = ios.newURI("http://www.mozilla.org/", null, null); |
michael@0 | 269 | |
michael@0 | 270 | // Push a filter and verify the results asynchronously |
michael@0 | 271 | |
michael@0 | 272 | filter_3_1 = new TestFilter("http", "foo", 8080, 0, 10); |
michael@0 | 273 | pps.registerFilter(filter_3_1, 20); |
michael@0 | 274 | |
michael@0 | 275 | var cb = new resolveCallback(); |
michael@0 | 276 | cb.nextFunction = filter_test3_1; |
michael@0 | 277 | var req = pps.asyncResolve(uri, 0, cb); |
michael@0 | 278 | } |
michael@0 | 279 | |
michael@0 | 280 | function filter_test3_1(pi) { |
michael@0 | 281 | check_proxy(pi, "http", "foo", 8080, 0, 10, false); |
michael@0 | 282 | pps.unregisterFilter(filter_3_1); |
michael@0 | 283 | run_pref_test(); |
michael@0 | 284 | } |
michael@0 | 285 | |
michael@0 | 286 | function run_pref_test() { |
michael@0 | 287 | var uri = ios.newURI("http://www.mozilla.org/", null, null); |
michael@0 | 288 | |
michael@0 | 289 | // Verify 'direct' setting |
michael@0 | 290 | |
michael@0 | 291 | prefs.setIntPref("network.proxy.type", 0); |
michael@0 | 292 | |
michael@0 | 293 | var cb = new resolveCallback(); |
michael@0 | 294 | cb.nextFunction = pref_test1_1; |
michael@0 | 295 | var req = pps.asyncResolve(uri, 0, cb); |
michael@0 | 296 | } |
michael@0 | 297 | |
michael@0 | 298 | function pref_test1_1(pi) |
michael@0 | 299 | { |
michael@0 | 300 | do_check_eq(pi, null); |
michael@0 | 301 | |
michael@0 | 302 | // Verify 'manual' setting |
michael@0 | 303 | prefs.setIntPref("network.proxy.type", 1); |
michael@0 | 304 | |
michael@0 | 305 | var cb = new resolveCallback(); |
michael@0 | 306 | cb.nextFunction = pref_test1_2; |
michael@0 | 307 | var uri = ios.newURI("http://www.mozilla.org/", null, null); |
michael@0 | 308 | var req = pps.asyncResolve(uri, 0, cb); |
michael@0 | 309 | } |
michael@0 | 310 | |
michael@0 | 311 | function pref_test1_2(pi) |
michael@0 | 312 | { |
michael@0 | 313 | // nothing yet configured |
michael@0 | 314 | do_check_eq(pi, null); |
michael@0 | 315 | |
michael@0 | 316 | // try HTTP configuration |
michael@0 | 317 | prefs.setCharPref("network.proxy.http", "foopy"); |
michael@0 | 318 | prefs.setIntPref("network.proxy.http_port", 8080); |
michael@0 | 319 | |
michael@0 | 320 | var cb = new resolveCallback(); |
michael@0 | 321 | cb.nextFunction = pref_test1_3; |
michael@0 | 322 | var uri = ios.newURI("http://www.mozilla.org/", null, null); |
michael@0 | 323 | var req = pps.asyncResolve(uri, 0, cb); |
michael@0 | 324 | } |
michael@0 | 325 | |
michael@0 | 326 | function pref_test1_3(pi) |
michael@0 | 327 | { |
michael@0 | 328 | check_proxy(pi, "http", "foopy", 8080, 0, -1, false); |
michael@0 | 329 | |
michael@0 | 330 | prefs.setCharPref("network.proxy.http", ""); |
michael@0 | 331 | prefs.setIntPref("network.proxy.http_port", 0); |
michael@0 | 332 | |
michael@0 | 333 | // try SOCKS configuration |
michael@0 | 334 | prefs.setCharPref("network.proxy.socks", "barbar"); |
michael@0 | 335 | prefs.setIntPref("network.proxy.socks_port", 1203); |
michael@0 | 336 | |
michael@0 | 337 | var cb = new resolveCallback(); |
michael@0 | 338 | cb.nextFunction = pref_test1_4; |
michael@0 | 339 | var uri = ios.newURI("http://www.mozilla.org/", null, null); |
michael@0 | 340 | var req = pps.asyncResolve(uri, 0, cb); |
michael@0 | 341 | } |
michael@0 | 342 | |
michael@0 | 343 | function pref_test1_4(pi) |
michael@0 | 344 | { |
michael@0 | 345 | check_proxy(pi, "socks", "barbar", 1203, 0, -1, false); |
michael@0 | 346 | run_pac_test(); |
michael@0 | 347 | } |
michael@0 | 348 | |
michael@0 | 349 | function run_protocol_handler_test() { |
michael@0 | 350 | var uri = ios.newURI("moz-test:foopy", null, null); |
michael@0 | 351 | |
michael@0 | 352 | var cb = new resolveCallback(); |
michael@0 | 353 | cb.nextFunction = protocol_handler_test_1; |
michael@0 | 354 | var req = pps.asyncResolve(uri, 0, cb); |
michael@0 | 355 | } |
michael@0 | 356 | |
michael@0 | 357 | function protocol_handler_test_1(pi) |
michael@0 | 358 | { |
michael@0 | 359 | do_check_eq(pi, null); |
michael@0 | 360 | prefs.setCharPref("network.proxy.autoconfig_url", ""); |
michael@0 | 361 | prefs.setIntPref("network.proxy.type", 0); |
michael@0 | 362 | |
michael@0 | 363 | run_pac_cancel_test(); |
michael@0 | 364 | } |
michael@0 | 365 | |
michael@0 | 366 | function TestResolveCallback() { |
michael@0 | 367 | } |
michael@0 | 368 | TestResolveCallback.prototype = { |
michael@0 | 369 | QueryInterface: |
michael@0 | 370 | function TestResolveCallback_QueryInterface(iid) { |
michael@0 | 371 | if (iid.equals(Components.interfaces.nsIProtocolProxyCallback) || |
michael@0 | 372 | iid.equals(Components.interfaces.nsISupports)) |
michael@0 | 373 | return this; |
michael@0 | 374 | throw Components.results.NS_ERROR_NO_INTERFACE; |
michael@0 | 375 | }, |
michael@0 | 376 | |
michael@0 | 377 | onProxyAvailable: |
michael@0 | 378 | function TestResolveCallback_onProxyAvailable(req, uri, pi, status) { |
michael@0 | 379 | dump("*** uri=" + uri.spec + ", status=" + status + "\n"); |
michael@0 | 380 | |
michael@0 | 381 | do_check_neq(req, null); |
michael@0 | 382 | do_check_neq(uri, null); |
michael@0 | 383 | do_check_eq(status, 0); |
michael@0 | 384 | do_check_neq(pi, null); |
michael@0 | 385 | |
michael@0 | 386 | check_proxy(pi, "http", "foopy", 8080, 0, -1, true); |
michael@0 | 387 | check_proxy(pi.failoverProxy, "direct", "", -1, -1, -1, false); |
michael@0 | 388 | |
michael@0 | 389 | run_protocol_handler_test(); |
michael@0 | 390 | } |
michael@0 | 391 | }; |
michael@0 | 392 | |
michael@0 | 393 | function run_pac_test() { |
michael@0 | 394 | var pac = 'data:text/plain,' + |
michael@0 | 395 | 'function FindProxyForURL(url, host) {' + |
michael@0 | 396 | ' return "PROXY foopy:8080; DIRECT";' + |
michael@0 | 397 | '}'; |
michael@0 | 398 | var uri = ios.newURI("http://www.mozilla.org/", null, null); |
michael@0 | 399 | |
michael@0 | 400 | // Configure PAC |
michael@0 | 401 | |
michael@0 | 402 | prefs.setIntPref("network.proxy.type", 2); |
michael@0 | 403 | prefs.setCharPref("network.proxy.autoconfig_url", pac); |
michael@0 | 404 | |
michael@0 | 405 | var req = pps.asyncResolve(uri, 0, new TestResolveCallback()); |
michael@0 | 406 | } |
michael@0 | 407 | |
michael@0 | 408 | function TestResolveCancelationCallback() { |
michael@0 | 409 | } |
michael@0 | 410 | TestResolveCancelationCallback.prototype = { |
michael@0 | 411 | QueryInterface: |
michael@0 | 412 | function TestResolveCallback_QueryInterface(iid) { |
michael@0 | 413 | if (iid.equals(Components.interfaces.nsIProtocolProxyCallback) || |
michael@0 | 414 | iid.equals(Components.interfaces.nsISupports)) |
michael@0 | 415 | return this; |
michael@0 | 416 | throw Components.results.NS_ERROR_NO_INTERFACE; |
michael@0 | 417 | }, |
michael@0 | 418 | |
michael@0 | 419 | onProxyAvailable: |
michael@0 | 420 | function TestResolveCancelationCallback_onProxyAvailable(req, uri, pi, status) { |
michael@0 | 421 | dump("*** uri=" + uri.spec + ", status=" + status + "\n"); |
michael@0 | 422 | |
michael@0 | 423 | do_check_neq(req, null); |
michael@0 | 424 | do_check_neq(uri, null); |
michael@0 | 425 | do_check_eq(status, Components.results.NS_ERROR_ABORT); |
michael@0 | 426 | do_check_eq(pi, null); |
michael@0 | 427 | |
michael@0 | 428 | prefs.setCharPref("network.proxy.autoconfig_url", ""); |
michael@0 | 429 | prefs.setIntPref("network.proxy.type", 0); |
michael@0 | 430 | |
michael@0 | 431 | run_proxy_host_filters_test(); |
michael@0 | 432 | } |
michael@0 | 433 | }; |
michael@0 | 434 | |
michael@0 | 435 | function run_pac_cancel_test() { |
michael@0 | 436 | var uri = ios.newURI("http://www.mozilla.org/", null, null); |
michael@0 | 437 | |
michael@0 | 438 | // Configure PAC |
michael@0 | 439 | var pac = 'data:text/plain,' + |
michael@0 | 440 | 'function FindProxyForURL(url, host) {' + |
michael@0 | 441 | ' return "PROXY foopy:8080; DIRECT";' + |
michael@0 | 442 | '}'; |
michael@0 | 443 | prefs.setIntPref("network.proxy.type", 2); |
michael@0 | 444 | prefs.setCharPref("network.proxy.autoconfig_url", pac); |
michael@0 | 445 | |
michael@0 | 446 | var req = pps.asyncResolve(uri, 0, new TestResolveCancelationCallback()); |
michael@0 | 447 | req.cancel(Components.results.NS_ERROR_ABORT); |
michael@0 | 448 | } |
michael@0 | 449 | |
michael@0 | 450 | var hostList; |
michael@0 | 451 | var hostIDX; |
michael@0 | 452 | var bShouldBeFiltered; |
michael@0 | 453 | var hostNextFX; |
michael@0 | 454 | |
michael@0 | 455 | function check_host_filters(hl, shouldBe, nextFX) { |
michael@0 | 456 | hostList = hl; |
michael@0 | 457 | hostIDX = 0; |
michael@0 | 458 | bShouldBeFiltered = shouldBe; |
michael@0 | 459 | hostNextFX = nextFX; |
michael@0 | 460 | |
michael@0 | 461 | if (hostList.length > hostIDX) |
michael@0 | 462 | check_host_filter(hostIDX); |
michael@0 | 463 | } |
michael@0 | 464 | |
michael@0 | 465 | function check_host_filters_cb() |
michael@0 | 466 | { |
michael@0 | 467 | hostIDX++; |
michael@0 | 468 | if (hostList.length > hostIDX) |
michael@0 | 469 | check_host_filter(hostIDX); |
michael@0 | 470 | else |
michael@0 | 471 | hostNextFX(); |
michael@0 | 472 | } |
michael@0 | 473 | |
michael@0 | 474 | function check_host_filter(i) { |
michael@0 | 475 | var uri; |
michael@0 | 476 | dump("*** uri=" + hostList[i] + " bShouldBeFiltered=" + bShouldBeFiltered + "\n"); |
michael@0 | 477 | uri = ios.newURI(hostList[i], null, null); |
michael@0 | 478 | |
michael@0 | 479 | var cb = new resolveCallback(); |
michael@0 | 480 | cb.nextFunction = host_filter_cb; |
michael@0 | 481 | var req = pps.asyncResolve(uri, 0, cb); |
michael@0 | 482 | } |
michael@0 | 483 | |
michael@0 | 484 | function host_filter_cb(proxy) |
michael@0 | 485 | { |
michael@0 | 486 | if (bShouldBeFiltered) { |
michael@0 | 487 | do_check_eq(proxy, null); |
michael@0 | 488 | } else { |
michael@0 | 489 | do_check_neq(proxy, null); |
michael@0 | 490 | // Just to be sure, let's check that the proxy is correct |
michael@0 | 491 | // - this should match the proxy setup in the calling function |
michael@0 | 492 | check_proxy(proxy, "http", "foopy", 8080, 0, -1, false); |
michael@0 | 493 | } |
michael@0 | 494 | check_host_filters_cb(); |
michael@0 | 495 | } |
michael@0 | 496 | |
michael@0 | 497 | |
michael@0 | 498 | // Verify that hists in the host filter list are not proxied |
michael@0 | 499 | // refers to "network.proxy.no_proxies_on" |
michael@0 | 500 | |
michael@0 | 501 | var uriStrUseProxyList; |
michael@0 | 502 | var uriStrUseProxyList; |
michael@0 | 503 | var hostFilterList; |
michael@0 | 504 | |
michael@0 | 505 | function run_proxy_host_filters_test() { |
michael@0 | 506 | // Get prefs object from DOM |
michael@0 | 507 | // Setup a basic HTTP proxy configuration |
michael@0 | 508 | // - pps.resolve() needs this to return proxy info for non-filtered hosts |
michael@0 | 509 | prefs.setIntPref("network.proxy.type", 1); |
michael@0 | 510 | prefs.setCharPref("network.proxy.http", "foopy"); |
michael@0 | 511 | prefs.setIntPref("network.proxy.http_port", 8080); |
michael@0 | 512 | |
michael@0 | 513 | // Setup host filter list string for "no_proxies_on" |
michael@0 | 514 | hostFilterList = "www.mozilla.org, www.google.com, www.apple.com, " |
michael@0 | 515 | + ".domain, .domain2.org" |
michael@0 | 516 | prefs.setCharPref("network.proxy.no_proxies_on", hostFilterList); |
michael@0 | 517 | do_check_eq(prefs.getCharPref("network.proxy.no_proxies_on"), hostFilterList); |
michael@0 | 518 | |
michael@0 | 519 | var rv; |
michael@0 | 520 | // Check the hosts that should be filtered out |
michael@0 | 521 | uriStrFilterList = [ "http://www.mozilla.org/", |
michael@0 | 522 | "http://www.google.com/", |
michael@0 | 523 | "http://www.apple.com/", |
michael@0 | 524 | "http://somehost.domain/", |
michael@0 | 525 | "http://someotherhost.domain/", |
michael@0 | 526 | "http://somehost.domain2.org/", |
michael@0 | 527 | "http://somehost.subdomain.domain2.org/" ]; |
michael@0 | 528 | check_host_filters(uriStrFilterList, true, host_filters_1); |
michael@0 | 529 | } |
michael@0 | 530 | |
michael@0 | 531 | function host_filters_1() |
michael@0 | 532 | { |
michael@0 | 533 | // Check the hosts that should be proxied |
michael@0 | 534 | uriStrUseProxyList = [ "http://www.mozilla.com/", |
michael@0 | 535 | "http://mail.google.com/", |
michael@0 | 536 | "http://somehost.domain.co.uk/", |
michael@0 | 537 | "http://somelocalhost/" ]; |
michael@0 | 538 | check_host_filters(uriStrUseProxyList, false, host_filters_2); |
michael@0 | 539 | } |
michael@0 | 540 | |
michael@0 | 541 | function host_filters_2() |
michael@0 | 542 | { |
michael@0 | 543 | // Set no_proxies_on to include local hosts |
michael@0 | 544 | prefs.setCharPref("network.proxy.no_proxies_on", hostFilterList + ", <local>"); |
michael@0 | 545 | do_check_eq(prefs.getCharPref("network.proxy.no_proxies_on"), |
michael@0 | 546 | hostFilterList + ", <local>"); |
michael@0 | 547 | // Amend lists - move local domain to filtered list |
michael@0 | 548 | uriStrFilterList.push(uriStrUseProxyList.pop()); |
michael@0 | 549 | check_host_filters(uriStrFilterList, true, host_filters_3); |
michael@0 | 550 | } |
michael@0 | 551 | |
michael@0 | 552 | function host_filters_3() |
michael@0 | 553 | { |
michael@0 | 554 | check_host_filters(uriStrUseProxyList, false, host_filters_4); |
michael@0 | 555 | } |
michael@0 | 556 | |
michael@0 | 557 | function host_filters_4() |
michael@0 | 558 | { |
michael@0 | 559 | // Cleanup |
michael@0 | 560 | prefs.setCharPref("network.proxy.no_proxies_on", ""); |
michael@0 | 561 | do_check_eq(prefs.getCharPref("network.proxy.no_proxies_on"), ""); |
michael@0 | 562 | |
michael@0 | 563 | run_myipaddress_test(); |
michael@0 | 564 | } |
michael@0 | 565 | |
michael@0 | 566 | function run_myipaddress_test() |
michael@0 | 567 | { |
michael@0 | 568 | // This test makes sure myIpAddress() comes up with some valid |
michael@0 | 569 | // IP address other than localhost. The DUT must be configured with |
michael@0 | 570 | // an Internet route for this to work - though no Internet traffic |
michael@0 | 571 | // should be created. |
michael@0 | 572 | |
michael@0 | 573 | var pac = 'data:text/plain,' + |
michael@0 | 574 | 'function FindProxyForURL(url, host) {' + |
michael@0 | 575 | ' return "PROXY " + myIpAddress() + ":1234";' + |
michael@0 | 576 | '}'; |
michael@0 | 577 | |
michael@0 | 578 | // no traffic to this IP is ever sent, it is just a public IP that |
michael@0 | 579 | // does not require DNS to determine a route. |
michael@0 | 580 | var uri = ios.newURI("http://192.0.43.10/", null, null); |
michael@0 | 581 | |
michael@0 | 582 | prefs.setIntPref("network.proxy.type", 2); |
michael@0 | 583 | prefs.setCharPref("network.proxy.autoconfig_url", pac); |
michael@0 | 584 | |
michael@0 | 585 | var cb = new resolveCallback(); |
michael@0 | 586 | cb.nextFunction = myipaddress_callback; |
michael@0 | 587 | var req = pps.asyncResolve(uri, 0, cb); |
michael@0 | 588 | } |
michael@0 | 589 | |
michael@0 | 590 | function myipaddress_callback(pi) |
michael@0 | 591 | { |
michael@0 | 592 | do_check_neq(pi, null); |
michael@0 | 593 | do_check_eq(pi.type, "http"); |
michael@0 | 594 | do_check_eq(pi.port, 1234); |
michael@0 | 595 | |
michael@0 | 596 | // make sure we didn't return localhost |
michael@0 | 597 | do_check_neq(pi.host, null); |
michael@0 | 598 | do_check_neq(pi.host, "127.0.0.1"); |
michael@0 | 599 | do_check_neq(pi.host, "::1"); |
michael@0 | 600 | |
michael@0 | 601 | run_myipaddress_test_2(); |
michael@0 | 602 | } |
michael@0 | 603 | |
michael@0 | 604 | function run_myipaddress_test_2() |
michael@0 | 605 | { |
michael@0 | 606 | // test that myIPAddress() can be used outside of the scope of |
michael@0 | 607 | // FindProxyForURL(). bug 829646. |
michael@0 | 608 | |
michael@0 | 609 | var pac = 'data:text/plain,' + |
michael@0 | 610 | 'var myaddr = myIpAddress(); ' + |
michael@0 | 611 | 'function FindProxyForURL(url, host) {' + |
michael@0 | 612 | ' return "PROXY " + myaddr + ":5678";' + |
michael@0 | 613 | '}'; |
michael@0 | 614 | |
michael@0 | 615 | var uri = ios.newURI("http://www.mozilla.org/", null, null); |
michael@0 | 616 | prefs.setIntPref("network.proxy.type", 2); |
michael@0 | 617 | prefs.setCharPref("network.proxy.autoconfig_url", pac); |
michael@0 | 618 | |
michael@0 | 619 | var cb = new resolveCallback(); |
michael@0 | 620 | cb.nextFunction = myipaddress2_callback; |
michael@0 | 621 | var req = pps.asyncResolve(uri, 0, cb); |
michael@0 | 622 | } |
michael@0 | 623 | |
michael@0 | 624 | function myipaddress2_callback(pi) |
michael@0 | 625 | { |
michael@0 | 626 | do_check_neq(pi, null); |
michael@0 | 627 | do_check_eq(pi.type, "http"); |
michael@0 | 628 | do_check_eq(pi.port, 5678); |
michael@0 | 629 | |
michael@0 | 630 | // make sure we didn't return localhost |
michael@0 | 631 | do_check_neq(pi.host, null); |
michael@0 | 632 | do_check_neq(pi.host, "127.0.0.1"); |
michael@0 | 633 | do_check_neq(pi.host, "::1"); |
michael@0 | 634 | |
michael@0 | 635 | run_failed_script_test(); |
michael@0 | 636 | } |
michael@0 | 637 | |
michael@0 | 638 | function run_failed_script_test() |
michael@0 | 639 | { |
michael@0 | 640 | // test to make sure we go direct with invalid PAC |
michael@0 | 641 | var pac = 'data:text/plain,' + |
michael@0 | 642 | '\nfor(;\n'; |
michael@0 | 643 | |
michael@0 | 644 | var uri = ios.newURI("http://www.mozilla.org/", null, null); |
michael@0 | 645 | |
michael@0 | 646 | prefs.setIntPref("network.proxy.type", 2); |
michael@0 | 647 | prefs.setCharPref("network.proxy.autoconfig_url", pac); |
michael@0 | 648 | |
michael@0 | 649 | var cb = new resolveCallback(); |
michael@0 | 650 | cb.nextFunction = failed_script_callback; |
michael@0 | 651 | var req = pps.asyncResolve(uri, 0, cb); |
michael@0 | 652 | } |
michael@0 | 653 | |
michael@0 | 654 | var directFilter; |
michael@0 | 655 | |
michael@0 | 656 | function failed_script_callback(pi) |
michael@0 | 657 | { |
michael@0 | 658 | // we should go direct |
michael@0 | 659 | do_check_eq(pi, null); |
michael@0 | 660 | |
michael@0 | 661 | // test that we honor filters when configured to go direct |
michael@0 | 662 | prefs.setIntPref("network.proxy.type", 0); |
michael@0 | 663 | directFilter = new TestFilter("http", "127.0.0.1", 7246, 0, 0); |
michael@0 | 664 | pps.registerFilter(directFilter, 10); |
michael@0 | 665 | |
michael@0 | 666 | // test that on-modify-request contains the proxy info too |
michael@0 | 667 | var obs = Components.classes["@mozilla.org/observer-service;1"].getService(); |
michael@0 | 668 | obs = obs.QueryInterface(Components.interfaces.nsIObserverService); |
michael@0 | 669 | obs.addObserver(directFilterListener, "http-on-modify-request", false); |
michael@0 | 670 | |
michael@0 | 671 | var chan = ios.newChannel("http://127.0.0.1:7247", "", null); |
michael@0 | 672 | chan.asyncOpen(directFilterListener, chan); |
michael@0 | 673 | } |
michael@0 | 674 | |
michael@0 | 675 | var directFilterListener = { |
michael@0 | 676 | onModifyRequestCalled : false, |
michael@0 | 677 | |
michael@0 | 678 | onStartRequest: function test_onStart(request, ctx) { }, |
michael@0 | 679 | onDataAvailable: function test_OnData() { }, |
michael@0 | 680 | |
michael@0 | 681 | onStopRequest: function test_onStop(request, ctx, status) { |
michael@0 | 682 | // check on the PI from the channel itself |
michael@0 | 683 | ctx.QueryInterface(Components.interfaces.nsIProxiedChannel); |
michael@0 | 684 | check_proxy(ctx.proxyInfo, "http", "127.0.0.1", 7246, 0, 0, false); |
michael@0 | 685 | pps.unregisterFilter(directFilter); |
michael@0 | 686 | |
michael@0 | 687 | // check on the PI from on-modify-request |
michael@0 | 688 | do_check_true(this.onModifyRequestCalled); |
michael@0 | 689 | var obs = Components.classes["@mozilla.org/observer-service;1"].getService(); |
michael@0 | 690 | obs = obs.QueryInterface(Components.interfaces.nsIObserverService); |
michael@0 | 691 | obs.removeObserver(this, "http-on-modify-request"); |
michael@0 | 692 | |
michael@0 | 693 | run_isresolvable_test(); |
michael@0 | 694 | }, |
michael@0 | 695 | |
michael@0 | 696 | observe: function(subject, topic, data) { |
michael@0 | 697 | if (topic === "http-on-modify-request" && |
michael@0 | 698 | subject instanceof Components.interfaces.nsIHttpChannel && |
michael@0 | 699 | subject instanceof Components.interfaces.nsIProxiedChannel) { |
michael@0 | 700 | check_proxy(subject.proxyInfo, "http", "127.0.0.1", 7246, 0, 0, false); |
michael@0 | 701 | this.onModifyRequestCalled = true; |
michael@0 | 702 | } |
michael@0 | 703 | } |
michael@0 | 704 | }; |
michael@0 | 705 | |
michael@0 | 706 | function run_isresolvable_test() |
michael@0 | 707 | { |
michael@0 | 708 | // test a non resolvable host in the pac file |
michael@0 | 709 | |
michael@0 | 710 | var pac = 'data:text/plain,' + |
michael@0 | 711 | 'function FindProxyForURL(url, host) {' + |
michael@0 | 712 | ' if (isResolvable("nonexistant.lan"))' + |
michael@0 | 713 | ' return "DIRECT";' + |
michael@0 | 714 | ' return "PROXY 127.0.0.1:1234";' + |
michael@0 | 715 | '}'; |
michael@0 | 716 | |
michael@0 | 717 | var uri = ios.newURI("http://www.mozilla.org/", null, null); |
michael@0 | 718 | |
michael@0 | 719 | prefs.setIntPref("network.proxy.type", 2); |
michael@0 | 720 | prefs.setCharPref("network.proxy.autoconfig_url", pac); |
michael@0 | 721 | |
michael@0 | 722 | var cb = new resolveCallback(); |
michael@0 | 723 | cb.nextFunction = isresolvable_callback; |
michael@0 | 724 | var req = pps.asyncResolve(uri, 0, cb); |
michael@0 | 725 | } |
michael@0 | 726 | |
michael@0 | 727 | function isresolvable_callback(pi) |
michael@0 | 728 | { |
michael@0 | 729 | do_check_neq(pi, null); |
michael@0 | 730 | do_check_eq(pi.type, "http"); |
michael@0 | 731 | do_check_eq(pi.port, 1234); |
michael@0 | 732 | do_check_eq(pi.host, "127.0.0.1"); |
michael@0 | 733 | |
michael@0 | 734 | prefs.setIntPref("network.proxy.type", 0); |
michael@0 | 735 | do_test_finished(); |
michael@0 | 736 | } |
michael@0 | 737 | |
michael@0 | 738 | function run_deprecated_sync_test() |
michael@0 | 739 | { |
michael@0 | 740 | var uri = ios.newURI("http://www.mozilla.org/", null, null); |
michael@0 | 741 | |
michael@0 | 742 | pps.QueryInterface(Components.interfaces.nsIProtocolProxyService2); |
michael@0 | 743 | |
michael@0 | 744 | // Verify initial state |
michael@0 | 745 | var pi = pps.deprecatedBlockingResolve(uri, 0); |
michael@0 | 746 | do_check_eq(pi, null); |
michael@0 | 747 | |
michael@0 | 748 | // Push a filter and verify the results |
michael@0 | 749 | var filter1 = new BasicFilter(); |
michael@0 | 750 | var filter2 = new BasicFilter(); |
michael@0 | 751 | pps.registerFilter(filter1, 10); |
michael@0 | 752 | pps.registerFilter(filter2, 20); |
michael@0 | 753 | |
michael@0 | 754 | pi = pps.deprecatedBlockingResolve(uri, 0); |
michael@0 | 755 | check_proxy(pi, "http", "localhost", 8080, 0, 10, true); |
michael@0 | 756 | check_proxy(pi.failoverProxy, "direct", "", -1, 0, 0, false); |
michael@0 | 757 | |
michael@0 | 758 | pps.unregisterFilter(filter2); |
michael@0 | 759 | pi = pps.deprecatedBlockingResolve(uri, 0); |
michael@0 | 760 | check_proxy(pi, "http", "localhost", 8080, 0, 10, true); |
michael@0 | 761 | check_proxy(pi.failoverProxy, "direct", "", -1, 0, 0, false); |
michael@0 | 762 | |
michael@0 | 763 | // Remove filter and verify that we return to the initial state |
michael@0 | 764 | pps.unregisterFilter(filter1); |
michael@0 | 765 | pi = pps.deprecatedBlockingResolve(uri, 0); |
michael@0 | 766 | do_check_eq(pi, null); |
michael@0 | 767 | } |
michael@0 | 768 | |
michael@0 | 769 | function run_test() { |
michael@0 | 770 | register_test_protocol_handler(); |
michael@0 | 771 | |
michael@0 | 772 | // any synchronous tests |
michael@0 | 773 | run_deprecated_sync_test(); |
michael@0 | 774 | |
michael@0 | 775 | // start of asynchronous test chain |
michael@0 | 776 | run_filter_test(); |
michael@0 | 777 | do_test_pending(); |
michael@0 | 778 | } |