Land Recent QUIC Changes.
[chromium-blink-merge.git] / net / quic / quic_stream_factory_test.cc
blob341ecb0a69e0e53c90a03258e40b555d10c97623
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/quic_http_stream.h"
21 #include "net/quic/quic_server_id.h"
22 #include "net/quic/test_tools/mock_clock.h"
23 #include "net/quic/test_tools/mock_crypto_client_stream_factory.h"
24 #include "net/quic/test_tools/mock_random.h"
25 #include "net/quic/test_tools/quic_test_packet_maker.h"
26 #include "net/quic/test_tools/quic_test_utils.h"
27 #include "net/socket/socket_test_util.h"
28 #include "net/spdy/spdy_test_utils.h"
29 #include "net/ssl/channel_id_service.h"
30 #include "net/ssl/default_channel_id_store.h"
31 #include "net/test/cert_test_util.h"
32 #include "testing/gtest/include/gtest/gtest.h"
34 using base::StringPiece;
35 using std::string;
36 using std::vector;
38 namespace net {
39 namespace test {
41 namespace {
42 const char kDefaultServerHostName[] = "www.google.com";
43 const int kDefaultServerPort = 443;
44 } // namespace anonymous
46 class QuicStreamFactoryPeer {
47 public:
48 static QuicCryptoClientConfig* GetCryptoConfig(QuicStreamFactory* factory) {
49 return &factory->crypto_config_;
52 static bool HasActiveSession(QuicStreamFactory* factory,
53 const HostPortPair& host_port_pair,
54 bool is_https) {
55 QuicServerId server_id(host_port_pair, is_https, PRIVACY_MODE_DISABLED);
56 return factory->HasActiveSession(server_id);
59 static QuicClientSession* GetActiveSession(
60 QuicStreamFactory* factory,
61 const HostPortPair& host_port_pair,
62 bool is_https) {
63 QuicServerId server_id(host_port_pair, is_https, PRIVACY_MODE_DISABLED);
64 DCHECK(factory->HasActiveSession(server_id));
65 return factory->active_sessions_[server_id];
68 static scoped_ptr<QuicHttpStream> CreateIfSessionExists(
69 QuicStreamFactory* factory,
70 const HostPortPair& host_port_pair,
71 bool is_https,
72 const BoundNetLog& net_log) {
73 QuicServerId server_id(host_port_pair, is_https, PRIVACY_MODE_DISABLED);
74 return factory->CreateIfSessionExists(server_id, net_log);
77 static bool IsLiveSession(QuicStreamFactory* factory,
78 QuicClientSession* session) {
79 for (QuicStreamFactory::SessionIdMap::iterator it =
80 factory->all_sessions_.begin();
81 it != factory->all_sessions_.end(); ++it) {
82 if (it->first == session)
83 return true;
85 return false;
88 static void DisableConnectionPooling(QuicStreamFactory* factory) {
89 factory->disable_connection_pooling_ = true;
93 class QuicStreamFactoryTest : public ::testing::TestWithParam<QuicVersion> {
94 protected:
95 QuicStreamFactoryTest()
96 : random_generator_(0),
97 clock_(new MockClock()),
98 maker_(GetParam(), 0, clock_),
99 cert_verifier_(CertVerifier::CreateDefault()),
100 channel_id_service_(
101 new ChannelIDService(new DefaultChannelIDStore(NULL),
102 base::MessageLoopProxy::current())),
103 factory_(&host_resolver_,
104 &socket_factory_,
105 base::WeakPtr<HttpServerProperties>(),
106 cert_verifier_.get(),
107 channel_id_service_.get(),
108 &transport_security_state_,
109 &crypto_client_stream_factory_,
110 &random_generator_,
111 clock_,
112 kDefaultMaxPacketSize,
113 std::string(),
114 SupportedVersions(GetParam()),
115 /*enable_port_selection=*/true,
116 /*always_require_handshake_confirmation=*/false,
117 /*disable_connection_pooling=*/false,
118 QuicTagVector()),
119 host_port_pair_(kDefaultServerHostName, kDefaultServerPort),
120 is_https_(false),
121 privacy_mode_(PRIVACY_MODE_DISABLED) {
122 factory_.set_require_confirmation(false);
123 clock_->AdvanceTime(QuicTime::Delta::FromSeconds(1));
126 scoped_ptr<QuicHttpStream> CreateIfSessionExists(
127 const HostPortPair& host_port_pair,
128 const BoundNetLog& net_log) {
129 return QuicStreamFactoryPeer::CreateIfSessionExists(
130 &factory_, host_port_pair, false, net_log_);
133 int GetSourcePortForNewSession(const HostPortPair& destination) {
134 return GetSourcePortForNewSessionInner(destination, false);
137 int GetSourcePortForNewSessionAndGoAway(
138 const HostPortPair& destination) {
139 return GetSourcePortForNewSessionInner(destination, true);
142 int GetSourcePortForNewSessionInner(const HostPortPair& destination,
143 bool goaway_received) {
144 // Should only be called if there is no active session for this destination.
145 EXPECT_EQ(NULL, CreateIfSessionExists(destination, net_log_).get());
146 size_t socket_count = socket_factory_.udp_client_sockets().size();
148 MockRead reads[] = {
149 MockRead(ASYNC, OK, 0) // EOF
151 DeterministicSocketData socket_data(reads, arraysize(reads), NULL, 0);
152 socket_data.StopAfter(1);
153 socket_factory_.AddSocketDataProvider(&socket_data);
155 QuicStreamRequest request(&factory_);
156 EXPECT_EQ(ERR_IO_PENDING,
157 request.Request(destination,
158 is_https_,
159 privacy_mode_,
160 "GET",
161 net_log_,
162 callback_.callback()));
164 EXPECT_EQ(OK, callback_.WaitForResult());
165 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
166 EXPECT_TRUE(stream.get());
167 stream.reset();
169 QuicClientSession* session = QuicStreamFactoryPeer::GetActiveSession(
170 &factory_, destination, is_https_);
172 if (socket_count + 1 != socket_factory_.udp_client_sockets().size()) {
173 EXPECT_TRUE(false);
174 return 0;
177 IPEndPoint endpoint;
178 socket_factory_.
179 udp_client_sockets()[socket_count]->GetLocalAddress(&endpoint);
180 int port = endpoint.port();
181 if (goaway_received) {
182 QuicGoAwayFrame goaway(QUIC_NO_ERROR, 1, "");
183 session->OnGoAway(goaway);
186 factory_.OnSessionClosed(session);
187 EXPECT_EQ(NULL, CreateIfSessionExists(destination, net_log_).get());
188 EXPECT_TRUE(socket_data.at_read_eof());
189 EXPECT_TRUE(socket_data.at_write_eof());
190 return port;
193 scoped_ptr<QuicEncryptedPacket> ConstructRstPacket() {
194 QuicStreamId stream_id = kClientDataStreamId1;
195 return maker_.MakeRstPacket(
196 1, true, stream_id,
197 AdjustErrorForVersion(QUIC_RST_FLOW_CONTROL_ACCOUNTING, GetParam()));
200 MockHostResolver host_resolver_;
201 DeterministicMockClientSocketFactory socket_factory_;
202 MockCryptoClientStreamFactory crypto_client_stream_factory_;
203 MockRandom random_generator_;
204 MockClock* clock_; // Owned by factory_.
205 QuicTestPacketMaker maker_;
206 scoped_ptr<CertVerifier> cert_verifier_;
207 scoped_ptr<ChannelIDService> channel_id_service_;
208 TransportSecurityState transport_security_state_;
209 QuicStreamFactory factory_;
210 HostPortPair host_port_pair_;
211 bool is_https_;
212 PrivacyMode privacy_mode_;
213 BoundNetLog net_log_;
214 TestCompletionCallback callback_;
217 INSTANTIATE_TEST_CASE_P(Version, QuicStreamFactoryTest,
218 ::testing::ValuesIn(QuicSupportedVersions()));
220 TEST_P(QuicStreamFactoryTest, CreateIfSessionExists) {
221 EXPECT_EQ(NULL, CreateIfSessionExists(host_port_pair_, net_log_).get());
224 TEST_P(QuicStreamFactoryTest, Create) {
225 MockRead reads[] = {
226 MockRead(ASYNC, OK, 0) // EOF
228 DeterministicSocketData socket_data(reads, arraysize(reads), NULL, 0);
229 socket_factory_.AddSocketDataProvider(&socket_data);
230 socket_data.StopAfter(1);
232 QuicStreamRequest request(&factory_);
233 EXPECT_EQ(ERR_IO_PENDING,
234 request.Request(host_port_pair_,
235 is_https_,
236 privacy_mode_,
237 "GET",
238 net_log_,
239 callback_.callback()));
241 EXPECT_EQ(OK, callback_.WaitForResult());
242 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
243 EXPECT_TRUE(stream.get());
245 // Will reset stream 3.
246 stream = CreateIfSessionExists(host_port_pair_, net_log_);
247 EXPECT_TRUE(stream.get());
249 // TODO(rtenneti): We should probably have a tests that HTTP and HTTPS result
250 // in streams on different sessions.
251 QuicStreamRequest request2(&factory_);
252 EXPECT_EQ(OK,
253 request2.Request(host_port_pair_,
254 is_https_,
255 privacy_mode_,
256 "GET",
257 net_log_,
258 callback_.callback()));
259 stream = request2.ReleaseStream(); // Will reset stream 5.
260 stream.reset(); // Will reset stream 7.
262 EXPECT_TRUE(socket_data.at_read_eof());
263 EXPECT_TRUE(socket_data.at_write_eof());
266 TEST_P(QuicStreamFactoryTest, CreateZeroRtt) {
267 MockRead reads[] = {
268 MockRead(ASYNC, OK, 0) // EOF
270 DeterministicSocketData socket_data(reads, arraysize(reads), NULL, 0);
271 socket_factory_.AddSocketDataProvider(&socket_data);
272 socket_data.StopAfter(1);
274 crypto_client_stream_factory_.set_handshake_mode(
275 MockCryptoClientStream::ZERO_RTT);
276 host_resolver_.set_synchronous_mode(true);
277 host_resolver_.rules()->AddIPLiteralRule(host_port_pair_.host(),
278 "192.168.0.1", "");
280 QuicStreamRequest request(&factory_);
281 EXPECT_EQ(OK,
282 request.Request(host_port_pair_,
283 is_https_,
284 privacy_mode_,
285 "GET",
286 net_log_,
287 callback_.callback()));
289 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
290 EXPECT_TRUE(stream.get());
291 EXPECT_TRUE(socket_data.at_read_eof());
292 EXPECT_TRUE(socket_data.at_write_eof());
295 TEST_P(QuicStreamFactoryTest, CreateZeroRttPost) {
296 MockRead reads[] = {
297 MockRead(ASYNC, OK, 0) // EOF
299 DeterministicSocketData socket_data(reads, arraysize(reads), NULL, 0);
300 socket_factory_.AddSocketDataProvider(&socket_data);
301 socket_data.StopAfter(1);
303 crypto_client_stream_factory_.set_handshake_mode(
304 MockCryptoClientStream::ZERO_RTT);
305 host_resolver_.set_synchronous_mode(true);
306 host_resolver_.rules()->AddIPLiteralRule(host_port_pair_.host(),
307 "192.168.0.1", "");
309 QuicStreamRequest request(&factory_);
310 // Posts require handshake confirmation, so this will return asynchronously.
311 EXPECT_EQ(ERR_IO_PENDING,
312 request.Request(host_port_pair_,
313 is_https_,
314 privacy_mode_,
315 "POST",
316 net_log_,
317 callback_.callback()));
319 // Confirm the handshake and verify that the stream is created.
320 crypto_client_stream_factory_.last_stream()->SendOnCryptoHandshakeEvent(
321 QuicSession::HANDSHAKE_CONFIRMED);
323 EXPECT_EQ(OK, callback_.WaitForResult());
324 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
325 EXPECT_TRUE(stream.get());
326 EXPECT_TRUE(socket_data.at_read_eof());
327 EXPECT_TRUE(socket_data.at_write_eof());
330 TEST_P(QuicStreamFactoryTest, CreateHttpVsHttps) {
331 MockRead reads[] = {
332 MockRead(ASYNC, OK, 0) // EOF
334 DeterministicSocketData socket_data1(reads, arraysize(reads), NULL, 0);
335 DeterministicSocketData socket_data2(reads, arraysize(reads), NULL, 0);
336 socket_factory_.AddSocketDataProvider(&socket_data1);
337 socket_factory_.AddSocketDataProvider(&socket_data2);
338 socket_data1.StopAfter(1);
339 socket_data2.StopAfter(1);
341 QuicStreamRequest request(&factory_);
342 EXPECT_EQ(ERR_IO_PENDING,
343 request.Request(host_port_pair_,
344 is_https_,
345 privacy_mode_,
346 "GET",
347 net_log_,
348 callback_.callback()));
350 EXPECT_EQ(OK, callback_.WaitForResult());
351 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
352 EXPECT_TRUE(stream.get());
354 QuicStreamRequest request2(&factory_);
355 EXPECT_EQ(ERR_IO_PENDING,
356 request2.Request(host_port_pair_,
357 !is_https_,
358 privacy_mode_,
359 "GET",
360 net_log_,
361 callback_.callback()));
362 EXPECT_EQ(OK, callback_.WaitForResult());
363 stream = request2.ReleaseStream();
364 EXPECT_TRUE(stream.get());
365 stream.reset();
367 EXPECT_NE(QuicStreamFactoryPeer::GetActiveSession(
368 &factory_, host_port_pair_, is_https_),
369 QuicStreamFactoryPeer::GetActiveSession(
370 &factory_, host_port_pair_, !is_https_));
372 EXPECT_TRUE(socket_data1.at_read_eof());
373 EXPECT_TRUE(socket_data1.at_write_eof());
374 EXPECT_TRUE(socket_data2.at_read_eof());
375 EXPECT_TRUE(socket_data2.at_write_eof());
378 TEST_P(QuicStreamFactoryTest, Pooling) {
379 MockRead reads[] = {
380 MockRead(ASYNC, OK, 0) // EOF
382 DeterministicSocketData socket_data(reads, arraysize(reads), NULL, 0);
383 socket_factory_.AddSocketDataProvider(&socket_data);
384 socket_data.StopAfter(1);
386 HostPortPair server2("mail.google.com", kDefaultServerPort);
387 host_resolver_.set_synchronous_mode(true);
388 host_resolver_.rules()->AddIPLiteralRule(
389 kDefaultServerHostName, "192.168.0.1", "");
390 host_resolver_.rules()->AddIPLiteralRule(
391 "mail.google.com", "192.168.0.1", "");
393 QuicStreamRequest request(&factory_);
394 EXPECT_EQ(OK,
395 request.Request(host_port_pair_,
396 is_https_,
397 privacy_mode_,
398 "GET",
399 net_log_,
400 callback_.callback()));
401 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
402 EXPECT_TRUE(stream.get());
404 TestCompletionCallback callback;
405 QuicStreamRequest request2(&factory_);
406 EXPECT_EQ(OK,
407 request2.Request(server2,
408 is_https_,
409 privacy_mode_,
410 "GET",
411 net_log_,
412 callback.callback()));
413 scoped_ptr<QuicHttpStream> stream2 = request2.ReleaseStream();
414 EXPECT_TRUE(stream2.get());
416 EXPECT_EQ(
417 QuicStreamFactoryPeer::GetActiveSession(
418 &factory_, host_port_pair_, is_https_),
419 QuicStreamFactoryPeer::GetActiveSession(&factory_, server2, is_https_));
421 EXPECT_TRUE(socket_data.at_read_eof());
422 EXPECT_TRUE(socket_data.at_write_eof());
425 TEST_P(QuicStreamFactoryTest, NoPoolingIfDisabled) {
426 MockRead reads[] = {
427 MockRead(ASYNC, OK, 0) // EOF
429 DeterministicSocketData socket_data1(reads, arraysize(reads), NULL, 0);
430 DeterministicSocketData socket_data2(reads, arraysize(reads), NULL, 0);
431 socket_factory_.AddSocketDataProvider(&socket_data1);
432 socket_factory_.AddSocketDataProvider(&socket_data2);
433 socket_data1.StopAfter(1);
434 socket_data2.StopAfter(1);
436 HostPortPair server2("mail.google.com", kDefaultServerPort);
437 host_resolver_.set_synchronous_mode(true);
438 host_resolver_.rules()->AddIPLiteralRule(
439 kDefaultServerHostName, "192.168.0.1", "");
440 host_resolver_.rules()->AddIPLiteralRule(
441 "mail.google.com", "192.168.0.1", "");
443 // Disable connection pooling.
444 QuicStreamFactoryPeer::DisableConnectionPooling(&factory_);
446 QuicStreamRequest request(&factory_);
447 EXPECT_EQ(OK,
448 request.Request(host_port_pair_,
449 is_https_,
450 privacy_mode_,
451 "GET",
452 net_log_,
453 callback_.callback()));
454 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
455 EXPECT_TRUE(stream.get());
457 TestCompletionCallback callback;
458 QuicStreamRequest request2(&factory_);
459 EXPECT_EQ(OK,
460 request2.Request(server2,
461 is_https_,
462 privacy_mode_,
463 "GET",
464 net_log_,
465 callback.callback()));
466 scoped_ptr<QuicHttpStream> stream2 = request2.ReleaseStream();
467 EXPECT_TRUE(stream2.get());
469 EXPECT_NE(
470 QuicStreamFactoryPeer::GetActiveSession(
471 &factory_, host_port_pair_, is_https_),
472 QuicStreamFactoryPeer::GetActiveSession(&factory_, server2, is_https_));
474 EXPECT_TRUE(socket_data1.at_read_eof());
475 EXPECT_TRUE(socket_data1.at_write_eof());
476 EXPECT_TRUE(socket_data2.at_read_eof());
477 EXPECT_TRUE(socket_data2.at_write_eof());
480 TEST_P(QuicStreamFactoryTest, NoPoolingAfterGoAway) {
481 MockRead reads[] = {
482 MockRead(ASYNC, OK, 0) // EOF
484 DeterministicSocketData socket_data1(reads, arraysize(reads), NULL, 0);
485 DeterministicSocketData socket_data2(reads, arraysize(reads), NULL, 0);
486 socket_factory_.AddSocketDataProvider(&socket_data1);
487 socket_factory_.AddSocketDataProvider(&socket_data2);
488 socket_data1.StopAfter(1);
489 socket_data2.StopAfter(1);
491 HostPortPair server2("mail.google.com", kDefaultServerPort);
492 host_resolver_.set_synchronous_mode(true);
493 host_resolver_.rules()->AddIPLiteralRule(
494 kDefaultServerHostName, "192.168.0.1", "");
495 host_resolver_.rules()->AddIPLiteralRule(
496 "mail.google.com", "192.168.0.1", "");
498 QuicStreamRequest request(&factory_);
499 EXPECT_EQ(OK,
500 request.Request(host_port_pair_,
501 is_https_,
502 privacy_mode_,
503 "GET",
504 net_log_,
505 callback_.callback()));
506 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
507 EXPECT_TRUE(stream.get());
509 TestCompletionCallback callback;
510 QuicStreamRequest request2(&factory_);
511 EXPECT_EQ(OK,
512 request2.Request(server2,
513 is_https_,
514 privacy_mode_,
515 "GET",
516 net_log_,
517 callback.callback()));
518 scoped_ptr<QuicHttpStream> stream2 = request2.ReleaseStream();
519 EXPECT_TRUE(stream2.get());
521 factory_.OnSessionGoingAway(QuicStreamFactoryPeer::GetActiveSession(
522 &factory_, host_port_pair_, is_https_));
523 EXPECT_FALSE(QuicStreamFactoryPeer::HasActiveSession(
524 &factory_, host_port_pair_, is_https_));
525 EXPECT_FALSE(QuicStreamFactoryPeer::HasActiveSession(
526 &factory_, server2, is_https_));
528 TestCompletionCallback callback3;
529 QuicStreamRequest request3(&factory_);
530 EXPECT_EQ(OK,
531 request3.Request(server2,
532 is_https_,
533 privacy_mode_,
534 "GET",
535 net_log_,
536 callback3.callback()));
537 scoped_ptr<QuicHttpStream> stream3 = request3.ReleaseStream();
538 EXPECT_TRUE(stream3.get());
540 EXPECT_TRUE(QuicStreamFactoryPeer::HasActiveSession(
541 &factory_, server2, is_https_));
543 EXPECT_TRUE(socket_data1.at_read_eof());
544 EXPECT_TRUE(socket_data1.at_write_eof());
545 EXPECT_TRUE(socket_data2.at_read_eof());
546 EXPECT_TRUE(socket_data2.at_write_eof());
549 TEST_P(QuicStreamFactoryTest, HttpsPooling) {
550 MockRead reads[] = {
551 MockRead(ASYNC, OK, 0) // EOF
553 DeterministicSocketData socket_data(reads, arraysize(reads), NULL, 0);
554 socket_factory_.AddSocketDataProvider(&socket_data);
555 socket_data.StopAfter(1);
557 HostPortPair server1("www.example.org", 443);
558 HostPortPair server2("mail.example.org", 443);
560 // Load a cert that is valid for:
561 // www.example.org (server1)
562 // mail.example.org (server2)
563 // www.example.com
564 base::FilePath certs_dir = GetTestCertsDirectory();
565 scoped_refptr<X509Certificate> test_cert(
566 ImportCertFromFile(certs_dir, "spdy_pooling.pem"));
567 ASSERT_NE(static_cast<X509Certificate*>(NULL), test_cert.get());
568 ProofVerifyDetailsChromium verify_details;
569 verify_details.cert_verify_result.verified_cert = test_cert;
570 verify_details.cert_verify_result.is_issued_by_known_root = true;
571 crypto_client_stream_factory_.set_proof_verify_details(&verify_details);
573 host_resolver_.set_synchronous_mode(true);
574 host_resolver_.rules()->AddIPLiteralRule(server1.host(), "192.168.0.1", "");
575 host_resolver_.rules()->AddIPLiteralRule(server2.host(), "192.168.0.1", "");
577 QuicStreamRequest request(&factory_);
578 is_https_ = true;
579 EXPECT_EQ(OK,
580 request.Request(server1,
581 is_https_,
582 privacy_mode_,
583 "GET",
584 net_log_,
585 callback_.callback()));
586 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
587 EXPECT_TRUE(stream.get());
589 TestCompletionCallback callback;
590 QuicStreamRequest request2(&factory_);
591 EXPECT_EQ(OK,
592 request2.Request(server2,
593 is_https_,
594 privacy_mode_,
595 "GET",
596 net_log_,
597 callback_.callback()));
598 scoped_ptr<QuicHttpStream> stream2 = request2.ReleaseStream();
599 EXPECT_TRUE(stream2.get());
601 EXPECT_EQ(QuicStreamFactoryPeer::GetActiveSession(
602 &factory_, server1, is_https_),
603 QuicStreamFactoryPeer::GetActiveSession(
604 &factory_, server2, is_https_));
606 EXPECT_TRUE(socket_data.at_read_eof());
607 EXPECT_TRUE(socket_data.at_write_eof());
610 TEST_P(QuicStreamFactoryTest, NoHttpsPoolingIfDisabled) {
611 MockRead reads[] = {
612 MockRead(ASYNC, OK, 0) // EOF
614 DeterministicSocketData socket_data1(reads, arraysize(reads), NULL, 0);
615 DeterministicSocketData socket_data2(reads, arraysize(reads), NULL, 0);
616 socket_factory_.AddSocketDataProvider(&socket_data1);
617 socket_factory_.AddSocketDataProvider(&socket_data2);
618 socket_data1.StopAfter(1);
619 socket_data2.StopAfter(1);
621 HostPortPair server1("www.example.org", 443);
622 HostPortPair server2("mail.example.org", 443);
624 // Load a cert that is valid for:
625 // www.example.org (server1)
626 // mail.example.org (server2)
627 // www.example.com
628 base::FilePath certs_dir = GetTestCertsDirectory();
629 scoped_refptr<X509Certificate> test_cert(
630 ImportCertFromFile(certs_dir, "spdy_pooling.pem"));
631 ASSERT_NE(static_cast<X509Certificate*>(NULL), test_cert.get());
632 ProofVerifyDetailsChromium verify_details;
633 verify_details.cert_verify_result.verified_cert = test_cert;
634 verify_details.cert_verify_result.is_issued_by_known_root = true;
635 crypto_client_stream_factory_.set_proof_verify_details(&verify_details);
637 host_resolver_.set_synchronous_mode(true);
638 host_resolver_.rules()->AddIPLiteralRule(server1.host(), "192.168.0.1", "");
639 host_resolver_.rules()->AddIPLiteralRule(server2.host(), "192.168.0.1", "");
641 // Disable connection pooling.
642 QuicStreamFactoryPeer::DisableConnectionPooling(&factory_);
644 QuicStreamRequest request(&factory_);
645 is_https_ = true;
646 EXPECT_EQ(OK,
647 request.Request(server1,
648 is_https_,
649 privacy_mode_,
650 "GET",
651 net_log_,
652 callback_.callback()));
653 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
654 EXPECT_TRUE(stream.get());
656 TestCompletionCallback callback;
657 QuicStreamRequest request2(&factory_);
658 EXPECT_EQ(OK,
659 request2.Request(server2,
660 is_https_,
661 privacy_mode_,
662 "GET",
663 net_log_,
664 callback_.callback()));
665 scoped_ptr<QuicHttpStream> stream2 = request2.ReleaseStream();
666 EXPECT_TRUE(stream2.get());
668 EXPECT_NE(QuicStreamFactoryPeer::GetActiveSession(
669 &factory_, server1, is_https_),
670 QuicStreamFactoryPeer::GetActiveSession(
671 &factory_, server2, is_https_));
673 EXPECT_TRUE(socket_data1.at_read_eof());
674 EXPECT_TRUE(socket_data1.at_write_eof());
675 EXPECT_TRUE(socket_data2.at_read_eof());
676 EXPECT_TRUE(socket_data2.at_write_eof());
679 TEST_P(QuicStreamFactoryTest, NoHttpsPoolingWithCertMismatch) {
680 MockRead reads[] = {
681 MockRead(ASYNC, OK, 0) // EOF
683 DeterministicSocketData socket_data1(reads, arraysize(reads), NULL, 0);
684 DeterministicSocketData socket_data2(reads, arraysize(reads), NULL, 0);
685 socket_factory_.AddSocketDataProvider(&socket_data1);
686 socket_factory_.AddSocketDataProvider(&socket_data2);
687 socket_data1.StopAfter(1);
688 socket_data2.StopAfter(1);
690 HostPortPair server1("www.example.org", 443);
691 HostPortPair server2("mail.google.com", 443);
693 // Load a cert that is valid for:
694 // www.example.org (server1)
695 // mail.example.org
696 // www.example.com
697 // But is not valid for mail.google.com (server2).
698 base::FilePath certs_dir = GetTestCertsDirectory();
699 scoped_refptr<X509Certificate> test_cert(
700 ImportCertFromFile(certs_dir, "spdy_pooling.pem"));
701 ASSERT_NE(static_cast<X509Certificate*>(NULL), test_cert.get());
702 ProofVerifyDetailsChromium verify_details;
703 verify_details.cert_verify_result.verified_cert = test_cert;
704 crypto_client_stream_factory_.set_proof_verify_details(&verify_details);
707 host_resolver_.set_synchronous_mode(true);
708 host_resolver_.rules()->AddIPLiteralRule(server1.host(), "192.168.0.1", "");
709 host_resolver_.rules()->AddIPLiteralRule(server2.host(), "192.168.0.1", "");
711 QuicStreamRequest request(&factory_);
712 is_https_ = true;
713 EXPECT_EQ(OK,
714 request.Request(server1,
715 is_https_,
716 privacy_mode_,
717 "GET",
718 net_log_,
719 callback_.callback()));
720 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
721 EXPECT_TRUE(stream.get());
723 TestCompletionCallback callback;
724 QuicStreamRequest request2(&factory_);
725 EXPECT_EQ(OK,
726 request2.Request(server2,
727 is_https_,
728 privacy_mode_,
729 "GET",
730 net_log_,
731 callback_.callback()));
732 scoped_ptr<QuicHttpStream> stream2 = request2.ReleaseStream();
733 EXPECT_TRUE(stream2.get());
735 EXPECT_NE(QuicStreamFactoryPeer::GetActiveSession(
736 &factory_, server1, is_https_),
737 QuicStreamFactoryPeer::GetActiveSession(
738 &factory_, server2, is_https_));
740 EXPECT_TRUE(socket_data1.at_read_eof());
741 EXPECT_TRUE(socket_data1.at_write_eof());
742 EXPECT_TRUE(socket_data2.at_read_eof());
743 EXPECT_TRUE(socket_data2.at_write_eof());
746 TEST_P(QuicStreamFactoryTest, HttpsPoolingWithMatchingPins) {
747 MockRead reads[] = {
748 MockRead(ASYNC, OK, 0) // EOF
750 DeterministicSocketData socket_data(reads, arraysize(reads), NULL, 0);
751 socket_factory_.AddSocketDataProvider(&socket_data);
752 socket_data.StopAfter(1);
754 HostPortPair server1("www.example.org", 443);
755 HostPortPair server2("mail.example.org", 443);
756 uint8 primary_pin = 1;
757 uint8 backup_pin = 2;
758 test::AddPin(&transport_security_state_, "mail.example.org", primary_pin,
759 backup_pin);
761 // Load a cert that is valid for:
762 // www.example.org (server1)
763 // mail.example.org (server2)
764 base::FilePath certs_dir = GetTestCertsDirectory();
765 scoped_refptr<X509Certificate> test_cert(
766 ImportCertFromFile(certs_dir, "spdy_pooling.pem"));
767 ASSERT_NE(static_cast<X509Certificate*>(NULL), test_cert.get());
768 ProofVerifyDetailsChromium verify_details;
769 verify_details.cert_verify_result.verified_cert = test_cert;
770 verify_details.cert_verify_result.is_issued_by_known_root = true;
771 verify_details.cert_verify_result.public_key_hashes.push_back(
772 test::GetTestHashValue(primary_pin));
773 crypto_client_stream_factory_.set_proof_verify_details(&verify_details);
776 host_resolver_.set_synchronous_mode(true);
777 host_resolver_.rules()->AddIPLiteralRule(server1.host(), "192.168.0.1", "");
778 host_resolver_.rules()->AddIPLiteralRule(server2.host(), "192.168.0.1", "");
780 QuicStreamRequest request(&factory_);
781 is_https_ = true;
782 EXPECT_EQ(OK,
783 request.Request(server1,
784 is_https_,
785 privacy_mode_,
786 "GET",
787 net_log_,
788 callback_.callback()));
789 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
790 EXPECT_TRUE(stream.get());
792 TestCompletionCallback callback;
793 QuicStreamRequest request2(&factory_);
794 EXPECT_EQ(OK,
795 request2.Request(server2,
796 is_https_,
797 privacy_mode_,
798 "GET",
799 net_log_,
800 callback_.callback()));
801 scoped_ptr<QuicHttpStream> stream2 = request2.ReleaseStream();
802 EXPECT_TRUE(stream2.get());
804 EXPECT_EQ(QuicStreamFactoryPeer::GetActiveSession(
805 &factory_, server1, is_https_),
806 QuicStreamFactoryPeer::GetActiveSession(
807 &factory_, server2, is_https_));
809 EXPECT_TRUE(socket_data.at_read_eof());
810 EXPECT_TRUE(socket_data.at_write_eof());
813 TEST_P(QuicStreamFactoryTest, NoHttpsPoolingWithMatchingPinsIfDisabled) {
814 MockRead reads[] = {
815 MockRead(ASYNC, OK, 0) // EOF
817 DeterministicSocketData socket_data1(reads, arraysize(reads), NULL, 0);
818 DeterministicSocketData socket_data2(reads, arraysize(reads), NULL, 0);
819 socket_factory_.AddSocketDataProvider(&socket_data1);
820 socket_factory_.AddSocketDataProvider(&socket_data2);
821 socket_data1.StopAfter(1);
822 socket_data2.StopAfter(1);
824 HostPortPair server1("www.example.org", 443);
825 HostPortPair server2("mail.example.org", 443);
826 uint8 primary_pin = 1;
827 uint8 backup_pin = 2;
828 test::AddPin(&transport_security_state_, "mail.example.org", primary_pin,
829 backup_pin);
831 // Load a cert that is valid for:
832 // www.example.org (server1)
833 // mail.example.org (server2)
834 base::FilePath certs_dir = GetTestCertsDirectory();
835 scoped_refptr<X509Certificate> test_cert(
836 ImportCertFromFile(certs_dir, "spdy_pooling.pem"));
837 ASSERT_NE(static_cast<X509Certificate*>(NULL), test_cert.get());
838 ProofVerifyDetailsChromium verify_details;
839 verify_details.cert_verify_result.verified_cert = test_cert;
840 verify_details.cert_verify_result.is_issued_by_known_root = true;
841 verify_details.cert_verify_result.public_key_hashes.push_back(
842 test::GetTestHashValue(primary_pin));
843 crypto_client_stream_factory_.set_proof_verify_details(&verify_details);
846 host_resolver_.set_synchronous_mode(true);
847 host_resolver_.rules()->AddIPLiteralRule(server1.host(), "192.168.0.1", "");
848 host_resolver_.rules()->AddIPLiteralRule(server2.host(), "192.168.0.1", "");
850 // Disable connection pooling.
851 QuicStreamFactoryPeer::DisableConnectionPooling(&factory_);
853 QuicStreamRequest request(&factory_);
854 is_https_ = true;
855 EXPECT_EQ(OK,
856 request.Request(server1,
857 is_https_,
858 privacy_mode_,
859 "GET",
860 net_log_,
861 callback_.callback()));
862 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
863 EXPECT_TRUE(stream.get());
865 TestCompletionCallback callback;
866 QuicStreamRequest request2(&factory_);
867 EXPECT_EQ(OK,
868 request2.Request(server2,
869 is_https_,
870 privacy_mode_,
871 "GET",
872 net_log_,
873 callback_.callback()));
874 scoped_ptr<QuicHttpStream> stream2 = request2.ReleaseStream();
875 EXPECT_TRUE(stream2.get());
877 EXPECT_NE(QuicStreamFactoryPeer::GetActiveSession(
878 &factory_, server1, is_https_),
879 QuicStreamFactoryPeer::GetActiveSession(
880 &factory_, server2, is_https_));
882 EXPECT_TRUE(socket_data1.at_read_eof());
883 EXPECT_TRUE(socket_data1.at_write_eof());
884 EXPECT_TRUE(socket_data2.at_read_eof());
885 EXPECT_TRUE(socket_data2.at_write_eof());
888 TEST_P(QuicStreamFactoryTest, NoHttpsPoolingWithDifferentPins) {
889 MockRead reads[] = {
890 MockRead(ASYNC, OK, 0) // EOF
892 DeterministicSocketData socket_data1(reads, arraysize(reads), NULL, 0);
893 DeterministicSocketData socket_data2(reads, arraysize(reads), NULL, 0);
894 socket_factory_.AddSocketDataProvider(&socket_data1);
895 socket_factory_.AddSocketDataProvider(&socket_data2);
896 socket_data1.StopAfter(1);
897 socket_data2.StopAfter(1);
899 HostPortPair server1("www.example.org", 443);
900 HostPortPair server2("mail.example.org", 443);
901 uint8 primary_pin = 1;
902 uint8 backup_pin = 2;
903 uint8 bad_pin = 3;
904 test::AddPin(&transport_security_state_, "mail.example.org", primary_pin,
905 backup_pin);
907 // Load a cert that is valid for:
908 // www.example.org (server1)
909 // mail.example.org (server2)
910 base::FilePath certs_dir = GetTestCertsDirectory();
911 scoped_refptr<X509Certificate> test_cert(
912 ImportCertFromFile(certs_dir, "spdy_pooling.pem"));
913 ASSERT_NE(static_cast<X509Certificate*>(NULL), test_cert.get());
914 ProofVerifyDetailsChromium verify_details;
915 verify_details.cert_verify_result.verified_cert = test_cert;
916 verify_details.cert_verify_result.is_issued_by_known_root = true;
917 verify_details.cert_verify_result.public_key_hashes.push_back(
918 test::GetTestHashValue(bad_pin));
919 crypto_client_stream_factory_.set_proof_verify_details(&verify_details);
922 host_resolver_.set_synchronous_mode(true);
923 host_resolver_.rules()->AddIPLiteralRule(server1.host(), "192.168.0.1", "");
924 host_resolver_.rules()->AddIPLiteralRule(server2.host(), "192.168.0.1", "");
926 QuicStreamRequest request(&factory_);
927 is_https_ = true;
928 EXPECT_EQ(OK,
929 request.Request(server1,
930 is_https_,
931 privacy_mode_,
932 "GET",
933 net_log_,
934 callback_.callback()));
935 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
936 EXPECT_TRUE(stream.get());
938 TestCompletionCallback callback;
939 QuicStreamRequest request2(&factory_);
940 EXPECT_EQ(OK,
941 request2.Request(server2,
942 is_https_,
943 privacy_mode_,
944 "GET",
945 net_log_,
946 callback_.callback()));
947 scoped_ptr<QuicHttpStream> stream2 = request2.ReleaseStream();
948 EXPECT_TRUE(stream2.get());
950 EXPECT_NE(QuicStreamFactoryPeer::GetActiveSession(
951 &factory_, server1, is_https_),
952 QuicStreamFactoryPeer::GetActiveSession(
953 &factory_, server2, is_https_));
955 EXPECT_TRUE(socket_data1.at_read_eof());
956 EXPECT_TRUE(socket_data1.at_write_eof());
957 EXPECT_TRUE(socket_data2.at_read_eof());
958 EXPECT_TRUE(socket_data2.at_write_eof());
961 TEST_P(QuicStreamFactoryTest, Goaway) {
962 MockRead reads[] = {
963 MockRead(ASYNC, OK, 0) // EOF
965 DeterministicSocketData socket_data(reads, arraysize(reads), NULL, 0);
966 socket_data.StopAfter(1);
967 socket_factory_.AddSocketDataProvider(&socket_data);
968 DeterministicSocketData socket_data2(reads, arraysize(reads), NULL, 0);
969 socket_data2.StopAfter(1);
970 socket_factory_.AddSocketDataProvider(&socket_data2);
972 QuicStreamRequest request(&factory_);
973 EXPECT_EQ(ERR_IO_PENDING,
974 request.Request(host_port_pair_,
975 is_https_,
976 privacy_mode_,
977 "GET",
978 net_log_,
979 callback_.callback()));
981 EXPECT_EQ(OK, callback_.WaitForResult());
982 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
983 EXPECT_TRUE(stream.get());
985 // Mark the session as going away. Ensure that while it is still alive
986 // that it is no longer active.
987 QuicClientSession* session = QuicStreamFactoryPeer::GetActiveSession(
988 &factory_, host_port_pair_, is_https_);
989 factory_.OnSessionGoingAway(session);
990 EXPECT_EQ(true, QuicStreamFactoryPeer::IsLiveSession(&factory_, session));
991 EXPECT_FALSE(QuicStreamFactoryPeer::HasActiveSession(
992 &factory_, host_port_pair_, is_https_));
993 EXPECT_EQ(NULL, CreateIfSessionExists(host_port_pair_, net_log_).get());
995 // Create a new request for the same destination and verify that a
996 // new session is created.
997 QuicStreamRequest request2(&factory_);
998 EXPECT_EQ(ERR_IO_PENDING,
999 request2.Request(host_port_pair_,
1000 is_https_,
1001 privacy_mode_,
1002 "GET",
1003 net_log_,
1004 callback_.callback()));
1005 EXPECT_EQ(OK, callback_.WaitForResult());
1006 scoped_ptr<QuicHttpStream> stream2 = request2.ReleaseStream();
1007 EXPECT_TRUE(stream2.get());
1009 EXPECT_TRUE(QuicStreamFactoryPeer::HasActiveSession(&factory_,
1010 host_port_pair_,
1011 is_https_));
1012 EXPECT_NE(session,
1013 QuicStreamFactoryPeer::GetActiveSession(
1014 &factory_, host_port_pair_, is_https_));
1015 EXPECT_EQ(true, QuicStreamFactoryPeer::IsLiveSession(&factory_, session));
1017 stream2.reset();
1018 stream.reset();
1020 EXPECT_TRUE(socket_data.at_read_eof());
1021 EXPECT_TRUE(socket_data.at_write_eof());
1022 EXPECT_TRUE(socket_data2.at_read_eof());
1023 EXPECT_TRUE(socket_data2.at_write_eof());
1026 TEST_P(QuicStreamFactoryTest, MaxOpenStream) {
1027 MockRead reads[] = {
1028 MockRead(ASYNC, OK, 0) // EOF
1030 QuicStreamId stream_id = kClientDataStreamId1;
1031 scoped_ptr<QuicEncryptedPacket> rst(
1032 maker_.MakeRstPacket(1, true, stream_id, QUIC_STREAM_CANCELLED));
1033 MockWrite writes[] = {
1034 MockWrite(ASYNC, rst->data(), rst->length(), 1),
1036 DeterministicSocketData socket_data(reads, arraysize(reads),
1037 writes, arraysize(writes));
1038 socket_factory_.AddSocketDataProvider(&socket_data);
1039 socket_data.StopAfter(1);
1041 HttpRequestInfo request_info;
1042 std::vector<QuicHttpStream*> streams;
1043 // The MockCryptoClientStream sets max_open_streams to be
1044 // 2 * kDefaultMaxStreamsPerConnection.
1045 for (size_t i = 0; i < 2 * kDefaultMaxStreamsPerConnection; i++) {
1046 QuicStreamRequest request(&factory_);
1047 int rv = request.Request(host_port_pair_,
1048 is_https_,
1049 privacy_mode_,
1050 "GET",
1051 net_log_,
1052 callback_.callback());
1053 if (i == 0) {
1054 EXPECT_EQ(ERR_IO_PENDING, rv);
1055 EXPECT_EQ(OK, callback_.WaitForResult());
1056 } else {
1057 EXPECT_EQ(OK, rv);
1059 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
1060 EXPECT_TRUE(stream);
1061 EXPECT_EQ(OK, stream->InitializeStream(
1062 &request_info, DEFAULT_PRIORITY, net_log_, CompletionCallback()));
1063 streams.push_back(stream.release());
1066 QuicStreamRequest request(&factory_);
1067 EXPECT_EQ(OK,
1068 request.Request(host_port_pair_,
1069 is_https_,
1070 privacy_mode_,
1071 "GET",
1072 net_log_,
1073 CompletionCallback()));
1074 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
1075 EXPECT_TRUE(stream);
1076 EXPECT_EQ(ERR_IO_PENDING, stream->InitializeStream(
1077 &request_info, DEFAULT_PRIORITY, net_log_, callback_.callback()));
1079 // Close the first stream.
1080 streams.front()->Close(false);
1082 ASSERT_TRUE(callback_.have_result());
1084 EXPECT_EQ(OK, callback_.WaitForResult());
1086 EXPECT_TRUE(socket_data.at_read_eof());
1087 EXPECT_TRUE(socket_data.at_write_eof());
1088 STLDeleteElements(&streams);
1091 TEST_P(QuicStreamFactoryTest, ResolutionErrorInCreate) {
1092 DeterministicSocketData socket_data(NULL, 0, NULL, 0);
1093 socket_factory_.AddSocketDataProvider(&socket_data);
1095 host_resolver_.rules()->AddSimulatedFailure(kDefaultServerHostName);
1097 QuicStreamRequest request(&factory_);
1098 EXPECT_EQ(ERR_IO_PENDING,
1099 request.Request(host_port_pair_,
1100 is_https_,
1101 privacy_mode_,
1102 "GET",
1103 net_log_,
1104 callback_.callback()));
1106 EXPECT_EQ(ERR_NAME_NOT_RESOLVED, callback_.WaitForResult());
1108 EXPECT_TRUE(socket_data.at_read_eof());
1109 EXPECT_TRUE(socket_data.at_write_eof());
1112 TEST_P(QuicStreamFactoryTest, ConnectErrorInCreate) {
1113 MockConnect connect(SYNCHRONOUS, ERR_ADDRESS_IN_USE);
1114 DeterministicSocketData socket_data(NULL, 0, NULL, 0);
1115 socket_data.set_connect_data(connect);
1116 socket_factory_.AddSocketDataProvider(&socket_data);
1117 socket_data.StopAfter(1);
1119 QuicStreamRequest request(&factory_);
1120 EXPECT_EQ(ERR_IO_PENDING,
1121 request.Request(host_port_pair_,
1122 is_https_,
1123 privacy_mode_,
1124 "GET",
1125 net_log_,
1126 callback_.callback()));
1128 EXPECT_EQ(ERR_ADDRESS_IN_USE, callback_.WaitForResult());
1130 EXPECT_TRUE(socket_data.at_read_eof());
1131 EXPECT_TRUE(socket_data.at_write_eof());
1134 TEST_P(QuicStreamFactoryTest, CancelCreate) {
1135 MockRead reads[] = {
1136 MockRead(ASYNC, OK, 0) // EOF
1138 DeterministicSocketData socket_data(reads, arraysize(reads), NULL, 0);
1139 socket_factory_.AddSocketDataProvider(&socket_data);
1141 QuicStreamRequest request(&factory_);
1142 EXPECT_EQ(ERR_IO_PENDING,
1143 request.Request(host_port_pair_,
1144 is_https_,
1145 privacy_mode_,
1146 "GET",
1147 net_log_,
1148 callback_.callback()));
1151 socket_data.StopAfter(1);
1152 base::RunLoop run_loop;
1153 run_loop.RunUntilIdle();
1155 scoped_ptr<QuicHttpStream> stream(
1156 CreateIfSessionExists(host_port_pair_, net_log_));
1157 EXPECT_TRUE(stream.get());
1158 stream.reset();
1160 EXPECT_TRUE(socket_data.at_read_eof());
1161 EXPECT_TRUE(socket_data.at_write_eof());
1164 TEST_P(QuicStreamFactoryTest, CreateConsistentEphemeralPort) {
1165 // Sequentially connect to the default host, then another host, and then the
1166 // default host. Verify that the default host gets a consistent ephemeral
1167 // port, that is different from the other host's connection.
1169 std::string other_server_name = "other.google.com";
1170 EXPECT_NE(kDefaultServerHostName, other_server_name);
1171 HostPortPair host_port_pair2(other_server_name, kDefaultServerPort);
1173 int original_port = GetSourcePortForNewSession(host_port_pair_);
1174 EXPECT_NE(original_port, GetSourcePortForNewSession(host_port_pair2));
1175 EXPECT_EQ(original_port, GetSourcePortForNewSession(host_port_pair_));
1178 TEST_P(QuicStreamFactoryTest, GoAwayDisablesConsistentEphemeralPort) {
1179 // Get a session to the host using the port suggester.
1180 int original_port =
1181 GetSourcePortForNewSessionAndGoAway(host_port_pair_);
1182 // Verify that the port is different after the goaway.
1183 EXPECT_NE(original_port, GetSourcePortForNewSession(host_port_pair_));
1184 // Since the previous session did not goaway we should see the original port.
1185 EXPECT_EQ(original_port, GetSourcePortForNewSession(host_port_pair_));
1188 TEST_P(QuicStreamFactoryTest, CloseAllSessions) {
1189 MockRead reads[] = {
1190 MockRead(ASYNC, 0, 0) // EOF
1192 scoped_ptr<QuicEncryptedPacket> rst(ConstructRstPacket());
1193 std::vector<MockWrite> writes;
1194 writes.push_back(MockWrite(ASYNC, rst->data(), rst->length(), 1));
1195 DeterministicSocketData socket_data(reads, arraysize(reads),
1196 writes.empty() ? NULL : &writes[0],
1197 writes.size());
1198 socket_factory_.AddSocketDataProvider(&socket_data);
1199 socket_data.StopAfter(1);
1201 MockRead reads2[] = {
1202 MockRead(ASYNC, 0, 0) // EOF
1204 DeterministicSocketData socket_data2(reads2, arraysize(reads2), NULL, 0);
1205 socket_factory_.AddSocketDataProvider(&socket_data2);
1206 socket_data2.StopAfter(1);
1208 QuicStreamRequest request(&factory_);
1209 EXPECT_EQ(ERR_IO_PENDING,
1210 request.Request(host_port_pair_,
1211 is_https_,
1212 privacy_mode_,
1213 "GET",
1214 net_log_,
1215 callback_.callback()));
1217 EXPECT_EQ(OK, callback_.WaitForResult());
1218 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
1219 HttpRequestInfo request_info;
1220 EXPECT_EQ(OK, stream->InitializeStream(&request_info,
1221 DEFAULT_PRIORITY,
1222 net_log_, CompletionCallback()));
1224 // Close the session and verify that stream saw the error.
1225 factory_.CloseAllSessions(ERR_INTERNET_DISCONNECTED);
1226 EXPECT_EQ(ERR_INTERNET_DISCONNECTED,
1227 stream->ReadResponseHeaders(callback_.callback()));
1229 // Now attempting to request a stream to the same origin should create
1230 // a new session.
1232 QuicStreamRequest request2(&factory_);
1233 EXPECT_EQ(ERR_IO_PENDING,
1234 request2.Request(host_port_pair_,
1235 is_https_,
1236 privacy_mode_,
1237 "GET",
1238 net_log_,
1239 callback_.callback()));
1241 EXPECT_EQ(OK, callback_.WaitForResult());
1242 stream = request2.ReleaseStream();
1243 stream.reset(); // Will reset stream 3.
1245 EXPECT_TRUE(socket_data.at_read_eof());
1246 EXPECT_TRUE(socket_data.at_write_eof());
1247 EXPECT_TRUE(socket_data2.at_read_eof());
1248 EXPECT_TRUE(socket_data2.at_write_eof());
1251 TEST_P(QuicStreamFactoryTest, OnIPAddressChanged) {
1252 MockRead reads[] = {
1253 MockRead(ASYNC, 0, 0) // EOF
1255 scoped_ptr<QuicEncryptedPacket> rst(ConstructRstPacket());
1256 std::vector<MockWrite> writes;
1257 writes.push_back(MockWrite(ASYNC, rst->data(), rst->length(), 1));
1258 DeterministicSocketData socket_data(reads, arraysize(reads),
1259 writes.empty() ? NULL : &writes[0],
1260 writes.size());
1261 socket_factory_.AddSocketDataProvider(&socket_data);
1262 socket_data.StopAfter(1);
1264 MockRead reads2[] = {
1265 MockRead(ASYNC, 0, 0) // EOF
1267 DeterministicSocketData socket_data2(reads2, arraysize(reads2), NULL, 0);
1268 socket_factory_.AddSocketDataProvider(&socket_data2);
1269 socket_data2.StopAfter(1);
1271 QuicStreamRequest request(&factory_);
1272 EXPECT_EQ(ERR_IO_PENDING,
1273 request.Request(host_port_pair_,
1274 is_https_,
1275 privacy_mode_,
1276 "GET",
1277 net_log_,
1278 callback_.callback()));
1280 EXPECT_EQ(OK, callback_.WaitForResult());
1281 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
1282 HttpRequestInfo request_info;
1283 EXPECT_EQ(OK, stream->InitializeStream(&request_info,
1284 DEFAULT_PRIORITY,
1285 net_log_, CompletionCallback()));
1287 // Change the IP address and verify that stream saw the error.
1288 factory_.OnIPAddressChanged();
1289 EXPECT_EQ(ERR_NETWORK_CHANGED,
1290 stream->ReadResponseHeaders(callback_.callback()));
1291 EXPECT_TRUE(factory_.require_confirmation());
1293 // Now attempting to request a stream to the same origin should create
1294 // a new session.
1296 QuicStreamRequest request2(&factory_);
1297 EXPECT_EQ(ERR_IO_PENDING,
1298 request2.Request(host_port_pair_,
1299 is_https_,
1300 privacy_mode_,
1301 "GET",
1302 net_log_,
1303 callback_.callback()));
1305 EXPECT_EQ(OK, callback_.WaitForResult());
1306 stream = request2.ReleaseStream();
1307 stream.reset(); // Will reset stream 3.
1309 EXPECT_TRUE(socket_data.at_read_eof());
1310 EXPECT_TRUE(socket_data.at_write_eof());
1311 EXPECT_TRUE(socket_data2.at_read_eof());
1312 EXPECT_TRUE(socket_data2.at_write_eof());
1315 TEST_P(QuicStreamFactoryTest, OnCertAdded) {
1316 MockRead reads[] = {
1317 MockRead(ASYNC, 0, 0) // EOF
1319 scoped_ptr<QuicEncryptedPacket> rst(ConstructRstPacket());
1320 std::vector<MockWrite> writes;
1321 writes.push_back(MockWrite(ASYNC, rst->data(), rst->length(), 1));
1322 DeterministicSocketData socket_data(reads, arraysize(reads),
1323 writes.empty() ? NULL : &writes[0],
1324 writes.size());
1325 socket_factory_.AddSocketDataProvider(&socket_data);
1326 socket_data.StopAfter(1);
1328 MockRead reads2[] = {
1329 MockRead(ASYNC, 0, 0) // EOF
1331 DeterministicSocketData socket_data2(reads2, arraysize(reads2), NULL, 0);
1332 socket_factory_.AddSocketDataProvider(&socket_data2);
1333 socket_data2.StopAfter(1);
1335 QuicStreamRequest request(&factory_);
1336 EXPECT_EQ(ERR_IO_PENDING,
1337 request.Request(host_port_pair_,
1338 is_https_,
1339 privacy_mode_,
1340 "GET",
1341 net_log_,
1342 callback_.callback()));
1344 EXPECT_EQ(OK, callback_.WaitForResult());
1345 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
1346 HttpRequestInfo request_info;
1347 EXPECT_EQ(OK, stream->InitializeStream(&request_info,
1348 DEFAULT_PRIORITY,
1349 net_log_, CompletionCallback()));
1351 // Add a cert and verify that stream saw the event.
1352 factory_.OnCertAdded(NULL);
1353 EXPECT_EQ(ERR_CERT_DATABASE_CHANGED,
1354 stream->ReadResponseHeaders(callback_.callback()));
1355 EXPECT_FALSE(factory_.require_confirmation());
1357 // Now attempting to request a stream to the same origin should create
1358 // a new session.
1360 QuicStreamRequest request2(&factory_);
1361 EXPECT_EQ(ERR_IO_PENDING,
1362 request2.Request(host_port_pair_,
1363 is_https_,
1364 privacy_mode_,
1365 "GET",
1366 net_log_,
1367 callback_.callback()));
1369 EXPECT_EQ(OK, callback_.WaitForResult());
1370 stream = request2.ReleaseStream();
1371 stream.reset(); // Will reset stream 3.
1373 EXPECT_TRUE(socket_data.at_read_eof());
1374 EXPECT_TRUE(socket_data.at_write_eof());
1375 EXPECT_TRUE(socket_data2.at_read_eof());
1376 EXPECT_TRUE(socket_data2.at_write_eof());
1379 TEST_P(QuicStreamFactoryTest, OnCACertChanged) {
1380 MockRead reads[] = {
1381 MockRead(ASYNC, 0, 0) // EOF
1383 scoped_ptr<QuicEncryptedPacket> rst(ConstructRstPacket());
1384 std::vector<MockWrite> writes;
1385 writes.push_back(MockWrite(ASYNC, rst->data(), rst->length(), 1));
1386 DeterministicSocketData socket_data(reads, arraysize(reads),
1387 writes.empty() ? NULL : &writes[0],
1388 writes.size());
1389 socket_factory_.AddSocketDataProvider(&socket_data);
1390 socket_data.StopAfter(1);
1392 MockRead reads2[] = {
1393 MockRead(ASYNC, 0, 0) // EOF
1395 DeterministicSocketData socket_data2(reads2, arraysize(reads2), NULL, 0);
1396 socket_factory_.AddSocketDataProvider(&socket_data2);
1397 socket_data2.StopAfter(1);
1399 QuicStreamRequest request(&factory_);
1400 EXPECT_EQ(ERR_IO_PENDING,
1401 request.Request(host_port_pair_,
1402 is_https_,
1403 privacy_mode_,
1404 "GET",
1405 net_log_,
1406 callback_.callback()));
1408 EXPECT_EQ(OK, callback_.WaitForResult());
1409 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
1410 HttpRequestInfo request_info;
1411 EXPECT_EQ(OK, stream->InitializeStream(&request_info,
1412 DEFAULT_PRIORITY,
1413 net_log_, CompletionCallback()));
1415 // Change the CA cert and verify that stream saw the event.
1416 factory_.OnCACertChanged(NULL);
1417 EXPECT_EQ(ERR_CERT_DATABASE_CHANGED,
1418 stream->ReadResponseHeaders(callback_.callback()));
1419 EXPECT_FALSE(factory_.require_confirmation());
1421 // Now attempting to request a stream to the same origin should create
1422 // a new session.
1424 QuicStreamRequest request2(&factory_);
1425 EXPECT_EQ(ERR_IO_PENDING,
1426 request2.Request(host_port_pair_,
1427 is_https_,
1428 privacy_mode_,
1429 "GET",
1430 net_log_,
1431 callback_.callback()));
1433 EXPECT_EQ(OK, callback_.WaitForResult());
1434 stream = request2.ReleaseStream();
1435 stream.reset(); // Will reset stream 3.
1437 EXPECT_TRUE(socket_data.at_read_eof());
1438 EXPECT_TRUE(socket_data.at_write_eof());
1439 EXPECT_TRUE(socket_data2.at_read_eof());
1440 EXPECT_TRUE(socket_data2.at_write_eof());
1443 TEST_P(QuicStreamFactoryTest, SharedCryptoConfig) {
1444 vector<string> cannoncial_suffixes;
1445 cannoncial_suffixes.push_back(string(".c.youtube.com"));
1446 cannoncial_suffixes.push_back(string(".googlevideo.com"));
1448 for (unsigned i = 0; i < cannoncial_suffixes.size(); ++i) {
1449 string r1_host_name("r1");
1450 string r2_host_name("r2");
1451 r1_host_name.append(cannoncial_suffixes[i]);
1452 r2_host_name.append(cannoncial_suffixes[i]);
1454 HostPortPair host_port_pair1(r1_host_name, 80);
1455 QuicCryptoClientConfig* crypto_config =
1456 QuicStreamFactoryPeer::GetCryptoConfig(&factory_);
1457 QuicServerId server_id1(host_port_pair1, is_https_, privacy_mode_);
1458 QuicCryptoClientConfig::CachedState* cached1 =
1459 crypto_config->LookupOrCreate(server_id1);
1460 EXPECT_FALSE(cached1->proof_valid());
1461 EXPECT_TRUE(cached1->source_address_token().empty());
1463 // Mutate the cached1 to have different data.
1464 // TODO(rtenneti): mutate other members of CachedState.
1465 cached1->set_source_address_token(r1_host_name);
1466 cached1->SetProofValid();
1468 HostPortPair host_port_pair2(r2_host_name, 80);
1469 QuicServerId server_id2(host_port_pair2, is_https_, privacy_mode_);
1470 QuicCryptoClientConfig::CachedState* cached2 =
1471 crypto_config->LookupOrCreate(server_id2);
1472 EXPECT_EQ(cached1->source_address_token(), cached2->source_address_token());
1473 EXPECT_TRUE(cached2->proof_valid());
1477 TEST_P(QuicStreamFactoryTest, CryptoConfigWhenProofIsInvalid) {
1478 vector<string> cannoncial_suffixes;
1479 cannoncial_suffixes.push_back(string(".c.youtube.com"));
1480 cannoncial_suffixes.push_back(string(".googlevideo.com"));
1482 for (unsigned i = 0; i < cannoncial_suffixes.size(); ++i) {
1483 string r3_host_name("r3");
1484 string r4_host_name("r4");
1485 r3_host_name.append(cannoncial_suffixes[i]);
1486 r4_host_name.append(cannoncial_suffixes[i]);
1488 HostPortPair host_port_pair1(r3_host_name, 80);
1489 QuicCryptoClientConfig* crypto_config =
1490 QuicStreamFactoryPeer::GetCryptoConfig(&factory_);
1491 QuicServerId server_id1(host_port_pair1, is_https_, privacy_mode_);
1492 QuicCryptoClientConfig::CachedState* cached1 =
1493 crypto_config->LookupOrCreate(server_id1);
1494 EXPECT_FALSE(cached1->proof_valid());
1495 EXPECT_TRUE(cached1->source_address_token().empty());
1497 // Mutate the cached1 to have different data.
1498 // TODO(rtenneti): mutate other members of CachedState.
1499 cached1->set_source_address_token(r3_host_name);
1500 cached1->SetProofInvalid();
1502 HostPortPair host_port_pair2(r4_host_name, 80);
1503 QuicServerId server_id2(host_port_pair2, is_https_, privacy_mode_);
1504 QuicCryptoClientConfig::CachedState* cached2 =
1505 crypto_config->LookupOrCreate(server_id2);
1506 EXPECT_NE(cached1->source_address_token(), cached2->source_address_token());
1507 EXPECT_TRUE(cached2->source_address_token().empty());
1508 EXPECT_FALSE(cached2->proof_valid());
1512 } // namespace test
1513 } // namespace net