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