Sort unlaunched apps on app list start page by apps grid order.
[chromium-blink-merge.git] / net / quic / quic_stream_factory_test.cc
blob390508436a288f54bc79828f56c595f34a3b9d4f
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 size_t GetNumberOfActiveJobs(QuicStreamFactory* factory,
139 const QuicServerId& server_id) {
140 return (factory->active_jobs_[server_id]).size();
144 class MockQuicServerInfo : public QuicServerInfo {
145 public:
146 MockQuicServerInfo(const QuicServerId& server_id)
147 : QuicServerInfo(server_id) {}
148 ~MockQuicServerInfo() override {}
150 void Start() override {}
152 int WaitForDataReady(const CompletionCallback& callback) override {
153 return ERR_IO_PENDING;
156 void ResetWaitForDataReadyCallback() override {}
158 void CancelWaitForDataReadyCallback() override {}
160 bool IsDataReady() override { return false; }
162 bool IsReadyToPersist() override { return false; }
164 void Persist() override {}
166 void OnExternalCacheHit() override {}
169 class MockQuicServerInfoFactory : public QuicServerInfoFactory {
170 public:
171 MockQuicServerInfoFactory() {}
172 ~MockQuicServerInfoFactory() override {}
174 QuicServerInfo* GetForServer(const QuicServerId& server_id) override {
175 return new MockQuicServerInfo(server_id);
179 class QuicStreamFactoryTest : public ::testing::TestWithParam<TestParams> {
180 protected:
181 QuicStreamFactoryTest()
182 : random_generator_(0),
183 clock_(new MockClock()),
184 runner_(new TestTaskRunner(clock_)),
185 maker_(GetParam().version, 0, clock_),
186 cert_verifier_(CertVerifier::CreateDefault()),
187 channel_id_service_(
188 new ChannelIDService(new DefaultChannelIDStore(nullptr),
189 base::MessageLoopProxy::current())),
190 factory_(&host_resolver_,
191 &socket_factory_,
192 base::WeakPtr<HttpServerProperties>(),
193 cert_verifier_.get(),
194 channel_id_service_.get(),
195 &transport_security_state_,
196 &crypto_client_stream_factory_,
197 &random_generator_,
198 clock_,
199 kDefaultMaxPacketSize,
200 std::string(),
201 SupportedVersions(GetParam().version),
202 /*enable_port_selection=*/true,
203 /*always_require_handshake_confirmation=*/false,
204 /*disable_connection_pooling=*/false,
205 /*load_server_info_timeout=*/0u,
206 /*load_server_info_timeout_srtt_multiplier=*/0.0f,
207 /*enable_truncated_connection_ids=*/true,
208 /*enable_connection_racing=*/false,
209 QuicTagVector()),
210 host_port_pair_(kDefaultServerHostName, kDefaultServerPort),
211 is_https_(false),
212 privacy_mode_(PRIVACY_MODE_DISABLED) {
213 factory_.set_require_confirmation(false);
214 clock_->AdvanceTime(QuicTime::Delta::FromSeconds(1));
215 QuicStreamFactoryPeer::SetEnableConnectionRacing(
216 &factory_, GetParam().enable_connection_racing);
219 scoped_ptr<QuicHttpStream> CreateIfSessionExists(
220 const HostPortPair& host_port_pair,
221 const BoundNetLog& net_log) {
222 return QuicStreamFactoryPeer::CreateIfSessionExists(
223 &factory_, host_port_pair, false, net_log_);
226 int GetSourcePortForNewSession(const HostPortPair& destination) {
227 return GetSourcePortForNewSessionInner(destination, false);
230 int GetSourcePortForNewSessionAndGoAway(
231 const HostPortPair& destination) {
232 return GetSourcePortForNewSessionInner(destination, true);
235 int GetSourcePortForNewSessionInner(const HostPortPair& destination,
236 bool goaway_received) {
237 // Should only be called if there is no active session for this destination.
238 EXPECT_EQ(nullptr, CreateIfSessionExists(destination, net_log_).get());
239 size_t socket_count = socket_factory_.udp_client_sockets().size();
241 MockRead reads[] = {
242 MockRead(ASYNC, OK, 0) // EOF
244 DeterministicSocketData socket_data(reads, arraysize(reads), nullptr, 0);
245 socket_data.StopAfter(1);
246 socket_factory_.AddSocketDataProvider(&socket_data);
248 QuicStreamRequest request(&factory_);
249 EXPECT_EQ(ERR_IO_PENDING,
250 request.Request(destination,
251 is_https_,
252 privacy_mode_,
253 "GET",
254 net_log_,
255 callback_.callback()));
257 EXPECT_EQ(OK, callback_.WaitForResult());
258 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
259 EXPECT_TRUE(stream.get());
260 stream.reset();
262 QuicClientSession* session = QuicStreamFactoryPeer::GetActiveSession(
263 &factory_, destination, is_https_);
265 if (socket_count + 1 != socket_factory_.udp_client_sockets().size()) {
266 EXPECT_TRUE(false);
267 return 0;
270 IPEndPoint endpoint;
271 socket_factory_.
272 udp_client_sockets()[socket_count]->GetLocalAddress(&endpoint);
273 int port = endpoint.port();
274 if (goaway_received) {
275 QuicGoAwayFrame goaway(QUIC_NO_ERROR, 1, "");
276 session->OnGoAway(goaway);
279 factory_.OnSessionClosed(session);
280 EXPECT_EQ(nullptr, CreateIfSessionExists(destination, net_log_).get());
281 EXPECT_TRUE(socket_data.at_read_eof());
282 EXPECT_TRUE(socket_data.at_write_eof());
283 return port;
286 scoped_ptr<QuicEncryptedPacket> ConstructRstPacket() {
287 QuicStreamId stream_id = kClientDataStreamId1;
288 return maker_.MakeRstPacket(
289 1, true, stream_id,
290 AdjustErrorForVersion(QUIC_RST_ACKNOWLEDGEMENT, GetParam().version));
293 MockQuicServerInfoFactory quic_server_info_factory_;
294 MockHostResolver host_resolver_;
295 DeterministicMockClientSocketFactory socket_factory_;
296 MockCryptoClientStreamFactory crypto_client_stream_factory_;
297 MockRandom random_generator_;
298 MockClock* clock_; // Owned by factory_.
299 scoped_refptr<TestTaskRunner> runner_;
300 QuicTestPacketMaker maker_;
301 scoped_ptr<CertVerifier> cert_verifier_;
302 scoped_ptr<ChannelIDService> channel_id_service_;
303 TransportSecurityState transport_security_state_;
304 QuicStreamFactory factory_;
305 HostPortPair host_port_pair_;
306 bool is_https_;
307 PrivacyMode privacy_mode_;
308 BoundNetLog net_log_;
309 TestCompletionCallback callback_;
312 INSTANTIATE_TEST_CASE_P(Version,
313 QuicStreamFactoryTest,
314 ::testing::ValuesIn(GetTestParams()));
316 TEST_P(QuicStreamFactoryTest, CreateIfSessionExists) {
317 EXPECT_EQ(nullptr, CreateIfSessionExists(host_port_pair_, net_log_).get());
320 TEST_P(QuicStreamFactoryTest, Create) {
321 MockRead reads[] = {
322 MockRead(ASYNC, OK, 0) // EOF
324 DeterministicSocketData socket_data(reads, arraysize(reads), nullptr, 0);
325 socket_factory_.AddSocketDataProvider(&socket_data);
326 socket_data.StopAfter(1);
328 QuicStreamRequest request(&factory_);
329 EXPECT_EQ(ERR_IO_PENDING,
330 request.Request(host_port_pair_,
331 is_https_,
332 privacy_mode_,
333 "GET",
334 net_log_,
335 callback_.callback()));
337 EXPECT_EQ(OK, callback_.WaitForResult());
338 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
339 EXPECT_TRUE(stream.get());
341 // Will reset stream 3.
342 stream = CreateIfSessionExists(host_port_pair_, net_log_);
343 EXPECT_TRUE(stream.get());
345 // TODO(rtenneti): We should probably have a tests that HTTP and HTTPS result
346 // in streams on different sessions.
347 QuicStreamRequest request2(&factory_);
348 EXPECT_EQ(OK,
349 request2.Request(host_port_pair_,
350 is_https_,
351 privacy_mode_,
352 "GET",
353 net_log_,
354 callback_.callback()));
355 stream = request2.ReleaseStream(); // Will reset stream 5.
356 stream.reset(); // Will reset stream 7.
358 EXPECT_TRUE(socket_data.at_read_eof());
359 EXPECT_TRUE(socket_data.at_write_eof());
362 TEST_P(QuicStreamFactoryTest, CreateZeroRtt) {
363 MockRead reads[] = {
364 MockRead(ASYNC, OK, 0) // EOF
366 DeterministicSocketData socket_data(reads, arraysize(reads), nullptr, 0);
367 socket_factory_.AddSocketDataProvider(&socket_data);
368 socket_data.StopAfter(1);
370 crypto_client_stream_factory_.set_handshake_mode(
371 MockCryptoClientStream::ZERO_RTT);
372 host_resolver_.set_synchronous_mode(true);
373 host_resolver_.rules()->AddIPLiteralRule(host_port_pair_.host(),
374 "192.168.0.1", "");
376 QuicStreamRequest request(&factory_);
377 EXPECT_EQ(OK,
378 request.Request(host_port_pair_,
379 is_https_,
380 privacy_mode_,
381 "GET",
382 net_log_,
383 callback_.callback()));
385 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
386 EXPECT_TRUE(stream.get());
387 EXPECT_TRUE(socket_data.at_read_eof());
388 EXPECT_TRUE(socket_data.at_write_eof());
391 TEST_P(QuicStreamFactoryTest, CreateZeroRttPost) {
392 MockRead reads[] = {
393 MockRead(ASYNC, OK, 0) // EOF
395 DeterministicSocketData socket_data(reads, arraysize(reads), nullptr, 0);
396 socket_factory_.AddSocketDataProvider(&socket_data);
397 socket_data.StopAfter(1);
399 crypto_client_stream_factory_.set_handshake_mode(
400 MockCryptoClientStream::ZERO_RTT);
401 host_resolver_.set_synchronous_mode(true);
402 host_resolver_.rules()->AddIPLiteralRule(host_port_pair_.host(),
403 "192.168.0.1", "");
405 QuicStreamRequest request(&factory_);
406 // Posts require handshake confirmation, so this will return asynchronously.
407 EXPECT_EQ(ERR_IO_PENDING,
408 request.Request(host_port_pair_,
409 is_https_,
410 privacy_mode_,
411 "POST",
412 net_log_,
413 callback_.callback()));
415 // Confirm the handshake and verify that the stream is created.
416 crypto_client_stream_factory_.last_stream()->SendOnCryptoHandshakeEvent(
417 QuicSession::HANDSHAKE_CONFIRMED);
419 EXPECT_EQ(OK, callback_.WaitForResult());
420 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
421 EXPECT_TRUE(stream.get());
422 EXPECT_TRUE(socket_data.at_read_eof());
423 EXPECT_TRUE(socket_data.at_write_eof());
426 TEST_P(QuicStreamFactoryTest, CreateHttpVsHttps) {
427 MockRead reads[] = {
428 MockRead(ASYNC, OK, 0) // EOF
430 DeterministicSocketData socket_data1(reads, arraysize(reads), nullptr, 0);
431 DeterministicSocketData socket_data2(reads, arraysize(reads), nullptr, 0);
432 socket_factory_.AddSocketDataProvider(&socket_data1);
433 socket_factory_.AddSocketDataProvider(&socket_data2);
434 socket_data1.StopAfter(1);
435 socket_data2.StopAfter(1);
437 QuicStreamRequest request(&factory_);
438 EXPECT_EQ(ERR_IO_PENDING,
439 request.Request(host_port_pair_,
440 is_https_,
441 privacy_mode_,
442 "GET",
443 net_log_,
444 callback_.callback()));
446 EXPECT_EQ(OK, callback_.WaitForResult());
447 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
448 EXPECT_TRUE(stream.get());
450 QuicStreamRequest request2(&factory_);
451 EXPECT_EQ(ERR_IO_PENDING,
452 request2.Request(host_port_pair_,
453 !is_https_,
454 privacy_mode_,
455 "GET",
456 net_log_,
457 callback_.callback()));
458 EXPECT_EQ(OK, callback_.WaitForResult());
459 stream = request2.ReleaseStream();
460 EXPECT_TRUE(stream.get());
461 stream.reset();
463 EXPECT_NE(QuicStreamFactoryPeer::GetActiveSession(
464 &factory_, host_port_pair_, is_https_),
465 QuicStreamFactoryPeer::GetActiveSession(
466 &factory_, host_port_pair_, !is_https_));
468 EXPECT_TRUE(socket_data1.at_read_eof());
469 EXPECT_TRUE(socket_data1.at_write_eof());
470 EXPECT_TRUE(socket_data2.at_read_eof());
471 EXPECT_TRUE(socket_data2.at_write_eof());
474 TEST_P(QuicStreamFactoryTest, Pooling) {
475 MockRead reads[] = {
476 MockRead(ASYNC, OK, 0) // EOF
478 DeterministicSocketData socket_data(reads, arraysize(reads), nullptr, 0);
479 socket_factory_.AddSocketDataProvider(&socket_data);
480 socket_data.StopAfter(1);
482 HostPortPair server2("mail.google.com", kDefaultServerPort);
483 host_resolver_.set_synchronous_mode(true);
484 host_resolver_.rules()->AddIPLiteralRule(
485 kDefaultServerHostName, "192.168.0.1", "");
486 host_resolver_.rules()->AddIPLiteralRule(
487 "mail.google.com", "192.168.0.1", "");
489 QuicStreamRequest request(&factory_);
490 EXPECT_EQ(OK,
491 request.Request(host_port_pair_,
492 is_https_,
493 privacy_mode_,
494 "GET",
495 net_log_,
496 callback_.callback()));
497 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
498 EXPECT_TRUE(stream.get());
500 TestCompletionCallback callback;
501 QuicStreamRequest request2(&factory_);
502 EXPECT_EQ(OK,
503 request2.Request(server2,
504 is_https_,
505 privacy_mode_,
506 "GET",
507 net_log_,
508 callback.callback()));
509 scoped_ptr<QuicHttpStream> stream2 = request2.ReleaseStream();
510 EXPECT_TRUE(stream2.get());
512 EXPECT_EQ(
513 QuicStreamFactoryPeer::GetActiveSession(
514 &factory_, host_port_pair_, is_https_),
515 QuicStreamFactoryPeer::GetActiveSession(&factory_, server2, is_https_));
517 EXPECT_TRUE(socket_data.at_read_eof());
518 EXPECT_TRUE(socket_data.at_write_eof());
521 TEST_P(QuicStreamFactoryTest, NoPoolingIfDisabled) {
522 MockRead reads[] = {
523 MockRead(ASYNC, OK, 0) // EOF
525 DeterministicSocketData socket_data1(reads, arraysize(reads), nullptr, 0);
526 DeterministicSocketData socket_data2(reads, arraysize(reads), nullptr, 0);
527 socket_factory_.AddSocketDataProvider(&socket_data1);
528 socket_factory_.AddSocketDataProvider(&socket_data2);
529 socket_data1.StopAfter(1);
530 socket_data2.StopAfter(1);
532 HostPortPair server2("mail.google.com", kDefaultServerPort);
533 host_resolver_.set_synchronous_mode(true);
534 host_resolver_.rules()->AddIPLiteralRule(
535 kDefaultServerHostName, "192.168.0.1", "");
536 host_resolver_.rules()->AddIPLiteralRule(
537 "mail.google.com", "192.168.0.1", "");
539 // Disable connection pooling.
540 QuicStreamFactoryPeer::DisableConnectionPooling(&factory_);
542 QuicStreamRequest request(&factory_);
543 EXPECT_EQ(OK,
544 request.Request(host_port_pair_,
545 is_https_,
546 privacy_mode_,
547 "GET",
548 net_log_,
549 callback_.callback()));
550 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
551 EXPECT_TRUE(stream.get());
553 TestCompletionCallback callback;
554 QuicStreamRequest request2(&factory_);
555 EXPECT_EQ(OK,
556 request2.Request(server2,
557 is_https_,
558 privacy_mode_,
559 "GET",
560 net_log_,
561 callback.callback()));
562 scoped_ptr<QuicHttpStream> stream2 = request2.ReleaseStream();
563 EXPECT_TRUE(stream2.get());
565 EXPECT_NE(
566 QuicStreamFactoryPeer::GetActiveSession(
567 &factory_, host_port_pair_, is_https_),
568 QuicStreamFactoryPeer::GetActiveSession(&factory_, server2, is_https_));
570 EXPECT_TRUE(socket_data1.at_read_eof());
571 EXPECT_TRUE(socket_data1.at_write_eof());
572 EXPECT_TRUE(socket_data2.at_read_eof());
573 EXPECT_TRUE(socket_data2.at_write_eof());
576 TEST_P(QuicStreamFactoryTest, NoPoolingAfterGoAway) {
577 MockRead reads[] = {
578 MockRead(ASYNC, OK, 0) // EOF
580 DeterministicSocketData socket_data1(reads, arraysize(reads), nullptr, 0);
581 DeterministicSocketData socket_data2(reads, arraysize(reads), nullptr, 0);
582 socket_factory_.AddSocketDataProvider(&socket_data1);
583 socket_factory_.AddSocketDataProvider(&socket_data2);
584 socket_data1.StopAfter(1);
585 socket_data2.StopAfter(1);
587 HostPortPair server2("mail.google.com", kDefaultServerPort);
588 host_resolver_.set_synchronous_mode(true);
589 host_resolver_.rules()->AddIPLiteralRule(
590 kDefaultServerHostName, "192.168.0.1", "");
591 host_resolver_.rules()->AddIPLiteralRule(
592 "mail.google.com", "192.168.0.1", "");
594 QuicStreamRequest request(&factory_);
595 EXPECT_EQ(OK,
596 request.Request(host_port_pair_,
597 is_https_,
598 privacy_mode_,
599 "GET",
600 net_log_,
601 callback_.callback()));
602 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
603 EXPECT_TRUE(stream.get());
605 TestCompletionCallback callback;
606 QuicStreamRequest request2(&factory_);
607 EXPECT_EQ(OK,
608 request2.Request(server2,
609 is_https_,
610 privacy_mode_,
611 "GET",
612 net_log_,
613 callback.callback()));
614 scoped_ptr<QuicHttpStream> stream2 = request2.ReleaseStream();
615 EXPECT_TRUE(stream2.get());
617 factory_.OnSessionGoingAway(QuicStreamFactoryPeer::GetActiveSession(
618 &factory_, host_port_pair_, is_https_));
619 EXPECT_FALSE(QuicStreamFactoryPeer::HasActiveSession(
620 &factory_, host_port_pair_, is_https_));
621 EXPECT_FALSE(QuicStreamFactoryPeer::HasActiveSession(
622 &factory_, server2, is_https_));
624 TestCompletionCallback callback3;
625 QuicStreamRequest request3(&factory_);
626 EXPECT_EQ(OK,
627 request3.Request(server2,
628 is_https_,
629 privacy_mode_,
630 "GET",
631 net_log_,
632 callback3.callback()));
633 scoped_ptr<QuicHttpStream> stream3 = request3.ReleaseStream();
634 EXPECT_TRUE(stream3.get());
636 EXPECT_TRUE(QuicStreamFactoryPeer::HasActiveSession(
637 &factory_, server2, is_https_));
639 EXPECT_TRUE(socket_data1.at_read_eof());
640 EXPECT_TRUE(socket_data1.at_write_eof());
641 EXPECT_TRUE(socket_data2.at_read_eof());
642 EXPECT_TRUE(socket_data2.at_write_eof());
645 TEST_P(QuicStreamFactoryTest, HttpsPooling) {
646 MockRead reads[] = {
647 MockRead(ASYNC, OK, 0) // EOF
649 DeterministicSocketData socket_data(reads, arraysize(reads), nullptr, 0);
650 socket_factory_.AddSocketDataProvider(&socket_data);
651 socket_data.StopAfter(1);
653 HostPortPair server1("www.example.org", 443);
654 HostPortPair server2("mail.example.org", 443);
656 // Load a cert that is valid for:
657 // www.example.org (server1)
658 // mail.example.org (server2)
659 // www.example.com
660 base::FilePath certs_dir = GetTestCertsDirectory();
661 scoped_refptr<X509Certificate> test_cert(
662 ImportCertFromFile(certs_dir, "spdy_pooling.pem"));
663 ASSERT_NE(static_cast<X509Certificate*>(nullptr), test_cert.get());
664 ProofVerifyDetailsChromium verify_details;
665 verify_details.cert_verify_result.verified_cert = test_cert;
666 verify_details.cert_verify_result.is_issued_by_known_root = true;
667 crypto_client_stream_factory_.set_proof_verify_details(&verify_details);
669 host_resolver_.set_synchronous_mode(true);
670 host_resolver_.rules()->AddIPLiteralRule(server1.host(), "192.168.0.1", "");
671 host_resolver_.rules()->AddIPLiteralRule(server2.host(), "192.168.0.1", "");
673 QuicStreamRequest request(&factory_);
674 is_https_ = true;
675 EXPECT_EQ(OK,
676 request.Request(server1,
677 is_https_,
678 privacy_mode_,
679 "GET",
680 net_log_,
681 callback_.callback()));
682 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
683 EXPECT_TRUE(stream.get());
685 TestCompletionCallback callback;
686 QuicStreamRequest request2(&factory_);
687 EXPECT_EQ(OK,
688 request2.Request(server2,
689 is_https_,
690 privacy_mode_,
691 "GET",
692 net_log_,
693 callback_.callback()));
694 scoped_ptr<QuicHttpStream> stream2 = request2.ReleaseStream();
695 EXPECT_TRUE(stream2.get());
697 EXPECT_EQ(QuicStreamFactoryPeer::GetActiveSession(
698 &factory_, server1, is_https_),
699 QuicStreamFactoryPeer::GetActiveSession(
700 &factory_, server2, is_https_));
702 EXPECT_TRUE(socket_data.at_read_eof());
703 EXPECT_TRUE(socket_data.at_write_eof());
706 TEST_P(QuicStreamFactoryTest, NoHttpsPoolingIfDisabled) {
707 MockRead reads[] = {
708 MockRead(ASYNC, OK, 0) // EOF
710 DeterministicSocketData socket_data1(reads, arraysize(reads), nullptr, 0);
711 DeterministicSocketData socket_data2(reads, arraysize(reads), nullptr, 0);
712 socket_factory_.AddSocketDataProvider(&socket_data1);
713 socket_factory_.AddSocketDataProvider(&socket_data2);
714 socket_data1.StopAfter(1);
715 socket_data2.StopAfter(1);
717 HostPortPair server1("www.example.org", 443);
718 HostPortPair server2("mail.example.org", 443);
720 // Load a cert that is valid for:
721 // www.example.org (server1)
722 // mail.example.org (server2)
723 // www.example.com
724 base::FilePath certs_dir = GetTestCertsDirectory();
725 scoped_refptr<X509Certificate> test_cert(
726 ImportCertFromFile(certs_dir, "spdy_pooling.pem"));
727 ASSERT_NE(static_cast<X509Certificate*>(nullptr), test_cert.get());
728 ProofVerifyDetailsChromium verify_details;
729 verify_details.cert_verify_result.verified_cert = test_cert;
730 verify_details.cert_verify_result.is_issued_by_known_root = true;
731 crypto_client_stream_factory_.set_proof_verify_details(&verify_details);
733 host_resolver_.set_synchronous_mode(true);
734 host_resolver_.rules()->AddIPLiteralRule(server1.host(), "192.168.0.1", "");
735 host_resolver_.rules()->AddIPLiteralRule(server2.host(), "192.168.0.1", "");
737 // Disable connection pooling.
738 QuicStreamFactoryPeer::DisableConnectionPooling(&factory_);
740 QuicStreamRequest request(&factory_);
741 is_https_ = true;
742 EXPECT_EQ(OK,
743 request.Request(server1,
744 is_https_,
745 privacy_mode_,
746 "GET",
747 net_log_,
748 callback_.callback()));
749 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
750 EXPECT_TRUE(stream.get());
752 TestCompletionCallback callback;
753 QuicStreamRequest request2(&factory_);
754 EXPECT_EQ(OK,
755 request2.Request(server2,
756 is_https_,
757 privacy_mode_,
758 "GET",
759 net_log_,
760 callback_.callback()));
761 scoped_ptr<QuicHttpStream> stream2 = request2.ReleaseStream();
762 EXPECT_TRUE(stream2.get());
764 EXPECT_NE(QuicStreamFactoryPeer::GetActiveSession(
765 &factory_, server1, is_https_),
766 QuicStreamFactoryPeer::GetActiveSession(
767 &factory_, server2, is_https_));
769 EXPECT_TRUE(socket_data1.at_read_eof());
770 EXPECT_TRUE(socket_data1.at_write_eof());
771 EXPECT_TRUE(socket_data2.at_read_eof());
772 EXPECT_TRUE(socket_data2.at_write_eof());
775 TEST_P(QuicStreamFactoryTest, NoHttpsPoolingWithCertMismatch) {
776 MockRead reads[] = {
777 MockRead(ASYNC, OK, 0) // EOF
779 DeterministicSocketData socket_data1(reads, arraysize(reads), nullptr, 0);
780 DeterministicSocketData socket_data2(reads, arraysize(reads), nullptr, 0);
781 socket_factory_.AddSocketDataProvider(&socket_data1);
782 socket_factory_.AddSocketDataProvider(&socket_data2);
783 socket_data1.StopAfter(1);
784 socket_data2.StopAfter(1);
786 HostPortPair server1("www.example.org", 443);
787 HostPortPair server2("mail.google.com", 443);
789 // Load a cert that is valid for:
790 // www.example.org (server1)
791 // mail.example.org
792 // www.example.com
793 // But is not valid for mail.google.com (server2).
794 base::FilePath certs_dir = GetTestCertsDirectory();
795 scoped_refptr<X509Certificate> test_cert(
796 ImportCertFromFile(certs_dir, "spdy_pooling.pem"));
797 ASSERT_NE(static_cast<X509Certificate*>(nullptr), test_cert.get());
798 ProofVerifyDetailsChromium verify_details;
799 verify_details.cert_verify_result.verified_cert = test_cert;
800 crypto_client_stream_factory_.set_proof_verify_details(&verify_details);
803 host_resolver_.set_synchronous_mode(true);
804 host_resolver_.rules()->AddIPLiteralRule(server1.host(), "192.168.0.1", "");
805 host_resolver_.rules()->AddIPLiteralRule(server2.host(), "192.168.0.1", "");
807 QuicStreamRequest request(&factory_);
808 is_https_ = true;
809 EXPECT_EQ(OK,
810 request.Request(server1,
811 is_https_,
812 privacy_mode_,
813 "GET",
814 net_log_,
815 callback_.callback()));
816 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
817 EXPECT_TRUE(stream.get());
819 TestCompletionCallback callback;
820 QuicStreamRequest request2(&factory_);
821 EXPECT_EQ(OK,
822 request2.Request(server2,
823 is_https_,
824 privacy_mode_,
825 "GET",
826 net_log_,
827 callback_.callback()));
828 scoped_ptr<QuicHttpStream> stream2 = request2.ReleaseStream();
829 EXPECT_TRUE(stream2.get());
831 EXPECT_NE(QuicStreamFactoryPeer::GetActiveSession(
832 &factory_, server1, is_https_),
833 QuicStreamFactoryPeer::GetActiveSession(
834 &factory_, server2, is_https_));
836 EXPECT_TRUE(socket_data1.at_read_eof());
837 EXPECT_TRUE(socket_data1.at_write_eof());
838 EXPECT_TRUE(socket_data2.at_read_eof());
839 EXPECT_TRUE(socket_data2.at_write_eof());
842 TEST_P(QuicStreamFactoryTest, HttpsPoolingWithMatchingPins) {
843 MockRead reads[] = {
844 MockRead(ASYNC, OK, 0) // EOF
846 DeterministicSocketData socket_data(reads, arraysize(reads), nullptr, 0);
847 socket_factory_.AddSocketDataProvider(&socket_data);
848 socket_data.StopAfter(1);
850 HostPortPair server1("www.example.org", 443);
851 HostPortPair server2("mail.example.org", 443);
852 uint8 primary_pin = 1;
853 uint8 backup_pin = 2;
854 test::AddPin(&transport_security_state_, "mail.example.org", primary_pin,
855 backup_pin);
857 // Load a cert that is valid for:
858 // www.example.org (server1)
859 // mail.example.org (server2)
860 base::FilePath certs_dir = GetTestCertsDirectory();
861 scoped_refptr<X509Certificate> test_cert(
862 ImportCertFromFile(certs_dir, "spdy_pooling.pem"));
863 ASSERT_NE(static_cast<X509Certificate*>(nullptr), test_cert.get());
864 ProofVerifyDetailsChromium verify_details;
865 verify_details.cert_verify_result.verified_cert = test_cert;
866 verify_details.cert_verify_result.is_issued_by_known_root = true;
867 verify_details.cert_verify_result.public_key_hashes.push_back(
868 test::GetTestHashValue(primary_pin));
869 crypto_client_stream_factory_.set_proof_verify_details(&verify_details);
872 host_resolver_.set_synchronous_mode(true);
873 host_resolver_.rules()->AddIPLiteralRule(server1.host(), "192.168.0.1", "");
874 host_resolver_.rules()->AddIPLiteralRule(server2.host(), "192.168.0.1", "");
876 QuicStreamRequest request(&factory_);
877 is_https_ = true;
878 EXPECT_EQ(OK,
879 request.Request(server1,
880 is_https_,
881 privacy_mode_,
882 "GET",
883 net_log_,
884 callback_.callback()));
885 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
886 EXPECT_TRUE(stream.get());
888 TestCompletionCallback callback;
889 QuicStreamRequest request2(&factory_);
890 EXPECT_EQ(OK,
891 request2.Request(server2,
892 is_https_,
893 privacy_mode_,
894 "GET",
895 net_log_,
896 callback_.callback()));
897 scoped_ptr<QuicHttpStream> stream2 = request2.ReleaseStream();
898 EXPECT_TRUE(stream2.get());
900 EXPECT_EQ(QuicStreamFactoryPeer::GetActiveSession(
901 &factory_, server1, is_https_),
902 QuicStreamFactoryPeer::GetActiveSession(
903 &factory_, server2, is_https_));
905 EXPECT_TRUE(socket_data.at_read_eof());
906 EXPECT_TRUE(socket_data.at_write_eof());
909 TEST_P(QuicStreamFactoryTest, NoHttpsPoolingWithMatchingPinsIfDisabled) {
910 MockRead reads[] = {
911 MockRead(ASYNC, OK, 0) // EOF
913 DeterministicSocketData socket_data1(reads, arraysize(reads), nullptr, 0);
914 DeterministicSocketData socket_data2(reads, arraysize(reads), nullptr, 0);
915 socket_factory_.AddSocketDataProvider(&socket_data1);
916 socket_factory_.AddSocketDataProvider(&socket_data2);
917 socket_data1.StopAfter(1);
918 socket_data2.StopAfter(1);
920 HostPortPair server1("www.example.org", 443);
921 HostPortPair server2("mail.example.org", 443);
922 uint8 primary_pin = 1;
923 uint8 backup_pin = 2;
924 test::AddPin(&transport_security_state_, "mail.example.org", primary_pin,
925 backup_pin);
927 // Load a cert that is valid for:
928 // www.example.org (server1)
929 // mail.example.org (server2)
930 base::FilePath certs_dir = GetTestCertsDirectory();
931 scoped_refptr<X509Certificate> test_cert(
932 ImportCertFromFile(certs_dir, "spdy_pooling.pem"));
933 ASSERT_NE(static_cast<X509Certificate*>(nullptr), test_cert.get());
934 ProofVerifyDetailsChromium verify_details;
935 verify_details.cert_verify_result.verified_cert = test_cert;
936 verify_details.cert_verify_result.is_issued_by_known_root = true;
937 verify_details.cert_verify_result.public_key_hashes.push_back(
938 test::GetTestHashValue(primary_pin));
939 crypto_client_stream_factory_.set_proof_verify_details(&verify_details);
942 host_resolver_.set_synchronous_mode(true);
943 host_resolver_.rules()->AddIPLiteralRule(server1.host(), "192.168.0.1", "");
944 host_resolver_.rules()->AddIPLiteralRule(server2.host(), "192.168.0.1", "");
946 // Disable connection pooling.
947 QuicStreamFactoryPeer::DisableConnectionPooling(&factory_);
949 QuicStreamRequest request(&factory_);
950 is_https_ = true;
951 EXPECT_EQ(OK,
952 request.Request(server1,
953 is_https_,
954 privacy_mode_,
955 "GET",
956 net_log_,
957 callback_.callback()));
958 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
959 EXPECT_TRUE(stream.get());
961 TestCompletionCallback callback;
962 QuicStreamRequest request2(&factory_);
963 EXPECT_EQ(OK,
964 request2.Request(server2,
965 is_https_,
966 privacy_mode_,
967 "GET",
968 net_log_,
969 callback_.callback()));
970 scoped_ptr<QuicHttpStream> stream2 = request2.ReleaseStream();
971 EXPECT_TRUE(stream2.get());
973 EXPECT_NE(QuicStreamFactoryPeer::GetActiveSession(
974 &factory_, server1, is_https_),
975 QuicStreamFactoryPeer::GetActiveSession(
976 &factory_, server2, is_https_));
978 EXPECT_TRUE(socket_data1.at_read_eof());
979 EXPECT_TRUE(socket_data1.at_write_eof());
980 EXPECT_TRUE(socket_data2.at_read_eof());
981 EXPECT_TRUE(socket_data2.at_write_eof());
984 TEST_P(QuicStreamFactoryTest, NoHttpsPoolingWithDifferentPins) {
985 MockRead reads[] = {
986 MockRead(ASYNC, OK, 0) // EOF
988 DeterministicSocketData socket_data1(reads, arraysize(reads), nullptr, 0);
989 DeterministicSocketData socket_data2(reads, arraysize(reads), nullptr, 0);
990 socket_factory_.AddSocketDataProvider(&socket_data1);
991 socket_factory_.AddSocketDataProvider(&socket_data2);
992 socket_data1.StopAfter(1);
993 socket_data2.StopAfter(1);
995 HostPortPair server1("www.example.org", 443);
996 HostPortPair server2("mail.example.org", 443);
997 uint8 primary_pin = 1;
998 uint8 backup_pin = 2;
999 uint8 bad_pin = 3;
1000 test::AddPin(&transport_security_state_, "mail.example.org", primary_pin,
1001 backup_pin);
1003 // Load a cert that is valid for:
1004 // www.example.org (server1)
1005 // mail.example.org (server2)
1006 base::FilePath certs_dir = GetTestCertsDirectory();
1007 scoped_refptr<X509Certificate> test_cert(
1008 ImportCertFromFile(certs_dir, "spdy_pooling.pem"));
1009 ASSERT_NE(static_cast<X509Certificate*>(nullptr), test_cert.get());
1010 ProofVerifyDetailsChromium verify_details;
1011 verify_details.cert_verify_result.verified_cert = test_cert;
1012 verify_details.cert_verify_result.is_issued_by_known_root = true;
1013 verify_details.cert_verify_result.public_key_hashes.push_back(
1014 test::GetTestHashValue(bad_pin));
1015 crypto_client_stream_factory_.set_proof_verify_details(&verify_details);
1018 host_resolver_.set_synchronous_mode(true);
1019 host_resolver_.rules()->AddIPLiteralRule(server1.host(), "192.168.0.1", "");
1020 host_resolver_.rules()->AddIPLiteralRule(server2.host(), "192.168.0.1", "");
1022 QuicStreamRequest request(&factory_);
1023 is_https_ = true;
1024 EXPECT_EQ(OK,
1025 request.Request(server1,
1026 is_https_,
1027 privacy_mode_,
1028 "GET",
1029 net_log_,
1030 callback_.callback()));
1031 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
1032 EXPECT_TRUE(stream.get());
1034 TestCompletionCallback callback;
1035 QuicStreamRequest request2(&factory_);
1036 EXPECT_EQ(OK,
1037 request2.Request(server2,
1038 is_https_,
1039 privacy_mode_,
1040 "GET",
1041 net_log_,
1042 callback_.callback()));
1043 scoped_ptr<QuicHttpStream> stream2 = request2.ReleaseStream();
1044 EXPECT_TRUE(stream2.get());
1046 EXPECT_NE(QuicStreamFactoryPeer::GetActiveSession(
1047 &factory_, server1, is_https_),
1048 QuicStreamFactoryPeer::GetActiveSession(
1049 &factory_, server2, is_https_));
1051 EXPECT_TRUE(socket_data1.at_read_eof());
1052 EXPECT_TRUE(socket_data1.at_write_eof());
1053 EXPECT_TRUE(socket_data2.at_read_eof());
1054 EXPECT_TRUE(socket_data2.at_write_eof());
1057 TEST_P(QuicStreamFactoryTest, Goaway) {
1058 MockRead reads[] = {
1059 MockRead(ASYNC, OK, 0) // EOF
1061 DeterministicSocketData socket_data(reads, arraysize(reads), nullptr, 0);
1062 socket_data.StopAfter(1);
1063 socket_factory_.AddSocketDataProvider(&socket_data);
1064 DeterministicSocketData socket_data2(reads, arraysize(reads), nullptr, 0);
1065 socket_data2.StopAfter(1);
1066 socket_factory_.AddSocketDataProvider(&socket_data2);
1068 QuicStreamRequest request(&factory_);
1069 EXPECT_EQ(ERR_IO_PENDING,
1070 request.Request(host_port_pair_,
1071 is_https_,
1072 privacy_mode_,
1073 "GET",
1074 net_log_,
1075 callback_.callback()));
1077 EXPECT_EQ(OK, callback_.WaitForResult());
1078 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
1079 EXPECT_TRUE(stream.get());
1081 // Mark the session as going away. Ensure that while it is still alive
1082 // that it is no longer active.
1083 QuicClientSession* session = QuicStreamFactoryPeer::GetActiveSession(
1084 &factory_, host_port_pair_, is_https_);
1085 factory_.OnSessionGoingAway(session);
1086 EXPECT_EQ(true, QuicStreamFactoryPeer::IsLiveSession(&factory_, session));
1087 EXPECT_FALSE(QuicStreamFactoryPeer::HasActiveSession(
1088 &factory_, host_port_pair_, is_https_));
1089 EXPECT_EQ(nullptr, CreateIfSessionExists(host_port_pair_, net_log_).get());
1091 // Create a new request for the same destination and verify that a
1092 // new session is created.
1093 QuicStreamRequest request2(&factory_);
1094 EXPECT_EQ(ERR_IO_PENDING,
1095 request2.Request(host_port_pair_,
1096 is_https_,
1097 privacy_mode_,
1098 "GET",
1099 net_log_,
1100 callback_.callback()));
1101 EXPECT_EQ(OK, callback_.WaitForResult());
1102 scoped_ptr<QuicHttpStream> stream2 = request2.ReleaseStream();
1103 EXPECT_TRUE(stream2.get());
1105 EXPECT_TRUE(QuicStreamFactoryPeer::HasActiveSession(&factory_,
1106 host_port_pair_,
1107 is_https_));
1108 EXPECT_NE(session,
1109 QuicStreamFactoryPeer::GetActiveSession(
1110 &factory_, host_port_pair_, is_https_));
1111 EXPECT_EQ(true, QuicStreamFactoryPeer::IsLiveSession(&factory_, session));
1113 stream2.reset();
1114 stream.reset();
1116 EXPECT_TRUE(socket_data.at_read_eof());
1117 EXPECT_TRUE(socket_data.at_write_eof());
1118 EXPECT_TRUE(socket_data2.at_read_eof());
1119 EXPECT_TRUE(socket_data2.at_write_eof());
1122 TEST_P(QuicStreamFactoryTest, MaxOpenStream) {
1123 MockRead reads[] = {
1124 MockRead(ASYNC, OK, 0) // EOF
1126 QuicStreamId stream_id = kClientDataStreamId1;
1127 scoped_ptr<QuicEncryptedPacket> rst(
1128 maker_.MakeRstPacket(1, true, stream_id, QUIC_STREAM_CANCELLED));
1129 MockWrite writes[] = {
1130 MockWrite(ASYNC, rst->data(), rst->length(), 1),
1132 DeterministicSocketData socket_data(reads, arraysize(reads),
1133 writes, arraysize(writes));
1134 socket_factory_.AddSocketDataProvider(&socket_data);
1135 socket_data.StopAfter(1);
1137 HttpRequestInfo request_info;
1138 std::vector<QuicHttpStream*> streams;
1139 // The MockCryptoClientStream sets max_open_streams to be
1140 // kDefaultMaxStreamsPerConnection / 2.
1141 for (size_t i = 0; i < kDefaultMaxStreamsPerConnection / 2; i++) {
1142 QuicStreamRequest request(&factory_);
1143 int rv = request.Request(host_port_pair_,
1144 is_https_,
1145 privacy_mode_,
1146 "GET",
1147 net_log_,
1148 callback_.callback());
1149 if (i == 0) {
1150 EXPECT_EQ(ERR_IO_PENDING, rv);
1151 EXPECT_EQ(OK, callback_.WaitForResult());
1152 } else {
1153 EXPECT_EQ(OK, rv);
1155 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
1156 EXPECT_TRUE(stream);
1157 EXPECT_EQ(OK, stream->InitializeStream(
1158 &request_info, DEFAULT_PRIORITY, net_log_, CompletionCallback()));
1159 streams.push_back(stream.release());
1162 QuicStreamRequest request(&factory_);
1163 EXPECT_EQ(OK,
1164 request.Request(host_port_pair_,
1165 is_https_,
1166 privacy_mode_,
1167 "GET",
1168 net_log_,
1169 CompletionCallback()));
1170 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
1171 EXPECT_TRUE(stream);
1172 EXPECT_EQ(ERR_IO_PENDING, stream->InitializeStream(
1173 &request_info, DEFAULT_PRIORITY, net_log_, callback_.callback()));
1175 // Close the first stream.
1176 streams.front()->Close(false);
1178 ASSERT_TRUE(callback_.have_result());
1180 EXPECT_EQ(OK, callback_.WaitForResult());
1182 EXPECT_TRUE(socket_data.at_read_eof());
1183 EXPECT_TRUE(socket_data.at_write_eof());
1184 STLDeleteElements(&streams);
1187 TEST_P(QuicStreamFactoryTest, ResolutionErrorInCreate) {
1188 DeterministicSocketData socket_data(nullptr, 0, nullptr, 0);
1189 socket_factory_.AddSocketDataProvider(&socket_data);
1191 host_resolver_.rules()->AddSimulatedFailure(kDefaultServerHostName);
1193 QuicStreamRequest request(&factory_);
1194 EXPECT_EQ(ERR_IO_PENDING,
1195 request.Request(host_port_pair_,
1196 is_https_,
1197 privacy_mode_,
1198 "GET",
1199 net_log_,
1200 callback_.callback()));
1202 EXPECT_EQ(ERR_NAME_NOT_RESOLVED, callback_.WaitForResult());
1204 EXPECT_TRUE(socket_data.at_read_eof());
1205 EXPECT_TRUE(socket_data.at_write_eof());
1208 TEST_P(QuicStreamFactoryTest, ConnectErrorInCreate) {
1209 MockConnect connect(SYNCHRONOUS, ERR_ADDRESS_IN_USE);
1210 DeterministicSocketData socket_data(nullptr, 0, nullptr, 0);
1211 socket_data.set_connect_data(connect);
1212 socket_factory_.AddSocketDataProvider(&socket_data);
1213 socket_data.StopAfter(1);
1215 QuicStreamRequest request(&factory_);
1216 EXPECT_EQ(ERR_IO_PENDING,
1217 request.Request(host_port_pair_,
1218 is_https_,
1219 privacy_mode_,
1220 "GET",
1221 net_log_,
1222 callback_.callback()));
1224 EXPECT_EQ(ERR_ADDRESS_IN_USE, callback_.WaitForResult());
1226 EXPECT_TRUE(socket_data.at_read_eof());
1227 EXPECT_TRUE(socket_data.at_write_eof());
1230 TEST_P(QuicStreamFactoryTest, CancelCreate) {
1231 MockRead reads[] = {
1232 MockRead(ASYNC, OK, 0) // EOF
1234 DeterministicSocketData socket_data(reads, arraysize(reads), nullptr, 0);
1235 socket_factory_.AddSocketDataProvider(&socket_data);
1237 QuicStreamRequest request(&factory_);
1238 EXPECT_EQ(ERR_IO_PENDING,
1239 request.Request(host_port_pair_,
1240 is_https_,
1241 privacy_mode_,
1242 "GET",
1243 net_log_,
1244 callback_.callback()));
1247 socket_data.StopAfter(1);
1248 base::RunLoop run_loop;
1249 run_loop.RunUntilIdle();
1251 scoped_ptr<QuicHttpStream> stream(
1252 CreateIfSessionExists(host_port_pair_, net_log_));
1253 EXPECT_TRUE(stream.get());
1254 stream.reset();
1256 EXPECT_TRUE(socket_data.at_read_eof());
1257 EXPECT_TRUE(socket_data.at_write_eof());
1260 TEST_P(QuicStreamFactoryTest, CreateConsistentEphemeralPort) {
1261 // Sequentially connect to the default host, then another host, and then the
1262 // default host. Verify that the default host gets a consistent ephemeral
1263 // port, that is different from the other host's connection.
1265 std::string other_server_name = "other.google.com";
1266 EXPECT_NE(kDefaultServerHostName, other_server_name);
1267 HostPortPair host_port_pair2(other_server_name, kDefaultServerPort);
1269 int original_port = GetSourcePortForNewSession(host_port_pair_);
1270 EXPECT_NE(original_port, GetSourcePortForNewSession(host_port_pair2));
1271 EXPECT_EQ(original_port, GetSourcePortForNewSession(host_port_pair_));
1274 TEST_P(QuicStreamFactoryTest, GoAwayDisablesConsistentEphemeralPort) {
1275 // Get a session to the host using the port suggester.
1276 int original_port =
1277 GetSourcePortForNewSessionAndGoAway(host_port_pair_);
1278 // Verify that the port is different after the goaway.
1279 EXPECT_NE(original_port, GetSourcePortForNewSession(host_port_pair_));
1280 // Since the previous session did not goaway we should see the original port.
1281 EXPECT_EQ(original_port, GetSourcePortForNewSession(host_port_pair_));
1284 TEST_P(QuicStreamFactoryTest, CloseAllSessions) {
1285 MockRead reads[] = {
1286 MockRead(ASYNC, 0, 0) // EOF
1288 scoped_ptr<QuicEncryptedPacket> rst(ConstructRstPacket());
1289 std::vector<MockWrite> writes;
1290 writes.push_back(MockWrite(ASYNC, rst->data(), rst->length(), 1));
1291 DeterministicSocketData socket_data(reads, arraysize(reads),
1292 writes.empty() ? nullptr : &writes[0],
1293 writes.size());
1294 socket_factory_.AddSocketDataProvider(&socket_data);
1295 socket_data.StopAfter(1);
1297 MockRead reads2[] = {
1298 MockRead(ASYNC, 0, 0) // EOF
1300 DeterministicSocketData socket_data2(reads2, arraysize(reads2), nullptr, 0);
1301 socket_factory_.AddSocketDataProvider(&socket_data2);
1302 socket_data2.StopAfter(1);
1304 QuicStreamRequest request(&factory_);
1305 EXPECT_EQ(ERR_IO_PENDING,
1306 request.Request(host_port_pair_,
1307 is_https_,
1308 privacy_mode_,
1309 "GET",
1310 net_log_,
1311 callback_.callback()));
1313 EXPECT_EQ(OK, callback_.WaitForResult());
1314 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
1315 HttpRequestInfo request_info;
1316 EXPECT_EQ(OK, stream->InitializeStream(&request_info,
1317 DEFAULT_PRIORITY,
1318 net_log_, CompletionCallback()));
1320 // Close the session and verify that stream saw the error.
1321 factory_.CloseAllSessions(ERR_INTERNET_DISCONNECTED);
1322 EXPECT_EQ(ERR_INTERNET_DISCONNECTED,
1323 stream->ReadResponseHeaders(callback_.callback()));
1325 // Now attempting to request a stream to the same origin should create
1326 // a new session.
1328 QuicStreamRequest request2(&factory_);
1329 EXPECT_EQ(ERR_IO_PENDING,
1330 request2.Request(host_port_pair_,
1331 is_https_,
1332 privacy_mode_,
1333 "GET",
1334 net_log_,
1335 callback_.callback()));
1337 EXPECT_EQ(OK, callback_.WaitForResult());
1338 stream = request2.ReleaseStream();
1339 stream.reset(); // Will reset stream 3.
1341 EXPECT_TRUE(socket_data.at_read_eof());
1342 EXPECT_TRUE(socket_data.at_write_eof());
1343 EXPECT_TRUE(socket_data2.at_read_eof());
1344 EXPECT_TRUE(socket_data2.at_write_eof());
1347 TEST_P(QuicStreamFactoryTest, OnIPAddressChanged) {
1348 MockRead reads[] = {
1349 MockRead(ASYNC, 0, 0) // EOF
1351 scoped_ptr<QuicEncryptedPacket> rst(ConstructRstPacket());
1352 std::vector<MockWrite> writes;
1353 writes.push_back(MockWrite(ASYNC, rst->data(), rst->length(), 1));
1354 DeterministicSocketData socket_data(reads, arraysize(reads),
1355 writes.empty() ? nullptr : &writes[0],
1356 writes.size());
1357 socket_factory_.AddSocketDataProvider(&socket_data);
1358 socket_data.StopAfter(1);
1360 MockRead reads2[] = {
1361 MockRead(ASYNC, 0, 0) // EOF
1363 DeterministicSocketData socket_data2(reads2, arraysize(reads2), nullptr, 0);
1364 socket_factory_.AddSocketDataProvider(&socket_data2);
1365 socket_data2.StopAfter(1);
1367 QuicStreamRequest request(&factory_);
1368 EXPECT_EQ(ERR_IO_PENDING,
1369 request.Request(host_port_pair_,
1370 is_https_,
1371 privacy_mode_,
1372 "GET",
1373 net_log_,
1374 callback_.callback()));
1376 EXPECT_EQ(OK, callback_.WaitForResult());
1377 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
1378 HttpRequestInfo request_info;
1379 EXPECT_EQ(OK, stream->InitializeStream(&request_info,
1380 DEFAULT_PRIORITY,
1381 net_log_, CompletionCallback()));
1383 // Change the IP address and verify that stream saw the error.
1384 factory_.OnIPAddressChanged();
1385 EXPECT_EQ(ERR_NETWORK_CHANGED,
1386 stream->ReadResponseHeaders(callback_.callback()));
1387 EXPECT_TRUE(factory_.require_confirmation());
1389 // Now attempting to request a stream to the same origin should create
1390 // a new session.
1392 QuicStreamRequest request2(&factory_);
1393 EXPECT_EQ(ERR_IO_PENDING,
1394 request2.Request(host_port_pair_,
1395 is_https_,
1396 privacy_mode_,
1397 "GET",
1398 net_log_,
1399 callback_.callback()));
1401 EXPECT_EQ(OK, callback_.WaitForResult());
1402 stream = request2.ReleaseStream();
1403 stream.reset(); // Will reset stream 3.
1405 EXPECT_TRUE(socket_data.at_read_eof());
1406 EXPECT_TRUE(socket_data.at_write_eof());
1407 EXPECT_TRUE(socket_data2.at_read_eof());
1408 EXPECT_TRUE(socket_data2.at_write_eof());
1411 TEST_P(QuicStreamFactoryTest, OnCertAdded) {
1412 MockRead reads[] = {
1413 MockRead(ASYNC, 0, 0) // EOF
1415 scoped_ptr<QuicEncryptedPacket> rst(ConstructRstPacket());
1416 std::vector<MockWrite> writes;
1417 writes.push_back(MockWrite(ASYNC, rst->data(), rst->length(), 1));
1418 DeterministicSocketData socket_data(reads, arraysize(reads),
1419 writes.empty() ? nullptr : &writes[0],
1420 writes.size());
1421 socket_factory_.AddSocketDataProvider(&socket_data);
1422 socket_data.StopAfter(1);
1424 MockRead reads2[] = {
1425 MockRead(ASYNC, 0, 0) // EOF
1427 DeterministicSocketData socket_data2(reads2, arraysize(reads2), nullptr, 0);
1428 socket_factory_.AddSocketDataProvider(&socket_data2);
1429 socket_data2.StopAfter(1);
1431 QuicStreamRequest request(&factory_);
1432 EXPECT_EQ(ERR_IO_PENDING,
1433 request.Request(host_port_pair_,
1434 is_https_,
1435 privacy_mode_,
1436 "GET",
1437 net_log_,
1438 callback_.callback()));
1440 EXPECT_EQ(OK, callback_.WaitForResult());
1441 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
1442 HttpRequestInfo request_info;
1443 EXPECT_EQ(OK, stream->InitializeStream(&request_info,
1444 DEFAULT_PRIORITY,
1445 net_log_, CompletionCallback()));
1447 // Add a cert and verify that stream saw the event.
1448 factory_.OnCertAdded(nullptr);
1449 EXPECT_EQ(ERR_CERT_DATABASE_CHANGED,
1450 stream->ReadResponseHeaders(callback_.callback()));
1451 EXPECT_FALSE(factory_.require_confirmation());
1453 // Now attempting to request a stream to the same origin should create
1454 // a new session.
1456 QuicStreamRequest request2(&factory_);
1457 EXPECT_EQ(ERR_IO_PENDING,
1458 request2.Request(host_port_pair_,
1459 is_https_,
1460 privacy_mode_,
1461 "GET",
1462 net_log_,
1463 callback_.callback()));
1465 EXPECT_EQ(OK, callback_.WaitForResult());
1466 stream = request2.ReleaseStream();
1467 stream.reset(); // Will reset stream 3.
1469 EXPECT_TRUE(socket_data.at_read_eof());
1470 EXPECT_TRUE(socket_data.at_write_eof());
1471 EXPECT_TRUE(socket_data2.at_read_eof());
1472 EXPECT_TRUE(socket_data2.at_write_eof());
1475 TEST_P(QuicStreamFactoryTest, OnCACertChanged) {
1476 MockRead reads[] = {
1477 MockRead(ASYNC, 0, 0) // EOF
1479 scoped_ptr<QuicEncryptedPacket> rst(ConstructRstPacket());
1480 std::vector<MockWrite> writes;
1481 writes.push_back(MockWrite(ASYNC, rst->data(), rst->length(), 1));
1482 DeterministicSocketData socket_data(reads, arraysize(reads),
1483 writes.empty() ? nullptr : &writes[0],
1484 writes.size());
1485 socket_factory_.AddSocketDataProvider(&socket_data);
1486 socket_data.StopAfter(1);
1488 MockRead reads2[] = {
1489 MockRead(ASYNC, 0, 0) // EOF
1491 DeterministicSocketData socket_data2(reads2, arraysize(reads2), nullptr, 0);
1492 socket_factory_.AddSocketDataProvider(&socket_data2);
1493 socket_data2.StopAfter(1);
1495 QuicStreamRequest request(&factory_);
1496 EXPECT_EQ(ERR_IO_PENDING,
1497 request.Request(host_port_pair_,
1498 is_https_,
1499 privacy_mode_,
1500 "GET",
1501 net_log_,
1502 callback_.callback()));
1504 EXPECT_EQ(OK, callback_.WaitForResult());
1505 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
1506 HttpRequestInfo request_info;
1507 EXPECT_EQ(OK, stream->InitializeStream(&request_info,
1508 DEFAULT_PRIORITY,
1509 net_log_, CompletionCallback()));
1511 // Change the CA cert and verify that stream saw the event.
1512 factory_.OnCACertChanged(nullptr);
1513 EXPECT_EQ(ERR_CERT_DATABASE_CHANGED,
1514 stream->ReadResponseHeaders(callback_.callback()));
1515 EXPECT_FALSE(factory_.require_confirmation());
1517 // Now attempting to request a stream to the same origin should create
1518 // a new session.
1520 QuicStreamRequest request2(&factory_);
1521 EXPECT_EQ(ERR_IO_PENDING,
1522 request2.Request(host_port_pair_,
1523 is_https_,
1524 privacy_mode_,
1525 "GET",
1526 net_log_,
1527 callback_.callback()));
1529 EXPECT_EQ(OK, callback_.WaitForResult());
1530 stream = request2.ReleaseStream();
1531 stream.reset(); // Will reset stream 3.
1533 EXPECT_TRUE(socket_data.at_read_eof());
1534 EXPECT_TRUE(socket_data.at_write_eof());
1535 EXPECT_TRUE(socket_data2.at_read_eof());
1536 EXPECT_TRUE(socket_data2.at_write_eof());
1539 TEST_P(QuicStreamFactoryTest, SharedCryptoConfig) {
1540 vector<string> cannoncial_suffixes;
1541 cannoncial_suffixes.push_back(string(".c.youtube.com"));
1542 cannoncial_suffixes.push_back(string(".googlevideo.com"));
1544 for (unsigned i = 0; i < cannoncial_suffixes.size(); ++i) {
1545 string r1_host_name("r1");
1546 string r2_host_name("r2");
1547 r1_host_name.append(cannoncial_suffixes[i]);
1548 r2_host_name.append(cannoncial_suffixes[i]);
1550 HostPortPair host_port_pair1(r1_host_name, 80);
1551 QuicCryptoClientConfig* crypto_config =
1552 QuicStreamFactoryPeer::GetCryptoConfig(&factory_);
1553 QuicServerId server_id1(host_port_pair1, is_https_, privacy_mode_);
1554 QuicCryptoClientConfig::CachedState* cached1 =
1555 crypto_config->LookupOrCreate(server_id1);
1556 EXPECT_FALSE(cached1->proof_valid());
1557 EXPECT_TRUE(cached1->source_address_token().empty());
1559 // Mutate the cached1 to have different data.
1560 // TODO(rtenneti): mutate other members of CachedState.
1561 cached1->set_source_address_token(r1_host_name);
1562 cached1->SetProofValid();
1564 HostPortPair host_port_pair2(r2_host_name, 80);
1565 QuicServerId server_id2(host_port_pair2, is_https_, privacy_mode_);
1566 QuicCryptoClientConfig::CachedState* cached2 =
1567 crypto_config->LookupOrCreate(server_id2);
1568 EXPECT_EQ(cached1->source_address_token(), cached2->source_address_token());
1569 EXPECT_TRUE(cached2->proof_valid());
1573 TEST_P(QuicStreamFactoryTest, CryptoConfigWhenProofIsInvalid) {
1574 vector<string> cannoncial_suffixes;
1575 cannoncial_suffixes.push_back(string(".c.youtube.com"));
1576 cannoncial_suffixes.push_back(string(".googlevideo.com"));
1578 for (unsigned i = 0; i < cannoncial_suffixes.size(); ++i) {
1579 string r3_host_name("r3");
1580 string r4_host_name("r4");
1581 r3_host_name.append(cannoncial_suffixes[i]);
1582 r4_host_name.append(cannoncial_suffixes[i]);
1584 HostPortPair host_port_pair1(r3_host_name, 80);
1585 QuicCryptoClientConfig* crypto_config =
1586 QuicStreamFactoryPeer::GetCryptoConfig(&factory_);
1587 QuicServerId server_id1(host_port_pair1, is_https_, privacy_mode_);
1588 QuicCryptoClientConfig::CachedState* cached1 =
1589 crypto_config->LookupOrCreate(server_id1);
1590 EXPECT_FALSE(cached1->proof_valid());
1591 EXPECT_TRUE(cached1->source_address_token().empty());
1593 // Mutate the cached1 to have different data.
1594 // TODO(rtenneti): mutate other members of CachedState.
1595 cached1->set_source_address_token(r3_host_name);
1596 cached1->SetProofInvalid();
1598 HostPortPair host_port_pair2(r4_host_name, 80);
1599 QuicServerId server_id2(host_port_pair2, is_https_, privacy_mode_);
1600 QuicCryptoClientConfig::CachedState* cached2 =
1601 crypto_config->LookupOrCreate(server_id2);
1602 EXPECT_NE(cached1->source_address_token(), cached2->source_address_token());
1603 EXPECT_TRUE(cached2->source_address_token().empty());
1604 EXPECT_FALSE(cached2->proof_valid());
1608 TEST_P(QuicStreamFactoryTest, CancelWaitForDataReady) {
1609 // Don't race quic connections when testing cancel reading of server config
1610 // from disk cache.
1611 if (GetParam().enable_connection_racing)
1612 return;
1613 factory_.set_quic_server_info_factory(&quic_server_info_factory_);
1614 QuicStreamFactoryPeer::SetTaskRunner(&factory_, runner_.get());
1615 const size_t kLoadServerInfoTimeoutMs = 50;
1616 QuicStreamFactoryPeer::SetLoadServerInfoTimeout(
1617 &factory_, kLoadServerInfoTimeoutMs);
1619 MockRead reads[] = {
1620 MockRead(ASYNC, OK, 0) // EOF
1622 DeterministicSocketData socket_data(reads, arraysize(reads), nullptr, 0);
1623 socket_factory_.AddSocketDataProvider(&socket_data);
1624 socket_data.StopAfter(1);
1626 crypto_client_stream_factory_.set_handshake_mode(
1627 MockCryptoClientStream::ZERO_RTT);
1628 host_resolver_.set_synchronous_mode(true);
1629 host_resolver_.rules()->AddIPLiteralRule(host_port_pair_.host(),
1630 "192.168.0.1", "");
1632 QuicStreamRequest request(&factory_);
1633 EXPECT_EQ(ERR_IO_PENDING,
1634 request.Request(host_port_pair_,
1635 is_https_,
1636 privacy_mode_,
1637 "GET",
1638 net_log_,
1639 callback_.callback()));
1641 // Verify that the CancelWaitForDataReady task has been posted.
1642 ASSERT_EQ(1u, runner_->GetPostedTasks().size());
1643 EXPECT_EQ(base::TimeDelta::FromMilliseconds(kLoadServerInfoTimeoutMs),
1644 runner_->GetPostedTasks()[0].delay);
1646 runner_->RunNextTask();
1647 ASSERT_EQ(0u, runner_->GetPostedTasks().size());
1649 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
1650 EXPECT_TRUE(stream.get());
1651 EXPECT_TRUE(socket_data.at_read_eof());
1652 EXPECT_TRUE(socket_data.at_write_eof());
1655 TEST_P(QuicStreamFactoryTest, RacingConnections) {
1656 if (!GetParam().enable_connection_racing)
1657 return;
1658 factory_.set_quic_server_info_factory(&quic_server_info_factory_);
1659 QuicStreamFactoryPeer::SetTaskRunner(&factory_, runner_.get());
1660 const size_t kLoadServerInfoTimeoutMs = 50;
1661 QuicStreamFactoryPeer::SetLoadServerInfoTimeout(&factory_,
1662 kLoadServerInfoTimeoutMs);
1664 MockRead reads[] = {
1665 MockRead(ASYNC, OK, 0) // EOF
1667 DeterministicSocketData socket_data(reads, arraysize(reads), nullptr, 0);
1668 socket_factory_.AddSocketDataProvider(&socket_data);
1669 socket_data.StopAfter(1);
1671 MockRead reads2[] = {
1672 MockRead(ASYNC, 0, 0) // EOF
1674 DeterministicSocketData socket_data2(reads2, arraysize(reads2), nullptr, 0);
1675 socket_factory_.AddSocketDataProvider(&socket_data2);
1676 socket_data2.StopAfter(1);
1678 crypto_client_stream_factory_.set_handshake_mode(
1679 MockCryptoClientStream::ZERO_RTT);
1680 host_resolver_.set_synchronous_mode(true);
1681 host_resolver_.rules()->AddIPLiteralRule(host_port_pair_.host(),
1682 "192.168.0.1", "");
1684 QuicStreamRequest request(&factory_);
1685 QuicServerId server_id(host_port_pair_, is_https_, privacy_mode_);
1686 EXPECT_EQ(ERR_IO_PENDING,
1687 request.Request(host_port_pair_, is_https_, privacy_mode_, "GET",
1688 net_log_, callback_.callback()));
1689 EXPECT_EQ(2u,
1690 QuicStreamFactoryPeer::GetNumberOfActiveJobs(&factory_, server_id));
1692 runner_->RunNextTask();
1694 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
1695 EXPECT_TRUE(stream.get());
1696 EXPECT_TRUE(socket_data.at_read_eof());
1697 EXPECT_TRUE(socket_data.at_write_eof());
1698 EXPECT_EQ(0u,
1699 QuicStreamFactoryPeer::GetNumberOfActiveJobs(&factory_, server_id));
1702 } // namespace test
1703 } // namespace net