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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
|
// Copyright (c) 2011 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_stream_factory_impl.h"
#include <string>
#include "base/basictypes.h"
#include "net/base/capturing_net_log.h"
#include "net/base/cert_verifier.h"
#include "net/base/mock_host_resolver.h"
#include "net/base/net_log.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_network_session_peer.h"
#include "net/http/http_request_info.h"
#include "net/http/http_server_properties_impl.h"
#include "net/http/http_stream.h"
#include "net/proxy/proxy_info.h"
#include "net/proxy/proxy_service.h"
#include "net/socket/socket_test_util.h"
#include "net/spdy/spdy_session.h"
#include "net/spdy/spdy_session_pool.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace net {
namespace {
class MockHttpStreamFactoryImpl : public HttpStreamFactoryImpl {
public:
MockHttpStreamFactoryImpl(HttpNetworkSession* session)
: HttpStreamFactoryImpl(session),
preconnect_done_(false),
waiting_for_preconnect_(false) {}
void WaitForPreconnects() {
while (!preconnect_done_) {
waiting_for_preconnect_ = true;
MessageLoop::current()->Run();
waiting_for_preconnect_ = false;
}
}
private:
// HttpStreamFactoryImpl methods.
virtual void OnPreconnectsCompleteInternal() {
preconnect_done_ = true;
if (waiting_for_preconnect_)
MessageLoop::current()->Quit();
}
bool preconnect_done_;
bool waiting_for_preconnect_;
};
class StreamRequestWaiter : public HttpStreamRequest::Delegate {
public:
StreamRequestWaiter()
: waiting_for_stream_(false),
stream_done_(false) {}
// HttpStreamRequest::Delegate
virtual void OnStreamReady(
const SSLConfig& used_ssl_config,
const ProxyInfo& used_proxy_info,
HttpStream* stream) OVERRIDE {
stream_done_ = true;
if (waiting_for_stream_)
MessageLoop::current()->Quit();
stream_.reset(stream);
}
virtual void OnStreamFailed(
int status,
const SSLConfig& used_ssl_config) OVERRIDE {}
virtual void OnCertificateError(
int status,
const SSLConfig& used_ssl_config,
const SSLInfo& ssl_info) OVERRIDE {}
virtual void OnNeedsProxyAuth(const HttpResponseInfo& proxy_response,
const SSLConfig& used_ssl_config,
const ProxyInfo& used_proxy_info,
HttpAuthController* auth_controller) OVERRIDE {}
virtual void OnNeedsClientAuth(const SSLConfig& used_ssl_config,
SSLCertRequestInfo* cert_info) OVERRIDE {}
virtual void OnHttpsProxyTunnelResponse(const HttpResponseInfo& response_info,
const SSLConfig& used_ssl_config,
const ProxyInfo& used_proxy_info,
HttpStream* stream) OVERRIDE {}
void WaitForStream() {
while (!stream_done_) {
waiting_for_stream_ = true;
MessageLoop::current()->Run();
waiting_for_stream_ = false;
}
}
private:
bool waiting_for_stream_;
bool stream_done_;
scoped_ptr<HttpStream> stream_;
DISALLOW_COPY_AND_ASSIGN(StreamRequestWaiter);
};
struct SessionDependencies {
// Custom proxy service dependency.
explicit SessionDependencies(ProxyService* proxy_service)
: host_resolver(new MockHostResolver),
cert_verifier(new CertVerifier),
proxy_service(proxy_service),
ssl_config_service(new SSLConfigServiceDefaults),
http_auth_handler_factory(
HttpAuthHandlerFactory::CreateDefault(host_resolver.get())),
net_log(NULL) {}
scoped_ptr<MockHostResolverBase> host_resolver;
scoped_ptr<CertVerifier> cert_verifier;
scoped_ptr<ProxyService> proxy_service;
scoped_refptr<SSLConfigService> ssl_config_service;
MockClientSocketFactory socket_factory;
scoped_ptr<HttpAuthHandlerFactory> http_auth_handler_factory;
HttpServerPropertiesImpl http_server_properties;
NetLog* net_log;
};
HttpNetworkSession* CreateSession(SessionDependencies* session_deps) {
HttpNetworkSession::Params params;
params.host_resolver = session_deps->host_resolver.get();
params.cert_verifier = session_deps->cert_verifier.get();
params.proxy_service = session_deps->proxy_service.get();
params.ssl_config_service = session_deps->ssl_config_service;
params.client_socket_factory = &session_deps->socket_factory;
params.http_auth_handler_factory =
session_deps->http_auth_handler_factory.get();
params.net_log = session_deps->net_log;
params.http_server_properties = &session_deps->http_server_properties;
return new HttpNetworkSession(params);
}
struct TestCase {
int num_streams;
bool ssl;
};
TestCase kTests[] = {
{ 1, false },
{ 2, false },
{ 1, true},
{ 2, true},
};
void PreconnectHelper(const TestCase& test,
HttpNetworkSession* session) {
HttpNetworkSessionPeer peer(session);
MockHttpStreamFactoryImpl* mock_factory =
new MockHttpStreamFactoryImpl(session);
peer.SetHttpStreamFactory(mock_factory);
SSLConfig ssl_config;
session->ssl_config_service()->GetSSLConfig(&ssl_config);
HttpRequestInfo request;
request.method = "GET";
request.url = test.ssl ? GURL("https://www.google.com") :
GURL("http://www.google.com");
request.load_flags = 0;
ProxyInfo proxy_info;
TestOldCompletionCallback callback;
session->http_stream_factory()->PreconnectStreams(
test.num_streams, request, ssl_config, ssl_config, BoundNetLog());
mock_factory->WaitForPreconnects();
};
template<typename ParentPool>
class CapturePreconnectsSocketPool : public ParentPool {
public:
CapturePreconnectsSocketPool(HostResolver* host_resolver,
CertVerifier* cert_verifier);
int last_num_streams() const {
return last_num_streams_;
}
virtual int RequestSocket(const std::string& group_name,
const void* socket_params,
RequestPriority priority,
ClientSocketHandle* handle,
OldCompletionCallback* callback,
const BoundNetLog& net_log) {
ADD_FAILURE();
return ERR_UNEXPECTED;
}
virtual void RequestSockets(const std::string& group_name,
const void* socket_params,
int num_sockets,
const BoundNetLog& net_log) {
last_num_streams_ = num_sockets;
}
virtual void CancelRequest(const std::string& group_name,
ClientSocketHandle* handle) {
ADD_FAILURE();
}
virtual void ReleaseSocket(const std::string& group_name,
StreamSocket* socket,
int id) {
ADD_FAILURE();
}
virtual void CloseIdleSockets() {
ADD_FAILURE();
}
virtual int IdleSocketCount() const {
ADD_FAILURE();
return 0;
}
virtual int IdleSocketCountInGroup(const std::string& group_name) const {
ADD_FAILURE();
return 0;
}
virtual LoadState GetLoadState(const std::string& group_name,
const ClientSocketHandle* handle) const {
ADD_FAILURE();
return LOAD_STATE_IDLE;
}
virtual base::TimeDelta ConnectionTimeout() const {
return base::TimeDelta();
}
private:
int last_num_streams_;
};
typedef CapturePreconnectsSocketPool<TransportClientSocketPool>
CapturePreconnectsTransportSocketPool;
typedef CapturePreconnectsSocketPool<HttpProxyClientSocketPool>
CapturePreconnectsHttpProxySocketPool;
typedef CapturePreconnectsSocketPool<SOCKSClientSocketPool>
CapturePreconnectsSOCKSSocketPool;
typedef CapturePreconnectsSocketPool<SSLClientSocketPool>
CapturePreconnectsSSLSocketPool;
template<typename ParentPool>
CapturePreconnectsSocketPool<ParentPool>::CapturePreconnectsSocketPool(
HostResolver* host_resolver, CertVerifier* /* cert_verifier */)
: ParentPool(0, 0, NULL, host_resolver, NULL, NULL),
last_num_streams_(-1) {}
template<>
CapturePreconnectsHttpProxySocketPool::CapturePreconnectsSocketPool(
HostResolver* host_resolver, CertVerifier* /* cert_verifier */)
: HttpProxyClientSocketPool(0, 0, NULL, host_resolver, NULL, NULL, NULL),
last_num_streams_(-1) {}
template<>
CapturePreconnectsSSLSocketPool::CapturePreconnectsSocketPool(
HostResolver* host_resolver, CertVerifier* cert_verifier)
: SSLClientSocketPool(0, 0, NULL, host_resolver, cert_verifier, NULL, NULL,
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL),
last_num_streams_(-1) {}
TEST(HttpStreamFactoryTest, PreconnectDirect) {
for (size_t i = 0; i < arraysize(kTests); ++i) {
SessionDependencies session_deps(ProxyService::CreateDirect());
scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps));
HttpNetworkSessionPeer peer(session);
CapturePreconnectsTransportSocketPool* transport_conn_pool =
new CapturePreconnectsTransportSocketPool(
session_deps.host_resolver.get(),
session_deps.cert_verifier.get());
peer.SetTransportSocketPool(transport_conn_pool);
CapturePreconnectsSSLSocketPool* ssl_conn_pool =
new CapturePreconnectsSSLSocketPool(
session_deps.host_resolver.get(),
session_deps.cert_verifier.get());
peer.SetSSLSocketPool(ssl_conn_pool);
PreconnectHelper(kTests[i], session);
if (kTests[i].ssl)
EXPECT_EQ(kTests[i].num_streams, ssl_conn_pool->last_num_streams());
else
EXPECT_EQ(kTests[i].num_streams, transport_conn_pool->last_num_streams());
}
}
TEST(HttpStreamFactoryTest, PreconnectHttpProxy) {
for (size_t i = 0; i < arraysize(kTests); ++i) {
SessionDependencies session_deps(ProxyService::CreateFixed("http_proxy"));
scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps));
HttpNetworkSessionPeer peer(session);
HostPortPair proxy_host("http_proxy", 80);
CapturePreconnectsHttpProxySocketPool* http_proxy_pool =
new CapturePreconnectsHttpProxySocketPool(
session_deps.host_resolver.get(),
session_deps.cert_verifier.get());
peer.SetSocketPoolForHTTPProxy(proxy_host, http_proxy_pool);
CapturePreconnectsSSLSocketPool* ssl_conn_pool =
new CapturePreconnectsSSLSocketPool(
session_deps.host_resolver.get(),
session_deps.cert_verifier.get());
peer.SetSocketPoolForSSLWithProxy(proxy_host, ssl_conn_pool);
PreconnectHelper(kTests[i], session);
if (kTests[i].ssl)
EXPECT_EQ(kTests[i].num_streams, ssl_conn_pool->last_num_streams());
else
EXPECT_EQ(kTests[i].num_streams, http_proxy_pool->last_num_streams());
}
}
TEST(HttpStreamFactoryTest, PreconnectSocksProxy) {
for (size_t i = 0; i < arraysize(kTests); ++i) {
SessionDependencies session_deps(
ProxyService::CreateFixed("socks4://socks_proxy:1080"));
scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps));
HttpNetworkSessionPeer peer(session);
HostPortPair proxy_host("socks_proxy", 1080);
CapturePreconnectsSOCKSSocketPool* socks_proxy_pool =
new CapturePreconnectsSOCKSSocketPool(
session_deps.host_resolver.get(),
session_deps.cert_verifier.get());
peer.SetSocketPoolForSOCKSProxy(proxy_host, socks_proxy_pool);
CapturePreconnectsSSLSocketPool* ssl_conn_pool =
new CapturePreconnectsSSLSocketPool(
session_deps.host_resolver.get(),
session_deps.cert_verifier.get());
peer.SetSocketPoolForSSLWithProxy(proxy_host, ssl_conn_pool);
PreconnectHelper(kTests[i], session);
if (kTests[i].ssl)
EXPECT_EQ(kTests[i].num_streams, ssl_conn_pool->last_num_streams());
else
EXPECT_EQ(kTests[i].num_streams, socks_proxy_pool->last_num_streams());
}
}
TEST(HttpStreamFactoryTest, PreconnectDirectWithExistingSpdySession) {
for (size_t i = 0; i < arraysize(kTests); ++i) {
SessionDependencies session_deps(ProxyService::CreateDirect());
scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps));
HttpNetworkSessionPeer peer(session);
// Set an existing SpdySession in the pool.
HostPortPair host_port_pair("www.google.com", 443);
HostPortProxyPair pair(host_port_pair, ProxyServer::Direct());
scoped_refptr<SpdySession> spdy_session =
session->spdy_session_pool()->Get(pair, BoundNetLog());
CapturePreconnectsTransportSocketPool* transport_conn_pool =
new CapturePreconnectsTransportSocketPool(
session_deps.host_resolver.get(),
session_deps.cert_verifier.get());
peer.SetTransportSocketPool(transport_conn_pool);
CapturePreconnectsSSLSocketPool* ssl_conn_pool =
new CapturePreconnectsSSLSocketPool(
session_deps.host_resolver.get(),
session_deps.cert_verifier.get());
peer.SetSSLSocketPool(ssl_conn_pool);
PreconnectHelper(kTests[i], session);
// We shouldn't be preconnecting if we have an existing session, which is
// the case for https://www.google.com.
if (kTests[i].ssl)
EXPECT_EQ(-1, ssl_conn_pool->last_num_streams());
else
EXPECT_EQ(kTests[i].num_streams, transport_conn_pool->last_num_streams());
}
}
TEST(HttpStreamFactoryTest, JobNotifiesProxy) {
const char* kProxyString = "PROXY bad:99; PROXY maybe:80; DIRECT";
SessionDependencies session_deps(
ProxyService::CreateFixedFromPacResult(kProxyString));
// First connection attempt fails
StaticSocketDataProvider socket_data1;
socket_data1.set_connect_data(MockConnect(true, ERR_ADDRESS_UNREACHABLE));
session_deps.socket_factory.AddSocketDataProvider(&socket_data1);
// Second connection attempt succeeds
StaticSocketDataProvider socket_data2;
socket_data2.set_connect_data(MockConnect(true, OK));
session_deps.socket_factory.AddSocketDataProvider(&socket_data2);
CapturingBoundNetLog log(CapturingNetLog::kUnbounded);
EXPECT_TRUE(log.bound().IsLoggingAllEvents());
scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps));
// Now request a stream. It should succeed using the second proxy in the
// list.
HttpRequestInfo request_info;
request_info.method = "GET";
request_info.url = GURL("http://www.google.com");
request_info.load_flags = 0;
SSLConfig ssl_config;
StreamRequestWaiter waiter;
scoped_ptr<HttpStreamRequest> request(
session->http_stream_factory()->RequestStream(request_info, ssl_config,
ssl_config, &waiter,
log.bound()));
waiter.WaitForStream();
// The proxy that failed should now be known to the proxy_service as bad.
const ProxyRetryInfoMap& retry_info =
session->proxy_service()->proxy_retry_info();
EXPECT_EQ(1u, retry_info.size());
ProxyRetryInfoMap::const_iterator iter = retry_info.find("bad:99");
EXPECT_TRUE(iter != retry_info.end());
}
} // namespace
} // namespace net
|