[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] | 9db44391 | 2013-02-25 05:27:03 | [diff] [blame] | 50 | QuicRstStreamFrame rst(stream_id, QUIC_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] | 5351cc4b | 2013-03-03 07:22:41 | [diff] [blame] | 74 | QuicFramer framer(kQuicVersion1, |
| 75 | QuicDecrypter::Create(kNULL), |
[email protected] | 14e8106c | 2013-03-14 16:25:33 | [diff] [blame] | 76 | QuicEncrypter::Create(kNULL), |
| 77 | false); |
[email protected] | dda3e9b | 2012-12-22 21:49:25 | [diff] [blame] | 78 | QuicFrames frames; |
| 79 | frames.push_back(QuicFrame(&ack)); |
| 80 | frames.push_back(QuicFrame(&feedback)); |
| 81 | scoped_ptr<QuicPacket> packet( |
[email protected] | 9db44391 | 2013-02-25 05:27:03 | [diff] [blame] | 82 | framer.ConstructFrameDataPacket(header, frames).packet); |
| 83 | return scoped_ptr<QuicEncryptedPacket>( |
| 84 | framer.EncryptPacket(header.packet_sequence_number, *packet)); |
[email protected] | e13201d8 | 2012-12-12 05:00:32 | [diff] [blame] | 85 | } |
| 86 | |
[email protected] | 26f3f8e | 2012-12-13 21:07:19 | [diff] [blame] | 87 | // Returns a newly created packet to send congestion feedback data. |
| 88 | scoped_ptr<QuicEncryptedPacket> ConstructFeedbackPacket( |
| 89 | QuicPacketSequenceNumber sequence_number) { |
| 90 | QuicPacketHeader header; |
[email protected] | c995c57 | 2013-01-18 05:43:20 | [diff] [blame] | 91 | header.public_header.guid = 0xDEADBEEF; |
[email protected] | 9db44391 | 2013-02-25 05:27:03 | [diff] [blame] | 92 | header.public_header.reset_flag = false; |
| 93 | header.public_header.version_flag = false; |
[email protected] | 26f3f8e | 2012-12-13 21:07:19 | [diff] [blame] | 94 | header.packet_sequence_number = sequence_number; |
[email protected] | 9db44391 | 2013-02-25 05:27:03 | [diff] [blame] | 95 | header.entropy_flag = false; |
| 96 | header.fec_entropy_flag = false; |
| 97 | header.fec_flag = false; |
[email protected] | 26f3f8e | 2012-12-13 21:07:19 | [diff] [blame] | 98 | header.fec_group = 0; |
| 99 | |
| 100 | QuicCongestionFeedbackFrame frame; |
| 101 | frame.type = kTCP; |
| 102 | frame.tcp.accumulated_number_of_lost_packets = 0; |
| 103 | frame.tcp.receive_window = 16000; |
| 104 | |
| 105 | return scoped_ptr<QuicEncryptedPacket>( |
| 106 | ConstructPacket(header, QuicFrame(&frame))); |
| 107 | } |
| 108 | |
[email protected] | e13201d8 | 2012-12-12 05:00:32 | [diff] [blame] | 109 | scoped_ptr<QuicEncryptedPacket> ConstructPacket( |
| 110 | const QuicPacketHeader& header, |
| 111 | const QuicFrame& frame) { |
[email protected] | 5351cc4b | 2013-03-03 07:22:41 | [diff] [blame] | 112 | QuicFramer framer(kQuicVersion1, |
| 113 | QuicDecrypter::Create(kNULL), |
[email protected] | 14e8106c | 2013-03-14 16:25:33 | [diff] [blame] | 114 | QuicEncrypter::Create(kNULL), |
| 115 | false); |
[email protected] | e13201d8 | 2012-12-12 05:00:32 | [diff] [blame] | 116 | QuicFrames frames; |
| 117 | frames.push_back(frame); |
[email protected] | 610a7e94 | 2012-12-18 00:21:39 | [diff] [blame] | 118 | scoped_ptr<QuicPacket> packet( |
[email protected] | 9db44391 | 2013-02-25 05:27:03 | [diff] [blame] | 119 | framer.ConstructFrameDataPacket(header, frames).packet); |
| 120 | return scoped_ptr<QuicEncryptedPacket>( |
| 121 | framer.EncryptPacket(header.packet_sequence_number, *packet)); |
[email protected] | e13201d8 | 2012-12-12 05:00:32 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | MockHostResolver host_resolver_; |
| 125 | MockClientSocketFactory socket_factory_; |
[email protected] | e8ff2684 | 2013-03-22 21:02:05 | [diff] [blame^] | 126 | MockCryptoClientStreamFactory crypto_client_stream_factory_; |
[email protected] | 9558c5d3 | 2012-12-22 00:08:14 | [diff] [blame] | 127 | MockRandom random_generator_; |
[email protected] | 872edd9e | 2013-01-16 08:51:15 | [diff] [blame] | 128 | MockClock* clock_; // Owned by factory_. |
[email protected] | e13201d8 | 2012-12-12 05:00:32 | [diff] [blame] | 129 | QuicStreamFactory factory_; |
| 130 | HostPortProxyPair host_port_proxy_pair_; |
| 131 | BoundNetLog net_log_; |
| 132 | TestCompletionCallback callback_; |
[email protected] | e13201d8 | 2012-12-12 05:00:32 | [diff] [blame] | 133 | }; |
| 134 | |
| 135 | TEST_F(QuicStreamFactoryTest, CreateIfSessionExists) { |
| 136 | EXPECT_EQ(NULL, factory_.CreateIfSessionExists(host_port_proxy_pair_, |
| 137 | net_log_).get()); |
| 138 | } |
| 139 | |
| 140 | TEST_F(QuicStreamFactoryTest, Create) { |
[email protected] | e8ff2684 | 2013-03-22 21:02:05 | [diff] [blame^] | 141 | scoped_ptr<QuicEncryptedPacket> rst3(ConstructRstPacket(1, 3)); |
| 142 | scoped_ptr<QuicEncryptedPacket> rst5(ConstructRstPacket(2, 5)); |
| 143 | scoped_ptr<QuicEncryptedPacket> rst7(ConstructRstPacket(3, 7)); |
[email protected] | e13201d8 | 2012-12-12 05:00:32 | [diff] [blame] | 144 | MockWrite writes[] = { |
[email protected] | e13201d8 | 2012-12-12 05:00:32 | [diff] [blame] | 145 | MockWrite(SYNCHRONOUS, rst3->data(), rst3->length()), |
| 146 | MockWrite(SYNCHRONOUS, rst5->data(), rst5->length()), |
| 147 | MockWrite(SYNCHRONOUS, rst7->data(), rst7->length()), |
| 148 | }; |
[email protected] | e13201d8 | 2012-12-12 05:00:32 | [diff] [blame] | 149 | MockRead reads[] = { |
[email protected] | e13201d8 | 2012-12-12 05:00:32 | [diff] [blame] | 150 | MockRead(ASYNC, OK), // EOF |
| 151 | }; |
| 152 | StaticSocketDataProvider socket_data(reads, arraysize(reads), |
| 153 | writes, arraysize(writes)); |
| 154 | socket_factory_.AddSocketDataProvider(&socket_data); |
| 155 | |
| 156 | QuicStreamRequest request(&factory_); |
| 157 | EXPECT_EQ(ERR_IO_PENDING, request.Request(host_port_proxy_pair_, net_log_, |
| 158 | callback_.callback())); |
| 159 | |
| 160 | EXPECT_EQ(OK, callback_.WaitForResult()); |
| 161 | scoped_ptr<QuicHttpStream> stream = request.ReleaseStream(); |
| 162 | |
| 163 | // Will reset stream 3. |
| 164 | stream = factory_.CreateIfSessionExists(host_port_proxy_pair_, net_log_); |
| 165 | EXPECT_TRUE(stream.get()); |
| 166 | |
| 167 | QuicStreamRequest request2(&factory_); |
| 168 | EXPECT_EQ(OK, request2.Request(host_port_proxy_pair_, net_log_, |
| 169 | callback_.callback())); |
| 170 | stream = request2.ReleaseStream(); // Will reset stream 5. |
| 171 | stream.reset(); // Will reset stream 7. |
| 172 | |
| 173 | EXPECT_TRUE(socket_data.at_read_eof()); |
| 174 | EXPECT_TRUE(socket_data.at_write_eof()); |
| 175 | } |
| 176 | |
| 177 | TEST_F(QuicStreamFactoryTest, CreateError) { |
| 178 | StaticSocketDataProvider socket_data(NULL, 0, NULL, 0); |
| 179 | socket_factory_.AddSocketDataProvider(&socket_data); |
| 180 | |
| 181 | host_resolver_.rules()->AddSimulatedFailure("www.google.com"); |
| 182 | |
| 183 | QuicStreamRequest request(&factory_); |
| 184 | EXPECT_EQ(ERR_IO_PENDING, request.Request(host_port_proxy_pair_, net_log_, |
| 185 | callback_.callback())); |
| 186 | |
| 187 | EXPECT_EQ(ERR_NAME_NOT_RESOLVED, callback_.WaitForResult()); |
| 188 | |
| 189 | EXPECT_TRUE(socket_data.at_read_eof()); |
| 190 | EXPECT_TRUE(socket_data.at_write_eof()); |
| 191 | } |
| 192 | |
| 193 | TEST_F(QuicStreamFactoryTest, CancelCreate) { |
[email protected] | e8ff2684 | 2013-03-22 21:02:05 | [diff] [blame^] | 194 | scoped_ptr<QuicEncryptedPacket> rst3(ConstructRstPacket(1, 3)); |
[email protected] | e13201d8 | 2012-12-12 05:00:32 | [diff] [blame] | 195 | |
| 196 | MockWrite writes[] = { |
[email protected] | e13201d8 | 2012-12-12 05:00:32 | [diff] [blame] | 197 | MockWrite(SYNCHRONOUS, rst3->data(), rst3->length()), |
| 198 | }; |
[email protected] | e13201d8 | 2012-12-12 05:00:32 | [diff] [blame] | 199 | MockRead reads[] = { |
[email protected] | e13201d8 | 2012-12-12 05:00:32 | [diff] [blame] | 200 | MockRead(ASYNC, OK), // EOF |
| 201 | }; |
| 202 | StaticSocketDataProvider socket_data(reads, arraysize(reads), |
| 203 | writes, arraysize(writes)); |
| 204 | socket_factory_.AddSocketDataProvider(&socket_data); |
| 205 | { |
| 206 | QuicStreamRequest request(&factory_); |
| 207 | EXPECT_EQ(ERR_IO_PENDING, request.Request(host_port_proxy_pair_, net_log_, |
| 208 | callback_.callback())); |
| 209 | } |
| 210 | |
| 211 | base::RunLoop run_loop; |
| 212 | run_loop.RunUntilIdle(); |
| 213 | |
| 214 | scoped_ptr<QuicHttpStream> stream( |
| 215 | factory_.CreateIfSessionExists(host_port_proxy_pair_, net_log_)); |
| 216 | EXPECT_TRUE(stream.get()); |
| 217 | stream.reset(); |
| 218 | |
| 219 | EXPECT_TRUE(socket_data.at_read_eof()); |
| 220 | EXPECT_TRUE(socket_data.at_write_eof()); |
| 221 | } |
| 222 | |
[email protected] | 56dfb90 | 2013-01-03 23:17:55 | [diff] [blame] | 223 | TEST_F(QuicStreamFactoryTest, CloseAllSessions) { |
[email protected] | e8ff2684 | 2013-03-22 21:02:05 | [diff] [blame^] | 224 | scoped_ptr<QuicEncryptedPacket> rst3(ConstructRstPacket(1, 3)); |
[email protected] | 56dfb90 | 2013-01-03 23:17:55 | [diff] [blame] | 225 | MockRead reads[] = { |
[email protected] | 56dfb90 | 2013-01-03 23:17:55 | [diff] [blame] | 226 | MockRead(ASYNC, OK), // EOF |
| 227 | }; |
| 228 | StaticSocketDataProvider socket_data(reads, arraysize(reads), |
[email protected] | e8ff2684 | 2013-03-22 21:02:05 | [diff] [blame^] | 229 | NULL, 0); |
[email protected] | 56dfb90 | 2013-01-03 23:17:55 | [diff] [blame] | 230 | socket_factory_.AddSocketDataProvider(&socket_data); |
| 231 | |
| 232 | MockWrite writes2[] = { |
[email protected] | 56dfb90 | 2013-01-03 23:17:55 | [diff] [blame] | 233 | MockWrite(SYNCHRONOUS, rst3->data(), rst3->length()), |
| 234 | }; |
| 235 | MockRead reads2[] = { |
[email protected] | 56dfb90 | 2013-01-03 23:17:55 | [diff] [blame] | 236 | MockRead(ASYNC, OK), // EOF |
| 237 | }; |
| 238 | StaticSocketDataProvider socket_data2(reads2, arraysize(reads2), |
| 239 | writes2, arraysize(writes2)); |
| 240 | socket_factory_.AddSocketDataProvider(&socket_data2); |
| 241 | |
| 242 | QuicStreamRequest request(&factory_); |
| 243 | EXPECT_EQ(ERR_IO_PENDING, request.Request(host_port_proxy_pair_, net_log_, |
| 244 | callback_.callback())); |
| 245 | |
| 246 | EXPECT_EQ(OK, callback_.WaitForResult()); |
| 247 | scoped_ptr<QuicHttpStream> stream = request.ReleaseStream(); |
| 248 | |
| 249 | // Close the session and verify that stream saw the error. |
| 250 | factory_.CloseAllSessions(ERR_INTERNET_DISCONNECTED); |
| 251 | EXPECT_EQ(ERR_INTERNET_DISCONNECTED, |
| 252 | stream->ReadResponseHeaders(callback_.callback())); |
| 253 | |
| 254 | // Now attempting to request a stream to the same origin should create |
| 255 | // a new session. |
| 256 | |
| 257 | QuicStreamRequest request2(&factory_); |
| 258 | EXPECT_EQ(ERR_IO_PENDING, request2.Request(host_port_proxy_pair_, net_log_, |
| 259 | callback_.callback())); |
| 260 | |
| 261 | EXPECT_EQ(OK, callback_.WaitForResult()); |
| 262 | stream = request2.ReleaseStream(); |
| 263 | stream.reset(); // Will reset stream 3. |
| 264 | |
| 265 | EXPECT_TRUE(socket_data.at_read_eof()); |
| 266 | EXPECT_TRUE(socket_data.at_write_eof()); |
| 267 | EXPECT_TRUE(socket_data2.at_read_eof()); |
| 268 | EXPECT_TRUE(socket_data2.at_write_eof()); |
| 269 | } |
| 270 | |
[email protected] | e13201d8 | 2012-12-12 05:00:32 | [diff] [blame] | 271 | } // namespace test |
[email protected] | e13201d8 | 2012-12-12 05:00:32 | [diff] [blame] | 272 | } // namespace net |