/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */ /* vim:set ts=2 sw=2 sts=2 et: */ /* This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
// This testcase exercises the Protocol Proxy Service
// These are the major sub tests: // run_filter_test(); // run_filter_test2() // run_filter_test3() // run_pref_test(); // run_pac_test(); // run_pac_cancel_test(); // run_proxy_host_filters_test(); // run_myipaddress_test(); // run_failed_script_test(); // run_isresolvable_test();
"use strict";
var ios = Services.io; var pps = Cc["@mozilla.org/network/protocol-proxy-service;1"].getService(); var prefs = Services.prefs; var again = true;
function register_test_protocol_handler() { var reg = Components.manager.QueryInterface(Ci.nsIComponentRegistrar);
reg.registerFactory(
Components.ID("{4ea7dd3a-8cae-499c-9f18-e1de773ca25b}"), "TestProtocolHandler", "@mozilla.org/network/protocol;1?name=moz-test", new TestProtocolHandlerFactory()
);
}
function run_filter_test_uri() { var cb = new resolveCallback();
cb.nextFunction = filter_test_uri0_1; var uri = ios.newURI("http://www.mozilla.org/");
pps.asyncResolve(uri, 0, cb);
}
function filter_test_uri0_1(pi) { Assert.equal(pi, null);
// Push a filter and verify the results
filter01 = new BasicFilter();
filter02 = new BasicFilter();
pps.registerFilter(filter01, 10);
pps.registerFilter(filter02, 20);
var cb = new resolveCallback();
cb.nextFunction = filter_test_uri0_2; var uri = ios.newURI("http://www.mozilla.org/");
pps.asyncResolve(uri, 0, cb);
}
var cb = new resolveCallback();
cb.nextFunction = filter_test1_2; var channel = NetUtil.newChannel({
uri: "http://www.mozilla.org/",
loadUsingSystemPrincipal: true,
});
pps.asyncResolve(channel, 0, cb);
}
function filter_test1_2(pi) {
check_proxy(pi, "http", "foo", 8080, 0, 10, false);
// Remove filter and verify that we return to the initial state
pps.unregisterFilter(filter11);
var cb = new resolveCallback();
cb.nextFunction = filter_test1_3; var channel = NetUtil.newChannel({
uri: "http://www.mozilla.org/",
loadUsingSystemPrincipal: true,
});
pps.asyncResolve(channel, 0, cb);
}
function filter_test1_3(pi) { Assert.equal(pi, null);
run_filter_test3();
}
var filter_3_1;
function run_filter_test3() { var channel = NetUtil.newChannel({
uri: "http://www.mozilla.org/",
loadUsingSystemPrincipal: true,
}); // Push a filter and verify the results asynchronously
filter_3_1 = new TestFilter("http", "foo", 8080, 0, 10);
pps.registerFilter(filter_3_1, 20);
var cb = new resolveCallback();
cb.nextFunction = filter_test3_1;
pps.asyncResolve(channel, 0, cb);
}
if (hostList.length > hostIDX) {
check_host_filter(hostIDX);
}
}
function check_host_filters_cb() {
hostIDX++; if (hostList.length > hostIDX) {
check_host_filter(hostIDX);
} else {
hostNextFX();
}
}
function check_host_filter(i) {
dump( "*** uri=" + hostList[i] + " bShouldBeFiltered=" + bShouldBeFiltered + "\n"
); var channel = NetUtil.newChannel({
uri: hostList[i],
loadUsingSystemPrincipal: true,
}); var cb = new resolveCallback();
cb.nextFunction = host_filter_cb;
pps.asyncResolve(channel, 0, cb);
}
function host_filter_cb(proxy) { if (bShouldBeFiltered) { Assert.equal(proxy, null);
} else { Assert.notEqual(proxy, null); // Just to be sure, let's check that the proxy is correct // - this should match the proxy setup in the calling function
check_proxy(proxy, "http", "foopy", 8080, 0, -1, false);
}
check_host_filters_cb();
}
// Verify that hists in the host filter list are not proxied // refers to "network.proxy.no_proxies_on"
var uriStrUseProxyList; var hostFilterList; var uriStrFilterList;
function run_proxy_host_filters_test() { // Get prefs object from DOM // Setup a basic HTTP proxy configuration // - pps.resolve() needs this to return proxy info for non-filtered hosts
prefs.setIntPref("network.proxy.type", 1);
prefs.setCharPref("network.proxy.http", "foopy");
prefs.setIntPref("network.proxy.http_port", 8080);
function host_filters_2() { // Set no_proxies_on to include local hosts
prefs.setCharPref( "network.proxy.no_proxies_on",
hostFilterList + ", <local>"
); Assert.equal(
prefs.getCharPref("network.proxy.no_proxies_on"),
hostFilterList + ", <local>"
); // Amend lists - move local domain to filtered list
uriStrFilterList.push(uriStrUseProxyList.pop());
check_host_filters(uriStrFilterList, true, host_filters_3);
}
function host_filters_3() {
check_host_filters(uriStrUseProxyList, false, host_filters_4);
}
function host_filters_4() { // Cleanup
prefs.setCharPref("network.proxy.no_proxies_on", ""); Assert.equal(prefs.getCharPref("network.proxy.no_proxies_on"), "");
run_myipaddress_test();
}
function run_myipaddress_test() { // This test makes sure myIpAddress() comes up with some valid // IP address other than localhost. The DUT must be configured with // an Internet route for this to work - though no Internet traffic // should be created.
// no traffic to this IP is ever sent, it is just a public IP that // does not require DNS to determine a route. var channel = NetUtil.newChannel({
uri: "http://192.0.43.10/",
loadUsingSystemPrincipal: true,
});
prefs.setIntPref("network.proxy.type", 2);
prefs.setCharPref("network.proxy.autoconfig_url", pac);
var cb = new resolveCallback();
cb.nextFunction = myipaddress_callback;
pps.asyncResolve(channel, 0, cb);
}
function myipaddress_callback(pi) { Assert.notEqual(pi, null); Assert.equal(pi.type, "http"); Assert.equal(pi.port, 1234);
// make sure we didn't return localhost Assert.notEqual(pi.host, null); Assert.notEqual(pi.host, "127.0.0.1"); Assert.notEqual(pi.host, "::1");
run_myipaddress_test_2();
}
function run_myipaddress_test_2() { // test that myIPAddress() can be used outside of the scope of // FindProxyForURL(). bug 829646.
var cb = new resolveCallback();
cb.nextFunction = myipaddress2_callback;
pps.asyncResolve(channel, 0, cb);
}
function myipaddress2_callback(pi) { Assert.notEqual(pi, null); Assert.equal(pi.type, "http"); Assert.equal(pi.port, 5678);
// make sure we didn't return localhost Assert.notEqual(pi.host, null); Assert.notEqual(pi.host, "127.0.0.1"); Assert.notEqual(pi.host, "::1");
run_failed_script_test();
}
function run_failed_script_test() { // test to make sure we go direct with invalid PAC // eslint-disable-next-line no-useless-concat var pac = "data:text/plain," + "\nfor(;\n";
function failed_script_callback(pi) { // we should go direct Assert.equal(pi, null);
// test that we honor filters when configured to go direct
prefs.setIntPref("network.proxy.type", 0);
directFilter = new TestFilter("http", "127.0.0.1", 7246, 0, 0);
pps.registerFilter(directFilter, 10);
// test that on-modify-request contains the proxy info too var obs = Cc["@mozilla.org/observer-service;1"].getService();
obs = obs.QueryInterface(Ci.nsIObserverService);
obs.addObserver(directFilterListener, "http-on-modify-request");
var ssm = Services.scriptSecurityManager;
let uri = TEST_URI; var chan = NetUtil.newChannel({
uri,
loadingPrincipal: ssm.createContentPrincipal(Services.io.newURI(uri), {}),
securityFlags: Ci.nsILoadInfo.SEC_ALLOW_CROSS_ORIGIN_INHERITS_SEC_CONTEXT,
contentPolicyType: Ci.nsIContentPolicy.TYPE_DOCUMENT,
});
chan.asyncOpen(directFilterListener);
}
var directFilterListener = {
onModifyRequestCalled: false,
onStartRequest: function test_onStart() {},
onDataAvailable: function test_OnData() {},
onStopRequest: function test_onStop(request) { // check on the PI from the channel itself
request.QueryInterface(Ci.nsIProxiedChannel);
check_proxy(request.proxyInfo, "http", "127.0.0.1", 7246, 0, 0, false);
pps.unregisterFilter(directFilter);
// check on the PI from on-modify-request Assert.ok(this.onModifyRequestCalled); var obs = Cc["@mozilla.org/observer-service;1"].getService();
obs = obs.QueryInterface(Ci.nsIObserverService);
obs.removeObserver(this, "http-on-modify-request");
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.