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 AlternativeServiceVector alternative_service_vector
=
335 http_server_properties_
.GetAlternativeServices(origin
);
336 EXPECT_EQ(1u, alternative_service_vector
.size());
337 EXPECT_TRUE(http_server_properties_
.IsAlternativeServiceBroken(
338 alternative_service_vector
[0]));
341 void ExpectQuicAlternateProtocolMapping() {
342 const HostPortPair origin
= HostPortPair::FromURL(request_
.url
);
343 const AlternativeServiceVector alternative_service_vector
=
344 http_server_properties_
.GetAlternativeServices(origin
);
345 EXPECT_EQ(1u, alternative_service_vector
.size());
346 EXPECT_EQ(QUIC
, alternative_service_vector
[0].protocol
);
349 void AddHangingNonAlternateProtocolSocketData() {
350 MockConnect
hanging_connect(SYNCHRONOUS
, ERR_IO_PENDING
);
351 hanging_data_
.set_connect_data(hanging_connect
);
352 socket_factory_
.AddSocketDataProvider(&hanging_data_
);
355 MockClock
* clock_
; // Owned by QuicStreamFactory after CreateSession.
356 QuicTestPacketMaker maker_
;
357 scoped_refptr
<HttpNetworkSession
> session_
;
358 MockClientSocketFactory socket_factory_
;
359 MockCryptoClientStreamFactory crypto_client_stream_factory_
;
360 MockHostResolver host_resolver_
;
361 MockCertVerifier cert_verifier_
;
362 TransportSecurityState transport_security_state_
;
363 scoped_refptr
<SSLConfigServiceDefaults
> ssl_config_service_
;
364 scoped_ptr
<ProxyService
> proxy_service_
;
365 scoped_ptr
<HttpAuthHandlerFactory
> auth_handler_factory_
;
366 MockRandom random_generator_
;
367 HttpServerPropertiesImpl http_server_properties_
;
368 HttpNetworkSession::Params params_
;
369 HttpRequestInfo request_
;
370 BoundTestNetLog net_log_
;
371 StaticSocketDataProvider hanging_data_
;
374 void SendRequestAndExpectQuicResponseMaybeFromProxy(
375 const std::string
& expected
,
378 scoped_ptr
<HttpNetworkTransaction
> trans(
379 new HttpNetworkTransaction(DEFAULT_PRIORITY
, session_
.get()));
380 ProxyHeadersHandler proxy_headers_handler
;
381 trans
->SetBeforeProxyHeadersSentCallback(
382 base::Bind(&ProxyHeadersHandler::OnBeforeProxyHeadersSent
,
383 base::Unretained(&proxy_headers_handler
)));
384 RunTransaction(trans
);
385 CheckWasQuicResponse(trans
);
386 CheckResponsePort(trans
, port
);
387 CheckResponseData(trans
, expected
);
388 EXPECT_EQ(used_proxy
, proxy_headers_handler
.was_called());
392 INSTANTIATE_TEST_CASE_P(Version
, QuicNetworkTransactionTest
,
393 ::testing::ValuesIn(QuicSupportedVersions()));
395 TEST_P(QuicNetworkTransactionTest
, ForceQuic
) {
396 params_
.origin_to_force_quic_on
=
397 HostPortPair::FromString("www.google.com:80");
399 MockQuicData mock_quic_data
;
400 mock_quic_data
.AddWrite(
401 ConstructRequestHeadersPacket(1, kClientDataStreamId1
, true, true,
402 GetRequestHeaders("GET", "http", "/")));
403 mock_quic_data
.AddRead(
404 ConstructResponseHeadersPacket(1, kClientDataStreamId1
, false, false,
405 GetResponseHeaders("200 OK")));
406 mock_quic_data
.AddRead(
407 ConstructDataPacket(2, kClientDataStreamId1
, false, true, 0, "hello!"));
408 mock_quic_data
.AddWrite(ConstructAckPacket(2, 1));
409 mock_quic_data
.AddRead(SYNCHRONOUS
, 0); // EOF
411 mock_quic_data
.AddSocketDataToFactory(&socket_factory_
);
413 // The non-alternate protocol job needs to hang in order to guarantee that
414 // the alternate-protocol job will "win".
415 AddHangingNonAlternateProtocolSocketData();
419 SendRequestAndExpectQuicResponse("hello!");
421 // Check that the NetLog was filled reasonably.
422 TestNetLogEntry::List entries
;
423 net_log_
.GetEntries(&entries
);
424 EXPECT_LT(0u, entries
.size());
426 // Check that we logged a QUIC_SESSION_PACKET_RECEIVED.
427 int pos
= ExpectLogContainsSomewhere(
428 entries
, 0, NetLog::TYPE_QUIC_SESSION_PACKET_RECEIVED
,
432 // ... and also a TYPE_QUIC_SESSION_PACKET_HEADER_RECEIVED.
433 pos
= ExpectLogContainsSomewhere(
434 entries
, 0, NetLog::TYPE_QUIC_SESSION_PACKET_HEADER_RECEIVED
,
438 std::string packet_sequence_number
;
439 ASSERT_TRUE(entries
[pos
].GetStringValue(
440 "packet_sequence_number", &packet_sequence_number
));
441 EXPECT_EQ("1", packet_sequence_number
);
443 // ... and also a QUIC_SESSION_STREAM_FRAME_RECEIVED.
444 pos
= ExpectLogContainsSomewhere(
445 entries
, 0, NetLog::TYPE_QUIC_SESSION_STREAM_FRAME_RECEIVED
,
450 ASSERT_TRUE(entries
[pos
].GetIntegerValue("stream_id", &log_stream_id
));
451 EXPECT_EQ(3, log_stream_id
);
454 TEST_P(QuicNetworkTransactionTest
, QuicProxy
) {
455 params_
.enable_quic_for_proxies
= true;
456 proxy_service_
.reset(
457 ProxyService::CreateFixedFromPacResult("QUIC myproxy:70"));
459 MockQuicData mock_quic_data
;
460 mock_quic_data
.AddWrite(
461 ConstructRequestHeadersPacket(1, kClientDataStreamId1
, true, true,
462 GetRequestHeaders("GET", "http", "/")));
463 mock_quic_data
.AddRead(
464 ConstructResponseHeadersPacket(1, kClientDataStreamId1
, false, false,
465 GetResponseHeaders("200 OK")));
466 mock_quic_data
.AddRead(
467 ConstructDataPacket(2, kClientDataStreamId1
, false, true, 0, "hello!"));
468 mock_quic_data
.AddWrite(ConstructAckPacket(2, 1));
469 mock_quic_data
.AddRead(SYNCHRONOUS
, 0); // EOF
471 mock_quic_data
.AddSocketDataToFactory(&socket_factory_
);
473 // There is no need to set up an alternate protocol job, because
474 // no attempt will be made to speak to the proxy over TCP.
478 SendRequestAndExpectQuicResponseFromProxyOnPort("hello!", 70);
481 // Regression test for https://crbug.com/492458. Test that for an HTTP
482 // connection through a QUIC proxy, the certificate exhibited by the proxy is
483 // checked against the proxy hostname, not the origin hostname.
484 TEST_P(QuicNetworkTransactionTest
, QuicProxyWithCert
) {
485 const std::string origin_host
= "news.example.com";
486 const std::string proxy_host
= "www.example.org";
488 params_
.enable_quic_for_proxies
= true;
489 proxy_service_
.reset(
490 ProxyService::CreateFixedFromPacResult("QUIC " + proxy_host
+ ":70"));
492 maker_
.set_hostname(origin_host
);
493 MockQuicData mock_quic_data
;
494 mock_quic_data
.AddWrite(
495 ConstructRequestHeadersPacket(1, kClientDataStreamId1
, true, true,
496 GetRequestHeaders("GET", "http", "/")));
497 mock_quic_data
.AddRead(ConstructResponseHeadersPacket(
498 1, kClientDataStreamId1
, false, false, GetResponseHeaders("200 OK")));
499 mock_quic_data
.AddRead(
500 ConstructDataPacket(2, kClientDataStreamId1
, false, true, 0, "hello!"));
501 mock_quic_data
.AddWrite(ConstructAckPacket(2, 1));
502 mock_quic_data
.AddRead(SYNCHRONOUS
, 0);
503 mock_quic_data
.AddSocketDataToFactory(&socket_factory_
);
505 scoped_refptr
<X509Certificate
> cert(
506 ImportCertFromFile(GetTestCertsDirectory(), "spdy_pooling.pem"));
507 ASSERT_TRUE(cert
.get());
508 // This certificate is valid for the proxy, but not for the origin.
509 bool common_name_fallback_used
;
510 EXPECT_TRUE(cert
->VerifyNameMatch(proxy_host
, &common_name_fallback_used
));
511 EXPECT_FALSE(cert
->VerifyNameMatch(origin_host
, &common_name_fallback_used
));
512 ProofVerifyDetailsChromium verify_details
;
513 verify_details
.cert_verify_result
.verified_cert
= cert
;
514 crypto_client_stream_factory_
.AddProofVerifyDetails(&verify_details
);
516 request_
.url
= GURL("http://" + origin_host
);
517 AddHangingNonAlternateProtocolSocketData();
518 CreateSessionWithNextProtos();
519 AddQuicAlternateProtocolMapping(MockCryptoClientStream::CONFIRM_HANDSHAKE
);
520 SendRequestAndExpectQuicResponseFromProxyOnPort("hello!", 70);
523 TEST_P(QuicNetworkTransactionTest
, ForceQuicWithErrorConnecting
) {
524 params_
.origin_to_force_quic_on
=
525 HostPortPair::FromString("www.google.com:80");
527 MockQuicData mock_quic_data
;
528 mock_quic_data
.AddRead(ASYNC
, ERR_SOCKET_NOT_CONNECTED
);
530 mock_quic_data
.AddSocketDataToFactory(&socket_factory_
);
534 scoped_ptr
<HttpNetworkTransaction
> trans(
535 new HttpNetworkTransaction(DEFAULT_PRIORITY
, session_
.get()));
536 TestCompletionCallback callback
;
537 int rv
= trans
->Start(&request_
, callback
.callback(), net_log_
.bound());
538 EXPECT_EQ(ERR_IO_PENDING
, rv
);
539 EXPECT_EQ(ERR_CONNECTION_CLOSED
, callback
.WaitForResult());
542 TEST_P(QuicNetworkTransactionTest
, DoNotForceQuicForHttps
) {
543 // Attempt to "force" quic on 443, which will not be honored.
544 params_
.origin_to_force_quic_on
=
545 HostPortPair::FromString("www.google.com:443");
547 MockRead http_reads
[] = {
548 MockRead("HTTP/1.1 200 OK\r\n\r\n"),
549 MockRead("hello world"),
550 MockRead(SYNCHRONOUS
, ERR_TEST_PEER_CLOSE_AFTER_NEXT_MOCK_READ
),
554 StaticSocketDataProvider
data(http_reads
, arraysize(http_reads
), nullptr, 0);
555 socket_factory_
.AddSocketDataProvider(&data
);
556 SSLSocketDataProvider
ssl(ASYNC
, OK
);
557 socket_factory_
.AddSSLSocketDataProvider(&ssl
);
561 SendRequestAndExpectHttpResponse("hello world");
564 TEST_P(QuicNetworkTransactionTest
, UseAlternateProtocolForQuic
) {
565 MockRead http_reads
[] = {
566 MockRead("HTTP/1.1 200 OK\r\n"),
567 MockRead(kQuicAlternateProtocolHttpHeader
),
568 MockRead("hello world"),
569 MockRead(SYNCHRONOUS
, ERR_TEST_PEER_CLOSE_AFTER_NEXT_MOCK_READ
),
573 StaticSocketDataProvider
http_data(http_reads
, arraysize(http_reads
),
575 socket_factory_
.AddSocketDataProvider(&http_data
);
577 MockQuicData mock_quic_data
;
578 mock_quic_data
.AddWrite(
579 ConstructRequestHeadersPacket(1, kClientDataStreamId1
, true, true,
580 GetRequestHeaders("GET", "http", "/")));
581 mock_quic_data
.AddRead(
582 ConstructResponseHeadersPacket(1, kClientDataStreamId1
, false, false,
583 GetResponseHeaders("200 OK")));
584 mock_quic_data
.AddRead(
585 ConstructDataPacket(2, kClientDataStreamId1
, false, true, 0, "hello!"));
586 mock_quic_data
.AddWrite(ConstructAckPacket(2, 1));
587 mock_quic_data
.AddRead(SYNCHRONOUS
, 0); // EOF
589 mock_quic_data
.AddSocketDataToFactory(&socket_factory_
);
591 // The non-alternate protocol job needs to hang in order to guarantee that
592 // the alternate-protocol job will "win".
593 AddHangingNonAlternateProtocolSocketData();
595 CreateSessionWithNextProtos();
597 SendRequestAndExpectHttpResponse("hello world");
598 SendRequestAndExpectQuicResponse("hello!");
601 TEST_P(QuicNetworkTransactionTest
, AlternateProtocolDifferentPort
) {
602 MockRead http_reads
[] = {
603 MockRead("HTTP/1.1 200 OK\r\n"),
604 MockRead(kQuicAlternateProtocolDifferentPortHttpHeader
),
605 MockRead("hello world"),
606 MockRead(SYNCHRONOUS
, ERR_TEST_PEER_CLOSE_AFTER_NEXT_MOCK_READ
),
607 MockRead(ASYNC
, OK
)};
609 StaticSocketDataProvider
http_data(http_reads
, arraysize(http_reads
), nullptr,
611 socket_factory_
.AddSocketDataProvider(&http_data
);
613 MockQuicData mock_quic_data
;
614 mock_quic_data
.AddWrite(
615 ConstructRequestHeadersPacket(1, kClientDataStreamId1
, true, true,
616 GetRequestHeaders("GET", "http", "/")));
617 mock_quic_data
.AddRead(ConstructResponseHeadersPacket(
618 1, kClientDataStreamId1
, false, false, GetResponseHeaders("200 OK")));
619 mock_quic_data
.AddRead(
620 ConstructDataPacket(2, kClientDataStreamId1
, false, true, 0, "hello!"));
621 mock_quic_data
.AddWrite(ConstructAckPacket(2, 1));
622 mock_quic_data
.AddRead(SYNCHRONOUS
, 0); // EOF
624 mock_quic_data
.AddSocketDataToFactory(&socket_factory_
);
626 // The non-alternate protocol job needs to hang in order to guarantee that
627 // the alternate-protocol job will "win".
628 AddHangingNonAlternateProtocolSocketData();
630 CreateSessionWithNextProtos();
632 SendRequestAndExpectHttpResponse("hello world");
633 SendRequestAndExpectQuicResponseOnPort("hello!", 137);
636 TEST_P(QuicNetworkTransactionTest
, ConfirmAlternativeService
) {
637 MockRead http_reads
[] = {
638 MockRead("HTTP/1.1 200 OK\r\n"),
639 MockRead(kQuicAlternateProtocolHttpHeader
),
640 MockRead("hello world"),
641 MockRead(SYNCHRONOUS
, ERR_TEST_PEER_CLOSE_AFTER_NEXT_MOCK_READ
),
642 MockRead(ASYNC
, OK
)};
644 StaticSocketDataProvider
http_data(http_reads
, arraysize(http_reads
), nullptr,
646 socket_factory_
.AddSocketDataProvider(&http_data
);
648 MockQuicData mock_quic_data
;
649 mock_quic_data
.AddWrite(
650 ConstructRequestHeadersPacket(1, kClientDataStreamId1
, true, true,
651 GetRequestHeaders("GET", "http", "/")));
652 mock_quic_data
.AddRead(ConstructResponseHeadersPacket(
653 1, kClientDataStreamId1
, false, false, GetResponseHeaders("200 OK")));
654 mock_quic_data
.AddRead(
655 ConstructDataPacket(2, kClientDataStreamId1
, false, true, 0, "hello!"));
656 mock_quic_data
.AddWrite(ConstructAckPacket(2, 1));
657 mock_quic_data
.AddRead(SYNCHRONOUS
, 0); // EOF
659 mock_quic_data
.AddSocketDataToFactory(&socket_factory_
);
661 // The non-alternate protocol job needs to hang in order to guarantee that
662 // the alternate-protocol job will "win".
663 AddHangingNonAlternateProtocolSocketData();
665 CreateSessionWithNextProtos();
667 AlternativeService
alternative_service(QUIC
,
668 HostPortPair::FromURL(request_
.url
));
669 http_server_properties_
.MarkAlternativeServiceRecentlyBroken(
670 alternative_service
);
671 EXPECT_TRUE(http_server_properties_
.WasAlternativeServiceRecentlyBroken(
672 alternative_service
));
674 SendRequestAndExpectHttpResponse("hello world");
675 SendRequestAndExpectQuicResponse("hello!");
677 EXPECT_FALSE(http_server_properties_
.WasAlternativeServiceRecentlyBroken(
678 alternative_service
));
681 TEST_P(QuicNetworkTransactionTest
, UseAlternateProtocolProbabilityForQuic
) {
682 MockRead http_reads
[] = {
683 MockRead("HTTP/1.1 200 OK\r\n"),
684 MockRead(kQuicAlternateProtocol50pctHttpHeader
),
685 MockRead("hello world"),
686 MockRead(SYNCHRONOUS
, ERR_TEST_PEER_CLOSE_AFTER_NEXT_MOCK_READ
),
690 StaticSocketDataProvider
http_data(http_reads
, arraysize(http_reads
),
692 socket_factory_
.AddSocketDataProvider(&http_data
);
694 MockQuicData mock_quic_data
;
695 mock_quic_data
.AddWrite(
696 ConstructRequestHeadersPacket(1, kClientDataStreamId1
, true, true,
697 GetRequestHeaders("GET", "http", "/")));
698 mock_quic_data
.AddRead(
699 ConstructResponseHeadersPacket(1, kClientDataStreamId1
, false, false,
700 GetResponseHeaders("200 OK")));
701 mock_quic_data
.AddRead(
702 ConstructDataPacket(2, kClientDataStreamId1
, false, true, 0, "hello!"));
703 mock_quic_data
.AddWrite(ConstructAckPacket(2, 1));
704 mock_quic_data
.AddRead(SYNCHRONOUS
, 0); // EOF
706 mock_quic_data
.AddSocketDataToFactory(&socket_factory_
);
708 // The non-alternate protocol job needs to hang in order to guarantee that
709 // the alternate-protocol job will "win".
710 AddHangingNonAlternateProtocolSocketData();
712 params_
.alternative_service_probability_threshold
= .25;
713 CreateSessionWithNextProtos();
715 SendRequestAndExpectHttpResponse("hello world");
716 SendRequestAndExpectQuicResponse("hello!");
719 TEST_P(QuicNetworkTransactionTest
, DontUseAlternateProtocolProbabilityForQuic
) {
720 MockRead http_reads
[] = {
721 MockRead("HTTP/1.1 200 OK\r\n"),
722 MockRead(kQuicAlternateProtocol50pctHttpHeader
),
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
, DontUseAlternateProtocolForInsecureQuic
) {
741 MockRead http_reads
[] = {MockRead("HTTP/1.1 200 OK\r\n"),
742 MockRead("Content-length: 11\r\n"),
743 MockRead("Alternate-Protocol: 443:quic\r\n\r\n"),
744 MockRead("hello world"),
745 MockRead("HTTP/1.1 200 OK\r\n"),
746 MockRead("Content-length: 11\r\n"),
747 MockRead("Alternate-Protocol: 443:quic\r\n\r\n"),
748 MockRead("hello world"),
749 MockRead(ASYNC
, OK
)};
751 StaticSocketDataProvider
http_data(http_reads
, arraysize(http_reads
), nullptr,
753 socket_factory_
.AddSocketDataProvider(&http_data
);
754 socket_factory_
.AddSocketDataProvider(&http_data
);
756 params_
.disable_insecure_quic
= true;
757 CreateSessionWithNextProtos();
759 SendRequestAndExpectHttpResponse("hello world");
760 SendRequestAndExpectHttpResponse("hello world");
763 TEST_P(QuicNetworkTransactionTest
,
764 DontUseAlternateProtocolWithBadProbabilityForQuic
) {
765 MockRead http_reads
[] = {
766 MockRead("HTTP/1.1 200 OK\r\n"),
767 MockRead("Alternate-Protocol: 443:quic,p=2\r\n\r\n"),
768 MockRead("hello world"),
769 MockRead(SYNCHRONOUS
, ERR_TEST_PEER_CLOSE_AFTER_NEXT_MOCK_READ
),
773 StaticSocketDataProvider
http_data(http_reads
, arraysize(http_reads
),
775 socket_factory_
.AddSocketDataProvider(&http_data
);
776 socket_factory_
.AddSocketDataProvider(&http_data
);
778 params_
.alternative_service_probability_threshold
= .75;
779 CreateSessionWithNextProtos();
781 SendRequestAndExpectHttpResponse("hello world");
782 SendRequestAndExpectHttpResponse("hello world");
785 TEST_P(QuicNetworkTransactionTest
, UseAlternateProtocolForQuicForHttps
) {
786 params_
.origin_to_force_quic_on
=
787 HostPortPair::FromString("www.google.com:443");
789 MockRead http_reads
[] = {
790 MockRead("HTTP/1.1 200 OK\r\n"),
791 MockRead(kQuicAlternateProtocolHttpsHeader
),
792 MockRead("hello world"),
793 MockRead(SYNCHRONOUS
, ERR_TEST_PEER_CLOSE_AFTER_NEXT_MOCK_READ
),
797 StaticSocketDataProvider
http_data(http_reads
, arraysize(http_reads
),
799 socket_factory_
.AddSocketDataProvider(&http_data
);
801 MockQuicData mock_quic_data
;
802 mock_quic_data
.AddWrite(
803 ConstructRequestHeadersPacket(1, kClientDataStreamId1
, true, true,
804 GetRequestHeaders("GET", "http", "/")));
805 mock_quic_data
.AddRead(
806 ConstructResponseHeadersPacket(1, kClientDataStreamId1
, false, false,
807 GetResponseHeaders("200 OK")));
808 mock_quic_data
.AddRead(
809 ConstructDataPacket(2, kClientDataStreamId1
, false, true, 0, "hello!"));
810 mock_quic_data
.AddWrite(ConstructAckPacket(2, 1));
811 mock_quic_data
.AddRead(SYNCHRONOUS
, 0); // EOF
813 mock_quic_data
.AddSocketDataToFactory(&socket_factory_
);
815 // The non-alternate protocol job needs to hang in order to guarantee that
816 // the alternate-protocol job will "win".
817 AddHangingNonAlternateProtocolSocketData();
819 CreateSessionWithNextProtos();
821 // TODO(rtenneti): Test QUIC over HTTPS, GetSSLInfo().
822 SendRequestAndExpectHttpResponse("hello world");
825 class QuicAltSvcCertificateVerificationTest
826 : public QuicNetworkTransactionTest
{
828 void Run(bool valid
) {
829 HostPortPair
origin(valid
? "mail.example.org" : "invalid.example.org",
831 HostPortPair
alternative("www.example.org", 443);
832 std::string
url("https://");
833 url
.append(origin
.host());
835 request_
.url
= GURL(url
);
837 maker_
.set_hostname(origin
.host());
838 MockQuicData mock_quic_data
;
839 mock_quic_data
.AddWrite(
840 ConstructRequestHeadersPacket(1, kClientDataStreamId1
, true, true,
841 GetRequestHeaders("GET", "https", "/")));
842 mock_quic_data
.AddRead(ConstructResponseHeadersPacket(
843 1, kClientDataStreamId1
, false, false, GetResponseHeaders("200 OK")));
844 mock_quic_data
.AddRead(
845 ConstructDataPacket(2, kClientDataStreamId1
, false, true, 0, "hello!"));
846 mock_quic_data
.AddWrite(ConstructAckPacket(2, 1));
847 mock_quic_data
.AddRead(SYNCHRONOUS
, 0);
848 mock_quic_data
.AddSocketDataToFactory(&socket_factory_
);
850 scoped_refptr
<X509Certificate
> cert(
851 ImportCertFromFile(GetTestCertsDirectory(), "spdy_pooling.pem"));
852 ASSERT_TRUE(cert
.get());
853 bool common_name_fallback_used
;
855 cert
->VerifyNameMatch(origin
.host(), &common_name_fallback_used
));
857 cert
->VerifyNameMatch(alternative
.host(), &common_name_fallback_used
));
858 ProofVerifyDetailsChromium verify_details
;
859 verify_details
.cert_verify_result
.verified_cert
= cert
;
860 verify_details
.cert_verify_result
.is_issued_by_known_root
= true;
861 crypto_client_stream_factory_
.AddProofVerifyDetails(&verify_details
);
862 crypto_client_stream_factory_
.set_handshake_mode(
863 MockCryptoClientStream::CONFIRM_HANDSHAKE
);
865 // Connection to |origin| fails, so that success of |request| depends on
866 // connection to |alternate| only.
867 MockConnect
refused_connect(ASYNC
, ERR_CONNECTION_REFUSED
);
868 StaticSocketDataProvider refused_data
;
869 refused_data
.set_connect_data(refused_connect
);
870 socket_factory_
.AddSocketDataProvider(&refused_data
);
872 CreateSessionWithNextProtos();
873 AlternativeService
alternative_service(QUIC
, alternative
);
874 session_
->http_server_properties()->SetAlternativeService(
875 origin
, alternative_service
, 1.0);
876 scoped_ptr
<HttpNetworkTransaction
> trans(
877 new HttpNetworkTransaction(DEFAULT_PRIORITY
, session_
.get()));
878 TestCompletionCallback callback
;
879 int rv
= trans
->Start(&request_
, callback
.callback(), net_log_
.bound());
880 EXPECT_EQ(ERR_IO_PENDING
, rv
);
881 rv
= callback
.WaitForResult();
884 CheckWasQuicResponse(trans
);
885 CheckResponsePort(trans
, 443);
886 CheckResponseData(trans
, "hello!");
888 EXPECT_EQ(ERR_CONNECTION_REFUSED
, rv
);
893 INSTANTIATE_TEST_CASE_P(Version
,
894 QuicAltSvcCertificateVerificationTest
,
895 ::testing::ValuesIn(QuicSupportedVersions()));
897 TEST_P(QuicAltSvcCertificateVerificationTest
,
898 RequestSucceedsWithValidCertificate
) {
902 TEST_P(QuicAltSvcCertificateVerificationTest
,
903 RequestFailsWithInvalidCertificate
) {
907 TEST_P(QuicNetworkTransactionTest
, HungAlternateProtocol
) {
908 crypto_client_stream_factory_
.set_handshake_mode(
909 MockCryptoClientStream::COLD_START
);
911 MockWrite http_writes
[] = {
912 MockWrite(SYNCHRONOUS
, 0, "GET / HTTP/1.1\r\n"),
913 MockWrite(SYNCHRONOUS
, 1, "Host: www.google.com\r\n"),
914 MockWrite(SYNCHRONOUS
, 2, "Connection: keep-alive\r\n\r\n")
917 MockRead http_reads
[] = {
918 MockRead(SYNCHRONOUS
, 3, "HTTP/1.1 200 OK\r\n"),
919 MockRead(SYNCHRONOUS
, 4, kQuicAlternateProtocolHttpHeader
),
920 MockRead(SYNCHRONOUS
, 5, "hello world"),
921 MockRead(SYNCHRONOUS
, OK
, 6)
924 DeterministicMockClientSocketFactory socket_factory
;
926 DeterministicSocketData
http_data(http_reads
, arraysize(http_reads
),
927 http_writes
, arraysize(http_writes
));
928 socket_factory
.AddSocketDataProvider(&http_data
);
930 // The QUIC transaction will not be allowed to complete.
931 MockWrite quic_writes
[] = {
932 MockWrite(ASYNC
, ERR_IO_PENDING
, 0)
934 MockRead quic_reads
[] = {
935 MockRead(ASYNC
, ERR_IO_PENDING
, 1),
937 DeterministicSocketData
quic_data(quic_reads
, arraysize(quic_reads
),
938 quic_writes
, arraysize(quic_writes
));
939 socket_factory
.AddSocketDataProvider(&quic_data
);
941 // The HTTP transaction will complete.
942 DeterministicSocketData
http_data2(http_reads
, arraysize(http_reads
),
943 http_writes
, arraysize(http_writes
));
944 socket_factory
.AddSocketDataProvider(&http_data2
);
946 CreateSessionWithFactory(&socket_factory
, true);
948 // Run the first request.
949 http_data
.StopAfter(arraysize(http_reads
) + arraysize(http_writes
));
950 SendRequestAndExpectHttpResponse("hello world");
951 ASSERT_TRUE(http_data
.AllReadDataConsumed());
952 ASSERT_TRUE(http_data
.AllWriteDataConsumed());
954 // Now run the second request in which the QUIC socket hangs,
955 // and verify the the transaction continues over HTTP.
956 http_data2
.StopAfter(arraysize(http_reads
) + arraysize(http_writes
));
957 SendRequestAndExpectHttpResponse("hello world");
959 ASSERT_TRUE(http_data2
.AllReadDataConsumed());
960 ASSERT_TRUE(http_data2
.AllWriteDataConsumed());
961 ASSERT_TRUE(!quic_data
.AllReadDataConsumed());
962 ASSERT_TRUE(!quic_data
.AllWriteDataConsumed());
965 TEST_P(QuicNetworkTransactionTest
, ZeroRTTWithHttpRace
) {
966 MockQuicData mock_quic_data
;
967 mock_quic_data
.AddWrite(
968 ConstructRequestHeadersPacket(1, kClientDataStreamId1
, true, true,
969 GetRequestHeaders("GET", "http", "/")));
970 mock_quic_data
.AddRead(
971 ConstructResponseHeadersPacket(1, kClientDataStreamId1
, false, false,
972 GetResponseHeaders("200 OK")));
973 mock_quic_data
.AddRead(
974 ConstructDataPacket(2, kClientDataStreamId1
, false, true, 0, "hello!"));
975 mock_quic_data
.AddWrite(ConstructAckPacket(2, 1));
976 mock_quic_data
.AddRead(SYNCHRONOUS
, 0); // EOF
978 mock_quic_data
.AddSocketDataToFactory(&socket_factory_
);
980 // The non-alternate protocol job needs to hang in order to guarantee that
981 // the alternate-protocol job will "win".
982 AddHangingNonAlternateProtocolSocketData();
984 CreateSessionWithNextProtos();
985 AddQuicAlternateProtocolMapping(MockCryptoClientStream::ZERO_RTT
);
986 SendRequestAndExpectQuicResponse("hello!");
989 TEST_P(QuicNetworkTransactionTest
, ZeroRTTWithNoHttpRace
) {
990 MockQuicData mock_quic_data
;
991 mock_quic_data
.AddWrite(
992 ConstructRequestHeadersPacket(1, kClientDataStreamId1
, true, true,
993 GetRequestHeaders("GET", "http", "/")));
994 mock_quic_data
.AddRead(
995 ConstructResponseHeadersPacket(1, kClientDataStreamId1
, false, false,
996 GetResponseHeaders("200 OK")));
997 mock_quic_data
.AddRead(
998 ConstructDataPacket(2, kClientDataStreamId1
, false, true, 0, "hello!"));
999 mock_quic_data
.AddWrite(ConstructAckPacket(2, 1));
1000 mock_quic_data
.AddRead(SYNCHRONOUS
, 0); // EOF
1001 mock_quic_data
.AddSocketDataToFactory(&socket_factory_
);
1003 // In order for a new QUIC session to be established via alternate-protocol
1004 // without racing an HTTP connection, we need the host resolution to happen
1006 host_resolver_
.set_synchronous_mode(true);
1007 host_resolver_
.rules()->AddIPLiteralRule("www.google.com", "192.168.0.1", "");
1008 HostResolver::RequestInfo
info(HostPortPair("www.google.com", 80));
1009 AddressList address
;
1010 host_resolver_
.Resolve(info
,
1013 CompletionCallback(),
1017 CreateSessionWithNextProtos();
1018 AddQuicAlternateProtocolMapping(MockCryptoClientStream::ZERO_RTT
);
1019 SendRequestAndExpectQuicResponse("hello!");
1022 TEST_P(QuicNetworkTransactionTest
, ZeroRTTWithProxy
) {
1023 proxy_service_
.reset(
1024 ProxyService::CreateFixedFromPacResult("PROXY myproxy:70"));
1026 // Since we are using a proxy, the QUIC job will not succeed.
1027 MockWrite http_writes
[] = {
1028 MockWrite(SYNCHRONOUS
, 0, "GET http://www.google.com/ HTTP/1.1\r\n"),
1029 MockWrite(SYNCHRONOUS
, 1, "Host: www.google.com\r\n"),
1030 MockWrite(SYNCHRONOUS
, 2, "Proxy-Connection: keep-alive\r\n\r\n")
1033 MockRead http_reads
[] = {
1034 MockRead(SYNCHRONOUS
, 3, "HTTP/1.1 200 OK\r\n"),
1035 MockRead(SYNCHRONOUS
, 4, kQuicAlternateProtocolHttpHeader
),
1036 MockRead(SYNCHRONOUS
, 5, "hello world"),
1037 MockRead(SYNCHRONOUS
, OK
, 6)
1040 StaticSocketDataProvider
http_data(http_reads
, arraysize(http_reads
),
1041 http_writes
, arraysize(http_writes
));
1042 socket_factory_
.AddSocketDataProvider(&http_data
);
1044 // In order for a new QUIC session to be established via alternate-protocol
1045 // without racing an HTTP connection, we need the host resolution to happen
1047 host_resolver_
.set_synchronous_mode(true);
1048 host_resolver_
.rules()->AddIPLiteralRule("www.google.com", "192.168.0.1", "");
1049 HostResolver::RequestInfo
info(HostPortPair("www.google.com", 80));
1050 AddressList address
;
1051 host_resolver_
.Resolve(info
,
1054 CompletionCallback(),
1058 CreateSessionWithNextProtos();
1059 AddQuicAlternateProtocolMapping(MockCryptoClientStream::ZERO_RTT
);
1060 SendRequestAndExpectHttpResponse("hello world");
1063 TEST_P(QuicNetworkTransactionTest
, ZeroRTTWithConfirmationRequired
) {
1064 MockQuicData mock_quic_data
;
1065 mock_quic_data
.AddWrite(
1066 ConstructRequestHeadersPacket(1, kClientDataStreamId1
, true, true,
1067 GetRequestHeaders("GET", "http", "/")));
1068 mock_quic_data
.AddRead(
1069 ConstructResponseHeadersPacket(1, kClientDataStreamId1
, false, false,
1070 GetResponseHeaders("200 OK")));
1071 mock_quic_data
.AddRead(
1072 ConstructDataPacket(2, kClientDataStreamId1
, false, true, 0, "hello!"));
1073 mock_quic_data
.AddWrite(ConstructAckPacket(2, 1));
1074 mock_quic_data
.AddRead(SYNCHRONOUS
, 0); // EOF
1075 mock_quic_data
.AddSocketDataToFactory(&socket_factory_
);
1077 // The non-alternate protocol job needs to hang in order to guarantee that
1078 // the alternate-protocol job will "win".
1079 AddHangingNonAlternateProtocolSocketData();
1081 // In order for a new QUIC session to be established via alternate-protocol
1082 // without racing an HTTP connection, we need the host resolution to happen
1083 // synchronously. Of course, even though QUIC *could* perform a 0-RTT
1084 // connection to the the server, in this test we require confirmation
1085 // before encrypting so the HTTP job will still start.
1086 host_resolver_
.set_synchronous_mode(true);
1087 host_resolver_
.rules()->AddIPLiteralRule("www.google.com", "192.168.0.1", "");
1088 HostResolver::RequestInfo
info(HostPortPair("www.google.com", 80));
1089 AddressList address
;
1090 host_resolver_
.Resolve(info
, DEFAULT_PRIORITY
, &address
,
1091 CompletionCallback(), nullptr, net_log_
.bound());
1093 CreateSessionWithNextProtos();
1094 session_
->quic_stream_factory()->set_require_confirmation(true);
1095 AddQuicAlternateProtocolMapping(MockCryptoClientStream::ZERO_RTT
);
1097 scoped_ptr
<HttpNetworkTransaction
> trans(
1098 new HttpNetworkTransaction(DEFAULT_PRIORITY
, session_
.get()));
1099 TestCompletionCallback callback
;
1100 int rv
= trans
->Start(&request_
, callback
.callback(), net_log_
.bound());
1101 EXPECT_EQ(ERR_IO_PENDING
, rv
);
1103 crypto_client_stream_factory_
.last_stream()->SendOnCryptoHandshakeEvent(
1104 QuicSession::HANDSHAKE_CONFIRMED
);
1105 EXPECT_EQ(OK
, callback
.WaitForResult());
1108 TEST_P(QuicNetworkTransactionTest
, BrokenAlternateProtocol
) {
1109 // Alternate-protocol job
1110 scoped_ptr
<QuicEncryptedPacket
> close(ConstructConnectionClosePacket(1));
1111 MockRead quic_reads
[] = {
1112 MockRead(ASYNC
, close
->data(), close
->length()),
1113 MockRead(ASYNC
, OK
), // EOF
1115 StaticSocketDataProvider
quic_data(quic_reads
, arraysize(quic_reads
),
1117 socket_factory_
.AddSocketDataProvider(&quic_data
);
1119 // Main job which will succeed even though the alternate job fails.
1120 MockRead http_reads
[] = {
1121 MockRead("HTTP/1.1 200 OK\r\n\r\n"),
1122 MockRead("hello from http"),
1123 MockRead(SYNCHRONOUS
, ERR_TEST_PEER_CLOSE_AFTER_NEXT_MOCK_READ
),
1127 StaticSocketDataProvider
http_data(http_reads
, arraysize(http_reads
),
1129 socket_factory_
.AddSocketDataProvider(&http_data
);
1131 CreateSessionWithNextProtos();
1132 AddQuicAlternateProtocolMapping(MockCryptoClientStream::COLD_START
);
1133 SendRequestAndExpectHttpResponse("hello from http");
1134 ExpectBrokenAlternateProtocolMapping();
1137 TEST_P(QuicNetworkTransactionTest
, BrokenAlternateProtocolReadError
) {
1138 // Alternate-protocol job
1139 MockRead quic_reads
[] = {
1140 MockRead(ASYNC
, ERR_SOCKET_NOT_CONNECTED
),
1142 StaticSocketDataProvider
quic_data(quic_reads
, arraysize(quic_reads
),
1144 socket_factory_
.AddSocketDataProvider(&quic_data
);
1146 // Main job which will succeed even though the alternate job fails.
1147 MockRead http_reads
[] = {
1148 MockRead("HTTP/1.1 200 OK\r\n\r\n"),
1149 MockRead("hello from http"),
1150 MockRead(SYNCHRONOUS
, ERR_TEST_PEER_CLOSE_AFTER_NEXT_MOCK_READ
),
1154 StaticSocketDataProvider
http_data(http_reads
, arraysize(http_reads
),
1156 socket_factory_
.AddSocketDataProvider(&http_data
);
1158 CreateSessionWithNextProtos();
1160 AddQuicAlternateProtocolMapping(MockCryptoClientStream::COLD_START
);
1161 SendRequestAndExpectHttpResponse("hello from http");
1162 ExpectBrokenAlternateProtocolMapping();
1165 TEST_P(QuicNetworkTransactionTest
, NoBrokenAlternateProtocolIfTcpFails
) {
1166 // Alternate-protocol job will fail when the session attempts to read.
1167 MockRead quic_reads
[] = {
1168 MockRead(ASYNC
, ERR_SOCKET_NOT_CONNECTED
),
1170 StaticSocketDataProvider
quic_data(quic_reads
, arraysize(quic_reads
),
1172 socket_factory_
.AddSocketDataProvider(&quic_data
);
1174 // Main job will also fail.
1175 MockRead http_reads
[] = {
1176 MockRead(ASYNC
, ERR_SOCKET_NOT_CONNECTED
),
1179 StaticSocketDataProvider
http_data(http_reads
, arraysize(http_reads
),
1181 http_data
.set_connect_data(MockConnect(ASYNC
, ERR_SOCKET_NOT_CONNECTED
));
1182 socket_factory_
.AddSocketDataProvider(&http_data
);
1184 CreateSessionWithNextProtos();
1186 AddQuicAlternateProtocolMapping(MockCryptoClientStream::COLD_START
);
1187 scoped_ptr
<HttpNetworkTransaction
> trans(
1188 new HttpNetworkTransaction(DEFAULT_PRIORITY
, session_
.get()));
1189 TestCompletionCallback callback
;
1190 int rv
= trans
->Start(&request_
, callback
.callback(), net_log_
.bound());
1191 EXPECT_EQ(ERR_IO_PENDING
, rv
);
1192 EXPECT_EQ(ERR_SOCKET_NOT_CONNECTED
, callback
.WaitForResult());
1193 ExpectQuicAlternateProtocolMapping();
1196 TEST_P(QuicNetworkTransactionTest
, FailedZeroRttBrokenAlternateProtocol
) {
1197 // Alternate-protocol job
1198 MockRead quic_reads
[] = {
1199 MockRead(ASYNC
, ERR_SOCKET_NOT_CONNECTED
),
1201 StaticSocketDataProvider
quic_data(quic_reads
, arraysize(quic_reads
),
1203 socket_factory_
.AddSocketDataProvider(&quic_data
);
1205 AddHangingNonAlternateProtocolSocketData();
1207 // Second Alternate-protocol job which will race with the TCP job.
1208 StaticSocketDataProvider
quic_data2(quic_reads
, arraysize(quic_reads
),
1210 socket_factory_
.AddSocketDataProvider(&quic_data2
);
1212 // Final job that will proceed when the QUIC job fails.
1213 MockRead http_reads
[] = {
1214 MockRead("HTTP/1.1 200 OK\r\n\r\n"),
1215 MockRead("hello from http"),
1216 MockRead(SYNCHRONOUS
, ERR_TEST_PEER_CLOSE_AFTER_NEXT_MOCK_READ
),
1220 StaticSocketDataProvider
http_data(http_reads
, arraysize(http_reads
),
1222 socket_factory_
.AddSocketDataProvider(&http_data
);
1224 CreateSessionWithNextProtos();
1226 AddQuicAlternateProtocolMapping(MockCryptoClientStream::ZERO_RTT
);
1228 SendRequestAndExpectHttpResponse("hello from http");
1230 ExpectBrokenAlternateProtocolMapping();
1232 EXPECT_TRUE(quic_data
.AllReadDataConsumed());
1233 EXPECT_TRUE(quic_data
.AllWriteDataConsumed());
1236 TEST_P(QuicNetworkTransactionTest
, DISABLED_HangingZeroRttFallback
) {
1237 // Alternate-protocol job
1238 MockRead quic_reads
[] = {
1239 MockRead(ASYNC
, ERR_IO_PENDING
),
1241 StaticSocketDataProvider
quic_data(quic_reads
, arraysize(quic_reads
),
1243 socket_factory_
.AddSocketDataProvider(&quic_data
);
1245 // Main job that will proceed when the QUIC job fails.
1246 MockRead http_reads
[] = {
1247 MockRead("HTTP/1.1 200 OK\r\n\r\n"),
1248 MockRead("hello from http"),
1249 MockRead(SYNCHRONOUS
, ERR_TEST_PEER_CLOSE_AFTER_NEXT_MOCK_READ
),
1253 StaticSocketDataProvider
http_data(http_reads
, arraysize(http_reads
),
1255 socket_factory_
.AddSocketDataProvider(&http_data
);
1257 CreateSessionWithNextProtos();
1259 AddQuicAlternateProtocolMapping(MockCryptoClientStream::ZERO_RTT
);
1261 SendRequestAndExpectHttpResponse("hello from http");
1264 TEST_P(QuicNetworkTransactionTest
, BrokenAlternateProtocolOnConnectFailure
) {
1265 // Alternate-protocol job will fail before creating a QUIC session.
1266 StaticSocketDataProvider
quic_data(nullptr, 0, nullptr, 0);
1267 quic_data
.set_connect_data(MockConnect(SYNCHRONOUS
,
1268 ERR_INTERNET_DISCONNECTED
));
1269 socket_factory_
.AddSocketDataProvider(&quic_data
);
1271 // Main job which will succeed even though the alternate job fails.
1272 MockRead http_reads
[] = {
1273 MockRead("HTTP/1.1 200 OK\r\n\r\n"),
1274 MockRead("hello from http"),
1275 MockRead(SYNCHRONOUS
, ERR_TEST_PEER_CLOSE_AFTER_NEXT_MOCK_READ
),
1279 StaticSocketDataProvider
http_data(http_reads
, arraysize(http_reads
),
1281 socket_factory_
.AddSocketDataProvider(&http_data
);
1283 CreateSessionWithNextProtos();
1284 AddQuicAlternateProtocolMapping(MockCryptoClientStream::COLD_START
);
1285 SendRequestAndExpectHttpResponse("hello from http");
1287 ExpectBrokenAlternateProtocolMapping();
1290 TEST_P(QuicNetworkTransactionTest
, ConnectionCloseDuringConnect
) {
1291 MockQuicData mock_quic_data
;
1292 mock_quic_data
.AddSynchronousRead(ConstructConnectionClosePacket(1));
1293 mock_quic_data
.AddWrite(
1294 ConstructRequestHeadersPacket(1, kClientDataStreamId1
, true, true,
1295 GetRequestHeaders("GET", "http", "/")));
1296 mock_quic_data
.AddWrite(ConstructAckPacket(2, 1));
1297 mock_quic_data
.AddSocketDataToFactory(&socket_factory_
);
1299 // When the QUIC connection fails, we will try the request again over HTTP.
1300 MockRead http_reads
[] = {
1301 MockRead("HTTP/1.1 200 OK\r\n"),
1302 MockRead(kQuicAlternateProtocolHttpHeader
),
1303 MockRead("hello world"),
1304 MockRead(SYNCHRONOUS
, ERR_TEST_PEER_CLOSE_AFTER_NEXT_MOCK_READ
),
1308 StaticSocketDataProvider
http_data(http_reads
, arraysize(http_reads
),
1310 socket_factory_
.AddSocketDataProvider(&http_data
);
1312 // In order for a new QUIC session to be established via alternate-protocol
1313 // without racing an HTTP connection, we need the host resolution to happen
1315 host_resolver_
.set_synchronous_mode(true);
1316 host_resolver_
.rules()->AddIPLiteralRule("www.google.com", "192.168.0.1", "");
1317 HostResolver::RequestInfo
info(HostPortPair("www.google.com", 80));
1318 AddressList address
;
1319 host_resolver_
.Resolve(info
,
1322 CompletionCallback(),
1326 CreateSessionWithNextProtos();
1327 AddQuicAlternateProtocolMapping(MockCryptoClientStream::ZERO_RTT
);
1328 SendRequestAndExpectHttpResponse("hello world");
1331 // Test that a secure request over an insecure QUIC connection fails with
1332 // the appropriate error code. Note that this never happens in production,
1333 // because the handshake (which this test mocks) would fail in this scenario.
1334 TEST_P(QuicNetworkTransactionTest
, SecureResourceOverInsecureQuic
) {
1335 maker_
.set_hostname("www.example.org");
1336 MockQuicData mock_quic_data
;
1337 mock_quic_data
.AddWrite(
1338 ConstructRequestHeadersPacket(1, kClientDataStreamId1
, true, true,
1339 GetRequestHeaders("GET", "https", "/")));
1340 mock_quic_data
.AddRead(ConstructResponseHeadersPacket(
1341 1, kClientDataStreamId1
, false, false, GetResponseHeaders("200 OK")));
1342 mock_quic_data
.AddRead(
1343 ConstructDataPacket(2, kClientDataStreamId1
, false, true, 0, "hello!"));
1344 mock_quic_data
.AddWrite(ConstructAckPacket(2, 1));
1345 mock_quic_data
.AddRead(SYNCHRONOUS
, 0);
1346 mock_quic_data
.AddSocketDataToFactory(&socket_factory_
);
1348 request_
.url
= GURL("https://www.example.org:443");
1349 AddHangingNonAlternateProtocolSocketData();
1350 CreateSessionWithNextProtos();
1351 AddQuicAlternateProtocolMapping(MockCryptoClientStream::CONFIRM_HANDSHAKE
);
1352 scoped_ptr
<HttpNetworkTransaction
> trans(
1353 new HttpNetworkTransaction(DEFAULT_PRIORITY
, session_
.get()));
1354 TestCompletionCallback callback
;
1355 int rv
= trans
->Start(&request_
, callback
.callback(), net_log_
.bound());
1356 EXPECT_EQ(ERR_REQUEST_FOR_SECURE_RESOURCE_OVER_INSECURE_QUIC
,
1357 callback
.GetResult(rv
));
1360 TEST_P(QuicNetworkTransactionTest
, SecureResourceOverSecureQuic
) {
1361 maker_
.set_hostname("www.example.org");
1362 MockQuicData mock_quic_data
;
1363 mock_quic_data
.AddWrite(
1364 ConstructRequestHeadersPacket(1, kClientDataStreamId1
, true, true,
1365 GetRequestHeaders("GET", "https", "/")));
1366 mock_quic_data
.AddRead(ConstructResponseHeadersPacket(
1367 1, kClientDataStreamId1
, false, false, GetResponseHeaders("200 OK")));
1368 mock_quic_data
.AddRead(
1369 ConstructDataPacket(2, kClientDataStreamId1
, false, true, 0, "hello!"));
1370 mock_quic_data
.AddWrite(ConstructAckPacket(2, 1));
1371 mock_quic_data
.AddRead(SYNCHRONOUS
, 0);
1372 mock_quic_data
.AddSocketDataToFactory(&socket_factory_
);
1374 scoped_refptr
<X509Certificate
> cert(
1375 ImportCertFromFile(GetTestCertsDirectory(), "spdy_pooling.pem"));
1376 ASSERT_TRUE(cert
.get());
1377 bool common_name_fallback_used
;
1379 cert
->VerifyNameMatch("www.example.org", &common_name_fallback_used
));
1380 ProofVerifyDetailsChromium verify_details
;
1381 verify_details
.cert_verify_result
.verified_cert
= cert
;
1382 crypto_client_stream_factory_
.AddProofVerifyDetails(&verify_details
);
1384 request_
.url
= GURL("https://www.example.org:443");
1385 AddHangingNonAlternateProtocolSocketData();
1386 CreateSessionWithNextProtos();
1387 AddQuicAlternateProtocolMapping(MockCryptoClientStream::CONFIRM_HANDSHAKE
);
1388 SendRequestAndExpectQuicResponse("hello!");