Switch global error menu icon to vectorized MD asset
[chromium-blink-merge.git] / net / quic / quic_network_transaction_unittest.cc
blobffd19182b53a8668766cab7988031ff9a92c49e7
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include <vector>
7 #include "base/basictypes.h"
8 #include "base/compiler_specific.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/stl_util.h"
11 #include "net/base/test_completion_callback.h"
12 #include "net/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"
51 namespace net {
52 namespace test {
54 namespace {
56 static const char kQuicAlternateProtocolHttpHeader[] =
57 "Alternate-Protocol: 80:quic\r\n\r\n";
58 static const char kQuicAlternateProtocol50pctHttpHeader[] =
59 "Alternate-Protocol: 80:quic,p=.5\r\n\r\n";
60 static const char kQuicAlternateProtocolDifferentPortHttpHeader[] =
61 "Alternate-Protocol: 137:quic\r\n\r\n";
62 static const char kQuicAlternateProtocolHttpsHeader[] =
63 "Alternate-Protocol: 443:quic\r\n\r\n";
64 static const char kQuicAlternativeServiceHttpHeader[] =
65 "Alt-Svc: quic=\":80\"\r\n\r\n";
66 static const char kQuicAlternativeService50pctHttpHeader[] =
67 "Alt-Svc: quic=\":80\";p=.5\r\n\r\n";
68 static const char kQuicAlternativeServiceDifferentPortHttpHeader[] =
69 "Alt-Svc: quic=\":137\"\r\n\r\n";
70 static const char kQuicAlternativeServiceHttpsHeader[] =
71 "Alt-Svc: quic=\":443\"\r\n\r\n";
73 const char kDefaultServerHostName[] = "www.google.com";
75 } // namespace
77 // Helper class to encapsulate MockReads and MockWrites for QUIC.
78 // Simplify ownership issues and the interaction with the MockSocketFactory.
79 class MockQuicData {
80 public:
81 MockQuicData() : packet_number_(0) {}
83 ~MockQuicData() {
84 STLDeleteElements(&packets_);
87 void AddSynchronousRead(scoped_ptr<QuicEncryptedPacket> packet) {
88 reads_.push_back(MockRead(SYNCHRONOUS, packet->data(), packet->length(),
89 packet_number_++));
90 packets_.push_back(packet.release());
93 void AddRead(scoped_ptr<QuicEncryptedPacket> packet) {
94 reads_.push_back(
95 MockRead(ASYNC, packet->data(), packet->length(), packet_number_++));
96 packets_.push_back(packet.release());
99 void AddRead(IoMode mode, int rv) {
100 reads_.push_back(MockRead(mode, rv, packet_number_++));
103 void AddWrite(scoped_ptr<QuicEncryptedPacket> packet) {
104 writes_.push_back(MockWrite(SYNCHRONOUS, packet->data(), packet->length(),
105 packet_number_++));
106 packets_.push_back(packet.release());
109 void AddSocketDataToFactory(MockClientSocketFactory* factory) {
110 MockRead* reads = reads_.empty() ? nullptr : &reads_[0];
111 MockWrite* writes = writes_.empty() ? nullptr : &writes_[0];
112 socket_data_.reset(
113 new SequencedSocketData(reads, reads_.size(), writes, writes_.size()));
114 factory->AddSocketDataProvider(socket_data_.get());
117 void CompleteRead() { socket_data_->CompleteRead(); }
119 private:
120 std::vector<QuicEncryptedPacket*> packets_;
121 std::vector<MockWrite> writes_;
122 std::vector<MockRead> reads_;
123 size_t packet_number_;
124 scoped_ptr<SequencedSocketData> socket_data_;
127 class ProxyHeadersHandler {
128 public:
129 ProxyHeadersHandler() : was_called_(false) {}
131 bool was_called() { return was_called_; }
133 void OnBeforeProxyHeadersSent(const ProxyInfo& proxy_info,
134 HttpRequestHeaders* request_headers) {
135 was_called_ = true;
138 private:
139 bool was_called_;
142 class QuicNetworkTransactionTest
143 : public PlatformTest,
144 public ::testing::WithParamInterface<QuicVersion> {
145 protected:
146 QuicNetworkTransactionTest()
147 : clock_(new MockClock),
148 maker_(GetParam(), 0, clock_, kDefaultServerHostName),
149 ssl_config_service_(new SSLConfigServiceDefaults),
150 proxy_service_(ProxyService::CreateDirect()),
151 auth_handler_factory_(
152 HttpAuthHandlerFactory::CreateDefault(&host_resolver_)),
153 random_generator_(0),
154 hanging_data_(nullptr, 0, nullptr, 0) {
155 request_.method = "GET";
156 std::string url("http://");
157 url.append(kDefaultServerHostName);
158 request_.url = GURL(url);
159 request_.load_flags = 0;
160 clock_->AdvanceTime(QuicTime::Delta::FromMilliseconds(20));
163 void SetUp() override {
164 NetworkChangeNotifier::NotifyObserversOfIPAddressChangeForTests();
165 base::MessageLoop::current()->RunUntilIdle();
168 void TearDown() override {
169 NetworkChangeNotifier::NotifyObserversOfIPAddressChangeForTests();
170 // Empty the current queue.
171 base::MessageLoop::current()->RunUntilIdle();
172 PlatformTest::TearDown();
173 NetworkChangeNotifier::NotifyObserversOfIPAddressChangeForTests();
174 base::MessageLoop::current()->RunUntilIdle();
177 scoped_ptr<QuicEncryptedPacket> ConstructConnectionClosePacket(
178 QuicPacketNumber num) {
179 return maker_.MakeConnectionClosePacket(num);
182 scoped_ptr<QuicEncryptedPacket> ConstructAckPacket(
183 QuicPacketNumber largest_received,
184 QuicPacketNumber least_unacked) {
185 return maker_.MakeAckPacket(2, largest_received, least_unacked, true);
188 SpdyHeaderBlock GetRequestHeaders(const std::string& method,
189 const std::string& scheme,
190 const std::string& path) {
191 return maker_.GetRequestHeaders(method, scheme, path);
194 SpdyHeaderBlock GetResponseHeaders(const std::string& status) {
195 return maker_.GetResponseHeaders(status);
198 scoped_ptr<QuicEncryptedPacket> ConstructDataPacket(
199 QuicPacketNumber packet_number,
200 QuicStreamId stream_id,
201 bool should_include_version,
202 bool fin,
203 QuicStreamOffset offset,
204 base::StringPiece data) {
205 return maker_.MakeDataPacket(packet_number, stream_id,
206 should_include_version, fin, offset, data);
209 scoped_ptr<QuicEncryptedPacket> ConstructRequestHeadersPacket(
210 QuicPacketNumber packet_number,
211 QuicStreamId stream_id,
212 bool should_include_version,
213 bool fin,
214 const SpdyHeaderBlock& headers) {
215 QuicPriority priority =
216 ConvertRequestPriorityToQuicPriority(DEFAULT_PRIORITY);
217 return maker_.MakeRequestHeadersPacket(packet_number, stream_id,
218 should_include_version, fin,
219 priority, headers);
222 scoped_ptr<QuicEncryptedPacket> ConstructResponseHeadersPacket(
223 QuicPacketNumber packet_number,
224 QuicStreamId stream_id,
225 bool should_include_version,
226 bool fin,
227 const SpdyHeaderBlock& headers) {
228 return maker_.MakeResponseHeadersPacket(
229 packet_number, stream_id, should_include_version, fin, headers);
232 void CreateSession() {
233 CreateSessionWithFactory(&socket_factory_, false);
236 void CreateSessionWithNextProtos() {
237 CreateSessionWithFactory(&socket_factory_, true);
240 // If |use_next_protos| is true, enables SPDY and QUIC.
241 void CreateSessionWithFactory(ClientSocketFactory* socket_factory,
242 bool use_next_protos) {
243 params_.enable_quic = true;
244 params_.quic_clock = clock_;
245 params_.quic_random = &random_generator_;
246 params_.client_socket_factory = socket_factory;
247 params_.quic_crypto_client_stream_factory = &crypto_client_stream_factory_;
248 params_.host_resolver = &host_resolver_;
249 params_.cert_verifier = &cert_verifier_;
250 params_.transport_security_state = &transport_security_state_;
251 params_.proxy_service = proxy_service_.get();
252 params_.ssl_config_service = ssl_config_service_.get();
253 params_.http_auth_handler_factory = auth_handler_factory_.get();
254 params_.http_server_properties = http_server_properties_.GetWeakPtr();
255 params_.quic_supported_versions = SupportedVersions(GetParam());
257 if (use_next_protos) {
258 params_.use_alternative_services = true;
259 params_.next_protos = NextProtosWithSpdyAndQuic(true, true);
262 session_ = new HttpNetworkSession(params_);
263 session_->quic_stream_factory()->set_require_confirmation(false);
264 ASSERT_EQ(params_.quic_socket_receive_buffer_size,
265 session_->quic_stream_factory()->socket_receive_buffer_size());
268 void CheckWasQuicResponse(const scoped_ptr<HttpNetworkTransaction>& trans) {
269 const HttpResponseInfo* response = trans->GetResponseInfo();
270 ASSERT_TRUE(response != nullptr);
271 ASSERT_TRUE(response->headers.get() != nullptr);
272 EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine());
273 EXPECT_TRUE(response->was_fetched_via_spdy);
274 EXPECT_TRUE(response->was_npn_negotiated);
275 EXPECT_EQ(HttpResponseInfo::CONNECTION_INFO_QUIC1_SPDY3,
276 response->connection_info);
279 void CheckResponsePort(const scoped_ptr<HttpNetworkTransaction>& trans,
280 uint16 port) {
281 const HttpResponseInfo* response = trans->GetResponseInfo();
282 ASSERT_TRUE(response != nullptr);
283 EXPECT_EQ(port, response->socket_address.port());
286 void CheckWasHttpResponse(const scoped_ptr<HttpNetworkTransaction>& trans) {
287 const HttpResponseInfo* response = trans->GetResponseInfo();
288 ASSERT_TRUE(response != nullptr);
289 ASSERT_TRUE(response->headers.get() != nullptr);
290 EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine());
291 EXPECT_FALSE(response->was_fetched_via_spdy);
292 EXPECT_FALSE(response->was_npn_negotiated);
293 EXPECT_EQ(HttpResponseInfo::CONNECTION_INFO_HTTP1,
294 response->connection_info);
297 void CheckResponseData(const scoped_ptr<HttpNetworkTransaction>& trans,
298 const std::string& expected) {
299 std::string response_data;
300 ASSERT_EQ(OK, ReadTransaction(trans.get(), &response_data));
301 EXPECT_EQ(expected, response_data);
304 void RunTransaction(const scoped_ptr<HttpNetworkTransaction>& trans) {
305 TestCompletionCallback callback;
306 int rv = trans->Start(&request_, callback.callback(), net_log_.bound());
307 EXPECT_EQ(ERR_IO_PENDING, rv);
308 EXPECT_EQ(OK, callback.WaitForResult());
311 void SendRequestAndExpectHttpResponse(const std::string& expected) {
312 scoped_ptr<HttpNetworkTransaction> trans(
313 new HttpNetworkTransaction(DEFAULT_PRIORITY, session_.get()));
314 RunTransaction(trans);
315 CheckWasHttpResponse(trans);
316 CheckResponseData(trans, expected);
319 void SendRequestAndExpectQuicResponse(const std::string& expected) {
320 SendRequestAndExpectQuicResponseMaybeFromProxy(expected, false, 80);
323 void SendRequestAndExpectQuicResponseOnPort(const std::string& expected,
324 uint16 port) {
325 SendRequestAndExpectQuicResponseMaybeFromProxy(expected, false, port);
328 void SendRequestAndExpectQuicResponseFromProxyOnPort(
329 const std::string& expected,
330 uint16 port) {
331 SendRequestAndExpectQuicResponseMaybeFromProxy(expected, true, port);
334 void AddQuicAlternateProtocolMapping(
335 MockCryptoClientStream::HandshakeMode handshake_mode) {
336 crypto_client_stream_factory_.set_handshake_mode(handshake_mode);
337 HostPortPair host_port_pair = HostPortPair::FromURL(request_.url);
338 AlternativeService alternative_service(QUIC, host_port_pair.host(), 80);
339 base::Time expiration = base::Time::Now() + base::TimeDelta::FromDays(1);
340 http_server_properties_.SetAlternativeService(
341 host_port_pair, alternative_service, 1.0, expiration);
344 void ExpectBrokenAlternateProtocolMapping() {
345 const HostPortPair origin = HostPortPair::FromURL(request_.url);
346 const AlternativeServiceVector alternative_service_vector =
347 http_server_properties_.GetAlternativeServices(origin);
348 EXPECT_EQ(1u, alternative_service_vector.size());
349 EXPECT_TRUE(http_server_properties_.IsAlternativeServiceBroken(
350 alternative_service_vector[0]));
353 void ExpectQuicAlternateProtocolMapping() {
354 const HostPortPair origin = HostPortPair::FromURL(request_.url);
355 const AlternativeServiceVector alternative_service_vector =
356 http_server_properties_.GetAlternativeServices(origin);
357 EXPECT_EQ(1u, alternative_service_vector.size());
358 EXPECT_EQ(QUIC, alternative_service_vector[0].protocol);
361 void AddHangingNonAlternateProtocolSocketData() {
362 MockConnect hanging_connect(SYNCHRONOUS, ERR_IO_PENDING);
363 hanging_data_.set_connect_data(hanging_connect);
364 socket_factory_.AddSocketDataProvider(&hanging_data_);
367 MockClock* clock_; // Owned by QuicStreamFactory after CreateSession.
368 QuicTestPacketMaker maker_;
369 scoped_refptr<HttpNetworkSession> session_;
370 MockClientSocketFactory socket_factory_;
371 MockCryptoClientStreamFactory crypto_client_stream_factory_;
372 MockHostResolver host_resolver_;
373 MockCertVerifier cert_verifier_;
374 TransportSecurityState transport_security_state_;
375 scoped_refptr<SSLConfigServiceDefaults> ssl_config_service_;
376 scoped_ptr<ProxyService> proxy_service_;
377 scoped_ptr<HttpAuthHandlerFactory> auth_handler_factory_;
378 MockRandom random_generator_;
379 HttpServerPropertiesImpl http_server_properties_;
380 HttpNetworkSession::Params params_;
381 HttpRequestInfo request_;
382 BoundTestNetLog net_log_;
383 StaticSocketDataProvider hanging_data_;
385 private:
386 void SendRequestAndExpectQuicResponseMaybeFromProxy(
387 const std::string& expected,
388 bool used_proxy,
389 uint16 port) {
390 scoped_ptr<HttpNetworkTransaction> trans(
391 new HttpNetworkTransaction(DEFAULT_PRIORITY, session_.get()));
392 ProxyHeadersHandler proxy_headers_handler;
393 trans->SetBeforeProxyHeadersSentCallback(
394 base::Bind(&ProxyHeadersHandler::OnBeforeProxyHeadersSent,
395 base::Unretained(&proxy_headers_handler)));
396 RunTransaction(trans);
397 CheckWasQuicResponse(trans);
398 CheckResponsePort(trans, port);
399 CheckResponseData(trans, expected);
400 EXPECT_EQ(used_proxy, proxy_headers_handler.was_called());
404 INSTANTIATE_TEST_CASE_P(Version, QuicNetworkTransactionTest,
405 ::testing::ValuesIn(QuicSupportedVersions()));
407 TEST_P(QuicNetworkTransactionTest, ForceQuic) {
408 // TODO(rch): switch these tests to use secure QUIC.
409 params_.enable_insecure_quic = true;
410 params_.origin_to_force_quic_on =
411 HostPortPair::FromString("www.google.com:80");
413 MockQuicData mock_quic_data;
414 mock_quic_data.AddWrite(
415 ConstructRequestHeadersPacket(1, kClientDataStreamId1, true, true,
416 GetRequestHeaders("GET", "http", "/")));
417 mock_quic_data.AddRead(
418 ConstructResponseHeadersPacket(1, kClientDataStreamId1, false, false,
419 GetResponseHeaders("200 OK")));
420 mock_quic_data.AddRead(
421 ConstructDataPacket(2, kClientDataStreamId1, false, true, 0, "hello!"));
422 mock_quic_data.AddWrite(ConstructAckPacket(2, 1));
423 mock_quic_data.AddRead(ASYNC, ERR_IO_PENDING); // No more data to read
425 mock_quic_data.AddSocketDataToFactory(&socket_factory_);
427 // The non-alternate protocol job needs to hang in order to guarantee that
428 // the alternate-protocol job will "win".
429 AddHangingNonAlternateProtocolSocketData();
431 CreateSession();
433 SendRequestAndExpectQuicResponse("hello!");
435 // Check that the NetLog was filled reasonably.
436 TestNetLogEntry::List entries;
437 net_log_.GetEntries(&entries);
438 EXPECT_LT(0u, entries.size());
440 // Check that we logged a QUIC_SESSION_PACKET_RECEIVED.
441 int pos = ExpectLogContainsSomewhere(
442 entries, 0, NetLog::TYPE_QUIC_SESSION_PACKET_RECEIVED,
443 NetLog::PHASE_NONE);
444 EXPECT_LT(0, pos);
446 // ... and also a TYPE_QUIC_SESSION_UNAUTHENTICATED_PACKET_HEADER_RECEIVED.
447 pos = ExpectLogContainsSomewhere(
448 entries, 0,
449 NetLog::TYPE_QUIC_SESSION_UNAUTHENTICATED_PACKET_HEADER_RECEIVED,
450 NetLog::PHASE_NONE);
451 EXPECT_LT(0, pos);
453 std::string packet_number;
454 ASSERT_TRUE(entries[pos].GetStringValue("packet_number", &packet_number));
455 EXPECT_EQ("1", packet_number);
457 // ... and also a TYPE_QUIC_SESSION_PACKET_AUTHENTICATED.
458 pos = ExpectLogContainsSomewhere(
459 entries, 0, NetLog::TYPE_QUIC_SESSION_PACKET_AUTHENTICATED,
460 NetLog::PHASE_NONE);
461 EXPECT_LT(0, pos);
463 // ... and also a QUIC_SESSION_STREAM_FRAME_RECEIVED.
464 pos = ExpectLogContainsSomewhere(
465 entries, 0, NetLog::TYPE_QUIC_SESSION_STREAM_FRAME_RECEIVED,
466 NetLog::PHASE_NONE);
467 EXPECT_LT(0, pos);
469 int log_stream_id;
470 ASSERT_TRUE(entries[pos].GetIntegerValue("stream_id", &log_stream_id));
471 EXPECT_EQ(3, log_stream_id);
474 TEST_P(QuicNetworkTransactionTest, QuicProxy) {
475 params_.enable_insecure_quic = true;
476 params_.enable_quic_for_proxies = true;
477 proxy_service_.reset(
478 ProxyService::CreateFixedFromPacResult("QUIC myproxy:70"));
480 MockQuicData mock_quic_data;
481 mock_quic_data.AddWrite(
482 ConstructRequestHeadersPacket(1, kClientDataStreamId1, true, true,
483 GetRequestHeaders("GET", "http", "/")));
484 mock_quic_data.AddRead(
485 ConstructResponseHeadersPacket(1, kClientDataStreamId1, false, false,
486 GetResponseHeaders("200 OK")));
487 mock_quic_data.AddRead(
488 ConstructDataPacket(2, kClientDataStreamId1, false, true, 0, "hello!"));
489 mock_quic_data.AddWrite(ConstructAckPacket(2, 1));
490 mock_quic_data.AddRead(ASYNC, ERR_IO_PENDING); // No more data to read
491 mock_quic_data.AddRead(SYNCHRONOUS, 0); // EOF
493 mock_quic_data.AddSocketDataToFactory(&socket_factory_);
495 // There is no need to set up an alternate protocol job, because
496 // no attempt will be made to speak to the proxy over TCP.
498 CreateSession();
500 SendRequestAndExpectQuicResponseFromProxyOnPort("hello!", 70);
503 // Regression test for https://crbug.com/492458. Test that for an HTTP
504 // connection through a QUIC proxy, the certificate exhibited by the proxy is
505 // checked against the proxy hostname, not the origin hostname.
506 TEST_P(QuicNetworkTransactionTest, QuicProxyWithCert) {
507 const std::string origin_host = "news.example.com";
508 const std::string proxy_host = "www.example.org";
510 params_.enable_insecure_quic = true;
511 params_.enable_quic_for_proxies = true;
512 proxy_service_.reset(
513 ProxyService::CreateFixedFromPacResult("QUIC " + proxy_host + ":70"));
515 maker_.set_hostname(origin_host);
516 MockQuicData mock_quic_data;
517 mock_quic_data.AddWrite(
518 ConstructRequestHeadersPacket(1, kClientDataStreamId1, true, true,
519 GetRequestHeaders("GET", "http", "/")));
520 mock_quic_data.AddRead(ConstructResponseHeadersPacket(
521 1, kClientDataStreamId1, false, false, GetResponseHeaders("200 OK")));
522 mock_quic_data.AddRead(
523 ConstructDataPacket(2, kClientDataStreamId1, false, true, 0, "hello!"));
524 mock_quic_data.AddWrite(ConstructAckPacket(2, 1));
525 mock_quic_data.AddRead(ASYNC, ERR_IO_PENDING); // No more data to read
526 mock_quic_data.AddRead(SYNCHRONOUS, 0);
527 mock_quic_data.AddSocketDataToFactory(&socket_factory_);
529 scoped_refptr<X509Certificate> cert(
530 ImportCertFromFile(GetTestCertsDirectory(), "spdy_pooling.pem"));
531 ASSERT_TRUE(cert.get());
532 // This certificate is valid for the proxy, but not for the origin.
533 bool common_name_fallback_used;
534 EXPECT_TRUE(cert->VerifyNameMatch(proxy_host, &common_name_fallback_used));
535 EXPECT_FALSE(cert->VerifyNameMatch(origin_host, &common_name_fallback_used));
536 ProofVerifyDetailsChromium verify_details;
537 verify_details.cert_verify_result.verified_cert = cert;
538 crypto_client_stream_factory_.AddProofVerifyDetails(&verify_details);
540 request_.url = GURL("http://" + origin_host);
541 AddHangingNonAlternateProtocolSocketData();
542 CreateSessionWithNextProtos();
543 AddQuicAlternateProtocolMapping(MockCryptoClientStream::CONFIRM_HANDSHAKE);
544 SendRequestAndExpectQuicResponseFromProxyOnPort("hello!", 70);
547 TEST_P(QuicNetworkTransactionTest, ForceQuicWithErrorConnecting) {
548 params_.enable_insecure_quic = true;
549 params_.origin_to_force_quic_on =
550 HostPortPair::FromString("www.google.com:80");
552 MockQuicData mock_quic_data;
553 mock_quic_data.AddRead(ASYNC, ERR_SOCKET_NOT_CONNECTED);
555 mock_quic_data.AddSocketDataToFactory(&socket_factory_);
557 CreateSession();
559 scoped_ptr<HttpNetworkTransaction> trans(
560 new HttpNetworkTransaction(DEFAULT_PRIORITY, session_.get()));
561 TestCompletionCallback callback;
562 int rv = trans->Start(&request_, callback.callback(), net_log_.bound());
563 EXPECT_EQ(ERR_IO_PENDING, rv);
564 EXPECT_EQ(ERR_CONNECTION_CLOSED, callback.WaitForResult());
567 TEST_P(QuicNetworkTransactionTest, DoNotForceQuicForHttps) {
568 // Attempt to "force" quic on 443, which will not be honored.
569 params_.origin_to_force_quic_on =
570 HostPortPair::FromString("www.google.com:443");
572 MockRead http_reads[] = {
573 MockRead("HTTP/1.1 200 OK\r\n\r\n"),
574 MockRead("hello world"),
575 MockRead(SYNCHRONOUS, ERR_TEST_PEER_CLOSE_AFTER_NEXT_MOCK_READ),
576 MockRead(ASYNC, OK)
579 StaticSocketDataProvider data(http_reads, arraysize(http_reads), nullptr, 0);
580 socket_factory_.AddSocketDataProvider(&data);
581 SSLSocketDataProvider ssl(ASYNC, OK);
582 socket_factory_.AddSSLSocketDataProvider(&ssl);
584 CreateSession();
586 SendRequestAndExpectHttpResponse("hello world");
589 TEST_P(QuicNetworkTransactionTest, UseAlternativeServiceForQuic) {
590 params_.enable_insecure_quic = true;
592 MockRead http_reads[] = {
593 MockRead("HTTP/1.1 200 OK\r\n"),
594 MockRead(kQuicAlternativeServiceHttpHeader), MockRead("hello world"),
595 MockRead(SYNCHRONOUS, ERR_TEST_PEER_CLOSE_AFTER_NEXT_MOCK_READ),
596 MockRead(ASYNC, OK)};
598 StaticSocketDataProvider http_data(http_reads, arraysize(http_reads), nullptr,
600 socket_factory_.AddSocketDataProvider(&http_data);
602 MockQuicData mock_quic_data;
603 mock_quic_data.AddWrite(
604 ConstructRequestHeadersPacket(1, kClientDataStreamId1, true, true,
605 GetRequestHeaders("GET", "http", "/")));
606 mock_quic_data.AddRead(ConstructResponseHeadersPacket(
607 1, kClientDataStreamId1, false, false, GetResponseHeaders("200 OK")));
608 mock_quic_data.AddRead(
609 ConstructDataPacket(2, kClientDataStreamId1, false, true, 0, "hello!"));
610 mock_quic_data.AddWrite(ConstructAckPacket(2, 1));
611 mock_quic_data.AddRead(ASYNC, ERR_IO_PENDING); // No more data to read
612 mock_quic_data.AddRead(SYNCHRONOUS, 0); // EOF
614 mock_quic_data.AddSocketDataToFactory(&socket_factory_);
616 AddHangingNonAlternateProtocolSocketData();
617 CreateSessionWithNextProtos();
619 SendRequestAndExpectHttpResponse("hello world");
620 SendRequestAndExpectQuicResponse("hello!");
623 // When multiple alternative services are advertised,
624 // HttpStreamFactoryImpl::RequestStreamInternal() only passes the first one to
625 // Job. This is what the following test verifies.
626 // TODO(bnc): Update this test when multiple alternative services are handled
627 // properly.
628 TEST_P(QuicNetworkTransactionTest, UseFirstAlternativeServiceForQuic) {
629 params_.enable_insecure_quic = true;
631 MockRead http_reads[] = {
632 MockRead("HTTP/1.1 200 OK\r\n"),
633 MockRead("Alt-Svc: quic=\":443\", quic=\":1234\"\r\n\r\n"),
634 MockRead("hello world"),
635 MockRead(SYNCHRONOUS, ERR_TEST_PEER_CLOSE_AFTER_NEXT_MOCK_READ),
636 MockRead(ASYNC, OK)};
638 StaticSocketDataProvider http_data(http_reads, arraysize(http_reads), nullptr,
640 socket_factory_.AddSocketDataProvider(&http_data);
642 MockQuicData mock_quic_data;
643 mock_quic_data.AddWrite(
644 ConstructRequestHeadersPacket(1, kClientDataStreamId1, true, true,
645 GetRequestHeaders("GET", "http", "/")));
646 mock_quic_data.AddRead(ConstructResponseHeadersPacket(
647 1, kClientDataStreamId1, false, false, GetResponseHeaders("200 OK")));
648 mock_quic_data.AddRead(
649 ConstructDataPacket(2, kClientDataStreamId1, false, true, 0, "hello!"));
650 mock_quic_data.AddWrite(ConstructAckPacket(2, 1));
651 mock_quic_data.AddRead(ASYNC, ERR_IO_PENDING); // No more data to read
652 mock_quic_data.AddRead(SYNCHRONOUS, 0); // EOF
654 mock_quic_data.AddSocketDataToFactory(&socket_factory_);
656 AddHangingNonAlternateProtocolSocketData();
657 CreateSessionWithNextProtos();
659 SendRequestAndExpectHttpResponse("hello world");
660 SendRequestAndExpectQuicResponseOnPort("hello!", 443);
663 TEST_P(QuicNetworkTransactionTest, AlternativeServiceDifferentPort) {
664 params_.enable_insecure_quic = true;
666 MockRead http_reads[] = {
667 MockRead("HTTP/1.1 200 OK\r\n"),
668 MockRead(kQuicAlternativeServiceDifferentPortHttpHeader),
669 MockRead("hello world"),
670 MockRead(SYNCHRONOUS, ERR_TEST_PEER_CLOSE_AFTER_NEXT_MOCK_READ),
671 MockRead(ASYNC, OK)};
673 StaticSocketDataProvider http_data(http_reads, arraysize(http_reads), nullptr,
675 socket_factory_.AddSocketDataProvider(&http_data);
677 MockQuicData mock_quic_data;
678 mock_quic_data.AddWrite(
679 ConstructRequestHeadersPacket(1, kClientDataStreamId1, true, true,
680 GetRequestHeaders("GET", "http", "/")));
681 mock_quic_data.AddRead(ConstructResponseHeadersPacket(
682 1, kClientDataStreamId1, false, false, GetResponseHeaders("200 OK")));
683 mock_quic_data.AddRead(
684 ConstructDataPacket(2, kClientDataStreamId1, false, true, 0, "hello!"));
685 mock_quic_data.AddWrite(ConstructAckPacket(2, 1));
686 mock_quic_data.AddRead(ASYNC, ERR_IO_PENDING); // No more data to read
687 mock_quic_data.AddRead(SYNCHRONOUS, 0); // EOF
689 mock_quic_data.AddSocketDataToFactory(&socket_factory_);
691 AddHangingNonAlternateProtocolSocketData();
692 CreateSessionWithNextProtos();
694 SendRequestAndExpectHttpResponse("hello world");
695 SendRequestAndExpectQuicResponseOnPort("hello!", 137);
698 TEST_P(QuicNetworkTransactionTest, ConfirmAlternativeService) {
699 params_.enable_insecure_quic = true;
701 MockRead http_reads[] = {
702 MockRead("HTTP/1.1 200 OK\r\n"),
703 MockRead(kQuicAlternativeServiceHttpHeader), MockRead("hello world"),
704 MockRead(SYNCHRONOUS, ERR_TEST_PEER_CLOSE_AFTER_NEXT_MOCK_READ),
705 MockRead(ASYNC, OK)};
707 StaticSocketDataProvider http_data(http_reads, arraysize(http_reads), nullptr,
709 socket_factory_.AddSocketDataProvider(&http_data);
711 MockQuicData mock_quic_data;
712 mock_quic_data.AddWrite(
713 ConstructRequestHeadersPacket(1, kClientDataStreamId1, true, true,
714 GetRequestHeaders("GET", "http", "/")));
715 mock_quic_data.AddRead(ConstructResponseHeadersPacket(
716 1, kClientDataStreamId1, false, false, GetResponseHeaders("200 OK")));
717 mock_quic_data.AddRead(
718 ConstructDataPacket(2, kClientDataStreamId1, false, true, 0, "hello!"));
719 mock_quic_data.AddWrite(ConstructAckPacket(2, 1));
720 mock_quic_data.AddRead(ASYNC, ERR_IO_PENDING); // No more data to read
721 mock_quic_data.AddRead(ASYNC, 0); // EOF
722 mock_quic_data.AddRead(SYNCHRONOUS, 0); // EOF
724 mock_quic_data.AddSocketDataToFactory(&socket_factory_);
726 AddHangingNonAlternateProtocolSocketData();
727 CreateSessionWithNextProtos();
729 AlternativeService alternative_service(QUIC,
730 HostPortPair::FromURL(request_.url));
731 http_server_properties_.MarkAlternativeServiceRecentlyBroken(
732 alternative_service);
733 EXPECT_TRUE(http_server_properties_.WasAlternativeServiceRecentlyBroken(
734 alternative_service));
736 SendRequestAndExpectHttpResponse("hello world");
737 SendRequestAndExpectQuicResponse("hello!");
739 mock_quic_data.CompleteRead();
741 EXPECT_FALSE(http_server_properties_.WasAlternativeServiceRecentlyBroken(
742 alternative_service));
745 TEST_P(QuicNetworkTransactionTest, UseAlternativeServiceProbabilityForQuic) {
746 params_.enable_insecure_quic = true;
748 MockRead http_reads[] = {
749 MockRead("HTTP/1.1 200 OK\r\n"),
750 MockRead(kQuicAlternativeService50pctHttpHeader), MockRead("hello world"),
751 MockRead(SYNCHRONOUS, ERR_TEST_PEER_CLOSE_AFTER_NEXT_MOCK_READ),
752 MockRead(ASYNC, OK)};
754 StaticSocketDataProvider http_data(http_reads, arraysize(http_reads), nullptr,
756 socket_factory_.AddSocketDataProvider(&http_data);
758 MockQuicData mock_quic_data;
759 mock_quic_data.AddWrite(
760 ConstructRequestHeadersPacket(1, kClientDataStreamId1, true, true,
761 GetRequestHeaders("GET", "http", "/")));
762 mock_quic_data.AddRead(ConstructResponseHeadersPacket(
763 1, kClientDataStreamId1, false, false, GetResponseHeaders("200 OK")));
764 mock_quic_data.AddRead(
765 ConstructDataPacket(2, kClientDataStreamId1, false, true, 0, "hello!"));
766 mock_quic_data.AddWrite(ConstructAckPacket(2, 1));
767 mock_quic_data.AddRead(ASYNC, ERR_IO_PENDING); // No more data to read
768 mock_quic_data.AddRead(SYNCHRONOUS, 0); // EOF
770 mock_quic_data.AddSocketDataToFactory(&socket_factory_);
772 AddHangingNonAlternateProtocolSocketData();
773 params_.alternative_service_probability_threshold = 0.25;
774 CreateSessionWithNextProtos();
776 SendRequestAndExpectHttpResponse("hello world");
777 SendRequestAndExpectQuicResponse("hello!");
780 TEST_P(QuicNetworkTransactionTest,
781 DontUseAlternativeServiceProbabilityForQuic) {
782 params_.enable_insecure_quic = true;
784 MockRead http_reads[] = {
785 MockRead("HTTP/1.1 200 OK\r\n"),
786 MockRead(kQuicAlternativeService50pctHttpHeader), MockRead("hello world"),
787 MockRead(SYNCHRONOUS, ERR_TEST_PEER_CLOSE_AFTER_NEXT_MOCK_READ),
788 MockRead(ASYNC, OK)};
790 StaticSocketDataProvider http_data(http_reads, arraysize(http_reads), nullptr,
792 socket_factory_.AddSocketDataProvider(&http_data);
793 socket_factory_.AddSocketDataProvider(&http_data);
795 params_.alternative_service_probability_threshold = 0.75;
796 CreateSessionWithNextProtos();
798 SendRequestAndExpectHttpResponse("hello world");
799 SendRequestAndExpectHttpResponse("hello world");
802 TEST_P(QuicNetworkTransactionTest, DontUseAlternativeServiceForInsecureQuic) {
803 params_.enable_insecure_quic = true;
805 MockRead http_reads[] = {MockRead("HTTP/1.1 200 OK\r\n"),
806 MockRead("Content-length: 11\r\n"),
807 MockRead("Alt-Svc: quic=\":443\"\r\n\r\n"),
808 MockRead("hello world"),
809 MockRead("HTTP/1.1 200 OK\r\n"),
810 MockRead("Content-length: 11\r\n"),
811 MockRead("Alt-Svc: quic=\":443\"\r\n\r\n"),
812 MockRead("hello world"),
813 MockRead(ASYNC, OK)};
815 StaticSocketDataProvider http_data(http_reads, arraysize(http_reads), nullptr,
817 socket_factory_.AddSocketDataProvider(&http_data);
818 socket_factory_.AddSocketDataProvider(&http_data);
820 params_.enable_insecure_quic = false;
821 CreateSessionWithNextProtos();
823 SendRequestAndExpectHttpResponse("hello world");
824 SendRequestAndExpectHttpResponse("hello world");
827 TEST_P(QuicNetworkTransactionTest,
828 DontUseAlternativeServiceWithBadProbabilityForQuic) {
829 params_.enable_insecure_quic = true;
831 MockRead http_reads[] = {
832 MockRead("HTTP/1.1 200 OK\r\n"),
833 MockRead("Alt-Svc: quic=\":443\";p=2\r\n\r\n"), MockRead("hello world"),
834 MockRead(SYNCHRONOUS, ERR_TEST_PEER_CLOSE_AFTER_NEXT_MOCK_READ),
835 MockRead(ASYNC, OK)};
837 StaticSocketDataProvider http_data(http_reads, arraysize(http_reads), nullptr,
839 socket_factory_.AddSocketDataProvider(&http_data);
840 socket_factory_.AddSocketDataProvider(&http_data);
842 params_.alternative_service_probability_threshold = 0.75;
843 CreateSessionWithNextProtos();
845 SendRequestAndExpectHttpResponse("hello world");
846 SendRequestAndExpectHttpResponse("hello world");
849 TEST_P(QuicNetworkTransactionTest, UseAlternativeServiceForQuicForHttps) {
850 params_.enable_insecure_quic = true;
851 params_.origin_to_force_quic_on =
852 HostPortPair::FromString("www.google.com:443");
854 MockRead http_reads[] = {
855 MockRead("HTTP/1.1 200 OK\r\n"),
856 MockRead(kQuicAlternativeServiceHttpsHeader), MockRead("hello world"),
857 MockRead(SYNCHRONOUS, ERR_TEST_PEER_CLOSE_AFTER_NEXT_MOCK_READ),
858 MockRead(ASYNC, OK)};
860 StaticSocketDataProvider http_data(http_reads, arraysize(http_reads), nullptr,
862 socket_factory_.AddSocketDataProvider(&http_data);
864 MockQuicData mock_quic_data;
865 mock_quic_data.AddWrite(
866 ConstructRequestHeadersPacket(1, kClientDataStreamId1, true, true,
867 GetRequestHeaders("GET", "http", "/")));
868 mock_quic_data.AddRead(ConstructResponseHeadersPacket(
869 1, kClientDataStreamId1, false, false, GetResponseHeaders("200 OK")));
870 mock_quic_data.AddRead(
871 ConstructDataPacket(2, kClientDataStreamId1, false, true, 0, "hello!"));
872 mock_quic_data.AddWrite(ConstructAckPacket(2, 1));
873 mock_quic_data.AddRead(SYNCHRONOUS, 0); // EOF
875 mock_quic_data.AddSocketDataToFactory(&socket_factory_);
877 AddHangingNonAlternateProtocolSocketData();
878 CreateSessionWithNextProtos();
880 // TODO(rtenneti): Test QUIC over HTTPS, GetSSLInfo().
881 SendRequestAndExpectHttpResponse("hello world");
884 TEST_P(QuicNetworkTransactionTest, UseAlternateProtocolForQuic) {
885 params_.enable_insecure_quic = true;
887 MockRead http_reads[] = {
888 MockRead("HTTP/1.1 200 OK\r\n"),
889 MockRead(kQuicAlternateProtocolHttpHeader),
890 MockRead("hello world"),
891 MockRead(SYNCHRONOUS, ERR_TEST_PEER_CLOSE_AFTER_NEXT_MOCK_READ),
892 MockRead(ASYNC, OK)
895 StaticSocketDataProvider http_data(http_reads, arraysize(http_reads),
896 nullptr, 0);
897 socket_factory_.AddSocketDataProvider(&http_data);
899 MockQuicData mock_quic_data;
900 mock_quic_data.AddWrite(
901 ConstructRequestHeadersPacket(1, kClientDataStreamId1, true, true,
902 GetRequestHeaders("GET", "http", "/")));
903 mock_quic_data.AddRead(
904 ConstructResponseHeadersPacket(1, kClientDataStreamId1, false, false,
905 GetResponseHeaders("200 OK")));
906 mock_quic_data.AddRead(
907 ConstructDataPacket(2, kClientDataStreamId1, false, true, 0, "hello!"));
908 mock_quic_data.AddWrite(ConstructAckPacket(2, 1));
909 mock_quic_data.AddRead(ASYNC, ERR_IO_PENDING); // No more data to read
910 mock_quic_data.AddRead(SYNCHRONOUS, 0); // EOF
912 mock_quic_data.AddSocketDataToFactory(&socket_factory_);
914 // The non-alternate protocol job needs to hang in order to guarantee that
915 // the alternate-protocol job will "win".
916 AddHangingNonAlternateProtocolSocketData();
918 CreateSessionWithNextProtos();
920 SendRequestAndExpectHttpResponse("hello world");
921 SendRequestAndExpectQuicResponse("hello!");
924 TEST_P(QuicNetworkTransactionTest, AlternateProtocolDifferentPort) {
925 params_.enable_insecure_quic = true;
927 MockRead http_reads[] = {
928 MockRead("HTTP/1.1 200 OK\r\n"),
929 MockRead(kQuicAlternateProtocolDifferentPortHttpHeader),
930 MockRead("hello world"),
931 MockRead(SYNCHRONOUS, ERR_TEST_PEER_CLOSE_AFTER_NEXT_MOCK_READ),
932 MockRead(ASYNC, OK)};
934 StaticSocketDataProvider http_data(http_reads, arraysize(http_reads), nullptr,
936 socket_factory_.AddSocketDataProvider(&http_data);
938 MockQuicData mock_quic_data;
939 mock_quic_data.AddWrite(
940 ConstructRequestHeadersPacket(1, kClientDataStreamId1, true, true,
941 GetRequestHeaders("GET", "http", "/")));
942 mock_quic_data.AddRead(ConstructResponseHeadersPacket(
943 1, kClientDataStreamId1, false, false, GetResponseHeaders("200 OK")));
944 mock_quic_data.AddRead(
945 ConstructDataPacket(2, kClientDataStreamId1, false, true, 0, "hello!"));
946 mock_quic_data.AddWrite(ConstructAckPacket(2, 1));
947 mock_quic_data.AddRead(ASYNC, ERR_IO_PENDING); // No more data to read
948 mock_quic_data.AddRead(SYNCHRONOUS, 0); // EOF
950 mock_quic_data.AddSocketDataToFactory(&socket_factory_);
952 // The non-alternate protocol job needs to hang in order to guarantee that
953 // the alternate-protocol job will "win".
954 AddHangingNonAlternateProtocolSocketData();
956 CreateSessionWithNextProtos();
958 SendRequestAndExpectHttpResponse("hello world");
959 SendRequestAndExpectQuicResponseOnPort("hello!", 137);
962 TEST_P(QuicNetworkTransactionTest, ConfirmAlternateProtocol) {
963 params_.enable_insecure_quic = true;
965 MockRead http_reads[] = {
966 MockRead("HTTP/1.1 200 OK\r\n"),
967 MockRead(kQuicAlternateProtocolHttpHeader),
968 MockRead("hello world"),
969 MockRead(SYNCHRONOUS, ERR_TEST_PEER_CLOSE_AFTER_NEXT_MOCK_READ),
970 MockRead(ASYNC, OK)};
972 StaticSocketDataProvider http_data(http_reads, arraysize(http_reads), nullptr,
974 socket_factory_.AddSocketDataProvider(&http_data);
976 MockQuicData mock_quic_data;
977 mock_quic_data.AddWrite(
978 ConstructRequestHeadersPacket(1, kClientDataStreamId1, true, true,
979 GetRequestHeaders("GET", "http", "/")));
980 mock_quic_data.AddRead(ConstructResponseHeadersPacket(
981 1, kClientDataStreamId1, false, false, GetResponseHeaders("200 OK")));
982 mock_quic_data.AddRead(
983 ConstructDataPacket(2, kClientDataStreamId1, false, true, 0, "hello!"));
984 mock_quic_data.AddWrite(ConstructAckPacket(2, 1));
985 mock_quic_data.AddRead(ASYNC, ERR_IO_PENDING); // No more data to read
986 mock_quic_data.AddRead(ASYNC, 0); // EOF
987 mock_quic_data.AddRead(SYNCHRONOUS, 0); // EOF
989 mock_quic_data.AddSocketDataToFactory(&socket_factory_);
991 // The non-alternate protocol job needs to hang in order to guarantee that
992 // the alternate-protocol job will "win".
993 AddHangingNonAlternateProtocolSocketData();
995 CreateSessionWithNextProtos();
997 AlternativeService alternative_service(QUIC,
998 HostPortPair::FromURL(request_.url));
999 http_server_properties_.MarkAlternativeServiceRecentlyBroken(
1000 alternative_service);
1001 EXPECT_TRUE(http_server_properties_.WasAlternativeServiceRecentlyBroken(
1002 alternative_service));
1004 SendRequestAndExpectHttpResponse("hello world");
1005 SendRequestAndExpectQuicResponse("hello!");
1007 mock_quic_data.CompleteRead();
1009 EXPECT_FALSE(http_server_properties_.WasAlternativeServiceRecentlyBroken(
1010 alternative_service));
1013 TEST_P(QuicNetworkTransactionTest, UseAlternateProtocolProbabilityForQuic) {
1014 params_.enable_insecure_quic = true;
1016 MockRead http_reads[] = {
1017 MockRead("HTTP/1.1 200 OK\r\n"),
1018 MockRead(kQuicAlternateProtocol50pctHttpHeader),
1019 MockRead("hello world"),
1020 MockRead(SYNCHRONOUS, ERR_TEST_PEER_CLOSE_AFTER_NEXT_MOCK_READ),
1021 MockRead(ASYNC, OK)
1024 StaticSocketDataProvider http_data(http_reads, arraysize(http_reads),
1025 nullptr, 0);
1026 socket_factory_.AddSocketDataProvider(&http_data);
1028 MockQuicData mock_quic_data;
1029 mock_quic_data.AddWrite(
1030 ConstructRequestHeadersPacket(1, kClientDataStreamId1, true, true,
1031 GetRequestHeaders("GET", "http", "/")));
1032 mock_quic_data.AddRead(
1033 ConstructResponseHeadersPacket(1, kClientDataStreamId1, false, false,
1034 GetResponseHeaders("200 OK")));
1035 mock_quic_data.AddRead(
1036 ConstructDataPacket(2, kClientDataStreamId1, false, true, 0, "hello!"));
1037 mock_quic_data.AddWrite(ConstructAckPacket(2, 1));
1038 mock_quic_data.AddRead(ASYNC, ERR_IO_PENDING); // No more data to read
1039 mock_quic_data.AddRead(SYNCHRONOUS, 0); // EOF
1041 mock_quic_data.AddSocketDataToFactory(&socket_factory_);
1043 // The non-alternate protocol job needs to hang in order to guarantee that
1044 // the alternate-protocol job will "win".
1045 AddHangingNonAlternateProtocolSocketData();
1047 params_.alternative_service_probability_threshold = .25;
1048 CreateSessionWithNextProtos();
1050 SendRequestAndExpectHttpResponse("hello world");
1051 SendRequestAndExpectQuicResponse("hello!");
1054 TEST_P(QuicNetworkTransactionTest, DontUseAlternateProtocolProbabilityForQuic) {
1055 params_.enable_insecure_quic = true;
1057 MockRead http_reads[] = {
1058 MockRead("HTTP/1.1 200 OK\r\n"),
1059 MockRead(kQuicAlternateProtocol50pctHttpHeader),
1060 MockRead("hello world"),
1061 MockRead(SYNCHRONOUS, ERR_TEST_PEER_CLOSE_AFTER_NEXT_MOCK_READ),
1062 MockRead(ASYNC, OK)
1065 StaticSocketDataProvider http_data(http_reads, arraysize(http_reads),
1066 nullptr, 0);
1067 socket_factory_.AddSocketDataProvider(&http_data);
1068 socket_factory_.AddSocketDataProvider(&http_data);
1070 params_.alternative_service_probability_threshold = .75;
1071 CreateSessionWithNextProtos();
1073 SendRequestAndExpectHttpResponse("hello world");
1074 SendRequestAndExpectHttpResponse("hello world");
1077 TEST_P(QuicNetworkTransactionTest, DontUseAlternateProtocolForInsecureQuic) {
1078 params_.enable_insecure_quic = true;
1080 MockRead http_reads[] = {MockRead("HTTP/1.1 200 OK\r\n"),
1081 MockRead("Content-length: 11\r\n"),
1082 MockRead("Alternate-Protocol: 443:quic\r\n\r\n"),
1083 MockRead("hello world"),
1084 MockRead("HTTP/1.1 200 OK\r\n"),
1085 MockRead("Content-length: 11\r\n"),
1086 MockRead("Alternate-Protocol: 443:quic\r\n\r\n"),
1087 MockRead("hello world"),
1088 MockRead(ASYNC, OK)};
1090 StaticSocketDataProvider http_data(http_reads, arraysize(http_reads), nullptr,
1092 socket_factory_.AddSocketDataProvider(&http_data);
1093 socket_factory_.AddSocketDataProvider(&http_data);
1095 params_.enable_insecure_quic = false;
1096 CreateSessionWithNextProtos();
1098 SendRequestAndExpectHttpResponse("hello world");
1099 SendRequestAndExpectHttpResponse("hello world");
1102 TEST_P(QuicNetworkTransactionTest,
1103 DontUseAlternateProtocolWithBadProbabilityForQuic) {
1104 params_.enable_insecure_quic = true;
1106 MockRead http_reads[] = {
1107 MockRead("HTTP/1.1 200 OK\r\n"),
1108 MockRead("Alternate-Protocol: 443:quic,p=2\r\n\r\n"),
1109 MockRead("hello world"),
1110 MockRead(SYNCHRONOUS, ERR_TEST_PEER_CLOSE_AFTER_NEXT_MOCK_READ),
1111 MockRead(ASYNC, OK)
1114 StaticSocketDataProvider http_data(http_reads, arraysize(http_reads),
1115 nullptr, 0);
1116 socket_factory_.AddSocketDataProvider(&http_data);
1117 socket_factory_.AddSocketDataProvider(&http_data);
1119 params_.alternative_service_probability_threshold = .75;
1120 CreateSessionWithNextProtos();
1122 SendRequestAndExpectHttpResponse("hello world");
1123 SendRequestAndExpectHttpResponse("hello world");
1126 TEST_P(QuicNetworkTransactionTest, UseAlternateProtocolForQuicForHttps) {
1127 params_.enable_insecure_quic = true;
1128 params_.origin_to_force_quic_on =
1129 HostPortPair::FromString("www.google.com:443");
1131 MockRead http_reads[] = {
1132 MockRead("HTTP/1.1 200 OK\r\n"),
1133 MockRead(kQuicAlternateProtocolHttpsHeader),
1134 MockRead("hello world"),
1135 MockRead(SYNCHRONOUS, ERR_TEST_PEER_CLOSE_AFTER_NEXT_MOCK_READ),
1136 MockRead(ASYNC, OK)
1139 StaticSocketDataProvider http_data(http_reads, arraysize(http_reads),
1140 nullptr, 0);
1141 socket_factory_.AddSocketDataProvider(&http_data);
1143 MockQuicData mock_quic_data;
1144 mock_quic_data.AddWrite(
1145 ConstructRequestHeadersPacket(1, kClientDataStreamId1, true, true,
1146 GetRequestHeaders("GET", "http", "/")));
1147 mock_quic_data.AddRead(
1148 ConstructResponseHeadersPacket(1, kClientDataStreamId1, false, false,
1149 GetResponseHeaders("200 OK")));
1150 mock_quic_data.AddRead(
1151 ConstructDataPacket(2, kClientDataStreamId1, false, true, 0, "hello!"));
1152 mock_quic_data.AddWrite(ConstructAckPacket(2, 1));
1153 mock_quic_data.AddRead(ASYNC, ERR_IO_PENDING); // No more data to read
1154 mock_quic_data.AddRead(SYNCHRONOUS, 0); // EOF
1156 mock_quic_data.AddSocketDataToFactory(&socket_factory_);
1158 // The non-alternate protocol job needs to hang in order to guarantee that
1159 // the alternate-protocol job will "win".
1160 AddHangingNonAlternateProtocolSocketData();
1162 CreateSessionWithNextProtos();
1164 // TODO(rtenneti): Test QUIC over HTTPS, GetSSLInfo().
1165 SendRequestAndExpectHttpResponse("hello world");
1168 class QuicAltSvcCertificateVerificationTest
1169 : public QuicNetworkTransactionTest {
1170 public:
1171 void Run(bool valid) {
1172 HostPortPair origin(valid ? "mail.example.org" : "invalid.example.org",
1173 443);
1174 HostPortPair alternative("www.example.org", 443);
1175 std::string url("https://");
1176 url.append(origin.host());
1177 url.append(":443");
1178 request_.url = GURL(url);
1180 maker_.set_hostname(origin.host());
1181 MockQuicData mock_quic_data;
1182 mock_quic_data.AddWrite(
1183 ConstructRequestHeadersPacket(1, kClientDataStreamId1, true, true,
1184 GetRequestHeaders("GET", "https", "/")));
1185 mock_quic_data.AddRead(ConstructResponseHeadersPacket(
1186 1, kClientDataStreamId1, false, false, GetResponseHeaders("200 OK")));
1187 mock_quic_data.AddRead(
1188 ConstructDataPacket(2, kClientDataStreamId1, false, true, 0, "hello!"));
1189 mock_quic_data.AddWrite(ConstructAckPacket(2, 1));
1190 mock_quic_data.AddRead(ASYNC, ERR_IO_PENDING);
1191 mock_quic_data.AddSocketDataToFactory(&socket_factory_);
1193 scoped_refptr<X509Certificate> cert(
1194 ImportCertFromFile(GetTestCertsDirectory(), "spdy_pooling.pem"));
1195 ASSERT_TRUE(cert.get());
1196 bool common_name_fallback_used;
1197 EXPECT_EQ(valid,
1198 cert->VerifyNameMatch(origin.host(), &common_name_fallback_used));
1199 EXPECT_TRUE(
1200 cert->VerifyNameMatch(alternative.host(), &common_name_fallback_used));
1201 ProofVerifyDetailsChromium verify_details;
1202 verify_details.cert_verify_result.verified_cert = cert;
1203 verify_details.cert_verify_result.is_issued_by_known_root = true;
1204 crypto_client_stream_factory_.AddProofVerifyDetails(&verify_details);
1205 crypto_client_stream_factory_.set_handshake_mode(
1206 MockCryptoClientStream::CONFIRM_HANDSHAKE);
1208 // Connection to |origin| fails, so that success of |request| depends on
1209 // connection to |alternate| only.
1210 MockConnect refused_connect(ASYNC, ERR_CONNECTION_REFUSED);
1211 StaticSocketDataProvider refused_data;
1212 refused_data.set_connect_data(refused_connect);
1213 socket_factory_.AddSocketDataProvider(&refused_data);
1215 CreateSessionWithNextProtos();
1216 AlternativeService alternative_service(QUIC, alternative);
1217 base::Time expiration = base::Time::Now() + base::TimeDelta::FromDays(1);
1218 session_->http_server_properties()->SetAlternativeService(
1219 origin, alternative_service, 1.0, expiration);
1220 scoped_ptr<HttpNetworkTransaction> trans(
1221 new HttpNetworkTransaction(DEFAULT_PRIORITY, session_.get()));
1222 TestCompletionCallback callback;
1223 int rv = trans->Start(&request_, callback.callback(), net_log_.bound());
1224 EXPECT_EQ(ERR_IO_PENDING, rv);
1225 rv = callback.WaitForResult();
1226 if (valid) {
1227 EXPECT_EQ(OK, rv);
1228 CheckWasQuicResponse(trans);
1229 CheckResponsePort(trans, 443);
1230 CheckResponseData(trans, "hello!");
1231 } else {
1232 EXPECT_EQ(ERR_CONNECTION_REFUSED, rv);
1237 INSTANTIATE_TEST_CASE_P(Version,
1238 QuicAltSvcCertificateVerificationTest,
1239 ::testing::ValuesIn(QuicSupportedVersions()));
1241 TEST_P(QuicAltSvcCertificateVerificationTest,
1242 RequestSucceedsWithValidCertificate) {
1243 Run(true);
1246 TEST_P(QuicAltSvcCertificateVerificationTest,
1247 RequestFailsWithInvalidCertificate) {
1248 Run(false);
1251 TEST_P(QuicNetworkTransactionTest, HungAlternateProtocol) {
1252 params_.enable_insecure_quic = true;
1253 crypto_client_stream_factory_.set_handshake_mode(
1254 MockCryptoClientStream::COLD_START);
1256 MockWrite http_writes[] = {
1257 MockWrite(SYNCHRONOUS, 0, "GET / HTTP/1.1\r\n"),
1258 MockWrite(SYNCHRONOUS, 1, "Host: www.google.com\r\n"),
1259 MockWrite(SYNCHRONOUS, 2, "Connection: keep-alive\r\n\r\n")
1262 MockRead http_reads[] = {
1263 MockRead(SYNCHRONOUS, 3, "HTTP/1.1 200 OK\r\n"),
1264 MockRead(SYNCHRONOUS, 4, kQuicAlternateProtocolHttpHeader),
1265 MockRead(SYNCHRONOUS, 5, "hello world"),
1266 MockRead(SYNCHRONOUS, OK, 6)
1269 DeterministicMockClientSocketFactory socket_factory;
1271 DeterministicSocketData http_data(http_reads, arraysize(http_reads),
1272 http_writes, arraysize(http_writes));
1273 socket_factory.AddSocketDataProvider(&http_data);
1275 // The QUIC transaction will not be allowed to complete.
1276 MockWrite quic_writes[] = {
1277 MockWrite(ASYNC, ERR_IO_PENDING, 0)
1279 MockRead quic_reads[] = {
1280 MockRead(ASYNC, ERR_IO_PENDING, 1),
1282 DeterministicSocketData quic_data(quic_reads, arraysize(quic_reads),
1283 quic_writes, arraysize(quic_writes));
1284 socket_factory.AddSocketDataProvider(&quic_data);
1286 // The HTTP transaction will complete.
1287 DeterministicSocketData http_data2(http_reads, arraysize(http_reads),
1288 http_writes, arraysize(http_writes));
1289 socket_factory.AddSocketDataProvider(&http_data2);
1291 CreateSessionWithFactory(&socket_factory, true);
1293 // Run the first request.
1294 http_data.StopAfter(arraysize(http_reads) + arraysize(http_writes));
1295 SendRequestAndExpectHttpResponse("hello world");
1296 ASSERT_TRUE(http_data.AllReadDataConsumed());
1297 ASSERT_TRUE(http_data.AllWriteDataConsumed());
1299 // Now run the second request in which the QUIC socket hangs,
1300 // and verify the the transaction continues over HTTP.
1301 http_data2.StopAfter(arraysize(http_reads) + arraysize(http_writes));
1302 SendRequestAndExpectHttpResponse("hello world");
1304 ASSERT_TRUE(http_data2.AllReadDataConsumed());
1305 ASSERT_TRUE(http_data2.AllWriteDataConsumed());
1306 ASSERT_TRUE(!quic_data.AllReadDataConsumed());
1307 ASSERT_TRUE(!quic_data.AllWriteDataConsumed());
1310 TEST_P(QuicNetworkTransactionTest, ZeroRTTWithHttpRace) {
1311 params_.enable_insecure_quic = true;
1312 MockQuicData mock_quic_data;
1313 mock_quic_data.AddWrite(
1314 ConstructRequestHeadersPacket(1, kClientDataStreamId1, true, true,
1315 GetRequestHeaders("GET", "http", "/")));
1316 mock_quic_data.AddRead(
1317 ConstructResponseHeadersPacket(1, kClientDataStreamId1, false, false,
1318 GetResponseHeaders("200 OK")));
1319 mock_quic_data.AddRead(
1320 ConstructDataPacket(2, kClientDataStreamId1, false, true, 0, "hello!"));
1321 mock_quic_data.AddWrite(ConstructAckPacket(2, 1));
1322 mock_quic_data.AddRead(ASYNC, ERR_IO_PENDING); // No more data to read
1323 mock_quic_data.AddRead(SYNCHRONOUS, 0); // EOF
1325 mock_quic_data.AddSocketDataToFactory(&socket_factory_);
1327 // The non-alternate protocol job needs to hang in order to guarantee that
1328 // the alternate-protocol job will "win".
1329 AddHangingNonAlternateProtocolSocketData();
1331 CreateSessionWithNextProtos();
1332 AddQuicAlternateProtocolMapping(MockCryptoClientStream::ZERO_RTT);
1333 SendRequestAndExpectQuicResponse("hello!");
1336 TEST_P(QuicNetworkTransactionTest, ZeroRTTWithNoHttpRace) {
1337 params_.enable_insecure_quic = true;
1338 MockQuicData mock_quic_data;
1339 mock_quic_data.AddWrite(
1340 ConstructRequestHeadersPacket(1, kClientDataStreamId1, true, true,
1341 GetRequestHeaders("GET", "http", "/")));
1342 mock_quic_data.AddRead(
1343 ConstructResponseHeadersPacket(1, kClientDataStreamId1, false, false,
1344 GetResponseHeaders("200 OK")));
1345 mock_quic_data.AddRead(
1346 ConstructDataPacket(2, kClientDataStreamId1, false, true, 0, "hello!"));
1347 mock_quic_data.AddWrite(ConstructAckPacket(2, 1));
1348 mock_quic_data.AddRead(ASYNC, ERR_IO_PENDING); // No more data to read
1349 mock_quic_data.AddRead(SYNCHRONOUS, 0); // EOF
1350 mock_quic_data.AddSocketDataToFactory(&socket_factory_);
1352 // In order for a new QUIC session to be established via alternate-protocol
1353 // without racing an HTTP connection, we need the host resolution to happen
1354 // synchronously.
1355 host_resolver_.set_synchronous_mode(true);
1356 host_resolver_.rules()->AddIPLiteralRule("www.google.com", "192.168.0.1", "");
1357 HostResolver::RequestInfo info(HostPortPair("www.google.com", 80));
1358 AddressList address;
1359 host_resolver_.Resolve(info,
1360 DEFAULT_PRIORITY,
1361 &address,
1362 CompletionCallback(),
1363 nullptr,
1364 net_log_.bound());
1366 CreateSessionWithNextProtos();
1367 AddQuicAlternateProtocolMapping(MockCryptoClientStream::ZERO_RTT);
1368 SendRequestAndExpectQuicResponse("hello!");
1371 TEST_P(QuicNetworkTransactionTest, ZeroRTTWithProxy) {
1372 params_.enable_insecure_quic = true;
1373 proxy_service_.reset(
1374 ProxyService::CreateFixedFromPacResult("PROXY myproxy:70"));
1376 // Since we are using a proxy, the QUIC job will not succeed.
1377 MockWrite http_writes[] = {
1378 MockWrite(SYNCHRONOUS, 0, "GET http://www.google.com/ HTTP/1.1\r\n"),
1379 MockWrite(SYNCHRONOUS, 1, "Host: www.google.com\r\n"),
1380 MockWrite(SYNCHRONOUS, 2, "Proxy-Connection: keep-alive\r\n\r\n")
1383 MockRead http_reads[] = {
1384 MockRead(SYNCHRONOUS, 3, "HTTP/1.1 200 OK\r\n"),
1385 MockRead(SYNCHRONOUS, 4, kQuicAlternateProtocolHttpHeader),
1386 MockRead(SYNCHRONOUS, 5, "hello world"),
1387 MockRead(SYNCHRONOUS, OK, 6)
1390 StaticSocketDataProvider http_data(http_reads, arraysize(http_reads),
1391 http_writes, arraysize(http_writes));
1392 socket_factory_.AddSocketDataProvider(&http_data);
1394 // In order for a new QUIC session to be established via alternate-protocol
1395 // without racing an HTTP connection, we need the host resolution to happen
1396 // synchronously.
1397 host_resolver_.set_synchronous_mode(true);
1398 host_resolver_.rules()->AddIPLiteralRule("www.google.com", "192.168.0.1", "");
1399 HostResolver::RequestInfo info(HostPortPair("www.google.com", 80));
1400 AddressList address;
1401 host_resolver_.Resolve(info,
1402 DEFAULT_PRIORITY,
1403 &address,
1404 CompletionCallback(),
1405 nullptr,
1406 net_log_.bound());
1408 CreateSessionWithNextProtos();
1409 AddQuicAlternateProtocolMapping(MockCryptoClientStream::ZERO_RTT);
1410 SendRequestAndExpectHttpResponse("hello world");
1413 TEST_P(QuicNetworkTransactionTest, ZeroRTTWithConfirmationRequired) {
1414 params_.enable_insecure_quic = true;
1415 MockQuicData mock_quic_data;
1416 mock_quic_data.AddWrite(
1417 ConstructRequestHeadersPacket(1, kClientDataStreamId1, true, true,
1418 GetRequestHeaders("GET", "http", "/")));
1419 mock_quic_data.AddRead(
1420 ConstructResponseHeadersPacket(1, kClientDataStreamId1, false, false,
1421 GetResponseHeaders("200 OK")));
1422 mock_quic_data.AddRead(
1423 ConstructDataPacket(2, kClientDataStreamId1, false, true, 0, "hello!"));
1424 mock_quic_data.AddWrite(ConstructAckPacket(2, 1));
1425 mock_quic_data.AddRead(ASYNC, ERR_IO_PENDING); // No more data to read
1426 mock_quic_data.AddSocketDataToFactory(&socket_factory_);
1428 // The non-alternate protocol job needs to hang in order to guarantee that
1429 // the alternate-protocol job will "win".
1430 AddHangingNonAlternateProtocolSocketData();
1432 // In order for a new QUIC session to be established via alternate-protocol
1433 // without racing an HTTP connection, we need the host resolution to happen
1434 // synchronously. Of course, even though QUIC *could* perform a 0-RTT
1435 // connection to the the server, in this test we require confirmation
1436 // before encrypting so the HTTP job will still start.
1437 host_resolver_.set_synchronous_mode(true);
1438 host_resolver_.rules()->AddIPLiteralRule("www.google.com", "192.168.0.1", "");
1439 HostResolver::RequestInfo info(HostPortPair("www.google.com", 80));
1440 AddressList address;
1441 host_resolver_.Resolve(info, DEFAULT_PRIORITY, &address,
1442 CompletionCallback(), nullptr, net_log_.bound());
1444 CreateSessionWithNextProtos();
1445 session_->quic_stream_factory()->set_require_confirmation(true);
1446 AddQuicAlternateProtocolMapping(MockCryptoClientStream::ZERO_RTT);
1448 scoped_ptr<HttpNetworkTransaction> trans(
1449 new HttpNetworkTransaction(DEFAULT_PRIORITY, session_.get()));
1450 TestCompletionCallback callback;
1451 int rv = trans->Start(&request_, callback.callback(), net_log_.bound());
1452 EXPECT_EQ(ERR_IO_PENDING, rv);
1454 crypto_client_stream_factory_.last_stream()->SendOnCryptoHandshakeEvent(
1455 QuicSession::HANDSHAKE_CONFIRMED);
1456 EXPECT_EQ(OK, callback.WaitForResult());
1458 CheckWasQuicResponse(trans);
1459 CheckResponseData(trans, "hello!");
1462 TEST_P(QuicNetworkTransactionTest, BrokenAlternateProtocol) {
1463 params_.enable_insecure_quic = true;
1464 // Alternate-protocol job
1465 scoped_ptr<QuicEncryptedPacket> close(ConstructConnectionClosePacket(1));
1466 MockRead quic_reads[] = {
1467 MockRead(ASYNC, close->data(), close->length()),
1468 MockRead(ASYNC, ERR_IO_PENDING), // No more data to read
1469 MockRead(ASYNC, OK), // EOF
1471 StaticSocketDataProvider quic_data(quic_reads, arraysize(quic_reads),
1472 nullptr, 0);
1473 socket_factory_.AddSocketDataProvider(&quic_data);
1475 // Main job which will succeed even though the alternate job fails.
1476 MockRead http_reads[] = {
1477 MockRead("HTTP/1.1 200 OK\r\n\r\n"),
1478 MockRead("hello from http"),
1479 MockRead(SYNCHRONOUS, ERR_TEST_PEER_CLOSE_AFTER_NEXT_MOCK_READ),
1480 MockRead(ASYNC, OK)
1483 StaticSocketDataProvider http_data(http_reads, arraysize(http_reads),
1484 nullptr, 0);
1485 socket_factory_.AddSocketDataProvider(&http_data);
1487 CreateSessionWithNextProtos();
1488 AddQuicAlternateProtocolMapping(MockCryptoClientStream::COLD_START);
1489 SendRequestAndExpectHttpResponse("hello from http");
1490 ExpectBrokenAlternateProtocolMapping();
1493 TEST_P(QuicNetworkTransactionTest, BrokenAlternateProtocolReadError) {
1494 params_.enable_insecure_quic = true;
1495 // Alternate-protocol job
1496 MockRead quic_reads[] = {
1497 MockRead(ASYNC, ERR_SOCKET_NOT_CONNECTED),
1499 StaticSocketDataProvider quic_data(quic_reads, arraysize(quic_reads),
1500 nullptr, 0);
1501 socket_factory_.AddSocketDataProvider(&quic_data);
1503 // Main job which will succeed even though the alternate job fails.
1504 MockRead http_reads[] = {
1505 MockRead("HTTP/1.1 200 OK\r\n\r\n"),
1506 MockRead("hello from http"),
1507 MockRead(SYNCHRONOUS, ERR_TEST_PEER_CLOSE_AFTER_NEXT_MOCK_READ),
1508 MockRead(ASYNC, OK)
1511 StaticSocketDataProvider http_data(http_reads, arraysize(http_reads),
1512 nullptr, 0);
1513 socket_factory_.AddSocketDataProvider(&http_data);
1515 CreateSessionWithNextProtos();
1517 AddQuicAlternateProtocolMapping(MockCryptoClientStream::COLD_START);
1518 SendRequestAndExpectHttpResponse("hello from http");
1519 ExpectBrokenAlternateProtocolMapping();
1522 TEST_P(QuicNetworkTransactionTest, NoBrokenAlternateProtocolIfTcpFails) {
1523 params_.enable_insecure_quic = true;
1524 // Alternate-protocol job will fail when the session attempts to read.
1525 MockRead quic_reads[] = {
1526 MockRead(ASYNC, ERR_SOCKET_NOT_CONNECTED),
1528 StaticSocketDataProvider quic_data(quic_reads, arraysize(quic_reads),
1529 nullptr, 0);
1530 socket_factory_.AddSocketDataProvider(&quic_data);
1532 // Main job will also fail.
1533 MockRead http_reads[] = {
1534 MockRead(ASYNC, ERR_SOCKET_NOT_CONNECTED),
1537 StaticSocketDataProvider http_data(http_reads, arraysize(http_reads),
1538 nullptr, 0);
1539 http_data.set_connect_data(MockConnect(ASYNC, ERR_SOCKET_NOT_CONNECTED));
1540 socket_factory_.AddSocketDataProvider(&http_data);
1542 CreateSessionWithNextProtos();
1544 AddQuicAlternateProtocolMapping(MockCryptoClientStream::COLD_START);
1545 scoped_ptr<HttpNetworkTransaction> trans(
1546 new HttpNetworkTransaction(DEFAULT_PRIORITY, session_.get()));
1547 TestCompletionCallback callback;
1548 int rv = trans->Start(&request_, callback.callback(), net_log_.bound());
1549 EXPECT_EQ(ERR_IO_PENDING, rv);
1550 EXPECT_EQ(ERR_SOCKET_NOT_CONNECTED, callback.WaitForResult());
1551 ExpectQuicAlternateProtocolMapping();
1554 TEST_P(QuicNetworkTransactionTest, FailedZeroRttBrokenAlternateProtocol) {
1555 params_.enable_insecure_quic = true;
1556 // Alternate-protocol job
1557 MockRead quic_reads[] = {
1558 MockRead(ASYNC, ERR_SOCKET_NOT_CONNECTED),
1560 StaticSocketDataProvider quic_data(quic_reads, arraysize(quic_reads),
1561 nullptr, 0);
1562 socket_factory_.AddSocketDataProvider(&quic_data);
1564 AddHangingNonAlternateProtocolSocketData();
1566 // Second Alternate-protocol job which will race with the TCP job.
1567 StaticSocketDataProvider quic_data2(quic_reads, arraysize(quic_reads),
1568 nullptr, 0);
1569 socket_factory_.AddSocketDataProvider(&quic_data2);
1571 // Final job that will proceed when the QUIC job fails.
1572 MockRead http_reads[] = {
1573 MockRead("HTTP/1.1 200 OK\r\n\r\n"),
1574 MockRead("hello from http"),
1575 MockRead(SYNCHRONOUS, ERR_TEST_PEER_CLOSE_AFTER_NEXT_MOCK_READ),
1576 MockRead(ASYNC, OK)
1579 StaticSocketDataProvider http_data(http_reads, arraysize(http_reads),
1580 nullptr, 0);
1581 socket_factory_.AddSocketDataProvider(&http_data);
1583 CreateSessionWithNextProtos();
1585 AddQuicAlternateProtocolMapping(MockCryptoClientStream::ZERO_RTT);
1587 SendRequestAndExpectHttpResponse("hello from http");
1589 ExpectBrokenAlternateProtocolMapping();
1591 EXPECT_TRUE(quic_data.AllReadDataConsumed());
1592 EXPECT_TRUE(quic_data.AllWriteDataConsumed());
1595 TEST_P(QuicNetworkTransactionTest, DISABLED_HangingZeroRttFallback) {
1596 params_.enable_insecure_quic = true;
1597 // Alternate-protocol job
1598 MockRead quic_reads[] = {
1599 MockRead(ASYNC, ERR_IO_PENDING),
1601 StaticSocketDataProvider quic_data(quic_reads, arraysize(quic_reads),
1602 nullptr, 0);
1603 socket_factory_.AddSocketDataProvider(&quic_data);
1605 // Main job that will proceed when the QUIC job fails.
1606 MockRead http_reads[] = {
1607 MockRead("HTTP/1.1 200 OK\r\n\r\n"),
1608 MockRead("hello from http"),
1609 MockRead(SYNCHRONOUS, ERR_TEST_PEER_CLOSE_AFTER_NEXT_MOCK_READ),
1610 MockRead(ASYNC, OK)
1613 StaticSocketDataProvider http_data(http_reads, arraysize(http_reads),
1614 nullptr, 0);
1615 socket_factory_.AddSocketDataProvider(&http_data);
1617 CreateSessionWithNextProtos();
1619 AddQuicAlternateProtocolMapping(MockCryptoClientStream::ZERO_RTT);
1621 SendRequestAndExpectHttpResponse("hello from http");
1624 TEST_P(QuicNetworkTransactionTest, BrokenAlternateProtocolOnConnectFailure) {
1625 params_.enable_insecure_quic = true;
1626 // Alternate-protocol job will fail before creating a QUIC session.
1627 StaticSocketDataProvider quic_data(nullptr, 0, nullptr, 0);
1628 quic_data.set_connect_data(MockConnect(SYNCHRONOUS,
1629 ERR_INTERNET_DISCONNECTED));
1630 socket_factory_.AddSocketDataProvider(&quic_data);
1632 // Main job which will succeed even though the alternate job fails.
1633 MockRead http_reads[] = {
1634 MockRead("HTTP/1.1 200 OK\r\n\r\n"),
1635 MockRead("hello from http"),
1636 MockRead(SYNCHRONOUS, ERR_TEST_PEER_CLOSE_AFTER_NEXT_MOCK_READ),
1637 MockRead(ASYNC, OK)
1640 StaticSocketDataProvider http_data(http_reads, arraysize(http_reads),
1641 nullptr, 0);
1642 socket_factory_.AddSocketDataProvider(&http_data);
1644 CreateSessionWithNextProtos();
1645 AddQuicAlternateProtocolMapping(MockCryptoClientStream::COLD_START);
1646 SendRequestAndExpectHttpResponse("hello from http");
1648 ExpectBrokenAlternateProtocolMapping();
1651 TEST_P(QuicNetworkTransactionTest, ConnectionCloseDuringConnect) {
1652 params_.enable_insecure_quic = true;
1653 MockQuicData mock_quic_data;
1654 mock_quic_data.AddSynchronousRead(ConstructConnectionClosePacket(1));
1655 mock_quic_data.AddWrite(
1656 ConstructRequestHeadersPacket(1, kClientDataStreamId1, true, true,
1657 GetRequestHeaders("GET", "http", "/")));
1658 mock_quic_data.AddWrite(ConstructAckPacket(2, 1));
1659 mock_quic_data.AddSocketDataToFactory(&socket_factory_);
1661 // When the QUIC connection fails, we will try the request again over HTTP.
1662 MockRead http_reads[] = {
1663 MockRead("HTTP/1.1 200 OK\r\n"),
1664 MockRead(kQuicAlternateProtocolHttpHeader),
1665 MockRead("hello world"),
1666 MockRead(SYNCHRONOUS, ERR_TEST_PEER_CLOSE_AFTER_NEXT_MOCK_READ),
1667 MockRead(ASYNC, OK)
1670 StaticSocketDataProvider http_data(http_reads, arraysize(http_reads),
1671 nullptr, 0);
1672 socket_factory_.AddSocketDataProvider(&http_data);
1674 // In order for a new QUIC session to be established via alternate-protocol
1675 // without racing an HTTP connection, we need the host resolution to happen
1676 // synchronously.
1677 host_resolver_.set_synchronous_mode(true);
1678 host_resolver_.rules()->AddIPLiteralRule("www.google.com", "192.168.0.1", "");
1679 HostResolver::RequestInfo info(HostPortPair("www.google.com", 80));
1680 AddressList address;
1681 host_resolver_.Resolve(info,
1682 DEFAULT_PRIORITY,
1683 &address,
1684 CompletionCallback(),
1685 nullptr,
1686 net_log_.bound());
1688 CreateSessionWithNextProtos();
1689 AddQuicAlternateProtocolMapping(MockCryptoClientStream::ZERO_RTT);
1690 SendRequestAndExpectHttpResponse("hello world");
1693 // Test that a secure request over an insecure QUIC connection fails with
1694 // the appropriate error code. Note that this never happens in production,
1695 // because the handshake (which this test mocks) would fail in this scenario.
1696 TEST_P(QuicNetworkTransactionTest, SecureResourceOverInsecureQuic) {
1697 params_.enable_insecure_quic = true;
1698 maker_.set_hostname("www.example.org");
1699 MockQuicData mock_quic_data;
1700 mock_quic_data.AddWrite(
1701 ConstructRequestHeadersPacket(1, kClientDataStreamId1, true, true,
1702 GetRequestHeaders("GET", "https", "/")));
1703 mock_quic_data.AddRead(ConstructResponseHeadersPacket(
1704 1, kClientDataStreamId1, false, false, GetResponseHeaders("200 OK")));
1705 mock_quic_data.AddRead(
1706 ConstructDataPacket(2, kClientDataStreamId1, false, true, 0, "hello!"));
1707 mock_quic_data.AddWrite(ConstructAckPacket(2, 1));
1708 mock_quic_data.AddRead(SYNCHRONOUS, 0);
1709 mock_quic_data.AddSocketDataToFactory(&socket_factory_);
1711 request_.url = GURL("https://www.example.org:443");
1712 AddHangingNonAlternateProtocolSocketData();
1713 CreateSessionWithNextProtos();
1714 AddQuicAlternateProtocolMapping(MockCryptoClientStream::CONFIRM_HANDSHAKE);
1715 scoped_ptr<HttpNetworkTransaction> trans(
1716 new HttpNetworkTransaction(DEFAULT_PRIORITY, session_.get()));
1717 TestCompletionCallback callback;
1718 int rv = trans->Start(&request_, callback.callback(), net_log_.bound());
1719 EXPECT_EQ(ERR_REQUEST_FOR_SECURE_RESOURCE_OVER_INSECURE_QUIC,
1720 callback.GetResult(rv));
1723 TEST_P(QuicNetworkTransactionTest, SecureResourceOverSecureQuic) {
1724 params_.enable_insecure_quic = true;
1725 maker_.set_hostname("www.example.org");
1726 MockQuicData mock_quic_data;
1727 mock_quic_data.AddWrite(
1728 ConstructRequestHeadersPacket(1, kClientDataStreamId1, true, true,
1729 GetRequestHeaders("GET", "https", "/")));
1730 mock_quic_data.AddRead(ConstructResponseHeadersPacket(
1731 1, kClientDataStreamId1, false, false, GetResponseHeaders("200 OK")));
1732 mock_quic_data.AddRead(
1733 ConstructDataPacket(2, kClientDataStreamId1, false, true, 0, "hello!"));
1734 mock_quic_data.AddWrite(ConstructAckPacket(2, 1));
1735 mock_quic_data.AddRead(SYNCHRONOUS, ERR_IO_PENDING); // No more read data.
1736 mock_quic_data.AddSocketDataToFactory(&socket_factory_);
1738 scoped_refptr<X509Certificate> cert(
1739 ImportCertFromFile(GetTestCertsDirectory(), "spdy_pooling.pem"));
1740 ASSERT_TRUE(cert.get());
1741 bool common_name_fallback_used;
1742 EXPECT_TRUE(
1743 cert->VerifyNameMatch("www.example.org", &common_name_fallback_used));
1744 ProofVerifyDetailsChromium verify_details;
1745 verify_details.cert_verify_result.verified_cert = cert;
1746 crypto_client_stream_factory_.AddProofVerifyDetails(&verify_details);
1748 request_.url = GURL("https://www.example.org:443");
1749 AddHangingNonAlternateProtocolSocketData();
1750 CreateSessionWithNextProtos();
1751 AddQuicAlternateProtocolMapping(MockCryptoClientStream::CONFIRM_HANDSHAKE);
1752 SendRequestAndExpectQuicResponse("hello!");
1755 } // namespace test
1756 } // namespace net