Roll src/third_party/WebKit eac3800:0237a66 (svn 202606:202607)
[chromium-blink-merge.git] / net / quic / quic_stream_factory_test.cc
blob3c2afbfd813bc531d836b431c7f435abf4884ae9
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_server_properties_impl.h"
16 #include "net/http/http_util.h"
17 #include "net/http/transport_security_state.h"
18 #include "net/quic/crypto/crypto_handshake.h"
19 #include "net/quic/crypto/proof_verifier_chromium.h"
20 #include "net/quic/crypto/quic_decrypter.h"
21 #include "net/quic/crypto/quic_encrypter.h"
22 #include "net/quic/crypto/quic_server_info.h"
23 #include "net/quic/quic_http_stream.h"
24 #include "net/quic/quic_server_id.h"
25 #include "net/quic/test_tools/mock_clock.h"
26 #include "net/quic/test_tools/mock_crypto_client_stream_factory.h"
27 #include "net/quic/test_tools/mock_random.h"
28 #include "net/quic/test_tools/quic_test_packet_maker.h"
29 #include "net/quic/test_tools/quic_test_utils.h"
30 #include "net/quic/test_tools/test_task_runner.h"
31 #include "net/socket/socket_test_util.h"
32 #include "net/spdy/spdy_test_utils.h"
33 #include "net/ssl/channel_id_service.h"
34 #include "net/ssl/default_channel_id_store.h"
35 #include "net/test/cert_test_util.h"
36 #include "testing/gtest/include/gtest/gtest.h"
38 using base::StringPiece;
39 using std::ostream;
40 using std::string;
41 using std::vector;
43 namespace net {
44 namespace test {
46 namespace {
47 const char kDefaultServerHostName[] = "www.google.com";
48 const int kDefaultServerPort = 443;
50 // Run all tests with all the combinations of versions and
51 // enable_connection_racing.
52 struct TestParams {
53 TestParams(const QuicVersion version, bool enable_connection_racing)
54 : version(version), enable_connection_racing(enable_connection_racing) {}
56 friend ostream& operator<<(ostream& os, const TestParams& p) {
57 os << "{ version: " << QuicVersionToString(p.version);
58 os << " enable_connection_racing: " << p.enable_connection_racing << " }";
59 return os;
62 QuicVersion version;
63 bool enable_connection_racing;
66 // Constructs various test permutations.
67 vector<TestParams> GetTestParams() {
68 vector<TestParams> params;
69 QuicVersionVector all_supported_versions = QuicSupportedVersions();
70 for (const QuicVersion version : all_supported_versions) {
71 params.push_back(TestParams(version, false));
72 params.push_back(TestParams(version, true));
74 return params;
77 } // namespace anonymous
79 class QuicStreamFactoryPeer {
80 public:
81 static QuicCryptoClientConfig* GetCryptoConfig(QuicStreamFactory* factory) {
82 return &factory->crypto_config_;
85 static bool HasActiveSession(QuicStreamFactory* factory,
86 const HostPortPair& host_port_pair,
87 bool is_https) {
88 QuicServerId server_id(host_port_pair, is_https, PRIVACY_MODE_DISABLED);
89 return factory->HasActiveSession(server_id);
92 static QuicChromiumClientSession* GetActiveSession(
93 QuicStreamFactory* factory,
94 const HostPortPair& host_port_pair,
95 bool is_https) {
96 QuicServerId server_id(host_port_pair, is_https, PRIVACY_MODE_DISABLED);
97 DCHECK(factory->HasActiveSession(server_id));
98 return factory->active_sessions_[server_id];
101 static scoped_ptr<QuicHttpStream> CreateFromSession(
102 QuicStreamFactory* factory,
103 QuicChromiumClientSession* session) {
104 return factory->CreateFromSession(session);
107 static bool IsLiveSession(QuicStreamFactory* factory,
108 QuicChromiumClientSession* session) {
109 for (QuicStreamFactory::SessionIdMap::iterator it =
110 factory->all_sessions_.begin();
111 it != factory->all_sessions_.end(); ++it) {
112 if (it->first == session)
113 return true;
115 return false;
118 static void DisableConnectionPooling(QuicStreamFactory* factory) {
119 factory->disable_connection_pooling_ = true;
122 static void SetTaskRunner(QuicStreamFactory* factory,
123 base::TaskRunner* task_runner) {
124 factory->task_runner_ = task_runner;
127 static void SetEnableConnectionRacing(QuicStreamFactory* factory,
128 bool enable_connection_racing) {
129 factory->enable_connection_racing_ = enable_connection_racing;
132 static void SetDisableDiskCache(QuicStreamFactory* factory,
133 bool disable_disk_cache) {
134 factory->disable_disk_cache_ = disable_disk_cache;
137 static void SetMaxNumberOfLossyConnections(
138 QuicStreamFactory* factory,
139 int max_number_of_lossy_connections) {
140 factory->max_number_of_lossy_connections_ = max_number_of_lossy_connections;
143 static int GetNumberOfLossyConnections(QuicStreamFactory* factory,
144 uint16 port) {
145 return factory->number_of_lossy_connections_[port];
148 static bool IsQuicDisabled(QuicStreamFactory* factory, uint16 port) {
149 return factory->IsQuicDisabled(port);
152 static bool GetDelayTcpRace(QuicStreamFactory* factory) {
153 return factory->delay_tcp_race_;
156 static void SetDelayTcpRace(QuicStreamFactory* factory, bool delay_tcp_race) {
157 factory->delay_tcp_race_ = delay_tcp_race;
160 static void SetHttpServerProperties(
161 QuicStreamFactory* factory,
162 base::WeakPtr<HttpServerProperties> http_server_properties) {
163 factory->http_server_properties_ = http_server_properties;
166 static size_t GetNumberOfActiveJobs(QuicStreamFactory* factory,
167 const QuicServerId& server_id) {
168 return (factory->active_jobs_[server_id]).size();
171 static void SetThresholdTimeoutsWithOpenStreams(
172 QuicStreamFactory* factory,
173 int threshold_timeouts_with_open_streams) {
174 factory->threshold_timeouts_with_open_streams_ =
175 threshold_timeouts_with_open_streams;
178 static int GetNumTimeoutsWithOpenStreams(QuicStreamFactory* factory) {
179 return factory->num_timeouts_with_open_streams_;
182 static void SetThresholdPublicResetsPostHandshake(
183 QuicStreamFactory* factory,
184 int threshold_public_resets_post_handshake) {
185 factory->threshold_public_resets_post_handshake_ =
186 threshold_public_resets_post_handshake;
189 static int GetNumPublicResetsPostHandshake(QuicStreamFactory* factory) {
190 return factory->num_public_resets_post_handshake_;
193 static void InitializeQuicSupportedServersAtStartup(
194 QuicStreamFactory* factory) {
195 factory->InitializeQuicSupportedServersAtStartup();
198 static bool GetQuicSupportedServersAtStartupInitialzied(
199 QuicStreamFactory* factory) {
200 return factory->quic_supported_servers_at_startup_initialzied_;
203 static bool SupportsQuicAtStartUp(QuicStreamFactory* factory,
204 HostPortPair host_port_pair) {
205 return ContainsKey(factory->quic_supported_servers_at_startup_,
206 host_port_pair);
210 class MockQuicServerInfo : public QuicServerInfo {
211 public:
212 MockQuicServerInfo(const QuicServerId& server_id)
213 : QuicServerInfo(server_id) {}
214 ~MockQuicServerInfo() override {}
216 void Start() override {}
218 int WaitForDataReady(const CompletionCallback& callback) override {
219 return ERR_IO_PENDING;
222 void ResetWaitForDataReadyCallback() override {}
224 void CancelWaitForDataReadyCallback() override {}
226 bool IsDataReady() override { return false; }
228 bool IsReadyToPersist() override { return false; }
230 void Persist() override {}
232 void OnExternalCacheHit() override {}
235 class MockQuicServerInfoFactory : public QuicServerInfoFactory {
236 public:
237 MockQuicServerInfoFactory() {}
238 ~MockQuicServerInfoFactory() override {}
240 QuicServerInfo* GetForServer(const QuicServerId& server_id) override {
241 return new MockQuicServerInfo(server_id);
245 class QuicStreamFactoryTest : public ::testing::TestWithParam<TestParams> {
246 protected:
247 QuicStreamFactoryTest()
248 : random_generator_(0),
249 clock_(new MockClock()),
250 runner_(new TestTaskRunner(clock_)),
251 maker_(GetParam().version, 0, clock_, kDefaultServerHostName),
252 cert_verifier_(CertVerifier::CreateDefault()),
253 channel_id_service_(
254 new ChannelIDService(new DefaultChannelIDStore(nullptr),
255 base::ThreadTaskRunnerHandle::Get())),
256 factory_(&host_resolver_,
257 &socket_factory_,
258 base::WeakPtr<HttpServerProperties>(),
259 cert_verifier_.get(),
260 nullptr,
261 channel_id_service_.get(),
262 &transport_security_state_,
263 &crypto_client_stream_factory_,
264 &random_generator_,
265 clock_,
266 kDefaultMaxPacketSize,
267 std::string(),
268 SupportedVersions(GetParam().version),
269 /*enable_port_selection=*/true,
270 /*always_require_handshake_confirmation=*/false,
271 /*disable_connection_pooling=*/false,
272 /*load_server_info_timeout_srtt_multiplier=*/0.0f,
273 /*enable_connection_racing=*/false,
274 /*enable_non_blocking_io=*/true,
275 /*disable_disk_cache=*/false,
276 /*prefer_aes=*/false,
277 /*max_number_of_lossy_connections=*/0,
278 /*packet_loss_threshold=*/1.0f,
279 /*max_disabled_reasons=*/3,
280 /*threshold_timeouts_with_open_streams=*/2,
281 /*threshold_pulic_resets_post_handshake=*/2,
282 /*receive_buffer_size=*/0,
283 /*delay_tcp_race=*/false,
284 QuicTagVector()),
285 host_port_pair_(kDefaultServerHostName, kDefaultServerPort),
286 is_https_(false),
287 privacy_mode_(PRIVACY_MODE_DISABLED) {
288 factory_.set_require_confirmation(false);
289 clock_->AdvanceTime(QuicTime::Delta::FromSeconds(1));
290 QuicStreamFactoryPeer::SetEnableConnectionRacing(
291 &factory_, GetParam().enable_connection_racing);
294 bool HasActiveSession(const HostPortPair& host_port_pair) {
295 return QuicStreamFactoryPeer::HasActiveSession(&factory_, host_port_pair,
296 /*is_https_=*/false);
299 scoped_ptr<QuicHttpStream> CreateFromSession(
300 const HostPortPair& host_port_pair) {
301 QuicChromiumClientSession* session =
302 QuicStreamFactoryPeer::GetActiveSession(&factory_, host_port_pair,
303 /*is_https=*/false);
304 return QuicStreamFactoryPeer::CreateFromSession(&factory_, session);
307 int GetSourcePortForNewSession(const HostPortPair& destination) {
308 return GetSourcePortForNewSessionInner(destination, false);
311 int GetSourcePortForNewSessionAndGoAway(
312 const HostPortPair& destination) {
313 return GetSourcePortForNewSessionInner(destination, true);
316 int GetSourcePortForNewSessionInner(const HostPortPair& destination,
317 bool goaway_received) {
318 // Should only be called if there is no active session for this destination.
319 EXPECT_FALSE(HasActiveSession(destination));
320 size_t socket_count = socket_factory_.udp_client_sockets().size();
322 MockRead reads[] = {
323 MockRead(ASYNC, OK, 0) // EOF
325 DeterministicSocketData socket_data(reads, arraysize(reads), nullptr, 0);
326 socket_data.StopAfter(1);
327 socket_factory_.AddSocketDataProvider(&socket_data);
329 QuicStreamRequest request(&factory_);
330 EXPECT_EQ(ERR_IO_PENDING,
331 request.Request(destination, is_https_, privacy_mode_,
332 /*cert_verify_flags=*/0, destination.host(),
333 "GET", net_log_, callback_.callback()));
335 EXPECT_EQ(OK, callback_.WaitForResult());
336 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
337 EXPECT_TRUE(stream.get());
338 stream.reset();
340 QuicChromiumClientSession* session =
341 QuicStreamFactoryPeer::GetActiveSession(&factory_, destination,
342 is_https_);
344 if (socket_count + 1 != socket_factory_.udp_client_sockets().size()) {
345 EXPECT_TRUE(false);
346 return 0;
349 IPEndPoint endpoint;
350 socket_factory_.
351 udp_client_sockets()[socket_count]->GetLocalAddress(&endpoint);
352 int port = endpoint.port();
353 if (goaway_received) {
354 QuicGoAwayFrame goaway(QUIC_NO_ERROR, 1, "");
355 session->connection()->OnGoAwayFrame(goaway);
358 factory_.OnSessionClosed(session);
359 EXPECT_FALSE(HasActiveSession(destination));
360 EXPECT_TRUE(socket_data.AllReadDataConsumed());
361 EXPECT_TRUE(socket_data.AllWriteDataConsumed());
362 return port;
365 scoped_ptr<QuicEncryptedPacket> ConstructRstPacket() {
366 QuicStreamId stream_id = kClientDataStreamId1;
367 return maker_.MakeRstPacket(
368 1, true, stream_id,
369 AdjustErrorForVersion(QUIC_RST_ACKNOWLEDGEMENT, GetParam().version));
372 static ProofVerifyDetailsChromium DefaultProofVerifyDetails() {
373 // Load a certificate that is valid for www.example.org, mail.example.org,
374 // and mail.example.com.
375 scoped_refptr<X509Certificate> test_cert(
376 ImportCertFromFile(GetTestCertsDirectory(), "spdy_pooling.pem"));
377 EXPECT_TRUE(test_cert.get());
378 ProofVerifyDetailsChromium verify_details;
379 verify_details.cert_verify_result.verified_cert = test_cert;
380 verify_details.cert_verify_result.is_issued_by_known_root = true;
381 return verify_details;
384 MockQuicServerInfoFactory quic_server_info_factory_;
385 MockHostResolver host_resolver_;
386 DeterministicMockClientSocketFactory socket_factory_;
387 MockCryptoClientStreamFactory crypto_client_stream_factory_;
388 MockRandom random_generator_;
389 MockClock* clock_; // Owned by factory_.
390 scoped_refptr<TestTaskRunner> runner_;
391 QuicTestPacketMaker maker_;
392 scoped_ptr<CertVerifier> cert_verifier_;
393 scoped_ptr<ChannelIDService> channel_id_service_;
394 TransportSecurityState transport_security_state_;
395 QuicStreamFactory factory_;
396 HostPortPair host_port_pair_;
397 bool is_https_;
398 PrivacyMode privacy_mode_;
399 BoundNetLog net_log_;
400 TestCompletionCallback callback_;
403 INSTANTIATE_TEST_CASE_P(Version,
404 QuicStreamFactoryTest,
405 ::testing::ValuesIn(GetTestParams()));
407 TEST_P(QuicStreamFactoryTest, Create) {
408 MockRead reads[] = {
409 MockRead(ASYNC, OK, 0) // EOF
411 DeterministicSocketData socket_data(reads, arraysize(reads), nullptr, 0);
412 socket_factory_.AddSocketDataProvider(&socket_data);
413 socket_data.StopAfter(1);
415 QuicStreamRequest request(&factory_);
416 EXPECT_EQ(ERR_IO_PENDING,
417 request.Request(host_port_pair_, is_https_, privacy_mode_,
418 /*cert_verify_flags=*/0, host_port_pair_.host(),
419 "GET", net_log_, callback_.callback()));
421 EXPECT_EQ(OK, callback_.WaitForResult());
422 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
423 EXPECT_TRUE(stream.get());
425 // Will reset stream 3.
426 stream = CreateFromSession(host_port_pair_);
427 EXPECT_TRUE(stream.get());
429 // TODO(rtenneti): We should probably have a tests that HTTP and HTTPS result
430 // in streams on different sessions.
431 QuicStreamRequest request2(&factory_);
432 EXPECT_EQ(OK,
433 request2.Request(host_port_pair_, is_https_, privacy_mode_,
434 /*cert_verify_flags=*/0, host_port_pair_.host(),
435 "GET", net_log_, callback_.callback()));
436 stream = request2.ReleaseStream(); // Will reset stream 5.
437 stream.reset(); // Will reset stream 7.
439 EXPECT_TRUE(socket_data.AllReadDataConsumed());
440 EXPECT_TRUE(socket_data.AllWriteDataConsumed());
443 TEST_P(QuicStreamFactoryTest, CreateZeroRtt) {
444 MockRead reads[] = {
445 MockRead(ASYNC, OK, 0) // EOF
447 DeterministicSocketData socket_data(reads, arraysize(reads), nullptr, 0);
448 socket_factory_.AddSocketDataProvider(&socket_data);
449 socket_data.StopAfter(1);
451 crypto_client_stream_factory_.set_handshake_mode(
452 MockCryptoClientStream::ZERO_RTT);
453 host_resolver_.set_synchronous_mode(true);
454 host_resolver_.rules()->AddIPLiteralRule(host_port_pair_.host(),
455 "192.168.0.1", "");
457 QuicStreamRequest request(&factory_);
458 EXPECT_EQ(OK, request.Request(host_port_pair_, is_https_, privacy_mode_,
459 /*cert_verify_flags=*/0, host_port_pair_.host(),
460 "GET", net_log_, callback_.callback()));
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, CreateZeroRttPost) {
469 MockRead reads[] = {
470 MockRead(ASYNC, OK, 0) // EOF
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 // Posts require handshake confirmation, so this will return asynchronously.
484 EXPECT_EQ(ERR_IO_PENDING,
485 request.Request(host_port_pair_, is_https_, privacy_mode_,
486 /*cert_verify_flags=*/0, host_port_pair_.host(),
487 "POST", net_log_, callback_.callback()));
489 // Confirm the handshake and verify that the stream is created.
490 crypto_client_stream_factory_.last_stream()->SendOnCryptoHandshakeEvent(
491 QuicSession::HANDSHAKE_CONFIRMED);
493 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, NoZeroRttForDifferentHost) {
501 MockRead reads[] = {
502 MockRead(ASYNC, OK, 0),
504 DeterministicSocketData socket_data(reads, arraysize(reads), nullptr, 0);
505 socket_factory_.AddSocketDataProvider(&socket_data);
506 socket_data.StopAfter(1);
508 crypto_client_stream_factory_.set_handshake_mode(
509 MockCryptoClientStream::ZERO_RTT);
510 host_resolver_.set_synchronous_mode(true);
511 host_resolver_.rules()->AddIPLiteralRule(host_port_pair_.host(),
512 "192.168.0.1", "");
514 QuicStreamRequest request(&factory_);
515 int rv = request.Request(
516 host_port_pair_, is_https_, privacy_mode_, /*cert_verify_flags=*/0,
517 "different.host.example.com", "GET", net_log_, callback_.callback());
518 // If server and origin have different hostnames, then handshake confirmation
519 // should be required, so Request will return asynchronously.
520 EXPECT_EQ(ERR_IO_PENDING, rv);
521 // Confirm handshake.
522 crypto_client_stream_factory_.last_stream()->SendOnCryptoHandshakeEvent(
523 QuicSession::HANDSHAKE_CONFIRMED);
524 EXPECT_EQ(OK, callback_.WaitForResult());
526 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
527 EXPECT_TRUE(stream.get());
528 EXPECT_TRUE(socket_data.AllReadDataConsumed());
529 EXPECT_TRUE(socket_data.AllWriteDataConsumed());
532 TEST_P(QuicStreamFactoryTest, CreateHttpVsHttps) {
533 MockRead reads[] = {
534 MockRead(ASYNC, OK, 0) // EOF
536 DeterministicSocketData socket_data1(reads, arraysize(reads), nullptr, 0);
537 DeterministicSocketData socket_data2(reads, arraysize(reads), nullptr, 0);
538 socket_factory_.AddSocketDataProvider(&socket_data1);
539 socket_factory_.AddSocketDataProvider(&socket_data2);
540 socket_data1.StopAfter(1);
541 socket_data2.StopAfter(1);
543 QuicStreamRequest request(&factory_);
544 EXPECT_EQ(ERR_IO_PENDING,
545 request.Request(host_port_pair_, is_https_, privacy_mode_,
546 /*cert_verify_flags=*/0, host_port_pair_.host(),
547 "GET", net_log_, callback_.callback()));
549 EXPECT_EQ(OK, callback_.WaitForResult());
550 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
551 EXPECT_TRUE(stream.get());
553 QuicStreamRequest request2(&factory_);
554 EXPECT_EQ(ERR_IO_PENDING,
555 request2.Request(host_port_pair_, !is_https_, privacy_mode_,
556 /*cert_verify_flags=*/0, host_port_pair_.host(),
557 "GET", net_log_, callback_.callback()));
558 EXPECT_EQ(OK, callback_.WaitForResult());
559 stream = request2.ReleaseStream();
560 EXPECT_TRUE(stream.get());
561 stream.reset();
563 EXPECT_NE(QuicStreamFactoryPeer::GetActiveSession(
564 &factory_, host_port_pair_, is_https_),
565 QuicStreamFactoryPeer::GetActiveSession(
566 &factory_, host_port_pair_, !is_https_));
568 EXPECT_TRUE(socket_data1.AllReadDataConsumed());
569 EXPECT_TRUE(socket_data1.AllWriteDataConsumed());
570 EXPECT_TRUE(socket_data2.AllReadDataConsumed());
571 EXPECT_TRUE(socket_data2.AllWriteDataConsumed());
574 TEST_P(QuicStreamFactoryTest, Pooling) {
575 MockRead reads[] = {
576 MockRead(ASYNC, OK, 0) // EOF
578 DeterministicSocketData socket_data(reads, arraysize(reads), nullptr, 0);
579 socket_factory_.AddSocketDataProvider(&socket_data);
580 socket_data.StopAfter(1);
582 HostPortPair server2("mail.google.com", kDefaultServerPort);
583 host_resolver_.set_synchronous_mode(true);
584 host_resolver_.rules()->AddIPLiteralRule(
585 kDefaultServerHostName, "192.168.0.1", "");
586 host_resolver_.rules()->AddIPLiteralRule(
587 "mail.google.com", "192.168.0.1", "");
589 QuicStreamRequest request(&factory_);
590 EXPECT_EQ(OK, request.Request(host_port_pair_, is_https_, privacy_mode_,
591 /*cert_verify_flags=*/0, host_port_pair_.host(),
592 "GET", net_log_, callback_.callback()));
593 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
594 EXPECT_TRUE(stream.get());
596 TestCompletionCallback callback;
597 QuicStreamRequest request2(&factory_);
598 EXPECT_EQ(OK, request2.Request(server2, is_https_, privacy_mode_,
599 /*cert_verify_flags=*/0, server2.host(), "GET",
600 net_log_, callback.callback()));
601 scoped_ptr<QuicHttpStream> stream2 = request2.ReleaseStream();
602 EXPECT_TRUE(stream2.get());
604 EXPECT_EQ(
605 QuicStreamFactoryPeer::GetActiveSession(
606 &factory_, host_port_pair_, is_https_),
607 QuicStreamFactoryPeer::GetActiveSession(&factory_, server2, is_https_));
609 EXPECT_TRUE(socket_data.AllReadDataConsumed());
610 EXPECT_TRUE(socket_data.AllWriteDataConsumed());
613 TEST_P(QuicStreamFactoryTest, NoPoolingIfDisabled) {
614 MockRead reads[] = {
615 MockRead(ASYNC, OK, 0) // EOF
617 DeterministicSocketData socket_data1(reads, arraysize(reads), nullptr, 0);
618 DeterministicSocketData socket_data2(reads, arraysize(reads), nullptr, 0);
619 socket_factory_.AddSocketDataProvider(&socket_data1);
620 socket_factory_.AddSocketDataProvider(&socket_data2);
621 socket_data1.StopAfter(1);
622 socket_data2.StopAfter(1);
624 HostPortPair server2("mail.google.com", kDefaultServerPort);
625 host_resolver_.set_synchronous_mode(true);
626 host_resolver_.rules()->AddIPLiteralRule(
627 kDefaultServerHostName, "192.168.0.1", "");
628 host_resolver_.rules()->AddIPLiteralRule(
629 "mail.google.com", "192.168.0.1", "");
631 // Disable connection pooling.
632 QuicStreamFactoryPeer::DisableConnectionPooling(&factory_);
634 QuicStreamRequest request(&factory_);
635 EXPECT_EQ(OK, request.Request(host_port_pair_, is_https_, privacy_mode_,
636 /*cert_verify_flags=*/0, host_port_pair_.host(),
637 "GET", net_log_, callback_.callback()));
638 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
639 EXPECT_TRUE(stream.get());
641 TestCompletionCallback callback;
642 QuicStreamRequest request2(&factory_);
643 EXPECT_EQ(OK, request2.Request(server2, is_https_, privacy_mode_,
644 /*cert_verify_flags=*/0, server2.host(), "GET",
645 net_log_, callback.callback()));
646 scoped_ptr<QuicHttpStream> stream2 = request2.ReleaseStream();
647 EXPECT_TRUE(stream2.get());
649 EXPECT_NE(
650 QuicStreamFactoryPeer::GetActiveSession(
651 &factory_, host_port_pair_, is_https_),
652 QuicStreamFactoryPeer::GetActiveSession(&factory_, server2, is_https_));
654 EXPECT_TRUE(socket_data1.AllReadDataConsumed());
655 EXPECT_TRUE(socket_data1.AllWriteDataConsumed());
656 EXPECT_TRUE(socket_data2.AllReadDataConsumed());
657 EXPECT_TRUE(socket_data2.AllWriteDataConsumed());
660 TEST_P(QuicStreamFactoryTest, NoPoolingAfterGoAway) {
661 MockRead reads[] = {
662 MockRead(ASYNC, OK, 0) // EOF
664 DeterministicSocketData socket_data1(reads, arraysize(reads), nullptr, 0);
665 DeterministicSocketData socket_data2(reads, arraysize(reads), nullptr, 0);
666 socket_factory_.AddSocketDataProvider(&socket_data1);
667 socket_factory_.AddSocketDataProvider(&socket_data2);
668 socket_data1.StopAfter(1);
669 socket_data2.StopAfter(1);
671 HostPortPair server2("mail.google.com", kDefaultServerPort);
672 host_resolver_.set_synchronous_mode(true);
673 host_resolver_.rules()->AddIPLiteralRule(
674 kDefaultServerHostName, "192.168.0.1", "");
675 host_resolver_.rules()->AddIPLiteralRule(
676 "mail.google.com", "192.168.0.1", "");
678 QuicStreamRequest request(&factory_);
679 EXPECT_EQ(OK, request.Request(host_port_pair_, is_https_, privacy_mode_,
680 /*cert_verify_flags=*/0, host_port_pair_.host(),
681 "GET", net_log_, callback_.callback()));
682 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
683 EXPECT_TRUE(stream.get());
685 TestCompletionCallback callback;
686 QuicStreamRequest request2(&factory_);
687 EXPECT_EQ(OK, request2.Request(server2, is_https_, privacy_mode_,
688 /*cert_verify_flags=*/0, server2.host(), "GET",
689 net_log_, callback.callback()));
690 scoped_ptr<QuicHttpStream> stream2 = request2.ReleaseStream();
691 EXPECT_TRUE(stream2.get());
693 factory_.OnSessionGoingAway(QuicStreamFactoryPeer::GetActiveSession(
694 &factory_, host_port_pair_, is_https_));
695 EXPECT_FALSE(QuicStreamFactoryPeer::HasActiveSession(
696 &factory_, host_port_pair_, is_https_));
697 EXPECT_FALSE(QuicStreamFactoryPeer::HasActiveSession(
698 &factory_, server2, is_https_));
700 TestCompletionCallback callback3;
701 QuicStreamRequest request3(&factory_);
702 EXPECT_EQ(OK, request3.Request(server2, is_https_, privacy_mode_,
703 /*cert_verify_flags=*/0, server2.host(), "GET",
704 net_log_, callback3.callback()));
705 scoped_ptr<QuicHttpStream> stream3 = request3.ReleaseStream();
706 EXPECT_TRUE(stream3.get());
708 EXPECT_TRUE(QuicStreamFactoryPeer::HasActiveSession(
709 &factory_, server2, is_https_));
711 EXPECT_TRUE(socket_data1.AllReadDataConsumed());
712 EXPECT_TRUE(socket_data1.AllWriteDataConsumed());
713 EXPECT_TRUE(socket_data2.AllReadDataConsumed());
714 EXPECT_TRUE(socket_data2.AllWriteDataConsumed());
717 TEST_P(QuicStreamFactoryTest, HttpsPooling) {
718 MockRead reads[] = {
719 MockRead(ASYNC, OK, 0) // EOF
721 DeterministicSocketData socket_data(reads, arraysize(reads), nullptr, 0);
722 socket_factory_.AddSocketDataProvider(&socket_data);
723 socket_data.StopAfter(1);
725 HostPortPair server1("www.example.org", 443);
726 HostPortPair server2("mail.example.org", 443);
728 ProofVerifyDetailsChromium verify_details = DefaultProofVerifyDetails();
729 crypto_client_stream_factory_.AddProofVerifyDetails(&verify_details);
731 host_resolver_.set_synchronous_mode(true);
732 host_resolver_.rules()->AddIPLiteralRule(server1.host(), "192.168.0.1", "");
733 host_resolver_.rules()->AddIPLiteralRule(server2.host(), "192.168.0.1", "");
735 QuicStreamRequest request(&factory_);
736 is_https_ = true;
737 EXPECT_EQ(OK, request.Request(server1, is_https_, privacy_mode_,
738 /*cert_verify_flags=*/0, server1.host(), "GET",
739 net_log_, callback_.callback()));
740 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
741 EXPECT_TRUE(stream.get());
743 TestCompletionCallback callback;
744 QuicStreamRequest request2(&factory_);
745 EXPECT_EQ(OK, request2.Request(server2, is_https_, privacy_mode_,
746 /*cert_verify_flags=*/0, server2.host(), "GET",
747 net_log_, callback_.callback()));
748 scoped_ptr<QuicHttpStream> stream2 = request2.ReleaseStream();
749 EXPECT_TRUE(stream2.get());
751 EXPECT_EQ(QuicStreamFactoryPeer::GetActiveSession(
752 &factory_, server1, is_https_),
753 QuicStreamFactoryPeer::GetActiveSession(
754 &factory_, server2, is_https_));
756 EXPECT_TRUE(socket_data.AllReadDataConsumed());
757 EXPECT_TRUE(socket_data.AllWriteDataConsumed());
760 TEST_P(QuicStreamFactoryTest, NoHttpsPoolingIfDisabled) {
761 MockRead reads[] = {
762 MockRead(ASYNC, OK, 0) // EOF
764 DeterministicSocketData socket_data1(reads, arraysize(reads), nullptr, 0);
765 DeterministicSocketData socket_data2(reads, arraysize(reads), nullptr, 0);
766 socket_factory_.AddSocketDataProvider(&socket_data1);
767 socket_factory_.AddSocketDataProvider(&socket_data2);
768 socket_data1.StopAfter(1);
769 socket_data2.StopAfter(1);
771 HostPortPair server1("www.example.org", 443);
772 HostPortPair server2("mail.example.org", 443);
774 ProofVerifyDetailsChromium verify_details = DefaultProofVerifyDetails();
775 crypto_client_stream_factory_.AddProofVerifyDetails(&verify_details);
777 host_resolver_.set_synchronous_mode(true);
778 host_resolver_.rules()->AddIPLiteralRule(server1.host(), "192.168.0.1", "");
779 host_resolver_.rules()->AddIPLiteralRule(server2.host(), "192.168.0.1", "");
781 // Disable connection pooling.
782 QuicStreamFactoryPeer::DisableConnectionPooling(&factory_);
784 QuicStreamRequest request(&factory_);
785 is_https_ = true;
786 EXPECT_EQ(OK, request.Request(server1, is_https_, privacy_mode_,
787 /*cert_verify_flags=*/0, server1.host(), "GET",
788 net_log_, callback_.callback()));
789 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
790 EXPECT_TRUE(stream.get());
792 TestCompletionCallback callback;
793 QuicStreamRequest request2(&factory_);
794 EXPECT_EQ(OK, request2.Request(server2, is_https_, privacy_mode_,
795 /*cert_verify_flags=*/0, server2.host(), "GET",
796 net_log_, callback_.callback()));
797 scoped_ptr<QuicHttpStream> stream2 = request2.ReleaseStream();
798 EXPECT_TRUE(stream2.get());
800 EXPECT_NE(QuicStreamFactoryPeer::GetActiveSession(
801 &factory_, server1, is_https_),
802 QuicStreamFactoryPeer::GetActiveSession(
803 &factory_, server2, is_https_));
805 EXPECT_TRUE(socket_data1.AllReadDataConsumed());
806 EXPECT_TRUE(socket_data1.AllWriteDataConsumed());
807 EXPECT_TRUE(socket_data2.AllReadDataConsumed());
808 EXPECT_TRUE(socket_data2.AllWriteDataConsumed());
811 class QuicAlternativeServiceCertificateValidationPooling
812 : public QuicStreamFactoryTest {
813 public:
814 void Run(bool valid) {
815 MockRead reads[] = {
816 MockRead(ASYNC, OK, 0) // EOF
818 DeterministicSocketData socket_data1(reads, arraysize(reads), nullptr, 0);
819 socket_factory_.AddSocketDataProvider(&socket_data1);
820 socket_data1.StopAfter(1);
822 HostPortPair server1("www.example.org", 443);
823 HostPortPair server2("mail.example.org", 443);
825 std::string origin_host(valid ? "mail.example.org" : "invalid.example.org");
826 HostPortPair alternative("www.example.org", 443);
828 ProofVerifyDetailsChromium verify_details = DefaultProofVerifyDetails();
829 bool common_name_fallback_used;
830 EXPECT_EQ(valid,
831 verify_details.cert_verify_result.verified_cert->VerifyNameMatch(
832 origin_host, &common_name_fallback_used));
833 EXPECT_TRUE(
834 verify_details.cert_verify_result.verified_cert->VerifyNameMatch(
835 alternative.host(), &common_name_fallback_used));
836 crypto_client_stream_factory_.AddProofVerifyDetails(&verify_details);
838 host_resolver_.set_synchronous_mode(true);
839 host_resolver_.rules()->AddIPLiteralRule(alternative.host(), "192.168.0.1",
840 "");
842 // Open first stream to alternative.
843 QuicStreamRequest request1(&factory_);
844 is_https_ = true;
845 EXPECT_EQ(OK, request1.Request(alternative, is_https_, privacy_mode_,
846 /*cert_verify_flags=*/0, alternative.host(),
847 "GET", net_log_, callback_.callback()));
848 scoped_ptr<QuicHttpStream> stream1 = request1.ReleaseStream();
849 EXPECT_TRUE(stream1.get());
851 QuicStreamRequest request2(&factory_);
852 int rv = request2.Request(alternative, is_https_, privacy_mode_,
853 /*cert_verify_flags=*/0, origin_host, "GET",
854 net_log_, callback_.callback());
855 if (valid) {
856 // Alternative service of origin to |alternative| should pool to session
857 // of |stream1| even if origin is different. Since only one
858 // SocketDataProvider is set up, the second request succeeding means that
859 // it pooled to the session opened by the first one.
860 EXPECT_EQ(OK, rv);
861 scoped_ptr<QuicHttpStream> stream2 = request2.ReleaseStream();
862 EXPECT_TRUE(stream2.get());
863 } else {
864 EXPECT_EQ(ERR_ALTERNATIVE_CERT_NOT_VALID_FOR_ORIGIN, rv);
867 EXPECT_TRUE(socket_data1.AllReadDataConsumed());
868 EXPECT_TRUE(socket_data1.AllWriteDataConsumed());
872 INSTANTIATE_TEST_CASE_P(Version,
873 QuicAlternativeServiceCertificateValidationPooling,
874 ::testing::ValuesIn(GetTestParams()));
876 TEST_P(QuicAlternativeServiceCertificateValidationPooling, Valid) {
877 Run(true);
880 TEST_P(QuicAlternativeServiceCertificateValidationPooling, Invalid) {
881 Run(false);
884 TEST_P(QuicStreamFactoryTest, HttpsPoolingWithMatchingPins) {
885 MockRead reads[] = {
886 MockRead(ASYNC, OK, 0) // EOF
888 DeterministicSocketData socket_data(reads, arraysize(reads), nullptr, 0);
889 socket_factory_.AddSocketDataProvider(&socket_data);
890 socket_data.StopAfter(1);
892 HostPortPair server1("www.example.org", 443);
893 HostPortPair server2("mail.example.org", 443);
894 uint8 primary_pin = 1;
895 uint8 backup_pin = 2;
896 test::AddPin(&transport_security_state_, "mail.example.org", primary_pin,
897 backup_pin);
899 ProofVerifyDetailsChromium verify_details = DefaultProofVerifyDetails();
900 verify_details.cert_verify_result.public_key_hashes.push_back(
901 test::GetTestHashValue(primary_pin));
902 crypto_client_stream_factory_.AddProofVerifyDetails(&verify_details);
904 host_resolver_.set_synchronous_mode(true);
905 host_resolver_.rules()->AddIPLiteralRule(server1.host(), "192.168.0.1", "");
906 host_resolver_.rules()->AddIPLiteralRule(server2.host(), "192.168.0.1", "");
908 QuicStreamRequest request(&factory_);
909 is_https_ = true;
910 EXPECT_EQ(OK, request.Request(server1, is_https_, privacy_mode_,
911 /*cert_verify_flags=*/0, server1.host(), "GET",
912 net_log_, callback_.callback()));
913 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
914 EXPECT_TRUE(stream.get());
916 TestCompletionCallback callback;
917 QuicStreamRequest request2(&factory_);
918 EXPECT_EQ(OK, request2.Request(server2, is_https_, privacy_mode_,
919 /*cert_verify_flags=*/0, server2.host(), "GET",
920 net_log_, callback_.callback()));
921 scoped_ptr<QuicHttpStream> stream2 = request2.ReleaseStream();
922 EXPECT_TRUE(stream2.get());
924 EXPECT_EQ(QuicStreamFactoryPeer::GetActiveSession(
925 &factory_, server1, is_https_),
926 QuicStreamFactoryPeer::GetActiveSession(
927 &factory_, server2, is_https_));
929 EXPECT_TRUE(socket_data.AllReadDataConsumed());
930 EXPECT_TRUE(socket_data.AllWriteDataConsumed());
933 TEST_P(QuicStreamFactoryTest, NoHttpsPoolingWithMatchingPinsIfDisabled) {
934 MockRead reads[] = {
935 MockRead(ASYNC, OK, 0) // EOF
937 DeterministicSocketData socket_data1(reads, arraysize(reads), nullptr, 0);
938 DeterministicSocketData socket_data2(reads, arraysize(reads), nullptr, 0);
939 socket_factory_.AddSocketDataProvider(&socket_data1);
940 socket_factory_.AddSocketDataProvider(&socket_data2);
941 socket_data1.StopAfter(1);
942 socket_data2.StopAfter(1);
944 HostPortPair server1("www.example.org", 443);
945 HostPortPair server2("mail.example.org", 443);
946 uint8 primary_pin = 1;
947 uint8 backup_pin = 2;
948 test::AddPin(&transport_security_state_, "mail.example.org", primary_pin,
949 backup_pin);
951 ProofVerifyDetailsChromium verify_details = DefaultProofVerifyDetails();
952 verify_details.cert_verify_result.public_key_hashes.push_back(
953 test::GetTestHashValue(primary_pin));
954 crypto_client_stream_factory_.AddProofVerifyDetails(&verify_details);
956 host_resolver_.set_synchronous_mode(true);
957 host_resolver_.rules()->AddIPLiteralRule(server1.host(), "192.168.0.1", "");
958 host_resolver_.rules()->AddIPLiteralRule(server2.host(), "192.168.0.1", "");
960 // Disable connection pooling.
961 QuicStreamFactoryPeer::DisableConnectionPooling(&factory_);
963 QuicStreamRequest request(&factory_);
964 is_https_ = true;
965 EXPECT_EQ(OK, request.Request(server1, is_https_, privacy_mode_,
966 /*cert_verify_flags=*/0, server1.host(), "GET",
967 net_log_, callback_.callback()));
968 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
969 EXPECT_TRUE(stream.get());
971 TestCompletionCallback callback;
972 QuicStreamRequest request2(&factory_);
973 EXPECT_EQ(OK, request2.Request(server2, is_https_, privacy_mode_,
974 /*cert_verify_flags=*/0, server2.host(), "GET",
975 net_log_, callback_.callback()));
976 scoped_ptr<QuicHttpStream> stream2 = request2.ReleaseStream();
977 EXPECT_TRUE(stream2.get());
979 EXPECT_NE(QuicStreamFactoryPeer::GetActiveSession(
980 &factory_, server1, is_https_),
981 QuicStreamFactoryPeer::GetActiveSession(
982 &factory_, server2, is_https_));
984 EXPECT_TRUE(socket_data1.AllReadDataConsumed());
985 EXPECT_TRUE(socket_data1.AllWriteDataConsumed());
986 EXPECT_TRUE(socket_data2.AllReadDataConsumed());
987 EXPECT_TRUE(socket_data2.AllWriteDataConsumed());
990 TEST_P(QuicStreamFactoryTest, NoHttpsPoolingWithDifferentPins) {
991 MockRead reads[] = {
992 MockRead(ASYNC, OK, 0) // EOF
994 DeterministicSocketData socket_data1(reads, arraysize(reads), nullptr, 0);
995 DeterministicSocketData socket_data2(reads, arraysize(reads), nullptr, 0);
996 socket_factory_.AddSocketDataProvider(&socket_data1);
997 socket_factory_.AddSocketDataProvider(&socket_data2);
998 socket_data1.StopAfter(1);
999 socket_data2.StopAfter(1);
1001 HostPortPair server1("www.example.org", 443);
1002 HostPortPair server2("mail.example.org", 443);
1003 uint8 primary_pin = 1;
1004 uint8 backup_pin = 2;
1005 uint8 bad_pin = 3;
1006 test::AddPin(&transport_security_state_, "mail.example.org", primary_pin,
1007 backup_pin);
1009 ProofVerifyDetailsChromium verify_details1 = DefaultProofVerifyDetails();
1010 verify_details1.cert_verify_result.public_key_hashes.push_back(
1011 test::GetTestHashValue(bad_pin));
1012 crypto_client_stream_factory_.AddProofVerifyDetails(&verify_details1);
1014 ProofVerifyDetailsChromium verify_details2 = DefaultProofVerifyDetails();
1015 verify_details2.cert_verify_result.public_key_hashes.push_back(
1016 test::GetTestHashValue(primary_pin));
1017 crypto_client_stream_factory_.AddProofVerifyDetails(&verify_details2);
1019 host_resolver_.set_synchronous_mode(true);
1020 host_resolver_.rules()->AddIPLiteralRule(server1.host(), "192.168.0.1", "");
1021 host_resolver_.rules()->AddIPLiteralRule(server2.host(), "192.168.0.1", "");
1023 QuicStreamRequest request(&factory_);
1024 is_https_ = true;
1025 EXPECT_EQ(OK, request.Request(server1, is_https_, privacy_mode_,
1026 /*cert_verify_flags=*/0, server1.host(), "GET",
1027 net_log_, callback_.callback()));
1028 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
1029 EXPECT_TRUE(stream.get());
1031 TestCompletionCallback callback;
1032 QuicStreamRequest request2(&factory_);
1033 EXPECT_EQ(OK, request2.Request(server2, is_https_, privacy_mode_,
1034 /*cert_verify_flags=*/0, server2.host(), "GET",
1035 net_log_, callback_.callback()));
1036 scoped_ptr<QuicHttpStream> stream2 = request2.ReleaseStream();
1037 EXPECT_TRUE(stream2.get());
1039 EXPECT_NE(QuicStreamFactoryPeer::GetActiveSession(
1040 &factory_, server1, is_https_),
1041 QuicStreamFactoryPeer::GetActiveSession(
1042 &factory_, server2, is_https_));
1044 EXPECT_TRUE(socket_data1.AllReadDataConsumed());
1045 EXPECT_TRUE(socket_data1.AllWriteDataConsumed());
1046 EXPECT_TRUE(socket_data2.AllReadDataConsumed());
1047 EXPECT_TRUE(socket_data2.AllWriteDataConsumed());
1050 TEST_P(QuicStreamFactoryTest, Goaway) {
1051 MockRead reads[] = {
1052 MockRead(ASYNC, OK, 0) // EOF
1054 DeterministicSocketData socket_data(reads, arraysize(reads), nullptr, 0);
1055 socket_data.StopAfter(1);
1056 socket_factory_.AddSocketDataProvider(&socket_data);
1057 DeterministicSocketData socket_data2(reads, arraysize(reads), nullptr, 0);
1058 socket_data2.StopAfter(1);
1059 socket_factory_.AddSocketDataProvider(&socket_data2);
1061 QuicStreamRequest request(&factory_);
1062 EXPECT_EQ(ERR_IO_PENDING,
1063 request.Request(host_port_pair_, is_https_, privacy_mode_,
1064 /*cert_verify_flags=*/0, host_port_pair_.host(),
1065 "GET", net_log_, callback_.callback()));
1067 EXPECT_EQ(OK, callback_.WaitForResult());
1068 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
1069 EXPECT_TRUE(stream.get());
1071 // Mark the session as going away. Ensure that while it is still alive
1072 // that it is no longer active.
1073 QuicChromiumClientSession* session = QuicStreamFactoryPeer::GetActiveSession(
1074 &factory_, host_port_pair_, is_https_);
1075 factory_.OnSessionGoingAway(session);
1076 EXPECT_EQ(true, QuicStreamFactoryPeer::IsLiveSession(&factory_, session));
1077 EXPECT_FALSE(QuicStreamFactoryPeer::HasActiveSession(
1078 &factory_, host_port_pair_, is_https_));
1079 EXPECT_FALSE(HasActiveSession(host_port_pair_));
1081 // Create a new request for the same destination and verify that a
1082 // new session is created.
1083 QuicStreamRequest request2(&factory_);
1084 EXPECT_EQ(ERR_IO_PENDING,
1085 request2.Request(host_port_pair_, is_https_, privacy_mode_,
1086 /*cert_verify_flags=*/0, host_port_pair_.host(),
1087 "GET", net_log_, callback_.callback()));
1088 EXPECT_EQ(OK, callback_.WaitForResult());
1089 scoped_ptr<QuicHttpStream> stream2 = request2.ReleaseStream();
1090 EXPECT_TRUE(stream2.get());
1092 EXPECT_TRUE(QuicStreamFactoryPeer::HasActiveSession(&factory_,
1093 host_port_pair_,
1094 is_https_));
1095 EXPECT_NE(session,
1096 QuicStreamFactoryPeer::GetActiveSession(
1097 &factory_, host_port_pair_, is_https_));
1098 EXPECT_EQ(true, QuicStreamFactoryPeer::IsLiveSession(&factory_, session));
1100 stream2.reset();
1101 stream.reset();
1103 EXPECT_TRUE(socket_data.AllReadDataConsumed());
1104 EXPECT_TRUE(socket_data.AllWriteDataConsumed());
1105 EXPECT_TRUE(socket_data2.AllReadDataConsumed());
1106 EXPECT_TRUE(socket_data2.AllWriteDataConsumed());
1109 TEST_P(QuicStreamFactoryTest, MaxOpenStream) {
1110 MockRead reads[] = {
1111 MockRead(ASYNC, OK, 0) // EOF
1113 QuicStreamId stream_id = kClientDataStreamId1;
1114 scoped_ptr<QuicEncryptedPacket> rst(
1115 maker_.MakeRstPacket(1, true, stream_id, QUIC_STREAM_CANCELLED));
1116 MockWrite writes[] = {
1117 MockWrite(ASYNC, rst->data(), rst->length(), 1),
1119 DeterministicSocketData socket_data(reads, arraysize(reads),
1120 writes, arraysize(writes));
1121 socket_factory_.AddSocketDataProvider(&socket_data);
1122 socket_data.StopAfter(1);
1124 HttpRequestInfo request_info;
1125 std::vector<QuicHttpStream*> streams;
1126 // The MockCryptoClientStream sets max_open_streams to be
1127 // kDefaultMaxStreamsPerConnection / 2.
1128 for (size_t i = 0; i < kDefaultMaxStreamsPerConnection / 2; i++) {
1129 QuicStreamRequest request(&factory_);
1130 int rv = request.Request(host_port_pair_, is_https_, privacy_mode_,
1131 /*cert_verify_flags=*/0, host_port_pair_.host(),
1132 "GET", net_log_, callback_.callback());
1133 if (i == 0) {
1134 EXPECT_EQ(ERR_IO_PENDING, rv);
1135 EXPECT_EQ(OK, callback_.WaitForResult());
1136 } else {
1137 EXPECT_EQ(OK, rv);
1139 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
1140 EXPECT_TRUE(stream);
1141 EXPECT_EQ(OK, stream->InitializeStream(
1142 &request_info, DEFAULT_PRIORITY, net_log_, CompletionCallback()));
1143 streams.push_back(stream.release());
1146 QuicStreamRequest request(&factory_);
1147 EXPECT_EQ(OK, request.Request(host_port_pair_, is_https_, privacy_mode_,
1148 /*cert_verify_flags=*/0, host_port_pair_.host(),
1149 "GET", net_log_, CompletionCallback()));
1150 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
1151 EXPECT_TRUE(stream);
1152 EXPECT_EQ(ERR_IO_PENDING, stream->InitializeStream(
1153 &request_info, DEFAULT_PRIORITY, net_log_, callback_.callback()));
1155 // Close the first stream.
1156 streams.front()->Close(false);
1158 ASSERT_TRUE(callback_.have_result());
1160 EXPECT_EQ(OK, callback_.WaitForResult());
1162 EXPECT_TRUE(socket_data.AllReadDataConsumed());
1163 EXPECT_TRUE(socket_data.AllWriteDataConsumed());
1164 STLDeleteElements(&streams);
1167 TEST_P(QuicStreamFactoryTest, ResolutionErrorInCreate) {
1168 DeterministicSocketData socket_data(nullptr, 0, nullptr, 0);
1169 socket_factory_.AddSocketDataProvider(&socket_data);
1171 host_resolver_.rules()->AddSimulatedFailure(kDefaultServerHostName);
1173 QuicStreamRequest request(&factory_);
1174 EXPECT_EQ(ERR_IO_PENDING,
1175 request.Request(host_port_pair_, is_https_, privacy_mode_,
1176 /*cert_verify_flags=*/0, host_port_pair_.host(),
1177 "GET", net_log_, callback_.callback()));
1179 EXPECT_EQ(ERR_NAME_NOT_RESOLVED, callback_.WaitForResult());
1181 EXPECT_TRUE(socket_data.AllReadDataConsumed());
1182 EXPECT_TRUE(socket_data.AllWriteDataConsumed());
1185 TEST_P(QuicStreamFactoryTest, ConnectErrorInCreate) {
1186 MockConnect connect(SYNCHRONOUS, ERR_ADDRESS_IN_USE);
1187 DeterministicSocketData socket_data(nullptr, 0, nullptr, 0);
1188 socket_data.set_connect_data(connect);
1189 socket_factory_.AddSocketDataProvider(&socket_data);
1190 socket_data.StopAfter(1);
1192 QuicStreamRequest request(&factory_);
1193 EXPECT_EQ(ERR_IO_PENDING,
1194 request.Request(host_port_pair_, is_https_, privacy_mode_,
1195 /*cert_verify_flags=*/0, host_port_pair_.host(),
1196 "GET", net_log_, callback_.callback()));
1198 EXPECT_EQ(ERR_ADDRESS_IN_USE, callback_.WaitForResult());
1200 EXPECT_TRUE(socket_data.AllReadDataConsumed());
1201 EXPECT_TRUE(socket_data.AllWriteDataConsumed());
1204 TEST_P(QuicStreamFactoryTest, CancelCreate) {
1205 MockRead reads[] = {
1206 MockRead(ASYNC, OK, 0) // EOF
1208 DeterministicSocketData socket_data(reads, arraysize(reads), nullptr, 0);
1209 socket_factory_.AddSocketDataProvider(&socket_data);
1211 QuicStreamRequest request(&factory_);
1212 EXPECT_EQ(ERR_IO_PENDING,
1213 request.Request(host_port_pair_, is_https_, privacy_mode_,
1214 /*cert_verify_flags=*/0, host_port_pair_.host(),
1215 "GET", net_log_, callback_.callback()));
1218 socket_data.StopAfter(1);
1219 base::RunLoop run_loop;
1220 run_loop.RunUntilIdle();
1222 scoped_ptr<QuicHttpStream> stream(CreateFromSession(host_port_pair_));
1223 EXPECT_TRUE(stream.get());
1224 stream.reset();
1226 EXPECT_TRUE(socket_data.AllReadDataConsumed());
1227 EXPECT_TRUE(socket_data.AllWriteDataConsumed());
1230 TEST_P(QuicStreamFactoryTest, CreateConsistentEphemeralPort) {
1231 // Sequentially connect to the default host, then another host, and then the
1232 // default host. Verify that the default host gets a consistent ephemeral
1233 // port, that is different from the other host's connection.
1235 std::string other_server_name = "other.google.com";
1236 EXPECT_NE(kDefaultServerHostName, other_server_name);
1237 HostPortPair host_port_pair2(other_server_name, kDefaultServerPort);
1239 int original_port = GetSourcePortForNewSession(host_port_pair_);
1240 EXPECT_NE(original_port, GetSourcePortForNewSession(host_port_pair2));
1241 EXPECT_EQ(original_port, GetSourcePortForNewSession(host_port_pair_));
1244 TEST_P(QuicStreamFactoryTest, GoAwayDisablesConsistentEphemeralPort) {
1245 // Get a session to the host using the port suggester.
1246 int original_port =
1247 GetSourcePortForNewSessionAndGoAway(host_port_pair_);
1248 // Verify that the port is different after the goaway.
1249 EXPECT_NE(original_port, GetSourcePortForNewSession(host_port_pair_));
1250 // Since the previous session did not goaway we should see the original port.
1251 EXPECT_EQ(original_port, GetSourcePortForNewSession(host_port_pair_));
1254 TEST_P(QuicStreamFactoryTest, CloseAllSessions) {
1255 MockRead reads[] = {
1256 MockRead(ASYNC, 0, 0) // EOF
1258 scoped_ptr<QuicEncryptedPacket> rst(ConstructRstPacket());
1259 std::vector<MockWrite> writes;
1260 writes.push_back(MockWrite(ASYNC, rst->data(), rst->length(), 1));
1261 DeterministicSocketData socket_data(reads, arraysize(reads),
1262 writes.empty() ? nullptr : &writes[0],
1263 writes.size());
1264 socket_factory_.AddSocketDataProvider(&socket_data);
1265 socket_data.StopAfter(1);
1267 MockRead reads2[] = {
1268 MockRead(ASYNC, 0, 0) // EOF
1270 DeterministicSocketData socket_data2(reads2, arraysize(reads2), nullptr, 0);
1271 socket_factory_.AddSocketDataProvider(&socket_data2);
1272 socket_data2.StopAfter(1);
1274 QuicStreamRequest request(&factory_);
1275 EXPECT_EQ(ERR_IO_PENDING,
1276 request.Request(host_port_pair_, is_https_, privacy_mode_,
1277 /*cert_verify_flags=*/0, host_port_pair_.host(),
1278 "GET", net_log_, callback_.callback()));
1280 EXPECT_EQ(OK, callback_.WaitForResult());
1281 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
1282 HttpRequestInfo request_info;
1283 EXPECT_EQ(OK, stream->InitializeStream(&request_info,
1284 DEFAULT_PRIORITY,
1285 net_log_, CompletionCallback()));
1287 // Close the session and verify that stream saw the error.
1288 factory_.CloseAllSessions(ERR_INTERNET_DISCONNECTED);
1289 EXPECT_EQ(ERR_INTERNET_DISCONNECTED,
1290 stream->ReadResponseHeaders(callback_.callback()));
1292 // Now attempting to request a stream to the same origin should create
1293 // a new session.
1295 QuicStreamRequest request2(&factory_);
1296 EXPECT_EQ(ERR_IO_PENDING,
1297 request2.Request(host_port_pair_, is_https_, privacy_mode_,
1298 /*cert_verify_flags=*/0, host_port_pair_.host(),
1299 "GET", net_log_, callback_.callback()));
1301 EXPECT_EQ(OK, callback_.WaitForResult());
1302 stream = request2.ReleaseStream();
1303 stream.reset(); // Will reset stream 3.
1305 EXPECT_TRUE(socket_data.AllReadDataConsumed());
1306 EXPECT_TRUE(socket_data.AllWriteDataConsumed());
1307 EXPECT_TRUE(socket_data2.AllReadDataConsumed());
1308 EXPECT_TRUE(socket_data2.AllWriteDataConsumed());
1311 TEST_P(QuicStreamFactoryTest, OnIPAddressChanged) {
1312 MockRead reads[] = {
1313 MockRead(ASYNC, 0, 0) // EOF
1315 scoped_ptr<QuicEncryptedPacket> rst(ConstructRstPacket());
1316 std::vector<MockWrite> writes;
1317 writes.push_back(MockWrite(ASYNC, rst->data(), rst->length(), 1));
1318 DeterministicSocketData socket_data(reads, arraysize(reads),
1319 writes.empty() ? nullptr : &writes[0],
1320 writes.size());
1321 socket_factory_.AddSocketDataProvider(&socket_data);
1322 socket_data.StopAfter(1);
1324 MockRead reads2[] = {
1325 MockRead(ASYNC, 0, 0) // EOF
1327 DeterministicSocketData socket_data2(reads2, arraysize(reads2), nullptr, 0);
1328 socket_factory_.AddSocketDataProvider(&socket_data2);
1329 socket_data2.StopAfter(1);
1331 QuicStreamRequest request(&factory_);
1332 EXPECT_EQ(ERR_IO_PENDING,
1333 request.Request(host_port_pair_, is_https_, privacy_mode_,
1334 /*cert_verify_flags=*/0, host_port_pair_.host(),
1335 "GET", net_log_, callback_.callback()));
1337 EXPECT_EQ(OK, callback_.WaitForResult());
1338 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
1339 HttpRequestInfo request_info;
1340 EXPECT_EQ(OK, stream->InitializeStream(&request_info,
1341 DEFAULT_PRIORITY,
1342 net_log_, CompletionCallback()));
1344 // Change the IP address and verify that stream saw the error.
1345 factory_.OnIPAddressChanged();
1346 EXPECT_EQ(ERR_NETWORK_CHANGED,
1347 stream->ReadResponseHeaders(callback_.callback()));
1348 EXPECT_TRUE(factory_.require_confirmation());
1350 // Now attempting to request a stream to the same origin should create
1351 // a new session.
1353 QuicStreamRequest request2(&factory_);
1354 EXPECT_EQ(ERR_IO_PENDING,
1355 request2.Request(host_port_pair_, is_https_, privacy_mode_,
1356 /*cert_verify_flags=*/0, host_port_pair_.host(),
1357 "GET", net_log_, callback_.callback()));
1359 EXPECT_EQ(OK, callback_.WaitForResult());
1360 stream = request2.ReleaseStream();
1361 stream.reset(); // Will reset stream 3.
1363 EXPECT_TRUE(socket_data.AllReadDataConsumed());
1364 EXPECT_TRUE(socket_data.AllWriteDataConsumed());
1365 EXPECT_TRUE(socket_data2.AllReadDataConsumed());
1366 EXPECT_TRUE(socket_data2.AllWriteDataConsumed());
1369 TEST_P(QuicStreamFactoryTest, OnSSLConfigChanged) {
1370 MockRead reads[] = {
1371 MockRead(ASYNC, 0, 0) // EOF
1373 scoped_ptr<QuicEncryptedPacket> rst(ConstructRstPacket());
1374 std::vector<MockWrite> writes;
1375 writes.push_back(MockWrite(ASYNC, rst->data(), rst->length(), 1));
1376 DeterministicSocketData socket_data(reads, arraysize(reads),
1377 writes.empty() ? nullptr : &writes[0],
1378 writes.size());
1379 socket_factory_.AddSocketDataProvider(&socket_data);
1380 socket_data.StopAfter(1);
1382 MockRead reads2[] = {
1383 MockRead(ASYNC, 0, 0) // EOF
1385 DeterministicSocketData socket_data2(reads2, arraysize(reads2), nullptr, 0);
1386 socket_factory_.AddSocketDataProvider(&socket_data2);
1387 socket_data2.StopAfter(1);
1389 QuicStreamRequest request(&factory_);
1390 EXPECT_EQ(ERR_IO_PENDING,
1391 request.Request(host_port_pair_, is_https_, privacy_mode_,
1392 /*cert_verify_flags=*/0, host_port_pair_.host(),
1393 "GET", net_log_, callback_.callback()));
1395 EXPECT_EQ(OK, callback_.WaitForResult());
1396 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
1397 HttpRequestInfo request_info;
1398 EXPECT_EQ(OK, stream->InitializeStream(&request_info, DEFAULT_PRIORITY,
1399 net_log_, CompletionCallback()));
1401 factory_.OnSSLConfigChanged();
1402 EXPECT_EQ(ERR_CERT_DATABASE_CHANGED,
1403 stream->ReadResponseHeaders(callback_.callback()));
1404 EXPECT_FALSE(factory_.require_confirmation());
1406 // Now attempting to request a stream to the same origin should create
1407 // a new session.
1409 QuicStreamRequest request2(&factory_);
1410 EXPECT_EQ(ERR_IO_PENDING,
1411 request2.Request(host_port_pair_, is_https_, privacy_mode_,
1412 /*cert_verify_flags=*/0, host_port_pair_.host(),
1413 "GET", net_log_, callback_.callback()));
1415 EXPECT_EQ(OK, callback_.WaitForResult());
1416 stream = request2.ReleaseStream();
1417 stream.reset(); // Will reset stream 3.
1419 EXPECT_TRUE(socket_data.AllReadDataConsumed());
1420 EXPECT_TRUE(socket_data.AllWriteDataConsumed());
1421 EXPECT_TRUE(socket_data2.AllReadDataConsumed());
1422 EXPECT_TRUE(socket_data2.AllWriteDataConsumed());
1425 TEST_P(QuicStreamFactoryTest, OnCertAdded) {
1426 MockRead reads[] = {
1427 MockRead(ASYNC, 0, 0) // EOF
1429 scoped_ptr<QuicEncryptedPacket> rst(ConstructRstPacket());
1430 std::vector<MockWrite> writes;
1431 writes.push_back(MockWrite(ASYNC, rst->data(), rst->length(), 1));
1432 DeterministicSocketData socket_data(reads, arraysize(reads),
1433 writes.empty() ? nullptr : &writes[0],
1434 writes.size());
1435 socket_factory_.AddSocketDataProvider(&socket_data);
1436 socket_data.StopAfter(1);
1438 MockRead reads2[] = {
1439 MockRead(ASYNC, 0, 0) // EOF
1441 DeterministicSocketData socket_data2(reads2, arraysize(reads2), nullptr, 0);
1442 socket_factory_.AddSocketDataProvider(&socket_data2);
1443 socket_data2.StopAfter(1);
1445 QuicStreamRequest request(&factory_);
1446 EXPECT_EQ(ERR_IO_PENDING,
1447 request.Request(host_port_pair_, is_https_, privacy_mode_,
1448 /*cert_verify_flags=*/0, host_port_pair_.host(),
1449 "GET", net_log_, callback_.callback()));
1451 EXPECT_EQ(OK, callback_.WaitForResult());
1452 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
1453 HttpRequestInfo request_info;
1454 EXPECT_EQ(OK, stream->InitializeStream(&request_info,
1455 DEFAULT_PRIORITY,
1456 net_log_, CompletionCallback()));
1458 // Add a cert and verify that stream saw the event.
1459 factory_.OnCertAdded(nullptr);
1460 EXPECT_EQ(ERR_CERT_DATABASE_CHANGED,
1461 stream->ReadResponseHeaders(callback_.callback()));
1462 EXPECT_FALSE(factory_.require_confirmation());
1464 // Now attempting to request a stream to the same origin should create
1465 // a new session.
1467 QuicStreamRequest request2(&factory_);
1468 EXPECT_EQ(ERR_IO_PENDING,
1469 request2.Request(host_port_pair_, is_https_, privacy_mode_,
1470 /*cert_verify_flags=*/0, host_port_pair_.host(),
1471 "GET", net_log_, callback_.callback()));
1473 EXPECT_EQ(OK, callback_.WaitForResult());
1474 stream = request2.ReleaseStream();
1475 stream.reset(); // Will reset stream 3.
1477 EXPECT_TRUE(socket_data.AllReadDataConsumed());
1478 EXPECT_TRUE(socket_data.AllWriteDataConsumed());
1479 EXPECT_TRUE(socket_data2.AllReadDataConsumed());
1480 EXPECT_TRUE(socket_data2.AllWriteDataConsumed());
1483 TEST_P(QuicStreamFactoryTest, OnCACertChanged) {
1484 MockRead reads[] = {
1485 MockRead(ASYNC, 0, 0) // EOF
1487 scoped_ptr<QuicEncryptedPacket> rst(ConstructRstPacket());
1488 std::vector<MockWrite> writes;
1489 writes.push_back(MockWrite(ASYNC, rst->data(), rst->length(), 1));
1490 DeterministicSocketData socket_data(reads, arraysize(reads),
1491 writes.empty() ? nullptr : &writes[0],
1492 writes.size());
1493 socket_factory_.AddSocketDataProvider(&socket_data);
1494 socket_data.StopAfter(1);
1496 MockRead reads2[] = {
1497 MockRead(ASYNC, 0, 0) // EOF
1499 DeterministicSocketData socket_data2(reads2, arraysize(reads2), nullptr, 0);
1500 socket_factory_.AddSocketDataProvider(&socket_data2);
1501 socket_data2.StopAfter(1);
1503 QuicStreamRequest request(&factory_);
1504 EXPECT_EQ(ERR_IO_PENDING,
1505 request.Request(host_port_pair_, is_https_, privacy_mode_,
1506 /*cert_verify_flags=*/0, host_port_pair_.host(),
1507 "GET", net_log_, callback_.callback()));
1509 EXPECT_EQ(OK, callback_.WaitForResult());
1510 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
1511 HttpRequestInfo request_info;
1512 EXPECT_EQ(OK, stream->InitializeStream(&request_info,
1513 DEFAULT_PRIORITY,
1514 net_log_, CompletionCallback()));
1516 // Change the CA cert and verify that stream saw the event.
1517 factory_.OnCACertChanged(nullptr);
1518 EXPECT_EQ(ERR_CERT_DATABASE_CHANGED,
1519 stream->ReadResponseHeaders(callback_.callback()));
1520 EXPECT_FALSE(factory_.require_confirmation());
1522 // Now attempting to request a stream to the same origin should create
1523 // a new session.
1525 QuicStreamRequest request2(&factory_);
1526 EXPECT_EQ(ERR_IO_PENDING,
1527 request2.Request(host_port_pair_, is_https_, privacy_mode_,
1528 /*cert_verify_flags=*/0, host_port_pair_.host(),
1529 "GET", net_log_, callback_.callback()));
1531 EXPECT_EQ(OK, callback_.WaitForResult());
1532 stream = request2.ReleaseStream();
1533 stream.reset(); // Will reset stream 3.
1535 EXPECT_TRUE(socket_data.AllReadDataConsumed());
1536 EXPECT_TRUE(socket_data.AllWriteDataConsumed());
1537 EXPECT_TRUE(socket_data2.AllReadDataConsumed());
1538 EXPECT_TRUE(socket_data2.AllWriteDataConsumed());
1541 TEST_P(QuicStreamFactoryTest, SharedCryptoConfig) {
1542 vector<string> cannoncial_suffixes;
1543 cannoncial_suffixes.push_back(string(".c.youtube.com"));
1544 cannoncial_suffixes.push_back(string(".googlevideo.com"));
1546 for (unsigned i = 0; i < cannoncial_suffixes.size(); ++i) {
1547 string r1_host_name("r1");
1548 string r2_host_name("r2");
1549 r1_host_name.append(cannoncial_suffixes[i]);
1550 r2_host_name.append(cannoncial_suffixes[i]);
1552 HostPortPair host_port_pair1(r1_host_name, 80);
1553 QuicCryptoClientConfig* crypto_config =
1554 QuicStreamFactoryPeer::GetCryptoConfig(&factory_);
1555 QuicServerId server_id1(host_port_pair1, is_https_, privacy_mode_);
1556 QuicCryptoClientConfig::CachedState* cached1 =
1557 crypto_config->LookupOrCreate(server_id1);
1558 EXPECT_FALSE(cached1->proof_valid());
1559 EXPECT_TRUE(cached1->source_address_token().empty());
1561 // Mutate the cached1 to have different data.
1562 // TODO(rtenneti): mutate other members of CachedState.
1563 cached1->set_source_address_token(r1_host_name);
1564 cached1->SetProofValid();
1566 HostPortPair host_port_pair2(r2_host_name, 80);
1567 QuicServerId server_id2(host_port_pair2, is_https_, privacy_mode_);
1568 QuicCryptoClientConfig::CachedState* cached2 =
1569 crypto_config->LookupOrCreate(server_id2);
1570 EXPECT_EQ(cached1->source_address_token(), cached2->source_address_token());
1571 EXPECT_TRUE(cached2->proof_valid());
1575 TEST_P(QuicStreamFactoryTest, CryptoConfigWhenProofIsInvalid) {
1576 vector<string> cannoncial_suffixes;
1577 cannoncial_suffixes.push_back(string(".c.youtube.com"));
1578 cannoncial_suffixes.push_back(string(".googlevideo.com"));
1580 for (unsigned i = 0; i < cannoncial_suffixes.size(); ++i) {
1581 string r3_host_name("r3");
1582 string r4_host_name("r4");
1583 r3_host_name.append(cannoncial_suffixes[i]);
1584 r4_host_name.append(cannoncial_suffixes[i]);
1586 HostPortPair host_port_pair1(r3_host_name, 80);
1587 QuicCryptoClientConfig* crypto_config =
1588 QuicStreamFactoryPeer::GetCryptoConfig(&factory_);
1589 QuicServerId server_id1(host_port_pair1, is_https_, privacy_mode_);
1590 QuicCryptoClientConfig::CachedState* cached1 =
1591 crypto_config->LookupOrCreate(server_id1);
1592 EXPECT_FALSE(cached1->proof_valid());
1593 EXPECT_TRUE(cached1->source_address_token().empty());
1595 // Mutate the cached1 to have different data.
1596 // TODO(rtenneti): mutate other members of CachedState.
1597 cached1->set_source_address_token(r3_host_name);
1598 cached1->SetProofInvalid();
1600 HostPortPair host_port_pair2(r4_host_name, 80);
1601 QuicServerId server_id2(host_port_pair2, is_https_, privacy_mode_);
1602 QuicCryptoClientConfig::CachedState* cached2 =
1603 crypto_config->LookupOrCreate(server_id2);
1604 EXPECT_NE(cached1->source_address_token(), cached2->source_address_token());
1605 EXPECT_TRUE(cached2->source_address_token().empty());
1606 EXPECT_FALSE(cached2->proof_valid());
1610 TEST_P(QuicStreamFactoryTest, RacingConnections) {
1611 if (!GetParam().enable_connection_racing)
1612 return;
1613 factory_.set_quic_server_info_factory(&quic_server_info_factory_);
1614 QuicStreamFactoryPeer::SetTaskRunner(&factory_, runner_.get());
1615 MockRead reads[] = {
1616 MockRead(ASYNC, OK, 0) // EOF
1618 DeterministicSocketData socket_data(reads, arraysize(reads), nullptr, 0);
1619 socket_factory_.AddSocketDataProvider(&socket_data);
1620 socket_data.StopAfter(1);
1622 MockRead reads2[] = {
1623 MockRead(ASYNC, 0, 0) // EOF
1625 DeterministicSocketData socket_data2(reads2, arraysize(reads2), nullptr, 0);
1626 socket_factory_.AddSocketDataProvider(&socket_data2);
1627 socket_data2.StopAfter(1);
1629 crypto_client_stream_factory_.set_handshake_mode(
1630 MockCryptoClientStream::ZERO_RTT);
1631 host_resolver_.set_synchronous_mode(true);
1632 host_resolver_.rules()->AddIPLiteralRule(host_port_pair_.host(),
1633 "192.168.0.1", "");
1635 QuicStreamRequest request(&factory_);
1636 QuicServerId server_id(host_port_pair_, is_https_, privacy_mode_);
1637 EXPECT_EQ(ERR_IO_PENDING,
1638 request.Request(host_port_pair_, is_https_, privacy_mode_,
1639 /*cert_verify_flags=*/0, host_port_pair_.host(),
1640 "GET", net_log_, callback_.callback()));
1641 EXPECT_EQ(2u,
1642 QuicStreamFactoryPeer::GetNumberOfActiveJobs(&factory_, server_id));
1644 runner_->RunNextTask();
1646 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
1647 EXPECT_TRUE(stream.get());
1648 EXPECT_TRUE(socket_data.AllReadDataConsumed());
1649 EXPECT_TRUE(socket_data.AllWriteDataConsumed());
1650 EXPECT_EQ(0u,
1651 QuicStreamFactoryPeer::GetNumberOfActiveJobs(&factory_, server_id));
1654 TEST_P(QuicStreamFactoryTest, EnableNotLoadFromDiskCache) {
1655 factory_.set_quic_server_info_factory(&quic_server_info_factory_);
1656 QuicStreamFactoryPeer::SetTaskRunner(&factory_, runner_.get());
1657 QuicStreamFactoryPeer::SetDisableDiskCache(&factory_, true);
1659 MockRead reads[] = {
1660 MockRead(ASYNC, OK, 0) // EOF
1662 DeterministicSocketData socket_data(reads, arraysize(reads), nullptr, 0);
1663 socket_factory_.AddSocketDataProvider(&socket_data);
1664 socket_data.StopAfter(1);
1666 crypto_client_stream_factory_.set_handshake_mode(
1667 MockCryptoClientStream::ZERO_RTT);
1668 host_resolver_.set_synchronous_mode(true);
1669 host_resolver_.rules()->AddIPLiteralRule(host_port_pair_.host(),
1670 "192.168.0.1", "");
1672 QuicStreamRequest request(&factory_);
1673 EXPECT_EQ(OK, request.Request(host_port_pair_, is_https_, privacy_mode_,
1674 /*cert_verify_flags=*/0, host_port_pair_.host(),
1675 "GET", net_log_, callback_.callback()));
1677 // If we are waiting for disk cache, we would have posted a task. Verify that
1678 // the CancelWaitForDataReady task hasn't been posted.
1679 ASSERT_EQ(0u, runner_->GetPostedTasks().size());
1681 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
1682 EXPECT_TRUE(stream.get());
1683 EXPECT_TRUE(socket_data.AllReadDataConsumed());
1684 EXPECT_TRUE(socket_data.AllWriteDataConsumed());
1687 TEST_P(QuicStreamFactoryTest, BadPacketLoss) {
1688 factory_.set_quic_server_info_factory(&quic_server_info_factory_);
1689 QuicStreamFactoryPeer::SetTaskRunner(&factory_, runner_.get());
1690 QuicStreamFactoryPeer::SetDisableDiskCache(&factory_, true);
1691 QuicStreamFactoryPeer::SetMaxNumberOfLossyConnections(&factory_, 2);
1692 EXPECT_FALSE(
1693 QuicStreamFactoryPeer::IsQuicDisabled(&factory_, host_port_pair_.port()));
1694 EXPECT_EQ(0, QuicStreamFactoryPeer::GetNumberOfLossyConnections(
1695 &factory_, host_port_pair_.port()));
1697 MockRead reads[] = {
1698 MockRead(ASYNC, OK, 0) // EOF
1700 DeterministicSocketData socket_data(reads, arraysize(reads), nullptr, 0);
1701 socket_factory_.AddSocketDataProvider(&socket_data);
1702 socket_data.StopAfter(1);
1704 DeterministicSocketData socket_data2(nullptr, 0, nullptr, 0);
1705 socket_factory_.AddSocketDataProvider(&socket_data2);
1706 socket_data2.StopAfter(1);
1708 DeterministicSocketData socket_data3(nullptr, 0, nullptr, 0);
1709 socket_factory_.AddSocketDataProvider(&socket_data3);
1710 socket_data3.StopAfter(1);
1712 DeterministicSocketData socket_data4(nullptr, 0, nullptr, 0);
1713 socket_factory_.AddSocketDataProvider(&socket_data4);
1714 socket_data4.StopAfter(1);
1716 HostPortPair server2("mail.example.org", kDefaultServerPort);
1717 HostPortPair server3("docs.example.org", kDefaultServerPort);
1718 HostPortPair server4("images.example.org", kDefaultServerPort);
1720 crypto_client_stream_factory_.set_handshake_mode(
1721 MockCryptoClientStream::ZERO_RTT);
1722 host_resolver_.set_synchronous_mode(true);
1723 host_resolver_.rules()->AddIPLiteralRule(host_port_pair_.host(),
1724 "192.168.0.1", "");
1725 host_resolver_.rules()->AddIPLiteralRule(server2.host(), "192.168.0.1", "");
1726 host_resolver_.rules()->AddIPLiteralRule(server3.host(), "192.168.0.1", "");
1727 host_resolver_.rules()->AddIPLiteralRule(server4.host(), "192.168.0.1", "");
1729 QuicStreamRequest request(&factory_);
1730 EXPECT_EQ(OK, request.Request(host_port_pair_, is_https_, privacy_mode_,
1731 /*cert_verify_flags=*/0, host_port_pair_.host(),
1732 "GET", net_log_, callback_.callback()));
1734 QuicChromiumClientSession* session = QuicStreamFactoryPeer::GetActiveSession(
1735 &factory_, host_port_pair_, is_https_);
1737 DVLOG(1) << "Create 1st session and test packet loss";
1739 // Set packet_loss_rate to a lower value than packet_loss_threshold.
1740 EXPECT_FALSE(
1741 factory_.OnHandshakeConfirmed(session, /*packet_loss_rate=*/0.9f));
1742 EXPECT_TRUE(session->connection()->connected());
1743 EXPECT_TRUE(QuicStreamFactoryPeer::HasActiveSession(
1744 &factory_, host_port_pair_, is_https_));
1745 EXPECT_FALSE(
1746 QuicStreamFactoryPeer::IsQuicDisabled(&factory_, host_port_pair_.port()));
1747 EXPECT_EQ(0, QuicStreamFactoryPeer::GetNumberOfLossyConnections(
1748 &factory_, host_port_pair_.port()));
1750 // Set packet_loss_rate to a higher value than packet_loss_threshold only once
1751 // and that shouldn't close the session and it shouldn't disable QUIC.
1752 EXPECT_FALSE(
1753 factory_.OnHandshakeConfirmed(session, /*packet_loss_rate=*/1.0f));
1754 EXPECT_EQ(1, QuicStreamFactoryPeer::GetNumberOfLossyConnections(
1755 &factory_, host_port_pair_.port()));
1756 EXPECT_TRUE(session->connection()->connected());
1757 EXPECT_FALSE(
1758 QuicStreamFactoryPeer::IsQuicDisabled(&factory_, host_port_pair_.port()));
1759 EXPECT_TRUE(QuicStreamFactoryPeer::HasActiveSession(
1760 &factory_, host_port_pair_, is_https_));
1762 // Test N-in-a-row high packet loss connections.
1764 DVLOG(1) << "Create 2nd session and test packet loss";
1766 TestCompletionCallback callback2;
1767 QuicStreamRequest request2(&factory_);
1768 EXPECT_EQ(OK, request2.Request(server2, is_https_, privacy_mode_,
1769 /*cert_verify_flags=*/0, server2.host(), "GET",
1770 net_log_, callback2.callback()));
1771 QuicChromiumClientSession* session2 =
1772 QuicStreamFactoryPeer::GetActiveSession(&factory_, server2, is_https_);
1774 // If there is no packet loss during handshake confirmation, number of lossy
1775 // connections for the port should be 0.
1776 EXPECT_EQ(1, QuicStreamFactoryPeer::GetNumberOfLossyConnections(
1777 &factory_, server2.port()));
1778 EXPECT_FALSE(
1779 factory_.OnHandshakeConfirmed(session2, /*packet_loss_rate=*/0.9f));
1780 EXPECT_EQ(0, QuicStreamFactoryPeer::GetNumberOfLossyConnections(
1781 &factory_, server2.port()));
1782 EXPECT_FALSE(
1783 QuicStreamFactoryPeer::IsQuicDisabled(&factory_, server2.port()));
1785 // Set packet_loss_rate to a higher value than packet_loss_threshold only once
1786 // and that shouldn't close the session and it shouldn't disable QUIC.
1787 EXPECT_FALSE(
1788 factory_.OnHandshakeConfirmed(session2, /*packet_loss_rate=*/1.0f));
1789 EXPECT_EQ(1, QuicStreamFactoryPeer::GetNumberOfLossyConnections(
1790 &factory_, server2.port()));
1791 EXPECT_TRUE(session2->connection()->connected());
1792 EXPECT_FALSE(
1793 QuicStreamFactoryPeer::IsQuicDisabled(&factory_, server2.port()));
1794 EXPECT_TRUE(
1795 QuicStreamFactoryPeer::HasActiveSession(&factory_, server2, is_https_));
1797 DVLOG(1) << "Create 3rd session which also has packet loss";
1799 TestCompletionCallback callback3;
1800 QuicStreamRequest request3(&factory_);
1801 EXPECT_EQ(OK, request3.Request(server3, is_https_, privacy_mode_,
1802 /*cert_verify_flags=*/0, server3.host(), "GET",
1803 net_log_, callback3.callback()));
1804 QuicChromiumClientSession* session3 =
1805 QuicStreamFactoryPeer::GetActiveSession(&factory_, server3, is_https_);
1807 DVLOG(1) << "Create 4th session with packet loss and test IsQuicDisabled()";
1808 TestCompletionCallback callback4;
1809 QuicStreamRequest request4(&factory_);
1810 EXPECT_EQ(OK, request4.Request(server4, is_https_, privacy_mode_,
1811 /*cert_verify_flags=*/0, server4.host(), "GET",
1812 net_log_, callback4.callback()));
1813 QuicChromiumClientSession* session4 =
1814 QuicStreamFactoryPeer::GetActiveSession(&factory_, server4, is_https_);
1816 // Set packet_loss_rate to higher value than packet_loss_threshold 2nd time in
1817 // a row and that should close the session and disable QUIC.
1818 EXPECT_TRUE(
1819 factory_.OnHandshakeConfirmed(session3, /*packet_loss_rate=*/1.0f));
1820 EXPECT_EQ(2, QuicStreamFactoryPeer::GetNumberOfLossyConnections(
1821 &factory_, server3.port()));
1822 EXPECT_FALSE(session3->connection()->connected());
1823 EXPECT_TRUE(QuicStreamFactoryPeer::IsQuicDisabled(&factory_, server3.port()));
1824 EXPECT_FALSE(
1825 QuicStreamFactoryPeer::HasActiveSession(&factory_, server3, is_https_));
1826 EXPECT_FALSE(HasActiveSession(server3));
1828 // Set packet_loss_rate to higher value than packet_loss_threshold 3rd time in
1829 // a row and IsQuicDisabled() should close the session.
1830 EXPECT_TRUE(
1831 factory_.OnHandshakeConfirmed(session4, /*packet_loss_rate=*/1.0f));
1832 EXPECT_EQ(3, QuicStreamFactoryPeer::GetNumberOfLossyConnections(
1833 &factory_, server4.port()));
1834 EXPECT_FALSE(session4->connection()->connected());
1835 EXPECT_TRUE(QuicStreamFactoryPeer::IsQuicDisabled(&factory_, server4.port()));
1836 EXPECT_FALSE(
1837 QuicStreamFactoryPeer::HasActiveSession(&factory_, server4, is_https_));
1838 EXPECT_FALSE(HasActiveSession(server4));
1840 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
1841 EXPECT_TRUE(stream.get());
1842 scoped_ptr<QuicHttpStream> stream2 = request2.ReleaseStream();
1843 EXPECT_TRUE(stream2.get());
1844 scoped_ptr<QuicHttpStream> stream3 = request3.ReleaseStream();
1845 EXPECT_TRUE(stream3.get());
1846 scoped_ptr<QuicHttpStream> stream4 = request4.ReleaseStream();
1847 EXPECT_TRUE(stream4.get());
1848 EXPECT_TRUE(socket_data.AllReadDataConsumed());
1849 EXPECT_TRUE(socket_data.AllWriteDataConsumed());
1850 EXPECT_TRUE(socket_data2.AllReadDataConsumed());
1851 EXPECT_TRUE(socket_data2.AllWriteDataConsumed());
1852 EXPECT_TRUE(socket_data3.AllReadDataConsumed());
1853 EXPECT_TRUE(socket_data3.AllWriteDataConsumed());
1854 EXPECT_TRUE(socket_data4.AllReadDataConsumed());
1855 EXPECT_TRUE(socket_data4.AllWriteDataConsumed());
1858 TEST_P(QuicStreamFactoryTest, PublicResetPostHandshakeTwoOfTwo) {
1859 factory_.set_quic_server_info_factory(&quic_server_info_factory_);
1860 QuicStreamFactoryPeer::SetTaskRunner(&factory_, runner_.get());
1861 QuicStreamFactoryPeer::SetDisableDiskCache(&factory_, true);
1862 QuicStreamFactoryPeer::SetThresholdPublicResetsPostHandshake(&factory_, 2);
1863 EXPECT_FALSE(
1864 QuicStreamFactoryPeer::IsQuicDisabled(&factory_, host_port_pair_.port()));
1865 EXPECT_EQ(0, QuicStreamFactoryPeer::GetNumberOfLossyConnections(
1866 &factory_, host_port_pair_.port()));
1868 MockRead reads[] = {
1869 MockRead(ASYNC, OK, 0) // EOF
1871 DeterministicSocketData socket_data(reads, arraysize(reads), nullptr, 0);
1872 socket_factory_.AddSocketDataProvider(&socket_data);
1873 socket_data.StopAfter(1);
1875 DeterministicSocketData socket_data2(reads, arraysize(reads), nullptr, 0);
1876 socket_factory_.AddSocketDataProvider(&socket_data2);
1877 socket_data2.StopAfter(1);
1879 HostPortPair server2("mail.example.org", kDefaultServerPort);
1881 crypto_client_stream_factory_.set_handshake_mode(
1882 MockCryptoClientStream::CONFIRM_HANDSHAKE);
1883 host_resolver_.set_synchronous_mode(true);
1884 host_resolver_.rules()->AddIPLiteralRule(host_port_pair_.host(),
1885 "192.168.0.1", "");
1886 host_resolver_.rules()->AddIPLiteralRule(server2.host(), "192.168.0.1", "");
1888 QuicStreamRequest request(&factory_);
1889 EXPECT_EQ(OK, request.Request(host_port_pair_, is_https_, privacy_mode_,
1890 /*cert_verify_flags=*/0, host_port_pair_.host(),
1891 "GET", net_log_, callback_.callback()));
1893 QuicChromiumClientSession* session = QuicStreamFactoryPeer::GetActiveSession(
1894 &factory_, host_port_pair_, is_https_);
1896 DVLOG(1) << "Created 1st session. Now trigger public reset post handshake";
1897 session->connection()->CloseConnection(QUIC_PUBLIC_RESET, true);
1898 // Need to spin the loop now to ensure that
1899 // QuicStreamFactory::OnSessionClosed() runs.
1900 base::RunLoop run_loop;
1901 run_loop.RunUntilIdle();
1903 EXPECT_EQ(1,
1904 QuicStreamFactoryPeer::GetNumPublicResetsPostHandshake(&factory_));
1905 EXPECT_FALSE(
1906 QuicStreamFactoryPeer::IsQuicDisabled(&factory_, host_port_pair_.port()));
1908 // Test two-in-a-row public reset post handshakes..
1909 DVLOG(1) << "Create 2nd session and trigger public reset post handshake";
1910 TestCompletionCallback callback2;
1911 QuicStreamRequest request2(&factory_);
1912 EXPECT_EQ(OK, request2.Request(server2, is_https_, privacy_mode_,
1913 /*cert_verify_flags=*/0, server2.host(), "GET",
1914 net_log_, callback2.callback()));
1915 QuicChromiumClientSession* session2 =
1916 QuicStreamFactoryPeer::GetActiveSession(&factory_, server2, is_https_);
1918 session2->connection()->CloseConnection(QUIC_PUBLIC_RESET, true);
1919 // Need to spin the loop now to ensure that
1920 // QuicStreamFactory::OnSessionClosed() runs.
1921 base::RunLoop run_loop2;
1922 run_loop2.RunUntilIdle();
1923 EXPECT_EQ(2,
1924 QuicStreamFactoryPeer::GetNumPublicResetsPostHandshake(&factory_));
1925 EXPECT_TRUE(
1926 QuicStreamFactoryPeer::IsQuicDisabled(&factory_, host_port_pair_.port()));
1927 EXPECT_EQ(
1928 QuicChromiumClientSession::QUIC_DISABLED_PUBLIC_RESET_POST_HANDSHAKE,
1929 factory_.QuicDisabledReason(host_port_pair_.port()));
1931 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
1932 EXPECT_TRUE(stream.get());
1933 scoped_ptr<QuicHttpStream> stream2 = request2.ReleaseStream();
1934 EXPECT_TRUE(stream2.get());
1935 EXPECT_TRUE(socket_data.AllReadDataConsumed());
1936 EXPECT_TRUE(socket_data.AllWriteDataConsumed());
1937 EXPECT_TRUE(socket_data2.AllReadDataConsumed());
1938 EXPECT_TRUE(socket_data2.AllWriteDataConsumed());
1941 TEST_P(QuicStreamFactoryTest, TimeoutsWithOpenStreamsTwoOfTwo) {
1942 factory_.set_quic_server_info_factory(&quic_server_info_factory_);
1943 QuicStreamFactoryPeer::SetTaskRunner(&factory_, runner_.get());
1944 QuicStreamFactoryPeer::SetDisableDiskCache(&factory_, true);
1945 QuicStreamFactoryPeer::SetThresholdTimeoutsWithOpenStreams(&factory_, 2);
1946 EXPECT_FALSE(
1947 QuicStreamFactoryPeer::IsQuicDisabled(&factory_, host_port_pair_.port()));
1948 EXPECT_EQ(0, QuicStreamFactoryPeer::GetNumberOfLossyConnections(
1949 &factory_, host_port_pair_.port()));
1951 MockRead reads[] = {
1952 MockRead(ASYNC, OK, 0) // EOF
1954 DeterministicSocketData socket_data(reads, arraysize(reads), nullptr, 0);
1955 socket_factory_.AddSocketDataProvider(&socket_data);
1956 socket_data.StopAfter(1);
1958 DeterministicSocketData socket_data2(reads, arraysize(reads), nullptr, 0);
1959 socket_factory_.AddSocketDataProvider(&socket_data2);
1960 socket_data2.StopAfter(1);
1962 HostPortPair server2("mail.example.org", kDefaultServerPort);
1964 crypto_client_stream_factory_.set_handshake_mode(
1965 MockCryptoClientStream::CONFIRM_HANDSHAKE);
1966 host_resolver_.set_synchronous_mode(true);
1967 host_resolver_.rules()->AddIPLiteralRule(host_port_pair_.host(),
1968 "192.168.0.1", "");
1969 host_resolver_.rules()->AddIPLiteralRule(server2.host(), "192.168.0.1", "");
1971 QuicStreamRequest request(&factory_);
1972 EXPECT_EQ(OK, request.Request(host_port_pair_, is_https_, privacy_mode_,
1973 /*cert_verify_flags=*/0, host_port_pair_.host(),
1974 "GET", net_log_, callback_.callback()));
1976 QuicChromiumClientSession* session = QuicStreamFactoryPeer::GetActiveSession(
1977 &factory_, host_port_pair_, is_https_);
1979 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
1980 EXPECT_TRUE(stream.get());
1981 HttpRequestInfo request_info;
1982 EXPECT_EQ(OK, stream->InitializeStream(&request_info, DEFAULT_PRIORITY,
1983 net_log_, CompletionCallback()));
1985 DVLOG(1)
1986 << "Created 1st session and initialized a stream. Now trigger timeout";
1987 session->connection()->CloseConnection(QUIC_CONNECTION_TIMED_OUT, false);
1988 // Need to spin the loop now to ensure that
1989 // QuicStreamFactory::OnSessionClosed() runs.
1990 base::RunLoop run_loop;
1991 run_loop.RunUntilIdle();
1993 EXPECT_EQ(1, QuicStreamFactoryPeer::GetNumTimeoutsWithOpenStreams(&factory_));
1994 EXPECT_FALSE(
1995 QuicStreamFactoryPeer::IsQuicDisabled(&factory_, host_port_pair_.port()));
1997 // Test two-in-a-row timeouts with open streams.
1998 DVLOG(1) << "Create 2nd session and timeout with open stream";
1999 TestCompletionCallback callback2;
2000 QuicStreamRequest request2(&factory_);
2001 EXPECT_EQ(OK, request2.Request(server2, is_https_, privacy_mode_,
2002 /*cert_verify_flags=*/0, server2.host(), "GET",
2003 net_log_, callback2.callback()));
2004 QuicChromiumClientSession* session2 =
2005 QuicStreamFactoryPeer::GetActiveSession(&factory_, server2, is_https_);
2007 scoped_ptr<QuicHttpStream> stream2 = request2.ReleaseStream();
2008 EXPECT_TRUE(stream2.get());
2009 EXPECT_EQ(OK, stream2->InitializeStream(&request_info, DEFAULT_PRIORITY,
2010 net_log_, CompletionCallback()));
2012 session2->connection()->CloseConnection(QUIC_CONNECTION_TIMED_OUT, false);
2013 // Need to spin the loop now to ensure that
2014 // QuicStreamFactory::OnSessionClosed() runs.
2015 base::RunLoop run_loop2;
2016 run_loop2.RunUntilIdle();
2017 EXPECT_EQ(2, QuicStreamFactoryPeer::GetNumTimeoutsWithOpenStreams(&factory_));
2018 EXPECT_TRUE(
2019 QuicStreamFactoryPeer::IsQuicDisabled(&factory_, host_port_pair_.port()));
2020 EXPECT_EQ(QuicChromiumClientSession::QUIC_DISABLED_TIMEOUT_WITH_OPEN_STREAMS,
2021 factory_.QuicDisabledReason(host_port_pair_.port()));
2023 EXPECT_TRUE(socket_data.AllReadDataConsumed());
2024 EXPECT_TRUE(socket_data.AllWriteDataConsumed());
2025 EXPECT_TRUE(socket_data2.AllReadDataConsumed());
2026 EXPECT_TRUE(socket_data2.AllWriteDataConsumed());
2029 TEST_P(QuicStreamFactoryTest, PublicResetPostHandshakeTwoOfThree) {
2030 factory_.set_quic_server_info_factory(&quic_server_info_factory_);
2031 QuicStreamFactoryPeer::SetTaskRunner(&factory_, runner_.get());
2032 QuicStreamFactoryPeer::SetDisableDiskCache(&factory_, true);
2033 QuicStreamFactoryPeer::SetThresholdPublicResetsPostHandshake(&factory_, 2);
2034 EXPECT_FALSE(
2035 QuicStreamFactoryPeer::IsQuicDisabled(&factory_, host_port_pair_.port()));
2036 EXPECT_EQ(0, QuicStreamFactoryPeer::GetNumberOfLossyConnections(
2037 &factory_, host_port_pair_.port()));
2039 MockRead reads[] = {
2040 MockRead(ASYNC, OK, 0) // EOF
2042 DeterministicSocketData socket_data(reads, arraysize(reads), nullptr, 0);
2043 socket_factory_.AddSocketDataProvider(&socket_data);
2044 socket_data.StopAfter(1);
2046 DeterministicSocketData socket_data2(reads, arraysize(reads), nullptr, 0);
2047 socket_factory_.AddSocketDataProvider(&socket_data2);
2048 socket_data2.StopAfter(1);
2050 DeterministicSocketData socket_data3(reads, arraysize(reads), nullptr, 0);
2051 socket_factory_.AddSocketDataProvider(&socket_data3);
2052 socket_data3.StopAfter(1);
2054 HostPortPair server2("mail.example.org", kDefaultServerPort);
2055 HostPortPair server3("docs.example.org", kDefaultServerPort);
2057 crypto_client_stream_factory_.set_handshake_mode(
2058 MockCryptoClientStream::CONFIRM_HANDSHAKE);
2059 host_resolver_.set_synchronous_mode(true);
2060 host_resolver_.rules()->AddIPLiteralRule(host_port_pair_.host(),
2061 "192.168.0.1", "");
2062 host_resolver_.rules()->AddIPLiteralRule(server2.host(), "192.168.0.1", "");
2063 host_resolver_.rules()->AddIPLiteralRule(server3.host(), "192.168.0.1", "");
2065 // Test first and third out of three public reset post handshakes.
2066 QuicStreamRequest request(&factory_);
2067 EXPECT_EQ(OK, request.Request(host_port_pair_, is_https_, privacy_mode_,
2068 /*cert_verify_flags=*/0, host_port_pair_.host(),
2069 "GET", net_log_, callback_.callback()));
2071 QuicChromiumClientSession* session = QuicStreamFactoryPeer::GetActiveSession(
2072 &factory_, host_port_pair_, is_https_);
2074 DVLOG(1) << "Created 1st session. Now trigger public reset post handshake";
2075 session->connection()->CloseConnection(QUIC_PUBLIC_RESET, true);
2076 // Need to spin the loop now to ensure that
2077 // QuicStreamFactory::OnSessionClosed() runs.
2078 base::RunLoop run_loop;
2079 run_loop.RunUntilIdle();
2081 EXPECT_EQ(1,
2082 QuicStreamFactoryPeer::GetNumPublicResetsPostHandshake(&factory_));
2083 EXPECT_FALSE(
2084 QuicStreamFactoryPeer::IsQuicDisabled(&factory_, host_port_pair_.port()));
2086 DVLOG(1) << "Create 2nd session without disable trigger";
2087 TestCompletionCallback callback2;
2088 QuicStreamRequest request2(&factory_);
2089 EXPECT_EQ(OK, request2.Request(server2, is_https_, privacy_mode_,
2090 /*cert_verify_flags=*/0, server2.host(), "GET",
2091 net_log_, callback2.callback()));
2092 QuicChromiumClientSession* session2 =
2093 QuicStreamFactoryPeer::GetActiveSession(&factory_, server2, is_https_);
2095 session2->connection()->CloseConnection(QUIC_NO_ERROR, false);
2096 // Need to spin the loop now to ensure that
2097 // QuicStreamFactory::OnSessionClosed() runs.
2098 base::RunLoop run_loop2;
2099 run_loop2.RunUntilIdle();
2100 EXPECT_EQ(1,
2101 QuicStreamFactoryPeer::GetNumPublicResetsPostHandshake(&factory_));
2102 EXPECT_FALSE(
2103 QuicStreamFactoryPeer::IsQuicDisabled(&factory_, host_port_pair_.port()));
2105 DVLOG(1) << "Create 3rd session with public reset post handshake,"
2106 << " will disable QUIC";
2107 TestCompletionCallback callback3;
2108 QuicStreamRequest request3(&factory_);
2109 EXPECT_EQ(OK, request3.Request(server3, is_https_, privacy_mode_,
2110 /*cert_verify_flags=*/0, server3.host(), "GET",
2111 net_log_, callback3.callback()));
2112 QuicChromiumClientSession* session3 =
2113 QuicStreamFactoryPeer::GetActiveSession(&factory_, server3, is_https_);
2115 session3->connection()->CloseConnection(QUIC_PUBLIC_RESET, true);
2116 // Need to spin the loop now to ensure that
2117 // QuicStreamFactory::OnSessionClosed() runs.
2118 base::RunLoop run_loop3;
2119 run_loop3.RunUntilIdle();
2120 EXPECT_EQ(2,
2121 QuicStreamFactoryPeer::GetNumPublicResetsPostHandshake(&factory_));
2122 EXPECT_TRUE(
2123 QuicStreamFactoryPeer::IsQuicDisabled(&factory_, host_port_pair_.port()));
2124 EXPECT_EQ(
2125 QuicChromiumClientSession::QUIC_DISABLED_PUBLIC_RESET_POST_HANDSHAKE,
2126 factory_.QuicDisabledReason(host_port_pair_.port()));
2128 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
2129 EXPECT_TRUE(stream.get());
2130 scoped_ptr<QuicHttpStream> stream2 = request2.ReleaseStream();
2131 EXPECT_TRUE(stream2.get());
2132 scoped_ptr<QuicHttpStream> stream3 = request3.ReleaseStream();
2133 EXPECT_TRUE(stream3.get());
2135 EXPECT_TRUE(socket_data.AllReadDataConsumed());
2136 EXPECT_TRUE(socket_data.AllWriteDataConsumed());
2137 EXPECT_TRUE(socket_data2.AllReadDataConsumed());
2138 EXPECT_TRUE(socket_data2.AllWriteDataConsumed());
2139 EXPECT_TRUE(socket_data3.AllReadDataConsumed());
2140 EXPECT_TRUE(socket_data3.AllWriteDataConsumed());
2143 TEST_P(QuicStreamFactoryTest, TimeoutsWithOpenStreamsTwoOfThree) {
2144 factory_.set_quic_server_info_factory(&quic_server_info_factory_);
2145 QuicStreamFactoryPeer::SetTaskRunner(&factory_, runner_.get());
2146 QuicStreamFactoryPeer::SetDisableDiskCache(&factory_, true);
2147 QuicStreamFactoryPeer::SetThresholdTimeoutsWithOpenStreams(&factory_, 2);
2148 EXPECT_FALSE(
2149 QuicStreamFactoryPeer::IsQuicDisabled(&factory_, host_port_pair_.port()));
2150 EXPECT_EQ(0, QuicStreamFactoryPeer::GetNumberOfLossyConnections(
2151 &factory_, host_port_pair_.port()));
2153 MockRead reads[] = {
2154 MockRead(ASYNC, OK, 0) // EOF
2156 DeterministicSocketData socket_data(reads, arraysize(reads), nullptr, 0);
2157 socket_factory_.AddSocketDataProvider(&socket_data);
2158 socket_data.StopAfter(1);
2160 // DeterministicSocketData socket_data2(nullptr, 0, nullptr, 0);
2161 DeterministicSocketData socket_data2(reads, arraysize(reads), nullptr, 0);
2162 socket_factory_.AddSocketDataProvider(&socket_data2);
2163 socket_data2.StopAfter(1);
2165 DeterministicSocketData socket_data3(reads, arraysize(reads), nullptr, 0);
2166 socket_factory_.AddSocketDataProvider(&socket_data3);
2167 socket_data3.StopAfter(1);
2169 HostPortPair server2("mail.example.org", kDefaultServerPort);
2170 HostPortPair server3("docs.example.org", kDefaultServerPort);
2172 crypto_client_stream_factory_.set_handshake_mode(
2173 MockCryptoClientStream::CONFIRM_HANDSHAKE);
2174 host_resolver_.set_synchronous_mode(true);
2175 host_resolver_.rules()->AddIPLiteralRule(host_port_pair_.host(),
2176 "192.168.0.1", "");
2177 host_resolver_.rules()->AddIPLiteralRule(server2.host(), "192.168.0.1", "");
2178 host_resolver_.rules()->AddIPLiteralRule(server3.host(), "192.168.0.1", "");
2180 // Test first and third out of three timeouts with open streams.
2181 QuicStreamRequest request(&factory_);
2182 EXPECT_EQ(OK, request.Request(host_port_pair_, is_https_, privacy_mode_,
2183 /*cert_verify_flags=*/0, host_port_pair_.host(),
2184 "GET", net_log_, callback_.callback()));
2186 QuicChromiumClientSession* session = QuicStreamFactoryPeer::GetActiveSession(
2187 &factory_, host_port_pair_, is_https_);
2189 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
2190 EXPECT_TRUE(stream.get());
2191 HttpRequestInfo request_info;
2192 EXPECT_EQ(OK, stream->InitializeStream(&request_info, DEFAULT_PRIORITY,
2193 net_log_, CompletionCallback()));
2195 DVLOG(1)
2196 << "Created 1st session and initialized a stream. Now trigger timeout";
2197 session->connection()->CloseConnection(QUIC_CONNECTION_TIMED_OUT, false);
2198 // Need to spin the loop now to ensure that
2199 // QuicStreamFactory::OnSessionClosed() runs.
2200 base::RunLoop run_loop;
2201 run_loop.RunUntilIdle();
2203 EXPECT_EQ(1, QuicStreamFactoryPeer::GetNumTimeoutsWithOpenStreams(&factory_));
2204 EXPECT_FALSE(
2205 QuicStreamFactoryPeer::IsQuicDisabled(&factory_, host_port_pair_.port()));
2207 // Test two-in-a-row timeouts with open streams.
2208 DVLOG(1) << "Create 2nd session without timeout";
2209 TestCompletionCallback callback2;
2210 QuicStreamRequest request2(&factory_);
2211 EXPECT_EQ(OK, request2.Request(server2, is_https_, privacy_mode_,
2212 /*cert_verify_flags=*/0, server2.host(), "GET",
2213 net_log_, callback2.callback()));
2214 QuicChromiumClientSession* session2 =
2215 QuicStreamFactoryPeer::GetActiveSession(&factory_, server2, is_https_);
2217 session2->connection()->CloseConnection(QUIC_NO_ERROR, true);
2218 // Need to spin the loop now to ensure that
2219 // QuicStreamFactory::OnSessionClosed() runs.
2220 base::RunLoop run_loop2;
2221 run_loop2.RunUntilIdle();
2222 EXPECT_EQ(1, QuicStreamFactoryPeer::GetNumTimeoutsWithOpenStreams(&factory_));
2223 EXPECT_FALSE(
2224 QuicStreamFactoryPeer::IsQuicDisabled(&factory_, host_port_pair_.port()));
2226 DVLOG(1) << "Create 3rd session with timeout with open streams,"
2227 << " will disable QUIC";
2229 TestCompletionCallback callback3;
2230 QuicStreamRequest request3(&factory_);
2231 EXPECT_EQ(OK, request3.Request(server3, is_https_, privacy_mode_,
2232 /*cert_verify_flags=*/0, server3.host(), "GET",
2233 net_log_, callback3.callback()));
2234 QuicChromiumClientSession* session3 =
2235 QuicStreamFactoryPeer::GetActiveSession(&factory_, server3, is_https_);
2237 scoped_ptr<QuicHttpStream> stream3 = request3.ReleaseStream();
2238 EXPECT_TRUE(stream3.get());
2239 EXPECT_EQ(OK, stream3->InitializeStream(&request_info, DEFAULT_PRIORITY,
2240 net_log_, CompletionCallback()));
2241 session3->connection()->CloseConnection(QUIC_CONNECTION_TIMED_OUT, false);
2242 // Need to spin the loop now to ensure that
2243 // QuicStreamFactory::OnSessionClosed() runs.
2244 base::RunLoop run_loop3;
2245 run_loop3.RunUntilIdle();
2246 EXPECT_EQ(2, QuicStreamFactoryPeer::GetNumTimeoutsWithOpenStreams(&factory_));
2247 EXPECT_TRUE(
2248 QuicStreamFactoryPeer::IsQuicDisabled(&factory_, host_port_pair_.port()));
2249 EXPECT_EQ(QuicChromiumClientSession::QUIC_DISABLED_TIMEOUT_WITH_OPEN_STREAMS,
2250 factory_.QuicDisabledReason(host_port_pair_.port()));
2252 scoped_ptr<QuicHttpStream> stream2 = request2.ReleaseStream();
2253 EXPECT_TRUE(stream2.get());
2254 EXPECT_TRUE(socket_data.AllReadDataConsumed());
2255 EXPECT_TRUE(socket_data.AllWriteDataConsumed());
2256 EXPECT_TRUE(socket_data2.AllReadDataConsumed());
2257 EXPECT_TRUE(socket_data2.AllWriteDataConsumed());
2258 EXPECT_TRUE(socket_data3.AllReadDataConsumed());
2259 EXPECT_TRUE(socket_data3.AllWriteDataConsumed());
2262 TEST_P(QuicStreamFactoryTest, PublicResetPostHandshakeTwoOfFour) {
2263 factory_.set_quic_server_info_factory(&quic_server_info_factory_);
2264 QuicStreamFactoryPeer::SetTaskRunner(&factory_, runner_.get());
2265 QuicStreamFactoryPeer::SetDisableDiskCache(&factory_, true);
2266 QuicStreamFactoryPeer::SetThresholdPublicResetsPostHandshake(&factory_, 2);
2267 EXPECT_FALSE(
2268 QuicStreamFactoryPeer::IsQuicDisabled(&factory_, host_port_pair_.port()));
2269 EXPECT_EQ(0, QuicStreamFactoryPeer::GetNumberOfLossyConnections(
2270 &factory_, host_port_pair_.port()));
2272 MockRead reads[] = {
2273 MockRead(ASYNC, OK, 0) // EOF
2275 DeterministicSocketData socket_data(reads, arraysize(reads), nullptr, 0);
2276 socket_factory_.AddSocketDataProvider(&socket_data);
2277 socket_data.StopAfter(1);
2279 DeterministicSocketData socket_data2(reads, arraysize(reads), nullptr, 0);
2280 socket_factory_.AddSocketDataProvider(&socket_data2);
2281 socket_data2.StopAfter(1);
2283 DeterministicSocketData socket_data3(reads, arraysize(reads), nullptr, 0);
2284 socket_factory_.AddSocketDataProvider(&socket_data3);
2285 socket_data3.StopAfter(1);
2287 DeterministicSocketData socket_data4(reads, arraysize(reads), nullptr, 0);
2288 socket_factory_.AddSocketDataProvider(&socket_data4);
2289 socket_data4.StopAfter(1);
2291 HostPortPair server2("mail.example.org", kDefaultServerPort);
2292 HostPortPair server3("docs.example.org", kDefaultServerPort);
2293 HostPortPair server4("images.example.org", kDefaultServerPort);
2295 crypto_client_stream_factory_.set_handshake_mode(
2296 MockCryptoClientStream::CONFIRM_HANDSHAKE);
2297 host_resolver_.set_synchronous_mode(true);
2298 host_resolver_.rules()->AddIPLiteralRule(host_port_pair_.host(),
2299 "192.168.0.1", "");
2300 host_resolver_.rules()->AddIPLiteralRule(server2.host(), "192.168.0.1", "");
2301 host_resolver_.rules()->AddIPLiteralRule(server3.host(), "192.168.0.1", "");
2302 host_resolver_.rules()->AddIPLiteralRule(server4.host(), "192.168.0.1", "");
2304 // Test first and fourth out of four public reset post handshakes.
2305 QuicStreamRequest request(&factory_);
2306 EXPECT_EQ(OK, request.Request(host_port_pair_, is_https_, privacy_mode_,
2307 /*cert_verify_flags=*/0, host_port_pair_.host(),
2308 "GET", net_log_, callback_.callback()));
2310 QuicChromiumClientSession* session = QuicStreamFactoryPeer::GetActiveSession(
2311 &factory_, host_port_pair_, is_https_);
2313 DVLOG(1) << "Created 1st session. Now trigger public reset post handshake";
2314 session->connection()->CloseConnection(QUIC_PUBLIC_RESET, true);
2315 // Need to spin the loop now to ensure that
2316 // QuicStreamFactory::OnSessionClosed() runs.
2317 base::RunLoop run_loop;
2318 run_loop.RunUntilIdle();
2320 EXPECT_EQ(1,
2321 QuicStreamFactoryPeer::GetNumPublicResetsPostHandshake(&factory_));
2322 EXPECT_FALSE(
2323 QuicStreamFactoryPeer::IsQuicDisabled(&factory_, host_port_pair_.port()));
2325 DVLOG(1) << "Create 2nd and 3rd sessions without disable trigger";
2326 TestCompletionCallback callback2;
2327 QuicStreamRequest request2(&factory_);
2328 EXPECT_EQ(OK, request2.Request(server2, is_https_, privacy_mode_,
2329 /*cert_verify_flags=*/0, server2.host(), "GET",
2330 net_log_, callback2.callback()));
2331 QuicChromiumClientSession* session2 =
2332 QuicStreamFactoryPeer::GetActiveSession(&factory_, server2, is_https_);
2334 session2->connection()->CloseConnection(QUIC_NO_ERROR, false);
2335 // Need to spin the loop now to ensure that
2336 // QuicStreamFactory::OnSessionClosed() runs.
2337 base::RunLoop run_loop2;
2338 run_loop2.RunUntilIdle();
2339 EXPECT_EQ(1,
2340 QuicStreamFactoryPeer::GetNumPublicResetsPostHandshake(&factory_));
2341 EXPECT_FALSE(
2342 QuicStreamFactoryPeer::IsQuicDisabled(&factory_, host_port_pair_.port()));
2344 TestCompletionCallback callback3;
2345 QuicStreamRequest request3(&factory_);
2346 EXPECT_EQ(OK, request3.Request(server3, is_https_, privacy_mode_,
2347 /*cert_verify_flags=*/0, server3.host(), "GET",
2348 net_log_, callback3.callback()));
2349 QuicChromiumClientSession* session3 =
2350 QuicStreamFactoryPeer::GetActiveSession(&factory_, server3, is_https_);
2352 session3->connection()->CloseConnection(QUIC_NO_ERROR, false);
2353 // Need to spin the loop now to ensure that
2354 // QuicStreamFactory::OnSessionClosed() runs.
2355 base::RunLoop run_loop3;
2356 run_loop3.RunUntilIdle();
2357 EXPECT_EQ(1,
2358 QuicStreamFactoryPeer::GetNumPublicResetsPostHandshake(&factory_));
2359 EXPECT_FALSE(
2360 QuicStreamFactoryPeer::IsQuicDisabled(&factory_, host_port_pair_.port()));
2362 DVLOG(1) << "Create 4rd session with public reset post handshake,"
2363 << " will not disable QUIC";
2364 TestCompletionCallback callback4;
2365 QuicStreamRequest request4(&factory_);
2366 EXPECT_EQ(OK, request4.Request(server4, is_https_, privacy_mode_,
2367 /*cert_verify_flags=*/0, server4.host(), "GET",
2368 net_log_, callback4.callback()));
2369 QuicChromiumClientSession* session4 =
2370 QuicStreamFactoryPeer::GetActiveSession(&factory_, server4, is_https_);
2372 session4->connection()->CloseConnection(QUIC_PUBLIC_RESET, true);
2373 // Need to spin the loop now to ensure that
2374 // QuicStreamFactory::OnSessionClosed() runs.
2375 base::RunLoop run_loop4;
2376 run_loop4.RunUntilIdle();
2377 EXPECT_EQ(1,
2378 QuicStreamFactoryPeer::GetNumPublicResetsPostHandshake(&factory_));
2379 EXPECT_FALSE(
2380 QuicStreamFactoryPeer::IsQuicDisabled(&factory_, host_port_pair_.port()));
2382 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
2383 EXPECT_TRUE(stream.get());
2384 scoped_ptr<QuicHttpStream> stream2 = request2.ReleaseStream();
2385 EXPECT_TRUE(stream2.get());
2386 scoped_ptr<QuicHttpStream> stream3 = request3.ReleaseStream();
2387 EXPECT_TRUE(stream3.get());
2388 scoped_ptr<QuicHttpStream> stream4 = request4.ReleaseStream();
2389 EXPECT_TRUE(stream4.get());
2391 EXPECT_TRUE(socket_data.AllReadDataConsumed());
2392 EXPECT_TRUE(socket_data.AllWriteDataConsumed());
2393 EXPECT_TRUE(socket_data2.AllReadDataConsumed());
2394 EXPECT_TRUE(socket_data2.AllWriteDataConsumed());
2395 EXPECT_TRUE(socket_data3.AllReadDataConsumed());
2396 EXPECT_TRUE(socket_data3.AllWriteDataConsumed());
2397 EXPECT_TRUE(socket_data4.AllReadDataConsumed());
2398 EXPECT_TRUE(socket_data4.AllWriteDataConsumed());
2401 TEST_P(QuicStreamFactoryTest, TimeoutsWithOpenStreamsTwoOfFour) {
2402 factory_.set_quic_server_info_factory(&quic_server_info_factory_);
2403 QuicStreamFactoryPeer::SetTaskRunner(&factory_, runner_.get());
2404 QuicStreamFactoryPeer::SetDisableDiskCache(&factory_, true);
2405 QuicStreamFactoryPeer::SetThresholdTimeoutsWithOpenStreams(&factory_, 2);
2406 EXPECT_FALSE(
2407 QuicStreamFactoryPeer::IsQuicDisabled(&factory_, host_port_pair_.port()));
2408 EXPECT_EQ(0, QuicStreamFactoryPeer::GetNumberOfLossyConnections(
2409 &factory_, host_port_pair_.port()));
2411 MockRead reads[] = {
2412 MockRead(ASYNC, OK, 0) // EOF
2414 DeterministicSocketData socket_data(reads, arraysize(reads), nullptr, 0);
2415 socket_factory_.AddSocketDataProvider(&socket_data);
2416 socket_data.StopAfter(1);
2418 // DeterministicSocketData socket_data2(nullptr, 0, nullptr, 0);
2419 DeterministicSocketData socket_data2(reads, arraysize(reads), nullptr, 0);
2420 socket_factory_.AddSocketDataProvider(&socket_data2);
2421 socket_data2.StopAfter(1);
2423 DeterministicSocketData socket_data3(reads, arraysize(reads), nullptr, 0);
2424 socket_factory_.AddSocketDataProvider(&socket_data3);
2425 socket_data3.StopAfter(1);
2427 DeterministicSocketData socket_data4(reads, arraysize(reads), nullptr, 0);
2428 socket_factory_.AddSocketDataProvider(&socket_data4);
2429 socket_data4.StopAfter(1);
2431 HostPortPair server2("mail.example.org", kDefaultServerPort);
2432 HostPortPair server3("docs.example.org", kDefaultServerPort);
2433 HostPortPair server4("images.example.org", kDefaultServerPort);
2435 crypto_client_stream_factory_.set_handshake_mode(
2436 MockCryptoClientStream::CONFIRM_HANDSHAKE);
2437 host_resolver_.set_synchronous_mode(true);
2438 host_resolver_.rules()->AddIPLiteralRule(host_port_pair_.host(),
2439 "192.168.0.1", "");
2440 host_resolver_.rules()->AddIPLiteralRule(server2.host(), "192.168.0.1", "");
2441 host_resolver_.rules()->AddIPLiteralRule(server3.host(), "192.168.0.1", "");
2442 host_resolver_.rules()->AddIPLiteralRule(server4.host(), "192.168.0.1", "");
2444 // Test first and fourth out of three timeouts with open streams.
2445 QuicStreamRequest request(&factory_);
2446 EXPECT_EQ(OK, request.Request(host_port_pair_, is_https_, privacy_mode_,
2447 /*cert_verify_flags=*/0, host_port_pair_.host(),
2448 "GET", net_log_, callback_.callback()));
2450 QuicChromiumClientSession* session = QuicStreamFactoryPeer::GetActiveSession(
2451 &factory_, host_port_pair_, is_https_);
2453 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
2454 EXPECT_TRUE(stream.get());
2455 HttpRequestInfo request_info;
2456 EXPECT_EQ(OK, stream->InitializeStream(&request_info, DEFAULT_PRIORITY,
2457 net_log_, CompletionCallback()));
2459 DVLOG(1)
2460 << "Created 1st session and initialized a stream. Now trigger timeout";
2461 session->connection()->CloseConnection(QUIC_CONNECTION_TIMED_OUT, false);
2462 // Need to spin the loop now to ensure that
2463 // QuicStreamFactory::OnSessionClosed() runs.
2464 base::RunLoop run_loop;
2465 run_loop.RunUntilIdle();
2467 EXPECT_EQ(1, QuicStreamFactoryPeer::GetNumTimeoutsWithOpenStreams(&factory_));
2468 EXPECT_FALSE(
2469 QuicStreamFactoryPeer::IsQuicDisabled(&factory_, host_port_pair_.port()));
2471 DVLOG(1) << "Create 2nd and 3rd sessions without timeout";
2472 TestCompletionCallback callback2;
2473 QuicStreamRequest request2(&factory_);
2474 EXPECT_EQ(OK, request2.Request(server2, is_https_, privacy_mode_,
2475 /*cert_verify_flags=*/0, server2.host(), "GET",
2476 net_log_, callback2.callback()));
2477 QuicChromiumClientSession* session2 =
2478 QuicStreamFactoryPeer::GetActiveSession(&factory_, server2, is_https_);
2480 session2->connection()->CloseConnection(QUIC_NO_ERROR, true);
2481 // Need to spin the loop now to ensure that
2482 // QuicStreamFactory::OnSessionClosed() runs.
2483 base::RunLoop run_loop2;
2484 run_loop2.RunUntilIdle();
2485 EXPECT_EQ(1, QuicStreamFactoryPeer::GetNumTimeoutsWithOpenStreams(&factory_));
2486 EXPECT_FALSE(
2487 QuicStreamFactoryPeer::IsQuicDisabled(&factory_, host_port_pair_.port()));
2489 TestCompletionCallback callback3;
2490 QuicStreamRequest request3(&factory_);
2491 EXPECT_EQ(OK, request3.Request(server3, is_https_, privacy_mode_,
2492 /*cert_verify_flags=*/0, server3.host(), "GET",
2493 net_log_, callback3.callback()));
2494 QuicChromiumClientSession* session3 =
2495 QuicStreamFactoryPeer::GetActiveSession(&factory_, server3, is_https_);
2497 session3->connection()->CloseConnection(QUIC_NO_ERROR, true);
2498 // Need to spin the loop now to ensure that
2499 // QuicStreamFactory::OnSessionClosed() runs.
2500 base::RunLoop run_loop3;
2501 run_loop3.RunUntilIdle();
2502 EXPECT_EQ(1, QuicStreamFactoryPeer::GetNumTimeoutsWithOpenStreams(&factory_));
2503 EXPECT_FALSE(
2504 QuicStreamFactoryPeer::IsQuicDisabled(&factory_, host_port_pair_.port()));
2506 DVLOG(1) << "Create 4th session with timeout with open streams,"
2507 << " will not disable QUIC";
2509 TestCompletionCallback callback4;
2510 QuicStreamRequest request4(&factory_);
2511 EXPECT_EQ(OK, request4.Request(server4, is_https_, privacy_mode_,
2512 /*cert_verify_flags=*/0, server4.host(), "GET",
2513 net_log_, callback4.callback()));
2514 QuicChromiumClientSession* session4 =
2515 QuicStreamFactoryPeer::GetActiveSession(&factory_, server4, is_https_);
2517 scoped_ptr<QuicHttpStream> stream4 = request4.ReleaseStream();
2518 EXPECT_TRUE(stream4.get());
2519 EXPECT_EQ(OK, stream4->InitializeStream(&request_info, DEFAULT_PRIORITY,
2520 net_log_, CompletionCallback()));
2521 session4->connection()->CloseConnection(QUIC_CONNECTION_TIMED_OUT, false);
2522 // Need to spin the loop now to ensure that
2523 // QuicStreamFactory::OnSessionClosed() runs.
2524 base::RunLoop run_loop4;
2525 run_loop4.RunUntilIdle();
2526 EXPECT_EQ(1, QuicStreamFactoryPeer::GetNumTimeoutsWithOpenStreams(&factory_));
2527 EXPECT_FALSE(
2528 QuicStreamFactoryPeer::IsQuicDisabled(&factory_, host_port_pair_.port()));
2530 scoped_ptr<QuicHttpStream> stream2 = request2.ReleaseStream();
2531 EXPECT_TRUE(stream2.get());
2532 scoped_ptr<QuicHttpStream> stream3 = request3.ReleaseStream();
2533 EXPECT_TRUE(stream3.get());
2534 EXPECT_TRUE(socket_data.AllReadDataConsumed());
2535 EXPECT_TRUE(socket_data.AllWriteDataConsumed());
2536 EXPECT_TRUE(socket_data2.AllReadDataConsumed());
2537 EXPECT_TRUE(socket_data2.AllWriteDataConsumed());
2538 EXPECT_TRUE(socket_data3.AllReadDataConsumed());
2539 EXPECT_TRUE(socket_data3.AllWriteDataConsumed());
2540 EXPECT_TRUE(socket_data4.AllReadDataConsumed());
2541 EXPECT_TRUE(socket_data4.AllWriteDataConsumed());
2544 TEST_P(QuicStreamFactoryTest, EnableDelayTcpRace) {
2545 bool delay_tcp_race = QuicStreamFactoryPeer::GetDelayTcpRace(&factory_);
2546 QuicStreamFactoryPeer::SetDelayTcpRace(&factory_, false);
2547 MockRead reads[] = {
2548 MockRead(ASYNC, OK, 0),
2550 DeterministicSocketData socket_data(reads, arraysize(reads), nullptr, 0);
2551 socket_factory_.AddSocketDataProvider(&socket_data);
2552 socket_data.StopAfter(1);
2554 // Set up data in HttpServerProperties.
2555 scoped_ptr<HttpServerProperties> http_server_properties(
2556 new HttpServerPropertiesImpl());
2557 QuicStreamFactoryPeer::SetHttpServerProperties(
2558 &factory_, http_server_properties->GetWeakPtr());
2560 const AlternativeService alternative_service1(QUIC, host_port_pair_.host(),
2561 host_port_pair_.port());
2562 AlternativeServiceInfoVector alternative_service_info_vector;
2563 base::Time expiration = base::Time::Now() + base::TimeDelta::FromDays(1);
2564 alternative_service_info_vector.push_back(
2565 AlternativeServiceInfo(alternative_service1, 1.0, expiration));
2567 http_server_properties->SetAlternativeServices(
2568 host_port_pair_, alternative_service_info_vector);
2570 ServerNetworkStats stats1;
2571 stats1.srtt = base::TimeDelta::FromMicroseconds(10);
2572 http_server_properties->SetServerNetworkStats(host_port_pair_, stats1);
2574 crypto_client_stream_factory_.set_handshake_mode(
2575 MockCryptoClientStream::ZERO_RTT);
2576 host_resolver_.set_synchronous_mode(true);
2577 host_resolver_.rules()->AddIPLiteralRule(host_port_pair_.host(),
2578 "192.168.0.1", "");
2580 QuicStreamRequest request(&factory_);
2581 EXPECT_EQ(ERR_IO_PENDING,
2582 request.Request(host_port_pair_, is_https_, privacy_mode_,
2583 /*cert_verify_flags=*/0, host_port_pair_.host(),
2584 "POST", net_log_, callback_.callback()));
2586 // If we don't delay TCP connection, then time delay should be 0.
2587 EXPECT_FALSE(factory_.delay_tcp_race());
2588 EXPECT_EQ(base::TimeDelta(), request.GetTimeDelayForWaitingJob());
2590 // Enable |delay_tcp_race_| param and verify delay is one RTT and that
2591 // server supports QUIC.
2592 QuicStreamFactoryPeer::SetDelayTcpRace(&factory_, true);
2593 EXPECT_TRUE(factory_.delay_tcp_race());
2594 EXPECT_EQ(base::TimeDelta::FromMicroseconds(15),
2595 request.GetTimeDelayForWaitingJob());
2597 // Confirm the handshake and verify that the stream is created.
2598 crypto_client_stream_factory_.last_stream()->SendOnCryptoHandshakeEvent(
2599 QuicSession::HANDSHAKE_CONFIRMED);
2601 EXPECT_EQ(OK, callback_.WaitForResult());
2603 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
2604 EXPECT_TRUE(stream.get());
2605 EXPECT_TRUE(socket_data.AllReadDataConsumed());
2606 EXPECT_TRUE(socket_data.AllWriteDataConsumed());
2607 QuicStreamFactoryPeer::SetDelayTcpRace(&factory_, delay_tcp_race);
2610 TEST_P(QuicStreamFactoryTest, QuicSupportedServersAtStartup) {
2611 factory_.set_quic_server_info_factory(&quic_server_info_factory_);
2612 QuicStreamFactoryPeer::SetTaskRunner(&factory_, runner_.get());
2614 // Set up data in HttpServerProperties.
2615 scoped_ptr<HttpServerProperties> http_server_properties(
2616 new HttpServerPropertiesImpl());
2617 QuicStreamFactoryPeer::SetHttpServerProperties(
2618 &factory_, http_server_properties->GetWeakPtr());
2620 const AlternativeService alternative_service1(QUIC, host_port_pair_.host(),
2621 host_port_pair_.port());
2622 AlternativeServiceInfoVector alternative_service_info_vector;
2623 base::Time expiration = base::Time::Now() + base::TimeDelta::FromDays(1);
2624 alternative_service_info_vector.push_back(
2625 AlternativeServiceInfo(alternative_service1, 1.0, expiration));
2627 http_server_properties->SetAlternativeServices(
2628 host_port_pair_, alternative_service_info_vector);
2630 QuicStreamFactoryPeer::InitializeQuicSupportedServersAtStartup(&factory_);
2631 EXPECT_TRUE(
2632 QuicStreamFactoryPeer::GetQuicSupportedServersAtStartupInitialzied(
2633 &factory_));
2634 EXPECT_TRUE(
2635 QuicStreamFactoryPeer::SupportsQuicAtStartUp(&factory_, host_port_pair_));
2638 } // namespace test
2639 } // namespace net