Only grant permissions to new extensions from sync if they have the expected version
[chromium-blink-merge.git] / net / quic / quic_stream_factory_test.cc
blob6a1779aebfb0bca8d68c03a2c56390bafd32ac73
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 "base/thread_task_runner_handle.h"
10 #include "net/base/test_data_directory.h"
11 #include "net/cert/cert_verifier.h"
12 #include "net/dns/mock_host_resolver.h"
13 #include "net/http/http_response_headers.h"
14 #include "net/http/http_response_info.h"
15 #include "net/http/http_util.h"
16 #include "net/http/transport_security_state.h"
17 #include "net/quic/crypto/crypto_handshake.h"
18 #include "net/quic/crypto/proof_verifier_chromium.h"
19 #include "net/quic/crypto/quic_decrypter.h"
20 #include "net/quic/crypto/quic_encrypter.h"
21 #include "net/quic/crypto/quic_server_info.h"
22 #include "net/quic/quic_http_stream.h"
23 #include "net/quic/quic_server_id.h"
24 #include "net/quic/test_tools/mock_clock.h"
25 #include "net/quic/test_tools/mock_crypto_client_stream_factory.h"
26 #include "net/quic/test_tools/mock_random.h"
27 #include "net/quic/test_tools/quic_test_packet_maker.h"
28 #include "net/quic/test_tools/quic_test_utils.h"
29 #include "net/quic/test_tools/test_task_runner.h"
30 #include "net/socket/socket_test_util.h"
31 #include "net/spdy/spdy_test_utils.h"
32 #include "net/ssl/channel_id_service.h"
33 #include "net/ssl/default_channel_id_store.h"
34 #include "net/test/cert_test_util.h"
35 #include "testing/gtest/include/gtest/gtest.h"
37 using base::StringPiece;
38 using std::ostream;
39 using std::string;
40 using std::vector;
42 namespace net {
43 namespace test {
45 namespace {
46 const char kDefaultServerHostName[] = "www.google.com";
47 const int kDefaultServerPort = 443;
49 // Run all tests with all the combinations of versions and
50 // enable_connection_racing.
51 struct TestParams {
52 TestParams(const QuicVersion version, bool enable_connection_racing)
53 : version(version), enable_connection_racing(enable_connection_racing) {}
55 friend ostream& operator<<(ostream& os, const TestParams& p) {
56 os << "{ version: " << QuicVersionToString(p.version);
57 os << " enable_connection_racing: " << p.enable_connection_racing << " }";
58 return os;
61 QuicVersion version;
62 bool enable_connection_racing;
65 // Constructs various test permutations.
66 vector<TestParams> GetTestParams() {
67 vector<TestParams> params;
68 QuicVersionVector all_supported_versions = QuicSupportedVersions();
69 for (const QuicVersion version : all_supported_versions) {
70 params.push_back(TestParams(version, false));
71 params.push_back(TestParams(version, true));
73 return params;
76 } // namespace anonymous
78 class QuicStreamFactoryPeer {
79 public:
80 static QuicCryptoClientConfig* GetCryptoConfig(QuicStreamFactory* factory) {
81 return &factory->crypto_config_;
84 static bool HasActiveSession(QuicStreamFactory* factory,
85 const HostPortPair& host_port_pair,
86 bool is_https) {
87 QuicServerId server_id(host_port_pair, is_https, PRIVACY_MODE_DISABLED);
88 return factory->HasActiveSession(server_id);
91 static QuicChromiumClientSession* GetActiveSession(
92 QuicStreamFactory* factory,
93 const HostPortPair& host_port_pair,
94 bool is_https) {
95 QuicServerId server_id(host_port_pair, is_https, PRIVACY_MODE_DISABLED);
96 DCHECK(factory->HasActiveSession(server_id));
97 return factory->active_sessions_[server_id];
100 static scoped_ptr<QuicHttpStream> CreateFromSession(
101 QuicStreamFactory* factory,
102 QuicChromiumClientSession* session) {
103 return factory->CreateFromSession(session);
106 static bool IsLiveSession(QuicStreamFactory* factory,
107 QuicChromiumClientSession* session) {
108 for (QuicStreamFactory::SessionIdMap::iterator it =
109 factory->all_sessions_.begin();
110 it != factory->all_sessions_.end(); ++it) {
111 if (it->first == session)
112 return true;
114 return false;
117 static void DisableConnectionPooling(QuicStreamFactory* factory) {
118 factory->disable_connection_pooling_ = true;
121 static void SetTaskRunner(QuicStreamFactory* factory,
122 base::TaskRunner* task_runner) {
123 factory->task_runner_ = task_runner;
126 static void SetEnableConnectionRacing(QuicStreamFactory* factory,
127 bool enable_connection_racing) {
128 factory->enable_connection_racing_ = enable_connection_racing;
131 static void SetDisableDiskCache(QuicStreamFactory* factory,
132 bool disable_disk_cache) {
133 factory->disable_disk_cache_ = disable_disk_cache;
136 static void SetMaxNumberOfLossyConnections(
137 QuicStreamFactory* factory,
138 int max_number_of_lossy_connections) {
139 factory->max_number_of_lossy_connections_ = max_number_of_lossy_connections;
142 static int GetNumberOfLossyConnections(QuicStreamFactory* factory,
143 uint16 port) {
144 return factory->number_of_lossy_connections_[port];
147 static bool IsQuicDisabled(QuicStreamFactory* factory, uint16 port) {
148 return factory->IsQuicDisabled(port);
151 static size_t GetNumberOfActiveJobs(QuicStreamFactory* factory,
152 const QuicServerId& server_id) {
153 return (factory->active_jobs_[server_id]).size();
156 static void SetThresholdTimeoutsWithOpenStreams(
157 QuicStreamFactory* factory,
158 int threshold_timeouts_with_open_streams) {
159 factory->threshold_timeouts_with_open_streams_ =
160 threshold_timeouts_with_open_streams;
163 static int GetNumTimeoutsWithOpenStreams(QuicStreamFactory* factory) {
164 return factory->num_timeouts_with_open_streams_;
167 static void SetThresholdPublicResetsPostHandshake(
168 QuicStreamFactory* factory,
169 int threshold_public_resets_post_handshake) {
170 factory->threshold_public_resets_post_handshake_ =
171 threshold_public_resets_post_handshake;
174 static int GetNumPublicResetsPostHandshake(QuicStreamFactory* factory) {
175 return factory->num_public_resets_post_handshake_;
179 class MockQuicServerInfo : public QuicServerInfo {
180 public:
181 MockQuicServerInfo(const QuicServerId& server_id)
182 : QuicServerInfo(server_id) {}
183 ~MockQuicServerInfo() override {}
185 void Start() override {}
187 int WaitForDataReady(const CompletionCallback& callback) override {
188 return ERR_IO_PENDING;
191 void ResetWaitForDataReadyCallback() override {}
193 void CancelWaitForDataReadyCallback() override {}
195 bool IsDataReady() override { return false; }
197 bool IsReadyToPersist() override { return false; }
199 void Persist() override {}
201 void OnExternalCacheHit() override {}
204 class MockQuicServerInfoFactory : public QuicServerInfoFactory {
205 public:
206 MockQuicServerInfoFactory() {}
207 ~MockQuicServerInfoFactory() override {}
209 QuicServerInfo* GetForServer(const QuicServerId& server_id) override {
210 return new MockQuicServerInfo(server_id);
214 class QuicStreamFactoryTest : public ::testing::TestWithParam<TestParams> {
215 protected:
216 QuicStreamFactoryTest()
217 : random_generator_(0),
218 clock_(new MockClock()),
219 runner_(new TestTaskRunner(clock_)),
220 maker_(GetParam().version, 0, clock_, kDefaultServerHostName),
221 cert_verifier_(CertVerifier::CreateDefault()),
222 channel_id_service_(
223 new ChannelIDService(new DefaultChannelIDStore(nullptr),
224 base::ThreadTaskRunnerHandle::Get())),
225 factory_(&host_resolver_,
226 &socket_factory_,
227 base::WeakPtr<HttpServerProperties>(),
228 cert_verifier_.get(),
229 nullptr,
230 channel_id_service_.get(),
231 &transport_security_state_,
232 &crypto_client_stream_factory_,
233 &random_generator_,
234 clock_,
235 kDefaultMaxPacketSize,
236 std::string(),
237 SupportedVersions(GetParam().version),
238 /*enable_port_selection=*/true,
239 /*always_require_handshake_confirmation=*/false,
240 /*disable_connection_pooling=*/false,
241 /*load_server_info_timeout_srtt_multiplier=*/0.0f,
242 /*enable_connection_racing=*/false,
243 /*enable_non_blocking_io=*/true,
244 /*disable_disk_cache=*/false,
245 /*prefer_aes=*/false,
246 /*max_number_of_lossy_connections=*/0,
247 /*packet_loss_threshold=*/1.0f,
248 /*max_disabled_reasons=*/3,
249 /*threshold_timeouts_with_open_streams=*/2,
250 /*threshold_pulic_resets_post_handshake=*/2,
251 /*receive_buffer_size=*/0,
252 QuicTagVector()),
253 host_port_pair_(kDefaultServerHostName, kDefaultServerPort),
254 is_https_(false),
255 privacy_mode_(PRIVACY_MODE_DISABLED) {
256 factory_.set_require_confirmation(false);
257 clock_->AdvanceTime(QuicTime::Delta::FromSeconds(1));
258 QuicStreamFactoryPeer::SetEnableConnectionRacing(
259 &factory_, GetParam().enable_connection_racing);
262 bool HasActiveSession(const HostPortPair& host_port_pair) {
263 return QuicStreamFactoryPeer::HasActiveSession(&factory_, host_port_pair,
264 /*is_https_=*/false);
267 scoped_ptr<QuicHttpStream> CreateFromSession(
268 const HostPortPair& host_port_pair) {
269 QuicChromiumClientSession* session =
270 QuicStreamFactoryPeer::GetActiveSession(&factory_, host_port_pair,
271 /*is_https=*/false);
272 return QuicStreamFactoryPeer::CreateFromSession(&factory_, session);
275 int GetSourcePortForNewSession(const HostPortPair& destination) {
276 return GetSourcePortForNewSessionInner(destination, false);
279 int GetSourcePortForNewSessionAndGoAway(
280 const HostPortPair& destination) {
281 return GetSourcePortForNewSessionInner(destination, true);
284 int GetSourcePortForNewSessionInner(const HostPortPair& destination,
285 bool goaway_received) {
286 // Should only be called if there is no active session for this destination.
287 EXPECT_FALSE(HasActiveSession(destination));
288 size_t socket_count = socket_factory_.udp_client_sockets().size();
290 MockRead reads[] = {
291 MockRead(ASYNC, OK, 0) // EOF
293 DeterministicSocketData socket_data(reads, arraysize(reads), nullptr, 0);
294 socket_data.StopAfter(1);
295 socket_factory_.AddSocketDataProvider(&socket_data);
297 QuicStreamRequest request(&factory_);
298 EXPECT_EQ(ERR_IO_PENDING,
299 request.Request(destination, is_https_, privacy_mode_,
300 /*cert_verify_flags=*/0, destination.host(),
301 "GET", net_log_, callback_.callback()));
303 EXPECT_EQ(OK, callback_.WaitForResult());
304 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
305 EXPECT_TRUE(stream.get());
306 stream.reset();
308 QuicChromiumClientSession* session =
309 QuicStreamFactoryPeer::GetActiveSession(&factory_, destination,
310 is_https_);
312 if (socket_count + 1 != socket_factory_.udp_client_sockets().size()) {
313 EXPECT_TRUE(false);
314 return 0;
317 IPEndPoint endpoint;
318 socket_factory_.
319 udp_client_sockets()[socket_count]->GetLocalAddress(&endpoint);
320 int port = endpoint.port();
321 if (goaway_received) {
322 QuicGoAwayFrame goaway(QUIC_NO_ERROR, 1, "");
323 session->connection()->OnGoAwayFrame(goaway);
326 factory_.OnSessionClosed(session);
327 EXPECT_FALSE(HasActiveSession(destination));
328 EXPECT_TRUE(socket_data.AllReadDataConsumed());
329 EXPECT_TRUE(socket_data.AllWriteDataConsumed());
330 return port;
333 scoped_ptr<QuicEncryptedPacket> ConstructRstPacket() {
334 QuicStreamId stream_id = kClientDataStreamId1;
335 return maker_.MakeRstPacket(
336 1, true, stream_id,
337 AdjustErrorForVersion(QUIC_RST_ACKNOWLEDGEMENT, GetParam().version));
340 static ProofVerifyDetailsChromium DefaultProofVerifyDetails() {
341 // Load a certificate that is valid for www.example.org, mail.example.org,
342 // and mail.example.com.
343 scoped_refptr<X509Certificate> test_cert(
344 ImportCertFromFile(GetTestCertsDirectory(), "spdy_pooling.pem"));
345 EXPECT_TRUE(test_cert.get());
346 ProofVerifyDetailsChromium verify_details;
347 verify_details.cert_verify_result.verified_cert = test_cert;
348 verify_details.cert_verify_result.is_issued_by_known_root = true;
349 return verify_details;
352 MockQuicServerInfoFactory quic_server_info_factory_;
353 MockHostResolver host_resolver_;
354 DeterministicMockClientSocketFactory socket_factory_;
355 MockCryptoClientStreamFactory crypto_client_stream_factory_;
356 MockRandom random_generator_;
357 MockClock* clock_; // Owned by factory_.
358 scoped_refptr<TestTaskRunner> runner_;
359 QuicTestPacketMaker maker_;
360 scoped_ptr<CertVerifier> cert_verifier_;
361 scoped_ptr<ChannelIDService> channel_id_service_;
362 TransportSecurityState transport_security_state_;
363 QuicStreamFactory factory_;
364 HostPortPair host_port_pair_;
365 bool is_https_;
366 PrivacyMode privacy_mode_;
367 BoundNetLog net_log_;
368 TestCompletionCallback callback_;
371 INSTANTIATE_TEST_CASE_P(Version,
372 QuicStreamFactoryTest,
373 ::testing::ValuesIn(GetTestParams()));
375 TEST_P(QuicStreamFactoryTest, Create) {
376 MockRead reads[] = {
377 MockRead(ASYNC, OK, 0) // EOF
379 DeterministicSocketData socket_data(reads, arraysize(reads), nullptr, 0);
380 socket_factory_.AddSocketDataProvider(&socket_data);
381 socket_data.StopAfter(1);
383 QuicStreamRequest request(&factory_);
384 EXPECT_EQ(ERR_IO_PENDING,
385 request.Request(host_port_pair_, is_https_, privacy_mode_,
386 /*cert_verify_flags=*/0, host_port_pair_.host(),
387 "GET", net_log_, callback_.callback()));
389 EXPECT_EQ(OK, callback_.WaitForResult());
390 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
391 EXPECT_TRUE(stream.get());
393 // Will reset stream 3.
394 stream = CreateFromSession(host_port_pair_);
395 EXPECT_TRUE(stream.get());
397 // TODO(rtenneti): We should probably have a tests that HTTP and HTTPS result
398 // in streams on different sessions.
399 QuicStreamRequest request2(&factory_);
400 EXPECT_EQ(OK,
401 request2.Request(host_port_pair_, is_https_, privacy_mode_,
402 /*cert_verify_flags=*/0, host_port_pair_.host(),
403 "GET", net_log_, callback_.callback()));
404 stream = request2.ReleaseStream(); // Will reset stream 5.
405 stream.reset(); // Will reset stream 7.
407 EXPECT_TRUE(socket_data.AllReadDataConsumed());
408 EXPECT_TRUE(socket_data.AllWriteDataConsumed());
411 TEST_P(QuicStreamFactoryTest, CreateZeroRtt) {
412 MockRead reads[] = {
413 MockRead(ASYNC, OK, 0) // EOF
415 DeterministicSocketData socket_data(reads, arraysize(reads), nullptr, 0);
416 socket_factory_.AddSocketDataProvider(&socket_data);
417 socket_data.StopAfter(1);
419 crypto_client_stream_factory_.set_handshake_mode(
420 MockCryptoClientStream::ZERO_RTT);
421 host_resolver_.set_synchronous_mode(true);
422 host_resolver_.rules()->AddIPLiteralRule(host_port_pair_.host(),
423 "192.168.0.1", "");
425 QuicStreamRequest request(&factory_);
426 EXPECT_EQ(OK, request.Request(host_port_pair_, is_https_, privacy_mode_,
427 /*cert_verify_flags=*/0, host_port_pair_.host(),
428 "GET", net_log_, callback_.callback()));
430 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
431 EXPECT_TRUE(stream.get());
432 EXPECT_TRUE(socket_data.AllReadDataConsumed());
433 EXPECT_TRUE(socket_data.AllWriteDataConsumed());
436 TEST_P(QuicStreamFactoryTest, CreateZeroRttPost) {
437 MockRead reads[] = {
438 MockRead(ASYNC, OK, 0) // EOF
440 DeterministicSocketData socket_data(reads, arraysize(reads), nullptr, 0);
441 socket_factory_.AddSocketDataProvider(&socket_data);
442 socket_data.StopAfter(1);
444 crypto_client_stream_factory_.set_handshake_mode(
445 MockCryptoClientStream::ZERO_RTT);
446 host_resolver_.set_synchronous_mode(true);
447 host_resolver_.rules()->AddIPLiteralRule(host_port_pair_.host(),
448 "192.168.0.1", "");
450 QuicStreamRequest request(&factory_);
451 // Posts require handshake confirmation, so this will return asynchronously.
452 EXPECT_EQ(ERR_IO_PENDING,
453 request.Request(host_port_pair_, is_https_, privacy_mode_,
454 /*cert_verify_flags=*/0, host_port_pair_.host(),
455 "POST", net_log_, callback_.callback()));
457 // Confirm the handshake and verify that the stream is created.
458 crypto_client_stream_factory_.last_stream()->SendOnCryptoHandshakeEvent(
459 QuicSession::HANDSHAKE_CONFIRMED);
461 EXPECT_EQ(OK, callback_.WaitForResult());
462 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
463 EXPECT_TRUE(stream.get());
464 EXPECT_TRUE(socket_data.AllReadDataConsumed());
465 EXPECT_TRUE(socket_data.AllWriteDataConsumed());
468 TEST_P(QuicStreamFactoryTest, NoZeroRttForDifferentHost) {
469 MockRead reads[] = {
470 MockRead(ASYNC, OK, 0),
472 DeterministicSocketData socket_data(reads, arraysize(reads), nullptr, 0);
473 socket_factory_.AddSocketDataProvider(&socket_data);
474 socket_data.StopAfter(1);
476 crypto_client_stream_factory_.set_handshake_mode(
477 MockCryptoClientStream::ZERO_RTT);
478 host_resolver_.set_synchronous_mode(true);
479 host_resolver_.rules()->AddIPLiteralRule(host_port_pair_.host(),
480 "192.168.0.1", "");
482 QuicStreamRequest request(&factory_);
483 int rv = request.Request(
484 host_port_pair_, is_https_, privacy_mode_, /*cert_verify_flags=*/0,
485 "different.host.example.com", "GET", net_log_, callback_.callback());
486 // If server and origin have different hostnames, then handshake confirmation
487 // should be required, so Request will return asynchronously.
488 EXPECT_EQ(ERR_IO_PENDING, rv);
489 // Confirm handshake.
490 crypto_client_stream_factory_.last_stream()->SendOnCryptoHandshakeEvent(
491 QuicSession::HANDSHAKE_CONFIRMED);
492 EXPECT_EQ(OK, callback_.WaitForResult());
494 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
495 EXPECT_TRUE(stream.get());
496 EXPECT_TRUE(socket_data.AllReadDataConsumed());
497 EXPECT_TRUE(socket_data.AllWriteDataConsumed());
500 TEST_P(QuicStreamFactoryTest, CreateHttpVsHttps) {
501 MockRead reads[] = {
502 MockRead(ASYNC, OK, 0) // EOF
504 DeterministicSocketData socket_data1(reads, arraysize(reads), nullptr, 0);
505 DeterministicSocketData socket_data2(reads, arraysize(reads), nullptr, 0);
506 socket_factory_.AddSocketDataProvider(&socket_data1);
507 socket_factory_.AddSocketDataProvider(&socket_data2);
508 socket_data1.StopAfter(1);
509 socket_data2.StopAfter(1);
511 QuicStreamRequest request(&factory_);
512 EXPECT_EQ(ERR_IO_PENDING,
513 request.Request(host_port_pair_, is_https_, privacy_mode_,
514 /*cert_verify_flags=*/0, host_port_pair_.host(),
515 "GET", net_log_, callback_.callback()));
517 EXPECT_EQ(OK, callback_.WaitForResult());
518 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
519 EXPECT_TRUE(stream.get());
521 QuicStreamRequest request2(&factory_);
522 EXPECT_EQ(ERR_IO_PENDING,
523 request2.Request(host_port_pair_, !is_https_, privacy_mode_,
524 /*cert_verify_flags=*/0, host_port_pair_.host(),
525 "GET", net_log_, callback_.callback()));
526 EXPECT_EQ(OK, callback_.WaitForResult());
527 stream = request2.ReleaseStream();
528 EXPECT_TRUE(stream.get());
529 stream.reset();
531 EXPECT_NE(QuicStreamFactoryPeer::GetActiveSession(
532 &factory_, host_port_pair_, is_https_),
533 QuicStreamFactoryPeer::GetActiveSession(
534 &factory_, host_port_pair_, !is_https_));
536 EXPECT_TRUE(socket_data1.AllReadDataConsumed());
537 EXPECT_TRUE(socket_data1.AllWriteDataConsumed());
538 EXPECT_TRUE(socket_data2.AllReadDataConsumed());
539 EXPECT_TRUE(socket_data2.AllWriteDataConsumed());
542 TEST_P(QuicStreamFactoryTest, Pooling) {
543 MockRead reads[] = {
544 MockRead(ASYNC, OK, 0) // EOF
546 DeterministicSocketData socket_data(reads, arraysize(reads), nullptr, 0);
547 socket_factory_.AddSocketDataProvider(&socket_data);
548 socket_data.StopAfter(1);
550 HostPortPair server2("mail.google.com", kDefaultServerPort);
551 host_resolver_.set_synchronous_mode(true);
552 host_resolver_.rules()->AddIPLiteralRule(
553 kDefaultServerHostName, "192.168.0.1", "");
554 host_resolver_.rules()->AddIPLiteralRule(
555 "mail.google.com", "192.168.0.1", "");
557 QuicStreamRequest request(&factory_);
558 EXPECT_EQ(OK, request.Request(host_port_pair_, is_https_, privacy_mode_,
559 /*cert_verify_flags=*/0, host_port_pair_.host(),
560 "GET", net_log_, callback_.callback()));
561 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
562 EXPECT_TRUE(stream.get());
564 TestCompletionCallback callback;
565 QuicStreamRequest request2(&factory_);
566 EXPECT_EQ(OK, request2.Request(server2, is_https_, privacy_mode_,
567 /*cert_verify_flags=*/0, server2.host(), "GET",
568 net_log_, callback.callback()));
569 scoped_ptr<QuicHttpStream> stream2 = request2.ReleaseStream();
570 EXPECT_TRUE(stream2.get());
572 EXPECT_EQ(
573 QuicStreamFactoryPeer::GetActiveSession(
574 &factory_, host_port_pair_, is_https_),
575 QuicStreamFactoryPeer::GetActiveSession(&factory_, server2, is_https_));
577 EXPECT_TRUE(socket_data.AllReadDataConsumed());
578 EXPECT_TRUE(socket_data.AllWriteDataConsumed());
581 TEST_P(QuicStreamFactoryTest, NoPoolingIfDisabled) {
582 MockRead reads[] = {
583 MockRead(ASYNC, OK, 0) // EOF
585 DeterministicSocketData socket_data1(reads, arraysize(reads), nullptr, 0);
586 DeterministicSocketData socket_data2(reads, arraysize(reads), nullptr, 0);
587 socket_factory_.AddSocketDataProvider(&socket_data1);
588 socket_factory_.AddSocketDataProvider(&socket_data2);
589 socket_data1.StopAfter(1);
590 socket_data2.StopAfter(1);
592 HostPortPair server2("mail.google.com", kDefaultServerPort);
593 host_resolver_.set_synchronous_mode(true);
594 host_resolver_.rules()->AddIPLiteralRule(
595 kDefaultServerHostName, "192.168.0.1", "");
596 host_resolver_.rules()->AddIPLiteralRule(
597 "mail.google.com", "192.168.0.1", "");
599 // Disable connection pooling.
600 QuicStreamFactoryPeer::DisableConnectionPooling(&factory_);
602 QuicStreamRequest request(&factory_);
603 EXPECT_EQ(OK, request.Request(host_port_pair_, is_https_, privacy_mode_,
604 /*cert_verify_flags=*/0, host_port_pair_.host(),
605 "GET", net_log_, callback_.callback()));
606 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
607 EXPECT_TRUE(stream.get());
609 TestCompletionCallback callback;
610 QuicStreamRequest request2(&factory_);
611 EXPECT_EQ(OK, request2.Request(server2, is_https_, privacy_mode_,
612 /*cert_verify_flags=*/0, server2.host(), "GET",
613 net_log_, callback.callback()));
614 scoped_ptr<QuicHttpStream> stream2 = request2.ReleaseStream();
615 EXPECT_TRUE(stream2.get());
617 EXPECT_NE(
618 QuicStreamFactoryPeer::GetActiveSession(
619 &factory_, host_port_pair_, is_https_),
620 QuicStreamFactoryPeer::GetActiveSession(&factory_, server2, is_https_));
622 EXPECT_TRUE(socket_data1.AllReadDataConsumed());
623 EXPECT_TRUE(socket_data1.AllWriteDataConsumed());
624 EXPECT_TRUE(socket_data2.AllReadDataConsumed());
625 EXPECT_TRUE(socket_data2.AllWriteDataConsumed());
628 TEST_P(QuicStreamFactoryTest, NoPoolingAfterGoAway) {
629 MockRead reads[] = {
630 MockRead(ASYNC, OK, 0) // EOF
632 DeterministicSocketData socket_data1(reads, arraysize(reads), nullptr, 0);
633 DeterministicSocketData socket_data2(reads, arraysize(reads), nullptr, 0);
634 socket_factory_.AddSocketDataProvider(&socket_data1);
635 socket_factory_.AddSocketDataProvider(&socket_data2);
636 socket_data1.StopAfter(1);
637 socket_data2.StopAfter(1);
639 HostPortPair server2("mail.google.com", kDefaultServerPort);
640 host_resolver_.set_synchronous_mode(true);
641 host_resolver_.rules()->AddIPLiteralRule(
642 kDefaultServerHostName, "192.168.0.1", "");
643 host_resolver_.rules()->AddIPLiteralRule(
644 "mail.google.com", "192.168.0.1", "");
646 QuicStreamRequest request(&factory_);
647 EXPECT_EQ(OK, request.Request(host_port_pair_, is_https_, privacy_mode_,
648 /*cert_verify_flags=*/0, host_port_pair_.host(),
649 "GET", net_log_, callback_.callback()));
650 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
651 EXPECT_TRUE(stream.get());
653 TestCompletionCallback callback;
654 QuicStreamRequest request2(&factory_);
655 EXPECT_EQ(OK, request2.Request(server2, is_https_, privacy_mode_,
656 /*cert_verify_flags=*/0, server2.host(), "GET",
657 net_log_, callback.callback()));
658 scoped_ptr<QuicHttpStream> stream2 = request2.ReleaseStream();
659 EXPECT_TRUE(stream2.get());
661 factory_.OnSessionGoingAway(QuicStreamFactoryPeer::GetActiveSession(
662 &factory_, host_port_pair_, is_https_));
663 EXPECT_FALSE(QuicStreamFactoryPeer::HasActiveSession(
664 &factory_, host_port_pair_, is_https_));
665 EXPECT_FALSE(QuicStreamFactoryPeer::HasActiveSession(
666 &factory_, server2, is_https_));
668 TestCompletionCallback callback3;
669 QuicStreamRequest request3(&factory_);
670 EXPECT_EQ(OK, request3.Request(server2, is_https_, privacy_mode_,
671 /*cert_verify_flags=*/0, server2.host(), "GET",
672 net_log_, callback3.callback()));
673 scoped_ptr<QuicHttpStream> stream3 = request3.ReleaseStream();
674 EXPECT_TRUE(stream3.get());
676 EXPECT_TRUE(QuicStreamFactoryPeer::HasActiveSession(
677 &factory_, server2, is_https_));
679 EXPECT_TRUE(socket_data1.AllReadDataConsumed());
680 EXPECT_TRUE(socket_data1.AllWriteDataConsumed());
681 EXPECT_TRUE(socket_data2.AllReadDataConsumed());
682 EXPECT_TRUE(socket_data2.AllWriteDataConsumed());
685 TEST_P(QuicStreamFactoryTest, HttpsPooling) {
686 MockRead reads[] = {
687 MockRead(ASYNC, OK, 0) // EOF
689 DeterministicSocketData socket_data(reads, arraysize(reads), nullptr, 0);
690 socket_factory_.AddSocketDataProvider(&socket_data);
691 socket_data.StopAfter(1);
693 HostPortPair server1("www.example.org", 443);
694 HostPortPair server2("mail.example.org", 443);
696 ProofVerifyDetailsChromium verify_details = DefaultProofVerifyDetails();
697 crypto_client_stream_factory_.AddProofVerifyDetails(&verify_details);
699 host_resolver_.set_synchronous_mode(true);
700 host_resolver_.rules()->AddIPLiteralRule(server1.host(), "192.168.0.1", "");
701 host_resolver_.rules()->AddIPLiteralRule(server2.host(), "192.168.0.1", "");
703 QuicStreamRequest request(&factory_);
704 is_https_ = true;
705 EXPECT_EQ(OK, request.Request(server1, is_https_, privacy_mode_,
706 /*cert_verify_flags=*/0, server1.host(), "GET",
707 net_log_, callback_.callback()));
708 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
709 EXPECT_TRUE(stream.get());
711 TestCompletionCallback callback;
712 QuicStreamRequest request2(&factory_);
713 EXPECT_EQ(OK, request2.Request(server2, is_https_, privacy_mode_,
714 /*cert_verify_flags=*/0, server2.host(), "GET",
715 net_log_, callback_.callback()));
716 scoped_ptr<QuicHttpStream> stream2 = request2.ReleaseStream();
717 EXPECT_TRUE(stream2.get());
719 EXPECT_EQ(QuicStreamFactoryPeer::GetActiveSession(
720 &factory_, server1, is_https_),
721 QuicStreamFactoryPeer::GetActiveSession(
722 &factory_, server2, is_https_));
724 EXPECT_TRUE(socket_data.AllReadDataConsumed());
725 EXPECT_TRUE(socket_data.AllWriteDataConsumed());
728 TEST_P(QuicStreamFactoryTest, NoHttpsPoolingIfDisabled) {
729 MockRead reads[] = {
730 MockRead(ASYNC, OK, 0) // EOF
732 DeterministicSocketData socket_data1(reads, arraysize(reads), nullptr, 0);
733 DeterministicSocketData socket_data2(reads, arraysize(reads), nullptr, 0);
734 socket_factory_.AddSocketDataProvider(&socket_data1);
735 socket_factory_.AddSocketDataProvider(&socket_data2);
736 socket_data1.StopAfter(1);
737 socket_data2.StopAfter(1);
739 HostPortPair server1("www.example.org", 443);
740 HostPortPair server2("mail.example.org", 443);
742 ProofVerifyDetailsChromium verify_details = DefaultProofVerifyDetails();
743 crypto_client_stream_factory_.AddProofVerifyDetails(&verify_details);
745 host_resolver_.set_synchronous_mode(true);
746 host_resolver_.rules()->AddIPLiteralRule(server1.host(), "192.168.0.1", "");
747 host_resolver_.rules()->AddIPLiteralRule(server2.host(), "192.168.0.1", "");
749 // Disable connection pooling.
750 QuicStreamFactoryPeer::DisableConnectionPooling(&factory_);
752 QuicStreamRequest request(&factory_);
753 is_https_ = true;
754 EXPECT_EQ(OK, request.Request(server1, is_https_, privacy_mode_,
755 /*cert_verify_flags=*/0, server1.host(), "GET",
756 net_log_, callback_.callback()));
757 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
758 EXPECT_TRUE(stream.get());
760 TestCompletionCallback callback;
761 QuicStreamRequest request2(&factory_);
762 EXPECT_EQ(OK, request2.Request(server2, is_https_, privacy_mode_,
763 /*cert_verify_flags=*/0, server2.host(), "GET",
764 net_log_, callback_.callback()));
765 scoped_ptr<QuicHttpStream> stream2 = request2.ReleaseStream();
766 EXPECT_TRUE(stream2.get());
768 EXPECT_NE(QuicStreamFactoryPeer::GetActiveSession(
769 &factory_, server1, is_https_),
770 QuicStreamFactoryPeer::GetActiveSession(
771 &factory_, server2, is_https_));
773 EXPECT_TRUE(socket_data1.AllReadDataConsumed());
774 EXPECT_TRUE(socket_data1.AllWriteDataConsumed());
775 EXPECT_TRUE(socket_data2.AllReadDataConsumed());
776 EXPECT_TRUE(socket_data2.AllWriteDataConsumed());
779 class QuicAlternativeServiceCertificateValidationPooling
780 : public QuicStreamFactoryTest {
781 public:
782 void Run(bool valid) {
783 MockRead reads[] = {
784 MockRead(ASYNC, OK, 0) // EOF
786 DeterministicSocketData socket_data1(reads, arraysize(reads), nullptr, 0);
787 socket_factory_.AddSocketDataProvider(&socket_data1);
788 socket_data1.StopAfter(1);
790 HostPortPair server1("www.example.org", 443);
791 HostPortPair server2("mail.example.org", 443);
793 std::string origin_host(valid ? "mail.example.org" : "invalid.example.org");
794 HostPortPair alternative("www.example.org", 443);
796 ProofVerifyDetailsChromium verify_details = DefaultProofVerifyDetails();
797 bool common_name_fallback_used;
798 EXPECT_EQ(valid,
799 verify_details.cert_verify_result.verified_cert->VerifyNameMatch(
800 origin_host, &common_name_fallback_used));
801 EXPECT_TRUE(
802 verify_details.cert_verify_result.verified_cert->VerifyNameMatch(
803 alternative.host(), &common_name_fallback_used));
804 crypto_client_stream_factory_.AddProofVerifyDetails(&verify_details);
806 host_resolver_.set_synchronous_mode(true);
807 host_resolver_.rules()->AddIPLiteralRule(alternative.host(), "192.168.0.1",
808 "");
810 // Open first stream to alternative.
811 QuicStreamRequest request1(&factory_);
812 is_https_ = true;
813 EXPECT_EQ(OK, request1.Request(alternative, is_https_, privacy_mode_,
814 /*cert_verify_flags=*/0, alternative.host(),
815 "GET", net_log_, callback_.callback()));
816 scoped_ptr<QuicHttpStream> stream1 = request1.ReleaseStream();
817 EXPECT_TRUE(stream1.get());
819 QuicStreamRequest request2(&factory_);
820 int rv = request2.Request(alternative, is_https_, privacy_mode_,
821 /*cert_verify_flags=*/0, origin_host, "GET",
822 net_log_, callback_.callback());
823 if (valid) {
824 // Alternative service of origin to |alternative| should pool to session
825 // of |stream1| even if origin is different. Since only one
826 // SocketDataProvider is set up, the second request succeeding means that
827 // it pooled to the session opened by the first one.
828 EXPECT_EQ(OK, rv);
829 scoped_ptr<QuicHttpStream> stream2 = request2.ReleaseStream();
830 EXPECT_TRUE(stream2.get());
831 } else {
832 EXPECT_EQ(ERR_ALTERNATIVE_CERT_NOT_VALID_FOR_ORIGIN, rv);
835 EXPECT_TRUE(socket_data1.AllReadDataConsumed());
836 EXPECT_TRUE(socket_data1.AllWriteDataConsumed());
840 INSTANTIATE_TEST_CASE_P(Version,
841 QuicAlternativeServiceCertificateValidationPooling,
842 ::testing::ValuesIn(GetTestParams()));
844 TEST_P(QuicAlternativeServiceCertificateValidationPooling, Valid) {
845 Run(true);
848 TEST_P(QuicAlternativeServiceCertificateValidationPooling, Invalid) {
849 Run(false);
852 TEST_P(QuicStreamFactoryTest, HttpsPoolingWithMatchingPins) {
853 MockRead reads[] = {
854 MockRead(ASYNC, OK, 0) // EOF
856 DeterministicSocketData socket_data(reads, arraysize(reads), nullptr, 0);
857 socket_factory_.AddSocketDataProvider(&socket_data);
858 socket_data.StopAfter(1);
860 HostPortPair server1("www.example.org", 443);
861 HostPortPair server2("mail.example.org", 443);
862 uint8 primary_pin = 1;
863 uint8 backup_pin = 2;
864 test::AddPin(&transport_security_state_, "mail.example.org", primary_pin,
865 backup_pin);
867 ProofVerifyDetailsChromium verify_details = DefaultProofVerifyDetails();
868 verify_details.cert_verify_result.public_key_hashes.push_back(
869 test::GetTestHashValue(primary_pin));
870 crypto_client_stream_factory_.AddProofVerifyDetails(&verify_details);
872 host_resolver_.set_synchronous_mode(true);
873 host_resolver_.rules()->AddIPLiteralRule(server1.host(), "192.168.0.1", "");
874 host_resolver_.rules()->AddIPLiteralRule(server2.host(), "192.168.0.1", "");
876 QuicStreamRequest request(&factory_);
877 is_https_ = true;
878 EXPECT_EQ(OK, request.Request(server1, is_https_, privacy_mode_,
879 /*cert_verify_flags=*/0, server1.host(), "GET",
880 net_log_, callback_.callback()));
881 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
882 EXPECT_TRUE(stream.get());
884 TestCompletionCallback callback;
885 QuicStreamRequest request2(&factory_);
886 EXPECT_EQ(OK, request2.Request(server2, is_https_, privacy_mode_,
887 /*cert_verify_flags=*/0, server2.host(), "GET",
888 net_log_, callback_.callback()));
889 scoped_ptr<QuicHttpStream> stream2 = request2.ReleaseStream();
890 EXPECT_TRUE(stream2.get());
892 EXPECT_EQ(QuicStreamFactoryPeer::GetActiveSession(
893 &factory_, server1, is_https_),
894 QuicStreamFactoryPeer::GetActiveSession(
895 &factory_, server2, is_https_));
897 EXPECT_TRUE(socket_data.AllReadDataConsumed());
898 EXPECT_TRUE(socket_data.AllWriteDataConsumed());
901 TEST_P(QuicStreamFactoryTest, NoHttpsPoolingWithMatchingPinsIfDisabled) {
902 MockRead reads[] = {
903 MockRead(ASYNC, OK, 0) // EOF
905 DeterministicSocketData socket_data1(reads, arraysize(reads), nullptr, 0);
906 DeterministicSocketData socket_data2(reads, arraysize(reads), nullptr, 0);
907 socket_factory_.AddSocketDataProvider(&socket_data1);
908 socket_factory_.AddSocketDataProvider(&socket_data2);
909 socket_data1.StopAfter(1);
910 socket_data2.StopAfter(1);
912 HostPortPair server1("www.example.org", 443);
913 HostPortPair server2("mail.example.org", 443);
914 uint8 primary_pin = 1;
915 uint8 backup_pin = 2;
916 test::AddPin(&transport_security_state_, "mail.example.org", primary_pin,
917 backup_pin);
919 ProofVerifyDetailsChromium verify_details = DefaultProofVerifyDetails();
920 verify_details.cert_verify_result.public_key_hashes.push_back(
921 test::GetTestHashValue(primary_pin));
922 crypto_client_stream_factory_.AddProofVerifyDetails(&verify_details);
924 host_resolver_.set_synchronous_mode(true);
925 host_resolver_.rules()->AddIPLiteralRule(server1.host(), "192.168.0.1", "");
926 host_resolver_.rules()->AddIPLiteralRule(server2.host(), "192.168.0.1", "");
928 // Disable connection pooling.
929 QuicStreamFactoryPeer::DisableConnectionPooling(&factory_);
931 QuicStreamRequest request(&factory_);
932 is_https_ = true;
933 EXPECT_EQ(OK, request.Request(server1, is_https_, privacy_mode_,
934 /*cert_verify_flags=*/0, server1.host(), "GET",
935 net_log_, callback_.callback()));
936 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
937 EXPECT_TRUE(stream.get());
939 TestCompletionCallback callback;
940 QuicStreamRequest request2(&factory_);
941 EXPECT_EQ(OK, request2.Request(server2, is_https_, privacy_mode_,
942 /*cert_verify_flags=*/0, server2.host(), "GET",
943 net_log_, callback_.callback()));
944 scoped_ptr<QuicHttpStream> stream2 = request2.ReleaseStream();
945 EXPECT_TRUE(stream2.get());
947 EXPECT_NE(QuicStreamFactoryPeer::GetActiveSession(
948 &factory_, server1, is_https_),
949 QuicStreamFactoryPeer::GetActiveSession(
950 &factory_, server2, is_https_));
952 EXPECT_TRUE(socket_data1.AllReadDataConsumed());
953 EXPECT_TRUE(socket_data1.AllWriteDataConsumed());
954 EXPECT_TRUE(socket_data2.AllReadDataConsumed());
955 EXPECT_TRUE(socket_data2.AllWriteDataConsumed());
958 TEST_P(QuicStreamFactoryTest, NoHttpsPoolingWithDifferentPins) {
959 MockRead reads[] = {
960 MockRead(ASYNC, OK, 0) // EOF
962 DeterministicSocketData socket_data1(reads, arraysize(reads), nullptr, 0);
963 DeterministicSocketData socket_data2(reads, arraysize(reads), nullptr, 0);
964 socket_factory_.AddSocketDataProvider(&socket_data1);
965 socket_factory_.AddSocketDataProvider(&socket_data2);
966 socket_data1.StopAfter(1);
967 socket_data2.StopAfter(1);
969 HostPortPair server1("www.example.org", 443);
970 HostPortPair server2("mail.example.org", 443);
971 uint8 primary_pin = 1;
972 uint8 backup_pin = 2;
973 uint8 bad_pin = 3;
974 test::AddPin(&transport_security_state_, "mail.example.org", primary_pin,
975 backup_pin);
977 ProofVerifyDetailsChromium verify_details1 = DefaultProofVerifyDetails();
978 verify_details1.cert_verify_result.public_key_hashes.push_back(
979 test::GetTestHashValue(bad_pin));
980 crypto_client_stream_factory_.AddProofVerifyDetails(&verify_details1);
982 ProofVerifyDetailsChromium verify_details2 = DefaultProofVerifyDetails();
983 verify_details2.cert_verify_result.public_key_hashes.push_back(
984 test::GetTestHashValue(primary_pin));
985 crypto_client_stream_factory_.AddProofVerifyDetails(&verify_details2);
987 host_resolver_.set_synchronous_mode(true);
988 host_resolver_.rules()->AddIPLiteralRule(server1.host(), "192.168.0.1", "");
989 host_resolver_.rules()->AddIPLiteralRule(server2.host(), "192.168.0.1", "");
991 QuicStreamRequest request(&factory_);
992 is_https_ = true;
993 EXPECT_EQ(OK, request.Request(server1, is_https_, privacy_mode_,
994 /*cert_verify_flags=*/0, server1.host(), "GET",
995 net_log_, callback_.callback()));
996 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
997 EXPECT_TRUE(stream.get());
999 TestCompletionCallback callback;
1000 QuicStreamRequest request2(&factory_);
1001 EXPECT_EQ(OK, request2.Request(server2, is_https_, privacy_mode_,
1002 /*cert_verify_flags=*/0, server2.host(), "GET",
1003 net_log_, callback_.callback()));
1004 scoped_ptr<QuicHttpStream> stream2 = request2.ReleaseStream();
1005 EXPECT_TRUE(stream2.get());
1007 EXPECT_NE(QuicStreamFactoryPeer::GetActiveSession(
1008 &factory_, server1, is_https_),
1009 QuicStreamFactoryPeer::GetActiveSession(
1010 &factory_, server2, is_https_));
1012 EXPECT_TRUE(socket_data1.AllReadDataConsumed());
1013 EXPECT_TRUE(socket_data1.AllWriteDataConsumed());
1014 EXPECT_TRUE(socket_data2.AllReadDataConsumed());
1015 EXPECT_TRUE(socket_data2.AllWriteDataConsumed());
1018 TEST_P(QuicStreamFactoryTest, Goaway) {
1019 MockRead reads[] = {
1020 MockRead(ASYNC, OK, 0) // EOF
1022 DeterministicSocketData socket_data(reads, arraysize(reads), nullptr, 0);
1023 socket_data.StopAfter(1);
1024 socket_factory_.AddSocketDataProvider(&socket_data);
1025 DeterministicSocketData socket_data2(reads, arraysize(reads), nullptr, 0);
1026 socket_data2.StopAfter(1);
1027 socket_factory_.AddSocketDataProvider(&socket_data2);
1029 QuicStreamRequest request(&factory_);
1030 EXPECT_EQ(ERR_IO_PENDING,
1031 request.Request(host_port_pair_, is_https_, privacy_mode_,
1032 /*cert_verify_flags=*/0, host_port_pair_.host(),
1033 "GET", net_log_, callback_.callback()));
1035 EXPECT_EQ(OK, callback_.WaitForResult());
1036 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
1037 EXPECT_TRUE(stream.get());
1039 // Mark the session as going away. Ensure that while it is still alive
1040 // that it is no longer active.
1041 QuicChromiumClientSession* session = QuicStreamFactoryPeer::GetActiveSession(
1042 &factory_, host_port_pair_, is_https_);
1043 factory_.OnSessionGoingAway(session);
1044 EXPECT_EQ(true, QuicStreamFactoryPeer::IsLiveSession(&factory_, session));
1045 EXPECT_FALSE(QuicStreamFactoryPeer::HasActiveSession(
1046 &factory_, host_port_pair_, is_https_));
1047 EXPECT_FALSE(HasActiveSession(host_port_pair_));
1049 // Create a new request for the same destination and verify that a
1050 // new session is created.
1051 QuicStreamRequest request2(&factory_);
1052 EXPECT_EQ(ERR_IO_PENDING,
1053 request2.Request(host_port_pair_, is_https_, privacy_mode_,
1054 /*cert_verify_flags=*/0, host_port_pair_.host(),
1055 "GET", net_log_, callback_.callback()));
1056 EXPECT_EQ(OK, callback_.WaitForResult());
1057 scoped_ptr<QuicHttpStream> stream2 = request2.ReleaseStream();
1058 EXPECT_TRUE(stream2.get());
1060 EXPECT_TRUE(QuicStreamFactoryPeer::HasActiveSession(&factory_,
1061 host_port_pair_,
1062 is_https_));
1063 EXPECT_NE(session,
1064 QuicStreamFactoryPeer::GetActiveSession(
1065 &factory_, host_port_pair_, is_https_));
1066 EXPECT_EQ(true, QuicStreamFactoryPeer::IsLiveSession(&factory_, session));
1068 stream2.reset();
1069 stream.reset();
1071 EXPECT_TRUE(socket_data.AllReadDataConsumed());
1072 EXPECT_TRUE(socket_data.AllWriteDataConsumed());
1073 EXPECT_TRUE(socket_data2.AllReadDataConsumed());
1074 EXPECT_TRUE(socket_data2.AllWriteDataConsumed());
1077 TEST_P(QuicStreamFactoryTest, MaxOpenStream) {
1078 MockRead reads[] = {
1079 MockRead(ASYNC, OK, 0) // EOF
1081 QuicStreamId stream_id = kClientDataStreamId1;
1082 scoped_ptr<QuicEncryptedPacket> rst(
1083 maker_.MakeRstPacket(1, true, stream_id, QUIC_STREAM_CANCELLED));
1084 MockWrite writes[] = {
1085 MockWrite(ASYNC, rst->data(), rst->length(), 1),
1087 DeterministicSocketData socket_data(reads, arraysize(reads),
1088 writes, arraysize(writes));
1089 socket_factory_.AddSocketDataProvider(&socket_data);
1090 socket_data.StopAfter(1);
1092 HttpRequestInfo request_info;
1093 std::vector<QuicHttpStream*> streams;
1094 // The MockCryptoClientStream sets max_open_streams to be
1095 // kDefaultMaxStreamsPerConnection / 2.
1096 for (size_t i = 0; i < kDefaultMaxStreamsPerConnection / 2; i++) {
1097 QuicStreamRequest request(&factory_);
1098 int rv = request.Request(host_port_pair_, is_https_, privacy_mode_,
1099 /*cert_verify_flags=*/0, host_port_pair_.host(),
1100 "GET", net_log_, callback_.callback());
1101 if (i == 0) {
1102 EXPECT_EQ(ERR_IO_PENDING, rv);
1103 EXPECT_EQ(OK, callback_.WaitForResult());
1104 } else {
1105 EXPECT_EQ(OK, rv);
1107 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
1108 EXPECT_TRUE(stream);
1109 EXPECT_EQ(OK, stream->InitializeStream(
1110 &request_info, DEFAULT_PRIORITY, net_log_, CompletionCallback()));
1111 streams.push_back(stream.release());
1114 QuicStreamRequest request(&factory_);
1115 EXPECT_EQ(OK, request.Request(host_port_pair_, is_https_, privacy_mode_,
1116 /*cert_verify_flags=*/0, host_port_pair_.host(),
1117 "GET", net_log_, CompletionCallback()));
1118 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
1119 EXPECT_TRUE(stream);
1120 EXPECT_EQ(ERR_IO_PENDING, stream->InitializeStream(
1121 &request_info, DEFAULT_PRIORITY, net_log_, callback_.callback()));
1123 // Close the first stream.
1124 streams.front()->Close(false);
1126 ASSERT_TRUE(callback_.have_result());
1128 EXPECT_EQ(OK, callback_.WaitForResult());
1130 EXPECT_TRUE(socket_data.AllReadDataConsumed());
1131 EXPECT_TRUE(socket_data.AllWriteDataConsumed());
1132 STLDeleteElements(&streams);
1135 TEST_P(QuicStreamFactoryTest, ResolutionErrorInCreate) {
1136 DeterministicSocketData socket_data(nullptr, 0, nullptr, 0);
1137 socket_factory_.AddSocketDataProvider(&socket_data);
1139 host_resolver_.rules()->AddSimulatedFailure(kDefaultServerHostName);
1141 QuicStreamRequest request(&factory_);
1142 EXPECT_EQ(ERR_IO_PENDING,
1143 request.Request(host_port_pair_, is_https_, privacy_mode_,
1144 /*cert_verify_flags=*/0, host_port_pair_.host(),
1145 "GET", net_log_, callback_.callback()));
1147 EXPECT_EQ(ERR_NAME_NOT_RESOLVED, callback_.WaitForResult());
1149 EXPECT_TRUE(socket_data.AllReadDataConsumed());
1150 EXPECT_TRUE(socket_data.AllWriteDataConsumed());
1153 TEST_P(QuicStreamFactoryTest, ConnectErrorInCreate) {
1154 MockConnect connect(SYNCHRONOUS, ERR_ADDRESS_IN_USE);
1155 DeterministicSocketData socket_data(nullptr, 0, nullptr, 0);
1156 socket_data.set_connect_data(connect);
1157 socket_factory_.AddSocketDataProvider(&socket_data);
1158 socket_data.StopAfter(1);
1160 QuicStreamRequest request(&factory_);
1161 EXPECT_EQ(ERR_IO_PENDING,
1162 request.Request(host_port_pair_, is_https_, privacy_mode_,
1163 /*cert_verify_flags=*/0, host_port_pair_.host(),
1164 "GET", net_log_, callback_.callback()));
1166 EXPECT_EQ(ERR_ADDRESS_IN_USE, callback_.WaitForResult());
1168 EXPECT_TRUE(socket_data.AllReadDataConsumed());
1169 EXPECT_TRUE(socket_data.AllWriteDataConsumed());
1172 TEST_P(QuicStreamFactoryTest, CancelCreate) {
1173 MockRead reads[] = {
1174 MockRead(ASYNC, OK, 0) // EOF
1176 DeterministicSocketData socket_data(reads, arraysize(reads), nullptr, 0);
1177 socket_factory_.AddSocketDataProvider(&socket_data);
1179 QuicStreamRequest request(&factory_);
1180 EXPECT_EQ(ERR_IO_PENDING,
1181 request.Request(host_port_pair_, is_https_, privacy_mode_,
1182 /*cert_verify_flags=*/0, host_port_pair_.host(),
1183 "GET", net_log_, callback_.callback()));
1186 socket_data.StopAfter(1);
1187 base::RunLoop run_loop;
1188 run_loop.RunUntilIdle();
1190 scoped_ptr<QuicHttpStream> stream(CreateFromSession(host_port_pair_));
1191 EXPECT_TRUE(stream.get());
1192 stream.reset();
1194 EXPECT_TRUE(socket_data.AllReadDataConsumed());
1195 EXPECT_TRUE(socket_data.AllWriteDataConsumed());
1198 TEST_P(QuicStreamFactoryTest, CreateConsistentEphemeralPort) {
1199 // Sequentially connect to the default host, then another host, and then the
1200 // default host. Verify that the default host gets a consistent ephemeral
1201 // port, that is different from the other host's connection.
1203 std::string other_server_name = "other.google.com";
1204 EXPECT_NE(kDefaultServerHostName, other_server_name);
1205 HostPortPair host_port_pair2(other_server_name, kDefaultServerPort);
1207 int original_port = GetSourcePortForNewSession(host_port_pair_);
1208 EXPECT_NE(original_port, GetSourcePortForNewSession(host_port_pair2));
1209 EXPECT_EQ(original_port, GetSourcePortForNewSession(host_port_pair_));
1212 TEST_P(QuicStreamFactoryTest, GoAwayDisablesConsistentEphemeralPort) {
1213 // Get a session to the host using the port suggester.
1214 int original_port =
1215 GetSourcePortForNewSessionAndGoAway(host_port_pair_);
1216 // Verify that the port is different after the goaway.
1217 EXPECT_NE(original_port, GetSourcePortForNewSession(host_port_pair_));
1218 // Since the previous session did not goaway we should see the original port.
1219 EXPECT_EQ(original_port, GetSourcePortForNewSession(host_port_pair_));
1222 TEST_P(QuicStreamFactoryTest, CloseAllSessions) {
1223 MockRead reads[] = {
1224 MockRead(ASYNC, 0, 0) // EOF
1226 scoped_ptr<QuicEncryptedPacket> rst(ConstructRstPacket());
1227 std::vector<MockWrite> writes;
1228 writes.push_back(MockWrite(ASYNC, rst->data(), rst->length(), 1));
1229 DeterministicSocketData socket_data(reads, arraysize(reads),
1230 writes.empty() ? nullptr : &writes[0],
1231 writes.size());
1232 socket_factory_.AddSocketDataProvider(&socket_data);
1233 socket_data.StopAfter(1);
1235 MockRead reads2[] = {
1236 MockRead(ASYNC, 0, 0) // EOF
1238 DeterministicSocketData socket_data2(reads2, arraysize(reads2), nullptr, 0);
1239 socket_factory_.AddSocketDataProvider(&socket_data2);
1240 socket_data2.StopAfter(1);
1242 QuicStreamRequest request(&factory_);
1243 EXPECT_EQ(ERR_IO_PENDING,
1244 request.Request(host_port_pair_, is_https_, privacy_mode_,
1245 /*cert_verify_flags=*/0, host_port_pair_.host(),
1246 "GET", net_log_, callback_.callback()));
1248 EXPECT_EQ(OK, callback_.WaitForResult());
1249 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
1250 HttpRequestInfo request_info;
1251 EXPECT_EQ(OK, stream->InitializeStream(&request_info,
1252 DEFAULT_PRIORITY,
1253 net_log_, CompletionCallback()));
1255 // Close the session and verify that stream saw the error.
1256 factory_.CloseAllSessions(ERR_INTERNET_DISCONNECTED);
1257 EXPECT_EQ(ERR_INTERNET_DISCONNECTED,
1258 stream->ReadResponseHeaders(callback_.callback()));
1260 // Now attempting to request a stream to the same origin should create
1261 // a new session.
1263 QuicStreamRequest request2(&factory_);
1264 EXPECT_EQ(ERR_IO_PENDING,
1265 request2.Request(host_port_pair_, is_https_, privacy_mode_,
1266 /*cert_verify_flags=*/0, host_port_pair_.host(),
1267 "GET", net_log_, callback_.callback()));
1269 EXPECT_EQ(OK, callback_.WaitForResult());
1270 stream = request2.ReleaseStream();
1271 stream.reset(); // Will reset stream 3.
1273 EXPECT_TRUE(socket_data.AllReadDataConsumed());
1274 EXPECT_TRUE(socket_data.AllWriteDataConsumed());
1275 EXPECT_TRUE(socket_data2.AllReadDataConsumed());
1276 EXPECT_TRUE(socket_data2.AllWriteDataConsumed());
1279 TEST_P(QuicStreamFactoryTest, OnIPAddressChanged) {
1280 MockRead reads[] = {
1281 MockRead(ASYNC, 0, 0) // EOF
1283 scoped_ptr<QuicEncryptedPacket> rst(ConstructRstPacket());
1284 std::vector<MockWrite> writes;
1285 writes.push_back(MockWrite(ASYNC, rst->data(), rst->length(), 1));
1286 DeterministicSocketData socket_data(reads, arraysize(reads),
1287 writes.empty() ? nullptr : &writes[0],
1288 writes.size());
1289 socket_factory_.AddSocketDataProvider(&socket_data);
1290 socket_data.StopAfter(1);
1292 MockRead reads2[] = {
1293 MockRead(ASYNC, 0, 0) // EOF
1295 DeterministicSocketData socket_data2(reads2, arraysize(reads2), nullptr, 0);
1296 socket_factory_.AddSocketDataProvider(&socket_data2);
1297 socket_data2.StopAfter(1);
1299 QuicStreamRequest request(&factory_);
1300 EXPECT_EQ(ERR_IO_PENDING,
1301 request.Request(host_port_pair_, is_https_, privacy_mode_,
1302 /*cert_verify_flags=*/0, host_port_pair_.host(),
1303 "GET", net_log_, callback_.callback()));
1305 EXPECT_EQ(OK, callback_.WaitForResult());
1306 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
1307 HttpRequestInfo request_info;
1308 EXPECT_EQ(OK, stream->InitializeStream(&request_info,
1309 DEFAULT_PRIORITY,
1310 net_log_, CompletionCallback()));
1312 // Change the IP address and verify that stream saw the error.
1313 factory_.OnIPAddressChanged();
1314 EXPECT_EQ(ERR_NETWORK_CHANGED,
1315 stream->ReadResponseHeaders(callback_.callback()));
1316 EXPECT_TRUE(factory_.require_confirmation());
1318 // Now attempting to request a stream to the same origin should create
1319 // a new session.
1321 QuicStreamRequest request2(&factory_);
1322 EXPECT_EQ(ERR_IO_PENDING,
1323 request2.Request(host_port_pair_, is_https_, privacy_mode_,
1324 /*cert_verify_flags=*/0, host_port_pair_.host(),
1325 "GET", net_log_, callback_.callback()));
1327 EXPECT_EQ(OK, callback_.WaitForResult());
1328 stream = request2.ReleaseStream();
1329 stream.reset(); // Will reset stream 3.
1331 EXPECT_TRUE(socket_data.AllReadDataConsumed());
1332 EXPECT_TRUE(socket_data.AllWriteDataConsumed());
1333 EXPECT_TRUE(socket_data2.AllReadDataConsumed());
1334 EXPECT_TRUE(socket_data2.AllWriteDataConsumed());
1337 TEST_P(QuicStreamFactoryTest, OnSSLConfigChanged) {
1338 MockRead reads[] = {
1339 MockRead(ASYNC, 0, 0) // EOF
1341 scoped_ptr<QuicEncryptedPacket> rst(ConstructRstPacket());
1342 std::vector<MockWrite> writes;
1343 writes.push_back(MockWrite(ASYNC, rst->data(), rst->length(), 1));
1344 DeterministicSocketData socket_data(reads, arraysize(reads),
1345 writes.empty() ? nullptr : &writes[0],
1346 writes.size());
1347 socket_factory_.AddSocketDataProvider(&socket_data);
1348 socket_data.StopAfter(1);
1350 MockRead reads2[] = {
1351 MockRead(ASYNC, 0, 0) // EOF
1353 DeterministicSocketData socket_data2(reads2, arraysize(reads2), nullptr, 0);
1354 socket_factory_.AddSocketDataProvider(&socket_data2);
1355 socket_data2.StopAfter(1);
1357 QuicStreamRequest request(&factory_);
1358 EXPECT_EQ(ERR_IO_PENDING,
1359 request.Request(host_port_pair_, is_https_, privacy_mode_,
1360 /*cert_verify_flags=*/0, host_port_pair_.host(),
1361 "GET", net_log_, callback_.callback()));
1363 EXPECT_EQ(OK, callback_.WaitForResult());
1364 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
1365 HttpRequestInfo request_info;
1366 EXPECT_EQ(OK, stream->InitializeStream(&request_info, DEFAULT_PRIORITY,
1367 net_log_, CompletionCallback()));
1369 factory_.OnSSLConfigChanged();
1370 EXPECT_EQ(ERR_CERT_DATABASE_CHANGED,
1371 stream->ReadResponseHeaders(callback_.callback()));
1372 EXPECT_FALSE(factory_.require_confirmation());
1374 // Now attempting to request a stream to the same origin should create
1375 // a new session.
1377 QuicStreamRequest request2(&factory_);
1378 EXPECT_EQ(ERR_IO_PENDING,
1379 request2.Request(host_port_pair_, is_https_, privacy_mode_,
1380 /*cert_verify_flags=*/0, host_port_pair_.host(),
1381 "GET", net_log_, callback_.callback()));
1383 EXPECT_EQ(OK, callback_.WaitForResult());
1384 stream = request2.ReleaseStream();
1385 stream.reset(); // Will reset stream 3.
1387 EXPECT_TRUE(socket_data.AllReadDataConsumed());
1388 EXPECT_TRUE(socket_data.AllWriteDataConsumed());
1389 EXPECT_TRUE(socket_data2.AllReadDataConsumed());
1390 EXPECT_TRUE(socket_data2.AllWriteDataConsumed());
1393 TEST_P(QuicStreamFactoryTest, OnCertAdded) {
1394 MockRead reads[] = {
1395 MockRead(ASYNC, 0, 0) // EOF
1397 scoped_ptr<QuicEncryptedPacket> rst(ConstructRstPacket());
1398 std::vector<MockWrite> writes;
1399 writes.push_back(MockWrite(ASYNC, rst->data(), rst->length(), 1));
1400 DeterministicSocketData socket_data(reads, arraysize(reads),
1401 writes.empty() ? nullptr : &writes[0],
1402 writes.size());
1403 socket_factory_.AddSocketDataProvider(&socket_data);
1404 socket_data.StopAfter(1);
1406 MockRead reads2[] = {
1407 MockRead(ASYNC, 0, 0) // EOF
1409 DeterministicSocketData socket_data2(reads2, arraysize(reads2), nullptr, 0);
1410 socket_factory_.AddSocketDataProvider(&socket_data2);
1411 socket_data2.StopAfter(1);
1413 QuicStreamRequest request(&factory_);
1414 EXPECT_EQ(ERR_IO_PENDING,
1415 request.Request(host_port_pair_, is_https_, privacy_mode_,
1416 /*cert_verify_flags=*/0, host_port_pair_.host(),
1417 "GET", net_log_, callback_.callback()));
1419 EXPECT_EQ(OK, callback_.WaitForResult());
1420 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
1421 HttpRequestInfo request_info;
1422 EXPECT_EQ(OK, stream->InitializeStream(&request_info,
1423 DEFAULT_PRIORITY,
1424 net_log_, CompletionCallback()));
1426 // Add a cert and verify that stream saw the event.
1427 factory_.OnCertAdded(nullptr);
1428 EXPECT_EQ(ERR_CERT_DATABASE_CHANGED,
1429 stream->ReadResponseHeaders(callback_.callback()));
1430 EXPECT_FALSE(factory_.require_confirmation());
1432 // Now attempting to request a stream to the same origin should create
1433 // a new session.
1435 QuicStreamRequest request2(&factory_);
1436 EXPECT_EQ(ERR_IO_PENDING,
1437 request2.Request(host_port_pair_, is_https_, privacy_mode_,
1438 /*cert_verify_flags=*/0, host_port_pair_.host(),
1439 "GET", net_log_, callback_.callback()));
1441 EXPECT_EQ(OK, callback_.WaitForResult());
1442 stream = request2.ReleaseStream();
1443 stream.reset(); // Will reset stream 3.
1445 EXPECT_TRUE(socket_data.AllReadDataConsumed());
1446 EXPECT_TRUE(socket_data.AllWriteDataConsumed());
1447 EXPECT_TRUE(socket_data2.AllReadDataConsumed());
1448 EXPECT_TRUE(socket_data2.AllWriteDataConsumed());
1451 TEST_P(QuicStreamFactoryTest, OnCACertChanged) {
1452 MockRead reads[] = {
1453 MockRead(ASYNC, 0, 0) // EOF
1455 scoped_ptr<QuicEncryptedPacket> rst(ConstructRstPacket());
1456 std::vector<MockWrite> writes;
1457 writes.push_back(MockWrite(ASYNC, rst->data(), rst->length(), 1));
1458 DeterministicSocketData socket_data(reads, arraysize(reads),
1459 writes.empty() ? nullptr : &writes[0],
1460 writes.size());
1461 socket_factory_.AddSocketDataProvider(&socket_data);
1462 socket_data.StopAfter(1);
1464 MockRead reads2[] = {
1465 MockRead(ASYNC, 0, 0) // EOF
1467 DeterministicSocketData socket_data2(reads2, arraysize(reads2), nullptr, 0);
1468 socket_factory_.AddSocketDataProvider(&socket_data2);
1469 socket_data2.StopAfter(1);
1471 QuicStreamRequest request(&factory_);
1472 EXPECT_EQ(ERR_IO_PENDING,
1473 request.Request(host_port_pair_, is_https_, privacy_mode_,
1474 /*cert_verify_flags=*/0, host_port_pair_.host(),
1475 "GET", net_log_, callback_.callback()));
1477 EXPECT_EQ(OK, callback_.WaitForResult());
1478 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
1479 HttpRequestInfo request_info;
1480 EXPECT_EQ(OK, stream->InitializeStream(&request_info,
1481 DEFAULT_PRIORITY,
1482 net_log_, CompletionCallback()));
1484 // Change the CA cert and verify that stream saw the event.
1485 factory_.OnCACertChanged(nullptr);
1486 EXPECT_EQ(ERR_CERT_DATABASE_CHANGED,
1487 stream->ReadResponseHeaders(callback_.callback()));
1488 EXPECT_FALSE(factory_.require_confirmation());
1490 // Now attempting to request a stream to the same origin should create
1491 // a new session.
1493 QuicStreamRequest request2(&factory_);
1494 EXPECT_EQ(ERR_IO_PENDING,
1495 request2.Request(host_port_pair_, is_https_, privacy_mode_,
1496 /*cert_verify_flags=*/0, host_port_pair_.host(),
1497 "GET", net_log_, callback_.callback()));
1499 EXPECT_EQ(OK, callback_.WaitForResult());
1500 stream = request2.ReleaseStream();
1501 stream.reset(); // Will reset stream 3.
1503 EXPECT_TRUE(socket_data.AllReadDataConsumed());
1504 EXPECT_TRUE(socket_data.AllWriteDataConsumed());
1505 EXPECT_TRUE(socket_data2.AllReadDataConsumed());
1506 EXPECT_TRUE(socket_data2.AllWriteDataConsumed());
1509 TEST_P(QuicStreamFactoryTest, SharedCryptoConfig) {
1510 vector<string> cannoncial_suffixes;
1511 cannoncial_suffixes.push_back(string(".c.youtube.com"));
1512 cannoncial_suffixes.push_back(string(".googlevideo.com"));
1514 for (unsigned i = 0; i < cannoncial_suffixes.size(); ++i) {
1515 string r1_host_name("r1");
1516 string r2_host_name("r2");
1517 r1_host_name.append(cannoncial_suffixes[i]);
1518 r2_host_name.append(cannoncial_suffixes[i]);
1520 HostPortPair host_port_pair1(r1_host_name, 80);
1521 QuicCryptoClientConfig* crypto_config =
1522 QuicStreamFactoryPeer::GetCryptoConfig(&factory_);
1523 QuicServerId server_id1(host_port_pair1, is_https_, privacy_mode_);
1524 QuicCryptoClientConfig::CachedState* cached1 =
1525 crypto_config->LookupOrCreate(server_id1);
1526 EXPECT_FALSE(cached1->proof_valid());
1527 EXPECT_TRUE(cached1->source_address_token().empty());
1529 // Mutate the cached1 to have different data.
1530 // TODO(rtenneti): mutate other members of CachedState.
1531 cached1->set_source_address_token(r1_host_name);
1532 cached1->SetProofValid();
1534 HostPortPair host_port_pair2(r2_host_name, 80);
1535 QuicServerId server_id2(host_port_pair2, is_https_, privacy_mode_);
1536 QuicCryptoClientConfig::CachedState* cached2 =
1537 crypto_config->LookupOrCreate(server_id2);
1538 EXPECT_EQ(cached1->source_address_token(), cached2->source_address_token());
1539 EXPECT_TRUE(cached2->proof_valid());
1543 TEST_P(QuicStreamFactoryTest, CryptoConfigWhenProofIsInvalid) {
1544 vector<string> cannoncial_suffixes;
1545 cannoncial_suffixes.push_back(string(".c.youtube.com"));
1546 cannoncial_suffixes.push_back(string(".googlevideo.com"));
1548 for (unsigned i = 0; i < cannoncial_suffixes.size(); ++i) {
1549 string r3_host_name("r3");
1550 string r4_host_name("r4");
1551 r3_host_name.append(cannoncial_suffixes[i]);
1552 r4_host_name.append(cannoncial_suffixes[i]);
1554 HostPortPair host_port_pair1(r3_host_name, 80);
1555 QuicCryptoClientConfig* crypto_config =
1556 QuicStreamFactoryPeer::GetCryptoConfig(&factory_);
1557 QuicServerId server_id1(host_port_pair1, is_https_, privacy_mode_);
1558 QuicCryptoClientConfig::CachedState* cached1 =
1559 crypto_config->LookupOrCreate(server_id1);
1560 EXPECT_FALSE(cached1->proof_valid());
1561 EXPECT_TRUE(cached1->source_address_token().empty());
1563 // Mutate the cached1 to have different data.
1564 // TODO(rtenneti): mutate other members of CachedState.
1565 cached1->set_source_address_token(r3_host_name);
1566 cached1->SetProofInvalid();
1568 HostPortPair host_port_pair2(r4_host_name, 80);
1569 QuicServerId server_id2(host_port_pair2, is_https_, privacy_mode_);
1570 QuicCryptoClientConfig::CachedState* cached2 =
1571 crypto_config->LookupOrCreate(server_id2);
1572 EXPECT_NE(cached1->source_address_token(), cached2->source_address_token());
1573 EXPECT_TRUE(cached2->source_address_token().empty());
1574 EXPECT_FALSE(cached2->proof_valid());
1578 TEST_P(QuicStreamFactoryTest, RacingConnections) {
1579 if (!GetParam().enable_connection_racing)
1580 return;
1581 factory_.set_quic_server_info_factory(&quic_server_info_factory_);
1582 QuicStreamFactoryPeer::SetTaskRunner(&factory_, runner_.get());
1583 MockRead reads[] = {
1584 MockRead(ASYNC, OK, 0) // EOF
1586 DeterministicSocketData socket_data(reads, arraysize(reads), nullptr, 0);
1587 socket_factory_.AddSocketDataProvider(&socket_data);
1588 socket_data.StopAfter(1);
1590 MockRead reads2[] = {
1591 MockRead(ASYNC, 0, 0) // EOF
1593 DeterministicSocketData socket_data2(reads2, arraysize(reads2), nullptr, 0);
1594 socket_factory_.AddSocketDataProvider(&socket_data2);
1595 socket_data2.StopAfter(1);
1597 crypto_client_stream_factory_.set_handshake_mode(
1598 MockCryptoClientStream::ZERO_RTT);
1599 host_resolver_.set_synchronous_mode(true);
1600 host_resolver_.rules()->AddIPLiteralRule(host_port_pair_.host(),
1601 "192.168.0.1", "");
1603 QuicStreamRequest request(&factory_);
1604 QuicServerId server_id(host_port_pair_, is_https_, privacy_mode_);
1605 EXPECT_EQ(ERR_IO_PENDING,
1606 request.Request(host_port_pair_, is_https_, privacy_mode_,
1607 /*cert_verify_flags=*/0, host_port_pair_.host(),
1608 "GET", net_log_, callback_.callback()));
1609 EXPECT_EQ(2u,
1610 QuicStreamFactoryPeer::GetNumberOfActiveJobs(&factory_, server_id));
1612 runner_->RunNextTask();
1614 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
1615 EXPECT_TRUE(stream.get());
1616 EXPECT_TRUE(socket_data.AllReadDataConsumed());
1617 EXPECT_TRUE(socket_data.AllWriteDataConsumed());
1618 EXPECT_EQ(0u,
1619 QuicStreamFactoryPeer::GetNumberOfActiveJobs(&factory_, server_id));
1622 TEST_P(QuicStreamFactoryTest, EnableNotLoadFromDiskCache) {
1623 factory_.set_quic_server_info_factory(&quic_server_info_factory_);
1624 QuicStreamFactoryPeer::SetTaskRunner(&factory_, runner_.get());
1625 QuicStreamFactoryPeer::SetDisableDiskCache(&factory_, true);
1627 MockRead reads[] = {
1628 MockRead(ASYNC, OK, 0) // EOF
1630 DeterministicSocketData socket_data(reads, arraysize(reads), nullptr, 0);
1631 socket_factory_.AddSocketDataProvider(&socket_data);
1632 socket_data.StopAfter(1);
1634 crypto_client_stream_factory_.set_handshake_mode(
1635 MockCryptoClientStream::ZERO_RTT);
1636 host_resolver_.set_synchronous_mode(true);
1637 host_resolver_.rules()->AddIPLiteralRule(host_port_pair_.host(),
1638 "192.168.0.1", "");
1640 QuicStreamRequest request(&factory_);
1641 EXPECT_EQ(OK, request.Request(host_port_pair_, is_https_, privacy_mode_,
1642 /*cert_verify_flags=*/0, host_port_pair_.host(),
1643 "GET", net_log_, callback_.callback()));
1645 // If we are waiting for disk cache, we would have posted a task. Verify that
1646 // the CancelWaitForDataReady task hasn't been posted.
1647 ASSERT_EQ(0u, runner_->GetPostedTasks().size());
1649 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
1650 EXPECT_TRUE(stream.get());
1651 EXPECT_TRUE(socket_data.AllReadDataConsumed());
1652 EXPECT_TRUE(socket_data.AllWriteDataConsumed());
1655 TEST_P(QuicStreamFactoryTest, BadPacketLoss) {
1656 factory_.set_quic_server_info_factory(&quic_server_info_factory_);
1657 QuicStreamFactoryPeer::SetTaskRunner(&factory_, runner_.get());
1658 QuicStreamFactoryPeer::SetDisableDiskCache(&factory_, true);
1659 QuicStreamFactoryPeer::SetMaxNumberOfLossyConnections(&factory_, 2);
1660 EXPECT_FALSE(
1661 QuicStreamFactoryPeer::IsQuicDisabled(&factory_, host_port_pair_.port()));
1662 EXPECT_EQ(0, QuicStreamFactoryPeer::GetNumberOfLossyConnections(
1663 &factory_, host_port_pair_.port()));
1665 MockRead reads[] = {
1666 MockRead(ASYNC, OK, 0) // EOF
1668 DeterministicSocketData socket_data(reads, arraysize(reads), nullptr, 0);
1669 socket_factory_.AddSocketDataProvider(&socket_data);
1670 socket_data.StopAfter(1);
1672 DeterministicSocketData socket_data2(nullptr, 0, nullptr, 0);
1673 socket_factory_.AddSocketDataProvider(&socket_data2);
1674 socket_data2.StopAfter(1);
1676 DeterministicSocketData socket_data3(nullptr, 0, nullptr, 0);
1677 socket_factory_.AddSocketDataProvider(&socket_data3);
1678 socket_data3.StopAfter(1);
1680 DeterministicSocketData socket_data4(nullptr, 0, nullptr, 0);
1681 socket_factory_.AddSocketDataProvider(&socket_data4);
1682 socket_data4.StopAfter(1);
1684 HostPortPair server2("mail.example.org", kDefaultServerPort);
1685 HostPortPair server3("docs.example.org", kDefaultServerPort);
1686 HostPortPair server4("images.example.org", kDefaultServerPort);
1688 crypto_client_stream_factory_.set_handshake_mode(
1689 MockCryptoClientStream::ZERO_RTT);
1690 host_resolver_.set_synchronous_mode(true);
1691 host_resolver_.rules()->AddIPLiteralRule(host_port_pair_.host(),
1692 "192.168.0.1", "");
1693 host_resolver_.rules()->AddIPLiteralRule(server2.host(), "192.168.0.1", "");
1694 host_resolver_.rules()->AddIPLiteralRule(server3.host(), "192.168.0.1", "");
1695 host_resolver_.rules()->AddIPLiteralRule(server4.host(), "192.168.0.1", "");
1697 QuicStreamRequest request(&factory_);
1698 EXPECT_EQ(OK, request.Request(host_port_pair_, is_https_, privacy_mode_,
1699 /*cert_verify_flags=*/0, host_port_pair_.host(),
1700 "GET", net_log_, callback_.callback()));
1702 QuicChromiumClientSession* session = QuicStreamFactoryPeer::GetActiveSession(
1703 &factory_, host_port_pair_, is_https_);
1705 DVLOG(1) << "Create 1st session and test packet loss";
1707 // Set packet_loss_rate to a lower value than packet_loss_threshold.
1708 EXPECT_FALSE(
1709 factory_.OnHandshakeConfirmed(session, /*packet_loss_rate=*/0.9f));
1710 EXPECT_TRUE(session->connection()->connected());
1711 EXPECT_TRUE(QuicStreamFactoryPeer::HasActiveSession(
1712 &factory_, host_port_pair_, is_https_));
1713 EXPECT_FALSE(
1714 QuicStreamFactoryPeer::IsQuicDisabled(&factory_, host_port_pair_.port()));
1715 EXPECT_EQ(0, QuicStreamFactoryPeer::GetNumberOfLossyConnections(
1716 &factory_, host_port_pair_.port()));
1718 // Set packet_loss_rate to a higher value than packet_loss_threshold only once
1719 // and that shouldn't close the session and it shouldn't disable QUIC.
1720 EXPECT_FALSE(
1721 factory_.OnHandshakeConfirmed(session, /*packet_loss_rate=*/1.0f));
1722 EXPECT_EQ(1, QuicStreamFactoryPeer::GetNumberOfLossyConnections(
1723 &factory_, host_port_pair_.port()));
1724 EXPECT_TRUE(session->connection()->connected());
1725 EXPECT_FALSE(
1726 QuicStreamFactoryPeer::IsQuicDisabled(&factory_, host_port_pair_.port()));
1727 EXPECT_TRUE(QuicStreamFactoryPeer::HasActiveSession(
1728 &factory_, host_port_pair_, is_https_));
1730 // Test N-in-a-row high packet loss connections.
1732 DVLOG(1) << "Create 2nd session and test packet loss";
1734 TestCompletionCallback callback2;
1735 QuicStreamRequest request2(&factory_);
1736 EXPECT_EQ(OK, request2.Request(server2, is_https_, privacy_mode_,
1737 /*cert_verify_flags=*/0, server2.host(), "GET",
1738 net_log_, callback2.callback()));
1739 QuicChromiumClientSession* session2 =
1740 QuicStreamFactoryPeer::GetActiveSession(&factory_, server2, is_https_);
1742 // If there is no packet loss during handshake confirmation, number of lossy
1743 // connections for the port should be 0.
1744 EXPECT_EQ(1, QuicStreamFactoryPeer::GetNumberOfLossyConnections(
1745 &factory_, server2.port()));
1746 EXPECT_FALSE(
1747 factory_.OnHandshakeConfirmed(session2, /*packet_loss_rate=*/0.9f));
1748 EXPECT_EQ(0, QuicStreamFactoryPeer::GetNumberOfLossyConnections(
1749 &factory_, server2.port()));
1750 EXPECT_FALSE(
1751 QuicStreamFactoryPeer::IsQuicDisabled(&factory_, server2.port()));
1753 // Set packet_loss_rate to a higher value than packet_loss_threshold only once
1754 // and that shouldn't close the session and it shouldn't disable QUIC.
1755 EXPECT_FALSE(
1756 factory_.OnHandshakeConfirmed(session2, /*packet_loss_rate=*/1.0f));
1757 EXPECT_EQ(1, QuicStreamFactoryPeer::GetNumberOfLossyConnections(
1758 &factory_, server2.port()));
1759 EXPECT_TRUE(session2->connection()->connected());
1760 EXPECT_FALSE(
1761 QuicStreamFactoryPeer::IsQuicDisabled(&factory_, server2.port()));
1762 EXPECT_TRUE(
1763 QuicStreamFactoryPeer::HasActiveSession(&factory_, server2, is_https_));
1765 DVLOG(1) << "Create 3rd session which also has packet loss";
1767 TestCompletionCallback callback3;
1768 QuicStreamRequest request3(&factory_);
1769 EXPECT_EQ(OK, request3.Request(server3, is_https_, privacy_mode_,
1770 /*cert_verify_flags=*/0, server3.host(), "GET",
1771 net_log_, callback3.callback()));
1772 QuicChromiumClientSession* session3 =
1773 QuicStreamFactoryPeer::GetActiveSession(&factory_, server3, is_https_);
1775 DVLOG(1) << "Create 4th session with packet loss and test IsQuicDisabled()";
1776 TestCompletionCallback callback4;
1777 QuicStreamRequest request4(&factory_);
1778 EXPECT_EQ(OK, request4.Request(server4, is_https_, privacy_mode_,
1779 /*cert_verify_flags=*/0, server4.host(), "GET",
1780 net_log_, callback4.callback()));
1781 QuicChromiumClientSession* session4 =
1782 QuicStreamFactoryPeer::GetActiveSession(&factory_, server4, is_https_);
1784 // Set packet_loss_rate to higher value than packet_loss_threshold 2nd time in
1785 // a row and that should close the session and disable QUIC.
1786 EXPECT_TRUE(
1787 factory_.OnHandshakeConfirmed(session3, /*packet_loss_rate=*/1.0f));
1788 EXPECT_EQ(2, QuicStreamFactoryPeer::GetNumberOfLossyConnections(
1789 &factory_, server3.port()));
1790 EXPECT_FALSE(session3->connection()->connected());
1791 EXPECT_TRUE(QuicStreamFactoryPeer::IsQuicDisabled(&factory_, server3.port()));
1792 EXPECT_FALSE(
1793 QuicStreamFactoryPeer::HasActiveSession(&factory_, server3, is_https_));
1794 EXPECT_FALSE(HasActiveSession(server3));
1796 // Set packet_loss_rate to higher value than packet_loss_threshold 3rd time in
1797 // a row and IsQuicDisabled() should close the session.
1798 EXPECT_TRUE(
1799 factory_.OnHandshakeConfirmed(session4, /*packet_loss_rate=*/1.0f));
1800 EXPECT_EQ(3, QuicStreamFactoryPeer::GetNumberOfLossyConnections(
1801 &factory_, server4.port()));
1802 EXPECT_FALSE(session4->connection()->connected());
1803 EXPECT_TRUE(QuicStreamFactoryPeer::IsQuicDisabled(&factory_, server4.port()));
1804 EXPECT_FALSE(
1805 QuicStreamFactoryPeer::HasActiveSession(&factory_, server4, is_https_));
1806 EXPECT_FALSE(HasActiveSession(server4));
1808 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
1809 EXPECT_TRUE(stream.get());
1810 scoped_ptr<QuicHttpStream> stream2 = request2.ReleaseStream();
1811 EXPECT_TRUE(stream2.get());
1812 scoped_ptr<QuicHttpStream> stream3 = request3.ReleaseStream();
1813 EXPECT_TRUE(stream3.get());
1814 scoped_ptr<QuicHttpStream> stream4 = request4.ReleaseStream();
1815 EXPECT_TRUE(stream4.get());
1816 EXPECT_TRUE(socket_data.AllReadDataConsumed());
1817 EXPECT_TRUE(socket_data.AllWriteDataConsumed());
1818 EXPECT_TRUE(socket_data2.AllReadDataConsumed());
1819 EXPECT_TRUE(socket_data2.AllWriteDataConsumed());
1820 EXPECT_TRUE(socket_data3.AllReadDataConsumed());
1821 EXPECT_TRUE(socket_data3.AllWriteDataConsumed());
1822 EXPECT_TRUE(socket_data4.AllReadDataConsumed());
1823 EXPECT_TRUE(socket_data4.AllWriteDataConsumed());
1826 TEST_P(QuicStreamFactoryTest, PublicResetPostHandshakeTwoOfTwo) {
1827 factory_.set_quic_server_info_factory(&quic_server_info_factory_);
1828 QuicStreamFactoryPeer::SetTaskRunner(&factory_, runner_.get());
1829 QuicStreamFactoryPeer::SetDisableDiskCache(&factory_, true);
1830 QuicStreamFactoryPeer::SetThresholdPublicResetsPostHandshake(&factory_, 2);
1831 EXPECT_FALSE(
1832 QuicStreamFactoryPeer::IsQuicDisabled(&factory_, host_port_pair_.port()));
1833 EXPECT_EQ(0, QuicStreamFactoryPeer::GetNumberOfLossyConnections(
1834 &factory_, host_port_pair_.port()));
1836 MockRead reads[] = {
1837 MockRead(ASYNC, OK, 0) // EOF
1839 DeterministicSocketData socket_data(reads, arraysize(reads), nullptr, 0);
1840 socket_factory_.AddSocketDataProvider(&socket_data);
1841 socket_data.StopAfter(1);
1843 DeterministicSocketData socket_data2(reads, arraysize(reads), nullptr, 0);
1844 socket_factory_.AddSocketDataProvider(&socket_data2);
1845 socket_data2.StopAfter(1);
1847 HostPortPair server2("mail.example.org", kDefaultServerPort);
1849 crypto_client_stream_factory_.set_handshake_mode(
1850 MockCryptoClientStream::CONFIRM_HANDSHAKE);
1851 host_resolver_.set_synchronous_mode(true);
1852 host_resolver_.rules()->AddIPLiteralRule(host_port_pair_.host(),
1853 "192.168.0.1", "");
1854 host_resolver_.rules()->AddIPLiteralRule(server2.host(), "192.168.0.1", "");
1856 QuicStreamRequest request(&factory_);
1857 EXPECT_EQ(OK, request.Request(host_port_pair_, is_https_, privacy_mode_,
1858 /*cert_verify_flags=*/0, host_port_pair_.host(),
1859 "GET", net_log_, callback_.callback()));
1861 QuicChromiumClientSession* session = QuicStreamFactoryPeer::GetActiveSession(
1862 &factory_, host_port_pair_, is_https_);
1864 DVLOG(1) << "Created 1st session. Now trigger public reset post handshake";
1865 session->connection()->CloseConnection(QUIC_PUBLIC_RESET, true);
1866 // Need to spin the loop now to ensure that
1867 // QuicStreamFactory::OnSessionClosed() runs.
1868 base::RunLoop run_loop;
1869 run_loop.RunUntilIdle();
1871 EXPECT_EQ(1,
1872 QuicStreamFactoryPeer::GetNumPublicResetsPostHandshake(&factory_));
1873 EXPECT_FALSE(
1874 QuicStreamFactoryPeer::IsQuicDisabled(&factory_, host_port_pair_.port()));
1876 // Test two-in-a-row public reset post handshakes..
1877 DVLOG(1) << "Create 2nd session and trigger public reset post handshake";
1878 TestCompletionCallback callback2;
1879 QuicStreamRequest request2(&factory_);
1880 EXPECT_EQ(OK, request2.Request(server2, is_https_, privacy_mode_,
1881 /*cert_verify_flags=*/0, server2.host(), "GET",
1882 net_log_, callback2.callback()));
1883 QuicChromiumClientSession* session2 =
1884 QuicStreamFactoryPeer::GetActiveSession(&factory_, server2, is_https_);
1886 session2->connection()->CloseConnection(QUIC_PUBLIC_RESET, true);
1887 // Need to spin the loop now to ensure that
1888 // QuicStreamFactory::OnSessionClosed() runs.
1889 base::RunLoop run_loop2;
1890 run_loop2.RunUntilIdle();
1891 EXPECT_EQ(2,
1892 QuicStreamFactoryPeer::GetNumPublicResetsPostHandshake(&factory_));
1893 EXPECT_TRUE(
1894 QuicStreamFactoryPeer::IsQuicDisabled(&factory_, host_port_pair_.port()));
1895 EXPECT_EQ(
1896 QuicChromiumClientSession::QUIC_DISABLED_PUBLIC_RESET_POST_HANDSHAKE,
1897 factory_.QuicDisabledReason(host_port_pair_.port()));
1899 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
1900 EXPECT_TRUE(stream.get());
1901 scoped_ptr<QuicHttpStream> stream2 = request2.ReleaseStream();
1902 EXPECT_TRUE(stream2.get());
1903 EXPECT_TRUE(socket_data.AllReadDataConsumed());
1904 EXPECT_TRUE(socket_data.AllWriteDataConsumed());
1905 EXPECT_TRUE(socket_data2.AllReadDataConsumed());
1906 EXPECT_TRUE(socket_data2.AllWriteDataConsumed());
1909 TEST_P(QuicStreamFactoryTest, TimeoutsWithOpenStreamsTwoOfTwo) {
1910 factory_.set_quic_server_info_factory(&quic_server_info_factory_);
1911 QuicStreamFactoryPeer::SetTaskRunner(&factory_, runner_.get());
1912 QuicStreamFactoryPeer::SetDisableDiskCache(&factory_, true);
1913 QuicStreamFactoryPeer::SetThresholdTimeoutsWithOpenStreams(&factory_, 2);
1914 EXPECT_FALSE(
1915 QuicStreamFactoryPeer::IsQuicDisabled(&factory_, host_port_pair_.port()));
1916 EXPECT_EQ(0, QuicStreamFactoryPeer::GetNumberOfLossyConnections(
1917 &factory_, host_port_pair_.port()));
1919 MockRead reads[] = {
1920 MockRead(ASYNC, OK, 0) // EOF
1922 DeterministicSocketData socket_data(reads, arraysize(reads), nullptr, 0);
1923 socket_factory_.AddSocketDataProvider(&socket_data);
1924 socket_data.StopAfter(1);
1926 DeterministicSocketData socket_data2(reads, arraysize(reads), nullptr, 0);
1927 socket_factory_.AddSocketDataProvider(&socket_data2);
1928 socket_data2.StopAfter(1);
1930 HostPortPair server2("mail.example.org", kDefaultServerPort);
1932 crypto_client_stream_factory_.set_handshake_mode(
1933 MockCryptoClientStream::CONFIRM_HANDSHAKE);
1934 host_resolver_.set_synchronous_mode(true);
1935 host_resolver_.rules()->AddIPLiteralRule(host_port_pair_.host(),
1936 "192.168.0.1", "");
1937 host_resolver_.rules()->AddIPLiteralRule(server2.host(), "192.168.0.1", "");
1939 QuicStreamRequest request(&factory_);
1940 EXPECT_EQ(OK, request.Request(host_port_pair_, is_https_, privacy_mode_,
1941 /*cert_verify_flags=*/0, host_port_pair_.host(),
1942 "GET", net_log_, callback_.callback()));
1944 QuicChromiumClientSession* session = QuicStreamFactoryPeer::GetActiveSession(
1945 &factory_, host_port_pair_, is_https_);
1947 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
1948 EXPECT_TRUE(stream.get());
1949 HttpRequestInfo request_info;
1950 EXPECT_EQ(OK, stream->InitializeStream(&request_info, DEFAULT_PRIORITY,
1951 net_log_, CompletionCallback()));
1953 DVLOG(1)
1954 << "Created 1st session and initialized a stream. Now trigger timeout";
1955 session->connection()->CloseConnection(QUIC_CONNECTION_TIMED_OUT, false);
1956 // Need to spin the loop now to ensure that
1957 // QuicStreamFactory::OnSessionClosed() runs.
1958 base::RunLoop run_loop;
1959 run_loop.RunUntilIdle();
1961 EXPECT_EQ(1, QuicStreamFactoryPeer::GetNumTimeoutsWithOpenStreams(&factory_));
1962 EXPECT_FALSE(
1963 QuicStreamFactoryPeer::IsQuicDisabled(&factory_, host_port_pair_.port()));
1965 // Test two-in-a-row timeouts with open streams.
1966 DVLOG(1) << "Create 2nd session and timeout with open stream";
1967 TestCompletionCallback callback2;
1968 QuicStreamRequest request2(&factory_);
1969 EXPECT_EQ(OK, request2.Request(server2, is_https_, privacy_mode_,
1970 /*cert_verify_flags=*/0, server2.host(), "GET",
1971 net_log_, callback2.callback()));
1972 QuicChromiumClientSession* session2 =
1973 QuicStreamFactoryPeer::GetActiveSession(&factory_, server2, is_https_);
1975 scoped_ptr<QuicHttpStream> stream2 = request2.ReleaseStream();
1976 EXPECT_TRUE(stream2.get());
1977 EXPECT_EQ(OK, stream2->InitializeStream(&request_info, DEFAULT_PRIORITY,
1978 net_log_, CompletionCallback()));
1980 session2->connection()->CloseConnection(QUIC_CONNECTION_TIMED_OUT, false);
1981 // Need to spin the loop now to ensure that
1982 // QuicStreamFactory::OnSessionClosed() runs.
1983 base::RunLoop run_loop2;
1984 run_loop2.RunUntilIdle();
1985 EXPECT_EQ(2, QuicStreamFactoryPeer::GetNumTimeoutsWithOpenStreams(&factory_));
1986 EXPECT_TRUE(
1987 QuicStreamFactoryPeer::IsQuicDisabled(&factory_, host_port_pair_.port()));
1988 EXPECT_EQ(QuicChromiumClientSession::QUIC_DISABLED_TIMEOUT_WITH_OPEN_STREAMS,
1989 factory_.QuicDisabledReason(host_port_pair_.port()));
1991 EXPECT_TRUE(socket_data.AllReadDataConsumed());
1992 EXPECT_TRUE(socket_data.AllWriteDataConsumed());
1993 EXPECT_TRUE(socket_data2.AllReadDataConsumed());
1994 EXPECT_TRUE(socket_data2.AllWriteDataConsumed());
1997 TEST_P(QuicStreamFactoryTest, PublicResetPostHandshakeTwoOfThree) {
1998 factory_.set_quic_server_info_factory(&quic_server_info_factory_);
1999 QuicStreamFactoryPeer::SetTaskRunner(&factory_, runner_.get());
2000 QuicStreamFactoryPeer::SetDisableDiskCache(&factory_, true);
2001 QuicStreamFactoryPeer::SetThresholdPublicResetsPostHandshake(&factory_, 2);
2002 EXPECT_FALSE(
2003 QuicStreamFactoryPeer::IsQuicDisabled(&factory_, host_port_pair_.port()));
2004 EXPECT_EQ(0, QuicStreamFactoryPeer::GetNumberOfLossyConnections(
2005 &factory_, host_port_pair_.port()));
2007 MockRead reads[] = {
2008 MockRead(ASYNC, OK, 0) // EOF
2010 DeterministicSocketData socket_data(reads, arraysize(reads), nullptr, 0);
2011 socket_factory_.AddSocketDataProvider(&socket_data);
2012 socket_data.StopAfter(1);
2014 DeterministicSocketData socket_data2(reads, arraysize(reads), nullptr, 0);
2015 socket_factory_.AddSocketDataProvider(&socket_data2);
2016 socket_data2.StopAfter(1);
2018 DeterministicSocketData socket_data3(reads, arraysize(reads), nullptr, 0);
2019 socket_factory_.AddSocketDataProvider(&socket_data3);
2020 socket_data3.StopAfter(1);
2022 HostPortPair server2("mail.example.org", kDefaultServerPort);
2023 HostPortPair server3("docs.example.org", kDefaultServerPort);
2025 crypto_client_stream_factory_.set_handshake_mode(
2026 MockCryptoClientStream::CONFIRM_HANDSHAKE);
2027 host_resolver_.set_synchronous_mode(true);
2028 host_resolver_.rules()->AddIPLiteralRule(host_port_pair_.host(),
2029 "192.168.0.1", "");
2030 host_resolver_.rules()->AddIPLiteralRule(server2.host(), "192.168.0.1", "");
2031 host_resolver_.rules()->AddIPLiteralRule(server3.host(), "192.168.0.1", "");
2033 // Test first and third out of three public reset post handshakes.
2034 QuicStreamRequest request(&factory_);
2035 EXPECT_EQ(OK, request.Request(host_port_pair_, is_https_, privacy_mode_,
2036 /*cert_verify_flags=*/0, host_port_pair_.host(),
2037 "GET", net_log_, callback_.callback()));
2039 QuicChromiumClientSession* session = QuicStreamFactoryPeer::GetActiveSession(
2040 &factory_, host_port_pair_, is_https_);
2042 DVLOG(1) << "Created 1st session. Now trigger public reset post handshake";
2043 session->connection()->CloseConnection(QUIC_PUBLIC_RESET, true);
2044 // Need to spin the loop now to ensure that
2045 // QuicStreamFactory::OnSessionClosed() runs.
2046 base::RunLoop run_loop;
2047 run_loop.RunUntilIdle();
2049 EXPECT_EQ(1,
2050 QuicStreamFactoryPeer::GetNumPublicResetsPostHandshake(&factory_));
2051 EXPECT_FALSE(
2052 QuicStreamFactoryPeer::IsQuicDisabled(&factory_, host_port_pair_.port()));
2054 DVLOG(1) << "Create 2nd session without disable trigger";
2055 TestCompletionCallback callback2;
2056 QuicStreamRequest request2(&factory_);
2057 EXPECT_EQ(OK, request2.Request(server2, is_https_, privacy_mode_,
2058 /*cert_verify_flags=*/0, server2.host(), "GET",
2059 net_log_, callback2.callback()));
2060 QuicChromiumClientSession* session2 =
2061 QuicStreamFactoryPeer::GetActiveSession(&factory_, server2, is_https_);
2063 session2->connection()->CloseConnection(QUIC_NO_ERROR, false);
2064 // Need to spin the loop now to ensure that
2065 // QuicStreamFactory::OnSessionClosed() runs.
2066 base::RunLoop run_loop2;
2067 run_loop2.RunUntilIdle();
2068 EXPECT_EQ(1,
2069 QuicStreamFactoryPeer::GetNumPublicResetsPostHandshake(&factory_));
2070 EXPECT_FALSE(
2071 QuicStreamFactoryPeer::IsQuicDisabled(&factory_, host_port_pair_.port()));
2073 DVLOG(1) << "Create 3rd session with public reset post handshake,"
2074 << " will disable QUIC";
2075 TestCompletionCallback callback3;
2076 QuicStreamRequest request3(&factory_);
2077 EXPECT_EQ(OK, request3.Request(server3, is_https_, privacy_mode_,
2078 /*cert_verify_flags=*/0, server3.host(), "GET",
2079 net_log_, callback3.callback()));
2080 QuicChromiumClientSession* session3 =
2081 QuicStreamFactoryPeer::GetActiveSession(&factory_, server3, is_https_);
2083 session3->connection()->CloseConnection(QUIC_PUBLIC_RESET, true);
2084 // Need to spin the loop now to ensure that
2085 // QuicStreamFactory::OnSessionClosed() runs.
2086 base::RunLoop run_loop3;
2087 run_loop3.RunUntilIdle();
2088 EXPECT_EQ(2,
2089 QuicStreamFactoryPeer::GetNumPublicResetsPostHandshake(&factory_));
2090 EXPECT_TRUE(
2091 QuicStreamFactoryPeer::IsQuicDisabled(&factory_, host_port_pair_.port()));
2092 EXPECT_EQ(
2093 QuicChromiumClientSession::QUIC_DISABLED_PUBLIC_RESET_POST_HANDSHAKE,
2094 factory_.QuicDisabledReason(host_port_pair_.port()));
2096 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
2097 EXPECT_TRUE(stream.get());
2098 scoped_ptr<QuicHttpStream> stream2 = request2.ReleaseStream();
2099 EXPECT_TRUE(stream2.get());
2100 scoped_ptr<QuicHttpStream> stream3 = request3.ReleaseStream();
2101 EXPECT_TRUE(stream3.get());
2103 EXPECT_TRUE(socket_data.AllReadDataConsumed());
2104 EXPECT_TRUE(socket_data.AllWriteDataConsumed());
2105 EXPECT_TRUE(socket_data2.AllReadDataConsumed());
2106 EXPECT_TRUE(socket_data2.AllWriteDataConsumed());
2107 EXPECT_TRUE(socket_data3.AllReadDataConsumed());
2108 EXPECT_TRUE(socket_data3.AllWriteDataConsumed());
2111 TEST_P(QuicStreamFactoryTest, TimeoutsWithOpenStreamsTwoOfThree) {
2112 factory_.set_quic_server_info_factory(&quic_server_info_factory_);
2113 QuicStreamFactoryPeer::SetTaskRunner(&factory_, runner_.get());
2114 QuicStreamFactoryPeer::SetDisableDiskCache(&factory_, true);
2115 QuicStreamFactoryPeer::SetThresholdTimeoutsWithOpenStreams(&factory_, 2);
2116 EXPECT_FALSE(
2117 QuicStreamFactoryPeer::IsQuicDisabled(&factory_, host_port_pair_.port()));
2118 EXPECT_EQ(0, QuicStreamFactoryPeer::GetNumberOfLossyConnections(
2119 &factory_, host_port_pair_.port()));
2121 MockRead reads[] = {
2122 MockRead(ASYNC, OK, 0) // EOF
2124 DeterministicSocketData socket_data(reads, arraysize(reads), nullptr, 0);
2125 socket_factory_.AddSocketDataProvider(&socket_data);
2126 socket_data.StopAfter(1);
2128 // DeterministicSocketData socket_data2(nullptr, 0, nullptr, 0);
2129 DeterministicSocketData socket_data2(reads, arraysize(reads), nullptr, 0);
2130 socket_factory_.AddSocketDataProvider(&socket_data2);
2131 socket_data2.StopAfter(1);
2133 DeterministicSocketData socket_data3(reads, arraysize(reads), nullptr, 0);
2134 socket_factory_.AddSocketDataProvider(&socket_data3);
2135 socket_data3.StopAfter(1);
2137 HostPortPair server2("mail.example.org", kDefaultServerPort);
2138 HostPortPair server3("docs.example.org", kDefaultServerPort);
2140 crypto_client_stream_factory_.set_handshake_mode(
2141 MockCryptoClientStream::CONFIRM_HANDSHAKE);
2142 host_resolver_.set_synchronous_mode(true);
2143 host_resolver_.rules()->AddIPLiteralRule(host_port_pair_.host(),
2144 "192.168.0.1", "");
2145 host_resolver_.rules()->AddIPLiteralRule(server2.host(), "192.168.0.1", "");
2146 host_resolver_.rules()->AddIPLiteralRule(server3.host(), "192.168.0.1", "");
2148 // Test first and third out of three timeouts with open streams.
2149 QuicStreamRequest request(&factory_);
2150 EXPECT_EQ(OK, request.Request(host_port_pair_, is_https_, privacy_mode_,
2151 /*cert_verify_flags=*/0, host_port_pair_.host(),
2152 "GET", net_log_, callback_.callback()));
2154 QuicChromiumClientSession* session = QuicStreamFactoryPeer::GetActiveSession(
2155 &factory_, host_port_pair_, is_https_);
2157 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
2158 EXPECT_TRUE(stream.get());
2159 HttpRequestInfo request_info;
2160 EXPECT_EQ(OK, stream->InitializeStream(&request_info, DEFAULT_PRIORITY,
2161 net_log_, CompletionCallback()));
2163 DVLOG(1)
2164 << "Created 1st session and initialized a stream. Now trigger timeout";
2165 session->connection()->CloseConnection(QUIC_CONNECTION_TIMED_OUT, false);
2166 // Need to spin the loop now to ensure that
2167 // QuicStreamFactory::OnSessionClosed() runs.
2168 base::RunLoop run_loop;
2169 run_loop.RunUntilIdle();
2171 EXPECT_EQ(1, QuicStreamFactoryPeer::GetNumTimeoutsWithOpenStreams(&factory_));
2172 EXPECT_FALSE(
2173 QuicStreamFactoryPeer::IsQuicDisabled(&factory_, host_port_pair_.port()));
2175 // Test two-in-a-row timeouts with open streams.
2176 DVLOG(1) << "Create 2nd session without timeout";
2177 TestCompletionCallback callback2;
2178 QuicStreamRequest request2(&factory_);
2179 EXPECT_EQ(OK, request2.Request(server2, is_https_, privacy_mode_,
2180 /*cert_verify_flags=*/0, server2.host(), "GET",
2181 net_log_, callback2.callback()));
2182 QuicChromiumClientSession* session2 =
2183 QuicStreamFactoryPeer::GetActiveSession(&factory_, server2, is_https_);
2185 session2->connection()->CloseConnection(QUIC_NO_ERROR, true);
2186 // Need to spin the loop now to ensure that
2187 // QuicStreamFactory::OnSessionClosed() runs.
2188 base::RunLoop run_loop2;
2189 run_loop2.RunUntilIdle();
2190 EXPECT_EQ(1, QuicStreamFactoryPeer::GetNumTimeoutsWithOpenStreams(&factory_));
2191 EXPECT_FALSE(
2192 QuicStreamFactoryPeer::IsQuicDisabled(&factory_, host_port_pair_.port()));
2194 DVLOG(1) << "Create 3rd session with timeout with open streams,"
2195 << " will disable QUIC";
2197 TestCompletionCallback callback3;
2198 QuicStreamRequest request3(&factory_);
2199 EXPECT_EQ(OK, request3.Request(server3, is_https_, privacy_mode_,
2200 /*cert_verify_flags=*/0, server3.host(), "GET",
2201 net_log_, callback3.callback()));
2202 QuicChromiumClientSession* session3 =
2203 QuicStreamFactoryPeer::GetActiveSession(&factory_, server3, is_https_);
2205 scoped_ptr<QuicHttpStream> stream3 = request3.ReleaseStream();
2206 EXPECT_TRUE(stream3.get());
2207 EXPECT_EQ(OK, stream3->InitializeStream(&request_info, DEFAULT_PRIORITY,
2208 net_log_, CompletionCallback()));
2209 session3->connection()->CloseConnection(QUIC_CONNECTION_TIMED_OUT, false);
2210 // Need to spin the loop now to ensure that
2211 // QuicStreamFactory::OnSessionClosed() runs.
2212 base::RunLoop run_loop3;
2213 run_loop3.RunUntilIdle();
2214 EXPECT_EQ(2, QuicStreamFactoryPeer::GetNumTimeoutsWithOpenStreams(&factory_));
2215 EXPECT_TRUE(
2216 QuicStreamFactoryPeer::IsQuicDisabled(&factory_, host_port_pair_.port()));
2217 EXPECT_EQ(QuicChromiumClientSession::QUIC_DISABLED_TIMEOUT_WITH_OPEN_STREAMS,
2218 factory_.QuicDisabledReason(host_port_pair_.port()));
2220 scoped_ptr<QuicHttpStream> stream2 = request2.ReleaseStream();
2221 EXPECT_TRUE(stream2.get());
2222 EXPECT_TRUE(socket_data.AllReadDataConsumed());
2223 EXPECT_TRUE(socket_data.AllWriteDataConsumed());
2224 EXPECT_TRUE(socket_data2.AllReadDataConsumed());
2225 EXPECT_TRUE(socket_data2.AllWriteDataConsumed());
2226 EXPECT_TRUE(socket_data3.AllReadDataConsumed());
2227 EXPECT_TRUE(socket_data3.AllWriteDataConsumed());
2230 TEST_P(QuicStreamFactoryTest, PublicResetPostHandshakeTwoOfFour) {
2231 factory_.set_quic_server_info_factory(&quic_server_info_factory_);
2232 QuicStreamFactoryPeer::SetTaskRunner(&factory_, runner_.get());
2233 QuicStreamFactoryPeer::SetDisableDiskCache(&factory_, true);
2234 QuicStreamFactoryPeer::SetThresholdPublicResetsPostHandshake(&factory_, 2);
2235 EXPECT_FALSE(
2236 QuicStreamFactoryPeer::IsQuicDisabled(&factory_, host_port_pair_.port()));
2237 EXPECT_EQ(0, QuicStreamFactoryPeer::GetNumberOfLossyConnections(
2238 &factory_, host_port_pair_.port()));
2240 MockRead reads[] = {
2241 MockRead(ASYNC, OK, 0) // EOF
2243 DeterministicSocketData socket_data(reads, arraysize(reads), nullptr, 0);
2244 socket_factory_.AddSocketDataProvider(&socket_data);
2245 socket_data.StopAfter(1);
2247 DeterministicSocketData socket_data2(reads, arraysize(reads), nullptr, 0);
2248 socket_factory_.AddSocketDataProvider(&socket_data2);
2249 socket_data2.StopAfter(1);
2251 DeterministicSocketData socket_data3(reads, arraysize(reads), nullptr, 0);
2252 socket_factory_.AddSocketDataProvider(&socket_data3);
2253 socket_data3.StopAfter(1);
2255 DeterministicSocketData socket_data4(reads, arraysize(reads), nullptr, 0);
2256 socket_factory_.AddSocketDataProvider(&socket_data4);
2257 socket_data4.StopAfter(1);
2259 HostPortPair server2("mail.example.org", kDefaultServerPort);
2260 HostPortPair server3("docs.example.org", kDefaultServerPort);
2261 HostPortPair server4("images.example.org", kDefaultServerPort);
2263 crypto_client_stream_factory_.set_handshake_mode(
2264 MockCryptoClientStream::CONFIRM_HANDSHAKE);
2265 host_resolver_.set_synchronous_mode(true);
2266 host_resolver_.rules()->AddIPLiteralRule(host_port_pair_.host(),
2267 "192.168.0.1", "");
2268 host_resolver_.rules()->AddIPLiteralRule(server2.host(), "192.168.0.1", "");
2269 host_resolver_.rules()->AddIPLiteralRule(server3.host(), "192.168.0.1", "");
2270 host_resolver_.rules()->AddIPLiteralRule(server4.host(), "192.168.0.1", "");
2272 // Test first and fourth out of four public reset post handshakes.
2273 QuicStreamRequest request(&factory_);
2274 EXPECT_EQ(OK, request.Request(host_port_pair_, is_https_, privacy_mode_,
2275 /*cert_verify_flags=*/0, host_port_pair_.host(),
2276 "GET", net_log_, callback_.callback()));
2278 QuicChromiumClientSession* session = QuicStreamFactoryPeer::GetActiveSession(
2279 &factory_, host_port_pair_, is_https_);
2281 DVLOG(1) << "Created 1st session. Now trigger public reset post handshake";
2282 session->connection()->CloseConnection(QUIC_PUBLIC_RESET, true);
2283 // Need to spin the loop now to ensure that
2284 // QuicStreamFactory::OnSessionClosed() runs.
2285 base::RunLoop run_loop;
2286 run_loop.RunUntilIdle();
2288 EXPECT_EQ(1,
2289 QuicStreamFactoryPeer::GetNumPublicResetsPostHandshake(&factory_));
2290 EXPECT_FALSE(
2291 QuicStreamFactoryPeer::IsQuicDisabled(&factory_, host_port_pair_.port()));
2293 DVLOG(1) << "Create 2nd and 3rd sessions without disable trigger";
2294 TestCompletionCallback callback2;
2295 QuicStreamRequest request2(&factory_);
2296 EXPECT_EQ(OK, request2.Request(server2, is_https_, privacy_mode_,
2297 /*cert_verify_flags=*/0, server2.host(), "GET",
2298 net_log_, callback2.callback()));
2299 QuicChromiumClientSession* session2 =
2300 QuicStreamFactoryPeer::GetActiveSession(&factory_, server2, is_https_);
2302 session2->connection()->CloseConnection(QUIC_NO_ERROR, false);
2303 // Need to spin the loop now to ensure that
2304 // QuicStreamFactory::OnSessionClosed() runs.
2305 base::RunLoop run_loop2;
2306 run_loop2.RunUntilIdle();
2307 EXPECT_EQ(1,
2308 QuicStreamFactoryPeer::GetNumPublicResetsPostHandshake(&factory_));
2309 EXPECT_FALSE(
2310 QuicStreamFactoryPeer::IsQuicDisabled(&factory_, host_port_pair_.port()));
2312 TestCompletionCallback callback3;
2313 QuicStreamRequest request3(&factory_);
2314 EXPECT_EQ(OK, request3.Request(server3, is_https_, privacy_mode_,
2315 /*cert_verify_flags=*/0, server3.host(), "GET",
2316 net_log_, callback3.callback()));
2317 QuicChromiumClientSession* session3 =
2318 QuicStreamFactoryPeer::GetActiveSession(&factory_, server3, is_https_);
2320 session3->connection()->CloseConnection(QUIC_NO_ERROR, false);
2321 // Need to spin the loop now to ensure that
2322 // QuicStreamFactory::OnSessionClosed() runs.
2323 base::RunLoop run_loop3;
2324 run_loop3.RunUntilIdle();
2325 EXPECT_EQ(1,
2326 QuicStreamFactoryPeer::GetNumPublicResetsPostHandshake(&factory_));
2327 EXPECT_FALSE(
2328 QuicStreamFactoryPeer::IsQuicDisabled(&factory_, host_port_pair_.port()));
2330 DVLOG(1) << "Create 4rd session with public reset post handshake,"
2331 << " will not disable QUIC";
2332 TestCompletionCallback callback4;
2333 QuicStreamRequest request4(&factory_);
2334 EXPECT_EQ(OK, request4.Request(server4, is_https_, privacy_mode_,
2335 /*cert_verify_flags=*/0, server4.host(), "GET",
2336 net_log_, callback4.callback()));
2337 QuicChromiumClientSession* session4 =
2338 QuicStreamFactoryPeer::GetActiveSession(&factory_, server4, is_https_);
2340 session4->connection()->CloseConnection(QUIC_PUBLIC_RESET, true);
2341 // Need to spin the loop now to ensure that
2342 // QuicStreamFactory::OnSessionClosed() runs.
2343 base::RunLoop run_loop4;
2344 run_loop4.RunUntilIdle();
2345 EXPECT_EQ(1,
2346 QuicStreamFactoryPeer::GetNumPublicResetsPostHandshake(&factory_));
2347 EXPECT_FALSE(
2348 QuicStreamFactoryPeer::IsQuicDisabled(&factory_, host_port_pair_.port()));
2350 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
2351 EXPECT_TRUE(stream.get());
2352 scoped_ptr<QuicHttpStream> stream2 = request2.ReleaseStream();
2353 EXPECT_TRUE(stream2.get());
2354 scoped_ptr<QuicHttpStream> stream3 = request3.ReleaseStream();
2355 EXPECT_TRUE(stream3.get());
2356 scoped_ptr<QuicHttpStream> stream4 = request4.ReleaseStream();
2357 EXPECT_TRUE(stream4.get());
2359 EXPECT_TRUE(socket_data.AllReadDataConsumed());
2360 EXPECT_TRUE(socket_data.AllWriteDataConsumed());
2361 EXPECT_TRUE(socket_data2.AllReadDataConsumed());
2362 EXPECT_TRUE(socket_data2.AllWriteDataConsumed());
2363 EXPECT_TRUE(socket_data3.AllReadDataConsumed());
2364 EXPECT_TRUE(socket_data3.AllWriteDataConsumed());
2365 EXPECT_TRUE(socket_data4.AllReadDataConsumed());
2366 EXPECT_TRUE(socket_data4.AllWriteDataConsumed());
2369 TEST_P(QuicStreamFactoryTest, TimeoutsWithOpenStreamsTwoOfFour) {
2370 factory_.set_quic_server_info_factory(&quic_server_info_factory_);
2371 QuicStreamFactoryPeer::SetTaskRunner(&factory_, runner_.get());
2372 QuicStreamFactoryPeer::SetDisableDiskCache(&factory_, true);
2373 QuicStreamFactoryPeer::SetThresholdTimeoutsWithOpenStreams(&factory_, 2);
2374 EXPECT_FALSE(
2375 QuicStreamFactoryPeer::IsQuicDisabled(&factory_, host_port_pair_.port()));
2376 EXPECT_EQ(0, QuicStreamFactoryPeer::GetNumberOfLossyConnections(
2377 &factory_, host_port_pair_.port()));
2379 MockRead reads[] = {
2380 MockRead(ASYNC, OK, 0) // EOF
2382 DeterministicSocketData socket_data(reads, arraysize(reads), nullptr, 0);
2383 socket_factory_.AddSocketDataProvider(&socket_data);
2384 socket_data.StopAfter(1);
2386 // DeterministicSocketData socket_data2(nullptr, 0, nullptr, 0);
2387 DeterministicSocketData socket_data2(reads, arraysize(reads), nullptr, 0);
2388 socket_factory_.AddSocketDataProvider(&socket_data2);
2389 socket_data2.StopAfter(1);
2391 DeterministicSocketData socket_data3(reads, arraysize(reads), nullptr, 0);
2392 socket_factory_.AddSocketDataProvider(&socket_data3);
2393 socket_data3.StopAfter(1);
2395 DeterministicSocketData socket_data4(reads, arraysize(reads), nullptr, 0);
2396 socket_factory_.AddSocketDataProvider(&socket_data4);
2397 socket_data4.StopAfter(1);
2399 HostPortPair server2("mail.example.org", kDefaultServerPort);
2400 HostPortPair server3("docs.example.org", kDefaultServerPort);
2401 HostPortPair server4("images.example.org", kDefaultServerPort);
2403 crypto_client_stream_factory_.set_handshake_mode(
2404 MockCryptoClientStream::CONFIRM_HANDSHAKE);
2405 host_resolver_.set_synchronous_mode(true);
2406 host_resolver_.rules()->AddIPLiteralRule(host_port_pair_.host(),
2407 "192.168.0.1", "");
2408 host_resolver_.rules()->AddIPLiteralRule(server2.host(), "192.168.0.1", "");
2409 host_resolver_.rules()->AddIPLiteralRule(server3.host(), "192.168.0.1", "");
2410 host_resolver_.rules()->AddIPLiteralRule(server4.host(), "192.168.0.1", "");
2412 // Test first and fourth out of three timeouts with open streams.
2413 QuicStreamRequest request(&factory_);
2414 EXPECT_EQ(OK, request.Request(host_port_pair_, is_https_, privacy_mode_,
2415 /*cert_verify_flags=*/0, host_port_pair_.host(),
2416 "GET", net_log_, callback_.callback()));
2418 QuicChromiumClientSession* session = QuicStreamFactoryPeer::GetActiveSession(
2419 &factory_, host_port_pair_, is_https_);
2421 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
2422 EXPECT_TRUE(stream.get());
2423 HttpRequestInfo request_info;
2424 EXPECT_EQ(OK, stream->InitializeStream(&request_info, DEFAULT_PRIORITY,
2425 net_log_, CompletionCallback()));
2427 DVLOG(1)
2428 << "Created 1st session and initialized a stream. Now trigger timeout";
2429 session->connection()->CloseConnection(QUIC_CONNECTION_TIMED_OUT, false);
2430 // Need to spin the loop now to ensure that
2431 // QuicStreamFactory::OnSessionClosed() runs.
2432 base::RunLoop run_loop;
2433 run_loop.RunUntilIdle();
2435 EXPECT_EQ(1, QuicStreamFactoryPeer::GetNumTimeoutsWithOpenStreams(&factory_));
2436 EXPECT_FALSE(
2437 QuicStreamFactoryPeer::IsQuicDisabled(&factory_, host_port_pair_.port()));
2439 DVLOG(1) << "Create 2nd and 3rd sessions without timeout";
2440 TestCompletionCallback callback2;
2441 QuicStreamRequest request2(&factory_);
2442 EXPECT_EQ(OK, request2.Request(server2, is_https_, privacy_mode_,
2443 /*cert_verify_flags=*/0, server2.host(), "GET",
2444 net_log_, callback2.callback()));
2445 QuicChromiumClientSession* session2 =
2446 QuicStreamFactoryPeer::GetActiveSession(&factory_, server2, is_https_);
2448 session2->connection()->CloseConnection(QUIC_NO_ERROR, true);
2449 // Need to spin the loop now to ensure that
2450 // QuicStreamFactory::OnSessionClosed() runs.
2451 base::RunLoop run_loop2;
2452 run_loop2.RunUntilIdle();
2453 EXPECT_EQ(1, QuicStreamFactoryPeer::GetNumTimeoutsWithOpenStreams(&factory_));
2454 EXPECT_FALSE(
2455 QuicStreamFactoryPeer::IsQuicDisabled(&factory_, host_port_pair_.port()));
2457 TestCompletionCallback callback3;
2458 QuicStreamRequest request3(&factory_);
2459 EXPECT_EQ(OK, request3.Request(server3, is_https_, privacy_mode_,
2460 /*cert_verify_flags=*/0, server3.host(), "GET",
2461 net_log_, callback3.callback()));
2462 QuicChromiumClientSession* session3 =
2463 QuicStreamFactoryPeer::GetActiveSession(&factory_, server3, is_https_);
2465 session3->connection()->CloseConnection(QUIC_NO_ERROR, true);
2466 // Need to spin the loop now to ensure that
2467 // QuicStreamFactory::OnSessionClosed() runs.
2468 base::RunLoop run_loop3;
2469 run_loop3.RunUntilIdle();
2470 EXPECT_EQ(1, QuicStreamFactoryPeer::GetNumTimeoutsWithOpenStreams(&factory_));
2471 EXPECT_FALSE(
2472 QuicStreamFactoryPeer::IsQuicDisabled(&factory_, host_port_pair_.port()));
2474 DVLOG(1) << "Create 4th session with timeout with open streams,"
2475 << " will not disable QUIC";
2477 TestCompletionCallback callback4;
2478 QuicStreamRequest request4(&factory_);
2479 EXPECT_EQ(OK, request4.Request(server4, is_https_, privacy_mode_,
2480 /*cert_verify_flags=*/0, server4.host(), "GET",
2481 net_log_, callback4.callback()));
2482 QuicChromiumClientSession* session4 =
2483 QuicStreamFactoryPeer::GetActiveSession(&factory_, server4, is_https_);
2485 scoped_ptr<QuicHttpStream> stream4 = request4.ReleaseStream();
2486 EXPECT_TRUE(stream4.get());
2487 EXPECT_EQ(OK, stream4->InitializeStream(&request_info, DEFAULT_PRIORITY,
2488 net_log_, CompletionCallback()));
2489 session4->connection()->CloseConnection(QUIC_CONNECTION_TIMED_OUT, false);
2490 // Need to spin the loop now to ensure that
2491 // QuicStreamFactory::OnSessionClosed() runs.
2492 base::RunLoop run_loop4;
2493 run_loop4.RunUntilIdle();
2494 EXPECT_EQ(1, QuicStreamFactoryPeer::GetNumTimeoutsWithOpenStreams(&factory_));
2495 EXPECT_FALSE(
2496 QuicStreamFactoryPeer::IsQuicDisabled(&factory_, host_port_pair_.port()));
2498 scoped_ptr<QuicHttpStream> stream2 = request2.ReleaseStream();
2499 EXPECT_TRUE(stream2.get());
2500 scoped_ptr<QuicHttpStream> stream3 = request3.ReleaseStream();
2501 EXPECT_TRUE(stream3.get());
2502 EXPECT_TRUE(socket_data.AllReadDataConsumed());
2503 EXPECT_TRUE(socket_data.AllWriteDataConsumed());
2504 EXPECT_TRUE(socket_data2.AllReadDataConsumed());
2505 EXPECT_TRUE(socket_data2.AllWriteDataConsumed());
2506 EXPECT_TRUE(socket_data3.AllReadDataConsumed());
2507 EXPECT_TRUE(socket_data3.AllWriteDataConsumed());
2508 EXPECT_TRUE(socket_data4.AllReadDataConsumed());
2509 EXPECT_TRUE(socket_data4.AllWriteDataConsumed());
2512 } // namespace test
2513 } // namespace net