Rewrite AndroidSyncSettings to be significantly simpler.
[chromium-blink-merge.git] / net / quic / quic_stream_factory_test.cc
blob7b74d2d7bd476de783ad5fb58838707a469122df
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "net/quic/quic_stream_factory.h"
7 #include "base/run_loop.h"
8 #include "base/strings/string_util.h"
9 #include "net/base/test_data_directory.h"
10 #include "net/cert/cert_verifier.h"
11 #include "net/dns/mock_host_resolver.h"
12 #include "net/http/http_response_headers.h"
13 #include "net/http/http_response_info.h"
14 #include "net/http/http_util.h"
15 #include "net/http/transport_security_state.h"
16 #include "net/quic/crypto/crypto_handshake.h"
17 #include "net/quic/crypto/proof_verifier_chromium.h"
18 #include "net/quic/crypto/quic_decrypter.h"
19 #include "net/quic/crypto/quic_encrypter.h"
20 #include "net/quic/crypto/quic_server_info.h"
21 #include "net/quic/quic_http_stream.h"
22 #include "net/quic/quic_server_id.h"
23 #include "net/quic/test_tools/mock_clock.h"
24 #include "net/quic/test_tools/mock_crypto_client_stream_factory.h"
25 #include "net/quic/test_tools/mock_random.h"
26 #include "net/quic/test_tools/quic_test_packet_maker.h"
27 #include "net/quic/test_tools/quic_test_utils.h"
28 #include "net/quic/test_tools/test_task_runner.h"
29 #include "net/socket/socket_test_util.h"
30 #include "net/spdy/spdy_test_utils.h"
31 #include "net/ssl/channel_id_service.h"
32 #include "net/ssl/default_channel_id_store.h"
33 #include "net/test/cert_test_util.h"
34 #include "testing/gtest/include/gtest/gtest.h"
36 using base::StringPiece;
37 using std::ostream;
38 using std::string;
39 using std::vector;
41 namespace net {
42 namespace test {
44 namespace {
45 const char kDefaultServerHostName[] = "www.google.com";
46 const int kDefaultServerPort = 443;
48 // Run all tests with all the combinations of versions and
49 // enable_connection_racing.
50 struct TestParams {
51 TestParams(const QuicVersion version, bool enable_connection_racing)
52 : version(version), enable_connection_racing(enable_connection_racing) {}
54 friend ostream& operator<<(ostream& os, const TestParams& p) {
55 os << "{ version: " << QuicVersionToString(p.version);
56 os << " enable_connection_racing: " << p.enable_connection_racing << " }";
57 return os;
60 QuicVersion version;
61 bool enable_connection_racing;
64 // Constructs various test permutations.
65 vector<TestParams> GetTestParams() {
66 vector<TestParams> params;
67 QuicVersionVector all_supported_versions = QuicSupportedVersions();
68 for (const QuicVersion version : all_supported_versions) {
69 params.push_back(TestParams(version, false));
70 params.push_back(TestParams(version, true));
72 return params;
75 } // namespace anonymous
77 class QuicStreamFactoryPeer {
78 public:
79 static QuicCryptoClientConfig* GetCryptoConfig(QuicStreamFactory* factory) {
80 return &factory->crypto_config_;
83 static bool HasActiveSession(QuicStreamFactory* factory,
84 const HostPortPair& host_port_pair,
85 bool is_https) {
86 QuicServerId server_id(host_port_pair, is_https, PRIVACY_MODE_DISABLED);
87 return factory->HasActiveSession(server_id);
90 static QuicClientSession* GetActiveSession(
91 QuicStreamFactory* factory,
92 const HostPortPair& host_port_pair,
93 bool is_https) {
94 QuicServerId server_id(host_port_pair, is_https, PRIVACY_MODE_DISABLED);
95 DCHECK(factory->HasActiveSession(server_id));
96 return factory->active_sessions_[server_id];
99 static scoped_ptr<QuicHttpStream> CreateIfSessionExists(
100 QuicStreamFactory* factory,
101 const HostPortPair& host_port_pair,
102 bool is_https,
103 const BoundNetLog& net_log) {
104 QuicServerId server_id(host_port_pair, is_https, PRIVACY_MODE_DISABLED);
105 return factory->CreateIfSessionExists(server_id, net_log);
108 static bool IsLiveSession(QuicStreamFactory* factory,
109 QuicClientSession* session) {
110 for (QuicStreamFactory::SessionIdMap::iterator it =
111 factory->all_sessions_.begin();
112 it != factory->all_sessions_.end(); ++it) {
113 if (it->first == session)
114 return true;
116 return false;
119 static void DisableConnectionPooling(QuicStreamFactory* factory) {
120 factory->disable_connection_pooling_ = true;
123 static void SetTaskRunner(QuicStreamFactory* factory,
124 base::TaskRunner* task_runner) {
125 factory->task_runner_ = task_runner;
128 static void SetLoadServerInfoTimeout(QuicStreamFactory* factory,
129 size_t load_server_info_timeout) {
130 factory->load_server_info_timeout_ms_ = load_server_info_timeout;
133 static void SetEnableConnectionRacing(QuicStreamFactory* factory,
134 bool enable_connection_racing) {
135 factory->enable_connection_racing_ = enable_connection_racing;
138 static void SetDisableDiskCache(QuicStreamFactory* factory,
139 bool disable_disk_cache) {
140 factory->disable_disk_cache_ = disable_disk_cache;
143 static size_t GetNumberOfActiveJobs(QuicStreamFactory* factory,
144 const QuicServerId& server_id) {
145 return (factory->active_jobs_[server_id]).size();
149 class MockQuicServerInfo : public QuicServerInfo {
150 public:
151 MockQuicServerInfo(const QuicServerId& server_id)
152 : QuicServerInfo(server_id) {}
153 ~MockQuicServerInfo() override {}
155 void Start() override {}
157 int WaitForDataReady(const CompletionCallback& callback) override {
158 return ERR_IO_PENDING;
161 void ResetWaitForDataReadyCallback() override {}
163 void CancelWaitForDataReadyCallback() override {}
165 bool IsDataReady() override { return false; }
167 bool IsReadyToPersist() override { return false; }
169 void Persist() override {}
171 void OnExternalCacheHit() override {}
174 class MockQuicServerInfoFactory : public QuicServerInfoFactory {
175 public:
176 MockQuicServerInfoFactory() {}
177 ~MockQuicServerInfoFactory() override {}
179 QuicServerInfo* GetForServer(const QuicServerId& server_id) override {
180 return new MockQuicServerInfo(server_id);
184 class QuicStreamFactoryTest : public ::testing::TestWithParam<TestParams> {
185 protected:
186 QuicStreamFactoryTest()
187 : random_generator_(0),
188 clock_(new MockClock()),
189 runner_(new TestTaskRunner(clock_)),
190 maker_(GetParam().version, 0, clock_),
191 cert_verifier_(CertVerifier::CreateDefault()),
192 channel_id_service_(
193 new ChannelIDService(new DefaultChannelIDStore(nullptr),
194 base::MessageLoopProxy::current())),
195 factory_(&host_resolver_,
196 &socket_factory_,
197 base::WeakPtr<HttpServerProperties>(),
198 cert_verifier_.get(),
199 channel_id_service_.get(),
200 &transport_security_state_,
201 &crypto_client_stream_factory_,
202 &random_generator_,
203 clock_,
204 kDefaultMaxPacketSize,
205 std::string(),
206 SupportedVersions(GetParam().version),
207 /*enable_port_selection=*/true,
208 /*always_require_handshake_confirmation=*/false,
209 /*disable_connection_pooling=*/false,
210 /*load_server_info_timeout=*/0u,
211 /*load_server_info_timeout_srtt_multiplier=*/0.0f,
212 /*enable_truncated_connection_ids=*/true,
213 /*enable_connection_racing=*/false,
214 /*disable_disk_cache=*/false,
215 QuicTagVector()),
216 host_port_pair_(kDefaultServerHostName, kDefaultServerPort),
217 is_https_(false),
218 privacy_mode_(PRIVACY_MODE_DISABLED) {
219 factory_.set_require_confirmation(false);
220 clock_->AdvanceTime(QuicTime::Delta::FromSeconds(1));
221 QuicStreamFactoryPeer::SetEnableConnectionRacing(
222 &factory_, GetParam().enable_connection_racing);
225 scoped_ptr<QuicHttpStream> CreateIfSessionExists(
226 const HostPortPair& host_port_pair,
227 const BoundNetLog& net_log) {
228 return QuicStreamFactoryPeer::CreateIfSessionExists(
229 &factory_, host_port_pair, false, net_log_);
232 int GetSourcePortForNewSession(const HostPortPair& destination) {
233 return GetSourcePortForNewSessionInner(destination, false);
236 int GetSourcePortForNewSessionAndGoAway(
237 const HostPortPair& destination) {
238 return GetSourcePortForNewSessionInner(destination, true);
241 int GetSourcePortForNewSessionInner(const HostPortPair& destination,
242 bool goaway_received) {
243 // Should only be called if there is no active session for this destination.
244 EXPECT_EQ(nullptr, CreateIfSessionExists(destination, net_log_).get());
245 size_t socket_count = socket_factory_.udp_client_sockets().size();
247 MockRead reads[] = {
248 MockRead(ASYNC, OK, 0) // EOF
250 DeterministicSocketData socket_data(reads, arraysize(reads), nullptr, 0);
251 socket_data.StopAfter(1);
252 socket_factory_.AddSocketDataProvider(&socket_data);
254 QuicStreamRequest request(&factory_);
255 EXPECT_EQ(ERR_IO_PENDING,
256 request.Request(destination,
257 is_https_,
258 privacy_mode_,
259 "GET",
260 net_log_,
261 callback_.callback()));
263 EXPECT_EQ(OK, callback_.WaitForResult());
264 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
265 EXPECT_TRUE(stream.get());
266 stream.reset();
268 QuicClientSession* session = QuicStreamFactoryPeer::GetActiveSession(
269 &factory_, destination, is_https_);
271 if (socket_count + 1 != socket_factory_.udp_client_sockets().size()) {
272 EXPECT_TRUE(false);
273 return 0;
276 IPEndPoint endpoint;
277 socket_factory_.
278 udp_client_sockets()[socket_count]->GetLocalAddress(&endpoint);
279 int port = endpoint.port();
280 if (goaway_received) {
281 QuicGoAwayFrame goaway(QUIC_NO_ERROR, 1, "");
282 session->OnGoAway(goaway);
285 factory_.OnSessionClosed(session);
286 EXPECT_EQ(nullptr, CreateIfSessionExists(destination, net_log_).get());
287 EXPECT_TRUE(socket_data.at_read_eof());
288 EXPECT_TRUE(socket_data.at_write_eof());
289 return port;
292 scoped_ptr<QuicEncryptedPacket> ConstructRstPacket() {
293 QuicStreamId stream_id = kClientDataStreamId1;
294 return maker_.MakeRstPacket(
295 1, true, stream_id,
296 AdjustErrorForVersion(QUIC_RST_ACKNOWLEDGEMENT, GetParam().version));
299 MockQuicServerInfoFactory quic_server_info_factory_;
300 MockHostResolver host_resolver_;
301 DeterministicMockClientSocketFactory socket_factory_;
302 MockCryptoClientStreamFactory crypto_client_stream_factory_;
303 MockRandom random_generator_;
304 MockClock* clock_; // Owned by factory_.
305 scoped_refptr<TestTaskRunner> runner_;
306 QuicTestPacketMaker maker_;
307 scoped_ptr<CertVerifier> cert_verifier_;
308 scoped_ptr<ChannelIDService> channel_id_service_;
309 TransportSecurityState transport_security_state_;
310 QuicStreamFactory factory_;
311 HostPortPair host_port_pair_;
312 bool is_https_;
313 PrivacyMode privacy_mode_;
314 BoundNetLog net_log_;
315 TestCompletionCallback callback_;
318 INSTANTIATE_TEST_CASE_P(Version,
319 QuicStreamFactoryTest,
320 ::testing::ValuesIn(GetTestParams()));
322 TEST_P(QuicStreamFactoryTest, CreateIfSessionExists) {
323 EXPECT_EQ(nullptr, CreateIfSessionExists(host_port_pair_, net_log_).get());
326 TEST_P(QuicStreamFactoryTest, Create) {
327 MockRead reads[] = {
328 MockRead(ASYNC, OK, 0) // EOF
330 DeterministicSocketData socket_data(reads, arraysize(reads), nullptr, 0);
331 socket_factory_.AddSocketDataProvider(&socket_data);
332 socket_data.StopAfter(1);
334 QuicStreamRequest request(&factory_);
335 EXPECT_EQ(ERR_IO_PENDING,
336 request.Request(host_port_pair_,
337 is_https_,
338 privacy_mode_,
339 "GET",
340 net_log_,
341 callback_.callback()));
343 EXPECT_EQ(OK, callback_.WaitForResult());
344 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
345 EXPECT_TRUE(stream.get());
347 // Will reset stream 3.
348 stream = CreateIfSessionExists(host_port_pair_, net_log_);
349 EXPECT_TRUE(stream.get());
351 // TODO(rtenneti): We should probably have a tests that HTTP and HTTPS result
352 // in streams on different sessions.
353 QuicStreamRequest request2(&factory_);
354 EXPECT_EQ(OK,
355 request2.Request(host_port_pair_,
356 is_https_,
357 privacy_mode_,
358 "GET",
359 net_log_,
360 callback_.callback()));
361 stream = request2.ReleaseStream(); // Will reset stream 5.
362 stream.reset(); // Will reset stream 7.
364 EXPECT_TRUE(socket_data.at_read_eof());
365 EXPECT_TRUE(socket_data.at_write_eof());
368 TEST_P(QuicStreamFactoryTest, CreateZeroRtt) {
369 MockRead reads[] = {
370 MockRead(ASYNC, OK, 0) // EOF
372 DeterministicSocketData socket_data(reads, arraysize(reads), nullptr, 0);
373 socket_factory_.AddSocketDataProvider(&socket_data);
374 socket_data.StopAfter(1);
376 crypto_client_stream_factory_.set_handshake_mode(
377 MockCryptoClientStream::ZERO_RTT);
378 host_resolver_.set_synchronous_mode(true);
379 host_resolver_.rules()->AddIPLiteralRule(host_port_pair_.host(),
380 "192.168.0.1", "");
382 QuicStreamRequest request(&factory_);
383 EXPECT_EQ(OK,
384 request.Request(host_port_pair_,
385 is_https_,
386 privacy_mode_,
387 "GET",
388 net_log_,
389 callback_.callback()));
391 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
392 EXPECT_TRUE(stream.get());
393 EXPECT_TRUE(socket_data.at_read_eof());
394 EXPECT_TRUE(socket_data.at_write_eof());
397 TEST_P(QuicStreamFactoryTest, CreateZeroRttPost) {
398 MockRead reads[] = {
399 MockRead(ASYNC, OK, 0) // EOF
401 DeterministicSocketData socket_data(reads, arraysize(reads), nullptr, 0);
402 socket_factory_.AddSocketDataProvider(&socket_data);
403 socket_data.StopAfter(1);
405 crypto_client_stream_factory_.set_handshake_mode(
406 MockCryptoClientStream::ZERO_RTT);
407 host_resolver_.set_synchronous_mode(true);
408 host_resolver_.rules()->AddIPLiteralRule(host_port_pair_.host(),
409 "192.168.0.1", "");
411 QuicStreamRequest request(&factory_);
412 // Posts require handshake confirmation, so this will return asynchronously.
413 EXPECT_EQ(ERR_IO_PENDING,
414 request.Request(host_port_pair_,
415 is_https_,
416 privacy_mode_,
417 "POST",
418 net_log_,
419 callback_.callback()));
421 // Confirm the handshake and verify that the stream is created.
422 crypto_client_stream_factory_.last_stream()->SendOnCryptoHandshakeEvent(
423 QuicSession::HANDSHAKE_CONFIRMED);
425 EXPECT_EQ(OK, callback_.WaitForResult());
426 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
427 EXPECT_TRUE(stream.get());
428 EXPECT_TRUE(socket_data.at_read_eof());
429 EXPECT_TRUE(socket_data.at_write_eof());
432 TEST_P(QuicStreamFactoryTest, CreateHttpVsHttps) {
433 MockRead reads[] = {
434 MockRead(ASYNC, OK, 0) // EOF
436 DeterministicSocketData socket_data1(reads, arraysize(reads), nullptr, 0);
437 DeterministicSocketData socket_data2(reads, arraysize(reads), nullptr, 0);
438 socket_factory_.AddSocketDataProvider(&socket_data1);
439 socket_factory_.AddSocketDataProvider(&socket_data2);
440 socket_data1.StopAfter(1);
441 socket_data2.StopAfter(1);
443 QuicStreamRequest request(&factory_);
444 EXPECT_EQ(ERR_IO_PENDING,
445 request.Request(host_port_pair_,
446 is_https_,
447 privacy_mode_,
448 "GET",
449 net_log_,
450 callback_.callback()));
452 EXPECT_EQ(OK, callback_.WaitForResult());
453 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
454 EXPECT_TRUE(stream.get());
456 QuicStreamRequest request2(&factory_);
457 EXPECT_EQ(ERR_IO_PENDING,
458 request2.Request(host_port_pair_,
459 !is_https_,
460 privacy_mode_,
461 "GET",
462 net_log_,
463 callback_.callback()));
464 EXPECT_EQ(OK, callback_.WaitForResult());
465 stream = request2.ReleaseStream();
466 EXPECT_TRUE(stream.get());
467 stream.reset();
469 EXPECT_NE(QuicStreamFactoryPeer::GetActiveSession(
470 &factory_, host_port_pair_, is_https_),
471 QuicStreamFactoryPeer::GetActiveSession(
472 &factory_, host_port_pair_, !is_https_));
474 EXPECT_TRUE(socket_data1.at_read_eof());
475 EXPECT_TRUE(socket_data1.at_write_eof());
476 EXPECT_TRUE(socket_data2.at_read_eof());
477 EXPECT_TRUE(socket_data2.at_write_eof());
480 TEST_P(QuicStreamFactoryTest, Pooling) {
481 MockRead reads[] = {
482 MockRead(ASYNC, OK, 0) // EOF
484 DeterministicSocketData socket_data(reads, arraysize(reads), nullptr, 0);
485 socket_factory_.AddSocketDataProvider(&socket_data);
486 socket_data.StopAfter(1);
488 HostPortPair server2("mail.google.com", kDefaultServerPort);
489 host_resolver_.set_synchronous_mode(true);
490 host_resolver_.rules()->AddIPLiteralRule(
491 kDefaultServerHostName, "192.168.0.1", "");
492 host_resolver_.rules()->AddIPLiteralRule(
493 "mail.google.com", "192.168.0.1", "");
495 QuicStreamRequest request(&factory_);
496 EXPECT_EQ(OK,
497 request.Request(host_port_pair_,
498 is_https_,
499 privacy_mode_,
500 "GET",
501 net_log_,
502 callback_.callback()));
503 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
504 EXPECT_TRUE(stream.get());
506 TestCompletionCallback callback;
507 QuicStreamRequest request2(&factory_);
508 EXPECT_EQ(OK,
509 request2.Request(server2,
510 is_https_,
511 privacy_mode_,
512 "GET",
513 net_log_,
514 callback.callback()));
515 scoped_ptr<QuicHttpStream> stream2 = request2.ReleaseStream();
516 EXPECT_TRUE(stream2.get());
518 EXPECT_EQ(
519 QuicStreamFactoryPeer::GetActiveSession(
520 &factory_, host_port_pair_, is_https_),
521 QuicStreamFactoryPeer::GetActiveSession(&factory_, server2, is_https_));
523 EXPECT_TRUE(socket_data.at_read_eof());
524 EXPECT_TRUE(socket_data.at_write_eof());
527 TEST_P(QuicStreamFactoryTest, NoPoolingIfDisabled) {
528 MockRead reads[] = {
529 MockRead(ASYNC, OK, 0) // EOF
531 DeterministicSocketData socket_data1(reads, arraysize(reads), nullptr, 0);
532 DeterministicSocketData socket_data2(reads, arraysize(reads), nullptr, 0);
533 socket_factory_.AddSocketDataProvider(&socket_data1);
534 socket_factory_.AddSocketDataProvider(&socket_data2);
535 socket_data1.StopAfter(1);
536 socket_data2.StopAfter(1);
538 HostPortPair server2("mail.google.com", kDefaultServerPort);
539 host_resolver_.set_synchronous_mode(true);
540 host_resolver_.rules()->AddIPLiteralRule(
541 kDefaultServerHostName, "192.168.0.1", "");
542 host_resolver_.rules()->AddIPLiteralRule(
543 "mail.google.com", "192.168.0.1", "");
545 // Disable connection pooling.
546 QuicStreamFactoryPeer::DisableConnectionPooling(&factory_);
548 QuicStreamRequest request(&factory_);
549 EXPECT_EQ(OK,
550 request.Request(host_port_pair_,
551 is_https_,
552 privacy_mode_,
553 "GET",
554 net_log_,
555 callback_.callback()));
556 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
557 EXPECT_TRUE(stream.get());
559 TestCompletionCallback callback;
560 QuicStreamRequest request2(&factory_);
561 EXPECT_EQ(OK,
562 request2.Request(server2,
563 is_https_,
564 privacy_mode_,
565 "GET",
566 net_log_,
567 callback.callback()));
568 scoped_ptr<QuicHttpStream> stream2 = request2.ReleaseStream();
569 EXPECT_TRUE(stream2.get());
571 EXPECT_NE(
572 QuicStreamFactoryPeer::GetActiveSession(
573 &factory_, host_port_pair_, is_https_),
574 QuicStreamFactoryPeer::GetActiveSession(&factory_, server2, is_https_));
576 EXPECT_TRUE(socket_data1.at_read_eof());
577 EXPECT_TRUE(socket_data1.at_write_eof());
578 EXPECT_TRUE(socket_data2.at_read_eof());
579 EXPECT_TRUE(socket_data2.at_write_eof());
582 TEST_P(QuicStreamFactoryTest, NoPoolingAfterGoAway) {
583 MockRead reads[] = {
584 MockRead(ASYNC, OK, 0) // EOF
586 DeterministicSocketData socket_data1(reads, arraysize(reads), nullptr, 0);
587 DeterministicSocketData socket_data2(reads, arraysize(reads), nullptr, 0);
588 socket_factory_.AddSocketDataProvider(&socket_data1);
589 socket_factory_.AddSocketDataProvider(&socket_data2);
590 socket_data1.StopAfter(1);
591 socket_data2.StopAfter(1);
593 HostPortPair server2("mail.google.com", kDefaultServerPort);
594 host_resolver_.set_synchronous_mode(true);
595 host_resolver_.rules()->AddIPLiteralRule(
596 kDefaultServerHostName, "192.168.0.1", "");
597 host_resolver_.rules()->AddIPLiteralRule(
598 "mail.google.com", "192.168.0.1", "");
600 QuicStreamRequest request(&factory_);
601 EXPECT_EQ(OK,
602 request.Request(host_port_pair_,
603 is_https_,
604 privacy_mode_,
605 "GET",
606 net_log_,
607 callback_.callback()));
608 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
609 EXPECT_TRUE(stream.get());
611 TestCompletionCallback callback;
612 QuicStreamRequest request2(&factory_);
613 EXPECT_EQ(OK,
614 request2.Request(server2,
615 is_https_,
616 privacy_mode_,
617 "GET",
618 net_log_,
619 callback.callback()));
620 scoped_ptr<QuicHttpStream> stream2 = request2.ReleaseStream();
621 EXPECT_TRUE(stream2.get());
623 factory_.OnSessionGoingAway(QuicStreamFactoryPeer::GetActiveSession(
624 &factory_, host_port_pair_, is_https_));
625 EXPECT_FALSE(QuicStreamFactoryPeer::HasActiveSession(
626 &factory_, host_port_pair_, is_https_));
627 EXPECT_FALSE(QuicStreamFactoryPeer::HasActiveSession(
628 &factory_, server2, is_https_));
630 TestCompletionCallback callback3;
631 QuicStreamRequest request3(&factory_);
632 EXPECT_EQ(OK,
633 request3.Request(server2,
634 is_https_,
635 privacy_mode_,
636 "GET",
637 net_log_,
638 callback3.callback()));
639 scoped_ptr<QuicHttpStream> stream3 = request3.ReleaseStream();
640 EXPECT_TRUE(stream3.get());
642 EXPECT_TRUE(QuicStreamFactoryPeer::HasActiveSession(
643 &factory_, server2, is_https_));
645 EXPECT_TRUE(socket_data1.at_read_eof());
646 EXPECT_TRUE(socket_data1.at_write_eof());
647 EXPECT_TRUE(socket_data2.at_read_eof());
648 EXPECT_TRUE(socket_data2.at_write_eof());
651 TEST_P(QuicStreamFactoryTest, HttpsPooling) {
652 MockRead reads[] = {
653 MockRead(ASYNC, OK, 0) // EOF
655 DeterministicSocketData socket_data(reads, arraysize(reads), nullptr, 0);
656 socket_factory_.AddSocketDataProvider(&socket_data);
657 socket_data.StopAfter(1);
659 HostPortPair server1("www.example.org", 443);
660 HostPortPair server2("mail.example.org", 443);
662 // Load a cert that is valid for:
663 // www.example.org (server1)
664 // mail.example.org (server2)
665 // www.example.com
666 base::FilePath certs_dir = GetTestCertsDirectory();
667 scoped_refptr<X509Certificate> test_cert(
668 ImportCertFromFile(certs_dir, "spdy_pooling.pem"));
669 ASSERT_NE(static_cast<X509Certificate*>(nullptr), test_cert.get());
670 ProofVerifyDetailsChromium verify_details;
671 verify_details.cert_verify_result.verified_cert = test_cert;
672 verify_details.cert_verify_result.is_issued_by_known_root = true;
673 crypto_client_stream_factory_.set_proof_verify_details(&verify_details);
675 host_resolver_.set_synchronous_mode(true);
676 host_resolver_.rules()->AddIPLiteralRule(server1.host(), "192.168.0.1", "");
677 host_resolver_.rules()->AddIPLiteralRule(server2.host(), "192.168.0.1", "");
679 QuicStreamRequest request(&factory_);
680 is_https_ = true;
681 EXPECT_EQ(OK,
682 request.Request(server1,
683 is_https_,
684 privacy_mode_,
685 "GET",
686 net_log_,
687 callback_.callback()));
688 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
689 EXPECT_TRUE(stream.get());
691 TestCompletionCallback callback;
692 QuicStreamRequest request2(&factory_);
693 EXPECT_EQ(OK,
694 request2.Request(server2,
695 is_https_,
696 privacy_mode_,
697 "GET",
698 net_log_,
699 callback_.callback()));
700 scoped_ptr<QuicHttpStream> stream2 = request2.ReleaseStream();
701 EXPECT_TRUE(stream2.get());
703 EXPECT_EQ(QuicStreamFactoryPeer::GetActiveSession(
704 &factory_, server1, is_https_),
705 QuicStreamFactoryPeer::GetActiveSession(
706 &factory_, server2, is_https_));
708 EXPECT_TRUE(socket_data.at_read_eof());
709 EXPECT_TRUE(socket_data.at_write_eof());
712 TEST_P(QuicStreamFactoryTest, NoHttpsPoolingIfDisabled) {
713 MockRead reads[] = {
714 MockRead(ASYNC, OK, 0) // EOF
716 DeterministicSocketData socket_data1(reads, arraysize(reads), nullptr, 0);
717 DeterministicSocketData socket_data2(reads, arraysize(reads), nullptr, 0);
718 socket_factory_.AddSocketDataProvider(&socket_data1);
719 socket_factory_.AddSocketDataProvider(&socket_data2);
720 socket_data1.StopAfter(1);
721 socket_data2.StopAfter(1);
723 HostPortPair server1("www.example.org", 443);
724 HostPortPair server2("mail.example.org", 443);
726 // Load a cert that is valid for:
727 // www.example.org (server1)
728 // mail.example.org (server2)
729 // www.example.com
730 base::FilePath certs_dir = GetTestCertsDirectory();
731 scoped_refptr<X509Certificate> test_cert(
732 ImportCertFromFile(certs_dir, "spdy_pooling.pem"));
733 ASSERT_NE(static_cast<X509Certificate*>(nullptr), test_cert.get());
734 ProofVerifyDetailsChromium verify_details;
735 verify_details.cert_verify_result.verified_cert = test_cert;
736 verify_details.cert_verify_result.is_issued_by_known_root = true;
737 crypto_client_stream_factory_.set_proof_verify_details(&verify_details);
739 host_resolver_.set_synchronous_mode(true);
740 host_resolver_.rules()->AddIPLiteralRule(server1.host(), "192.168.0.1", "");
741 host_resolver_.rules()->AddIPLiteralRule(server2.host(), "192.168.0.1", "");
743 // Disable connection pooling.
744 QuicStreamFactoryPeer::DisableConnectionPooling(&factory_);
746 QuicStreamRequest request(&factory_);
747 is_https_ = true;
748 EXPECT_EQ(OK,
749 request.Request(server1,
750 is_https_,
751 privacy_mode_,
752 "GET",
753 net_log_,
754 callback_.callback()));
755 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
756 EXPECT_TRUE(stream.get());
758 TestCompletionCallback callback;
759 QuicStreamRequest request2(&factory_);
760 EXPECT_EQ(OK,
761 request2.Request(server2,
762 is_https_,
763 privacy_mode_,
764 "GET",
765 net_log_,
766 callback_.callback()));
767 scoped_ptr<QuicHttpStream> stream2 = request2.ReleaseStream();
768 EXPECT_TRUE(stream2.get());
770 EXPECT_NE(QuicStreamFactoryPeer::GetActiveSession(
771 &factory_, server1, is_https_),
772 QuicStreamFactoryPeer::GetActiveSession(
773 &factory_, server2, is_https_));
775 EXPECT_TRUE(socket_data1.at_read_eof());
776 EXPECT_TRUE(socket_data1.at_write_eof());
777 EXPECT_TRUE(socket_data2.at_read_eof());
778 EXPECT_TRUE(socket_data2.at_write_eof());
781 TEST_P(QuicStreamFactoryTest, NoHttpsPoolingWithCertMismatch) {
782 MockRead reads[] = {
783 MockRead(ASYNC, OK, 0) // EOF
785 DeterministicSocketData socket_data1(reads, arraysize(reads), nullptr, 0);
786 DeterministicSocketData socket_data2(reads, arraysize(reads), nullptr, 0);
787 socket_factory_.AddSocketDataProvider(&socket_data1);
788 socket_factory_.AddSocketDataProvider(&socket_data2);
789 socket_data1.StopAfter(1);
790 socket_data2.StopAfter(1);
792 HostPortPair server1("www.example.org", 443);
793 HostPortPair server2("mail.google.com", 443);
795 // Load a cert that is valid for:
796 // www.example.org (server1)
797 // mail.example.org
798 // www.example.com
799 // But is not valid for mail.google.com (server2).
800 base::FilePath certs_dir = GetTestCertsDirectory();
801 scoped_refptr<X509Certificate> test_cert(
802 ImportCertFromFile(certs_dir, "spdy_pooling.pem"));
803 ASSERT_NE(static_cast<X509Certificate*>(nullptr), test_cert.get());
804 ProofVerifyDetailsChromium verify_details;
805 verify_details.cert_verify_result.verified_cert = test_cert;
806 crypto_client_stream_factory_.set_proof_verify_details(&verify_details);
809 host_resolver_.set_synchronous_mode(true);
810 host_resolver_.rules()->AddIPLiteralRule(server1.host(), "192.168.0.1", "");
811 host_resolver_.rules()->AddIPLiteralRule(server2.host(), "192.168.0.1", "");
813 QuicStreamRequest request(&factory_);
814 is_https_ = true;
815 EXPECT_EQ(OK,
816 request.Request(server1,
817 is_https_,
818 privacy_mode_,
819 "GET",
820 net_log_,
821 callback_.callback()));
822 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
823 EXPECT_TRUE(stream.get());
825 TestCompletionCallback callback;
826 QuicStreamRequest request2(&factory_);
827 EXPECT_EQ(OK,
828 request2.Request(server2,
829 is_https_,
830 privacy_mode_,
831 "GET",
832 net_log_,
833 callback_.callback()));
834 scoped_ptr<QuicHttpStream> stream2 = request2.ReleaseStream();
835 EXPECT_TRUE(stream2.get());
837 EXPECT_NE(QuicStreamFactoryPeer::GetActiveSession(
838 &factory_, server1, is_https_),
839 QuicStreamFactoryPeer::GetActiveSession(
840 &factory_, server2, is_https_));
842 EXPECT_TRUE(socket_data1.at_read_eof());
843 EXPECT_TRUE(socket_data1.at_write_eof());
844 EXPECT_TRUE(socket_data2.at_read_eof());
845 EXPECT_TRUE(socket_data2.at_write_eof());
848 TEST_P(QuicStreamFactoryTest, HttpsPoolingWithMatchingPins) {
849 MockRead reads[] = {
850 MockRead(ASYNC, OK, 0) // EOF
852 DeterministicSocketData socket_data(reads, arraysize(reads), nullptr, 0);
853 socket_factory_.AddSocketDataProvider(&socket_data);
854 socket_data.StopAfter(1);
856 HostPortPair server1("www.example.org", 443);
857 HostPortPair server2("mail.example.org", 443);
858 uint8 primary_pin = 1;
859 uint8 backup_pin = 2;
860 test::AddPin(&transport_security_state_, "mail.example.org", primary_pin,
861 backup_pin);
863 // Load a cert that is valid for:
864 // www.example.org (server1)
865 // mail.example.org (server2)
866 base::FilePath certs_dir = GetTestCertsDirectory();
867 scoped_refptr<X509Certificate> test_cert(
868 ImportCertFromFile(certs_dir, "spdy_pooling.pem"));
869 ASSERT_NE(static_cast<X509Certificate*>(nullptr), test_cert.get());
870 ProofVerifyDetailsChromium verify_details;
871 verify_details.cert_verify_result.verified_cert = test_cert;
872 verify_details.cert_verify_result.is_issued_by_known_root = true;
873 verify_details.cert_verify_result.public_key_hashes.push_back(
874 test::GetTestHashValue(primary_pin));
875 crypto_client_stream_factory_.set_proof_verify_details(&verify_details);
878 host_resolver_.set_synchronous_mode(true);
879 host_resolver_.rules()->AddIPLiteralRule(server1.host(), "192.168.0.1", "");
880 host_resolver_.rules()->AddIPLiteralRule(server2.host(), "192.168.0.1", "");
882 QuicStreamRequest request(&factory_);
883 is_https_ = true;
884 EXPECT_EQ(OK,
885 request.Request(server1,
886 is_https_,
887 privacy_mode_,
888 "GET",
889 net_log_,
890 callback_.callback()));
891 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
892 EXPECT_TRUE(stream.get());
894 TestCompletionCallback callback;
895 QuicStreamRequest request2(&factory_);
896 EXPECT_EQ(OK,
897 request2.Request(server2,
898 is_https_,
899 privacy_mode_,
900 "GET",
901 net_log_,
902 callback_.callback()));
903 scoped_ptr<QuicHttpStream> stream2 = request2.ReleaseStream();
904 EXPECT_TRUE(stream2.get());
906 EXPECT_EQ(QuicStreamFactoryPeer::GetActiveSession(
907 &factory_, server1, is_https_),
908 QuicStreamFactoryPeer::GetActiveSession(
909 &factory_, server2, is_https_));
911 EXPECT_TRUE(socket_data.at_read_eof());
912 EXPECT_TRUE(socket_data.at_write_eof());
915 TEST_P(QuicStreamFactoryTest, NoHttpsPoolingWithMatchingPinsIfDisabled) {
916 MockRead reads[] = {
917 MockRead(ASYNC, OK, 0) // EOF
919 DeterministicSocketData socket_data1(reads, arraysize(reads), nullptr, 0);
920 DeterministicSocketData socket_data2(reads, arraysize(reads), nullptr, 0);
921 socket_factory_.AddSocketDataProvider(&socket_data1);
922 socket_factory_.AddSocketDataProvider(&socket_data2);
923 socket_data1.StopAfter(1);
924 socket_data2.StopAfter(1);
926 HostPortPair server1("www.example.org", 443);
927 HostPortPair server2("mail.example.org", 443);
928 uint8 primary_pin = 1;
929 uint8 backup_pin = 2;
930 test::AddPin(&transport_security_state_, "mail.example.org", primary_pin,
931 backup_pin);
933 // Load a cert that is valid for:
934 // www.example.org (server1)
935 // mail.example.org (server2)
936 base::FilePath certs_dir = GetTestCertsDirectory();
937 scoped_refptr<X509Certificate> test_cert(
938 ImportCertFromFile(certs_dir, "spdy_pooling.pem"));
939 ASSERT_NE(static_cast<X509Certificate*>(nullptr), test_cert.get());
940 ProofVerifyDetailsChromium verify_details;
941 verify_details.cert_verify_result.verified_cert = test_cert;
942 verify_details.cert_verify_result.is_issued_by_known_root = true;
943 verify_details.cert_verify_result.public_key_hashes.push_back(
944 test::GetTestHashValue(primary_pin));
945 crypto_client_stream_factory_.set_proof_verify_details(&verify_details);
948 host_resolver_.set_synchronous_mode(true);
949 host_resolver_.rules()->AddIPLiteralRule(server1.host(), "192.168.0.1", "");
950 host_resolver_.rules()->AddIPLiteralRule(server2.host(), "192.168.0.1", "");
952 // Disable connection pooling.
953 QuicStreamFactoryPeer::DisableConnectionPooling(&factory_);
955 QuicStreamRequest request(&factory_);
956 is_https_ = true;
957 EXPECT_EQ(OK,
958 request.Request(server1,
959 is_https_,
960 privacy_mode_,
961 "GET",
962 net_log_,
963 callback_.callback()));
964 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
965 EXPECT_TRUE(stream.get());
967 TestCompletionCallback callback;
968 QuicStreamRequest request2(&factory_);
969 EXPECT_EQ(OK,
970 request2.Request(server2,
971 is_https_,
972 privacy_mode_,
973 "GET",
974 net_log_,
975 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.at_read_eof());
985 EXPECT_TRUE(socket_data1.at_write_eof());
986 EXPECT_TRUE(socket_data2.at_read_eof());
987 EXPECT_TRUE(socket_data2.at_write_eof());
990 TEST_P(QuicStreamFactoryTest, NoHttpsPoolingWithDifferentPins) {
991 MockRead reads[] = {
992 MockRead(ASYNC, OK, 0) // EOF
994 DeterministicSocketData socket_data1(reads, arraysize(reads), nullptr, 0);
995 DeterministicSocketData socket_data2(reads, arraysize(reads), nullptr, 0);
996 socket_factory_.AddSocketDataProvider(&socket_data1);
997 socket_factory_.AddSocketDataProvider(&socket_data2);
998 socket_data1.StopAfter(1);
999 socket_data2.StopAfter(1);
1001 HostPortPair server1("www.example.org", 443);
1002 HostPortPair server2("mail.example.org", 443);
1003 uint8 primary_pin = 1;
1004 uint8 backup_pin = 2;
1005 uint8 bad_pin = 3;
1006 test::AddPin(&transport_security_state_, "mail.example.org", primary_pin,
1007 backup_pin);
1009 // Load a cert that is valid for:
1010 // www.example.org (server1)
1011 // mail.example.org (server2)
1012 base::FilePath certs_dir = GetTestCertsDirectory();
1013 scoped_refptr<X509Certificate> test_cert(
1014 ImportCertFromFile(certs_dir, "spdy_pooling.pem"));
1015 ASSERT_NE(static_cast<X509Certificate*>(nullptr), test_cert.get());
1016 ProofVerifyDetailsChromium verify_details;
1017 verify_details.cert_verify_result.verified_cert = test_cert;
1018 verify_details.cert_verify_result.is_issued_by_known_root = true;
1019 verify_details.cert_verify_result.public_key_hashes.push_back(
1020 test::GetTestHashValue(bad_pin));
1021 crypto_client_stream_factory_.set_proof_verify_details(&verify_details);
1024 host_resolver_.set_synchronous_mode(true);
1025 host_resolver_.rules()->AddIPLiteralRule(server1.host(), "192.168.0.1", "");
1026 host_resolver_.rules()->AddIPLiteralRule(server2.host(), "192.168.0.1", "");
1028 QuicStreamRequest request(&factory_);
1029 is_https_ = true;
1030 EXPECT_EQ(OK,
1031 request.Request(server1,
1032 is_https_,
1033 privacy_mode_,
1034 "GET",
1035 net_log_,
1036 callback_.callback()));
1037 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
1038 EXPECT_TRUE(stream.get());
1040 TestCompletionCallback callback;
1041 QuicStreamRequest request2(&factory_);
1042 EXPECT_EQ(OK,
1043 request2.Request(server2,
1044 is_https_,
1045 privacy_mode_,
1046 "GET",
1047 net_log_,
1048 callback_.callback()));
1049 scoped_ptr<QuicHttpStream> stream2 = request2.ReleaseStream();
1050 EXPECT_TRUE(stream2.get());
1052 EXPECT_NE(QuicStreamFactoryPeer::GetActiveSession(
1053 &factory_, server1, is_https_),
1054 QuicStreamFactoryPeer::GetActiveSession(
1055 &factory_, server2, is_https_));
1057 EXPECT_TRUE(socket_data1.at_read_eof());
1058 EXPECT_TRUE(socket_data1.at_write_eof());
1059 EXPECT_TRUE(socket_data2.at_read_eof());
1060 EXPECT_TRUE(socket_data2.at_write_eof());
1063 TEST_P(QuicStreamFactoryTest, Goaway) {
1064 MockRead reads[] = {
1065 MockRead(ASYNC, OK, 0) // EOF
1067 DeterministicSocketData socket_data(reads, arraysize(reads), nullptr, 0);
1068 socket_data.StopAfter(1);
1069 socket_factory_.AddSocketDataProvider(&socket_data);
1070 DeterministicSocketData socket_data2(reads, arraysize(reads), nullptr, 0);
1071 socket_data2.StopAfter(1);
1072 socket_factory_.AddSocketDataProvider(&socket_data2);
1074 QuicStreamRequest request(&factory_);
1075 EXPECT_EQ(ERR_IO_PENDING,
1076 request.Request(host_port_pair_,
1077 is_https_,
1078 privacy_mode_,
1079 "GET",
1080 net_log_,
1081 callback_.callback()));
1083 EXPECT_EQ(OK, callback_.WaitForResult());
1084 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
1085 EXPECT_TRUE(stream.get());
1087 // Mark the session as going away. Ensure that while it is still alive
1088 // that it is no longer active.
1089 QuicClientSession* session = QuicStreamFactoryPeer::GetActiveSession(
1090 &factory_, host_port_pair_, is_https_);
1091 factory_.OnSessionGoingAway(session);
1092 EXPECT_EQ(true, QuicStreamFactoryPeer::IsLiveSession(&factory_, session));
1093 EXPECT_FALSE(QuicStreamFactoryPeer::HasActiveSession(
1094 &factory_, host_port_pair_, is_https_));
1095 EXPECT_EQ(nullptr, CreateIfSessionExists(host_port_pair_, net_log_).get());
1097 // Create a new request for the same destination and verify that a
1098 // new session is created.
1099 QuicStreamRequest request2(&factory_);
1100 EXPECT_EQ(ERR_IO_PENDING,
1101 request2.Request(host_port_pair_,
1102 is_https_,
1103 privacy_mode_,
1104 "GET",
1105 net_log_,
1106 callback_.callback()));
1107 EXPECT_EQ(OK, callback_.WaitForResult());
1108 scoped_ptr<QuicHttpStream> stream2 = request2.ReleaseStream();
1109 EXPECT_TRUE(stream2.get());
1111 EXPECT_TRUE(QuicStreamFactoryPeer::HasActiveSession(&factory_,
1112 host_port_pair_,
1113 is_https_));
1114 EXPECT_NE(session,
1115 QuicStreamFactoryPeer::GetActiveSession(
1116 &factory_, host_port_pair_, is_https_));
1117 EXPECT_EQ(true, QuicStreamFactoryPeer::IsLiveSession(&factory_, session));
1119 stream2.reset();
1120 stream.reset();
1122 EXPECT_TRUE(socket_data.at_read_eof());
1123 EXPECT_TRUE(socket_data.at_write_eof());
1124 EXPECT_TRUE(socket_data2.at_read_eof());
1125 EXPECT_TRUE(socket_data2.at_write_eof());
1128 TEST_P(QuicStreamFactoryTest, MaxOpenStream) {
1129 MockRead reads[] = {
1130 MockRead(ASYNC, OK, 0) // EOF
1132 QuicStreamId stream_id = kClientDataStreamId1;
1133 scoped_ptr<QuicEncryptedPacket> rst(
1134 maker_.MakeRstPacket(1, true, stream_id, QUIC_STREAM_CANCELLED));
1135 MockWrite writes[] = {
1136 MockWrite(ASYNC, rst->data(), rst->length(), 1),
1138 DeterministicSocketData socket_data(reads, arraysize(reads),
1139 writes, arraysize(writes));
1140 socket_factory_.AddSocketDataProvider(&socket_data);
1141 socket_data.StopAfter(1);
1143 HttpRequestInfo request_info;
1144 std::vector<QuicHttpStream*> streams;
1145 // The MockCryptoClientStream sets max_open_streams to be
1146 // kDefaultMaxStreamsPerConnection / 2.
1147 for (size_t i = 0; i < kDefaultMaxStreamsPerConnection / 2; i++) {
1148 QuicStreamRequest request(&factory_);
1149 int rv = request.Request(host_port_pair_,
1150 is_https_,
1151 privacy_mode_,
1152 "GET",
1153 net_log_,
1154 callback_.callback());
1155 if (i == 0) {
1156 EXPECT_EQ(ERR_IO_PENDING, rv);
1157 EXPECT_EQ(OK, callback_.WaitForResult());
1158 } else {
1159 EXPECT_EQ(OK, rv);
1161 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
1162 EXPECT_TRUE(stream);
1163 EXPECT_EQ(OK, stream->InitializeStream(
1164 &request_info, DEFAULT_PRIORITY, net_log_, CompletionCallback()));
1165 streams.push_back(stream.release());
1168 QuicStreamRequest request(&factory_);
1169 EXPECT_EQ(OK,
1170 request.Request(host_port_pair_,
1171 is_https_,
1172 privacy_mode_,
1173 "GET",
1174 net_log_,
1175 CompletionCallback()));
1176 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
1177 EXPECT_TRUE(stream);
1178 EXPECT_EQ(ERR_IO_PENDING, stream->InitializeStream(
1179 &request_info, DEFAULT_PRIORITY, net_log_, callback_.callback()));
1181 // Close the first stream.
1182 streams.front()->Close(false);
1184 ASSERT_TRUE(callback_.have_result());
1186 EXPECT_EQ(OK, callback_.WaitForResult());
1188 EXPECT_TRUE(socket_data.at_read_eof());
1189 EXPECT_TRUE(socket_data.at_write_eof());
1190 STLDeleteElements(&streams);
1193 TEST_P(QuicStreamFactoryTest, ResolutionErrorInCreate) {
1194 DeterministicSocketData socket_data(nullptr, 0, nullptr, 0);
1195 socket_factory_.AddSocketDataProvider(&socket_data);
1197 host_resolver_.rules()->AddSimulatedFailure(kDefaultServerHostName);
1199 QuicStreamRequest request(&factory_);
1200 EXPECT_EQ(ERR_IO_PENDING,
1201 request.Request(host_port_pair_,
1202 is_https_,
1203 privacy_mode_,
1204 "GET",
1205 net_log_,
1206 callback_.callback()));
1208 EXPECT_EQ(ERR_NAME_NOT_RESOLVED, callback_.WaitForResult());
1210 EXPECT_TRUE(socket_data.at_read_eof());
1211 EXPECT_TRUE(socket_data.at_write_eof());
1214 TEST_P(QuicStreamFactoryTest, ConnectErrorInCreate) {
1215 MockConnect connect(SYNCHRONOUS, ERR_ADDRESS_IN_USE);
1216 DeterministicSocketData socket_data(nullptr, 0, nullptr, 0);
1217 socket_data.set_connect_data(connect);
1218 socket_factory_.AddSocketDataProvider(&socket_data);
1219 socket_data.StopAfter(1);
1221 QuicStreamRequest request(&factory_);
1222 EXPECT_EQ(ERR_IO_PENDING,
1223 request.Request(host_port_pair_,
1224 is_https_,
1225 privacy_mode_,
1226 "GET",
1227 net_log_,
1228 callback_.callback()));
1230 EXPECT_EQ(ERR_ADDRESS_IN_USE, callback_.WaitForResult());
1232 EXPECT_TRUE(socket_data.at_read_eof());
1233 EXPECT_TRUE(socket_data.at_write_eof());
1236 TEST_P(QuicStreamFactoryTest, CancelCreate) {
1237 MockRead reads[] = {
1238 MockRead(ASYNC, OK, 0) // EOF
1240 DeterministicSocketData socket_data(reads, arraysize(reads), nullptr, 0);
1241 socket_factory_.AddSocketDataProvider(&socket_data);
1243 QuicStreamRequest request(&factory_);
1244 EXPECT_EQ(ERR_IO_PENDING,
1245 request.Request(host_port_pair_,
1246 is_https_,
1247 privacy_mode_,
1248 "GET",
1249 net_log_,
1250 callback_.callback()));
1253 socket_data.StopAfter(1);
1254 base::RunLoop run_loop;
1255 run_loop.RunUntilIdle();
1257 scoped_ptr<QuicHttpStream> stream(
1258 CreateIfSessionExists(host_port_pair_, net_log_));
1259 EXPECT_TRUE(stream.get());
1260 stream.reset();
1262 EXPECT_TRUE(socket_data.at_read_eof());
1263 EXPECT_TRUE(socket_data.at_write_eof());
1266 TEST_P(QuicStreamFactoryTest, CreateConsistentEphemeralPort) {
1267 // Sequentially connect to the default host, then another host, and then the
1268 // default host. Verify that the default host gets a consistent ephemeral
1269 // port, that is different from the other host's connection.
1271 std::string other_server_name = "other.google.com";
1272 EXPECT_NE(kDefaultServerHostName, other_server_name);
1273 HostPortPair host_port_pair2(other_server_name, kDefaultServerPort);
1275 int original_port = GetSourcePortForNewSession(host_port_pair_);
1276 EXPECT_NE(original_port, GetSourcePortForNewSession(host_port_pair2));
1277 EXPECT_EQ(original_port, GetSourcePortForNewSession(host_port_pair_));
1280 TEST_P(QuicStreamFactoryTest, GoAwayDisablesConsistentEphemeralPort) {
1281 // Get a session to the host using the port suggester.
1282 int original_port =
1283 GetSourcePortForNewSessionAndGoAway(host_port_pair_);
1284 // Verify that the port is different after the goaway.
1285 EXPECT_NE(original_port, GetSourcePortForNewSession(host_port_pair_));
1286 // Since the previous session did not goaway we should see the original port.
1287 EXPECT_EQ(original_port, GetSourcePortForNewSession(host_port_pair_));
1290 TEST_P(QuicStreamFactoryTest, CloseAllSessions) {
1291 MockRead reads[] = {
1292 MockRead(ASYNC, 0, 0) // EOF
1294 scoped_ptr<QuicEncryptedPacket> rst(ConstructRstPacket());
1295 std::vector<MockWrite> writes;
1296 writes.push_back(MockWrite(ASYNC, rst->data(), rst->length(), 1));
1297 DeterministicSocketData socket_data(reads, arraysize(reads),
1298 writes.empty() ? nullptr : &writes[0],
1299 writes.size());
1300 socket_factory_.AddSocketDataProvider(&socket_data);
1301 socket_data.StopAfter(1);
1303 MockRead reads2[] = {
1304 MockRead(ASYNC, 0, 0) // EOF
1306 DeterministicSocketData socket_data2(reads2, arraysize(reads2), nullptr, 0);
1307 socket_factory_.AddSocketDataProvider(&socket_data2);
1308 socket_data2.StopAfter(1);
1310 QuicStreamRequest request(&factory_);
1311 EXPECT_EQ(ERR_IO_PENDING,
1312 request.Request(host_port_pair_,
1313 is_https_,
1314 privacy_mode_,
1315 "GET",
1316 net_log_,
1317 callback_.callback()));
1319 EXPECT_EQ(OK, callback_.WaitForResult());
1320 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
1321 HttpRequestInfo request_info;
1322 EXPECT_EQ(OK, stream->InitializeStream(&request_info,
1323 DEFAULT_PRIORITY,
1324 net_log_, CompletionCallback()));
1326 // Close the session and verify that stream saw the error.
1327 factory_.CloseAllSessions(ERR_INTERNET_DISCONNECTED);
1328 EXPECT_EQ(ERR_INTERNET_DISCONNECTED,
1329 stream->ReadResponseHeaders(callback_.callback()));
1331 // Now attempting to request a stream to the same origin should create
1332 // a new session.
1334 QuicStreamRequest request2(&factory_);
1335 EXPECT_EQ(ERR_IO_PENDING,
1336 request2.Request(host_port_pair_,
1337 is_https_,
1338 privacy_mode_,
1339 "GET",
1340 net_log_,
1341 callback_.callback()));
1343 EXPECT_EQ(OK, callback_.WaitForResult());
1344 stream = request2.ReleaseStream();
1345 stream.reset(); // Will reset stream 3.
1347 EXPECT_TRUE(socket_data.at_read_eof());
1348 EXPECT_TRUE(socket_data.at_write_eof());
1349 EXPECT_TRUE(socket_data2.at_read_eof());
1350 EXPECT_TRUE(socket_data2.at_write_eof());
1353 TEST_P(QuicStreamFactoryTest, OnIPAddressChanged) {
1354 MockRead reads[] = {
1355 MockRead(ASYNC, 0, 0) // EOF
1357 scoped_ptr<QuicEncryptedPacket> rst(ConstructRstPacket());
1358 std::vector<MockWrite> writes;
1359 writes.push_back(MockWrite(ASYNC, rst->data(), rst->length(), 1));
1360 DeterministicSocketData socket_data(reads, arraysize(reads),
1361 writes.empty() ? nullptr : &writes[0],
1362 writes.size());
1363 socket_factory_.AddSocketDataProvider(&socket_data);
1364 socket_data.StopAfter(1);
1366 MockRead reads2[] = {
1367 MockRead(ASYNC, 0, 0) // EOF
1369 DeterministicSocketData socket_data2(reads2, arraysize(reads2), nullptr, 0);
1370 socket_factory_.AddSocketDataProvider(&socket_data2);
1371 socket_data2.StopAfter(1);
1373 QuicStreamRequest request(&factory_);
1374 EXPECT_EQ(ERR_IO_PENDING,
1375 request.Request(host_port_pair_,
1376 is_https_,
1377 privacy_mode_,
1378 "GET",
1379 net_log_,
1380 callback_.callback()));
1382 EXPECT_EQ(OK, callback_.WaitForResult());
1383 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
1384 HttpRequestInfo request_info;
1385 EXPECT_EQ(OK, stream->InitializeStream(&request_info,
1386 DEFAULT_PRIORITY,
1387 net_log_, CompletionCallback()));
1389 // Change the IP address and verify that stream saw the error.
1390 factory_.OnIPAddressChanged();
1391 EXPECT_EQ(ERR_NETWORK_CHANGED,
1392 stream->ReadResponseHeaders(callback_.callback()));
1393 EXPECT_TRUE(factory_.require_confirmation());
1395 // Now attempting to request a stream to the same origin should create
1396 // a new session.
1398 QuicStreamRequest request2(&factory_);
1399 EXPECT_EQ(ERR_IO_PENDING,
1400 request2.Request(host_port_pair_,
1401 is_https_,
1402 privacy_mode_,
1403 "GET",
1404 net_log_,
1405 callback_.callback()));
1407 EXPECT_EQ(OK, callback_.WaitForResult());
1408 stream = request2.ReleaseStream();
1409 stream.reset(); // Will reset stream 3.
1411 EXPECT_TRUE(socket_data.at_read_eof());
1412 EXPECT_TRUE(socket_data.at_write_eof());
1413 EXPECT_TRUE(socket_data2.at_read_eof());
1414 EXPECT_TRUE(socket_data2.at_write_eof());
1417 TEST_P(QuicStreamFactoryTest, OnCertAdded) {
1418 MockRead reads[] = {
1419 MockRead(ASYNC, 0, 0) // EOF
1421 scoped_ptr<QuicEncryptedPacket> rst(ConstructRstPacket());
1422 std::vector<MockWrite> writes;
1423 writes.push_back(MockWrite(ASYNC, rst->data(), rst->length(), 1));
1424 DeterministicSocketData socket_data(reads, arraysize(reads),
1425 writes.empty() ? nullptr : &writes[0],
1426 writes.size());
1427 socket_factory_.AddSocketDataProvider(&socket_data);
1428 socket_data.StopAfter(1);
1430 MockRead reads2[] = {
1431 MockRead(ASYNC, 0, 0) // EOF
1433 DeterministicSocketData socket_data2(reads2, arraysize(reads2), nullptr, 0);
1434 socket_factory_.AddSocketDataProvider(&socket_data2);
1435 socket_data2.StopAfter(1);
1437 QuicStreamRequest request(&factory_);
1438 EXPECT_EQ(ERR_IO_PENDING,
1439 request.Request(host_port_pair_,
1440 is_https_,
1441 privacy_mode_,
1442 "GET",
1443 net_log_,
1444 callback_.callback()));
1446 EXPECT_EQ(OK, callback_.WaitForResult());
1447 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
1448 HttpRequestInfo request_info;
1449 EXPECT_EQ(OK, stream->InitializeStream(&request_info,
1450 DEFAULT_PRIORITY,
1451 net_log_, CompletionCallback()));
1453 // Add a cert and verify that stream saw the event.
1454 factory_.OnCertAdded(nullptr);
1455 EXPECT_EQ(ERR_CERT_DATABASE_CHANGED,
1456 stream->ReadResponseHeaders(callback_.callback()));
1457 EXPECT_FALSE(factory_.require_confirmation());
1459 // Now attempting to request a stream to the same origin should create
1460 // a new session.
1462 QuicStreamRequest request2(&factory_);
1463 EXPECT_EQ(ERR_IO_PENDING,
1464 request2.Request(host_port_pair_,
1465 is_https_,
1466 privacy_mode_,
1467 "GET",
1468 net_log_,
1469 callback_.callback()));
1471 EXPECT_EQ(OK, callback_.WaitForResult());
1472 stream = request2.ReleaseStream();
1473 stream.reset(); // Will reset stream 3.
1475 EXPECT_TRUE(socket_data.at_read_eof());
1476 EXPECT_TRUE(socket_data.at_write_eof());
1477 EXPECT_TRUE(socket_data2.at_read_eof());
1478 EXPECT_TRUE(socket_data2.at_write_eof());
1481 TEST_P(QuicStreamFactoryTest, OnCACertChanged) {
1482 MockRead reads[] = {
1483 MockRead(ASYNC, 0, 0) // EOF
1485 scoped_ptr<QuicEncryptedPacket> rst(ConstructRstPacket());
1486 std::vector<MockWrite> writes;
1487 writes.push_back(MockWrite(ASYNC, rst->data(), rst->length(), 1));
1488 DeterministicSocketData socket_data(reads, arraysize(reads),
1489 writes.empty() ? nullptr : &writes[0],
1490 writes.size());
1491 socket_factory_.AddSocketDataProvider(&socket_data);
1492 socket_data.StopAfter(1);
1494 MockRead reads2[] = {
1495 MockRead(ASYNC, 0, 0) // EOF
1497 DeterministicSocketData socket_data2(reads2, arraysize(reads2), nullptr, 0);
1498 socket_factory_.AddSocketDataProvider(&socket_data2);
1499 socket_data2.StopAfter(1);
1501 QuicStreamRequest request(&factory_);
1502 EXPECT_EQ(ERR_IO_PENDING,
1503 request.Request(host_port_pair_,
1504 is_https_,
1505 privacy_mode_,
1506 "GET",
1507 net_log_,
1508 callback_.callback()));
1510 EXPECT_EQ(OK, callback_.WaitForResult());
1511 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
1512 HttpRequestInfo request_info;
1513 EXPECT_EQ(OK, stream->InitializeStream(&request_info,
1514 DEFAULT_PRIORITY,
1515 net_log_, CompletionCallback()));
1517 // Change the CA cert and verify that stream saw the event.
1518 factory_.OnCACertChanged(nullptr);
1519 EXPECT_EQ(ERR_CERT_DATABASE_CHANGED,
1520 stream->ReadResponseHeaders(callback_.callback()));
1521 EXPECT_FALSE(factory_.require_confirmation());
1523 // Now attempting to request a stream to the same origin should create
1524 // a new session.
1526 QuicStreamRequest request2(&factory_);
1527 EXPECT_EQ(ERR_IO_PENDING,
1528 request2.Request(host_port_pair_,
1529 is_https_,
1530 privacy_mode_,
1531 "GET",
1532 net_log_,
1533 callback_.callback()));
1535 EXPECT_EQ(OK, callback_.WaitForResult());
1536 stream = request2.ReleaseStream();
1537 stream.reset(); // Will reset stream 3.
1539 EXPECT_TRUE(socket_data.at_read_eof());
1540 EXPECT_TRUE(socket_data.at_write_eof());
1541 EXPECT_TRUE(socket_data2.at_read_eof());
1542 EXPECT_TRUE(socket_data2.at_write_eof());
1545 TEST_P(QuicStreamFactoryTest, SharedCryptoConfig) {
1546 vector<string> cannoncial_suffixes;
1547 cannoncial_suffixes.push_back(string(".c.youtube.com"));
1548 cannoncial_suffixes.push_back(string(".googlevideo.com"));
1550 for (unsigned i = 0; i < cannoncial_suffixes.size(); ++i) {
1551 string r1_host_name("r1");
1552 string r2_host_name("r2");
1553 r1_host_name.append(cannoncial_suffixes[i]);
1554 r2_host_name.append(cannoncial_suffixes[i]);
1556 HostPortPair host_port_pair1(r1_host_name, 80);
1557 QuicCryptoClientConfig* crypto_config =
1558 QuicStreamFactoryPeer::GetCryptoConfig(&factory_);
1559 QuicServerId server_id1(host_port_pair1, is_https_, privacy_mode_);
1560 QuicCryptoClientConfig::CachedState* cached1 =
1561 crypto_config->LookupOrCreate(server_id1);
1562 EXPECT_FALSE(cached1->proof_valid());
1563 EXPECT_TRUE(cached1->source_address_token().empty());
1565 // Mutate the cached1 to have different data.
1566 // TODO(rtenneti): mutate other members of CachedState.
1567 cached1->set_source_address_token(r1_host_name);
1568 cached1->SetProofValid();
1570 HostPortPair host_port_pair2(r2_host_name, 80);
1571 QuicServerId server_id2(host_port_pair2, is_https_, privacy_mode_);
1572 QuicCryptoClientConfig::CachedState* cached2 =
1573 crypto_config->LookupOrCreate(server_id2);
1574 EXPECT_EQ(cached1->source_address_token(), cached2->source_address_token());
1575 EXPECT_TRUE(cached2->proof_valid());
1579 TEST_P(QuicStreamFactoryTest, CryptoConfigWhenProofIsInvalid) {
1580 vector<string> cannoncial_suffixes;
1581 cannoncial_suffixes.push_back(string(".c.youtube.com"));
1582 cannoncial_suffixes.push_back(string(".googlevideo.com"));
1584 for (unsigned i = 0; i < cannoncial_suffixes.size(); ++i) {
1585 string r3_host_name("r3");
1586 string r4_host_name("r4");
1587 r3_host_name.append(cannoncial_suffixes[i]);
1588 r4_host_name.append(cannoncial_suffixes[i]);
1590 HostPortPair host_port_pair1(r3_host_name, 80);
1591 QuicCryptoClientConfig* crypto_config =
1592 QuicStreamFactoryPeer::GetCryptoConfig(&factory_);
1593 QuicServerId server_id1(host_port_pair1, is_https_, privacy_mode_);
1594 QuicCryptoClientConfig::CachedState* cached1 =
1595 crypto_config->LookupOrCreate(server_id1);
1596 EXPECT_FALSE(cached1->proof_valid());
1597 EXPECT_TRUE(cached1->source_address_token().empty());
1599 // Mutate the cached1 to have different data.
1600 // TODO(rtenneti): mutate other members of CachedState.
1601 cached1->set_source_address_token(r3_host_name);
1602 cached1->SetProofInvalid();
1604 HostPortPair host_port_pair2(r4_host_name, 80);
1605 QuicServerId server_id2(host_port_pair2, is_https_, privacy_mode_);
1606 QuicCryptoClientConfig::CachedState* cached2 =
1607 crypto_config->LookupOrCreate(server_id2);
1608 EXPECT_NE(cached1->source_address_token(), cached2->source_address_token());
1609 EXPECT_TRUE(cached2->source_address_token().empty());
1610 EXPECT_FALSE(cached2->proof_valid());
1614 TEST_P(QuicStreamFactoryTest, CancelWaitForDataReady) {
1615 // Don't race quic connections when testing cancel reading of server config
1616 // from disk cache.
1617 if (GetParam().enable_connection_racing)
1618 return;
1619 factory_.set_quic_server_info_factory(&quic_server_info_factory_);
1620 QuicStreamFactoryPeer::SetTaskRunner(&factory_, runner_.get());
1621 const size_t kLoadServerInfoTimeoutMs = 50;
1622 QuicStreamFactoryPeer::SetLoadServerInfoTimeout(
1623 &factory_, kLoadServerInfoTimeoutMs);
1625 MockRead reads[] = {
1626 MockRead(ASYNC, OK, 0) // EOF
1628 DeterministicSocketData socket_data(reads, arraysize(reads), nullptr, 0);
1629 socket_factory_.AddSocketDataProvider(&socket_data);
1630 socket_data.StopAfter(1);
1632 crypto_client_stream_factory_.set_handshake_mode(
1633 MockCryptoClientStream::ZERO_RTT);
1634 host_resolver_.set_synchronous_mode(true);
1635 host_resolver_.rules()->AddIPLiteralRule(host_port_pair_.host(),
1636 "192.168.0.1", "");
1638 QuicStreamRequest request(&factory_);
1639 EXPECT_EQ(ERR_IO_PENDING,
1640 request.Request(host_port_pair_,
1641 is_https_,
1642 privacy_mode_,
1643 "GET",
1644 net_log_,
1645 callback_.callback()));
1647 // Verify that the CancelWaitForDataReady task has been posted.
1648 ASSERT_EQ(1u, runner_->GetPostedTasks().size());
1649 EXPECT_EQ(base::TimeDelta::FromMilliseconds(kLoadServerInfoTimeoutMs),
1650 runner_->GetPostedTasks()[0].delay);
1652 runner_->RunNextTask();
1653 ASSERT_EQ(0u, runner_->GetPostedTasks().size());
1655 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
1656 EXPECT_TRUE(stream.get());
1657 EXPECT_TRUE(socket_data.at_read_eof());
1658 EXPECT_TRUE(socket_data.at_write_eof());
1661 TEST_P(QuicStreamFactoryTest, RacingConnections) {
1662 if (!GetParam().enable_connection_racing)
1663 return;
1664 factory_.set_quic_server_info_factory(&quic_server_info_factory_);
1665 QuicStreamFactoryPeer::SetTaskRunner(&factory_, runner_.get());
1666 const size_t kLoadServerInfoTimeoutMs = 50;
1667 QuicStreamFactoryPeer::SetLoadServerInfoTimeout(&factory_,
1668 kLoadServerInfoTimeoutMs);
1670 MockRead reads[] = {
1671 MockRead(ASYNC, OK, 0) // EOF
1673 DeterministicSocketData socket_data(reads, arraysize(reads), nullptr, 0);
1674 socket_factory_.AddSocketDataProvider(&socket_data);
1675 socket_data.StopAfter(1);
1677 MockRead reads2[] = {
1678 MockRead(ASYNC, 0, 0) // EOF
1680 DeterministicSocketData socket_data2(reads2, arraysize(reads2), nullptr, 0);
1681 socket_factory_.AddSocketDataProvider(&socket_data2);
1682 socket_data2.StopAfter(1);
1684 crypto_client_stream_factory_.set_handshake_mode(
1685 MockCryptoClientStream::ZERO_RTT);
1686 host_resolver_.set_synchronous_mode(true);
1687 host_resolver_.rules()->AddIPLiteralRule(host_port_pair_.host(),
1688 "192.168.0.1", "");
1690 QuicStreamRequest request(&factory_);
1691 QuicServerId server_id(host_port_pair_, is_https_, privacy_mode_);
1692 EXPECT_EQ(ERR_IO_PENDING,
1693 request.Request(host_port_pair_, is_https_, privacy_mode_, "GET",
1694 net_log_, callback_.callback()));
1695 EXPECT_EQ(2u,
1696 QuicStreamFactoryPeer::GetNumberOfActiveJobs(&factory_, server_id));
1698 runner_->RunNextTask();
1700 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
1701 EXPECT_TRUE(stream.get());
1702 EXPECT_TRUE(socket_data.at_read_eof());
1703 EXPECT_TRUE(socket_data.at_write_eof());
1704 EXPECT_EQ(0u,
1705 QuicStreamFactoryPeer::GetNumberOfActiveJobs(&factory_, server_id));
1708 TEST_P(QuicStreamFactoryTest, EnableNotLoadFromDiskCache) {
1709 factory_.set_quic_server_info_factory(&quic_server_info_factory_);
1710 QuicStreamFactoryPeer::SetTaskRunner(&factory_, runner_.get());
1711 const size_t kLoadServerInfoTimeoutMs = 50;
1712 QuicStreamFactoryPeer::SetLoadServerInfoTimeout(&factory_,
1713 kLoadServerInfoTimeoutMs);
1714 QuicStreamFactoryPeer::SetDisableDiskCache(&factory_, true);
1716 MockRead reads[] = {
1717 MockRead(ASYNC, OK, 0) // EOF
1719 DeterministicSocketData socket_data(reads, arraysize(reads), nullptr, 0);
1720 socket_factory_.AddSocketDataProvider(&socket_data);
1721 socket_data.StopAfter(1);
1723 crypto_client_stream_factory_.set_handshake_mode(
1724 MockCryptoClientStream::ZERO_RTT);
1725 host_resolver_.set_synchronous_mode(true);
1726 host_resolver_.rules()->AddIPLiteralRule(host_port_pair_.host(),
1727 "192.168.0.1", "");
1729 QuicStreamRequest request(&factory_);
1730 EXPECT_EQ(OK, request.Request(host_port_pair_, is_https_, privacy_mode_,
1731 "GET", net_log_, callback_.callback()));
1733 // If we are waiting for disk cache, we would have posted a task. Verify that
1734 // the CancelWaitForDataReady task hasn't been posted.
1735 ASSERT_EQ(0u, runner_->GetPostedTasks().size());
1737 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
1738 EXPECT_TRUE(stream.get());
1739 EXPECT_TRUE(socket_data.at_read_eof());
1740 EXPECT_TRUE(socket_data.at_write_eof());
1743 } // namespace test
1744 } // namespace net