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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
|
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "net/http/http_proxy_client_socket_pool.h"
#include "base/callback.h"
#include "base/compiler_specific.h"
#include "base/string_util.h"
#include "base/utf_string_conversions.h"
#include "net/base/mock_host_resolver.h"
#include "net/base/net_errors.h"
#include "net/base/ssl_config_service_defaults.h"
#include "net/base/test_completion_callback.h"
#include "net/http/http_auth_handler_factory.h"
#include "net/http/http_network_session.h"
#include "net/http/http_proxy_client_socket.h"
#include "net/proxy/proxy_service.h"
#include "net/socket/client_socket_handle.h"
#include "net/socket/client_socket_pool_histograms.h"
#include "net/socket/socket_test_util.h"
#include "net/spdy/spdy_session_pool.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace net {
namespace {
const int kMaxSockets = 32;
const int kMaxSocketsPerGroup = 6;
enum HttpProxyType {
HTTP,
HTTPS
};
typedef ::testing::TestWithParam<HttpProxyType> TestWithHttpParam;
class HttpProxyClientSocketPoolTest : public TestWithHttpParam {
protected:
HttpProxyClientSocketPoolTest()
: ssl_config_(),
ignored_tcp_socket_params_(new TCPSocketParams(
HostPortPair("proxy", 80), MEDIUM, GURL(), false)),
ignored_ssl_socket_params_(new SSLSocketParams(
ignored_tcp_socket_params_, NULL, NULL, ProxyServer::SCHEME_DIRECT,
"host", ssl_config_, 0, false, false)),
tcp_histograms_("MockTCP"),
tcp_socket_pool_(
kMaxSockets, kMaxSocketsPerGroup,
&tcp_histograms_,
&tcp_client_socket_factory_),
ssl_histograms_("MockSSL"),
ssl_socket_pool_(kMaxSockets, kMaxSocketsPerGroup,
&ssl_histograms_,
&tcp_client_socket_factory_,
&tcp_socket_pool_),
host_resolver_(new MockHostResolver),
http_auth_handler_factory_(
HttpAuthHandlerFactory::CreateDefault(host_resolver_.get())),
session_(new HttpNetworkSession(host_resolver_.get(),
NULL /* dnsrr_resolver */,
ProxyService::CreateDirect(),
&socket_factory_,
new SSLConfigServiceDefaults,
new SpdySessionPool(NULL),
http_auth_handler_factory_.get(),
NULL,
NULL)),
http_proxy_histograms_("HttpProxyUnitTest"),
pool_(kMaxSockets, kMaxSocketsPerGroup,
&http_proxy_histograms_,
NULL,
&tcp_socket_pool_,
&ssl_socket_pool_,
NULL) {
}
virtual ~HttpProxyClientSocketPoolTest() {
}
void AddAuthToCache() {
const string16 kFoo(ASCIIToUTF16("foo"));
const string16 kBar(ASCIIToUTF16("bar"));
session_->auth_cache()->Add(GURL("http://proxy/"), "MyRealm1", "Basic",
"Basic realm=MyRealm1", kFoo, kBar, "/");
}
scoped_refptr<TCPSocketParams> GetTcpParams() {
if (GetParam() == HTTPS)
return scoped_refptr<TCPSocketParams>();
return ignored_tcp_socket_params_;
}
scoped_refptr<SSLSocketParams> GetSslParams() {
if (GetParam() == HTTP)
return scoped_refptr<SSLSocketParams>();
return ignored_ssl_socket_params_;
}
// Returns the a correctly constructed HttpProxyParms
// for the HTTP or HTTPS proxy.
scoped_refptr<HttpProxySocketParams> GetParams(bool tunnel) {
return scoped_refptr<HttpProxySocketParams>(
new HttpProxySocketParams(
GetTcpParams(),
GetSslParams(),
GURL("http://host/"),
"",
HostPortPair("host", 80),
session_->auth_cache(),
session_->http_auth_handler_factory(),
tunnel));
}
scoped_refptr<HttpProxySocketParams> GetTunnelParams() {
return GetParams(true);
}
scoped_refptr<HttpProxySocketParams> GetNoTunnelParams() {
return GetParams(false);
}
SSLConfig ssl_config_;
scoped_refptr<TCPSocketParams> ignored_tcp_socket_params_;
scoped_refptr<SSLSocketParams> ignored_ssl_socket_params_;
ClientSocketPoolHistograms tcp_histograms_;
MockClientSocketFactory tcp_client_socket_factory_;
MockTCPClientSocketPool tcp_socket_pool_;
ClientSocketPoolHistograms ssl_histograms_;
MockSSLClientSocketPool ssl_socket_pool_;
MockClientSocketFactory socket_factory_;
scoped_ptr<HostResolver> host_resolver_;
scoped_ptr<HttpAuthHandlerFactory> http_auth_handler_factory_;
scoped_refptr<HttpNetworkSession> session_;
ClientSocketPoolHistograms http_proxy_histograms_;
HttpProxyClientSocketPool pool_;
};
//-----------------------------------------------------------------------------
// All tests are run with three different connection types: SPDY after NPN
// negotiation, SPDY without SSL, and SPDY with SSL.
INSTANTIATE_TEST_CASE_P(HttpProxyClientSocketPoolTests,
HttpProxyClientSocketPoolTest,
::testing::Values(HTTP, HTTPS));
TEST_P(HttpProxyClientSocketPoolTest, NoTunnel) {
StaticSocketDataProvider data;
data.set_connect_data(MockConnect(false, 0));
tcp_client_socket_factory_.AddSocketDataProvider(&data);
ClientSocketHandle handle;
int rv = handle.Init("a", GetNoTunnelParams(), LOW, NULL, &pool_,
BoundNetLog());
EXPECT_EQ(OK, rv);
EXPECT_TRUE(handle.is_initialized());
EXPECT_TRUE(handle.socket());
HttpProxyClientSocket* tunnel_socket =
static_cast<HttpProxyClientSocket*>(handle.socket());
EXPECT_TRUE(tunnel_socket->IsConnected());
}
TEST_P(HttpProxyClientSocketPoolTest, NeedAuth) {
MockWrite writes[] = {
MockWrite("CONNECT host:80 HTTP/1.1\r\n"
"Host: host\r\n"
"Proxy-Connection: keep-alive\r\n\r\n"),
};
MockRead reads[] = {
// No credentials.
MockRead("HTTP/1.1 407 Proxy Authentication Required\r\n"),
MockRead("Proxy-Authenticate: Basic realm=\"MyRealm1\"\r\n"),
MockRead("Content-Length: 10\r\n\r\n"),
MockRead("0123456789"),
};
StaticSocketDataProvider data(reads, arraysize(reads), writes,
arraysize(writes));
tcp_client_socket_factory_.AddSocketDataProvider(&data);
ClientSocketHandle handle;
TestCompletionCallback callback;
int rv = handle.Init("a", GetTunnelParams(), LOW, &callback, &pool_,
BoundNetLog());
EXPECT_EQ(ERR_IO_PENDING, rv);
EXPECT_FALSE(handle.is_initialized());
EXPECT_FALSE(handle.socket());
EXPECT_EQ(ERR_PROXY_AUTH_REQUESTED, callback.WaitForResult());
EXPECT_TRUE(handle.is_initialized());
EXPECT_TRUE(handle.socket());
HttpProxyClientSocket* tunnel_socket =
static_cast<HttpProxyClientSocket*>(handle.socket());
EXPECT_FALSE(tunnel_socket->IsConnected());
}
TEST_P(HttpProxyClientSocketPoolTest, HaveAuth) {
MockWrite writes[] = {
MockWrite(false,
"CONNECT host:80 HTTP/1.1\r\n"
"Host: host\r\n"
"Proxy-Connection: keep-alive\r\n"
"Proxy-Authorization: Basic Zm9vOmJhcg==\r\n\r\n"),
};
MockRead reads[] = {
MockRead(false, "HTTP/1.1 200 Connection Established\r\n\r\n"),
};
StaticSocketDataProvider data(reads, arraysize(reads), writes,
arraysize(writes));
data.set_connect_data(MockConnect(false, 0));
tcp_client_socket_factory_.AddSocketDataProvider(&data);
AddAuthToCache();
ClientSocketHandle handle;
TestCompletionCallback callback;
int rv = handle.Init("a", GetTunnelParams(), LOW, &callback, &pool_,
BoundNetLog());
EXPECT_EQ(OK, rv);
EXPECT_TRUE(handle.is_initialized());
EXPECT_TRUE(handle.socket());
HttpProxyClientSocket* tunnel_socket =
static_cast<HttpProxyClientSocket*>(handle.socket());
EXPECT_TRUE(tunnel_socket->IsConnected());
}
TEST_P(HttpProxyClientSocketPoolTest, AsyncHaveAuth) {
MockWrite writes[] = {
MockWrite("CONNECT host:80 HTTP/1.1\r\n"
"Host: host\r\n"
"Proxy-Connection: keep-alive\r\n"
"Proxy-Authorization: Basic Zm9vOmJhcg==\r\n\r\n"),
};
MockRead reads[] = {
MockRead("HTTP/1.1 200 Connection Established\r\n\r\n"),
};
StaticSocketDataProvider data(reads, arraysize(reads), writes,
arraysize(writes));
tcp_client_socket_factory_.AddSocketDataProvider(&data);
AddAuthToCache();
ClientSocketHandle handle;
TestCompletionCallback callback;
int rv = handle.Init("a", GetTunnelParams(), LOW, &callback, &pool_,
BoundNetLog());
EXPECT_EQ(ERR_IO_PENDING, rv);
EXPECT_FALSE(handle.is_initialized());
EXPECT_FALSE(handle.socket());
EXPECT_EQ(OK, callback.WaitForResult());
EXPECT_TRUE(handle.is_initialized());
EXPECT_TRUE(handle.socket());
HttpProxyClientSocket* tunnel_socket =
static_cast<HttpProxyClientSocket*>(handle.socket());
EXPECT_TRUE(tunnel_socket->IsConnected());
}
TEST_P(HttpProxyClientSocketPoolTest, TCPError) {
StaticSocketDataProvider data;
data.set_connect_data(MockConnect(true, ERR_CONNECTION_CLOSED));
tcp_client_socket_factory_.AddSocketDataProvider(&data);
ClientSocketHandle handle;
TestCompletionCallback callback;
int rv = handle.Init("a", GetTunnelParams(), LOW, &callback, &pool_,
BoundNetLog());
EXPECT_EQ(ERR_IO_PENDING, rv);
EXPECT_FALSE(handle.is_initialized());
EXPECT_FALSE(handle.socket());
if (GetParam() == HTTP)
EXPECT_EQ(ERR_PROXY_CONNECTION_FAILED, callback.WaitForResult());
else
EXPECT_EQ(ERR_CONNECTION_CLOSED, callback.WaitForResult());
EXPECT_FALSE(handle.is_initialized());
EXPECT_FALSE(handle.socket());
}
TEST_P(HttpProxyClientSocketPoolTest, TunnelUnexpectedClose) {
MockWrite writes[] = {
MockWrite("CONNECT host:80 HTTP/1.1\r\n"
"Host: host\r\n"
"Proxy-Connection: keep-alive\r\n"
"Proxy-Authorization: Basic Zm9vOmJhcg==\r\n\r\n"),
};
MockRead reads[] = {
MockRead("HTTP/1.1 200 Conn"),
MockRead(true, ERR_CONNECTION_CLOSED),
};
StaticSocketDataProvider data(reads, arraysize(reads), writes,
arraysize(writes));
tcp_client_socket_factory_.AddSocketDataProvider(&data);
AddAuthToCache();
ClientSocketHandle handle;
TestCompletionCallback callback;
int rv = handle.Init("a", GetTunnelParams(), LOW, &callback, &pool_,
BoundNetLog());
EXPECT_EQ(ERR_IO_PENDING, rv);
EXPECT_FALSE(handle.is_initialized());
EXPECT_FALSE(handle.socket());
EXPECT_EQ(ERR_CONNECTION_CLOSED, callback.WaitForResult());
EXPECT_FALSE(handle.is_initialized());
EXPECT_FALSE(handle.socket());
}
TEST_P(HttpProxyClientSocketPoolTest, TunnelSetupError) {
MockWrite writes[] = {
MockWrite("CONNECT host:80 HTTP/1.1\r\n"
"Host: host\r\n"
"Proxy-Connection: keep-alive\r\n"
"Proxy-Authorization: Basic Zm9vOmJhcg==\r\n\r\n"),
};
MockRead reads[] = {
MockRead("HTTP/1.1 304 Not Modified\r\n\r\n"),
};
StaticSocketDataProvider data(reads, arraysize(reads), writes,
arraysize(writes));
tcp_client_socket_factory_.AddSocketDataProvider(&data);
AddAuthToCache();
ClientSocketHandle handle;
TestCompletionCallback callback;
int rv = handle.Init("a", GetTunnelParams(), LOW, &callback, &pool_,
BoundNetLog());
EXPECT_EQ(ERR_IO_PENDING, rv);
EXPECT_FALSE(handle.is_initialized());
EXPECT_FALSE(handle.socket());
EXPECT_EQ(ERR_TUNNEL_CONNECTION_FAILED, callback.WaitForResult());
EXPECT_FALSE(handle.is_initialized());
EXPECT_FALSE(handle.socket());
}
// It would be nice to also test the timeouts in HttpProxyClientSocketPool.
} // namespace
} // namespace net
|