michael@0: /* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* vim:set ts=2 sw=2 sts=2 et: */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: // This testcase exercises the Protocol Proxy Service michael@0: michael@0: // These are the major sub tests: michael@0: // run_filter_test(); michael@0: // run_filter_test2() michael@0: // run_filter_test3() michael@0: // run_pref_test(); michael@0: // run_pac_test(); michael@0: // run_pac_cancel_test(); michael@0: // run_proxy_host_filters_test(); michael@0: // run_myipaddress_test(); michael@0: // run_failed_script_test(); michael@0: // run_isresolvable_test(); michael@0: michael@0: var ios = Components.classes["@mozilla.org/network/io-service;1"] michael@0: .getService(Components.interfaces.nsIIOService); michael@0: var pps = Components.classes["@mozilla.org/network/protocol-proxy-service;1"] michael@0: .getService(); michael@0: var prefs = Components.classes["@mozilla.org/preferences-service;1"] michael@0: .getService(Components.interfaces.nsIPrefBranch); michael@0: michael@0: /** michael@0: * Test nsIProtocolHandler that allows proxying, but doesn't allow HTTP michael@0: * proxying. michael@0: */ michael@0: function TestProtocolHandler() { michael@0: } michael@0: TestProtocolHandler.prototype = { michael@0: QueryInterface: function(iid) { michael@0: if (iid.equals(Components.interfaces.nsIProtocolHandler) || michael@0: iid.equals(Components.interfaces.nsISupports)) michael@0: return this; michael@0: throw Components.results.NS_ERROR_NO_INTERFACE; michael@0: }, michael@0: scheme: "moz-test", michael@0: defaultPort: -1, michael@0: protocolFlags: Components.interfaces.nsIProtocolHandler.URI_NOAUTH | michael@0: Components.interfaces.nsIProtocolHandler.URI_NORELATIVE | michael@0: Components.interfaces.nsIProtocolHandler.ALLOWS_PROXY | michael@0: Components.interfaces.nsIProtocolHandler.URI_DANGEROUS_TO_LOAD, michael@0: newURI: function(spec, originCharset, baseURI) { michael@0: var uri = Components.classes["@mozilla.org/network/simple-uri;1"] michael@0: .createInstance(Components.interfaces.nsIURI); michael@0: uri.spec = spec; michael@0: return uri; michael@0: }, michael@0: newChannel: function(uri) { michael@0: throw Components.results.NS_ERROR_NOT_IMPLEMENTED; michael@0: }, michael@0: allowPort: function(port, scheme) { michael@0: return true; michael@0: } michael@0: }; michael@0: michael@0: function TestProtocolHandlerFactory() { michael@0: } michael@0: TestProtocolHandlerFactory.prototype = { michael@0: createInstance: function(delegate, iid) { michael@0: return new TestProtocolHandler().QueryInterface(iid); michael@0: }, michael@0: lockFactory: function(lock) { michael@0: } michael@0: }; michael@0: michael@0: function register_test_protocol_handler() { michael@0: var reg = Components.manager.QueryInterface( michael@0: Components.interfaces.nsIComponentRegistrar); michael@0: reg.registerFactory(Components.ID("{4ea7dd3a-8cae-499c-9f18-e1de773ca25b}"), michael@0: "TestProtocolHandler", michael@0: "@mozilla.org/network/protocol;1?name=moz-test", michael@0: new TestProtocolHandlerFactory()); michael@0: } michael@0: michael@0: function check_proxy(pi, type, host, port, flags, timeout, hasNext) { michael@0: do_check_neq(pi, null); michael@0: do_check_eq(pi.type, type); michael@0: do_check_eq(pi.host, host); michael@0: do_check_eq(pi.port, port); michael@0: if (flags != -1) michael@0: do_check_eq(pi.flags, flags); michael@0: if (timeout != -1) michael@0: do_check_eq(pi.failoverTimeout, timeout); michael@0: if (hasNext) michael@0: do_check_neq(pi.failoverProxy, null); michael@0: else michael@0: do_check_eq(pi.failoverProxy, null); michael@0: } michael@0: michael@0: function TestFilter(type, host, port, flags, timeout) { michael@0: this._type = type; michael@0: this._host = host; michael@0: this._port = port; michael@0: this._flags = flags; michael@0: this._timeout = timeout; michael@0: } michael@0: TestFilter.prototype = { michael@0: _type: "", michael@0: _host: "", michael@0: _port: -1, michael@0: _flags: 0, michael@0: _timeout: 0, michael@0: QueryInterface: function(iid) { michael@0: if (iid.equals(Components.interfaces.nsIProtocolProxyFilter) || michael@0: iid.equals(Components.interfaces.nsISupports)) michael@0: return this; michael@0: throw Components.results.NS_ERROR_NO_INTERFACE; michael@0: }, michael@0: applyFilter: function(pps, uri, pi) { michael@0: var pi_tail = pps.newProxyInfo(this._type, this._host, this._port, michael@0: this._flags, this._timeout, null); michael@0: if (pi) michael@0: pi.failoverProxy = pi_tail; michael@0: else michael@0: pi = pi_tail; michael@0: return pi; michael@0: } michael@0: }; michael@0: michael@0: function BasicFilter() {} michael@0: BasicFilter.prototype = { michael@0: QueryInterface: function(iid) { michael@0: if (iid.equals(Components.interfaces.nsIProtocolProxyFilter) || michael@0: iid.equals(Components.interfaces.nsISupports)) michael@0: return this; michael@0: throw Components.results.NS_ERROR_NO_INTERFACE; michael@0: }, michael@0: applyFilter: function(pps, uri, pi) { michael@0: return pps.newProxyInfo("http", "localhost", 8080, 0, 10, michael@0: pps.newProxyInfo("direct", "", -1, 0, 0, null)); michael@0: } michael@0: }; michael@0: michael@0: function resolveCallback() { } michael@0: resolveCallback.prototype = { michael@0: nextFunction: null, michael@0: michael@0: QueryInterface : function (iid) { michael@0: const interfaces = [Components.interfaces.nsIProtocolProxyCallback, michael@0: Components.interfaces.nsISupports]; michael@0: if (!interfaces.some( function(v) { return iid.equals(v) } )) michael@0: throw Components.results.NS_ERROR_NO_INTERFACE; michael@0: return this; michael@0: }, michael@0: michael@0: onProxyAvailable : function (req, uri, pi, status) { michael@0: this.nextFunction(pi); michael@0: } michael@0: }; michael@0: michael@0: function run_filter_test() { michael@0: var uri = ios.newURI("http://www.mozilla.org/", null, null); michael@0: michael@0: // Verify initial state michael@0: var cb = new resolveCallback(); michael@0: cb.nextFunction = filter_test0_1; michael@0: var req = pps.asyncResolve(uri, 0, cb); michael@0: } michael@0: michael@0: var filter01; michael@0: var filter02; michael@0: michael@0: function filter_test0_1(pi) { michael@0: do_check_eq(pi, null); michael@0: michael@0: // Push a filter and verify the results michael@0: michael@0: filter01 = new BasicFilter(); michael@0: filter02 = new BasicFilter(); michael@0: pps.registerFilter(filter01, 10); michael@0: pps.registerFilter(filter02, 20); michael@0: michael@0: var cb = new resolveCallback(); michael@0: cb.nextFunction = filter_test0_2; michael@0: var uri = ios.newURI("http://www.mozilla.org/", null, null); michael@0: var req = pps.asyncResolve(uri, 0, cb); michael@0: } michael@0: michael@0: function filter_test0_2(pi) michael@0: { michael@0: check_proxy(pi, "http", "localhost", 8080, 0, 10, true); michael@0: check_proxy(pi.failoverProxy, "direct", "", -1, 0, 0, false); michael@0: michael@0: pps.unregisterFilter(filter02); michael@0: michael@0: var cb = new resolveCallback(); michael@0: cb.nextFunction = filter_test0_3; michael@0: var uri = ios.newURI("http://www.mozilla.org/", null, null); michael@0: var req = pps.asyncResolve(uri, 0, cb); michael@0: } michael@0: michael@0: function filter_test0_3(pi) michael@0: { michael@0: check_proxy(pi, "http", "localhost", 8080, 0, 10, true); michael@0: check_proxy(pi.failoverProxy, "direct", "", -1, 0, 0, false); michael@0: michael@0: // Remove filter and verify that we return to the initial state michael@0: michael@0: pps.unregisterFilter(filter01); michael@0: michael@0: var cb = new resolveCallback(); michael@0: cb.nextFunction = filter_test0_4; michael@0: var uri = ios.newURI("http://www.mozilla.org/", null, null); michael@0: var req = pps.asyncResolve(uri, 0, cb); michael@0: } michael@0: michael@0: function filter_test0_4(pi) michael@0: { michael@0: do_check_eq(pi, null); michael@0: run_filter_test2(); michael@0: } michael@0: michael@0: var filter11; michael@0: var filter12; michael@0: michael@0: function run_filter_test2() { michael@0: // Push a filter and verify the results michael@0: michael@0: filter11 = new TestFilter("http", "foo", 8080, 0, 10); michael@0: filter12 = new TestFilter("http", "bar", 8090, 0, 10); michael@0: pps.registerFilter(filter11, 20); michael@0: pps.registerFilter(filter12, 10); michael@0: michael@0: var cb = new resolveCallback(); michael@0: cb.nextFunction = filter_test1_1; michael@0: var uri = ios.newURI("http://www.mozilla.org/", null, null); michael@0: var req = pps.asyncResolve(uri, 0, cb); michael@0: } michael@0: michael@0: function filter_test1_1(pi) { michael@0: check_proxy(pi, "http", "bar", 8090, 0, 10, true); michael@0: check_proxy(pi.failoverProxy, "http", "foo", 8080, 0, 10, false); michael@0: michael@0: pps.unregisterFilter(filter12); michael@0: michael@0: var cb = new resolveCallback(); michael@0: cb.nextFunction = filter_test1_2; michael@0: var uri = ios.newURI("http://www.mozilla.org/", null, null); michael@0: var req = pps.asyncResolve(uri, 0, cb); michael@0: } michael@0: michael@0: function filter_test1_2(pi) { michael@0: check_proxy(pi, "http", "foo", 8080, 0, 10, false); michael@0: michael@0: // Remove filter and verify that we return to the initial state michael@0: michael@0: pps.unregisterFilter(filter11); michael@0: michael@0: var cb = new resolveCallback(); michael@0: cb.nextFunction = filter_test1_3; michael@0: var uri = ios.newURI("http://www.mozilla.org/", null, null); michael@0: var req = pps.asyncResolve(uri, 0, cb); michael@0: } michael@0: michael@0: function filter_test1_3(pi) { michael@0: do_check_eq(pi, null); michael@0: run_filter_test3(); michael@0: } michael@0: michael@0: var filter_3_1; michael@0: michael@0: function run_filter_test3() { michael@0: var uri = ios.newURI("http://www.mozilla.org/", null, null); michael@0: michael@0: // Push a filter and verify the results asynchronously michael@0: michael@0: filter_3_1 = new TestFilter("http", "foo", 8080, 0, 10); michael@0: pps.registerFilter(filter_3_1, 20); michael@0: michael@0: var cb = new resolveCallback(); michael@0: cb.nextFunction = filter_test3_1; michael@0: var req = pps.asyncResolve(uri, 0, cb); michael@0: } michael@0: michael@0: function filter_test3_1(pi) { michael@0: check_proxy(pi, "http", "foo", 8080, 0, 10, false); michael@0: pps.unregisterFilter(filter_3_1); michael@0: run_pref_test(); michael@0: } michael@0: michael@0: function run_pref_test() { michael@0: var uri = ios.newURI("http://www.mozilla.org/", null, null); michael@0: michael@0: // Verify 'direct' setting michael@0: michael@0: prefs.setIntPref("network.proxy.type", 0); michael@0: michael@0: var cb = new resolveCallback(); michael@0: cb.nextFunction = pref_test1_1; michael@0: var req = pps.asyncResolve(uri, 0, cb); michael@0: } michael@0: michael@0: function pref_test1_1(pi) michael@0: { michael@0: do_check_eq(pi, null); michael@0: michael@0: // Verify 'manual' setting michael@0: prefs.setIntPref("network.proxy.type", 1); michael@0: michael@0: var cb = new resolveCallback(); michael@0: cb.nextFunction = pref_test1_2; michael@0: var uri = ios.newURI("http://www.mozilla.org/", null, null); michael@0: var req = pps.asyncResolve(uri, 0, cb); michael@0: } michael@0: michael@0: function pref_test1_2(pi) michael@0: { michael@0: // nothing yet configured michael@0: do_check_eq(pi, null); michael@0: michael@0: // try HTTP configuration michael@0: prefs.setCharPref("network.proxy.http", "foopy"); michael@0: prefs.setIntPref("network.proxy.http_port", 8080); michael@0: michael@0: var cb = new resolveCallback(); michael@0: cb.nextFunction = pref_test1_3; michael@0: var uri = ios.newURI("http://www.mozilla.org/", null, null); michael@0: var req = pps.asyncResolve(uri, 0, cb); michael@0: } michael@0: michael@0: function pref_test1_3(pi) michael@0: { michael@0: check_proxy(pi, "http", "foopy", 8080, 0, -1, false); michael@0: michael@0: prefs.setCharPref("network.proxy.http", ""); michael@0: prefs.setIntPref("network.proxy.http_port", 0); michael@0: michael@0: // try SOCKS configuration michael@0: prefs.setCharPref("network.proxy.socks", "barbar"); michael@0: prefs.setIntPref("network.proxy.socks_port", 1203); michael@0: michael@0: var cb = new resolveCallback(); michael@0: cb.nextFunction = pref_test1_4; michael@0: var uri = ios.newURI("http://www.mozilla.org/", null, null); michael@0: var req = pps.asyncResolve(uri, 0, cb); michael@0: } michael@0: michael@0: function pref_test1_4(pi) michael@0: { michael@0: check_proxy(pi, "socks", "barbar", 1203, 0, -1, false); michael@0: run_pac_test(); michael@0: } michael@0: michael@0: function run_protocol_handler_test() { michael@0: var uri = ios.newURI("moz-test:foopy", null, null); michael@0: michael@0: var cb = new resolveCallback(); michael@0: cb.nextFunction = protocol_handler_test_1; michael@0: var req = pps.asyncResolve(uri, 0, cb); michael@0: } michael@0: michael@0: function protocol_handler_test_1(pi) michael@0: { michael@0: do_check_eq(pi, null); michael@0: prefs.setCharPref("network.proxy.autoconfig_url", ""); michael@0: prefs.setIntPref("network.proxy.type", 0); michael@0: michael@0: run_pac_cancel_test(); michael@0: } michael@0: michael@0: function TestResolveCallback() { michael@0: } michael@0: TestResolveCallback.prototype = { michael@0: QueryInterface: michael@0: function TestResolveCallback_QueryInterface(iid) { michael@0: if (iid.equals(Components.interfaces.nsIProtocolProxyCallback) || michael@0: iid.equals(Components.interfaces.nsISupports)) michael@0: return this; michael@0: throw Components.results.NS_ERROR_NO_INTERFACE; michael@0: }, michael@0: michael@0: onProxyAvailable: michael@0: function TestResolveCallback_onProxyAvailable(req, uri, pi, status) { michael@0: dump("*** uri=" + uri.spec + ", status=" + status + "\n"); michael@0: michael@0: do_check_neq(req, null); michael@0: do_check_neq(uri, null); michael@0: do_check_eq(status, 0); michael@0: do_check_neq(pi, null); michael@0: michael@0: check_proxy(pi, "http", "foopy", 8080, 0, -1, true); michael@0: check_proxy(pi.failoverProxy, "direct", "", -1, -1, -1, false); michael@0: michael@0: run_protocol_handler_test(); michael@0: } michael@0: }; michael@0: michael@0: function run_pac_test() { michael@0: var pac = 'data:text/plain,' + michael@0: 'function FindProxyForURL(url, host) {' + michael@0: ' return "PROXY foopy:8080; DIRECT";' + michael@0: '}'; michael@0: var uri = ios.newURI("http://www.mozilla.org/", null, null); michael@0: michael@0: // Configure PAC michael@0: michael@0: prefs.setIntPref("network.proxy.type", 2); michael@0: prefs.setCharPref("network.proxy.autoconfig_url", pac); michael@0: michael@0: var req = pps.asyncResolve(uri, 0, new TestResolveCallback()); michael@0: } michael@0: michael@0: function TestResolveCancelationCallback() { michael@0: } michael@0: TestResolveCancelationCallback.prototype = { michael@0: QueryInterface: michael@0: function TestResolveCallback_QueryInterface(iid) { michael@0: if (iid.equals(Components.interfaces.nsIProtocolProxyCallback) || michael@0: iid.equals(Components.interfaces.nsISupports)) michael@0: return this; michael@0: throw Components.results.NS_ERROR_NO_INTERFACE; michael@0: }, michael@0: michael@0: onProxyAvailable: michael@0: function TestResolveCancelationCallback_onProxyAvailable(req, uri, pi, status) { michael@0: dump("*** uri=" + uri.spec + ", status=" + status + "\n"); michael@0: michael@0: do_check_neq(req, null); michael@0: do_check_neq(uri, null); michael@0: do_check_eq(status, Components.results.NS_ERROR_ABORT); michael@0: do_check_eq(pi, null); michael@0: michael@0: prefs.setCharPref("network.proxy.autoconfig_url", ""); michael@0: prefs.setIntPref("network.proxy.type", 0); michael@0: michael@0: run_proxy_host_filters_test(); michael@0: } michael@0: }; michael@0: michael@0: function run_pac_cancel_test() { michael@0: var uri = ios.newURI("http://www.mozilla.org/", null, null); michael@0: michael@0: // Configure PAC michael@0: var pac = 'data:text/plain,' + michael@0: 'function FindProxyForURL(url, host) {' + michael@0: ' return "PROXY foopy:8080; DIRECT";' + michael@0: '}'; michael@0: prefs.setIntPref("network.proxy.type", 2); michael@0: prefs.setCharPref("network.proxy.autoconfig_url", pac); michael@0: michael@0: var req = pps.asyncResolve(uri, 0, new TestResolveCancelationCallback()); michael@0: req.cancel(Components.results.NS_ERROR_ABORT); michael@0: } michael@0: michael@0: var hostList; michael@0: var hostIDX; michael@0: var bShouldBeFiltered; michael@0: var hostNextFX; michael@0: michael@0: function check_host_filters(hl, shouldBe, nextFX) { michael@0: hostList = hl; michael@0: hostIDX = 0; michael@0: bShouldBeFiltered = shouldBe; michael@0: hostNextFX = nextFX; michael@0: michael@0: if (hostList.length > hostIDX) michael@0: check_host_filter(hostIDX); michael@0: } michael@0: michael@0: function check_host_filters_cb() michael@0: { michael@0: hostIDX++; michael@0: if (hostList.length > hostIDX) michael@0: check_host_filter(hostIDX); michael@0: else michael@0: hostNextFX(); michael@0: } michael@0: michael@0: function check_host_filter(i) { michael@0: var uri; michael@0: dump("*** uri=" + hostList[i] + " bShouldBeFiltered=" + bShouldBeFiltered + "\n"); michael@0: uri = ios.newURI(hostList[i], null, null); michael@0: michael@0: var cb = new resolveCallback(); michael@0: cb.nextFunction = host_filter_cb; michael@0: var req = pps.asyncResolve(uri, 0, cb); michael@0: } michael@0: michael@0: function host_filter_cb(proxy) michael@0: { michael@0: if (bShouldBeFiltered) { michael@0: do_check_eq(proxy, null); michael@0: } else { michael@0: do_check_neq(proxy, null); michael@0: // Just to be sure, let's check that the proxy is correct michael@0: // - this should match the proxy setup in the calling function michael@0: check_proxy(proxy, "http", "foopy", 8080, 0, -1, false); michael@0: } michael@0: check_host_filters_cb(); michael@0: } michael@0: michael@0: michael@0: // Verify that hists in the host filter list are not proxied michael@0: // refers to "network.proxy.no_proxies_on" michael@0: michael@0: var uriStrUseProxyList; michael@0: var uriStrUseProxyList; michael@0: var hostFilterList; michael@0: michael@0: function run_proxy_host_filters_test() { michael@0: // Get prefs object from DOM michael@0: // Setup a basic HTTP proxy configuration michael@0: // - pps.resolve() needs this to return proxy info for non-filtered hosts michael@0: prefs.setIntPref("network.proxy.type", 1); michael@0: prefs.setCharPref("network.proxy.http", "foopy"); michael@0: prefs.setIntPref("network.proxy.http_port", 8080); michael@0: michael@0: // Setup host filter list string for "no_proxies_on" michael@0: hostFilterList = "www.mozilla.org, www.google.com, www.apple.com, " michael@0: + ".domain, .domain2.org" michael@0: prefs.setCharPref("network.proxy.no_proxies_on", hostFilterList); michael@0: do_check_eq(prefs.getCharPref("network.proxy.no_proxies_on"), hostFilterList); michael@0: michael@0: var rv; michael@0: // Check the hosts that should be filtered out michael@0: uriStrFilterList = [ "http://www.mozilla.org/", michael@0: "http://www.google.com/", michael@0: "http://www.apple.com/", michael@0: "http://somehost.domain/", michael@0: "http://someotherhost.domain/", michael@0: "http://somehost.domain2.org/", michael@0: "http://somehost.subdomain.domain2.org/" ]; michael@0: check_host_filters(uriStrFilterList, true, host_filters_1); michael@0: } michael@0: michael@0: function host_filters_1() michael@0: { michael@0: // Check the hosts that should be proxied michael@0: uriStrUseProxyList = [ "http://www.mozilla.com/", michael@0: "http://mail.google.com/", michael@0: "http://somehost.domain.co.uk/", michael@0: "http://somelocalhost/" ]; michael@0: check_host_filters(uriStrUseProxyList, false, host_filters_2); michael@0: } michael@0: michael@0: function host_filters_2() michael@0: { michael@0: // Set no_proxies_on to include local hosts michael@0: prefs.setCharPref("network.proxy.no_proxies_on", hostFilterList + ", "); michael@0: do_check_eq(prefs.getCharPref("network.proxy.no_proxies_on"), michael@0: hostFilterList + ", "); michael@0: // Amend lists - move local domain to filtered list michael@0: uriStrFilterList.push(uriStrUseProxyList.pop()); michael@0: check_host_filters(uriStrFilterList, true, host_filters_3); michael@0: } michael@0: michael@0: function host_filters_3() michael@0: { michael@0: check_host_filters(uriStrUseProxyList, false, host_filters_4); michael@0: } michael@0: michael@0: function host_filters_4() michael@0: { michael@0: // Cleanup michael@0: prefs.setCharPref("network.proxy.no_proxies_on", ""); michael@0: do_check_eq(prefs.getCharPref("network.proxy.no_proxies_on"), ""); michael@0: michael@0: run_myipaddress_test(); michael@0: } michael@0: michael@0: function run_myipaddress_test() michael@0: { michael@0: // This test makes sure myIpAddress() comes up with some valid michael@0: // IP address other than localhost. The DUT must be configured with michael@0: // an Internet route for this to work - though no Internet traffic michael@0: // should be created. michael@0: michael@0: var pac = 'data:text/plain,' + michael@0: 'function FindProxyForURL(url, host) {' + michael@0: ' return "PROXY " + myIpAddress() + ":1234";' + michael@0: '}'; michael@0: michael@0: // no traffic to this IP is ever sent, it is just a public IP that michael@0: // does not require DNS to determine a route. michael@0: var uri = ios.newURI("http://192.0.43.10/", null, null); michael@0: michael@0: prefs.setIntPref("network.proxy.type", 2); michael@0: prefs.setCharPref("network.proxy.autoconfig_url", pac); michael@0: michael@0: var cb = new resolveCallback(); michael@0: cb.nextFunction = myipaddress_callback; michael@0: var req = pps.asyncResolve(uri, 0, cb); michael@0: } michael@0: michael@0: function myipaddress_callback(pi) michael@0: { michael@0: do_check_neq(pi, null); michael@0: do_check_eq(pi.type, "http"); michael@0: do_check_eq(pi.port, 1234); michael@0: michael@0: // make sure we didn't return localhost michael@0: do_check_neq(pi.host, null); michael@0: do_check_neq(pi.host, "127.0.0.1"); michael@0: do_check_neq(pi.host, "::1"); michael@0: michael@0: run_myipaddress_test_2(); michael@0: } michael@0: michael@0: function run_myipaddress_test_2() michael@0: { michael@0: // test that myIPAddress() can be used outside of the scope of michael@0: // FindProxyForURL(). bug 829646. michael@0: michael@0: var pac = 'data:text/plain,' + michael@0: 'var myaddr = myIpAddress(); ' + michael@0: 'function FindProxyForURL(url, host) {' + michael@0: ' return "PROXY " + myaddr + ":5678";' + michael@0: '}'; michael@0: michael@0: var uri = ios.newURI("http://www.mozilla.org/", null, null); michael@0: prefs.setIntPref("network.proxy.type", 2); michael@0: prefs.setCharPref("network.proxy.autoconfig_url", pac); michael@0: michael@0: var cb = new resolveCallback(); michael@0: cb.nextFunction = myipaddress2_callback; michael@0: var req = pps.asyncResolve(uri, 0, cb); michael@0: } michael@0: michael@0: function myipaddress2_callback(pi) michael@0: { michael@0: do_check_neq(pi, null); michael@0: do_check_eq(pi.type, "http"); michael@0: do_check_eq(pi.port, 5678); michael@0: michael@0: // make sure we didn't return localhost michael@0: do_check_neq(pi.host, null); michael@0: do_check_neq(pi.host, "127.0.0.1"); michael@0: do_check_neq(pi.host, "::1"); michael@0: michael@0: run_failed_script_test(); michael@0: } michael@0: michael@0: function run_failed_script_test() michael@0: { michael@0: // test to make sure we go direct with invalid PAC michael@0: var pac = 'data:text/plain,' + michael@0: '\nfor(;\n'; michael@0: michael@0: var uri = ios.newURI("http://www.mozilla.org/", null, null); michael@0: michael@0: prefs.setIntPref("network.proxy.type", 2); michael@0: prefs.setCharPref("network.proxy.autoconfig_url", pac); michael@0: michael@0: var cb = new resolveCallback(); michael@0: cb.nextFunction = failed_script_callback; michael@0: var req = pps.asyncResolve(uri, 0, cb); michael@0: } michael@0: michael@0: var directFilter; michael@0: michael@0: function failed_script_callback(pi) michael@0: { michael@0: // we should go direct michael@0: do_check_eq(pi, null); michael@0: michael@0: // test that we honor filters when configured to go direct michael@0: prefs.setIntPref("network.proxy.type", 0); michael@0: directFilter = new TestFilter("http", "127.0.0.1", 7246, 0, 0); michael@0: pps.registerFilter(directFilter, 10); michael@0: michael@0: // test that on-modify-request contains the proxy info too michael@0: var obs = Components.classes["@mozilla.org/observer-service;1"].getService(); michael@0: obs = obs.QueryInterface(Components.interfaces.nsIObserverService); michael@0: obs.addObserver(directFilterListener, "http-on-modify-request", false); michael@0: michael@0: var chan = ios.newChannel("http://127.0.0.1:7247", "", null); michael@0: chan.asyncOpen(directFilterListener, chan); michael@0: } michael@0: michael@0: var directFilterListener = { michael@0: onModifyRequestCalled : false, michael@0: michael@0: onStartRequest: function test_onStart(request, ctx) { }, michael@0: onDataAvailable: function test_OnData() { }, michael@0: michael@0: onStopRequest: function test_onStop(request, ctx, status) { michael@0: // check on the PI from the channel itself michael@0: ctx.QueryInterface(Components.interfaces.nsIProxiedChannel); michael@0: check_proxy(ctx.proxyInfo, "http", "127.0.0.1", 7246, 0, 0, false); michael@0: pps.unregisterFilter(directFilter); michael@0: michael@0: // check on the PI from on-modify-request michael@0: do_check_true(this.onModifyRequestCalled); michael@0: var obs = Components.classes["@mozilla.org/observer-service;1"].getService(); michael@0: obs = obs.QueryInterface(Components.interfaces.nsIObserverService); michael@0: obs.removeObserver(this, "http-on-modify-request"); michael@0: michael@0: run_isresolvable_test(); michael@0: }, michael@0: michael@0: observe: function(subject, topic, data) { michael@0: if (topic === "http-on-modify-request" && michael@0: subject instanceof Components.interfaces.nsIHttpChannel && michael@0: subject instanceof Components.interfaces.nsIProxiedChannel) { michael@0: check_proxy(subject.proxyInfo, "http", "127.0.0.1", 7246, 0, 0, false); michael@0: this.onModifyRequestCalled = true; michael@0: } michael@0: } michael@0: }; michael@0: michael@0: function run_isresolvable_test() michael@0: { michael@0: // test a non resolvable host in the pac file michael@0: michael@0: var pac = 'data:text/plain,' + michael@0: 'function FindProxyForURL(url, host) {' + michael@0: ' if (isResolvable("nonexistant.lan"))' + michael@0: ' return "DIRECT";' + michael@0: ' return "PROXY 127.0.0.1:1234";' + michael@0: '}'; michael@0: michael@0: var uri = ios.newURI("http://www.mozilla.org/", null, null); michael@0: michael@0: prefs.setIntPref("network.proxy.type", 2); michael@0: prefs.setCharPref("network.proxy.autoconfig_url", pac); michael@0: michael@0: var cb = new resolveCallback(); michael@0: cb.nextFunction = isresolvable_callback; michael@0: var req = pps.asyncResolve(uri, 0, cb); michael@0: } michael@0: michael@0: function isresolvable_callback(pi) michael@0: { michael@0: do_check_neq(pi, null); michael@0: do_check_eq(pi.type, "http"); michael@0: do_check_eq(pi.port, 1234); michael@0: do_check_eq(pi.host, "127.0.0.1"); michael@0: michael@0: prefs.setIntPref("network.proxy.type", 0); michael@0: do_test_finished(); michael@0: } michael@0: michael@0: function run_deprecated_sync_test() michael@0: { michael@0: var uri = ios.newURI("http://www.mozilla.org/", null, null); michael@0: michael@0: pps.QueryInterface(Components.interfaces.nsIProtocolProxyService2); michael@0: michael@0: // Verify initial state michael@0: var pi = pps.deprecatedBlockingResolve(uri, 0); michael@0: do_check_eq(pi, null); michael@0: michael@0: // Push a filter and verify the results michael@0: var filter1 = new BasicFilter(); michael@0: var filter2 = new BasicFilter(); michael@0: pps.registerFilter(filter1, 10); michael@0: pps.registerFilter(filter2, 20); michael@0: michael@0: pi = pps.deprecatedBlockingResolve(uri, 0); michael@0: check_proxy(pi, "http", "localhost", 8080, 0, 10, true); michael@0: check_proxy(pi.failoverProxy, "direct", "", -1, 0, 0, false); michael@0: michael@0: pps.unregisterFilter(filter2); michael@0: pi = pps.deprecatedBlockingResolve(uri, 0); michael@0: check_proxy(pi, "http", "localhost", 8080, 0, 10, true); michael@0: check_proxy(pi.failoverProxy, "direct", "", -1, 0, 0, false); michael@0: michael@0: // Remove filter and verify that we return to the initial state michael@0: pps.unregisterFilter(filter1); michael@0: pi = pps.deprecatedBlockingResolve(uri, 0); michael@0: do_check_eq(pi, null); michael@0: } michael@0: michael@0: function run_test() { michael@0: register_test_protocol_handler(); michael@0: michael@0: // any synchronous tests michael@0: run_deprecated_sync_test(); michael@0: michael@0: // start of asynchronous test chain michael@0: run_filter_test(); michael@0: do_test_pending(); michael@0: }