Lots of random cleanups, mostly for native_theme_win.cc:
[chromium-blink-merge.git] / net / quic / quic_stream_factory_test.cc
blob7c09cbf7d2b04b41add7c1a6cc478f9ec439e0ce
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/quic/crypto/crypto_handshake.h"
16 #include "net/quic/crypto/proof_verifier_chromium.h"
17 #include "net/quic/crypto/quic_decrypter.h"
18 #include "net/quic/crypto/quic_encrypter.h"
19 #include "net/quic/quic_http_stream.h"
20 #include "net/quic/quic_server_id.h"
21 #include "net/quic/test_tools/mock_clock.h"
22 #include "net/quic/test_tools/mock_crypto_client_stream_factory.h"
23 #include "net/quic/test_tools/mock_random.h"
24 #include "net/quic/test_tools/quic_test_packet_maker.h"
25 #include "net/quic/test_tools/quic_test_utils.h"
26 #include "net/socket/socket_test_util.h"
27 #include "net/ssl/default_server_bound_cert_store.h"
28 #include "net/test/cert_test_util.h"
29 #include "testing/gtest/include/gtest/gtest.h"
31 using base::StringPiece;
32 using std::string;
33 using std::vector;
35 namespace net {
36 namespace test {
38 namespace {
39 const char kDefaultServerHostName[] = "www.google.com";
40 const int kDefaultServerPort = 443;
41 } // namespace anonymous
43 class QuicStreamFactoryPeer {
44 public:
45 static QuicCryptoClientConfig* GetCryptoConfig(QuicStreamFactory* factory) {
46 return &factory->crypto_config_;
49 static bool HasActiveSession(QuicStreamFactory* factory,
50 const HostPortPair& host_port_pair,
51 bool is_https) {
52 QuicServerId server_id(host_port_pair, is_https, PRIVACY_MODE_DISABLED);
53 return factory->HasActiveSession(server_id);
56 static QuicClientSession* GetActiveSession(
57 QuicStreamFactory* factory,
58 const HostPortPair& host_port_pair,
59 bool is_https) {
60 QuicServerId server_id(host_port_pair, is_https, PRIVACY_MODE_DISABLED);
61 DCHECK(factory->HasActiveSession(server_id));
62 return factory->active_sessions_[server_id];
65 static scoped_ptr<QuicHttpStream> CreateIfSessionExists(
66 QuicStreamFactory* factory,
67 const HostPortPair& host_port_pair,
68 bool is_https,
69 const BoundNetLog& net_log) {
70 QuicServerId server_id(host_port_pair, is_https, PRIVACY_MODE_DISABLED);
71 return factory->CreateIfSessionExists(server_id, net_log);
74 static bool IsLiveSession(QuicStreamFactory* factory,
75 QuicClientSession* session) {
76 for (QuicStreamFactory::SessionIdMap::iterator it =
77 factory->all_sessions_.begin();
78 it != factory->all_sessions_.end(); ++it) {
79 if (it->first == session)
80 return true;
82 return false;
86 class QuicStreamFactoryTest : public ::testing::TestWithParam<QuicVersion> {
87 protected:
88 QuicStreamFactoryTest()
89 : random_generator_(0),
90 maker_(GetParam(), 0),
91 clock_(new MockClock()),
92 cert_verifier_(CertVerifier::CreateDefault()),
93 server_bound_cert_service_(new ServerBoundCertService(
94 new DefaultServerBoundCertStore(NULL),
95 base::MessageLoopProxy::current())),
96 factory_(&host_resolver_, &socket_factory_,
97 base::WeakPtr<HttpServerProperties>(), cert_verifier_.get(),
98 server_bound_cert_service_.get(),
99 &crypto_client_stream_factory_, &random_generator_, clock_,
100 kDefaultMaxPacketSize, std::string(),
101 SupportedVersions(GetParam()), true, true, true,
102 QuicTagVector()),
103 host_port_pair_(kDefaultServerHostName, kDefaultServerPort),
104 is_https_(false),
105 privacy_mode_(PRIVACY_MODE_DISABLED) {
106 factory_.set_require_confirmation(false);
109 scoped_ptr<QuicHttpStream> CreateIfSessionExists(
110 const HostPortPair& host_port_pair,
111 const BoundNetLog& net_log) {
112 return QuicStreamFactoryPeer::CreateIfSessionExists(
113 &factory_, host_port_pair, false, net_log_);
116 int GetSourcePortForNewSession(const HostPortPair& destination) {
117 return GetSourcePortForNewSessionInner(destination, false);
120 int GetSourcePortForNewSessionAndGoAway(
121 const HostPortPair& destination) {
122 return GetSourcePortForNewSessionInner(destination, true);
125 int GetSourcePortForNewSessionInner(const HostPortPair& destination,
126 bool goaway_received) {
127 // Should only be called if there is no active session for this destination.
128 EXPECT_EQ(NULL, CreateIfSessionExists(destination, net_log_).get());
129 size_t socket_count = socket_factory_.udp_client_sockets().size();
131 MockRead reads[] = {
132 MockRead(ASYNC, OK, 0) // EOF
134 DeterministicSocketData socket_data(reads, arraysize(reads), NULL, 0);
135 socket_data.StopAfter(1);
136 socket_factory_.AddSocketDataProvider(&socket_data);
138 QuicStreamRequest request(&factory_);
139 EXPECT_EQ(ERR_IO_PENDING,
140 request.Request(destination,
141 is_https_,
142 privacy_mode_,
143 "GET",
144 net_log_,
145 callback_.callback()));
147 EXPECT_EQ(OK, callback_.WaitForResult());
148 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
149 EXPECT_TRUE(stream.get());
150 stream.reset();
152 QuicClientSession* session = QuicStreamFactoryPeer::GetActiveSession(
153 &factory_, destination, is_https_);
155 if (socket_count + 1 != socket_factory_.udp_client_sockets().size()) {
156 EXPECT_TRUE(false);
157 return 0;
160 IPEndPoint endpoint;
161 socket_factory_.
162 udp_client_sockets()[socket_count]->GetLocalAddress(&endpoint);
163 int port = endpoint.port();
164 if (goaway_received) {
165 QuicGoAwayFrame goaway(QUIC_NO_ERROR, 1, "");
166 session->OnGoAway(goaway);
169 factory_.OnSessionClosed(session);
170 EXPECT_EQ(NULL, CreateIfSessionExists(destination, net_log_).get());
171 EXPECT_TRUE(socket_data.at_read_eof());
172 EXPECT_TRUE(socket_data.at_write_eof());
173 return port;
176 scoped_ptr<QuicEncryptedPacket> ConstructRstPacket() {
177 QuicStreamId stream_id = kClientDataStreamId1;
178 return maker_.MakeRstPacket(
179 1, true, stream_id,
180 AdjustErrorForVersion(QUIC_RST_FLOW_CONTROL_ACCOUNTING, GetParam()));
183 MockHostResolver host_resolver_;
184 DeterministicMockClientSocketFactory socket_factory_;
185 MockCryptoClientStreamFactory crypto_client_stream_factory_;
186 MockRandom random_generator_;
187 QuicTestPacketMaker maker_;
188 MockClock* clock_; // Owned by factory_.
189 scoped_ptr<CertVerifier> cert_verifier_;
190 scoped_ptr<ServerBoundCertService> server_bound_cert_service_;
191 QuicStreamFactory factory_;
192 HostPortPair host_port_pair_;
193 bool is_https_;
194 PrivacyMode privacy_mode_;
195 BoundNetLog net_log_;
196 TestCompletionCallback callback_;
199 INSTANTIATE_TEST_CASE_P(Version, QuicStreamFactoryTest,
200 ::testing::ValuesIn(QuicSupportedVersions()));
202 TEST_P(QuicStreamFactoryTest, CreateIfSessionExists) {
203 EXPECT_EQ(NULL, CreateIfSessionExists(host_port_pair_, net_log_).get());
206 TEST_P(QuicStreamFactoryTest, Create) {
207 MockRead reads[] = {
208 MockRead(ASYNC, OK, 0) // EOF
210 DeterministicSocketData socket_data(reads, arraysize(reads), NULL, 0);
211 socket_factory_.AddSocketDataProvider(&socket_data);
212 socket_data.StopAfter(1);
214 QuicStreamRequest request(&factory_);
215 EXPECT_EQ(ERR_IO_PENDING,
216 request.Request(host_port_pair_,
217 is_https_,
218 privacy_mode_,
219 "GET",
220 net_log_,
221 callback_.callback()));
223 EXPECT_EQ(OK, callback_.WaitForResult());
224 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
225 EXPECT_TRUE(stream.get());
227 // Will reset stream 3.
228 stream = CreateIfSessionExists(host_port_pair_, net_log_);
229 EXPECT_TRUE(stream.get());
231 // TODO(rtenneti): We should probably have a tests that HTTP and HTTPS result
232 // in streams on different sessions.
233 QuicStreamRequest request2(&factory_);
234 EXPECT_EQ(OK,
235 request2.Request(host_port_pair_,
236 is_https_,
237 privacy_mode_,
238 "GET",
239 net_log_,
240 callback_.callback()));
241 stream = request2.ReleaseStream(); // Will reset stream 5.
242 stream.reset(); // Will reset stream 7.
244 EXPECT_TRUE(socket_data.at_read_eof());
245 EXPECT_TRUE(socket_data.at_write_eof());
248 TEST_P(QuicStreamFactoryTest, CreateZeroRtt) {
249 MockRead reads[] = {
250 MockRead(ASYNC, OK, 0) // EOF
252 DeterministicSocketData socket_data(reads, arraysize(reads), NULL, 0);
253 socket_factory_.AddSocketDataProvider(&socket_data);
254 socket_data.StopAfter(1);
256 crypto_client_stream_factory_.set_handshake_mode(
257 MockCryptoClientStream::ZERO_RTT);
258 host_resolver_.set_synchronous_mode(true);
259 host_resolver_.rules()->AddIPLiteralRule(host_port_pair_.host(),
260 "192.168.0.1", "");
262 QuicStreamRequest request(&factory_);
263 EXPECT_EQ(OK,
264 request.Request(host_port_pair_,
265 is_https_,
266 privacy_mode_,
267 "GET",
268 net_log_,
269 callback_.callback()));
271 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
272 EXPECT_TRUE(stream.get());
273 EXPECT_TRUE(socket_data.at_read_eof());
274 EXPECT_TRUE(socket_data.at_write_eof());
277 TEST_P(QuicStreamFactoryTest, CreateZeroRttPost) {
278 MockRead reads[] = {
279 MockRead(ASYNC, OK, 0) // EOF
281 DeterministicSocketData socket_data(reads, arraysize(reads), NULL, 0);
282 socket_factory_.AddSocketDataProvider(&socket_data);
283 socket_data.StopAfter(1);
285 crypto_client_stream_factory_.set_handshake_mode(
286 MockCryptoClientStream::ZERO_RTT);
287 host_resolver_.set_synchronous_mode(true);
288 host_resolver_.rules()->AddIPLiteralRule(host_port_pair_.host(),
289 "192.168.0.1", "");
291 QuicStreamRequest request(&factory_);
292 // Posts require handshake confirmation, so this will return asynchronously.
293 EXPECT_EQ(ERR_IO_PENDING,
294 request.Request(host_port_pair_,
295 is_https_,
296 privacy_mode_,
297 "POST",
298 net_log_,
299 callback_.callback()));
301 // Confirm the handshake and verify that the stream is created.
302 crypto_client_stream_factory_.last_stream()->SendOnCryptoHandshakeEvent(
303 QuicSession::HANDSHAKE_CONFIRMED);
305 EXPECT_EQ(OK, callback_.WaitForResult());
306 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
307 EXPECT_TRUE(stream.get());
308 EXPECT_TRUE(socket_data.at_read_eof());
309 EXPECT_TRUE(socket_data.at_write_eof());
312 TEST_P(QuicStreamFactoryTest, CreateHttpVsHttps) {
313 MockRead reads[] = {
314 MockRead(ASYNC, OK, 0) // EOF
316 DeterministicSocketData socket_data1(reads, arraysize(reads), NULL, 0);
317 DeterministicSocketData socket_data2(reads, arraysize(reads), NULL, 0);
318 socket_factory_.AddSocketDataProvider(&socket_data1);
319 socket_factory_.AddSocketDataProvider(&socket_data2);
320 socket_data1.StopAfter(1);
321 socket_data2.StopAfter(1);
323 QuicStreamRequest request(&factory_);
324 EXPECT_EQ(ERR_IO_PENDING,
325 request.Request(host_port_pair_,
326 is_https_,
327 privacy_mode_,
328 "GET",
329 net_log_,
330 callback_.callback()));
332 EXPECT_EQ(OK, callback_.WaitForResult());
333 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
334 EXPECT_TRUE(stream.get());
336 QuicStreamRequest request2(&factory_);
337 EXPECT_EQ(ERR_IO_PENDING,
338 request2.Request(host_port_pair_,
339 !is_https_,
340 privacy_mode_,
341 "GET",
342 net_log_,
343 callback_.callback()));
344 EXPECT_EQ(OK, callback_.WaitForResult());
345 stream = request2.ReleaseStream();
346 EXPECT_TRUE(stream.get());
347 stream.reset();
349 EXPECT_NE(QuicStreamFactoryPeer::GetActiveSession(
350 &factory_, host_port_pair_, is_https_),
351 QuicStreamFactoryPeer::GetActiveSession(
352 &factory_, host_port_pair_, !is_https_));
354 EXPECT_TRUE(socket_data1.at_read_eof());
355 EXPECT_TRUE(socket_data1.at_write_eof());
356 EXPECT_TRUE(socket_data2.at_read_eof());
357 EXPECT_TRUE(socket_data2.at_write_eof());
360 TEST_P(QuicStreamFactoryTest, Pooling) {
361 MockRead reads[] = {
362 MockRead(ASYNC, OK, 0) // EOF
364 DeterministicSocketData socket_data(reads, arraysize(reads), NULL, 0);
365 socket_factory_.AddSocketDataProvider(&socket_data);
366 socket_data.StopAfter(1);
368 HostPortPair server2("mail.google.com", kDefaultServerPort);
369 host_resolver_.set_synchronous_mode(true);
370 host_resolver_.rules()->AddIPLiteralRule(
371 kDefaultServerHostName, "192.168.0.1", "");
372 host_resolver_.rules()->AddIPLiteralRule(
373 "mail.google.com", "192.168.0.1", "");
375 QuicStreamRequest request(&factory_);
376 EXPECT_EQ(OK,
377 request.Request(host_port_pair_,
378 is_https_,
379 privacy_mode_,
380 "GET",
381 net_log_,
382 callback_.callback()));
383 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
384 EXPECT_TRUE(stream.get());
386 TestCompletionCallback callback;
387 QuicStreamRequest request2(&factory_);
388 EXPECT_EQ(OK,
389 request2.Request(server2,
390 is_https_,
391 privacy_mode_,
392 "GET",
393 net_log_,
394 callback.callback()));
395 scoped_ptr<QuicHttpStream> stream2 = request2.ReleaseStream();
396 EXPECT_TRUE(stream2.get());
398 EXPECT_EQ(
399 QuicStreamFactoryPeer::GetActiveSession(
400 &factory_, host_port_pair_, is_https_),
401 QuicStreamFactoryPeer::GetActiveSession(&factory_, server2, is_https_));
403 EXPECT_TRUE(socket_data.at_read_eof());
404 EXPECT_TRUE(socket_data.at_write_eof());
407 TEST_P(QuicStreamFactoryTest, NoPoolingAfterGoAway) {
408 MockRead reads[] = {
409 MockRead(ASYNC, OK, 0) // EOF
411 DeterministicSocketData socket_data1(reads, arraysize(reads), NULL, 0);
412 DeterministicSocketData socket_data2(reads, arraysize(reads), NULL, 0);
413 socket_factory_.AddSocketDataProvider(&socket_data1);
414 socket_factory_.AddSocketDataProvider(&socket_data2);
415 socket_data1.StopAfter(1);
416 socket_data2.StopAfter(1);
418 HostPortPair server2("mail.google.com", kDefaultServerPort);
419 host_resolver_.set_synchronous_mode(true);
420 host_resolver_.rules()->AddIPLiteralRule(
421 kDefaultServerHostName, "192.168.0.1", "");
422 host_resolver_.rules()->AddIPLiteralRule(
423 "mail.google.com", "192.168.0.1", "");
425 QuicStreamRequest request(&factory_);
426 EXPECT_EQ(OK,
427 request.Request(host_port_pair_,
428 is_https_,
429 privacy_mode_,
430 "GET",
431 net_log_,
432 callback_.callback()));
433 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
434 EXPECT_TRUE(stream.get());
436 TestCompletionCallback callback;
437 QuicStreamRequest request2(&factory_);
438 EXPECT_EQ(OK,
439 request2.Request(server2,
440 is_https_,
441 privacy_mode_,
442 "GET",
443 net_log_,
444 callback.callback()));
445 scoped_ptr<QuicHttpStream> stream2 = request2.ReleaseStream();
446 EXPECT_TRUE(stream2.get());
448 factory_.OnSessionGoingAway(QuicStreamFactoryPeer::GetActiveSession(
449 &factory_, host_port_pair_, is_https_));
450 EXPECT_FALSE(QuicStreamFactoryPeer::HasActiveSession(
451 &factory_, host_port_pair_, is_https_));
452 EXPECT_FALSE(QuicStreamFactoryPeer::HasActiveSession(
453 &factory_, server2, is_https_));
455 TestCompletionCallback callback3;
456 QuicStreamRequest request3(&factory_);
457 EXPECT_EQ(OK,
458 request3.Request(server2,
459 is_https_,
460 privacy_mode_,
461 "GET",
462 net_log_,
463 callback3.callback()));
464 scoped_ptr<QuicHttpStream> stream3 = request3.ReleaseStream();
465 EXPECT_TRUE(stream3.get());
467 EXPECT_TRUE(QuicStreamFactoryPeer::HasActiveSession(
468 &factory_, server2, is_https_));
470 EXPECT_TRUE(socket_data1.at_read_eof());
471 EXPECT_TRUE(socket_data1.at_write_eof());
472 EXPECT_TRUE(socket_data2.at_read_eof());
473 EXPECT_TRUE(socket_data2.at_write_eof());
476 TEST_P(QuicStreamFactoryTest, HttpsPooling) {
477 MockRead reads[] = {
478 MockRead(ASYNC, OK, 0) // EOF
480 DeterministicSocketData socket_data(reads, arraysize(reads), NULL, 0);
481 socket_factory_.AddSocketDataProvider(&socket_data);
482 socket_data.StopAfter(1);
484 HostPortPair server1("www.example.org", 443);
485 HostPortPair server2("mail.example.org", 443);
487 // Load a cert that is valid for:
488 // www.example.org (server1)
489 // mail.example.org (server2)
490 // www.example.com
491 base::FilePath certs_dir = GetTestCertsDirectory();
492 scoped_refptr<X509Certificate> test_cert(
493 ImportCertFromFile(certs_dir, "spdy_pooling.pem"));
494 ASSERT_NE(static_cast<X509Certificate*>(NULL), test_cert);
495 ProofVerifyDetailsChromium verify_details;
496 verify_details.cert_verify_result.verified_cert = test_cert;
497 crypto_client_stream_factory_.set_proof_verify_details(&verify_details);
499 host_resolver_.set_synchronous_mode(true);
500 host_resolver_.rules()->AddIPLiteralRule(server1.host(), "192.168.0.1", "");
501 host_resolver_.rules()->AddIPLiteralRule(server2.host(), "192.168.0.1", "");
503 QuicStreamRequest request(&factory_);
504 is_https_ = true;
505 EXPECT_EQ(OK,
506 request.Request(server1,
507 is_https_,
508 privacy_mode_,
509 "GET",
510 net_log_,
511 callback_.callback()));
512 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
513 EXPECT_TRUE(stream.get());
515 TestCompletionCallback callback;
516 QuicStreamRequest request2(&factory_);
517 EXPECT_EQ(OK,
518 request2.Request(server2,
519 is_https_,
520 privacy_mode_,
521 "GET",
522 net_log_,
523 callback_.callback()));
524 scoped_ptr<QuicHttpStream> stream2 = request2.ReleaseStream();
525 EXPECT_TRUE(stream2.get());
527 EXPECT_EQ(QuicStreamFactoryPeer::GetActiveSession(
528 &factory_, server1, is_https_),
529 QuicStreamFactoryPeer::GetActiveSession(
530 &factory_, server2, is_https_));
532 EXPECT_TRUE(socket_data.at_read_eof());
533 EXPECT_TRUE(socket_data.at_write_eof());
536 TEST_P(QuicStreamFactoryTest, NoHttpsPoolingWithCertMismatch) {
537 MockRead reads[] = {
538 MockRead(ASYNC, OK, 0) // EOF
540 DeterministicSocketData socket_data1(reads, arraysize(reads), NULL, 0);
541 DeterministicSocketData socket_data2(reads, arraysize(reads), NULL, 0);
542 socket_factory_.AddSocketDataProvider(&socket_data1);
543 socket_factory_.AddSocketDataProvider(&socket_data2);
544 socket_data1.StopAfter(1);
545 socket_data2.StopAfter(1);
547 HostPortPair server1("www.example.org", 443);
548 HostPortPair server2("mail.google.com", 443);
550 // Load a cert that is valid for:
551 // www.example.org (server1)
552 // mail.example.org
553 // www.example.com
554 // But is not valid for mail.google.com (server2).
555 base::FilePath certs_dir = GetTestCertsDirectory();
556 scoped_refptr<X509Certificate> test_cert(
557 ImportCertFromFile(certs_dir, "spdy_pooling.pem"));
558 ASSERT_NE(static_cast<X509Certificate*>(NULL), test_cert);
559 ProofVerifyDetailsChromium verify_details;
560 verify_details.cert_verify_result.verified_cert = test_cert;
561 crypto_client_stream_factory_.set_proof_verify_details(&verify_details);
564 host_resolver_.set_synchronous_mode(true);
565 host_resolver_.rules()->AddIPLiteralRule(server1.host(), "192.168.0.1", "");
566 host_resolver_.rules()->AddIPLiteralRule(server2.host(), "192.168.0.1", "");
568 QuicStreamRequest request(&factory_);
569 is_https_ = true;
570 EXPECT_EQ(OK,
571 request.Request(server1,
572 is_https_,
573 privacy_mode_,
574 "GET",
575 net_log_,
576 callback_.callback()));
577 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
578 EXPECT_TRUE(stream.get());
580 TestCompletionCallback callback;
581 QuicStreamRequest request2(&factory_);
582 EXPECT_EQ(OK,
583 request2.Request(server2,
584 is_https_,
585 privacy_mode_,
586 "GET",
587 net_log_,
588 callback_.callback()));
589 scoped_ptr<QuicHttpStream> stream2 = request2.ReleaseStream();
590 EXPECT_TRUE(stream2.get());
592 EXPECT_NE(QuicStreamFactoryPeer::GetActiveSession(
593 &factory_, server1, is_https_),
594 QuicStreamFactoryPeer::GetActiveSession(
595 &factory_, server2, is_https_));
597 EXPECT_TRUE(socket_data1.at_read_eof());
598 EXPECT_TRUE(socket_data1.at_write_eof());
599 EXPECT_TRUE(socket_data2.at_read_eof());
600 EXPECT_TRUE(socket_data2.at_write_eof());
603 TEST_P(QuicStreamFactoryTest, Goaway) {
604 MockRead reads[] = {
605 MockRead(ASYNC, OK, 0) // EOF
607 DeterministicSocketData socket_data(reads, arraysize(reads), NULL, 0);
608 socket_data.StopAfter(1);
609 socket_factory_.AddSocketDataProvider(&socket_data);
610 DeterministicSocketData socket_data2(reads, arraysize(reads), NULL, 0);
611 socket_data2.StopAfter(1);
612 socket_factory_.AddSocketDataProvider(&socket_data2);
614 QuicStreamRequest request(&factory_);
615 EXPECT_EQ(ERR_IO_PENDING,
616 request.Request(host_port_pair_,
617 is_https_,
618 privacy_mode_,
619 "GET",
620 net_log_,
621 callback_.callback()));
623 EXPECT_EQ(OK, callback_.WaitForResult());
624 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
625 EXPECT_TRUE(stream.get());
627 // Mark the session as going away. Ensure that while it is still alive
628 // that it is no longer active.
629 QuicClientSession* session = QuicStreamFactoryPeer::GetActiveSession(
630 &factory_, host_port_pair_, is_https_);
631 factory_.OnSessionGoingAway(session);
632 EXPECT_EQ(true, QuicStreamFactoryPeer::IsLiveSession(&factory_, session));
633 EXPECT_FALSE(QuicStreamFactoryPeer::HasActiveSession(
634 &factory_, host_port_pair_, is_https_));
635 EXPECT_EQ(NULL, CreateIfSessionExists(host_port_pair_, net_log_).get());
637 // Create a new request for the same destination and verify that a
638 // new session is created.
639 QuicStreamRequest request2(&factory_);
640 EXPECT_EQ(ERR_IO_PENDING,
641 request2.Request(host_port_pair_,
642 is_https_,
643 privacy_mode_,
644 "GET",
645 net_log_,
646 callback_.callback()));
647 EXPECT_EQ(OK, callback_.WaitForResult());
648 scoped_ptr<QuicHttpStream> stream2 = request2.ReleaseStream();
649 EXPECT_TRUE(stream2.get());
651 EXPECT_TRUE(QuicStreamFactoryPeer::HasActiveSession(&factory_,
652 host_port_pair_,
653 is_https_));
654 EXPECT_NE(session,
655 QuicStreamFactoryPeer::GetActiveSession(
656 &factory_, host_port_pair_, is_https_));
657 EXPECT_EQ(true, QuicStreamFactoryPeer::IsLiveSession(&factory_, session));
659 stream2.reset();
660 stream.reset();
662 EXPECT_TRUE(socket_data.at_read_eof());
663 EXPECT_TRUE(socket_data.at_write_eof());
664 EXPECT_TRUE(socket_data2.at_read_eof());
665 EXPECT_TRUE(socket_data2.at_write_eof());
668 TEST_P(QuicStreamFactoryTest, MaxOpenStream) {
669 MockRead reads[] = {
670 MockRead(ASYNC, OK, 0) // EOF
672 QuicStreamId stream_id = kClientDataStreamId1;
673 scoped_ptr<QuicEncryptedPacket> rst(
674 maker_.MakeRstPacket(1, true, stream_id, QUIC_STREAM_CANCELLED));
675 MockWrite writes[] = {
676 MockWrite(ASYNC, rst->data(), rst->length(), 1),
678 DeterministicSocketData socket_data(reads, arraysize(reads),
679 writes, arraysize(writes));
680 socket_factory_.AddSocketDataProvider(&socket_data);
681 socket_data.StopAfter(1);
683 HttpRequestInfo request_info;
684 std::vector<QuicHttpStream*> streams;
685 // The MockCryptoClientStream sets max_open_streams to be
686 // 2 * kDefaultMaxStreamsPerConnection.
687 for (size_t i = 0; i < 2 * kDefaultMaxStreamsPerConnection; i++) {
688 QuicStreamRequest request(&factory_);
689 int rv = request.Request(host_port_pair_,
690 is_https_,
691 privacy_mode_,
692 "GET",
693 net_log_,
694 callback_.callback());
695 if (i == 0) {
696 EXPECT_EQ(ERR_IO_PENDING, rv);
697 EXPECT_EQ(OK, callback_.WaitForResult());
698 } else {
699 EXPECT_EQ(OK, rv);
701 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
702 EXPECT_TRUE(stream);
703 EXPECT_EQ(OK, stream->InitializeStream(
704 &request_info, DEFAULT_PRIORITY, net_log_, CompletionCallback()));
705 streams.push_back(stream.release());
708 QuicStreamRequest request(&factory_);
709 EXPECT_EQ(OK,
710 request.Request(host_port_pair_,
711 is_https_,
712 privacy_mode_,
713 "GET",
714 net_log_,
715 CompletionCallback()));
716 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
717 EXPECT_TRUE(stream);
718 EXPECT_EQ(ERR_IO_PENDING, stream->InitializeStream(
719 &request_info, DEFAULT_PRIORITY, net_log_, callback_.callback()));
721 // Close the first stream.
722 streams.front()->Close(false);
724 ASSERT_TRUE(callback_.have_result());
726 EXPECT_EQ(OK, callback_.WaitForResult());
728 EXPECT_TRUE(socket_data.at_read_eof());
729 EXPECT_TRUE(socket_data.at_write_eof());
730 STLDeleteElements(&streams);
733 TEST_P(QuicStreamFactoryTest, ResolutionErrorInCreate) {
734 DeterministicSocketData socket_data(NULL, 0, NULL, 0);
735 socket_factory_.AddSocketDataProvider(&socket_data);
737 host_resolver_.rules()->AddSimulatedFailure(kDefaultServerHostName);
739 QuicStreamRequest request(&factory_);
740 EXPECT_EQ(ERR_IO_PENDING,
741 request.Request(host_port_pair_,
742 is_https_,
743 privacy_mode_,
744 "GET",
745 net_log_,
746 callback_.callback()));
748 EXPECT_EQ(ERR_NAME_NOT_RESOLVED, callback_.WaitForResult());
750 EXPECT_TRUE(socket_data.at_read_eof());
751 EXPECT_TRUE(socket_data.at_write_eof());
754 TEST_P(QuicStreamFactoryTest, ConnectErrorInCreate) {
755 MockConnect connect(SYNCHRONOUS, ERR_ADDRESS_IN_USE);
756 DeterministicSocketData socket_data(NULL, 0, NULL, 0);
757 socket_data.set_connect_data(connect);
758 socket_factory_.AddSocketDataProvider(&socket_data);
759 socket_data.StopAfter(1);
761 QuicStreamRequest request(&factory_);
762 EXPECT_EQ(ERR_IO_PENDING,
763 request.Request(host_port_pair_,
764 is_https_,
765 privacy_mode_,
766 "GET",
767 net_log_,
768 callback_.callback()));
770 EXPECT_EQ(ERR_ADDRESS_IN_USE, callback_.WaitForResult());
772 EXPECT_TRUE(socket_data.at_read_eof());
773 EXPECT_TRUE(socket_data.at_write_eof());
776 TEST_P(QuicStreamFactoryTest, CancelCreate) {
777 MockRead reads[] = {
778 MockRead(ASYNC, OK, 0) // EOF
780 DeterministicSocketData socket_data(reads, arraysize(reads), NULL, 0);
781 socket_factory_.AddSocketDataProvider(&socket_data);
783 QuicStreamRequest request(&factory_);
784 EXPECT_EQ(ERR_IO_PENDING,
785 request.Request(host_port_pair_,
786 is_https_,
787 privacy_mode_,
788 "GET",
789 net_log_,
790 callback_.callback()));
793 socket_data.StopAfter(1);
794 base::RunLoop run_loop;
795 run_loop.RunUntilIdle();
797 scoped_ptr<QuicHttpStream> stream(
798 CreateIfSessionExists(host_port_pair_, net_log_));
799 EXPECT_TRUE(stream.get());
800 stream.reset();
802 EXPECT_TRUE(socket_data.at_read_eof());
803 EXPECT_TRUE(socket_data.at_write_eof());
806 TEST_P(QuicStreamFactoryTest, CreateConsistentEphemeralPort) {
807 // Sequentially connect to the default host, then another host, and then the
808 // default host. Verify that the default host gets a consistent ephemeral
809 // port, that is different from the other host's connection.
811 std::string other_server_name = "other.google.com";
812 EXPECT_NE(kDefaultServerHostName, other_server_name);
813 HostPortPair host_port_pair2(other_server_name, kDefaultServerPort);
815 int original_port = GetSourcePortForNewSession(host_port_pair_);
816 EXPECT_NE(original_port, GetSourcePortForNewSession(host_port_pair2));
817 EXPECT_EQ(original_port, GetSourcePortForNewSession(host_port_pair_));
820 TEST_P(QuicStreamFactoryTest, GoAwayDisablesConsistentEphemeralPort) {
821 // Get a session to the host using the port suggester.
822 int original_port =
823 GetSourcePortForNewSessionAndGoAway(host_port_pair_);
824 // Verify that the port is different after the goaway.
825 EXPECT_NE(original_port, GetSourcePortForNewSession(host_port_pair_));
826 // Since the previous session did not goaway we should see the original port.
827 EXPECT_EQ(original_port, GetSourcePortForNewSession(host_port_pair_));
830 TEST_P(QuicStreamFactoryTest, CloseAllSessions) {
831 MockRead reads[] = {
832 MockRead(ASYNC, 0, 0) // EOF
834 scoped_ptr<QuicEncryptedPacket> rst(ConstructRstPacket());
835 std::vector<MockWrite> writes;
836 writes.push_back(MockWrite(ASYNC, rst->data(), rst->length(), 1));
837 DeterministicSocketData socket_data(reads, arraysize(reads),
838 writes.empty() ? NULL : &writes[0],
839 writes.size());
840 socket_factory_.AddSocketDataProvider(&socket_data);
841 socket_data.StopAfter(1);
843 MockRead reads2[] = {
844 MockRead(ASYNC, 0, 0) // EOF
846 DeterministicSocketData socket_data2(reads2, arraysize(reads2), NULL, 0);
847 socket_factory_.AddSocketDataProvider(&socket_data2);
848 socket_data2.StopAfter(1);
850 QuicStreamRequest request(&factory_);
851 EXPECT_EQ(ERR_IO_PENDING,
852 request.Request(host_port_pair_,
853 is_https_,
854 privacy_mode_,
855 "GET",
856 net_log_,
857 callback_.callback()));
859 EXPECT_EQ(OK, callback_.WaitForResult());
860 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
861 HttpRequestInfo request_info;
862 EXPECT_EQ(OK, stream->InitializeStream(&request_info,
863 DEFAULT_PRIORITY,
864 net_log_, CompletionCallback()));
866 // Close the session and verify that stream saw the error.
867 factory_.CloseAllSessions(ERR_INTERNET_DISCONNECTED);
868 EXPECT_EQ(ERR_INTERNET_DISCONNECTED,
869 stream->ReadResponseHeaders(callback_.callback()));
871 // Now attempting to request a stream to the same origin should create
872 // a new session.
874 QuicStreamRequest request2(&factory_);
875 EXPECT_EQ(ERR_IO_PENDING,
876 request2.Request(host_port_pair_,
877 is_https_,
878 privacy_mode_,
879 "GET",
880 net_log_,
881 callback_.callback()));
883 EXPECT_EQ(OK, callback_.WaitForResult());
884 stream = request2.ReleaseStream();
885 stream.reset(); // Will reset stream 3.
887 EXPECT_TRUE(socket_data.at_read_eof());
888 EXPECT_TRUE(socket_data.at_write_eof());
889 EXPECT_TRUE(socket_data2.at_read_eof());
890 EXPECT_TRUE(socket_data2.at_write_eof());
893 TEST_P(QuicStreamFactoryTest, OnIPAddressChanged) {
894 MockRead reads[] = {
895 MockRead(ASYNC, 0, 0) // EOF
897 scoped_ptr<QuicEncryptedPacket> rst(ConstructRstPacket());
898 std::vector<MockWrite> writes;
899 writes.push_back(MockWrite(ASYNC, rst->data(), rst->length(), 1));
900 DeterministicSocketData socket_data(reads, arraysize(reads),
901 writes.empty() ? NULL : &writes[0],
902 writes.size());
903 socket_factory_.AddSocketDataProvider(&socket_data);
904 socket_data.StopAfter(1);
906 MockRead reads2[] = {
907 MockRead(ASYNC, 0, 0) // EOF
909 DeterministicSocketData socket_data2(reads2, arraysize(reads2), NULL, 0);
910 socket_factory_.AddSocketDataProvider(&socket_data2);
911 socket_data2.StopAfter(1);
913 QuicStreamRequest request(&factory_);
914 EXPECT_EQ(ERR_IO_PENDING,
915 request.Request(host_port_pair_,
916 is_https_,
917 privacy_mode_,
918 "GET",
919 net_log_,
920 callback_.callback()));
922 EXPECT_EQ(OK, callback_.WaitForResult());
923 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
924 HttpRequestInfo request_info;
925 EXPECT_EQ(OK, stream->InitializeStream(&request_info,
926 DEFAULT_PRIORITY,
927 net_log_, CompletionCallback()));
929 // Change the IP address and verify that stream saw the error.
930 factory_.OnIPAddressChanged();
931 EXPECT_EQ(ERR_NETWORK_CHANGED,
932 stream->ReadResponseHeaders(callback_.callback()));
933 EXPECT_TRUE(factory_.require_confirmation());
935 // Now attempting to request a stream to the same origin should create
936 // a new session.
938 QuicStreamRequest request2(&factory_);
939 EXPECT_EQ(ERR_IO_PENDING,
940 request2.Request(host_port_pair_,
941 is_https_,
942 privacy_mode_,
943 "GET",
944 net_log_,
945 callback_.callback()));
947 EXPECT_EQ(OK, callback_.WaitForResult());
948 stream = request2.ReleaseStream();
949 stream.reset(); // Will reset stream 3.
951 EXPECT_TRUE(socket_data.at_read_eof());
952 EXPECT_TRUE(socket_data.at_write_eof());
953 EXPECT_TRUE(socket_data2.at_read_eof());
954 EXPECT_TRUE(socket_data2.at_write_eof());
957 TEST_P(QuicStreamFactoryTest, OnCertAdded) {
958 MockRead reads[] = {
959 MockRead(ASYNC, 0, 0) // EOF
961 scoped_ptr<QuicEncryptedPacket> rst(ConstructRstPacket());
962 std::vector<MockWrite> writes;
963 writes.push_back(MockWrite(ASYNC, rst->data(), rst->length(), 1));
964 DeterministicSocketData socket_data(reads, arraysize(reads),
965 writes.empty() ? NULL : &writes[0],
966 writes.size());
967 socket_factory_.AddSocketDataProvider(&socket_data);
968 socket_data.StopAfter(1);
970 MockRead reads2[] = {
971 MockRead(ASYNC, 0, 0) // EOF
973 DeterministicSocketData socket_data2(reads2, arraysize(reads2), NULL, 0);
974 socket_factory_.AddSocketDataProvider(&socket_data2);
975 socket_data2.StopAfter(1);
977 QuicStreamRequest request(&factory_);
978 EXPECT_EQ(ERR_IO_PENDING,
979 request.Request(host_port_pair_,
980 is_https_,
981 privacy_mode_,
982 "GET",
983 net_log_,
984 callback_.callback()));
986 EXPECT_EQ(OK, callback_.WaitForResult());
987 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
988 HttpRequestInfo request_info;
989 EXPECT_EQ(OK, stream->InitializeStream(&request_info,
990 DEFAULT_PRIORITY,
991 net_log_, CompletionCallback()));
993 // Add a cert and verify that stream saw the event.
994 factory_.OnCertAdded(NULL);
995 EXPECT_EQ(ERR_CERT_DATABASE_CHANGED,
996 stream->ReadResponseHeaders(callback_.callback()));
997 EXPECT_FALSE(factory_.require_confirmation());
999 // Now attempting to request a stream to the same origin should create
1000 // a new session.
1002 QuicStreamRequest request2(&factory_);
1003 EXPECT_EQ(ERR_IO_PENDING,
1004 request2.Request(host_port_pair_,
1005 is_https_,
1006 privacy_mode_,
1007 "GET",
1008 net_log_,
1009 callback_.callback()));
1011 EXPECT_EQ(OK, callback_.WaitForResult());
1012 stream = request2.ReleaseStream();
1013 stream.reset(); // Will reset stream 3.
1015 EXPECT_TRUE(socket_data.at_read_eof());
1016 EXPECT_TRUE(socket_data.at_write_eof());
1017 EXPECT_TRUE(socket_data2.at_read_eof());
1018 EXPECT_TRUE(socket_data2.at_write_eof());
1021 TEST_P(QuicStreamFactoryTest, OnCACertChanged) {
1022 MockRead reads[] = {
1023 MockRead(ASYNC, 0, 0) // EOF
1025 scoped_ptr<QuicEncryptedPacket> rst(ConstructRstPacket());
1026 std::vector<MockWrite> writes;
1027 writes.push_back(MockWrite(ASYNC, rst->data(), rst->length(), 1));
1028 DeterministicSocketData socket_data(reads, arraysize(reads),
1029 writes.empty() ? NULL : &writes[0],
1030 writes.size());
1031 socket_factory_.AddSocketDataProvider(&socket_data);
1032 socket_data.StopAfter(1);
1034 MockRead reads2[] = {
1035 MockRead(ASYNC, 0, 0) // EOF
1037 DeterministicSocketData socket_data2(reads2, arraysize(reads2), NULL, 0);
1038 socket_factory_.AddSocketDataProvider(&socket_data2);
1039 socket_data2.StopAfter(1);
1041 QuicStreamRequest request(&factory_);
1042 EXPECT_EQ(ERR_IO_PENDING,
1043 request.Request(host_port_pair_,
1044 is_https_,
1045 privacy_mode_,
1046 "GET",
1047 net_log_,
1048 callback_.callback()));
1050 EXPECT_EQ(OK, callback_.WaitForResult());
1051 scoped_ptr<QuicHttpStream> stream = request.ReleaseStream();
1052 HttpRequestInfo request_info;
1053 EXPECT_EQ(OK, stream->InitializeStream(&request_info,
1054 DEFAULT_PRIORITY,
1055 net_log_, CompletionCallback()));
1057 // Change the CA cert and verify that stream saw the event.
1058 factory_.OnCACertChanged(NULL);
1059 EXPECT_EQ(ERR_CERT_DATABASE_CHANGED,
1060 stream->ReadResponseHeaders(callback_.callback()));
1061 EXPECT_FALSE(factory_.require_confirmation());
1063 // Now attempting to request a stream to the same origin should create
1064 // a new session.
1066 QuicStreamRequest request2(&factory_);
1067 EXPECT_EQ(ERR_IO_PENDING,
1068 request2.Request(host_port_pair_,
1069 is_https_,
1070 privacy_mode_,
1071 "GET",
1072 net_log_,
1073 callback_.callback()));
1075 EXPECT_EQ(OK, callback_.WaitForResult());
1076 stream = request2.ReleaseStream();
1077 stream.reset(); // Will reset stream 3.
1079 EXPECT_TRUE(socket_data.at_read_eof());
1080 EXPECT_TRUE(socket_data.at_write_eof());
1081 EXPECT_TRUE(socket_data2.at_read_eof());
1082 EXPECT_TRUE(socket_data2.at_write_eof());
1085 TEST_P(QuicStreamFactoryTest, SharedCryptoConfig) {
1086 vector<string> cannoncial_suffixes;
1087 cannoncial_suffixes.push_back(string(".c.youtube.com"));
1088 cannoncial_suffixes.push_back(string(".googlevideo.com"));
1090 for (unsigned i = 0; i < cannoncial_suffixes.size(); ++i) {
1091 string r1_host_name("r1");
1092 string r2_host_name("r2");
1093 r1_host_name.append(cannoncial_suffixes[i]);
1094 r2_host_name.append(cannoncial_suffixes[i]);
1096 HostPortPair host_port_pair1(r1_host_name, 80);
1097 QuicCryptoClientConfig* crypto_config =
1098 QuicStreamFactoryPeer::GetCryptoConfig(&factory_);
1099 QuicServerId server_id1(host_port_pair1, is_https_, privacy_mode_);
1100 QuicCryptoClientConfig::CachedState* cached1 =
1101 crypto_config->LookupOrCreate(server_id1);
1102 EXPECT_FALSE(cached1->proof_valid());
1103 EXPECT_TRUE(cached1->source_address_token().empty());
1105 // Mutate the cached1 to have different data.
1106 // TODO(rtenneti): mutate other members of CachedState.
1107 cached1->set_source_address_token(r1_host_name);
1108 cached1->SetProofValid();
1110 HostPortPair host_port_pair2(r2_host_name, 80);
1111 QuicServerId server_id2(host_port_pair2, is_https_, privacy_mode_);
1112 QuicCryptoClientConfig::CachedState* cached2 =
1113 crypto_config->LookupOrCreate(server_id2);
1114 EXPECT_EQ(cached1->source_address_token(), cached2->source_address_token());
1115 EXPECT_TRUE(cached2->proof_valid());
1119 TEST_P(QuicStreamFactoryTest, CryptoConfigWhenProofIsInvalid) {
1120 vector<string> cannoncial_suffixes;
1121 cannoncial_suffixes.push_back(string(".c.youtube.com"));
1122 cannoncial_suffixes.push_back(string(".googlevideo.com"));
1124 for (unsigned i = 0; i < cannoncial_suffixes.size(); ++i) {
1125 string r3_host_name("r3");
1126 string r4_host_name("r4");
1127 r3_host_name.append(cannoncial_suffixes[i]);
1128 r4_host_name.append(cannoncial_suffixes[i]);
1130 HostPortPair host_port_pair1(r3_host_name, 80);
1131 QuicCryptoClientConfig* crypto_config =
1132 QuicStreamFactoryPeer::GetCryptoConfig(&factory_);
1133 QuicServerId server_id1(host_port_pair1, is_https_, privacy_mode_);
1134 QuicCryptoClientConfig::CachedState* cached1 =
1135 crypto_config->LookupOrCreate(server_id1);
1136 EXPECT_FALSE(cached1->proof_valid());
1137 EXPECT_TRUE(cached1->source_address_token().empty());
1139 // Mutate the cached1 to have different data.
1140 // TODO(rtenneti): mutate other members of CachedState.
1141 cached1->set_source_address_token(r3_host_name);
1142 cached1->SetProofInvalid();
1144 HostPortPair host_port_pair2(r4_host_name, 80);
1145 QuicServerId server_id2(host_port_pair2, is_https_, privacy_mode_);
1146 QuicCryptoClientConfig::CachedState* cached2 =
1147 crypto_config->LookupOrCreate(server_id2);
1148 EXPECT_NE(cached1->source_address_token(), cached2->source_address_token());
1149 EXPECT_TRUE(cached2->source_address_token().empty());
1150 EXPECT_FALSE(cached2->proof_valid());
1154 } // namespace test
1155 } // namespace net