Re-land: C++ readability review
[chromium-blink-merge.git] / net / quic / quic_stream_factory_test.cc
blobdd7865bd5138b34a1969b966186b5c98da47f912
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "net/quic/quic_stream_factory.h"
7 #include "base/run_loop.h"
8 #include "base/strings/string_util.h"
9 #include "net/base/test_data_directory.h"
10 #include "net/cert/cert_verifier.h"
11 #include "net/dns/mock_host_resolver.h"
12 #include "net/http/http_response_headers.h"
13 #include "net/http/http_response_info.h"
14 #include "net/http/http_util.h"
15 #include "net/http/transport_security_state.h"
16 #include "net/quic/crypto/crypto_handshake.h"
17 #include "net/quic/crypto/proof_verifier_chromium.h"
18 #include "net/quic/crypto/quic_decrypter.h"
19 #include "net/quic/crypto/quic_encrypter.h"
20 #include "net/quic/crypto/quic_server_info.h"
21 #include "net/quic/quic_http_stream.h"
22 #include "net/quic/quic_server_id.h"
23 #include "net/quic/test_tools/mock_clock.h"
24 #include "net/quic/test_tools/mock_crypto_client_stream_factory.h"
25 #include "net/quic/test_tools/mock_random.h"
26 #include "net/quic/test_tools/quic_test_packet_maker.h"
27 #include "net/quic/test_tools/quic_test_utils.h"
28 #include "net/quic/test_tools/test_task_runner.h"
29 #include "net/socket/socket_test_util.h"
30 #include "net/spdy/spdy_test_utils.h"
31 #include "net/ssl/channel_id_service.h"
32 #include "net/ssl/default_channel_id_store.h"
33 #include "net/test/cert_test_util.h"
34 #include "testing/gtest/include/gtest/gtest.h"
36 using base::StringPiece;
37 using std::ostream;
38 using std::string;
39 using std::vector;
41 namespace net {
42 namespace test {
44 namespace {
45 const char kDefaultServerHostName[] = "www.google.com";
46 const int kDefaultServerPort = 443;
48 // Run all tests with all the combinations of versions and
49 // enable_connection_racing.
50 struct TestParams {
51 TestParams(const QuicVersion version, bool enable_connection_racing)
52 : version(version), enable_connection_racing(enable_connection_racing) {}
54 friend ostream& operator<<(ostream& os, const TestParams& p) {
55 os << "{ version: " << QuicVersionToString(p.version);
56 os << " enable_connection_racing: " << p.enable_connection_racing << " }";
57 return os;
60 QuicVersion version;
61 bool enable_connection_racing;
64 // Constructs various test permutations.
65 vector<TestParams> GetTestParams() {
66 vector<TestParams> params;
67 QuicVersionVector all_supported_versions = QuicSupportedVersions();
68 for (const QuicVersion version : all_supported_versions) {
69 params.push_back(TestParams(version, false));
70 params.push_back(TestParams(version, true));
72 return params;
75 } // namespace anonymous
77 class QuicStreamFactoryPeer {
78 public:
79 static QuicCryptoClientConfig* GetCryptoConfig(QuicStreamFactory* factory) {
80 return &factory->crypto_config_;
83 static bool HasActiveSession(QuicStreamFactory* factory,
84 const HostPortPair& host_port_pair,
85 bool is_https) {
86 QuicServerId server_id(host_port_pair, is_https, PRIVACY_MODE_DISABLED);
87 return factory->HasActiveSession(server_id);
90 static QuicClientSession* GetActiveSession(
91 QuicStreamFactory* factory,
92 const HostPortPair& host_port_pair,
93 bool is_https) {
94 QuicServerId server_id(host_port_pair, is_https, PRIVACY_MODE_DISABLED);
95 DCHECK(factory->HasActiveSession(server_id));
96 return factory->active_sessions_[server_id];
99 static scoped_ptr<QuicHttpStream> CreateIfSessionExists(
100 QuicStreamFactory* factory,
101 const HostPortPair& host_port_pair,
102 bool is_https,
103 const BoundNetLog& net_log) {
104 QuicServerId server_id(host_port_pair, is_https, PRIVACY_MODE_DISABLED);
105 return factory->CreateIfSessionExists(server_id, net_log);
108 static bool IsLiveSession(QuicStreamFactory* factory,
109 QuicClientSession* session) {
110 for (QuicStreamFactory::SessionIdMap::iterator it =
111 factory->all_sessions_.begin();
112 it != factory->all_sessions_.end(); ++it) {
113 if (it->first == session)
114 return true;
116 return false;
119 static void DisableConnectionPooling(QuicStreamFactory* factory) {
120 factory->disable_connection_pooling_ = true;
123 static void SetTaskRunner(QuicStreamFactory* factory,
124 base::TaskRunner* task_runner) {
125 factory->task_runner_ = task_runner;
128 static void SetEnableConnectionRacing(QuicStreamFactory* factory,
129 bool enable_connection_racing) {
130 factory->enable_connection_racing_ = enable_connection_racing;
133 static void SetDisableDiskCache(QuicStreamFactory* factory,
134 bool disable_disk_cache) {
135 factory->disable_disk_cache_ = disable_disk_cache;
138 static void SetMaxNumberOfLossyConnections(
139 QuicStreamFactory* factory,
140 int max_number_of_lossy_connections) {
141 factory->max_number_of_lossy_connections_ = max_number_of_lossy_connections;
144 static int GetNumberOfLossyConnections(QuicStreamFactory* factory,
145 uint16 port) {
146 return factory->number_of_lossy_connections_[port];
149 static bool IsQuicDisabled(QuicStreamFactory* factory, uint16 port) {
150 return factory->IsQuicDisabled(port);
153 static size_t GetNumberOfActiveJobs(QuicStreamFactory* factory,
154 const QuicServerId& server_id) {
155 return (factory->active_jobs_[server_id]).size();
159 class MockQuicServerInfo : public QuicServerInfo {
160 public:
161 MockQuicServerInfo(const QuicServerId& server_id)
162 : QuicServerInfo(server_id) {}
163 ~MockQuicServerInfo() override {}
165 void Start() override {}
167 int WaitForDataReady(const CompletionCallback& callback) override {
168 return ERR_IO_PENDING;
171 void ResetWaitForDataReadyCallback() override {}
173 void CancelWaitForDataReadyCallback() override {}
175 bool IsDataReady() override { return false; }
177 bool IsReadyToPersist() override { return false; }
179 void Persist() override {}
181 void OnExternalCacheHit() override {}
184 class MockQuicServerInfoFactory : public QuicServerInfoFactory {
185 public:
186 MockQuicServerInfoFactory() {}
187 ~MockQuicServerInfoFactory() override {}
189 QuicServerInfo* GetForServer(const QuicServerId& server_id) override {
190 return new MockQuicServerInfo(server_id);
194 class QuicStreamFactoryTest : public ::testing::TestWithParam<TestParams> {
195 protected:
196 QuicStreamFactoryTest()
197 : random_generator_(0),
198 clock_(new MockClock()),
199 runner_(new TestTaskRunner(clock_)),
200 maker_(GetParam().version, 0, clock_),
201 cert_verifier_(CertVerifier::CreateDefault()),
202 channel_id_service_(
203 new ChannelIDService(new DefaultChannelIDStore(nullptr),
204 base::MessageLoopProxy::current())),
205 factory_(&host_resolver_,
206 &socket_factory_,
207 base::WeakPtr<HttpServerProperties>(),
208 cert_verifier_.get(),
209 channel_id_service_.get(),
210 &transport_security_state_,
211 &crypto_client_stream_factory_,
212 &random_generator_,
213 clock_,
214 kDefaultMaxPacketSize,
215 std::string(),
216 SupportedVersions(GetParam().version),
217 /*enable_port_selection=*/true,
218 /*always_require_handshake_confirmation=*/false,
219 /*disable_connection_pooling=*/false,
220 /*load_server_info_timeout_srtt_multiplier=*/0.0f,
221 /*enable_connection_racing=*/false,
222 /*enable_non_blocking_io=*/true,
223 /*disable_disk_cache=*/false,
224 /*max_number_of_lossy_connections=*/0,
225 /*packet_loss_threshold=*/1.0f,
226 /*receive_buffer_size=*/0,
227 QuicTagVector()),
228 host_port_pair_(kDefaultServerHostName, kDefaultServerPort),
229 is_https_(false),
230 privacy_mode_(PRIVACY_MODE_DISABLED) {
231 factory_.set_require_confirmation(false);
232 clock_->AdvanceTime(QuicTime::Delta::FromSeconds(1));
233 QuicStreamFactoryPeer::SetEnableConnectionRacing(
234 &factory_, GetParam().enable_connection_racing);
237 scoped_ptr<QuicHttpStream> CreateIfSessionExists(
238 const HostPortPair& host_port_pair,
239 const BoundNetLog& net_log) {
240 return QuicStreamFactoryPeer::CreateIfSessionExists(
241 &factory_, host_port_pair, false, net_log_);
244 int GetSourcePortForNewSession(const HostPortPair& destination) {
245 return GetSourcePortForNewSessionInner(destination, false);
248 int GetSourcePortForNewSessionAndGoAway(
249 const HostPortPair& destination) {
250 return GetSourcePortForNewSessionInner(destination, true);
253 int GetSourcePortForNewSessionInner(const HostPortPair& destination,
254 bool goaway_received) {
255 // Should only be called if there is no active session for this destination.
256 EXPECT_EQ(nullptr, CreateIfSessionExists(destination, net_log_).get());
257 size_t socket_count = socket_factory_.udp_client_sockets().size();
259 MockRead reads[] = {
260 MockRead(ASYNC, OK, 0) // EOF
262 DeterministicSocketData socket_data(reads, arraysize(reads), nullptr, 0);
263 socket_data.StopAfter(1);
264 socket_factory_.AddSocketDataProvider(&socket_data);
266 QuicStreamRequest request(&factory_);
267 EXPECT_EQ(ERR_IO_PENDING,
268 request.Request(destination,
269 is_https_,
270 privacy_mode_,
271 "GET",
272 net_log_,
273 callback_.callback()));
275 EXPECT_EQ(OK, callback_.WaitForResult());
276 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
277 EXPECT_TRUE(stream.get());
278 stream.reset();
280 QuicClientSession* session = QuicStreamFactoryPeer::GetActiveSession(
281 &factory_, destination, is_https_);
283 if (socket_count + 1 != socket_factory_.udp_client_sockets().size()) {
284 EXPECT_TRUE(false);
285 return 0;
288 IPEndPoint endpoint;
289 socket_factory_.
290 udp_client_sockets()[socket_count]->GetLocalAddress(&endpoint);
291 int port = endpoint.port();
292 if (goaway_received) {
293 QuicGoAwayFrame goaway(QUIC_NO_ERROR, 1, "");
294 session->OnGoAway(goaway);
297 factory_.OnSessionClosed(session);
298 EXPECT_EQ(nullptr, CreateIfSessionExists(destination, net_log_).get());
299 EXPECT_TRUE(socket_data.at_read_eof());
300 EXPECT_TRUE(socket_data.at_write_eof());
301 return port;
304 scoped_ptr<QuicEncryptedPacket> ConstructRstPacket() {
305 QuicStreamId stream_id = kClientDataStreamId1;
306 return maker_.MakeRstPacket(
307 1, true, stream_id,
308 AdjustErrorForVersion(QUIC_RST_ACKNOWLEDGEMENT, GetParam().version));
311 MockQuicServerInfoFactory quic_server_info_factory_;
312 MockHostResolver host_resolver_;
313 DeterministicMockClientSocketFactory socket_factory_;
314 MockCryptoClientStreamFactory crypto_client_stream_factory_;
315 MockRandom random_generator_;
316 MockClock* clock_; // Owned by factory_.
317 scoped_refptr<TestTaskRunner> runner_;
318 QuicTestPacketMaker maker_;
319 scoped_ptr<CertVerifier> cert_verifier_;
320 scoped_ptr<ChannelIDService> channel_id_service_;
321 TransportSecurityState transport_security_state_;
322 QuicStreamFactory factory_;
323 HostPortPair host_port_pair_;
324 bool is_https_;
325 PrivacyMode privacy_mode_;
326 BoundNetLog net_log_;
327 TestCompletionCallback callback_;
330 INSTANTIATE_TEST_CASE_P(Version,
331 QuicStreamFactoryTest,
332 ::testing::ValuesIn(GetTestParams()));
334 TEST_P(QuicStreamFactoryTest, CreateIfSessionExists) {
335 EXPECT_EQ(nullptr, CreateIfSessionExists(host_port_pair_, net_log_).get());
338 TEST_P(QuicStreamFactoryTest, Create) {
339 MockRead reads[] = {
340 MockRead(ASYNC, OK, 0) // EOF
342 DeterministicSocketData socket_data(reads, arraysize(reads), nullptr, 0);
343 socket_factory_.AddSocketDataProvider(&socket_data);
344 socket_data.StopAfter(1);
346 QuicStreamRequest request(&factory_);
347 EXPECT_EQ(ERR_IO_PENDING,
348 request.Request(host_port_pair_,
349 is_https_,
350 privacy_mode_,
351 "GET",
352 net_log_,
353 callback_.callback()));
355 EXPECT_EQ(OK, callback_.WaitForResult());
356 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
357 EXPECT_TRUE(stream.get());
359 // Will reset stream 3.
360 stream = CreateIfSessionExists(host_port_pair_, net_log_);
361 EXPECT_TRUE(stream.get());
363 // TODO(rtenneti): We should probably have a tests that HTTP and HTTPS result
364 // in streams on different sessions.
365 QuicStreamRequest request2(&factory_);
366 EXPECT_EQ(OK,
367 request2.Request(host_port_pair_,
368 is_https_,
369 privacy_mode_,
370 "GET",
371 net_log_,
372 callback_.callback()));
373 stream = request2.ReleaseStream(); // Will reset stream 5.
374 stream.reset(); // Will reset stream 7.
376 EXPECT_TRUE(socket_data.at_read_eof());
377 EXPECT_TRUE(socket_data.at_write_eof());
380 TEST_P(QuicStreamFactoryTest, CreateZeroRtt) {
381 MockRead reads[] = {
382 MockRead(ASYNC, OK, 0) // EOF
384 DeterministicSocketData socket_data(reads, arraysize(reads), nullptr, 0);
385 socket_factory_.AddSocketDataProvider(&socket_data);
386 socket_data.StopAfter(1);
388 crypto_client_stream_factory_.set_handshake_mode(
389 MockCryptoClientStream::ZERO_RTT);
390 host_resolver_.set_synchronous_mode(true);
391 host_resolver_.rules()->AddIPLiteralRule(host_port_pair_.host(),
392 "192.168.0.1", "");
394 QuicStreamRequest request(&factory_);
395 EXPECT_EQ(OK,
396 request.Request(host_port_pair_,
397 is_https_,
398 privacy_mode_,
399 "GET",
400 net_log_,
401 callback_.callback()));
403 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
404 EXPECT_TRUE(stream.get());
405 EXPECT_TRUE(socket_data.at_read_eof());
406 EXPECT_TRUE(socket_data.at_write_eof());
409 TEST_P(QuicStreamFactoryTest, CreateZeroRttPost) {
410 MockRead reads[] = {
411 MockRead(ASYNC, OK, 0) // EOF
413 DeterministicSocketData socket_data(reads, arraysize(reads), nullptr, 0);
414 socket_factory_.AddSocketDataProvider(&socket_data);
415 socket_data.StopAfter(1);
417 crypto_client_stream_factory_.set_handshake_mode(
418 MockCryptoClientStream::ZERO_RTT);
419 host_resolver_.set_synchronous_mode(true);
420 host_resolver_.rules()->AddIPLiteralRule(host_port_pair_.host(),
421 "192.168.0.1", "");
423 QuicStreamRequest request(&factory_);
424 // Posts require handshake confirmation, so this will return asynchronously.
425 EXPECT_EQ(ERR_IO_PENDING,
426 request.Request(host_port_pair_,
427 is_https_,
428 privacy_mode_,
429 "POST",
430 net_log_,
431 callback_.callback()));
433 // Confirm the handshake and verify that the stream is created.
434 crypto_client_stream_factory_.last_stream()->SendOnCryptoHandshakeEvent(
435 QuicSession::HANDSHAKE_CONFIRMED);
437 EXPECT_EQ(OK, callback_.WaitForResult());
438 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
439 EXPECT_TRUE(stream.get());
440 EXPECT_TRUE(socket_data.at_read_eof());
441 EXPECT_TRUE(socket_data.at_write_eof());
444 TEST_P(QuicStreamFactoryTest, CreateHttpVsHttps) {
445 MockRead reads[] = {
446 MockRead(ASYNC, OK, 0) // EOF
448 DeterministicSocketData socket_data1(reads, arraysize(reads), nullptr, 0);
449 DeterministicSocketData socket_data2(reads, arraysize(reads), nullptr, 0);
450 socket_factory_.AddSocketDataProvider(&socket_data1);
451 socket_factory_.AddSocketDataProvider(&socket_data2);
452 socket_data1.StopAfter(1);
453 socket_data2.StopAfter(1);
455 QuicStreamRequest request(&factory_);
456 EXPECT_EQ(ERR_IO_PENDING,
457 request.Request(host_port_pair_,
458 is_https_,
459 privacy_mode_,
460 "GET",
461 net_log_,
462 callback_.callback()));
464 EXPECT_EQ(OK, callback_.WaitForResult());
465 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
466 EXPECT_TRUE(stream.get());
468 QuicStreamRequest request2(&factory_);
469 EXPECT_EQ(ERR_IO_PENDING,
470 request2.Request(host_port_pair_,
471 !is_https_,
472 privacy_mode_,
473 "GET",
474 net_log_,
475 callback_.callback()));
476 EXPECT_EQ(OK, callback_.WaitForResult());
477 stream = request2.ReleaseStream();
478 EXPECT_TRUE(stream.get());
479 stream.reset();
481 EXPECT_NE(QuicStreamFactoryPeer::GetActiveSession(
482 &factory_, host_port_pair_, is_https_),
483 QuicStreamFactoryPeer::GetActiveSession(
484 &factory_, host_port_pair_, !is_https_));
486 EXPECT_TRUE(socket_data1.at_read_eof());
487 EXPECT_TRUE(socket_data1.at_write_eof());
488 EXPECT_TRUE(socket_data2.at_read_eof());
489 EXPECT_TRUE(socket_data2.at_write_eof());
492 TEST_P(QuicStreamFactoryTest, Pooling) {
493 MockRead reads[] = {
494 MockRead(ASYNC, OK, 0) // EOF
496 DeterministicSocketData socket_data(reads, arraysize(reads), nullptr, 0);
497 socket_factory_.AddSocketDataProvider(&socket_data);
498 socket_data.StopAfter(1);
500 HostPortPair server2("mail.google.com", kDefaultServerPort);
501 host_resolver_.set_synchronous_mode(true);
502 host_resolver_.rules()->AddIPLiteralRule(
503 kDefaultServerHostName, "192.168.0.1", "");
504 host_resolver_.rules()->AddIPLiteralRule(
505 "mail.google.com", "192.168.0.1", "");
507 QuicStreamRequest request(&factory_);
508 EXPECT_EQ(OK,
509 request.Request(host_port_pair_,
510 is_https_,
511 privacy_mode_,
512 "GET",
513 net_log_,
514 callback_.callback()));
515 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
516 EXPECT_TRUE(stream.get());
518 TestCompletionCallback callback;
519 QuicStreamRequest request2(&factory_);
520 EXPECT_EQ(OK,
521 request2.Request(server2,
522 is_https_,
523 privacy_mode_,
524 "GET",
525 net_log_,
526 callback.callback()));
527 scoped_ptr<QuicHttpStream> stream2 = request2.ReleaseStream();
528 EXPECT_TRUE(stream2.get());
530 EXPECT_EQ(
531 QuicStreamFactoryPeer::GetActiveSession(
532 &factory_, host_port_pair_, is_https_),
533 QuicStreamFactoryPeer::GetActiveSession(&factory_, server2, is_https_));
535 EXPECT_TRUE(socket_data.at_read_eof());
536 EXPECT_TRUE(socket_data.at_write_eof());
539 TEST_P(QuicStreamFactoryTest, NoPoolingIfDisabled) {
540 MockRead reads[] = {
541 MockRead(ASYNC, OK, 0) // EOF
543 DeterministicSocketData socket_data1(reads, arraysize(reads), nullptr, 0);
544 DeterministicSocketData socket_data2(reads, arraysize(reads), nullptr, 0);
545 socket_factory_.AddSocketDataProvider(&socket_data1);
546 socket_factory_.AddSocketDataProvider(&socket_data2);
547 socket_data1.StopAfter(1);
548 socket_data2.StopAfter(1);
550 HostPortPair server2("mail.google.com", kDefaultServerPort);
551 host_resolver_.set_synchronous_mode(true);
552 host_resolver_.rules()->AddIPLiteralRule(
553 kDefaultServerHostName, "192.168.0.1", "");
554 host_resolver_.rules()->AddIPLiteralRule(
555 "mail.google.com", "192.168.0.1", "");
557 // Disable connection pooling.
558 QuicStreamFactoryPeer::DisableConnectionPooling(&factory_);
560 QuicStreamRequest request(&factory_);
561 EXPECT_EQ(OK,
562 request.Request(host_port_pair_,
563 is_https_,
564 privacy_mode_,
565 "GET",
566 net_log_,
567 callback_.callback()));
568 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
569 EXPECT_TRUE(stream.get());
571 TestCompletionCallback callback;
572 QuicStreamRequest request2(&factory_);
573 EXPECT_EQ(OK,
574 request2.Request(server2,
575 is_https_,
576 privacy_mode_,
577 "GET",
578 net_log_,
579 callback.callback()));
580 scoped_ptr<QuicHttpStream> stream2 = request2.ReleaseStream();
581 EXPECT_TRUE(stream2.get());
583 EXPECT_NE(
584 QuicStreamFactoryPeer::GetActiveSession(
585 &factory_, host_port_pair_, is_https_),
586 QuicStreamFactoryPeer::GetActiveSession(&factory_, server2, is_https_));
588 EXPECT_TRUE(socket_data1.at_read_eof());
589 EXPECT_TRUE(socket_data1.at_write_eof());
590 EXPECT_TRUE(socket_data2.at_read_eof());
591 EXPECT_TRUE(socket_data2.at_write_eof());
594 TEST_P(QuicStreamFactoryTest, NoPoolingAfterGoAway) {
595 MockRead reads[] = {
596 MockRead(ASYNC, OK, 0) // EOF
598 DeterministicSocketData socket_data1(reads, arraysize(reads), nullptr, 0);
599 DeterministicSocketData socket_data2(reads, arraysize(reads), nullptr, 0);
600 socket_factory_.AddSocketDataProvider(&socket_data1);
601 socket_factory_.AddSocketDataProvider(&socket_data2);
602 socket_data1.StopAfter(1);
603 socket_data2.StopAfter(1);
605 HostPortPair server2("mail.google.com", kDefaultServerPort);
606 host_resolver_.set_synchronous_mode(true);
607 host_resolver_.rules()->AddIPLiteralRule(
608 kDefaultServerHostName, "192.168.0.1", "");
609 host_resolver_.rules()->AddIPLiteralRule(
610 "mail.google.com", "192.168.0.1", "");
612 QuicStreamRequest request(&factory_);
613 EXPECT_EQ(OK,
614 request.Request(host_port_pair_,
615 is_https_,
616 privacy_mode_,
617 "GET",
618 net_log_,
619 callback_.callback()));
620 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
621 EXPECT_TRUE(stream.get());
623 TestCompletionCallback callback;
624 QuicStreamRequest request2(&factory_);
625 EXPECT_EQ(OK,
626 request2.Request(server2,
627 is_https_,
628 privacy_mode_,
629 "GET",
630 net_log_,
631 callback.callback()));
632 scoped_ptr<QuicHttpStream> stream2 = request2.ReleaseStream();
633 EXPECT_TRUE(stream2.get());
635 factory_.OnSessionGoingAway(QuicStreamFactoryPeer::GetActiveSession(
636 &factory_, host_port_pair_, is_https_));
637 EXPECT_FALSE(QuicStreamFactoryPeer::HasActiveSession(
638 &factory_, host_port_pair_, is_https_));
639 EXPECT_FALSE(QuicStreamFactoryPeer::HasActiveSession(
640 &factory_, server2, is_https_));
642 TestCompletionCallback callback3;
643 QuicStreamRequest request3(&factory_);
644 EXPECT_EQ(OK,
645 request3.Request(server2,
646 is_https_,
647 privacy_mode_,
648 "GET",
649 net_log_,
650 callback3.callback()));
651 scoped_ptr<QuicHttpStream> stream3 = request3.ReleaseStream();
652 EXPECT_TRUE(stream3.get());
654 EXPECT_TRUE(QuicStreamFactoryPeer::HasActiveSession(
655 &factory_, server2, is_https_));
657 EXPECT_TRUE(socket_data1.at_read_eof());
658 EXPECT_TRUE(socket_data1.at_write_eof());
659 EXPECT_TRUE(socket_data2.at_read_eof());
660 EXPECT_TRUE(socket_data2.at_write_eof());
663 TEST_P(QuicStreamFactoryTest, HttpsPooling) {
664 MockRead reads[] = {
665 MockRead(ASYNC, OK, 0) // EOF
667 DeterministicSocketData socket_data(reads, arraysize(reads), nullptr, 0);
668 socket_factory_.AddSocketDataProvider(&socket_data);
669 socket_data.StopAfter(1);
671 HostPortPair server1("www.example.org", 443);
672 HostPortPair server2("mail.example.org", 443);
674 // Load a cert that is valid for:
675 // www.example.org (server1)
676 // mail.example.org (server2)
677 // www.example.com
678 base::FilePath certs_dir = GetTestCertsDirectory();
679 scoped_refptr<X509Certificate> test_cert(
680 ImportCertFromFile(certs_dir, "spdy_pooling.pem"));
681 ASSERT_NE(static_cast<X509Certificate*>(nullptr), test_cert.get());
682 ProofVerifyDetailsChromium verify_details;
683 verify_details.cert_verify_result.verified_cert = test_cert;
684 verify_details.cert_verify_result.is_issued_by_known_root = true;
685 crypto_client_stream_factory_.set_proof_verify_details(&verify_details);
687 host_resolver_.set_synchronous_mode(true);
688 host_resolver_.rules()->AddIPLiteralRule(server1.host(), "192.168.0.1", "");
689 host_resolver_.rules()->AddIPLiteralRule(server2.host(), "192.168.0.1", "");
691 QuicStreamRequest request(&factory_);
692 is_https_ = true;
693 EXPECT_EQ(OK,
694 request.Request(server1,
695 is_https_,
696 privacy_mode_,
697 "GET",
698 net_log_,
699 callback_.callback()));
700 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
701 EXPECT_TRUE(stream.get());
703 TestCompletionCallback callback;
704 QuicStreamRequest request2(&factory_);
705 EXPECT_EQ(OK,
706 request2.Request(server2,
707 is_https_,
708 privacy_mode_,
709 "GET",
710 net_log_,
711 callback_.callback()));
712 scoped_ptr<QuicHttpStream> stream2 = request2.ReleaseStream();
713 EXPECT_TRUE(stream2.get());
715 EXPECT_EQ(QuicStreamFactoryPeer::GetActiveSession(
716 &factory_, server1, is_https_),
717 QuicStreamFactoryPeer::GetActiveSession(
718 &factory_, server2, is_https_));
720 EXPECT_TRUE(socket_data.at_read_eof());
721 EXPECT_TRUE(socket_data.at_write_eof());
724 TEST_P(QuicStreamFactoryTest, NoHttpsPoolingIfDisabled) {
725 MockRead reads[] = {
726 MockRead(ASYNC, OK, 0) // EOF
728 DeterministicSocketData socket_data1(reads, arraysize(reads), nullptr, 0);
729 DeterministicSocketData socket_data2(reads, arraysize(reads), nullptr, 0);
730 socket_factory_.AddSocketDataProvider(&socket_data1);
731 socket_factory_.AddSocketDataProvider(&socket_data2);
732 socket_data1.StopAfter(1);
733 socket_data2.StopAfter(1);
735 HostPortPair server1("www.example.org", 443);
736 HostPortPair server2("mail.example.org", 443);
738 // Load a cert that is valid for:
739 // www.example.org (server1)
740 // mail.example.org (server2)
741 // www.example.com
742 base::FilePath certs_dir = GetTestCertsDirectory();
743 scoped_refptr<X509Certificate> test_cert(
744 ImportCertFromFile(certs_dir, "spdy_pooling.pem"));
745 ASSERT_NE(static_cast<X509Certificate*>(nullptr), test_cert.get());
746 ProofVerifyDetailsChromium verify_details;
747 verify_details.cert_verify_result.verified_cert = test_cert;
748 verify_details.cert_verify_result.is_issued_by_known_root = true;
749 crypto_client_stream_factory_.set_proof_verify_details(&verify_details);
751 host_resolver_.set_synchronous_mode(true);
752 host_resolver_.rules()->AddIPLiteralRule(server1.host(), "192.168.0.1", "");
753 host_resolver_.rules()->AddIPLiteralRule(server2.host(), "192.168.0.1", "");
755 // Disable connection pooling.
756 QuicStreamFactoryPeer::DisableConnectionPooling(&factory_);
758 QuicStreamRequest request(&factory_);
759 is_https_ = true;
760 EXPECT_EQ(OK,
761 request.Request(server1,
762 is_https_,
763 privacy_mode_,
764 "GET",
765 net_log_,
766 callback_.callback()));
767 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
768 EXPECT_TRUE(stream.get());
770 TestCompletionCallback callback;
771 QuicStreamRequest request2(&factory_);
772 EXPECT_EQ(OK,
773 request2.Request(server2,
774 is_https_,
775 privacy_mode_,
776 "GET",
777 net_log_,
778 callback_.callback()));
779 scoped_ptr<QuicHttpStream> stream2 = request2.ReleaseStream();
780 EXPECT_TRUE(stream2.get());
782 EXPECT_NE(QuicStreamFactoryPeer::GetActiveSession(
783 &factory_, server1, is_https_),
784 QuicStreamFactoryPeer::GetActiveSession(
785 &factory_, server2, is_https_));
787 EXPECT_TRUE(socket_data1.at_read_eof());
788 EXPECT_TRUE(socket_data1.at_write_eof());
789 EXPECT_TRUE(socket_data2.at_read_eof());
790 EXPECT_TRUE(socket_data2.at_write_eof());
793 TEST_P(QuicStreamFactoryTest, NoHttpsPoolingWithCertMismatch) {
794 MockRead reads[] = {
795 MockRead(ASYNC, OK, 0) // EOF
797 DeterministicSocketData socket_data1(reads, arraysize(reads), nullptr, 0);
798 DeterministicSocketData socket_data2(reads, arraysize(reads), nullptr, 0);
799 socket_factory_.AddSocketDataProvider(&socket_data1);
800 socket_factory_.AddSocketDataProvider(&socket_data2);
801 socket_data1.StopAfter(1);
802 socket_data2.StopAfter(1);
804 HostPortPair server1("www.example.org", 443);
805 HostPortPair server2("mail.google.com", 443);
807 // Load a cert that is valid for:
808 // www.example.org (server1)
809 // mail.example.org
810 // www.example.com
811 // But is not valid for mail.google.com (server2).
812 base::FilePath certs_dir = GetTestCertsDirectory();
813 scoped_refptr<X509Certificate> test_cert(
814 ImportCertFromFile(certs_dir, "spdy_pooling.pem"));
815 ASSERT_NE(static_cast<X509Certificate*>(nullptr), test_cert.get());
816 ProofVerifyDetailsChromium verify_details;
817 verify_details.cert_verify_result.verified_cert = test_cert;
818 crypto_client_stream_factory_.set_proof_verify_details(&verify_details);
821 host_resolver_.set_synchronous_mode(true);
822 host_resolver_.rules()->AddIPLiteralRule(server1.host(), "192.168.0.1", "");
823 host_resolver_.rules()->AddIPLiteralRule(server2.host(), "192.168.0.1", "");
825 QuicStreamRequest request(&factory_);
826 is_https_ = true;
827 EXPECT_EQ(OK,
828 request.Request(server1,
829 is_https_,
830 privacy_mode_,
831 "GET",
832 net_log_,
833 callback_.callback()));
834 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
835 EXPECT_TRUE(stream.get());
837 TestCompletionCallback callback;
838 QuicStreamRequest request2(&factory_);
839 EXPECT_EQ(OK,
840 request2.Request(server2,
841 is_https_,
842 privacy_mode_,
843 "GET",
844 net_log_,
845 callback_.callback()));
846 scoped_ptr<QuicHttpStream> stream2 = request2.ReleaseStream();
847 EXPECT_TRUE(stream2.get());
849 EXPECT_NE(QuicStreamFactoryPeer::GetActiveSession(
850 &factory_, server1, is_https_),
851 QuicStreamFactoryPeer::GetActiveSession(
852 &factory_, server2, is_https_));
854 EXPECT_TRUE(socket_data1.at_read_eof());
855 EXPECT_TRUE(socket_data1.at_write_eof());
856 EXPECT_TRUE(socket_data2.at_read_eof());
857 EXPECT_TRUE(socket_data2.at_write_eof());
860 TEST_P(QuicStreamFactoryTest, HttpsPoolingWithMatchingPins) {
861 MockRead reads[] = {
862 MockRead(ASYNC, OK, 0) // EOF
864 DeterministicSocketData socket_data(reads, arraysize(reads), nullptr, 0);
865 socket_factory_.AddSocketDataProvider(&socket_data);
866 socket_data.StopAfter(1);
868 HostPortPair server1("www.example.org", 443);
869 HostPortPair server2("mail.example.org", 443);
870 uint8 primary_pin = 1;
871 uint8 backup_pin = 2;
872 test::AddPin(&transport_security_state_, "mail.example.org", primary_pin,
873 backup_pin);
875 // Load a cert that is valid for:
876 // www.example.org (server1)
877 // mail.example.org (server2)
878 base::FilePath certs_dir = GetTestCertsDirectory();
879 scoped_refptr<X509Certificate> test_cert(
880 ImportCertFromFile(certs_dir, "spdy_pooling.pem"));
881 ASSERT_NE(static_cast<X509Certificate*>(nullptr), test_cert.get());
882 ProofVerifyDetailsChromium verify_details;
883 verify_details.cert_verify_result.verified_cert = test_cert;
884 verify_details.cert_verify_result.is_issued_by_known_root = true;
885 verify_details.cert_verify_result.public_key_hashes.push_back(
886 test::GetTestHashValue(primary_pin));
887 crypto_client_stream_factory_.set_proof_verify_details(&verify_details);
890 host_resolver_.set_synchronous_mode(true);
891 host_resolver_.rules()->AddIPLiteralRule(server1.host(), "192.168.0.1", "");
892 host_resolver_.rules()->AddIPLiteralRule(server2.host(), "192.168.0.1", "");
894 QuicStreamRequest request(&factory_);
895 is_https_ = true;
896 EXPECT_EQ(OK,
897 request.Request(server1,
898 is_https_,
899 privacy_mode_,
900 "GET",
901 net_log_,
902 callback_.callback()));
903 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
904 EXPECT_TRUE(stream.get());
906 TestCompletionCallback callback;
907 QuicStreamRequest request2(&factory_);
908 EXPECT_EQ(OK,
909 request2.Request(server2,
910 is_https_,
911 privacy_mode_,
912 "GET",
913 net_log_,
914 callback_.callback()));
915 scoped_ptr<QuicHttpStream> stream2 = request2.ReleaseStream();
916 EXPECT_TRUE(stream2.get());
918 EXPECT_EQ(QuicStreamFactoryPeer::GetActiveSession(
919 &factory_, server1, is_https_),
920 QuicStreamFactoryPeer::GetActiveSession(
921 &factory_, server2, is_https_));
923 EXPECT_TRUE(socket_data.at_read_eof());
924 EXPECT_TRUE(socket_data.at_write_eof());
927 TEST_P(QuicStreamFactoryTest, NoHttpsPoolingWithMatchingPinsIfDisabled) {
928 MockRead reads[] = {
929 MockRead(ASYNC, OK, 0) // EOF
931 DeterministicSocketData socket_data1(reads, arraysize(reads), nullptr, 0);
932 DeterministicSocketData socket_data2(reads, arraysize(reads), nullptr, 0);
933 socket_factory_.AddSocketDataProvider(&socket_data1);
934 socket_factory_.AddSocketDataProvider(&socket_data2);
935 socket_data1.StopAfter(1);
936 socket_data2.StopAfter(1);
938 HostPortPair server1("www.example.org", 443);
939 HostPortPair server2("mail.example.org", 443);
940 uint8 primary_pin = 1;
941 uint8 backup_pin = 2;
942 test::AddPin(&transport_security_state_, "mail.example.org", primary_pin,
943 backup_pin);
945 // Load a cert that is valid for:
946 // www.example.org (server1)
947 // mail.example.org (server2)
948 base::FilePath certs_dir = GetTestCertsDirectory();
949 scoped_refptr<X509Certificate> test_cert(
950 ImportCertFromFile(certs_dir, "spdy_pooling.pem"));
951 ASSERT_NE(static_cast<X509Certificate*>(nullptr), test_cert.get());
952 ProofVerifyDetailsChromium verify_details;
953 verify_details.cert_verify_result.verified_cert = test_cert;
954 verify_details.cert_verify_result.is_issued_by_known_root = true;
955 verify_details.cert_verify_result.public_key_hashes.push_back(
956 test::GetTestHashValue(primary_pin));
957 crypto_client_stream_factory_.set_proof_verify_details(&verify_details);
960 host_resolver_.set_synchronous_mode(true);
961 host_resolver_.rules()->AddIPLiteralRule(server1.host(), "192.168.0.1", "");
962 host_resolver_.rules()->AddIPLiteralRule(server2.host(), "192.168.0.1", "");
964 // Disable connection pooling.
965 QuicStreamFactoryPeer::DisableConnectionPooling(&factory_);
967 QuicStreamRequest request(&factory_);
968 is_https_ = true;
969 EXPECT_EQ(OK,
970 request.Request(server1,
971 is_https_,
972 privacy_mode_,
973 "GET",
974 net_log_,
975 callback_.callback()));
976 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
977 EXPECT_TRUE(stream.get());
979 TestCompletionCallback callback;
980 QuicStreamRequest request2(&factory_);
981 EXPECT_EQ(OK,
982 request2.Request(server2,
983 is_https_,
984 privacy_mode_,
985 "GET",
986 net_log_,
987 callback_.callback()));
988 scoped_ptr<QuicHttpStream> stream2 = request2.ReleaseStream();
989 EXPECT_TRUE(stream2.get());
991 EXPECT_NE(QuicStreamFactoryPeer::GetActiveSession(
992 &factory_, server1, is_https_),
993 QuicStreamFactoryPeer::GetActiveSession(
994 &factory_, server2, is_https_));
996 EXPECT_TRUE(socket_data1.at_read_eof());
997 EXPECT_TRUE(socket_data1.at_write_eof());
998 EXPECT_TRUE(socket_data2.at_read_eof());
999 EXPECT_TRUE(socket_data2.at_write_eof());
1002 TEST_P(QuicStreamFactoryTest, NoHttpsPoolingWithDifferentPins) {
1003 MockRead reads[] = {
1004 MockRead(ASYNC, OK, 0) // EOF
1006 DeterministicSocketData socket_data1(reads, arraysize(reads), nullptr, 0);
1007 DeterministicSocketData socket_data2(reads, arraysize(reads), nullptr, 0);
1008 socket_factory_.AddSocketDataProvider(&socket_data1);
1009 socket_factory_.AddSocketDataProvider(&socket_data2);
1010 socket_data1.StopAfter(1);
1011 socket_data2.StopAfter(1);
1013 HostPortPair server1("www.example.org", 443);
1014 HostPortPair server2("mail.example.org", 443);
1015 uint8 primary_pin = 1;
1016 uint8 backup_pin = 2;
1017 uint8 bad_pin = 3;
1018 test::AddPin(&transport_security_state_, "mail.example.org", primary_pin,
1019 backup_pin);
1021 // Load a cert that is valid for:
1022 // www.example.org (server1)
1023 // mail.example.org (server2)
1024 base::FilePath certs_dir = GetTestCertsDirectory();
1025 scoped_refptr<X509Certificate> test_cert(
1026 ImportCertFromFile(certs_dir, "spdy_pooling.pem"));
1027 ASSERT_NE(static_cast<X509Certificate*>(nullptr), test_cert.get());
1028 ProofVerifyDetailsChromium verify_details;
1029 verify_details.cert_verify_result.verified_cert = test_cert;
1030 verify_details.cert_verify_result.is_issued_by_known_root = true;
1031 verify_details.cert_verify_result.public_key_hashes.push_back(
1032 test::GetTestHashValue(bad_pin));
1033 crypto_client_stream_factory_.set_proof_verify_details(&verify_details);
1036 host_resolver_.set_synchronous_mode(true);
1037 host_resolver_.rules()->AddIPLiteralRule(server1.host(), "192.168.0.1", "");
1038 host_resolver_.rules()->AddIPLiteralRule(server2.host(), "192.168.0.1", "");
1040 QuicStreamRequest request(&factory_);
1041 is_https_ = true;
1042 EXPECT_EQ(OK,
1043 request.Request(server1,
1044 is_https_,
1045 privacy_mode_,
1046 "GET",
1047 net_log_,
1048 callback_.callback()));
1049 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
1050 EXPECT_TRUE(stream.get());
1052 TestCompletionCallback callback;
1053 QuicStreamRequest request2(&factory_);
1054 EXPECT_EQ(OK,
1055 request2.Request(server2,
1056 is_https_,
1057 privacy_mode_,
1058 "GET",
1059 net_log_,
1060 callback_.callback()));
1061 scoped_ptr<QuicHttpStream> stream2 = request2.ReleaseStream();
1062 EXPECT_TRUE(stream2.get());
1064 EXPECT_NE(QuicStreamFactoryPeer::GetActiveSession(
1065 &factory_, server1, is_https_),
1066 QuicStreamFactoryPeer::GetActiveSession(
1067 &factory_, server2, is_https_));
1069 EXPECT_TRUE(socket_data1.at_read_eof());
1070 EXPECT_TRUE(socket_data1.at_write_eof());
1071 EXPECT_TRUE(socket_data2.at_read_eof());
1072 EXPECT_TRUE(socket_data2.at_write_eof());
1075 TEST_P(QuicStreamFactoryTest, Goaway) {
1076 MockRead reads[] = {
1077 MockRead(ASYNC, OK, 0) // EOF
1079 DeterministicSocketData socket_data(reads, arraysize(reads), nullptr, 0);
1080 socket_data.StopAfter(1);
1081 socket_factory_.AddSocketDataProvider(&socket_data);
1082 DeterministicSocketData socket_data2(reads, arraysize(reads), nullptr, 0);
1083 socket_data2.StopAfter(1);
1084 socket_factory_.AddSocketDataProvider(&socket_data2);
1086 QuicStreamRequest request(&factory_);
1087 EXPECT_EQ(ERR_IO_PENDING,
1088 request.Request(host_port_pair_,
1089 is_https_,
1090 privacy_mode_,
1091 "GET",
1092 net_log_,
1093 callback_.callback()));
1095 EXPECT_EQ(OK, callback_.WaitForResult());
1096 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
1097 EXPECT_TRUE(stream.get());
1099 // Mark the session as going away. Ensure that while it is still alive
1100 // that it is no longer active.
1101 QuicClientSession* session = QuicStreamFactoryPeer::GetActiveSession(
1102 &factory_, host_port_pair_, is_https_);
1103 factory_.OnSessionGoingAway(session);
1104 EXPECT_EQ(true, QuicStreamFactoryPeer::IsLiveSession(&factory_, session));
1105 EXPECT_FALSE(QuicStreamFactoryPeer::HasActiveSession(
1106 &factory_, host_port_pair_, is_https_));
1107 EXPECT_EQ(nullptr, CreateIfSessionExists(host_port_pair_, net_log_).get());
1109 // Create a new request for the same destination and verify that a
1110 // new session is created.
1111 QuicStreamRequest request2(&factory_);
1112 EXPECT_EQ(ERR_IO_PENDING,
1113 request2.Request(host_port_pair_,
1114 is_https_,
1115 privacy_mode_,
1116 "GET",
1117 net_log_,
1118 callback_.callback()));
1119 EXPECT_EQ(OK, callback_.WaitForResult());
1120 scoped_ptr<QuicHttpStream> stream2 = request2.ReleaseStream();
1121 EXPECT_TRUE(stream2.get());
1123 EXPECT_TRUE(QuicStreamFactoryPeer::HasActiveSession(&factory_,
1124 host_port_pair_,
1125 is_https_));
1126 EXPECT_NE(session,
1127 QuicStreamFactoryPeer::GetActiveSession(
1128 &factory_, host_port_pair_, is_https_));
1129 EXPECT_EQ(true, QuicStreamFactoryPeer::IsLiveSession(&factory_, session));
1131 stream2.reset();
1132 stream.reset();
1134 EXPECT_TRUE(socket_data.at_read_eof());
1135 EXPECT_TRUE(socket_data.at_write_eof());
1136 EXPECT_TRUE(socket_data2.at_read_eof());
1137 EXPECT_TRUE(socket_data2.at_write_eof());
1140 TEST_P(QuicStreamFactoryTest, MaxOpenStream) {
1141 MockRead reads[] = {
1142 MockRead(ASYNC, OK, 0) // EOF
1144 QuicStreamId stream_id = kClientDataStreamId1;
1145 scoped_ptr<QuicEncryptedPacket> rst(
1146 maker_.MakeRstPacket(1, true, stream_id, QUIC_STREAM_CANCELLED));
1147 MockWrite writes[] = {
1148 MockWrite(ASYNC, rst->data(), rst->length(), 1),
1150 DeterministicSocketData socket_data(reads, arraysize(reads),
1151 writes, arraysize(writes));
1152 socket_factory_.AddSocketDataProvider(&socket_data);
1153 socket_data.StopAfter(1);
1155 HttpRequestInfo request_info;
1156 std::vector<QuicHttpStream*> streams;
1157 // The MockCryptoClientStream sets max_open_streams to be
1158 // kDefaultMaxStreamsPerConnection / 2.
1159 for (size_t i = 0; i < kDefaultMaxStreamsPerConnection / 2; i++) {
1160 QuicStreamRequest request(&factory_);
1161 int rv = request.Request(host_port_pair_,
1162 is_https_,
1163 privacy_mode_,
1164 "GET",
1165 net_log_,
1166 callback_.callback());
1167 if (i == 0) {
1168 EXPECT_EQ(ERR_IO_PENDING, rv);
1169 EXPECT_EQ(OK, callback_.WaitForResult());
1170 } else {
1171 EXPECT_EQ(OK, rv);
1173 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
1174 EXPECT_TRUE(stream);
1175 EXPECT_EQ(OK, stream->InitializeStream(
1176 &request_info, DEFAULT_PRIORITY, net_log_, CompletionCallback()));
1177 streams.push_back(stream.release());
1180 QuicStreamRequest request(&factory_);
1181 EXPECT_EQ(OK,
1182 request.Request(host_port_pair_,
1183 is_https_,
1184 privacy_mode_,
1185 "GET",
1186 net_log_,
1187 CompletionCallback()));
1188 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
1189 EXPECT_TRUE(stream);
1190 EXPECT_EQ(ERR_IO_PENDING, stream->InitializeStream(
1191 &request_info, DEFAULT_PRIORITY, net_log_, callback_.callback()));
1193 // Close the first stream.
1194 streams.front()->Close(false);
1196 ASSERT_TRUE(callback_.have_result());
1198 EXPECT_EQ(OK, callback_.WaitForResult());
1200 EXPECT_TRUE(socket_data.at_read_eof());
1201 EXPECT_TRUE(socket_data.at_write_eof());
1202 STLDeleteElements(&streams);
1205 TEST_P(QuicStreamFactoryTest, ResolutionErrorInCreate) {
1206 DeterministicSocketData socket_data(nullptr, 0, nullptr, 0);
1207 socket_factory_.AddSocketDataProvider(&socket_data);
1209 host_resolver_.rules()->AddSimulatedFailure(kDefaultServerHostName);
1211 QuicStreamRequest request(&factory_);
1212 EXPECT_EQ(ERR_IO_PENDING,
1213 request.Request(host_port_pair_,
1214 is_https_,
1215 privacy_mode_,
1216 "GET",
1217 net_log_,
1218 callback_.callback()));
1220 EXPECT_EQ(ERR_NAME_NOT_RESOLVED, callback_.WaitForResult());
1222 EXPECT_TRUE(socket_data.at_read_eof());
1223 EXPECT_TRUE(socket_data.at_write_eof());
1226 TEST_P(QuicStreamFactoryTest, ConnectErrorInCreate) {
1227 MockConnect connect(SYNCHRONOUS, ERR_ADDRESS_IN_USE);
1228 DeterministicSocketData socket_data(nullptr, 0, nullptr, 0);
1229 socket_data.set_connect_data(connect);
1230 socket_factory_.AddSocketDataProvider(&socket_data);
1231 socket_data.StopAfter(1);
1233 QuicStreamRequest request(&factory_);
1234 EXPECT_EQ(ERR_IO_PENDING,
1235 request.Request(host_port_pair_,
1236 is_https_,
1237 privacy_mode_,
1238 "GET",
1239 net_log_,
1240 callback_.callback()));
1242 EXPECT_EQ(ERR_ADDRESS_IN_USE, callback_.WaitForResult());
1244 EXPECT_TRUE(socket_data.at_read_eof());
1245 EXPECT_TRUE(socket_data.at_write_eof());
1248 TEST_P(QuicStreamFactoryTest, CancelCreate) {
1249 MockRead reads[] = {
1250 MockRead(ASYNC, OK, 0) // EOF
1252 DeterministicSocketData socket_data(reads, arraysize(reads), nullptr, 0);
1253 socket_factory_.AddSocketDataProvider(&socket_data);
1255 QuicStreamRequest request(&factory_);
1256 EXPECT_EQ(ERR_IO_PENDING,
1257 request.Request(host_port_pair_,
1258 is_https_,
1259 privacy_mode_,
1260 "GET",
1261 net_log_,
1262 callback_.callback()));
1265 socket_data.StopAfter(1);
1266 base::RunLoop run_loop;
1267 run_loop.RunUntilIdle();
1269 scoped_ptr<QuicHttpStream> stream(
1270 CreateIfSessionExists(host_port_pair_, net_log_));
1271 EXPECT_TRUE(stream.get());
1272 stream.reset();
1274 EXPECT_TRUE(socket_data.at_read_eof());
1275 EXPECT_TRUE(socket_data.at_write_eof());
1278 TEST_P(QuicStreamFactoryTest, CreateConsistentEphemeralPort) {
1279 // Sequentially connect to the default host, then another host, and then the
1280 // default host. Verify that the default host gets a consistent ephemeral
1281 // port, that is different from the other host's connection.
1283 std::string other_server_name = "other.google.com";
1284 EXPECT_NE(kDefaultServerHostName, other_server_name);
1285 HostPortPair host_port_pair2(other_server_name, kDefaultServerPort);
1287 int original_port = GetSourcePortForNewSession(host_port_pair_);
1288 EXPECT_NE(original_port, GetSourcePortForNewSession(host_port_pair2));
1289 EXPECT_EQ(original_port, GetSourcePortForNewSession(host_port_pair_));
1292 TEST_P(QuicStreamFactoryTest, GoAwayDisablesConsistentEphemeralPort) {
1293 // Get a session to the host using the port suggester.
1294 int original_port =
1295 GetSourcePortForNewSessionAndGoAway(host_port_pair_);
1296 // Verify that the port is different after the goaway.
1297 EXPECT_NE(original_port, GetSourcePortForNewSession(host_port_pair_));
1298 // Since the previous session did not goaway we should see the original port.
1299 EXPECT_EQ(original_port, GetSourcePortForNewSession(host_port_pair_));
1302 TEST_P(QuicStreamFactoryTest, CloseAllSessions) {
1303 MockRead reads[] = {
1304 MockRead(ASYNC, 0, 0) // EOF
1306 scoped_ptr<QuicEncryptedPacket> rst(ConstructRstPacket());
1307 std::vector<MockWrite> writes;
1308 writes.push_back(MockWrite(ASYNC, rst->data(), rst->length(), 1));
1309 DeterministicSocketData socket_data(reads, arraysize(reads),
1310 writes.empty() ? nullptr : &writes[0],
1311 writes.size());
1312 socket_factory_.AddSocketDataProvider(&socket_data);
1313 socket_data.StopAfter(1);
1315 MockRead reads2[] = {
1316 MockRead(ASYNC, 0, 0) // EOF
1318 DeterministicSocketData socket_data2(reads2, arraysize(reads2), nullptr, 0);
1319 socket_factory_.AddSocketDataProvider(&socket_data2);
1320 socket_data2.StopAfter(1);
1322 QuicStreamRequest request(&factory_);
1323 EXPECT_EQ(ERR_IO_PENDING,
1324 request.Request(host_port_pair_,
1325 is_https_,
1326 privacy_mode_,
1327 "GET",
1328 net_log_,
1329 callback_.callback()));
1331 EXPECT_EQ(OK, callback_.WaitForResult());
1332 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
1333 HttpRequestInfo request_info;
1334 EXPECT_EQ(OK, stream->InitializeStream(&request_info,
1335 DEFAULT_PRIORITY,
1336 net_log_, CompletionCallback()));
1338 // Close the session and verify that stream saw the error.
1339 factory_.CloseAllSessions(ERR_INTERNET_DISCONNECTED);
1340 EXPECT_EQ(ERR_INTERNET_DISCONNECTED,
1341 stream->ReadResponseHeaders(callback_.callback()));
1343 // Now attempting to request a stream to the same origin should create
1344 // a new session.
1346 QuicStreamRequest request2(&factory_);
1347 EXPECT_EQ(ERR_IO_PENDING,
1348 request2.Request(host_port_pair_,
1349 is_https_,
1350 privacy_mode_,
1351 "GET",
1352 net_log_,
1353 callback_.callback()));
1355 EXPECT_EQ(OK, callback_.WaitForResult());
1356 stream = request2.ReleaseStream();
1357 stream.reset(); // Will reset stream 3.
1359 EXPECT_TRUE(socket_data.at_read_eof());
1360 EXPECT_TRUE(socket_data.at_write_eof());
1361 EXPECT_TRUE(socket_data2.at_read_eof());
1362 EXPECT_TRUE(socket_data2.at_write_eof());
1365 TEST_P(QuicStreamFactoryTest, OnIPAddressChanged) {
1366 MockRead reads[] = {
1367 MockRead(ASYNC, 0, 0) // EOF
1369 scoped_ptr<QuicEncryptedPacket> rst(ConstructRstPacket());
1370 std::vector<MockWrite> writes;
1371 writes.push_back(MockWrite(ASYNC, rst->data(), rst->length(), 1));
1372 DeterministicSocketData socket_data(reads, arraysize(reads),
1373 writes.empty() ? nullptr : &writes[0],
1374 writes.size());
1375 socket_factory_.AddSocketDataProvider(&socket_data);
1376 socket_data.StopAfter(1);
1378 MockRead reads2[] = {
1379 MockRead(ASYNC, 0, 0) // EOF
1381 DeterministicSocketData socket_data2(reads2, arraysize(reads2), nullptr, 0);
1382 socket_factory_.AddSocketDataProvider(&socket_data2);
1383 socket_data2.StopAfter(1);
1385 QuicStreamRequest request(&factory_);
1386 EXPECT_EQ(ERR_IO_PENDING,
1387 request.Request(host_port_pair_,
1388 is_https_,
1389 privacy_mode_,
1390 "GET",
1391 net_log_,
1392 callback_.callback()));
1394 EXPECT_EQ(OK, callback_.WaitForResult());
1395 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
1396 HttpRequestInfo request_info;
1397 EXPECT_EQ(OK, stream->InitializeStream(&request_info,
1398 DEFAULT_PRIORITY,
1399 net_log_, CompletionCallback()));
1401 // Change the IP address and verify that stream saw the error.
1402 factory_.OnIPAddressChanged();
1403 EXPECT_EQ(ERR_NETWORK_CHANGED,
1404 stream->ReadResponseHeaders(callback_.callback()));
1405 EXPECT_TRUE(factory_.require_confirmation());
1407 // Now attempting to request a stream to the same origin should create
1408 // a new session.
1410 QuicStreamRequest request2(&factory_);
1411 EXPECT_EQ(ERR_IO_PENDING,
1412 request2.Request(host_port_pair_,
1413 is_https_,
1414 privacy_mode_,
1415 "GET",
1416 net_log_,
1417 callback_.callback()));
1419 EXPECT_EQ(OK, callback_.WaitForResult());
1420 stream = request2.ReleaseStream();
1421 stream.reset(); // Will reset stream 3.
1423 EXPECT_TRUE(socket_data.at_read_eof());
1424 EXPECT_TRUE(socket_data.at_write_eof());
1425 EXPECT_TRUE(socket_data2.at_read_eof());
1426 EXPECT_TRUE(socket_data2.at_write_eof());
1429 TEST_P(QuicStreamFactoryTest, OnCertAdded) {
1430 MockRead reads[] = {
1431 MockRead(ASYNC, 0, 0) // EOF
1433 scoped_ptr<QuicEncryptedPacket> rst(ConstructRstPacket());
1434 std::vector<MockWrite> writes;
1435 writes.push_back(MockWrite(ASYNC, rst->data(), rst->length(), 1));
1436 DeterministicSocketData socket_data(reads, arraysize(reads),
1437 writes.empty() ? nullptr : &writes[0],
1438 writes.size());
1439 socket_factory_.AddSocketDataProvider(&socket_data);
1440 socket_data.StopAfter(1);
1442 MockRead reads2[] = {
1443 MockRead(ASYNC, 0, 0) // EOF
1445 DeterministicSocketData socket_data2(reads2, arraysize(reads2), nullptr, 0);
1446 socket_factory_.AddSocketDataProvider(&socket_data2);
1447 socket_data2.StopAfter(1);
1449 QuicStreamRequest request(&factory_);
1450 EXPECT_EQ(ERR_IO_PENDING,
1451 request.Request(host_port_pair_,
1452 is_https_,
1453 privacy_mode_,
1454 "GET",
1455 net_log_,
1456 callback_.callback()));
1458 EXPECT_EQ(OK, callback_.WaitForResult());
1459 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
1460 HttpRequestInfo request_info;
1461 EXPECT_EQ(OK, stream->InitializeStream(&request_info,
1462 DEFAULT_PRIORITY,
1463 net_log_, CompletionCallback()));
1465 // Add a cert and verify that stream saw the event.
1466 factory_.OnCertAdded(nullptr);
1467 EXPECT_EQ(ERR_CERT_DATABASE_CHANGED,
1468 stream->ReadResponseHeaders(callback_.callback()));
1469 EXPECT_FALSE(factory_.require_confirmation());
1471 // Now attempting to request a stream to the same origin should create
1472 // a new session.
1474 QuicStreamRequest request2(&factory_);
1475 EXPECT_EQ(ERR_IO_PENDING,
1476 request2.Request(host_port_pair_,
1477 is_https_,
1478 privacy_mode_,
1479 "GET",
1480 net_log_,
1481 callback_.callback()));
1483 EXPECT_EQ(OK, callback_.WaitForResult());
1484 stream = request2.ReleaseStream();
1485 stream.reset(); // Will reset stream 3.
1487 EXPECT_TRUE(socket_data.at_read_eof());
1488 EXPECT_TRUE(socket_data.at_write_eof());
1489 EXPECT_TRUE(socket_data2.at_read_eof());
1490 EXPECT_TRUE(socket_data2.at_write_eof());
1493 TEST_P(QuicStreamFactoryTest, OnCACertChanged) {
1494 MockRead reads[] = {
1495 MockRead(ASYNC, 0, 0) // EOF
1497 scoped_ptr<QuicEncryptedPacket> rst(ConstructRstPacket());
1498 std::vector<MockWrite> writes;
1499 writes.push_back(MockWrite(ASYNC, rst->data(), rst->length(), 1));
1500 DeterministicSocketData socket_data(reads, arraysize(reads),
1501 writes.empty() ? nullptr : &writes[0],
1502 writes.size());
1503 socket_factory_.AddSocketDataProvider(&socket_data);
1504 socket_data.StopAfter(1);
1506 MockRead reads2[] = {
1507 MockRead(ASYNC, 0, 0) // EOF
1509 DeterministicSocketData socket_data2(reads2, arraysize(reads2), nullptr, 0);
1510 socket_factory_.AddSocketDataProvider(&socket_data2);
1511 socket_data2.StopAfter(1);
1513 QuicStreamRequest request(&factory_);
1514 EXPECT_EQ(ERR_IO_PENDING,
1515 request.Request(host_port_pair_,
1516 is_https_,
1517 privacy_mode_,
1518 "GET",
1519 net_log_,
1520 callback_.callback()));
1522 EXPECT_EQ(OK, callback_.WaitForResult());
1523 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
1524 HttpRequestInfo request_info;
1525 EXPECT_EQ(OK, stream->InitializeStream(&request_info,
1526 DEFAULT_PRIORITY,
1527 net_log_, CompletionCallback()));
1529 // Change the CA cert and verify that stream saw the event.
1530 factory_.OnCACertChanged(nullptr);
1531 EXPECT_EQ(ERR_CERT_DATABASE_CHANGED,
1532 stream->ReadResponseHeaders(callback_.callback()));
1533 EXPECT_FALSE(factory_.require_confirmation());
1535 // Now attempting to request a stream to the same origin should create
1536 // a new session.
1538 QuicStreamRequest request2(&factory_);
1539 EXPECT_EQ(ERR_IO_PENDING,
1540 request2.Request(host_port_pair_,
1541 is_https_,
1542 privacy_mode_,
1543 "GET",
1544 net_log_,
1545 callback_.callback()));
1547 EXPECT_EQ(OK, callback_.WaitForResult());
1548 stream = request2.ReleaseStream();
1549 stream.reset(); // Will reset stream 3.
1551 EXPECT_TRUE(socket_data.at_read_eof());
1552 EXPECT_TRUE(socket_data.at_write_eof());
1553 EXPECT_TRUE(socket_data2.at_read_eof());
1554 EXPECT_TRUE(socket_data2.at_write_eof());
1557 TEST_P(QuicStreamFactoryTest, SharedCryptoConfig) {
1558 vector<string> cannoncial_suffixes;
1559 cannoncial_suffixes.push_back(string(".c.youtube.com"));
1560 cannoncial_suffixes.push_back(string(".googlevideo.com"));
1562 for (unsigned i = 0; i < cannoncial_suffixes.size(); ++i) {
1563 string r1_host_name("r1");
1564 string r2_host_name("r2");
1565 r1_host_name.append(cannoncial_suffixes[i]);
1566 r2_host_name.append(cannoncial_suffixes[i]);
1568 HostPortPair host_port_pair1(r1_host_name, 80);
1569 QuicCryptoClientConfig* crypto_config =
1570 QuicStreamFactoryPeer::GetCryptoConfig(&factory_);
1571 QuicServerId server_id1(host_port_pair1, is_https_, privacy_mode_);
1572 QuicCryptoClientConfig::CachedState* cached1 =
1573 crypto_config->LookupOrCreate(server_id1);
1574 EXPECT_FALSE(cached1->proof_valid());
1575 EXPECT_TRUE(cached1->source_address_token().empty());
1577 // Mutate the cached1 to have different data.
1578 // TODO(rtenneti): mutate other members of CachedState.
1579 cached1->set_source_address_token(r1_host_name);
1580 cached1->SetProofValid();
1582 HostPortPair host_port_pair2(r2_host_name, 80);
1583 QuicServerId server_id2(host_port_pair2, is_https_, privacy_mode_);
1584 QuicCryptoClientConfig::CachedState* cached2 =
1585 crypto_config->LookupOrCreate(server_id2);
1586 EXPECT_EQ(cached1->source_address_token(), cached2->source_address_token());
1587 EXPECT_TRUE(cached2->proof_valid());
1591 TEST_P(QuicStreamFactoryTest, CryptoConfigWhenProofIsInvalid) {
1592 vector<string> cannoncial_suffixes;
1593 cannoncial_suffixes.push_back(string(".c.youtube.com"));
1594 cannoncial_suffixes.push_back(string(".googlevideo.com"));
1596 for (unsigned i = 0; i < cannoncial_suffixes.size(); ++i) {
1597 string r3_host_name("r3");
1598 string r4_host_name("r4");
1599 r3_host_name.append(cannoncial_suffixes[i]);
1600 r4_host_name.append(cannoncial_suffixes[i]);
1602 HostPortPair host_port_pair1(r3_host_name, 80);
1603 QuicCryptoClientConfig* crypto_config =
1604 QuicStreamFactoryPeer::GetCryptoConfig(&factory_);
1605 QuicServerId server_id1(host_port_pair1, is_https_, privacy_mode_);
1606 QuicCryptoClientConfig::CachedState* cached1 =
1607 crypto_config->LookupOrCreate(server_id1);
1608 EXPECT_FALSE(cached1->proof_valid());
1609 EXPECT_TRUE(cached1->source_address_token().empty());
1611 // Mutate the cached1 to have different data.
1612 // TODO(rtenneti): mutate other members of CachedState.
1613 cached1->set_source_address_token(r3_host_name);
1614 cached1->SetProofInvalid();
1616 HostPortPair host_port_pair2(r4_host_name, 80);
1617 QuicServerId server_id2(host_port_pair2, is_https_, privacy_mode_);
1618 QuicCryptoClientConfig::CachedState* cached2 =
1619 crypto_config->LookupOrCreate(server_id2);
1620 EXPECT_NE(cached1->source_address_token(), cached2->source_address_token());
1621 EXPECT_TRUE(cached2->source_address_token().empty());
1622 EXPECT_FALSE(cached2->proof_valid());
1626 TEST_P(QuicStreamFactoryTest, RacingConnections) {
1627 if (!GetParam().enable_connection_racing)
1628 return;
1629 factory_.set_quic_server_info_factory(&quic_server_info_factory_);
1630 QuicStreamFactoryPeer::SetTaskRunner(&factory_, runner_.get());
1631 MockRead reads[] = {
1632 MockRead(ASYNC, OK, 0) // EOF
1634 DeterministicSocketData socket_data(reads, arraysize(reads), nullptr, 0);
1635 socket_factory_.AddSocketDataProvider(&socket_data);
1636 socket_data.StopAfter(1);
1638 MockRead reads2[] = {
1639 MockRead(ASYNC, 0, 0) // EOF
1641 DeterministicSocketData socket_data2(reads2, arraysize(reads2), nullptr, 0);
1642 socket_factory_.AddSocketDataProvider(&socket_data2);
1643 socket_data2.StopAfter(1);
1645 crypto_client_stream_factory_.set_handshake_mode(
1646 MockCryptoClientStream::ZERO_RTT);
1647 host_resolver_.set_synchronous_mode(true);
1648 host_resolver_.rules()->AddIPLiteralRule(host_port_pair_.host(),
1649 "192.168.0.1", "");
1651 QuicStreamRequest request(&factory_);
1652 QuicServerId server_id(host_port_pair_, is_https_, privacy_mode_);
1653 EXPECT_EQ(ERR_IO_PENDING,
1654 request.Request(host_port_pair_, is_https_, privacy_mode_, "GET",
1655 net_log_, callback_.callback()));
1656 EXPECT_EQ(2u,
1657 QuicStreamFactoryPeer::GetNumberOfActiveJobs(&factory_, server_id));
1659 runner_->RunNextTask();
1661 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
1662 EXPECT_TRUE(stream.get());
1663 EXPECT_TRUE(socket_data.at_read_eof());
1664 EXPECT_TRUE(socket_data.at_write_eof());
1665 EXPECT_EQ(0u,
1666 QuicStreamFactoryPeer::GetNumberOfActiveJobs(&factory_, server_id));
1669 TEST_P(QuicStreamFactoryTest, EnableNotLoadFromDiskCache) {
1670 factory_.set_quic_server_info_factory(&quic_server_info_factory_);
1671 QuicStreamFactoryPeer::SetTaskRunner(&factory_, runner_.get());
1672 QuicStreamFactoryPeer::SetDisableDiskCache(&factory_, true);
1674 MockRead reads[] = {
1675 MockRead(ASYNC, OK, 0) // EOF
1677 DeterministicSocketData socket_data(reads, arraysize(reads), nullptr, 0);
1678 socket_factory_.AddSocketDataProvider(&socket_data);
1679 socket_data.StopAfter(1);
1681 crypto_client_stream_factory_.set_handshake_mode(
1682 MockCryptoClientStream::ZERO_RTT);
1683 host_resolver_.set_synchronous_mode(true);
1684 host_resolver_.rules()->AddIPLiteralRule(host_port_pair_.host(),
1685 "192.168.0.1", "");
1687 QuicStreamRequest request(&factory_);
1688 EXPECT_EQ(OK, request.Request(host_port_pair_, is_https_, privacy_mode_,
1689 "GET", net_log_, callback_.callback()));
1691 // If we are waiting for disk cache, we would have posted a task. Verify that
1692 // the CancelWaitForDataReady task hasn't been posted.
1693 ASSERT_EQ(0u, runner_->GetPostedTasks().size());
1695 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
1696 EXPECT_TRUE(stream.get());
1697 EXPECT_TRUE(socket_data.at_read_eof());
1698 EXPECT_TRUE(socket_data.at_write_eof());
1701 TEST_P(QuicStreamFactoryTest, BadPacketLoss) {
1702 factory_.set_quic_server_info_factory(&quic_server_info_factory_);
1703 QuicStreamFactoryPeer::SetTaskRunner(&factory_, runner_.get());
1704 QuicStreamFactoryPeer::SetDisableDiskCache(&factory_, true);
1705 QuicStreamFactoryPeer::SetMaxNumberOfLossyConnections(&factory_, 2);
1706 EXPECT_FALSE(
1707 QuicStreamFactoryPeer::IsQuicDisabled(&factory_, host_port_pair_.port()));
1708 EXPECT_EQ(0, QuicStreamFactoryPeer::GetNumberOfLossyConnections(
1709 &factory_, host_port_pair_.port()));
1711 MockRead reads[] = {
1712 MockRead(ASYNC, OK, 0) // EOF
1714 DeterministicSocketData socket_data(reads, arraysize(reads), nullptr, 0);
1715 socket_factory_.AddSocketDataProvider(&socket_data);
1716 socket_data.StopAfter(1);
1718 DeterministicSocketData socket_data2(reads, arraysize(reads), nullptr, 0);
1719 socket_factory_.AddSocketDataProvider(&socket_data2);
1720 socket_data2.StopAfter(1);
1722 DeterministicSocketData socket_data3(reads, arraysize(reads), nullptr, 0);
1723 socket_factory_.AddSocketDataProvider(&socket_data3);
1724 socket_data3.StopAfter(1);
1726 HostPortPair server2("mail.example.org", kDefaultServerPort);
1727 HostPortPair server3("docs.example.org", kDefaultServerPort);
1729 crypto_client_stream_factory_.set_handshake_mode(
1730 MockCryptoClientStream::ZERO_RTT);
1731 host_resolver_.set_synchronous_mode(true);
1732 host_resolver_.rules()->AddIPLiteralRule(host_port_pair_.host(),
1733 "192.168.0.1", "");
1734 host_resolver_.rules()->AddIPLiteralRule(server2.host(), "192.168.0.1", "");
1735 host_resolver_.rules()->AddIPLiteralRule(server3.host(), "192.168.0.1", "");
1737 QuicStreamRequest request(&factory_);
1738 EXPECT_EQ(OK, request.Request(host_port_pair_, is_https_, privacy_mode_,
1739 "GET", net_log_, callback_.callback()));
1741 QuicClientSession* session = QuicStreamFactoryPeer::GetActiveSession(
1742 &factory_, host_port_pair_, is_https_);
1744 DVLOG(1) << "Create 1st session and test packet loss";
1746 // Set packet_loss_rate to a lower value than packet_loss_threshold.
1747 EXPECT_FALSE(
1748 factory_.OnHandshakeConfirmed(session, /*packet_loss_rate=*/0.9f));
1749 EXPECT_TRUE(session->connection()->connected());
1750 EXPECT_TRUE(QuicStreamFactoryPeer::HasActiveSession(
1751 &factory_, host_port_pair_, is_https_));
1752 EXPECT_FALSE(
1753 QuicStreamFactoryPeer::IsQuicDisabled(&factory_, host_port_pair_.port()));
1754 EXPECT_EQ(0, QuicStreamFactoryPeer::GetNumberOfLossyConnections(
1755 &factory_, host_port_pair_.port()));
1757 // Set packet_loss_rate to a higher value than packet_loss_threshold only once
1758 // and that should close the session, but shouldn't disable QUIC.
1759 EXPECT_TRUE(
1760 factory_.OnHandshakeConfirmed(session, /*packet_loss_rate=*/1.0f));
1761 EXPECT_EQ(1, QuicStreamFactoryPeer::GetNumberOfLossyConnections(
1762 &factory_, host_port_pair_.port()));
1763 EXPECT_FALSE(
1764 QuicStreamFactoryPeer::IsQuicDisabled(&factory_, host_port_pair_.port()));
1765 EXPECT_FALSE(QuicStreamFactoryPeer::HasActiveSession(
1766 &factory_, host_port_pair_, is_https_));
1767 EXPECT_EQ(nullptr, CreateIfSessionExists(host_port_pair_, net_log_).get());
1769 // Test N-in-a-row high packet loss connections.
1771 DVLOG(1) << "Create 2nd session and test packet loss";
1773 TestCompletionCallback callback2;
1774 QuicStreamRequest request2(&factory_);
1775 EXPECT_EQ(OK, request2.Request(server2, is_https_, privacy_mode_, "GET",
1776 net_log_, callback2.callback()));
1777 QuicClientSession* session2 =
1778 QuicStreamFactoryPeer::GetActiveSession(&factory_, server2, is_https_);
1780 // If there is no packet loss during handshake confirmation, number of lossy
1781 // connections for the port should be 0.
1782 EXPECT_EQ(1, QuicStreamFactoryPeer::GetNumberOfLossyConnections(
1783 &factory_, server2.port()));
1784 EXPECT_FALSE(
1785 factory_.OnHandshakeConfirmed(session2, /*packet_loss_rate=*/0.9f));
1786 EXPECT_EQ(0, QuicStreamFactoryPeer::GetNumberOfLossyConnections(
1787 &factory_, server2.port()));
1788 EXPECT_FALSE(
1789 QuicStreamFactoryPeer::IsQuicDisabled(&factory_, server2.port()));
1791 // Set packet_loss_rate to a higher value than packet_loss_threshold only once
1792 // and that should close the session, but shouldn't disable QUIC.
1793 EXPECT_TRUE(
1794 factory_.OnHandshakeConfirmed(session2, /*packet_loss_rate=*/1.0f));
1795 EXPECT_EQ(1, QuicStreamFactoryPeer::GetNumberOfLossyConnections(
1796 &factory_, server2.port()));
1797 EXPECT_FALSE(session2->connection()->connected());
1798 EXPECT_FALSE(
1799 QuicStreamFactoryPeer::IsQuicDisabled(&factory_, server2.port()));
1800 EXPECT_FALSE(
1801 QuicStreamFactoryPeer::HasActiveSession(&factory_, server2, is_https_));
1802 EXPECT_EQ(nullptr, CreateIfSessionExists(server2, net_log_).get());
1804 DVLOG(1) << "Create 3rd session which also has packet loss";
1806 TestCompletionCallback callback3;
1807 QuicStreamRequest request3(&factory_);
1808 EXPECT_EQ(OK, request3.Request(server3, is_https_, privacy_mode_, "GET",
1809 net_log_, callback3.callback()));
1810 QuicClientSession* session3 =
1811 QuicStreamFactoryPeer::GetActiveSession(&factory_, server3, is_https_);
1813 // Set packet_loss_rate to higher value than packet_loss_threshold 2nd time in
1814 // a row and that should close the session and disable QUIC.
1815 EXPECT_TRUE(
1816 factory_.OnHandshakeConfirmed(session3, /*packet_loss_rate=*/1.0f));
1817 EXPECT_EQ(2, QuicStreamFactoryPeer::GetNumberOfLossyConnections(
1818 &factory_, server3.port()));
1819 EXPECT_FALSE(session2->connection()->connected());
1820 EXPECT_TRUE(QuicStreamFactoryPeer::IsQuicDisabled(&factory_, server3.port()));
1821 EXPECT_FALSE(
1822 QuicStreamFactoryPeer::HasActiveSession(&factory_, server3, is_https_));
1823 EXPECT_EQ(nullptr, CreateIfSessionExists(server3, net_log_).get());
1825 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
1826 EXPECT_TRUE(stream.get());
1827 scoped_ptr<QuicHttpStream> stream2 = request2.ReleaseStream();
1828 EXPECT_TRUE(stream2.get());
1829 scoped_ptr<QuicHttpStream> stream3 = request3.ReleaseStream();
1830 EXPECT_TRUE(stream3.get());
1831 EXPECT_TRUE(socket_data.at_read_eof());
1832 EXPECT_TRUE(socket_data.at_write_eof());
1833 EXPECT_TRUE(socket_data2.at_read_eof());
1834 EXPECT_TRUE(socket_data2.at_write_eof());
1835 EXPECT_TRUE(socket_data3.at_read_eof());
1836 EXPECT_TRUE(socket_data3.at_write_eof());
1839 } // namespace test
1840 } // namespace net