Thu, 15 Jan 2015 15:59:08 +0100
Implement a real Private Browsing Mode condition by changing the API/ABI;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.
michael@0 | 1 | /* -*- Mode: javascript; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
michael@0 | 2 | /* vim: set ts=8 sts=2 et sw=2 tw=80: */ |
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 | "use strict"; |
michael@0 | 8 | |
michael@0 | 9 | Cu.import("resource://testing-common/httpd.js"); |
michael@0 | 10 | |
michael@0 | 11 | var baseURL; |
michael@0 | 12 | const kResponseTimeoutPref = "network.http.response.timeout"; |
michael@0 | 13 | const kResponseTimeout = 1; |
michael@0 | 14 | const kShortLivedKeepalivePref = |
michael@0 | 15 | "network.http.tcp_keepalive.short_lived_connections"; |
michael@0 | 16 | const kLongLivedKeepalivePref = |
michael@0 | 17 | "network.http.tcp_keepalive.long_lived_connections"; |
michael@0 | 18 | |
michael@0 | 19 | const prefService = Cc["@mozilla.org/preferences-service;1"] |
michael@0 | 20 | .getService(Ci.nsIPrefBranch); |
michael@0 | 21 | |
michael@0 | 22 | var server = new HttpServer(); |
michael@0 | 23 | |
michael@0 | 24 | function TimeoutListener(expectResponse) { |
michael@0 | 25 | this.expectResponse = expectResponse; |
michael@0 | 26 | } |
michael@0 | 27 | |
michael@0 | 28 | TimeoutListener.prototype = { |
michael@0 | 29 | onStartRequest: function (request, ctx) { |
michael@0 | 30 | }, |
michael@0 | 31 | |
michael@0 | 32 | onDataAvailable: function (request, ctx, stream) { |
michael@0 | 33 | }, |
michael@0 | 34 | |
michael@0 | 35 | onStopRequest: function (request, ctx, status) { |
michael@0 | 36 | if (this.expectResponse) { |
michael@0 | 37 | do_check_eq(status, Cr.NS_OK); |
michael@0 | 38 | } else { |
michael@0 | 39 | do_check_eq(status, Cr.NS_ERROR_NET_TIMEOUT); |
michael@0 | 40 | } |
michael@0 | 41 | |
michael@0 | 42 | run_next_test(); |
michael@0 | 43 | }, |
michael@0 | 44 | }; |
michael@0 | 45 | |
michael@0 | 46 | function serverStopListener() { |
michael@0 | 47 | do_test_finished(); |
michael@0 | 48 | } |
michael@0 | 49 | |
michael@0 | 50 | function testTimeout(timeoutEnabled, expectResponse) { |
michael@0 | 51 | // Set timeout pref. |
michael@0 | 52 | if (timeoutEnabled) { |
michael@0 | 53 | prefService.setIntPref(kResponseTimeoutPref, kResponseTimeout); |
michael@0 | 54 | } else { |
michael@0 | 55 | prefService.setIntPref(kResponseTimeoutPref, 0); |
michael@0 | 56 | } |
michael@0 | 57 | |
michael@0 | 58 | var ios = Cc["@mozilla.org/network/io-service;1"] |
michael@0 | 59 | .getService(Ci.nsIIOService); |
michael@0 | 60 | var chan = ios.newChannel(baseURL, null, null) |
michael@0 | 61 | .QueryInterface(Ci.nsIHttpChannel); |
michael@0 | 62 | var listener = new TimeoutListener(expectResponse); |
michael@0 | 63 | chan.asyncOpen(listener, null); |
michael@0 | 64 | } |
michael@0 | 65 | |
michael@0 | 66 | function testTimeoutEnabled() { |
michael@0 | 67 | // Set a timeout value; expect a timeout and no response. |
michael@0 | 68 | testTimeout(true, false); |
michael@0 | 69 | } |
michael@0 | 70 | |
michael@0 | 71 | function testTimeoutDisabled() { |
michael@0 | 72 | // Set a timeout value of 0; expect a response. |
michael@0 | 73 | testTimeout(false, true); |
michael@0 | 74 | } |
michael@0 | 75 | |
michael@0 | 76 | function testTimeoutDisabledByShortLivedKeepalives() { |
michael@0 | 77 | // Enable TCP Keepalives for short lived HTTP connections. |
michael@0 | 78 | prefService.setBoolPref(kShortLivedKeepalivePref, true); |
michael@0 | 79 | prefService.setBoolPref(kLongLivedKeepalivePref, false); |
michael@0 | 80 | |
michael@0 | 81 | // Try to set a timeout value, but expect a response without timeout. |
michael@0 | 82 | testTimeout(true, true); |
michael@0 | 83 | } |
michael@0 | 84 | |
michael@0 | 85 | function testTimeoutDisabledByLongLivedKeepalives() { |
michael@0 | 86 | // Enable TCP Keepalives for long lived HTTP connections. |
michael@0 | 87 | prefService.setBoolPref(kShortLivedKeepalivePref, false); |
michael@0 | 88 | prefService.setBoolPref(kLongLivedKeepalivePref, true); |
michael@0 | 89 | |
michael@0 | 90 | // Try to set a timeout value, but expect a response without timeout. |
michael@0 | 91 | testTimeout(true, true); |
michael@0 | 92 | } |
michael@0 | 93 | |
michael@0 | 94 | function testTimeoutDisabledByBothKeepalives() { |
michael@0 | 95 | // Enable TCP Keepalives for short and long lived HTTP connections. |
michael@0 | 96 | prefService.setBoolPref(kShortLivedKeepalivePref, true); |
michael@0 | 97 | prefService.setBoolPref(kLongLivedKeepalivePref, true); |
michael@0 | 98 | |
michael@0 | 99 | // Try to set a timeout value, but expect a response without timeout. |
michael@0 | 100 | testTimeout(true, true); |
michael@0 | 101 | } |
michael@0 | 102 | |
michael@0 | 103 | function setup_tests() { |
michael@0 | 104 | // Start tests with timeout enabled, i.e. disable TCP keepalives for HTTP. |
michael@0 | 105 | // Reset pref in cleanup. |
michael@0 | 106 | if (prefService.getBoolPref(kShortLivedKeepalivePref)) { |
michael@0 | 107 | prefService.setBoolPref(kShortLivedKeepalivePref, false); |
michael@0 | 108 | do_register_cleanup(function() { |
michael@0 | 109 | prefService.setBoolPref(kShortLivedKeepalivePref, true); |
michael@0 | 110 | }); |
michael@0 | 111 | } |
michael@0 | 112 | if (prefService.getBoolPref(kLongLivedKeepalivePref)) { |
michael@0 | 113 | prefService.setBoolPref(kLongLivedKeepalivePref, false); |
michael@0 | 114 | do_register_cleanup(function() { |
michael@0 | 115 | prefService.setBoolPref(kLongLivedKeepalivePref, true); |
michael@0 | 116 | }); |
michael@0 | 117 | } |
michael@0 | 118 | |
michael@0 | 119 | var tests = [ |
michael@0 | 120 | // Enable with a timeout value >0; |
michael@0 | 121 | testTimeoutEnabled, |
michael@0 | 122 | // Disable with a timeout value of 0; |
michael@0 | 123 | testTimeoutDisabled, |
michael@0 | 124 | // Disable by enabling TCP keepalive for short-lived HTTP connections. |
michael@0 | 125 | testTimeoutDisabledByShortLivedKeepalives, |
michael@0 | 126 | // Disable by enabling TCP keepalive for long-lived HTTP connections. |
michael@0 | 127 | testTimeoutDisabledByLongLivedKeepalives, |
michael@0 | 128 | // Disable by enabling TCP keepalive for both HTTP connection types. |
michael@0 | 129 | testTimeoutDisabledByBothKeepalives |
michael@0 | 130 | ]; |
michael@0 | 131 | |
michael@0 | 132 | for (var i=0; i < tests.length; i++) { |
michael@0 | 133 | add_test(tests[i]); |
michael@0 | 134 | } |
michael@0 | 135 | } |
michael@0 | 136 | |
michael@0 | 137 | function setup_http_server() { |
michael@0 | 138 | // Start server; will be stopped at test cleanup time. |
michael@0 | 139 | server.start(-1); |
michael@0 | 140 | baseURL = "http://localhost:" + server.identity.primaryPort + "/"; |
michael@0 | 141 | do_print("Using baseURL: " + baseURL); |
michael@0 | 142 | server.registerPathHandler('/', function(metadata, response) { |
michael@0 | 143 | // Wait until the timeout should have passed, then respond. |
michael@0 | 144 | response.processAsync(); |
michael@0 | 145 | |
michael@0 | 146 | do_timeout((kResponseTimeout+1)*1000 /* ms */, function() { |
michael@0 | 147 | response.setStatusLine(metadata.httpVersion, 200, "OK"); |
michael@0 | 148 | response.write("Hello world"); |
michael@0 | 149 | response.finish(); |
michael@0 | 150 | }); |
michael@0 | 151 | }); |
michael@0 | 152 | do_register_cleanup(function() { |
michael@0 | 153 | server.stop(serverStopListener); |
michael@0 | 154 | }); |
michael@0 | 155 | } |
michael@0 | 156 | |
michael@0 | 157 | function run_test() { |
michael@0 | 158 | setup_http_server(); |
michael@0 | 159 | |
michael@0 | 160 | setup_tests(); |
michael@0 | 161 | |
michael@0 | 162 | run_next_test(); |
michael@0 | 163 | } |