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