netwerk/test/unit/test_speculative_connect.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/netwerk/test/unit/test_speculative_connect.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,330 @@
     1.4 +/* -*- Mode: JavaScript; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
     1.5 +/* vim: set ts=4 sts=4 et sw=4 tw=80: */
     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 +const CC = Components.Constructor;
    1.11 +const ServerSocket = CC("@mozilla.org/network/server-socket;1",
    1.12 +                        "nsIServerSocket",
    1.13 +                        "init");
    1.14 +var serv;
    1.15 +var ios;
    1.16 +
    1.17 +/** Example local IP addresses (literal IP address hostname).
    1.18 + *
    1.19 + * Note: for IPv6 Unique Local and Link Local, a wider range of addresses is
    1.20 + * set aside than those most commonly used. Technically, link local addresses
    1.21 + * include those beginning with fe80:: through febf::, although in practise
    1.22 + * only fe80:: is used. Necko code blocks speculative connections for the wider
    1.23 + * range; hence, this test considers that range too.
    1.24 + */
    1.25 +var localIPv4Literals =
    1.26 +    [ // IPv4 RFC1918 \
    1.27 +      "10.0.0.1", "10.10.10.10", "10.255.255.255",         // 10/8
    1.28 +      "172.16.0.1", "172.23.172.12", "172.31.255.255",     // 172.16/20
    1.29 +      "192.168.0.1", "192.168.192.168", "192.168.255.255", // 192.168/16
    1.30 +      // IPv4 Link Local
    1.31 +      "169.254.0.1", "169.254.192.154", "169.254.255.255"  // 169.254/16
    1.32 +    ];
    1.33 +var localIPv6Literals =
    1.34 +    [ // IPv6 Unique Local fc00::/7
    1.35 +      "fc00::1", "fdfe:dcba:9876:abcd:ef01:2345:6789:abcd",
    1.36 +      "fdff:ffff:ffff:ffff:ffff:ffff:ffff:ffff",
    1.37 +      // IPv6 Link Local fe80::/10
    1.38 +      "fe80::1", "fe80::abcd:ef01:2345:6789",
    1.39 +      "febf:ffff:ffff:ffff:ffff:ffff:ffff:ffff"
    1.40 +    ];
    1.41 +var localIPLiterals = localIPv4Literals.concat(localIPv6Literals);
    1.42 +
    1.43 +/** Test function list and descriptions.
    1.44 + */
    1.45 +var testList =
    1.46 +    [ test_speculative_connect,
    1.47 +      test_hostnames_resolving_to_local_addresses,
    1.48 +      test_proxies_with_local_addresses
    1.49 +    ];
    1.50 +
    1.51 +var testDescription =
    1.52 +    [ "Expect pass with localhost",
    1.53 +      "Expect failure with resolved local IPs",
    1.54 +      "Expect failure for proxies with local IPs"
    1.55 +    ];
    1.56 +
    1.57 +var testIdx = 0;
    1.58 +var hostIdx = 0;
    1.59 +
    1.60 +
    1.61 +/** TestServer
    1.62 + *
    1.63 + * Implements nsIServerSocket for test_speculative_connect.
    1.64 + */
    1.65 +function TestServer() {
    1.66 +    this.listener = ServerSocket(-1, true, -1);
    1.67 +    this.listener.asyncListen(this);
    1.68 +}
    1.69 +
    1.70 +TestServer.prototype = {
    1.71 +    QueryInterface: function(iid) {
    1.72 +        if (iid.equals(Ci.nsIServerSocket) ||
    1.73 +            iid.equals(Ci.nsISupports))
    1.74 +            return this;
    1.75 +        throw Cr.NS_ERROR_NO_INTERFACE;
    1.76 +    },
    1.77 +    onSocketAccepted: function(socket, trans) {
    1.78 +        try { this.listener.close(); } catch(e) {}
    1.79 +        do_check_true(true);
    1.80 +        next_test();
    1.81 +    },
    1.82 +
    1.83 +    onStopListening: function(socket) {}
    1.84 +};
    1.85 +
    1.86 +/** TestOutputStreamCallback
    1.87 + *
    1.88 + * Implements nsIOutputStreamCallback for socket layer tests.
    1.89 + */
    1.90 +function TestOutputStreamCallback(transport, hostname, proxied, expectSuccess, next) {
    1.91 +    this.transport = transport;
    1.92 +    this.hostname = hostname;
    1.93 +    this.proxied = proxied;
    1.94 +    this.expectSuccess = expectSuccess;
    1.95 +    this.next = next;
    1.96 +    this.dummyContent = "Dummy content";
    1.97 +}
    1.98 +
    1.99 +TestOutputStreamCallback.prototype = {
   1.100 +    QueryInterface: function(iid) {
   1.101 +        if (iid.equals(Ci.nsIOutputStreamCallback) ||
   1.102 +            iid.equals(Ci.nsISupports))
   1.103 +            return this;
   1.104 +        throw Cr.NS_ERROR_NO_INTERFACE;
   1.105 +    },
   1.106 +    onOutputStreamReady: function(stream) {
   1.107 +        do_check_neq(typeof(stream), undefined);
   1.108 +        try {
   1.109 +            stream.write(this.dummyContent, this.dummyContent.length);
   1.110 +        } catch (e) {
   1.111 +            // Spec Connect FAILED.
   1.112 +            do_check_instanceof(e, Ci.nsIException);
   1.113 +            if (this.expectSuccess) {
   1.114 +                // We may expect success, but the address could be unreachable
   1.115 +                // in the test environment, so expect errors.
   1.116 +                if (this.proxied) {
   1.117 +                    do_check_true(e.result == Cr.NS_ERROR_NET_TIMEOUT ||
   1.118 +                                  e.result == Cr.NS_ERROR_PROXY_CONNECTION_REFUSED);
   1.119 +                } else {
   1.120 +                    do_check_true(e.result == Cr.NS_ERROR_NET_TIMEOUT ||
   1.121 +                                  e.result == Cr.NS_ERROR_CONNECTION_REFUSED);
   1.122 +                }
   1.123 +            } else {
   1.124 +                // A refusal to connect speculatively should throw an error.
   1.125 +                do_check_eq(e.result, Cr.NS_ERROR_CONNECTION_REFUSED);
   1.126 +            }
   1.127 +            this.transport.close(Cr.NS_BINDING_ABORTED);
   1.128 +            this.next();
   1.129 +            return;
   1.130 +        }
   1.131 +        // Spec Connect SUCCEEDED.
   1.132 +        if (this.expectSuccess) {
   1.133 +            do_check_true(true, "Success for " + this.hostname);
   1.134 +        } else {
   1.135 +            do_throw("Speculative Connect should have failed for " +
   1.136 +                     this.hostname);
   1.137 +        }
   1.138 +        this.transport.close(Cr.NS_BINDING_ABORTED);
   1.139 +        this.next();
   1.140 +    }
   1.141 +};
   1.142 +
   1.143 +/** test_speculative_connect
   1.144 + *
   1.145 + * Tests a basic positive case using nsIOService.SpeculativeConnect:
   1.146 + * connecting to localhost.
   1.147 + */
   1.148 +function test_speculative_connect() {
   1.149 +    serv = new TestServer();
   1.150 +    var URI = ios.newURI("http://localhost:" + serv.listener.port + "/just/a/test", null, null);
   1.151 +    ios.QueryInterface(Ci.nsISpeculativeConnect)
   1.152 +        .speculativeConnect(URI, null);
   1.153 +}
   1.154 +
   1.155 +/* Speculative connections should not be allowed for hosts with local IP
   1.156 + * addresses (Bug 853423). That list includes:
   1.157 + *  -- IPv4 RFC1918 and Link Local Addresses.
   1.158 + *  -- IPv6 Unique and Link Local Addresses.
   1.159 + *
   1.160 + * Two tests are required:
   1.161 + *  1. Verify IP Literals passed to the SpeculativeConnect API.
   1.162 + *  2. Verify hostnames that need to be resolved at the socket layer.
   1.163 + */
   1.164 +
   1.165 +/** test_hostnames_resolving_to_addresses
   1.166 + *
   1.167 + * Common test function for resolved hostnames. Takes a list of hosts, a
   1.168 + * boolean to determine if the test is expected to succeed or fail, and a
   1.169 + * function to call the next test case.
   1.170 + */
   1.171 +function test_hostnames_resolving_to_addresses(host, expectSuccess, next) {
   1.172 +    do_print(host);
   1.173 +    var sts = Cc["@mozilla.org/network/socket-transport-service;1"]
   1.174 +              .getService(Ci.nsISocketTransportService);
   1.175 +    do_check_neq(typeof(sts), undefined);
   1.176 +    var transport = sts.createTransport(null, 0, host, 80, null);
   1.177 +    do_check_neq(typeof(transport), undefined);
   1.178 +
   1.179 +    transport.connectionFlags = Ci.nsISocketTransport.DISABLE_RFC1918;
   1.180 +    transport.setTimeout(Ci.nsISocketTransport.TIMEOUT_CONNECT, 1);
   1.181 +    transport.setTimeout(Ci.nsISocketTransport.TIMEOUT_READ_WRITE, 1);
   1.182 +    do_check_eq(1, transport.getTimeout(Ci.nsISocketTransport.TIMEOUT_CONNECT));
   1.183 +
   1.184 +    var outStream = transport.openOutputStream(Ci.nsITransport.OPEN_UNBUFFERED,0,0);
   1.185 +    do_check_neq(typeof(outStream), undefined);
   1.186 +
   1.187 +    var callback = new TestOutputStreamCallback(transport, host, false,
   1.188 +                                                expectSuccess,
   1.189 +                                                next);
   1.190 +    do_check_neq(typeof(callback), undefined);
   1.191 +
   1.192 +    // Need to get main thread pointer to ensure nsSocketTransport::AsyncWait
   1.193 +    // adds callback to nsOutputStreamReadyEvent on main thread, and doesn't
   1.194 +    // addref off the main thread.
   1.195 +    var gThreadManager = Cc["@mozilla.org/thread-manager;1"]
   1.196 +    .getService(Ci.nsIThreadManager);
   1.197 +    var mainThread = gThreadManager.currentThread;
   1.198 +
   1.199 +    try {
   1.200 +        outStream.QueryInterface(Ci.nsIAsyncOutputStream)
   1.201 +                 .asyncWait(callback, 0, 0, mainThread);
   1.202 +    } catch (e) {
   1.203 +        do_throw("asyncWait should not fail!");
   1.204 +    }
   1.205 +}
   1.206 +
   1.207 +/**
   1.208 + * test_hostnames_resolving_to_local_addresses
   1.209 + *
   1.210 + * Creates an nsISocketTransport and simulates a speculative connect request
   1.211 + * for a hostname that resolves to a local IP address.
   1.212 + * Runs asynchronously; on test success (i.e. failure to connect), the callback
   1.213 + * will call this function again until all hostnames in the test list are done.
   1.214 + *
   1.215 + * Note: This test also uses an IP literal for the hostname. This should be ok,
   1.216 + * as the socket layer will ask for the hostname to be resolved anyway, and DNS
   1.217 + * code should return a numerical version of the address internally.
   1.218 + */
   1.219 +function test_hostnames_resolving_to_local_addresses() {
   1.220 +    if (hostIdx >= localIPLiterals.length) {
   1.221 +        // No more local IP addresses; move on.
   1.222 +        next_test();
   1.223 +        return;
   1.224 +    }
   1.225 +    var host = localIPLiterals[hostIdx++];
   1.226 +    // Test another local IP address when the current one is done.
   1.227 +    var next = test_hostnames_resolving_to_local_addresses;
   1.228 +    test_hostnames_resolving_to_addresses(host, false, next);
   1.229 +}
   1.230 +
   1.231 +/** test_speculative_connect_with_host_list
   1.232 + *
   1.233 + * Common test function for resolved proxy hosts. Takes a list of hosts, a
   1.234 + * boolean to determine if the test is expected to succeed or fail, and a
   1.235 + * function to call the next test case.
   1.236 + */
   1.237 +function test_proxies(proxyHost, expectSuccess, next) {
   1.238 +    do_print("Proxy: " + proxyHost);
   1.239 +    var sts = Cc["@mozilla.org/network/socket-transport-service;1"]
   1.240 +              .getService(Ci.nsISocketTransportService);
   1.241 +    do_check_neq(typeof(sts), undefined);
   1.242 +    var pps = Cc["@mozilla.org/network/protocol-proxy-service;1"]
   1.243 +              .getService();
   1.244 +    do_check_neq(typeof(pps), undefined);
   1.245 +
   1.246 +    var proxyInfo = pps.newProxyInfo("http", proxyHost, 8080, 0, 1, null);
   1.247 +    do_check_neq(typeof(proxyInfo), undefined);
   1.248 +
   1.249 +    var transport = sts.createTransport(null, 0, "dummyHost", 80, proxyInfo);
   1.250 +    do_check_neq(typeof(transport), undefined);
   1.251 +
   1.252 +    transport.connectionFlags = Ci.nsISocketTransport.DISABLE_RFC1918;
   1.253 +
   1.254 +    transport.setTimeout(Ci.nsISocketTransport.TIMEOUT_CONNECT, 1);
   1.255 +    do_check_eq(1, transport.getTimeout(Ci.nsISocketTransport.TIMEOUT_CONNECT));
   1.256 +    transport.setTimeout(Ci.nsISocketTransport.TIMEOUT_READ_WRITE, 1);
   1.257 +
   1.258 +    var outStream = transport.openOutputStream(Ci.nsITransport.OPEN_UNBUFFERED,0,0);
   1.259 +    do_check_neq(typeof(outStream), undefined);
   1.260 +
   1.261 +    var callback = new TestOutputStreamCallback(transport, proxyHost, true,
   1.262 +                                                expectSuccess,
   1.263 +                                                next);
   1.264 +    do_check_neq(typeof(callback), undefined);
   1.265 +
   1.266 +    // Need to get main thread pointer to ensure nsSocketTransport::AsyncWait
   1.267 +    // adds callback to nsOutputStreamReadyEvent on main thread, and doesn't
   1.268 +    // addref off the main thread.
   1.269 +    var gThreadManager = Cc["@mozilla.org/thread-manager;1"]
   1.270 +    .getService(Ci.nsIThreadManager);
   1.271 +    var mainThread = gThreadManager.currentThread;
   1.272 +
   1.273 +    try {
   1.274 +        outStream.QueryInterface(Ci.nsIAsyncOutputStream)
   1.275 +                 .asyncWait(callback, 0, 0, mainThread);
   1.276 +    } catch (e) {
   1.277 +        do_throw("asyncWait should not fail!");
   1.278 +    }
   1.279 +}
   1.280 +
   1.281 +/**
   1.282 + * test_proxies_with_local_addresses
   1.283 + *
   1.284 + * Creates an nsISocketTransport and simulates a speculative connect request
   1.285 + * for a proxy that resolves to a local IP address.
   1.286 + * Runs asynchronously; on test success (i.e. failure to connect), the callback
   1.287 + * will call this function again until all proxies in the test list are done.
   1.288 + *
   1.289 + * Note: This test also uses an IP literal for the proxy. This should be ok,
   1.290 + * as the socket layer will ask for the proxy to be resolved anyway, and DNS
   1.291 + * code should return a numerical version of the address internally.
   1.292 + */
   1.293 +function test_proxies_with_local_addresses() {
   1.294 +    if (hostIdx >= localIPLiterals.length) {
   1.295 +        // No more local IP addresses; move on.
   1.296 +        next_test();
   1.297 +        return;
   1.298 +    }
   1.299 +    var host = localIPLiterals[hostIdx++];
   1.300 +    // Test another local IP address when the current one is done.
   1.301 +    var next = test_proxies_with_local_addresses;
   1.302 +    test_proxies(host, false, next);
   1.303 +}
   1.304 +
   1.305 +/** next_test
   1.306 + *
   1.307 + * Calls the next test in testList. Each test is responsible for calling this
   1.308 + * function when its test cases are complete.
   1.309 + */
   1.310 +function next_test() {
   1.311 +    if (testIdx >= testList.length) {
   1.312 +        // No more tests; we're done.
   1.313 +        do_test_finished();
   1.314 +        return;
   1.315 +    }
   1.316 +    do_print("SpeculativeConnect: " + testDescription[testIdx]);
   1.317 +    hostIdx = 0;
   1.318 +    // Start next test in list.
   1.319 +    testList[testIdx++]();
   1.320 +}
   1.321 +
   1.322 +/** run_test
   1.323 + *
   1.324 + * Main entry function for test execution.
   1.325 + */
   1.326 +function run_test() {
   1.327 +    ios = Cc["@mozilla.org/network/io-service;1"]
   1.328 +        .getService(Ci.nsIIOService);
   1.329 +
   1.330 +    do_test_pending();
   1.331 +    next_test();
   1.332 +}
   1.333 +

mercurial