[email protected] | e13201d8 | 2012-12-12 05:00:32 | [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 "net/quic/quic_stream_factory.h" |
| 6 | |
[email protected] | e13201d8 | 2012-12-12 05:00:32 | [diff] [blame] | 7 | #include "base/run_loop.h" |
| 8 | #include "base/string_util.h" |
[email protected] | f2cb3cf | 2013-03-21 01:40:53 | [diff] [blame] | 9 | #include "net/dns/mock_host_resolver.h" |
[email protected] | e13201d8 | 2012-12-12 05:00:32 | [diff] [blame] | 10 | #include "net/http/http_response_headers.h" |
| 11 | #include "net/http/http_response_info.h" |
| 12 | #include "net/http/http_util.h" |
[email protected] | 4df6984 | 2013-02-27 06:32:16 | [diff] [blame] | 13 | #include "net/quic/crypto/quic_decrypter.h" |
| 14 | #include "net/quic/crypto/quic_encrypter.h" |
[email protected] | e13201d8 | 2012-12-12 05:00:32 | [diff] [blame] | 15 | #include "net/quic/quic_http_stream.h" |
| 16 | #include "net/quic/test_tools/mock_clock.h" |
[email protected] | e8ff2684 | 2013-03-22 21:02:05 | [diff] [blame] | 17 | #include "net/quic/test_tools/mock_crypto_client_stream_factory.h" |
[email protected] | 9558c5d3 | 2012-12-22 00:08:14 | [diff] [blame] | 18 | #include "net/quic/test_tools/mock_random.h" |
[email protected] | e13201d8 | 2012-12-12 05:00:32 | [diff] [blame] | 19 | #include "net/quic/test_tools/quic_test_utils.h" |
| 20 | #include "net/socket/socket_test_util.h" |
| 21 | #include "testing/gtest/include/gtest/gtest.h" |
| 22 | |
| 23 | namespace net { |
[email protected] | e13201d8 | 2012-12-12 05:00:32 | [diff] [blame] | 24 | namespace test { |
| 25 | |
| 26 | class QuicStreamFactoryTest : public ::testing::Test { |
| 27 | protected: |
| 28 | QuicStreamFactoryTest() |
[email protected] | 872edd9e | 2013-01-16 08:51:15 | [diff] [blame] | 29 | : clock_(new MockClock()), |
| 30 | factory_(&host_resolver_, &socket_factory_, |
[email protected] | e8ff2684 | 2013-03-22 21:02:05 | [diff] [blame] | 31 | &crypto_client_stream_factory_, |
[email protected] | 9d9e793 | 2013-02-25 18:31:05 | [diff] [blame] | 32 | &random_generator_, clock_), |
[email protected] | e13201d8 | 2012-12-12 05:00:32 | [diff] [blame] | 33 | host_port_proxy_pair_(HostPortPair("www.google.com", 443), |
| 34 | ProxyServer::Direct()) { |
| 35 | } |
| 36 | |
[email protected] | e13201d8 | 2012-12-12 05:00:32 | [diff] [blame] | 37 | scoped_ptr<QuicEncryptedPacket> ConstructRstPacket( |
| 38 | QuicPacketSequenceNumber num, |
| 39 | QuicStreamId stream_id) { |
| 40 | QuicPacketHeader header; |
[email protected] | c995c57 | 2013-01-18 05:43:20 | [diff] [blame] | 41 | header.public_header.guid = 0xDEADBEEF; |
[email protected] | 9db44391 | 2013-02-25 05:27:03 | [diff] [blame] | 42 | header.public_header.reset_flag = false; |
[email protected] | e8ff2684 | 2013-03-22 21:02:05 | [diff] [blame] | 43 | header.public_header.version_flag = true; |
[email protected] | e13201d8 | 2012-12-12 05:00:32 | [diff] [blame] | 44 | header.packet_sequence_number = num; |
[email protected] | 9db44391 | 2013-02-25 05:27:03 | [diff] [blame] | 45 | header.entropy_flag = false; |
| 46 | header.fec_entropy_flag = false; |
| 47 | header.fec_flag = false; |
[email protected] | e13201d8 | 2012-12-12 05:00:32 | [diff] [blame] | 48 | header.fec_group = 0; |
| 49 | |
[email protected] | 74bda14 | 2013-03-31 02:49:11 | [diff] [blame] | 50 | QuicRstStreamFrame rst(stream_id, QUIC_STREAM_NO_ERROR); |
[email protected] | e13201d8 | 2012-12-12 05:00:32 | [diff] [blame] | 51 | return scoped_ptr<QuicEncryptedPacket>( |
| 52 | ConstructPacket(header, QuicFrame(&rst))); |
| 53 | } |
| 54 | |
| 55 | scoped_ptr<QuicEncryptedPacket> ConstructAckPacket( |
| 56 | QuicPacketSequenceNumber largest_received, |
| 57 | QuicPacketSequenceNumber least_unacked) { |
| 58 | QuicPacketHeader header; |
[email protected] | c995c57 | 2013-01-18 05:43:20 | [diff] [blame] | 59 | header.public_header.guid = 0xDEADBEEF; |
[email protected] | 9db44391 | 2013-02-25 05:27:03 | [diff] [blame] | 60 | header.public_header.reset_flag = false; |
| 61 | header.public_header.version_flag = false; |
[email protected] | e13201d8 | 2012-12-12 05:00:32 | [diff] [blame] | 62 | header.packet_sequence_number = 2; |
[email protected] | 9db44391 | 2013-02-25 05:27:03 | [diff] [blame] | 63 | header.entropy_flag = false; |
| 64 | header.fec_entropy_flag = false; |
| 65 | header.fec_flag = false; |
[email protected] | e13201d8 | 2012-12-12 05:00:32 | [diff] [blame] | 66 | header.fec_group = 0; |
| 67 | |
[email protected] | 14e8106c | 2013-03-14 16:25:33 | [diff] [blame] | 68 | QuicAckFrame ack(largest_received, QuicTime::Zero(), least_unacked); |
[email protected] | dda3e9b | 2012-12-22 21:49:25 | [diff] [blame] | 69 | QuicCongestionFeedbackFrame feedback; |
| 70 | feedback.type = kTCP; |
| 71 | feedback.tcp.accumulated_number_of_lost_packets = 0; |
| 72 | feedback.tcp.receive_window = 16000; |
| 73 | |
[email protected] | 8ba8121 | 2013-05-03 13:11:48 | [diff] [blame] | 74 | QuicFramer framer(kQuicVersion1, QuicTime::Zero(), false); |
[email protected] | dda3e9b | 2012-12-22 21:49:25 | [diff] [blame] | 75 | QuicFrames frames; |
| 76 | frames.push_back(QuicFrame(&ack)); |
| 77 | frames.push_back(QuicFrame(&feedback)); |
| 78 | scoped_ptr<QuicPacket> packet( |
[email protected] | 9db44391 | 2013-02-25 05:27:03 | [diff] [blame] | 79 | framer.ConstructFrameDataPacket(header, frames).packet); |
[email protected] | 8ba8121 | 2013-05-03 13:11:48 | [diff] [blame] | 80 | return scoped_ptr<QuicEncryptedPacket>(framer.EncryptPacket( |
| 81 | ENCRYPTION_NONE, header.packet_sequence_number, *packet)); |
[email protected] | e13201d8 | 2012-12-12 05:00:32 | [diff] [blame] | 82 | } |
| 83 | |
[email protected] | 26f3f8e | 2012-12-13 21:07:19 | [diff] [blame] | 84 | // Returns a newly created packet to send congestion feedback data. |
| 85 | scoped_ptr<QuicEncryptedPacket> ConstructFeedbackPacket( |
| 86 | QuicPacketSequenceNumber sequence_number) { |
| 87 | QuicPacketHeader header; |
[email protected] | c995c57 | 2013-01-18 05:43:20 | [diff] [blame] | 88 | header.public_header.guid = 0xDEADBEEF; |
[email protected] | 9db44391 | 2013-02-25 05:27:03 | [diff] [blame] | 89 | header.public_header.reset_flag = false; |
| 90 | header.public_header.version_flag = false; |
[email protected] | 26f3f8e | 2012-12-13 21:07:19 | [diff] [blame] | 91 | header.packet_sequence_number = sequence_number; |
[email protected] | 9db44391 | 2013-02-25 05:27:03 | [diff] [blame] | 92 | header.entropy_flag = false; |
| 93 | header.fec_entropy_flag = false; |
| 94 | header.fec_flag = false; |
[email protected] | 26f3f8e | 2012-12-13 21:07:19 | [diff] [blame] | 95 | header.fec_group = 0; |
| 96 | |
| 97 | QuicCongestionFeedbackFrame frame; |
| 98 | frame.type = kTCP; |
| 99 | frame.tcp.accumulated_number_of_lost_packets = 0; |
| 100 | frame.tcp.receive_window = 16000; |
| 101 | |
| 102 | return scoped_ptr<QuicEncryptedPacket>( |
| 103 | ConstructPacket(header, QuicFrame(&frame))); |
| 104 | } |
| 105 | |
[email protected] | e13201d8 | 2012-12-12 05:00:32 | [diff] [blame] | 106 | scoped_ptr<QuicEncryptedPacket> ConstructPacket( |
| 107 | const QuicPacketHeader& header, |
| 108 | const QuicFrame& frame) { |
[email protected] | 8ba8121 | 2013-05-03 13:11:48 | [diff] [blame] | 109 | QuicFramer framer(kQuicVersion1, QuicTime::Zero(), false); |
[email protected] | e13201d8 | 2012-12-12 05:00:32 | [diff] [blame] | 110 | QuicFrames frames; |
| 111 | frames.push_back(frame); |
[email protected] | 610a7e94 | 2012-12-18 00:21:39 | [diff] [blame] | 112 | scoped_ptr<QuicPacket> packet( |
[email protected] | 9db44391 | 2013-02-25 05:27:03 | [diff] [blame] | 113 | framer.ConstructFrameDataPacket(header, frames).packet); |
[email protected] | 8ba8121 | 2013-05-03 13:11:48 | [diff] [blame] | 114 | return scoped_ptr<QuicEncryptedPacket>(framer.EncryptPacket( |
| 115 | ENCRYPTION_NONE, header.packet_sequence_number, *packet)); |
[email protected] | e13201d8 | 2012-12-12 05:00:32 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | MockHostResolver host_resolver_; |
| 119 | MockClientSocketFactory socket_factory_; |
[email protected] | e8ff2684 | 2013-03-22 21:02:05 | [diff] [blame] | 120 | MockCryptoClientStreamFactory crypto_client_stream_factory_; |
[email protected] | 9558c5d3 | 2012-12-22 00:08:14 | [diff] [blame] | 121 | MockRandom random_generator_; |
[email protected] | 872edd9e | 2013-01-16 08:51:15 | [diff] [blame] | 122 | MockClock* clock_; // Owned by factory_. |
[email protected] | e13201d8 | 2012-12-12 05:00:32 | [diff] [blame] | 123 | QuicStreamFactory factory_; |
| 124 | HostPortProxyPair host_port_proxy_pair_; |
| 125 | BoundNetLog net_log_; |
| 126 | TestCompletionCallback callback_; |
[email protected] | e13201d8 | 2012-12-12 05:00:32 | [diff] [blame] | 127 | }; |
| 128 | |
| 129 | TEST_F(QuicStreamFactoryTest, CreateIfSessionExists) { |
| 130 | EXPECT_EQ(NULL, factory_.CreateIfSessionExists(host_port_proxy_pair_, |
| 131 | net_log_).get()); |
| 132 | } |
| 133 | |
| 134 | TEST_F(QuicStreamFactoryTest, Create) { |
[email protected] | e8ff2684 | 2013-03-22 21:02:05 | [diff] [blame] | 135 | scoped_ptr<QuicEncryptedPacket> rst3(ConstructRstPacket(1, 3)); |
| 136 | scoped_ptr<QuicEncryptedPacket> rst5(ConstructRstPacket(2, 5)); |
| 137 | scoped_ptr<QuicEncryptedPacket> rst7(ConstructRstPacket(3, 7)); |
[email protected] | e13201d8 | 2012-12-12 05:00:32 | [diff] [blame] | 138 | MockWrite writes[] = { |
[email protected] | e13201d8 | 2012-12-12 05:00:32 | [diff] [blame] | 139 | MockWrite(SYNCHRONOUS, rst3->data(), rst3->length()), |
| 140 | MockWrite(SYNCHRONOUS, rst5->data(), rst5->length()), |
| 141 | MockWrite(SYNCHRONOUS, rst7->data(), rst7->length()), |
| 142 | }; |
[email protected] | e13201d8 | 2012-12-12 05:00:32 | [diff] [blame] | 143 | MockRead reads[] = { |
[email protected] | d03a66d | 2013-05-06 12:55:59 | [diff] [blame] | 144 | MockRead(SYNCHRONOUS, ERR_IO_PENDING) // Stall forever. |
[email protected] | e13201d8 | 2012-12-12 05:00:32 | [diff] [blame] | 145 | }; |
| 146 | StaticSocketDataProvider socket_data(reads, arraysize(reads), |
| 147 | writes, arraysize(writes)); |
| 148 | socket_factory_.AddSocketDataProvider(&socket_data); |
| 149 | |
| 150 | QuicStreamRequest request(&factory_); |
| 151 | EXPECT_EQ(ERR_IO_PENDING, request.Request(host_port_proxy_pair_, net_log_, |
| 152 | callback_.callback())); |
| 153 | |
| 154 | EXPECT_EQ(OK, callback_.WaitForResult()); |
| 155 | scoped_ptr<QuicHttpStream> stream = request.ReleaseStream(); |
| 156 | |
| 157 | // Will reset stream 3. |
| 158 | stream = factory_.CreateIfSessionExists(host_port_proxy_pair_, net_log_); |
| 159 | EXPECT_TRUE(stream.get()); |
| 160 | |
| 161 | QuicStreamRequest request2(&factory_); |
| 162 | EXPECT_EQ(OK, request2.Request(host_port_proxy_pair_, net_log_, |
| 163 | callback_.callback())); |
| 164 | stream = request2.ReleaseStream(); // Will reset stream 5. |
| 165 | stream.reset(); // Will reset stream 7. |
| 166 | |
| 167 | EXPECT_TRUE(socket_data.at_read_eof()); |
| 168 | EXPECT_TRUE(socket_data.at_write_eof()); |
| 169 | } |
| 170 | |
| 171 | TEST_F(QuicStreamFactoryTest, CreateError) { |
| 172 | StaticSocketDataProvider socket_data(NULL, 0, NULL, 0); |
| 173 | socket_factory_.AddSocketDataProvider(&socket_data); |
| 174 | |
| 175 | host_resolver_.rules()->AddSimulatedFailure("www.google.com"); |
| 176 | |
| 177 | QuicStreamRequest request(&factory_); |
| 178 | EXPECT_EQ(ERR_IO_PENDING, request.Request(host_port_proxy_pair_, net_log_, |
| 179 | callback_.callback())); |
| 180 | |
| 181 | EXPECT_EQ(ERR_NAME_NOT_RESOLVED, callback_.WaitForResult()); |
| 182 | |
| 183 | EXPECT_TRUE(socket_data.at_read_eof()); |
| 184 | EXPECT_TRUE(socket_data.at_write_eof()); |
| 185 | } |
| 186 | |
| 187 | TEST_F(QuicStreamFactoryTest, CancelCreate) { |
[email protected] | e8ff2684 | 2013-03-22 21:02:05 | [diff] [blame] | 188 | scoped_ptr<QuicEncryptedPacket> rst3(ConstructRstPacket(1, 3)); |
[email protected] | e13201d8 | 2012-12-12 05:00:32 | [diff] [blame] | 189 | |
| 190 | MockWrite writes[] = { |
[email protected] | e13201d8 | 2012-12-12 05:00:32 | [diff] [blame] | 191 | MockWrite(SYNCHRONOUS, rst3->data(), rst3->length()), |
| 192 | }; |
[email protected] | e13201d8 | 2012-12-12 05:00:32 | [diff] [blame] | 193 | MockRead reads[] = { |
[email protected] | d03a66d | 2013-05-06 12:55:59 | [diff] [blame] | 194 | MockRead(SYNCHRONOUS, ERR_IO_PENDING) // Stall forever. |
[email protected] | e13201d8 | 2012-12-12 05:00:32 | [diff] [blame] | 195 | }; |
| 196 | StaticSocketDataProvider socket_data(reads, arraysize(reads), |
| 197 | writes, arraysize(writes)); |
| 198 | socket_factory_.AddSocketDataProvider(&socket_data); |
| 199 | { |
| 200 | QuicStreamRequest request(&factory_); |
| 201 | EXPECT_EQ(ERR_IO_PENDING, request.Request(host_port_proxy_pair_, net_log_, |
| 202 | callback_.callback())); |
| 203 | } |
| 204 | |
| 205 | base::RunLoop run_loop; |
| 206 | run_loop.RunUntilIdle(); |
| 207 | |
| 208 | scoped_ptr<QuicHttpStream> stream( |
| 209 | factory_.CreateIfSessionExists(host_port_proxy_pair_, net_log_)); |
| 210 | EXPECT_TRUE(stream.get()); |
| 211 | stream.reset(); |
| 212 | |
| 213 | EXPECT_TRUE(socket_data.at_read_eof()); |
| 214 | EXPECT_TRUE(socket_data.at_write_eof()); |
| 215 | } |
| 216 | |
[email protected] | 56dfb90 | 2013-01-03 23:17:55 | [diff] [blame] | 217 | TEST_F(QuicStreamFactoryTest, CloseAllSessions) { |
[email protected] | e8ff2684 | 2013-03-22 21:02:05 | [diff] [blame] | 218 | scoped_ptr<QuicEncryptedPacket> rst3(ConstructRstPacket(1, 3)); |
[email protected] | 56dfb90 | 2013-01-03 23:17:55 | [diff] [blame] | 219 | MockRead reads[] = { |
[email protected] | d03a66d | 2013-05-06 12:55:59 | [diff] [blame] | 220 | MockRead(SYNCHRONOUS, ERR_IO_PENDING) // Stall forever. |
[email protected] | 56dfb90 | 2013-01-03 23:17:55 | [diff] [blame] | 221 | }; |
| 222 | StaticSocketDataProvider socket_data(reads, arraysize(reads), |
[email protected] | e8ff2684 | 2013-03-22 21:02:05 | [diff] [blame] | 223 | NULL, 0); |
[email protected] | 56dfb90 | 2013-01-03 23:17:55 | [diff] [blame] | 224 | socket_factory_.AddSocketDataProvider(&socket_data); |
| 225 | |
| 226 | MockWrite writes2[] = { |
[email protected] | 56dfb90 | 2013-01-03 23:17:55 | [diff] [blame] | 227 | MockWrite(SYNCHRONOUS, rst3->data(), rst3->length()), |
| 228 | }; |
| 229 | MockRead reads2[] = { |
[email protected] | d03a66d | 2013-05-06 12:55:59 | [diff] [blame] | 230 | MockRead(SYNCHRONOUS, ERR_IO_PENDING) // Stall forever. |
[email protected] | 56dfb90 | 2013-01-03 23:17:55 | [diff] [blame] | 231 | }; |
| 232 | StaticSocketDataProvider socket_data2(reads2, arraysize(reads2), |
| 233 | writes2, arraysize(writes2)); |
| 234 | socket_factory_.AddSocketDataProvider(&socket_data2); |
| 235 | |
| 236 | QuicStreamRequest request(&factory_); |
| 237 | EXPECT_EQ(ERR_IO_PENDING, request.Request(host_port_proxy_pair_, net_log_, |
| 238 | callback_.callback())); |
| 239 | |
| 240 | EXPECT_EQ(OK, callback_.WaitForResult()); |
| 241 | scoped_ptr<QuicHttpStream> stream = request.ReleaseStream(); |
| 242 | |
| 243 | // Close the session and verify that stream saw the error. |
| 244 | factory_.CloseAllSessions(ERR_INTERNET_DISCONNECTED); |
| 245 | EXPECT_EQ(ERR_INTERNET_DISCONNECTED, |
| 246 | stream->ReadResponseHeaders(callback_.callback())); |
| 247 | |
| 248 | // Now attempting to request a stream to the same origin should create |
| 249 | // a new session. |
| 250 | |
| 251 | QuicStreamRequest request2(&factory_); |
| 252 | EXPECT_EQ(ERR_IO_PENDING, request2.Request(host_port_proxy_pair_, net_log_, |
| 253 | callback_.callback())); |
| 254 | |
| 255 | EXPECT_EQ(OK, callback_.WaitForResult()); |
| 256 | stream = request2.ReleaseStream(); |
| 257 | stream.reset(); // Will reset stream 3. |
| 258 | |
| 259 | EXPECT_TRUE(socket_data.at_read_eof()); |
| 260 | EXPECT_TRUE(socket_data.at_write_eof()); |
| 261 | EXPECT_TRUE(socket_data2.at_read_eof()); |
| 262 | EXPECT_TRUE(socket_data2.at_write_eof()); |
| 263 | } |
| 264 | |
[email protected] | f698a01 | 2013-05-06 20:18:59 | [diff] [blame^] | 265 | TEST_F(QuicStreamFactoryTest, OnIPAddressChanged) { |
| 266 | scoped_ptr<QuicEncryptedPacket> rst3(ConstructRstPacket(1, 3)); |
| 267 | MockRead reads[] = { |
| 268 | MockRead(SYNCHRONOUS, ERR_IO_PENDING) // Stall forever. |
| 269 | }; |
| 270 | StaticSocketDataProvider socket_data(reads, arraysize(reads), |
| 271 | NULL, 0); |
| 272 | socket_factory_.AddSocketDataProvider(&socket_data); |
| 273 | |
| 274 | MockWrite writes2[] = { |
| 275 | MockWrite(SYNCHRONOUS, rst3->data(), rst3->length()), |
| 276 | }; |
| 277 | MockRead reads2[] = { |
| 278 | MockRead(SYNCHRONOUS, ERR_IO_PENDING) // Stall forever. |
| 279 | }; |
| 280 | StaticSocketDataProvider socket_data2(reads2, arraysize(reads2), |
| 281 | writes2, arraysize(writes2)); |
| 282 | socket_factory_.AddSocketDataProvider(&socket_data2); |
| 283 | |
| 284 | QuicStreamRequest request(&factory_); |
| 285 | EXPECT_EQ(ERR_IO_PENDING, request.Request(host_port_proxy_pair_, net_log_, |
| 286 | callback_.callback())); |
| 287 | |
| 288 | EXPECT_EQ(OK, callback_.WaitForResult()); |
| 289 | scoped_ptr<QuicHttpStream> stream = request.ReleaseStream(); |
| 290 | |
| 291 | // Change the IP address and verify that stream saw the error. |
| 292 | factory_.OnIPAddressChanged(); |
| 293 | EXPECT_EQ(ERR_NETWORK_CHANGED, |
| 294 | stream->ReadResponseHeaders(callback_.callback())); |
| 295 | |
| 296 | // Now attempting to request a stream to the same origin should create |
| 297 | // a new session. |
| 298 | |
| 299 | QuicStreamRequest request2(&factory_); |
| 300 | EXPECT_EQ(ERR_IO_PENDING, request2.Request(host_port_proxy_pair_, net_log_, |
| 301 | callback_.callback())); |
| 302 | |
| 303 | EXPECT_EQ(OK, callback_.WaitForResult()); |
| 304 | stream = request2.ReleaseStream(); |
| 305 | stream.reset(); // Will reset stream 3. |
| 306 | |
| 307 | EXPECT_TRUE(socket_data.at_read_eof()); |
| 308 | EXPECT_TRUE(socket_data.at_write_eof()); |
| 309 | EXPECT_TRUE(socket_data2.at_read_eof()); |
| 310 | EXPECT_TRUE(socket_data2.at_write_eof()); |
| 311 | } |
| 312 | |
[email protected] | e13201d8 | 2012-12-12 05:00:32 | [diff] [blame] | 313 | } // namespace test |
[email protected] | e13201d8 | 2012-12-12 05:00:32 | [diff] [blame] | 314 | } // namespace net |