1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "net/quic/quic_stream_factory.h"
7 #include "base/run_loop.h"
8 #include "base/strings/string_util.h"
9 #include "net/base/test_data_directory.h"
10 #include "net/cert/cert_verifier.h"
11 #include "net/dns/mock_host_resolver.h"
12 #include "net/http/http_response_headers.h"
13 #include "net/http/http_response_info.h"
14 #include "net/http/http_util.h"
15 #include "net/quic/crypto/crypto_handshake.h"
16 #include "net/quic/crypto/quic_decrypter.h"
17 #include "net/quic/crypto/quic_encrypter.h"
18 #include "net/quic/quic_http_stream.h"
19 #include "net/quic/test_tools/mock_clock.h"
20 #include "net/quic/test_tools/mock_crypto_client_stream_factory.h"
21 #include "net/quic/test_tools/mock_random.h"
22 #include "net/quic/test_tools/quic_test_packet_maker.h"
23 #include "net/quic/test_tools/quic_test_utils.h"
24 #include "net/socket/socket_test_util.h"
25 #include "net/test/cert_test_util.h"
26 #include "testing/gtest/include/gtest/gtest.h"
28 using base::StringPiece
;
36 const char kDefaultServerHostName
[] = "www.google.com";
37 const int kDefaultServerPort
= 443;
38 } // namespace anonymous
40 class QuicStreamFactoryPeer
{
42 static QuicCryptoClientConfig
* GetOrCreateCryptoConfig(
43 QuicStreamFactory
* factory
,
44 const HostPortProxyPair
& host_port_proxy_pair
) {
45 return factory
->GetOrCreateCryptoConfig(host_port_proxy_pair
);
48 static bool HasActiveSession(QuicStreamFactory
* factory
,
49 const HostPortProxyPair
& host_port_proxy_pair
) {
50 return factory
->HasActiveSession(host_port_proxy_pair
);
53 static QuicClientSession
* GetActiveSession(
54 QuicStreamFactory
* factory
,
55 const HostPortProxyPair
& host_port_proxy_pair
) {
56 DCHECK(factory
->HasActiveSession(host_port_proxy_pair
));
57 return factory
->active_sessions_
[host_port_proxy_pair
];
60 static bool IsLiveSession(QuicStreamFactory
* factory
,
61 QuicClientSession
* session
) {
62 for (QuicStreamFactory::SessionSet::iterator it
=
63 factory
->all_sessions_
.begin();
64 it
!= factory
->all_sessions_
.end(); ++it
) {
72 class QuicStreamFactoryTest
: public ::testing::TestWithParam
<QuicVersion
> {
74 QuicStreamFactoryTest()
75 : random_generator_(0),
76 maker_(GetParam(), 0),
77 clock_(new MockClock()),
78 factory_(&host_resolver_
, &socket_factory_
,
79 base::WeakPtr
<HttpServerProperties
>(),
80 &crypto_client_stream_factory_
,
81 &random_generator_
, clock_
, kDefaultMaxPacketSize
,
82 SupportedVersions(GetParam())),
83 host_port_proxy_pair_(HostPortPair(kDefaultServerHostName
,
85 ProxyServer::Direct()),
87 cert_verifier_(CertVerifier::CreateDefault()) {
88 factory_
.set_require_confirmation(false);
92 int GetSourcePortForNewSession(const HostPortProxyPair
& destination
) {
93 // Should only be called if there is no active session for this destination.
94 EXPECT_EQ(NULL
, factory_
.CreateIfSessionExists(destination
,
96 size_t socket_count
= socket_factory_
.udp_client_sockets().size();
99 MockRead(ASYNC
, OK
, 0) // EOF
101 DeterministicSocketData
socket_data(reads
, arraysize(reads
), NULL
, 0);
102 socket_data
.StopAfter(1);
103 socket_factory_
.AddSocketDataProvider(&socket_data
);
105 QuicStreamRequest
request(&factory_
);
106 EXPECT_EQ(ERR_IO_PENDING
, request
.Request(destination
, is_https_
,
107 cert_verifier_
.get(), net_log_
,
108 callback_
.callback()));
110 EXPECT_EQ(OK
, callback_
.WaitForResult());
111 scoped_ptr
<QuicHttpStream
> stream
= request
.ReleaseStream();
112 EXPECT_TRUE(stream
.get());
115 QuicClientSession
* session
= QuicStreamFactoryPeer::GetActiveSession(
116 &factory_
, destination
);
118 if (socket_count
+ 1 != socket_factory_
.udp_client_sockets().size()) {
125 udp_client_sockets()[socket_count
]->GetLocalAddress(&endpoint
);
126 int port
= endpoint
.port();
128 factory_
.OnSessionClosed(session
);
129 EXPECT_EQ(NULL
, factory_
.CreateIfSessionExists(destination
,
131 EXPECT_TRUE(socket_data
.at_read_eof());
132 EXPECT_TRUE(socket_data
.at_write_eof());
136 MockHostResolver host_resolver_
;
137 DeterministicMockClientSocketFactory socket_factory_
;
138 MockCryptoClientStreamFactory crypto_client_stream_factory_
;
139 MockRandom random_generator_
;
140 QuicTestPacketMaker maker_
;
141 MockClock
* clock_
; // Owned by factory_.
142 QuicStreamFactory factory_
;
143 HostPortProxyPair host_port_proxy_pair_
;
145 scoped_ptr
<CertVerifier
> cert_verifier_
;
146 BoundNetLog net_log_
;
147 TestCompletionCallback callback_
;
150 INSTANTIATE_TEST_CASE_P(Version
, QuicStreamFactoryTest
,
151 ::testing::ValuesIn(QuicSupportedVersions()));
153 TEST_P(QuicStreamFactoryTest
, CreateIfSessionExists
) {
154 EXPECT_EQ(NULL
, factory_
.CreateIfSessionExists(host_port_proxy_pair_
,
158 TEST_P(QuicStreamFactoryTest
, Create
) {
160 MockRead(ASYNC
, OK
, 0) // EOF
162 DeterministicSocketData
socket_data(reads
, arraysize(reads
), NULL
, 0);
163 socket_factory_
.AddSocketDataProvider(&socket_data
);
164 socket_data
.StopAfter(1);
166 QuicStreamRequest
request(&factory_
);
167 EXPECT_EQ(ERR_IO_PENDING
, request
.Request(host_port_proxy_pair_
, is_https_
,
168 cert_verifier_
.get(), net_log_
,
169 callback_
.callback()));
171 EXPECT_EQ(OK
, callback_
.WaitForResult());
172 scoped_ptr
<QuicHttpStream
> stream
= request
.ReleaseStream();
173 EXPECT_TRUE(stream
.get());
175 // Will reset stream 3.
176 stream
= factory_
.CreateIfSessionExists(host_port_proxy_pair_
, net_log_
);
177 EXPECT_TRUE(stream
.get());
179 // TODO(rtenneti): We should probably have a tests that HTTP and HTTPS result
180 // in streams on different sessions.
181 QuicStreamRequest
request2(&factory_
);
182 EXPECT_EQ(OK
, request2
.Request(host_port_proxy_pair_
, is_https_
,
183 cert_verifier_
.get(), net_log_
,
184 callback_
.callback()));
185 stream
= request2
.ReleaseStream(); // Will reset stream 5.
186 stream
.reset(); // Will reset stream 7.
188 EXPECT_TRUE(socket_data
.at_read_eof());
189 EXPECT_TRUE(socket_data
.at_write_eof());
192 TEST_P(QuicStreamFactoryTest
, Pooling
) {
194 MockRead(ASYNC
, OK
, 0) // EOF
196 DeterministicSocketData
socket_data(reads
, arraysize(reads
), NULL
, 0);
197 socket_factory_
.AddSocketDataProvider(&socket_data
);
198 socket_data
.StopAfter(1);
200 HostPortProxyPair server2
= HostPortProxyPair(
201 HostPortPair("mail.google.com", kDefaultServerPort
),
202 host_port_proxy_pair_
.second
);
204 host_resolver_
.set_synchronous_mode(true);
205 host_resolver_
.rules()->AddIPLiteralRule(
206 kDefaultServerHostName
, "192.168.0.1", "");
207 host_resolver_
.rules()->AddIPLiteralRule(
208 "mail.google.com", "192.168.0.1", "");
210 QuicStreamRequest
request(&factory_
);
211 EXPECT_EQ(OK
, request
.Request(host_port_proxy_pair_
, is_https_
,
212 cert_verifier_
.get(), net_log_
,
213 callback_
.callback()));
214 scoped_ptr
<QuicHttpStream
> stream
= request
.ReleaseStream();
215 EXPECT_TRUE(stream
.get());
217 TestCompletionCallback callback
;
218 QuicStreamRequest
request2(&factory_
);
219 EXPECT_EQ(OK
, request2
.Request(server2
, is_https_
,
220 cert_verifier_
.get(), net_log_
,
221 callback
.callback()));
222 scoped_ptr
<QuicHttpStream
> stream2
= request2
.ReleaseStream();
223 EXPECT_TRUE(stream2
.get());
226 QuicStreamFactoryPeer::GetActiveSession(&factory_
, host_port_proxy_pair_
),
227 QuicStreamFactoryPeer::GetActiveSession(&factory_
, server2
));
229 EXPECT_TRUE(socket_data
.at_read_eof());
230 EXPECT_TRUE(socket_data
.at_write_eof());
233 TEST_P(QuicStreamFactoryTest
, NoPoolingAfterGoAway
) {
235 MockRead(ASYNC
, OK
, 0) // EOF
237 DeterministicSocketData
socket_data1(reads
, arraysize(reads
), NULL
, 0);
238 DeterministicSocketData
socket_data2(reads
, arraysize(reads
), NULL
, 0);
239 socket_factory_
.AddSocketDataProvider(&socket_data1
);
240 socket_factory_
.AddSocketDataProvider(&socket_data2
);
241 socket_data1
.StopAfter(1);
242 socket_data2
.StopAfter(1);
244 HostPortProxyPair server2
= HostPortProxyPair(
245 HostPortPair("mail.google.com", kDefaultServerPort
),
246 host_port_proxy_pair_
.second
);
248 host_resolver_
.set_synchronous_mode(true);
249 host_resolver_
.rules()->AddIPLiteralRule(
250 kDefaultServerHostName
, "192.168.0.1", "");
251 host_resolver_
.rules()->AddIPLiteralRule(
252 "mail.google.com", "192.168.0.1", "");
254 QuicStreamRequest
request(&factory_
);
255 EXPECT_EQ(OK
, request
.Request(host_port_proxy_pair_
, is_https_
,
256 cert_verifier_
.get(), net_log_
,
257 callback_
.callback()));
258 scoped_ptr
<QuicHttpStream
> stream
= request
.ReleaseStream();
259 EXPECT_TRUE(stream
.get());
261 TestCompletionCallback callback
;
262 QuicStreamRequest
request2(&factory_
);
263 EXPECT_EQ(OK
, request2
.Request(server2
, is_https_
,
264 cert_verifier_
.get(), net_log_
,
265 callback
.callback()));
266 scoped_ptr
<QuicHttpStream
> stream2
= request2
.ReleaseStream();
267 EXPECT_TRUE(stream2
.get());
269 factory_
.OnSessionGoingAway(
270 QuicStreamFactoryPeer::GetActiveSession(&factory_
,
271 host_port_proxy_pair_
));
272 EXPECT_FALSE(QuicStreamFactoryPeer::HasActiveSession(&factory_
,
273 host_port_proxy_pair_
));
274 EXPECT_FALSE(QuicStreamFactoryPeer::HasActiveSession(&factory_
, server2
));
276 TestCompletionCallback callback3
;
277 QuicStreamRequest
request3(&factory_
);
278 EXPECT_EQ(OK
, request3
.Request(server2
, is_https_
,
279 cert_verifier_
.get(), net_log_
,
280 callback3
.callback()));
281 scoped_ptr
<QuicHttpStream
> stream3
= request3
.ReleaseStream();
282 EXPECT_TRUE(stream3
.get());
284 EXPECT_TRUE(QuicStreamFactoryPeer::HasActiveSession(&factory_
, server2
));
286 EXPECT_TRUE(socket_data1
.at_read_eof());
287 EXPECT_TRUE(socket_data1
.at_write_eof());
288 EXPECT_TRUE(socket_data2
.at_read_eof());
289 EXPECT_TRUE(socket_data2
.at_write_eof());
292 TEST_P(QuicStreamFactoryTest
, HttpsPooling
) {
294 MockRead(ASYNC
, OK
, 0) // EOF
296 DeterministicSocketData
socket_data(reads
, arraysize(reads
), NULL
, 0);
297 socket_factory_
.AddSocketDataProvider(&socket_data
);
298 socket_data
.StopAfter(1);
300 HostPortProxyPair
server1(HostPortPair("www.example.org", 443),
301 ProxyServer::Direct());
302 HostPortProxyPair
server2(HostPortPair("mail.example.org", 443),
303 ProxyServer::Direct());
305 // Load a cert that is valid for:
306 // www.example.org (server1)
307 // mail.example.org (server2)
309 base::FilePath certs_dir
= GetTestCertsDirectory();
310 scoped_refptr
<X509Certificate
> test_cert(
311 ImportCertFromFile(certs_dir
, "spdy_pooling.pem"));
312 ASSERT_NE(static_cast<X509Certificate
*>(NULL
), test_cert
);
314 ssl_info
.cert
= test_cert
.get();
315 crypto_client_stream_factory_
.set_ssl_info(&ssl_info
);
317 host_resolver_
.set_synchronous_mode(true);
318 host_resolver_
.rules()->AddIPLiteralRule(
319 server1
.first
.host(), "192.168.0.1", "");
320 host_resolver_
.rules()->AddIPLiteralRule(
321 server2
.first
.host(), "192.168.0.1", "");
323 QuicStreamRequest
request(&factory_
);
325 EXPECT_EQ(OK
, request
.Request(server1
, is_https_
, cert_verifier_
.get(),
326 net_log_
, callback_
.callback()));
327 scoped_ptr
<QuicHttpStream
> stream
= request
.ReleaseStream();
328 EXPECT_TRUE(stream
.get());
330 TestCompletionCallback callback
;
331 QuicStreamRequest
request2(&factory_
);
332 EXPECT_EQ(OK
, request2
.Request(server2
, is_https_
, cert_verifier_
.get(),
333 net_log_
, callback_
.callback()));
334 scoped_ptr
<QuicHttpStream
> stream2
= request2
.ReleaseStream();
335 EXPECT_TRUE(stream2
.get());
337 EXPECT_EQ(QuicStreamFactoryPeer::GetActiveSession(&factory_
, server1
),
338 QuicStreamFactoryPeer::GetActiveSession(&factory_
, server2
));
340 EXPECT_TRUE(socket_data
.at_read_eof());
341 EXPECT_TRUE(socket_data
.at_write_eof());
344 TEST_P(QuicStreamFactoryTest
, NoHttpsPoolingWithCertMismatch
) {
346 MockRead(ASYNC
, OK
, 0) // EOF
348 DeterministicSocketData
socket_data1(reads
, arraysize(reads
), NULL
, 0);
349 DeterministicSocketData
socket_data2(reads
, arraysize(reads
), NULL
, 0);
350 socket_factory_
.AddSocketDataProvider(&socket_data1
);
351 socket_factory_
.AddSocketDataProvider(&socket_data2
);
352 socket_data1
.StopAfter(1);
353 socket_data2
.StopAfter(1);
355 HostPortProxyPair
server1(HostPortPair("www.example.org", 443),
356 ProxyServer::Direct());
357 HostPortProxyPair
server2(HostPortPair("mail.google.com", 443),
358 ProxyServer::Direct());
360 // Load a cert that is valid for:
361 // www.example.org (server1)
364 // But is not valid for mail.google.com (server2).
365 base::FilePath certs_dir
= GetTestCertsDirectory();
366 scoped_refptr
<X509Certificate
> test_cert(
367 ImportCertFromFile(certs_dir
, "spdy_pooling.pem"));
368 ASSERT_NE(static_cast<X509Certificate
*>(NULL
), test_cert
);
370 ssl_info
.cert
= test_cert
.get();
371 crypto_client_stream_factory_
.set_ssl_info(&ssl_info
);
373 host_resolver_
.set_synchronous_mode(true);
374 host_resolver_
.rules()->AddIPLiteralRule(
375 server1
.first
.host(), "192.168.0.1", "");
376 host_resolver_
.rules()->AddIPLiteralRule(
377 server2
.first
.host(), "192.168.0.1", "");
379 QuicStreamRequest
request(&factory_
);
381 EXPECT_EQ(OK
, request
.Request(server1
, is_https_
, cert_verifier_
.get(),
382 net_log_
, callback_
.callback()));
383 scoped_ptr
<QuicHttpStream
> stream
= request
.ReleaseStream();
384 EXPECT_TRUE(stream
.get());
386 TestCompletionCallback callback
;
387 QuicStreamRequest
request2(&factory_
);
388 EXPECT_EQ(OK
, request2
.Request(server2
, is_https_
, cert_verifier_
.get(),
389 net_log_
, callback_
.callback()));
390 scoped_ptr
<QuicHttpStream
> stream2
= request2
.ReleaseStream();
391 EXPECT_TRUE(stream2
.get());
393 EXPECT_NE(QuicStreamFactoryPeer::GetActiveSession(&factory_
, server1
),
394 QuicStreamFactoryPeer::GetActiveSession(&factory_
, server2
));
396 EXPECT_TRUE(socket_data1
.at_read_eof());
397 EXPECT_TRUE(socket_data1
.at_write_eof());
398 EXPECT_TRUE(socket_data2
.at_read_eof());
399 EXPECT_TRUE(socket_data2
.at_write_eof());
402 TEST_P(QuicStreamFactoryTest
, Goaway
) {
404 MockRead(ASYNC
, OK
, 0) // EOF
406 DeterministicSocketData
socket_data(reads
, arraysize(reads
), NULL
, 0);
407 socket_data
.StopAfter(1);
408 socket_factory_
.AddSocketDataProvider(&socket_data
);
409 DeterministicSocketData
socket_data2(reads
, arraysize(reads
), NULL
, 0);
410 socket_data2
.StopAfter(1);
411 socket_factory_
.AddSocketDataProvider(&socket_data2
);
413 QuicStreamRequest
request(&factory_
);
414 EXPECT_EQ(ERR_IO_PENDING
, request
.Request(host_port_proxy_pair_
, is_https_
,
415 cert_verifier_
.get(), net_log_
,
416 callback_
.callback()));
418 EXPECT_EQ(OK
, callback_
.WaitForResult());
419 scoped_ptr
<QuicHttpStream
> stream
= request
.ReleaseStream();
420 EXPECT_TRUE(stream
.get());
422 // Mark the session as going away. Ensure that while it is still alive
423 // that it is no longer active.
424 QuicClientSession
* session
= QuicStreamFactoryPeer::GetActiveSession(
425 &factory_
, host_port_proxy_pair_
);
426 factory_
.OnSessionGoingAway(session
);
427 EXPECT_EQ(true, QuicStreamFactoryPeer::IsLiveSession(&factory_
, session
));
428 EXPECT_FALSE(QuicStreamFactoryPeer::HasActiveSession(&factory_
,
429 host_port_proxy_pair_
));
430 EXPECT_EQ(NULL
, factory_
.CreateIfSessionExists(host_port_proxy_pair_
,
433 // Create a new request for the same destination and verify that a
434 // new session is created.
435 QuicStreamRequest
request2(&factory_
);
436 EXPECT_EQ(ERR_IO_PENDING
, request2
.Request(host_port_proxy_pair_
, is_https_
,
437 cert_verifier_
.get(), net_log_
,
438 callback_
.callback()));
439 EXPECT_EQ(OK
, callback_
.WaitForResult());
440 scoped_ptr
<QuicHttpStream
> stream2
= request2
.ReleaseStream();
441 EXPECT_TRUE(stream2
.get());
443 EXPECT_TRUE(QuicStreamFactoryPeer::HasActiveSession(&factory_
,
444 host_port_proxy_pair_
));
446 QuicStreamFactoryPeer::GetActiveSession(
447 &factory_
, host_port_proxy_pair_
));
448 EXPECT_EQ(true, QuicStreamFactoryPeer::IsLiveSession(&factory_
, session
));
453 EXPECT_TRUE(socket_data
.at_read_eof());
454 EXPECT_TRUE(socket_data
.at_write_eof());
455 EXPECT_TRUE(socket_data2
.at_read_eof());
456 EXPECT_TRUE(socket_data2
.at_write_eof());
459 TEST_P(QuicStreamFactoryTest
, MaxOpenStream
) {
461 MockRead(ASYNC
, OK
, 0) // EOF
463 QuicStreamId stream_id
= GetParam() > QUIC_VERSION_12
? 5 : 3;
464 scoped_ptr
<QuicEncryptedPacket
> rst(
465 maker_
.MakeRstPacket(1, true, stream_id
, QUIC_STREAM_CANCELLED
));
466 MockWrite writes
[] = {
467 MockWrite(ASYNC
, rst
->data(), rst
->length(), 1),
469 DeterministicSocketData
socket_data(reads
, arraysize(reads
),
470 writes
, arraysize(writes
));
471 socket_factory_
.AddSocketDataProvider(&socket_data
);
472 socket_data
.StopAfter(1);
474 HttpRequestInfo request_info
;
475 std::vector
<QuicHttpStream
*> streams
;
476 // The MockCryptoClientStream sets max_open_streams to be
477 // 2 * kDefaultMaxStreamsPerConnection.
478 for (size_t i
= 0; i
< 2 * kDefaultMaxStreamsPerConnection
; i
++) {
479 QuicStreamRequest
request(&factory_
);
480 int rv
= request
.Request(host_port_proxy_pair_
, is_https_
,
481 cert_verifier_
.get(), net_log_
,
482 callback_
.callback());
484 EXPECT_EQ(ERR_IO_PENDING
, rv
);
485 EXPECT_EQ(OK
, callback_
.WaitForResult());
489 scoped_ptr
<QuicHttpStream
> stream
= request
.ReleaseStream();
491 EXPECT_EQ(OK
, stream
->InitializeStream(
492 &request_info
, DEFAULT_PRIORITY
, net_log_
, CompletionCallback()));
493 streams
.push_back(stream
.release());
496 QuicStreamRequest
request(&factory_
);
497 EXPECT_EQ(OK
, request
.Request(host_port_proxy_pair_
, is_https_
,
498 cert_verifier_
.get(), net_log_
,
499 CompletionCallback()));
500 scoped_ptr
<QuicHttpStream
> stream
= request
.ReleaseStream();
502 EXPECT_EQ(ERR_IO_PENDING
, stream
->InitializeStream(
503 &request_info
, DEFAULT_PRIORITY
, net_log_
, callback_
.callback()));
505 // Close the first stream.
506 streams
.front()->Close(false);
508 ASSERT_TRUE(callback_
.have_result());
510 EXPECT_EQ(OK
, callback_
.WaitForResult());
512 EXPECT_TRUE(socket_data
.at_read_eof());
513 EXPECT_TRUE(socket_data
.at_write_eof());
514 STLDeleteElements(&streams
);
517 TEST_P(QuicStreamFactoryTest
, ResolutionErrorInCreate
) {
518 DeterministicSocketData
socket_data(NULL
, 0, NULL
, 0);
519 socket_factory_
.AddSocketDataProvider(&socket_data
);
521 host_resolver_
.rules()->AddSimulatedFailure(kDefaultServerHostName
);
523 QuicStreamRequest
request(&factory_
);
524 EXPECT_EQ(ERR_IO_PENDING
, request
.Request(host_port_proxy_pair_
, is_https_
,
525 cert_verifier_
.get(), net_log_
,
526 callback_
.callback()));
528 EXPECT_EQ(ERR_NAME_NOT_RESOLVED
, callback_
.WaitForResult());
530 EXPECT_TRUE(socket_data
.at_read_eof());
531 EXPECT_TRUE(socket_data
.at_write_eof());
534 TEST_P(QuicStreamFactoryTest
, ConnectErrorInCreate
) {
535 MockConnect
connect(SYNCHRONOUS
, ERR_ADDRESS_IN_USE
);
536 DeterministicSocketData
socket_data(NULL
, 0, NULL
, 0);
537 socket_data
.set_connect_data(connect
);
538 socket_factory_
.AddSocketDataProvider(&socket_data
);
539 socket_data
.StopAfter(1);
541 QuicStreamRequest
request(&factory_
);
542 EXPECT_EQ(ERR_IO_PENDING
, request
.Request(host_port_proxy_pair_
, is_https_
,
543 cert_verifier_
.get(), net_log_
,
544 callback_
.callback()));
546 EXPECT_EQ(ERR_ADDRESS_IN_USE
, callback_
.WaitForResult());
548 EXPECT_TRUE(socket_data
.at_read_eof());
549 EXPECT_TRUE(socket_data
.at_write_eof());
552 TEST_P(QuicStreamFactoryTest
, CancelCreate
) {
554 MockRead(ASYNC
, OK
, 0) // EOF
556 DeterministicSocketData
socket_data(reads
, arraysize(reads
), NULL
, 0);
557 socket_factory_
.AddSocketDataProvider(&socket_data
);
559 QuicStreamRequest
request(&factory_
);
560 EXPECT_EQ(ERR_IO_PENDING
, request
.Request(host_port_proxy_pair_
, is_https_
,
561 cert_verifier_
.get(), net_log_
,
562 callback_
.callback()));
565 socket_data
.StopAfter(1);
566 base::RunLoop run_loop
;
567 run_loop
.RunUntilIdle();
569 scoped_ptr
<QuicHttpStream
> stream(
570 factory_
.CreateIfSessionExists(host_port_proxy_pair_
, net_log_
));
571 EXPECT_TRUE(stream
.get());
574 EXPECT_TRUE(socket_data
.at_read_eof());
575 EXPECT_TRUE(socket_data
.at_write_eof());
578 TEST_P(QuicStreamFactoryTest
, CreateConsistentEphemeralPort
) {
579 // Sequentially connect to the default host, then another host, and then the
580 // default host. Verify that the default host gets a consistent ephemeral
581 // port, that is different from the other host's connection.
583 std::string other_server_name
= "other.google.com";
584 EXPECT_NE(kDefaultServerHostName
, other_server_name
);
585 HostPortPair
host_port_pair2(other_server_name
, kDefaultServerPort
);
586 HostPortProxyPair
host_port_proxy_pair2(host_port_pair2
,
587 host_port_proxy_pair_
.second
);
589 int original_port
= GetSourcePortForNewSession(host_port_proxy_pair_
);
590 EXPECT_NE(original_port
, GetSourcePortForNewSession(host_port_proxy_pair2
));
591 EXPECT_EQ(original_port
, GetSourcePortForNewSession(host_port_proxy_pair_
));
594 TEST_P(QuicStreamFactoryTest
, CloseAllSessions
) {
596 MockRead(ASYNC
, 0, 0) // EOF
598 DeterministicSocketData
socket_data(reads
, arraysize(reads
), NULL
, 0);
599 socket_factory_
.AddSocketDataProvider(&socket_data
);
600 socket_data
.StopAfter(1);
602 MockRead reads2
[] = {
603 MockRead(ASYNC
, 0, 0) // EOF
605 DeterministicSocketData
socket_data2(reads2
, arraysize(reads2
), NULL
, 0);
606 socket_factory_
.AddSocketDataProvider(&socket_data2
);
607 socket_data2
.StopAfter(1);
609 QuicStreamRequest
request(&factory_
);
610 EXPECT_EQ(ERR_IO_PENDING
, request
.Request(host_port_proxy_pair_
, is_https_
,
611 cert_verifier_
.get(), net_log_
,
612 callback_
.callback()));
614 EXPECT_EQ(OK
, callback_
.WaitForResult());
615 scoped_ptr
<QuicHttpStream
> stream
= request
.ReleaseStream();
616 HttpRequestInfo request_info
;
617 EXPECT_EQ(OK
, stream
->InitializeStream(&request_info
,
619 net_log_
, CompletionCallback()));
621 // Close the session and verify that stream saw the error.
622 factory_
.CloseAllSessions(ERR_INTERNET_DISCONNECTED
);
623 EXPECT_EQ(ERR_INTERNET_DISCONNECTED
,
624 stream
->ReadResponseHeaders(callback_
.callback()));
626 // Now attempting to request a stream to the same origin should create
629 QuicStreamRequest
request2(&factory_
);
630 EXPECT_EQ(ERR_IO_PENDING
, request2
.Request(host_port_proxy_pair_
, is_https_
,
631 cert_verifier_
.get(), net_log_
,
632 callback_
.callback()));
634 EXPECT_EQ(OK
, callback_
.WaitForResult());
635 stream
= request2
.ReleaseStream();
636 stream
.reset(); // Will reset stream 3.
638 EXPECT_TRUE(socket_data
.at_read_eof());
639 EXPECT_TRUE(socket_data
.at_write_eof());
640 EXPECT_TRUE(socket_data2
.at_read_eof());
641 EXPECT_TRUE(socket_data2
.at_write_eof());
644 TEST_P(QuicStreamFactoryTest
, OnIPAddressChanged
) {
646 MockRead(ASYNC
, 0, 0) // EOF
648 DeterministicSocketData
socket_data(reads
, arraysize(reads
), NULL
, 0);
649 socket_factory_
.AddSocketDataProvider(&socket_data
);
650 socket_data
.StopAfter(1);
652 MockRead reads2
[] = {
653 MockRead(ASYNC
, 0, 0) // EOF
655 DeterministicSocketData
socket_data2(reads2
, arraysize(reads2
), NULL
, 0);
656 socket_factory_
.AddSocketDataProvider(&socket_data2
);
657 socket_data2
.StopAfter(1);
659 QuicStreamRequest
request(&factory_
);
660 EXPECT_EQ(ERR_IO_PENDING
, request
.Request(host_port_proxy_pair_
, is_https_
,
661 cert_verifier_
.get(), net_log_
,
662 callback_
.callback()));
664 EXPECT_EQ(OK
, callback_
.WaitForResult());
665 scoped_ptr
<QuicHttpStream
> stream
= request
.ReleaseStream();
666 HttpRequestInfo request_info
;
667 EXPECT_EQ(OK
, stream
->InitializeStream(&request_info
,
669 net_log_
, CompletionCallback()));
671 // Change the IP address and verify that stream saw the error.
672 factory_
.OnIPAddressChanged();
673 EXPECT_EQ(ERR_NETWORK_CHANGED
,
674 stream
->ReadResponseHeaders(callback_
.callback()));
675 EXPECT_TRUE(factory_
.require_confirmation());
677 // Now attempting to request a stream to the same origin should create
680 QuicStreamRequest
request2(&factory_
);
681 EXPECT_EQ(ERR_IO_PENDING
, request2
.Request(host_port_proxy_pair_
, is_https_
,
682 cert_verifier_
.get(), net_log_
,
683 callback_
.callback()));
685 EXPECT_EQ(OK
, callback_
.WaitForResult());
686 stream
= request2
.ReleaseStream();
687 stream
.reset(); // Will reset stream 3.
689 EXPECT_TRUE(socket_data
.at_read_eof());
690 EXPECT_TRUE(socket_data
.at_write_eof());
691 EXPECT_TRUE(socket_data2
.at_read_eof());
692 EXPECT_TRUE(socket_data2
.at_write_eof());
695 TEST_P(QuicStreamFactoryTest
, OnCertAdded
) {
697 MockRead(ASYNC
, 0, 0) // EOF
699 DeterministicSocketData
socket_data(reads
, arraysize(reads
), NULL
, 0);
700 socket_factory_
.AddSocketDataProvider(&socket_data
);
701 socket_data
.StopAfter(1);
703 MockRead reads2
[] = {
704 MockRead(ASYNC
, 0, 0) // EOF
706 DeterministicSocketData
socket_data2(reads2
, arraysize(reads2
), NULL
, 0);
707 socket_factory_
.AddSocketDataProvider(&socket_data2
);
708 socket_data2
.StopAfter(1);
710 QuicStreamRequest
request(&factory_
);
711 EXPECT_EQ(ERR_IO_PENDING
, request
.Request(host_port_proxy_pair_
, is_https_
,
712 cert_verifier_
.get(), net_log_
,
713 callback_
.callback()));
715 EXPECT_EQ(OK
, callback_
.WaitForResult());
716 scoped_ptr
<QuicHttpStream
> stream
= request
.ReleaseStream();
717 HttpRequestInfo request_info
;
718 EXPECT_EQ(OK
, stream
->InitializeStream(&request_info
,
720 net_log_
, CompletionCallback()));
722 // Add a cert and verify that stream saw the event.
723 factory_
.OnCertAdded(NULL
);
724 EXPECT_EQ(ERR_CERT_DATABASE_CHANGED
,
725 stream
->ReadResponseHeaders(callback_
.callback()));
726 EXPECT_FALSE(factory_
.require_confirmation());
728 // Now attempting to request a stream to the same origin should create
731 QuicStreamRequest
request2(&factory_
);
732 EXPECT_EQ(ERR_IO_PENDING
, request2
.Request(host_port_proxy_pair_
, is_https_
,
733 cert_verifier_
.get(), net_log_
,
734 callback_
.callback()));
736 EXPECT_EQ(OK
, callback_
.WaitForResult());
737 stream
= request2
.ReleaseStream();
738 stream
.reset(); // Will reset stream 3.
740 EXPECT_TRUE(socket_data
.at_read_eof());
741 EXPECT_TRUE(socket_data
.at_write_eof());
742 EXPECT_TRUE(socket_data2
.at_read_eof());
743 EXPECT_TRUE(socket_data2
.at_write_eof());
746 TEST_P(QuicStreamFactoryTest
, OnCACertChanged
) {
748 MockRead(ASYNC
, 0, 0) // EOF
750 DeterministicSocketData
socket_data(reads
, arraysize(reads
), NULL
, 0);
751 socket_factory_
.AddSocketDataProvider(&socket_data
);
752 socket_data
.StopAfter(1);
754 MockRead reads2
[] = {
755 MockRead(ASYNC
, 0, 0) // EOF
757 DeterministicSocketData
socket_data2(reads2
, arraysize(reads2
), NULL
, 0);
758 socket_factory_
.AddSocketDataProvider(&socket_data2
);
759 socket_data2
.StopAfter(1);
761 QuicStreamRequest
request(&factory_
);
762 EXPECT_EQ(ERR_IO_PENDING
, request
.Request(host_port_proxy_pair_
, is_https_
,
763 cert_verifier_
.get(), net_log_
,
764 callback_
.callback()));
766 EXPECT_EQ(OK
, callback_
.WaitForResult());
767 scoped_ptr
<QuicHttpStream
> stream
= request
.ReleaseStream();
768 HttpRequestInfo request_info
;
769 EXPECT_EQ(OK
, stream
->InitializeStream(&request_info
,
771 net_log_
, CompletionCallback()));
773 // Change the CA cert and verify that stream saw the event.
774 factory_
.OnCACertChanged(NULL
);
775 EXPECT_EQ(ERR_CERT_DATABASE_CHANGED
,
776 stream
->ReadResponseHeaders(callback_
.callback()));
777 EXPECT_FALSE(factory_
.require_confirmation());
779 // Now attempting to request a stream to the same origin should create
782 QuicStreamRequest
request2(&factory_
);
783 EXPECT_EQ(ERR_IO_PENDING
, request2
.Request(host_port_proxy_pair_
, is_https_
,
784 cert_verifier_
.get(), net_log_
,
785 callback_
.callback()));
787 EXPECT_EQ(OK
, callback_
.WaitForResult());
788 stream
= request2
.ReleaseStream();
789 stream
.reset(); // Will reset stream 3.
791 EXPECT_TRUE(socket_data
.at_read_eof());
792 EXPECT_TRUE(socket_data
.at_write_eof());
793 EXPECT_TRUE(socket_data2
.at_read_eof());
794 EXPECT_TRUE(socket_data2
.at_write_eof());
797 TEST_P(QuicStreamFactoryTest
, SharedCryptoConfig
) {
798 vector
<string
> cannoncial_suffixes
;
799 cannoncial_suffixes
.push_back(string(".c.youtube.com"));
800 cannoncial_suffixes
.push_back(string(".googlevideo.com"));
802 for (unsigned i
= 0; i
< cannoncial_suffixes
.size(); ++i
) {
803 string
r1_host_name("r1");
804 string
r2_host_name("r2");
805 r1_host_name
.append(cannoncial_suffixes
[i
]);
806 r2_host_name
.append(cannoncial_suffixes
[i
]);
808 HostPortProxyPair
host_port_proxy_pair1(HostPortPair(r1_host_name
, 80),
809 ProxyServer::Direct());
811 QuicCryptoClientConfig
* crypto_config1
=
812 QuicStreamFactoryPeer::GetOrCreateCryptoConfig(&factory_
,
813 host_port_proxy_pair1
);
814 DCHECK(crypto_config1
);
815 QuicCryptoClientConfig::CachedState
* cached1
=
816 crypto_config1
->LookupOrCreate(host_port_proxy_pair1
.first
.host());
817 EXPECT_FALSE(cached1
->proof_valid());
818 EXPECT_TRUE(cached1
->source_address_token().empty());
820 // Mutate the cached1 to have different data.
821 // TODO(rtenneti): mutate other members of CachedState.
822 cached1
->set_source_address_token(r1_host_name
);
823 cached1
->SetProofValid();
825 HostPortProxyPair
host_port_proxy_pair2(HostPortPair(r2_host_name
, 80),
826 ProxyServer::Direct());
827 QuicCryptoClientConfig
* crypto_config2
=
828 QuicStreamFactoryPeer::GetOrCreateCryptoConfig(&factory_
,
829 host_port_proxy_pair2
);
830 DCHECK(crypto_config2
);
831 QuicCryptoClientConfig::CachedState
* cached2
=
832 crypto_config2
->LookupOrCreate(host_port_proxy_pair2
.first
.host());
833 EXPECT_EQ(cached1
->source_address_token(), cached2
->source_address_token());
834 EXPECT_TRUE(cached2
->proof_valid());
838 TEST_P(QuicStreamFactoryTest
, CryptoConfigWhenProofIsInvalid
) {
839 vector
<string
> cannoncial_suffixes
;
840 cannoncial_suffixes
.push_back(string(".c.youtube.com"));
841 cannoncial_suffixes
.push_back(string(".googlevideo.com"));
843 for (unsigned i
= 0; i
< cannoncial_suffixes
.size(); ++i
) {
844 string
r3_host_name("r3");
845 string
r4_host_name("r4");
846 r3_host_name
.append(cannoncial_suffixes
[i
]);
847 r4_host_name
.append(cannoncial_suffixes
[i
]);
849 HostPortProxyPair
host_port_proxy_pair1(HostPortPair(r3_host_name
, 80),
850 ProxyServer::Direct());
852 QuicCryptoClientConfig
* crypto_config1
=
853 QuicStreamFactoryPeer::GetOrCreateCryptoConfig(&factory_
,
854 host_port_proxy_pair1
);
855 DCHECK(crypto_config1
);
856 QuicCryptoClientConfig::CachedState
* cached1
=
857 crypto_config1
->LookupOrCreate(host_port_proxy_pair1
.first
.host());
858 EXPECT_FALSE(cached1
->proof_valid());
859 EXPECT_TRUE(cached1
->source_address_token().empty());
861 // Mutate the cached1 to have different data.
862 // TODO(rtenneti): mutate other members of CachedState.
863 cached1
->set_source_address_token(r3_host_name
);
864 cached1
->SetProofInvalid();
866 HostPortProxyPair
host_port_proxy_pair2(HostPortPair(r4_host_name
, 80),
867 ProxyServer::Direct());
868 QuicCryptoClientConfig
* crypto_config2
=
869 QuicStreamFactoryPeer::GetOrCreateCryptoConfig(&factory_
,
870 host_port_proxy_pair2
);
871 DCHECK(crypto_config2
);
872 QuicCryptoClientConfig::CachedState
* cached2
=
873 crypto_config2
->LookupOrCreate(host_port_proxy_pair2
.first
.host());
874 EXPECT_NE(cached1
->source_address_token(), cached2
->source_address_token());
875 EXPECT_TRUE(cached2
->source_address_token().empty());
876 EXPECT_FALSE(cached2
->proof_valid());