Make castv2 performance test work.
[chromium-blink-merge.git] / net / quic / quic_network_transaction_unittest.cc
blob5445abdd2ebd10fbbfd6abec8dcb94632f78821c
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/test_completion_callback.h"
12 #include "net/cert/mock_cert_verifier.h"
13 #include "net/dns/mock_host_resolver.h"
14 #include "net/http/http_auth_handler_factory.h"
15 #include "net/http/http_network_session.h"
16 #include "net/http/http_network_transaction.h"
17 #include "net/http/http_server_properties_impl.h"
18 #include "net/http/http_stream.h"
19 #include "net/http/http_stream_factory.h"
20 #include "net/http/http_transaction_test_util.h"
21 #include "net/http/transport_security_state.h"
22 #include "net/log/capturing_net_log.h"
23 #include "net/log/net_log_unittest.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 kQuicAlternateProtocolDifferentPortHttpHeader[] =
57 "Alternate-Protocol: 137:quic\r\n\r\n";
58 static const char kQuicAlternateProtocolHttpsHeader[] =
59 "Alternate-Protocol: 443:quic\r\n\r\n";
61 } // namespace
63 namespace net {
64 namespace test {
66 // Helper class to encapsulate MockReads and MockWrites for QUIC.
67 // Simplify ownership issues and the interaction with the MockSocketFactory.
68 class MockQuicData {
69 public:
70 ~MockQuicData() {
71 STLDeleteElements(&packets_);
74 void AddRead(scoped_ptr<QuicEncryptedPacket> packet) {
75 reads_.push_back(MockRead(SYNCHRONOUS, packet->data(), packet->length(),
76 sequence_number_++));
77 packets_.push_back(packet.release());
80 void AddRead(IoMode mode, int rv) {
81 reads_.push_back(MockRead(mode, rv));
84 void AddWrite(scoped_ptr<QuicEncryptedPacket> packet) {
85 writes_.push_back(MockWrite(SYNCHRONOUS, packet->data(), packet->length(),
86 sequence_number_++));
87 packets_.push_back(packet.release());
90 void AddDelayedSocketDataToFactory(MockClientSocketFactory* factory,
91 size_t delay) {
92 MockRead* reads = reads_.empty() ? nullptr : &reads_[0];
93 MockWrite* writes = writes_.empty() ? nullptr : &writes_[0];
94 socket_data_.reset(new DelayedSocketData(
95 delay, reads, reads_.size(), writes, writes_.size()));
96 factory->AddSocketDataProvider(socket_data_.get());
99 private:
100 std::vector<QuicEncryptedPacket*> packets_;
101 std::vector<MockWrite> writes_;
102 std::vector<MockRead> reads_;
103 size_t sequence_number_;
104 scoped_ptr<SocketDataProvider> socket_data_;
107 class ProxyHeadersHandler {
108 public:
109 ProxyHeadersHandler() : was_called_(false) {}
111 bool was_called() { return was_called_; }
113 void OnBeforeProxyHeadersSent(const ProxyInfo& proxy_info,
114 HttpRequestHeaders* request_headers) {
115 was_called_ = true;
118 private:
119 bool was_called_;
122 class QuicNetworkTransactionTest
123 : public PlatformTest,
124 public ::testing::WithParamInterface<QuicVersion> {
125 protected:
126 QuicNetworkTransactionTest()
127 : clock_(new MockClock),
128 maker_(GetParam(), 0, clock_),
129 ssl_config_service_(new SSLConfigServiceDefaults),
130 proxy_service_(ProxyService::CreateDirect()),
131 auth_handler_factory_(
132 HttpAuthHandlerFactory::CreateDefault(&host_resolver_)),
133 random_generator_(0),
134 hanging_data_(nullptr, 0, nullptr, 0) {
135 request_.method = "GET";
136 request_.url = GURL("http://www.google.com/");
137 request_.load_flags = 0;
138 clock_->AdvanceTime(QuicTime::Delta::FromMilliseconds(20));
141 void SetUp() override {
142 NetworkChangeNotifier::NotifyObserversOfIPAddressChangeForTests();
143 base::MessageLoop::current()->RunUntilIdle();
146 void TearDown() override {
147 NetworkChangeNotifier::NotifyObserversOfIPAddressChangeForTests();
148 // Empty the current queue.
149 base::MessageLoop::current()->RunUntilIdle();
150 PlatformTest::TearDown();
151 NetworkChangeNotifier::NotifyObserversOfIPAddressChangeForTests();
152 base::MessageLoop::current()->RunUntilIdle();
155 scoped_ptr<QuicEncryptedPacket> ConstructConnectionClosePacket(
156 QuicPacketSequenceNumber num) {
157 return maker_.MakeConnectionClosePacket(num);
160 scoped_ptr<QuicEncryptedPacket> ConstructAckPacket(
161 QuicPacketSequenceNumber largest_received,
162 QuicPacketSequenceNumber least_unacked) {
163 return maker_.MakeAckPacket(2, largest_received, least_unacked, true);
166 SpdyHeaderBlock GetRequestHeaders(const std::string& method,
167 const std::string& scheme,
168 const std::string& path) {
169 return maker_.GetRequestHeaders(method, scheme, path);
172 SpdyHeaderBlock GetResponseHeaders(const std::string& status) {
173 return maker_.GetResponseHeaders(status);
176 scoped_ptr<QuicEncryptedPacket> ConstructDataPacket(
177 QuicPacketSequenceNumber sequence_number,
178 QuicStreamId stream_id,
179 bool should_include_version,
180 bool fin,
181 QuicStreamOffset offset,
182 base::StringPiece data) {
183 return maker_.MakeDataPacket(
184 sequence_number, stream_id, should_include_version, fin, offset, data);
187 scoped_ptr<QuicEncryptedPacket> ConstructRequestHeadersPacket(
188 QuicPacketSequenceNumber sequence_number,
189 QuicStreamId stream_id,
190 bool should_include_version,
191 bool fin,
192 const SpdyHeaderBlock& headers) {
193 QuicPriority priority =
194 ConvertRequestPriorityToQuicPriority(DEFAULT_PRIORITY);
195 return maker_.MakeRequestHeadersPacket(sequence_number, stream_id,
196 should_include_version, fin,
197 priority, headers);
200 scoped_ptr<QuicEncryptedPacket> ConstructResponseHeadersPacket(
201 QuicPacketSequenceNumber sequence_number,
202 QuicStreamId stream_id,
203 bool should_include_version,
204 bool fin,
205 const SpdyHeaderBlock& headers) {
206 return maker_.MakeResponseHeadersPacket(
207 sequence_number, stream_id, should_include_version, fin, headers);
210 void CreateSession() {
211 CreateSessionWithFactory(&socket_factory_, false);
214 void CreateSessionWithNextProtos() {
215 CreateSessionWithFactory(&socket_factory_, true);
218 // If |use_next_protos| is true, enables SPDY and QUIC.
219 void CreateSessionWithFactory(ClientSocketFactory* socket_factory,
220 bool use_next_protos) {
221 params_.enable_quic = true;
222 params_.quic_clock = clock_;
223 params_.quic_random = &random_generator_;
224 params_.client_socket_factory = socket_factory;
225 params_.quic_crypto_client_stream_factory = &crypto_client_stream_factory_;
226 params_.host_resolver = &host_resolver_;
227 params_.cert_verifier = &cert_verifier_;
228 params_.transport_security_state = &transport_security_state_;
229 params_.proxy_service = proxy_service_.get();
230 params_.ssl_config_service = ssl_config_service_.get();
231 params_.http_auth_handler_factory = auth_handler_factory_.get();
232 params_.http_server_properties = http_server_properties.GetWeakPtr();
233 params_.quic_supported_versions = SupportedVersions(GetParam());
235 if (use_next_protos) {
236 params_.use_alternate_protocols = true;
237 params_.next_protos = NextProtosWithSpdyAndQuic(true, true);
240 session_ = new HttpNetworkSession(params_);
241 session_->quic_stream_factory()->set_require_confirmation(false);
244 void CheckWasQuicResponse(const scoped_ptr<HttpNetworkTransaction>& trans) {
245 const HttpResponseInfo* response = trans->GetResponseInfo();
246 ASSERT_TRUE(response != nullptr);
247 ASSERT_TRUE(response->headers.get() != nullptr);
248 EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine());
249 EXPECT_TRUE(response->was_fetched_via_spdy);
250 EXPECT_TRUE(response->was_npn_negotiated);
251 EXPECT_EQ(HttpResponseInfo::CONNECTION_INFO_QUIC1_SPDY3,
252 response->connection_info);
255 void CheckResponsePort(const scoped_ptr<HttpNetworkTransaction>& trans,
256 uint16 port) {
257 const HttpResponseInfo* response = trans->GetResponseInfo();
258 ASSERT_TRUE(response != nullptr);
259 EXPECT_EQ(port, response->socket_address.port());
262 void CheckWasHttpResponse(const scoped_ptr<HttpNetworkTransaction>& trans) {
263 const HttpResponseInfo* response = trans->GetResponseInfo();
264 ASSERT_TRUE(response != nullptr);
265 ASSERT_TRUE(response->headers.get() != nullptr);
266 EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine());
267 EXPECT_FALSE(response->was_fetched_via_spdy);
268 EXPECT_FALSE(response->was_npn_negotiated);
269 EXPECT_EQ(HttpResponseInfo::CONNECTION_INFO_HTTP1,
270 response->connection_info);
273 void CheckResponseData(HttpNetworkTransaction* trans,
274 const std::string& expected) {
275 std::string response_data;
276 ASSERT_EQ(OK, ReadTransaction(trans, &response_data));
277 EXPECT_EQ(expected, response_data);
280 void RunTransaction(HttpNetworkTransaction* trans) {
281 TestCompletionCallback callback;
282 int rv = trans->Start(&request_, callback.callback(), net_log_.bound());
283 EXPECT_EQ(ERR_IO_PENDING, rv);
284 EXPECT_EQ(OK, callback.WaitForResult());
287 void SendRequestAndExpectHttpResponse(const std::string& expected) {
288 scoped_ptr<HttpNetworkTransaction> trans(
289 new HttpNetworkTransaction(DEFAULT_PRIORITY, session_.get()));
290 RunTransaction(trans.get());
291 CheckWasHttpResponse(trans);
292 CheckResponseData(trans.get(), expected);
295 void SendRequestAndExpectQuicResponse(const std::string& expected) {
296 SendRequestAndExpectQuicResponseMaybeFromProxy(expected, false, 80);
299 void SendRequestAndExpectQuicResponseOnPort(const std::string& expected,
300 uint16 port) {
301 SendRequestAndExpectQuicResponseMaybeFromProxy(expected, false, port);
304 void SendRequestAndExpectQuicResponseFromProxyOnPort(
305 const std::string& expected,
306 uint16 port) {
307 SendRequestAndExpectQuicResponseMaybeFromProxy(expected, true, port);
310 void AddQuicAlternateProtocolMapping(
311 MockCryptoClientStream::HandshakeMode handshake_mode) {
312 crypto_client_stream_factory_.set_handshake_mode(handshake_mode);
313 HostPortPair host_port_pair = HostPortPair::FromURL(request_.url);
314 AlternativeService alternative_service(QUIC, host_port_pair.host(), 80);
315 session_->http_server_properties()->SetAlternativeService(
316 host_port_pair, alternative_service, 1.0);
319 void ExpectBrokenAlternateProtocolMapping() {
320 const HostPortPair origin = HostPortPair::FromURL(request_.url);
321 const AlternativeService alternative_service =
322 session_->http_server_properties()->GetAlternativeService(origin);
323 EXPECT_NE(UNINITIALIZED_ALTERNATE_PROTOCOL, alternative_service.protocol);
324 EXPECT_TRUE(session_->http_server_properties()->IsAlternativeServiceBroken(
325 alternative_service));
328 void ExpectQuicAlternateProtocolMapping() {
329 const AlternativeService alternative_service =
330 session_->http_server_properties()->GetAlternativeService(
331 HostPortPair::FromURL(request_.url));
332 EXPECT_EQ(QUIC, alternative_service.protocol);
335 void AddHangingNonAlternateProtocolSocketData() {
336 MockConnect hanging_connect(SYNCHRONOUS, ERR_IO_PENDING);
337 hanging_data_.set_connect_data(hanging_connect);
338 socket_factory_.AddSocketDataProvider(&hanging_data_);
341 MockClock* clock_; // Owned by QuicStreamFactory after CreateSession.
342 QuicTestPacketMaker maker_;
343 scoped_refptr<HttpNetworkSession> session_;
344 MockClientSocketFactory socket_factory_;
345 MockCryptoClientStreamFactory crypto_client_stream_factory_;
346 MockHostResolver host_resolver_;
347 MockCertVerifier cert_verifier_;
348 TransportSecurityState transport_security_state_;
349 scoped_refptr<SSLConfigServiceDefaults> ssl_config_service_;
350 scoped_ptr<ProxyService> proxy_service_;
351 scoped_ptr<HttpAuthHandlerFactory> auth_handler_factory_;
352 MockRandom random_generator_;
353 HttpServerPropertiesImpl http_server_properties;
354 HttpNetworkSession::Params params_;
355 HttpRequestInfo request_;
356 CapturingBoundNetLog net_log_;
357 StaticSocketDataProvider hanging_data_;
359 private:
360 void SendRequestAndExpectQuicResponseMaybeFromProxy(
361 const std::string& expected,
362 bool used_proxy,
363 uint16 port) {
364 scoped_ptr<HttpNetworkTransaction> trans(
365 new HttpNetworkTransaction(DEFAULT_PRIORITY, session_.get()));
366 ProxyHeadersHandler proxy_headers_handler;
367 trans->SetBeforeProxyHeadersSentCallback(
368 base::Bind(&ProxyHeadersHandler::OnBeforeProxyHeadersSent,
369 base::Unretained(&proxy_headers_handler)));
370 RunTransaction(trans.get());
371 CheckWasQuicResponse(trans);
372 CheckResponsePort(trans, port);
373 CheckResponseData(trans.get(), expected);
374 EXPECT_EQ(used_proxy, proxy_headers_handler.was_called());
378 INSTANTIATE_TEST_CASE_P(Version, QuicNetworkTransactionTest,
379 ::testing::ValuesIn(QuicSupportedVersions()));
381 TEST_P(QuicNetworkTransactionTest, ForceQuic) {
382 params_.origin_to_force_quic_on =
383 HostPortPair::FromString("www.google.com:80");
385 MockQuicData mock_quic_data;
386 mock_quic_data.AddWrite(
387 ConstructRequestHeadersPacket(1, kClientDataStreamId1, true, true,
388 GetRequestHeaders("GET", "http", "/")));
389 mock_quic_data.AddRead(
390 ConstructResponseHeadersPacket(1, kClientDataStreamId1, false, false,
391 GetResponseHeaders("200 OK")));
392 mock_quic_data.AddRead(
393 ConstructDataPacket(2, kClientDataStreamId1, false, true, 0, "hello!"));
394 mock_quic_data.AddWrite(ConstructAckPacket(2, 1));
395 mock_quic_data.AddRead(SYNCHRONOUS, 0); // EOF
397 mock_quic_data.AddDelayedSocketDataToFactory(&socket_factory_, 1);
399 // The non-alternate protocol job needs to hang in order to guarantee that
400 // the alternate-protocol job will "win".
401 AddHangingNonAlternateProtocolSocketData();
403 CreateSession();
405 SendRequestAndExpectQuicResponse("hello!");
407 // Check that the NetLog was filled reasonably.
408 net::CapturingNetLog::CapturedEntryList entries;
409 net_log_.GetEntries(&entries);
410 EXPECT_LT(0u, entries.size());
412 // Check that we logged a QUIC_SESSION_PACKET_RECEIVED.
413 int pos = net::ExpectLogContainsSomewhere(
414 entries, 0,
415 net::NetLog::TYPE_QUIC_SESSION_PACKET_RECEIVED,
416 net::NetLog::PHASE_NONE);
417 EXPECT_LT(0, pos);
419 // ... and also a TYPE_QUIC_SESSION_PACKET_HEADER_RECEIVED.
420 pos = net::ExpectLogContainsSomewhere(
421 entries, 0,
422 net::NetLog::TYPE_QUIC_SESSION_PACKET_HEADER_RECEIVED,
423 net::NetLog::PHASE_NONE);
424 EXPECT_LT(0, pos);
426 std::string packet_sequence_number;
427 ASSERT_TRUE(entries[pos].GetStringValue(
428 "packet_sequence_number", &packet_sequence_number));
429 EXPECT_EQ("1", packet_sequence_number);
431 // ... and also a QUIC_SESSION_STREAM_FRAME_RECEIVED.
432 pos = net::ExpectLogContainsSomewhere(
433 entries, 0,
434 net::NetLog::TYPE_QUIC_SESSION_STREAM_FRAME_RECEIVED,
435 net::NetLog::PHASE_NONE);
436 EXPECT_LT(0, pos);
438 int log_stream_id;
439 ASSERT_TRUE(entries[pos].GetIntegerValue("stream_id", &log_stream_id));
440 EXPECT_EQ(3, log_stream_id);
443 TEST_P(QuicNetworkTransactionTest, QuicProxy) {
444 params_.enable_quic_for_proxies = true;
445 proxy_service_.reset(
446 ProxyService::CreateFixedFromPacResult("QUIC myproxy:70"));
448 MockQuicData mock_quic_data;
449 mock_quic_data.AddWrite(
450 ConstructRequestHeadersPacket(1, kClientDataStreamId1, true, true,
451 GetRequestHeaders("GET", "http", "/")));
452 mock_quic_data.AddRead(
453 ConstructResponseHeadersPacket(1, kClientDataStreamId1, false, false,
454 GetResponseHeaders("200 OK")));
455 mock_quic_data.AddRead(
456 ConstructDataPacket(2, kClientDataStreamId1, false, true, 0, "hello!"));
457 mock_quic_data.AddWrite(ConstructAckPacket(2, 1));
458 mock_quic_data.AddRead(SYNCHRONOUS, 0); // EOF
460 mock_quic_data.AddDelayedSocketDataToFactory(&socket_factory_, 1);
462 // There is no need to set up an alternate protocol job, because
463 // no attempt will be made to speak to the proxy over TCP.
465 CreateSession();
467 SendRequestAndExpectQuicResponseFromProxyOnPort("hello!", 70);
470 TEST_P(QuicNetworkTransactionTest, ForceQuicWithErrorConnecting) {
471 params_.origin_to_force_quic_on =
472 HostPortPair::FromString("www.google.com:80");
474 MockQuicData mock_quic_data;
475 mock_quic_data.AddRead(ASYNC, ERR_SOCKET_NOT_CONNECTED);
477 mock_quic_data.AddDelayedSocketDataToFactory(&socket_factory_, 0);
479 CreateSession();
481 scoped_ptr<HttpNetworkTransaction> trans(
482 new HttpNetworkTransaction(DEFAULT_PRIORITY, session_.get()));
483 TestCompletionCallback callback;
484 int rv = trans->Start(&request_, callback.callback(), net_log_.bound());
485 EXPECT_EQ(ERR_IO_PENDING, rv);
486 EXPECT_EQ(ERR_CONNECTION_CLOSED, callback.WaitForResult());
489 TEST_P(QuicNetworkTransactionTest, DoNotForceQuicForHttps) {
490 // Attempt to "force" quic on 443, which will not be honored.
491 params_.origin_to_force_quic_on =
492 HostPortPair::FromString("www.google.com:443");
494 MockRead http_reads[] = {
495 MockRead("HTTP/1.1 200 OK\r\n\r\n"),
496 MockRead("hello world"),
497 MockRead(SYNCHRONOUS, ERR_TEST_PEER_CLOSE_AFTER_NEXT_MOCK_READ),
498 MockRead(ASYNC, OK)
501 StaticSocketDataProvider data(http_reads, arraysize(http_reads), nullptr, 0);
502 socket_factory_.AddSocketDataProvider(&data);
503 SSLSocketDataProvider ssl(ASYNC, OK);
504 socket_factory_.AddSSLSocketDataProvider(&ssl);
506 CreateSession();
508 SendRequestAndExpectHttpResponse("hello world");
511 TEST_P(QuicNetworkTransactionTest, UseAlternateProtocolForQuic) {
512 MockRead http_reads[] = {
513 MockRead("HTTP/1.1 200 OK\r\n"),
514 MockRead(kQuicAlternateProtocolHttpHeader),
515 MockRead("hello world"),
516 MockRead(SYNCHRONOUS, ERR_TEST_PEER_CLOSE_AFTER_NEXT_MOCK_READ),
517 MockRead(ASYNC, OK)
520 StaticSocketDataProvider http_data(http_reads, arraysize(http_reads),
521 nullptr, 0);
522 socket_factory_.AddSocketDataProvider(&http_data);
524 MockQuicData mock_quic_data;
525 mock_quic_data.AddWrite(
526 ConstructRequestHeadersPacket(1, kClientDataStreamId1, true, true,
527 GetRequestHeaders("GET", "http", "/")));
528 mock_quic_data.AddRead(
529 ConstructResponseHeadersPacket(1, kClientDataStreamId1, false, false,
530 GetResponseHeaders("200 OK")));
531 mock_quic_data.AddRead(
532 ConstructDataPacket(2, kClientDataStreamId1, false, true, 0, "hello!"));
533 mock_quic_data.AddWrite(ConstructAckPacket(2, 1));
534 mock_quic_data.AddRead(SYNCHRONOUS, 0); // EOF
536 mock_quic_data.AddDelayedSocketDataToFactory(&socket_factory_, 1);
538 // The non-alternate protocol job needs to hang in order to guarantee that
539 // the alternate-protocol job will "win".
540 AddHangingNonAlternateProtocolSocketData();
542 CreateSessionWithNextProtos();
544 SendRequestAndExpectHttpResponse("hello world");
545 SendRequestAndExpectQuicResponse("hello!");
548 TEST_P(QuicNetworkTransactionTest, AlternateProtocolDifferentPort) {
549 MockRead http_reads[] = {
550 MockRead("HTTP/1.1 200 OK\r\n"),
551 MockRead(kQuicAlternateProtocolDifferentPortHttpHeader),
552 MockRead("hello world"),
553 MockRead(SYNCHRONOUS, ERR_TEST_PEER_CLOSE_AFTER_NEXT_MOCK_READ),
554 MockRead(ASYNC, OK)};
556 StaticSocketDataProvider http_data(http_reads, arraysize(http_reads), nullptr,
558 socket_factory_.AddSocketDataProvider(&http_data);
560 MockQuicData mock_quic_data;
561 mock_quic_data.AddWrite(
562 ConstructRequestHeadersPacket(1, kClientDataStreamId1, true, true,
563 GetRequestHeaders("GET", "http", "/")));
564 mock_quic_data.AddRead(ConstructResponseHeadersPacket(
565 1, kClientDataStreamId1, false, false, GetResponseHeaders("200 OK")));
566 mock_quic_data.AddRead(
567 ConstructDataPacket(2, kClientDataStreamId1, false, true, 0, "hello!"));
568 mock_quic_data.AddWrite(ConstructAckPacket(2, 1));
569 mock_quic_data.AddRead(SYNCHRONOUS, 0); // EOF
571 mock_quic_data.AddDelayedSocketDataToFactory(&socket_factory_, 1);
573 // The non-alternate protocol job needs to hang in order to guarantee that
574 // the alternate-protocol job will "win".
575 AddHangingNonAlternateProtocolSocketData();
577 CreateSessionWithNextProtos();
579 SendRequestAndExpectHttpResponse("hello world");
580 SendRequestAndExpectQuicResponseOnPort("hello!", 137);
583 TEST_P(QuicNetworkTransactionTest, ConfirmAlternativeService) {
584 MockRead http_reads[] = {
585 MockRead("HTTP/1.1 200 OK\r\n"),
586 MockRead(kQuicAlternateProtocolHttpHeader),
587 MockRead("hello world"),
588 MockRead(SYNCHRONOUS, ERR_TEST_PEER_CLOSE_AFTER_NEXT_MOCK_READ),
589 MockRead(ASYNC, OK)};
591 StaticSocketDataProvider http_data(http_reads, arraysize(http_reads), nullptr,
593 socket_factory_.AddSocketDataProvider(&http_data);
595 MockQuicData mock_quic_data;
596 mock_quic_data.AddWrite(
597 ConstructRequestHeadersPacket(1, kClientDataStreamId1, true, true,
598 GetRequestHeaders("GET", "http", "/")));
599 mock_quic_data.AddRead(ConstructResponseHeadersPacket(
600 1, kClientDataStreamId1, false, false, GetResponseHeaders("200 OK")));
601 mock_quic_data.AddRead(
602 ConstructDataPacket(2, kClientDataStreamId1, false, true, 0, "hello!"));
603 mock_quic_data.AddWrite(ConstructAckPacket(2, 1));
604 mock_quic_data.AddRead(SYNCHRONOUS, 0); // EOF
606 mock_quic_data.AddDelayedSocketDataToFactory(&socket_factory_, 1);
608 // The non-alternate protocol job needs to hang in order to guarantee that
609 // the alternate-protocol job will "win".
610 AddHangingNonAlternateProtocolSocketData();
612 CreateSessionWithNextProtos();
614 AlternativeService alternative_service(QUIC,
615 HostPortPair::FromURL(request_.url));
616 session_->http_server_properties()->MarkAlternativeServiceRecentlyBroken(
617 alternative_service);
618 EXPECT_TRUE(
619 session_->http_server_properties()->WasAlternativeServiceRecentlyBroken(
620 alternative_service));
622 SendRequestAndExpectHttpResponse("hello world");
623 SendRequestAndExpectQuicResponse("hello!");
625 EXPECT_FALSE(
626 session_->http_server_properties()->WasAlternativeServiceRecentlyBroken(
627 alternative_service));
630 TEST_P(QuicNetworkTransactionTest, UseAlternateProtocolProbabilityForQuic) {
631 MockRead http_reads[] = {
632 MockRead("HTTP/1.1 200 OK\r\n"),
633 MockRead(kQuicAlternateProtocol50pctHttpHeader),
634 MockRead("hello world"),
635 MockRead(SYNCHRONOUS, ERR_TEST_PEER_CLOSE_AFTER_NEXT_MOCK_READ),
636 MockRead(ASYNC, OK)
639 StaticSocketDataProvider http_data(http_reads, arraysize(http_reads),
640 nullptr, 0);
641 socket_factory_.AddSocketDataProvider(&http_data);
643 MockQuicData mock_quic_data;
644 mock_quic_data.AddWrite(
645 ConstructRequestHeadersPacket(1, kClientDataStreamId1, true, true,
646 GetRequestHeaders("GET", "http", "/")));
647 mock_quic_data.AddRead(
648 ConstructResponseHeadersPacket(1, kClientDataStreamId1, false, false,
649 GetResponseHeaders("200 OK")));
650 mock_quic_data.AddRead(
651 ConstructDataPacket(2, kClientDataStreamId1, false, true, 0, "hello!"));
652 mock_quic_data.AddWrite(ConstructAckPacket(2, 1));
653 mock_quic_data.AddRead(SYNCHRONOUS, 0); // EOF
655 mock_quic_data.AddDelayedSocketDataToFactory(&socket_factory_, 1);
657 // The non-alternate protocol job needs to hang in order to guarantee that
658 // the alternate-protocol job will "win".
659 AddHangingNonAlternateProtocolSocketData();
661 params_.alternate_protocol_probability_threshold = .25;
662 CreateSessionWithNextProtos();
664 SendRequestAndExpectHttpResponse("hello world");
665 SendRequestAndExpectQuicResponse("hello!");
668 TEST_P(QuicNetworkTransactionTest, DontUseAlternateProtocolProbabilityForQuic) {
669 MockRead http_reads[] = {
670 MockRead("HTTP/1.1 200 OK\r\n"),
671 MockRead(kQuicAlternateProtocol50pctHttpHeader),
672 MockRead("hello world"),
673 MockRead(SYNCHRONOUS, ERR_TEST_PEER_CLOSE_AFTER_NEXT_MOCK_READ),
674 MockRead(ASYNC, OK)
677 StaticSocketDataProvider http_data(http_reads, arraysize(http_reads),
678 nullptr, 0);
679 socket_factory_.AddSocketDataProvider(&http_data);
680 socket_factory_.AddSocketDataProvider(&http_data);
682 params_.alternate_protocol_probability_threshold = .75;
683 CreateSessionWithNextProtos();
685 SendRequestAndExpectHttpResponse("hello world");
686 SendRequestAndExpectHttpResponse("hello world");
689 TEST_P(QuicNetworkTransactionTest,
690 DontUseAlternateProtocolWithBadProbabilityForQuic) {
691 MockRead http_reads[] = {
692 MockRead("HTTP/1.1 200 OK\r\n"),
693 MockRead("Alternate-Protocol: 443:quic,p=2\r\n\r\n"),
694 MockRead("hello world"),
695 MockRead(SYNCHRONOUS, ERR_TEST_PEER_CLOSE_AFTER_NEXT_MOCK_READ),
696 MockRead(ASYNC, OK)
699 StaticSocketDataProvider http_data(http_reads, arraysize(http_reads),
700 nullptr, 0);
701 socket_factory_.AddSocketDataProvider(&http_data);
702 socket_factory_.AddSocketDataProvider(&http_data);
704 params_.alternate_protocol_probability_threshold = .75;
705 CreateSessionWithNextProtos();
707 SendRequestAndExpectHttpResponse("hello world");
708 SendRequestAndExpectHttpResponse("hello world");
711 TEST_P(QuicNetworkTransactionTest, UseAlternateProtocolForQuicForHttps) {
712 params_.origin_to_force_quic_on =
713 HostPortPair::FromString("www.google.com:443");
715 MockRead http_reads[] = {
716 MockRead("HTTP/1.1 200 OK\r\n"),
717 MockRead(kQuicAlternateProtocolHttpsHeader),
718 MockRead("hello world"),
719 MockRead(SYNCHRONOUS, ERR_TEST_PEER_CLOSE_AFTER_NEXT_MOCK_READ),
720 MockRead(ASYNC, OK)
723 StaticSocketDataProvider http_data(http_reads, arraysize(http_reads),
724 nullptr, 0);
725 socket_factory_.AddSocketDataProvider(&http_data);
727 MockQuicData mock_quic_data;
728 mock_quic_data.AddWrite(
729 ConstructRequestHeadersPacket(1, kClientDataStreamId1, true, true,
730 GetRequestHeaders("GET", "http", "/")));
731 mock_quic_data.AddRead(
732 ConstructResponseHeadersPacket(1, kClientDataStreamId1, false, false,
733 GetResponseHeaders("200 OK")));
734 mock_quic_data.AddRead(
735 ConstructDataPacket(2, kClientDataStreamId1, false, true, 0, "hello!"));
736 mock_quic_data.AddWrite(ConstructAckPacket(2, 1));
737 mock_quic_data.AddRead(SYNCHRONOUS, 0); // EOF
739 mock_quic_data.AddDelayedSocketDataToFactory(&socket_factory_, 1);
741 // The non-alternate protocol job needs to hang in order to guarantee that
742 // the alternate-protocol job will "win".
743 AddHangingNonAlternateProtocolSocketData();
745 CreateSessionWithNextProtos();
747 // TODO(rtenneti): Test QUIC over HTTPS, GetSSLInfo().
748 SendRequestAndExpectHttpResponse("hello world");
751 TEST_P(QuicNetworkTransactionTest, HungAlternateProtocol) {
752 crypto_client_stream_factory_.set_handshake_mode(
753 MockCryptoClientStream::COLD_START);
755 MockWrite http_writes[] = {
756 MockWrite(SYNCHRONOUS, 0, "GET / HTTP/1.1\r\n"),
757 MockWrite(SYNCHRONOUS, 1, "Host: www.google.com\r\n"),
758 MockWrite(SYNCHRONOUS, 2, "Connection: keep-alive\r\n\r\n")
761 MockRead http_reads[] = {
762 MockRead(SYNCHRONOUS, 3, "HTTP/1.1 200 OK\r\n"),
763 MockRead(SYNCHRONOUS, 4, kQuicAlternateProtocolHttpHeader),
764 MockRead(SYNCHRONOUS, 5, "hello world"),
765 MockRead(SYNCHRONOUS, OK, 6)
768 DeterministicMockClientSocketFactory socket_factory;
770 DeterministicSocketData http_data(http_reads, arraysize(http_reads),
771 http_writes, arraysize(http_writes));
772 socket_factory.AddSocketDataProvider(&http_data);
774 // The QUIC transaction will not be allowed to complete.
775 MockWrite quic_writes[] = {
776 MockWrite(ASYNC, ERR_IO_PENDING, 0)
778 MockRead quic_reads[] = {
779 MockRead(ASYNC, ERR_IO_PENDING, 1),
781 DeterministicSocketData quic_data(quic_reads, arraysize(quic_reads),
782 quic_writes, arraysize(quic_writes));
783 socket_factory.AddSocketDataProvider(&quic_data);
785 // The HTTP transaction will complete.
786 DeterministicSocketData http_data2(http_reads, arraysize(http_reads),
787 http_writes, arraysize(http_writes));
788 socket_factory.AddSocketDataProvider(&http_data2);
790 CreateSessionWithFactory(&socket_factory, true);
792 // Run the first request.
793 http_data.StopAfter(arraysize(http_reads) + arraysize(http_writes));
794 SendRequestAndExpectHttpResponse("hello world");
795 ASSERT_TRUE(http_data.at_read_eof());
796 ASSERT_TRUE(http_data.at_write_eof());
798 // Now run the second request in which the QUIC socket hangs,
799 // and verify the the transaction continues over HTTP.
800 http_data2.StopAfter(arraysize(http_reads) + arraysize(http_writes));
801 SendRequestAndExpectHttpResponse("hello world");
803 ASSERT_TRUE(http_data2.at_read_eof());
804 ASSERT_TRUE(http_data2.at_write_eof());
805 ASSERT_TRUE(!quic_data.at_read_eof());
806 ASSERT_TRUE(!quic_data.at_write_eof());
809 TEST_P(QuicNetworkTransactionTest, ZeroRTTWithHttpRace) {
810 MockQuicData mock_quic_data;
811 mock_quic_data.AddWrite(
812 ConstructRequestHeadersPacket(1, kClientDataStreamId1, true, true,
813 GetRequestHeaders("GET", "http", "/")));
814 mock_quic_data.AddRead(
815 ConstructResponseHeadersPacket(1, kClientDataStreamId1, false, false,
816 GetResponseHeaders("200 OK")));
817 mock_quic_data.AddRead(
818 ConstructDataPacket(2, kClientDataStreamId1, false, true, 0, "hello!"));
819 mock_quic_data.AddWrite(ConstructAckPacket(2, 1));
820 mock_quic_data.AddRead(SYNCHRONOUS, 0); // EOF
822 mock_quic_data.AddDelayedSocketDataToFactory(&socket_factory_, 1);
824 // The non-alternate protocol job needs to hang in order to guarantee that
825 // the alternate-protocol job will "win".
826 AddHangingNonAlternateProtocolSocketData();
828 CreateSessionWithNextProtos();
829 AddQuicAlternateProtocolMapping(MockCryptoClientStream::ZERO_RTT);
830 SendRequestAndExpectQuicResponse("hello!");
833 TEST_P(QuicNetworkTransactionTest, ZeroRTTWithNoHttpRace) {
834 MockQuicData mock_quic_data;
835 mock_quic_data.AddWrite(
836 ConstructRequestHeadersPacket(1, kClientDataStreamId1, true, true,
837 GetRequestHeaders("GET", "http", "/")));
838 mock_quic_data.AddRead(
839 ConstructResponseHeadersPacket(1, kClientDataStreamId1, false, false,
840 GetResponseHeaders("200 OK")));
841 mock_quic_data.AddRead(
842 ConstructDataPacket(2, kClientDataStreamId1, false, true, 0, "hello!"));
843 mock_quic_data.AddWrite(ConstructAckPacket(2, 1));
844 mock_quic_data.AddRead(SYNCHRONOUS, 0); // EOF
845 mock_quic_data.AddDelayedSocketDataToFactory(&socket_factory_, 1);
847 // In order for a new QUIC session to be established via alternate-protocol
848 // without racing an HTTP connection, we need the host resolution to happen
849 // synchronously.
850 host_resolver_.set_synchronous_mode(true);
851 host_resolver_.rules()->AddIPLiteralRule("www.google.com", "192.168.0.1", "");
852 HostResolver::RequestInfo info(HostPortPair("www.google.com", 80));
853 AddressList address;
854 host_resolver_.Resolve(info,
855 DEFAULT_PRIORITY,
856 &address,
857 CompletionCallback(),
858 nullptr,
859 net_log_.bound());
861 CreateSessionWithNextProtos();
862 AddQuicAlternateProtocolMapping(MockCryptoClientStream::ZERO_RTT);
863 SendRequestAndExpectQuicResponse("hello!");
866 TEST_P(QuicNetworkTransactionTest, ZeroRTTWithProxy) {
867 proxy_service_.reset(
868 ProxyService::CreateFixedFromPacResult("PROXY myproxy:70"));
870 // Since we are using a proxy, the QUIC job will not succeed.
871 MockWrite http_writes[] = {
872 MockWrite(SYNCHRONOUS, 0, "GET http://www.google.com/ HTTP/1.1\r\n"),
873 MockWrite(SYNCHRONOUS, 1, "Host: www.google.com\r\n"),
874 MockWrite(SYNCHRONOUS, 2, "Proxy-Connection: keep-alive\r\n\r\n")
877 MockRead http_reads[] = {
878 MockRead(SYNCHRONOUS, 3, "HTTP/1.1 200 OK\r\n"),
879 MockRead(SYNCHRONOUS, 4, kQuicAlternateProtocolHttpHeader),
880 MockRead(SYNCHRONOUS, 5, "hello world"),
881 MockRead(SYNCHRONOUS, OK, 6)
884 StaticSocketDataProvider http_data(http_reads, arraysize(http_reads),
885 http_writes, arraysize(http_writes));
886 socket_factory_.AddSocketDataProvider(&http_data);
888 // In order for a new QUIC session to be established via alternate-protocol
889 // without racing an HTTP connection, we need the host resolution to happen
890 // synchronously.
891 host_resolver_.set_synchronous_mode(true);
892 host_resolver_.rules()->AddIPLiteralRule("www.google.com", "192.168.0.1", "");
893 HostResolver::RequestInfo info(HostPortPair("www.google.com", 80));
894 AddressList address;
895 host_resolver_.Resolve(info,
896 DEFAULT_PRIORITY,
897 &address,
898 CompletionCallback(),
899 nullptr,
900 net_log_.bound());
902 CreateSessionWithNextProtos();
903 AddQuicAlternateProtocolMapping(MockCryptoClientStream::ZERO_RTT);
904 SendRequestAndExpectHttpResponse("hello world");
907 TEST_P(QuicNetworkTransactionTest, ZeroRTTWithConfirmationRequired) {
908 MockQuicData mock_quic_data;
909 mock_quic_data.AddWrite(
910 ConstructRequestHeadersPacket(1, kClientDataStreamId1, true, true,
911 GetRequestHeaders("GET", "http", "/")));
912 mock_quic_data.AddRead(
913 ConstructResponseHeadersPacket(1, kClientDataStreamId1, false, false,
914 GetResponseHeaders("200 OK")));
915 mock_quic_data.AddRead(
916 ConstructDataPacket(2, kClientDataStreamId1, false, true, 0, "hello!"));
917 mock_quic_data.AddWrite(ConstructAckPacket(2, 1));
918 mock_quic_data.AddRead(SYNCHRONOUS, 0); // EOF
919 mock_quic_data.AddDelayedSocketDataToFactory(&socket_factory_, 1);
921 // The non-alternate protocol job needs to hang in order to guarantee that
922 // the alternate-protocol job will "win".
923 AddHangingNonAlternateProtocolSocketData();
925 // In order for a new QUIC session to be established via alternate-protocol
926 // without racing an HTTP connection, we need the host resolution to happen
927 // synchronously. Of course, even though QUIC *could* perform a 0-RTT
928 // connection to the the server, in this test we require confirmation
929 // before encrypting so the HTTP job will still start.
930 host_resolver_.set_synchronous_mode(true);
931 host_resolver_.rules()->AddIPLiteralRule("www.google.com", "192.168.0.1", "");
932 HostResolver::RequestInfo info(HostPortPair("www.google.com", 80));
933 AddressList address;
934 host_resolver_.Resolve(info, DEFAULT_PRIORITY, &address,
935 CompletionCallback(), nullptr, net_log_.bound());
937 CreateSessionWithNextProtos();
938 session_->quic_stream_factory()->set_require_confirmation(true);
939 AddQuicAlternateProtocolMapping(MockCryptoClientStream::ZERO_RTT);
941 scoped_ptr<HttpNetworkTransaction> trans(
942 new HttpNetworkTransaction(DEFAULT_PRIORITY, session_.get()));
943 TestCompletionCallback callback;
944 int rv = trans->Start(&request_, callback.callback(), net_log_.bound());
945 EXPECT_EQ(ERR_IO_PENDING, rv);
947 crypto_client_stream_factory_.last_stream()->SendOnCryptoHandshakeEvent(
948 QuicSession::HANDSHAKE_CONFIRMED);
949 EXPECT_EQ(OK, callback.WaitForResult());
952 TEST_P(QuicNetworkTransactionTest, BrokenAlternateProtocol) {
953 // Alternate-protocol job
954 scoped_ptr<QuicEncryptedPacket> close(ConstructConnectionClosePacket(1));
955 MockRead quic_reads[] = {
956 MockRead(ASYNC, close->data(), close->length()),
957 MockRead(ASYNC, OK), // EOF
959 StaticSocketDataProvider quic_data(quic_reads, arraysize(quic_reads),
960 nullptr, 0);
961 socket_factory_.AddSocketDataProvider(&quic_data);
963 // Main job which will succeed even though the alternate job fails.
964 MockRead http_reads[] = {
965 MockRead("HTTP/1.1 200 OK\r\n\r\n"),
966 MockRead("hello from http"),
967 MockRead(SYNCHRONOUS, ERR_TEST_PEER_CLOSE_AFTER_NEXT_MOCK_READ),
968 MockRead(ASYNC, OK)
971 StaticSocketDataProvider http_data(http_reads, arraysize(http_reads),
972 nullptr, 0);
973 socket_factory_.AddSocketDataProvider(&http_data);
975 CreateSessionWithNextProtos();
976 AddQuicAlternateProtocolMapping(MockCryptoClientStream::COLD_START);
977 SendRequestAndExpectHttpResponse("hello from http");
978 ExpectBrokenAlternateProtocolMapping();
981 TEST_P(QuicNetworkTransactionTest, BrokenAlternateProtocolReadError) {
982 // Alternate-protocol job
983 MockRead quic_reads[] = {
984 MockRead(ASYNC, ERR_SOCKET_NOT_CONNECTED),
986 StaticSocketDataProvider quic_data(quic_reads, arraysize(quic_reads),
987 nullptr, 0);
988 socket_factory_.AddSocketDataProvider(&quic_data);
990 // Main job which will succeed even though the alternate job fails.
991 MockRead http_reads[] = {
992 MockRead("HTTP/1.1 200 OK\r\n\r\n"),
993 MockRead("hello from http"),
994 MockRead(SYNCHRONOUS, ERR_TEST_PEER_CLOSE_AFTER_NEXT_MOCK_READ),
995 MockRead(ASYNC, OK)
998 StaticSocketDataProvider http_data(http_reads, arraysize(http_reads),
999 nullptr, 0);
1000 socket_factory_.AddSocketDataProvider(&http_data);
1002 CreateSessionWithNextProtos();
1004 AddQuicAlternateProtocolMapping(MockCryptoClientStream::COLD_START);
1005 SendRequestAndExpectHttpResponse("hello from http");
1006 ExpectBrokenAlternateProtocolMapping();
1009 TEST_P(QuicNetworkTransactionTest, NoBrokenAlternateProtocolIfTcpFails) {
1010 // Alternate-protocol job will fail when the session attempts to read.
1011 MockRead quic_reads[] = {
1012 MockRead(ASYNC, ERR_SOCKET_NOT_CONNECTED),
1014 StaticSocketDataProvider quic_data(quic_reads, arraysize(quic_reads),
1015 nullptr, 0);
1016 socket_factory_.AddSocketDataProvider(&quic_data);
1018 // Main job will also fail.
1019 MockRead http_reads[] = {
1020 MockRead(ASYNC, ERR_SOCKET_NOT_CONNECTED),
1023 StaticSocketDataProvider http_data(http_reads, arraysize(http_reads),
1024 nullptr, 0);
1025 http_data.set_connect_data(MockConnect(ASYNC, ERR_SOCKET_NOT_CONNECTED));
1026 socket_factory_.AddSocketDataProvider(&http_data);
1028 CreateSessionWithNextProtos();
1030 AddQuicAlternateProtocolMapping(MockCryptoClientStream::COLD_START);
1031 scoped_ptr<HttpNetworkTransaction> trans(
1032 new HttpNetworkTransaction(DEFAULT_PRIORITY, session_.get()));
1033 TestCompletionCallback callback;
1034 int rv = trans->Start(&request_, callback.callback(), net_log_.bound());
1035 EXPECT_EQ(ERR_IO_PENDING, rv);
1036 EXPECT_EQ(ERR_SOCKET_NOT_CONNECTED, callback.WaitForResult());
1037 ExpectQuicAlternateProtocolMapping();
1040 TEST_P(QuicNetworkTransactionTest, FailedZeroRttBrokenAlternateProtocol) {
1041 // Alternate-protocol job
1042 MockRead quic_reads[] = {
1043 MockRead(ASYNC, ERR_SOCKET_NOT_CONNECTED),
1045 StaticSocketDataProvider quic_data(quic_reads, arraysize(quic_reads),
1046 nullptr, 0);
1047 socket_factory_.AddSocketDataProvider(&quic_data);
1049 AddHangingNonAlternateProtocolSocketData();
1051 // Second Alternate-protocol job which will race with the TCP job.
1052 StaticSocketDataProvider quic_data2(quic_reads, arraysize(quic_reads),
1053 nullptr, 0);
1054 socket_factory_.AddSocketDataProvider(&quic_data2);
1056 // Final job that will proceed when the QUIC job fails.
1057 MockRead http_reads[] = {
1058 MockRead("HTTP/1.1 200 OK\r\n\r\n"),
1059 MockRead("hello from http"),
1060 MockRead(SYNCHRONOUS, ERR_TEST_PEER_CLOSE_AFTER_NEXT_MOCK_READ),
1061 MockRead(ASYNC, OK)
1064 StaticSocketDataProvider http_data(http_reads, arraysize(http_reads),
1065 nullptr, 0);
1066 socket_factory_.AddSocketDataProvider(&http_data);
1068 CreateSessionWithNextProtos();
1070 AddQuicAlternateProtocolMapping(MockCryptoClientStream::ZERO_RTT);
1072 SendRequestAndExpectHttpResponse("hello from http");
1074 ExpectBrokenAlternateProtocolMapping();
1076 EXPECT_TRUE(quic_data.at_read_eof());
1077 EXPECT_TRUE(quic_data.at_write_eof());
1080 TEST_P(QuicNetworkTransactionTest, DISABLED_HangingZeroRttFallback) {
1081 // Alternate-protocol job
1082 MockRead quic_reads[] = {
1083 MockRead(ASYNC, ERR_IO_PENDING),
1085 StaticSocketDataProvider quic_data(quic_reads, arraysize(quic_reads),
1086 nullptr, 0);
1087 socket_factory_.AddSocketDataProvider(&quic_data);
1089 // Main job that will proceed when the QUIC job fails.
1090 MockRead http_reads[] = {
1091 MockRead("HTTP/1.1 200 OK\r\n\r\n"),
1092 MockRead("hello from http"),
1093 MockRead(SYNCHRONOUS, ERR_TEST_PEER_CLOSE_AFTER_NEXT_MOCK_READ),
1094 MockRead(ASYNC, OK)
1097 StaticSocketDataProvider http_data(http_reads, arraysize(http_reads),
1098 nullptr, 0);
1099 socket_factory_.AddSocketDataProvider(&http_data);
1101 CreateSessionWithNextProtos();
1103 AddQuicAlternateProtocolMapping(MockCryptoClientStream::ZERO_RTT);
1105 SendRequestAndExpectHttpResponse("hello from http");
1108 TEST_P(QuicNetworkTransactionTest, BrokenAlternateProtocolOnConnectFailure) {
1109 // Alternate-protocol job will fail before creating a QUIC session.
1110 StaticSocketDataProvider quic_data(nullptr, 0, nullptr, 0);
1111 quic_data.set_connect_data(MockConnect(SYNCHRONOUS,
1112 ERR_INTERNET_DISCONNECTED));
1113 socket_factory_.AddSocketDataProvider(&quic_data);
1115 // Main job which will succeed even though the alternate job fails.
1116 MockRead http_reads[] = {
1117 MockRead("HTTP/1.1 200 OK\r\n\r\n"),
1118 MockRead("hello from http"),
1119 MockRead(SYNCHRONOUS, ERR_TEST_PEER_CLOSE_AFTER_NEXT_MOCK_READ),
1120 MockRead(ASYNC, OK)
1123 StaticSocketDataProvider http_data(http_reads, arraysize(http_reads),
1124 nullptr, 0);
1125 socket_factory_.AddSocketDataProvider(&http_data);
1127 CreateSessionWithNextProtos();
1128 AddQuicAlternateProtocolMapping(MockCryptoClientStream::COLD_START);
1129 SendRequestAndExpectHttpResponse("hello from http");
1131 ExpectBrokenAlternateProtocolMapping();
1134 TEST_P(QuicNetworkTransactionTest, ConnectionCloseDuringConnect) {
1135 MockQuicData mock_quic_data;
1136 mock_quic_data.AddRead(ConstructConnectionClosePacket(1));
1137 mock_quic_data.AddWrite(
1138 ConstructRequestHeadersPacket(1, kClientDataStreamId1, true, true,
1139 GetRequestHeaders("GET", "http", "/")));
1140 mock_quic_data.AddWrite(ConstructAckPacket(2, 1));
1141 mock_quic_data.AddDelayedSocketDataToFactory(&socket_factory_, 0);
1143 // When the QUIC connection fails, we will try the request again over HTTP.
1144 MockRead http_reads[] = {
1145 MockRead("HTTP/1.1 200 OK\r\n"),
1146 MockRead(kQuicAlternateProtocolHttpHeader),
1147 MockRead("hello world"),
1148 MockRead(SYNCHRONOUS, ERR_TEST_PEER_CLOSE_AFTER_NEXT_MOCK_READ),
1149 MockRead(ASYNC, OK)
1152 StaticSocketDataProvider http_data(http_reads, arraysize(http_reads),
1153 nullptr, 0);
1154 socket_factory_.AddSocketDataProvider(&http_data);
1156 // In order for a new QUIC session to be established via alternate-protocol
1157 // without racing an HTTP connection, we need the host resolution to happen
1158 // synchronously.
1159 host_resolver_.set_synchronous_mode(true);
1160 host_resolver_.rules()->AddIPLiteralRule("www.google.com", "192.168.0.1", "");
1161 HostResolver::RequestInfo info(HostPortPair("www.google.com", 80));
1162 AddressList address;
1163 host_resolver_.Resolve(info,
1164 DEFAULT_PRIORITY,
1165 &address,
1166 CompletionCallback(),
1167 nullptr,
1168 net_log_.bound());
1170 CreateSessionWithNextProtos();
1171 AddQuicAlternateProtocolMapping(MockCryptoClientStream::ZERO_RTT);
1172 SendRequestAndExpectHttpResponse("hello world");
1175 } // namespace test
1176 } // namespace net