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