netwerk/test/unit/test_protocolproxyservice.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/netwerk/test/unit/test_protocolproxyservice.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,778 @@
     1.4 +/* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* vim:set ts=2 sw=2 sts=2 et: */
     1.6 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.9 +
    1.10 +// This testcase exercises the Protocol Proxy Service
    1.11 +
    1.12 +// These are the major sub tests:
    1.13 +// run_filter_test();
    1.14 +// run_filter_test2()
    1.15 +// run_filter_test3()
    1.16 +// run_pref_test();
    1.17 +// run_pac_test();
    1.18 +// run_pac_cancel_test();
    1.19 +// run_proxy_host_filters_test();
    1.20 +// run_myipaddress_test();
    1.21 +// run_failed_script_test();
    1.22 +// run_isresolvable_test();
    1.23 +
    1.24 +var ios = Components.classes["@mozilla.org/network/io-service;1"]
    1.25 +                    .getService(Components.interfaces.nsIIOService);
    1.26 +var pps = Components.classes["@mozilla.org/network/protocol-proxy-service;1"]
    1.27 +                    .getService();
    1.28 +var prefs = Components.classes["@mozilla.org/preferences-service;1"]
    1.29 +                     .getService(Components.interfaces.nsIPrefBranch);
    1.30 +
    1.31 +/**
    1.32 + * Test nsIProtocolHandler that allows proxying, but doesn't allow HTTP
    1.33 + * proxying.
    1.34 + */
    1.35 +function TestProtocolHandler() {
    1.36 +}
    1.37 +TestProtocolHandler.prototype = {
    1.38 +  QueryInterface: function(iid) {
    1.39 +    if (iid.equals(Components.interfaces.nsIProtocolHandler) ||
    1.40 +        iid.equals(Components.interfaces.nsISupports))
    1.41 +      return this;
    1.42 +    throw Components.results.NS_ERROR_NO_INTERFACE;
    1.43 +  },
    1.44 +  scheme: "moz-test",
    1.45 +  defaultPort: -1,
    1.46 +  protocolFlags: Components.interfaces.nsIProtocolHandler.URI_NOAUTH |
    1.47 +                 Components.interfaces.nsIProtocolHandler.URI_NORELATIVE |
    1.48 +                 Components.interfaces.nsIProtocolHandler.ALLOWS_PROXY |
    1.49 +                 Components.interfaces.nsIProtocolHandler.URI_DANGEROUS_TO_LOAD,
    1.50 +  newURI: function(spec, originCharset, baseURI) {
    1.51 +    var uri = Components.classes["@mozilla.org/network/simple-uri;1"]
    1.52 +                        .createInstance(Components.interfaces.nsIURI);
    1.53 +    uri.spec = spec;
    1.54 +    return uri;
    1.55 +  },
    1.56 +  newChannel: function(uri) {
    1.57 +    throw Components.results.NS_ERROR_NOT_IMPLEMENTED;
    1.58 +  },
    1.59 +  allowPort: function(port, scheme) {
    1.60 +    return true;
    1.61 +  }
    1.62 +};
    1.63 +
    1.64 +function TestProtocolHandlerFactory() {
    1.65 +}
    1.66 +TestProtocolHandlerFactory.prototype = {
    1.67 +  createInstance: function(delegate, iid) {
    1.68 +    return new TestProtocolHandler().QueryInterface(iid);
    1.69 +  },
    1.70 +  lockFactory: function(lock) {
    1.71 +  }
    1.72 +};
    1.73 +
    1.74 +function register_test_protocol_handler() {
    1.75 +  var reg = Components.manager.QueryInterface(
    1.76 +      Components.interfaces.nsIComponentRegistrar);
    1.77 +  reg.registerFactory(Components.ID("{4ea7dd3a-8cae-499c-9f18-e1de773ca25b}"),
    1.78 +                      "TestProtocolHandler",
    1.79 +                      "@mozilla.org/network/protocol;1?name=moz-test",
    1.80 +                      new TestProtocolHandlerFactory());
    1.81 +}
    1.82 +
    1.83 +function check_proxy(pi, type, host, port, flags, timeout, hasNext) {
    1.84 +  do_check_neq(pi, null);
    1.85 +  do_check_eq(pi.type, type);
    1.86 +  do_check_eq(pi.host, host);
    1.87 +  do_check_eq(pi.port, port);
    1.88 +  if (flags != -1)
    1.89 +    do_check_eq(pi.flags, flags);
    1.90 +  if (timeout != -1)
    1.91 +    do_check_eq(pi.failoverTimeout, timeout);
    1.92 +  if (hasNext)
    1.93 +    do_check_neq(pi.failoverProxy, null);
    1.94 +  else
    1.95 +    do_check_eq(pi.failoverProxy, null);
    1.96 +}
    1.97 +
    1.98 +function TestFilter(type, host, port, flags, timeout) {
    1.99 +  this._type = type;
   1.100 +  this._host = host;
   1.101 +  this._port = port;
   1.102 +  this._flags = flags;
   1.103 +  this._timeout = timeout;
   1.104 +}
   1.105 +TestFilter.prototype = {
   1.106 +  _type: "",
   1.107 +  _host: "",
   1.108 +  _port: -1,
   1.109 +  _flags: 0,
   1.110 +  _timeout: 0,
   1.111 +  QueryInterface: function(iid) {
   1.112 +    if (iid.equals(Components.interfaces.nsIProtocolProxyFilter) ||
   1.113 +        iid.equals(Components.interfaces.nsISupports))
   1.114 +      return this;
   1.115 +    throw Components.results.NS_ERROR_NO_INTERFACE;
   1.116 +  },
   1.117 +  applyFilter: function(pps, uri, pi) {
   1.118 +    var pi_tail = pps.newProxyInfo(this._type, this._host, this._port,
   1.119 +                                   this._flags, this._timeout, null);
   1.120 +    if (pi)
   1.121 +      pi.failoverProxy = pi_tail;
   1.122 +    else
   1.123 +      pi = pi_tail;
   1.124 +    return pi;
   1.125 +  }
   1.126 +};
   1.127 +
   1.128 +function BasicFilter() {}
   1.129 +BasicFilter.prototype = {
   1.130 +  QueryInterface: function(iid) {
   1.131 +    if (iid.equals(Components.interfaces.nsIProtocolProxyFilter) ||
   1.132 +        iid.equals(Components.interfaces.nsISupports))
   1.133 +      return this;
   1.134 +    throw Components.results.NS_ERROR_NO_INTERFACE;
   1.135 +  },
   1.136 +  applyFilter: function(pps, uri, pi) {
   1.137 +    return pps.newProxyInfo("http", "localhost", 8080, 0, 10,
   1.138 +           pps.newProxyInfo("direct", "", -1, 0, 0, null));
   1.139 +  }
   1.140 +};
   1.141 +
   1.142 +function resolveCallback() { }
   1.143 +resolveCallback.prototype = {
   1.144 +  nextFunction: null,
   1.145 +
   1.146 +  QueryInterface : function (iid) {
   1.147 +    const interfaces = [Components.interfaces.nsIProtocolProxyCallback,
   1.148 +                        Components.interfaces.nsISupports];
   1.149 +    if (!interfaces.some( function(v) { return iid.equals(v) } ))
   1.150 +      throw Components.results.NS_ERROR_NO_INTERFACE;
   1.151 +    return this;
   1.152 +  },
   1.153 +
   1.154 +  onProxyAvailable : function (req, uri, pi, status) {
   1.155 +    this.nextFunction(pi);
   1.156 +  }
   1.157 +};
   1.158 +
   1.159 +function run_filter_test() {
   1.160 +  var uri = ios.newURI("http://www.mozilla.org/", null, null);
   1.161 +
   1.162 +  // Verify initial state
   1.163 +  var cb = new resolveCallback();
   1.164 +  cb.nextFunction = filter_test0_1;
   1.165 +  var req = pps.asyncResolve(uri, 0, cb);
   1.166 +}
   1.167 +
   1.168 +var filter01;
   1.169 +var filter02;
   1.170 +
   1.171 +function filter_test0_1(pi) {
   1.172 +  do_check_eq(pi, null);
   1.173 +
   1.174 +  // Push a filter and verify the results
   1.175 +
   1.176 +  filter01 = new BasicFilter();
   1.177 +  filter02 = new BasicFilter();
   1.178 +  pps.registerFilter(filter01, 10);
   1.179 +  pps.registerFilter(filter02, 20);
   1.180 +
   1.181 +  var cb = new resolveCallback();
   1.182 +  cb.nextFunction = filter_test0_2;
   1.183 +  var uri = ios.newURI("http://www.mozilla.org/", null, null);
   1.184 +  var req = pps.asyncResolve(uri, 0, cb);
   1.185 +}
   1.186 +
   1.187 +function filter_test0_2(pi)
   1.188 +{
   1.189 +  check_proxy(pi, "http", "localhost", 8080, 0, 10, true);
   1.190 +  check_proxy(pi.failoverProxy, "direct", "", -1, 0, 0, false);
   1.191 +
   1.192 +  pps.unregisterFilter(filter02);
   1.193 +
   1.194 +  var cb = new resolveCallback();
   1.195 +  cb.nextFunction = filter_test0_3;
   1.196 +  var uri = ios.newURI("http://www.mozilla.org/", null, null);
   1.197 +  var req = pps.asyncResolve(uri, 0, cb);
   1.198 +}
   1.199 +
   1.200 +function filter_test0_3(pi)
   1.201 +{
   1.202 +  check_proxy(pi, "http", "localhost", 8080, 0, 10, true);
   1.203 +  check_proxy(pi.failoverProxy, "direct", "", -1, 0, 0, false);
   1.204 +
   1.205 +  // Remove filter and verify that we return to the initial state
   1.206 +
   1.207 +  pps.unregisterFilter(filter01);
   1.208 +
   1.209 +  var cb = new resolveCallback();
   1.210 +  cb.nextFunction = filter_test0_4;
   1.211 +  var uri = ios.newURI("http://www.mozilla.org/", null, null);
   1.212 +  var req = pps.asyncResolve(uri, 0, cb);
   1.213 +}
   1.214 +
   1.215 +function filter_test0_4(pi)
   1.216 +{
   1.217 +  do_check_eq(pi, null);
   1.218 +  run_filter_test2();
   1.219 +}
   1.220 +
   1.221 +var filter11;
   1.222 +var filter12;
   1.223 +
   1.224 +function run_filter_test2() {
   1.225 +  // Push a filter and verify the results
   1.226 +
   1.227 +  filter11 = new TestFilter("http", "foo", 8080, 0, 10);
   1.228 +  filter12 = new TestFilter("http", "bar", 8090, 0, 10);
   1.229 +  pps.registerFilter(filter11, 20);
   1.230 +  pps.registerFilter(filter12, 10);
   1.231 +
   1.232 +  var cb = new resolveCallback();
   1.233 +  cb.nextFunction = filter_test1_1;
   1.234 +  var uri = ios.newURI("http://www.mozilla.org/", null, null);
   1.235 +  var req = pps.asyncResolve(uri, 0, cb);
   1.236 +}
   1.237 +
   1.238 +function filter_test1_1(pi) {
   1.239 +  check_proxy(pi, "http", "bar", 8090, 0, 10, true);
   1.240 +  check_proxy(pi.failoverProxy, "http", "foo", 8080, 0, 10, false);
   1.241 +
   1.242 +  pps.unregisterFilter(filter12);
   1.243 +
   1.244 +  var cb = new resolveCallback();
   1.245 +  cb.nextFunction = filter_test1_2;
   1.246 +  var uri = ios.newURI("http://www.mozilla.org/", null, null);
   1.247 +  var req = pps.asyncResolve(uri, 0, cb);
   1.248 +}
   1.249 +
   1.250 +function filter_test1_2(pi) {
   1.251 +  check_proxy(pi, "http", "foo", 8080, 0, 10, false);
   1.252 +
   1.253 +  // Remove filter and verify that we return to the initial state
   1.254 +
   1.255 +  pps.unregisterFilter(filter11);
   1.256 +
   1.257 +  var cb = new resolveCallback();
   1.258 +  cb.nextFunction = filter_test1_3;
   1.259 +  var uri = ios.newURI("http://www.mozilla.org/", null, null);
   1.260 +  var req = pps.asyncResolve(uri, 0, cb);
   1.261 +}
   1.262 +
   1.263 +function filter_test1_3(pi) {
   1.264 +  do_check_eq(pi, null);
   1.265 +  run_filter_test3();
   1.266 +}
   1.267 +
   1.268 +var filter_3_1;
   1.269 +
   1.270 +function run_filter_test3() {
   1.271 +  var uri = ios.newURI("http://www.mozilla.org/", null, null);
   1.272 +
   1.273 +  // Push a filter and verify the results asynchronously
   1.274 +
   1.275 +  filter_3_1 = new TestFilter("http", "foo", 8080, 0, 10);
   1.276 +  pps.registerFilter(filter_3_1, 20);
   1.277 +
   1.278 +  var cb = new resolveCallback();
   1.279 +  cb.nextFunction = filter_test3_1;
   1.280 +  var req = pps.asyncResolve(uri, 0, cb);
   1.281 +}
   1.282 +
   1.283 +function filter_test3_1(pi) {
   1.284 +  check_proxy(pi, "http", "foo", 8080, 0, 10, false);
   1.285 +  pps.unregisterFilter(filter_3_1);
   1.286 +  run_pref_test();
   1.287 +}
   1.288 +
   1.289 +function run_pref_test() {
   1.290 +  var uri = ios.newURI("http://www.mozilla.org/", null, null);
   1.291 +
   1.292 +  // Verify 'direct' setting
   1.293 +
   1.294 +  prefs.setIntPref("network.proxy.type", 0);
   1.295 +
   1.296 +  var cb = new resolveCallback();
   1.297 +  cb.nextFunction = pref_test1_1;
   1.298 +  var req = pps.asyncResolve(uri, 0, cb);
   1.299 +}
   1.300 +
   1.301 +function pref_test1_1(pi)
   1.302 +{
   1.303 +  do_check_eq(pi, null);
   1.304 +
   1.305 +  // Verify 'manual' setting
   1.306 +  prefs.setIntPref("network.proxy.type", 1);
   1.307 +
   1.308 +  var cb = new resolveCallback();
   1.309 +  cb.nextFunction = pref_test1_2;
   1.310 +  var uri = ios.newURI("http://www.mozilla.org/", null, null);
   1.311 +  var req = pps.asyncResolve(uri, 0, cb);
   1.312 +}
   1.313 +
   1.314 +function pref_test1_2(pi)
   1.315 +{
   1.316 +  // nothing yet configured
   1.317 +  do_check_eq(pi, null);
   1.318 +
   1.319 +  // try HTTP configuration
   1.320 +  prefs.setCharPref("network.proxy.http", "foopy");
   1.321 +  prefs.setIntPref("network.proxy.http_port", 8080);
   1.322 +
   1.323 +  var cb = new resolveCallback();
   1.324 +  cb.nextFunction = pref_test1_3;
   1.325 +  var uri = ios.newURI("http://www.mozilla.org/", null, null);
   1.326 +  var req = pps.asyncResolve(uri, 0, cb);
   1.327 +}
   1.328 +
   1.329 +function pref_test1_3(pi)
   1.330 +{
   1.331 +  check_proxy(pi, "http", "foopy", 8080, 0, -1, false);
   1.332 +
   1.333 +  prefs.setCharPref("network.proxy.http", "");
   1.334 +  prefs.setIntPref("network.proxy.http_port", 0);
   1.335 +
   1.336 +  // try SOCKS configuration
   1.337 +  prefs.setCharPref("network.proxy.socks", "barbar");
   1.338 +  prefs.setIntPref("network.proxy.socks_port", 1203);
   1.339 +
   1.340 +  var cb = new resolveCallback();
   1.341 +  cb.nextFunction = pref_test1_4;
   1.342 +  var uri = ios.newURI("http://www.mozilla.org/", null, null);
   1.343 +  var req = pps.asyncResolve(uri, 0, cb);
   1.344 +}
   1.345 +
   1.346 +function pref_test1_4(pi)
   1.347 +{
   1.348 +  check_proxy(pi, "socks", "barbar", 1203, 0, -1, false);
   1.349 +  run_pac_test();
   1.350 +}
   1.351 +
   1.352 +function run_protocol_handler_test() {
   1.353 +  var uri = ios.newURI("moz-test:foopy", null, null);
   1.354 +
   1.355 +  var cb = new resolveCallback();
   1.356 +  cb.nextFunction = protocol_handler_test_1;
   1.357 +  var req = pps.asyncResolve(uri, 0, cb);
   1.358 +}
   1.359 +
   1.360 +function protocol_handler_test_1(pi)
   1.361 +{
   1.362 +  do_check_eq(pi, null);
   1.363 +  prefs.setCharPref("network.proxy.autoconfig_url", "");
   1.364 +  prefs.setIntPref("network.proxy.type", 0);
   1.365 +
   1.366 +  run_pac_cancel_test();
   1.367 +}
   1.368 +
   1.369 +function TestResolveCallback() {
   1.370 +}
   1.371 +TestResolveCallback.prototype = {
   1.372 +  QueryInterface:
   1.373 +  function TestResolveCallback_QueryInterface(iid) {
   1.374 +    if (iid.equals(Components.interfaces.nsIProtocolProxyCallback) ||
   1.375 +        iid.equals(Components.interfaces.nsISupports))
   1.376 +      return this;
   1.377 +    throw Components.results.NS_ERROR_NO_INTERFACE;
   1.378 +  },
   1.379 +
   1.380 +  onProxyAvailable:
   1.381 +  function TestResolveCallback_onProxyAvailable(req, uri, pi, status) {
   1.382 +    dump("*** uri=" + uri.spec + ", status=" + status + "\n");
   1.383 +
   1.384 +    do_check_neq(req, null);
   1.385 +    do_check_neq(uri, null);
   1.386 +    do_check_eq(status, 0);
   1.387 +    do_check_neq(pi, null);
   1.388 +
   1.389 +    check_proxy(pi, "http", "foopy", 8080, 0, -1, true);
   1.390 +    check_proxy(pi.failoverProxy, "direct", "", -1, -1, -1, false);
   1.391 +
   1.392 +    run_protocol_handler_test();
   1.393 +  }
   1.394 +};
   1.395 +
   1.396 +function run_pac_test() {
   1.397 +  var pac = 'data:text/plain,' +
   1.398 +            'function FindProxyForURL(url, host) {' +
   1.399 +            '  return "PROXY foopy:8080; DIRECT";' +
   1.400 +            '}';
   1.401 +  var uri = ios.newURI("http://www.mozilla.org/", null, null);
   1.402 +
   1.403 +  // Configure PAC
   1.404 +
   1.405 +  prefs.setIntPref("network.proxy.type", 2);
   1.406 +  prefs.setCharPref("network.proxy.autoconfig_url", pac);
   1.407 +
   1.408 +  var req = pps.asyncResolve(uri, 0, new TestResolveCallback());
   1.409 +}
   1.410 +
   1.411 +function TestResolveCancelationCallback() {
   1.412 +}
   1.413 +TestResolveCancelationCallback.prototype = {
   1.414 +  QueryInterface:
   1.415 +  function TestResolveCallback_QueryInterface(iid) {
   1.416 +    if (iid.equals(Components.interfaces.nsIProtocolProxyCallback) ||
   1.417 +        iid.equals(Components.interfaces.nsISupports))
   1.418 +      return this;
   1.419 +    throw Components.results.NS_ERROR_NO_INTERFACE;
   1.420 +  },
   1.421 +
   1.422 +  onProxyAvailable:
   1.423 +  function TestResolveCancelationCallback_onProxyAvailable(req, uri, pi, status) {
   1.424 +    dump("*** uri=" + uri.spec + ", status=" + status + "\n");
   1.425 +
   1.426 +    do_check_neq(req, null);
   1.427 +    do_check_neq(uri, null);
   1.428 +    do_check_eq(status, Components.results.NS_ERROR_ABORT);
   1.429 +    do_check_eq(pi, null);
   1.430 +
   1.431 +    prefs.setCharPref("network.proxy.autoconfig_url", "");
   1.432 +    prefs.setIntPref("network.proxy.type", 0);
   1.433 +
   1.434 +    run_proxy_host_filters_test();
   1.435 +  }
   1.436 +};
   1.437 +
   1.438 +function run_pac_cancel_test() {
   1.439 +  var uri = ios.newURI("http://www.mozilla.org/", null, null);
   1.440 +
   1.441 +  // Configure PAC
   1.442 +  var pac = 'data:text/plain,' +
   1.443 +            'function FindProxyForURL(url, host) {' +
   1.444 +            '  return "PROXY foopy:8080; DIRECT";' +
   1.445 +            '}';
   1.446 +  prefs.setIntPref("network.proxy.type", 2);
   1.447 +  prefs.setCharPref("network.proxy.autoconfig_url", pac);
   1.448 +
   1.449 +  var req = pps.asyncResolve(uri, 0, new TestResolveCancelationCallback());
   1.450 +  req.cancel(Components.results.NS_ERROR_ABORT);
   1.451 +}
   1.452 +
   1.453 +var hostList;
   1.454 +var hostIDX;
   1.455 +var bShouldBeFiltered;
   1.456 +var hostNextFX;
   1.457 +
   1.458 +function check_host_filters(hl, shouldBe, nextFX) {
   1.459 +  hostList = hl;
   1.460 +  hostIDX = 0;
   1.461 +  bShouldBeFiltered = shouldBe;
   1.462 +  hostNextFX = nextFX;
   1.463 +
   1.464 +  if (hostList.length > hostIDX)
   1.465 +    check_host_filter(hostIDX);
   1.466 +}
   1.467 +
   1.468 +function check_host_filters_cb()
   1.469 +{
   1.470 +  hostIDX++;
   1.471 +  if (hostList.length > hostIDX)
   1.472 +    check_host_filter(hostIDX);
   1.473 +  else
   1.474 +    hostNextFX();
   1.475 +}
   1.476 +
   1.477 +function check_host_filter(i) {
   1.478 +  var uri;
   1.479 +  dump("*** uri=" + hostList[i] + " bShouldBeFiltered=" + bShouldBeFiltered + "\n");
   1.480 +  uri = ios.newURI(hostList[i], null, null);
   1.481 +
   1.482 +  var cb = new resolveCallback();
   1.483 +  cb.nextFunction = host_filter_cb;
   1.484 +  var req = pps.asyncResolve(uri, 0, cb);
   1.485 +}
   1.486 +
   1.487 +function host_filter_cb(proxy)
   1.488 +{
   1.489 +  if (bShouldBeFiltered) {
   1.490 +    do_check_eq(proxy, null);
   1.491 +  } else {
   1.492 +    do_check_neq(proxy, null);
   1.493 +    // Just to be sure, let's check that the proxy is correct
   1.494 +    // - this should match the proxy setup in the calling function
   1.495 +    check_proxy(proxy, "http", "foopy", 8080, 0, -1, false);
   1.496 +  }
   1.497 +  check_host_filters_cb();
   1.498 +}
   1.499 +
   1.500 +
   1.501 +// Verify that hists in the host filter list are not proxied
   1.502 +// refers to "network.proxy.no_proxies_on"
   1.503 +
   1.504 +var uriStrUseProxyList;
   1.505 +var uriStrUseProxyList;
   1.506 +var hostFilterList;
   1.507 +
   1.508 +function run_proxy_host_filters_test() {
   1.509 +  // Get prefs object from DOM
   1.510 +  // Setup a basic HTTP proxy configuration
   1.511 +  // - pps.resolve() needs this to return proxy info for non-filtered hosts
   1.512 +  prefs.setIntPref("network.proxy.type", 1);
   1.513 +  prefs.setCharPref("network.proxy.http", "foopy");
   1.514 +  prefs.setIntPref("network.proxy.http_port", 8080);
   1.515 +
   1.516 +  // Setup host filter list string for "no_proxies_on"
   1.517 +  hostFilterList = "www.mozilla.org, www.google.com, www.apple.com, "
   1.518 +                       + ".domain, .domain2.org"
   1.519 +  prefs.setCharPref("network.proxy.no_proxies_on", hostFilterList);
   1.520 +  do_check_eq(prefs.getCharPref("network.proxy.no_proxies_on"), hostFilterList);
   1.521 +
   1.522 +  var rv;
   1.523 +  // Check the hosts that should be filtered out
   1.524 +  uriStrFilterList = [ "http://www.mozilla.org/",
   1.525 +                           "http://www.google.com/",
   1.526 +                           "http://www.apple.com/",
   1.527 +                           "http://somehost.domain/",
   1.528 +                           "http://someotherhost.domain/",
   1.529 +                           "http://somehost.domain2.org/",
   1.530 +                           "http://somehost.subdomain.domain2.org/" ];
   1.531 +  check_host_filters(uriStrFilterList, true, host_filters_1);
   1.532 +}
   1.533 +
   1.534 +function host_filters_1()
   1.535 +{
   1.536 +  // Check the hosts that should be proxied
   1.537 +  uriStrUseProxyList = [ "http://www.mozilla.com/",
   1.538 +                             "http://mail.google.com/",
   1.539 +                             "http://somehost.domain.co.uk/",
   1.540 +                             "http://somelocalhost/" ];  
   1.541 +  check_host_filters(uriStrUseProxyList, false, host_filters_2);
   1.542 +}
   1.543 +
   1.544 +function host_filters_2()
   1.545 +{
   1.546 +  // Set no_proxies_on to include local hosts
   1.547 +  prefs.setCharPref("network.proxy.no_proxies_on", hostFilterList + ", <local>");
   1.548 +  do_check_eq(prefs.getCharPref("network.proxy.no_proxies_on"),
   1.549 +              hostFilterList + ", <local>");
   1.550 +  // Amend lists - move local domain to filtered list
   1.551 +  uriStrFilterList.push(uriStrUseProxyList.pop());
   1.552 +  check_host_filters(uriStrFilterList, true, host_filters_3);
   1.553 +}
   1.554 +
   1.555 +function host_filters_3()
   1.556 +{
   1.557 +  check_host_filters(uriStrUseProxyList, false, host_filters_4);
   1.558 +}
   1.559 +
   1.560 +function host_filters_4()
   1.561 +{
   1.562 +  // Cleanup
   1.563 +  prefs.setCharPref("network.proxy.no_proxies_on", "");
   1.564 +  do_check_eq(prefs.getCharPref("network.proxy.no_proxies_on"), "");  
   1.565 +
   1.566 +  run_myipaddress_test();
   1.567 +}
   1.568 +
   1.569 +function run_myipaddress_test()
   1.570 +{
   1.571 +  // This test makes sure myIpAddress() comes up with some valid
   1.572 +  // IP address other than localhost. The DUT must be configured with
   1.573 +  // an Internet route for this to work - though no Internet traffic
   1.574 +  // should be created.
   1.575 +
   1.576 +  var pac = 'data:text/plain,' +
   1.577 +            'function FindProxyForURL(url, host) {' +
   1.578 +            ' return "PROXY " + myIpAddress() + ":1234";' +
   1.579 +            '}';
   1.580 +
   1.581 +  // no traffic to this IP is ever sent, it is just a public IP that
   1.582 +  // does not require DNS to determine a route.
   1.583 +  var uri = ios.newURI("http://192.0.43.10/", null, null);
   1.584 +
   1.585 +  prefs.setIntPref("network.proxy.type", 2);
   1.586 +  prefs.setCharPref("network.proxy.autoconfig_url", pac);
   1.587 +
   1.588 +  var cb = new resolveCallback();
   1.589 +  cb.nextFunction = myipaddress_callback;
   1.590 +  var req = pps.asyncResolve(uri, 0, cb);
   1.591 +}
   1.592 +
   1.593 +function myipaddress_callback(pi)
   1.594 +{
   1.595 +  do_check_neq(pi, null);
   1.596 +  do_check_eq(pi.type, "http");
   1.597 +  do_check_eq(pi.port, 1234);
   1.598 +
   1.599 +  // make sure we didn't return localhost
   1.600 +  do_check_neq(pi.host, null);
   1.601 +  do_check_neq(pi.host, "127.0.0.1");
   1.602 +  do_check_neq(pi.host, "::1");
   1.603 +  
   1.604 +  run_myipaddress_test_2();
   1.605 +}
   1.606 +
   1.607 +function run_myipaddress_test_2()
   1.608 +{
   1.609 +  // test that myIPAddress() can be used outside of the scope of
   1.610 +  // FindProxyForURL(). bug 829646.
   1.611 +
   1.612 +  var pac = 'data:text/plain,' +
   1.613 +            'var myaddr = myIpAddress(); ' +
   1.614 +            'function FindProxyForURL(url, host) {' +
   1.615 +            ' return "PROXY " + myaddr + ":5678";' +
   1.616 +            '}';
   1.617 +
   1.618 +  var uri = ios.newURI("http://www.mozilla.org/", null, null);
   1.619 +  prefs.setIntPref("network.proxy.type", 2);
   1.620 +  prefs.setCharPref("network.proxy.autoconfig_url", pac);
   1.621 +
   1.622 +  var cb = new resolveCallback();
   1.623 +  cb.nextFunction = myipaddress2_callback;
   1.624 +  var req = pps.asyncResolve(uri, 0, cb);
   1.625 +}
   1.626 +
   1.627 +function myipaddress2_callback(pi)
   1.628 +{
   1.629 +  do_check_neq(pi, null);
   1.630 +  do_check_eq(pi.type, "http");
   1.631 +  do_check_eq(pi.port, 5678);
   1.632 +
   1.633 +  // make sure we didn't return localhost
   1.634 +  do_check_neq(pi.host, null);
   1.635 +  do_check_neq(pi.host, "127.0.0.1");
   1.636 +  do_check_neq(pi.host, "::1");
   1.637 +  
   1.638 +  run_failed_script_test();
   1.639 +}
   1.640 +
   1.641 +function run_failed_script_test()
   1.642 +{
   1.643 +  // test to make sure we go direct with invalid PAC
   1.644 +  var pac = 'data:text/plain,' +
   1.645 +            '\nfor(;\n';
   1.646 +
   1.647 +  var uri = ios.newURI("http://www.mozilla.org/", null, null);
   1.648 +
   1.649 +  prefs.setIntPref("network.proxy.type", 2);
   1.650 +  prefs.setCharPref("network.proxy.autoconfig_url", pac);
   1.651 +
   1.652 +  var cb = new resolveCallback();
   1.653 +  cb.nextFunction = failed_script_callback;
   1.654 +  var req = pps.asyncResolve(uri, 0, cb);
   1.655 +}
   1.656 +
   1.657 +var directFilter;
   1.658 +
   1.659 +function failed_script_callback(pi)
   1.660 +{
   1.661 +  // we should go direct
   1.662 +  do_check_eq(pi, null);
   1.663 +
   1.664 +  // test that we honor filters when configured to go direct
   1.665 +  prefs.setIntPref("network.proxy.type", 0);
   1.666 +  directFilter = new TestFilter("http", "127.0.0.1", 7246, 0, 0);
   1.667 +  pps.registerFilter(directFilter, 10);
   1.668 +
   1.669 +  // test that on-modify-request contains the proxy info too
   1.670 +  var obs = Components.classes["@mozilla.org/observer-service;1"].getService();
   1.671 +  obs = obs.QueryInterface(Components.interfaces.nsIObserverService);
   1.672 +  obs.addObserver(directFilterListener, "http-on-modify-request", false);
   1.673 +
   1.674 +  var chan = ios.newChannel("http://127.0.0.1:7247", "", null);
   1.675 +  chan.asyncOpen(directFilterListener, chan);
   1.676 +}
   1.677 +
   1.678 +var directFilterListener = {
   1.679 +  onModifyRequestCalled : false,
   1.680 +
   1.681 +  onStartRequest: function test_onStart(request, ctx) {  },
   1.682 +  onDataAvailable: function test_OnData() { },
   1.683 +
   1.684 +  onStopRequest: function test_onStop(request, ctx, status) {
   1.685 +    // check on the PI from the channel itself
   1.686 +    ctx.QueryInterface(Components.interfaces.nsIProxiedChannel);
   1.687 +    check_proxy(ctx.proxyInfo, "http", "127.0.0.1", 7246, 0, 0, false);
   1.688 +    pps.unregisterFilter(directFilter);
   1.689 +
   1.690 +    // check on the PI from on-modify-request
   1.691 +    do_check_true(this.onModifyRequestCalled);
   1.692 +    var obs = Components.classes["@mozilla.org/observer-service;1"].getService();
   1.693 +    obs = obs.QueryInterface(Components.interfaces.nsIObserverService);
   1.694 +    obs.removeObserver(this, "http-on-modify-request");
   1.695 +
   1.696 +    run_isresolvable_test();
   1.697 +  },
   1.698 +
   1.699 +   observe: function(subject, topic, data) {
   1.700 +     if (topic === "http-on-modify-request" &&
   1.701 +         subject instanceof Components.interfaces.nsIHttpChannel &&
   1.702 +         subject instanceof Components.interfaces.nsIProxiedChannel) {
   1.703 +       check_proxy(subject.proxyInfo, "http", "127.0.0.1", 7246, 0, 0, false);
   1.704 +       this.onModifyRequestCalled = true;
   1.705 +     }
   1.706 +   }
   1.707 +};
   1.708 +
   1.709 +function run_isresolvable_test()
   1.710 +{
   1.711 +  // test a non resolvable host in the pac file
   1.712 +
   1.713 +  var pac = 'data:text/plain,' +
   1.714 +            'function FindProxyForURL(url, host) {' +
   1.715 +            ' if (isResolvable("nonexistant.lan"))' +
   1.716 +            '   return "DIRECT";' +
   1.717 +            ' return "PROXY 127.0.0.1:1234";' +
   1.718 +            '}';
   1.719 +
   1.720 +  var uri = ios.newURI("http://www.mozilla.org/", null, null);
   1.721 +
   1.722 +  prefs.setIntPref("network.proxy.type", 2);
   1.723 +  prefs.setCharPref("network.proxy.autoconfig_url", pac);
   1.724 +
   1.725 +  var cb = new resolveCallback();
   1.726 +  cb.nextFunction = isresolvable_callback;
   1.727 +  var req = pps.asyncResolve(uri, 0, cb);
   1.728 +}
   1.729 +
   1.730 +function isresolvable_callback(pi)
   1.731 +{
   1.732 +  do_check_neq(pi, null);
   1.733 +  do_check_eq(pi.type, "http");
   1.734 +  do_check_eq(pi.port, 1234);
   1.735 +  do_check_eq(pi.host, "127.0.0.1");
   1.736 +
   1.737 +  prefs.setIntPref("network.proxy.type", 0);
   1.738 +  do_test_finished();
   1.739 +}
   1.740 +
   1.741 +function run_deprecated_sync_test()
   1.742 +{
   1.743 +  var uri = ios.newURI("http://www.mozilla.org/", null, null);
   1.744 +
   1.745 +  pps.QueryInterface(Components.interfaces.nsIProtocolProxyService2);
   1.746 +
   1.747 +  // Verify initial state
   1.748 +  var pi = pps.deprecatedBlockingResolve(uri, 0);
   1.749 +  do_check_eq(pi, null);
   1.750 +
   1.751 +  // Push a filter and verify the results
   1.752 +  var filter1 = new BasicFilter();
   1.753 +  var filter2 = new BasicFilter();
   1.754 +  pps.registerFilter(filter1, 10);
   1.755 +  pps.registerFilter(filter2, 20);
   1.756 +
   1.757 +  pi = pps.deprecatedBlockingResolve(uri, 0);
   1.758 +  check_proxy(pi, "http", "localhost", 8080, 0, 10, true);
   1.759 +  check_proxy(pi.failoverProxy, "direct", "", -1, 0, 0, false);
   1.760 +
   1.761 +  pps.unregisterFilter(filter2);
   1.762 +  pi = pps.deprecatedBlockingResolve(uri, 0);
   1.763 +  check_proxy(pi, "http", "localhost", 8080, 0, 10, true);
   1.764 +  check_proxy(pi.failoverProxy, "direct", "", -1, 0, 0, false);
   1.765 +
   1.766 +  // Remove filter and verify that we return to the initial state
   1.767 +  pps.unregisterFilter(filter1);
   1.768 +  pi = pps.deprecatedBlockingResolve(uri, 0);
   1.769 +  do_check_eq(pi, null);
   1.770 +}
   1.771 +
   1.772 +function run_test() {
   1.773 +  register_test_protocol_handler();
   1.774 +
   1.775 +  // any synchronous tests
   1.776 +  run_deprecated_sync_test();
   1.777 +
   1.778 +  // start of asynchronous test chain
   1.779 +  run_filter_test();
   1.780 +  do_test_pending();
   1.781 +}

mercurial