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.
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/base/test_data_directory.h"
13 #include "net/cert/mock_cert_verifier.h"
14 #include "net/dns/mock_host_resolver.h"
15 #include "net/http/http_auth_handler_factory.h"
16 #include "net/http/http_network_session.h"
17 #include "net/http/http_network_transaction.h"
18 #include "net/http/http_server_properties_impl.h"
19 #include "net/http/http_stream.h"
20 #include "net/http/http_stream_factory.h"
21 #include "net/http/http_transaction_test_util.h"
22 #include "net/http/transport_security_state.h"
23 #include "net/log/test_net_log.h"
24 #include "net/log/test_net_log_entry.h"
25 #include "net/log/test_net_log_util.h"
26 #include "net/proxy/proxy_config_service_fixed.h"
27 #include "net/proxy/proxy_resolver.h"
28 #include "net/proxy/proxy_service.h"
29 #include "net/quic/crypto/proof_verifier_chromium.h"
30 #include "net/quic/crypto/quic_decrypter.h"
31 #include "net/quic/crypto/quic_encrypter.h"
32 #include "net/quic/quic_framer.h"
33 #include "net/quic/quic_http_utils.h"
34 #include "net/quic/test_tools/crypto_test_utils.h"
35 #include "net/quic/test_tools/mock_clock.h"
36 #include "net/quic/test_tools/mock_crypto_client_stream_factory.h"
37 #include "net/quic/test_tools/mock_random.h"
38 #include "net/quic/test_tools/quic_test_packet_maker.h"
39 #include "net/quic/test_tools/quic_test_utils.h"
40 #include "net/socket/client_socket_factory.h"
41 #include "net/socket/mock_client_socket_pool_manager.h"
42 #include "net/socket/socket_test_util.h"
43 #include "net/socket/ssl_client_socket.h"
44 #include "net/spdy/spdy_frame_builder.h"
45 #include "net/spdy/spdy_framer.h"
46 #include "net/ssl/ssl_config_service_defaults.h"
47 #include "net/test/cert_test_util.h"
48 #include "testing/gtest/include/gtest/gtest.h"
49 #include "testing/platform_test.h"
56 // This is the expected return from a current server advertising QUIC.
57 static const char kQuicAlternateProtocolHttpHeader
[] =
58 "Alternate-Protocol: 80:quic\r\n\r\n";
59 static const char kQuicAlternateProtocol50pctHttpHeader
[] =
60 "Alternate-Protocol: 80:quic,p=.5\r\n\r\n";
61 static const char kQuicAlternateProtocolDifferentPortHttpHeader
[] =
62 "Alternate-Protocol: 137:quic\r\n\r\n";
63 static const char kQuicAlternateProtocolHttpsHeader
[] =
64 "Alternate-Protocol: 443:quic\r\n\r\n";
68 // Helper class to encapsulate MockReads and MockWrites for QUIC.
69 // Simplify ownership issues and the interaction with the MockSocketFactory.
72 MockQuicData() : sequence_number_(0) {}
75 STLDeleteElements(&packets_
);
78 void AddSynchronousRead(scoped_ptr
<QuicEncryptedPacket
> packet
) {
79 reads_
.push_back(MockRead(SYNCHRONOUS
, packet
->data(), packet
->length(),
81 packets_
.push_back(packet
.release());
84 void AddRead(scoped_ptr
<QuicEncryptedPacket
> packet
) {
86 MockRead(ASYNC
, packet
->data(), packet
->length(), sequence_number_
++));
87 packets_
.push_back(packet
.release());
90 void AddRead(IoMode mode
, int rv
) {
91 reads_
.push_back(MockRead(mode
, rv
, sequence_number_
++));
94 void AddWrite(scoped_ptr
<QuicEncryptedPacket
> packet
) {
95 writes_
.push_back(MockWrite(SYNCHRONOUS
, packet
->data(), packet
->length(),
97 packets_
.push_back(packet
.release());
100 void AddSocketDataToFactory(MockClientSocketFactory
* factory
) {
101 MockRead
* reads
= reads_
.empty() ? nullptr : &reads_
[0];
102 MockWrite
* writes
= writes_
.empty() ? nullptr : &writes_
[0];
104 new SequencedSocketData(reads
, reads_
.size(), writes
, writes_
.size()));
105 factory
->AddSocketDataProvider(socket_data_
.get());
109 std::vector
<QuicEncryptedPacket
*> packets_
;
110 std::vector
<MockWrite
> writes_
;
111 std::vector
<MockRead
> reads_
;
112 size_t sequence_number_
;
113 scoped_ptr
<SocketDataProvider
> socket_data_
;
116 class ProxyHeadersHandler
{
118 ProxyHeadersHandler() : was_called_(false) {}
120 bool was_called() { return was_called_
; }
122 void OnBeforeProxyHeadersSent(const ProxyInfo
& proxy_info
,
123 HttpRequestHeaders
* request_headers
) {
131 class QuicNetworkTransactionTest
132 : public PlatformTest
,
133 public ::testing::WithParamInterface
<QuicVersion
> {
135 QuicNetworkTransactionTest()
136 : clock_(new MockClock
),
137 maker_(GetParam(), 0, clock_
),
138 ssl_config_service_(new SSLConfigServiceDefaults
),
139 proxy_service_(ProxyService::CreateDirect()),
140 auth_handler_factory_(
141 HttpAuthHandlerFactory::CreateDefault(&host_resolver_
)),
142 random_generator_(0),
143 hanging_data_(nullptr, 0, nullptr, 0) {
144 request_
.method
= "GET";
145 request_
.url
= GURL("http://www.google.com/");
146 request_
.load_flags
= 0;
147 clock_
->AdvanceTime(QuicTime::Delta::FromMilliseconds(20));
150 void SetUp() override
{
151 NetworkChangeNotifier::NotifyObserversOfIPAddressChangeForTests();
152 base::MessageLoop::current()->RunUntilIdle();
155 void TearDown() override
{
156 NetworkChangeNotifier::NotifyObserversOfIPAddressChangeForTests();
157 // Empty the current queue.
158 base::MessageLoop::current()->RunUntilIdle();
159 PlatformTest::TearDown();
160 NetworkChangeNotifier::NotifyObserversOfIPAddressChangeForTests();
161 base::MessageLoop::current()->RunUntilIdle();
164 scoped_ptr
<QuicEncryptedPacket
> ConstructConnectionClosePacket(
165 QuicPacketSequenceNumber num
) {
166 return maker_
.MakeConnectionClosePacket(num
);
169 scoped_ptr
<QuicEncryptedPacket
> ConstructAckPacket(
170 QuicPacketSequenceNumber largest_received
,
171 QuicPacketSequenceNumber least_unacked
) {
172 return maker_
.MakeAckPacket(2, largest_received
, least_unacked
, true);
175 SpdyHeaderBlock
GetRequestHeaders(const std::string
& method
,
176 const std::string
& scheme
,
177 const std::string
& path
) {
178 return maker_
.GetRequestHeaders(method
, scheme
, path
);
181 SpdyHeaderBlock
GetResponseHeaders(const std::string
& status
) {
182 return maker_
.GetResponseHeaders(status
);
185 scoped_ptr
<QuicEncryptedPacket
> ConstructDataPacket(
186 QuicPacketSequenceNumber sequence_number
,
187 QuicStreamId stream_id
,
188 bool should_include_version
,
190 QuicStreamOffset offset
,
191 base::StringPiece data
) {
192 return maker_
.MakeDataPacket(
193 sequence_number
, stream_id
, should_include_version
, fin
, offset
, data
);
196 scoped_ptr
<QuicEncryptedPacket
> ConstructRequestHeadersPacket(
197 QuicPacketSequenceNumber sequence_number
,
198 QuicStreamId stream_id
,
199 bool should_include_version
,
201 const SpdyHeaderBlock
& headers
) {
202 QuicPriority priority
=
203 ConvertRequestPriorityToQuicPriority(DEFAULT_PRIORITY
);
204 return maker_
.MakeRequestHeadersPacket(sequence_number
, stream_id
,
205 should_include_version
, fin
,
209 scoped_ptr
<QuicEncryptedPacket
> ConstructResponseHeadersPacket(
210 QuicPacketSequenceNumber sequence_number
,
211 QuicStreamId stream_id
,
212 bool should_include_version
,
214 const SpdyHeaderBlock
& headers
) {
215 return maker_
.MakeResponseHeadersPacket(
216 sequence_number
, stream_id
, should_include_version
, fin
, headers
);
219 void CreateSession() {
220 CreateSessionWithFactory(&socket_factory_
, false);
223 void CreateSessionWithNextProtos() {
224 CreateSessionWithFactory(&socket_factory_
, true);
227 // If |use_next_protos| is true, enables SPDY and QUIC.
228 void CreateSessionWithFactory(ClientSocketFactory
* socket_factory
,
229 bool use_next_protos
) {
230 params_
.enable_quic
= true;
231 params_
.quic_clock
= clock_
;
232 params_
.quic_random
= &random_generator_
;
233 params_
.client_socket_factory
= socket_factory
;
234 params_
.quic_crypto_client_stream_factory
= &crypto_client_stream_factory_
;
235 params_
.host_resolver
= &host_resolver_
;
236 params_
.cert_verifier
= &cert_verifier_
;
237 params_
.transport_security_state
= &transport_security_state_
;
238 params_
.proxy_service
= proxy_service_
.get();
239 params_
.ssl_config_service
= ssl_config_service_
.get();
240 params_
.http_auth_handler_factory
= auth_handler_factory_
.get();
241 params_
.http_server_properties
= http_server_properties
.GetWeakPtr();
242 params_
.quic_supported_versions
= SupportedVersions(GetParam());
244 if (use_next_protos
) {
245 params_
.use_alternate_protocols
= true;
246 params_
.next_protos
= NextProtosWithSpdyAndQuic(true, true);
249 session_
= new HttpNetworkSession(params_
);
250 session_
->quic_stream_factory()->set_require_confirmation(false);
253 void CheckWasQuicResponse(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_TRUE(response
->was_fetched_via_spdy
);
259 EXPECT_TRUE(response
->was_npn_negotiated
);
260 EXPECT_EQ(HttpResponseInfo::CONNECTION_INFO_QUIC1_SPDY3
,
261 response
->connection_info
);
264 void CheckResponsePort(const scoped_ptr
<HttpNetworkTransaction
>& trans
,
266 const HttpResponseInfo
* response
= trans
->GetResponseInfo();
267 ASSERT_TRUE(response
!= nullptr);
268 EXPECT_EQ(port
, response
->socket_address
.port());
271 void CheckWasHttpResponse(const scoped_ptr
<HttpNetworkTransaction
>& trans
) {
272 const HttpResponseInfo
* response
= trans
->GetResponseInfo();
273 ASSERT_TRUE(response
!= nullptr);
274 ASSERT_TRUE(response
->headers
.get() != nullptr);
275 EXPECT_EQ("HTTP/1.1 200 OK", response
->headers
->GetStatusLine());
276 EXPECT_FALSE(response
->was_fetched_via_spdy
);
277 EXPECT_FALSE(response
->was_npn_negotiated
);
278 EXPECT_EQ(HttpResponseInfo::CONNECTION_INFO_HTTP1
,
279 response
->connection_info
);
282 void CheckResponseData(HttpNetworkTransaction
* trans
,
283 const std::string
& expected
) {
284 std::string response_data
;
285 ASSERT_EQ(OK
, ReadTransaction(trans
, &response_data
));
286 EXPECT_EQ(expected
, response_data
);
289 void RunTransaction(HttpNetworkTransaction
* trans
) {
290 TestCompletionCallback callback
;
291 int rv
= trans
->Start(&request_
, callback
.callback(), net_log_
.bound());
292 EXPECT_EQ(ERR_IO_PENDING
, rv
);
293 EXPECT_EQ(OK
, callback
.WaitForResult());
296 void SendRequestAndExpectHttpResponse(const std::string
& expected
) {
297 scoped_ptr
<HttpNetworkTransaction
> trans(
298 new HttpNetworkTransaction(DEFAULT_PRIORITY
, session_
.get()));
299 RunTransaction(trans
.get());
300 CheckWasHttpResponse(trans
);
301 CheckResponseData(trans
.get(), expected
);
304 void SendRequestAndExpectQuicResponse(const std::string
& expected
) {
305 SendRequestAndExpectQuicResponseMaybeFromProxy(expected
, false, 80);
308 void SendRequestAndExpectQuicResponseOnPort(const std::string
& expected
,
310 SendRequestAndExpectQuicResponseMaybeFromProxy(expected
, false, port
);
313 void SendRequestAndExpectQuicResponseFromProxyOnPort(
314 const std::string
& expected
,
316 SendRequestAndExpectQuicResponseMaybeFromProxy(expected
, true, port
);
319 void AddQuicAlternateProtocolMapping(
320 MockCryptoClientStream::HandshakeMode handshake_mode
) {
321 crypto_client_stream_factory_
.set_handshake_mode(handshake_mode
);
322 HostPortPair host_port_pair
= HostPortPair::FromURL(request_
.url
);
323 AlternativeService
alternative_service(QUIC
, host_port_pair
.host(), 80);
324 session_
->http_server_properties()->SetAlternativeService(
325 host_port_pair
, alternative_service
, 1.0);
328 void ExpectBrokenAlternateProtocolMapping() {
329 const HostPortPair origin
= HostPortPair::FromURL(request_
.url
);
330 const AlternativeService alternative_service
=
331 session_
->http_server_properties()->GetAlternativeService(origin
);
332 EXPECT_NE(UNINITIALIZED_ALTERNATE_PROTOCOL
, alternative_service
.protocol
);
333 EXPECT_TRUE(session_
->http_server_properties()->IsAlternativeServiceBroken(
334 alternative_service
));
337 void ExpectQuicAlternateProtocolMapping() {
338 const AlternativeService alternative_service
=
339 session_
->http_server_properties()->GetAlternativeService(
340 HostPortPair::FromURL(request_
.url
));
341 EXPECT_EQ(QUIC
, alternative_service
.protocol
);
344 void AddHangingNonAlternateProtocolSocketData() {
345 MockConnect
hanging_connect(SYNCHRONOUS
, ERR_IO_PENDING
);
346 hanging_data_
.set_connect_data(hanging_connect
);
347 socket_factory_
.AddSocketDataProvider(&hanging_data_
);
350 MockClock
* clock_
; // Owned by QuicStreamFactory after CreateSession.
351 QuicTestPacketMaker maker_
;
352 scoped_refptr
<HttpNetworkSession
> session_
;
353 MockClientSocketFactory socket_factory_
;
354 MockCryptoClientStreamFactory crypto_client_stream_factory_
;
355 MockHostResolver host_resolver_
;
356 MockCertVerifier cert_verifier_
;
357 TransportSecurityState transport_security_state_
;
358 scoped_refptr
<SSLConfigServiceDefaults
> ssl_config_service_
;
359 scoped_ptr
<ProxyService
> proxy_service_
;
360 scoped_ptr
<HttpAuthHandlerFactory
> auth_handler_factory_
;
361 MockRandom random_generator_
;
362 HttpServerPropertiesImpl http_server_properties
;
363 HttpNetworkSession::Params params_
;
364 HttpRequestInfo request_
;
365 BoundTestNetLog net_log_
;
366 StaticSocketDataProvider hanging_data_
;
369 void SendRequestAndExpectQuicResponseMaybeFromProxy(
370 const std::string
& expected
,
373 scoped_ptr
<HttpNetworkTransaction
> trans(
374 new HttpNetworkTransaction(DEFAULT_PRIORITY
, session_
.get()));
375 ProxyHeadersHandler proxy_headers_handler
;
376 trans
->SetBeforeProxyHeadersSentCallback(
377 base::Bind(&ProxyHeadersHandler::OnBeforeProxyHeadersSent
,
378 base::Unretained(&proxy_headers_handler
)));
379 RunTransaction(trans
.get());
380 CheckWasQuicResponse(trans
);
381 CheckResponsePort(trans
, port
);
382 CheckResponseData(trans
.get(), expected
);
383 EXPECT_EQ(used_proxy
, proxy_headers_handler
.was_called());
387 INSTANTIATE_TEST_CASE_P(Version
, QuicNetworkTransactionTest
,
388 ::testing::ValuesIn(QuicSupportedVersions()));
390 TEST_P(QuicNetworkTransactionTest
, ForceQuic
) {
391 params_
.origin_to_force_quic_on
=
392 HostPortPair::FromString("www.google.com:80");
394 MockQuicData mock_quic_data
;
395 mock_quic_data
.AddWrite(
396 ConstructRequestHeadersPacket(1, kClientDataStreamId1
, true, true,
397 GetRequestHeaders("GET", "http", "/")));
398 mock_quic_data
.AddRead(
399 ConstructResponseHeadersPacket(1, kClientDataStreamId1
, false, false,
400 GetResponseHeaders("200 OK")));
401 mock_quic_data
.AddRead(
402 ConstructDataPacket(2, kClientDataStreamId1
, false, true, 0, "hello!"));
403 mock_quic_data
.AddWrite(ConstructAckPacket(2, 1));
404 mock_quic_data
.AddRead(SYNCHRONOUS
, 0); // EOF
406 mock_quic_data
.AddSocketDataToFactory(&socket_factory_
);
408 // The non-alternate protocol job needs to hang in order to guarantee that
409 // the alternate-protocol job will "win".
410 AddHangingNonAlternateProtocolSocketData();
414 SendRequestAndExpectQuicResponse("hello!");
416 // Check that the NetLog was filled reasonably.
417 TestNetLogEntry::List entries
;
418 net_log_
.GetEntries(&entries
);
419 EXPECT_LT(0u, entries
.size());
421 // Check that we logged a QUIC_SESSION_PACKET_RECEIVED.
422 int pos
= ExpectLogContainsSomewhere(
423 entries
, 0, NetLog::TYPE_QUIC_SESSION_PACKET_RECEIVED
,
427 // ... and also a TYPE_QUIC_SESSION_PACKET_HEADER_RECEIVED.
428 pos
= ExpectLogContainsSomewhere(
429 entries
, 0, NetLog::TYPE_QUIC_SESSION_PACKET_HEADER_RECEIVED
,
433 std::string packet_sequence_number
;
434 ASSERT_TRUE(entries
[pos
].GetStringValue(
435 "packet_sequence_number", &packet_sequence_number
));
436 EXPECT_EQ("1", packet_sequence_number
);
438 // ... and also a QUIC_SESSION_STREAM_FRAME_RECEIVED.
439 pos
= ExpectLogContainsSomewhere(
440 entries
, 0, NetLog::TYPE_QUIC_SESSION_STREAM_FRAME_RECEIVED
,
445 ASSERT_TRUE(entries
[pos
].GetIntegerValue("stream_id", &log_stream_id
));
446 EXPECT_EQ(3, log_stream_id
);
449 TEST_P(QuicNetworkTransactionTest
, QuicProxy
) {
450 params_
.enable_quic_for_proxies
= true;
451 proxy_service_
.reset(
452 ProxyService::CreateFixedFromPacResult("QUIC myproxy:70"));
454 MockQuicData mock_quic_data
;
455 mock_quic_data
.AddWrite(
456 ConstructRequestHeadersPacket(1, kClientDataStreamId1
, true, true,
457 GetRequestHeaders("GET", "http", "/")));
458 mock_quic_data
.AddRead(
459 ConstructResponseHeadersPacket(1, kClientDataStreamId1
, false, false,
460 GetResponseHeaders("200 OK")));
461 mock_quic_data
.AddRead(
462 ConstructDataPacket(2, kClientDataStreamId1
, false, true, 0, "hello!"));
463 mock_quic_data
.AddWrite(ConstructAckPacket(2, 1));
464 mock_quic_data
.AddRead(SYNCHRONOUS
, 0); // EOF
466 mock_quic_data
.AddSocketDataToFactory(&socket_factory_
);
468 // There is no need to set up an alternate protocol job, because
469 // no attempt will be made to speak to the proxy over TCP.
473 SendRequestAndExpectQuicResponseFromProxyOnPort("hello!", 70);
476 TEST_P(QuicNetworkTransactionTest
, ForceQuicWithErrorConnecting
) {
477 params_
.origin_to_force_quic_on
=
478 HostPortPair::FromString("www.google.com:80");
480 MockQuicData mock_quic_data
;
481 mock_quic_data
.AddRead(ASYNC
, ERR_SOCKET_NOT_CONNECTED
);
483 mock_quic_data
.AddSocketDataToFactory(&socket_factory_
);
487 scoped_ptr
<HttpNetworkTransaction
> trans(
488 new HttpNetworkTransaction(DEFAULT_PRIORITY
, session_
.get()));
489 TestCompletionCallback callback
;
490 int rv
= trans
->Start(&request_
, callback
.callback(), net_log_
.bound());
491 EXPECT_EQ(ERR_IO_PENDING
, rv
);
492 EXPECT_EQ(ERR_CONNECTION_CLOSED
, callback
.WaitForResult());
495 TEST_P(QuicNetworkTransactionTest
, DoNotForceQuicForHttps
) {
496 // Attempt to "force" quic on 443, which will not be honored.
497 params_
.origin_to_force_quic_on
=
498 HostPortPair::FromString("www.google.com:443");
500 MockRead http_reads
[] = {
501 MockRead("HTTP/1.1 200 OK\r\n\r\n"),
502 MockRead("hello world"),
503 MockRead(SYNCHRONOUS
, ERR_TEST_PEER_CLOSE_AFTER_NEXT_MOCK_READ
),
507 StaticSocketDataProvider
data(http_reads
, arraysize(http_reads
), nullptr, 0);
508 socket_factory_
.AddSocketDataProvider(&data
);
509 SSLSocketDataProvider
ssl(ASYNC
, OK
);
510 socket_factory_
.AddSSLSocketDataProvider(&ssl
);
514 SendRequestAndExpectHttpResponse("hello world");
517 TEST_P(QuicNetworkTransactionTest
, UseAlternateProtocolForQuic
) {
518 MockRead http_reads
[] = {
519 MockRead("HTTP/1.1 200 OK\r\n"),
520 MockRead(kQuicAlternateProtocolHttpHeader
),
521 MockRead("hello world"),
522 MockRead(SYNCHRONOUS
, ERR_TEST_PEER_CLOSE_AFTER_NEXT_MOCK_READ
),
526 StaticSocketDataProvider
http_data(http_reads
, arraysize(http_reads
),
528 socket_factory_
.AddSocketDataProvider(&http_data
);
530 MockQuicData mock_quic_data
;
531 mock_quic_data
.AddWrite(
532 ConstructRequestHeadersPacket(1, kClientDataStreamId1
, true, true,
533 GetRequestHeaders("GET", "http", "/")));
534 mock_quic_data
.AddRead(
535 ConstructResponseHeadersPacket(1, kClientDataStreamId1
, false, false,
536 GetResponseHeaders("200 OK")));
537 mock_quic_data
.AddRead(
538 ConstructDataPacket(2, kClientDataStreamId1
, false, true, 0, "hello!"));
539 mock_quic_data
.AddWrite(ConstructAckPacket(2, 1));
540 mock_quic_data
.AddRead(SYNCHRONOUS
, 0); // EOF
542 mock_quic_data
.AddSocketDataToFactory(&socket_factory_
);
544 // The non-alternate protocol job needs to hang in order to guarantee that
545 // the alternate-protocol job will "win".
546 AddHangingNonAlternateProtocolSocketData();
548 CreateSessionWithNextProtos();
550 SendRequestAndExpectHttpResponse("hello world");
551 SendRequestAndExpectQuicResponse("hello!");
554 TEST_P(QuicNetworkTransactionTest
, AlternateProtocolDifferentPort
) {
555 MockRead http_reads
[] = {
556 MockRead("HTTP/1.1 200 OK\r\n"),
557 MockRead(kQuicAlternateProtocolDifferentPortHttpHeader
),
558 MockRead("hello world"),
559 MockRead(SYNCHRONOUS
, ERR_TEST_PEER_CLOSE_AFTER_NEXT_MOCK_READ
),
560 MockRead(ASYNC
, OK
)};
562 StaticSocketDataProvider
http_data(http_reads
, arraysize(http_reads
), nullptr,
564 socket_factory_
.AddSocketDataProvider(&http_data
);
566 MockQuicData mock_quic_data
;
567 mock_quic_data
.AddWrite(
568 ConstructRequestHeadersPacket(1, kClientDataStreamId1
, true, true,
569 GetRequestHeaders("GET", "http", "/")));
570 mock_quic_data
.AddRead(ConstructResponseHeadersPacket(
571 1, kClientDataStreamId1
, false, false, GetResponseHeaders("200 OK")));
572 mock_quic_data
.AddRead(
573 ConstructDataPacket(2, kClientDataStreamId1
, false, true, 0, "hello!"));
574 mock_quic_data
.AddWrite(ConstructAckPacket(2, 1));
575 mock_quic_data
.AddRead(SYNCHRONOUS
, 0); // EOF
577 mock_quic_data
.AddSocketDataToFactory(&socket_factory_
);
579 // The non-alternate protocol job needs to hang in order to guarantee that
580 // the alternate-protocol job will "win".
581 AddHangingNonAlternateProtocolSocketData();
583 CreateSessionWithNextProtos();
585 SendRequestAndExpectHttpResponse("hello world");
586 SendRequestAndExpectQuicResponseOnPort("hello!", 137);
589 TEST_P(QuicNetworkTransactionTest
, ConfirmAlternativeService
) {
590 MockRead http_reads
[] = {
591 MockRead("HTTP/1.1 200 OK\r\n"),
592 MockRead(kQuicAlternateProtocolHttpHeader
),
593 MockRead("hello world"),
594 MockRead(SYNCHRONOUS
, ERR_TEST_PEER_CLOSE_AFTER_NEXT_MOCK_READ
),
595 MockRead(ASYNC
, OK
)};
597 StaticSocketDataProvider
http_data(http_reads
, arraysize(http_reads
), nullptr,
599 socket_factory_
.AddSocketDataProvider(&http_data
);
601 MockQuicData mock_quic_data
;
602 mock_quic_data
.AddWrite(
603 ConstructRequestHeadersPacket(1, kClientDataStreamId1
, true, true,
604 GetRequestHeaders("GET", "http", "/")));
605 mock_quic_data
.AddRead(ConstructResponseHeadersPacket(
606 1, kClientDataStreamId1
, false, false, GetResponseHeaders("200 OK")));
607 mock_quic_data
.AddRead(
608 ConstructDataPacket(2, kClientDataStreamId1
, false, true, 0, "hello!"));
609 mock_quic_data
.AddWrite(ConstructAckPacket(2, 1));
610 mock_quic_data
.AddRead(SYNCHRONOUS
, 0); // EOF
612 mock_quic_data
.AddSocketDataToFactory(&socket_factory_
);
614 // The non-alternate protocol job needs to hang in order to guarantee that
615 // the alternate-protocol job will "win".
616 AddHangingNonAlternateProtocolSocketData();
618 CreateSessionWithNextProtos();
620 AlternativeService
alternative_service(QUIC
,
621 HostPortPair::FromURL(request_
.url
));
622 session_
->http_server_properties()->MarkAlternativeServiceRecentlyBroken(
623 alternative_service
);
625 session_
->http_server_properties()->WasAlternativeServiceRecentlyBroken(
626 alternative_service
));
628 SendRequestAndExpectHttpResponse("hello world");
629 SendRequestAndExpectQuicResponse("hello!");
632 session_
->http_server_properties()->WasAlternativeServiceRecentlyBroken(
633 alternative_service
));
636 TEST_P(QuicNetworkTransactionTest
, UseAlternateProtocolProbabilityForQuic
) {
637 MockRead http_reads
[] = {
638 MockRead("HTTP/1.1 200 OK\r\n"),
639 MockRead(kQuicAlternateProtocol50pctHttpHeader
),
640 MockRead("hello world"),
641 MockRead(SYNCHRONOUS
, ERR_TEST_PEER_CLOSE_AFTER_NEXT_MOCK_READ
),
645 StaticSocketDataProvider
http_data(http_reads
, arraysize(http_reads
),
647 socket_factory_
.AddSocketDataProvider(&http_data
);
649 MockQuicData mock_quic_data
;
650 mock_quic_data
.AddWrite(
651 ConstructRequestHeadersPacket(1, kClientDataStreamId1
, true, true,
652 GetRequestHeaders("GET", "http", "/")));
653 mock_quic_data
.AddRead(
654 ConstructResponseHeadersPacket(1, kClientDataStreamId1
, false, false,
655 GetResponseHeaders("200 OK")));
656 mock_quic_data
.AddRead(
657 ConstructDataPacket(2, kClientDataStreamId1
, false, true, 0, "hello!"));
658 mock_quic_data
.AddWrite(ConstructAckPacket(2, 1));
659 mock_quic_data
.AddRead(SYNCHRONOUS
, 0); // EOF
661 mock_quic_data
.AddSocketDataToFactory(&socket_factory_
);
663 // The non-alternate protocol job needs to hang in order to guarantee that
664 // the alternate-protocol job will "win".
665 AddHangingNonAlternateProtocolSocketData();
667 params_
.alternative_service_probability_threshold
= .25;
668 CreateSessionWithNextProtos();
670 SendRequestAndExpectHttpResponse("hello world");
671 SendRequestAndExpectQuicResponse("hello!");
674 TEST_P(QuicNetworkTransactionTest
, DontUseAlternateProtocolProbabilityForQuic
) {
675 MockRead http_reads
[] = {
676 MockRead("HTTP/1.1 200 OK\r\n"),
677 MockRead(kQuicAlternateProtocol50pctHttpHeader
),
678 MockRead("hello world"),
679 MockRead(SYNCHRONOUS
, ERR_TEST_PEER_CLOSE_AFTER_NEXT_MOCK_READ
),
683 StaticSocketDataProvider
http_data(http_reads
, arraysize(http_reads
),
685 socket_factory_
.AddSocketDataProvider(&http_data
);
686 socket_factory_
.AddSocketDataProvider(&http_data
);
688 params_
.alternative_service_probability_threshold
= .75;
689 CreateSessionWithNextProtos();
691 SendRequestAndExpectHttpResponse("hello world");
692 SendRequestAndExpectHttpResponse("hello world");
695 TEST_P(QuicNetworkTransactionTest
, DontUseAlternateProtocolForInsecureQuic
) {
696 MockRead http_reads
[] = {MockRead("HTTP/1.1 200 OK\r\n"),
697 MockRead("Content-length: 11\r\n"),
698 MockRead("Alternate-Protocol: 443:quic\r\n\r\n"),
699 MockRead("hello world"),
700 MockRead("HTTP/1.1 200 OK\r\n"),
701 MockRead("Content-length: 11\r\n"),
702 MockRead("Alternate-Protocol: 443:quic\r\n\r\n"),
703 MockRead("hello world"),
704 MockRead(ASYNC
, OK
)};
706 StaticSocketDataProvider
http_data(http_reads
, arraysize(http_reads
), nullptr,
708 socket_factory_
.AddSocketDataProvider(&http_data
);
709 socket_factory_
.AddSocketDataProvider(&http_data
);
711 params_
.disable_insecure_quic
= true;
712 CreateSessionWithNextProtos();
714 SendRequestAndExpectHttpResponse("hello world");
715 SendRequestAndExpectHttpResponse("hello world");
718 TEST_P(QuicNetworkTransactionTest
,
719 DontUseAlternateProtocolWithBadProbabilityForQuic
) {
720 MockRead http_reads
[] = {
721 MockRead("HTTP/1.1 200 OK\r\n"),
722 MockRead("Alternate-Protocol: 443:quic,p=2\r\n\r\n"),
723 MockRead("hello world"),
724 MockRead(SYNCHRONOUS
, ERR_TEST_PEER_CLOSE_AFTER_NEXT_MOCK_READ
),
728 StaticSocketDataProvider
http_data(http_reads
, arraysize(http_reads
),
730 socket_factory_
.AddSocketDataProvider(&http_data
);
731 socket_factory_
.AddSocketDataProvider(&http_data
);
733 params_
.alternative_service_probability_threshold
= .75;
734 CreateSessionWithNextProtos();
736 SendRequestAndExpectHttpResponse("hello world");
737 SendRequestAndExpectHttpResponse("hello world");
740 TEST_P(QuicNetworkTransactionTest
, UseAlternateProtocolForQuicForHttps
) {
741 params_
.origin_to_force_quic_on
=
742 HostPortPair::FromString("www.google.com:443");
744 MockRead http_reads
[] = {
745 MockRead("HTTP/1.1 200 OK\r\n"),
746 MockRead(kQuicAlternateProtocolHttpsHeader
),
747 MockRead("hello world"),
748 MockRead(SYNCHRONOUS
, ERR_TEST_PEER_CLOSE_AFTER_NEXT_MOCK_READ
),
752 StaticSocketDataProvider
http_data(http_reads
, arraysize(http_reads
),
754 socket_factory_
.AddSocketDataProvider(&http_data
);
756 MockQuicData mock_quic_data
;
757 mock_quic_data
.AddWrite(
758 ConstructRequestHeadersPacket(1, kClientDataStreamId1
, true, true,
759 GetRequestHeaders("GET", "http", "/")));
760 mock_quic_data
.AddRead(
761 ConstructResponseHeadersPacket(1, kClientDataStreamId1
, false, false,
762 GetResponseHeaders("200 OK")));
763 mock_quic_data
.AddRead(
764 ConstructDataPacket(2, kClientDataStreamId1
, false, true, 0, "hello!"));
765 mock_quic_data
.AddWrite(ConstructAckPacket(2, 1));
766 mock_quic_data
.AddRead(SYNCHRONOUS
, 0); // EOF
768 mock_quic_data
.AddSocketDataToFactory(&socket_factory_
);
770 // The non-alternate protocol job needs to hang in order to guarantee that
771 // the alternate-protocol job will "win".
772 AddHangingNonAlternateProtocolSocketData();
774 CreateSessionWithNextProtos();
776 // TODO(rtenneti): Test QUIC over HTTPS, GetSSLInfo().
777 SendRequestAndExpectHttpResponse("hello world");
780 TEST_P(QuicNetworkTransactionTest
, HungAlternateProtocol
) {
781 crypto_client_stream_factory_
.set_handshake_mode(
782 MockCryptoClientStream::COLD_START
);
784 MockWrite http_writes
[] = {
785 MockWrite(SYNCHRONOUS
, 0, "GET / HTTP/1.1\r\n"),
786 MockWrite(SYNCHRONOUS
, 1, "Host: www.google.com\r\n"),
787 MockWrite(SYNCHRONOUS
, 2, "Connection: keep-alive\r\n\r\n")
790 MockRead http_reads
[] = {
791 MockRead(SYNCHRONOUS
, 3, "HTTP/1.1 200 OK\r\n"),
792 MockRead(SYNCHRONOUS
, 4, kQuicAlternateProtocolHttpHeader
),
793 MockRead(SYNCHRONOUS
, 5, "hello world"),
794 MockRead(SYNCHRONOUS
, OK
, 6)
797 DeterministicMockClientSocketFactory socket_factory
;
799 DeterministicSocketData
http_data(http_reads
, arraysize(http_reads
),
800 http_writes
, arraysize(http_writes
));
801 socket_factory
.AddSocketDataProvider(&http_data
);
803 // The QUIC transaction will not be allowed to complete.
804 MockWrite quic_writes
[] = {
805 MockWrite(ASYNC
, ERR_IO_PENDING
, 0)
807 MockRead quic_reads
[] = {
808 MockRead(ASYNC
, ERR_IO_PENDING
, 1),
810 DeterministicSocketData
quic_data(quic_reads
, arraysize(quic_reads
),
811 quic_writes
, arraysize(quic_writes
));
812 socket_factory
.AddSocketDataProvider(&quic_data
);
814 // The HTTP transaction will complete.
815 DeterministicSocketData
http_data2(http_reads
, arraysize(http_reads
),
816 http_writes
, arraysize(http_writes
));
817 socket_factory
.AddSocketDataProvider(&http_data2
);
819 CreateSessionWithFactory(&socket_factory
, true);
821 // Run the first request.
822 http_data
.StopAfter(arraysize(http_reads
) + arraysize(http_writes
));
823 SendRequestAndExpectHttpResponse("hello world");
824 ASSERT_TRUE(http_data
.at_read_eof());
825 ASSERT_TRUE(http_data
.at_write_eof());
827 // Now run the second request in which the QUIC socket hangs,
828 // and verify the the transaction continues over HTTP.
829 http_data2
.StopAfter(arraysize(http_reads
) + arraysize(http_writes
));
830 SendRequestAndExpectHttpResponse("hello world");
832 ASSERT_TRUE(http_data2
.at_read_eof());
833 ASSERT_TRUE(http_data2
.at_write_eof());
834 ASSERT_TRUE(!quic_data
.at_read_eof());
835 ASSERT_TRUE(!quic_data
.at_write_eof());
838 TEST_P(QuicNetworkTransactionTest
, ZeroRTTWithHttpRace
) {
839 MockQuicData mock_quic_data
;
840 mock_quic_data
.AddWrite(
841 ConstructRequestHeadersPacket(1, kClientDataStreamId1
, true, true,
842 GetRequestHeaders("GET", "http", "/")));
843 mock_quic_data
.AddRead(
844 ConstructResponseHeadersPacket(1, kClientDataStreamId1
, false, false,
845 GetResponseHeaders("200 OK")));
846 mock_quic_data
.AddRead(
847 ConstructDataPacket(2, kClientDataStreamId1
, false, true, 0, "hello!"));
848 mock_quic_data
.AddWrite(ConstructAckPacket(2, 1));
849 mock_quic_data
.AddRead(SYNCHRONOUS
, 0); // EOF
851 mock_quic_data
.AddSocketDataToFactory(&socket_factory_
);
853 // The non-alternate protocol job needs to hang in order to guarantee that
854 // the alternate-protocol job will "win".
855 AddHangingNonAlternateProtocolSocketData();
857 CreateSessionWithNextProtos();
858 AddQuicAlternateProtocolMapping(MockCryptoClientStream::ZERO_RTT
);
859 SendRequestAndExpectQuicResponse("hello!");
862 TEST_P(QuicNetworkTransactionTest
, ZeroRTTWithNoHttpRace
) {
863 MockQuicData mock_quic_data
;
864 mock_quic_data
.AddWrite(
865 ConstructRequestHeadersPacket(1, kClientDataStreamId1
, true, true,
866 GetRequestHeaders("GET", "http", "/")));
867 mock_quic_data
.AddRead(
868 ConstructResponseHeadersPacket(1, kClientDataStreamId1
, false, false,
869 GetResponseHeaders("200 OK")));
870 mock_quic_data
.AddRead(
871 ConstructDataPacket(2, kClientDataStreamId1
, false, true, 0, "hello!"));
872 mock_quic_data
.AddWrite(ConstructAckPacket(2, 1));
873 mock_quic_data
.AddRead(SYNCHRONOUS
, 0); // EOF
874 mock_quic_data
.AddSocketDataToFactory(&socket_factory_
);
876 // In order for a new QUIC session to be established via alternate-protocol
877 // without racing an HTTP connection, we need the host resolution to happen
879 host_resolver_
.set_synchronous_mode(true);
880 host_resolver_
.rules()->AddIPLiteralRule("www.google.com", "192.168.0.1", "");
881 HostResolver::RequestInfo
info(HostPortPair("www.google.com", 80));
883 host_resolver_
.Resolve(info
,
886 CompletionCallback(),
890 CreateSessionWithNextProtos();
891 AddQuicAlternateProtocolMapping(MockCryptoClientStream::ZERO_RTT
);
892 SendRequestAndExpectQuicResponse("hello!");
895 TEST_P(QuicNetworkTransactionTest
, ZeroRTTWithProxy
) {
896 proxy_service_
.reset(
897 ProxyService::CreateFixedFromPacResult("PROXY myproxy:70"));
899 // Since we are using a proxy, the QUIC job will not succeed.
900 MockWrite http_writes
[] = {
901 MockWrite(SYNCHRONOUS
, 0, "GET http://www.google.com/ HTTP/1.1\r\n"),
902 MockWrite(SYNCHRONOUS
, 1, "Host: www.google.com\r\n"),
903 MockWrite(SYNCHRONOUS
, 2, "Proxy-Connection: keep-alive\r\n\r\n")
906 MockRead http_reads
[] = {
907 MockRead(SYNCHRONOUS
, 3, "HTTP/1.1 200 OK\r\n"),
908 MockRead(SYNCHRONOUS
, 4, kQuicAlternateProtocolHttpHeader
),
909 MockRead(SYNCHRONOUS
, 5, "hello world"),
910 MockRead(SYNCHRONOUS
, OK
, 6)
913 StaticSocketDataProvider
http_data(http_reads
, arraysize(http_reads
),
914 http_writes
, arraysize(http_writes
));
915 socket_factory_
.AddSocketDataProvider(&http_data
);
917 // In order for a new QUIC session to be established via alternate-protocol
918 // without racing an HTTP connection, we need the host resolution to happen
920 host_resolver_
.set_synchronous_mode(true);
921 host_resolver_
.rules()->AddIPLiteralRule("www.google.com", "192.168.0.1", "");
922 HostResolver::RequestInfo
info(HostPortPair("www.google.com", 80));
924 host_resolver_
.Resolve(info
,
927 CompletionCallback(),
931 CreateSessionWithNextProtos();
932 AddQuicAlternateProtocolMapping(MockCryptoClientStream::ZERO_RTT
);
933 SendRequestAndExpectHttpResponse("hello world");
936 TEST_P(QuicNetworkTransactionTest
, ZeroRTTWithConfirmationRequired
) {
937 MockQuicData mock_quic_data
;
938 mock_quic_data
.AddWrite(
939 ConstructRequestHeadersPacket(1, kClientDataStreamId1
, true, true,
940 GetRequestHeaders("GET", "http", "/")));
941 mock_quic_data
.AddRead(
942 ConstructResponseHeadersPacket(1, kClientDataStreamId1
, false, false,
943 GetResponseHeaders("200 OK")));
944 mock_quic_data
.AddRead(
945 ConstructDataPacket(2, kClientDataStreamId1
, false, true, 0, "hello!"));
946 mock_quic_data
.AddWrite(ConstructAckPacket(2, 1));
947 mock_quic_data
.AddRead(SYNCHRONOUS
, 0); // EOF
948 mock_quic_data
.AddSocketDataToFactory(&socket_factory_
);
950 // The non-alternate protocol job needs to hang in order to guarantee that
951 // the alternate-protocol job will "win".
952 AddHangingNonAlternateProtocolSocketData();
954 // In order for a new QUIC session to be established via alternate-protocol
955 // without racing an HTTP connection, we need the host resolution to happen
956 // synchronously. Of course, even though QUIC *could* perform a 0-RTT
957 // connection to the the server, in this test we require confirmation
958 // before encrypting so the HTTP job will still start.
959 host_resolver_
.set_synchronous_mode(true);
960 host_resolver_
.rules()->AddIPLiteralRule("www.google.com", "192.168.0.1", "");
961 HostResolver::RequestInfo
info(HostPortPair("www.google.com", 80));
963 host_resolver_
.Resolve(info
, DEFAULT_PRIORITY
, &address
,
964 CompletionCallback(), nullptr, net_log_
.bound());
966 CreateSessionWithNextProtos();
967 session_
->quic_stream_factory()->set_require_confirmation(true);
968 AddQuicAlternateProtocolMapping(MockCryptoClientStream::ZERO_RTT
);
970 scoped_ptr
<HttpNetworkTransaction
> trans(
971 new HttpNetworkTransaction(DEFAULT_PRIORITY
, session_
.get()));
972 TestCompletionCallback callback
;
973 int rv
= trans
->Start(&request_
, callback
.callback(), net_log_
.bound());
974 EXPECT_EQ(ERR_IO_PENDING
, rv
);
976 crypto_client_stream_factory_
.last_stream()->SendOnCryptoHandshakeEvent(
977 QuicSession::HANDSHAKE_CONFIRMED
);
978 EXPECT_EQ(OK
, callback
.WaitForResult());
981 TEST_P(QuicNetworkTransactionTest
, BrokenAlternateProtocol
) {
982 // Alternate-protocol job
983 scoped_ptr
<QuicEncryptedPacket
> close(ConstructConnectionClosePacket(1));
984 MockRead quic_reads
[] = {
985 MockRead(ASYNC
, close
->data(), close
->length()),
986 MockRead(ASYNC
, OK
), // EOF
988 StaticSocketDataProvider
quic_data(quic_reads
, arraysize(quic_reads
),
990 socket_factory_
.AddSocketDataProvider(&quic_data
);
992 // Main job which will succeed even though the alternate job fails.
993 MockRead http_reads
[] = {
994 MockRead("HTTP/1.1 200 OK\r\n\r\n"),
995 MockRead("hello from http"),
996 MockRead(SYNCHRONOUS
, ERR_TEST_PEER_CLOSE_AFTER_NEXT_MOCK_READ
),
1000 StaticSocketDataProvider
http_data(http_reads
, arraysize(http_reads
),
1002 socket_factory_
.AddSocketDataProvider(&http_data
);
1004 CreateSessionWithNextProtos();
1005 AddQuicAlternateProtocolMapping(MockCryptoClientStream::COLD_START
);
1006 SendRequestAndExpectHttpResponse("hello from http");
1007 ExpectBrokenAlternateProtocolMapping();
1010 TEST_P(QuicNetworkTransactionTest
, BrokenAlternateProtocolReadError
) {
1011 // Alternate-protocol job
1012 MockRead quic_reads
[] = {
1013 MockRead(ASYNC
, ERR_SOCKET_NOT_CONNECTED
),
1015 StaticSocketDataProvider
quic_data(quic_reads
, arraysize(quic_reads
),
1017 socket_factory_
.AddSocketDataProvider(&quic_data
);
1019 // Main job which will succeed even though the alternate job fails.
1020 MockRead http_reads
[] = {
1021 MockRead("HTTP/1.1 200 OK\r\n\r\n"),
1022 MockRead("hello from http"),
1023 MockRead(SYNCHRONOUS
, ERR_TEST_PEER_CLOSE_AFTER_NEXT_MOCK_READ
),
1027 StaticSocketDataProvider
http_data(http_reads
, arraysize(http_reads
),
1029 socket_factory_
.AddSocketDataProvider(&http_data
);
1031 CreateSessionWithNextProtos();
1033 AddQuicAlternateProtocolMapping(MockCryptoClientStream::COLD_START
);
1034 SendRequestAndExpectHttpResponse("hello from http");
1035 ExpectBrokenAlternateProtocolMapping();
1038 TEST_P(QuicNetworkTransactionTest
, NoBrokenAlternateProtocolIfTcpFails
) {
1039 // Alternate-protocol job will fail when the session attempts to read.
1040 MockRead quic_reads
[] = {
1041 MockRead(ASYNC
, ERR_SOCKET_NOT_CONNECTED
),
1043 StaticSocketDataProvider
quic_data(quic_reads
, arraysize(quic_reads
),
1045 socket_factory_
.AddSocketDataProvider(&quic_data
);
1047 // Main job will also fail.
1048 MockRead http_reads
[] = {
1049 MockRead(ASYNC
, ERR_SOCKET_NOT_CONNECTED
),
1052 StaticSocketDataProvider
http_data(http_reads
, arraysize(http_reads
),
1054 http_data
.set_connect_data(MockConnect(ASYNC
, ERR_SOCKET_NOT_CONNECTED
));
1055 socket_factory_
.AddSocketDataProvider(&http_data
);
1057 CreateSessionWithNextProtos();
1059 AddQuicAlternateProtocolMapping(MockCryptoClientStream::COLD_START
);
1060 scoped_ptr
<HttpNetworkTransaction
> trans(
1061 new HttpNetworkTransaction(DEFAULT_PRIORITY
, session_
.get()));
1062 TestCompletionCallback callback
;
1063 int rv
= trans
->Start(&request_
, callback
.callback(), net_log_
.bound());
1064 EXPECT_EQ(ERR_IO_PENDING
, rv
);
1065 EXPECT_EQ(ERR_SOCKET_NOT_CONNECTED
, callback
.WaitForResult());
1066 ExpectQuicAlternateProtocolMapping();
1069 TEST_P(QuicNetworkTransactionTest
, FailedZeroRttBrokenAlternateProtocol
) {
1070 // Alternate-protocol job
1071 MockRead quic_reads
[] = {
1072 MockRead(ASYNC
, ERR_SOCKET_NOT_CONNECTED
),
1074 StaticSocketDataProvider
quic_data(quic_reads
, arraysize(quic_reads
),
1076 socket_factory_
.AddSocketDataProvider(&quic_data
);
1078 AddHangingNonAlternateProtocolSocketData();
1080 // Second Alternate-protocol job which will race with the TCP job.
1081 StaticSocketDataProvider
quic_data2(quic_reads
, arraysize(quic_reads
),
1083 socket_factory_
.AddSocketDataProvider(&quic_data2
);
1085 // Final job that will proceed when the QUIC job fails.
1086 MockRead http_reads
[] = {
1087 MockRead("HTTP/1.1 200 OK\r\n\r\n"),
1088 MockRead("hello from http"),
1089 MockRead(SYNCHRONOUS
, ERR_TEST_PEER_CLOSE_AFTER_NEXT_MOCK_READ
),
1093 StaticSocketDataProvider
http_data(http_reads
, arraysize(http_reads
),
1095 socket_factory_
.AddSocketDataProvider(&http_data
);
1097 CreateSessionWithNextProtos();
1099 AddQuicAlternateProtocolMapping(MockCryptoClientStream::ZERO_RTT
);
1101 SendRequestAndExpectHttpResponse("hello from http");
1103 ExpectBrokenAlternateProtocolMapping();
1105 EXPECT_TRUE(quic_data
.at_read_eof());
1106 EXPECT_TRUE(quic_data
.at_write_eof());
1109 TEST_P(QuicNetworkTransactionTest
, DISABLED_HangingZeroRttFallback
) {
1110 // Alternate-protocol job
1111 MockRead quic_reads
[] = {
1112 MockRead(ASYNC
, ERR_IO_PENDING
),
1114 StaticSocketDataProvider
quic_data(quic_reads
, arraysize(quic_reads
),
1116 socket_factory_
.AddSocketDataProvider(&quic_data
);
1118 // Main job that will proceed when the QUIC job fails.
1119 MockRead http_reads
[] = {
1120 MockRead("HTTP/1.1 200 OK\r\n\r\n"),
1121 MockRead("hello from http"),
1122 MockRead(SYNCHRONOUS
, ERR_TEST_PEER_CLOSE_AFTER_NEXT_MOCK_READ
),
1126 StaticSocketDataProvider
http_data(http_reads
, arraysize(http_reads
),
1128 socket_factory_
.AddSocketDataProvider(&http_data
);
1130 CreateSessionWithNextProtos();
1132 AddQuicAlternateProtocolMapping(MockCryptoClientStream::ZERO_RTT
);
1134 SendRequestAndExpectHttpResponse("hello from http");
1137 TEST_P(QuicNetworkTransactionTest
, BrokenAlternateProtocolOnConnectFailure
) {
1138 // Alternate-protocol job will fail before creating a QUIC session.
1139 StaticSocketDataProvider
quic_data(nullptr, 0, nullptr, 0);
1140 quic_data
.set_connect_data(MockConnect(SYNCHRONOUS
,
1141 ERR_INTERNET_DISCONNECTED
));
1142 socket_factory_
.AddSocketDataProvider(&quic_data
);
1144 // Main job which will succeed even though the alternate job fails.
1145 MockRead http_reads
[] = {
1146 MockRead("HTTP/1.1 200 OK\r\n\r\n"),
1147 MockRead("hello from http"),
1148 MockRead(SYNCHRONOUS
, ERR_TEST_PEER_CLOSE_AFTER_NEXT_MOCK_READ
),
1152 StaticSocketDataProvider
http_data(http_reads
, arraysize(http_reads
),
1154 socket_factory_
.AddSocketDataProvider(&http_data
);
1156 CreateSessionWithNextProtos();
1157 AddQuicAlternateProtocolMapping(MockCryptoClientStream::COLD_START
);
1158 SendRequestAndExpectHttpResponse("hello from http");
1160 ExpectBrokenAlternateProtocolMapping();
1163 TEST_P(QuicNetworkTransactionTest
, ConnectionCloseDuringConnect
) {
1164 MockQuicData mock_quic_data
;
1165 mock_quic_data
.AddSynchronousRead(ConstructConnectionClosePacket(1));
1166 mock_quic_data
.AddWrite(
1167 ConstructRequestHeadersPacket(1, kClientDataStreamId1
, true, true,
1168 GetRequestHeaders("GET", "http", "/")));
1169 mock_quic_data
.AddWrite(ConstructAckPacket(2, 1));
1170 mock_quic_data
.AddSocketDataToFactory(&socket_factory_
);
1172 // When the QUIC connection fails, we will try the request again over HTTP.
1173 MockRead http_reads
[] = {
1174 MockRead("HTTP/1.1 200 OK\r\n"),
1175 MockRead(kQuicAlternateProtocolHttpHeader
),
1176 MockRead("hello world"),
1177 MockRead(SYNCHRONOUS
, ERR_TEST_PEER_CLOSE_AFTER_NEXT_MOCK_READ
),
1181 StaticSocketDataProvider
http_data(http_reads
, arraysize(http_reads
),
1183 socket_factory_
.AddSocketDataProvider(&http_data
);
1185 // In order for a new QUIC session to be established via alternate-protocol
1186 // without racing an HTTP connection, we need the host resolution to happen
1188 host_resolver_
.set_synchronous_mode(true);
1189 host_resolver_
.rules()->AddIPLiteralRule("www.google.com", "192.168.0.1", "");
1190 HostResolver::RequestInfo
info(HostPortPair("www.google.com", 80));
1191 AddressList address
;
1192 host_resolver_
.Resolve(info
,
1195 CompletionCallback(),
1199 CreateSessionWithNextProtos();
1200 AddQuicAlternateProtocolMapping(MockCryptoClientStream::ZERO_RTT
);
1201 SendRequestAndExpectHttpResponse("hello world");
1204 // Test that a secure request over an insecure QUIC connection fails with
1205 // the appropriate error code. Note that this never happens in production,
1206 // because the handshake (which this test mocks) would fail in this scenario.
1207 TEST_P(QuicNetworkTransactionTest
, SecureResourceOverInsecureQuic
) {
1208 MockQuicData mock_quic_data
;
1209 mock_quic_data
.AddWrite(
1210 ConstructRequestHeadersPacket(1, kClientDataStreamId1
, true, true,
1211 GetRequestHeaders("GET", "https", "/")));
1212 mock_quic_data
.AddRead(ConstructResponseHeadersPacket(
1213 1, kClientDataStreamId1
, false, false, GetResponseHeaders("200 OK")));
1214 mock_quic_data
.AddRead(
1215 ConstructDataPacket(2, kClientDataStreamId1
, false, true, 0, "hello!"));
1216 mock_quic_data
.AddWrite(ConstructAckPacket(2, 1));
1217 mock_quic_data
.AddRead(SYNCHRONOUS
, 0);
1218 mock_quic_data
.AddSocketDataToFactory(&socket_factory_
);
1220 request_
.url
= GURL("https://www.google.com:443");
1221 AddHangingNonAlternateProtocolSocketData();
1222 CreateSessionWithNextProtos();
1223 AddQuicAlternateProtocolMapping(MockCryptoClientStream::CONFIRM_HANDSHAKE
);
1224 scoped_ptr
<HttpNetworkTransaction
> trans(
1225 new HttpNetworkTransaction(DEFAULT_PRIORITY
, session_
.get()));
1226 TestCompletionCallback callback
;
1227 int rv
= trans
->Start(&request_
, callback
.callback(), net_log_
.bound());
1228 EXPECT_EQ(ERR_REQUEST_FOR_SECURE_RESOURCE_OVER_INSECURE_QUIC
,
1229 callback
.GetResult(rv
));
1232 TEST_P(QuicNetworkTransactionTest
, SecureResourceOverSecureQuic
) {
1233 MockQuicData mock_quic_data
;
1234 mock_quic_data
.AddWrite(
1235 ConstructRequestHeadersPacket(1, kClientDataStreamId1
, true, true,
1236 GetRequestHeaders("GET", "https", "/")));
1237 mock_quic_data
.AddRead(ConstructResponseHeadersPacket(
1238 1, kClientDataStreamId1
, false, false, GetResponseHeaders("200 OK")));
1239 mock_quic_data
.AddRead(
1240 ConstructDataPacket(2, kClientDataStreamId1
, false, true, 0, "hello!"));
1241 mock_quic_data
.AddWrite(ConstructAckPacket(2, 1));
1242 mock_quic_data
.AddRead(SYNCHRONOUS
, 0);
1243 mock_quic_data
.AddSocketDataToFactory(&socket_factory_
);
1245 scoped_refptr
<X509Certificate
> cert(
1246 ImportCertFromFile(GetTestCertsDirectory(), "spdy_pooling.pem"));
1247 ASSERT_TRUE(cert
.get());
1248 ProofVerifyDetailsChromium verify_details
;
1249 verify_details
.cert_verify_result
.verified_cert
= cert
;
1250 crypto_client_stream_factory_
.set_proof_verify_details(&verify_details
);
1252 request_
.url
= GURL("https://www.google.com:443");
1253 AddHangingNonAlternateProtocolSocketData();
1254 CreateSessionWithNextProtos();
1255 AddQuicAlternateProtocolMapping(MockCryptoClientStream::CONFIRM_HANDSHAKE
);
1256 SendRequestAndExpectQuicResponse("hello!");