[email protected] | 61a52778 | 2013-02-21 03:58:00 | [diff] [blame] | 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. |
| 4 | |
[email protected] | 1e96003 | 2013-12-20 19:00:20 | [diff] [blame] | 5 | #include <vector> |
| 6 | |
[email protected] | 61a52778 | 2013-02-21 03:58:00 | [diff] [blame] | 7 | #include "base/basictypes.h" |
| 8 | #include "base/compiler_specific.h" |
| 9 | #include "base/memory/scoped_ptr.h" |
[email protected] | 98b20ce | 2013-05-10 05:55:26 | [diff] [blame] | 10 | #include "base/stl_util.h" |
| 11 | #include "net/base/capturing_net_log.h" |
| 12 | #include "net/base/net_log_unittest.h" |
[email protected] | 61a52778 | 2013-02-21 03:58:00 | [diff] [blame] | 13 | #include "net/base/test_completion_callback.h" |
[email protected] | 6e7845ae | 2013-03-29 21:48:11 | [diff] [blame] | 14 | #include "net/cert/mock_cert_verifier.h" |
[email protected] | f2cb3cf | 2013-03-21 01:40:53 | [diff] [blame] | 15 | #include "net/dns/mock_host_resolver.h" |
[email protected] | 61a52778 | 2013-02-21 03:58:00 | [diff] [blame] | 16 | #include "net/http/http_auth_handler_factory.h" |
| 17 | #include "net/http/http_network_session.h" |
| 18 | #include "net/http/http_network_transaction.h" |
| 19 | #include "net/http/http_server_properties_impl.h" |
| 20 | #include "net/http/http_stream.h" |
| 21 | #include "net/http/http_stream_factory.h" |
| 22 | #include "net/http/http_transaction_unittest.h" |
[email protected] | b1c988b | 2013-06-13 06:48:11 | [diff] [blame] | 23 | #include "net/http/transport_security_state.h" |
[email protected] | 61a52778 | 2013-02-21 03:58:00 | [diff] [blame] | 24 | #include "net/proxy/proxy_config_service_fixed.h" |
| 25 | #include "net/proxy/proxy_resolver.h" |
| 26 | #include "net/proxy/proxy_service.h" |
| 27 | #include "net/quic/crypto/quic_decrypter.h" |
| 28 | #include "net/quic/crypto/quic_encrypter.h" |
| 29 | #include "net/quic/quic_framer.h" |
[email protected] | 24e5bc5 | 2013-09-18 15:36:58 | [diff] [blame] | 30 | #include "net/quic/quic_http_utils.h" |
[email protected] | ed3fc15d | 2013-03-08 18:37:44 | [diff] [blame] | 31 | #include "net/quic/test_tools/crypto_test_utils.h" |
[email protected] | 61a52778 | 2013-02-21 03:58:00 | [diff] [blame] | 32 | #include "net/quic/test_tools/mock_clock.h" |
[email protected] | e8ff2684 | 2013-03-22 21:02:05 | [diff] [blame] | 33 | #include "net/quic/test_tools/mock_crypto_client_stream_factory.h" |
[email protected] | 61a52778 | 2013-02-21 03:58:00 | [diff] [blame] | 34 | #include "net/quic/test_tools/mock_random.h" |
[email protected] | 1e96003 | 2013-12-20 19:00:20 | [diff] [blame] | 35 | #include "net/quic/test_tools/quic_test_packet_maker.h" |
[email protected] | 61a52778 | 2013-02-21 03:58:00 | [diff] [blame] | 36 | #include "net/quic/test_tools/quic_test_utils.h" |
| 37 | #include "net/socket/client_socket_factory.h" |
| 38 | #include "net/socket/mock_client_socket_pool_manager.h" |
| 39 | #include "net/socket/socket_test_util.h" |
| 40 | #include "net/socket/ssl_client_socket.h" |
| 41 | #include "net/spdy/spdy_frame_builder.h" |
| 42 | #include "net/spdy/spdy_framer.h" |
[email protected] | 536fd0b | 2013-03-14 17:41:57 | [diff] [blame] | 43 | #include "net/ssl/ssl_config_service_defaults.h" |
[email protected] | 61a52778 | 2013-02-21 03:58:00 | [diff] [blame] | 44 | #include "testing/gtest/include/gtest/gtest.h" |
| 45 | #include "testing/platform_test.h" |
| 46 | |
| 47 | //----------------------------------------------------------------------------- |
| 48 | |
| 49 | namespace { |
| 50 | |
| 51 | // This is the expected return from a current server advertising QUIC. |
| 52 | static const char kQuicAlternateProtocolHttpHeader[] = |
[email protected] | 4ff6537 | 2013-06-21 05:45:46 | [diff] [blame] | 53 | "Alternate-Protocol: 80:quic\r\n\r\n"; |
[email protected] | 6d1b4ed | 2013-07-10 03:57:54 | [diff] [blame] | 54 | static const char kQuicAlternateProtocolHttpsHeader[] = |
| 55 | "Alternate-Protocol: 443:quic\r\n\r\n"; |
[email protected] | 1e96003 | 2013-12-20 19:00:20 | [diff] [blame] | 56 | |
[email protected] | 92bf17c | 2014-03-03 21:14:03 | [diff] [blame] | 57 | // Used by the QuicNetworkTransactionTest unit tests. |
| 58 | const net::QuicStreamId kStreamId5 = 5; |
| 59 | |
[email protected] | 61a52778 | 2013-02-21 03:58:00 | [diff] [blame] | 60 | } // namespace |
| 61 | |
| 62 | namespace net { |
| 63 | namespace test { |
| 64 | |
[email protected] | 1e96003 | 2013-12-20 19:00:20 | [diff] [blame] | 65 | // Helper class to encapsulate MockReads and MockWrites for QUIC. |
| 66 | // Simplify ownership issues and the interaction with the MockSocketFactory. |
| 67 | class MockQuicData { |
| 68 | public: |
| 69 | ~MockQuicData() { |
| 70 | STLDeleteElements(&packets_); |
| 71 | } |
| 72 | |
| 73 | void AddRead(scoped_ptr<QuicEncryptedPacket> packet) { |
| 74 | reads_.push_back(MockRead(SYNCHRONOUS, packet->data(), packet->length(), |
| 75 | sequence_number_++)); |
| 76 | packets_.push_back(packet.release()); |
| 77 | } |
| 78 | |
| 79 | void AddRead(IoMode mode, int rv) { |
| 80 | reads_.push_back(MockRead(mode, rv)); |
| 81 | } |
| 82 | |
| 83 | void AddWrite(scoped_ptr<QuicEncryptedPacket> packet) { |
| 84 | writes_.push_back(MockWrite(SYNCHRONOUS, packet->data(), packet->length(), |
| 85 | sequence_number_++)); |
| 86 | packets_.push_back(packet.release()); |
| 87 | } |
| 88 | |
| 89 | void AddDelayedSocketDataToFactory(MockClientSocketFactory* factory, |
| 90 | size_t delay) { |
| 91 | MockRead* reads = reads_.empty() ? NULL : &reads_[0]; |
| 92 | MockWrite* writes = writes_.empty() ? NULL : &writes_[0]; |
| 93 | socket_data_.reset(new DelayedSocketData( |
| 94 | delay, reads, reads_.size(), writes, writes_.size())); |
| 95 | factory->AddSocketDataProvider(socket_data_.get()); |
| 96 | } |
| 97 | |
| 98 | private: |
| 99 | std::vector<QuicEncryptedPacket*> packets_; |
| 100 | std::vector<MockWrite> writes_; |
| 101 | std::vector<MockRead> reads_; |
| 102 | size_t sequence_number_; |
| 103 | scoped_ptr<SocketDataProvider> socket_data_; |
| 104 | }; |
| 105 | |
| 106 | class QuicNetworkTransactionTest |
| 107 | : public PlatformTest, |
[email protected] | 5d03bbd | 2014-03-07 16:19:16 | [diff] [blame] | 108 | public ::testing::WithParamInterface<QuicVersion> { |
[email protected] | 61a52778 | 2013-02-21 03:58:00 | [diff] [blame] | 109 | protected: |
[email protected] | 1c04f952 | 2013-02-21 20:32:43 | [diff] [blame] | 110 | QuicNetworkTransactionTest() |
[email protected] | 1e96003 | 2013-12-20 19:00:20 | [diff] [blame] | 111 | : maker_(GetParam(), 0), |
| 112 | clock_(new MockClock), |
[email protected] | 1c04f952 | 2013-02-21 20:32:43 | [diff] [blame] | 113 | ssl_config_service_(new SSLConfigServiceDefaults), |
| 114 | proxy_service_(ProxyService::CreateDirect()), |
| 115 | auth_handler_factory_( |
[email protected] | dda75ab | 2013-06-22 22:43:30 | [diff] [blame] | 116 | HttpAuthHandlerFactory::CreateDefault(&host_resolver_)), |
[email protected] | 457d695 | 2013-12-13 09:24:58 | [diff] [blame] | 117 | random_generator_(0), |
[email protected] | dda75ab | 2013-06-22 22:43:30 | [diff] [blame] | 118 | hanging_data_(NULL, 0, NULL, 0) { |
[email protected] | aa9b14d | 2013-05-10 23:45:19 | [diff] [blame] | 119 | request_.method = "GET"; |
| 120 | request_.url = GURL("http://www.google.com/"); |
| 121 | request_.load_flags = 0; |
[email protected] | 98a9d125 | 2014-04-04 00:43:59 | [diff] [blame] | 122 | clock_->AdvanceTime(QuicTime::Delta::FromMilliseconds(20)); |
[email protected] | 1c04f952 | 2013-02-21 20:32:43 | [diff] [blame] | 123 | } |
[email protected] | 61a52778 | 2013-02-21 03:58:00 | [diff] [blame] | 124 | |
| 125 | virtual void SetUp() { |
| 126 | NetworkChangeNotifier::NotifyObserversOfIPAddressChangeForTests(); |
[email protected] | 2da659e | 2013-05-23 20:51:34 | [diff] [blame] | 127 | base::MessageLoop::current()->RunUntilIdle(); |
[email protected] | 61a52778 | 2013-02-21 03:58:00 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | virtual void TearDown() { |
| 131 | NetworkChangeNotifier::NotifyObserversOfIPAddressChangeForTests(); |
| 132 | // Empty the current queue. |
[email protected] | 2da659e | 2013-05-23 20:51:34 | [diff] [blame] | 133 | base::MessageLoop::current()->RunUntilIdle(); |
[email protected] | 61a52778 | 2013-02-21 03:58:00 | [diff] [blame] | 134 | PlatformTest::TearDown(); |
| 135 | NetworkChangeNotifier::NotifyObserversOfIPAddressChangeForTests(); |
[email protected] | 2da659e | 2013-05-23 20:51:34 | [diff] [blame] | 136 | base::MessageLoop::current()->RunUntilIdle(); |
[email protected] | 61a52778 | 2013-02-21 03:58:00 | [diff] [blame] | 137 | HttpStreamFactory::set_use_alternate_protocols(false); |
[email protected] | 0ce3af8 | 2013-07-22 16:17:16 | [diff] [blame] | 138 | HttpStreamFactory::SetNextProtos(std::vector<NextProto>()); |
[email protected] | 61a52778 | 2013-02-21 03:58:00 | [diff] [blame] | 139 | } |
| 140 | |
[email protected] | 3316d42 | 2013-05-03 21:45:30 | [diff] [blame] | 141 | scoped_ptr<QuicEncryptedPacket> ConstructConnectionClosePacket( |
| 142 | QuicPacketSequenceNumber num) { |
[email protected] | 1e96003 | 2013-12-20 19:00:20 | [diff] [blame] | 143 | return maker_.MakeConnectionClosePacket(num); |
[email protected] | 3316d42 | 2013-05-03 21:45:30 | [diff] [blame] | 144 | } |
| 145 | |
[email protected] | 61a52778 | 2013-02-21 03:58:00 | [diff] [blame] | 146 | scoped_ptr<QuicEncryptedPacket> ConstructAckPacket( |
| 147 | QuicPacketSequenceNumber largest_received, |
| 148 | QuicPacketSequenceNumber least_unacked) { |
[email protected] | 1e96003 | 2013-12-20 19:00:20 | [diff] [blame] | 149 | return maker_.MakeAckPacket(2, largest_received, least_unacked, true); |
| 150 | } |
[email protected] | 61a52778 | 2013-02-21 03:58:00 | [diff] [blame] | 151 | |
[email protected] | 1e96003 | 2013-12-20 19:00:20 | [diff] [blame] | 152 | SpdyHeaderBlock GetRequestHeaders(const std::string& method, |
| 153 | const std::string& scheme, |
| 154 | const std::string& path) { |
| 155 | return maker_.GetRequestHeaders(method, scheme, path); |
[email protected] | 61a52778 | 2013-02-21 03:58:00 | [diff] [blame] | 156 | } |
| 157 | |
[email protected] | 1e96003 | 2013-12-20 19:00:20 | [diff] [blame] | 158 | SpdyHeaderBlock GetResponseHeaders(const std::string& status) { |
| 159 | return maker_.GetResponseHeaders(status); |
[email protected] | 61a52778 | 2013-02-21 03:58:00 | [diff] [blame] | 160 | } |
| 161 | |
[email protected] | 1e96003 | 2013-12-20 19:00:20 | [diff] [blame] | 162 | scoped_ptr<QuicEncryptedPacket> ConstructDataPacket( |
[email protected] | 61a52778 | 2013-02-21 03:58:00 | [diff] [blame] | 163 | QuicPacketSequenceNumber sequence_number, |
[email protected] | 98b20ce | 2013-05-10 05:55:26 | [diff] [blame] | 164 | QuicStreamId stream_id, |
[email protected] | e8ff2684 | 2013-03-22 21:02:05 | [diff] [blame] | 165 | bool should_include_version, |
[email protected] | 61a52778 | 2013-02-21 03:58:00 | [diff] [blame] | 166 | bool fin, |
| 167 | QuicStreamOffset offset, |
| 168 | base::StringPiece data) { |
[email protected] | 1e96003 | 2013-12-20 19:00:20 | [diff] [blame] | 169 | return maker_.MakeDataPacket( |
| 170 | sequence_number, stream_id, should_include_version, fin, offset, data); |
[email protected] | 61a52778 | 2013-02-21 03:58:00 | [diff] [blame] | 171 | } |
| 172 | |
[email protected] | 1e96003 | 2013-12-20 19:00:20 | [diff] [blame] | 173 | scoped_ptr<QuicEncryptedPacket> ConstructRequestHeadersPacket( |
| 174 | QuicPacketSequenceNumber sequence_number, |
| 175 | QuicStreamId stream_id, |
| 176 | bool should_include_version, |
| 177 | bool fin, |
| 178 | const SpdyHeaderBlock& headers) { |
| 179 | return maker_.MakeRequestHeadersPacket( |
| 180 | sequence_number, stream_id, should_include_version, fin, headers); |
[email protected] | 61a52778 | 2013-02-21 03:58:00 | [diff] [blame] | 181 | } |
| 182 | |
[email protected] | 1e96003 | 2013-12-20 19:00:20 | [diff] [blame] | 183 | scoped_ptr<QuicEncryptedPacket> ConstructResponseHeadersPacket( |
| 184 | QuicPacketSequenceNumber sequence_number, |
| 185 | QuicStreamId stream_id, |
| 186 | bool should_include_version, |
| 187 | bool fin, |
| 188 | const SpdyHeaderBlock& headers) { |
| 189 | return maker_.MakeResponseHeadersPacket( |
| 190 | sequence_number, stream_id, should_include_version, fin, headers); |
[email protected] | 61a52778 | 2013-02-21 03:58:00 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | void CreateSession() { |
[email protected] | dda75ab | 2013-06-22 22:43:30 | [diff] [blame] | 194 | CreateSessionWithFactory(&socket_factory_); |
| 195 | } |
| 196 | |
| 197 | void CreateSessionWithFactory(ClientSocketFactory* socket_factory) { |
[email protected] | 4dca587c | 2013-03-07 16:54:47 | [diff] [blame] | 198 | params_.enable_quic = true; |
| 199 | params_.quic_clock = clock_; |
| 200 | params_.quic_random = &random_generator_; |
[email protected] | dda75ab | 2013-06-22 22:43:30 | [diff] [blame] | 201 | params_.client_socket_factory = socket_factory; |
[email protected] | e8ff2684 | 2013-03-22 21:02:05 | [diff] [blame] | 202 | params_.quic_crypto_client_stream_factory = &crypto_client_stream_factory_; |
[email protected] | 1c04f952 | 2013-02-21 20:32:43 | [diff] [blame] | 203 | params_.host_resolver = &host_resolver_; |
| 204 | params_.cert_verifier = &cert_verifier_; |
[email protected] | b1c988b | 2013-06-13 06:48:11 | [diff] [blame] | 205 | params_.transport_security_state = &transport_security_state_; |
[email protected] | 1c04f952 | 2013-02-21 20:32:43 | [diff] [blame] | 206 | params_.proxy_service = proxy_service_.get(); |
| 207 | params_.ssl_config_service = ssl_config_service_.get(); |
| 208 | params_.http_auth_handler_factory = auth_handler_factory_.get(); |
[email protected] | 30d4c02 | 2013-07-18 22:58:16 | [diff] [blame] | 209 | params_.http_server_properties = http_server_properties.GetWeakPtr(); |
[email protected] | 1e96003 | 2013-12-20 19:00:20 | [diff] [blame] | 210 | params_.quic_supported_versions = SupportedVersions(GetParam()); |
[email protected] | 61a52778 | 2013-02-21 03:58:00 | [diff] [blame] | 211 | |
| 212 | session_ = new HttpNetworkSession(params_); |
[email protected] | 11c0587 | 2013-08-20 02:04:12 | [diff] [blame] | 213 | session_->quic_stream_factory()->set_require_confirmation(false); |
[email protected] | 61a52778 | 2013-02-21 03:58:00 | [diff] [blame] | 214 | } |
| 215 | |
[email protected] | aa9b14d | 2013-05-10 23:45:19 | [diff] [blame] | 216 | void CheckWasQuicResponse(const scoped_ptr<HttpNetworkTransaction>& trans) { |
| 217 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 218 | ASSERT_TRUE(response != NULL); |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 219 | ASSERT_TRUE(response->headers.get() != NULL); |
[email protected] | aa9b14d | 2013-05-10 23:45:19 | [diff] [blame] | 220 | EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
| 221 | EXPECT_TRUE(response->was_fetched_via_spdy); |
| 222 | EXPECT_TRUE(response->was_npn_negotiated); |
| 223 | EXPECT_EQ(HttpResponseInfo::CONNECTION_INFO_QUIC1_SPDY3, |
| 224 | response->connection_info); |
| 225 | } |
| 226 | |
| 227 | void CheckWasHttpResponse(const scoped_ptr<HttpNetworkTransaction>& trans) { |
| 228 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 229 | ASSERT_TRUE(response != NULL); |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 230 | ASSERT_TRUE(response->headers.get() != NULL); |
[email protected] | aa9b14d | 2013-05-10 23:45:19 | [diff] [blame] | 231 | EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
| 232 | EXPECT_FALSE(response->was_fetched_via_spdy); |
| 233 | EXPECT_FALSE(response->was_npn_negotiated); |
| 234 | EXPECT_EQ(HttpResponseInfo::CONNECTION_INFO_HTTP1, |
| 235 | response->connection_info); |
| 236 | } |
| 237 | |
| 238 | void CheckResponseData(HttpNetworkTransaction* trans, |
| 239 | const std::string& expected) { |
| 240 | std::string response_data; |
| 241 | ASSERT_EQ(OK, ReadTransaction(trans, &response_data)); |
| 242 | EXPECT_EQ(expected, response_data); |
| 243 | } |
| 244 | |
| 245 | void RunTransaction(HttpNetworkTransaction* trans) { |
| 246 | TestCompletionCallback callback; |
| 247 | int rv = trans->Start(&request_, callback.callback(), net_log_.bound()); |
| 248 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 249 | EXPECT_EQ(OK, callback.WaitForResult()); |
| 250 | } |
| 251 | |
| 252 | void SendRequestAndExpectHttpResponse(const std::string& expected) { |
| 253 | scoped_ptr<HttpNetworkTransaction> trans( |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 254 | new HttpNetworkTransaction(DEFAULT_PRIORITY, session_.get())); |
[email protected] | aa9b14d | 2013-05-10 23:45:19 | [diff] [blame] | 255 | RunTransaction(trans.get()); |
| 256 | CheckWasHttpResponse(trans); |
| 257 | CheckResponseData(trans.get(), expected); |
| 258 | } |
| 259 | |
| 260 | void SendRequestAndExpectQuicResponse(const std::string& expected) { |
| 261 | scoped_ptr<HttpNetworkTransaction> trans( |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 262 | new HttpNetworkTransaction(DEFAULT_PRIORITY, session_.get())); |
[email protected] | aa9b14d | 2013-05-10 23:45:19 | [diff] [blame] | 263 | RunTransaction(trans.get()); |
| 264 | CheckWasQuicResponse(trans); |
| 265 | CheckResponseData(trans.get(), expected); |
| 266 | } |
| 267 | |
| 268 | void AddQuicAlternateProtocolMapping( |
| 269 | MockCryptoClientStream::HandshakeMode handshake_mode) { |
| 270 | crypto_client_stream_factory_.set_handshake_mode(handshake_mode); |
| 271 | session_->http_server_properties()->SetAlternateProtocol( |
| 272 | HostPortPair::FromURL(request_.url), 80, QUIC); |
| 273 | } |
| 274 | |
| 275 | void ExpectBrokenAlternateProtocolMapping() { |
| 276 | ASSERT_TRUE(session_->http_server_properties()->HasAlternateProtocol( |
| 277 | HostPortPair::FromURL(request_.url))); |
| 278 | const PortAlternateProtocolPair alternate = |
| 279 | session_->http_server_properties()->GetAlternateProtocol( |
| 280 | HostPortPair::FromURL(request_.url)); |
| 281 | EXPECT_EQ(ALTERNATE_PROTOCOL_BROKEN, alternate.protocol); |
| 282 | } |
| 283 | |
[email protected] | 4d590c9c | 2014-05-02 05:14:33 | [diff] [blame^] | 284 | void ExpectQuicAlternateProtocolMapping() { |
| 285 | ASSERT_TRUE(session_->http_server_properties()->HasAlternateProtocol( |
| 286 | HostPortPair::FromURL(request_.url))); |
| 287 | const PortAlternateProtocolPair alternate = |
| 288 | session_->http_server_properties()->GetAlternateProtocol( |
| 289 | HostPortPair::FromURL(request_.url)); |
| 290 | EXPECT_EQ(QUIC, alternate.protocol); |
| 291 | } |
| 292 | |
[email protected] | aa9b14d | 2013-05-10 23:45:19 | [diff] [blame] | 293 | void AddHangingNonAlternateProtocolSocketData() { |
[email protected] | dda75ab | 2013-06-22 22:43:30 | [diff] [blame] | 294 | MockConnect hanging_connect(SYNCHRONOUS, ERR_IO_PENDING); |
| 295 | hanging_data_.set_connect_data(hanging_connect); |
| 296 | socket_factory_.AddSocketDataProvider(&hanging_data_); |
[email protected] | aa9b14d | 2013-05-10 23:45:19 | [diff] [blame] | 297 | } |
| 298 | |
[email protected] | 1e96003 | 2013-12-20 19:00:20 | [diff] [blame] | 299 | QuicTestPacketMaker maker_; |
[email protected] | 61a52778 | 2013-02-21 03:58:00 | [diff] [blame] | 300 | scoped_refptr<HttpNetworkSession> session_; |
| 301 | MockClientSocketFactory socket_factory_; |
[email protected] | e8ff2684 | 2013-03-22 21:02:05 | [diff] [blame] | 302 | MockCryptoClientStreamFactory crypto_client_stream_factory_; |
[email protected] | 1c04f952 | 2013-02-21 20:32:43 | [diff] [blame] | 303 | MockClock* clock_; // Owned by QuicStreamFactory after CreateSession. |
| 304 | MockHostResolver host_resolver_; |
| 305 | MockCertVerifier cert_verifier_; |
[email protected] | b1c988b | 2013-06-13 06:48:11 | [diff] [blame] | 306 | TransportSecurityState transport_security_state_; |
[email protected] | 1c04f952 | 2013-02-21 20:32:43 | [diff] [blame] | 307 | scoped_refptr<SSLConfigServiceDefaults> ssl_config_service_; |
| 308 | scoped_ptr<ProxyService> proxy_service_; |
| 309 | scoped_ptr<HttpAuthHandlerFactory> auth_handler_factory_; |
[email protected] | 61a52778 | 2013-02-21 03:58:00 | [diff] [blame] | 310 | MockRandom random_generator_; |
| 311 | HttpServerPropertiesImpl http_server_properties; |
| 312 | HttpNetworkSession::Params params_; |
[email protected] | aa9b14d | 2013-05-10 23:45:19 | [diff] [blame] | 313 | HttpRequestInfo request_; |
| 314 | CapturingBoundNetLog net_log_; |
[email protected] | dda75ab | 2013-06-22 22:43:30 | [diff] [blame] | 315 | StaticSocketDataProvider hanging_data_; |
[email protected] | 61a52778 | 2013-02-21 03:58:00 | [diff] [blame] | 316 | }; |
| 317 | |
[email protected] | 1e96003 | 2013-12-20 19:00:20 | [diff] [blame] | 318 | INSTANTIATE_TEST_CASE_P(Version, QuicNetworkTransactionTest, |
| 319 | ::testing::ValuesIn(QuicSupportedVersions())); |
| 320 | |
| 321 | TEST_P(QuicNetworkTransactionTest, ForceQuic) { |
[email protected] | 49e8533 | 2013-06-04 04:18:03 | [diff] [blame] | 322 | params_.origin_to_force_quic_on = |
| 323 | HostPortPair::FromString("www.google.com:80"); |
[email protected] | 4dca587c | 2013-03-07 16:54:47 | [diff] [blame] | 324 | |
[email protected] | 1e96003 | 2013-12-20 19:00:20 | [diff] [blame] | 325 | MockQuicData mock_quic_data; |
[email protected] | 92bf17c | 2014-03-03 21:14:03 | [diff] [blame] | 326 | mock_quic_data.AddWrite( |
| 327 | ConstructRequestHeadersPacket(1, kStreamId5, true, true, |
| 328 | GetRequestHeaders("GET", "http", "/"))); |
| 329 | mock_quic_data.AddRead( |
| 330 | ConstructResponseHeadersPacket(1, kStreamId5, false, false, |
| 331 | GetResponseHeaders("200 OK"))); |
| 332 | mock_quic_data.AddRead( |
| 333 | ConstructDataPacket(2, kStreamId5, false, true, 0, "hello!")); |
| 334 | mock_quic_data.AddWrite(ConstructAckPacket(2, 1)); |
[email protected] | 1e96003 | 2013-12-20 19:00:20 | [diff] [blame] | 335 | mock_quic_data.AddRead(SYNCHRONOUS, 0); // EOF |
[email protected] | 4dca587c | 2013-03-07 16:54:47 | [diff] [blame] | 336 | |
[email protected] | 1e96003 | 2013-12-20 19:00:20 | [diff] [blame] | 337 | mock_quic_data.AddDelayedSocketDataToFactory(&socket_factory_, 1); |
[email protected] | 4dca587c | 2013-03-07 16:54:47 | [diff] [blame] | 338 | |
[email protected] | aa9b14d | 2013-05-10 23:45:19 | [diff] [blame] | 339 | // The non-alternate protocol job needs to hang in order to guarantee that |
| 340 | // the alternate-protocol job will "win". |
| 341 | AddHangingNonAlternateProtocolSocketData(); |
[email protected] | 4dca587c | 2013-03-07 16:54:47 | [diff] [blame] | 342 | |
| 343 | CreateSession(); |
[email protected] | 4dca587c | 2013-03-07 16:54:47 | [diff] [blame] | 344 | |
[email protected] | aa9b14d | 2013-05-10 23:45:19 | [diff] [blame] | 345 | SendRequestAndExpectQuicResponse("hello!"); |
[email protected] | 4dca587c | 2013-03-07 16:54:47 | [diff] [blame] | 346 | |
[email protected] | 98b20ce | 2013-05-10 05:55:26 | [diff] [blame] | 347 | // Check that the NetLog was filled reasonably. |
| 348 | net::CapturingNetLog::CapturedEntryList entries; |
[email protected] | aa9b14d | 2013-05-10 23:45:19 | [diff] [blame] | 349 | net_log_.GetEntries(&entries); |
[email protected] | 98b20ce | 2013-05-10 05:55:26 | [diff] [blame] | 350 | EXPECT_LT(0u, entries.size()); |
| 351 | |
| 352 | // Check that we logged a QUIC_SESSION_PACKET_RECEIVED. |
| 353 | int pos = net::ExpectLogContainsSomewhere( |
| 354 | entries, 0, |
| 355 | net::NetLog::TYPE_QUIC_SESSION_PACKET_RECEIVED, |
| 356 | net::NetLog::PHASE_NONE); |
| 357 | EXPECT_LT(0, pos); |
| 358 | |
| 359 | // ... and also a TYPE_QUIC_SESSION_PACKET_HEADER_RECEIVED. |
| 360 | pos = net::ExpectLogContainsSomewhere( |
| 361 | entries, 0, |
| 362 | net::NetLog::TYPE_QUIC_SESSION_PACKET_HEADER_RECEIVED, |
| 363 | net::NetLog::PHASE_NONE); |
| 364 | EXPECT_LT(0, pos); |
| 365 | |
| 366 | std::string packet_sequence_number; |
| 367 | ASSERT_TRUE(entries[pos].GetStringValue( |
| 368 | "packet_sequence_number", &packet_sequence_number)); |
| 369 | EXPECT_EQ("1", packet_sequence_number); |
| 370 | |
| 371 | // ... and also a QUIC_SESSION_STREAM_FRAME_RECEIVED. |
| 372 | pos = net::ExpectLogContainsSomewhere( |
| 373 | entries, 0, |
| 374 | net::NetLog::TYPE_QUIC_SESSION_STREAM_FRAME_RECEIVED, |
| 375 | net::NetLog::PHASE_NONE); |
| 376 | EXPECT_LT(0, pos); |
| 377 | |
| 378 | int log_stream_id; |
| 379 | ASSERT_TRUE(entries[pos].GetIntegerValue("stream_id", &log_stream_id)); |
[email protected] | 1e96003 | 2013-12-20 19:00:20 | [diff] [blame] | 380 | EXPECT_EQ(3, log_stream_id); |
[email protected] | 4dca587c | 2013-03-07 16:54:47 | [diff] [blame] | 381 | } |
| 382 | |
[email protected] | cf3e3cd6 | 2014-02-05 16:16:16 | [diff] [blame] | 383 | TEST_P(QuicNetworkTransactionTest, QuicProxy) { |
| 384 | proxy_service_.reset( |
| 385 | ProxyService::CreateFixedFromPacResult("QUIC myproxy:70")); |
| 386 | |
[email protected] | cf3e3cd6 | 2014-02-05 16:16:16 | [diff] [blame] | 387 | MockQuicData mock_quic_data; |
[email protected] | 92bf17c | 2014-03-03 21:14:03 | [diff] [blame] | 388 | mock_quic_data.AddWrite( |
| 389 | ConstructRequestHeadersPacket(1, kStreamId5, true, true, |
| 390 | GetRequestHeaders("GET", "http", "/"))); |
| 391 | mock_quic_data.AddRead( |
| 392 | ConstructResponseHeadersPacket(1, kStreamId5, false, false, |
| 393 | GetResponseHeaders("200 OK"))); |
| 394 | mock_quic_data.AddRead( |
| 395 | ConstructDataPacket(2, kStreamId5, false, true, 0, "hello!")); |
| 396 | mock_quic_data.AddWrite(ConstructAckPacket(2, 1)); |
[email protected] | cf3e3cd6 | 2014-02-05 16:16:16 | [diff] [blame] | 397 | mock_quic_data.AddRead(SYNCHRONOUS, 0); // EOF |
| 398 | |
| 399 | mock_quic_data.AddDelayedSocketDataToFactory(&socket_factory_, 1); |
| 400 | |
| 401 | // There is no need to set up an alternate protocol job, because |
| 402 | // no attempt will be made to speak to the proxy over TCP. |
| 403 | |
| 404 | CreateSession(); |
| 405 | |
| 406 | SendRequestAndExpectQuicResponse("hello!"); |
| 407 | } |
| 408 | |
[email protected] | 1e96003 | 2013-12-20 19:00:20 | [diff] [blame] | 409 | TEST_P(QuicNetworkTransactionTest, ForceQuicWithErrorConnecting) { |
[email protected] | 49e8533 | 2013-06-04 04:18:03 | [diff] [blame] | 410 | params_.origin_to_force_quic_on = |
| 411 | HostPortPair::FromString("www.google.com:80"); |
[email protected] | cebe328 | 2013-05-22 23:49:30 | [diff] [blame] | 412 | |
[email protected] | 1e96003 | 2013-12-20 19:00:20 | [diff] [blame] | 413 | MockQuicData mock_quic_data; |
| 414 | mock_quic_data.AddRead(ASYNC, ERR_SOCKET_NOT_CONNECTED); |
| 415 | |
| 416 | mock_quic_data.AddDelayedSocketDataToFactory(&socket_factory_, 0); |
[email protected] | cebe328 | 2013-05-22 23:49:30 | [diff] [blame] | 417 | |
| 418 | CreateSession(); |
| 419 | |
| 420 | scoped_ptr<HttpNetworkTransaction> trans( |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 421 | new HttpNetworkTransaction(DEFAULT_PRIORITY, session_.get())); |
[email protected] | cebe328 | 2013-05-22 23:49:30 | [diff] [blame] | 422 | TestCompletionCallback callback; |
| 423 | int rv = trans->Start(&request_, callback.callback(), net_log_.bound()); |
| 424 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 0b2294d3 | 2013-08-02 00:46:36 | [diff] [blame] | 425 | EXPECT_EQ(ERR_CONNECTION_CLOSED, callback.WaitForResult()); |
[email protected] | cebe328 | 2013-05-22 23:49:30 | [diff] [blame] | 426 | } |
| 427 | |
[email protected] | 1e96003 | 2013-12-20 19:00:20 | [diff] [blame] | 428 | TEST_P(QuicNetworkTransactionTest, DoNotForceQuicForHttps) { |
[email protected] | 4dca587c | 2013-03-07 16:54:47 | [diff] [blame] | 429 | // Attempt to "force" quic on 443, which will not be honored. |
[email protected] | 49e8533 | 2013-06-04 04:18:03 | [diff] [blame] | 430 | params_.origin_to_force_quic_on = |
| 431 | HostPortPair::FromString("www.google.com:443"); |
[email protected] | 4dca587c | 2013-03-07 16:54:47 | [diff] [blame] | 432 | |
[email protected] | aa9b14d | 2013-05-10 23:45:19 | [diff] [blame] | 433 | MockRead http_reads[] = { |
[email protected] | 4dca587c | 2013-03-07 16:54:47 | [diff] [blame] | 434 | MockRead("HTTP/1.1 200 OK\r\n\r\n"), |
| 435 | MockRead("hello world"), |
| 436 | MockRead(SYNCHRONOUS, ERR_TEST_PEER_CLOSE_AFTER_NEXT_MOCK_READ), |
| 437 | MockRead(ASYNC, OK) |
| 438 | }; |
| 439 | |
[email protected] | aa9b14d | 2013-05-10 23:45:19 | [diff] [blame] | 440 | StaticSocketDataProvider data(http_reads, arraysize(http_reads), NULL, 0); |
[email protected] | 4dca587c | 2013-03-07 16:54:47 | [diff] [blame] | 441 | socket_factory_.AddSocketDataProvider(&data); |
| 442 | SSLSocketDataProvider ssl(ASYNC, OK); |
| 443 | socket_factory_.AddSSLSocketDataProvider(&ssl); |
| 444 | |
[email protected] | 4dca587c | 2013-03-07 16:54:47 | [diff] [blame] | 445 | CreateSession(); |
[email protected] | 4dca587c | 2013-03-07 16:54:47 | [diff] [blame] | 446 | |
[email protected] | aa9b14d | 2013-05-10 23:45:19 | [diff] [blame] | 447 | SendRequestAndExpectHttpResponse("hello world"); |
[email protected] | 4dca587c | 2013-03-07 16:54:47 | [diff] [blame] | 448 | } |
| 449 | |
[email protected] | 1e96003 | 2013-12-20 19:00:20 | [diff] [blame] | 450 | TEST_P(QuicNetworkTransactionTest, UseAlternateProtocolForQuic) { |
[email protected] | b05bcaa3 | 2013-10-06 05:26:02 | [diff] [blame] | 451 | HttpStreamFactory::EnableNpnSpdy3(); // Enables QUIC too. |
[email protected] | 61a52778 | 2013-02-21 03:58:00 | [diff] [blame] | 452 | |
[email protected] | aa9b14d | 2013-05-10 23:45:19 | [diff] [blame] | 453 | MockRead http_reads[] = { |
[email protected] | 61a52778 | 2013-02-21 03:58:00 | [diff] [blame] | 454 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 455 | MockRead(kQuicAlternateProtocolHttpHeader), |
| 456 | MockRead("hello world"), |
| 457 | MockRead(SYNCHRONOUS, ERR_TEST_PEER_CLOSE_AFTER_NEXT_MOCK_READ), |
| 458 | MockRead(ASYNC, OK) |
| 459 | }; |
| 460 | |
[email protected] | aa9b14d | 2013-05-10 23:45:19 | [diff] [blame] | 461 | StaticSocketDataProvider http_data(http_reads, arraysize(http_reads), |
| 462 | NULL, 0); |
| 463 | socket_factory_.AddSocketDataProvider(&http_data); |
[email protected] | 61a52778 | 2013-02-21 03:58:00 | [diff] [blame] | 464 | |
[email protected] | 1e96003 | 2013-12-20 19:00:20 | [diff] [blame] | 465 | MockQuicData mock_quic_data; |
[email protected] | 92bf17c | 2014-03-03 21:14:03 | [diff] [blame] | 466 | mock_quic_data.AddWrite( |
| 467 | ConstructRequestHeadersPacket(1, kStreamId5, true, true, |
| 468 | GetRequestHeaders("GET", "http", "/"))); |
| 469 | mock_quic_data.AddRead( |
| 470 | ConstructResponseHeadersPacket(1, kStreamId5, false, false, |
| 471 | GetResponseHeaders("200 OK"))); |
| 472 | mock_quic_data.AddRead( |
| 473 | ConstructDataPacket(2, kStreamId5, false, true, 0, "hello!")); |
| 474 | mock_quic_data.AddWrite(ConstructAckPacket(2, 1)); |
[email protected] | 1e96003 | 2013-12-20 19:00:20 | [diff] [blame] | 475 | mock_quic_data.AddRead(SYNCHRONOUS, 0); // EOF |
[email protected] | 61a52778 | 2013-02-21 03:58:00 | [diff] [blame] | 476 | |
[email protected] | 1e96003 | 2013-12-20 19:00:20 | [diff] [blame] | 477 | mock_quic_data.AddDelayedSocketDataToFactory(&socket_factory_, 1); |
[email protected] | 61a52778 | 2013-02-21 03:58:00 | [diff] [blame] | 478 | |
[email protected] | aa9b14d | 2013-05-10 23:45:19 | [diff] [blame] | 479 | // The non-alternate protocol job needs to hang in order to guarantee that |
| 480 | // the alternate-protocol job will "win". |
| 481 | AddHangingNonAlternateProtocolSocketData(); |
[email protected] | 61a52778 | 2013-02-21 03:58:00 | [diff] [blame] | 482 | |
| 483 | CreateSession(); |
[email protected] | 61a52778 | 2013-02-21 03:58:00 | [diff] [blame] | 484 | |
[email protected] | aa9b14d | 2013-05-10 23:45:19 | [diff] [blame] | 485 | SendRequestAndExpectHttpResponse("hello world"); |
| 486 | SendRequestAndExpectQuicResponse("hello!"); |
[email protected] | 61a52778 | 2013-02-21 03:58:00 | [diff] [blame] | 487 | } |
| 488 | |
[email protected] | 1e96003 | 2013-12-20 19:00:20 | [diff] [blame] | 489 | TEST_P(QuicNetworkTransactionTest, UseAlternateProtocolForQuicForHttps) { |
[email protected] | 6d1b4ed | 2013-07-10 03:57:54 | [diff] [blame] | 490 | params_.origin_to_force_quic_on = |
| 491 | HostPortPair::FromString("www.google.com:443"); |
| 492 | params_.enable_quic_https = true; |
[email protected] | b05bcaa3 | 2013-10-06 05:26:02 | [diff] [blame] | 493 | HttpStreamFactory::EnableNpnSpdy3(); // Enables QUIC too. |
[email protected] | 6d1b4ed | 2013-07-10 03:57:54 | [diff] [blame] | 494 | |
| 495 | MockRead http_reads[] = { |
| 496 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 497 | MockRead(kQuicAlternateProtocolHttpsHeader), |
| 498 | MockRead("hello world"), |
| 499 | MockRead(SYNCHRONOUS, ERR_TEST_PEER_CLOSE_AFTER_NEXT_MOCK_READ), |
| 500 | MockRead(ASYNC, OK) |
| 501 | }; |
| 502 | |
| 503 | StaticSocketDataProvider http_data(http_reads, arraysize(http_reads), |
| 504 | NULL, 0); |
| 505 | socket_factory_.AddSocketDataProvider(&http_data); |
| 506 | |
[email protected] | 1e96003 | 2013-12-20 19:00:20 | [diff] [blame] | 507 | MockQuicData mock_quic_data; |
[email protected] | 92bf17c | 2014-03-03 21:14:03 | [diff] [blame] | 508 | mock_quic_data.AddWrite( |
| 509 | ConstructRequestHeadersPacket(1, kStreamId5, true, true, |
| 510 | GetRequestHeaders("GET", "http", "/"))); |
| 511 | mock_quic_data.AddRead( |
| 512 | ConstructResponseHeadersPacket(1, kStreamId5, false, false, |
| 513 | GetResponseHeaders("200 OK"))); |
| 514 | mock_quic_data.AddRead( |
| 515 | ConstructDataPacket(2, kStreamId5, false, true, 0, "hello!")); |
| 516 | mock_quic_data.AddWrite(ConstructAckPacket(2, 1)); |
[email protected] | 1e96003 | 2013-12-20 19:00:20 | [diff] [blame] | 517 | mock_quic_data.AddRead(SYNCHRONOUS, 0); // EOF |
[email protected] | 6d1b4ed | 2013-07-10 03:57:54 | [diff] [blame] | 518 | |
[email protected] | 1e96003 | 2013-12-20 19:00:20 | [diff] [blame] | 519 | mock_quic_data.AddDelayedSocketDataToFactory(&socket_factory_, 1); |
[email protected] | 6d1b4ed | 2013-07-10 03:57:54 | [diff] [blame] | 520 | |
| 521 | // The non-alternate protocol job needs to hang in order to guarantee that |
| 522 | // the alternate-protocol job will "win". |
| 523 | AddHangingNonAlternateProtocolSocketData(); |
| 524 | |
| 525 | CreateSession(); |
| 526 | |
| 527 | // TODO(rtenneti): Test QUIC over HTTPS, GetSSLInfo(). |
| 528 | SendRequestAndExpectHttpResponse("hello world"); |
| 529 | } |
| 530 | |
[email protected] | 1e96003 | 2013-12-20 19:00:20 | [diff] [blame] | 531 | TEST_P(QuicNetworkTransactionTest, HungAlternateProtocol) { |
[email protected] | b05bcaa3 | 2013-10-06 05:26:02 | [diff] [blame] | 532 | HttpStreamFactory::EnableNpnSpdy3(); // Enables QUIC too. |
[email protected] | dda75ab | 2013-06-22 22:43:30 | [diff] [blame] | 533 | crypto_client_stream_factory_.set_handshake_mode( |
| 534 | MockCryptoClientStream::COLD_START); |
| 535 | |
| 536 | MockWrite http_writes[] = { |
| 537 | MockWrite(SYNCHRONOUS, 0, "GET / HTTP/1.1\r\n"), |
| 538 | MockWrite(SYNCHRONOUS, 1, "Host: www.google.com\r\n"), |
| 539 | MockWrite(SYNCHRONOUS, 2, "Connection: keep-alive\r\n\r\n") |
| 540 | }; |
| 541 | |
| 542 | MockRead http_reads[] = { |
| 543 | MockRead(SYNCHRONOUS, 3, "HTTP/1.1 200 OK\r\n"), |
| 544 | MockRead(SYNCHRONOUS, 4, kQuicAlternateProtocolHttpHeader), |
| 545 | MockRead(SYNCHRONOUS, 5, "hello world"), |
| 546 | MockRead(SYNCHRONOUS, OK, 6) |
| 547 | }; |
| 548 | |
| 549 | DeterministicMockClientSocketFactory socket_factory; |
| 550 | |
| 551 | DeterministicSocketData http_data(http_reads, arraysize(http_reads), |
| 552 | http_writes, arraysize(http_writes)); |
| 553 | socket_factory.AddSocketDataProvider(&http_data); |
| 554 | |
| 555 | // The QUIC transaction will not be allowed to complete. |
| 556 | MockWrite quic_writes[] = { |
| 557 | MockWrite(ASYNC, ERR_IO_PENDING, 0) |
| 558 | }; |
| 559 | MockRead quic_reads[] = { |
| 560 | MockRead(ASYNC, ERR_IO_PENDING, 1), |
| 561 | }; |
| 562 | DeterministicSocketData quic_data(quic_reads, arraysize(quic_reads), |
| 563 | quic_writes, arraysize(quic_writes)); |
| 564 | socket_factory.AddSocketDataProvider(&quic_data); |
| 565 | |
| 566 | // The HTTP transaction will complete. |
| 567 | DeterministicSocketData http_data2(http_reads, arraysize(http_reads), |
| 568 | http_writes, arraysize(http_writes)); |
| 569 | socket_factory.AddSocketDataProvider(&http_data2); |
| 570 | |
| 571 | CreateSessionWithFactory(&socket_factory); |
| 572 | |
| 573 | // Run the first request. |
| 574 | http_data.StopAfter(arraysize(http_reads) + arraysize(http_writes)); |
| 575 | SendRequestAndExpectHttpResponse("hello world"); |
| 576 | ASSERT_TRUE(http_data.at_read_eof()); |
| 577 | ASSERT_TRUE(http_data.at_write_eof()); |
| 578 | |
| 579 | // Now run the second request in which the QUIC socket hangs, |
| 580 | // and verify the the transaction continues over HTTP. |
| 581 | http_data2.StopAfter(arraysize(http_reads) + arraysize(http_writes)); |
| 582 | SendRequestAndExpectHttpResponse("hello world"); |
| 583 | |
| 584 | ASSERT_TRUE(http_data2.at_read_eof()); |
| 585 | ASSERT_TRUE(http_data2.at_write_eof()); |
| 586 | ASSERT_TRUE(!quic_data.at_read_eof()); |
| 587 | ASSERT_TRUE(!quic_data.at_write_eof()); |
| 588 | } |
| 589 | |
[email protected] | 1e96003 | 2013-12-20 19:00:20 | [diff] [blame] | 590 | TEST_P(QuicNetworkTransactionTest, ZeroRTTWithHttpRace) { |
[email protected] | b05bcaa3 | 2013-10-06 05:26:02 | [diff] [blame] | 591 | HttpStreamFactory::EnableNpnSpdy3(); // Enables QUIC too. |
[email protected] | 8ba8121 | 2013-05-03 13:11:48 | [diff] [blame] | 592 | |
[email protected] | 1e96003 | 2013-12-20 19:00:20 | [diff] [blame] | 593 | MockQuicData mock_quic_data; |
[email protected] | 92bf17c | 2014-03-03 21:14:03 | [diff] [blame] | 594 | mock_quic_data.AddWrite( |
| 595 | ConstructRequestHeadersPacket(1, kStreamId5, true, true, |
| 596 | GetRequestHeaders("GET", "http", "/"))); |
| 597 | mock_quic_data.AddRead( |
| 598 | ConstructResponseHeadersPacket(1, kStreamId5, false, false, |
| 599 | GetResponseHeaders("200 OK"))); |
| 600 | mock_quic_data.AddRead( |
| 601 | ConstructDataPacket(2, kStreamId5, false, true, 0, "hello!")); |
| 602 | mock_quic_data.AddWrite(ConstructAckPacket(2, 1)); |
[email protected] | 1e96003 | 2013-12-20 19:00:20 | [diff] [blame] | 603 | mock_quic_data.AddRead(SYNCHRONOUS, 0); // EOF |
[email protected] | 8ba8121 | 2013-05-03 13:11:48 | [diff] [blame] | 604 | |
[email protected] | 1e96003 | 2013-12-20 19:00:20 | [diff] [blame] | 605 | mock_quic_data.AddDelayedSocketDataToFactory(&socket_factory_, 1); |
[email protected] | 8ba8121 | 2013-05-03 13:11:48 | [diff] [blame] | 606 | |
[email protected] | 3a120a6b | 2013-06-25 01:08:27 | [diff] [blame] | 607 | // The non-alternate protocol job needs to hang in order to guarantee that |
| 608 | // the alternate-protocol job will "win". |
[email protected] | dda75ab | 2013-06-22 22:43:30 | [diff] [blame] | 609 | AddHangingNonAlternateProtocolSocketData(); |
| 610 | |
[email protected] | 8ba8121 | 2013-05-03 13:11:48 | [diff] [blame] | 611 | CreateSession(); |
[email protected] | aa9b14d | 2013-05-10 23:45:19 | [diff] [blame] | 612 | AddQuicAlternateProtocolMapping(MockCryptoClientStream::ZERO_RTT); |
| 613 | SendRequestAndExpectQuicResponse("hello!"); |
[email protected] | 8ba8121 | 2013-05-03 13:11:48 | [diff] [blame] | 614 | } |
| 615 | |
[email protected] | 1e96003 | 2013-12-20 19:00:20 | [diff] [blame] | 616 | TEST_P(QuicNetworkTransactionTest, ZeroRTTWithNoHttpRace) { |
[email protected] | b05bcaa3 | 2013-10-06 05:26:02 | [diff] [blame] | 617 | HttpStreamFactory::EnableNpnSpdy3(); // Enables QUIC too. |
[email protected] | 3a120a6b | 2013-06-25 01:08:27 | [diff] [blame] | 618 | |
[email protected] | 1e96003 | 2013-12-20 19:00:20 | [diff] [blame] | 619 | MockQuicData mock_quic_data; |
[email protected] | 92bf17c | 2014-03-03 21:14:03 | [diff] [blame] | 620 | mock_quic_data.AddWrite( |
| 621 | ConstructRequestHeadersPacket(1, kStreamId5, true, true, |
| 622 | GetRequestHeaders("GET", "http", "/"))); |
| 623 | mock_quic_data.AddRead( |
| 624 | ConstructResponseHeadersPacket(1, kStreamId5, false, false, |
| 625 | GetResponseHeaders("200 OK"))); |
| 626 | mock_quic_data.AddRead( |
| 627 | ConstructDataPacket(2, kStreamId5, false, true, 0, "hello!")); |
| 628 | mock_quic_data.AddWrite(ConstructAckPacket(2, 1)); |
[email protected] | 1e96003 | 2013-12-20 19:00:20 | [diff] [blame] | 629 | mock_quic_data.AddRead(SYNCHRONOUS, 0); // EOF |
| 630 | mock_quic_data.AddDelayedSocketDataToFactory(&socket_factory_, 1); |
[email protected] | 3a120a6b | 2013-06-25 01:08:27 | [diff] [blame] | 631 | |
| 632 | // In order for a new QUIC session to be established via alternate-protocol |
| 633 | // without racing an HTTP connection, we need the host resolution to happen |
| 634 | // synchronously. |
| 635 | host_resolver_.set_synchronous_mode(true); |
| 636 | host_resolver_.rules()->AddIPLiteralRule("www.google.com", "192.168.0.1", ""); |
[email protected] | 5109c195 | 2013-08-20 18:44:10 | [diff] [blame] | 637 | HostResolver::RequestInfo info(HostPortPair("www.google.com", 80)); |
[email protected] | 3a120a6b | 2013-06-25 01:08:27 | [diff] [blame] | 638 | AddressList address; |
[email protected] | 5109c195 | 2013-08-20 18:44:10 | [diff] [blame] | 639 | host_resolver_.Resolve(info, |
| 640 | DEFAULT_PRIORITY, |
| 641 | &address, |
| 642 | CompletionCallback(), |
| 643 | NULL, |
[email protected] | 3a120a6b | 2013-06-25 01:08:27 | [diff] [blame] | 644 | net_log_.bound()); |
| 645 | |
| 646 | CreateSession(); |
| 647 | AddQuicAlternateProtocolMapping(MockCryptoClientStream::ZERO_RTT); |
| 648 | SendRequestAndExpectQuicResponse("hello!"); |
| 649 | } |
| 650 | |
[email protected] | 0fc924b | 2014-03-31 04:34:15 | [diff] [blame] | 651 | TEST_P(QuicNetworkTransactionTest, ZeroRTTWithProxy) { |
| 652 | proxy_service_.reset( |
| 653 | ProxyService::CreateFixedFromPacResult("PROXY myproxy:70")); |
| 654 | HttpStreamFactory::EnableNpnSpdy3(); // Enables QUIC too. |
| 655 | |
| 656 | // Since we are using a proxy, the QUIC job will not succeed. |
| 657 | MockWrite http_writes[] = { |
| 658 | MockWrite(SYNCHRONOUS, 0, "GET http://www.google.com/ HTTP/1.1\r\n"), |
| 659 | MockWrite(SYNCHRONOUS, 1, "Host: www.google.com\r\n"), |
| 660 | MockWrite(SYNCHRONOUS, 2, "Proxy-Connection: keep-alive\r\n\r\n") |
| 661 | }; |
| 662 | |
| 663 | MockRead http_reads[] = { |
| 664 | MockRead(SYNCHRONOUS, 3, "HTTP/1.1 200 OK\r\n"), |
| 665 | MockRead(SYNCHRONOUS, 4, kQuicAlternateProtocolHttpHeader), |
| 666 | MockRead(SYNCHRONOUS, 5, "hello world"), |
| 667 | MockRead(SYNCHRONOUS, OK, 6) |
| 668 | }; |
| 669 | |
| 670 | StaticSocketDataProvider http_data(http_reads, arraysize(http_reads), |
| 671 | http_writes, arraysize(http_writes)); |
| 672 | socket_factory_.AddSocketDataProvider(&http_data); |
| 673 | |
| 674 | // In order for a new QUIC session to be established via alternate-protocol |
| 675 | // without racing an HTTP connection, we need the host resolution to happen |
| 676 | // synchronously. |
| 677 | host_resolver_.set_synchronous_mode(true); |
| 678 | host_resolver_.rules()->AddIPLiteralRule("www.google.com", "192.168.0.1", ""); |
| 679 | HostResolver::RequestInfo info(HostPortPair("www.google.com", 80)); |
| 680 | AddressList address; |
| 681 | host_resolver_.Resolve(info, |
| 682 | DEFAULT_PRIORITY, |
| 683 | &address, |
| 684 | CompletionCallback(), |
| 685 | NULL, |
| 686 | net_log_.bound()); |
| 687 | |
| 688 | CreateSession(); |
| 689 | AddQuicAlternateProtocolMapping(MockCryptoClientStream::ZERO_RTT); |
| 690 | SendRequestAndExpectHttpResponse("hello world"); |
| 691 | } |
| 692 | |
[email protected] | 1e96003 | 2013-12-20 19:00:20 | [diff] [blame] | 693 | TEST_P(QuicNetworkTransactionTest, ZeroRTTWithConfirmationRequired) { |
[email protected] | b05bcaa3 | 2013-10-06 05:26:02 | [diff] [blame] | 694 | HttpStreamFactory::EnableNpnSpdy3(); // Enables QUIC too. |
[email protected] | 11c0587 | 2013-08-20 02:04:12 | [diff] [blame] | 695 | |
[email protected] | 1e96003 | 2013-12-20 19:00:20 | [diff] [blame] | 696 | MockQuicData mock_quic_data; |
[email protected] | 92bf17c | 2014-03-03 21:14:03 | [diff] [blame] | 697 | mock_quic_data.AddWrite( |
| 698 | ConstructRequestHeadersPacket(1, kStreamId5, true, true, |
| 699 | GetRequestHeaders("GET", "http", "/"))); |
| 700 | mock_quic_data.AddRead( |
| 701 | ConstructResponseHeadersPacket(1, kStreamId5, false, false, |
| 702 | GetResponseHeaders("200 OK"))); |
| 703 | mock_quic_data.AddRead( |
| 704 | ConstructDataPacket(2, kStreamId5, false, true, 0, "hello!")); |
| 705 | mock_quic_data.AddWrite(ConstructAckPacket(2, 1)); |
[email protected] | 1e96003 | 2013-12-20 19:00:20 | [diff] [blame] | 706 | mock_quic_data.AddRead(SYNCHRONOUS, 0); // EOF |
| 707 | mock_quic_data.AddDelayedSocketDataToFactory(&socket_factory_, 1); |
[email protected] | 11c0587 | 2013-08-20 02:04:12 | [diff] [blame] | 708 | |
| 709 | // The non-alternate protocol job needs to hang in order to guarantee that |
| 710 | // the alternate-protocol job will "win". |
| 711 | AddHangingNonAlternateProtocolSocketData(); |
| 712 | |
| 713 | // In order for a new QUIC session to be established via alternate-protocol |
| 714 | // without racing an HTTP connection, we need the host resolution to happen |
| 715 | // synchronously. Of course, even though QUIC *could* perform a 0-RTT |
| 716 | // connection to the the server, in this test we require confirmation |
| 717 | // before encrypting so the HTTP job will still start. |
| 718 | host_resolver_.set_synchronous_mode(true); |
| 719 | host_resolver_.rules()->AddIPLiteralRule("www.google.com", "192.168.0.1", ""); |
[email protected] | 5109c195 | 2013-08-20 18:44:10 | [diff] [blame] | 720 | HostResolver::RequestInfo info(HostPortPair("www.google.com", 80)); |
[email protected] | 11c0587 | 2013-08-20 02:04:12 | [diff] [blame] | 721 | AddressList address; |
[email protected] | 5109c195 | 2013-08-20 18:44:10 | [diff] [blame] | 722 | host_resolver_.Resolve(info, DEFAULT_PRIORITY, &address, |
| 723 | CompletionCallback(), NULL, net_log_.bound()); |
[email protected] | 11c0587 | 2013-08-20 02:04:12 | [diff] [blame] | 724 | |
| 725 | CreateSession(); |
| 726 | session_->quic_stream_factory()->set_require_confirmation(true); |
| 727 | AddQuicAlternateProtocolMapping(MockCryptoClientStream::ZERO_RTT); |
| 728 | |
| 729 | scoped_ptr<HttpNetworkTransaction> trans( |
| 730 | new HttpNetworkTransaction(DEFAULT_PRIORITY, session_.get())); |
| 731 | TestCompletionCallback callback; |
| 732 | int rv = trans->Start(&request_, callback.callback(), net_log_.bound()); |
| 733 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 734 | |
| 735 | crypto_client_stream_factory_.last_stream()->SendOnCryptoHandshakeEvent( |
| 736 | QuicSession::HANDSHAKE_CONFIRMED); |
| 737 | EXPECT_EQ(OK, callback.WaitForResult()); |
| 738 | } |
| 739 | |
[email protected] | 1e96003 | 2013-12-20 19:00:20 | [diff] [blame] | 740 | TEST_P(QuicNetworkTransactionTest, BrokenAlternateProtocol) { |
[email protected] | b05bcaa3 | 2013-10-06 05:26:02 | [diff] [blame] | 741 | HttpStreamFactory::EnableNpnSpdy3(); // Enables QUIC too. |
[email protected] | 3316d42 | 2013-05-03 21:45:30 | [diff] [blame] | 742 | |
[email protected] | 3316d42 | 2013-05-03 21:45:30 | [diff] [blame] | 743 | // Alternate-protocol job |
| 744 | scoped_ptr<QuicEncryptedPacket> close(ConstructConnectionClosePacket(1)); |
| 745 | MockRead quic_reads[] = { |
| 746 | MockRead(ASYNC, close->data(), close->length()), |
| 747 | MockRead(ASYNC, OK), // EOF |
| 748 | }; |
| 749 | StaticSocketDataProvider quic_data(quic_reads, arraysize(quic_reads), |
| 750 | NULL, 0); |
| 751 | socket_factory_.AddSocketDataProvider(&quic_data); |
| 752 | |
| 753 | // Main job which will succeed even though the alternate job fails. |
| 754 | MockRead http_reads[] = { |
| 755 | MockRead("HTTP/1.1 200 OK\r\n\r\n"), |
| 756 | MockRead("hello from http"), |
| 757 | MockRead(SYNCHRONOUS, ERR_TEST_PEER_CLOSE_AFTER_NEXT_MOCK_READ), |
| 758 | MockRead(ASYNC, OK) |
| 759 | }; |
| 760 | |
| 761 | StaticSocketDataProvider http_data(http_reads, arraysize(http_reads), |
| 762 | NULL, 0); |
| 763 | socket_factory_.AddSocketDataProvider(&http_data); |
| 764 | |
[email protected] | 3316d42 | 2013-05-03 21:45:30 | [diff] [blame] | 765 | CreateSession(); |
[email protected] | aa9b14d | 2013-05-10 23:45:19 | [diff] [blame] | 766 | AddQuicAlternateProtocolMapping(MockCryptoClientStream::COLD_START); |
| 767 | SendRequestAndExpectHttpResponse("hello from http"); |
| 768 | ExpectBrokenAlternateProtocolMapping(); |
[email protected] | 3316d42 | 2013-05-03 21:45:30 | [diff] [blame] | 769 | } |
| 770 | |
[email protected] | 1e96003 | 2013-12-20 19:00:20 | [diff] [blame] | 771 | TEST_P(QuicNetworkTransactionTest, BrokenAlternateProtocolReadError) { |
[email protected] | b05bcaa3 | 2013-10-06 05:26:02 | [diff] [blame] | 772 | HttpStreamFactory::EnableNpnSpdy3(); // Enables QUIC too. |
[email protected] | d03a66d | 2013-05-06 12:55:59 | [diff] [blame] | 773 | |
[email protected] | d03a66d | 2013-05-06 12:55:59 | [diff] [blame] | 774 | // Alternate-protocol job |
| 775 | MockRead quic_reads[] = { |
| 776 | MockRead(ASYNC, ERR_SOCKET_NOT_CONNECTED), |
| 777 | }; |
| 778 | StaticSocketDataProvider quic_data(quic_reads, arraysize(quic_reads), |
| 779 | NULL, 0); |
| 780 | socket_factory_.AddSocketDataProvider(&quic_data); |
| 781 | |
| 782 | // Main job which will succeed even though the alternate job fails. |
| 783 | MockRead http_reads[] = { |
| 784 | MockRead("HTTP/1.1 200 OK\r\n\r\n"), |
| 785 | MockRead("hello from http"), |
| 786 | MockRead(SYNCHRONOUS, ERR_TEST_PEER_CLOSE_AFTER_NEXT_MOCK_READ), |
| 787 | MockRead(ASYNC, OK) |
| 788 | }; |
| 789 | |
| 790 | StaticSocketDataProvider http_data(http_reads, arraysize(http_reads), |
| 791 | NULL, 0); |
| 792 | socket_factory_.AddSocketDataProvider(&http_data); |
| 793 | |
[email protected] | d03a66d | 2013-05-06 12:55:59 | [diff] [blame] | 794 | CreateSession(); |
[email protected] | d03a66d | 2013-05-06 12:55:59 | [diff] [blame] | 795 | |
[email protected] | aa9b14d | 2013-05-10 23:45:19 | [diff] [blame] | 796 | AddQuicAlternateProtocolMapping(MockCryptoClientStream::COLD_START); |
| 797 | SendRequestAndExpectHttpResponse("hello from http"); |
| 798 | ExpectBrokenAlternateProtocolMapping(); |
[email protected] | d03a66d | 2013-05-06 12:55:59 | [diff] [blame] | 799 | } |
| 800 | |
[email protected] | 1e96003 | 2013-12-20 19:00:20 | [diff] [blame] | 801 | TEST_P(QuicNetworkTransactionTest, FailedZeroRttBrokenAlternateProtocol) { |
[email protected] | b05bcaa3 | 2013-10-06 05:26:02 | [diff] [blame] | 802 | HttpStreamFactory::EnableNpnSpdy3(); // Enables QUIC too. |
[email protected] | 77c6c16 | 2013-08-17 02:57:45 | [diff] [blame] | 803 | |
| 804 | // Alternate-protocol job |
| 805 | MockRead quic_reads[] = { |
| 806 | MockRead(ASYNC, ERR_SOCKET_NOT_CONNECTED), |
| 807 | }; |
| 808 | StaticSocketDataProvider quic_data(quic_reads, arraysize(quic_reads), |
| 809 | NULL, 0); |
| 810 | socket_factory_.AddSocketDataProvider(&quic_data); |
| 811 | |
| 812 | AddHangingNonAlternateProtocolSocketData(); |
| 813 | |
[email protected] | 4d283b3 | 2013-10-17 12:57:27 | [diff] [blame] | 814 | // Final job that will proceed when the QUIC job fails. |
| 815 | MockRead http_reads[] = { |
| 816 | MockRead("HTTP/1.1 200 OK\r\n\r\n"), |
| 817 | MockRead("hello from http"), |
| 818 | MockRead(SYNCHRONOUS, ERR_TEST_PEER_CLOSE_AFTER_NEXT_MOCK_READ), |
| 819 | MockRead(ASYNC, OK) |
| 820 | }; |
| 821 | |
| 822 | StaticSocketDataProvider http_data(http_reads, arraysize(http_reads), |
| 823 | NULL, 0); |
| 824 | socket_factory_.AddSocketDataProvider(&http_data); |
| 825 | |
[email protected] | 77c6c16 | 2013-08-17 02:57:45 | [diff] [blame] | 826 | CreateSession(); |
| 827 | |
| 828 | AddQuicAlternateProtocolMapping(MockCryptoClientStream::ZERO_RTT); |
| 829 | |
[email protected] | 4d283b3 | 2013-10-17 12:57:27 | [diff] [blame] | 830 | SendRequestAndExpectHttpResponse("hello from http"); |
[email protected] | 77c6c16 | 2013-08-17 02:57:45 | [diff] [blame] | 831 | |
| 832 | ExpectBrokenAlternateProtocolMapping(); |
[email protected] | 4d283b3 | 2013-10-17 12:57:27 | [diff] [blame] | 833 | |
| 834 | EXPECT_TRUE(quic_data.at_read_eof()); |
| 835 | EXPECT_TRUE(quic_data.at_write_eof()); |
[email protected] | 77c6c16 | 2013-08-17 02:57:45 | [diff] [blame] | 836 | } |
| 837 | |
[email protected] | 4d590c9c | 2014-05-02 05:14:33 | [diff] [blame^] | 838 | TEST_P(QuicNetworkTransactionTest, NoBrokenAlternateProtocolOnConnectFailure) { |
| 839 | HttpStreamFactory::EnableNpnSpdy3(); // Enables QUIC too. |
| 840 | |
| 841 | // Alternate-protocol job will fail before creating a QUIC session. |
| 842 | StaticSocketDataProvider quic_data(NULL, 0, NULL, 0); |
| 843 | quic_data.set_connect_data(MockConnect(SYNCHRONOUS, |
| 844 | ERR_INTERNET_DISCONNECTED)); |
| 845 | socket_factory_.AddSocketDataProvider(&quic_data); |
| 846 | |
| 847 | // Main job which will succeed even though the alternate job fails. |
| 848 | MockRead http_reads[] = { |
| 849 | MockRead("HTTP/1.1 200 OK\r\n\r\n"), |
| 850 | MockRead("hello from http"), |
| 851 | MockRead(SYNCHRONOUS, ERR_TEST_PEER_CLOSE_AFTER_NEXT_MOCK_READ), |
| 852 | MockRead(ASYNC, OK) |
| 853 | }; |
| 854 | |
| 855 | StaticSocketDataProvider http_data(http_reads, arraysize(http_reads), |
| 856 | NULL, 0); |
| 857 | socket_factory_.AddSocketDataProvider(&http_data); |
| 858 | |
| 859 | CreateSession(); |
| 860 | AddQuicAlternateProtocolMapping(MockCryptoClientStream::COLD_START); |
| 861 | SendRequestAndExpectHttpResponse("hello from http"); |
| 862 | ExpectQuicAlternateProtocolMapping(); |
| 863 | } |
| 864 | |
[email protected] | 4fee967 | 2014-01-08 14:47:15 | [diff] [blame] | 865 | TEST_P(QuicNetworkTransactionTest, ConnectionCloseDuringConnect) { |
| 866 | HttpStreamFactory::EnableNpnSpdy3(); // Enables QUIC too. |
| 867 | |
[email protected] | 4fee967 | 2014-01-08 14:47:15 | [diff] [blame] | 868 | MockQuicData mock_quic_data; |
| 869 | mock_quic_data.AddRead(ConstructConnectionClosePacket(1)); |
[email protected] | 92bf17c | 2014-03-03 21:14:03 | [diff] [blame] | 870 | mock_quic_data.AddWrite( |
| 871 | ConstructRequestHeadersPacket(1, kStreamId5, true, true, |
| 872 | GetRequestHeaders("GET", "http", "/"))); |
| 873 | mock_quic_data.AddWrite(ConstructAckPacket(2, 1)); |
[email protected] | 4fee967 | 2014-01-08 14:47:15 | [diff] [blame] | 874 | mock_quic_data.AddDelayedSocketDataToFactory(&socket_factory_, 0); |
| 875 | |
| 876 | // When the QUIC connection fails, we will try the request again over HTTP. |
| 877 | MockRead http_reads[] = { |
| 878 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 879 | MockRead(kQuicAlternateProtocolHttpHeader), |
| 880 | MockRead("hello world"), |
| 881 | MockRead(SYNCHRONOUS, ERR_TEST_PEER_CLOSE_AFTER_NEXT_MOCK_READ), |
| 882 | MockRead(ASYNC, OK) |
| 883 | }; |
| 884 | |
| 885 | StaticSocketDataProvider http_data(http_reads, arraysize(http_reads), |
| 886 | NULL, 0); |
| 887 | socket_factory_.AddSocketDataProvider(&http_data); |
| 888 | |
| 889 | // In order for a new QUIC session to be established via alternate-protocol |
| 890 | // without racing an HTTP connection, we need the host resolution to happen |
| 891 | // synchronously. |
| 892 | host_resolver_.set_synchronous_mode(true); |
| 893 | host_resolver_.rules()->AddIPLiteralRule("www.google.com", "192.168.0.1", ""); |
| 894 | HostResolver::RequestInfo info(HostPortPair("www.google.com", 80)); |
| 895 | AddressList address; |
| 896 | host_resolver_.Resolve(info, |
| 897 | DEFAULT_PRIORITY, |
| 898 | &address, |
| 899 | CompletionCallback(), |
| 900 | NULL, |
| 901 | net_log_.bound()); |
| 902 | |
| 903 | CreateSession(); |
| 904 | AddQuicAlternateProtocolMapping(MockCryptoClientStream::ZERO_RTT); |
| 905 | SendRequestAndExpectHttpResponse("hello world"); |
| 906 | } |
| 907 | |
[email protected] | 61a52778 | 2013-02-21 03:58:00 | [diff] [blame] | 908 | } // namespace test |
| 909 | } // namespace net |