ozone: evdev: Sync caps lock LED state to evdev
[chromium-blink-merge.git] / net / quic / quic_stream_factory_test.cc
blob2e62d5ead4cef7b83f1d0a0d7321aa6f8d507db6
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "net/quic/quic_stream_factory.h"
7 #include "base/run_loop.h"
8 #include "base/strings/string_util.h"
9 #include "net/base/test_data_directory.h"
10 #include "net/cert/cert_verifier.h"
11 #include "net/dns/mock_host_resolver.h"
12 #include "net/http/http_response_headers.h"
13 #include "net/http/http_response_info.h"
14 #include "net/http/http_util.h"
15 #include "net/http/transport_security_state.h"
16 #include "net/quic/crypto/crypto_handshake.h"
17 #include "net/quic/crypto/proof_verifier_chromium.h"
18 #include "net/quic/crypto/quic_decrypter.h"
19 #include "net/quic/crypto/quic_encrypter.h"
20 #include "net/quic/crypto/quic_server_info.h"
21 #include "net/quic/quic_http_stream.h"
22 #include "net/quic/quic_server_id.h"
23 #include "net/quic/test_tools/mock_clock.h"
24 #include "net/quic/test_tools/mock_crypto_client_stream_factory.h"
25 #include "net/quic/test_tools/mock_random.h"
26 #include "net/quic/test_tools/quic_test_packet_maker.h"
27 #include "net/quic/test_tools/quic_test_utils.h"
28 #include "net/quic/test_tools/test_task_runner.h"
29 #include "net/socket/socket_test_util.h"
30 #include "net/spdy/spdy_test_utils.h"
31 #include "net/ssl/channel_id_service.h"
32 #include "net/ssl/default_channel_id_store.h"
33 #include "net/test/cert_test_util.h"
34 #include "testing/gtest/include/gtest/gtest.h"
36 using base::StringPiece;
37 using std::ostream;
38 using std::string;
39 using std::vector;
41 namespace net {
42 namespace test {
44 namespace {
45 const char kDefaultServerHostName[] = "www.google.com";
46 const int kDefaultServerPort = 443;
48 // Run all tests with all the combinations of versions and
49 // enable_connection_racing.
50 struct TestParams {
51 TestParams(const QuicVersion version, bool enable_connection_racing)
52 : version(version), enable_connection_racing(enable_connection_racing) {}
54 friend ostream& operator<<(ostream& os, const TestParams& p) {
55 os << "{ version: " << QuicVersionToString(p.version);
56 os << " enable_connection_racing: " << p.enable_connection_racing << " }";
57 return os;
60 QuicVersion version;
61 bool enable_connection_racing;
64 // Constructs various test permutations.
65 vector<TestParams> GetTestParams() {
66 vector<TestParams> params;
67 QuicVersionVector all_supported_versions = QuicSupportedVersions();
68 for (const QuicVersion version : all_supported_versions) {
69 params.push_back(TestParams(version, false));
70 params.push_back(TestParams(version, true));
72 return params;
75 } // namespace anonymous
77 class QuicStreamFactoryPeer {
78 public:
79 static QuicCryptoClientConfig* GetCryptoConfig(QuicStreamFactory* factory) {
80 return &factory->crypto_config_;
83 static bool HasActiveSession(QuicStreamFactory* factory,
84 const HostPortPair& host_port_pair,
85 bool is_https) {
86 QuicServerId server_id(host_port_pair, is_https, PRIVACY_MODE_DISABLED);
87 return factory->HasActiveSession(server_id);
90 static QuicClientSession* GetActiveSession(
91 QuicStreamFactory* factory,
92 const HostPortPair& host_port_pair,
93 bool is_https) {
94 QuicServerId server_id(host_port_pair, is_https, PRIVACY_MODE_DISABLED);
95 DCHECK(factory->HasActiveSession(server_id));
96 return factory->active_sessions_[server_id];
99 static scoped_ptr<QuicHttpStream> CreateIfSessionExists(
100 QuicStreamFactory* factory,
101 const HostPortPair& host_port_pair,
102 bool is_https,
103 const BoundNetLog& net_log) {
104 QuicServerId server_id(host_port_pair, is_https, PRIVACY_MODE_DISABLED);
105 return factory->CreateIfSessionExists(server_id, net_log);
108 static bool IsLiveSession(QuicStreamFactory* factory,
109 QuicClientSession* session) {
110 for (QuicStreamFactory::SessionIdMap::iterator it =
111 factory->all_sessions_.begin();
112 it != factory->all_sessions_.end(); ++it) {
113 if (it->first == session)
114 return true;
116 return false;
119 static void DisableConnectionPooling(QuicStreamFactory* factory) {
120 factory->disable_connection_pooling_ = true;
123 static void SetTaskRunner(QuicStreamFactory* factory,
124 base::TaskRunner* task_runner) {
125 factory->task_runner_ = task_runner;
128 static void SetLoadServerInfoTimeout(QuicStreamFactory* factory,
129 size_t load_server_info_timeout) {
130 factory->load_server_info_timeout_ms_ = load_server_info_timeout;
133 static void SetEnableConnectionRacing(QuicStreamFactory* factory,
134 bool enable_connection_racing) {
135 factory->enable_connection_racing_ = enable_connection_racing;
138 static void SetDisableDiskCache(QuicStreamFactory* factory,
139 bool disable_disk_cache) {
140 factory->disable_disk_cache_ = disable_disk_cache;
143 static size_t GetNumberOfActiveJobs(QuicStreamFactory* factory,
144 const QuicServerId& server_id) {
145 return (factory->active_jobs_[server_id]).size();
149 class MockQuicServerInfo : public QuicServerInfo {
150 public:
151 MockQuicServerInfo(const QuicServerId& server_id)
152 : QuicServerInfo(server_id) {}
153 ~MockQuicServerInfo() override {}
155 void Start() override {}
157 int WaitForDataReady(const CompletionCallback& callback) override {
158 return ERR_IO_PENDING;
161 void ResetWaitForDataReadyCallback() override {}
163 void CancelWaitForDataReadyCallback() override {}
165 bool IsDataReady() override { return false; }
167 bool IsReadyToPersist() override { return false; }
169 void Persist() override {}
171 void OnExternalCacheHit() override {}
174 class MockQuicServerInfoFactory : public QuicServerInfoFactory {
175 public:
176 MockQuicServerInfoFactory() {}
177 ~MockQuicServerInfoFactory() override {}
179 QuicServerInfo* GetForServer(const QuicServerId& server_id) override {
180 return new MockQuicServerInfo(server_id);
184 class QuicStreamFactoryTest : public ::testing::TestWithParam<TestParams> {
185 protected:
186 QuicStreamFactoryTest()
187 : random_generator_(0),
188 clock_(new MockClock()),
189 runner_(new TestTaskRunner(clock_)),
190 maker_(GetParam().version, 0, clock_),
191 cert_verifier_(CertVerifier::CreateDefault()),
192 channel_id_service_(
193 new ChannelIDService(new DefaultChannelIDStore(nullptr),
194 base::MessageLoopProxy::current())),
195 factory_(&host_resolver_,
196 &socket_factory_,
197 base::WeakPtr<HttpServerProperties>(),
198 cert_verifier_.get(),
199 channel_id_service_.get(),
200 &transport_security_state_,
201 &crypto_client_stream_factory_,
202 &random_generator_,
203 clock_,
204 kDefaultMaxPacketSize,
205 std::string(),
206 SupportedVersions(GetParam().version),
207 /*enable_port_selection=*/true,
208 /*always_require_handshake_confirmation=*/false,
209 /*disable_connection_pooling=*/false,
210 /*load_server_info_timeout=*/0u,
211 /*load_server_info_timeout_srtt_multiplier=*/0.0f,
212 /*enable_truncated_connection_ids=*/true,
213 /*enable_connection_racing=*/false,
214 /*enable_non_blocking_io=*/true,
215 /*disable_disk_cache=*/false,
216 /*receive_buffer_size=*/0,
217 QuicTagVector()),
218 host_port_pair_(kDefaultServerHostName, kDefaultServerPort),
219 is_https_(false),
220 privacy_mode_(PRIVACY_MODE_DISABLED) {
221 factory_.set_require_confirmation(false);
222 clock_->AdvanceTime(QuicTime::Delta::FromSeconds(1));
223 QuicStreamFactoryPeer::SetEnableConnectionRacing(
224 &factory_, GetParam().enable_connection_racing);
227 scoped_ptr<QuicHttpStream> CreateIfSessionExists(
228 const HostPortPair& host_port_pair,
229 const BoundNetLog& net_log) {
230 return QuicStreamFactoryPeer::CreateIfSessionExists(
231 &factory_, host_port_pair, false, net_log_);
234 int GetSourcePortForNewSession(const HostPortPair& destination) {
235 return GetSourcePortForNewSessionInner(destination, false);
238 int GetSourcePortForNewSessionAndGoAway(
239 const HostPortPair& destination) {
240 return GetSourcePortForNewSessionInner(destination, true);
243 int GetSourcePortForNewSessionInner(const HostPortPair& destination,
244 bool goaway_received) {
245 // Should only be called if there is no active session for this destination.
246 EXPECT_EQ(nullptr, CreateIfSessionExists(destination, net_log_).get());
247 size_t socket_count = socket_factory_.udp_client_sockets().size();
249 MockRead reads[] = {
250 MockRead(ASYNC, OK, 0) // EOF
252 DeterministicSocketData socket_data(reads, arraysize(reads), nullptr, 0);
253 socket_data.StopAfter(1);
254 socket_factory_.AddSocketDataProvider(&socket_data);
256 QuicStreamRequest request(&factory_);
257 EXPECT_EQ(ERR_IO_PENDING,
258 request.Request(destination,
259 is_https_,
260 privacy_mode_,
261 "GET",
262 net_log_,
263 callback_.callback()));
265 EXPECT_EQ(OK, callback_.WaitForResult());
266 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
267 EXPECT_TRUE(stream.get());
268 stream.reset();
270 QuicClientSession* session = QuicStreamFactoryPeer::GetActiveSession(
271 &factory_, destination, is_https_);
273 if (socket_count + 1 != socket_factory_.udp_client_sockets().size()) {
274 EXPECT_TRUE(false);
275 return 0;
278 IPEndPoint endpoint;
279 socket_factory_.
280 udp_client_sockets()[socket_count]->GetLocalAddress(&endpoint);
281 int port = endpoint.port();
282 if (goaway_received) {
283 QuicGoAwayFrame goaway(QUIC_NO_ERROR, 1, "");
284 session->OnGoAway(goaway);
287 factory_.OnSessionClosed(session);
288 EXPECT_EQ(nullptr, CreateIfSessionExists(destination, net_log_).get());
289 EXPECT_TRUE(socket_data.at_read_eof());
290 EXPECT_TRUE(socket_data.at_write_eof());
291 return port;
294 scoped_ptr<QuicEncryptedPacket> ConstructRstPacket() {
295 QuicStreamId stream_id = kClientDataStreamId1;
296 return maker_.MakeRstPacket(
297 1, true, stream_id,
298 AdjustErrorForVersion(QUIC_RST_ACKNOWLEDGEMENT, GetParam().version));
301 MockQuicServerInfoFactory quic_server_info_factory_;
302 MockHostResolver host_resolver_;
303 DeterministicMockClientSocketFactory socket_factory_;
304 MockCryptoClientStreamFactory crypto_client_stream_factory_;
305 MockRandom random_generator_;
306 MockClock* clock_; // Owned by factory_.
307 scoped_refptr<TestTaskRunner> runner_;
308 QuicTestPacketMaker maker_;
309 scoped_ptr<CertVerifier> cert_verifier_;
310 scoped_ptr<ChannelIDService> channel_id_service_;
311 TransportSecurityState transport_security_state_;
312 QuicStreamFactory factory_;
313 HostPortPair host_port_pair_;
314 bool is_https_;
315 PrivacyMode privacy_mode_;
316 BoundNetLog net_log_;
317 TestCompletionCallback callback_;
320 INSTANTIATE_TEST_CASE_P(Version,
321 QuicStreamFactoryTest,
322 ::testing::ValuesIn(GetTestParams()));
324 TEST_P(QuicStreamFactoryTest, CreateIfSessionExists) {
325 EXPECT_EQ(nullptr, CreateIfSessionExists(host_port_pair_, net_log_).get());
328 TEST_P(QuicStreamFactoryTest, Create) {
329 MockRead reads[] = {
330 MockRead(ASYNC, OK, 0) // EOF
332 DeterministicSocketData socket_data(reads, arraysize(reads), nullptr, 0);
333 socket_factory_.AddSocketDataProvider(&socket_data);
334 socket_data.StopAfter(1);
336 QuicStreamRequest request(&factory_);
337 EXPECT_EQ(ERR_IO_PENDING,
338 request.Request(host_port_pair_,
339 is_https_,
340 privacy_mode_,
341 "GET",
342 net_log_,
343 callback_.callback()));
345 EXPECT_EQ(OK, callback_.WaitForResult());
346 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
347 EXPECT_TRUE(stream.get());
349 // Will reset stream 3.
350 stream = CreateIfSessionExists(host_port_pair_, net_log_);
351 EXPECT_TRUE(stream.get());
353 // TODO(rtenneti): We should probably have a tests that HTTP and HTTPS result
354 // in streams on different sessions.
355 QuicStreamRequest request2(&factory_);
356 EXPECT_EQ(OK,
357 request2.Request(host_port_pair_,
358 is_https_,
359 privacy_mode_,
360 "GET",
361 net_log_,
362 callback_.callback()));
363 stream = request2.ReleaseStream(); // Will reset stream 5.
364 stream.reset(); // Will reset stream 7.
366 EXPECT_TRUE(socket_data.at_read_eof());
367 EXPECT_TRUE(socket_data.at_write_eof());
370 TEST_P(QuicStreamFactoryTest, CreateZeroRtt) {
371 MockRead reads[] = {
372 MockRead(ASYNC, OK, 0) // EOF
374 DeterministicSocketData socket_data(reads, arraysize(reads), nullptr, 0);
375 socket_factory_.AddSocketDataProvider(&socket_data);
376 socket_data.StopAfter(1);
378 crypto_client_stream_factory_.set_handshake_mode(
379 MockCryptoClientStream::ZERO_RTT);
380 host_resolver_.set_synchronous_mode(true);
381 host_resolver_.rules()->AddIPLiteralRule(host_port_pair_.host(),
382 "192.168.0.1", "");
384 QuicStreamRequest request(&factory_);
385 EXPECT_EQ(OK,
386 request.Request(host_port_pair_,
387 is_https_,
388 privacy_mode_,
389 "GET",
390 net_log_,
391 callback_.callback()));
393 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
394 EXPECT_TRUE(stream.get());
395 EXPECT_TRUE(socket_data.at_read_eof());
396 EXPECT_TRUE(socket_data.at_write_eof());
399 TEST_P(QuicStreamFactoryTest, CreateZeroRttPost) {
400 MockRead reads[] = {
401 MockRead(ASYNC, OK, 0) // EOF
403 DeterministicSocketData socket_data(reads, arraysize(reads), nullptr, 0);
404 socket_factory_.AddSocketDataProvider(&socket_data);
405 socket_data.StopAfter(1);
407 crypto_client_stream_factory_.set_handshake_mode(
408 MockCryptoClientStream::ZERO_RTT);
409 host_resolver_.set_synchronous_mode(true);
410 host_resolver_.rules()->AddIPLiteralRule(host_port_pair_.host(),
411 "192.168.0.1", "");
413 QuicStreamRequest request(&factory_);
414 // Posts require handshake confirmation, so this will return asynchronously.
415 EXPECT_EQ(ERR_IO_PENDING,
416 request.Request(host_port_pair_,
417 is_https_,
418 privacy_mode_,
419 "POST",
420 net_log_,
421 callback_.callback()));
423 // Confirm the handshake and verify that the stream is created.
424 crypto_client_stream_factory_.last_stream()->SendOnCryptoHandshakeEvent(
425 QuicSession::HANDSHAKE_CONFIRMED);
427 EXPECT_EQ(OK, callback_.WaitForResult());
428 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
429 EXPECT_TRUE(stream.get());
430 EXPECT_TRUE(socket_data.at_read_eof());
431 EXPECT_TRUE(socket_data.at_write_eof());
434 TEST_P(QuicStreamFactoryTest, CreateHttpVsHttps) {
435 MockRead reads[] = {
436 MockRead(ASYNC, OK, 0) // EOF
438 DeterministicSocketData socket_data1(reads, arraysize(reads), nullptr, 0);
439 DeterministicSocketData socket_data2(reads, arraysize(reads), nullptr, 0);
440 socket_factory_.AddSocketDataProvider(&socket_data1);
441 socket_factory_.AddSocketDataProvider(&socket_data2);
442 socket_data1.StopAfter(1);
443 socket_data2.StopAfter(1);
445 QuicStreamRequest request(&factory_);
446 EXPECT_EQ(ERR_IO_PENDING,
447 request.Request(host_port_pair_,
448 is_https_,
449 privacy_mode_,
450 "GET",
451 net_log_,
452 callback_.callback()));
454 EXPECT_EQ(OK, callback_.WaitForResult());
455 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
456 EXPECT_TRUE(stream.get());
458 QuicStreamRequest request2(&factory_);
459 EXPECT_EQ(ERR_IO_PENDING,
460 request2.Request(host_port_pair_,
461 !is_https_,
462 privacy_mode_,
463 "GET",
464 net_log_,
465 callback_.callback()));
466 EXPECT_EQ(OK, callback_.WaitForResult());
467 stream = request2.ReleaseStream();
468 EXPECT_TRUE(stream.get());
469 stream.reset();
471 EXPECT_NE(QuicStreamFactoryPeer::GetActiveSession(
472 &factory_, host_port_pair_, is_https_),
473 QuicStreamFactoryPeer::GetActiveSession(
474 &factory_, host_port_pair_, !is_https_));
476 EXPECT_TRUE(socket_data1.at_read_eof());
477 EXPECT_TRUE(socket_data1.at_write_eof());
478 EXPECT_TRUE(socket_data2.at_read_eof());
479 EXPECT_TRUE(socket_data2.at_write_eof());
482 TEST_P(QuicStreamFactoryTest, Pooling) {
483 MockRead reads[] = {
484 MockRead(ASYNC, OK, 0) // EOF
486 DeterministicSocketData socket_data(reads, arraysize(reads), nullptr, 0);
487 socket_factory_.AddSocketDataProvider(&socket_data);
488 socket_data.StopAfter(1);
490 HostPortPair server2("mail.google.com", kDefaultServerPort);
491 host_resolver_.set_synchronous_mode(true);
492 host_resolver_.rules()->AddIPLiteralRule(
493 kDefaultServerHostName, "192.168.0.1", "");
494 host_resolver_.rules()->AddIPLiteralRule(
495 "mail.google.com", "192.168.0.1", "");
497 QuicStreamRequest request(&factory_);
498 EXPECT_EQ(OK,
499 request.Request(host_port_pair_,
500 is_https_,
501 privacy_mode_,
502 "GET",
503 net_log_,
504 callback_.callback()));
505 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
506 EXPECT_TRUE(stream.get());
508 TestCompletionCallback callback;
509 QuicStreamRequest request2(&factory_);
510 EXPECT_EQ(OK,
511 request2.Request(server2,
512 is_https_,
513 privacy_mode_,
514 "GET",
515 net_log_,
516 callback.callback()));
517 scoped_ptr<QuicHttpStream> stream2 = request2.ReleaseStream();
518 EXPECT_TRUE(stream2.get());
520 EXPECT_EQ(
521 QuicStreamFactoryPeer::GetActiveSession(
522 &factory_, host_port_pair_, is_https_),
523 QuicStreamFactoryPeer::GetActiveSession(&factory_, server2, is_https_));
525 EXPECT_TRUE(socket_data.at_read_eof());
526 EXPECT_TRUE(socket_data.at_write_eof());
529 TEST_P(QuicStreamFactoryTest, NoPoolingIfDisabled) {
530 MockRead reads[] = {
531 MockRead(ASYNC, OK, 0) // EOF
533 DeterministicSocketData socket_data1(reads, arraysize(reads), nullptr, 0);
534 DeterministicSocketData socket_data2(reads, arraysize(reads), nullptr, 0);
535 socket_factory_.AddSocketDataProvider(&socket_data1);
536 socket_factory_.AddSocketDataProvider(&socket_data2);
537 socket_data1.StopAfter(1);
538 socket_data2.StopAfter(1);
540 HostPortPair server2("mail.google.com", kDefaultServerPort);
541 host_resolver_.set_synchronous_mode(true);
542 host_resolver_.rules()->AddIPLiteralRule(
543 kDefaultServerHostName, "192.168.0.1", "");
544 host_resolver_.rules()->AddIPLiteralRule(
545 "mail.google.com", "192.168.0.1", "");
547 // Disable connection pooling.
548 QuicStreamFactoryPeer::DisableConnectionPooling(&factory_);
550 QuicStreamRequest request(&factory_);
551 EXPECT_EQ(OK,
552 request.Request(host_port_pair_,
553 is_https_,
554 privacy_mode_,
555 "GET",
556 net_log_,
557 callback_.callback()));
558 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
559 EXPECT_TRUE(stream.get());
561 TestCompletionCallback callback;
562 QuicStreamRequest request2(&factory_);
563 EXPECT_EQ(OK,
564 request2.Request(server2,
565 is_https_,
566 privacy_mode_,
567 "GET",
568 net_log_,
569 callback.callback()));
570 scoped_ptr<QuicHttpStream> stream2 = request2.ReleaseStream();
571 EXPECT_TRUE(stream2.get());
573 EXPECT_NE(
574 QuicStreamFactoryPeer::GetActiveSession(
575 &factory_, host_port_pair_, is_https_),
576 QuicStreamFactoryPeer::GetActiveSession(&factory_, server2, is_https_));
578 EXPECT_TRUE(socket_data1.at_read_eof());
579 EXPECT_TRUE(socket_data1.at_write_eof());
580 EXPECT_TRUE(socket_data2.at_read_eof());
581 EXPECT_TRUE(socket_data2.at_write_eof());
584 TEST_P(QuicStreamFactoryTest, NoPoolingAfterGoAway) {
585 MockRead reads[] = {
586 MockRead(ASYNC, OK, 0) // EOF
588 DeterministicSocketData socket_data1(reads, arraysize(reads), nullptr, 0);
589 DeterministicSocketData socket_data2(reads, arraysize(reads), nullptr, 0);
590 socket_factory_.AddSocketDataProvider(&socket_data1);
591 socket_factory_.AddSocketDataProvider(&socket_data2);
592 socket_data1.StopAfter(1);
593 socket_data2.StopAfter(1);
595 HostPortPair server2("mail.google.com", kDefaultServerPort);
596 host_resolver_.set_synchronous_mode(true);
597 host_resolver_.rules()->AddIPLiteralRule(
598 kDefaultServerHostName, "192.168.0.1", "");
599 host_resolver_.rules()->AddIPLiteralRule(
600 "mail.google.com", "192.168.0.1", "");
602 QuicStreamRequest request(&factory_);
603 EXPECT_EQ(OK,
604 request.Request(host_port_pair_,
605 is_https_,
606 privacy_mode_,
607 "GET",
608 net_log_,
609 callback_.callback()));
610 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
611 EXPECT_TRUE(stream.get());
613 TestCompletionCallback callback;
614 QuicStreamRequest request2(&factory_);
615 EXPECT_EQ(OK,
616 request2.Request(server2,
617 is_https_,
618 privacy_mode_,
619 "GET",
620 net_log_,
621 callback.callback()));
622 scoped_ptr<QuicHttpStream> stream2 = request2.ReleaseStream();
623 EXPECT_TRUE(stream2.get());
625 factory_.OnSessionGoingAway(QuicStreamFactoryPeer::GetActiveSession(
626 &factory_, host_port_pair_, is_https_));
627 EXPECT_FALSE(QuicStreamFactoryPeer::HasActiveSession(
628 &factory_, host_port_pair_, is_https_));
629 EXPECT_FALSE(QuicStreamFactoryPeer::HasActiveSession(
630 &factory_, server2, is_https_));
632 TestCompletionCallback callback3;
633 QuicStreamRequest request3(&factory_);
634 EXPECT_EQ(OK,
635 request3.Request(server2,
636 is_https_,
637 privacy_mode_,
638 "GET",
639 net_log_,
640 callback3.callback()));
641 scoped_ptr<QuicHttpStream> stream3 = request3.ReleaseStream();
642 EXPECT_TRUE(stream3.get());
644 EXPECT_TRUE(QuicStreamFactoryPeer::HasActiveSession(
645 &factory_, server2, is_https_));
647 EXPECT_TRUE(socket_data1.at_read_eof());
648 EXPECT_TRUE(socket_data1.at_write_eof());
649 EXPECT_TRUE(socket_data2.at_read_eof());
650 EXPECT_TRUE(socket_data2.at_write_eof());
653 TEST_P(QuicStreamFactoryTest, HttpsPooling) {
654 MockRead reads[] = {
655 MockRead(ASYNC, OK, 0) // EOF
657 DeterministicSocketData socket_data(reads, arraysize(reads), nullptr, 0);
658 socket_factory_.AddSocketDataProvider(&socket_data);
659 socket_data.StopAfter(1);
661 HostPortPair server1("www.example.org", 443);
662 HostPortPair server2("mail.example.org", 443);
664 // Load a cert that is valid for:
665 // www.example.org (server1)
666 // mail.example.org (server2)
667 // www.example.com
668 base::FilePath certs_dir = GetTestCertsDirectory();
669 scoped_refptr<X509Certificate> test_cert(
670 ImportCertFromFile(certs_dir, "spdy_pooling.pem"));
671 ASSERT_NE(static_cast<X509Certificate*>(nullptr), test_cert.get());
672 ProofVerifyDetailsChromium verify_details;
673 verify_details.cert_verify_result.verified_cert = test_cert;
674 verify_details.cert_verify_result.is_issued_by_known_root = true;
675 crypto_client_stream_factory_.set_proof_verify_details(&verify_details);
677 host_resolver_.set_synchronous_mode(true);
678 host_resolver_.rules()->AddIPLiteralRule(server1.host(), "192.168.0.1", "");
679 host_resolver_.rules()->AddIPLiteralRule(server2.host(), "192.168.0.1", "");
681 QuicStreamRequest request(&factory_);
682 is_https_ = true;
683 EXPECT_EQ(OK,
684 request.Request(server1,
685 is_https_,
686 privacy_mode_,
687 "GET",
688 net_log_,
689 callback_.callback()));
690 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
691 EXPECT_TRUE(stream.get());
693 TestCompletionCallback callback;
694 QuicStreamRequest request2(&factory_);
695 EXPECT_EQ(OK,
696 request2.Request(server2,
697 is_https_,
698 privacy_mode_,
699 "GET",
700 net_log_,
701 callback_.callback()));
702 scoped_ptr<QuicHttpStream> stream2 = request2.ReleaseStream();
703 EXPECT_TRUE(stream2.get());
705 EXPECT_EQ(QuicStreamFactoryPeer::GetActiveSession(
706 &factory_, server1, is_https_),
707 QuicStreamFactoryPeer::GetActiveSession(
708 &factory_, server2, is_https_));
710 EXPECT_TRUE(socket_data.at_read_eof());
711 EXPECT_TRUE(socket_data.at_write_eof());
714 TEST_P(QuicStreamFactoryTest, NoHttpsPoolingIfDisabled) {
715 MockRead reads[] = {
716 MockRead(ASYNC, OK, 0) // EOF
718 DeterministicSocketData socket_data1(reads, arraysize(reads), nullptr, 0);
719 DeterministicSocketData socket_data2(reads, arraysize(reads), nullptr, 0);
720 socket_factory_.AddSocketDataProvider(&socket_data1);
721 socket_factory_.AddSocketDataProvider(&socket_data2);
722 socket_data1.StopAfter(1);
723 socket_data2.StopAfter(1);
725 HostPortPair server1("www.example.org", 443);
726 HostPortPair server2("mail.example.org", 443);
728 // Load a cert that is valid for:
729 // www.example.org (server1)
730 // mail.example.org (server2)
731 // www.example.com
732 base::FilePath certs_dir = GetTestCertsDirectory();
733 scoped_refptr<X509Certificate> test_cert(
734 ImportCertFromFile(certs_dir, "spdy_pooling.pem"));
735 ASSERT_NE(static_cast<X509Certificate*>(nullptr), test_cert.get());
736 ProofVerifyDetailsChromium verify_details;
737 verify_details.cert_verify_result.verified_cert = test_cert;
738 verify_details.cert_verify_result.is_issued_by_known_root = true;
739 crypto_client_stream_factory_.set_proof_verify_details(&verify_details);
741 host_resolver_.set_synchronous_mode(true);
742 host_resolver_.rules()->AddIPLiteralRule(server1.host(), "192.168.0.1", "");
743 host_resolver_.rules()->AddIPLiteralRule(server2.host(), "192.168.0.1", "");
745 // Disable connection pooling.
746 QuicStreamFactoryPeer::DisableConnectionPooling(&factory_);
748 QuicStreamRequest request(&factory_);
749 is_https_ = true;
750 EXPECT_EQ(OK,
751 request.Request(server1,
752 is_https_,
753 privacy_mode_,
754 "GET",
755 net_log_,
756 callback_.callback()));
757 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
758 EXPECT_TRUE(stream.get());
760 TestCompletionCallback callback;
761 QuicStreamRequest request2(&factory_);
762 EXPECT_EQ(OK,
763 request2.Request(server2,
764 is_https_,
765 privacy_mode_,
766 "GET",
767 net_log_,
768 callback_.callback()));
769 scoped_ptr<QuicHttpStream> stream2 = request2.ReleaseStream();
770 EXPECT_TRUE(stream2.get());
772 EXPECT_NE(QuicStreamFactoryPeer::GetActiveSession(
773 &factory_, server1, is_https_),
774 QuicStreamFactoryPeer::GetActiveSession(
775 &factory_, server2, is_https_));
777 EXPECT_TRUE(socket_data1.at_read_eof());
778 EXPECT_TRUE(socket_data1.at_write_eof());
779 EXPECT_TRUE(socket_data2.at_read_eof());
780 EXPECT_TRUE(socket_data2.at_write_eof());
783 TEST_P(QuicStreamFactoryTest, NoHttpsPoolingWithCertMismatch) {
784 MockRead reads[] = {
785 MockRead(ASYNC, OK, 0) // EOF
787 DeterministicSocketData socket_data1(reads, arraysize(reads), nullptr, 0);
788 DeterministicSocketData socket_data2(reads, arraysize(reads), nullptr, 0);
789 socket_factory_.AddSocketDataProvider(&socket_data1);
790 socket_factory_.AddSocketDataProvider(&socket_data2);
791 socket_data1.StopAfter(1);
792 socket_data2.StopAfter(1);
794 HostPortPair server1("www.example.org", 443);
795 HostPortPair server2("mail.google.com", 443);
797 // Load a cert that is valid for:
798 // www.example.org (server1)
799 // mail.example.org
800 // www.example.com
801 // But is not valid for mail.google.com (server2).
802 base::FilePath certs_dir = GetTestCertsDirectory();
803 scoped_refptr<X509Certificate> test_cert(
804 ImportCertFromFile(certs_dir, "spdy_pooling.pem"));
805 ASSERT_NE(static_cast<X509Certificate*>(nullptr), test_cert.get());
806 ProofVerifyDetailsChromium verify_details;
807 verify_details.cert_verify_result.verified_cert = test_cert;
808 crypto_client_stream_factory_.set_proof_verify_details(&verify_details);
811 host_resolver_.set_synchronous_mode(true);
812 host_resolver_.rules()->AddIPLiteralRule(server1.host(), "192.168.0.1", "");
813 host_resolver_.rules()->AddIPLiteralRule(server2.host(), "192.168.0.1", "");
815 QuicStreamRequest request(&factory_);
816 is_https_ = true;
817 EXPECT_EQ(OK,
818 request.Request(server1,
819 is_https_,
820 privacy_mode_,
821 "GET",
822 net_log_,
823 callback_.callback()));
824 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
825 EXPECT_TRUE(stream.get());
827 TestCompletionCallback callback;
828 QuicStreamRequest request2(&factory_);
829 EXPECT_EQ(OK,
830 request2.Request(server2,
831 is_https_,
832 privacy_mode_,
833 "GET",
834 net_log_,
835 callback_.callback()));
836 scoped_ptr<QuicHttpStream> stream2 = request2.ReleaseStream();
837 EXPECT_TRUE(stream2.get());
839 EXPECT_NE(QuicStreamFactoryPeer::GetActiveSession(
840 &factory_, server1, is_https_),
841 QuicStreamFactoryPeer::GetActiveSession(
842 &factory_, server2, is_https_));
844 EXPECT_TRUE(socket_data1.at_read_eof());
845 EXPECT_TRUE(socket_data1.at_write_eof());
846 EXPECT_TRUE(socket_data2.at_read_eof());
847 EXPECT_TRUE(socket_data2.at_write_eof());
850 TEST_P(QuicStreamFactoryTest, HttpsPoolingWithMatchingPins) {
851 MockRead reads[] = {
852 MockRead(ASYNC, OK, 0) // EOF
854 DeterministicSocketData socket_data(reads, arraysize(reads), nullptr, 0);
855 socket_factory_.AddSocketDataProvider(&socket_data);
856 socket_data.StopAfter(1);
858 HostPortPair server1("www.example.org", 443);
859 HostPortPair server2("mail.example.org", 443);
860 uint8 primary_pin = 1;
861 uint8 backup_pin = 2;
862 test::AddPin(&transport_security_state_, "mail.example.org", primary_pin,
863 backup_pin);
865 // Load a cert that is valid for:
866 // www.example.org (server1)
867 // mail.example.org (server2)
868 base::FilePath certs_dir = GetTestCertsDirectory();
869 scoped_refptr<X509Certificate> test_cert(
870 ImportCertFromFile(certs_dir, "spdy_pooling.pem"));
871 ASSERT_NE(static_cast<X509Certificate*>(nullptr), test_cert.get());
872 ProofVerifyDetailsChromium verify_details;
873 verify_details.cert_verify_result.verified_cert = test_cert;
874 verify_details.cert_verify_result.is_issued_by_known_root = true;
875 verify_details.cert_verify_result.public_key_hashes.push_back(
876 test::GetTestHashValue(primary_pin));
877 crypto_client_stream_factory_.set_proof_verify_details(&verify_details);
880 host_resolver_.set_synchronous_mode(true);
881 host_resolver_.rules()->AddIPLiteralRule(server1.host(), "192.168.0.1", "");
882 host_resolver_.rules()->AddIPLiteralRule(server2.host(), "192.168.0.1", "");
884 QuicStreamRequest request(&factory_);
885 is_https_ = true;
886 EXPECT_EQ(OK,
887 request.Request(server1,
888 is_https_,
889 privacy_mode_,
890 "GET",
891 net_log_,
892 callback_.callback()));
893 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
894 EXPECT_TRUE(stream.get());
896 TestCompletionCallback callback;
897 QuicStreamRequest request2(&factory_);
898 EXPECT_EQ(OK,
899 request2.Request(server2,
900 is_https_,
901 privacy_mode_,
902 "GET",
903 net_log_,
904 callback_.callback()));
905 scoped_ptr<QuicHttpStream> stream2 = request2.ReleaseStream();
906 EXPECT_TRUE(stream2.get());
908 EXPECT_EQ(QuicStreamFactoryPeer::GetActiveSession(
909 &factory_, server1, is_https_),
910 QuicStreamFactoryPeer::GetActiveSession(
911 &factory_, server2, is_https_));
913 EXPECT_TRUE(socket_data.at_read_eof());
914 EXPECT_TRUE(socket_data.at_write_eof());
917 TEST_P(QuicStreamFactoryTest, NoHttpsPoolingWithMatchingPinsIfDisabled) {
918 MockRead reads[] = {
919 MockRead(ASYNC, OK, 0) // EOF
921 DeterministicSocketData socket_data1(reads, arraysize(reads), nullptr, 0);
922 DeterministicSocketData socket_data2(reads, arraysize(reads), nullptr, 0);
923 socket_factory_.AddSocketDataProvider(&socket_data1);
924 socket_factory_.AddSocketDataProvider(&socket_data2);
925 socket_data1.StopAfter(1);
926 socket_data2.StopAfter(1);
928 HostPortPair server1("www.example.org", 443);
929 HostPortPair server2("mail.example.org", 443);
930 uint8 primary_pin = 1;
931 uint8 backup_pin = 2;
932 test::AddPin(&transport_security_state_, "mail.example.org", primary_pin,
933 backup_pin);
935 // Load a cert that is valid for:
936 // www.example.org (server1)
937 // mail.example.org (server2)
938 base::FilePath certs_dir = GetTestCertsDirectory();
939 scoped_refptr<X509Certificate> test_cert(
940 ImportCertFromFile(certs_dir, "spdy_pooling.pem"));
941 ASSERT_NE(static_cast<X509Certificate*>(nullptr), test_cert.get());
942 ProofVerifyDetailsChromium verify_details;
943 verify_details.cert_verify_result.verified_cert = test_cert;
944 verify_details.cert_verify_result.is_issued_by_known_root = true;
945 verify_details.cert_verify_result.public_key_hashes.push_back(
946 test::GetTestHashValue(primary_pin));
947 crypto_client_stream_factory_.set_proof_verify_details(&verify_details);
950 host_resolver_.set_synchronous_mode(true);
951 host_resolver_.rules()->AddIPLiteralRule(server1.host(), "192.168.0.1", "");
952 host_resolver_.rules()->AddIPLiteralRule(server2.host(), "192.168.0.1", "");
954 // Disable connection pooling.
955 QuicStreamFactoryPeer::DisableConnectionPooling(&factory_);
957 QuicStreamRequest request(&factory_);
958 is_https_ = true;
959 EXPECT_EQ(OK,
960 request.Request(server1,
961 is_https_,
962 privacy_mode_,
963 "GET",
964 net_log_,
965 callback_.callback()));
966 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
967 EXPECT_TRUE(stream.get());
969 TestCompletionCallback callback;
970 QuicStreamRequest request2(&factory_);
971 EXPECT_EQ(OK,
972 request2.Request(server2,
973 is_https_,
974 privacy_mode_,
975 "GET",
976 net_log_,
977 callback_.callback()));
978 scoped_ptr<QuicHttpStream> stream2 = request2.ReleaseStream();
979 EXPECT_TRUE(stream2.get());
981 EXPECT_NE(QuicStreamFactoryPeer::GetActiveSession(
982 &factory_, server1, is_https_),
983 QuicStreamFactoryPeer::GetActiveSession(
984 &factory_, server2, is_https_));
986 EXPECT_TRUE(socket_data1.at_read_eof());
987 EXPECT_TRUE(socket_data1.at_write_eof());
988 EXPECT_TRUE(socket_data2.at_read_eof());
989 EXPECT_TRUE(socket_data2.at_write_eof());
992 TEST_P(QuicStreamFactoryTest, NoHttpsPoolingWithDifferentPins) {
993 MockRead reads[] = {
994 MockRead(ASYNC, OK, 0) // EOF
996 DeterministicSocketData socket_data1(reads, arraysize(reads), nullptr, 0);
997 DeterministicSocketData socket_data2(reads, arraysize(reads), nullptr, 0);
998 socket_factory_.AddSocketDataProvider(&socket_data1);
999 socket_factory_.AddSocketDataProvider(&socket_data2);
1000 socket_data1.StopAfter(1);
1001 socket_data2.StopAfter(1);
1003 HostPortPair server1("www.example.org", 443);
1004 HostPortPair server2("mail.example.org", 443);
1005 uint8 primary_pin = 1;
1006 uint8 backup_pin = 2;
1007 uint8 bad_pin = 3;
1008 test::AddPin(&transport_security_state_, "mail.example.org", primary_pin,
1009 backup_pin);
1011 // Load a cert that is valid for:
1012 // www.example.org (server1)
1013 // mail.example.org (server2)
1014 base::FilePath certs_dir = GetTestCertsDirectory();
1015 scoped_refptr<X509Certificate> test_cert(
1016 ImportCertFromFile(certs_dir, "spdy_pooling.pem"));
1017 ASSERT_NE(static_cast<X509Certificate*>(nullptr), test_cert.get());
1018 ProofVerifyDetailsChromium verify_details;
1019 verify_details.cert_verify_result.verified_cert = test_cert;
1020 verify_details.cert_verify_result.is_issued_by_known_root = true;
1021 verify_details.cert_verify_result.public_key_hashes.push_back(
1022 test::GetTestHashValue(bad_pin));
1023 crypto_client_stream_factory_.set_proof_verify_details(&verify_details);
1026 host_resolver_.set_synchronous_mode(true);
1027 host_resolver_.rules()->AddIPLiteralRule(server1.host(), "192.168.0.1", "");
1028 host_resolver_.rules()->AddIPLiteralRule(server2.host(), "192.168.0.1", "");
1030 QuicStreamRequest request(&factory_);
1031 is_https_ = true;
1032 EXPECT_EQ(OK,
1033 request.Request(server1,
1034 is_https_,
1035 privacy_mode_,
1036 "GET",
1037 net_log_,
1038 callback_.callback()));
1039 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
1040 EXPECT_TRUE(stream.get());
1042 TestCompletionCallback callback;
1043 QuicStreamRequest request2(&factory_);
1044 EXPECT_EQ(OK,
1045 request2.Request(server2,
1046 is_https_,
1047 privacy_mode_,
1048 "GET",
1049 net_log_,
1050 callback_.callback()));
1051 scoped_ptr<QuicHttpStream> stream2 = request2.ReleaseStream();
1052 EXPECT_TRUE(stream2.get());
1054 EXPECT_NE(QuicStreamFactoryPeer::GetActiveSession(
1055 &factory_, server1, is_https_),
1056 QuicStreamFactoryPeer::GetActiveSession(
1057 &factory_, server2, is_https_));
1059 EXPECT_TRUE(socket_data1.at_read_eof());
1060 EXPECT_TRUE(socket_data1.at_write_eof());
1061 EXPECT_TRUE(socket_data2.at_read_eof());
1062 EXPECT_TRUE(socket_data2.at_write_eof());
1065 TEST_P(QuicStreamFactoryTest, Goaway) {
1066 MockRead reads[] = {
1067 MockRead(ASYNC, OK, 0) // EOF
1069 DeterministicSocketData socket_data(reads, arraysize(reads), nullptr, 0);
1070 socket_data.StopAfter(1);
1071 socket_factory_.AddSocketDataProvider(&socket_data);
1072 DeterministicSocketData socket_data2(reads, arraysize(reads), nullptr, 0);
1073 socket_data2.StopAfter(1);
1074 socket_factory_.AddSocketDataProvider(&socket_data2);
1076 QuicStreamRequest request(&factory_);
1077 EXPECT_EQ(ERR_IO_PENDING,
1078 request.Request(host_port_pair_,
1079 is_https_,
1080 privacy_mode_,
1081 "GET",
1082 net_log_,
1083 callback_.callback()));
1085 EXPECT_EQ(OK, callback_.WaitForResult());
1086 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
1087 EXPECT_TRUE(stream.get());
1089 // Mark the session as going away. Ensure that while it is still alive
1090 // that it is no longer active.
1091 QuicClientSession* session = QuicStreamFactoryPeer::GetActiveSession(
1092 &factory_, host_port_pair_, is_https_);
1093 factory_.OnSessionGoingAway(session);
1094 EXPECT_EQ(true, QuicStreamFactoryPeer::IsLiveSession(&factory_, session));
1095 EXPECT_FALSE(QuicStreamFactoryPeer::HasActiveSession(
1096 &factory_, host_port_pair_, is_https_));
1097 EXPECT_EQ(nullptr, CreateIfSessionExists(host_port_pair_, net_log_).get());
1099 // Create a new request for the same destination and verify that a
1100 // new session is created.
1101 QuicStreamRequest request2(&factory_);
1102 EXPECT_EQ(ERR_IO_PENDING,
1103 request2.Request(host_port_pair_,
1104 is_https_,
1105 privacy_mode_,
1106 "GET",
1107 net_log_,
1108 callback_.callback()));
1109 EXPECT_EQ(OK, callback_.WaitForResult());
1110 scoped_ptr<QuicHttpStream> stream2 = request2.ReleaseStream();
1111 EXPECT_TRUE(stream2.get());
1113 EXPECT_TRUE(QuicStreamFactoryPeer::HasActiveSession(&factory_,
1114 host_port_pair_,
1115 is_https_));
1116 EXPECT_NE(session,
1117 QuicStreamFactoryPeer::GetActiveSession(
1118 &factory_, host_port_pair_, is_https_));
1119 EXPECT_EQ(true, QuicStreamFactoryPeer::IsLiveSession(&factory_, session));
1121 stream2.reset();
1122 stream.reset();
1124 EXPECT_TRUE(socket_data.at_read_eof());
1125 EXPECT_TRUE(socket_data.at_write_eof());
1126 EXPECT_TRUE(socket_data2.at_read_eof());
1127 EXPECT_TRUE(socket_data2.at_write_eof());
1130 TEST_P(QuicStreamFactoryTest, MaxOpenStream) {
1131 MockRead reads[] = {
1132 MockRead(ASYNC, OK, 0) // EOF
1134 QuicStreamId stream_id = kClientDataStreamId1;
1135 scoped_ptr<QuicEncryptedPacket> rst(
1136 maker_.MakeRstPacket(1, true, stream_id, QUIC_STREAM_CANCELLED));
1137 MockWrite writes[] = {
1138 MockWrite(ASYNC, rst->data(), rst->length(), 1),
1140 DeterministicSocketData socket_data(reads, arraysize(reads),
1141 writes, arraysize(writes));
1142 socket_factory_.AddSocketDataProvider(&socket_data);
1143 socket_data.StopAfter(1);
1145 HttpRequestInfo request_info;
1146 std::vector<QuicHttpStream*> streams;
1147 // The MockCryptoClientStream sets max_open_streams to be
1148 // kDefaultMaxStreamsPerConnection / 2.
1149 for (size_t i = 0; i < kDefaultMaxStreamsPerConnection / 2; i++) {
1150 QuicStreamRequest request(&factory_);
1151 int rv = request.Request(host_port_pair_,
1152 is_https_,
1153 privacy_mode_,
1154 "GET",
1155 net_log_,
1156 callback_.callback());
1157 if (i == 0) {
1158 EXPECT_EQ(ERR_IO_PENDING, rv);
1159 EXPECT_EQ(OK, callback_.WaitForResult());
1160 } else {
1161 EXPECT_EQ(OK, rv);
1163 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
1164 EXPECT_TRUE(stream);
1165 EXPECT_EQ(OK, stream->InitializeStream(
1166 &request_info, DEFAULT_PRIORITY, net_log_, CompletionCallback()));
1167 streams.push_back(stream.release());
1170 QuicStreamRequest request(&factory_);
1171 EXPECT_EQ(OK,
1172 request.Request(host_port_pair_,
1173 is_https_,
1174 privacy_mode_,
1175 "GET",
1176 net_log_,
1177 CompletionCallback()));
1178 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
1179 EXPECT_TRUE(stream);
1180 EXPECT_EQ(ERR_IO_PENDING, stream->InitializeStream(
1181 &request_info, DEFAULT_PRIORITY, net_log_, callback_.callback()));
1183 // Close the first stream.
1184 streams.front()->Close(false);
1186 ASSERT_TRUE(callback_.have_result());
1188 EXPECT_EQ(OK, callback_.WaitForResult());
1190 EXPECT_TRUE(socket_data.at_read_eof());
1191 EXPECT_TRUE(socket_data.at_write_eof());
1192 STLDeleteElements(&streams);
1195 TEST_P(QuicStreamFactoryTest, ResolutionErrorInCreate) {
1196 DeterministicSocketData socket_data(nullptr, 0, nullptr, 0);
1197 socket_factory_.AddSocketDataProvider(&socket_data);
1199 host_resolver_.rules()->AddSimulatedFailure(kDefaultServerHostName);
1201 QuicStreamRequest request(&factory_);
1202 EXPECT_EQ(ERR_IO_PENDING,
1203 request.Request(host_port_pair_,
1204 is_https_,
1205 privacy_mode_,
1206 "GET",
1207 net_log_,
1208 callback_.callback()));
1210 EXPECT_EQ(ERR_NAME_NOT_RESOLVED, callback_.WaitForResult());
1212 EXPECT_TRUE(socket_data.at_read_eof());
1213 EXPECT_TRUE(socket_data.at_write_eof());
1216 TEST_P(QuicStreamFactoryTest, ConnectErrorInCreate) {
1217 MockConnect connect(SYNCHRONOUS, ERR_ADDRESS_IN_USE);
1218 DeterministicSocketData socket_data(nullptr, 0, nullptr, 0);
1219 socket_data.set_connect_data(connect);
1220 socket_factory_.AddSocketDataProvider(&socket_data);
1221 socket_data.StopAfter(1);
1223 QuicStreamRequest request(&factory_);
1224 EXPECT_EQ(ERR_IO_PENDING,
1225 request.Request(host_port_pair_,
1226 is_https_,
1227 privacy_mode_,
1228 "GET",
1229 net_log_,
1230 callback_.callback()));
1232 EXPECT_EQ(ERR_ADDRESS_IN_USE, callback_.WaitForResult());
1234 EXPECT_TRUE(socket_data.at_read_eof());
1235 EXPECT_TRUE(socket_data.at_write_eof());
1238 TEST_P(QuicStreamFactoryTest, CancelCreate) {
1239 MockRead reads[] = {
1240 MockRead(ASYNC, OK, 0) // EOF
1242 DeterministicSocketData socket_data(reads, arraysize(reads), nullptr, 0);
1243 socket_factory_.AddSocketDataProvider(&socket_data);
1245 QuicStreamRequest request(&factory_);
1246 EXPECT_EQ(ERR_IO_PENDING,
1247 request.Request(host_port_pair_,
1248 is_https_,
1249 privacy_mode_,
1250 "GET",
1251 net_log_,
1252 callback_.callback()));
1255 socket_data.StopAfter(1);
1256 base::RunLoop run_loop;
1257 run_loop.RunUntilIdle();
1259 scoped_ptr<QuicHttpStream> stream(
1260 CreateIfSessionExists(host_port_pair_, net_log_));
1261 EXPECT_TRUE(stream.get());
1262 stream.reset();
1264 EXPECT_TRUE(socket_data.at_read_eof());
1265 EXPECT_TRUE(socket_data.at_write_eof());
1268 TEST_P(QuicStreamFactoryTest, CreateConsistentEphemeralPort) {
1269 // Sequentially connect to the default host, then another host, and then the
1270 // default host. Verify that the default host gets a consistent ephemeral
1271 // port, that is different from the other host's connection.
1273 std::string other_server_name = "other.google.com";
1274 EXPECT_NE(kDefaultServerHostName, other_server_name);
1275 HostPortPair host_port_pair2(other_server_name, kDefaultServerPort);
1277 int original_port = GetSourcePortForNewSession(host_port_pair_);
1278 EXPECT_NE(original_port, GetSourcePortForNewSession(host_port_pair2));
1279 EXPECT_EQ(original_port, GetSourcePortForNewSession(host_port_pair_));
1282 TEST_P(QuicStreamFactoryTest, GoAwayDisablesConsistentEphemeralPort) {
1283 // Get a session to the host using the port suggester.
1284 int original_port =
1285 GetSourcePortForNewSessionAndGoAway(host_port_pair_);
1286 // Verify that the port is different after the goaway.
1287 EXPECT_NE(original_port, GetSourcePortForNewSession(host_port_pair_));
1288 // Since the previous session did not goaway we should see the original port.
1289 EXPECT_EQ(original_port, GetSourcePortForNewSession(host_port_pair_));
1292 TEST_P(QuicStreamFactoryTest, CloseAllSessions) {
1293 MockRead reads[] = {
1294 MockRead(ASYNC, 0, 0) // EOF
1296 scoped_ptr<QuicEncryptedPacket> rst(ConstructRstPacket());
1297 std::vector<MockWrite> writes;
1298 writes.push_back(MockWrite(ASYNC, rst->data(), rst->length(), 1));
1299 DeterministicSocketData socket_data(reads, arraysize(reads),
1300 writes.empty() ? nullptr : &writes[0],
1301 writes.size());
1302 socket_factory_.AddSocketDataProvider(&socket_data);
1303 socket_data.StopAfter(1);
1305 MockRead reads2[] = {
1306 MockRead(ASYNC, 0, 0) // EOF
1308 DeterministicSocketData socket_data2(reads2, arraysize(reads2), nullptr, 0);
1309 socket_factory_.AddSocketDataProvider(&socket_data2);
1310 socket_data2.StopAfter(1);
1312 QuicStreamRequest request(&factory_);
1313 EXPECT_EQ(ERR_IO_PENDING,
1314 request.Request(host_port_pair_,
1315 is_https_,
1316 privacy_mode_,
1317 "GET",
1318 net_log_,
1319 callback_.callback()));
1321 EXPECT_EQ(OK, callback_.WaitForResult());
1322 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
1323 HttpRequestInfo request_info;
1324 EXPECT_EQ(OK, stream->InitializeStream(&request_info,
1325 DEFAULT_PRIORITY,
1326 net_log_, CompletionCallback()));
1328 // Close the session and verify that stream saw the error.
1329 factory_.CloseAllSessions(ERR_INTERNET_DISCONNECTED);
1330 EXPECT_EQ(ERR_INTERNET_DISCONNECTED,
1331 stream->ReadResponseHeaders(callback_.callback()));
1333 // Now attempting to request a stream to the same origin should create
1334 // a new session.
1336 QuicStreamRequest request2(&factory_);
1337 EXPECT_EQ(ERR_IO_PENDING,
1338 request2.Request(host_port_pair_,
1339 is_https_,
1340 privacy_mode_,
1341 "GET",
1342 net_log_,
1343 callback_.callback()));
1345 EXPECT_EQ(OK, callback_.WaitForResult());
1346 stream = request2.ReleaseStream();
1347 stream.reset(); // Will reset stream 3.
1349 EXPECT_TRUE(socket_data.at_read_eof());
1350 EXPECT_TRUE(socket_data.at_write_eof());
1351 EXPECT_TRUE(socket_data2.at_read_eof());
1352 EXPECT_TRUE(socket_data2.at_write_eof());
1355 TEST_P(QuicStreamFactoryTest, OnIPAddressChanged) {
1356 MockRead reads[] = {
1357 MockRead(ASYNC, 0, 0) // EOF
1359 scoped_ptr<QuicEncryptedPacket> rst(ConstructRstPacket());
1360 std::vector<MockWrite> writes;
1361 writes.push_back(MockWrite(ASYNC, rst->data(), rst->length(), 1));
1362 DeterministicSocketData socket_data(reads, arraysize(reads),
1363 writes.empty() ? nullptr : &writes[0],
1364 writes.size());
1365 socket_factory_.AddSocketDataProvider(&socket_data);
1366 socket_data.StopAfter(1);
1368 MockRead reads2[] = {
1369 MockRead(ASYNC, 0, 0) // EOF
1371 DeterministicSocketData socket_data2(reads2, arraysize(reads2), nullptr, 0);
1372 socket_factory_.AddSocketDataProvider(&socket_data2);
1373 socket_data2.StopAfter(1);
1375 QuicStreamRequest request(&factory_);
1376 EXPECT_EQ(ERR_IO_PENDING,
1377 request.Request(host_port_pair_,
1378 is_https_,
1379 privacy_mode_,
1380 "GET",
1381 net_log_,
1382 callback_.callback()));
1384 EXPECT_EQ(OK, callback_.WaitForResult());
1385 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
1386 HttpRequestInfo request_info;
1387 EXPECT_EQ(OK, stream->InitializeStream(&request_info,
1388 DEFAULT_PRIORITY,
1389 net_log_, CompletionCallback()));
1391 // Change the IP address and verify that stream saw the error.
1392 factory_.OnIPAddressChanged();
1393 EXPECT_EQ(ERR_NETWORK_CHANGED,
1394 stream->ReadResponseHeaders(callback_.callback()));
1395 EXPECT_TRUE(factory_.require_confirmation());
1397 // Now attempting to request a stream to the same origin should create
1398 // a new session.
1400 QuicStreamRequest request2(&factory_);
1401 EXPECT_EQ(ERR_IO_PENDING,
1402 request2.Request(host_port_pair_,
1403 is_https_,
1404 privacy_mode_,
1405 "GET",
1406 net_log_,
1407 callback_.callback()));
1409 EXPECT_EQ(OK, callback_.WaitForResult());
1410 stream = request2.ReleaseStream();
1411 stream.reset(); // Will reset stream 3.
1413 EXPECT_TRUE(socket_data.at_read_eof());
1414 EXPECT_TRUE(socket_data.at_write_eof());
1415 EXPECT_TRUE(socket_data2.at_read_eof());
1416 EXPECT_TRUE(socket_data2.at_write_eof());
1419 TEST_P(QuicStreamFactoryTest, OnCertAdded) {
1420 MockRead reads[] = {
1421 MockRead(ASYNC, 0, 0) // EOF
1423 scoped_ptr<QuicEncryptedPacket> rst(ConstructRstPacket());
1424 std::vector<MockWrite> writes;
1425 writes.push_back(MockWrite(ASYNC, rst->data(), rst->length(), 1));
1426 DeterministicSocketData socket_data(reads, arraysize(reads),
1427 writes.empty() ? nullptr : &writes[0],
1428 writes.size());
1429 socket_factory_.AddSocketDataProvider(&socket_data);
1430 socket_data.StopAfter(1);
1432 MockRead reads2[] = {
1433 MockRead(ASYNC, 0, 0) // EOF
1435 DeterministicSocketData socket_data2(reads2, arraysize(reads2), nullptr, 0);
1436 socket_factory_.AddSocketDataProvider(&socket_data2);
1437 socket_data2.StopAfter(1);
1439 QuicStreamRequest request(&factory_);
1440 EXPECT_EQ(ERR_IO_PENDING,
1441 request.Request(host_port_pair_,
1442 is_https_,
1443 privacy_mode_,
1444 "GET",
1445 net_log_,
1446 callback_.callback()));
1448 EXPECT_EQ(OK, callback_.WaitForResult());
1449 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
1450 HttpRequestInfo request_info;
1451 EXPECT_EQ(OK, stream->InitializeStream(&request_info,
1452 DEFAULT_PRIORITY,
1453 net_log_, CompletionCallback()));
1455 // Add a cert and verify that stream saw the event.
1456 factory_.OnCertAdded(nullptr);
1457 EXPECT_EQ(ERR_CERT_DATABASE_CHANGED,
1458 stream->ReadResponseHeaders(callback_.callback()));
1459 EXPECT_FALSE(factory_.require_confirmation());
1461 // Now attempting to request a stream to the same origin should create
1462 // a new session.
1464 QuicStreamRequest request2(&factory_);
1465 EXPECT_EQ(ERR_IO_PENDING,
1466 request2.Request(host_port_pair_,
1467 is_https_,
1468 privacy_mode_,
1469 "GET",
1470 net_log_,
1471 callback_.callback()));
1473 EXPECT_EQ(OK, callback_.WaitForResult());
1474 stream = request2.ReleaseStream();
1475 stream.reset(); // Will reset stream 3.
1477 EXPECT_TRUE(socket_data.at_read_eof());
1478 EXPECT_TRUE(socket_data.at_write_eof());
1479 EXPECT_TRUE(socket_data2.at_read_eof());
1480 EXPECT_TRUE(socket_data2.at_write_eof());
1483 TEST_P(QuicStreamFactoryTest, OnCACertChanged) {
1484 MockRead reads[] = {
1485 MockRead(ASYNC, 0, 0) // EOF
1487 scoped_ptr<QuicEncryptedPacket> rst(ConstructRstPacket());
1488 std::vector<MockWrite> writes;
1489 writes.push_back(MockWrite(ASYNC, rst->data(), rst->length(), 1));
1490 DeterministicSocketData socket_data(reads, arraysize(reads),
1491 writes.empty() ? nullptr : &writes[0],
1492 writes.size());
1493 socket_factory_.AddSocketDataProvider(&socket_data);
1494 socket_data.StopAfter(1);
1496 MockRead reads2[] = {
1497 MockRead(ASYNC, 0, 0) // EOF
1499 DeterministicSocketData socket_data2(reads2, arraysize(reads2), nullptr, 0);
1500 socket_factory_.AddSocketDataProvider(&socket_data2);
1501 socket_data2.StopAfter(1);
1503 QuicStreamRequest request(&factory_);
1504 EXPECT_EQ(ERR_IO_PENDING,
1505 request.Request(host_port_pair_,
1506 is_https_,
1507 privacy_mode_,
1508 "GET",
1509 net_log_,
1510 callback_.callback()));
1512 EXPECT_EQ(OK, callback_.WaitForResult());
1513 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
1514 HttpRequestInfo request_info;
1515 EXPECT_EQ(OK, stream->InitializeStream(&request_info,
1516 DEFAULT_PRIORITY,
1517 net_log_, CompletionCallback()));
1519 // Change the CA cert and verify that stream saw the event.
1520 factory_.OnCACertChanged(nullptr);
1521 EXPECT_EQ(ERR_CERT_DATABASE_CHANGED,
1522 stream->ReadResponseHeaders(callback_.callback()));
1523 EXPECT_FALSE(factory_.require_confirmation());
1525 // Now attempting to request a stream to the same origin should create
1526 // a new session.
1528 QuicStreamRequest request2(&factory_);
1529 EXPECT_EQ(ERR_IO_PENDING,
1530 request2.Request(host_port_pair_,
1531 is_https_,
1532 privacy_mode_,
1533 "GET",
1534 net_log_,
1535 callback_.callback()));
1537 EXPECT_EQ(OK, callback_.WaitForResult());
1538 stream = request2.ReleaseStream();
1539 stream.reset(); // Will reset stream 3.
1541 EXPECT_TRUE(socket_data.at_read_eof());
1542 EXPECT_TRUE(socket_data.at_write_eof());
1543 EXPECT_TRUE(socket_data2.at_read_eof());
1544 EXPECT_TRUE(socket_data2.at_write_eof());
1547 TEST_P(QuicStreamFactoryTest, SharedCryptoConfig) {
1548 vector<string> cannoncial_suffixes;
1549 cannoncial_suffixes.push_back(string(".c.youtube.com"));
1550 cannoncial_suffixes.push_back(string(".googlevideo.com"));
1552 for (unsigned i = 0; i < cannoncial_suffixes.size(); ++i) {
1553 string r1_host_name("r1");
1554 string r2_host_name("r2");
1555 r1_host_name.append(cannoncial_suffixes[i]);
1556 r2_host_name.append(cannoncial_suffixes[i]);
1558 HostPortPair host_port_pair1(r1_host_name, 80);
1559 QuicCryptoClientConfig* crypto_config =
1560 QuicStreamFactoryPeer::GetCryptoConfig(&factory_);
1561 QuicServerId server_id1(host_port_pair1, is_https_, privacy_mode_);
1562 QuicCryptoClientConfig::CachedState* cached1 =
1563 crypto_config->LookupOrCreate(server_id1);
1564 EXPECT_FALSE(cached1->proof_valid());
1565 EXPECT_TRUE(cached1->source_address_token().empty());
1567 // Mutate the cached1 to have different data.
1568 // TODO(rtenneti): mutate other members of CachedState.
1569 cached1->set_source_address_token(r1_host_name);
1570 cached1->SetProofValid();
1572 HostPortPair host_port_pair2(r2_host_name, 80);
1573 QuicServerId server_id2(host_port_pair2, is_https_, privacy_mode_);
1574 QuicCryptoClientConfig::CachedState* cached2 =
1575 crypto_config->LookupOrCreate(server_id2);
1576 EXPECT_EQ(cached1->source_address_token(), cached2->source_address_token());
1577 EXPECT_TRUE(cached2->proof_valid());
1581 TEST_P(QuicStreamFactoryTest, CryptoConfigWhenProofIsInvalid) {
1582 vector<string> cannoncial_suffixes;
1583 cannoncial_suffixes.push_back(string(".c.youtube.com"));
1584 cannoncial_suffixes.push_back(string(".googlevideo.com"));
1586 for (unsigned i = 0; i < cannoncial_suffixes.size(); ++i) {
1587 string r3_host_name("r3");
1588 string r4_host_name("r4");
1589 r3_host_name.append(cannoncial_suffixes[i]);
1590 r4_host_name.append(cannoncial_suffixes[i]);
1592 HostPortPair host_port_pair1(r3_host_name, 80);
1593 QuicCryptoClientConfig* crypto_config =
1594 QuicStreamFactoryPeer::GetCryptoConfig(&factory_);
1595 QuicServerId server_id1(host_port_pair1, is_https_, privacy_mode_);
1596 QuicCryptoClientConfig::CachedState* cached1 =
1597 crypto_config->LookupOrCreate(server_id1);
1598 EXPECT_FALSE(cached1->proof_valid());
1599 EXPECT_TRUE(cached1->source_address_token().empty());
1601 // Mutate the cached1 to have different data.
1602 // TODO(rtenneti): mutate other members of CachedState.
1603 cached1->set_source_address_token(r3_host_name);
1604 cached1->SetProofInvalid();
1606 HostPortPair host_port_pair2(r4_host_name, 80);
1607 QuicServerId server_id2(host_port_pair2, is_https_, privacy_mode_);
1608 QuicCryptoClientConfig::CachedState* cached2 =
1609 crypto_config->LookupOrCreate(server_id2);
1610 EXPECT_NE(cached1->source_address_token(), cached2->source_address_token());
1611 EXPECT_TRUE(cached2->source_address_token().empty());
1612 EXPECT_FALSE(cached2->proof_valid());
1616 TEST_P(QuicStreamFactoryTest, CancelWaitForDataReady) {
1617 // Don't race quic connections when testing cancel reading of server config
1618 // from disk cache.
1619 if (GetParam().enable_connection_racing)
1620 return;
1621 factory_.set_quic_server_info_factory(&quic_server_info_factory_);
1622 QuicStreamFactoryPeer::SetTaskRunner(&factory_, runner_.get());
1623 const size_t kLoadServerInfoTimeoutMs = 50;
1624 QuicStreamFactoryPeer::SetLoadServerInfoTimeout(
1625 &factory_, kLoadServerInfoTimeoutMs);
1627 MockRead reads[] = {
1628 MockRead(ASYNC, OK, 0) // EOF
1630 DeterministicSocketData socket_data(reads, arraysize(reads), nullptr, 0);
1631 socket_factory_.AddSocketDataProvider(&socket_data);
1632 socket_data.StopAfter(1);
1634 crypto_client_stream_factory_.set_handshake_mode(
1635 MockCryptoClientStream::ZERO_RTT);
1636 host_resolver_.set_synchronous_mode(true);
1637 host_resolver_.rules()->AddIPLiteralRule(host_port_pair_.host(),
1638 "192.168.0.1", "");
1640 QuicStreamRequest request(&factory_);
1641 EXPECT_EQ(ERR_IO_PENDING,
1642 request.Request(host_port_pair_,
1643 is_https_,
1644 privacy_mode_,
1645 "GET",
1646 net_log_,
1647 callback_.callback()));
1649 // Verify that the CancelWaitForDataReady task has been posted.
1650 ASSERT_EQ(1u, runner_->GetPostedTasks().size());
1651 EXPECT_EQ(base::TimeDelta::FromMilliseconds(kLoadServerInfoTimeoutMs),
1652 runner_->GetPostedTasks()[0].delay);
1654 runner_->RunNextTask();
1655 ASSERT_EQ(0u, runner_->GetPostedTasks().size());
1657 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
1658 EXPECT_TRUE(stream.get());
1659 EXPECT_TRUE(socket_data.at_read_eof());
1660 EXPECT_TRUE(socket_data.at_write_eof());
1663 TEST_P(QuicStreamFactoryTest, RacingConnections) {
1664 if (!GetParam().enable_connection_racing)
1665 return;
1666 factory_.set_quic_server_info_factory(&quic_server_info_factory_);
1667 QuicStreamFactoryPeer::SetTaskRunner(&factory_, runner_.get());
1668 const size_t kLoadServerInfoTimeoutMs = 50;
1669 QuicStreamFactoryPeer::SetLoadServerInfoTimeout(&factory_,
1670 kLoadServerInfoTimeoutMs);
1672 MockRead reads[] = {
1673 MockRead(ASYNC, OK, 0) // EOF
1675 DeterministicSocketData socket_data(reads, arraysize(reads), nullptr, 0);
1676 socket_factory_.AddSocketDataProvider(&socket_data);
1677 socket_data.StopAfter(1);
1679 MockRead reads2[] = {
1680 MockRead(ASYNC, 0, 0) // EOF
1682 DeterministicSocketData socket_data2(reads2, arraysize(reads2), nullptr, 0);
1683 socket_factory_.AddSocketDataProvider(&socket_data2);
1684 socket_data2.StopAfter(1);
1686 crypto_client_stream_factory_.set_handshake_mode(
1687 MockCryptoClientStream::ZERO_RTT);
1688 host_resolver_.set_synchronous_mode(true);
1689 host_resolver_.rules()->AddIPLiteralRule(host_port_pair_.host(),
1690 "192.168.0.1", "");
1692 QuicStreamRequest request(&factory_);
1693 QuicServerId server_id(host_port_pair_, is_https_, privacy_mode_);
1694 EXPECT_EQ(ERR_IO_PENDING,
1695 request.Request(host_port_pair_, is_https_, privacy_mode_, "GET",
1696 net_log_, callback_.callback()));
1697 EXPECT_EQ(2u,
1698 QuicStreamFactoryPeer::GetNumberOfActiveJobs(&factory_, server_id));
1700 runner_->RunNextTask();
1702 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
1703 EXPECT_TRUE(stream.get());
1704 EXPECT_TRUE(socket_data.at_read_eof());
1705 EXPECT_TRUE(socket_data.at_write_eof());
1706 EXPECT_EQ(0u,
1707 QuicStreamFactoryPeer::GetNumberOfActiveJobs(&factory_, server_id));
1710 TEST_P(QuicStreamFactoryTest, EnableNotLoadFromDiskCache) {
1711 factory_.set_quic_server_info_factory(&quic_server_info_factory_);
1712 QuicStreamFactoryPeer::SetTaskRunner(&factory_, runner_.get());
1713 const size_t kLoadServerInfoTimeoutMs = 50;
1714 QuicStreamFactoryPeer::SetLoadServerInfoTimeout(&factory_,
1715 kLoadServerInfoTimeoutMs);
1716 QuicStreamFactoryPeer::SetDisableDiskCache(&factory_, true);
1718 MockRead reads[] = {
1719 MockRead(ASYNC, OK, 0) // EOF
1721 DeterministicSocketData socket_data(reads, arraysize(reads), nullptr, 0);
1722 socket_factory_.AddSocketDataProvider(&socket_data);
1723 socket_data.StopAfter(1);
1725 crypto_client_stream_factory_.set_handshake_mode(
1726 MockCryptoClientStream::ZERO_RTT);
1727 host_resolver_.set_synchronous_mode(true);
1728 host_resolver_.rules()->AddIPLiteralRule(host_port_pair_.host(),
1729 "192.168.0.1", "");
1731 QuicStreamRequest request(&factory_);
1732 EXPECT_EQ(OK, request.Request(host_port_pair_, is_https_, privacy_mode_,
1733 "GET", net_log_, callback_.callback()));
1735 // If we are waiting for disk cache, we would have posted a task. Verify that
1736 // the CancelWaitForDataReady task hasn't been posted.
1737 ASSERT_EQ(0u, runner_->GetPostedTasks().size());
1739 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
1740 EXPECT_TRUE(stream.get());
1741 EXPECT_TRUE(socket_data.at_read_eof());
1742 EXPECT_TRUE(socket_data.at_write_eof());
1745 } // namespace test
1746 } // namespace net