Snap pinch zoom gestures near the screen edge.
[chromium-blink-merge.git] / net / quic / quic_stream_factory_test.cc
blob195e410ff72517f6ee587ffc27ad0eae6460a58c
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "net/quic/quic_stream_factory.h"
7 #include "base/run_loop.h"
8 #include "base/strings/string_util.h"
9 #include "base/thread_task_runner_handle.h"
10 #include "net/base/test_data_directory.h"
11 #include "net/cert/cert_verifier.h"
12 #include "net/dns/mock_host_resolver.h"
13 #include "net/http/http_response_headers.h"
14 #include "net/http/http_response_info.h"
15 #include "net/http/http_util.h"
16 #include "net/http/transport_security_state.h"
17 #include "net/quic/crypto/crypto_handshake.h"
18 #include "net/quic/crypto/proof_verifier_chromium.h"
19 #include "net/quic/crypto/quic_decrypter.h"
20 #include "net/quic/crypto/quic_encrypter.h"
21 #include "net/quic/crypto/quic_server_info.h"
22 #include "net/quic/quic_http_stream.h"
23 #include "net/quic/quic_server_id.h"
24 #include "net/quic/test_tools/mock_clock.h"
25 #include "net/quic/test_tools/mock_crypto_client_stream_factory.h"
26 #include "net/quic/test_tools/mock_random.h"
27 #include "net/quic/test_tools/quic_test_packet_maker.h"
28 #include "net/quic/test_tools/quic_test_utils.h"
29 #include "net/quic/test_tools/test_task_runner.h"
30 #include "net/socket/socket_test_util.h"
31 #include "net/spdy/spdy_test_utils.h"
32 #include "net/ssl/channel_id_service.h"
33 #include "net/ssl/default_channel_id_store.h"
34 #include "net/test/cert_test_util.h"
35 #include "testing/gtest/include/gtest/gtest.h"
37 using base::StringPiece;
38 using std::ostream;
39 using std::string;
40 using std::vector;
42 namespace net {
43 namespace test {
45 namespace {
46 const char kDefaultServerHostName[] = "www.google.com";
47 const int kDefaultServerPort = 443;
49 // Run all tests with all the combinations of versions and
50 // enable_connection_racing.
51 struct TestParams {
52 TestParams(const QuicVersion version, bool enable_connection_racing)
53 : version(version), enable_connection_racing(enable_connection_racing) {}
55 friend ostream& operator<<(ostream& os, const TestParams& p) {
56 os << "{ version: " << QuicVersionToString(p.version);
57 os << " enable_connection_racing: " << p.enable_connection_racing << " }";
58 return os;
61 QuicVersion version;
62 bool enable_connection_racing;
65 // Constructs various test permutations.
66 vector<TestParams> GetTestParams() {
67 vector<TestParams> params;
68 QuicVersionVector all_supported_versions = QuicSupportedVersions();
69 for (const QuicVersion version : all_supported_versions) {
70 params.push_back(TestParams(version, false));
71 params.push_back(TestParams(version, true));
73 return params;
76 } // namespace anonymous
78 class QuicStreamFactoryPeer {
79 public:
80 static QuicCryptoClientConfig* GetCryptoConfig(QuicStreamFactory* factory) {
81 return &factory->crypto_config_;
84 static bool HasActiveSession(QuicStreamFactory* factory,
85 const HostPortPair& host_port_pair,
86 bool is_https) {
87 QuicServerId server_id(host_port_pair, is_https, PRIVACY_MODE_DISABLED);
88 return factory->HasActiveSession(server_id);
91 static QuicClientSession* GetActiveSession(
92 QuicStreamFactory* factory,
93 const HostPortPair& host_port_pair,
94 bool is_https) {
95 QuicServerId server_id(host_port_pair, is_https, PRIVACY_MODE_DISABLED);
96 DCHECK(factory->HasActiveSession(server_id));
97 return factory->active_sessions_[server_id];
100 static scoped_ptr<QuicHttpStream> CreateFromSession(
101 QuicStreamFactory* factory,
102 QuicClientSession* session) {
103 return factory->CreateFromSession(session);
106 static bool IsLiveSession(QuicStreamFactory* factory,
107 QuicClientSession* session) {
108 for (QuicStreamFactory::SessionIdMap::iterator it =
109 factory->all_sessions_.begin();
110 it != factory->all_sessions_.end(); ++it) {
111 if (it->first == session)
112 return true;
114 return false;
117 static void DisableConnectionPooling(QuicStreamFactory* factory) {
118 factory->disable_connection_pooling_ = true;
121 static void SetTaskRunner(QuicStreamFactory* factory,
122 base::TaskRunner* task_runner) {
123 factory->task_runner_ = task_runner;
126 static void SetEnableConnectionRacing(QuicStreamFactory* factory,
127 bool enable_connection_racing) {
128 factory->enable_connection_racing_ = enable_connection_racing;
131 static void SetDisableDiskCache(QuicStreamFactory* factory,
132 bool disable_disk_cache) {
133 factory->disable_disk_cache_ = disable_disk_cache;
136 static void SetMaxNumberOfLossyConnections(
137 QuicStreamFactory* factory,
138 int max_number_of_lossy_connections) {
139 factory->max_number_of_lossy_connections_ = max_number_of_lossy_connections;
142 static int GetNumberOfLossyConnections(QuicStreamFactory* factory,
143 uint16 port) {
144 return factory->number_of_lossy_connections_[port];
147 static bool IsQuicDisabled(QuicStreamFactory* factory, uint16 port) {
148 return factory->IsQuicDisabled(port);
151 static size_t GetNumberOfActiveJobs(QuicStreamFactory* factory,
152 const QuicServerId& server_id) {
153 return (factory->active_jobs_[server_id]).size();
157 class MockQuicServerInfo : public QuicServerInfo {
158 public:
159 MockQuicServerInfo(const QuicServerId& server_id)
160 : QuicServerInfo(server_id) {}
161 ~MockQuicServerInfo() override {}
163 void Start() override {}
165 int WaitForDataReady(const CompletionCallback& callback) override {
166 return ERR_IO_PENDING;
169 void ResetWaitForDataReadyCallback() override {}
171 void CancelWaitForDataReadyCallback() override {}
173 bool IsDataReady() override { return false; }
175 bool IsReadyToPersist() override { return false; }
177 void Persist() override {}
179 void OnExternalCacheHit() override {}
182 class MockQuicServerInfoFactory : public QuicServerInfoFactory {
183 public:
184 MockQuicServerInfoFactory() {}
185 ~MockQuicServerInfoFactory() override {}
187 QuicServerInfo* GetForServer(const QuicServerId& server_id) override {
188 return new MockQuicServerInfo(server_id);
192 class QuicStreamFactoryTest : public ::testing::TestWithParam<TestParams> {
193 protected:
194 QuicStreamFactoryTest()
195 : random_generator_(0),
196 clock_(new MockClock()),
197 runner_(new TestTaskRunner(clock_)),
198 maker_(GetParam().version, 0, clock_, kDefaultServerHostName),
199 cert_verifier_(CertVerifier::CreateDefault()),
200 channel_id_service_(
201 new ChannelIDService(new DefaultChannelIDStore(nullptr),
202 base::ThreadTaskRunnerHandle::Get())),
203 factory_(&host_resolver_,
204 &socket_factory_,
205 base::WeakPtr<HttpServerProperties>(),
206 cert_verifier_.get(),
207 channel_id_service_.get(),
208 &transport_security_state_,
209 &crypto_client_stream_factory_,
210 &random_generator_,
211 clock_,
212 kDefaultMaxPacketSize,
213 std::string(),
214 SupportedVersions(GetParam().version),
215 /*enable_port_selection=*/true,
216 /*always_require_handshake_confirmation=*/false,
217 /*disable_connection_pooling=*/false,
218 /*load_server_info_timeout_srtt_multiplier=*/0.0f,
219 /*enable_connection_racing=*/false,
220 /*enable_non_blocking_io=*/true,
221 /*disable_disk_cache=*/false,
222 /*prefer_aes=*/false,
223 /*max_number_of_lossy_connections=*/0,
224 /*packet_loss_threshold=*/1.0f,
225 /*receive_buffer_size=*/0,
226 QuicTagVector()),
227 host_port_pair_(kDefaultServerHostName, kDefaultServerPort),
228 is_https_(false),
229 privacy_mode_(PRIVACY_MODE_DISABLED) {
230 factory_.set_require_confirmation(false);
231 clock_->AdvanceTime(QuicTime::Delta::FromSeconds(1));
232 QuicStreamFactoryPeer::SetEnableConnectionRacing(
233 &factory_, GetParam().enable_connection_racing);
236 bool HasActiveSession(const HostPortPair& host_port_pair) {
237 return QuicStreamFactoryPeer::HasActiveSession(&factory_, host_port_pair,
238 /*is_https_=*/false);
241 scoped_ptr<QuicHttpStream> CreateFromSession(
242 const HostPortPair& host_port_pair) {
243 QuicClientSession* session = QuicStreamFactoryPeer::GetActiveSession(
244 &factory_, host_port_pair, /*is_https=*/false);
245 return QuicStreamFactoryPeer::CreateFromSession(&factory_, session);
248 int GetSourcePortForNewSession(const HostPortPair& destination) {
249 return GetSourcePortForNewSessionInner(destination, false);
252 int GetSourcePortForNewSessionAndGoAway(
253 const HostPortPair& destination) {
254 return GetSourcePortForNewSessionInner(destination, true);
257 int GetSourcePortForNewSessionInner(const HostPortPair& destination,
258 bool goaway_received) {
259 // Should only be called if there is no active session for this destination.
260 EXPECT_FALSE(HasActiveSession(destination));
261 size_t socket_count = socket_factory_.udp_client_sockets().size();
263 MockRead reads[] = {
264 MockRead(ASYNC, OK, 0) // EOF
266 DeterministicSocketData socket_data(reads, arraysize(reads), nullptr, 0);
267 socket_data.StopAfter(1);
268 socket_factory_.AddSocketDataProvider(&socket_data);
270 QuicStreamRequest request(&factory_);
271 EXPECT_EQ(ERR_IO_PENDING,
272 request.Request(destination, is_https_, privacy_mode_,
273 /*cert_verify_flags=*/0, destination.host(),
274 "GET", net_log_, callback_.callback()));
276 EXPECT_EQ(OK, callback_.WaitForResult());
277 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
278 EXPECT_TRUE(stream.get());
279 stream.reset();
281 QuicClientSession* session = QuicStreamFactoryPeer::GetActiveSession(
282 &factory_, destination, is_https_);
284 if (socket_count + 1 != socket_factory_.udp_client_sockets().size()) {
285 EXPECT_TRUE(false);
286 return 0;
289 IPEndPoint endpoint;
290 socket_factory_.
291 udp_client_sockets()[socket_count]->GetLocalAddress(&endpoint);
292 int port = endpoint.port();
293 if (goaway_received) {
294 QuicGoAwayFrame goaway(QUIC_NO_ERROR, 1, "");
295 session->OnGoAway(goaway);
298 factory_.OnSessionClosed(session);
299 EXPECT_FALSE(HasActiveSession(destination));
300 EXPECT_TRUE(socket_data.AllReadDataConsumed());
301 EXPECT_TRUE(socket_data.AllWriteDataConsumed());
302 return port;
305 scoped_ptr<QuicEncryptedPacket> ConstructRstPacket() {
306 QuicStreamId stream_id = kClientDataStreamId1;
307 return maker_.MakeRstPacket(
308 1, true, stream_id,
309 AdjustErrorForVersion(QUIC_RST_ACKNOWLEDGEMENT, GetParam().version));
312 static ProofVerifyDetailsChromium DefaultProofVerifyDetails() {
313 // Load a certificate that is valid for www.example.org, mail.example.org,
314 // and mail.example.com.
315 scoped_refptr<X509Certificate> test_cert(
316 ImportCertFromFile(GetTestCertsDirectory(), "spdy_pooling.pem"));
317 EXPECT_TRUE(test_cert.get());
318 ProofVerifyDetailsChromium verify_details;
319 verify_details.cert_verify_result.verified_cert = test_cert;
320 verify_details.cert_verify_result.is_issued_by_known_root = true;
321 return verify_details;
324 MockQuicServerInfoFactory quic_server_info_factory_;
325 MockHostResolver host_resolver_;
326 DeterministicMockClientSocketFactory socket_factory_;
327 MockCryptoClientStreamFactory crypto_client_stream_factory_;
328 MockRandom random_generator_;
329 MockClock* clock_; // Owned by factory_.
330 scoped_refptr<TestTaskRunner> runner_;
331 QuicTestPacketMaker maker_;
332 scoped_ptr<CertVerifier> cert_verifier_;
333 scoped_ptr<ChannelIDService> channel_id_service_;
334 TransportSecurityState transport_security_state_;
335 QuicStreamFactory factory_;
336 HostPortPair host_port_pair_;
337 bool is_https_;
338 PrivacyMode privacy_mode_;
339 BoundNetLog net_log_;
340 TestCompletionCallback callback_;
343 INSTANTIATE_TEST_CASE_P(Version,
344 QuicStreamFactoryTest,
345 ::testing::ValuesIn(GetTestParams()));
347 TEST_P(QuicStreamFactoryTest, Create) {
348 MockRead reads[] = {
349 MockRead(ASYNC, OK, 0) // EOF
351 DeterministicSocketData socket_data(reads, arraysize(reads), nullptr, 0);
352 socket_factory_.AddSocketDataProvider(&socket_data);
353 socket_data.StopAfter(1);
355 QuicStreamRequest request(&factory_);
356 EXPECT_EQ(ERR_IO_PENDING,
357 request.Request(host_port_pair_, is_https_, privacy_mode_,
358 /*cert_verify_flags=*/0, host_port_pair_.host(),
359 "GET", net_log_, callback_.callback()));
361 EXPECT_EQ(OK, callback_.WaitForResult());
362 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
363 EXPECT_TRUE(stream.get());
365 // Will reset stream 3.
366 stream = CreateFromSession(host_port_pair_);
367 EXPECT_TRUE(stream.get());
369 // TODO(rtenneti): We should probably have a tests that HTTP and HTTPS result
370 // in streams on different sessions.
371 QuicStreamRequest request2(&factory_);
372 EXPECT_EQ(OK,
373 request2.Request(host_port_pair_, is_https_, privacy_mode_,
374 /*cert_verify_flags=*/0, host_port_pair_.host(),
375 "GET", net_log_, callback_.callback()));
376 stream = request2.ReleaseStream(); // Will reset stream 5.
377 stream.reset(); // Will reset stream 7.
379 EXPECT_TRUE(socket_data.AllReadDataConsumed());
380 EXPECT_TRUE(socket_data.AllWriteDataConsumed());
383 TEST_P(QuicStreamFactoryTest, CreateZeroRtt) {
384 MockRead reads[] = {
385 MockRead(ASYNC, OK, 0) // EOF
387 DeterministicSocketData socket_data(reads, arraysize(reads), nullptr, 0);
388 socket_factory_.AddSocketDataProvider(&socket_data);
389 socket_data.StopAfter(1);
391 crypto_client_stream_factory_.set_handshake_mode(
392 MockCryptoClientStream::ZERO_RTT);
393 host_resolver_.set_synchronous_mode(true);
394 host_resolver_.rules()->AddIPLiteralRule(host_port_pair_.host(),
395 "192.168.0.1", "");
397 QuicStreamRequest request(&factory_);
398 EXPECT_EQ(OK, request.Request(host_port_pair_, is_https_, privacy_mode_,
399 /*cert_verify_flags=*/0, host_port_pair_.host(),
400 "GET", net_log_, callback_.callback()));
402 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
403 EXPECT_TRUE(stream.get());
404 EXPECT_TRUE(socket_data.AllReadDataConsumed());
405 EXPECT_TRUE(socket_data.AllWriteDataConsumed());
408 TEST_P(QuicStreamFactoryTest, CreateZeroRttPost) {
409 MockRead reads[] = {
410 MockRead(ASYNC, OK, 0) // EOF
412 DeterministicSocketData socket_data(reads, arraysize(reads), nullptr, 0);
413 socket_factory_.AddSocketDataProvider(&socket_data);
414 socket_data.StopAfter(1);
416 crypto_client_stream_factory_.set_handshake_mode(
417 MockCryptoClientStream::ZERO_RTT);
418 host_resolver_.set_synchronous_mode(true);
419 host_resolver_.rules()->AddIPLiteralRule(host_port_pair_.host(),
420 "192.168.0.1", "");
422 QuicStreamRequest request(&factory_);
423 // Posts require handshake confirmation, so this will return asynchronously.
424 EXPECT_EQ(ERR_IO_PENDING,
425 request.Request(host_port_pair_, is_https_, privacy_mode_,
426 /*cert_verify_flags=*/0, host_port_pair_.host(),
427 "POST", net_log_, callback_.callback()));
429 // Confirm the handshake and verify that the stream is created.
430 crypto_client_stream_factory_.last_stream()->SendOnCryptoHandshakeEvent(
431 QuicSession::HANDSHAKE_CONFIRMED);
433 EXPECT_EQ(OK, callback_.WaitForResult());
434 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
435 EXPECT_TRUE(stream.get());
436 EXPECT_TRUE(socket_data.AllReadDataConsumed());
437 EXPECT_TRUE(socket_data.AllWriteDataConsumed());
440 TEST_P(QuicStreamFactoryTest, NoZeroRttForDifferentHost) {
441 MockRead reads[] = {
442 MockRead(ASYNC, OK, 0),
444 DeterministicSocketData socket_data(reads, arraysize(reads), nullptr, 0);
445 socket_factory_.AddSocketDataProvider(&socket_data);
446 socket_data.StopAfter(1);
448 crypto_client_stream_factory_.set_handshake_mode(
449 MockCryptoClientStream::ZERO_RTT);
450 host_resolver_.set_synchronous_mode(true);
451 host_resolver_.rules()->AddIPLiteralRule(host_port_pair_.host(),
452 "192.168.0.1", "");
454 QuicStreamRequest request(&factory_);
455 int rv = request.Request(
456 host_port_pair_, is_https_, privacy_mode_, /*cert_verify_flags=*/0,
457 "different.host.example.com", "GET", net_log_, callback_.callback());
458 // If server and origin have different hostnames, then handshake confirmation
459 // should be required, so Request will return asynchronously.
460 EXPECT_EQ(ERR_IO_PENDING, rv);
461 // Confirm handshake.
462 crypto_client_stream_factory_.last_stream()->SendOnCryptoHandshakeEvent(
463 QuicSession::HANDSHAKE_CONFIRMED);
464 EXPECT_EQ(OK, callback_.WaitForResult());
466 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
467 EXPECT_TRUE(stream.get());
468 EXPECT_TRUE(socket_data.AllReadDataConsumed());
469 EXPECT_TRUE(socket_data.AllWriteDataConsumed());
472 TEST_P(QuicStreamFactoryTest, CreateHttpVsHttps) {
473 MockRead reads[] = {
474 MockRead(ASYNC, OK, 0) // EOF
476 DeterministicSocketData socket_data1(reads, arraysize(reads), nullptr, 0);
477 DeterministicSocketData socket_data2(reads, arraysize(reads), nullptr, 0);
478 socket_factory_.AddSocketDataProvider(&socket_data1);
479 socket_factory_.AddSocketDataProvider(&socket_data2);
480 socket_data1.StopAfter(1);
481 socket_data2.StopAfter(1);
483 QuicStreamRequest request(&factory_);
484 EXPECT_EQ(ERR_IO_PENDING,
485 request.Request(host_port_pair_, is_https_, privacy_mode_,
486 /*cert_verify_flags=*/0, host_port_pair_.host(),
487 "GET", net_log_, callback_.callback()));
489 EXPECT_EQ(OK, callback_.WaitForResult());
490 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
491 EXPECT_TRUE(stream.get());
493 QuicStreamRequest request2(&factory_);
494 EXPECT_EQ(ERR_IO_PENDING,
495 request2.Request(host_port_pair_, !is_https_, privacy_mode_,
496 /*cert_verify_flags=*/0, host_port_pair_.host(),
497 "GET", net_log_, callback_.callback()));
498 EXPECT_EQ(OK, callback_.WaitForResult());
499 stream = request2.ReleaseStream();
500 EXPECT_TRUE(stream.get());
501 stream.reset();
503 EXPECT_NE(QuicStreamFactoryPeer::GetActiveSession(
504 &factory_, host_port_pair_, is_https_),
505 QuicStreamFactoryPeer::GetActiveSession(
506 &factory_, host_port_pair_, !is_https_));
508 EXPECT_TRUE(socket_data1.AllReadDataConsumed());
509 EXPECT_TRUE(socket_data1.AllWriteDataConsumed());
510 EXPECT_TRUE(socket_data2.AllReadDataConsumed());
511 EXPECT_TRUE(socket_data2.AllWriteDataConsumed());
514 TEST_P(QuicStreamFactoryTest, Pooling) {
515 MockRead reads[] = {
516 MockRead(ASYNC, OK, 0) // EOF
518 DeterministicSocketData socket_data(reads, arraysize(reads), nullptr, 0);
519 socket_factory_.AddSocketDataProvider(&socket_data);
520 socket_data.StopAfter(1);
522 HostPortPair server2("mail.google.com", kDefaultServerPort);
523 host_resolver_.set_synchronous_mode(true);
524 host_resolver_.rules()->AddIPLiteralRule(
525 kDefaultServerHostName, "192.168.0.1", "");
526 host_resolver_.rules()->AddIPLiteralRule(
527 "mail.google.com", "192.168.0.1", "");
529 QuicStreamRequest request(&factory_);
530 EXPECT_EQ(OK, request.Request(host_port_pair_, is_https_, privacy_mode_,
531 /*cert_verify_flags=*/0, host_port_pair_.host(),
532 "GET", net_log_, callback_.callback()));
533 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
534 EXPECT_TRUE(stream.get());
536 TestCompletionCallback callback;
537 QuicStreamRequest request2(&factory_);
538 EXPECT_EQ(OK, request2.Request(server2, is_https_, privacy_mode_,
539 /*cert_verify_flags=*/0, server2.host(), "GET",
540 net_log_, callback.callback()));
541 scoped_ptr<QuicHttpStream> stream2 = request2.ReleaseStream();
542 EXPECT_TRUE(stream2.get());
544 EXPECT_EQ(
545 QuicStreamFactoryPeer::GetActiveSession(
546 &factory_, host_port_pair_, is_https_),
547 QuicStreamFactoryPeer::GetActiveSession(&factory_, server2, is_https_));
549 EXPECT_TRUE(socket_data.AllReadDataConsumed());
550 EXPECT_TRUE(socket_data.AllWriteDataConsumed());
553 TEST_P(QuicStreamFactoryTest, NoPoolingIfDisabled) {
554 MockRead reads[] = {
555 MockRead(ASYNC, OK, 0) // EOF
557 DeterministicSocketData socket_data1(reads, arraysize(reads), nullptr, 0);
558 DeterministicSocketData socket_data2(reads, arraysize(reads), nullptr, 0);
559 socket_factory_.AddSocketDataProvider(&socket_data1);
560 socket_factory_.AddSocketDataProvider(&socket_data2);
561 socket_data1.StopAfter(1);
562 socket_data2.StopAfter(1);
564 HostPortPair server2("mail.google.com", kDefaultServerPort);
565 host_resolver_.set_synchronous_mode(true);
566 host_resolver_.rules()->AddIPLiteralRule(
567 kDefaultServerHostName, "192.168.0.1", "");
568 host_resolver_.rules()->AddIPLiteralRule(
569 "mail.google.com", "192.168.0.1", "");
571 // Disable connection pooling.
572 QuicStreamFactoryPeer::DisableConnectionPooling(&factory_);
574 QuicStreamRequest request(&factory_);
575 EXPECT_EQ(OK, request.Request(host_port_pair_, is_https_, privacy_mode_,
576 /*cert_verify_flags=*/0, host_port_pair_.host(),
577 "GET", net_log_, callback_.callback()));
578 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
579 EXPECT_TRUE(stream.get());
581 TestCompletionCallback callback;
582 QuicStreamRequest request2(&factory_);
583 EXPECT_EQ(OK, request2.Request(server2, is_https_, privacy_mode_,
584 /*cert_verify_flags=*/0, server2.host(), "GET",
585 net_log_, callback.callback()));
586 scoped_ptr<QuicHttpStream> stream2 = request2.ReleaseStream();
587 EXPECT_TRUE(stream2.get());
589 EXPECT_NE(
590 QuicStreamFactoryPeer::GetActiveSession(
591 &factory_, host_port_pair_, is_https_),
592 QuicStreamFactoryPeer::GetActiveSession(&factory_, server2, is_https_));
594 EXPECT_TRUE(socket_data1.AllReadDataConsumed());
595 EXPECT_TRUE(socket_data1.AllWriteDataConsumed());
596 EXPECT_TRUE(socket_data2.AllReadDataConsumed());
597 EXPECT_TRUE(socket_data2.AllWriteDataConsumed());
600 TEST_P(QuicStreamFactoryTest, NoPoolingAfterGoAway) {
601 MockRead reads[] = {
602 MockRead(ASYNC, OK, 0) // EOF
604 DeterministicSocketData socket_data1(reads, arraysize(reads), nullptr, 0);
605 DeterministicSocketData socket_data2(reads, arraysize(reads), nullptr, 0);
606 socket_factory_.AddSocketDataProvider(&socket_data1);
607 socket_factory_.AddSocketDataProvider(&socket_data2);
608 socket_data1.StopAfter(1);
609 socket_data2.StopAfter(1);
611 HostPortPair server2("mail.google.com", kDefaultServerPort);
612 host_resolver_.set_synchronous_mode(true);
613 host_resolver_.rules()->AddIPLiteralRule(
614 kDefaultServerHostName, "192.168.0.1", "");
615 host_resolver_.rules()->AddIPLiteralRule(
616 "mail.google.com", "192.168.0.1", "");
618 QuicStreamRequest request(&factory_);
619 EXPECT_EQ(OK, request.Request(host_port_pair_, is_https_, privacy_mode_,
620 /*cert_verify_flags=*/0, host_port_pair_.host(),
621 "GET", net_log_, callback_.callback()));
622 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
623 EXPECT_TRUE(stream.get());
625 TestCompletionCallback callback;
626 QuicStreamRequest request2(&factory_);
627 EXPECT_EQ(OK, request2.Request(server2, is_https_, privacy_mode_,
628 /*cert_verify_flags=*/0, server2.host(), "GET",
629 net_log_, callback.callback()));
630 scoped_ptr<QuicHttpStream> stream2 = request2.ReleaseStream();
631 EXPECT_TRUE(stream2.get());
633 factory_.OnSessionGoingAway(QuicStreamFactoryPeer::GetActiveSession(
634 &factory_, host_port_pair_, is_https_));
635 EXPECT_FALSE(QuicStreamFactoryPeer::HasActiveSession(
636 &factory_, host_port_pair_, is_https_));
637 EXPECT_FALSE(QuicStreamFactoryPeer::HasActiveSession(
638 &factory_, server2, is_https_));
640 TestCompletionCallback callback3;
641 QuicStreamRequest request3(&factory_);
642 EXPECT_EQ(OK, request3.Request(server2, is_https_, privacy_mode_,
643 /*cert_verify_flags=*/0, server2.host(), "GET",
644 net_log_, callback3.callback()));
645 scoped_ptr<QuicHttpStream> stream3 = request3.ReleaseStream();
646 EXPECT_TRUE(stream3.get());
648 EXPECT_TRUE(QuicStreamFactoryPeer::HasActiveSession(
649 &factory_, server2, is_https_));
651 EXPECT_TRUE(socket_data1.AllReadDataConsumed());
652 EXPECT_TRUE(socket_data1.AllWriteDataConsumed());
653 EXPECT_TRUE(socket_data2.AllReadDataConsumed());
654 EXPECT_TRUE(socket_data2.AllWriteDataConsumed());
657 TEST_P(QuicStreamFactoryTest, HttpsPooling) {
658 MockRead reads[] = {
659 MockRead(ASYNC, OK, 0) // EOF
661 DeterministicSocketData socket_data(reads, arraysize(reads), nullptr, 0);
662 socket_factory_.AddSocketDataProvider(&socket_data);
663 socket_data.StopAfter(1);
665 HostPortPair server1("www.example.org", 443);
666 HostPortPair server2("mail.example.org", 443);
668 ProofVerifyDetailsChromium verify_details = DefaultProofVerifyDetails();
669 crypto_client_stream_factory_.AddProofVerifyDetails(&verify_details);
671 host_resolver_.set_synchronous_mode(true);
672 host_resolver_.rules()->AddIPLiteralRule(server1.host(), "192.168.0.1", "");
673 host_resolver_.rules()->AddIPLiteralRule(server2.host(), "192.168.0.1", "");
675 QuicStreamRequest request(&factory_);
676 is_https_ = true;
677 EXPECT_EQ(OK, request.Request(server1, is_https_, privacy_mode_,
678 /*cert_verify_flags=*/0, server1.host(), "GET",
679 net_log_, callback_.callback()));
680 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
681 EXPECT_TRUE(stream.get());
683 TestCompletionCallback callback;
684 QuicStreamRequest request2(&factory_);
685 EXPECT_EQ(OK, request2.Request(server2, is_https_, privacy_mode_,
686 /*cert_verify_flags=*/0, server2.host(), "GET",
687 net_log_, callback_.callback()));
688 scoped_ptr<QuicHttpStream> stream2 = request2.ReleaseStream();
689 EXPECT_TRUE(stream2.get());
691 EXPECT_EQ(QuicStreamFactoryPeer::GetActiveSession(
692 &factory_, server1, is_https_),
693 QuicStreamFactoryPeer::GetActiveSession(
694 &factory_, server2, is_https_));
696 EXPECT_TRUE(socket_data.AllReadDataConsumed());
697 EXPECT_TRUE(socket_data.AllWriteDataConsumed());
700 TEST_P(QuicStreamFactoryTest, NoHttpsPoolingIfDisabled) {
701 MockRead reads[] = {
702 MockRead(ASYNC, OK, 0) // EOF
704 DeterministicSocketData socket_data1(reads, arraysize(reads), nullptr, 0);
705 DeterministicSocketData socket_data2(reads, arraysize(reads), nullptr, 0);
706 socket_factory_.AddSocketDataProvider(&socket_data1);
707 socket_factory_.AddSocketDataProvider(&socket_data2);
708 socket_data1.StopAfter(1);
709 socket_data2.StopAfter(1);
711 HostPortPair server1("www.example.org", 443);
712 HostPortPair server2("mail.example.org", 443);
714 ProofVerifyDetailsChromium verify_details = DefaultProofVerifyDetails();
715 crypto_client_stream_factory_.AddProofVerifyDetails(&verify_details);
717 host_resolver_.set_synchronous_mode(true);
718 host_resolver_.rules()->AddIPLiteralRule(server1.host(), "192.168.0.1", "");
719 host_resolver_.rules()->AddIPLiteralRule(server2.host(), "192.168.0.1", "");
721 // Disable connection pooling.
722 QuicStreamFactoryPeer::DisableConnectionPooling(&factory_);
724 QuicStreamRequest request(&factory_);
725 is_https_ = true;
726 EXPECT_EQ(OK, request.Request(server1, is_https_, privacy_mode_,
727 /*cert_verify_flags=*/0, server1.host(), "GET",
728 net_log_, callback_.callback()));
729 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
730 EXPECT_TRUE(stream.get());
732 TestCompletionCallback callback;
733 QuicStreamRequest request2(&factory_);
734 EXPECT_EQ(OK, request2.Request(server2, is_https_, privacy_mode_,
735 /*cert_verify_flags=*/0, server2.host(), "GET",
736 net_log_, callback_.callback()));
737 scoped_ptr<QuicHttpStream> stream2 = request2.ReleaseStream();
738 EXPECT_TRUE(stream2.get());
740 EXPECT_NE(QuicStreamFactoryPeer::GetActiveSession(
741 &factory_, server1, is_https_),
742 QuicStreamFactoryPeer::GetActiveSession(
743 &factory_, server2, is_https_));
745 EXPECT_TRUE(socket_data1.AllReadDataConsumed());
746 EXPECT_TRUE(socket_data1.AllWriteDataConsumed());
747 EXPECT_TRUE(socket_data2.AllReadDataConsumed());
748 EXPECT_TRUE(socket_data2.AllWriteDataConsumed());
751 class QuicAlternativeServiceCertificateValidationPooling
752 : public QuicStreamFactoryTest {
753 public:
754 void Run(bool valid) {
755 MockRead reads[] = {
756 MockRead(ASYNC, OK, 0) // EOF
758 DeterministicSocketData socket_data1(reads, arraysize(reads), nullptr, 0);
759 socket_factory_.AddSocketDataProvider(&socket_data1);
760 socket_data1.StopAfter(1);
762 HostPortPair server1("www.example.org", 443);
763 HostPortPair server2("mail.example.org", 443);
765 std::string origin_host(valid ? "mail.example.org" : "invalid.example.org");
766 HostPortPair alternative("www.example.org", 443);
768 ProofVerifyDetailsChromium verify_details = DefaultProofVerifyDetails();
769 bool common_name_fallback_used;
770 EXPECT_EQ(valid,
771 verify_details.cert_verify_result.verified_cert->VerifyNameMatch(
772 origin_host, &common_name_fallback_used));
773 EXPECT_TRUE(
774 verify_details.cert_verify_result.verified_cert->VerifyNameMatch(
775 alternative.host(), &common_name_fallback_used));
776 crypto_client_stream_factory_.AddProofVerifyDetails(&verify_details);
778 host_resolver_.set_synchronous_mode(true);
779 host_resolver_.rules()->AddIPLiteralRule(alternative.host(), "192.168.0.1",
780 "");
782 // Open first stream to alternative.
783 QuicStreamRequest request1(&factory_);
784 is_https_ = true;
785 EXPECT_EQ(OK, request1.Request(alternative, is_https_, privacy_mode_,
786 /*cert_verify_flags=*/0, alternative.host(),
787 "GET", net_log_, callback_.callback()));
788 scoped_ptr<QuicHttpStream> stream1 = request1.ReleaseStream();
789 EXPECT_TRUE(stream1.get());
791 QuicStreamRequest request2(&factory_);
792 int rv = request2.Request(alternative, is_https_, privacy_mode_,
793 /*cert_verify_flags=*/0, origin_host, "GET",
794 net_log_, callback_.callback());
795 if (valid) {
796 // Alternative service of origin to |alternative| should pool to session
797 // of |stream1| even if origin is different. Since only one
798 // SocketDataProvider is set up, the second request succeeding means that
799 // it pooled to the session opened by the first one.
800 EXPECT_EQ(OK, rv);
801 scoped_ptr<QuicHttpStream> stream2 = request2.ReleaseStream();
802 EXPECT_TRUE(stream2.get());
803 } else {
804 EXPECT_EQ(ERR_ALTERNATIVE_CERT_NOT_VALID_FOR_ORIGIN, rv);
807 EXPECT_TRUE(socket_data1.AllReadDataConsumed());
808 EXPECT_TRUE(socket_data1.AllWriteDataConsumed());
812 INSTANTIATE_TEST_CASE_P(Version,
813 QuicAlternativeServiceCertificateValidationPooling,
814 ::testing::ValuesIn(GetTestParams()));
816 TEST_P(QuicAlternativeServiceCertificateValidationPooling, Valid) {
817 Run(true);
820 TEST_P(QuicAlternativeServiceCertificateValidationPooling, Invalid) {
821 Run(false);
824 TEST_P(QuicStreamFactoryTest, HttpsPoolingWithMatchingPins) {
825 MockRead reads[] = {
826 MockRead(ASYNC, OK, 0) // EOF
828 DeterministicSocketData socket_data(reads, arraysize(reads), nullptr, 0);
829 socket_factory_.AddSocketDataProvider(&socket_data);
830 socket_data.StopAfter(1);
832 HostPortPair server1("www.example.org", 443);
833 HostPortPair server2("mail.example.org", 443);
834 uint8 primary_pin = 1;
835 uint8 backup_pin = 2;
836 test::AddPin(&transport_security_state_, "mail.example.org", primary_pin,
837 backup_pin);
839 ProofVerifyDetailsChromium verify_details = DefaultProofVerifyDetails();
840 verify_details.cert_verify_result.public_key_hashes.push_back(
841 test::GetTestHashValue(primary_pin));
842 crypto_client_stream_factory_.AddProofVerifyDetails(&verify_details);
844 host_resolver_.set_synchronous_mode(true);
845 host_resolver_.rules()->AddIPLiteralRule(server1.host(), "192.168.0.1", "");
846 host_resolver_.rules()->AddIPLiteralRule(server2.host(), "192.168.0.1", "");
848 QuicStreamRequest request(&factory_);
849 is_https_ = true;
850 EXPECT_EQ(OK, request.Request(server1, is_https_, privacy_mode_,
851 /*cert_verify_flags=*/0, server1.host(), "GET",
852 net_log_, callback_.callback()));
853 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
854 EXPECT_TRUE(stream.get());
856 TestCompletionCallback callback;
857 QuicStreamRequest request2(&factory_);
858 EXPECT_EQ(OK, request2.Request(server2, is_https_, privacy_mode_,
859 /*cert_verify_flags=*/0, server2.host(), "GET",
860 net_log_, callback_.callback()));
861 scoped_ptr<QuicHttpStream> stream2 = request2.ReleaseStream();
862 EXPECT_TRUE(stream2.get());
864 EXPECT_EQ(QuicStreamFactoryPeer::GetActiveSession(
865 &factory_, server1, is_https_),
866 QuicStreamFactoryPeer::GetActiveSession(
867 &factory_, server2, is_https_));
869 EXPECT_TRUE(socket_data.AllReadDataConsumed());
870 EXPECT_TRUE(socket_data.AllWriteDataConsumed());
873 TEST_P(QuicStreamFactoryTest, NoHttpsPoolingWithMatchingPinsIfDisabled) {
874 MockRead reads[] = {
875 MockRead(ASYNC, OK, 0) // EOF
877 DeterministicSocketData socket_data1(reads, arraysize(reads), nullptr, 0);
878 DeterministicSocketData socket_data2(reads, arraysize(reads), nullptr, 0);
879 socket_factory_.AddSocketDataProvider(&socket_data1);
880 socket_factory_.AddSocketDataProvider(&socket_data2);
881 socket_data1.StopAfter(1);
882 socket_data2.StopAfter(1);
884 HostPortPair server1("www.example.org", 443);
885 HostPortPair server2("mail.example.org", 443);
886 uint8 primary_pin = 1;
887 uint8 backup_pin = 2;
888 test::AddPin(&transport_security_state_, "mail.example.org", primary_pin,
889 backup_pin);
891 ProofVerifyDetailsChromium verify_details = DefaultProofVerifyDetails();
892 verify_details.cert_verify_result.public_key_hashes.push_back(
893 test::GetTestHashValue(primary_pin));
894 crypto_client_stream_factory_.AddProofVerifyDetails(&verify_details);
896 host_resolver_.set_synchronous_mode(true);
897 host_resolver_.rules()->AddIPLiteralRule(server1.host(), "192.168.0.1", "");
898 host_resolver_.rules()->AddIPLiteralRule(server2.host(), "192.168.0.1", "");
900 // Disable connection pooling.
901 QuicStreamFactoryPeer::DisableConnectionPooling(&factory_);
903 QuicStreamRequest request(&factory_);
904 is_https_ = true;
905 EXPECT_EQ(OK, request.Request(server1, is_https_, privacy_mode_,
906 /*cert_verify_flags=*/0, server1.host(), "GET",
907 net_log_, callback_.callback()));
908 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
909 EXPECT_TRUE(stream.get());
911 TestCompletionCallback callback;
912 QuicStreamRequest request2(&factory_);
913 EXPECT_EQ(OK, request2.Request(server2, is_https_, privacy_mode_,
914 /*cert_verify_flags=*/0, server2.host(), "GET",
915 net_log_, callback_.callback()));
916 scoped_ptr<QuicHttpStream> stream2 = request2.ReleaseStream();
917 EXPECT_TRUE(stream2.get());
919 EXPECT_NE(QuicStreamFactoryPeer::GetActiveSession(
920 &factory_, server1, is_https_),
921 QuicStreamFactoryPeer::GetActiveSession(
922 &factory_, server2, is_https_));
924 EXPECT_TRUE(socket_data1.AllReadDataConsumed());
925 EXPECT_TRUE(socket_data1.AllWriteDataConsumed());
926 EXPECT_TRUE(socket_data2.AllReadDataConsumed());
927 EXPECT_TRUE(socket_data2.AllWriteDataConsumed());
930 TEST_P(QuicStreamFactoryTest, NoHttpsPoolingWithDifferentPins) {
931 MockRead reads[] = {
932 MockRead(ASYNC, OK, 0) // EOF
934 DeterministicSocketData socket_data1(reads, arraysize(reads), nullptr, 0);
935 DeterministicSocketData socket_data2(reads, arraysize(reads), nullptr, 0);
936 socket_factory_.AddSocketDataProvider(&socket_data1);
937 socket_factory_.AddSocketDataProvider(&socket_data2);
938 socket_data1.StopAfter(1);
939 socket_data2.StopAfter(1);
941 HostPortPair server1("www.example.org", 443);
942 HostPortPair server2("mail.example.org", 443);
943 uint8 primary_pin = 1;
944 uint8 backup_pin = 2;
945 uint8 bad_pin = 3;
946 test::AddPin(&transport_security_state_, "mail.example.org", primary_pin,
947 backup_pin);
949 ProofVerifyDetailsChromium verify_details1 = DefaultProofVerifyDetails();
950 verify_details1.cert_verify_result.public_key_hashes.push_back(
951 test::GetTestHashValue(bad_pin));
952 crypto_client_stream_factory_.AddProofVerifyDetails(&verify_details1);
954 ProofVerifyDetailsChromium verify_details2 = DefaultProofVerifyDetails();
955 verify_details2.cert_verify_result.public_key_hashes.push_back(
956 test::GetTestHashValue(primary_pin));
957 crypto_client_stream_factory_.AddProofVerifyDetails(&verify_details2);
959 host_resolver_.set_synchronous_mode(true);
960 host_resolver_.rules()->AddIPLiteralRule(server1.host(), "192.168.0.1", "");
961 host_resolver_.rules()->AddIPLiteralRule(server2.host(), "192.168.0.1", "");
963 QuicStreamRequest request(&factory_);
964 is_https_ = true;
965 EXPECT_EQ(OK, request.Request(server1, is_https_, privacy_mode_,
966 /*cert_verify_flags=*/0, server1.host(), "GET",
967 net_log_, callback_.callback()));
968 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
969 EXPECT_TRUE(stream.get());
971 TestCompletionCallback callback;
972 QuicStreamRequest request2(&factory_);
973 EXPECT_EQ(OK, request2.Request(server2, is_https_, privacy_mode_,
974 /*cert_verify_flags=*/0, server2.host(), "GET",
975 net_log_, callback_.callback()));
976 scoped_ptr<QuicHttpStream> stream2 = request2.ReleaseStream();
977 EXPECT_TRUE(stream2.get());
979 EXPECT_NE(QuicStreamFactoryPeer::GetActiveSession(
980 &factory_, server1, is_https_),
981 QuicStreamFactoryPeer::GetActiveSession(
982 &factory_, server2, is_https_));
984 EXPECT_TRUE(socket_data1.AllReadDataConsumed());
985 EXPECT_TRUE(socket_data1.AllWriteDataConsumed());
986 EXPECT_TRUE(socket_data2.AllReadDataConsumed());
987 EXPECT_TRUE(socket_data2.AllWriteDataConsumed());
990 TEST_P(QuicStreamFactoryTest, Goaway) {
991 MockRead reads[] = {
992 MockRead(ASYNC, OK, 0) // EOF
994 DeterministicSocketData socket_data(reads, arraysize(reads), nullptr, 0);
995 socket_data.StopAfter(1);
996 socket_factory_.AddSocketDataProvider(&socket_data);
997 DeterministicSocketData socket_data2(reads, arraysize(reads), nullptr, 0);
998 socket_data2.StopAfter(1);
999 socket_factory_.AddSocketDataProvider(&socket_data2);
1001 QuicStreamRequest request(&factory_);
1002 EXPECT_EQ(ERR_IO_PENDING,
1003 request.Request(host_port_pair_, is_https_, privacy_mode_,
1004 /*cert_verify_flags=*/0, host_port_pair_.host(),
1005 "GET", net_log_, callback_.callback()));
1007 EXPECT_EQ(OK, callback_.WaitForResult());
1008 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
1009 EXPECT_TRUE(stream.get());
1011 // Mark the session as going away. Ensure that while it is still alive
1012 // that it is no longer active.
1013 QuicClientSession* session = QuicStreamFactoryPeer::GetActiveSession(
1014 &factory_, host_port_pair_, is_https_);
1015 factory_.OnSessionGoingAway(session);
1016 EXPECT_EQ(true, QuicStreamFactoryPeer::IsLiveSession(&factory_, session));
1017 EXPECT_FALSE(QuicStreamFactoryPeer::HasActiveSession(
1018 &factory_, host_port_pair_, is_https_));
1019 EXPECT_FALSE(HasActiveSession(host_port_pair_));
1021 // Create a new request for the same destination and verify that a
1022 // new session is created.
1023 QuicStreamRequest request2(&factory_);
1024 EXPECT_EQ(ERR_IO_PENDING,
1025 request2.Request(host_port_pair_, is_https_, privacy_mode_,
1026 /*cert_verify_flags=*/0, host_port_pair_.host(),
1027 "GET", net_log_, callback_.callback()));
1028 EXPECT_EQ(OK, callback_.WaitForResult());
1029 scoped_ptr<QuicHttpStream> stream2 = request2.ReleaseStream();
1030 EXPECT_TRUE(stream2.get());
1032 EXPECT_TRUE(QuicStreamFactoryPeer::HasActiveSession(&factory_,
1033 host_port_pair_,
1034 is_https_));
1035 EXPECT_NE(session,
1036 QuicStreamFactoryPeer::GetActiveSession(
1037 &factory_, host_port_pair_, is_https_));
1038 EXPECT_EQ(true, QuicStreamFactoryPeer::IsLiveSession(&factory_, session));
1040 stream2.reset();
1041 stream.reset();
1043 EXPECT_TRUE(socket_data.AllReadDataConsumed());
1044 EXPECT_TRUE(socket_data.AllWriteDataConsumed());
1045 EXPECT_TRUE(socket_data2.AllReadDataConsumed());
1046 EXPECT_TRUE(socket_data2.AllWriteDataConsumed());
1049 TEST_P(QuicStreamFactoryTest, MaxOpenStream) {
1050 MockRead reads[] = {
1051 MockRead(ASYNC, OK, 0) // EOF
1053 QuicStreamId stream_id = kClientDataStreamId1;
1054 scoped_ptr<QuicEncryptedPacket> rst(
1055 maker_.MakeRstPacket(1, true, stream_id, QUIC_STREAM_CANCELLED));
1056 MockWrite writes[] = {
1057 MockWrite(ASYNC, rst->data(), rst->length(), 1),
1059 DeterministicSocketData socket_data(reads, arraysize(reads),
1060 writes, arraysize(writes));
1061 socket_factory_.AddSocketDataProvider(&socket_data);
1062 socket_data.StopAfter(1);
1064 HttpRequestInfo request_info;
1065 std::vector<QuicHttpStream*> streams;
1066 // The MockCryptoClientStream sets max_open_streams to be
1067 // kDefaultMaxStreamsPerConnection / 2.
1068 for (size_t i = 0; i < kDefaultMaxStreamsPerConnection / 2; i++) {
1069 QuicStreamRequest request(&factory_);
1070 int rv = request.Request(host_port_pair_, is_https_, privacy_mode_,
1071 /*cert_verify_flags=*/0, host_port_pair_.host(),
1072 "GET", net_log_, callback_.callback());
1073 if (i == 0) {
1074 EXPECT_EQ(ERR_IO_PENDING, rv);
1075 EXPECT_EQ(OK, callback_.WaitForResult());
1076 } else {
1077 EXPECT_EQ(OK, rv);
1079 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
1080 EXPECT_TRUE(stream);
1081 EXPECT_EQ(OK, stream->InitializeStream(
1082 &request_info, DEFAULT_PRIORITY, net_log_, CompletionCallback()));
1083 streams.push_back(stream.release());
1086 QuicStreamRequest request(&factory_);
1087 EXPECT_EQ(OK, request.Request(host_port_pair_, is_https_, privacy_mode_,
1088 /*cert_verify_flags=*/0, host_port_pair_.host(),
1089 "GET", net_log_, CompletionCallback()));
1090 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
1091 EXPECT_TRUE(stream);
1092 EXPECT_EQ(ERR_IO_PENDING, stream->InitializeStream(
1093 &request_info, DEFAULT_PRIORITY, net_log_, callback_.callback()));
1095 // Close the first stream.
1096 streams.front()->Close(false);
1098 ASSERT_TRUE(callback_.have_result());
1100 EXPECT_EQ(OK, callback_.WaitForResult());
1102 EXPECT_TRUE(socket_data.AllReadDataConsumed());
1103 EXPECT_TRUE(socket_data.AllWriteDataConsumed());
1104 STLDeleteElements(&streams);
1107 TEST_P(QuicStreamFactoryTest, ResolutionErrorInCreate) {
1108 DeterministicSocketData socket_data(nullptr, 0, nullptr, 0);
1109 socket_factory_.AddSocketDataProvider(&socket_data);
1111 host_resolver_.rules()->AddSimulatedFailure(kDefaultServerHostName);
1113 QuicStreamRequest request(&factory_);
1114 EXPECT_EQ(ERR_IO_PENDING,
1115 request.Request(host_port_pair_, is_https_, privacy_mode_,
1116 /*cert_verify_flags=*/0, host_port_pair_.host(),
1117 "GET", net_log_, callback_.callback()));
1119 EXPECT_EQ(ERR_NAME_NOT_RESOLVED, callback_.WaitForResult());
1121 EXPECT_TRUE(socket_data.AllReadDataConsumed());
1122 EXPECT_TRUE(socket_data.AllWriteDataConsumed());
1125 TEST_P(QuicStreamFactoryTest, ConnectErrorInCreate) {
1126 MockConnect connect(SYNCHRONOUS, ERR_ADDRESS_IN_USE);
1127 DeterministicSocketData socket_data(nullptr, 0, nullptr, 0);
1128 socket_data.set_connect_data(connect);
1129 socket_factory_.AddSocketDataProvider(&socket_data);
1130 socket_data.StopAfter(1);
1132 QuicStreamRequest request(&factory_);
1133 EXPECT_EQ(ERR_IO_PENDING,
1134 request.Request(host_port_pair_, is_https_, privacy_mode_,
1135 /*cert_verify_flags=*/0, host_port_pair_.host(),
1136 "GET", net_log_, callback_.callback()));
1138 EXPECT_EQ(ERR_ADDRESS_IN_USE, callback_.WaitForResult());
1140 EXPECT_TRUE(socket_data.AllReadDataConsumed());
1141 EXPECT_TRUE(socket_data.AllWriteDataConsumed());
1144 TEST_P(QuicStreamFactoryTest, CancelCreate) {
1145 MockRead reads[] = {
1146 MockRead(ASYNC, OK, 0) // EOF
1148 DeterministicSocketData socket_data(reads, arraysize(reads), nullptr, 0);
1149 socket_factory_.AddSocketDataProvider(&socket_data);
1151 QuicStreamRequest request(&factory_);
1152 EXPECT_EQ(ERR_IO_PENDING,
1153 request.Request(host_port_pair_, is_https_, privacy_mode_,
1154 /*cert_verify_flags=*/0, host_port_pair_.host(),
1155 "GET", net_log_, callback_.callback()));
1158 socket_data.StopAfter(1);
1159 base::RunLoop run_loop;
1160 run_loop.RunUntilIdle();
1162 scoped_ptr<QuicHttpStream> stream(CreateFromSession(host_port_pair_));
1163 EXPECT_TRUE(stream.get());
1164 stream.reset();
1166 EXPECT_TRUE(socket_data.AllReadDataConsumed());
1167 EXPECT_TRUE(socket_data.AllWriteDataConsumed());
1170 TEST_P(QuicStreamFactoryTest, CreateConsistentEphemeralPort) {
1171 // Sequentially connect to the default host, then another host, and then the
1172 // default host. Verify that the default host gets a consistent ephemeral
1173 // port, that is different from the other host's connection.
1175 std::string other_server_name = "other.google.com";
1176 EXPECT_NE(kDefaultServerHostName, other_server_name);
1177 HostPortPair host_port_pair2(other_server_name, kDefaultServerPort);
1179 int original_port = GetSourcePortForNewSession(host_port_pair_);
1180 EXPECT_NE(original_port, GetSourcePortForNewSession(host_port_pair2));
1181 EXPECT_EQ(original_port, GetSourcePortForNewSession(host_port_pair_));
1184 TEST_P(QuicStreamFactoryTest, GoAwayDisablesConsistentEphemeralPort) {
1185 // Get a session to the host using the port suggester.
1186 int original_port =
1187 GetSourcePortForNewSessionAndGoAway(host_port_pair_);
1188 // Verify that the port is different after the goaway.
1189 EXPECT_NE(original_port, GetSourcePortForNewSession(host_port_pair_));
1190 // Since the previous session did not goaway we should see the original port.
1191 EXPECT_EQ(original_port, GetSourcePortForNewSession(host_port_pair_));
1194 TEST_P(QuicStreamFactoryTest, CloseAllSessions) {
1195 MockRead reads[] = {
1196 MockRead(ASYNC, 0, 0) // EOF
1198 scoped_ptr<QuicEncryptedPacket> rst(ConstructRstPacket());
1199 std::vector<MockWrite> writes;
1200 writes.push_back(MockWrite(ASYNC, rst->data(), rst->length(), 1));
1201 DeterministicSocketData socket_data(reads, arraysize(reads),
1202 writes.empty() ? nullptr : &writes[0],
1203 writes.size());
1204 socket_factory_.AddSocketDataProvider(&socket_data);
1205 socket_data.StopAfter(1);
1207 MockRead reads2[] = {
1208 MockRead(ASYNC, 0, 0) // EOF
1210 DeterministicSocketData socket_data2(reads2, arraysize(reads2), nullptr, 0);
1211 socket_factory_.AddSocketDataProvider(&socket_data2);
1212 socket_data2.StopAfter(1);
1214 QuicStreamRequest request(&factory_);
1215 EXPECT_EQ(ERR_IO_PENDING,
1216 request.Request(host_port_pair_, is_https_, privacy_mode_,
1217 /*cert_verify_flags=*/0, host_port_pair_.host(),
1218 "GET", net_log_, callback_.callback()));
1220 EXPECT_EQ(OK, callback_.WaitForResult());
1221 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
1222 HttpRequestInfo request_info;
1223 EXPECT_EQ(OK, stream->InitializeStream(&request_info,
1224 DEFAULT_PRIORITY,
1225 net_log_, CompletionCallback()));
1227 // Close the session and verify that stream saw the error.
1228 factory_.CloseAllSessions(ERR_INTERNET_DISCONNECTED);
1229 EXPECT_EQ(ERR_INTERNET_DISCONNECTED,
1230 stream->ReadResponseHeaders(callback_.callback()));
1232 // Now attempting to request a stream to the same origin should create
1233 // a new session.
1235 QuicStreamRequest request2(&factory_);
1236 EXPECT_EQ(ERR_IO_PENDING,
1237 request2.Request(host_port_pair_, is_https_, privacy_mode_,
1238 /*cert_verify_flags=*/0, host_port_pair_.host(),
1239 "GET", net_log_, callback_.callback()));
1241 EXPECT_EQ(OK, callback_.WaitForResult());
1242 stream = request2.ReleaseStream();
1243 stream.reset(); // Will reset stream 3.
1245 EXPECT_TRUE(socket_data.AllReadDataConsumed());
1246 EXPECT_TRUE(socket_data.AllWriteDataConsumed());
1247 EXPECT_TRUE(socket_data2.AllReadDataConsumed());
1248 EXPECT_TRUE(socket_data2.AllWriteDataConsumed());
1251 TEST_P(QuicStreamFactoryTest, OnIPAddressChanged) {
1252 MockRead reads[] = {
1253 MockRead(ASYNC, 0, 0) // EOF
1255 scoped_ptr<QuicEncryptedPacket> rst(ConstructRstPacket());
1256 std::vector<MockWrite> writes;
1257 writes.push_back(MockWrite(ASYNC, rst->data(), rst->length(), 1));
1258 DeterministicSocketData socket_data(reads, arraysize(reads),
1259 writes.empty() ? nullptr : &writes[0],
1260 writes.size());
1261 socket_factory_.AddSocketDataProvider(&socket_data);
1262 socket_data.StopAfter(1);
1264 MockRead reads2[] = {
1265 MockRead(ASYNC, 0, 0) // EOF
1267 DeterministicSocketData socket_data2(reads2, arraysize(reads2), nullptr, 0);
1268 socket_factory_.AddSocketDataProvider(&socket_data2);
1269 socket_data2.StopAfter(1);
1271 QuicStreamRequest request(&factory_);
1272 EXPECT_EQ(ERR_IO_PENDING,
1273 request.Request(host_port_pair_, is_https_, privacy_mode_,
1274 /*cert_verify_flags=*/0, host_port_pair_.host(),
1275 "GET", net_log_, callback_.callback()));
1277 EXPECT_EQ(OK, callback_.WaitForResult());
1278 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
1279 HttpRequestInfo request_info;
1280 EXPECT_EQ(OK, stream->InitializeStream(&request_info,
1281 DEFAULT_PRIORITY,
1282 net_log_, CompletionCallback()));
1284 // Change the IP address and verify that stream saw the error.
1285 factory_.OnIPAddressChanged();
1286 EXPECT_EQ(ERR_NETWORK_CHANGED,
1287 stream->ReadResponseHeaders(callback_.callback()));
1288 EXPECT_TRUE(factory_.require_confirmation());
1290 // Now attempting to request a stream to the same origin should create
1291 // a new session.
1293 QuicStreamRequest request2(&factory_);
1294 EXPECT_EQ(ERR_IO_PENDING,
1295 request2.Request(host_port_pair_, is_https_, privacy_mode_,
1296 /*cert_verify_flags=*/0, host_port_pair_.host(),
1297 "GET", net_log_, callback_.callback()));
1299 EXPECT_EQ(OK, callback_.WaitForResult());
1300 stream = request2.ReleaseStream();
1301 stream.reset(); // Will reset stream 3.
1303 EXPECT_TRUE(socket_data.AllReadDataConsumed());
1304 EXPECT_TRUE(socket_data.AllWriteDataConsumed());
1305 EXPECT_TRUE(socket_data2.AllReadDataConsumed());
1306 EXPECT_TRUE(socket_data2.AllWriteDataConsumed());
1309 TEST_P(QuicStreamFactoryTest, OnCertAdded) {
1310 MockRead reads[] = {
1311 MockRead(ASYNC, 0, 0) // EOF
1313 scoped_ptr<QuicEncryptedPacket> rst(ConstructRstPacket());
1314 std::vector<MockWrite> writes;
1315 writes.push_back(MockWrite(ASYNC, rst->data(), rst->length(), 1));
1316 DeterministicSocketData socket_data(reads, arraysize(reads),
1317 writes.empty() ? nullptr : &writes[0],
1318 writes.size());
1319 socket_factory_.AddSocketDataProvider(&socket_data);
1320 socket_data.StopAfter(1);
1322 MockRead reads2[] = {
1323 MockRead(ASYNC, 0, 0) // EOF
1325 DeterministicSocketData socket_data2(reads2, arraysize(reads2), nullptr, 0);
1326 socket_factory_.AddSocketDataProvider(&socket_data2);
1327 socket_data2.StopAfter(1);
1329 QuicStreamRequest request(&factory_);
1330 EXPECT_EQ(ERR_IO_PENDING,
1331 request.Request(host_port_pair_, is_https_, privacy_mode_,
1332 /*cert_verify_flags=*/0, host_port_pair_.host(),
1333 "GET", net_log_, callback_.callback()));
1335 EXPECT_EQ(OK, callback_.WaitForResult());
1336 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
1337 HttpRequestInfo request_info;
1338 EXPECT_EQ(OK, stream->InitializeStream(&request_info,
1339 DEFAULT_PRIORITY,
1340 net_log_, CompletionCallback()));
1342 // Add a cert and verify that stream saw the event.
1343 factory_.OnCertAdded(nullptr);
1344 EXPECT_EQ(ERR_CERT_DATABASE_CHANGED,
1345 stream->ReadResponseHeaders(callback_.callback()));
1346 EXPECT_FALSE(factory_.require_confirmation());
1348 // Now attempting to request a stream to the same origin should create
1349 // a new session.
1351 QuicStreamRequest request2(&factory_);
1352 EXPECT_EQ(ERR_IO_PENDING,
1353 request2.Request(host_port_pair_, is_https_, privacy_mode_,
1354 /*cert_verify_flags=*/0, host_port_pair_.host(),
1355 "GET", net_log_, callback_.callback()));
1357 EXPECT_EQ(OK, callback_.WaitForResult());
1358 stream = request2.ReleaseStream();
1359 stream.reset(); // Will reset stream 3.
1361 EXPECT_TRUE(socket_data.AllReadDataConsumed());
1362 EXPECT_TRUE(socket_data.AllWriteDataConsumed());
1363 EXPECT_TRUE(socket_data2.AllReadDataConsumed());
1364 EXPECT_TRUE(socket_data2.AllWriteDataConsumed());
1367 TEST_P(QuicStreamFactoryTest, OnCACertChanged) {
1368 MockRead reads[] = {
1369 MockRead(ASYNC, 0, 0) // EOF
1371 scoped_ptr<QuicEncryptedPacket> rst(ConstructRstPacket());
1372 std::vector<MockWrite> writes;
1373 writes.push_back(MockWrite(ASYNC, rst->data(), rst->length(), 1));
1374 DeterministicSocketData socket_data(reads, arraysize(reads),
1375 writes.empty() ? nullptr : &writes[0],
1376 writes.size());
1377 socket_factory_.AddSocketDataProvider(&socket_data);
1378 socket_data.StopAfter(1);
1380 MockRead reads2[] = {
1381 MockRead(ASYNC, 0, 0) // EOF
1383 DeterministicSocketData socket_data2(reads2, arraysize(reads2), nullptr, 0);
1384 socket_factory_.AddSocketDataProvider(&socket_data2);
1385 socket_data2.StopAfter(1);
1387 QuicStreamRequest request(&factory_);
1388 EXPECT_EQ(ERR_IO_PENDING,
1389 request.Request(host_port_pair_, is_https_, privacy_mode_,
1390 /*cert_verify_flags=*/0, host_port_pair_.host(),
1391 "GET", net_log_, callback_.callback()));
1393 EXPECT_EQ(OK, callback_.WaitForResult());
1394 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
1395 HttpRequestInfo request_info;
1396 EXPECT_EQ(OK, stream->InitializeStream(&request_info,
1397 DEFAULT_PRIORITY,
1398 net_log_, CompletionCallback()));
1400 // Change the CA cert and verify that stream saw the event.
1401 factory_.OnCACertChanged(nullptr);
1402 EXPECT_EQ(ERR_CERT_DATABASE_CHANGED,
1403 stream->ReadResponseHeaders(callback_.callback()));
1404 EXPECT_FALSE(factory_.require_confirmation());
1406 // Now attempting to request a stream to the same origin should create
1407 // a new session.
1409 QuicStreamRequest request2(&factory_);
1410 EXPECT_EQ(ERR_IO_PENDING,
1411 request2.Request(host_port_pair_, is_https_, privacy_mode_,
1412 /*cert_verify_flags=*/0, host_port_pair_.host(),
1413 "GET", net_log_, callback_.callback()));
1415 EXPECT_EQ(OK, callback_.WaitForResult());
1416 stream = request2.ReleaseStream();
1417 stream.reset(); // Will reset stream 3.
1419 EXPECT_TRUE(socket_data.AllReadDataConsumed());
1420 EXPECT_TRUE(socket_data.AllWriteDataConsumed());
1421 EXPECT_TRUE(socket_data2.AllReadDataConsumed());
1422 EXPECT_TRUE(socket_data2.AllWriteDataConsumed());
1425 TEST_P(QuicStreamFactoryTest, SharedCryptoConfig) {
1426 vector<string> cannoncial_suffixes;
1427 cannoncial_suffixes.push_back(string(".c.youtube.com"));
1428 cannoncial_suffixes.push_back(string(".googlevideo.com"));
1430 for (unsigned i = 0; i < cannoncial_suffixes.size(); ++i) {
1431 string r1_host_name("r1");
1432 string r2_host_name("r2");
1433 r1_host_name.append(cannoncial_suffixes[i]);
1434 r2_host_name.append(cannoncial_suffixes[i]);
1436 HostPortPair host_port_pair1(r1_host_name, 80);
1437 QuicCryptoClientConfig* crypto_config =
1438 QuicStreamFactoryPeer::GetCryptoConfig(&factory_);
1439 QuicServerId server_id1(host_port_pair1, is_https_, privacy_mode_);
1440 QuicCryptoClientConfig::CachedState* cached1 =
1441 crypto_config->LookupOrCreate(server_id1);
1442 EXPECT_FALSE(cached1->proof_valid());
1443 EXPECT_TRUE(cached1->source_address_token().empty());
1445 // Mutate the cached1 to have different data.
1446 // TODO(rtenneti): mutate other members of CachedState.
1447 cached1->set_source_address_token(r1_host_name);
1448 cached1->SetProofValid();
1450 HostPortPair host_port_pair2(r2_host_name, 80);
1451 QuicServerId server_id2(host_port_pair2, is_https_, privacy_mode_);
1452 QuicCryptoClientConfig::CachedState* cached2 =
1453 crypto_config->LookupOrCreate(server_id2);
1454 EXPECT_EQ(cached1->source_address_token(), cached2->source_address_token());
1455 EXPECT_TRUE(cached2->proof_valid());
1459 TEST_P(QuicStreamFactoryTest, CryptoConfigWhenProofIsInvalid) {
1460 vector<string> cannoncial_suffixes;
1461 cannoncial_suffixes.push_back(string(".c.youtube.com"));
1462 cannoncial_suffixes.push_back(string(".googlevideo.com"));
1464 for (unsigned i = 0; i < cannoncial_suffixes.size(); ++i) {
1465 string r3_host_name("r3");
1466 string r4_host_name("r4");
1467 r3_host_name.append(cannoncial_suffixes[i]);
1468 r4_host_name.append(cannoncial_suffixes[i]);
1470 HostPortPair host_port_pair1(r3_host_name, 80);
1471 QuicCryptoClientConfig* crypto_config =
1472 QuicStreamFactoryPeer::GetCryptoConfig(&factory_);
1473 QuicServerId server_id1(host_port_pair1, is_https_, privacy_mode_);
1474 QuicCryptoClientConfig::CachedState* cached1 =
1475 crypto_config->LookupOrCreate(server_id1);
1476 EXPECT_FALSE(cached1->proof_valid());
1477 EXPECT_TRUE(cached1->source_address_token().empty());
1479 // Mutate the cached1 to have different data.
1480 // TODO(rtenneti): mutate other members of CachedState.
1481 cached1->set_source_address_token(r3_host_name);
1482 cached1->SetProofInvalid();
1484 HostPortPair host_port_pair2(r4_host_name, 80);
1485 QuicServerId server_id2(host_port_pair2, is_https_, privacy_mode_);
1486 QuicCryptoClientConfig::CachedState* cached2 =
1487 crypto_config->LookupOrCreate(server_id2);
1488 EXPECT_NE(cached1->source_address_token(), cached2->source_address_token());
1489 EXPECT_TRUE(cached2->source_address_token().empty());
1490 EXPECT_FALSE(cached2->proof_valid());
1494 TEST_P(QuicStreamFactoryTest, RacingConnections) {
1495 if (!GetParam().enable_connection_racing)
1496 return;
1497 factory_.set_quic_server_info_factory(&quic_server_info_factory_);
1498 QuicStreamFactoryPeer::SetTaskRunner(&factory_, runner_.get());
1499 MockRead reads[] = {
1500 MockRead(ASYNC, OK, 0) // EOF
1502 DeterministicSocketData socket_data(reads, arraysize(reads), nullptr, 0);
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 crypto_client_stream_factory_.set_handshake_mode(
1514 MockCryptoClientStream::ZERO_RTT);
1515 host_resolver_.set_synchronous_mode(true);
1516 host_resolver_.rules()->AddIPLiteralRule(host_port_pair_.host(),
1517 "192.168.0.1", "");
1519 QuicStreamRequest request(&factory_);
1520 QuicServerId server_id(host_port_pair_, is_https_, privacy_mode_);
1521 EXPECT_EQ(ERR_IO_PENDING,
1522 request.Request(host_port_pair_, is_https_, privacy_mode_,
1523 /*cert_verify_flags=*/0, host_port_pair_.host(),
1524 "GET", net_log_, callback_.callback()));
1525 EXPECT_EQ(2u,
1526 QuicStreamFactoryPeer::GetNumberOfActiveJobs(&factory_, server_id));
1528 runner_->RunNextTask();
1530 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
1531 EXPECT_TRUE(stream.get());
1532 EXPECT_TRUE(socket_data.AllReadDataConsumed());
1533 EXPECT_TRUE(socket_data.AllWriteDataConsumed());
1534 EXPECT_EQ(0u,
1535 QuicStreamFactoryPeer::GetNumberOfActiveJobs(&factory_, server_id));
1538 TEST_P(QuicStreamFactoryTest, EnableNotLoadFromDiskCache) {
1539 factory_.set_quic_server_info_factory(&quic_server_info_factory_);
1540 QuicStreamFactoryPeer::SetTaskRunner(&factory_, runner_.get());
1541 QuicStreamFactoryPeer::SetDisableDiskCache(&factory_, true);
1543 MockRead reads[] = {
1544 MockRead(ASYNC, OK, 0) // EOF
1546 DeterministicSocketData socket_data(reads, arraysize(reads), nullptr, 0);
1547 socket_factory_.AddSocketDataProvider(&socket_data);
1548 socket_data.StopAfter(1);
1550 crypto_client_stream_factory_.set_handshake_mode(
1551 MockCryptoClientStream::ZERO_RTT);
1552 host_resolver_.set_synchronous_mode(true);
1553 host_resolver_.rules()->AddIPLiteralRule(host_port_pair_.host(),
1554 "192.168.0.1", "");
1556 QuicStreamRequest request(&factory_);
1557 EXPECT_EQ(OK, request.Request(host_port_pair_, is_https_, privacy_mode_,
1558 /*cert_verify_flags=*/0, host_port_pair_.host(),
1559 "GET", net_log_, callback_.callback()));
1561 // If we are waiting for disk cache, we would have posted a task. Verify that
1562 // the CancelWaitForDataReady task hasn't been posted.
1563 ASSERT_EQ(0u, runner_->GetPostedTasks().size());
1565 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
1566 EXPECT_TRUE(stream.get());
1567 EXPECT_TRUE(socket_data.AllReadDataConsumed());
1568 EXPECT_TRUE(socket_data.AllWriteDataConsumed());
1571 TEST_P(QuicStreamFactoryTest, BadPacketLoss) {
1572 factory_.set_quic_server_info_factory(&quic_server_info_factory_);
1573 QuicStreamFactoryPeer::SetTaskRunner(&factory_, runner_.get());
1574 QuicStreamFactoryPeer::SetDisableDiskCache(&factory_, true);
1575 QuicStreamFactoryPeer::SetMaxNumberOfLossyConnections(&factory_, 2);
1576 EXPECT_FALSE(
1577 QuicStreamFactoryPeer::IsQuicDisabled(&factory_, host_port_pair_.port()));
1578 EXPECT_EQ(0, QuicStreamFactoryPeer::GetNumberOfLossyConnections(
1579 &factory_, host_port_pair_.port()));
1581 MockRead reads[] = {
1582 MockRead(ASYNC, OK, 0) // EOF
1584 DeterministicSocketData socket_data(reads, arraysize(reads), nullptr, 0);
1585 socket_factory_.AddSocketDataProvider(&socket_data);
1586 socket_data.StopAfter(1);
1588 DeterministicSocketData socket_data2(nullptr, 0, nullptr, 0);
1589 socket_factory_.AddSocketDataProvider(&socket_data2);
1590 socket_data2.StopAfter(1);
1592 DeterministicSocketData socket_data3(nullptr, 0, nullptr, 0);
1593 socket_factory_.AddSocketDataProvider(&socket_data3);
1594 socket_data3.StopAfter(1);
1596 DeterministicSocketData socket_data4(nullptr, 0, nullptr, 0);
1597 socket_factory_.AddSocketDataProvider(&socket_data4);
1598 socket_data4.StopAfter(1);
1600 HostPortPair server2("mail.example.org", kDefaultServerPort);
1601 HostPortPair server3("docs.example.org", kDefaultServerPort);
1602 HostPortPair server4("images.example.org", kDefaultServerPort);
1604 crypto_client_stream_factory_.set_handshake_mode(
1605 MockCryptoClientStream::ZERO_RTT);
1606 host_resolver_.set_synchronous_mode(true);
1607 host_resolver_.rules()->AddIPLiteralRule(host_port_pair_.host(),
1608 "192.168.0.1", "");
1609 host_resolver_.rules()->AddIPLiteralRule(server2.host(), "192.168.0.1", "");
1610 host_resolver_.rules()->AddIPLiteralRule(server3.host(), "192.168.0.1", "");
1611 host_resolver_.rules()->AddIPLiteralRule(server4.host(), "192.168.0.1", "");
1613 QuicStreamRequest request(&factory_);
1614 EXPECT_EQ(OK, request.Request(host_port_pair_, is_https_, privacy_mode_,
1615 /*cert_verify_flags=*/0, host_port_pair_.host(),
1616 "GET", net_log_, callback_.callback()));
1618 QuicClientSession* session = QuicStreamFactoryPeer::GetActiveSession(
1619 &factory_, host_port_pair_, is_https_);
1621 DVLOG(1) << "Create 1st session and test packet loss";
1623 // Set packet_loss_rate to a lower value than packet_loss_threshold.
1624 EXPECT_FALSE(
1625 factory_.OnHandshakeConfirmed(session, /*packet_loss_rate=*/0.9f));
1626 EXPECT_TRUE(session->connection()->connected());
1627 EXPECT_TRUE(QuicStreamFactoryPeer::HasActiveSession(
1628 &factory_, host_port_pair_, is_https_));
1629 EXPECT_FALSE(
1630 QuicStreamFactoryPeer::IsQuicDisabled(&factory_, host_port_pair_.port()));
1631 EXPECT_EQ(0, QuicStreamFactoryPeer::GetNumberOfLossyConnections(
1632 &factory_, host_port_pair_.port()));
1634 // Set packet_loss_rate to a higher value than packet_loss_threshold only once
1635 // and that shouldn't close the session and it shouldn't disable QUIC.
1636 EXPECT_FALSE(
1637 factory_.OnHandshakeConfirmed(session, /*packet_loss_rate=*/1.0f));
1638 EXPECT_EQ(1, QuicStreamFactoryPeer::GetNumberOfLossyConnections(
1639 &factory_, host_port_pair_.port()));
1640 EXPECT_TRUE(session->connection()->connected());
1641 EXPECT_FALSE(
1642 QuicStreamFactoryPeer::IsQuicDisabled(&factory_, host_port_pair_.port()));
1643 EXPECT_TRUE(QuicStreamFactoryPeer::HasActiveSession(
1644 &factory_, host_port_pair_, is_https_));
1646 // Test N-in-a-row high packet loss connections.
1648 DVLOG(1) << "Create 2nd session and test packet loss";
1650 TestCompletionCallback callback2;
1651 QuicStreamRequest request2(&factory_);
1652 EXPECT_EQ(OK, request2.Request(server2, is_https_, privacy_mode_,
1653 /*cert_verify_flags=*/0, server2.host(), "GET",
1654 net_log_, callback2.callback()));
1655 QuicClientSession* session2 =
1656 QuicStreamFactoryPeer::GetActiveSession(&factory_, server2, is_https_);
1658 // If there is no packet loss during handshake confirmation, number of lossy
1659 // connections for the port should be 0.
1660 EXPECT_EQ(1, QuicStreamFactoryPeer::GetNumberOfLossyConnections(
1661 &factory_, server2.port()));
1662 EXPECT_FALSE(
1663 factory_.OnHandshakeConfirmed(session2, /*packet_loss_rate=*/0.9f));
1664 EXPECT_EQ(0, QuicStreamFactoryPeer::GetNumberOfLossyConnections(
1665 &factory_, server2.port()));
1666 EXPECT_FALSE(
1667 QuicStreamFactoryPeer::IsQuicDisabled(&factory_, server2.port()));
1669 // Set packet_loss_rate to a higher value than packet_loss_threshold only once
1670 // and that shouldn't close the session and it shouldn't disable QUIC.
1671 EXPECT_FALSE(
1672 factory_.OnHandshakeConfirmed(session2, /*packet_loss_rate=*/1.0f));
1673 EXPECT_EQ(1, QuicStreamFactoryPeer::GetNumberOfLossyConnections(
1674 &factory_, server2.port()));
1675 EXPECT_TRUE(session2->connection()->connected());
1676 EXPECT_FALSE(
1677 QuicStreamFactoryPeer::IsQuicDisabled(&factory_, server2.port()));
1678 EXPECT_TRUE(
1679 QuicStreamFactoryPeer::HasActiveSession(&factory_, server2, is_https_));
1681 DVLOG(1) << "Create 3rd session which also has packet loss";
1683 TestCompletionCallback callback3;
1684 QuicStreamRequest request3(&factory_);
1685 EXPECT_EQ(OK, request3.Request(server3, is_https_, privacy_mode_,
1686 /*cert_verify_flags=*/0, server3.host(), "GET",
1687 net_log_, callback3.callback()));
1688 QuicClientSession* session3 =
1689 QuicStreamFactoryPeer::GetActiveSession(&factory_, server3, is_https_);
1691 DVLOG(1) << "Create 4th session with packet loss and test IsQuicDisabled()";
1692 TestCompletionCallback callback4;
1693 QuicStreamRequest request4(&factory_);
1694 EXPECT_EQ(OK, request4.Request(server4, is_https_, privacy_mode_,
1695 /*cert_verify_flags=*/0, server4.host(), "GET",
1696 net_log_, callback4.callback()));
1697 QuicClientSession* session4 =
1698 QuicStreamFactoryPeer::GetActiveSession(&factory_, server4, is_https_);
1700 // Set packet_loss_rate to higher value than packet_loss_threshold 2nd time in
1701 // a row and that should close the session and disable QUIC.
1702 EXPECT_TRUE(
1703 factory_.OnHandshakeConfirmed(session3, /*packet_loss_rate=*/1.0f));
1704 EXPECT_EQ(2, QuicStreamFactoryPeer::GetNumberOfLossyConnections(
1705 &factory_, server3.port()));
1706 EXPECT_FALSE(session3->connection()->connected());
1707 EXPECT_TRUE(QuicStreamFactoryPeer::IsQuicDisabled(&factory_, server3.port()));
1708 EXPECT_FALSE(
1709 QuicStreamFactoryPeer::HasActiveSession(&factory_, server3, is_https_));
1710 EXPECT_FALSE(HasActiveSession(server3));
1712 // Set packet_loss_rate to higher value than packet_loss_threshold 3rd time in
1713 // a row and IsQuicDisabled() should close the session.
1714 EXPECT_TRUE(
1715 factory_.OnHandshakeConfirmed(session4, /*packet_loss_rate=*/1.0f));
1716 EXPECT_EQ(3, QuicStreamFactoryPeer::GetNumberOfLossyConnections(
1717 &factory_, server4.port()));
1718 EXPECT_FALSE(session4->connection()->connected());
1719 EXPECT_TRUE(QuicStreamFactoryPeer::IsQuicDisabled(&factory_, server4.port()));
1720 EXPECT_FALSE(
1721 QuicStreamFactoryPeer::HasActiveSession(&factory_, server4, is_https_));
1722 EXPECT_FALSE(HasActiveSession(server4));
1724 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
1725 EXPECT_TRUE(stream.get());
1726 scoped_ptr<QuicHttpStream> stream2 = request2.ReleaseStream();
1727 EXPECT_TRUE(stream2.get());
1728 scoped_ptr<QuicHttpStream> stream3 = request3.ReleaseStream();
1729 EXPECT_TRUE(stream3.get());
1730 scoped_ptr<QuicHttpStream> stream4 = request4.ReleaseStream();
1731 EXPECT_TRUE(stream4.get());
1732 EXPECT_TRUE(socket_data.AllReadDataConsumed());
1733 EXPECT_TRUE(socket_data.AllWriteDataConsumed());
1734 EXPECT_TRUE(socket_data2.AllReadDataConsumed());
1735 EXPECT_TRUE(socket_data2.AllWriteDataConsumed());
1736 EXPECT_TRUE(socket_data3.AllReadDataConsumed());
1737 EXPECT_TRUE(socket_data3.AllWriteDataConsumed());
1738 EXPECT_TRUE(socket_data4.AllReadDataConsumed());
1739 EXPECT_TRUE(socket_data4.AllWriteDataConsumed());
1742 } // namespace test
1743 } // namespace net