Rewrite AndroidSyncSettings to be significantly simpler.
[chromium-blink-merge.git] / net / quic / quic_network_transaction_unittest.cc
blob0410c5973d7eb5c949470d0089ef25703f33e818
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 <vector>
7 #include "base/basictypes.h"
8 #include "base/compiler_specific.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/stl_util.h"
11 #include "net/base/capturing_net_log.h"
12 #include "net/base/net_log_unittest.h"
13 #include "net/base/test_completion_callback.h"
14 #include "net/cert/mock_cert_verifier.h"
15 #include "net/dns/mock_host_resolver.h"
16 #include "net/http/http_auth_handler_factory.h"
17 #include "net/http/http_network_session.h"
18 #include "net/http/http_network_transaction.h"
19 #include "net/http/http_server_properties_impl.h"
20 #include "net/http/http_stream.h"
21 #include "net/http/http_stream_factory.h"
22 #include "net/http/http_transaction_test_util.h"
23 #include "net/http/transport_security_state.h"
24 #include "net/proxy/proxy_config_service_fixed.h"
25 #include "net/proxy/proxy_resolver.h"
26 #include "net/proxy/proxy_service.h"
27 #include "net/quic/crypto/quic_decrypter.h"
28 #include "net/quic/crypto/quic_encrypter.h"
29 #include "net/quic/quic_framer.h"
30 #include "net/quic/quic_http_utils.h"
31 #include "net/quic/test_tools/crypto_test_utils.h"
32 #include "net/quic/test_tools/mock_clock.h"
33 #include "net/quic/test_tools/mock_crypto_client_stream_factory.h"
34 #include "net/quic/test_tools/mock_random.h"
35 #include "net/quic/test_tools/quic_test_packet_maker.h"
36 #include "net/quic/test_tools/quic_test_utils.h"
37 #include "net/socket/client_socket_factory.h"
38 #include "net/socket/mock_client_socket_pool_manager.h"
39 #include "net/socket/socket_test_util.h"
40 #include "net/socket/ssl_client_socket.h"
41 #include "net/spdy/spdy_frame_builder.h"
42 #include "net/spdy/spdy_framer.h"
43 #include "net/ssl/ssl_config_service_defaults.h"
44 #include "testing/gtest/include/gtest/gtest.h"
45 #include "testing/platform_test.h"
47 //-----------------------------------------------------------------------------
49 namespace {
51 // This is the expected return from a current server advertising QUIC.
52 static const char kQuicAlternateProtocolHttpHeader[] =
53 "Alternate-Protocol: 80:quic\r\n\r\n";
54 static const char kQuicAlternateProtocol50pctHttpHeader[] =
55 "Alternate-Protocol: 80:quic,p=.5\r\n\r\n";
56 static const char kQuicAlternateProtocolHttpsHeader[] =
57 "Alternate-Protocol: 443:quic\r\n\r\n";
59 } // namespace
61 namespace net {
62 namespace test {
64 // Helper class to encapsulate MockReads and MockWrites for QUIC.
65 // Simplify ownership issues and the interaction with the MockSocketFactory.
66 class MockQuicData {
67 public:
68 ~MockQuicData() {
69 STLDeleteElements(&packets_);
72 void AddRead(scoped_ptr<QuicEncryptedPacket> packet) {
73 reads_.push_back(MockRead(SYNCHRONOUS, packet->data(), packet->length(),
74 sequence_number_++));
75 packets_.push_back(packet.release());
78 void AddRead(IoMode mode, int rv) {
79 reads_.push_back(MockRead(mode, rv));
82 void AddWrite(scoped_ptr<QuicEncryptedPacket> packet) {
83 writes_.push_back(MockWrite(SYNCHRONOUS, packet->data(), packet->length(),
84 sequence_number_++));
85 packets_.push_back(packet.release());
88 void AddDelayedSocketDataToFactory(MockClientSocketFactory* factory,
89 size_t delay) {
90 MockRead* reads = reads_.empty() ? nullptr : &reads_[0];
91 MockWrite* writes = writes_.empty() ? nullptr : &writes_[0];
92 socket_data_.reset(new DelayedSocketData(
93 delay, reads, reads_.size(), writes, writes_.size()));
94 factory->AddSocketDataProvider(socket_data_.get());
97 private:
98 std::vector<QuicEncryptedPacket*> packets_;
99 std::vector<MockWrite> writes_;
100 std::vector<MockRead> reads_;
101 size_t sequence_number_;
102 scoped_ptr<SocketDataProvider> socket_data_;
105 class ProxyHeadersHandler {
106 public:
107 ProxyHeadersHandler() : was_called_(false) {}
109 bool was_called() { return was_called_; }
111 void OnBeforeProxyHeadersSent(const ProxyInfo& proxy_info,
112 HttpRequestHeaders* request_headers) {
113 was_called_ = true;
116 private:
117 bool was_called_;
120 class QuicNetworkTransactionTest
121 : public PlatformTest,
122 public ::testing::WithParamInterface<QuicVersion> {
123 protected:
124 QuicNetworkTransactionTest()
125 : clock_(new MockClock),
126 maker_(GetParam(), 0, clock_),
127 ssl_config_service_(new SSLConfigServiceDefaults),
128 proxy_service_(ProxyService::CreateDirect()),
129 auth_handler_factory_(
130 HttpAuthHandlerFactory::CreateDefault(&host_resolver_)),
131 random_generator_(0),
132 hanging_data_(nullptr, 0, nullptr, 0) {
133 request_.method = "GET";
134 request_.url = GURL("http://www.google.com/");
135 request_.load_flags = 0;
136 clock_->AdvanceTime(QuicTime::Delta::FromMilliseconds(20));
139 void SetUp() override {
140 NetworkChangeNotifier::NotifyObserversOfIPAddressChangeForTests();
141 base::MessageLoop::current()->RunUntilIdle();
144 void TearDown() override {
145 NetworkChangeNotifier::NotifyObserversOfIPAddressChangeForTests();
146 // Empty the current queue.
147 base::MessageLoop::current()->RunUntilIdle();
148 PlatformTest::TearDown();
149 NetworkChangeNotifier::NotifyObserversOfIPAddressChangeForTests();
150 base::MessageLoop::current()->RunUntilIdle();
153 scoped_ptr<QuicEncryptedPacket> ConstructConnectionClosePacket(
154 QuicPacketSequenceNumber num) {
155 return maker_.MakeConnectionClosePacket(num);
158 scoped_ptr<QuicEncryptedPacket> ConstructAckPacket(
159 QuicPacketSequenceNumber largest_received,
160 QuicPacketSequenceNumber least_unacked) {
161 return maker_.MakeAckPacket(2, largest_received, least_unacked, true);
164 SpdyHeaderBlock GetRequestHeaders(const std::string& method,
165 const std::string& scheme,
166 const std::string& path) {
167 return maker_.GetRequestHeaders(method, scheme, path);
170 SpdyHeaderBlock GetResponseHeaders(const std::string& status) {
171 return maker_.GetResponseHeaders(status);
174 scoped_ptr<QuicEncryptedPacket> ConstructDataPacket(
175 QuicPacketSequenceNumber sequence_number,
176 QuicStreamId stream_id,
177 bool should_include_version,
178 bool fin,
179 QuicStreamOffset offset,
180 base::StringPiece data) {
181 return maker_.MakeDataPacket(
182 sequence_number, stream_id, should_include_version, fin, offset, data);
185 scoped_ptr<QuicEncryptedPacket> ConstructRequestHeadersPacket(
186 QuicPacketSequenceNumber sequence_number,
187 QuicStreamId stream_id,
188 bool should_include_version,
189 bool fin,
190 const SpdyHeaderBlock& headers) {
191 QuicPriority priority =
192 ConvertRequestPriorityToQuicPriority(DEFAULT_PRIORITY);
193 return maker_.MakeRequestHeadersPacket(sequence_number, stream_id,
194 should_include_version, fin,
195 priority, headers);
198 scoped_ptr<QuicEncryptedPacket> ConstructResponseHeadersPacket(
199 QuicPacketSequenceNumber sequence_number,
200 QuicStreamId stream_id,
201 bool should_include_version,
202 bool fin,
203 const SpdyHeaderBlock& headers) {
204 return maker_.MakeResponseHeadersPacket(
205 sequence_number, stream_id, should_include_version, fin, headers);
208 void CreateSession() {
209 CreateSessionWithFactory(&socket_factory_, false);
212 void CreateSessionWithNextProtos() {
213 CreateSessionWithFactory(&socket_factory_, true);
216 // If |use_next_protos| is true, enables SPDY and QUIC.
217 void CreateSessionWithFactory(ClientSocketFactory* socket_factory,
218 bool use_next_protos) {
219 params_.enable_quic = true;
220 params_.quic_clock = clock_;
221 params_.quic_random = &random_generator_;
222 params_.client_socket_factory = socket_factory;
223 params_.quic_crypto_client_stream_factory = &crypto_client_stream_factory_;
224 params_.host_resolver = &host_resolver_;
225 params_.cert_verifier = &cert_verifier_;
226 params_.transport_security_state = &transport_security_state_;
227 params_.proxy_service = proxy_service_.get();
228 params_.ssl_config_service = ssl_config_service_.get();
229 params_.http_auth_handler_factory = auth_handler_factory_.get();
230 params_.http_server_properties = http_server_properties.GetWeakPtr();
231 params_.quic_supported_versions = SupportedVersions(GetParam());
233 if (use_next_protos) {
234 params_.use_alternate_protocols = true;
235 params_.next_protos = NextProtosWithSpdyAndQuic(true, true);
238 session_ = new HttpNetworkSession(params_);
239 session_->quic_stream_factory()->set_require_confirmation(false);
242 void CheckWasQuicResponse(const scoped_ptr<HttpNetworkTransaction>& trans) {
243 const HttpResponseInfo* response = trans->GetResponseInfo();
244 ASSERT_TRUE(response != nullptr);
245 ASSERT_TRUE(response->headers.get() != nullptr);
246 EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine());
247 EXPECT_TRUE(response->was_fetched_via_spdy);
248 EXPECT_TRUE(response->was_npn_negotiated);
249 EXPECT_EQ(HttpResponseInfo::CONNECTION_INFO_QUIC1_SPDY3,
250 response->connection_info);
253 void CheckWasHttpResponse(const scoped_ptr<HttpNetworkTransaction>& trans) {
254 const HttpResponseInfo* response = trans->GetResponseInfo();
255 ASSERT_TRUE(response != nullptr);
256 ASSERT_TRUE(response->headers.get() != nullptr);
257 EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine());
258 EXPECT_FALSE(response->was_fetched_via_spdy);
259 EXPECT_FALSE(response->was_npn_negotiated);
260 EXPECT_EQ(HttpResponseInfo::CONNECTION_INFO_HTTP1,
261 response->connection_info);
264 void CheckResponseData(HttpNetworkTransaction* trans,
265 const std::string& expected) {
266 std::string response_data;
267 ASSERT_EQ(OK, ReadTransaction(trans, &response_data));
268 EXPECT_EQ(expected, response_data);
271 void RunTransaction(HttpNetworkTransaction* trans) {
272 TestCompletionCallback callback;
273 int rv = trans->Start(&request_, callback.callback(), net_log_.bound());
274 EXPECT_EQ(ERR_IO_PENDING, rv);
275 EXPECT_EQ(OK, callback.WaitForResult());
278 void SendRequestAndExpectHttpResponse(const std::string& expected) {
279 scoped_ptr<HttpNetworkTransaction> trans(
280 new HttpNetworkTransaction(DEFAULT_PRIORITY, session_.get()));
281 RunTransaction(trans.get());
282 CheckWasHttpResponse(trans);
283 CheckResponseData(trans.get(), expected);
286 void SendRequestAndExpectQuicResponse(const std::string& expected) {
287 SendRequestAndExpectQuicResponseMaybeFromProxy(expected, false);
290 void SendRequestAndExpectQuicResponseFromProxy(const std::string& expected) {
291 SendRequestAndExpectQuicResponseMaybeFromProxy(expected, true);
294 void AddQuicAlternateProtocolMapping(
295 MockCryptoClientStream::HandshakeMode handshake_mode) {
296 crypto_client_stream_factory_.set_handshake_mode(handshake_mode);
297 session_->http_server_properties()->SetAlternateProtocol(
298 HostPortPair::FromURL(request_.url), 80, QUIC, 1);
301 void ExpectBrokenAlternateProtocolMapping() {
302 const AlternateProtocolInfo alternate =
303 session_->http_server_properties()->GetAlternateProtocol(
304 HostPortPair::FromURL(request_.url));
305 EXPECT_NE(UNINITIALIZED_ALTERNATE_PROTOCOL, alternate.protocol);
306 EXPECT_TRUE(alternate.is_broken);
309 void ExpectQuicAlternateProtocolMapping() {
310 const AlternateProtocolInfo alternate =
311 session_->http_server_properties()->GetAlternateProtocol(
312 HostPortPair::FromURL(request_.url));
313 EXPECT_EQ(QUIC, alternate.protocol);
316 void AddHangingNonAlternateProtocolSocketData() {
317 MockConnect hanging_connect(SYNCHRONOUS, ERR_IO_PENDING);
318 hanging_data_.set_connect_data(hanging_connect);
319 socket_factory_.AddSocketDataProvider(&hanging_data_);
322 MockClock* clock_; // Owned by QuicStreamFactory after CreateSession.
323 QuicTestPacketMaker maker_;
324 scoped_refptr<HttpNetworkSession> session_;
325 MockClientSocketFactory socket_factory_;
326 MockCryptoClientStreamFactory crypto_client_stream_factory_;
327 MockHostResolver host_resolver_;
328 MockCertVerifier cert_verifier_;
329 TransportSecurityState transport_security_state_;
330 scoped_refptr<SSLConfigServiceDefaults> ssl_config_service_;
331 scoped_ptr<ProxyService> proxy_service_;
332 scoped_ptr<HttpAuthHandlerFactory> auth_handler_factory_;
333 MockRandom random_generator_;
334 HttpServerPropertiesImpl http_server_properties;
335 HttpNetworkSession::Params params_;
336 HttpRequestInfo request_;
337 CapturingBoundNetLog net_log_;
338 StaticSocketDataProvider hanging_data_;
340 private:
341 void SendRequestAndExpectQuicResponseMaybeFromProxy(
342 const std::string& expected,
343 bool used_proxy) {
344 scoped_ptr<HttpNetworkTransaction> trans(
345 new HttpNetworkTransaction(DEFAULT_PRIORITY, session_.get()));
346 ProxyHeadersHandler proxy_headers_handler;
347 trans->SetBeforeProxyHeadersSentCallback(
348 base::Bind(&ProxyHeadersHandler::OnBeforeProxyHeadersSent,
349 base::Unretained(&proxy_headers_handler)));
350 RunTransaction(trans.get());
351 CheckWasQuicResponse(trans);
352 CheckResponseData(trans.get(), expected);
353 EXPECT_EQ(used_proxy, proxy_headers_handler.was_called());
357 INSTANTIATE_TEST_CASE_P(Version, QuicNetworkTransactionTest,
358 ::testing::ValuesIn(QuicSupportedVersions()));
360 TEST_P(QuicNetworkTransactionTest, ForceQuic) {
361 params_.origin_to_force_quic_on =
362 HostPortPair::FromString("www.google.com:80");
364 MockQuicData mock_quic_data;
365 mock_quic_data.AddWrite(
366 ConstructRequestHeadersPacket(1, kClientDataStreamId1, true, true,
367 GetRequestHeaders("GET", "http", "/")));
368 mock_quic_data.AddRead(
369 ConstructResponseHeadersPacket(1, kClientDataStreamId1, false, false,
370 GetResponseHeaders("200 OK")));
371 mock_quic_data.AddRead(
372 ConstructDataPacket(2, kClientDataStreamId1, false, true, 0, "hello!"));
373 mock_quic_data.AddWrite(ConstructAckPacket(2, 1));
374 mock_quic_data.AddRead(SYNCHRONOUS, 0); // EOF
376 mock_quic_data.AddDelayedSocketDataToFactory(&socket_factory_, 1);
378 // The non-alternate protocol job needs to hang in order to guarantee that
379 // the alternate-protocol job will "win".
380 AddHangingNonAlternateProtocolSocketData();
382 CreateSession();
384 SendRequestAndExpectQuicResponse("hello!");
386 // Check that the NetLog was filled reasonably.
387 net::CapturingNetLog::CapturedEntryList entries;
388 net_log_.GetEntries(&entries);
389 EXPECT_LT(0u, entries.size());
391 // Check that we logged a QUIC_SESSION_PACKET_RECEIVED.
392 int pos = net::ExpectLogContainsSomewhere(
393 entries, 0,
394 net::NetLog::TYPE_QUIC_SESSION_PACKET_RECEIVED,
395 net::NetLog::PHASE_NONE);
396 EXPECT_LT(0, pos);
398 // ... and also a TYPE_QUIC_SESSION_PACKET_HEADER_RECEIVED.
399 pos = net::ExpectLogContainsSomewhere(
400 entries, 0,
401 net::NetLog::TYPE_QUIC_SESSION_PACKET_HEADER_RECEIVED,
402 net::NetLog::PHASE_NONE);
403 EXPECT_LT(0, pos);
405 std::string packet_sequence_number;
406 ASSERT_TRUE(entries[pos].GetStringValue(
407 "packet_sequence_number", &packet_sequence_number));
408 EXPECT_EQ("1", packet_sequence_number);
410 // ... and also a QUIC_SESSION_STREAM_FRAME_RECEIVED.
411 pos = net::ExpectLogContainsSomewhere(
412 entries, 0,
413 net::NetLog::TYPE_QUIC_SESSION_STREAM_FRAME_RECEIVED,
414 net::NetLog::PHASE_NONE);
415 EXPECT_LT(0, pos);
417 int log_stream_id;
418 ASSERT_TRUE(entries[pos].GetIntegerValue("stream_id", &log_stream_id));
419 EXPECT_EQ(3, log_stream_id);
422 TEST_P(QuicNetworkTransactionTest, QuicProxy) {
423 params_.enable_quic_for_proxies = true;
424 proxy_service_.reset(
425 ProxyService::CreateFixedFromPacResult("QUIC myproxy:70"));
427 MockQuicData mock_quic_data;
428 mock_quic_data.AddWrite(
429 ConstructRequestHeadersPacket(1, kClientDataStreamId1, true, true,
430 GetRequestHeaders("GET", "http", "/")));
431 mock_quic_data.AddRead(
432 ConstructResponseHeadersPacket(1, kClientDataStreamId1, false, false,
433 GetResponseHeaders("200 OK")));
434 mock_quic_data.AddRead(
435 ConstructDataPacket(2, kClientDataStreamId1, false, true, 0, "hello!"));
436 mock_quic_data.AddWrite(ConstructAckPacket(2, 1));
437 mock_quic_data.AddRead(SYNCHRONOUS, 0); // EOF
439 mock_quic_data.AddDelayedSocketDataToFactory(&socket_factory_, 1);
441 // There is no need to set up an alternate protocol job, because
442 // no attempt will be made to speak to the proxy over TCP.
444 CreateSession();
446 SendRequestAndExpectQuicResponseFromProxy("hello!");
449 TEST_P(QuicNetworkTransactionTest, ForceQuicWithErrorConnecting) {
450 params_.origin_to_force_quic_on =
451 HostPortPair::FromString("www.google.com:80");
453 MockQuicData mock_quic_data;
454 mock_quic_data.AddRead(ASYNC, ERR_SOCKET_NOT_CONNECTED);
456 mock_quic_data.AddDelayedSocketDataToFactory(&socket_factory_, 0);
458 CreateSession();
460 scoped_ptr<HttpNetworkTransaction> trans(
461 new HttpNetworkTransaction(DEFAULT_PRIORITY, session_.get()));
462 TestCompletionCallback callback;
463 int rv = trans->Start(&request_, callback.callback(), net_log_.bound());
464 EXPECT_EQ(ERR_IO_PENDING, rv);
465 EXPECT_EQ(ERR_CONNECTION_CLOSED, callback.WaitForResult());
468 TEST_P(QuicNetworkTransactionTest, DoNotForceQuicForHttps) {
469 // Attempt to "force" quic on 443, which will not be honored.
470 params_.origin_to_force_quic_on =
471 HostPortPair::FromString("www.google.com:443");
473 MockRead http_reads[] = {
474 MockRead("HTTP/1.1 200 OK\r\n\r\n"),
475 MockRead("hello world"),
476 MockRead(SYNCHRONOUS, ERR_TEST_PEER_CLOSE_AFTER_NEXT_MOCK_READ),
477 MockRead(ASYNC, OK)
480 StaticSocketDataProvider data(http_reads, arraysize(http_reads), nullptr, 0);
481 socket_factory_.AddSocketDataProvider(&data);
482 SSLSocketDataProvider ssl(ASYNC, OK);
483 socket_factory_.AddSSLSocketDataProvider(&ssl);
485 CreateSession();
487 SendRequestAndExpectHttpResponse("hello world");
490 TEST_P(QuicNetworkTransactionTest, UseAlternateProtocolForQuic) {
491 MockRead http_reads[] = {
492 MockRead("HTTP/1.1 200 OK\r\n"),
493 MockRead(kQuicAlternateProtocolHttpHeader),
494 MockRead("hello world"),
495 MockRead(SYNCHRONOUS, ERR_TEST_PEER_CLOSE_AFTER_NEXT_MOCK_READ),
496 MockRead(ASYNC, OK)
499 StaticSocketDataProvider http_data(http_reads, arraysize(http_reads),
500 nullptr, 0);
501 socket_factory_.AddSocketDataProvider(&http_data);
503 MockQuicData mock_quic_data;
504 mock_quic_data.AddWrite(
505 ConstructRequestHeadersPacket(1, kClientDataStreamId1, true, true,
506 GetRequestHeaders("GET", "http", "/")));
507 mock_quic_data.AddRead(
508 ConstructResponseHeadersPacket(1, kClientDataStreamId1, false, false,
509 GetResponseHeaders("200 OK")));
510 mock_quic_data.AddRead(
511 ConstructDataPacket(2, kClientDataStreamId1, false, true, 0, "hello!"));
512 mock_quic_data.AddWrite(ConstructAckPacket(2, 1));
513 mock_quic_data.AddRead(SYNCHRONOUS, 0); // EOF
515 mock_quic_data.AddDelayedSocketDataToFactory(&socket_factory_, 1);
517 // The non-alternate protocol job needs to hang in order to guarantee that
518 // the alternate-protocol job will "win".
519 AddHangingNonAlternateProtocolSocketData();
521 CreateSessionWithNextProtos();
523 SendRequestAndExpectHttpResponse("hello world");
524 SendRequestAndExpectQuicResponse("hello!");
527 TEST_P(QuicNetworkTransactionTest, UseAlternateProtocolProbabilityForQuic) {
528 MockRead http_reads[] = {
529 MockRead("HTTP/1.1 200 OK\r\n"),
530 MockRead(kQuicAlternateProtocol50pctHttpHeader),
531 MockRead("hello world"),
532 MockRead(SYNCHRONOUS, ERR_TEST_PEER_CLOSE_AFTER_NEXT_MOCK_READ),
533 MockRead(ASYNC, OK)
536 StaticSocketDataProvider http_data(http_reads, arraysize(http_reads),
537 nullptr, 0);
538 socket_factory_.AddSocketDataProvider(&http_data);
540 MockQuicData mock_quic_data;
541 mock_quic_data.AddWrite(
542 ConstructRequestHeadersPacket(1, kClientDataStreamId1, true, true,
543 GetRequestHeaders("GET", "http", "/")));
544 mock_quic_data.AddRead(
545 ConstructResponseHeadersPacket(1, kClientDataStreamId1, false, false,
546 GetResponseHeaders("200 OK")));
547 mock_quic_data.AddRead(
548 ConstructDataPacket(2, kClientDataStreamId1, false, true, 0, "hello!"));
549 mock_quic_data.AddWrite(ConstructAckPacket(2, 1));
550 mock_quic_data.AddRead(SYNCHRONOUS, 0); // EOF
552 mock_quic_data.AddDelayedSocketDataToFactory(&socket_factory_, 1);
554 // The non-alternate protocol job needs to hang in order to guarantee that
555 // the alternate-protocol job will "win".
556 AddHangingNonAlternateProtocolSocketData();
558 params_.alternate_protocol_probability_threshold = .25;
559 CreateSessionWithNextProtos();
561 SendRequestAndExpectHttpResponse("hello world");
562 SendRequestAndExpectQuicResponse("hello!");
565 TEST_P(QuicNetworkTransactionTest, DontUseAlternateProtocolProbabilityForQuic) {
566 MockRead http_reads[] = {
567 MockRead("HTTP/1.1 200 OK\r\n"),
568 MockRead(kQuicAlternateProtocol50pctHttpHeader),
569 MockRead("hello world"),
570 MockRead(SYNCHRONOUS, ERR_TEST_PEER_CLOSE_AFTER_NEXT_MOCK_READ),
571 MockRead(ASYNC, OK)
574 StaticSocketDataProvider http_data(http_reads, arraysize(http_reads),
575 nullptr, 0);
576 socket_factory_.AddSocketDataProvider(&http_data);
577 socket_factory_.AddSocketDataProvider(&http_data);
579 params_.alternate_protocol_probability_threshold = .75;
580 CreateSessionWithNextProtos();
582 SendRequestAndExpectHttpResponse("hello world");
583 SendRequestAndExpectHttpResponse("hello world");
586 TEST_P(QuicNetworkTransactionTest,
587 DontUseAlternateProtocolWithBadProbabilityForQuic) {
588 MockRead http_reads[] = {
589 MockRead("HTTP/1.1 200 OK\r\n"),
590 MockRead("Alternate-Protocol: 443:quic,p=2\r\n\r\n"),
591 MockRead("hello world"),
592 MockRead(SYNCHRONOUS, ERR_TEST_PEER_CLOSE_AFTER_NEXT_MOCK_READ),
593 MockRead(ASYNC, OK)
596 StaticSocketDataProvider http_data(http_reads, arraysize(http_reads),
597 nullptr, 0);
598 socket_factory_.AddSocketDataProvider(&http_data);
599 socket_factory_.AddSocketDataProvider(&http_data);
601 params_.alternate_protocol_probability_threshold = .75;
602 CreateSessionWithNextProtos();
604 SendRequestAndExpectHttpResponse("hello world");
605 SendRequestAndExpectHttpResponse("hello world");
608 TEST_P(QuicNetworkTransactionTest, UseAlternateProtocolForQuicForHttps) {
609 params_.origin_to_force_quic_on =
610 HostPortPair::FromString("www.google.com:443");
612 MockRead http_reads[] = {
613 MockRead("HTTP/1.1 200 OK\r\n"),
614 MockRead(kQuicAlternateProtocolHttpsHeader),
615 MockRead("hello world"),
616 MockRead(SYNCHRONOUS, ERR_TEST_PEER_CLOSE_AFTER_NEXT_MOCK_READ),
617 MockRead(ASYNC, OK)
620 StaticSocketDataProvider http_data(http_reads, arraysize(http_reads),
621 nullptr, 0);
622 socket_factory_.AddSocketDataProvider(&http_data);
624 MockQuicData mock_quic_data;
625 mock_quic_data.AddWrite(
626 ConstructRequestHeadersPacket(1, kClientDataStreamId1, true, true,
627 GetRequestHeaders("GET", "http", "/")));
628 mock_quic_data.AddRead(
629 ConstructResponseHeadersPacket(1, kClientDataStreamId1, false, false,
630 GetResponseHeaders("200 OK")));
631 mock_quic_data.AddRead(
632 ConstructDataPacket(2, kClientDataStreamId1, false, true, 0, "hello!"));
633 mock_quic_data.AddWrite(ConstructAckPacket(2, 1));
634 mock_quic_data.AddRead(SYNCHRONOUS, 0); // EOF
636 mock_quic_data.AddDelayedSocketDataToFactory(&socket_factory_, 1);
638 // The non-alternate protocol job needs to hang in order to guarantee that
639 // the alternate-protocol job will "win".
640 AddHangingNonAlternateProtocolSocketData();
642 CreateSessionWithNextProtos();
644 // TODO(rtenneti): Test QUIC over HTTPS, GetSSLInfo().
645 SendRequestAndExpectHttpResponse("hello world");
648 TEST_P(QuicNetworkTransactionTest, HungAlternateProtocol) {
649 crypto_client_stream_factory_.set_handshake_mode(
650 MockCryptoClientStream::COLD_START);
652 MockWrite http_writes[] = {
653 MockWrite(SYNCHRONOUS, 0, "GET / HTTP/1.1\r\n"),
654 MockWrite(SYNCHRONOUS, 1, "Host: www.google.com\r\n"),
655 MockWrite(SYNCHRONOUS, 2, "Connection: keep-alive\r\n\r\n")
658 MockRead http_reads[] = {
659 MockRead(SYNCHRONOUS, 3, "HTTP/1.1 200 OK\r\n"),
660 MockRead(SYNCHRONOUS, 4, kQuicAlternateProtocolHttpHeader),
661 MockRead(SYNCHRONOUS, 5, "hello world"),
662 MockRead(SYNCHRONOUS, OK, 6)
665 DeterministicMockClientSocketFactory socket_factory;
667 DeterministicSocketData http_data(http_reads, arraysize(http_reads),
668 http_writes, arraysize(http_writes));
669 socket_factory.AddSocketDataProvider(&http_data);
671 // The QUIC transaction will not be allowed to complete.
672 MockWrite quic_writes[] = {
673 MockWrite(ASYNC, ERR_IO_PENDING, 0)
675 MockRead quic_reads[] = {
676 MockRead(ASYNC, ERR_IO_PENDING, 1),
678 DeterministicSocketData quic_data(quic_reads, arraysize(quic_reads),
679 quic_writes, arraysize(quic_writes));
680 socket_factory.AddSocketDataProvider(&quic_data);
682 // The HTTP transaction will complete.
683 DeterministicSocketData http_data2(http_reads, arraysize(http_reads),
684 http_writes, arraysize(http_writes));
685 socket_factory.AddSocketDataProvider(&http_data2);
687 CreateSessionWithFactory(&socket_factory, true);
689 // Run the first request.
690 http_data.StopAfter(arraysize(http_reads) + arraysize(http_writes));
691 SendRequestAndExpectHttpResponse("hello world");
692 ASSERT_TRUE(http_data.at_read_eof());
693 ASSERT_TRUE(http_data.at_write_eof());
695 // Now run the second request in which the QUIC socket hangs,
696 // and verify the the transaction continues over HTTP.
697 http_data2.StopAfter(arraysize(http_reads) + arraysize(http_writes));
698 SendRequestAndExpectHttpResponse("hello world");
700 ASSERT_TRUE(http_data2.at_read_eof());
701 ASSERT_TRUE(http_data2.at_write_eof());
702 ASSERT_TRUE(!quic_data.at_read_eof());
703 ASSERT_TRUE(!quic_data.at_write_eof());
706 TEST_P(QuicNetworkTransactionTest, ZeroRTTWithHttpRace) {
707 MockQuicData mock_quic_data;
708 mock_quic_data.AddWrite(
709 ConstructRequestHeadersPacket(1, kClientDataStreamId1, true, true,
710 GetRequestHeaders("GET", "http", "/")));
711 mock_quic_data.AddRead(
712 ConstructResponseHeadersPacket(1, kClientDataStreamId1, false, false,
713 GetResponseHeaders("200 OK")));
714 mock_quic_data.AddRead(
715 ConstructDataPacket(2, kClientDataStreamId1, false, true, 0, "hello!"));
716 mock_quic_data.AddWrite(ConstructAckPacket(2, 1));
717 mock_quic_data.AddRead(SYNCHRONOUS, 0); // EOF
719 mock_quic_data.AddDelayedSocketDataToFactory(&socket_factory_, 1);
721 // The non-alternate protocol job needs to hang in order to guarantee that
722 // the alternate-protocol job will "win".
723 AddHangingNonAlternateProtocolSocketData();
725 CreateSessionWithNextProtos();
726 AddQuicAlternateProtocolMapping(MockCryptoClientStream::ZERO_RTT);
727 SendRequestAndExpectQuicResponse("hello!");
730 TEST_P(QuicNetworkTransactionTest, ZeroRTTWithNoHttpRace) {
731 MockQuicData mock_quic_data;
732 mock_quic_data.AddWrite(
733 ConstructRequestHeadersPacket(1, kClientDataStreamId1, true, true,
734 GetRequestHeaders("GET", "http", "/")));
735 mock_quic_data.AddRead(
736 ConstructResponseHeadersPacket(1, kClientDataStreamId1, false, false,
737 GetResponseHeaders("200 OK")));
738 mock_quic_data.AddRead(
739 ConstructDataPacket(2, kClientDataStreamId1, false, true, 0, "hello!"));
740 mock_quic_data.AddWrite(ConstructAckPacket(2, 1));
741 mock_quic_data.AddRead(SYNCHRONOUS, 0); // EOF
742 mock_quic_data.AddDelayedSocketDataToFactory(&socket_factory_, 1);
744 // In order for a new QUIC session to be established via alternate-protocol
745 // without racing an HTTP connection, we need the host resolution to happen
746 // synchronously.
747 host_resolver_.set_synchronous_mode(true);
748 host_resolver_.rules()->AddIPLiteralRule("www.google.com", "192.168.0.1", "");
749 HostResolver::RequestInfo info(HostPortPair("www.google.com", 80));
750 AddressList address;
751 host_resolver_.Resolve(info,
752 DEFAULT_PRIORITY,
753 &address,
754 CompletionCallback(),
755 nullptr,
756 net_log_.bound());
758 CreateSessionWithNextProtos();
759 AddQuicAlternateProtocolMapping(MockCryptoClientStream::ZERO_RTT);
760 SendRequestAndExpectQuicResponse("hello!");
763 TEST_P(QuicNetworkTransactionTest, ZeroRTTWithProxy) {
764 proxy_service_.reset(
765 ProxyService::CreateFixedFromPacResult("PROXY myproxy:70"));
767 // Since we are using a proxy, the QUIC job will not succeed.
768 MockWrite http_writes[] = {
769 MockWrite(SYNCHRONOUS, 0, "GET http://www.google.com/ HTTP/1.1\r\n"),
770 MockWrite(SYNCHRONOUS, 1, "Host: www.google.com\r\n"),
771 MockWrite(SYNCHRONOUS, 2, "Proxy-Connection: keep-alive\r\n\r\n")
774 MockRead http_reads[] = {
775 MockRead(SYNCHRONOUS, 3, "HTTP/1.1 200 OK\r\n"),
776 MockRead(SYNCHRONOUS, 4, kQuicAlternateProtocolHttpHeader),
777 MockRead(SYNCHRONOUS, 5, "hello world"),
778 MockRead(SYNCHRONOUS, OK, 6)
781 StaticSocketDataProvider http_data(http_reads, arraysize(http_reads),
782 http_writes, arraysize(http_writes));
783 socket_factory_.AddSocketDataProvider(&http_data);
785 // In order for a new QUIC session to be established via alternate-protocol
786 // without racing an HTTP connection, we need the host resolution to happen
787 // synchronously.
788 host_resolver_.set_synchronous_mode(true);
789 host_resolver_.rules()->AddIPLiteralRule("www.google.com", "192.168.0.1", "");
790 HostResolver::RequestInfo info(HostPortPair("www.google.com", 80));
791 AddressList address;
792 host_resolver_.Resolve(info,
793 DEFAULT_PRIORITY,
794 &address,
795 CompletionCallback(),
796 nullptr,
797 net_log_.bound());
799 CreateSessionWithNextProtos();
800 AddQuicAlternateProtocolMapping(MockCryptoClientStream::ZERO_RTT);
801 SendRequestAndExpectHttpResponse("hello world");
804 TEST_P(QuicNetworkTransactionTest, ZeroRTTWithConfirmationRequired) {
805 MockQuicData mock_quic_data;
806 mock_quic_data.AddWrite(
807 ConstructRequestHeadersPacket(1, kClientDataStreamId1, true, true,
808 GetRequestHeaders("GET", "http", "/")));
809 mock_quic_data.AddRead(
810 ConstructResponseHeadersPacket(1, kClientDataStreamId1, false, false,
811 GetResponseHeaders("200 OK")));
812 mock_quic_data.AddRead(
813 ConstructDataPacket(2, kClientDataStreamId1, false, true, 0, "hello!"));
814 mock_quic_data.AddWrite(ConstructAckPacket(2, 1));
815 mock_quic_data.AddRead(SYNCHRONOUS, 0); // EOF
816 mock_quic_data.AddDelayedSocketDataToFactory(&socket_factory_, 1);
818 // The non-alternate protocol job needs to hang in order to guarantee that
819 // the alternate-protocol job will "win".
820 AddHangingNonAlternateProtocolSocketData();
822 // In order for a new QUIC session to be established via alternate-protocol
823 // without racing an HTTP connection, we need the host resolution to happen
824 // synchronously. Of course, even though QUIC *could* perform a 0-RTT
825 // connection to the the server, in this test we require confirmation
826 // before encrypting so the HTTP job will still start.
827 host_resolver_.set_synchronous_mode(true);
828 host_resolver_.rules()->AddIPLiteralRule("www.google.com", "192.168.0.1", "");
829 HostResolver::RequestInfo info(HostPortPair("www.google.com", 80));
830 AddressList address;
831 host_resolver_.Resolve(info, DEFAULT_PRIORITY, &address,
832 CompletionCallback(), nullptr, net_log_.bound());
834 CreateSessionWithNextProtos();
835 session_->quic_stream_factory()->set_require_confirmation(true);
836 AddQuicAlternateProtocolMapping(MockCryptoClientStream::ZERO_RTT);
838 scoped_ptr<HttpNetworkTransaction> trans(
839 new HttpNetworkTransaction(DEFAULT_PRIORITY, session_.get()));
840 TestCompletionCallback callback;
841 int rv = trans->Start(&request_, callback.callback(), net_log_.bound());
842 EXPECT_EQ(ERR_IO_PENDING, rv);
844 crypto_client_stream_factory_.last_stream()->SendOnCryptoHandshakeEvent(
845 QuicSession::HANDSHAKE_CONFIRMED);
846 EXPECT_EQ(OK, callback.WaitForResult());
849 TEST_P(QuicNetworkTransactionTest, BrokenAlternateProtocol) {
850 // Alternate-protocol job
851 scoped_ptr<QuicEncryptedPacket> close(ConstructConnectionClosePacket(1));
852 MockRead quic_reads[] = {
853 MockRead(ASYNC, close->data(), close->length()),
854 MockRead(ASYNC, OK), // EOF
856 StaticSocketDataProvider quic_data(quic_reads, arraysize(quic_reads),
857 nullptr, 0);
858 socket_factory_.AddSocketDataProvider(&quic_data);
860 // Main job which will succeed even though the alternate job fails.
861 MockRead http_reads[] = {
862 MockRead("HTTP/1.1 200 OK\r\n\r\n"),
863 MockRead("hello from http"),
864 MockRead(SYNCHRONOUS, ERR_TEST_PEER_CLOSE_AFTER_NEXT_MOCK_READ),
865 MockRead(ASYNC, OK)
868 StaticSocketDataProvider http_data(http_reads, arraysize(http_reads),
869 nullptr, 0);
870 socket_factory_.AddSocketDataProvider(&http_data);
872 CreateSessionWithNextProtos();
873 AddQuicAlternateProtocolMapping(MockCryptoClientStream::COLD_START);
874 SendRequestAndExpectHttpResponse("hello from http");
875 ExpectBrokenAlternateProtocolMapping();
878 TEST_P(QuicNetworkTransactionTest, BrokenAlternateProtocolReadError) {
879 // Alternate-protocol job
880 MockRead quic_reads[] = {
881 MockRead(ASYNC, ERR_SOCKET_NOT_CONNECTED),
883 StaticSocketDataProvider quic_data(quic_reads, arraysize(quic_reads),
884 nullptr, 0);
885 socket_factory_.AddSocketDataProvider(&quic_data);
887 // Main job which will succeed even though the alternate job fails.
888 MockRead http_reads[] = {
889 MockRead("HTTP/1.1 200 OK\r\n\r\n"),
890 MockRead("hello from http"),
891 MockRead(SYNCHRONOUS, ERR_TEST_PEER_CLOSE_AFTER_NEXT_MOCK_READ),
892 MockRead(ASYNC, OK)
895 StaticSocketDataProvider http_data(http_reads, arraysize(http_reads),
896 nullptr, 0);
897 socket_factory_.AddSocketDataProvider(&http_data);
899 CreateSessionWithNextProtos();
901 AddQuicAlternateProtocolMapping(MockCryptoClientStream::COLD_START);
902 SendRequestAndExpectHttpResponse("hello from http");
903 ExpectBrokenAlternateProtocolMapping();
906 TEST_P(QuicNetworkTransactionTest, NoBrokenAlternateProtocolIfTcpFails) {
907 // Alternate-protocol job will fail when the session attempts to read.
908 MockRead quic_reads[] = {
909 MockRead(ASYNC, ERR_SOCKET_NOT_CONNECTED),
911 StaticSocketDataProvider quic_data(quic_reads, arraysize(quic_reads),
912 nullptr, 0);
913 socket_factory_.AddSocketDataProvider(&quic_data);
915 // Main job will also fail.
916 MockRead http_reads[] = {
917 MockRead(ASYNC, ERR_SOCKET_NOT_CONNECTED),
920 StaticSocketDataProvider http_data(http_reads, arraysize(http_reads),
921 nullptr, 0);
922 http_data.set_connect_data(MockConnect(ASYNC, ERR_SOCKET_NOT_CONNECTED));
923 socket_factory_.AddSocketDataProvider(&http_data);
925 CreateSessionWithNextProtos();
927 AddQuicAlternateProtocolMapping(MockCryptoClientStream::COLD_START);
928 scoped_ptr<HttpNetworkTransaction> trans(
929 new HttpNetworkTransaction(DEFAULT_PRIORITY, session_.get()));
930 TestCompletionCallback callback;
931 int rv = trans->Start(&request_, callback.callback(), net_log_.bound());
932 EXPECT_EQ(ERR_IO_PENDING, rv);
933 EXPECT_EQ(ERR_SOCKET_NOT_CONNECTED, callback.WaitForResult());
934 ExpectQuicAlternateProtocolMapping();
937 TEST_P(QuicNetworkTransactionTest, FailedZeroRttBrokenAlternateProtocol) {
938 // Alternate-protocol job
939 MockRead quic_reads[] = {
940 MockRead(ASYNC, ERR_SOCKET_NOT_CONNECTED),
942 StaticSocketDataProvider quic_data(quic_reads, arraysize(quic_reads),
943 nullptr, 0);
944 socket_factory_.AddSocketDataProvider(&quic_data);
946 AddHangingNonAlternateProtocolSocketData();
948 // Second Alternate-protocol job which will race with the TCP job.
949 StaticSocketDataProvider quic_data2(quic_reads, arraysize(quic_reads),
950 nullptr, 0);
951 socket_factory_.AddSocketDataProvider(&quic_data2);
953 // Final job that will proceed when the QUIC job fails.
954 MockRead http_reads[] = {
955 MockRead("HTTP/1.1 200 OK\r\n\r\n"),
956 MockRead("hello from http"),
957 MockRead(SYNCHRONOUS, ERR_TEST_PEER_CLOSE_AFTER_NEXT_MOCK_READ),
958 MockRead(ASYNC, OK)
961 StaticSocketDataProvider http_data(http_reads, arraysize(http_reads),
962 nullptr, 0);
963 socket_factory_.AddSocketDataProvider(&http_data);
965 CreateSessionWithNextProtos();
967 AddQuicAlternateProtocolMapping(MockCryptoClientStream::ZERO_RTT);
969 SendRequestAndExpectHttpResponse("hello from http");
971 ExpectBrokenAlternateProtocolMapping();
973 EXPECT_TRUE(quic_data.at_read_eof());
974 EXPECT_TRUE(quic_data.at_write_eof());
977 TEST_P(QuicNetworkTransactionTest, DISABLED_HangingZeroRttFallback) {
978 // Alternate-protocol job
979 MockRead quic_reads[] = {
980 MockRead(ASYNC, ERR_IO_PENDING),
982 StaticSocketDataProvider quic_data(quic_reads, arraysize(quic_reads),
983 nullptr, 0);
984 socket_factory_.AddSocketDataProvider(&quic_data);
986 // Main job that will proceed when the QUIC job fails.
987 MockRead http_reads[] = {
988 MockRead("HTTP/1.1 200 OK\r\n\r\n"),
989 MockRead("hello from http"),
990 MockRead(SYNCHRONOUS, ERR_TEST_PEER_CLOSE_AFTER_NEXT_MOCK_READ),
991 MockRead(ASYNC, OK)
994 StaticSocketDataProvider http_data(http_reads, arraysize(http_reads),
995 nullptr, 0);
996 socket_factory_.AddSocketDataProvider(&http_data);
998 CreateSessionWithNextProtos();
1000 AddQuicAlternateProtocolMapping(MockCryptoClientStream::ZERO_RTT);
1002 SendRequestAndExpectHttpResponse("hello from http");
1005 TEST_P(QuicNetworkTransactionTest, BrokenAlternateProtocolOnConnectFailure) {
1006 // Alternate-protocol job will fail before creating a QUIC session.
1007 StaticSocketDataProvider quic_data(nullptr, 0, nullptr, 0);
1008 quic_data.set_connect_data(MockConnect(SYNCHRONOUS,
1009 ERR_INTERNET_DISCONNECTED));
1010 socket_factory_.AddSocketDataProvider(&quic_data);
1012 // Main job which will succeed even though the alternate job fails.
1013 MockRead http_reads[] = {
1014 MockRead("HTTP/1.1 200 OK\r\n\r\n"),
1015 MockRead("hello from http"),
1016 MockRead(SYNCHRONOUS, ERR_TEST_PEER_CLOSE_AFTER_NEXT_MOCK_READ),
1017 MockRead(ASYNC, OK)
1020 StaticSocketDataProvider http_data(http_reads, arraysize(http_reads),
1021 nullptr, 0);
1022 socket_factory_.AddSocketDataProvider(&http_data);
1024 CreateSessionWithNextProtos();
1025 AddQuicAlternateProtocolMapping(MockCryptoClientStream::COLD_START);
1026 SendRequestAndExpectHttpResponse("hello from http");
1028 ExpectBrokenAlternateProtocolMapping();
1031 TEST_P(QuicNetworkTransactionTest, ConnectionCloseDuringConnect) {
1032 MockQuicData mock_quic_data;
1033 mock_quic_data.AddRead(ConstructConnectionClosePacket(1));
1034 mock_quic_data.AddWrite(
1035 ConstructRequestHeadersPacket(1, kClientDataStreamId1, true, true,
1036 GetRequestHeaders("GET", "http", "/")));
1037 mock_quic_data.AddWrite(ConstructAckPacket(2, 1));
1038 mock_quic_data.AddDelayedSocketDataToFactory(&socket_factory_, 0);
1040 // When the QUIC connection fails, we will try the request again over HTTP.
1041 MockRead http_reads[] = {
1042 MockRead("HTTP/1.1 200 OK\r\n"),
1043 MockRead(kQuicAlternateProtocolHttpHeader),
1044 MockRead("hello world"),
1045 MockRead(SYNCHRONOUS, ERR_TEST_PEER_CLOSE_AFTER_NEXT_MOCK_READ),
1046 MockRead(ASYNC, OK)
1049 StaticSocketDataProvider http_data(http_reads, arraysize(http_reads),
1050 nullptr, 0);
1051 socket_factory_.AddSocketDataProvider(&http_data);
1053 // In order for a new QUIC session to be established via alternate-protocol
1054 // without racing an HTTP connection, we need the host resolution to happen
1055 // synchronously.
1056 host_resolver_.set_synchronous_mode(true);
1057 host_resolver_.rules()->AddIPLiteralRule("www.google.com", "192.168.0.1", "");
1058 HostResolver::RequestInfo info(HostPortPair("www.google.com", 80));
1059 AddressList address;
1060 host_resolver_.Resolve(info,
1061 DEFAULT_PRIORITY,
1062 &address,
1063 CompletionCallback(),
1064 nullptr,
1065 net_log_.bound());
1067 CreateSessionWithNextProtos();
1068 AddQuicAlternateProtocolMapping(MockCryptoClientStream::ZERO_RTT);
1069 SendRequestAndExpectHttpResponse("hello world");
1072 } // namespace test
1073 } // namespace net