Don't show supervised user as "already on this device" while they're being imported.
[chromium-blink-merge.git] / net / quic / quic_stream_factory_test.cc
blobbc85df2cae7caff1b4cdc432d2a42f811b8ff274
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> CreateFromSession(
100 QuicStreamFactory* factory,
101 QuicClientSession* session) {
102 return factory->CreateFromSession(session);
105 static bool IsLiveSession(QuicStreamFactory* factory,
106 QuicClientSession* session) {
107 for (QuicStreamFactory::SessionIdMap::iterator it =
108 factory->all_sessions_.begin();
109 it != factory->all_sessions_.end(); ++it) {
110 if (it->first == session)
111 return true;
113 return false;
116 static void DisableConnectionPooling(QuicStreamFactory* factory) {
117 factory->disable_connection_pooling_ = true;
120 static void SetTaskRunner(QuicStreamFactory* factory,
121 base::TaskRunner* task_runner) {
122 factory->task_runner_ = task_runner;
125 static void SetEnableConnectionRacing(QuicStreamFactory* factory,
126 bool enable_connection_racing) {
127 factory->enable_connection_racing_ = enable_connection_racing;
130 static void SetDisableDiskCache(QuicStreamFactory* factory,
131 bool disable_disk_cache) {
132 factory->disable_disk_cache_ = disable_disk_cache;
135 static void SetMaxNumberOfLossyConnections(
136 QuicStreamFactory* factory,
137 int max_number_of_lossy_connections) {
138 factory->max_number_of_lossy_connections_ = max_number_of_lossy_connections;
141 static int GetNumberOfLossyConnections(QuicStreamFactory* factory,
142 uint16 port) {
143 return factory->number_of_lossy_connections_[port];
146 static bool IsQuicDisabled(QuicStreamFactory* factory, uint16 port) {
147 return factory->IsQuicDisabled(port);
150 static size_t GetNumberOfActiveJobs(QuicStreamFactory* factory,
151 const QuicServerId& server_id) {
152 return (factory->active_jobs_[server_id]).size();
156 class MockQuicServerInfo : public QuicServerInfo {
157 public:
158 MockQuicServerInfo(const QuicServerId& server_id)
159 : QuicServerInfo(server_id) {}
160 ~MockQuicServerInfo() override {}
162 void Start() override {}
164 int WaitForDataReady(const CompletionCallback& callback) override {
165 return ERR_IO_PENDING;
168 void ResetWaitForDataReadyCallback() override {}
170 void CancelWaitForDataReadyCallback() override {}
172 bool IsDataReady() override { return false; }
174 bool IsReadyToPersist() override { return false; }
176 void Persist() override {}
178 void OnExternalCacheHit() override {}
181 class MockQuicServerInfoFactory : public QuicServerInfoFactory {
182 public:
183 MockQuicServerInfoFactory() {}
184 ~MockQuicServerInfoFactory() override {}
186 QuicServerInfo* GetForServer(const QuicServerId& server_id) override {
187 return new MockQuicServerInfo(server_id);
191 class QuicStreamFactoryTest : public ::testing::TestWithParam<TestParams> {
192 protected:
193 QuicStreamFactoryTest()
194 : random_generator_(0),
195 clock_(new MockClock()),
196 runner_(new TestTaskRunner(clock_)),
197 maker_(GetParam().version, 0, clock_, kDefaultServerHostName),
198 cert_verifier_(CertVerifier::CreateDefault()),
199 channel_id_service_(
200 new ChannelIDService(new DefaultChannelIDStore(nullptr),
201 base::MessageLoopProxy::current())),
202 factory_(&host_resolver_,
203 &socket_factory_,
204 base::WeakPtr<HttpServerProperties>(),
205 cert_verifier_.get(),
206 channel_id_service_.get(),
207 &transport_security_state_,
208 &crypto_client_stream_factory_,
209 &random_generator_,
210 clock_,
211 kDefaultMaxPacketSize,
212 std::string(),
213 SupportedVersions(GetParam().version),
214 /*enable_port_selection=*/true,
215 /*always_require_handshake_confirmation=*/false,
216 /*disable_connection_pooling=*/false,
217 /*load_server_info_timeout_srtt_multiplier=*/0.0f,
218 /*enable_connection_racing=*/false,
219 /*enable_non_blocking_io=*/true,
220 /*disable_disk_cache=*/false,
221 /*max_number_of_lossy_connections=*/0,
222 /*packet_loss_threshold=*/1.0f,
223 /*receive_buffer_size=*/0,
224 QuicTagVector()),
225 host_port_pair_(kDefaultServerHostName, kDefaultServerPort),
226 is_https_(false),
227 privacy_mode_(PRIVACY_MODE_DISABLED) {
228 factory_.set_require_confirmation(false);
229 clock_->AdvanceTime(QuicTime::Delta::FromSeconds(1));
230 QuicStreamFactoryPeer::SetEnableConnectionRacing(
231 &factory_, GetParam().enable_connection_racing);
234 bool HasActiveSession(const HostPortPair& host_port_pair) {
235 return QuicStreamFactoryPeer::HasActiveSession(&factory_, host_port_pair,
236 /*is_https_=*/false);
239 scoped_ptr<QuicHttpStream> CreateFromSession(
240 const HostPortPair& host_port_pair) {
241 QuicClientSession* session = QuicStreamFactoryPeer::GetActiveSession(
242 &factory_, host_port_pair, /*is_https=*/false);
243 return QuicStreamFactoryPeer::CreateFromSession(&factory_, session);
246 int GetSourcePortForNewSession(const HostPortPair& destination) {
247 return GetSourcePortForNewSessionInner(destination, false);
250 int GetSourcePortForNewSessionAndGoAway(
251 const HostPortPair& destination) {
252 return GetSourcePortForNewSessionInner(destination, true);
255 int GetSourcePortForNewSessionInner(const HostPortPair& destination,
256 bool goaway_received) {
257 // Should only be called if there is no active session for this destination.
258 EXPECT_FALSE(HasActiveSession(destination));
259 size_t socket_count = socket_factory_.udp_client_sockets().size();
261 MockRead reads[] = {
262 MockRead(ASYNC, OK, 0) // EOF
264 DeterministicSocketData socket_data(reads, arraysize(reads), nullptr, 0);
265 socket_data.StopAfter(1);
266 socket_factory_.AddSocketDataProvider(&socket_data);
268 QuicStreamRequest request(&factory_);
269 EXPECT_EQ(ERR_IO_PENDING,
270 request.Request(destination, is_https_, privacy_mode_,
271 destination.host(), "GET", net_log_,
272 callback_.callback()));
274 EXPECT_EQ(OK, callback_.WaitForResult());
275 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
276 EXPECT_TRUE(stream.get());
277 stream.reset();
279 QuicClientSession* session = QuicStreamFactoryPeer::GetActiveSession(
280 &factory_, destination, is_https_);
282 if (socket_count + 1 != socket_factory_.udp_client_sockets().size()) {
283 EXPECT_TRUE(false);
284 return 0;
287 IPEndPoint endpoint;
288 socket_factory_.
289 udp_client_sockets()[socket_count]->GetLocalAddress(&endpoint);
290 int port = endpoint.port();
291 if (goaway_received) {
292 QuicGoAwayFrame goaway(QUIC_NO_ERROR, 1, "");
293 session->OnGoAway(goaway);
296 factory_.OnSessionClosed(session);
297 EXPECT_FALSE(HasActiveSession(destination));
298 EXPECT_TRUE(socket_data.AllReadDataConsumed());
299 EXPECT_TRUE(socket_data.AllWriteDataConsumed());
300 return port;
303 scoped_ptr<QuicEncryptedPacket> ConstructRstPacket() {
304 QuicStreamId stream_id = kClientDataStreamId1;
305 return maker_.MakeRstPacket(
306 1, true, stream_id,
307 AdjustErrorForVersion(QUIC_RST_ACKNOWLEDGEMENT, GetParam().version));
310 static ProofVerifyDetailsChromium DefaultProofVerifyDetails() {
311 // Load a certificate that is valid for www.example.org, mail.example.org,
312 // and mail.example.com.
313 scoped_refptr<X509Certificate> test_cert(
314 ImportCertFromFile(GetTestCertsDirectory(), "spdy_pooling.pem"));
315 EXPECT_TRUE(test_cert.get());
316 ProofVerifyDetailsChromium verify_details;
317 verify_details.cert_verify_result.verified_cert = test_cert;
318 verify_details.cert_verify_result.is_issued_by_known_root = true;
319 return verify_details;
322 MockQuicServerInfoFactory quic_server_info_factory_;
323 MockHostResolver host_resolver_;
324 DeterministicMockClientSocketFactory socket_factory_;
325 MockCryptoClientStreamFactory crypto_client_stream_factory_;
326 MockRandom random_generator_;
327 MockClock* clock_; // Owned by factory_.
328 scoped_refptr<TestTaskRunner> runner_;
329 QuicTestPacketMaker maker_;
330 scoped_ptr<CertVerifier> cert_verifier_;
331 scoped_ptr<ChannelIDService> channel_id_service_;
332 TransportSecurityState transport_security_state_;
333 QuicStreamFactory factory_;
334 HostPortPair host_port_pair_;
335 bool is_https_;
336 PrivacyMode privacy_mode_;
337 BoundNetLog net_log_;
338 TestCompletionCallback callback_;
341 INSTANTIATE_TEST_CASE_P(Version,
342 QuicStreamFactoryTest,
343 ::testing::ValuesIn(GetTestParams()));
345 TEST_P(QuicStreamFactoryTest, Create) {
346 MockRead reads[] = {
347 MockRead(ASYNC, OK, 0) // EOF
349 DeterministicSocketData socket_data(reads, arraysize(reads), nullptr, 0);
350 socket_factory_.AddSocketDataProvider(&socket_data);
351 socket_data.StopAfter(1);
353 QuicStreamRequest request(&factory_);
354 EXPECT_EQ(ERR_IO_PENDING,
355 request.Request(host_port_pair_, is_https_, privacy_mode_,
356 host_port_pair_.host(), "GET", net_log_,
357 callback_.callback()));
359 EXPECT_EQ(OK, callback_.WaitForResult());
360 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
361 EXPECT_TRUE(stream.get());
363 // Will reset stream 3.
364 stream = CreateFromSession(host_port_pair_);
365 EXPECT_TRUE(stream.get());
367 // TODO(rtenneti): We should probably have a tests that HTTP and HTTPS result
368 // in streams on different sessions.
369 QuicStreamRequest request2(&factory_);
370 EXPECT_EQ(OK, request2.Request(host_port_pair_, is_https_, privacy_mode_,
371 host_port_pair_.host(), "GET", 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.AllReadDataConsumed());
377 EXPECT_TRUE(socket_data.AllWriteDataConsumed());
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, request.Request(host_port_pair_, is_https_, privacy_mode_,
396 host_port_pair_.host(), "GET", net_log_,
397 callback_.callback()));
399 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
400 EXPECT_TRUE(stream.get());
401 EXPECT_TRUE(socket_data.AllReadDataConsumed());
402 EXPECT_TRUE(socket_data.AllWriteDataConsumed());
405 TEST_P(QuicStreamFactoryTest, CreateZeroRttPost) {
406 MockRead reads[] = {
407 MockRead(ASYNC, OK, 0) // EOF
409 DeterministicSocketData socket_data(reads, arraysize(reads), nullptr, 0);
410 socket_factory_.AddSocketDataProvider(&socket_data);
411 socket_data.StopAfter(1);
413 crypto_client_stream_factory_.set_handshake_mode(
414 MockCryptoClientStream::ZERO_RTT);
415 host_resolver_.set_synchronous_mode(true);
416 host_resolver_.rules()->AddIPLiteralRule(host_port_pair_.host(),
417 "192.168.0.1", "");
419 QuicStreamRequest request(&factory_);
420 // Posts require handshake confirmation, so this will return asynchronously.
421 EXPECT_EQ(ERR_IO_PENDING,
422 request.Request(host_port_pair_, is_https_, privacy_mode_,
423 host_port_pair_.host(), "POST", net_log_,
424 callback_.callback()));
426 // Confirm the handshake and verify that the stream is created.
427 crypto_client_stream_factory_.last_stream()->SendOnCryptoHandshakeEvent(
428 QuicSession::HANDSHAKE_CONFIRMED);
430 EXPECT_EQ(OK, callback_.WaitForResult());
431 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
432 EXPECT_TRUE(stream.get());
433 EXPECT_TRUE(socket_data.AllReadDataConsumed());
434 EXPECT_TRUE(socket_data.AllWriteDataConsumed());
437 TEST_P(QuicStreamFactoryTest, NoZeroRttForDifferentHost) {
438 MockRead reads[] = {
439 MockRead(ASYNC, OK, 0),
441 DeterministicSocketData socket_data(reads, arraysize(reads), nullptr, 0);
442 socket_factory_.AddSocketDataProvider(&socket_data);
443 socket_data.StopAfter(1);
445 crypto_client_stream_factory_.set_handshake_mode(
446 MockCryptoClientStream::ZERO_RTT);
447 host_resolver_.set_synchronous_mode(true);
448 host_resolver_.rules()->AddIPLiteralRule(host_port_pair_.host(),
449 "192.168.0.1", "");
451 QuicStreamRequest request(&factory_);
452 int rv = request.Request(host_port_pair_, is_https_, privacy_mode_,
453 "different.host.example.com", "GET", net_log_,
454 callback_.callback());
455 // If server and origin have different hostnames, then handshake confirmation
456 // should be required, so Request will return asynchronously.
457 EXPECT_EQ(ERR_IO_PENDING, rv);
458 // Confirm handshake.
459 crypto_client_stream_factory_.last_stream()->SendOnCryptoHandshakeEvent(
460 QuicSession::HANDSHAKE_CONFIRMED);
461 EXPECT_EQ(OK, callback_.WaitForResult());
463 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
464 EXPECT_TRUE(stream.get());
465 EXPECT_TRUE(socket_data.AllReadDataConsumed());
466 EXPECT_TRUE(socket_data.AllWriteDataConsumed());
469 TEST_P(QuicStreamFactoryTest, CreateHttpVsHttps) {
470 MockRead reads[] = {
471 MockRead(ASYNC, OK, 0) // EOF
473 DeterministicSocketData socket_data1(reads, arraysize(reads), nullptr, 0);
474 DeterministicSocketData socket_data2(reads, arraysize(reads), nullptr, 0);
475 socket_factory_.AddSocketDataProvider(&socket_data1);
476 socket_factory_.AddSocketDataProvider(&socket_data2);
477 socket_data1.StopAfter(1);
478 socket_data2.StopAfter(1);
480 QuicStreamRequest request(&factory_);
481 EXPECT_EQ(ERR_IO_PENDING,
482 request.Request(host_port_pair_, is_https_, privacy_mode_,
483 host_port_pair_.host(), "GET", net_log_,
484 callback_.callback()));
486 EXPECT_EQ(OK, callback_.WaitForResult());
487 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
488 EXPECT_TRUE(stream.get());
490 QuicStreamRequest request2(&factory_);
491 EXPECT_EQ(ERR_IO_PENDING,
492 request2.Request(host_port_pair_, !is_https_, privacy_mode_,
493 host_port_pair_.host(), "GET", net_log_,
494 callback_.callback()));
495 EXPECT_EQ(OK, callback_.WaitForResult());
496 stream = request2.ReleaseStream();
497 EXPECT_TRUE(stream.get());
498 stream.reset();
500 EXPECT_NE(QuicStreamFactoryPeer::GetActiveSession(
501 &factory_, host_port_pair_, is_https_),
502 QuicStreamFactoryPeer::GetActiveSession(
503 &factory_, host_port_pair_, !is_https_));
505 EXPECT_TRUE(socket_data1.AllReadDataConsumed());
506 EXPECT_TRUE(socket_data1.AllWriteDataConsumed());
507 EXPECT_TRUE(socket_data2.AllReadDataConsumed());
508 EXPECT_TRUE(socket_data2.AllWriteDataConsumed());
511 TEST_P(QuicStreamFactoryTest, Pooling) {
512 MockRead reads[] = {
513 MockRead(ASYNC, OK, 0) // EOF
515 DeterministicSocketData socket_data(reads, arraysize(reads), nullptr, 0);
516 socket_factory_.AddSocketDataProvider(&socket_data);
517 socket_data.StopAfter(1);
519 HostPortPair server2("mail.google.com", kDefaultServerPort);
520 host_resolver_.set_synchronous_mode(true);
521 host_resolver_.rules()->AddIPLiteralRule(
522 kDefaultServerHostName, "192.168.0.1", "");
523 host_resolver_.rules()->AddIPLiteralRule(
524 "mail.google.com", "192.168.0.1", "");
526 QuicStreamRequest request(&factory_);
527 EXPECT_EQ(OK, request.Request(host_port_pair_, is_https_, privacy_mode_,
528 host_port_pair_.host(), "GET", net_log_,
529 callback_.callback()));
530 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
531 EXPECT_TRUE(stream.get());
533 TestCompletionCallback callback;
534 QuicStreamRequest request2(&factory_);
535 EXPECT_EQ(OK,
536 request2.Request(server2, is_https_, privacy_mode_, server2.host(),
537 "GET", net_log_, callback.callback()));
538 scoped_ptr<QuicHttpStream> stream2 = request2.ReleaseStream();
539 EXPECT_TRUE(stream2.get());
541 EXPECT_EQ(
542 QuicStreamFactoryPeer::GetActiveSession(
543 &factory_, host_port_pair_, is_https_),
544 QuicStreamFactoryPeer::GetActiveSession(&factory_, server2, is_https_));
546 EXPECT_TRUE(socket_data.AllReadDataConsumed());
547 EXPECT_TRUE(socket_data.AllWriteDataConsumed());
550 TEST_P(QuicStreamFactoryTest, NoPoolingIfDisabled) {
551 MockRead reads[] = {
552 MockRead(ASYNC, OK, 0) // EOF
554 DeterministicSocketData socket_data1(reads, arraysize(reads), nullptr, 0);
555 DeterministicSocketData socket_data2(reads, arraysize(reads), nullptr, 0);
556 socket_factory_.AddSocketDataProvider(&socket_data1);
557 socket_factory_.AddSocketDataProvider(&socket_data2);
558 socket_data1.StopAfter(1);
559 socket_data2.StopAfter(1);
561 HostPortPair server2("mail.google.com", kDefaultServerPort);
562 host_resolver_.set_synchronous_mode(true);
563 host_resolver_.rules()->AddIPLiteralRule(
564 kDefaultServerHostName, "192.168.0.1", "");
565 host_resolver_.rules()->AddIPLiteralRule(
566 "mail.google.com", "192.168.0.1", "");
568 // Disable connection pooling.
569 QuicStreamFactoryPeer::DisableConnectionPooling(&factory_);
571 QuicStreamRequest request(&factory_);
572 EXPECT_EQ(OK, request.Request(host_port_pair_, is_https_, privacy_mode_,
573 host_port_pair_.host(), "GET", net_log_,
574 callback_.callback()));
575 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
576 EXPECT_TRUE(stream.get());
578 TestCompletionCallback callback;
579 QuicStreamRequest request2(&factory_);
580 EXPECT_EQ(OK,
581 request2.Request(server2, is_https_, privacy_mode_, server2.host(),
582 "GET", net_log_, callback.callback()));
583 scoped_ptr<QuicHttpStream> stream2 = request2.ReleaseStream();
584 EXPECT_TRUE(stream2.get());
586 EXPECT_NE(
587 QuicStreamFactoryPeer::GetActiveSession(
588 &factory_, host_port_pair_, is_https_),
589 QuicStreamFactoryPeer::GetActiveSession(&factory_, server2, is_https_));
591 EXPECT_TRUE(socket_data1.AllReadDataConsumed());
592 EXPECT_TRUE(socket_data1.AllWriteDataConsumed());
593 EXPECT_TRUE(socket_data2.AllReadDataConsumed());
594 EXPECT_TRUE(socket_data2.AllWriteDataConsumed());
597 TEST_P(QuicStreamFactoryTest, NoPoolingAfterGoAway) {
598 MockRead reads[] = {
599 MockRead(ASYNC, OK, 0) // EOF
601 DeterministicSocketData socket_data1(reads, arraysize(reads), nullptr, 0);
602 DeterministicSocketData socket_data2(reads, arraysize(reads), nullptr, 0);
603 socket_factory_.AddSocketDataProvider(&socket_data1);
604 socket_factory_.AddSocketDataProvider(&socket_data2);
605 socket_data1.StopAfter(1);
606 socket_data2.StopAfter(1);
608 HostPortPair server2("mail.google.com", kDefaultServerPort);
609 host_resolver_.set_synchronous_mode(true);
610 host_resolver_.rules()->AddIPLiteralRule(
611 kDefaultServerHostName, "192.168.0.1", "");
612 host_resolver_.rules()->AddIPLiteralRule(
613 "mail.google.com", "192.168.0.1", "");
615 QuicStreamRequest request(&factory_);
616 EXPECT_EQ(OK, request.Request(host_port_pair_, is_https_, privacy_mode_,
617 host_port_pair_.host(), "GET", net_log_,
618 callback_.callback()));
619 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
620 EXPECT_TRUE(stream.get());
622 TestCompletionCallback callback;
623 QuicStreamRequest request2(&factory_);
624 EXPECT_EQ(OK,
625 request2.Request(server2, is_https_, privacy_mode_, server2.host(),
626 "GET", net_log_, callback.callback()));
627 scoped_ptr<QuicHttpStream> stream2 = request2.ReleaseStream();
628 EXPECT_TRUE(stream2.get());
630 factory_.OnSessionGoingAway(QuicStreamFactoryPeer::GetActiveSession(
631 &factory_, host_port_pair_, is_https_));
632 EXPECT_FALSE(QuicStreamFactoryPeer::HasActiveSession(
633 &factory_, host_port_pair_, is_https_));
634 EXPECT_FALSE(QuicStreamFactoryPeer::HasActiveSession(
635 &factory_, server2, is_https_));
637 TestCompletionCallback callback3;
638 QuicStreamRequest request3(&factory_);
639 EXPECT_EQ(OK,
640 request3.Request(server2, is_https_, privacy_mode_, server2.host(),
641 "GET", net_log_, callback3.callback()));
642 scoped_ptr<QuicHttpStream> stream3 = request3.ReleaseStream();
643 EXPECT_TRUE(stream3.get());
645 EXPECT_TRUE(QuicStreamFactoryPeer::HasActiveSession(
646 &factory_, server2, is_https_));
648 EXPECT_TRUE(socket_data1.AllReadDataConsumed());
649 EXPECT_TRUE(socket_data1.AllWriteDataConsumed());
650 EXPECT_TRUE(socket_data2.AllReadDataConsumed());
651 EXPECT_TRUE(socket_data2.AllWriteDataConsumed());
654 TEST_P(QuicStreamFactoryTest, HttpsPooling) {
655 MockRead reads[] = {
656 MockRead(ASYNC, OK, 0) // EOF
658 DeterministicSocketData socket_data(reads, arraysize(reads), nullptr, 0);
659 socket_factory_.AddSocketDataProvider(&socket_data);
660 socket_data.StopAfter(1);
662 HostPortPair server1("www.example.org", 443);
663 HostPortPair server2("mail.example.org", 443);
665 ProofVerifyDetailsChromium verify_details = DefaultProofVerifyDetails();
666 crypto_client_stream_factory_.AddProofVerifyDetails(&verify_details);
668 host_resolver_.set_synchronous_mode(true);
669 host_resolver_.rules()->AddIPLiteralRule(server1.host(), "192.168.0.1", "");
670 host_resolver_.rules()->AddIPLiteralRule(server2.host(), "192.168.0.1", "");
672 QuicStreamRequest request(&factory_);
673 is_https_ = true;
674 EXPECT_EQ(OK,
675 request.Request(server1, is_https_, privacy_mode_, server1.host(),
676 "GET", net_log_, callback_.callback()));
677 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
678 EXPECT_TRUE(stream.get());
680 TestCompletionCallback callback;
681 QuicStreamRequest request2(&factory_);
682 EXPECT_EQ(OK,
683 request2.Request(server2, is_https_, privacy_mode_, server2.host(),
684 "GET", net_log_, callback_.callback()));
685 scoped_ptr<QuicHttpStream> stream2 = request2.ReleaseStream();
686 EXPECT_TRUE(stream2.get());
688 EXPECT_EQ(QuicStreamFactoryPeer::GetActiveSession(
689 &factory_, server1, is_https_),
690 QuicStreamFactoryPeer::GetActiveSession(
691 &factory_, server2, is_https_));
693 EXPECT_TRUE(socket_data.AllReadDataConsumed());
694 EXPECT_TRUE(socket_data.AllWriteDataConsumed());
697 TEST_P(QuicStreamFactoryTest, NoHttpsPoolingIfDisabled) {
698 MockRead reads[] = {
699 MockRead(ASYNC, OK, 0) // EOF
701 DeterministicSocketData socket_data1(reads, arraysize(reads), nullptr, 0);
702 DeterministicSocketData socket_data2(reads, arraysize(reads), nullptr, 0);
703 socket_factory_.AddSocketDataProvider(&socket_data1);
704 socket_factory_.AddSocketDataProvider(&socket_data2);
705 socket_data1.StopAfter(1);
706 socket_data2.StopAfter(1);
708 HostPortPair server1("www.example.org", 443);
709 HostPortPair server2("mail.example.org", 443);
711 ProofVerifyDetailsChromium verify_details = DefaultProofVerifyDetails();
712 crypto_client_stream_factory_.AddProofVerifyDetails(&verify_details);
714 host_resolver_.set_synchronous_mode(true);
715 host_resolver_.rules()->AddIPLiteralRule(server1.host(), "192.168.0.1", "");
716 host_resolver_.rules()->AddIPLiteralRule(server2.host(), "192.168.0.1", "");
718 // Disable connection pooling.
719 QuicStreamFactoryPeer::DisableConnectionPooling(&factory_);
721 QuicStreamRequest request(&factory_);
722 is_https_ = true;
723 EXPECT_EQ(OK,
724 request.Request(server1, is_https_, privacy_mode_, server1.host(),
725 "GET", net_log_, callback_.callback()));
726 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
727 EXPECT_TRUE(stream.get());
729 TestCompletionCallback callback;
730 QuicStreamRequest request2(&factory_);
731 EXPECT_EQ(OK,
732 request2.Request(server2, is_https_, privacy_mode_, server2.host(),
733 "GET", net_log_, callback_.callback()));
734 scoped_ptr<QuicHttpStream> stream2 = request2.ReleaseStream();
735 EXPECT_TRUE(stream2.get());
737 EXPECT_NE(QuicStreamFactoryPeer::GetActiveSession(
738 &factory_, server1, is_https_),
739 QuicStreamFactoryPeer::GetActiveSession(
740 &factory_, server2, is_https_));
742 EXPECT_TRUE(socket_data1.AllReadDataConsumed());
743 EXPECT_TRUE(socket_data1.AllWriteDataConsumed());
744 EXPECT_TRUE(socket_data2.AllReadDataConsumed());
745 EXPECT_TRUE(socket_data2.AllWriteDataConsumed());
748 class QuicAlternativeServiceCertificateValidationPooling
749 : public QuicStreamFactoryTest {
750 public:
751 void Run(bool valid) {
752 MockRead reads[] = {
753 MockRead(ASYNC, OK, 0) // EOF
755 DeterministicSocketData socket_data1(reads, arraysize(reads), nullptr, 0);
756 socket_factory_.AddSocketDataProvider(&socket_data1);
757 socket_data1.StopAfter(1);
759 HostPortPair server1("www.example.org", 443);
760 HostPortPair server2("mail.example.org", 443);
762 std::string origin_host(valid ? "mail.example.org" : "invalid.example.org");
763 HostPortPair alternative("www.example.org", 443);
765 ProofVerifyDetailsChromium verify_details = DefaultProofVerifyDetails();
766 bool common_name_fallback_used;
767 EXPECT_EQ(valid,
768 verify_details.cert_verify_result.verified_cert->VerifyNameMatch(
769 origin_host, &common_name_fallback_used));
770 EXPECT_TRUE(
771 verify_details.cert_verify_result.verified_cert->VerifyNameMatch(
772 alternative.host(), &common_name_fallback_used));
773 crypto_client_stream_factory_.AddProofVerifyDetails(&verify_details);
775 host_resolver_.set_synchronous_mode(true);
776 host_resolver_.rules()->AddIPLiteralRule(alternative.host(), "192.168.0.1",
777 "");
779 // Open first stream to alternative.
780 QuicStreamRequest request1(&factory_);
781 is_https_ = true;
782 EXPECT_EQ(OK, request1.Request(alternative, is_https_, privacy_mode_,
783 alternative.host(), "GET", net_log_,
784 callback_.callback()));
785 scoped_ptr<QuicHttpStream> stream1 = request1.ReleaseStream();
786 EXPECT_TRUE(stream1.get());
788 QuicStreamRequest request2(&factory_);
789 int rv =
790 request2.Request(alternative, is_https_, privacy_mode_, origin_host,
791 "GET", net_log_, callback_.callback());
792 if (valid) {
793 // Alternative service of origin to |alternative| should pool to session
794 // of |stream1| even if origin is different. Since only one
795 // SocketDataProvider is set up, the second request succeeding means that
796 // it pooled to the session opened by the first one.
797 EXPECT_EQ(OK, rv);
798 scoped_ptr<QuicHttpStream> stream2 = request2.ReleaseStream();
799 EXPECT_TRUE(stream2.get());
800 } else {
801 EXPECT_EQ(ERR_ALTERNATIVE_CERT_NOT_VALID_FOR_ORIGIN, rv);
804 EXPECT_TRUE(socket_data1.AllReadDataConsumed());
805 EXPECT_TRUE(socket_data1.AllWriteDataConsumed());
809 INSTANTIATE_TEST_CASE_P(Version,
810 QuicAlternativeServiceCertificateValidationPooling,
811 ::testing::ValuesIn(GetTestParams()));
813 TEST_P(QuicAlternativeServiceCertificateValidationPooling, Valid) {
814 Run(true);
817 TEST_P(QuicAlternativeServiceCertificateValidationPooling, Invalid) {
818 Run(false);
821 TEST_P(QuicStreamFactoryTest, HttpsPoolingWithMatchingPins) {
822 MockRead reads[] = {
823 MockRead(ASYNC, OK, 0) // EOF
825 DeterministicSocketData socket_data(reads, arraysize(reads), nullptr, 0);
826 socket_factory_.AddSocketDataProvider(&socket_data);
827 socket_data.StopAfter(1);
829 HostPortPair server1("www.example.org", 443);
830 HostPortPair server2("mail.example.org", 443);
831 uint8 primary_pin = 1;
832 uint8 backup_pin = 2;
833 test::AddPin(&transport_security_state_, "mail.example.org", primary_pin,
834 backup_pin);
836 ProofVerifyDetailsChromium verify_details = DefaultProofVerifyDetails();
837 verify_details.cert_verify_result.public_key_hashes.push_back(
838 test::GetTestHashValue(primary_pin));
839 crypto_client_stream_factory_.AddProofVerifyDetails(&verify_details);
841 host_resolver_.set_synchronous_mode(true);
842 host_resolver_.rules()->AddIPLiteralRule(server1.host(), "192.168.0.1", "");
843 host_resolver_.rules()->AddIPLiteralRule(server2.host(), "192.168.0.1", "");
845 QuicStreamRequest request(&factory_);
846 is_https_ = true;
847 EXPECT_EQ(OK,
848 request.Request(server1, is_https_, privacy_mode_, server1.host(),
849 "GET", net_log_, callback_.callback()));
850 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
851 EXPECT_TRUE(stream.get());
853 TestCompletionCallback callback;
854 QuicStreamRequest request2(&factory_);
855 EXPECT_EQ(OK,
856 request2.Request(server2, is_https_, privacy_mode_, server2.host(),
857 "GET", net_log_, callback_.callback()));
858 scoped_ptr<QuicHttpStream> stream2 = request2.ReleaseStream();
859 EXPECT_TRUE(stream2.get());
861 EXPECT_EQ(QuicStreamFactoryPeer::GetActiveSession(
862 &factory_, server1, is_https_),
863 QuicStreamFactoryPeer::GetActiveSession(
864 &factory_, server2, is_https_));
866 EXPECT_TRUE(socket_data.AllReadDataConsumed());
867 EXPECT_TRUE(socket_data.AllWriteDataConsumed());
870 TEST_P(QuicStreamFactoryTest, NoHttpsPoolingWithMatchingPinsIfDisabled) {
871 MockRead reads[] = {
872 MockRead(ASYNC, OK, 0) // EOF
874 DeterministicSocketData socket_data1(reads, arraysize(reads), nullptr, 0);
875 DeterministicSocketData socket_data2(reads, arraysize(reads), nullptr, 0);
876 socket_factory_.AddSocketDataProvider(&socket_data1);
877 socket_factory_.AddSocketDataProvider(&socket_data2);
878 socket_data1.StopAfter(1);
879 socket_data2.StopAfter(1);
881 HostPortPair server1("www.example.org", 443);
882 HostPortPair server2("mail.example.org", 443);
883 uint8 primary_pin = 1;
884 uint8 backup_pin = 2;
885 test::AddPin(&transport_security_state_, "mail.example.org", primary_pin,
886 backup_pin);
888 ProofVerifyDetailsChromium verify_details = DefaultProofVerifyDetails();
889 verify_details.cert_verify_result.public_key_hashes.push_back(
890 test::GetTestHashValue(primary_pin));
891 crypto_client_stream_factory_.AddProofVerifyDetails(&verify_details);
893 host_resolver_.set_synchronous_mode(true);
894 host_resolver_.rules()->AddIPLiteralRule(server1.host(), "192.168.0.1", "");
895 host_resolver_.rules()->AddIPLiteralRule(server2.host(), "192.168.0.1", "");
897 // Disable connection pooling.
898 QuicStreamFactoryPeer::DisableConnectionPooling(&factory_);
900 QuicStreamRequest request(&factory_);
901 is_https_ = true;
902 EXPECT_EQ(OK,
903 request.Request(server1, is_https_, privacy_mode_, server1.host(),
904 "GET", net_log_, callback_.callback()));
905 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
906 EXPECT_TRUE(stream.get());
908 TestCompletionCallback callback;
909 QuicStreamRequest request2(&factory_);
910 EXPECT_EQ(OK,
911 request2.Request(server2, is_https_, privacy_mode_, server2.host(),
912 "GET", net_log_, callback_.callback()));
913 scoped_ptr<QuicHttpStream> stream2 = request2.ReleaseStream();
914 EXPECT_TRUE(stream2.get());
916 EXPECT_NE(QuicStreamFactoryPeer::GetActiveSession(
917 &factory_, server1, is_https_),
918 QuicStreamFactoryPeer::GetActiveSession(
919 &factory_, server2, is_https_));
921 EXPECT_TRUE(socket_data1.AllReadDataConsumed());
922 EXPECT_TRUE(socket_data1.AllWriteDataConsumed());
923 EXPECT_TRUE(socket_data2.AllReadDataConsumed());
924 EXPECT_TRUE(socket_data2.AllWriteDataConsumed());
927 TEST_P(QuicStreamFactoryTest, NoHttpsPoolingWithDifferentPins) {
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 uint8 bad_pin = 3;
943 test::AddPin(&transport_security_state_, "mail.example.org", primary_pin,
944 backup_pin);
946 ProofVerifyDetailsChromium verify_details1 = DefaultProofVerifyDetails();
947 verify_details1.cert_verify_result.public_key_hashes.push_back(
948 test::GetTestHashValue(bad_pin));
949 crypto_client_stream_factory_.AddProofVerifyDetails(&verify_details1);
951 ProofVerifyDetailsChromium verify_details2 = DefaultProofVerifyDetails();
952 verify_details2.cert_verify_result.public_key_hashes.push_back(
953 test::GetTestHashValue(primary_pin));
954 crypto_client_stream_factory_.AddProofVerifyDetails(&verify_details2);
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 QuicStreamRequest request(&factory_);
961 is_https_ = true;
962 EXPECT_EQ(OK,
963 request.Request(server1, is_https_, privacy_mode_, server1.host(),
964 "GET", net_log_, callback_.callback()));
965 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
966 EXPECT_TRUE(stream.get());
968 TestCompletionCallback callback;
969 QuicStreamRequest request2(&factory_);
970 EXPECT_EQ(OK,
971 request2.Request(server2, is_https_, privacy_mode_, server2.host(),
972 "GET", net_log_, callback_.callback()));
973 scoped_ptr<QuicHttpStream> stream2 = request2.ReleaseStream();
974 EXPECT_TRUE(stream2.get());
976 EXPECT_NE(QuicStreamFactoryPeer::GetActiveSession(
977 &factory_, server1, is_https_),
978 QuicStreamFactoryPeer::GetActiveSession(
979 &factory_, server2, is_https_));
981 EXPECT_TRUE(socket_data1.AllReadDataConsumed());
982 EXPECT_TRUE(socket_data1.AllWriteDataConsumed());
983 EXPECT_TRUE(socket_data2.AllReadDataConsumed());
984 EXPECT_TRUE(socket_data2.AllWriteDataConsumed());
987 TEST_P(QuicStreamFactoryTest, Goaway) {
988 MockRead reads[] = {
989 MockRead(ASYNC, OK, 0) // EOF
991 DeterministicSocketData socket_data(reads, arraysize(reads), nullptr, 0);
992 socket_data.StopAfter(1);
993 socket_factory_.AddSocketDataProvider(&socket_data);
994 DeterministicSocketData socket_data2(reads, arraysize(reads), nullptr, 0);
995 socket_data2.StopAfter(1);
996 socket_factory_.AddSocketDataProvider(&socket_data2);
998 QuicStreamRequest request(&factory_);
999 EXPECT_EQ(ERR_IO_PENDING,
1000 request.Request(host_port_pair_, is_https_, privacy_mode_,
1001 host_port_pair_.host(), "GET", net_log_,
1002 callback_.callback()));
1004 EXPECT_EQ(OK, callback_.WaitForResult());
1005 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
1006 EXPECT_TRUE(stream.get());
1008 // Mark the session as going away. Ensure that while it is still alive
1009 // that it is no longer active.
1010 QuicClientSession* session = QuicStreamFactoryPeer::GetActiveSession(
1011 &factory_, host_port_pair_, is_https_);
1012 factory_.OnSessionGoingAway(session);
1013 EXPECT_EQ(true, QuicStreamFactoryPeer::IsLiveSession(&factory_, session));
1014 EXPECT_FALSE(QuicStreamFactoryPeer::HasActiveSession(
1015 &factory_, host_port_pair_, is_https_));
1016 EXPECT_FALSE(HasActiveSession(host_port_pair_));
1018 // Create a new request for the same destination and verify that a
1019 // new session is created.
1020 QuicStreamRequest request2(&factory_);
1021 EXPECT_EQ(ERR_IO_PENDING,
1022 request2.Request(host_port_pair_, is_https_, privacy_mode_,
1023 host_port_pair_.host(), "GET", net_log_,
1024 callback_.callback()));
1025 EXPECT_EQ(OK, callback_.WaitForResult());
1026 scoped_ptr<QuicHttpStream> stream2 = request2.ReleaseStream();
1027 EXPECT_TRUE(stream2.get());
1029 EXPECT_TRUE(QuicStreamFactoryPeer::HasActiveSession(&factory_,
1030 host_port_pair_,
1031 is_https_));
1032 EXPECT_NE(session,
1033 QuicStreamFactoryPeer::GetActiveSession(
1034 &factory_, host_port_pair_, is_https_));
1035 EXPECT_EQ(true, QuicStreamFactoryPeer::IsLiveSession(&factory_, session));
1037 stream2.reset();
1038 stream.reset();
1040 EXPECT_TRUE(socket_data.AllReadDataConsumed());
1041 EXPECT_TRUE(socket_data.AllWriteDataConsumed());
1042 EXPECT_TRUE(socket_data2.AllReadDataConsumed());
1043 EXPECT_TRUE(socket_data2.AllWriteDataConsumed());
1046 TEST_P(QuicStreamFactoryTest, MaxOpenStream) {
1047 MockRead reads[] = {
1048 MockRead(ASYNC, OK, 0) // EOF
1050 QuicStreamId stream_id = kClientDataStreamId1;
1051 scoped_ptr<QuicEncryptedPacket> rst(
1052 maker_.MakeRstPacket(1, true, stream_id, QUIC_STREAM_CANCELLED));
1053 MockWrite writes[] = {
1054 MockWrite(ASYNC, rst->data(), rst->length(), 1),
1056 DeterministicSocketData socket_data(reads, arraysize(reads),
1057 writes, arraysize(writes));
1058 socket_factory_.AddSocketDataProvider(&socket_data);
1059 socket_data.StopAfter(1);
1061 HttpRequestInfo request_info;
1062 std::vector<QuicHttpStream*> streams;
1063 // The MockCryptoClientStream sets max_open_streams to be
1064 // kDefaultMaxStreamsPerConnection / 2.
1065 for (size_t i = 0; i < kDefaultMaxStreamsPerConnection / 2; i++) {
1066 QuicStreamRequest request(&factory_);
1067 int rv = request.Request(host_port_pair_, is_https_, privacy_mode_,
1068 host_port_pair_.host(), "GET", net_log_,
1069 callback_.callback());
1070 if (i == 0) {
1071 EXPECT_EQ(ERR_IO_PENDING, rv);
1072 EXPECT_EQ(OK, callback_.WaitForResult());
1073 } else {
1074 EXPECT_EQ(OK, rv);
1076 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
1077 EXPECT_TRUE(stream);
1078 EXPECT_EQ(OK, stream->InitializeStream(
1079 &request_info, DEFAULT_PRIORITY, net_log_, CompletionCallback()));
1080 streams.push_back(stream.release());
1083 QuicStreamRequest request(&factory_);
1084 EXPECT_EQ(OK, request.Request(host_port_pair_, is_https_, privacy_mode_,
1085 host_port_pair_.host(), "GET", net_log_,
1086 CompletionCallback()));
1087 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
1088 EXPECT_TRUE(stream);
1089 EXPECT_EQ(ERR_IO_PENDING, stream->InitializeStream(
1090 &request_info, DEFAULT_PRIORITY, net_log_, callback_.callback()));
1092 // Close the first stream.
1093 streams.front()->Close(false);
1095 ASSERT_TRUE(callback_.have_result());
1097 EXPECT_EQ(OK, callback_.WaitForResult());
1099 EXPECT_TRUE(socket_data.AllReadDataConsumed());
1100 EXPECT_TRUE(socket_data.AllWriteDataConsumed());
1101 STLDeleteElements(&streams);
1104 TEST_P(QuicStreamFactoryTest, ResolutionErrorInCreate) {
1105 DeterministicSocketData socket_data(nullptr, 0, nullptr, 0);
1106 socket_factory_.AddSocketDataProvider(&socket_data);
1108 host_resolver_.rules()->AddSimulatedFailure(kDefaultServerHostName);
1110 QuicStreamRequest request(&factory_);
1111 EXPECT_EQ(ERR_IO_PENDING,
1112 request.Request(host_port_pair_, is_https_, privacy_mode_,
1113 host_port_pair_.host(), "GET", net_log_,
1114 callback_.callback()));
1116 EXPECT_EQ(ERR_NAME_NOT_RESOLVED, callback_.WaitForResult());
1118 EXPECT_TRUE(socket_data.AllReadDataConsumed());
1119 EXPECT_TRUE(socket_data.AllWriteDataConsumed());
1122 TEST_P(QuicStreamFactoryTest, ConnectErrorInCreate) {
1123 MockConnect connect(SYNCHRONOUS, ERR_ADDRESS_IN_USE);
1124 DeterministicSocketData socket_data(nullptr, 0, nullptr, 0);
1125 socket_data.set_connect_data(connect);
1126 socket_factory_.AddSocketDataProvider(&socket_data);
1127 socket_data.StopAfter(1);
1129 QuicStreamRequest request(&factory_);
1130 EXPECT_EQ(ERR_IO_PENDING,
1131 request.Request(host_port_pair_, is_https_, privacy_mode_,
1132 host_port_pair_.host(), "GET", net_log_,
1133 callback_.callback()));
1135 EXPECT_EQ(ERR_ADDRESS_IN_USE, callback_.WaitForResult());
1137 EXPECT_TRUE(socket_data.AllReadDataConsumed());
1138 EXPECT_TRUE(socket_data.AllWriteDataConsumed());
1141 TEST_P(QuicStreamFactoryTest, CancelCreate) {
1142 MockRead reads[] = {
1143 MockRead(ASYNC, OK, 0) // EOF
1145 DeterministicSocketData socket_data(reads, arraysize(reads), nullptr, 0);
1146 socket_factory_.AddSocketDataProvider(&socket_data);
1148 QuicStreamRequest request(&factory_);
1149 EXPECT_EQ(ERR_IO_PENDING,
1150 request.Request(host_port_pair_, is_https_, privacy_mode_,
1151 host_port_pair_.host(), "GET", net_log_,
1152 callback_.callback()));
1155 socket_data.StopAfter(1);
1156 base::RunLoop run_loop;
1157 run_loop.RunUntilIdle();
1159 scoped_ptr<QuicHttpStream> stream(CreateFromSession(host_port_pair_));
1160 EXPECT_TRUE(stream.get());
1161 stream.reset();
1163 EXPECT_TRUE(socket_data.AllReadDataConsumed());
1164 EXPECT_TRUE(socket_data.AllWriteDataConsumed());
1167 TEST_P(QuicStreamFactoryTest, CreateConsistentEphemeralPort) {
1168 // Sequentially connect to the default host, then another host, and then the
1169 // default host. Verify that the default host gets a consistent ephemeral
1170 // port, that is different from the other host's connection.
1172 std::string other_server_name = "other.google.com";
1173 EXPECT_NE(kDefaultServerHostName, other_server_name);
1174 HostPortPair host_port_pair2(other_server_name, kDefaultServerPort);
1176 int original_port = GetSourcePortForNewSession(host_port_pair_);
1177 EXPECT_NE(original_port, GetSourcePortForNewSession(host_port_pair2));
1178 EXPECT_EQ(original_port, GetSourcePortForNewSession(host_port_pair_));
1181 TEST_P(QuicStreamFactoryTest, GoAwayDisablesConsistentEphemeralPort) {
1182 // Get a session to the host using the port suggester.
1183 int original_port =
1184 GetSourcePortForNewSessionAndGoAway(host_port_pair_);
1185 // Verify that the port is different after the goaway.
1186 EXPECT_NE(original_port, GetSourcePortForNewSession(host_port_pair_));
1187 // Since the previous session did not goaway we should see the original port.
1188 EXPECT_EQ(original_port, GetSourcePortForNewSession(host_port_pair_));
1191 TEST_P(QuicStreamFactoryTest, CloseAllSessions) {
1192 MockRead reads[] = {
1193 MockRead(ASYNC, 0, 0) // EOF
1195 scoped_ptr<QuicEncryptedPacket> rst(ConstructRstPacket());
1196 std::vector<MockWrite> writes;
1197 writes.push_back(MockWrite(ASYNC, rst->data(), rst->length(), 1));
1198 DeterministicSocketData socket_data(reads, arraysize(reads),
1199 writes.empty() ? nullptr : &writes[0],
1200 writes.size());
1201 socket_factory_.AddSocketDataProvider(&socket_data);
1202 socket_data.StopAfter(1);
1204 MockRead reads2[] = {
1205 MockRead(ASYNC, 0, 0) // EOF
1207 DeterministicSocketData socket_data2(reads2, arraysize(reads2), nullptr, 0);
1208 socket_factory_.AddSocketDataProvider(&socket_data2);
1209 socket_data2.StopAfter(1);
1211 QuicStreamRequest request(&factory_);
1212 EXPECT_EQ(ERR_IO_PENDING,
1213 request.Request(host_port_pair_, is_https_, privacy_mode_,
1214 host_port_pair_.host(), "GET", net_log_,
1215 callback_.callback()));
1217 EXPECT_EQ(OK, callback_.WaitForResult());
1218 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
1219 HttpRequestInfo request_info;
1220 EXPECT_EQ(OK, stream->InitializeStream(&request_info,
1221 DEFAULT_PRIORITY,
1222 net_log_, CompletionCallback()));
1224 // Close the session and verify that stream saw the error.
1225 factory_.CloseAllSessions(ERR_INTERNET_DISCONNECTED);
1226 EXPECT_EQ(ERR_INTERNET_DISCONNECTED,
1227 stream->ReadResponseHeaders(callback_.callback()));
1229 // Now attempting to request a stream to the same origin should create
1230 // a new session.
1232 QuicStreamRequest request2(&factory_);
1233 EXPECT_EQ(ERR_IO_PENDING,
1234 request2.Request(host_port_pair_, is_https_, privacy_mode_,
1235 host_port_pair_.host(), "GET", net_log_,
1236 callback_.callback()));
1238 EXPECT_EQ(OK, callback_.WaitForResult());
1239 stream = request2.ReleaseStream();
1240 stream.reset(); // Will reset stream 3.
1242 EXPECT_TRUE(socket_data.AllReadDataConsumed());
1243 EXPECT_TRUE(socket_data.AllWriteDataConsumed());
1244 EXPECT_TRUE(socket_data2.AllReadDataConsumed());
1245 EXPECT_TRUE(socket_data2.AllWriteDataConsumed());
1248 TEST_P(QuicStreamFactoryTest, OnIPAddressChanged) {
1249 MockRead reads[] = {
1250 MockRead(ASYNC, 0, 0) // EOF
1252 scoped_ptr<QuicEncryptedPacket> rst(ConstructRstPacket());
1253 std::vector<MockWrite> writes;
1254 writes.push_back(MockWrite(ASYNC, rst->data(), rst->length(), 1));
1255 DeterministicSocketData socket_data(reads, arraysize(reads),
1256 writes.empty() ? nullptr : &writes[0],
1257 writes.size());
1258 socket_factory_.AddSocketDataProvider(&socket_data);
1259 socket_data.StopAfter(1);
1261 MockRead reads2[] = {
1262 MockRead(ASYNC, 0, 0) // EOF
1264 DeterministicSocketData socket_data2(reads2, arraysize(reads2), nullptr, 0);
1265 socket_factory_.AddSocketDataProvider(&socket_data2);
1266 socket_data2.StopAfter(1);
1268 QuicStreamRequest request(&factory_);
1269 EXPECT_EQ(ERR_IO_PENDING,
1270 request.Request(host_port_pair_, is_https_, privacy_mode_,
1271 host_port_pair_.host(), "GET", net_log_,
1272 callback_.callback()));
1274 EXPECT_EQ(OK, callback_.WaitForResult());
1275 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
1276 HttpRequestInfo request_info;
1277 EXPECT_EQ(OK, stream->InitializeStream(&request_info,
1278 DEFAULT_PRIORITY,
1279 net_log_, CompletionCallback()));
1281 // Change the IP address and verify that stream saw the error.
1282 factory_.OnIPAddressChanged();
1283 EXPECT_EQ(ERR_NETWORK_CHANGED,
1284 stream->ReadResponseHeaders(callback_.callback()));
1285 EXPECT_TRUE(factory_.require_confirmation());
1287 // Now attempting to request a stream to the same origin should create
1288 // a new session.
1290 QuicStreamRequest request2(&factory_);
1291 EXPECT_EQ(ERR_IO_PENDING,
1292 request2.Request(host_port_pair_, is_https_, privacy_mode_,
1293 host_port_pair_.host(), "GET", net_log_,
1294 callback_.callback()));
1296 EXPECT_EQ(OK, callback_.WaitForResult());
1297 stream = request2.ReleaseStream();
1298 stream.reset(); // Will reset stream 3.
1300 EXPECT_TRUE(socket_data.AllReadDataConsumed());
1301 EXPECT_TRUE(socket_data.AllWriteDataConsumed());
1302 EXPECT_TRUE(socket_data2.AllReadDataConsumed());
1303 EXPECT_TRUE(socket_data2.AllWriteDataConsumed());
1306 TEST_P(QuicStreamFactoryTest, OnCertAdded) {
1307 MockRead reads[] = {
1308 MockRead(ASYNC, 0, 0) // EOF
1310 scoped_ptr<QuicEncryptedPacket> rst(ConstructRstPacket());
1311 std::vector<MockWrite> writes;
1312 writes.push_back(MockWrite(ASYNC, rst->data(), rst->length(), 1));
1313 DeterministicSocketData socket_data(reads, arraysize(reads),
1314 writes.empty() ? nullptr : &writes[0],
1315 writes.size());
1316 socket_factory_.AddSocketDataProvider(&socket_data);
1317 socket_data.StopAfter(1);
1319 MockRead reads2[] = {
1320 MockRead(ASYNC, 0, 0) // EOF
1322 DeterministicSocketData socket_data2(reads2, arraysize(reads2), nullptr, 0);
1323 socket_factory_.AddSocketDataProvider(&socket_data2);
1324 socket_data2.StopAfter(1);
1326 QuicStreamRequest request(&factory_);
1327 EXPECT_EQ(ERR_IO_PENDING,
1328 request.Request(host_port_pair_, is_https_, privacy_mode_,
1329 host_port_pair_.host(), "GET", net_log_,
1330 callback_.callback()));
1332 EXPECT_EQ(OK, callback_.WaitForResult());
1333 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
1334 HttpRequestInfo request_info;
1335 EXPECT_EQ(OK, stream->InitializeStream(&request_info,
1336 DEFAULT_PRIORITY,
1337 net_log_, CompletionCallback()));
1339 // Add a cert and verify that stream saw the event.
1340 factory_.OnCertAdded(nullptr);
1341 EXPECT_EQ(ERR_CERT_DATABASE_CHANGED,
1342 stream->ReadResponseHeaders(callback_.callback()));
1343 EXPECT_FALSE(factory_.require_confirmation());
1345 // Now attempting to request a stream to the same origin should create
1346 // a new session.
1348 QuicStreamRequest request2(&factory_);
1349 EXPECT_EQ(ERR_IO_PENDING,
1350 request2.Request(host_port_pair_, is_https_, privacy_mode_,
1351 host_port_pair_.host(), "GET", net_log_,
1352 callback_.callback()));
1354 EXPECT_EQ(OK, callback_.WaitForResult());
1355 stream = request2.ReleaseStream();
1356 stream.reset(); // Will reset stream 3.
1358 EXPECT_TRUE(socket_data.AllReadDataConsumed());
1359 EXPECT_TRUE(socket_data.AllWriteDataConsumed());
1360 EXPECT_TRUE(socket_data2.AllReadDataConsumed());
1361 EXPECT_TRUE(socket_data2.AllWriteDataConsumed());
1364 TEST_P(QuicStreamFactoryTest, OnCACertChanged) {
1365 MockRead reads[] = {
1366 MockRead(ASYNC, 0, 0) // EOF
1368 scoped_ptr<QuicEncryptedPacket> rst(ConstructRstPacket());
1369 std::vector<MockWrite> writes;
1370 writes.push_back(MockWrite(ASYNC, rst->data(), rst->length(), 1));
1371 DeterministicSocketData socket_data(reads, arraysize(reads),
1372 writes.empty() ? nullptr : &writes[0],
1373 writes.size());
1374 socket_factory_.AddSocketDataProvider(&socket_data);
1375 socket_data.StopAfter(1);
1377 MockRead reads2[] = {
1378 MockRead(ASYNC, 0, 0) // EOF
1380 DeterministicSocketData socket_data2(reads2, arraysize(reads2), nullptr, 0);
1381 socket_factory_.AddSocketDataProvider(&socket_data2);
1382 socket_data2.StopAfter(1);
1384 QuicStreamRequest request(&factory_);
1385 EXPECT_EQ(ERR_IO_PENDING,
1386 request.Request(host_port_pair_, is_https_, privacy_mode_,
1387 host_port_pair_.host(), "GET", net_log_,
1388 callback_.callback()));
1390 EXPECT_EQ(OK, callback_.WaitForResult());
1391 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
1392 HttpRequestInfo request_info;
1393 EXPECT_EQ(OK, stream->InitializeStream(&request_info,
1394 DEFAULT_PRIORITY,
1395 net_log_, CompletionCallback()));
1397 // Change the CA cert and verify that stream saw the event.
1398 factory_.OnCACertChanged(nullptr);
1399 EXPECT_EQ(ERR_CERT_DATABASE_CHANGED,
1400 stream->ReadResponseHeaders(callback_.callback()));
1401 EXPECT_FALSE(factory_.require_confirmation());
1403 // Now attempting to request a stream to the same origin should create
1404 // a new session.
1406 QuicStreamRequest request2(&factory_);
1407 EXPECT_EQ(ERR_IO_PENDING,
1408 request2.Request(host_port_pair_, is_https_, privacy_mode_,
1409 host_port_pair_.host(), "GET", net_log_,
1410 callback_.callback()));
1412 EXPECT_EQ(OK, callback_.WaitForResult());
1413 stream = request2.ReleaseStream();
1414 stream.reset(); // Will reset stream 3.
1416 EXPECT_TRUE(socket_data.AllReadDataConsumed());
1417 EXPECT_TRUE(socket_data.AllWriteDataConsumed());
1418 EXPECT_TRUE(socket_data2.AllReadDataConsumed());
1419 EXPECT_TRUE(socket_data2.AllWriteDataConsumed());
1422 TEST_P(QuicStreamFactoryTest, SharedCryptoConfig) {
1423 vector<string> cannoncial_suffixes;
1424 cannoncial_suffixes.push_back(string(".c.youtube.com"));
1425 cannoncial_suffixes.push_back(string(".googlevideo.com"));
1427 for (unsigned i = 0; i < cannoncial_suffixes.size(); ++i) {
1428 string r1_host_name("r1");
1429 string r2_host_name("r2");
1430 r1_host_name.append(cannoncial_suffixes[i]);
1431 r2_host_name.append(cannoncial_suffixes[i]);
1433 HostPortPair host_port_pair1(r1_host_name, 80);
1434 QuicCryptoClientConfig* crypto_config =
1435 QuicStreamFactoryPeer::GetCryptoConfig(&factory_);
1436 QuicServerId server_id1(host_port_pair1, is_https_, privacy_mode_);
1437 QuicCryptoClientConfig::CachedState* cached1 =
1438 crypto_config->LookupOrCreate(server_id1);
1439 EXPECT_FALSE(cached1->proof_valid());
1440 EXPECT_TRUE(cached1->source_address_token().empty());
1442 // Mutate the cached1 to have different data.
1443 // TODO(rtenneti): mutate other members of CachedState.
1444 cached1->set_source_address_token(r1_host_name);
1445 cached1->SetProofValid();
1447 HostPortPair host_port_pair2(r2_host_name, 80);
1448 QuicServerId server_id2(host_port_pair2, is_https_, privacy_mode_);
1449 QuicCryptoClientConfig::CachedState* cached2 =
1450 crypto_config->LookupOrCreate(server_id2);
1451 EXPECT_EQ(cached1->source_address_token(), cached2->source_address_token());
1452 EXPECT_TRUE(cached2->proof_valid());
1456 TEST_P(QuicStreamFactoryTest, CryptoConfigWhenProofIsInvalid) {
1457 vector<string> cannoncial_suffixes;
1458 cannoncial_suffixes.push_back(string(".c.youtube.com"));
1459 cannoncial_suffixes.push_back(string(".googlevideo.com"));
1461 for (unsigned i = 0; i < cannoncial_suffixes.size(); ++i) {
1462 string r3_host_name("r3");
1463 string r4_host_name("r4");
1464 r3_host_name.append(cannoncial_suffixes[i]);
1465 r4_host_name.append(cannoncial_suffixes[i]);
1467 HostPortPair host_port_pair1(r3_host_name, 80);
1468 QuicCryptoClientConfig* crypto_config =
1469 QuicStreamFactoryPeer::GetCryptoConfig(&factory_);
1470 QuicServerId server_id1(host_port_pair1, is_https_, privacy_mode_);
1471 QuicCryptoClientConfig::CachedState* cached1 =
1472 crypto_config->LookupOrCreate(server_id1);
1473 EXPECT_FALSE(cached1->proof_valid());
1474 EXPECT_TRUE(cached1->source_address_token().empty());
1476 // Mutate the cached1 to have different data.
1477 // TODO(rtenneti): mutate other members of CachedState.
1478 cached1->set_source_address_token(r3_host_name);
1479 cached1->SetProofInvalid();
1481 HostPortPair host_port_pair2(r4_host_name, 80);
1482 QuicServerId server_id2(host_port_pair2, is_https_, privacy_mode_);
1483 QuicCryptoClientConfig::CachedState* cached2 =
1484 crypto_config->LookupOrCreate(server_id2);
1485 EXPECT_NE(cached1->source_address_token(), cached2->source_address_token());
1486 EXPECT_TRUE(cached2->source_address_token().empty());
1487 EXPECT_FALSE(cached2->proof_valid());
1491 TEST_P(QuicStreamFactoryTest, RacingConnections) {
1492 if (!GetParam().enable_connection_racing)
1493 return;
1494 factory_.set_quic_server_info_factory(&quic_server_info_factory_);
1495 QuicStreamFactoryPeer::SetTaskRunner(&factory_, runner_.get());
1496 MockRead reads[] = {
1497 MockRead(ASYNC, OK, 0) // EOF
1499 DeterministicSocketData socket_data(reads, arraysize(reads), nullptr, 0);
1500 socket_factory_.AddSocketDataProvider(&socket_data);
1501 socket_data.StopAfter(1);
1503 MockRead reads2[] = {
1504 MockRead(ASYNC, 0, 0) // EOF
1506 DeterministicSocketData socket_data2(reads2, arraysize(reads2), nullptr, 0);
1507 socket_factory_.AddSocketDataProvider(&socket_data2);
1508 socket_data2.StopAfter(1);
1510 crypto_client_stream_factory_.set_handshake_mode(
1511 MockCryptoClientStream::ZERO_RTT);
1512 host_resolver_.set_synchronous_mode(true);
1513 host_resolver_.rules()->AddIPLiteralRule(host_port_pair_.host(),
1514 "192.168.0.1", "");
1516 QuicStreamRequest request(&factory_);
1517 QuicServerId server_id(host_port_pair_, is_https_, privacy_mode_);
1518 EXPECT_EQ(ERR_IO_PENDING,
1519 request.Request(host_port_pair_, is_https_, privacy_mode_,
1520 host_port_pair_.host(), "GET", net_log_,
1521 callback_.callback()));
1522 EXPECT_EQ(2u,
1523 QuicStreamFactoryPeer::GetNumberOfActiveJobs(&factory_, server_id));
1525 runner_->RunNextTask();
1527 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
1528 EXPECT_TRUE(stream.get());
1529 EXPECT_TRUE(socket_data.AllReadDataConsumed());
1530 EXPECT_TRUE(socket_data.AllWriteDataConsumed());
1531 EXPECT_EQ(0u,
1532 QuicStreamFactoryPeer::GetNumberOfActiveJobs(&factory_, server_id));
1535 TEST_P(QuicStreamFactoryTest, EnableNotLoadFromDiskCache) {
1536 factory_.set_quic_server_info_factory(&quic_server_info_factory_);
1537 QuicStreamFactoryPeer::SetTaskRunner(&factory_, runner_.get());
1538 QuicStreamFactoryPeer::SetDisableDiskCache(&factory_, true);
1540 MockRead reads[] = {
1541 MockRead(ASYNC, OK, 0) // EOF
1543 DeterministicSocketData socket_data(reads, arraysize(reads), nullptr, 0);
1544 socket_factory_.AddSocketDataProvider(&socket_data);
1545 socket_data.StopAfter(1);
1547 crypto_client_stream_factory_.set_handshake_mode(
1548 MockCryptoClientStream::ZERO_RTT);
1549 host_resolver_.set_synchronous_mode(true);
1550 host_resolver_.rules()->AddIPLiteralRule(host_port_pair_.host(),
1551 "192.168.0.1", "");
1553 QuicStreamRequest request(&factory_);
1554 EXPECT_EQ(OK, request.Request(host_port_pair_, is_https_, privacy_mode_,
1555 host_port_pair_.host(), "GET", net_log_,
1556 callback_.callback()));
1558 // If we are waiting for disk cache, we would have posted a task. Verify that
1559 // the CancelWaitForDataReady task hasn't been posted.
1560 ASSERT_EQ(0u, runner_->GetPostedTasks().size());
1562 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
1563 EXPECT_TRUE(stream.get());
1564 EXPECT_TRUE(socket_data.AllReadDataConsumed());
1565 EXPECT_TRUE(socket_data.AllWriteDataConsumed());
1568 TEST_P(QuicStreamFactoryTest, BadPacketLoss) {
1569 factory_.set_quic_server_info_factory(&quic_server_info_factory_);
1570 QuicStreamFactoryPeer::SetTaskRunner(&factory_, runner_.get());
1571 QuicStreamFactoryPeer::SetDisableDiskCache(&factory_, true);
1572 QuicStreamFactoryPeer::SetMaxNumberOfLossyConnections(&factory_, 2);
1573 EXPECT_FALSE(
1574 QuicStreamFactoryPeer::IsQuicDisabled(&factory_, host_port_pair_.port()));
1575 EXPECT_EQ(0, QuicStreamFactoryPeer::GetNumberOfLossyConnections(
1576 &factory_, host_port_pair_.port()));
1578 MockRead reads[] = {
1579 MockRead(ASYNC, OK, 0) // EOF
1581 DeterministicSocketData socket_data(reads, arraysize(reads), nullptr, 0);
1582 socket_factory_.AddSocketDataProvider(&socket_data);
1583 socket_data.StopAfter(1);
1585 DeterministicSocketData socket_data2(reads, arraysize(reads), nullptr, 0);
1586 socket_factory_.AddSocketDataProvider(&socket_data2);
1587 socket_data2.StopAfter(1);
1589 DeterministicSocketData socket_data3(reads, arraysize(reads), nullptr, 0);
1590 socket_factory_.AddSocketDataProvider(&socket_data3);
1591 socket_data3.StopAfter(1);
1593 HostPortPair server2("mail.example.org", kDefaultServerPort);
1594 HostPortPair server3("docs.example.org", kDefaultServerPort);
1596 crypto_client_stream_factory_.set_handshake_mode(
1597 MockCryptoClientStream::ZERO_RTT);
1598 host_resolver_.set_synchronous_mode(true);
1599 host_resolver_.rules()->AddIPLiteralRule(host_port_pair_.host(),
1600 "192.168.0.1", "");
1601 host_resolver_.rules()->AddIPLiteralRule(server2.host(), "192.168.0.1", "");
1602 host_resolver_.rules()->AddIPLiteralRule(server3.host(), "192.168.0.1", "");
1604 QuicStreamRequest request(&factory_);
1605 EXPECT_EQ(OK, request.Request(host_port_pair_, is_https_, privacy_mode_,
1606 host_port_pair_.host(), "GET", net_log_,
1607 callback_.callback()));
1609 QuicClientSession* session = QuicStreamFactoryPeer::GetActiveSession(
1610 &factory_, host_port_pair_, is_https_);
1612 DVLOG(1) << "Create 1st session and test packet loss";
1614 // Set packet_loss_rate to a lower value than packet_loss_threshold.
1615 EXPECT_FALSE(
1616 factory_.OnHandshakeConfirmed(session, /*packet_loss_rate=*/0.9f));
1617 EXPECT_TRUE(session->connection()->connected());
1618 EXPECT_TRUE(QuicStreamFactoryPeer::HasActiveSession(
1619 &factory_, host_port_pair_, is_https_));
1620 EXPECT_FALSE(
1621 QuicStreamFactoryPeer::IsQuicDisabled(&factory_, host_port_pair_.port()));
1622 EXPECT_EQ(0, QuicStreamFactoryPeer::GetNumberOfLossyConnections(
1623 &factory_, host_port_pair_.port()));
1625 // Set packet_loss_rate to a higher value than packet_loss_threshold only once
1626 // and that should close the session, but shouldn't disable QUIC.
1627 EXPECT_TRUE(
1628 factory_.OnHandshakeConfirmed(session, /*packet_loss_rate=*/1.0f));
1629 EXPECT_EQ(1, QuicStreamFactoryPeer::GetNumberOfLossyConnections(
1630 &factory_, host_port_pair_.port()));
1631 EXPECT_FALSE(
1632 QuicStreamFactoryPeer::IsQuicDisabled(&factory_, host_port_pair_.port()));
1633 EXPECT_FALSE(QuicStreamFactoryPeer::HasActiveSession(
1634 &factory_, host_port_pair_, is_https_));
1635 EXPECT_FALSE(HasActiveSession(host_port_pair_));
1637 // Test N-in-a-row high packet loss connections.
1639 DVLOG(1) << "Create 2nd session and test packet loss";
1641 TestCompletionCallback callback2;
1642 QuicStreamRequest request2(&factory_);
1643 EXPECT_EQ(OK,
1644 request2.Request(server2, is_https_, privacy_mode_, server2.host(),
1645 "GET", net_log_, callback2.callback()));
1646 QuicClientSession* session2 =
1647 QuicStreamFactoryPeer::GetActiveSession(&factory_, server2, is_https_);
1649 // If there is no packet loss during handshake confirmation, number of lossy
1650 // connections for the port should be 0.
1651 EXPECT_EQ(1, QuicStreamFactoryPeer::GetNumberOfLossyConnections(
1652 &factory_, server2.port()));
1653 EXPECT_FALSE(
1654 factory_.OnHandshakeConfirmed(session2, /*packet_loss_rate=*/0.9f));
1655 EXPECT_EQ(0, QuicStreamFactoryPeer::GetNumberOfLossyConnections(
1656 &factory_, server2.port()));
1657 EXPECT_FALSE(
1658 QuicStreamFactoryPeer::IsQuicDisabled(&factory_, server2.port()));
1660 // Set packet_loss_rate to a higher value than packet_loss_threshold only once
1661 // and that should close the session, but shouldn't disable QUIC.
1662 EXPECT_TRUE(
1663 factory_.OnHandshakeConfirmed(session2, /*packet_loss_rate=*/1.0f));
1664 EXPECT_EQ(1, QuicStreamFactoryPeer::GetNumberOfLossyConnections(
1665 &factory_, server2.port()));
1666 EXPECT_FALSE(session2->connection()->connected());
1667 EXPECT_FALSE(
1668 QuicStreamFactoryPeer::IsQuicDisabled(&factory_, server2.port()));
1669 EXPECT_FALSE(
1670 QuicStreamFactoryPeer::HasActiveSession(&factory_, server2, is_https_));
1671 EXPECT_FALSE(HasActiveSession(server2));
1673 DVLOG(1) << "Create 3rd session which also has packet loss";
1675 TestCompletionCallback callback3;
1676 QuicStreamRequest request3(&factory_);
1677 EXPECT_EQ(OK,
1678 request3.Request(server3, is_https_, privacy_mode_, server3.host(),
1679 "GET", net_log_, callback3.callback()));
1680 QuicClientSession* session3 =
1681 QuicStreamFactoryPeer::GetActiveSession(&factory_, server3, is_https_);
1683 // Set packet_loss_rate to higher value than packet_loss_threshold 2nd time in
1684 // a row and that should close the session and disable QUIC.
1685 EXPECT_TRUE(
1686 factory_.OnHandshakeConfirmed(session3, /*packet_loss_rate=*/1.0f));
1687 EXPECT_EQ(2, QuicStreamFactoryPeer::GetNumberOfLossyConnections(
1688 &factory_, server3.port()));
1689 EXPECT_FALSE(session2->connection()->connected());
1690 EXPECT_TRUE(QuicStreamFactoryPeer::IsQuicDisabled(&factory_, server3.port()));
1691 EXPECT_FALSE(
1692 QuicStreamFactoryPeer::HasActiveSession(&factory_, server3, is_https_));
1693 EXPECT_FALSE(HasActiveSession(server3));
1695 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
1696 EXPECT_TRUE(stream.get());
1697 scoped_ptr<QuicHttpStream> stream2 = request2.ReleaseStream();
1698 EXPECT_TRUE(stream2.get());
1699 scoped_ptr<QuicHttpStream> stream3 = request3.ReleaseStream();
1700 EXPECT_TRUE(stream3.get());
1701 EXPECT_TRUE(socket_data.AllReadDataConsumed());
1702 EXPECT_TRUE(socket_data.AllWriteDataConsumed());
1703 EXPECT_TRUE(socket_data2.AllReadDataConsumed());
1704 EXPECT_TRUE(socket_data2.AllWriteDataConsumed());
1705 EXPECT_TRUE(socket_data3.AllReadDataConsumed());
1706 EXPECT_TRUE(socket_data3.AllWriteDataConsumed());
1709 } // namespace test
1710 } // namespace net