Refactors gesture conversion functions to ui/events/blink
[chromium-blink-merge.git] / net / quic / quic_stream_factory_test.cc
blobfdc023b28ac5d8420c165d16d3cc5baf0530cb7b
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "net/quic/quic_stream_factory.h"
7 #include "base/run_loop.h"
8 #include "base/strings/string_util.h"
9 #include "net/base/test_data_directory.h"
10 #include "net/cert/cert_verifier.h"
11 #include "net/dns/mock_host_resolver.h"
12 #include "net/http/http_response_headers.h"
13 #include "net/http/http_response_info.h"
14 #include "net/http/http_util.h"
15 #include "net/http/transport_security_state.h"
16 #include "net/quic/crypto/crypto_handshake.h"
17 #include "net/quic/crypto/proof_verifier_chromium.h"
18 #include "net/quic/crypto/quic_decrypter.h"
19 #include "net/quic/crypto/quic_encrypter.h"
20 #include "net/quic/crypto/quic_server_info.h"
21 #include "net/quic/quic_http_stream.h"
22 #include "net/quic/quic_server_id.h"
23 #include "net/quic/test_tools/mock_clock.h"
24 #include "net/quic/test_tools/mock_crypto_client_stream_factory.h"
25 #include "net/quic/test_tools/mock_random.h"
26 #include "net/quic/test_tools/quic_test_packet_maker.h"
27 #include "net/quic/test_tools/quic_test_utils.h"
28 #include "net/quic/test_tools/test_task_runner.h"
29 #include "net/socket/socket_test_util.h"
30 #include "net/spdy/spdy_test_utils.h"
31 #include "net/ssl/channel_id_service.h"
32 #include "net/ssl/default_channel_id_store.h"
33 #include "net/test/cert_test_util.h"
34 #include "testing/gtest/include/gtest/gtest.h"
36 using base::StringPiece;
37 using std::ostream;
38 using std::string;
39 using std::vector;
41 namespace net {
42 namespace test {
44 namespace {
45 const char kDefaultServerHostName[] = "www.google.com";
46 const int kDefaultServerPort = 443;
48 // Run all tests with all the combinations of versions and
49 // enable_connection_racing.
50 struct TestParams {
51 TestParams(const QuicVersion version, bool enable_connection_racing)
52 : version(version), enable_connection_racing(enable_connection_racing) {}
54 friend ostream& operator<<(ostream& os, const TestParams& p) {
55 os << "{ version: " << QuicVersionToString(p.version);
56 os << " enable_connection_racing: " << p.enable_connection_racing << " }";
57 return os;
60 QuicVersion version;
61 bool enable_connection_racing;
64 // Constructs various test permutations.
65 vector<TestParams> GetTestParams() {
66 vector<TestParams> params;
67 QuicVersionVector all_supported_versions = QuicSupportedVersions();
68 for (const QuicVersion version : all_supported_versions) {
69 params.push_back(TestParams(version, false));
70 params.push_back(TestParams(version, true));
72 return params;
75 } // namespace anonymous
77 class QuicStreamFactoryPeer {
78 public:
79 static QuicCryptoClientConfig* GetCryptoConfig(QuicStreamFactory* factory) {
80 return &factory->crypto_config_;
83 static bool HasActiveSession(QuicStreamFactory* factory,
84 const HostPortPair& host_port_pair,
85 bool is_https) {
86 QuicServerId server_id(host_port_pair, is_https, PRIVACY_MODE_DISABLED);
87 return factory->HasActiveSession(server_id);
90 static QuicClientSession* GetActiveSession(
91 QuicStreamFactory* factory,
92 const HostPortPair& host_port_pair,
93 bool is_https) {
94 QuicServerId server_id(host_port_pair, is_https, PRIVACY_MODE_DISABLED);
95 DCHECK(factory->HasActiveSession(server_id));
96 return factory->active_sessions_[server_id];
99 static scoped_ptr<QuicHttpStream> CreateIfSessionExists(
100 QuicStreamFactory* factory,
101 const HostPortPair& host_port_pair,
102 bool is_https,
103 const BoundNetLog& net_log) {
104 QuicServerId server_id(host_port_pair, is_https, PRIVACY_MODE_DISABLED);
105 return factory->CreateIfSessionExists(server_id, net_log);
108 static bool IsLiveSession(QuicStreamFactory* factory,
109 QuicClientSession* session) {
110 for (QuicStreamFactory::SessionIdMap::iterator it =
111 factory->all_sessions_.begin();
112 it != factory->all_sessions_.end(); ++it) {
113 if (it->first == session)
114 return true;
116 return false;
119 static void DisableConnectionPooling(QuicStreamFactory* factory) {
120 factory->disable_connection_pooling_ = true;
123 static void SetTaskRunner(QuicStreamFactory* factory,
124 base::TaskRunner* task_runner) {
125 factory->task_runner_ = task_runner;
128 static void SetEnableConnectionRacing(QuicStreamFactory* factory,
129 bool enable_connection_racing) {
130 factory->enable_connection_racing_ = enable_connection_racing;
133 static void SetDisableDiskCache(QuicStreamFactory* factory,
134 bool disable_disk_cache) {
135 factory->disable_disk_cache_ = disable_disk_cache;
138 static 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_srtt_multiplier=*/0.0f,
206 /*enable_connection_racing=*/false,
207 /*enable_non_blocking_io=*/true,
208 /*disable_disk_cache=*/false,
209 /*receive_buffer_size=*/0,
210 QuicTagVector()),
211 host_port_pair_(kDefaultServerHostName, kDefaultServerPort),
212 is_https_(false),
213 privacy_mode_(PRIVACY_MODE_DISABLED) {
214 factory_.set_require_confirmation(false);
215 clock_->AdvanceTime(QuicTime::Delta::FromSeconds(1));
216 QuicStreamFactoryPeer::SetEnableConnectionRacing(
217 &factory_, GetParam().enable_connection_racing);
220 scoped_ptr<QuicHttpStream> CreateIfSessionExists(
221 const HostPortPair& host_port_pair,
222 const BoundNetLog& net_log) {
223 return QuicStreamFactoryPeer::CreateIfSessionExists(
224 &factory_, host_port_pair, false, net_log_);
227 int GetSourcePortForNewSession(const HostPortPair& destination) {
228 return GetSourcePortForNewSessionInner(destination, false);
231 int GetSourcePortForNewSessionAndGoAway(
232 const HostPortPair& destination) {
233 return GetSourcePortForNewSessionInner(destination, true);
236 int GetSourcePortForNewSessionInner(const HostPortPair& destination,
237 bool goaway_received) {
238 // Should only be called if there is no active session for this destination.
239 EXPECT_EQ(nullptr, CreateIfSessionExists(destination, net_log_).get());
240 size_t socket_count = socket_factory_.udp_client_sockets().size();
242 MockRead reads[] = {
243 MockRead(ASYNC, OK, 0) // EOF
245 DeterministicSocketData socket_data(reads, arraysize(reads), nullptr, 0);
246 socket_data.StopAfter(1);
247 socket_factory_.AddSocketDataProvider(&socket_data);
249 QuicStreamRequest request(&factory_);
250 EXPECT_EQ(ERR_IO_PENDING,
251 request.Request(destination,
252 is_https_,
253 privacy_mode_,
254 "GET",
255 net_log_,
256 callback_.callback()));
258 EXPECT_EQ(OK, callback_.WaitForResult());
259 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
260 EXPECT_TRUE(stream.get());
261 stream.reset();
263 QuicClientSession* session = QuicStreamFactoryPeer::GetActiveSession(
264 &factory_, destination, is_https_);
266 if (socket_count + 1 != socket_factory_.udp_client_sockets().size()) {
267 EXPECT_TRUE(false);
268 return 0;
271 IPEndPoint endpoint;
272 socket_factory_.
273 udp_client_sockets()[socket_count]->GetLocalAddress(&endpoint);
274 int port = endpoint.port();
275 if (goaway_received) {
276 QuicGoAwayFrame goaway(QUIC_NO_ERROR, 1, "");
277 session->OnGoAway(goaway);
280 factory_.OnSessionClosed(session);
281 EXPECT_EQ(nullptr, CreateIfSessionExists(destination, net_log_).get());
282 EXPECT_TRUE(socket_data.at_read_eof());
283 EXPECT_TRUE(socket_data.at_write_eof());
284 return port;
287 scoped_ptr<QuicEncryptedPacket> ConstructRstPacket() {
288 QuicStreamId stream_id = kClientDataStreamId1;
289 return maker_.MakeRstPacket(
290 1, true, stream_id,
291 AdjustErrorForVersion(QUIC_RST_ACKNOWLEDGEMENT, GetParam().version));
294 MockQuicServerInfoFactory quic_server_info_factory_;
295 MockHostResolver host_resolver_;
296 DeterministicMockClientSocketFactory socket_factory_;
297 MockCryptoClientStreamFactory crypto_client_stream_factory_;
298 MockRandom random_generator_;
299 MockClock* clock_; // Owned by factory_.
300 scoped_refptr<TestTaskRunner> runner_;
301 QuicTestPacketMaker maker_;
302 scoped_ptr<CertVerifier> cert_verifier_;
303 scoped_ptr<ChannelIDService> channel_id_service_;
304 TransportSecurityState transport_security_state_;
305 QuicStreamFactory factory_;
306 HostPortPair host_port_pair_;
307 bool is_https_;
308 PrivacyMode privacy_mode_;
309 BoundNetLog net_log_;
310 TestCompletionCallback callback_;
313 INSTANTIATE_TEST_CASE_P(Version,
314 QuicStreamFactoryTest,
315 ::testing::ValuesIn(GetTestParams()));
317 TEST_P(QuicStreamFactoryTest, CreateIfSessionExists) {
318 EXPECT_EQ(nullptr, CreateIfSessionExists(host_port_pair_, net_log_).get());
321 TEST_P(QuicStreamFactoryTest, Create) {
322 MockRead reads[] = {
323 MockRead(ASYNC, OK, 0) // EOF
325 DeterministicSocketData socket_data(reads, arraysize(reads), nullptr, 0);
326 socket_factory_.AddSocketDataProvider(&socket_data);
327 socket_data.StopAfter(1);
329 QuicStreamRequest request(&factory_);
330 EXPECT_EQ(ERR_IO_PENDING,
331 request.Request(host_port_pair_,
332 is_https_,
333 privacy_mode_,
334 "GET",
335 net_log_,
336 callback_.callback()));
338 EXPECT_EQ(OK, callback_.WaitForResult());
339 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
340 EXPECT_TRUE(stream.get());
342 // Will reset stream 3.
343 stream = CreateIfSessionExists(host_port_pair_, net_log_);
344 EXPECT_TRUE(stream.get());
346 // TODO(rtenneti): We should probably have a tests that HTTP and HTTPS result
347 // in streams on different sessions.
348 QuicStreamRequest request2(&factory_);
349 EXPECT_EQ(OK,
350 request2.Request(host_port_pair_,
351 is_https_,
352 privacy_mode_,
353 "GET",
354 net_log_,
355 callback_.callback()));
356 stream = request2.ReleaseStream(); // Will reset stream 5.
357 stream.reset(); // Will reset stream 7.
359 EXPECT_TRUE(socket_data.at_read_eof());
360 EXPECT_TRUE(socket_data.at_write_eof());
363 TEST_P(QuicStreamFactoryTest, CreateZeroRtt) {
364 MockRead reads[] = {
365 MockRead(ASYNC, OK, 0) // EOF
367 DeterministicSocketData socket_data(reads, arraysize(reads), nullptr, 0);
368 socket_factory_.AddSocketDataProvider(&socket_data);
369 socket_data.StopAfter(1);
371 crypto_client_stream_factory_.set_handshake_mode(
372 MockCryptoClientStream::ZERO_RTT);
373 host_resolver_.set_synchronous_mode(true);
374 host_resolver_.rules()->AddIPLiteralRule(host_port_pair_.host(),
375 "192.168.0.1", "");
377 QuicStreamRequest request(&factory_);
378 EXPECT_EQ(OK,
379 request.Request(host_port_pair_,
380 is_https_,
381 privacy_mode_,
382 "GET",
383 net_log_,
384 callback_.callback()));
386 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
387 EXPECT_TRUE(stream.get());
388 EXPECT_TRUE(socket_data.at_read_eof());
389 EXPECT_TRUE(socket_data.at_write_eof());
392 TEST_P(QuicStreamFactoryTest, CreateZeroRttPost) {
393 MockRead reads[] = {
394 MockRead(ASYNC, OK, 0) // EOF
396 DeterministicSocketData socket_data(reads, arraysize(reads), nullptr, 0);
397 socket_factory_.AddSocketDataProvider(&socket_data);
398 socket_data.StopAfter(1);
400 crypto_client_stream_factory_.set_handshake_mode(
401 MockCryptoClientStream::ZERO_RTT);
402 host_resolver_.set_synchronous_mode(true);
403 host_resolver_.rules()->AddIPLiteralRule(host_port_pair_.host(),
404 "192.168.0.1", "");
406 QuicStreamRequest request(&factory_);
407 // Posts require handshake confirmation, so this will return asynchronously.
408 EXPECT_EQ(ERR_IO_PENDING,
409 request.Request(host_port_pair_,
410 is_https_,
411 privacy_mode_,
412 "POST",
413 net_log_,
414 callback_.callback()));
416 // Confirm the handshake and verify that the stream is created.
417 crypto_client_stream_factory_.last_stream()->SendOnCryptoHandshakeEvent(
418 QuicSession::HANDSHAKE_CONFIRMED);
420 EXPECT_EQ(OK, callback_.WaitForResult());
421 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
422 EXPECT_TRUE(stream.get());
423 EXPECT_TRUE(socket_data.at_read_eof());
424 EXPECT_TRUE(socket_data.at_write_eof());
427 TEST_P(QuicStreamFactoryTest, CreateHttpVsHttps) {
428 MockRead reads[] = {
429 MockRead(ASYNC, OK, 0) // EOF
431 DeterministicSocketData socket_data1(reads, arraysize(reads), nullptr, 0);
432 DeterministicSocketData socket_data2(reads, arraysize(reads), nullptr, 0);
433 socket_factory_.AddSocketDataProvider(&socket_data1);
434 socket_factory_.AddSocketDataProvider(&socket_data2);
435 socket_data1.StopAfter(1);
436 socket_data2.StopAfter(1);
438 QuicStreamRequest request(&factory_);
439 EXPECT_EQ(ERR_IO_PENDING,
440 request.Request(host_port_pair_,
441 is_https_,
442 privacy_mode_,
443 "GET",
444 net_log_,
445 callback_.callback()));
447 EXPECT_EQ(OK, callback_.WaitForResult());
448 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
449 EXPECT_TRUE(stream.get());
451 QuicStreamRequest request2(&factory_);
452 EXPECT_EQ(ERR_IO_PENDING,
453 request2.Request(host_port_pair_,
454 !is_https_,
455 privacy_mode_,
456 "GET",
457 net_log_,
458 callback_.callback()));
459 EXPECT_EQ(OK, callback_.WaitForResult());
460 stream = request2.ReleaseStream();
461 EXPECT_TRUE(stream.get());
462 stream.reset();
464 EXPECT_NE(QuicStreamFactoryPeer::GetActiveSession(
465 &factory_, host_port_pair_, is_https_),
466 QuicStreamFactoryPeer::GetActiveSession(
467 &factory_, host_port_pair_, !is_https_));
469 EXPECT_TRUE(socket_data1.at_read_eof());
470 EXPECT_TRUE(socket_data1.at_write_eof());
471 EXPECT_TRUE(socket_data2.at_read_eof());
472 EXPECT_TRUE(socket_data2.at_write_eof());
475 TEST_P(QuicStreamFactoryTest, Pooling) {
476 MockRead reads[] = {
477 MockRead(ASYNC, OK, 0) // EOF
479 DeterministicSocketData socket_data(reads, arraysize(reads), nullptr, 0);
480 socket_factory_.AddSocketDataProvider(&socket_data);
481 socket_data.StopAfter(1);
483 HostPortPair server2("mail.google.com", kDefaultServerPort);
484 host_resolver_.set_synchronous_mode(true);
485 host_resolver_.rules()->AddIPLiteralRule(
486 kDefaultServerHostName, "192.168.0.1", "");
487 host_resolver_.rules()->AddIPLiteralRule(
488 "mail.google.com", "192.168.0.1", "");
490 QuicStreamRequest request(&factory_);
491 EXPECT_EQ(OK,
492 request.Request(host_port_pair_,
493 is_https_,
494 privacy_mode_,
495 "GET",
496 net_log_,
497 callback_.callback()));
498 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
499 EXPECT_TRUE(stream.get());
501 TestCompletionCallback callback;
502 QuicStreamRequest request2(&factory_);
503 EXPECT_EQ(OK,
504 request2.Request(server2,
505 is_https_,
506 privacy_mode_,
507 "GET",
508 net_log_,
509 callback.callback()));
510 scoped_ptr<QuicHttpStream> stream2 = request2.ReleaseStream();
511 EXPECT_TRUE(stream2.get());
513 EXPECT_EQ(
514 QuicStreamFactoryPeer::GetActiveSession(
515 &factory_, host_port_pair_, is_https_),
516 QuicStreamFactoryPeer::GetActiveSession(&factory_, server2, is_https_));
518 EXPECT_TRUE(socket_data.at_read_eof());
519 EXPECT_TRUE(socket_data.at_write_eof());
522 TEST_P(QuicStreamFactoryTest, NoPoolingIfDisabled) {
523 MockRead reads[] = {
524 MockRead(ASYNC, OK, 0) // EOF
526 DeterministicSocketData socket_data1(reads, arraysize(reads), nullptr, 0);
527 DeterministicSocketData socket_data2(reads, arraysize(reads), nullptr, 0);
528 socket_factory_.AddSocketDataProvider(&socket_data1);
529 socket_factory_.AddSocketDataProvider(&socket_data2);
530 socket_data1.StopAfter(1);
531 socket_data2.StopAfter(1);
533 HostPortPair server2("mail.google.com", kDefaultServerPort);
534 host_resolver_.set_synchronous_mode(true);
535 host_resolver_.rules()->AddIPLiteralRule(
536 kDefaultServerHostName, "192.168.0.1", "");
537 host_resolver_.rules()->AddIPLiteralRule(
538 "mail.google.com", "192.168.0.1", "");
540 // Disable connection pooling.
541 QuicStreamFactoryPeer::DisableConnectionPooling(&factory_);
543 QuicStreamRequest request(&factory_);
544 EXPECT_EQ(OK,
545 request.Request(host_port_pair_,
546 is_https_,
547 privacy_mode_,
548 "GET",
549 net_log_,
550 callback_.callback()));
551 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
552 EXPECT_TRUE(stream.get());
554 TestCompletionCallback callback;
555 QuicStreamRequest request2(&factory_);
556 EXPECT_EQ(OK,
557 request2.Request(server2,
558 is_https_,
559 privacy_mode_,
560 "GET",
561 net_log_,
562 callback.callback()));
563 scoped_ptr<QuicHttpStream> stream2 = request2.ReleaseStream();
564 EXPECT_TRUE(stream2.get());
566 EXPECT_NE(
567 QuicStreamFactoryPeer::GetActiveSession(
568 &factory_, host_port_pair_, is_https_),
569 QuicStreamFactoryPeer::GetActiveSession(&factory_, server2, is_https_));
571 EXPECT_TRUE(socket_data1.at_read_eof());
572 EXPECT_TRUE(socket_data1.at_write_eof());
573 EXPECT_TRUE(socket_data2.at_read_eof());
574 EXPECT_TRUE(socket_data2.at_write_eof());
577 TEST_P(QuicStreamFactoryTest, NoPoolingAfterGoAway) {
578 MockRead reads[] = {
579 MockRead(ASYNC, OK, 0) // EOF
581 DeterministicSocketData socket_data1(reads, arraysize(reads), nullptr, 0);
582 DeterministicSocketData socket_data2(reads, arraysize(reads), nullptr, 0);
583 socket_factory_.AddSocketDataProvider(&socket_data1);
584 socket_factory_.AddSocketDataProvider(&socket_data2);
585 socket_data1.StopAfter(1);
586 socket_data2.StopAfter(1);
588 HostPortPair server2("mail.google.com", kDefaultServerPort);
589 host_resolver_.set_synchronous_mode(true);
590 host_resolver_.rules()->AddIPLiteralRule(
591 kDefaultServerHostName, "192.168.0.1", "");
592 host_resolver_.rules()->AddIPLiteralRule(
593 "mail.google.com", "192.168.0.1", "");
595 QuicStreamRequest request(&factory_);
596 EXPECT_EQ(OK,
597 request.Request(host_port_pair_,
598 is_https_,
599 privacy_mode_,
600 "GET",
601 net_log_,
602 callback_.callback()));
603 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
604 EXPECT_TRUE(stream.get());
606 TestCompletionCallback callback;
607 QuicStreamRequest request2(&factory_);
608 EXPECT_EQ(OK,
609 request2.Request(server2,
610 is_https_,
611 privacy_mode_,
612 "GET",
613 net_log_,
614 callback.callback()));
615 scoped_ptr<QuicHttpStream> stream2 = request2.ReleaseStream();
616 EXPECT_TRUE(stream2.get());
618 factory_.OnSessionGoingAway(QuicStreamFactoryPeer::GetActiveSession(
619 &factory_, host_port_pair_, is_https_));
620 EXPECT_FALSE(QuicStreamFactoryPeer::HasActiveSession(
621 &factory_, host_port_pair_, is_https_));
622 EXPECT_FALSE(QuicStreamFactoryPeer::HasActiveSession(
623 &factory_, server2, is_https_));
625 TestCompletionCallback callback3;
626 QuicStreamRequest request3(&factory_);
627 EXPECT_EQ(OK,
628 request3.Request(server2,
629 is_https_,
630 privacy_mode_,
631 "GET",
632 net_log_,
633 callback3.callback()));
634 scoped_ptr<QuicHttpStream> stream3 = request3.ReleaseStream();
635 EXPECT_TRUE(stream3.get());
637 EXPECT_TRUE(QuicStreamFactoryPeer::HasActiveSession(
638 &factory_, server2, is_https_));
640 EXPECT_TRUE(socket_data1.at_read_eof());
641 EXPECT_TRUE(socket_data1.at_write_eof());
642 EXPECT_TRUE(socket_data2.at_read_eof());
643 EXPECT_TRUE(socket_data2.at_write_eof());
646 TEST_P(QuicStreamFactoryTest, HttpsPooling) {
647 MockRead reads[] = {
648 MockRead(ASYNC, OK, 0) // EOF
650 DeterministicSocketData socket_data(reads, arraysize(reads), nullptr, 0);
651 socket_factory_.AddSocketDataProvider(&socket_data);
652 socket_data.StopAfter(1);
654 HostPortPair server1("www.example.org", 443);
655 HostPortPair server2("mail.example.org", 443);
657 // Load a cert that is valid for:
658 // www.example.org (server1)
659 // mail.example.org (server2)
660 // www.example.com
661 base::FilePath certs_dir = GetTestCertsDirectory();
662 scoped_refptr<X509Certificate> test_cert(
663 ImportCertFromFile(certs_dir, "spdy_pooling.pem"));
664 ASSERT_NE(static_cast<X509Certificate*>(nullptr), test_cert.get());
665 ProofVerifyDetailsChromium verify_details;
666 verify_details.cert_verify_result.verified_cert = test_cert;
667 verify_details.cert_verify_result.is_issued_by_known_root = true;
668 crypto_client_stream_factory_.set_proof_verify_details(&verify_details);
670 host_resolver_.set_synchronous_mode(true);
671 host_resolver_.rules()->AddIPLiteralRule(server1.host(), "192.168.0.1", "");
672 host_resolver_.rules()->AddIPLiteralRule(server2.host(), "192.168.0.1", "");
674 QuicStreamRequest request(&factory_);
675 is_https_ = true;
676 EXPECT_EQ(OK,
677 request.Request(server1,
678 is_https_,
679 privacy_mode_,
680 "GET",
681 net_log_,
682 callback_.callback()));
683 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
684 EXPECT_TRUE(stream.get());
686 TestCompletionCallback callback;
687 QuicStreamRequest request2(&factory_);
688 EXPECT_EQ(OK,
689 request2.Request(server2,
690 is_https_,
691 privacy_mode_,
692 "GET",
693 net_log_,
694 callback_.callback()));
695 scoped_ptr<QuicHttpStream> stream2 = request2.ReleaseStream();
696 EXPECT_TRUE(stream2.get());
698 EXPECT_EQ(QuicStreamFactoryPeer::GetActiveSession(
699 &factory_, server1, is_https_),
700 QuicStreamFactoryPeer::GetActiveSession(
701 &factory_, server2, is_https_));
703 EXPECT_TRUE(socket_data.at_read_eof());
704 EXPECT_TRUE(socket_data.at_write_eof());
707 TEST_P(QuicStreamFactoryTest, NoHttpsPoolingIfDisabled) {
708 MockRead reads[] = {
709 MockRead(ASYNC, OK, 0) // EOF
711 DeterministicSocketData socket_data1(reads, arraysize(reads), nullptr, 0);
712 DeterministicSocketData socket_data2(reads, arraysize(reads), nullptr, 0);
713 socket_factory_.AddSocketDataProvider(&socket_data1);
714 socket_factory_.AddSocketDataProvider(&socket_data2);
715 socket_data1.StopAfter(1);
716 socket_data2.StopAfter(1);
718 HostPortPair server1("www.example.org", 443);
719 HostPortPair server2("mail.example.org", 443);
721 // Load a cert that is valid for:
722 // www.example.org (server1)
723 // mail.example.org (server2)
724 // www.example.com
725 base::FilePath certs_dir = GetTestCertsDirectory();
726 scoped_refptr<X509Certificate> test_cert(
727 ImportCertFromFile(certs_dir, "spdy_pooling.pem"));
728 ASSERT_NE(static_cast<X509Certificate*>(nullptr), test_cert.get());
729 ProofVerifyDetailsChromium verify_details;
730 verify_details.cert_verify_result.verified_cert = test_cert;
731 verify_details.cert_verify_result.is_issued_by_known_root = true;
732 crypto_client_stream_factory_.set_proof_verify_details(&verify_details);
734 host_resolver_.set_synchronous_mode(true);
735 host_resolver_.rules()->AddIPLiteralRule(server1.host(), "192.168.0.1", "");
736 host_resolver_.rules()->AddIPLiteralRule(server2.host(), "192.168.0.1", "");
738 // Disable connection pooling.
739 QuicStreamFactoryPeer::DisableConnectionPooling(&factory_);
741 QuicStreamRequest request(&factory_);
742 is_https_ = true;
743 EXPECT_EQ(OK,
744 request.Request(server1,
745 is_https_,
746 privacy_mode_,
747 "GET",
748 net_log_,
749 callback_.callback()));
750 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
751 EXPECT_TRUE(stream.get());
753 TestCompletionCallback callback;
754 QuicStreamRequest request2(&factory_);
755 EXPECT_EQ(OK,
756 request2.Request(server2,
757 is_https_,
758 privacy_mode_,
759 "GET",
760 net_log_,
761 callback_.callback()));
762 scoped_ptr<QuicHttpStream> stream2 = request2.ReleaseStream();
763 EXPECT_TRUE(stream2.get());
765 EXPECT_NE(QuicStreamFactoryPeer::GetActiveSession(
766 &factory_, server1, is_https_),
767 QuicStreamFactoryPeer::GetActiveSession(
768 &factory_, server2, is_https_));
770 EXPECT_TRUE(socket_data1.at_read_eof());
771 EXPECT_TRUE(socket_data1.at_write_eof());
772 EXPECT_TRUE(socket_data2.at_read_eof());
773 EXPECT_TRUE(socket_data2.at_write_eof());
776 TEST_P(QuicStreamFactoryTest, NoHttpsPoolingWithCertMismatch) {
777 MockRead reads[] = {
778 MockRead(ASYNC, OK, 0) // EOF
780 DeterministicSocketData socket_data1(reads, arraysize(reads), nullptr, 0);
781 DeterministicSocketData socket_data2(reads, arraysize(reads), nullptr, 0);
782 socket_factory_.AddSocketDataProvider(&socket_data1);
783 socket_factory_.AddSocketDataProvider(&socket_data2);
784 socket_data1.StopAfter(1);
785 socket_data2.StopAfter(1);
787 HostPortPair server1("www.example.org", 443);
788 HostPortPair server2("mail.google.com", 443);
790 // Load a cert that is valid for:
791 // www.example.org (server1)
792 // mail.example.org
793 // www.example.com
794 // But is not valid for mail.google.com (server2).
795 base::FilePath certs_dir = GetTestCertsDirectory();
796 scoped_refptr<X509Certificate> test_cert(
797 ImportCertFromFile(certs_dir, "spdy_pooling.pem"));
798 ASSERT_NE(static_cast<X509Certificate*>(nullptr), test_cert.get());
799 ProofVerifyDetailsChromium verify_details;
800 verify_details.cert_verify_result.verified_cert = test_cert;
801 crypto_client_stream_factory_.set_proof_verify_details(&verify_details);
804 host_resolver_.set_synchronous_mode(true);
805 host_resolver_.rules()->AddIPLiteralRule(server1.host(), "192.168.0.1", "");
806 host_resolver_.rules()->AddIPLiteralRule(server2.host(), "192.168.0.1", "");
808 QuicStreamRequest request(&factory_);
809 is_https_ = true;
810 EXPECT_EQ(OK,
811 request.Request(server1,
812 is_https_,
813 privacy_mode_,
814 "GET",
815 net_log_,
816 callback_.callback()));
817 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
818 EXPECT_TRUE(stream.get());
820 TestCompletionCallback callback;
821 QuicStreamRequest request2(&factory_);
822 EXPECT_EQ(OK,
823 request2.Request(server2,
824 is_https_,
825 privacy_mode_,
826 "GET",
827 net_log_,
828 callback_.callback()));
829 scoped_ptr<QuicHttpStream> stream2 = request2.ReleaseStream();
830 EXPECT_TRUE(stream2.get());
832 EXPECT_NE(QuicStreamFactoryPeer::GetActiveSession(
833 &factory_, server1, is_https_),
834 QuicStreamFactoryPeer::GetActiveSession(
835 &factory_, server2, is_https_));
837 EXPECT_TRUE(socket_data1.at_read_eof());
838 EXPECT_TRUE(socket_data1.at_write_eof());
839 EXPECT_TRUE(socket_data2.at_read_eof());
840 EXPECT_TRUE(socket_data2.at_write_eof());
843 TEST_P(QuicStreamFactoryTest, HttpsPoolingWithMatchingPins) {
844 MockRead reads[] = {
845 MockRead(ASYNC, OK, 0) // EOF
847 DeterministicSocketData socket_data(reads, arraysize(reads), nullptr, 0);
848 socket_factory_.AddSocketDataProvider(&socket_data);
849 socket_data.StopAfter(1);
851 HostPortPair server1("www.example.org", 443);
852 HostPortPair server2("mail.example.org", 443);
853 uint8 primary_pin = 1;
854 uint8 backup_pin = 2;
855 test::AddPin(&transport_security_state_, "mail.example.org", primary_pin,
856 backup_pin);
858 // Load a cert that is valid for:
859 // www.example.org (server1)
860 // mail.example.org (server2)
861 base::FilePath certs_dir = GetTestCertsDirectory();
862 scoped_refptr<X509Certificate> test_cert(
863 ImportCertFromFile(certs_dir, "spdy_pooling.pem"));
864 ASSERT_NE(static_cast<X509Certificate*>(nullptr), test_cert.get());
865 ProofVerifyDetailsChromium verify_details;
866 verify_details.cert_verify_result.verified_cert = test_cert;
867 verify_details.cert_verify_result.is_issued_by_known_root = true;
868 verify_details.cert_verify_result.public_key_hashes.push_back(
869 test::GetTestHashValue(primary_pin));
870 crypto_client_stream_factory_.set_proof_verify_details(&verify_details);
873 host_resolver_.set_synchronous_mode(true);
874 host_resolver_.rules()->AddIPLiteralRule(server1.host(), "192.168.0.1", "");
875 host_resolver_.rules()->AddIPLiteralRule(server2.host(), "192.168.0.1", "");
877 QuicStreamRequest request(&factory_);
878 is_https_ = true;
879 EXPECT_EQ(OK,
880 request.Request(server1,
881 is_https_,
882 privacy_mode_,
883 "GET",
884 net_log_,
885 callback_.callback()));
886 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
887 EXPECT_TRUE(stream.get());
889 TestCompletionCallback callback;
890 QuicStreamRequest request2(&factory_);
891 EXPECT_EQ(OK,
892 request2.Request(server2,
893 is_https_,
894 privacy_mode_,
895 "GET",
896 net_log_,
897 callback_.callback()));
898 scoped_ptr<QuicHttpStream> stream2 = request2.ReleaseStream();
899 EXPECT_TRUE(stream2.get());
901 EXPECT_EQ(QuicStreamFactoryPeer::GetActiveSession(
902 &factory_, server1, is_https_),
903 QuicStreamFactoryPeer::GetActiveSession(
904 &factory_, server2, is_https_));
906 EXPECT_TRUE(socket_data.at_read_eof());
907 EXPECT_TRUE(socket_data.at_write_eof());
910 TEST_P(QuicStreamFactoryTest, NoHttpsPoolingWithMatchingPinsIfDisabled) {
911 MockRead reads[] = {
912 MockRead(ASYNC, OK, 0) // EOF
914 DeterministicSocketData socket_data1(reads, arraysize(reads), nullptr, 0);
915 DeterministicSocketData socket_data2(reads, arraysize(reads), nullptr, 0);
916 socket_factory_.AddSocketDataProvider(&socket_data1);
917 socket_factory_.AddSocketDataProvider(&socket_data2);
918 socket_data1.StopAfter(1);
919 socket_data2.StopAfter(1);
921 HostPortPair server1("www.example.org", 443);
922 HostPortPair server2("mail.example.org", 443);
923 uint8 primary_pin = 1;
924 uint8 backup_pin = 2;
925 test::AddPin(&transport_security_state_, "mail.example.org", primary_pin,
926 backup_pin);
928 // Load a cert that is valid for:
929 // www.example.org (server1)
930 // mail.example.org (server2)
931 base::FilePath certs_dir = GetTestCertsDirectory();
932 scoped_refptr<X509Certificate> test_cert(
933 ImportCertFromFile(certs_dir, "spdy_pooling.pem"));
934 ASSERT_NE(static_cast<X509Certificate*>(nullptr), test_cert.get());
935 ProofVerifyDetailsChromium verify_details;
936 verify_details.cert_verify_result.verified_cert = test_cert;
937 verify_details.cert_verify_result.is_issued_by_known_root = true;
938 verify_details.cert_verify_result.public_key_hashes.push_back(
939 test::GetTestHashValue(primary_pin));
940 crypto_client_stream_factory_.set_proof_verify_details(&verify_details);
943 host_resolver_.set_synchronous_mode(true);
944 host_resolver_.rules()->AddIPLiteralRule(server1.host(), "192.168.0.1", "");
945 host_resolver_.rules()->AddIPLiteralRule(server2.host(), "192.168.0.1", "");
947 // Disable connection pooling.
948 QuicStreamFactoryPeer::DisableConnectionPooling(&factory_);
950 QuicStreamRequest request(&factory_);
951 is_https_ = true;
952 EXPECT_EQ(OK,
953 request.Request(server1,
954 is_https_,
955 privacy_mode_,
956 "GET",
957 net_log_,
958 callback_.callback()));
959 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
960 EXPECT_TRUE(stream.get());
962 TestCompletionCallback callback;
963 QuicStreamRequest request2(&factory_);
964 EXPECT_EQ(OK,
965 request2.Request(server2,
966 is_https_,
967 privacy_mode_,
968 "GET",
969 net_log_,
970 callback_.callback()));
971 scoped_ptr<QuicHttpStream> stream2 = request2.ReleaseStream();
972 EXPECT_TRUE(stream2.get());
974 EXPECT_NE(QuicStreamFactoryPeer::GetActiveSession(
975 &factory_, server1, is_https_),
976 QuicStreamFactoryPeer::GetActiveSession(
977 &factory_, server2, is_https_));
979 EXPECT_TRUE(socket_data1.at_read_eof());
980 EXPECT_TRUE(socket_data1.at_write_eof());
981 EXPECT_TRUE(socket_data2.at_read_eof());
982 EXPECT_TRUE(socket_data2.at_write_eof());
985 TEST_P(QuicStreamFactoryTest, NoHttpsPoolingWithDifferentPins) {
986 MockRead reads[] = {
987 MockRead(ASYNC, OK, 0) // EOF
989 DeterministicSocketData socket_data1(reads, arraysize(reads), nullptr, 0);
990 DeterministicSocketData socket_data2(reads, arraysize(reads), nullptr, 0);
991 socket_factory_.AddSocketDataProvider(&socket_data1);
992 socket_factory_.AddSocketDataProvider(&socket_data2);
993 socket_data1.StopAfter(1);
994 socket_data2.StopAfter(1);
996 HostPortPair server1("www.example.org", 443);
997 HostPortPair server2("mail.example.org", 443);
998 uint8 primary_pin = 1;
999 uint8 backup_pin = 2;
1000 uint8 bad_pin = 3;
1001 test::AddPin(&transport_security_state_, "mail.example.org", primary_pin,
1002 backup_pin);
1004 // Load a cert that is valid for:
1005 // www.example.org (server1)
1006 // mail.example.org (server2)
1007 base::FilePath certs_dir = GetTestCertsDirectory();
1008 scoped_refptr<X509Certificate> test_cert(
1009 ImportCertFromFile(certs_dir, "spdy_pooling.pem"));
1010 ASSERT_NE(static_cast<X509Certificate*>(nullptr), test_cert.get());
1011 ProofVerifyDetailsChromium verify_details;
1012 verify_details.cert_verify_result.verified_cert = test_cert;
1013 verify_details.cert_verify_result.is_issued_by_known_root = true;
1014 verify_details.cert_verify_result.public_key_hashes.push_back(
1015 test::GetTestHashValue(bad_pin));
1016 crypto_client_stream_factory_.set_proof_verify_details(&verify_details);
1019 host_resolver_.set_synchronous_mode(true);
1020 host_resolver_.rules()->AddIPLiteralRule(server1.host(), "192.168.0.1", "");
1021 host_resolver_.rules()->AddIPLiteralRule(server2.host(), "192.168.0.1", "");
1023 QuicStreamRequest request(&factory_);
1024 is_https_ = true;
1025 EXPECT_EQ(OK,
1026 request.Request(server1,
1027 is_https_,
1028 privacy_mode_,
1029 "GET",
1030 net_log_,
1031 callback_.callback()));
1032 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
1033 EXPECT_TRUE(stream.get());
1035 TestCompletionCallback callback;
1036 QuicStreamRequest request2(&factory_);
1037 EXPECT_EQ(OK,
1038 request2.Request(server2,
1039 is_https_,
1040 privacy_mode_,
1041 "GET",
1042 net_log_,
1043 callback_.callback()));
1044 scoped_ptr<QuicHttpStream> stream2 = request2.ReleaseStream();
1045 EXPECT_TRUE(stream2.get());
1047 EXPECT_NE(QuicStreamFactoryPeer::GetActiveSession(
1048 &factory_, server1, is_https_),
1049 QuicStreamFactoryPeer::GetActiveSession(
1050 &factory_, server2, is_https_));
1052 EXPECT_TRUE(socket_data1.at_read_eof());
1053 EXPECT_TRUE(socket_data1.at_write_eof());
1054 EXPECT_TRUE(socket_data2.at_read_eof());
1055 EXPECT_TRUE(socket_data2.at_write_eof());
1058 TEST_P(QuicStreamFactoryTest, Goaway) {
1059 MockRead reads[] = {
1060 MockRead(ASYNC, OK, 0) // EOF
1062 DeterministicSocketData socket_data(reads, arraysize(reads), nullptr, 0);
1063 socket_data.StopAfter(1);
1064 socket_factory_.AddSocketDataProvider(&socket_data);
1065 DeterministicSocketData socket_data2(reads, arraysize(reads), nullptr, 0);
1066 socket_data2.StopAfter(1);
1067 socket_factory_.AddSocketDataProvider(&socket_data2);
1069 QuicStreamRequest request(&factory_);
1070 EXPECT_EQ(ERR_IO_PENDING,
1071 request.Request(host_port_pair_,
1072 is_https_,
1073 privacy_mode_,
1074 "GET",
1075 net_log_,
1076 callback_.callback()));
1078 EXPECT_EQ(OK, callback_.WaitForResult());
1079 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
1080 EXPECT_TRUE(stream.get());
1082 // Mark the session as going away. Ensure that while it is still alive
1083 // that it is no longer active.
1084 QuicClientSession* session = QuicStreamFactoryPeer::GetActiveSession(
1085 &factory_, host_port_pair_, is_https_);
1086 factory_.OnSessionGoingAway(session);
1087 EXPECT_EQ(true, QuicStreamFactoryPeer::IsLiveSession(&factory_, session));
1088 EXPECT_FALSE(QuicStreamFactoryPeer::HasActiveSession(
1089 &factory_, host_port_pair_, is_https_));
1090 EXPECT_EQ(nullptr, CreateIfSessionExists(host_port_pair_, net_log_).get());
1092 // Create a new request for the same destination and verify that a
1093 // new session is created.
1094 QuicStreamRequest request2(&factory_);
1095 EXPECT_EQ(ERR_IO_PENDING,
1096 request2.Request(host_port_pair_,
1097 is_https_,
1098 privacy_mode_,
1099 "GET",
1100 net_log_,
1101 callback_.callback()));
1102 EXPECT_EQ(OK, callback_.WaitForResult());
1103 scoped_ptr<QuicHttpStream> stream2 = request2.ReleaseStream();
1104 EXPECT_TRUE(stream2.get());
1106 EXPECT_TRUE(QuicStreamFactoryPeer::HasActiveSession(&factory_,
1107 host_port_pair_,
1108 is_https_));
1109 EXPECT_NE(session,
1110 QuicStreamFactoryPeer::GetActiveSession(
1111 &factory_, host_port_pair_, is_https_));
1112 EXPECT_EQ(true, QuicStreamFactoryPeer::IsLiveSession(&factory_, session));
1114 stream2.reset();
1115 stream.reset();
1117 EXPECT_TRUE(socket_data.at_read_eof());
1118 EXPECT_TRUE(socket_data.at_write_eof());
1119 EXPECT_TRUE(socket_data2.at_read_eof());
1120 EXPECT_TRUE(socket_data2.at_write_eof());
1123 TEST_P(QuicStreamFactoryTest, MaxOpenStream) {
1124 MockRead reads[] = {
1125 MockRead(ASYNC, OK, 0) // EOF
1127 QuicStreamId stream_id = kClientDataStreamId1;
1128 scoped_ptr<QuicEncryptedPacket> rst(
1129 maker_.MakeRstPacket(1, true, stream_id, QUIC_STREAM_CANCELLED));
1130 MockWrite writes[] = {
1131 MockWrite(ASYNC, rst->data(), rst->length(), 1),
1133 DeterministicSocketData socket_data(reads, arraysize(reads),
1134 writes, arraysize(writes));
1135 socket_factory_.AddSocketDataProvider(&socket_data);
1136 socket_data.StopAfter(1);
1138 HttpRequestInfo request_info;
1139 std::vector<QuicHttpStream*> streams;
1140 // The MockCryptoClientStream sets max_open_streams to be
1141 // kDefaultMaxStreamsPerConnection / 2.
1142 for (size_t i = 0; i < kDefaultMaxStreamsPerConnection / 2; i++) {
1143 QuicStreamRequest request(&factory_);
1144 int rv = request.Request(host_port_pair_,
1145 is_https_,
1146 privacy_mode_,
1147 "GET",
1148 net_log_,
1149 callback_.callback());
1150 if (i == 0) {
1151 EXPECT_EQ(ERR_IO_PENDING, rv);
1152 EXPECT_EQ(OK, callback_.WaitForResult());
1153 } else {
1154 EXPECT_EQ(OK, rv);
1156 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
1157 EXPECT_TRUE(stream);
1158 EXPECT_EQ(OK, stream->InitializeStream(
1159 &request_info, DEFAULT_PRIORITY, net_log_, CompletionCallback()));
1160 streams.push_back(stream.release());
1163 QuicStreamRequest request(&factory_);
1164 EXPECT_EQ(OK,
1165 request.Request(host_port_pair_,
1166 is_https_,
1167 privacy_mode_,
1168 "GET",
1169 net_log_,
1170 CompletionCallback()));
1171 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
1172 EXPECT_TRUE(stream);
1173 EXPECT_EQ(ERR_IO_PENDING, stream->InitializeStream(
1174 &request_info, DEFAULT_PRIORITY, net_log_, callback_.callback()));
1176 // Close the first stream.
1177 streams.front()->Close(false);
1179 ASSERT_TRUE(callback_.have_result());
1181 EXPECT_EQ(OK, callback_.WaitForResult());
1183 EXPECT_TRUE(socket_data.at_read_eof());
1184 EXPECT_TRUE(socket_data.at_write_eof());
1185 STLDeleteElements(&streams);
1188 TEST_P(QuicStreamFactoryTest, ResolutionErrorInCreate) {
1189 DeterministicSocketData socket_data(nullptr, 0, nullptr, 0);
1190 socket_factory_.AddSocketDataProvider(&socket_data);
1192 host_resolver_.rules()->AddSimulatedFailure(kDefaultServerHostName);
1194 QuicStreamRequest request(&factory_);
1195 EXPECT_EQ(ERR_IO_PENDING,
1196 request.Request(host_port_pair_,
1197 is_https_,
1198 privacy_mode_,
1199 "GET",
1200 net_log_,
1201 callback_.callback()));
1203 EXPECT_EQ(ERR_NAME_NOT_RESOLVED, callback_.WaitForResult());
1205 EXPECT_TRUE(socket_data.at_read_eof());
1206 EXPECT_TRUE(socket_data.at_write_eof());
1209 TEST_P(QuicStreamFactoryTest, ConnectErrorInCreate) {
1210 MockConnect connect(SYNCHRONOUS, ERR_ADDRESS_IN_USE);
1211 DeterministicSocketData socket_data(nullptr, 0, nullptr, 0);
1212 socket_data.set_connect_data(connect);
1213 socket_factory_.AddSocketDataProvider(&socket_data);
1214 socket_data.StopAfter(1);
1216 QuicStreamRequest request(&factory_);
1217 EXPECT_EQ(ERR_IO_PENDING,
1218 request.Request(host_port_pair_,
1219 is_https_,
1220 privacy_mode_,
1221 "GET",
1222 net_log_,
1223 callback_.callback()));
1225 EXPECT_EQ(ERR_ADDRESS_IN_USE, callback_.WaitForResult());
1227 EXPECT_TRUE(socket_data.at_read_eof());
1228 EXPECT_TRUE(socket_data.at_write_eof());
1231 TEST_P(QuicStreamFactoryTest, CancelCreate) {
1232 MockRead reads[] = {
1233 MockRead(ASYNC, OK, 0) // EOF
1235 DeterministicSocketData socket_data(reads, arraysize(reads), nullptr, 0);
1236 socket_factory_.AddSocketDataProvider(&socket_data);
1238 QuicStreamRequest request(&factory_);
1239 EXPECT_EQ(ERR_IO_PENDING,
1240 request.Request(host_port_pair_,
1241 is_https_,
1242 privacy_mode_,
1243 "GET",
1244 net_log_,
1245 callback_.callback()));
1248 socket_data.StopAfter(1);
1249 base::RunLoop run_loop;
1250 run_loop.RunUntilIdle();
1252 scoped_ptr<QuicHttpStream> stream(
1253 CreateIfSessionExists(host_port_pair_, net_log_));
1254 EXPECT_TRUE(stream.get());
1255 stream.reset();
1257 EXPECT_TRUE(socket_data.at_read_eof());
1258 EXPECT_TRUE(socket_data.at_write_eof());
1261 TEST_P(QuicStreamFactoryTest, CreateConsistentEphemeralPort) {
1262 // Sequentially connect to the default host, then another host, and then the
1263 // default host. Verify that the default host gets a consistent ephemeral
1264 // port, that is different from the other host's connection.
1266 std::string other_server_name = "other.google.com";
1267 EXPECT_NE(kDefaultServerHostName, other_server_name);
1268 HostPortPair host_port_pair2(other_server_name, kDefaultServerPort);
1270 int original_port = GetSourcePortForNewSession(host_port_pair_);
1271 EXPECT_NE(original_port, GetSourcePortForNewSession(host_port_pair2));
1272 EXPECT_EQ(original_port, GetSourcePortForNewSession(host_port_pair_));
1275 TEST_P(QuicStreamFactoryTest, GoAwayDisablesConsistentEphemeralPort) {
1276 // Get a session to the host using the port suggester.
1277 int original_port =
1278 GetSourcePortForNewSessionAndGoAway(host_port_pair_);
1279 // Verify that the port is different after the goaway.
1280 EXPECT_NE(original_port, GetSourcePortForNewSession(host_port_pair_));
1281 // Since the previous session did not goaway we should see the original port.
1282 EXPECT_EQ(original_port, GetSourcePortForNewSession(host_port_pair_));
1285 TEST_P(QuicStreamFactoryTest, CloseAllSessions) {
1286 MockRead reads[] = {
1287 MockRead(ASYNC, 0, 0) // EOF
1289 scoped_ptr<QuicEncryptedPacket> rst(ConstructRstPacket());
1290 std::vector<MockWrite> writes;
1291 writes.push_back(MockWrite(ASYNC, rst->data(), rst->length(), 1));
1292 DeterministicSocketData socket_data(reads, arraysize(reads),
1293 writes.empty() ? nullptr : &writes[0],
1294 writes.size());
1295 socket_factory_.AddSocketDataProvider(&socket_data);
1296 socket_data.StopAfter(1);
1298 MockRead reads2[] = {
1299 MockRead(ASYNC, 0, 0) // EOF
1301 DeterministicSocketData socket_data2(reads2, arraysize(reads2), nullptr, 0);
1302 socket_factory_.AddSocketDataProvider(&socket_data2);
1303 socket_data2.StopAfter(1);
1305 QuicStreamRequest request(&factory_);
1306 EXPECT_EQ(ERR_IO_PENDING,
1307 request.Request(host_port_pair_,
1308 is_https_,
1309 privacy_mode_,
1310 "GET",
1311 net_log_,
1312 callback_.callback()));
1314 EXPECT_EQ(OK, callback_.WaitForResult());
1315 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
1316 HttpRequestInfo request_info;
1317 EXPECT_EQ(OK, stream->InitializeStream(&request_info,
1318 DEFAULT_PRIORITY,
1319 net_log_, CompletionCallback()));
1321 // Close the session and verify that stream saw the error.
1322 factory_.CloseAllSessions(ERR_INTERNET_DISCONNECTED);
1323 EXPECT_EQ(ERR_INTERNET_DISCONNECTED,
1324 stream->ReadResponseHeaders(callback_.callback()));
1326 // Now attempting to request a stream to the same origin should create
1327 // a new session.
1329 QuicStreamRequest request2(&factory_);
1330 EXPECT_EQ(ERR_IO_PENDING,
1331 request2.Request(host_port_pair_,
1332 is_https_,
1333 privacy_mode_,
1334 "GET",
1335 net_log_,
1336 callback_.callback()));
1338 EXPECT_EQ(OK, callback_.WaitForResult());
1339 stream = request2.ReleaseStream();
1340 stream.reset(); // Will reset stream 3.
1342 EXPECT_TRUE(socket_data.at_read_eof());
1343 EXPECT_TRUE(socket_data.at_write_eof());
1344 EXPECT_TRUE(socket_data2.at_read_eof());
1345 EXPECT_TRUE(socket_data2.at_write_eof());
1348 TEST_P(QuicStreamFactoryTest, OnIPAddressChanged) {
1349 MockRead reads[] = {
1350 MockRead(ASYNC, 0, 0) // EOF
1352 scoped_ptr<QuicEncryptedPacket> rst(ConstructRstPacket());
1353 std::vector<MockWrite> writes;
1354 writes.push_back(MockWrite(ASYNC, rst->data(), rst->length(), 1));
1355 DeterministicSocketData socket_data(reads, arraysize(reads),
1356 writes.empty() ? nullptr : &writes[0],
1357 writes.size());
1358 socket_factory_.AddSocketDataProvider(&socket_data);
1359 socket_data.StopAfter(1);
1361 MockRead reads2[] = {
1362 MockRead(ASYNC, 0, 0) // EOF
1364 DeterministicSocketData socket_data2(reads2, arraysize(reads2), nullptr, 0);
1365 socket_factory_.AddSocketDataProvider(&socket_data2);
1366 socket_data2.StopAfter(1);
1368 QuicStreamRequest request(&factory_);
1369 EXPECT_EQ(ERR_IO_PENDING,
1370 request.Request(host_port_pair_,
1371 is_https_,
1372 privacy_mode_,
1373 "GET",
1374 net_log_,
1375 callback_.callback()));
1377 EXPECT_EQ(OK, callback_.WaitForResult());
1378 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
1379 HttpRequestInfo request_info;
1380 EXPECT_EQ(OK, stream->InitializeStream(&request_info,
1381 DEFAULT_PRIORITY,
1382 net_log_, CompletionCallback()));
1384 // Change the IP address and verify that stream saw the error.
1385 factory_.OnIPAddressChanged();
1386 EXPECT_EQ(ERR_NETWORK_CHANGED,
1387 stream->ReadResponseHeaders(callback_.callback()));
1388 EXPECT_TRUE(factory_.require_confirmation());
1390 // Now attempting to request a stream to the same origin should create
1391 // a new session.
1393 QuicStreamRequest request2(&factory_);
1394 EXPECT_EQ(ERR_IO_PENDING,
1395 request2.Request(host_port_pair_,
1396 is_https_,
1397 privacy_mode_,
1398 "GET",
1399 net_log_,
1400 callback_.callback()));
1402 EXPECT_EQ(OK, callback_.WaitForResult());
1403 stream = request2.ReleaseStream();
1404 stream.reset(); // Will reset stream 3.
1406 EXPECT_TRUE(socket_data.at_read_eof());
1407 EXPECT_TRUE(socket_data.at_write_eof());
1408 EXPECT_TRUE(socket_data2.at_read_eof());
1409 EXPECT_TRUE(socket_data2.at_write_eof());
1412 TEST_P(QuicStreamFactoryTest, OnCertAdded) {
1413 MockRead reads[] = {
1414 MockRead(ASYNC, 0, 0) // EOF
1416 scoped_ptr<QuicEncryptedPacket> rst(ConstructRstPacket());
1417 std::vector<MockWrite> writes;
1418 writes.push_back(MockWrite(ASYNC, rst->data(), rst->length(), 1));
1419 DeterministicSocketData socket_data(reads, arraysize(reads),
1420 writes.empty() ? nullptr : &writes[0],
1421 writes.size());
1422 socket_factory_.AddSocketDataProvider(&socket_data);
1423 socket_data.StopAfter(1);
1425 MockRead reads2[] = {
1426 MockRead(ASYNC, 0, 0) // EOF
1428 DeterministicSocketData socket_data2(reads2, arraysize(reads2), nullptr, 0);
1429 socket_factory_.AddSocketDataProvider(&socket_data2);
1430 socket_data2.StopAfter(1);
1432 QuicStreamRequest request(&factory_);
1433 EXPECT_EQ(ERR_IO_PENDING,
1434 request.Request(host_port_pair_,
1435 is_https_,
1436 privacy_mode_,
1437 "GET",
1438 net_log_,
1439 callback_.callback()));
1441 EXPECT_EQ(OK, callback_.WaitForResult());
1442 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
1443 HttpRequestInfo request_info;
1444 EXPECT_EQ(OK, stream->InitializeStream(&request_info,
1445 DEFAULT_PRIORITY,
1446 net_log_, CompletionCallback()));
1448 // Add a cert and verify that stream saw the event.
1449 factory_.OnCertAdded(nullptr);
1450 EXPECT_EQ(ERR_CERT_DATABASE_CHANGED,
1451 stream->ReadResponseHeaders(callback_.callback()));
1452 EXPECT_FALSE(factory_.require_confirmation());
1454 // Now attempting to request a stream to the same origin should create
1455 // a new session.
1457 QuicStreamRequest request2(&factory_);
1458 EXPECT_EQ(ERR_IO_PENDING,
1459 request2.Request(host_port_pair_,
1460 is_https_,
1461 privacy_mode_,
1462 "GET",
1463 net_log_,
1464 callback_.callback()));
1466 EXPECT_EQ(OK, callback_.WaitForResult());
1467 stream = request2.ReleaseStream();
1468 stream.reset(); // Will reset stream 3.
1470 EXPECT_TRUE(socket_data.at_read_eof());
1471 EXPECT_TRUE(socket_data.at_write_eof());
1472 EXPECT_TRUE(socket_data2.at_read_eof());
1473 EXPECT_TRUE(socket_data2.at_write_eof());
1476 TEST_P(QuicStreamFactoryTest, OnCACertChanged) {
1477 MockRead reads[] = {
1478 MockRead(ASYNC, 0, 0) // EOF
1480 scoped_ptr<QuicEncryptedPacket> rst(ConstructRstPacket());
1481 std::vector<MockWrite> writes;
1482 writes.push_back(MockWrite(ASYNC, rst->data(), rst->length(), 1));
1483 DeterministicSocketData socket_data(reads, arraysize(reads),
1484 writes.empty() ? nullptr : &writes[0],
1485 writes.size());
1486 socket_factory_.AddSocketDataProvider(&socket_data);
1487 socket_data.StopAfter(1);
1489 MockRead reads2[] = {
1490 MockRead(ASYNC, 0, 0) // EOF
1492 DeterministicSocketData socket_data2(reads2, arraysize(reads2), nullptr, 0);
1493 socket_factory_.AddSocketDataProvider(&socket_data2);
1494 socket_data2.StopAfter(1);
1496 QuicStreamRequest request(&factory_);
1497 EXPECT_EQ(ERR_IO_PENDING,
1498 request.Request(host_port_pair_,
1499 is_https_,
1500 privacy_mode_,
1501 "GET",
1502 net_log_,
1503 callback_.callback()));
1505 EXPECT_EQ(OK, callback_.WaitForResult());
1506 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
1507 HttpRequestInfo request_info;
1508 EXPECT_EQ(OK, stream->InitializeStream(&request_info,
1509 DEFAULT_PRIORITY,
1510 net_log_, CompletionCallback()));
1512 // Change the CA cert and verify that stream saw the event.
1513 factory_.OnCACertChanged(nullptr);
1514 EXPECT_EQ(ERR_CERT_DATABASE_CHANGED,
1515 stream->ReadResponseHeaders(callback_.callback()));
1516 EXPECT_FALSE(factory_.require_confirmation());
1518 // Now attempting to request a stream to the same origin should create
1519 // a new session.
1521 QuicStreamRequest request2(&factory_);
1522 EXPECT_EQ(ERR_IO_PENDING,
1523 request2.Request(host_port_pair_,
1524 is_https_,
1525 privacy_mode_,
1526 "GET",
1527 net_log_,
1528 callback_.callback()));
1530 EXPECT_EQ(OK, callback_.WaitForResult());
1531 stream = request2.ReleaseStream();
1532 stream.reset(); // Will reset stream 3.
1534 EXPECT_TRUE(socket_data.at_read_eof());
1535 EXPECT_TRUE(socket_data.at_write_eof());
1536 EXPECT_TRUE(socket_data2.at_read_eof());
1537 EXPECT_TRUE(socket_data2.at_write_eof());
1540 TEST_P(QuicStreamFactoryTest, SharedCryptoConfig) {
1541 vector<string> cannoncial_suffixes;
1542 cannoncial_suffixes.push_back(string(".c.youtube.com"));
1543 cannoncial_suffixes.push_back(string(".googlevideo.com"));
1545 for (unsigned i = 0; i < cannoncial_suffixes.size(); ++i) {
1546 string r1_host_name("r1");
1547 string r2_host_name("r2");
1548 r1_host_name.append(cannoncial_suffixes[i]);
1549 r2_host_name.append(cannoncial_suffixes[i]);
1551 HostPortPair host_port_pair1(r1_host_name, 80);
1552 QuicCryptoClientConfig* crypto_config =
1553 QuicStreamFactoryPeer::GetCryptoConfig(&factory_);
1554 QuicServerId server_id1(host_port_pair1, is_https_, privacy_mode_);
1555 QuicCryptoClientConfig::CachedState* cached1 =
1556 crypto_config->LookupOrCreate(server_id1);
1557 EXPECT_FALSE(cached1->proof_valid());
1558 EXPECT_TRUE(cached1->source_address_token().empty());
1560 // Mutate the cached1 to have different data.
1561 // TODO(rtenneti): mutate other members of CachedState.
1562 cached1->set_source_address_token(r1_host_name);
1563 cached1->SetProofValid();
1565 HostPortPair host_port_pair2(r2_host_name, 80);
1566 QuicServerId server_id2(host_port_pair2, is_https_, privacy_mode_);
1567 QuicCryptoClientConfig::CachedState* cached2 =
1568 crypto_config->LookupOrCreate(server_id2);
1569 EXPECT_EQ(cached1->source_address_token(), cached2->source_address_token());
1570 EXPECT_TRUE(cached2->proof_valid());
1574 TEST_P(QuicStreamFactoryTest, CryptoConfigWhenProofIsInvalid) {
1575 vector<string> cannoncial_suffixes;
1576 cannoncial_suffixes.push_back(string(".c.youtube.com"));
1577 cannoncial_suffixes.push_back(string(".googlevideo.com"));
1579 for (unsigned i = 0; i < cannoncial_suffixes.size(); ++i) {
1580 string r3_host_name("r3");
1581 string r4_host_name("r4");
1582 r3_host_name.append(cannoncial_suffixes[i]);
1583 r4_host_name.append(cannoncial_suffixes[i]);
1585 HostPortPair host_port_pair1(r3_host_name, 80);
1586 QuicCryptoClientConfig* crypto_config =
1587 QuicStreamFactoryPeer::GetCryptoConfig(&factory_);
1588 QuicServerId server_id1(host_port_pair1, is_https_, privacy_mode_);
1589 QuicCryptoClientConfig::CachedState* cached1 =
1590 crypto_config->LookupOrCreate(server_id1);
1591 EXPECT_FALSE(cached1->proof_valid());
1592 EXPECT_TRUE(cached1->source_address_token().empty());
1594 // Mutate the cached1 to have different data.
1595 // TODO(rtenneti): mutate other members of CachedState.
1596 cached1->set_source_address_token(r3_host_name);
1597 cached1->SetProofInvalid();
1599 HostPortPair host_port_pair2(r4_host_name, 80);
1600 QuicServerId server_id2(host_port_pair2, is_https_, privacy_mode_);
1601 QuicCryptoClientConfig::CachedState* cached2 =
1602 crypto_config->LookupOrCreate(server_id2);
1603 EXPECT_NE(cached1->source_address_token(), cached2->source_address_token());
1604 EXPECT_TRUE(cached2->source_address_token().empty());
1605 EXPECT_FALSE(cached2->proof_valid());
1609 TEST_P(QuicStreamFactoryTest, RacingConnections) {
1610 if (!GetParam().enable_connection_racing)
1611 return;
1612 factory_.set_quic_server_info_factory(&quic_server_info_factory_);
1613 QuicStreamFactoryPeer::SetTaskRunner(&factory_, runner_.get());
1614 MockRead reads[] = {
1615 MockRead(ASYNC, OK, 0) // EOF
1617 DeterministicSocketData socket_data(reads, arraysize(reads), nullptr, 0);
1618 socket_factory_.AddSocketDataProvider(&socket_data);
1619 socket_data.StopAfter(1);
1621 MockRead reads2[] = {
1622 MockRead(ASYNC, 0, 0) // EOF
1624 DeterministicSocketData socket_data2(reads2, arraysize(reads2), nullptr, 0);
1625 socket_factory_.AddSocketDataProvider(&socket_data2);
1626 socket_data2.StopAfter(1);
1628 crypto_client_stream_factory_.set_handshake_mode(
1629 MockCryptoClientStream::ZERO_RTT);
1630 host_resolver_.set_synchronous_mode(true);
1631 host_resolver_.rules()->AddIPLiteralRule(host_port_pair_.host(),
1632 "192.168.0.1", "");
1634 QuicStreamRequest request(&factory_);
1635 QuicServerId server_id(host_port_pair_, is_https_, privacy_mode_);
1636 EXPECT_EQ(ERR_IO_PENDING,
1637 request.Request(host_port_pair_, is_https_, privacy_mode_, "GET",
1638 net_log_, callback_.callback()));
1639 EXPECT_EQ(2u,
1640 QuicStreamFactoryPeer::GetNumberOfActiveJobs(&factory_, server_id));
1642 runner_->RunNextTask();
1644 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
1645 EXPECT_TRUE(stream.get());
1646 EXPECT_TRUE(socket_data.at_read_eof());
1647 EXPECT_TRUE(socket_data.at_write_eof());
1648 EXPECT_EQ(0u,
1649 QuicStreamFactoryPeer::GetNumberOfActiveJobs(&factory_, server_id));
1652 TEST_P(QuicStreamFactoryTest, EnableNotLoadFromDiskCache) {
1653 factory_.set_quic_server_info_factory(&quic_server_info_factory_);
1654 QuicStreamFactoryPeer::SetTaskRunner(&factory_, runner_.get());
1655 QuicStreamFactoryPeer::SetDisableDiskCache(&factory_, true);
1657 MockRead reads[] = {
1658 MockRead(ASYNC, OK, 0) // EOF
1660 DeterministicSocketData socket_data(reads, arraysize(reads), nullptr, 0);
1661 socket_factory_.AddSocketDataProvider(&socket_data);
1662 socket_data.StopAfter(1);
1664 crypto_client_stream_factory_.set_handshake_mode(
1665 MockCryptoClientStream::ZERO_RTT);
1666 host_resolver_.set_synchronous_mode(true);
1667 host_resolver_.rules()->AddIPLiteralRule(host_port_pair_.host(),
1668 "192.168.0.1", "");
1670 QuicStreamRequest request(&factory_);
1671 EXPECT_EQ(OK, request.Request(host_port_pair_, is_https_, privacy_mode_,
1672 "GET", net_log_, callback_.callback()));
1674 // If we are waiting for disk cache, we would have posted a task. Verify that
1675 // the CancelWaitForDataReady task hasn't been posted.
1676 ASSERT_EQ(0u, runner_->GetPostedTasks().size());
1678 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
1679 EXPECT_TRUE(stream.get());
1680 EXPECT_TRUE(socket_data.at_read_eof());
1681 EXPECT_TRUE(socket_data.at_write_eof());
1684 } // namespace test
1685 } // namespace net