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