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";
66 const char kDefaultServerHostName
[] = "www.google.com";
70 // Helper class to encapsulate MockReads and MockWrites for QUIC.
71 // Simplify ownership issues and the interaction with the MockSocketFactory.
74 MockQuicData() : sequence_number_(0) {}
77 STLDeleteElements(&packets_
);
80 void AddSynchronousRead(scoped_ptr
<QuicEncryptedPacket
> packet
) {
81 reads_
.push_back(MockRead(SYNCHRONOUS
, packet
->data(), packet
->length(),
83 packets_
.push_back(packet
.release());
86 void AddRead(scoped_ptr
<QuicEncryptedPacket
> packet
) {
88 MockRead(ASYNC
, packet
->data(), packet
->length(), sequence_number_
++));
89 packets_
.push_back(packet
.release());
92 void AddRead(IoMode mode
, int rv
) {
93 reads_
.push_back(MockRead(mode
, rv
, sequence_number_
++));
96 void AddWrite(scoped_ptr
<QuicEncryptedPacket
> packet
) {
97 writes_
.push_back(MockWrite(SYNCHRONOUS
, packet
->data(), packet
->length(),
99 packets_
.push_back(packet
.release());
102 void AddSocketDataToFactory(MockClientSocketFactory
* factory
) {
103 MockRead
* reads
= reads_
.empty() ? nullptr : &reads_
[0];
104 MockWrite
* writes
= writes_
.empty() ? nullptr : &writes_
[0];
106 new SequencedSocketData(reads
, reads_
.size(), writes
, writes_
.size()));
107 factory
->AddSocketDataProvider(socket_data_
.get());
111 std::vector
<QuicEncryptedPacket
*> packets_
;
112 std::vector
<MockWrite
> writes_
;
113 std::vector
<MockRead
> reads_
;
114 size_t sequence_number_
;
115 scoped_ptr
<SocketDataProvider
> socket_data_
;
118 class ProxyHeadersHandler
{
120 ProxyHeadersHandler() : was_called_(false) {}
122 bool was_called() { return was_called_
; }
124 void OnBeforeProxyHeadersSent(const ProxyInfo
& proxy_info
,
125 HttpRequestHeaders
* request_headers
) {
133 class QuicNetworkTransactionTest
134 : public PlatformTest
,
135 public ::testing::WithParamInterface
<QuicVersion
> {
137 QuicNetworkTransactionTest()
138 : clock_(new MockClock
),
139 maker_(GetParam(), 0, clock_
, kDefaultServerHostName
),
140 ssl_config_service_(new SSLConfigServiceDefaults
),
141 proxy_service_(ProxyService::CreateDirect()),
142 auth_handler_factory_(
143 HttpAuthHandlerFactory::CreateDefault(&host_resolver_
)),
144 random_generator_(0),
145 hanging_data_(nullptr, 0, nullptr, 0) {
146 request_
.method
= "GET";
147 std::string
url("http://");
148 url
.append(kDefaultServerHostName
);
149 request_
.url
= GURL(url
);
150 request_
.load_flags
= 0;
151 clock_
->AdvanceTime(QuicTime::Delta::FromMilliseconds(20));
154 void SetUp() override
{
155 NetworkChangeNotifier::NotifyObserversOfIPAddressChangeForTests();
156 base::MessageLoop::current()->RunUntilIdle();
159 void TearDown() override
{
160 NetworkChangeNotifier::NotifyObserversOfIPAddressChangeForTests();
161 // Empty the current queue.
162 base::MessageLoop::current()->RunUntilIdle();
163 PlatformTest::TearDown();
164 NetworkChangeNotifier::NotifyObserversOfIPAddressChangeForTests();
165 base::MessageLoop::current()->RunUntilIdle();
168 scoped_ptr
<QuicEncryptedPacket
> ConstructConnectionClosePacket(
169 QuicPacketSequenceNumber num
) {
170 return maker_
.MakeConnectionClosePacket(num
);
173 scoped_ptr
<QuicEncryptedPacket
> ConstructAckPacket(
174 QuicPacketSequenceNumber largest_received
,
175 QuicPacketSequenceNumber least_unacked
) {
176 return maker_
.MakeAckPacket(2, largest_received
, least_unacked
, true);
179 SpdyHeaderBlock
GetRequestHeaders(const std::string
& method
,
180 const std::string
& scheme
,
181 const std::string
& path
) {
182 return maker_
.GetRequestHeaders(method
, scheme
, path
);
185 SpdyHeaderBlock
GetResponseHeaders(const std::string
& status
) {
186 return maker_
.GetResponseHeaders(status
);
189 scoped_ptr
<QuicEncryptedPacket
> ConstructDataPacket(
190 QuicPacketSequenceNumber sequence_number
,
191 QuicStreamId stream_id
,
192 bool should_include_version
,
194 QuicStreamOffset offset
,
195 base::StringPiece data
) {
196 return maker_
.MakeDataPacket(
197 sequence_number
, stream_id
, should_include_version
, fin
, offset
, data
);
200 scoped_ptr
<QuicEncryptedPacket
> ConstructRequestHeadersPacket(
201 QuicPacketSequenceNumber sequence_number
,
202 QuicStreamId stream_id
,
203 bool should_include_version
,
205 const SpdyHeaderBlock
& headers
) {
206 QuicPriority priority
=
207 ConvertRequestPriorityToQuicPriority(DEFAULT_PRIORITY
);
208 return maker_
.MakeRequestHeadersPacket(sequence_number
, stream_id
,
209 should_include_version
, fin
,
213 scoped_ptr
<QuicEncryptedPacket
> ConstructResponseHeadersPacket(
214 QuicPacketSequenceNumber sequence_number
,
215 QuicStreamId stream_id
,
216 bool should_include_version
,
218 const SpdyHeaderBlock
& headers
) {
219 return maker_
.MakeResponseHeadersPacket(
220 sequence_number
, stream_id
, should_include_version
, fin
, headers
);
223 void CreateSession() {
224 CreateSessionWithFactory(&socket_factory_
, false);
227 void CreateSessionWithNextProtos() {
228 CreateSessionWithFactory(&socket_factory_
, true);
231 // If |use_next_protos| is true, enables SPDY and QUIC.
232 void CreateSessionWithFactory(ClientSocketFactory
* socket_factory
,
233 bool use_next_protos
) {
234 params_
.enable_quic
= true;
235 params_
.quic_clock
= clock_
;
236 params_
.quic_random
= &random_generator_
;
237 params_
.client_socket_factory
= socket_factory
;
238 params_
.quic_crypto_client_stream_factory
= &crypto_client_stream_factory_
;
239 params_
.host_resolver
= &host_resolver_
;
240 params_
.cert_verifier
= &cert_verifier_
;
241 params_
.transport_security_state
= &transport_security_state_
;
242 params_
.proxy_service
= proxy_service_
.get();
243 params_
.ssl_config_service
= ssl_config_service_
.get();
244 params_
.http_auth_handler_factory
= auth_handler_factory_
.get();
245 params_
.http_server_properties
= http_server_properties_
.GetWeakPtr();
246 params_
.quic_supported_versions
= SupportedVersions(GetParam());
248 if (use_next_protos
) {
249 params_
.use_alternate_protocols
= true;
250 params_
.next_protos
= NextProtosWithSpdyAndQuic(true, true);
253 session_
= new HttpNetworkSession(params_
);
254 session_
->quic_stream_factory()->set_require_confirmation(false);
257 void CheckWasQuicResponse(const scoped_ptr
<HttpNetworkTransaction
>& trans
) {
258 const HttpResponseInfo
* response
= trans
->GetResponseInfo();
259 ASSERT_TRUE(response
!= nullptr);
260 ASSERT_TRUE(response
->headers
.get() != nullptr);
261 EXPECT_EQ("HTTP/1.1 200 OK", response
->headers
->GetStatusLine());
262 EXPECT_TRUE(response
->was_fetched_via_spdy
);
263 EXPECT_TRUE(response
->was_npn_negotiated
);
264 EXPECT_EQ(HttpResponseInfo::CONNECTION_INFO_QUIC1_SPDY3
,
265 response
->connection_info
);
268 void CheckResponsePort(const scoped_ptr
<HttpNetworkTransaction
>& trans
,
270 const HttpResponseInfo
* response
= trans
->GetResponseInfo();
271 ASSERT_TRUE(response
!= nullptr);
272 EXPECT_EQ(port
, response
->socket_address
.port());
275 void CheckWasHttpResponse(const scoped_ptr
<HttpNetworkTransaction
>& trans
) {
276 const HttpResponseInfo
* response
= trans
->GetResponseInfo();
277 ASSERT_TRUE(response
!= nullptr);
278 ASSERT_TRUE(response
->headers
.get() != nullptr);
279 EXPECT_EQ("HTTP/1.1 200 OK", response
->headers
->GetStatusLine());
280 EXPECT_FALSE(response
->was_fetched_via_spdy
);
281 EXPECT_FALSE(response
->was_npn_negotiated
);
282 EXPECT_EQ(HttpResponseInfo::CONNECTION_INFO_HTTP1
,
283 response
->connection_info
);
286 void CheckResponseData(const scoped_ptr
<HttpNetworkTransaction
>& trans
,
287 const std::string
& expected
) {
288 std::string response_data
;
289 ASSERT_EQ(OK
, ReadTransaction(trans
.get(), &response_data
));
290 EXPECT_EQ(expected
, response_data
);
293 void RunTransaction(const scoped_ptr
<HttpNetworkTransaction
>& trans
) {
294 TestCompletionCallback callback
;
295 int rv
= trans
->Start(&request_
, callback
.callback(), net_log_
.bound());
296 EXPECT_EQ(ERR_IO_PENDING
, rv
);
297 EXPECT_EQ(OK
, callback
.WaitForResult());
300 void SendRequestAndExpectHttpResponse(const std::string
& expected
) {
301 scoped_ptr
<HttpNetworkTransaction
> trans(
302 new HttpNetworkTransaction(DEFAULT_PRIORITY
, session_
.get()));
303 RunTransaction(trans
);
304 CheckWasHttpResponse(trans
);
305 CheckResponseData(trans
, expected
);
308 void SendRequestAndExpectQuicResponse(const std::string
& expected
) {
309 SendRequestAndExpectQuicResponseMaybeFromProxy(expected
, false, 80);
312 void SendRequestAndExpectQuicResponseOnPort(const std::string
& expected
,
314 SendRequestAndExpectQuicResponseMaybeFromProxy(expected
, false, port
);
317 void SendRequestAndExpectQuicResponseFromProxyOnPort(
318 const std::string
& expected
,
320 SendRequestAndExpectQuicResponseMaybeFromProxy(expected
, true, port
);
323 void AddQuicAlternateProtocolMapping(
324 MockCryptoClientStream::HandshakeMode handshake_mode
) {
325 crypto_client_stream_factory_
.set_handshake_mode(handshake_mode
);
326 HostPortPair host_port_pair
= HostPortPair::FromURL(request_
.url
);
327 AlternativeService
alternative_service(QUIC
, host_port_pair
.host(), 80);
328 http_server_properties_
.SetAlternativeService(host_port_pair
,
329 alternative_service
, 1.0);
332 void ExpectBrokenAlternateProtocolMapping() {
333 const HostPortPair origin
= HostPortPair::FromURL(request_
.url
);
334 const AlternativeService alternative_service
=
335 http_server_properties_
.GetAlternativeService(origin
);
336 EXPECT_NE(UNINITIALIZED_ALTERNATE_PROTOCOL
, alternative_service
.protocol
);
337 EXPECT_TRUE(http_server_properties_
.IsAlternativeServiceBroken(
338 alternative_service
));
341 void ExpectQuicAlternateProtocolMapping() {
342 const AlternativeService alternative_service
=
343 http_server_properties_
.GetAlternativeService(
344 HostPortPair::FromURL(request_
.url
));
345 EXPECT_EQ(QUIC
, alternative_service
.protocol
);
348 void AddHangingNonAlternateProtocolSocketData() {
349 MockConnect
hanging_connect(SYNCHRONOUS
, ERR_IO_PENDING
);
350 hanging_data_
.set_connect_data(hanging_connect
);
351 socket_factory_
.AddSocketDataProvider(&hanging_data_
);
354 MockClock
* clock_
; // Owned by QuicStreamFactory after CreateSession.
355 QuicTestPacketMaker maker_
;
356 scoped_refptr
<HttpNetworkSession
> session_
;
357 MockClientSocketFactory socket_factory_
;
358 MockCryptoClientStreamFactory crypto_client_stream_factory_
;
359 MockHostResolver host_resolver_
;
360 MockCertVerifier cert_verifier_
;
361 TransportSecurityState transport_security_state_
;
362 scoped_refptr
<SSLConfigServiceDefaults
> ssl_config_service_
;
363 scoped_ptr
<ProxyService
> proxy_service_
;
364 scoped_ptr
<HttpAuthHandlerFactory
> auth_handler_factory_
;
365 MockRandom random_generator_
;
366 HttpServerPropertiesImpl http_server_properties_
;
367 HttpNetworkSession::Params params_
;
368 HttpRequestInfo request_
;
369 BoundTestNetLog net_log_
;
370 StaticSocketDataProvider hanging_data_
;
373 void SendRequestAndExpectQuicResponseMaybeFromProxy(
374 const std::string
& expected
,
377 scoped_ptr
<HttpNetworkTransaction
> trans(
378 new HttpNetworkTransaction(DEFAULT_PRIORITY
, session_
.get()));
379 ProxyHeadersHandler proxy_headers_handler
;
380 trans
->SetBeforeProxyHeadersSentCallback(
381 base::Bind(&ProxyHeadersHandler::OnBeforeProxyHeadersSent
,
382 base::Unretained(&proxy_headers_handler
)));
383 RunTransaction(trans
);
384 CheckWasQuicResponse(trans
);
385 CheckResponsePort(trans
, port
);
386 CheckResponseData(trans
, expected
);
387 EXPECT_EQ(used_proxy
, proxy_headers_handler
.was_called());
391 INSTANTIATE_TEST_CASE_P(Version
, QuicNetworkTransactionTest
,
392 ::testing::ValuesIn(QuicSupportedVersions()));
394 TEST_P(QuicNetworkTransactionTest
, ForceQuic
) {
395 params_
.origin_to_force_quic_on
=
396 HostPortPair::FromString("www.google.com:80");
398 MockQuicData mock_quic_data
;
399 mock_quic_data
.AddWrite(
400 ConstructRequestHeadersPacket(1, kClientDataStreamId1
, true, true,
401 GetRequestHeaders("GET", "http", "/")));
402 mock_quic_data
.AddRead(
403 ConstructResponseHeadersPacket(1, kClientDataStreamId1
, false, false,
404 GetResponseHeaders("200 OK")));
405 mock_quic_data
.AddRead(
406 ConstructDataPacket(2, kClientDataStreamId1
, false, true, 0, "hello!"));
407 mock_quic_data
.AddWrite(ConstructAckPacket(2, 1));
408 mock_quic_data
.AddRead(SYNCHRONOUS
, 0); // EOF
410 mock_quic_data
.AddSocketDataToFactory(&socket_factory_
);
412 // The non-alternate protocol job needs to hang in order to guarantee that
413 // the alternate-protocol job will "win".
414 AddHangingNonAlternateProtocolSocketData();
418 SendRequestAndExpectQuicResponse("hello!");
420 // Check that the NetLog was filled reasonably.
421 TestNetLogEntry::List entries
;
422 net_log_
.GetEntries(&entries
);
423 EXPECT_LT(0u, entries
.size());
425 // Check that we logged a QUIC_SESSION_PACKET_RECEIVED.
426 int pos
= ExpectLogContainsSomewhere(
427 entries
, 0, NetLog::TYPE_QUIC_SESSION_PACKET_RECEIVED
,
431 // ... and also a TYPE_QUIC_SESSION_PACKET_HEADER_RECEIVED.
432 pos
= ExpectLogContainsSomewhere(
433 entries
, 0, NetLog::TYPE_QUIC_SESSION_PACKET_HEADER_RECEIVED
,
437 std::string packet_sequence_number
;
438 ASSERT_TRUE(entries
[pos
].GetStringValue(
439 "packet_sequence_number", &packet_sequence_number
));
440 EXPECT_EQ("1", packet_sequence_number
);
442 // ... and also a QUIC_SESSION_STREAM_FRAME_RECEIVED.
443 pos
= ExpectLogContainsSomewhere(
444 entries
, 0, NetLog::TYPE_QUIC_SESSION_STREAM_FRAME_RECEIVED
,
449 ASSERT_TRUE(entries
[pos
].GetIntegerValue("stream_id", &log_stream_id
));
450 EXPECT_EQ(3, log_stream_id
);
453 TEST_P(QuicNetworkTransactionTest
, QuicProxy
) {
454 params_
.enable_quic_for_proxies
= true;
455 proxy_service_
.reset(
456 ProxyService::CreateFixedFromPacResult("QUIC myproxy:70"));
458 MockQuicData mock_quic_data
;
459 mock_quic_data
.AddWrite(
460 ConstructRequestHeadersPacket(1, kClientDataStreamId1
, true, true,
461 GetRequestHeaders("GET", "http", "/")));
462 mock_quic_data
.AddRead(
463 ConstructResponseHeadersPacket(1, kClientDataStreamId1
, false, false,
464 GetResponseHeaders("200 OK")));
465 mock_quic_data
.AddRead(
466 ConstructDataPacket(2, kClientDataStreamId1
, false, true, 0, "hello!"));
467 mock_quic_data
.AddWrite(ConstructAckPacket(2, 1));
468 mock_quic_data
.AddRead(SYNCHRONOUS
, 0); // EOF
470 mock_quic_data
.AddSocketDataToFactory(&socket_factory_
);
472 // There is no need to set up an alternate protocol job, because
473 // no attempt will be made to speak to the proxy over TCP.
477 SendRequestAndExpectQuicResponseFromProxyOnPort("hello!", 70);
480 // Regression test for https://crbug.com/492458. Test that for an HTTP
481 // connection through a QUIC proxy, the certificate exhibited by the proxy is
482 // checked against the proxy hostname, not the origin hostname.
483 TEST_P(QuicNetworkTransactionTest
, QuicProxyWithCert
) {
484 const std::string origin_host
= "news.example.com";
485 const std::string proxy_host
= "www.example.org";
487 params_
.enable_quic_for_proxies
= true;
488 proxy_service_
.reset(
489 ProxyService::CreateFixedFromPacResult("QUIC " + proxy_host
+ ":70"));
491 maker_
.set_hostname(origin_host
);
492 MockQuicData mock_quic_data
;
493 mock_quic_data
.AddWrite(
494 ConstructRequestHeadersPacket(1, kClientDataStreamId1
, true, true,
495 GetRequestHeaders("GET", "http", "/")));
496 mock_quic_data
.AddRead(ConstructResponseHeadersPacket(
497 1, kClientDataStreamId1
, false, false, GetResponseHeaders("200 OK")));
498 mock_quic_data
.AddRead(
499 ConstructDataPacket(2, kClientDataStreamId1
, false, true, 0, "hello!"));
500 mock_quic_data
.AddWrite(ConstructAckPacket(2, 1));
501 mock_quic_data
.AddRead(SYNCHRONOUS
, 0);
502 mock_quic_data
.AddSocketDataToFactory(&socket_factory_
);
504 scoped_refptr
<X509Certificate
> cert(
505 ImportCertFromFile(GetTestCertsDirectory(), "spdy_pooling.pem"));
506 ASSERT_TRUE(cert
.get());
507 // This certificate is valid for the proxy, but not for the origin.
508 bool common_name_fallback_used
;
509 EXPECT_TRUE(cert
->VerifyNameMatch(proxy_host
, &common_name_fallback_used
));
510 EXPECT_FALSE(cert
->VerifyNameMatch(origin_host
, &common_name_fallback_used
));
511 ProofVerifyDetailsChromium verify_details
;
512 verify_details
.cert_verify_result
.verified_cert
= cert
;
513 crypto_client_stream_factory_
.AddProofVerifyDetails(&verify_details
);
515 request_
.url
= GURL("http://" + origin_host
);
516 AddHangingNonAlternateProtocolSocketData();
517 CreateSessionWithNextProtos();
518 AddQuicAlternateProtocolMapping(MockCryptoClientStream::CONFIRM_HANDSHAKE
);
519 SendRequestAndExpectQuicResponseFromProxyOnPort("hello!", 70);
522 TEST_P(QuicNetworkTransactionTest
, ForceQuicWithErrorConnecting
) {
523 params_
.origin_to_force_quic_on
=
524 HostPortPair::FromString("www.google.com:80");
526 MockQuicData mock_quic_data
;
527 mock_quic_data
.AddRead(ASYNC
, ERR_SOCKET_NOT_CONNECTED
);
529 mock_quic_data
.AddSocketDataToFactory(&socket_factory_
);
533 scoped_ptr
<HttpNetworkTransaction
> trans(
534 new HttpNetworkTransaction(DEFAULT_PRIORITY
, session_
.get()));
535 TestCompletionCallback callback
;
536 int rv
= trans
->Start(&request_
, callback
.callback(), net_log_
.bound());
537 EXPECT_EQ(ERR_IO_PENDING
, rv
);
538 EXPECT_EQ(ERR_CONNECTION_CLOSED
, callback
.WaitForResult());
541 TEST_P(QuicNetworkTransactionTest
, DoNotForceQuicForHttps
) {
542 // Attempt to "force" quic on 443, which will not be honored.
543 params_
.origin_to_force_quic_on
=
544 HostPortPair::FromString("www.google.com:443");
546 MockRead http_reads
[] = {
547 MockRead("HTTP/1.1 200 OK\r\n\r\n"),
548 MockRead("hello world"),
549 MockRead(SYNCHRONOUS
, ERR_TEST_PEER_CLOSE_AFTER_NEXT_MOCK_READ
),
553 StaticSocketDataProvider
data(http_reads
, arraysize(http_reads
), nullptr, 0);
554 socket_factory_
.AddSocketDataProvider(&data
);
555 SSLSocketDataProvider
ssl(ASYNC
, OK
);
556 socket_factory_
.AddSSLSocketDataProvider(&ssl
);
560 SendRequestAndExpectHttpResponse("hello world");
563 TEST_P(QuicNetworkTransactionTest
, UseAlternateProtocolForQuic
) {
564 MockRead http_reads
[] = {
565 MockRead("HTTP/1.1 200 OK\r\n"),
566 MockRead(kQuicAlternateProtocolHttpHeader
),
567 MockRead("hello world"),
568 MockRead(SYNCHRONOUS
, ERR_TEST_PEER_CLOSE_AFTER_NEXT_MOCK_READ
),
572 StaticSocketDataProvider
http_data(http_reads
, arraysize(http_reads
),
574 socket_factory_
.AddSocketDataProvider(&http_data
);
576 MockQuicData mock_quic_data
;
577 mock_quic_data
.AddWrite(
578 ConstructRequestHeadersPacket(1, kClientDataStreamId1
, true, true,
579 GetRequestHeaders("GET", "http", "/")));
580 mock_quic_data
.AddRead(
581 ConstructResponseHeadersPacket(1, kClientDataStreamId1
, false, false,
582 GetResponseHeaders("200 OK")));
583 mock_quic_data
.AddRead(
584 ConstructDataPacket(2, kClientDataStreamId1
, false, true, 0, "hello!"));
585 mock_quic_data
.AddWrite(ConstructAckPacket(2, 1));
586 mock_quic_data
.AddRead(SYNCHRONOUS
, 0); // EOF
588 mock_quic_data
.AddSocketDataToFactory(&socket_factory_
);
590 // The non-alternate protocol job needs to hang in order to guarantee that
591 // the alternate-protocol job will "win".
592 AddHangingNonAlternateProtocolSocketData();
594 CreateSessionWithNextProtos();
596 SendRequestAndExpectHttpResponse("hello world");
597 SendRequestAndExpectQuicResponse("hello!");
600 TEST_P(QuicNetworkTransactionTest
, AlternateProtocolDifferentPort
) {
601 MockRead http_reads
[] = {
602 MockRead("HTTP/1.1 200 OK\r\n"),
603 MockRead(kQuicAlternateProtocolDifferentPortHttpHeader
),
604 MockRead("hello world"),
605 MockRead(SYNCHRONOUS
, ERR_TEST_PEER_CLOSE_AFTER_NEXT_MOCK_READ
),
606 MockRead(ASYNC
, OK
)};
608 StaticSocketDataProvider
http_data(http_reads
, arraysize(http_reads
), nullptr,
610 socket_factory_
.AddSocketDataProvider(&http_data
);
612 MockQuicData mock_quic_data
;
613 mock_quic_data
.AddWrite(
614 ConstructRequestHeadersPacket(1, kClientDataStreamId1
, true, true,
615 GetRequestHeaders("GET", "http", "/")));
616 mock_quic_data
.AddRead(ConstructResponseHeadersPacket(
617 1, kClientDataStreamId1
, false, false, GetResponseHeaders("200 OK")));
618 mock_quic_data
.AddRead(
619 ConstructDataPacket(2, kClientDataStreamId1
, false, true, 0, "hello!"));
620 mock_quic_data
.AddWrite(ConstructAckPacket(2, 1));
621 mock_quic_data
.AddRead(SYNCHRONOUS
, 0); // EOF
623 mock_quic_data
.AddSocketDataToFactory(&socket_factory_
);
625 // The non-alternate protocol job needs to hang in order to guarantee that
626 // the alternate-protocol job will "win".
627 AddHangingNonAlternateProtocolSocketData();
629 CreateSessionWithNextProtos();
631 SendRequestAndExpectHttpResponse("hello world");
632 SendRequestAndExpectQuicResponseOnPort("hello!", 137);
635 TEST_P(QuicNetworkTransactionTest
, ConfirmAlternativeService
) {
636 MockRead http_reads
[] = {
637 MockRead("HTTP/1.1 200 OK\r\n"),
638 MockRead(kQuicAlternateProtocolHttpHeader
),
639 MockRead("hello world"),
640 MockRead(SYNCHRONOUS
, ERR_TEST_PEER_CLOSE_AFTER_NEXT_MOCK_READ
),
641 MockRead(ASYNC
, OK
)};
643 StaticSocketDataProvider
http_data(http_reads
, arraysize(http_reads
), nullptr,
645 socket_factory_
.AddSocketDataProvider(&http_data
);
647 MockQuicData mock_quic_data
;
648 mock_quic_data
.AddWrite(
649 ConstructRequestHeadersPacket(1, kClientDataStreamId1
, true, true,
650 GetRequestHeaders("GET", "http", "/")));
651 mock_quic_data
.AddRead(ConstructResponseHeadersPacket(
652 1, kClientDataStreamId1
, false, false, GetResponseHeaders("200 OK")));
653 mock_quic_data
.AddRead(
654 ConstructDataPacket(2, kClientDataStreamId1
, false, true, 0, "hello!"));
655 mock_quic_data
.AddWrite(ConstructAckPacket(2, 1));
656 mock_quic_data
.AddRead(SYNCHRONOUS
, 0); // EOF
658 mock_quic_data
.AddSocketDataToFactory(&socket_factory_
);
660 // The non-alternate protocol job needs to hang in order to guarantee that
661 // the alternate-protocol job will "win".
662 AddHangingNonAlternateProtocolSocketData();
664 CreateSessionWithNextProtos();
666 AlternativeService
alternative_service(QUIC
,
667 HostPortPair::FromURL(request_
.url
));
668 http_server_properties_
.MarkAlternativeServiceRecentlyBroken(
669 alternative_service
);
670 EXPECT_TRUE(http_server_properties_
.WasAlternativeServiceRecentlyBroken(
671 alternative_service
));
673 SendRequestAndExpectHttpResponse("hello world");
674 SendRequestAndExpectQuicResponse("hello!");
676 EXPECT_FALSE(http_server_properties_
.WasAlternativeServiceRecentlyBroken(
677 alternative_service
));
680 TEST_P(QuicNetworkTransactionTest
, UseAlternateProtocolProbabilityForQuic
) {
681 MockRead http_reads
[] = {
682 MockRead("HTTP/1.1 200 OK\r\n"),
683 MockRead(kQuicAlternateProtocol50pctHttpHeader
),
684 MockRead("hello world"),
685 MockRead(SYNCHRONOUS
, ERR_TEST_PEER_CLOSE_AFTER_NEXT_MOCK_READ
),
689 StaticSocketDataProvider
http_data(http_reads
, arraysize(http_reads
),
691 socket_factory_
.AddSocketDataProvider(&http_data
);
693 MockQuicData mock_quic_data
;
694 mock_quic_data
.AddWrite(
695 ConstructRequestHeadersPacket(1, kClientDataStreamId1
, true, true,
696 GetRequestHeaders("GET", "http", "/")));
697 mock_quic_data
.AddRead(
698 ConstructResponseHeadersPacket(1, kClientDataStreamId1
, false, false,
699 GetResponseHeaders("200 OK")));
700 mock_quic_data
.AddRead(
701 ConstructDataPacket(2, kClientDataStreamId1
, false, true, 0, "hello!"));
702 mock_quic_data
.AddWrite(ConstructAckPacket(2, 1));
703 mock_quic_data
.AddRead(SYNCHRONOUS
, 0); // EOF
705 mock_quic_data
.AddSocketDataToFactory(&socket_factory_
);
707 // The non-alternate protocol job needs to hang in order to guarantee that
708 // the alternate-protocol job will "win".
709 AddHangingNonAlternateProtocolSocketData();
711 params_
.alternative_service_probability_threshold
= .25;
712 CreateSessionWithNextProtos();
714 SendRequestAndExpectHttpResponse("hello world");
715 SendRequestAndExpectQuicResponse("hello!");
718 TEST_P(QuicNetworkTransactionTest
, DontUseAlternateProtocolProbabilityForQuic
) {
719 MockRead http_reads
[] = {
720 MockRead("HTTP/1.1 200 OK\r\n"),
721 MockRead(kQuicAlternateProtocol50pctHttpHeader
),
722 MockRead("hello world"),
723 MockRead(SYNCHRONOUS
, ERR_TEST_PEER_CLOSE_AFTER_NEXT_MOCK_READ
),
727 StaticSocketDataProvider
http_data(http_reads
, arraysize(http_reads
),
729 socket_factory_
.AddSocketDataProvider(&http_data
);
730 socket_factory_
.AddSocketDataProvider(&http_data
);
732 params_
.alternative_service_probability_threshold
= .75;
733 CreateSessionWithNextProtos();
735 SendRequestAndExpectHttpResponse("hello world");
736 SendRequestAndExpectHttpResponse("hello world");
739 TEST_P(QuicNetworkTransactionTest
, DontUseAlternateProtocolForInsecureQuic
) {
740 MockRead http_reads
[] = {MockRead("HTTP/1.1 200 OK\r\n"),
741 MockRead("Content-length: 11\r\n"),
742 MockRead("Alternate-Protocol: 443:quic\r\n\r\n"),
743 MockRead("hello world"),
744 MockRead("HTTP/1.1 200 OK\r\n"),
745 MockRead("Content-length: 11\r\n"),
746 MockRead("Alternate-Protocol: 443:quic\r\n\r\n"),
747 MockRead("hello world"),
748 MockRead(ASYNC
, OK
)};
750 StaticSocketDataProvider
http_data(http_reads
, arraysize(http_reads
), nullptr,
752 socket_factory_
.AddSocketDataProvider(&http_data
);
753 socket_factory_
.AddSocketDataProvider(&http_data
);
755 params_
.disable_insecure_quic
= true;
756 CreateSessionWithNextProtos();
758 SendRequestAndExpectHttpResponse("hello world");
759 SendRequestAndExpectHttpResponse("hello world");
762 TEST_P(QuicNetworkTransactionTest
,
763 DontUseAlternateProtocolWithBadProbabilityForQuic
) {
764 MockRead http_reads
[] = {
765 MockRead("HTTP/1.1 200 OK\r\n"),
766 MockRead("Alternate-Protocol: 443:quic,p=2\r\n\r\n"),
767 MockRead("hello world"),
768 MockRead(SYNCHRONOUS
, ERR_TEST_PEER_CLOSE_AFTER_NEXT_MOCK_READ
),
772 StaticSocketDataProvider
http_data(http_reads
, arraysize(http_reads
),
774 socket_factory_
.AddSocketDataProvider(&http_data
);
775 socket_factory_
.AddSocketDataProvider(&http_data
);
777 params_
.alternative_service_probability_threshold
= .75;
778 CreateSessionWithNextProtos();
780 SendRequestAndExpectHttpResponse("hello world");
781 SendRequestAndExpectHttpResponse("hello world");
784 TEST_P(QuicNetworkTransactionTest
, UseAlternateProtocolForQuicForHttps
) {
785 params_
.origin_to_force_quic_on
=
786 HostPortPair::FromString("www.google.com:443");
788 MockRead http_reads
[] = {
789 MockRead("HTTP/1.1 200 OK\r\n"),
790 MockRead(kQuicAlternateProtocolHttpsHeader
),
791 MockRead("hello world"),
792 MockRead(SYNCHRONOUS
, ERR_TEST_PEER_CLOSE_AFTER_NEXT_MOCK_READ
),
796 StaticSocketDataProvider
http_data(http_reads
, arraysize(http_reads
),
798 socket_factory_
.AddSocketDataProvider(&http_data
);
800 MockQuicData mock_quic_data
;
801 mock_quic_data
.AddWrite(
802 ConstructRequestHeadersPacket(1, kClientDataStreamId1
, true, true,
803 GetRequestHeaders("GET", "http", "/")));
804 mock_quic_data
.AddRead(
805 ConstructResponseHeadersPacket(1, kClientDataStreamId1
, false, false,
806 GetResponseHeaders("200 OK")));
807 mock_quic_data
.AddRead(
808 ConstructDataPacket(2, kClientDataStreamId1
, false, true, 0, "hello!"));
809 mock_quic_data
.AddWrite(ConstructAckPacket(2, 1));
810 mock_quic_data
.AddRead(SYNCHRONOUS
, 0); // EOF
812 mock_quic_data
.AddSocketDataToFactory(&socket_factory_
);
814 // The non-alternate protocol job needs to hang in order to guarantee that
815 // the alternate-protocol job will "win".
816 AddHangingNonAlternateProtocolSocketData();
818 CreateSessionWithNextProtos();
820 // TODO(rtenneti): Test QUIC over HTTPS, GetSSLInfo().
821 SendRequestAndExpectHttpResponse("hello world");
824 class QuicAltSvcCertificateVerificationTest
825 : public QuicNetworkTransactionTest
{
827 void Run(bool valid
) {
828 HostPortPair
origin(valid
? "mail.example.org" : "invalid.example.org",
830 HostPortPair
alternative("www.example.org", 443);
831 std::string
url("https://");
832 url
.append(origin
.host());
834 request_
.url
= GURL(url
);
836 maker_
.set_hostname(origin
.host());
837 MockQuicData mock_quic_data
;
838 mock_quic_data
.AddWrite(
839 ConstructRequestHeadersPacket(1, kClientDataStreamId1
, true, true,
840 GetRequestHeaders("GET", "https", "/")));
841 mock_quic_data
.AddRead(ConstructResponseHeadersPacket(
842 1, kClientDataStreamId1
, false, false, GetResponseHeaders("200 OK")));
843 mock_quic_data
.AddRead(
844 ConstructDataPacket(2, kClientDataStreamId1
, false, true, 0, "hello!"));
845 mock_quic_data
.AddWrite(ConstructAckPacket(2, 1));
846 mock_quic_data
.AddRead(SYNCHRONOUS
, 0);
847 mock_quic_data
.AddSocketDataToFactory(&socket_factory_
);
849 scoped_refptr
<X509Certificate
> cert(
850 ImportCertFromFile(GetTestCertsDirectory(), "spdy_pooling.pem"));
851 ASSERT_TRUE(cert
.get());
852 bool common_name_fallback_used
;
854 cert
->VerifyNameMatch(origin
.host(), &common_name_fallback_used
));
856 cert
->VerifyNameMatch(alternative
.host(), &common_name_fallback_used
));
857 ProofVerifyDetailsChromium verify_details
;
858 verify_details
.cert_verify_result
.verified_cert
= cert
;
859 verify_details
.cert_verify_result
.is_issued_by_known_root
= true;
860 crypto_client_stream_factory_
.AddProofVerifyDetails(&verify_details
);
861 crypto_client_stream_factory_
.set_handshake_mode(
862 MockCryptoClientStream::CONFIRM_HANDSHAKE
);
864 // Connection to |origin| fails, so that success of |request| depends on
865 // connection to |alternate| only.
866 MockConnect
refused_connect(ASYNC
, ERR_CONNECTION_REFUSED
);
867 StaticSocketDataProvider refused_data
;
868 refused_data
.set_connect_data(refused_connect
);
869 socket_factory_
.AddSocketDataProvider(&refused_data
);
871 CreateSessionWithNextProtos();
872 AlternativeService
alternative_service(QUIC
, alternative
);
873 session_
->http_server_properties()->SetAlternativeService(
874 origin
, alternative_service
, 1.0);
875 scoped_ptr
<HttpNetworkTransaction
> trans(
876 new HttpNetworkTransaction(DEFAULT_PRIORITY
, session_
.get()));
877 TestCompletionCallback callback
;
878 int rv
= trans
->Start(&request_
, callback
.callback(), net_log_
.bound());
879 EXPECT_EQ(ERR_IO_PENDING
, rv
);
880 rv
= callback
.WaitForResult();
883 CheckWasQuicResponse(trans
);
884 CheckResponsePort(trans
, 443);
885 CheckResponseData(trans
, "hello!");
887 EXPECT_EQ(ERR_CONNECTION_REFUSED
, rv
);
892 INSTANTIATE_TEST_CASE_P(Version
,
893 QuicAltSvcCertificateVerificationTest
,
894 ::testing::ValuesIn(QuicSupportedVersions()));
896 TEST_P(QuicAltSvcCertificateVerificationTest
,
897 RequestSucceedsWithValidCertificate
) {
901 TEST_P(QuicAltSvcCertificateVerificationTest
,
902 RequestFailsWithInvalidCertificate
) {
906 TEST_P(QuicNetworkTransactionTest
, HungAlternateProtocol
) {
907 crypto_client_stream_factory_
.set_handshake_mode(
908 MockCryptoClientStream::COLD_START
);
910 MockWrite http_writes
[] = {
911 MockWrite(SYNCHRONOUS
, 0, "GET / HTTP/1.1\r\n"),
912 MockWrite(SYNCHRONOUS
, 1, "Host: www.google.com\r\n"),
913 MockWrite(SYNCHRONOUS
, 2, "Connection: keep-alive\r\n\r\n")
916 MockRead http_reads
[] = {
917 MockRead(SYNCHRONOUS
, 3, "HTTP/1.1 200 OK\r\n"),
918 MockRead(SYNCHRONOUS
, 4, kQuicAlternateProtocolHttpHeader
),
919 MockRead(SYNCHRONOUS
, 5, "hello world"),
920 MockRead(SYNCHRONOUS
, OK
, 6)
923 DeterministicMockClientSocketFactory socket_factory
;
925 DeterministicSocketData
http_data(http_reads
, arraysize(http_reads
),
926 http_writes
, arraysize(http_writes
));
927 socket_factory
.AddSocketDataProvider(&http_data
);
929 // The QUIC transaction will not be allowed to complete.
930 MockWrite quic_writes
[] = {
931 MockWrite(ASYNC
, ERR_IO_PENDING
, 0)
933 MockRead quic_reads
[] = {
934 MockRead(ASYNC
, ERR_IO_PENDING
, 1),
936 DeterministicSocketData
quic_data(quic_reads
, arraysize(quic_reads
),
937 quic_writes
, arraysize(quic_writes
));
938 socket_factory
.AddSocketDataProvider(&quic_data
);
940 // The HTTP transaction will complete.
941 DeterministicSocketData
http_data2(http_reads
, arraysize(http_reads
),
942 http_writes
, arraysize(http_writes
));
943 socket_factory
.AddSocketDataProvider(&http_data2
);
945 CreateSessionWithFactory(&socket_factory
, true);
947 // Run the first request.
948 http_data
.StopAfter(arraysize(http_reads
) + arraysize(http_writes
));
949 SendRequestAndExpectHttpResponse("hello world");
950 ASSERT_TRUE(http_data
.AllReadDataConsumed());
951 ASSERT_TRUE(http_data
.AllWriteDataConsumed());
953 // Now run the second request in which the QUIC socket hangs,
954 // and verify the the transaction continues over HTTP.
955 http_data2
.StopAfter(arraysize(http_reads
) + arraysize(http_writes
));
956 SendRequestAndExpectHttpResponse("hello world");
958 ASSERT_TRUE(http_data2
.AllReadDataConsumed());
959 ASSERT_TRUE(http_data2
.AllWriteDataConsumed());
960 ASSERT_TRUE(!quic_data
.AllReadDataConsumed());
961 ASSERT_TRUE(!quic_data
.AllWriteDataConsumed());
964 TEST_P(QuicNetworkTransactionTest
, ZeroRTTWithHttpRace
) {
965 MockQuicData mock_quic_data
;
966 mock_quic_data
.AddWrite(
967 ConstructRequestHeadersPacket(1, kClientDataStreamId1
, true, true,
968 GetRequestHeaders("GET", "http", "/")));
969 mock_quic_data
.AddRead(
970 ConstructResponseHeadersPacket(1, kClientDataStreamId1
, false, false,
971 GetResponseHeaders("200 OK")));
972 mock_quic_data
.AddRead(
973 ConstructDataPacket(2, kClientDataStreamId1
, false, true, 0, "hello!"));
974 mock_quic_data
.AddWrite(ConstructAckPacket(2, 1));
975 mock_quic_data
.AddRead(SYNCHRONOUS
, 0); // EOF
977 mock_quic_data
.AddSocketDataToFactory(&socket_factory_
);
979 // The non-alternate protocol job needs to hang in order to guarantee that
980 // the alternate-protocol job will "win".
981 AddHangingNonAlternateProtocolSocketData();
983 CreateSessionWithNextProtos();
984 AddQuicAlternateProtocolMapping(MockCryptoClientStream::ZERO_RTT
);
985 SendRequestAndExpectQuicResponse("hello!");
988 TEST_P(QuicNetworkTransactionTest
, ZeroRTTWithNoHttpRace
) {
989 MockQuicData mock_quic_data
;
990 mock_quic_data
.AddWrite(
991 ConstructRequestHeadersPacket(1, kClientDataStreamId1
, true, true,
992 GetRequestHeaders("GET", "http", "/")));
993 mock_quic_data
.AddRead(
994 ConstructResponseHeadersPacket(1, kClientDataStreamId1
, false, false,
995 GetResponseHeaders("200 OK")));
996 mock_quic_data
.AddRead(
997 ConstructDataPacket(2, kClientDataStreamId1
, false, true, 0, "hello!"));
998 mock_quic_data
.AddWrite(ConstructAckPacket(2, 1));
999 mock_quic_data
.AddRead(SYNCHRONOUS
, 0); // EOF
1000 mock_quic_data
.AddSocketDataToFactory(&socket_factory_
);
1002 // In order for a new QUIC session to be established via alternate-protocol
1003 // without racing an HTTP connection, we need the host resolution to happen
1005 host_resolver_
.set_synchronous_mode(true);
1006 host_resolver_
.rules()->AddIPLiteralRule("www.google.com", "192.168.0.1", "");
1007 HostResolver::RequestInfo
info(HostPortPair("www.google.com", 80));
1008 AddressList address
;
1009 host_resolver_
.Resolve(info
,
1012 CompletionCallback(),
1016 CreateSessionWithNextProtos();
1017 AddQuicAlternateProtocolMapping(MockCryptoClientStream::ZERO_RTT
);
1018 SendRequestAndExpectQuicResponse("hello!");
1021 TEST_P(QuicNetworkTransactionTest
, ZeroRTTWithProxy
) {
1022 proxy_service_
.reset(
1023 ProxyService::CreateFixedFromPacResult("PROXY myproxy:70"));
1025 // Since we are using a proxy, the QUIC job will not succeed.
1026 MockWrite http_writes
[] = {
1027 MockWrite(SYNCHRONOUS
, 0, "GET http://www.google.com/ HTTP/1.1\r\n"),
1028 MockWrite(SYNCHRONOUS
, 1, "Host: www.google.com\r\n"),
1029 MockWrite(SYNCHRONOUS
, 2, "Proxy-Connection: keep-alive\r\n\r\n")
1032 MockRead http_reads
[] = {
1033 MockRead(SYNCHRONOUS
, 3, "HTTP/1.1 200 OK\r\n"),
1034 MockRead(SYNCHRONOUS
, 4, kQuicAlternateProtocolHttpHeader
),
1035 MockRead(SYNCHRONOUS
, 5, "hello world"),
1036 MockRead(SYNCHRONOUS
, OK
, 6)
1039 StaticSocketDataProvider
http_data(http_reads
, arraysize(http_reads
),
1040 http_writes
, arraysize(http_writes
));
1041 socket_factory_
.AddSocketDataProvider(&http_data
);
1043 // In order for a new QUIC session to be established via alternate-protocol
1044 // without racing an HTTP connection, we need the host resolution to happen
1046 host_resolver_
.set_synchronous_mode(true);
1047 host_resolver_
.rules()->AddIPLiteralRule("www.google.com", "192.168.0.1", "");
1048 HostResolver::RequestInfo
info(HostPortPair("www.google.com", 80));
1049 AddressList address
;
1050 host_resolver_
.Resolve(info
,
1053 CompletionCallback(),
1057 CreateSessionWithNextProtos();
1058 AddQuicAlternateProtocolMapping(MockCryptoClientStream::ZERO_RTT
);
1059 SendRequestAndExpectHttpResponse("hello world");
1062 TEST_P(QuicNetworkTransactionTest
, ZeroRTTWithConfirmationRequired
) {
1063 MockQuicData mock_quic_data
;
1064 mock_quic_data
.AddWrite(
1065 ConstructRequestHeadersPacket(1, kClientDataStreamId1
, true, true,
1066 GetRequestHeaders("GET", "http", "/")));
1067 mock_quic_data
.AddRead(
1068 ConstructResponseHeadersPacket(1, kClientDataStreamId1
, false, false,
1069 GetResponseHeaders("200 OK")));
1070 mock_quic_data
.AddRead(
1071 ConstructDataPacket(2, kClientDataStreamId1
, false, true, 0, "hello!"));
1072 mock_quic_data
.AddWrite(ConstructAckPacket(2, 1));
1073 mock_quic_data
.AddRead(SYNCHRONOUS
, 0); // EOF
1074 mock_quic_data
.AddSocketDataToFactory(&socket_factory_
);
1076 // The non-alternate protocol job needs to hang in order to guarantee that
1077 // the alternate-protocol job will "win".
1078 AddHangingNonAlternateProtocolSocketData();
1080 // In order for a new QUIC session to be established via alternate-protocol
1081 // without racing an HTTP connection, we need the host resolution to happen
1082 // synchronously. Of course, even though QUIC *could* perform a 0-RTT
1083 // connection to the the server, in this test we require confirmation
1084 // before encrypting so the HTTP job will still start.
1085 host_resolver_
.set_synchronous_mode(true);
1086 host_resolver_
.rules()->AddIPLiteralRule("www.google.com", "192.168.0.1", "");
1087 HostResolver::RequestInfo
info(HostPortPair("www.google.com", 80));
1088 AddressList address
;
1089 host_resolver_
.Resolve(info
, DEFAULT_PRIORITY
, &address
,
1090 CompletionCallback(), nullptr, net_log_
.bound());
1092 CreateSessionWithNextProtos();
1093 session_
->quic_stream_factory()->set_require_confirmation(true);
1094 AddQuicAlternateProtocolMapping(MockCryptoClientStream::ZERO_RTT
);
1096 scoped_ptr
<HttpNetworkTransaction
> trans(
1097 new HttpNetworkTransaction(DEFAULT_PRIORITY
, session_
.get()));
1098 TestCompletionCallback callback
;
1099 int rv
= trans
->Start(&request_
, callback
.callback(), net_log_
.bound());
1100 EXPECT_EQ(ERR_IO_PENDING
, rv
);
1102 crypto_client_stream_factory_
.last_stream()->SendOnCryptoHandshakeEvent(
1103 QuicSession::HANDSHAKE_CONFIRMED
);
1104 EXPECT_EQ(OK
, callback
.WaitForResult());
1107 TEST_P(QuicNetworkTransactionTest
, BrokenAlternateProtocol
) {
1108 // Alternate-protocol job
1109 scoped_ptr
<QuicEncryptedPacket
> close(ConstructConnectionClosePacket(1));
1110 MockRead quic_reads
[] = {
1111 MockRead(ASYNC
, close
->data(), close
->length()),
1112 MockRead(ASYNC
, OK
), // EOF
1114 StaticSocketDataProvider
quic_data(quic_reads
, arraysize(quic_reads
),
1116 socket_factory_
.AddSocketDataProvider(&quic_data
);
1118 // Main job which will succeed even though the alternate 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();
1131 AddQuicAlternateProtocolMapping(MockCryptoClientStream::COLD_START
);
1132 SendRequestAndExpectHttpResponse("hello from http");
1133 ExpectBrokenAlternateProtocolMapping();
1136 TEST_P(QuicNetworkTransactionTest
, BrokenAlternateProtocolReadError
) {
1137 // Alternate-protocol job
1138 MockRead quic_reads
[] = {
1139 MockRead(ASYNC
, ERR_SOCKET_NOT_CONNECTED
),
1141 StaticSocketDataProvider
quic_data(quic_reads
, arraysize(quic_reads
),
1143 socket_factory_
.AddSocketDataProvider(&quic_data
);
1145 // Main job which will succeed even though the alternate job fails.
1146 MockRead http_reads
[] = {
1147 MockRead("HTTP/1.1 200 OK\r\n\r\n"),
1148 MockRead("hello from http"),
1149 MockRead(SYNCHRONOUS
, ERR_TEST_PEER_CLOSE_AFTER_NEXT_MOCK_READ
),
1153 StaticSocketDataProvider
http_data(http_reads
, arraysize(http_reads
),
1155 socket_factory_
.AddSocketDataProvider(&http_data
);
1157 CreateSessionWithNextProtos();
1159 AddQuicAlternateProtocolMapping(MockCryptoClientStream::COLD_START
);
1160 SendRequestAndExpectHttpResponse("hello from http");
1161 ExpectBrokenAlternateProtocolMapping();
1164 TEST_P(QuicNetworkTransactionTest
, NoBrokenAlternateProtocolIfTcpFails
) {
1165 // Alternate-protocol job will fail when the session attempts to read.
1166 MockRead quic_reads
[] = {
1167 MockRead(ASYNC
, ERR_SOCKET_NOT_CONNECTED
),
1169 StaticSocketDataProvider
quic_data(quic_reads
, arraysize(quic_reads
),
1171 socket_factory_
.AddSocketDataProvider(&quic_data
);
1173 // Main job will also fail.
1174 MockRead http_reads
[] = {
1175 MockRead(ASYNC
, ERR_SOCKET_NOT_CONNECTED
),
1178 StaticSocketDataProvider
http_data(http_reads
, arraysize(http_reads
),
1180 http_data
.set_connect_data(MockConnect(ASYNC
, ERR_SOCKET_NOT_CONNECTED
));
1181 socket_factory_
.AddSocketDataProvider(&http_data
);
1183 CreateSessionWithNextProtos();
1185 AddQuicAlternateProtocolMapping(MockCryptoClientStream::COLD_START
);
1186 scoped_ptr
<HttpNetworkTransaction
> trans(
1187 new HttpNetworkTransaction(DEFAULT_PRIORITY
, session_
.get()));
1188 TestCompletionCallback callback
;
1189 int rv
= trans
->Start(&request_
, callback
.callback(), net_log_
.bound());
1190 EXPECT_EQ(ERR_IO_PENDING
, rv
);
1191 EXPECT_EQ(ERR_SOCKET_NOT_CONNECTED
, callback
.WaitForResult());
1192 ExpectQuicAlternateProtocolMapping();
1195 TEST_P(QuicNetworkTransactionTest
, FailedZeroRttBrokenAlternateProtocol
) {
1196 // Alternate-protocol job
1197 MockRead quic_reads
[] = {
1198 MockRead(ASYNC
, ERR_SOCKET_NOT_CONNECTED
),
1200 StaticSocketDataProvider
quic_data(quic_reads
, arraysize(quic_reads
),
1202 socket_factory_
.AddSocketDataProvider(&quic_data
);
1204 AddHangingNonAlternateProtocolSocketData();
1206 // Second Alternate-protocol job which will race with the TCP job.
1207 StaticSocketDataProvider
quic_data2(quic_reads
, arraysize(quic_reads
),
1209 socket_factory_
.AddSocketDataProvider(&quic_data2
);
1211 // Final job that will proceed when the QUIC job fails.
1212 MockRead http_reads
[] = {
1213 MockRead("HTTP/1.1 200 OK\r\n\r\n"),
1214 MockRead("hello from http"),
1215 MockRead(SYNCHRONOUS
, ERR_TEST_PEER_CLOSE_AFTER_NEXT_MOCK_READ
),
1219 StaticSocketDataProvider
http_data(http_reads
, arraysize(http_reads
),
1221 socket_factory_
.AddSocketDataProvider(&http_data
);
1223 CreateSessionWithNextProtos();
1225 AddQuicAlternateProtocolMapping(MockCryptoClientStream::ZERO_RTT
);
1227 SendRequestAndExpectHttpResponse("hello from http");
1229 ExpectBrokenAlternateProtocolMapping();
1231 EXPECT_TRUE(quic_data
.AllReadDataConsumed());
1232 EXPECT_TRUE(quic_data
.AllWriteDataConsumed());
1235 TEST_P(QuicNetworkTransactionTest
, DISABLED_HangingZeroRttFallback
) {
1236 // Alternate-protocol job
1237 MockRead quic_reads
[] = {
1238 MockRead(ASYNC
, ERR_IO_PENDING
),
1240 StaticSocketDataProvider
quic_data(quic_reads
, arraysize(quic_reads
),
1242 socket_factory_
.AddSocketDataProvider(&quic_data
);
1244 // Main job that will proceed when the QUIC job fails.
1245 MockRead http_reads
[] = {
1246 MockRead("HTTP/1.1 200 OK\r\n\r\n"),
1247 MockRead("hello from http"),
1248 MockRead(SYNCHRONOUS
, ERR_TEST_PEER_CLOSE_AFTER_NEXT_MOCK_READ
),
1252 StaticSocketDataProvider
http_data(http_reads
, arraysize(http_reads
),
1254 socket_factory_
.AddSocketDataProvider(&http_data
);
1256 CreateSessionWithNextProtos();
1258 AddQuicAlternateProtocolMapping(MockCryptoClientStream::ZERO_RTT
);
1260 SendRequestAndExpectHttpResponse("hello from http");
1263 TEST_P(QuicNetworkTransactionTest
, BrokenAlternateProtocolOnConnectFailure
) {
1264 // Alternate-protocol job will fail before creating a QUIC session.
1265 StaticSocketDataProvider
quic_data(nullptr, 0, nullptr, 0);
1266 quic_data
.set_connect_data(MockConnect(SYNCHRONOUS
,
1267 ERR_INTERNET_DISCONNECTED
));
1268 socket_factory_
.AddSocketDataProvider(&quic_data
);
1270 // Main job which will succeed even though the alternate job fails.
1271 MockRead http_reads
[] = {
1272 MockRead("HTTP/1.1 200 OK\r\n\r\n"),
1273 MockRead("hello from http"),
1274 MockRead(SYNCHRONOUS
, ERR_TEST_PEER_CLOSE_AFTER_NEXT_MOCK_READ
),
1278 StaticSocketDataProvider
http_data(http_reads
, arraysize(http_reads
),
1280 socket_factory_
.AddSocketDataProvider(&http_data
);
1282 CreateSessionWithNextProtos();
1283 AddQuicAlternateProtocolMapping(MockCryptoClientStream::COLD_START
);
1284 SendRequestAndExpectHttpResponse("hello from http");
1286 ExpectBrokenAlternateProtocolMapping();
1289 TEST_P(QuicNetworkTransactionTest
, ConnectionCloseDuringConnect
) {
1290 MockQuicData mock_quic_data
;
1291 mock_quic_data
.AddSynchronousRead(ConstructConnectionClosePacket(1));
1292 mock_quic_data
.AddWrite(
1293 ConstructRequestHeadersPacket(1, kClientDataStreamId1
, true, true,
1294 GetRequestHeaders("GET", "http", "/")));
1295 mock_quic_data
.AddWrite(ConstructAckPacket(2, 1));
1296 mock_quic_data
.AddSocketDataToFactory(&socket_factory_
);
1298 // When the QUIC connection fails, we will try the request again over HTTP.
1299 MockRead http_reads
[] = {
1300 MockRead("HTTP/1.1 200 OK\r\n"),
1301 MockRead(kQuicAlternateProtocolHttpHeader
),
1302 MockRead("hello world"),
1303 MockRead(SYNCHRONOUS
, ERR_TEST_PEER_CLOSE_AFTER_NEXT_MOCK_READ
),
1307 StaticSocketDataProvider
http_data(http_reads
, arraysize(http_reads
),
1309 socket_factory_
.AddSocketDataProvider(&http_data
);
1311 // In order for a new QUIC session to be established via alternate-protocol
1312 // without racing an HTTP connection, we need the host resolution to happen
1314 host_resolver_
.set_synchronous_mode(true);
1315 host_resolver_
.rules()->AddIPLiteralRule("www.google.com", "192.168.0.1", "");
1316 HostResolver::RequestInfo
info(HostPortPair("www.google.com", 80));
1317 AddressList address
;
1318 host_resolver_
.Resolve(info
,
1321 CompletionCallback(),
1325 CreateSessionWithNextProtos();
1326 AddQuicAlternateProtocolMapping(MockCryptoClientStream::ZERO_RTT
);
1327 SendRequestAndExpectHttpResponse("hello world");
1330 // Test that a secure request over an insecure QUIC connection fails with
1331 // the appropriate error code. Note that this never happens in production,
1332 // because the handshake (which this test mocks) would fail in this scenario.
1333 TEST_P(QuicNetworkTransactionTest
, SecureResourceOverInsecureQuic
) {
1334 maker_
.set_hostname("www.example.org");
1335 MockQuicData mock_quic_data
;
1336 mock_quic_data
.AddWrite(
1337 ConstructRequestHeadersPacket(1, kClientDataStreamId1
, true, true,
1338 GetRequestHeaders("GET", "https", "/")));
1339 mock_quic_data
.AddRead(ConstructResponseHeadersPacket(
1340 1, kClientDataStreamId1
, false, false, GetResponseHeaders("200 OK")));
1341 mock_quic_data
.AddRead(
1342 ConstructDataPacket(2, kClientDataStreamId1
, false, true, 0, "hello!"));
1343 mock_quic_data
.AddWrite(ConstructAckPacket(2, 1));
1344 mock_quic_data
.AddRead(SYNCHRONOUS
, 0);
1345 mock_quic_data
.AddSocketDataToFactory(&socket_factory_
);
1347 request_
.url
= GURL("https://www.example.org:443");
1348 AddHangingNonAlternateProtocolSocketData();
1349 CreateSessionWithNextProtos();
1350 AddQuicAlternateProtocolMapping(MockCryptoClientStream::CONFIRM_HANDSHAKE
);
1351 scoped_ptr
<HttpNetworkTransaction
> trans(
1352 new HttpNetworkTransaction(DEFAULT_PRIORITY
, session_
.get()));
1353 TestCompletionCallback callback
;
1354 int rv
= trans
->Start(&request_
, callback
.callback(), net_log_
.bound());
1355 EXPECT_EQ(ERR_REQUEST_FOR_SECURE_RESOURCE_OVER_INSECURE_QUIC
,
1356 callback
.GetResult(rv
));
1359 TEST_P(QuicNetworkTransactionTest
, SecureResourceOverSecureQuic
) {
1360 maker_
.set_hostname("www.example.org");
1361 MockQuicData mock_quic_data
;
1362 mock_quic_data
.AddWrite(
1363 ConstructRequestHeadersPacket(1, kClientDataStreamId1
, true, true,
1364 GetRequestHeaders("GET", "https", "/")));
1365 mock_quic_data
.AddRead(ConstructResponseHeadersPacket(
1366 1, kClientDataStreamId1
, false, false, GetResponseHeaders("200 OK")));
1367 mock_quic_data
.AddRead(
1368 ConstructDataPacket(2, kClientDataStreamId1
, false, true, 0, "hello!"));
1369 mock_quic_data
.AddWrite(ConstructAckPacket(2, 1));
1370 mock_quic_data
.AddRead(SYNCHRONOUS
, 0);
1371 mock_quic_data
.AddSocketDataToFactory(&socket_factory_
);
1373 scoped_refptr
<X509Certificate
> cert(
1374 ImportCertFromFile(GetTestCertsDirectory(), "spdy_pooling.pem"));
1375 ASSERT_TRUE(cert
.get());
1376 bool common_name_fallback_used
;
1378 cert
->VerifyNameMatch("www.example.org", &common_name_fallback_used
));
1379 ProofVerifyDetailsChromium verify_details
;
1380 verify_details
.cert_verify_result
.verified_cert
= cert
;
1381 crypto_client_stream_factory_
.AddProofVerifyDetails(&verify_details
);
1383 request_
.url
= GURL("https://www.example.org:443");
1384 AddHangingNonAlternateProtocolSocketData();
1385 CreateSessionWithNextProtos();
1386 AddQuicAlternateProtocolMapping(MockCryptoClientStream::CONFIRM_HANDSHAKE
);
1387 SendRequestAndExpectQuicResponse("hello!");