[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" |
| 8 | #include "net/base/mock_cert_verifier.h" |
| 9 | #include "net/base/mock_host_resolver.h" |
| 10 | #include "net/base/ssl_config_service_defaults.h" |
| 11 | #include "net/base/test_completion_callback.h" |
| 12 | #include "net/http/http_auth_handler_factory.h" |
| 13 | #include "net/http/http_network_session.h" |
| 14 | #include "net/http/http_network_transaction.h" |
| 15 | #include "net/http/http_server_properties_impl.h" |
| 16 | #include "net/http/http_stream.h" |
| 17 | #include "net/http/http_stream_factory.h" |
| 18 | #include "net/http/http_transaction_unittest.h" |
| 19 | #include "net/proxy/proxy_config_service_fixed.h" |
| 20 | #include "net/proxy/proxy_resolver.h" |
| 21 | #include "net/proxy/proxy_service.h" |
| 22 | #include "net/quic/crypto/quic_decrypter.h" |
| 23 | #include "net/quic/crypto/quic_encrypter.h" |
| 24 | #include "net/quic/quic_framer.h" |
| 25 | #include "net/quic/quic_protocol.h" |
| 26 | #include "net/quic/test_tools/mock_clock.h" |
| 27 | #include "net/quic/test_tools/mock_random.h" |
| 28 | #include "net/quic/test_tools/quic_test_utils.h" |
| 29 | #include "net/socket/client_socket_factory.h" |
| 30 | #include "net/socket/mock_client_socket_pool_manager.h" |
| 31 | #include "net/socket/socket_test_util.h" |
| 32 | #include "net/socket/ssl_client_socket.h" |
| 33 | #include "net/spdy/spdy_frame_builder.h" |
| 34 | #include "net/spdy/spdy_framer.h" |
| 35 | #include "testing/gtest/include/gtest/gtest.h" |
| 36 | #include "testing/platform_test.h" |
| 37 | |
| 38 | //----------------------------------------------------------------------------- |
| 39 | |
| 40 | namespace { |
| 41 | |
| 42 | // This is the expected return from a current server advertising QUIC. |
| 43 | static const char kQuicAlternateProtocolHttpHeader[] = |
| 44 | "Alternate-Protocol: 443:quic/1\r\n\r\n"; |
| 45 | |
| 46 | // Returns a vector of NPN protocol strings for negotiating QUIC. |
| 47 | std::vector<std::string> QuicNextProtos() { |
| 48 | std::vector<std::string> protos; |
| 49 | protos.push_back("http/1.1"); |
| 50 | protos.push_back("quic/1"); |
| 51 | return protos; |
| 52 | } |
| 53 | |
| 54 | } // namespace |
| 55 | |
| 56 | namespace net { |
| 57 | namespace test { |
| 58 | |
| 59 | class QuicNetworkTransactionTest : public PlatformTest { |
| 60 | protected: |
[email protected] | 1c04f952 | 2013-02-21 20:32:43 | [diff] [blame] | 61 | QuicNetworkTransactionTest() |
| 62 | : clock_(new MockClock), |
| 63 | ssl_config_service_(new SSLConfigServiceDefaults), |
| 64 | proxy_service_(ProxyService::CreateDirect()), |
| 65 | auth_handler_factory_( |
| 66 | HttpAuthHandlerFactory::CreateDefault(&host_resolver_)) { |
| 67 | } |
[email protected] | 61a52778 | 2013-02-21 03:58:00 | [diff] [blame] | 68 | |
| 69 | virtual void SetUp() { |
| 70 | NetworkChangeNotifier::NotifyObserversOfIPAddressChangeForTests(); |
| 71 | MessageLoop::current()->RunUntilIdle(); |
| 72 | } |
| 73 | |
| 74 | virtual void TearDown() { |
| 75 | NetworkChangeNotifier::NotifyObserversOfIPAddressChangeForTests(); |
| 76 | // Empty the current queue. |
| 77 | MessageLoop::current()->RunUntilIdle(); |
| 78 | PlatformTest::TearDown(); |
| 79 | NetworkChangeNotifier::NotifyObserversOfIPAddressChangeForTests(); |
| 80 | MessageLoop::current()->RunUntilIdle(); |
| 81 | HttpStreamFactory::set_use_alternate_protocols(false); |
| 82 | HttpStreamFactory::SetNextProtos(std::vector<std::string>()); |
| 83 | } |
| 84 | |
| 85 | // TODO(rch): factor these Construct* methods out into a test helper class. |
| 86 | scoped_ptr<QuicEncryptedPacket> ConstructChlo() { |
| 87 | const std::string host = "www.google.com"; |
| 88 | scoped_ptr<QuicPacket> chlo(ConstructClientHelloPacket(0xDEADBEEF, |
| 89 | clock_, |
| 90 | &random_generator_, |
| 91 | host)); |
[email protected] | 5351cc4b | 2013-03-03 07:22:41 | [diff] [blame^] | 92 | QuicFramer framer(kQuicVersion1, |
| 93 | QuicDecrypter::Create(kNULL), |
[email protected] | 61a52778 | 2013-02-21 03:58:00 | [diff] [blame] | 94 | QuicEncrypter::Create(kNULL)); |
[email protected] | 9db44391 | 2013-02-25 05:27:03 | [diff] [blame] | 95 | return scoped_ptr<QuicEncryptedPacket>(framer.EncryptPacket(1, *chlo)); |
[email protected] | 61a52778 | 2013-02-21 03:58:00 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | scoped_ptr<QuicEncryptedPacket> ConstructShlo() { |
| 99 | scoped_ptr<QuicPacket> shlo(ConstructHandshakePacket(0xDEADBEEF, kSHLO)); |
[email protected] | 5351cc4b | 2013-03-03 07:22:41 | [diff] [blame^] | 100 | QuicFramer framer(kQuicVersion1, |
| 101 | QuicDecrypter::Create(kNULL), |
[email protected] | 61a52778 | 2013-02-21 03:58:00 | [diff] [blame] | 102 | QuicEncrypter::Create(kNULL)); |
[email protected] | 9db44391 | 2013-02-25 05:27:03 | [diff] [blame] | 103 | return scoped_ptr<QuicEncryptedPacket>(framer.EncryptPacket(1, *shlo)); |
[email protected] | 61a52778 | 2013-02-21 03:58:00 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | scoped_ptr<QuicEncryptedPacket> ConstructRstPacket( |
| 107 | QuicPacketSequenceNumber num, |
| 108 | QuicStreamId stream_id) { |
| 109 | QuicPacketHeader header; |
| 110 | header.public_header.guid = 0xDEADBEEF; |
[email protected] | 9db44391 | 2013-02-25 05:27:03 | [diff] [blame] | 111 | header.public_header.reset_flag = false; |
| 112 | header.public_header.version_flag = false; |
[email protected] | 61a52778 | 2013-02-21 03:58:00 | [diff] [blame] | 113 | header.packet_sequence_number = num; |
[email protected] | 9db44391 | 2013-02-25 05:27:03 | [diff] [blame] | 114 | header.entropy_flag = false; |
| 115 | header.fec_flag = false; |
| 116 | header.fec_entropy_flag = false; |
[email protected] | 61a52778 | 2013-02-21 03:58:00 | [diff] [blame] | 117 | header.fec_group = 0; |
| 118 | |
[email protected] | 9db44391 | 2013-02-25 05:27:03 | [diff] [blame] | 119 | QuicRstStreamFrame rst(stream_id, QUIC_NO_ERROR); |
[email protected] | 61a52778 | 2013-02-21 03:58:00 | [diff] [blame] | 120 | return scoped_ptr<QuicEncryptedPacket>( |
| 121 | ConstructPacket(header, QuicFrame(&rst))); |
| 122 | } |
| 123 | |
| 124 | scoped_ptr<QuicEncryptedPacket> ConstructAckPacket( |
| 125 | QuicPacketSequenceNumber largest_received, |
| 126 | QuicPacketSequenceNumber least_unacked) { |
| 127 | QuicPacketHeader header; |
| 128 | header.public_header.guid = 0xDEADBEEF; |
[email protected] | 9db44391 | 2013-02-25 05:27:03 | [diff] [blame] | 129 | header.public_header.reset_flag = false; |
| 130 | header.public_header.version_flag = false; |
[email protected] | 61a52778 | 2013-02-21 03:58:00 | [diff] [blame] | 131 | header.packet_sequence_number = 3; |
[email protected] | 9db44391 | 2013-02-25 05:27:03 | [diff] [blame] | 132 | header.entropy_flag = false; |
| 133 | header.fec_flag = false; |
| 134 | header.fec_entropy_flag = false; |
[email protected] | 61a52778 | 2013-02-21 03:58:00 | [diff] [blame] | 135 | header.fec_group = 0; |
| 136 | |
| 137 | QuicAckFrame ack(largest_received, least_unacked); |
| 138 | |
| 139 | QuicCongestionFeedbackFrame feedback; |
| 140 | feedback.type = kTCP; |
| 141 | feedback.tcp.accumulated_number_of_lost_packets = 0; |
| 142 | feedback.tcp.receive_window = 256000; |
| 143 | |
[email protected] | 5351cc4b | 2013-03-03 07:22:41 | [diff] [blame^] | 144 | QuicFramer framer(kQuicVersion1, |
| 145 | QuicDecrypter::Create(kNULL), |
[email protected] | 61a52778 | 2013-02-21 03:58:00 | [diff] [blame] | 146 | QuicEncrypter::Create(kNULL)); |
| 147 | QuicFrames frames; |
| 148 | frames.push_back(QuicFrame(&ack)); |
| 149 | frames.push_back(QuicFrame(&feedback)); |
| 150 | scoped_ptr<QuicPacket> packet( |
[email protected] | 9db44391 | 2013-02-25 05:27:03 | [diff] [blame] | 151 | framer.ConstructFrameDataPacket(header, frames).packet); |
| 152 | return scoped_ptr<QuicEncryptedPacket>( |
| 153 | framer.EncryptPacket(header.packet_sequence_number, *packet)); |
[email protected] | 61a52778 | 2013-02-21 03:58:00 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | std::string GetRequestString(const std::string& method, |
| 157 | const std::string& path) { |
| 158 | SpdyHeaderBlock headers; |
| 159 | headers[":method"] = method; |
| 160 | headers[":host"] = "www.google.com"; |
| 161 | headers[":path"] = path; |
| 162 | headers[":scheme"] = "http"; |
| 163 | headers[":version"] = "HTTP/1.1"; |
| 164 | return SerializeHeaderBlock(headers); |
| 165 | } |
| 166 | |
| 167 | std::string GetResponseString(const std::string& status, |
| 168 | const std::string& body) { |
| 169 | SpdyHeaderBlock headers; |
| 170 | headers[":status"] = status; |
| 171 | headers[":version"] = "HTTP/1.1"; |
| 172 | headers["content-type"] = "text/plain"; |
| 173 | return SerializeHeaderBlock(headers) + body; |
| 174 | } |
| 175 | |
| 176 | std::string SerializeHeaderBlock(const SpdyHeaderBlock& headers) { |
| 177 | size_t len = SpdyFramer::GetSerializedLength(3, &headers); |
| 178 | SpdyFrameBuilder builder(len); |
| 179 | SpdyFramer::WriteHeaderBlock(&builder, 3, &headers); |
| 180 | scoped_ptr<SpdyFrame> frame(builder.take()); |
| 181 | return std::string(frame->data(), len); |
| 182 | } |
| 183 | |
| 184 | // Returns a newly created packet to send kData on stream 1. |
| 185 | QuicEncryptedPacket* ConstructDataPacket( |
| 186 | QuicPacketSequenceNumber sequence_number, |
| 187 | bool fin, |
| 188 | QuicStreamOffset offset, |
| 189 | base::StringPiece data) { |
| 190 | InitializeHeader(sequence_number); |
| 191 | QuicStreamFrame frame(3, fin, offset, data); |
| 192 | return ConstructPacket(header_, QuicFrame(&frame)).release(); |
| 193 | } |
| 194 | |
| 195 | scoped_ptr<QuicEncryptedPacket> ConstructPacket( |
| 196 | const QuicPacketHeader& header, |
| 197 | const QuicFrame& frame) { |
[email protected] | 5351cc4b | 2013-03-03 07:22:41 | [diff] [blame^] | 198 | QuicFramer framer(kQuicVersion1, |
| 199 | QuicDecrypter::Create(kNULL), |
[email protected] | 61a52778 | 2013-02-21 03:58:00 | [diff] [blame] | 200 | QuicEncrypter::Create(kNULL)); |
| 201 | QuicFrames frames; |
| 202 | frames.push_back(frame); |
| 203 | scoped_ptr<QuicPacket> packet( |
[email protected] | 9db44391 | 2013-02-25 05:27:03 | [diff] [blame] | 204 | framer.ConstructFrameDataPacket(header, frames).packet); |
| 205 | return scoped_ptr<QuicEncryptedPacket>( |
| 206 | framer.EncryptPacket(header.packet_sequence_number, *packet)); |
[email protected] | 61a52778 | 2013-02-21 03:58:00 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | void InitializeHeader(QuicPacketSequenceNumber sequence_number) { |
| 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; |
| 212 | header_.public_header.version_flag = false; |
[email protected] | 61a52778 | 2013-02-21 03:58:00 | [diff] [blame] | 213 | header_.packet_sequence_number = sequence_number; |
| 214 | header_.fec_group = 0; |
[email protected] | 9db44391 | 2013-02-25 05:27:03 | [diff] [blame] | 215 | header_.entropy_flag = false; |
| 216 | header_.fec_flag = false; |
| 217 | header_.fec_entropy_flag = false; |
[email protected] | 61a52778 | 2013-02-21 03:58:00 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | void CreateSession() { |
| 221 | params_.client_socket_factory = &socket_factory_; |
[email protected] | 1c04f952 | 2013-02-21 20:32:43 | [diff] [blame] | 222 | params_.host_resolver = &host_resolver_; |
| 223 | params_.cert_verifier = &cert_verifier_; |
| 224 | params_.proxy_service = proxy_service_.get(); |
| 225 | params_.ssl_config_service = ssl_config_service_.get(); |
| 226 | params_.http_auth_handler_factory = auth_handler_factory_.get(); |
[email protected] | 61a52778 | 2013-02-21 03:58:00 | [diff] [blame] | 227 | params_.http_server_properties = &http_server_properties; |
| 228 | |
| 229 | session_ = new HttpNetworkSession(params_); |
| 230 | } |
| 231 | |
| 232 | QuicPacketHeader header_; |
| 233 | scoped_refptr<HttpNetworkSession> session_; |
| 234 | MockClientSocketFactory socket_factory_; |
[email protected] | 1c04f952 | 2013-02-21 20:32:43 | [diff] [blame] | 235 | MockClock* clock_; // Owned by QuicStreamFactory after CreateSession. |
| 236 | MockHostResolver host_resolver_; |
| 237 | MockCertVerifier cert_verifier_; |
| 238 | scoped_refptr<SSLConfigServiceDefaults> ssl_config_service_; |
| 239 | scoped_ptr<ProxyService> proxy_service_; |
| 240 | scoped_ptr<HttpAuthHandlerFactory> auth_handler_factory_; |
[email protected] | 61a52778 | 2013-02-21 03:58:00 | [diff] [blame] | 241 | MockRandom random_generator_; |
| 242 | HttpServerPropertiesImpl http_server_properties; |
| 243 | HttpNetworkSession::Params params_; |
| 244 | }; |
| 245 | |
| 246 | TEST_F(QuicNetworkTransactionTest, UseAlternateProtocolForQuic) { |
| 247 | HttpStreamFactory::set_use_alternate_protocols(true); |
| 248 | HttpStreamFactory::SetNextProtos(QuicNextProtos()); |
| 249 | params_.enable_quic = true; |
| 250 | params_.quic_clock = clock_; |
[email protected] | 61a52778 | 2013-02-21 03:58:00 | [diff] [blame] | 251 | params_.quic_random = &random_generator_; |
| 252 | |
| 253 | HttpRequestInfo request; |
| 254 | request.method = "GET"; |
| 255 | request.url = GURL("http://www.google.com/"); |
| 256 | request.load_flags = 0; |
| 257 | |
| 258 | MockRead data_reads[] = { |
| 259 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 260 | MockRead(kQuicAlternateProtocolHttpHeader), |
| 261 | MockRead("hello world"), |
| 262 | MockRead(SYNCHRONOUS, ERR_TEST_PEER_CLOSE_AFTER_NEXT_MOCK_READ), |
| 263 | MockRead(ASYNC, OK) |
| 264 | }; |
| 265 | |
| 266 | StaticSocketDataProvider first_transaction( |
| 267 | data_reads, arraysize(data_reads), NULL, 0); |
| 268 | socket_factory_.AddSocketDataProvider(&first_transaction); |
| 269 | |
| 270 | |
| 271 | scoped_ptr<QuicEncryptedPacket> chlo(ConstructChlo()); |
| 272 | scoped_ptr<QuicEncryptedPacket> data( |
| 273 | ConstructDataPacket(2, true, 0, GetRequestString("GET", "/"))); |
| 274 | scoped_ptr<QuicEncryptedPacket> ack(ConstructAckPacket(2, 1)); |
| 275 | |
| 276 | MockWrite quic_writes[] = { |
| 277 | MockWrite(SYNCHRONOUS, chlo->data(), chlo->length()), |
| 278 | MockWrite(SYNCHRONOUS, data->data(), data->length()), |
| 279 | MockWrite(SYNCHRONOUS, ack->data(), ack->length()), |
| 280 | }; |
| 281 | |
| 282 | scoped_ptr<QuicEncryptedPacket> shlo(ConstructShlo()); |
| 283 | scoped_ptr<QuicEncryptedPacket> resp( |
| 284 | ConstructDataPacket(2, true, 0, GetResponseString("200 OK", "hello!"))); |
| 285 | MockRead quic_reads[] = { |
| 286 | MockRead(SYNCHRONOUS, shlo->data(), shlo->length()), |
| 287 | MockRead(SYNCHRONOUS, resp->data(), resp->length()), |
| 288 | MockRead(ASYNC, OK), // EOF |
| 289 | }; |
| 290 | |
| 291 | DelayedSocketData quic_data( |
| 292 | 1, // wait for one write to finish before reading. |
| 293 | quic_reads, arraysize(quic_reads), |
| 294 | quic_writes, arraysize(quic_writes)); |
| 295 | |
| 296 | socket_factory_.AddSocketDataProvider(&quic_data); |
| 297 | |
| 298 | // The non-alternate protocol job needs to hang in order to guarantee that the |
| 299 | // alternate-protocol job will "win". |
| 300 | MockConnect never_finishing_connect(SYNCHRONOUS, ERR_IO_PENDING); |
| 301 | StaticSocketDataProvider hanging_non_alternate_protocol_socket( |
| 302 | NULL, 0, NULL, 0); |
| 303 | hanging_non_alternate_protocol_socket.set_connect_data( |
| 304 | never_finishing_connect); |
| 305 | socket_factory_.AddSocketDataProvider( |
| 306 | &hanging_non_alternate_protocol_socket); |
| 307 | |
| 308 | TestCompletionCallback callback; |
| 309 | |
| 310 | CreateSession(); |
| 311 | scoped_ptr<HttpNetworkTransaction> trans( |
| 312 | new HttpNetworkTransaction(session_)); |
| 313 | |
| 314 | int rv = trans->Start(&request, callback.callback(), BoundNetLog()); |
| 315 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 316 | EXPECT_EQ(OK, callback.WaitForResult()); |
| 317 | |
| 318 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 319 | ASSERT_TRUE(response != NULL); |
| 320 | ASSERT_TRUE(response->headers != NULL); |
| 321 | EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
| 322 | |
| 323 | std::string response_data; |
| 324 | ASSERT_EQ(OK, ReadTransaction(trans.get(), &response_data)); |
| 325 | EXPECT_EQ("hello world", response_data); |
| 326 | |
| 327 | trans.reset(new HttpNetworkTransaction(session_)); |
| 328 | |
| 329 | rv = trans->Start(&request, callback.callback(), BoundNetLog()); |
| 330 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 331 | EXPECT_EQ(OK, callback.WaitForResult()); |
| 332 | |
| 333 | response = trans->GetResponseInfo(); |
| 334 | ASSERT_TRUE(response != NULL); |
| 335 | ASSERT_TRUE(response->headers != NULL); |
| 336 | EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
| 337 | EXPECT_TRUE(response->was_fetched_via_spdy); |
| 338 | |
| 339 | ASSERT_EQ(OK, ReadTransaction(trans.get(), &response_data)); |
| 340 | EXPECT_EQ("hello!", response_data); |
| 341 | } |
| 342 | |
| 343 | } // namespace test |
| 344 | } // namespace net |