[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" |
[email protected] | fc9be580 | 2013-06-11 10:56:51 | [diff] [blame] | 8 | #include "base/strings/string_util.h" |
[email protected] | 6d1b4ed | 2013-07-10 03:57:54 | [diff] [blame] | 9 | #include "net/cert/cert_verifier.h" |
[email protected] | f2cb3cf | 2013-03-21 01:40:53 | [diff] [blame] | 10 | #include "net/dns/mock_host_resolver.h" |
[email protected] | e13201d8 | 2012-12-12 05:00:32 | [diff] [blame] | 11 | #include "net/http/http_response_headers.h" |
| 12 | #include "net/http/http_response_info.h" |
| 13 | #include "net/http/http_util.h" |
[email protected] | c49ff18 | 2013-09-28 08:33:26 | [diff] [blame^] | 14 | #include "net/quic/crypto/crypto_handshake.h" |
[email protected] | 4df6984 | 2013-02-27 06:32:16 | [diff] [blame] | 15 | #include "net/quic/crypto/quic_decrypter.h" |
| 16 | #include "net/quic/crypto/quic_encrypter.h" |
[email protected] | e13201d8 | 2012-12-12 05:00:32 | [diff] [blame] | 17 | #include "net/quic/quic_http_stream.h" |
| 18 | #include "net/quic/test_tools/mock_clock.h" |
[email protected] | e8ff2684 | 2013-03-22 21:02:05 | [diff] [blame] | 19 | #include "net/quic/test_tools/mock_crypto_client_stream_factory.h" |
[email protected] | 9558c5d3 | 2012-12-22 00:08:14 | [diff] [blame] | 20 | #include "net/quic/test_tools/mock_random.h" |
[email protected] | e13201d8 | 2012-12-12 05:00:32 | [diff] [blame] | 21 | #include "net/quic/test_tools/quic_test_utils.h" |
| 22 | #include "net/socket/socket_test_util.h" |
| 23 | #include "testing/gtest/include/gtest/gtest.h" |
| 24 | |
| 25 | namespace net { |
[email protected] | e13201d8 | 2012-12-12 05:00:32 | [diff] [blame] | 26 | namespace test { |
| 27 | |
[email protected] | c49ff18 | 2013-09-28 08:33:26 | [diff] [blame^] | 28 | class QuicStreamFactoryPeer { |
| 29 | public: |
| 30 | static QuicCryptoClientConfig* GetOrCreateCryptoConfig( |
| 31 | QuicStreamFactory* factory, |
| 32 | const HostPortProxyPair& host_port_proxy_pair) { |
| 33 | return factory->GetOrCreateCryptoConfig(host_port_proxy_pair); |
| 34 | } |
| 35 | }; |
| 36 | |
[email protected] | e13201d8 | 2012-12-12 05:00:32 | [diff] [blame] | 37 | class QuicStreamFactoryTest : public ::testing::Test { |
| 38 | protected: |
| 39 | QuicStreamFactoryTest() |
[email protected] | 872edd9e | 2013-01-16 08:51:15 | [diff] [blame] | 40 | : clock_(new MockClock()), |
| 41 | factory_(&host_resolver_, &socket_factory_, |
[email protected] | 77c6c16 | 2013-08-17 02:57:45 | [diff] [blame] | 42 | base::WeakPtr<HttpServerProperties>(), |
[email protected] | e8ff2684 | 2013-03-22 21:02:05 | [diff] [blame] | 43 | &crypto_client_stream_factory_, |
[email protected] | 9d9e793 | 2013-02-25 18:31:05 | [diff] [blame] | 44 | &random_generator_, clock_), |
[email protected] | e13201d8 | 2012-12-12 05:00:32 | [diff] [blame] | 45 | host_port_proxy_pair_(HostPortPair("www.google.com", 443), |
[email protected] | 6d1b4ed | 2013-07-10 03:57:54 | [diff] [blame] | 46 | ProxyServer::Direct()), |
| 47 | is_https_(false), |
| 48 | cert_verifier_(CertVerifier::CreateDefault()) { |
[email protected] | 11c0587 | 2013-08-20 02:04:12 | [diff] [blame] | 49 | factory_.set_require_confirmation(false); |
[email protected] | e13201d8 | 2012-12-12 05:00:32 | [diff] [blame] | 50 | } |
| 51 | |
[email protected] | e13201d8 | 2012-12-12 05:00:32 | [diff] [blame] | 52 | scoped_ptr<QuicEncryptedPacket> ConstructRstPacket( |
| 53 | QuicPacketSequenceNumber num, |
| 54 | QuicStreamId stream_id) { |
| 55 | QuicPacketHeader header; |
[email protected] | c995c57 | 2013-01-18 05:43:20 | [diff] [blame] | 56 | header.public_header.guid = 0xDEADBEEF; |
[email protected] | 9db44391 | 2013-02-25 05:27:03 | [diff] [blame] | 57 | header.public_header.reset_flag = false; |
[email protected] | e8ff2684 | 2013-03-22 21:02:05 | [diff] [blame] | 58 | header.public_header.version_flag = true; |
[email protected] | e13201d8 | 2012-12-12 05:00:32 | [diff] [blame] | 59 | header.packet_sequence_number = num; |
[email protected] | 06ff515 | 2013-08-29 01:03:05 | [diff] [blame] | 60 | header.public_header.sequence_number_length = PACKET_1BYTE_SEQUENCE_NUMBER; |
[email protected] | 9db44391 | 2013-02-25 05:27:03 | [diff] [blame] | 61 | header.entropy_flag = false; |
[email protected] | 9db44391 | 2013-02-25 05:27:03 | [diff] [blame] | 62 | header.fec_flag = false; |
[email protected] | e13201d8 | 2012-12-12 05:00:32 | [diff] [blame] | 63 | header.fec_group = 0; |
| 64 | |
[email protected] | 24e5bc5 | 2013-09-18 15:36:58 | [diff] [blame] | 65 | QuicRstStreamFrame rst(stream_id, QUIC_ERROR_PROCESSING_STREAM); |
[email protected] | e13201d8 | 2012-12-12 05:00:32 | [diff] [blame] | 66 | return scoped_ptr<QuicEncryptedPacket>( |
| 67 | ConstructPacket(header, QuicFrame(&rst))); |
| 68 | } |
| 69 | |
| 70 | scoped_ptr<QuicEncryptedPacket> ConstructAckPacket( |
| 71 | QuicPacketSequenceNumber largest_received, |
| 72 | QuicPacketSequenceNumber least_unacked) { |
| 73 | QuicPacketHeader header; |
[email protected] | c995c57 | 2013-01-18 05:43:20 | [diff] [blame] | 74 | header.public_header.guid = 0xDEADBEEF; |
[email protected] | 9db44391 | 2013-02-25 05:27:03 | [diff] [blame] | 75 | header.public_header.reset_flag = false; |
| 76 | header.public_header.version_flag = false; |
[email protected] | e13201d8 | 2012-12-12 05:00:32 | [diff] [blame] | 77 | header.packet_sequence_number = 2; |
[email protected] | 9db44391 | 2013-02-25 05:27:03 | [diff] [blame] | 78 | header.entropy_flag = false; |
[email protected] | 9db44391 | 2013-02-25 05:27:03 | [diff] [blame] | 79 | header.fec_flag = false; |
[email protected] | e13201d8 | 2012-12-12 05:00:32 | [diff] [blame] | 80 | header.fec_group = 0; |
| 81 | |
[email protected] | 14e8106c | 2013-03-14 16:25:33 | [diff] [blame] | 82 | QuicAckFrame ack(largest_received, QuicTime::Zero(), least_unacked); |
[email protected] | dda3e9b | 2012-12-22 21:49:25 | [diff] [blame] | 83 | QuicCongestionFeedbackFrame feedback; |
| 84 | feedback.type = kTCP; |
| 85 | feedback.tcp.accumulated_number_of_lost_packets = 0; |
| 86 | feedback.tcp.receive_window = 16000; |
| 87 | |
[email protected] | 4887809 | 2013-07-26 14:51:56 | [diff] [blame] | 88 | QuicFramer framer(QuicVersionMax(), QuicTime::Zero(), false); |
[email protected] | dda3e9b | 2012-12-22 21:49:25 | [diff] [blame] | 89 | QuicFrames frames; |
| 90 | frames.push_back(QuicFrame(&ack)); |
| 91 | frames.push_back(QuicFrame(&feedback)); |
| 92 | scoped_ptr<QuicPacket> packet( |
[email protected] | 3e60db8 | 2013-08-05 19:43:06 | [diff] [blame] | 93 | framer.BuildUnsizedDataPacket(header, frames).packet); |
[email protected] | 8ba8121 | 2013-05-03 13:11:48 | [diff] [blame] | 94 | return scoped_ptr<QuicEncryptedPacket>(framer.EncryptPacket( |
| 95 | ENCRYPTION_NONE, header.packet_sequence_number, *packet)); |
[email protected] | e13201d8 | 2012-12-12 05:00:32 | [diff] [blame] | 96 | } |
| 97 | |
[email protected] | 26f3f8e | 2012-12-13 21:07:19 | [diff] [blame] | 98 | // Returns a newly created packet to send congestion feedback data. |
| 99 | scoped_ptr<QuicEncryptedPacket> ConstructFeedbackPacket( |
| 100 | QuicPacketSequenceNumber sequence_number) { |
| 101 | QuicPacketHeader header; |
[email protected] | c995c57 | 2013-01-18 05:43:20 | [diff] [blame] | 102 | header.public_header.guid = 0xDEADBEEF; |
[email protected] | 9db44391 | 2013-02-25 05:27:03 | [diff] [blame] | 103 | header.public_header.reset_flag = false; |
| 104 | header.public_header.version_flag = false; |
[email protected] | 26f3f8e | 2012-12-13 21:07:19 | [diff] [blame] | 105 | header.packet_sequence_number = sequence_number; |
[email protected] | 9db44391 | 2013-02-25 05:27:03 | [diff] [blame] | 106 | header.entropy_flag = false; |
[email protected] | 9db44391 | 2013-02-25 05:27:03 | [diff] [blame] | 107 | header.fec_flag = false; |
[email protected] | 26f3f8e | 2012-12-13 21:07:19 | [diff] [blame] | 108 | header.fec_group = 0; |
| 109 | |
| 110 | QuicCongestionFeedbackFrame frame; |
| 111 | frame.type = kTCP; |
| 112 | frame.tcp.accumulated_number_of_lost_packets = 0; |
| 113 | frame.tcp.receive_window = 16000; |
| 114 | |
| 115 | return scoped_ptr<QuicEncryptedPacket>( |
| 116 | ConstructPacket(header, QuicFrame(&frame))); |
| 117 | } |
| 118 | |
[email protected] | e13201d8 | 2012-12-12 05:00:32 | [diff] [blame] | 119 | scoped_ptr<QuicEncryptedPacket> ConstructPacket( |
| 120 | const QuicPacketHeader& header, |
| 121 | const QuicFrame& frame) { |
[email protected] | 4887809 | 2013-07-26 14:51:56 | [diff] [blame] | 122 | QuicFramer framer(QuicVersionMax(), QuicTime::Zero(), false); |
[email protected] | e13201d8 | 2012-12-12 05:00:32 | [diff] [blame] | 123 | QuicFrames frames; |
| 124 | frames.push_back(frame); |
[email protected] | 610a7e94 | 2012-12-18 00:21:39 | [diff] [blame] | 125 | scoped_ptr<QuicPacket> packet( |
[email protected] | 3e60db8 | 2013-08-05 19:43:06 | [diff] [blame] | 126 | framer.BuildUnsizedDataPacket(header, frames).packet); |
[email protected] | 8ba8121 | 2013-05-03 13:11:48 | [diff] [blame] | 127 | return scoped_ptr<QuicEncryptedPacket>(framer.EncryptPacket( |
| 128 | ENCRYPTION_NONE, header.packet_sequence_number, *packet)); |
[email protected] | e13201d8 | 2012-12-12 05:00:32 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | MockHostResolver host_resolver_; |
[email protected] | 0edce6a | 2013-05-08 18:02:40 | [diff] [blame] | 132 | DeterministicMockClientSocketFactory socket_factory_; |
[email protected] | e8ff2684 | 2013-03-22 21:02:05 | [diff] [blame] | 133 | MockCryptoClientStreamFactory crypto_client_stream_factory_; |
[email protected] | 9558c5d3 | 2012-12-22 00:08:14 | [diff] [blame] | 134 | MockRandom random_generator_; |
[email protected] | 872edd9e | 2013-01-16 08:51:15 | [diff] [blame] | 135 | MockClock* clock_; // Owned by factory_. |
[email protected] | e13201d8 | 2012-12-12 05:00:32 | [diff] [blame] | 136 | QuicStreamFactory factory_; |
| 137 | HostPortProxyPair host_port_proxy_pair_; |
[email protected] | 6d1b4ed | 2013-07-10 03:57:54 | [diff] [blame] | 138 | bool is_https_; |
[email protected] | ce542d5 | 2013-07-11 01:08:39 | [diff] [blame] | 139 | scoped_ptr<CertVerifier> cert_verifier_; |
[email protected] | e13201d8 | 2012-12-12 05:00:32 | [diff] [blame] | 140 | BoundNetLog net_log_; |
| 141 | TestCompletionCallback callback_; |
[email protected] | e13201d8 | 2012-12-12 05:00:32 | [diff] [blame] | 142 | }; |
| 143 | |
| 144 | TEST_F(QuicStreamFactoryTest, CreateIfSessionExists) { |
| 145 | EXPECT_EQ(NULL, factory_.CreateIfSessionExists(host_port_proxy_pair_, |
| 146 | net_log_).get()); |
| 147 | } |
| 148 | |
| 149 | TEST_F(QuicStreamFactoryTest, Create) { |
[email protected] | 69dfd1b | 2013-06-04 22:20:12 | [diff] [blame] | 150 | MockRead reads[] = { |
[email protected] | 25c31dc | 2013-06-05 17:56:04 | [diff] [blame] | 151 | MockRead(ASYNC, OK, 0) // EOF |
[email protected] | 69dfd1b | 2013-06-04 22:20:12 | [diff] [blame] | 152 | }; |
[email protected] | 25c31dc | 2013-06-05 17:56:04 | [diff] [blame] | 153 | DeterministicSocketData socket_data(reads, arraysize(reads), NULL, 0); |
[email protected] | e13201d8 | 2012-12-12 05:00:32 | [diff] [blame] | 154 | socket_factory_.AddSocketDataProvider(&socket_data); |
[email protected] | 25c31dc | 2013-06-05 17:56:04 | [diff] [blame] | 155 | socket_data.StopAfter(1); |
[email protected] | e13201d8 | 2012-12-12 05:00:32 | [diff] [blame] | 156 | |
| 157 | QuicStreamRequest request(&factory_); |
[email protected] | 6d1b4ed | 2013-07-10 03:57:54 | [diff] [blame] | 158 | EXPECT_EQ(ERR_IO_PENDING, request.Request(host_port_proxy_pair_, is_https_, |
[email protected] | ce542d5 | 2013-07-11 01:08:39 | [diff] [blame] | 159 | cert_verifier_.get(), net_log_, |
[email protected] | e13201d8 | 2012-12-12 05:00:32 | [diff] [blame] | 160 | callback_.callback())); |
| 161 | |
| 162 | EXPECT_EQ(OK, callback_.WaitForResult()); |
| 163 | scoped_ptr<QuicHttpStream> stream = request.ReleaseStream(); |
[email protected] | 0edce6a | 2013-05-08 18:02:40 | [diff] [blame] | 164 | EXPECT_TRUE(stream.get()); |
[email protected] | e13201d8 | 2012-12-12 05:00:32 | [diff] [blame] | 165 | |
| 166 | // Will reset stream 3. |
| 167 | stream = factory_.CreateIfSessionExists(host_port_proxy_pair_, net_log_); |
| 168 | EXPECT_TRUE(stream.get()); |
| 169 | |
[email protected] | 6d1b4ed | 2013-07-10 03:57:54 | [diff] [blame] | 170 | // TODO(rtenneti): We should probably have a tests that HTTP and HTTPS result |
| 171 | // in streams on different sessions. |
[email protected] | e13201d8 | 2012-12-12 05:00:32 | [diff] [blame] | 172 | QuicStreamRequest request2(&factory_); |
[email protected] | 6d1b4ed | 2013-07-10 03:57:54 | [diff] [blame] | 173 | EXPECT_EQ(OK, request2.Request(host_port_proxy_pair_, is_https_, |
[email protected] | ce542d5 | 2013-07-11 01:08:39 | [diff] [blame] | 174 | cert_verifier_.get(), net_log_, |
[email protected] | e13201d8 | 2012-12-12 05:00:32 | [diff] [blame] | 175 | callback_.callback())); |
| 176 | stream = request2.ReleaseStream(); // Will reset stream 5. |
| 177 | stream.reset(); // Will reset stream 7. |
| 178 | |
| 179 | EXPECT_TRUE(socket_data.at_read_eof()); |
| 180 | EXPECT_TRUE(socket_data.at_write_eof()); |
| 181 | } |
| 182 | |
[email protected] | 0b2294d3 | 2013-08-02 00:46:36 | [diff] [blame] | 183 | TEST_F(QuicStreamFactoryTest, MaxOpenStream) { |
| 184 | MockRead reads[] = { |
| 185 | MockRead(ASYNC, OK, 0) // EOF |
| 186 | }; |
[email protected] | 06ff515 | 2013-08-29 01:03:05 | [diff] [blame] | 187 | scoped_ptr<QuicEncryptedPacket> rst(ConstructRstPacket(1, 3)); |
| 188 | MockWrite writes[] = { |
| 189 | MockWrite(ASYNC, rst->data(), rst->length(), 1), |
| 190 | }; |
| 191 | DeterministicSocketData socket_data(reads, arraysize(reads), |
| 192 | writes, arraysize(writes)); |
[email protected] | 0b2294d3 | 2013-08-02 00:46:36 | [diff] [blame] | 193 | socket_factory_.AddSocketDataProvider(&socket_data); |
| 194 | socket_data.StopAfter(1); |
| 195 | |
| 196 | HttpRequestInfo request_info; |
| 197 | std::vector<QuicHttpStream*> streams; |
| 198 | // The MockCryptoClientStream sets max_open_streams to be |
| 199 | // 2 * kDefaultMaxStreamsPerConnection. |
| 200 | for (size_t i = 0; i < 2 * kDefaultMaxStreamsPerConnection; i++) { |
| 201 | QuicStreamRequest request(&factory_); |
| 202 | int rv = request.Request(host_port_proxy_pair_, is_https_, |
| 203 | cert_verifier_.get(), net_log_, |
| 204 | callback_.callback()); |
| 205 | if (i == 0) { |
| 206 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 207 | EXPECT_EQ(OK, callback_.WaitForResult()); |
| 208 | } else { |
| 209 | EXPECT_EQ(OK, rv); |
| 210 | } |
| 211 | scoped_ptr<QuicHttpStream> stream = request.ReleaseStream(); |
| 212 | EXPECT_TRUE(stream); |
| 213 | EXPECT_EQ(OK, stream->InitializeStream( |
| 214 | &request_info, DEFAULT_PRIORITY, net_log_, CompletionCallback())); |
| 215 | streams.push_back(stream.release()); |
| 216 | } |
| 217 | |
| 218 | QuicStreamRequest request(&factory_); |
| 219 | EXPECT_EQ(OK, request.Request(host_port_proxy_pair_, is_https_, |
| 220 | cert_verifier_.get(), net_log_, |
| 221 | CompletionCallback())); |
| 222 | scoped_ptr<QuicHttpStream> stream = request.ReleaseStream(); |
| 223 | EXPECT_TRUE(stream); |
| 224 | EXPECT_EQ(ERR_IO_PENDING, stream->InitializeStream( |
| 225 | &request_info, DEFAULT_PRIORITY, net_log_, callback_.callback())); |
| 226 | |
| 227 | // Close the first stream. |
| 228 | streams.front()->Close(false); |
| 229 | |
| 230 | ASSERT_TRUE(callback_.have_result()); |
| 231 | |
| 232 | EXPECT_EQ(OK, callback_.WaitForResult()); |
| 233 | |
| 234 | EXPECT_TRUE(socket_data.at_read_eof()); |
| 235 | EXPECT_TRUE(socket_data.at_write_eof()); |
| 236 | STLDeleteElements(&streams); |
| 237 | } |
| 238 | |
[email protected] | e13201d8 | 2012-12-12 05:00:32 | [diff] [blame] | 239 | TEST_F(QuicStreamFactoryTest, CreateError) { |
[email protected] | 0edce6a | 2013-05-08 18:02:40 | [diff] [blame] | 240 | DeterministicSocketData socket_data(NULL, 0, NULL, 0); |
[email protected] | e13201d8 | 2012-12-12 05:00:32 | [diff] [blame] | 241 | socket_factory_.AddSocketDataProvider(&socket_data); |
| 242 | |
| 243 | host_resolver_.rules()->AddSimulatedFailure("www.google.com"); |
| 244 | |
| 245 | QuicStreamRequest request(&factory_); |
[email protected] | 6d1b4ed | 2013-07-10 03:57:54 | [diff] [blame] | 246 | EXPECT_EQ(ERR_IO_PENDING, request.Request(host_port_proxy_pair_, is_https_, |
[email protected] | ce542d5 | 2013-07-11 01:08:39 | [diff] [blame] | 247 | cert_verifier_.get(), net_log_, |
[email protected] | e13201d8 | 2012-12-12 05:00:32 | [diff] [blame] | 248 | callback_.callback())); |
| 249 | |
| 250 | EXPECT_EQ(ERR_NAME_NOT_RESOLVED, callback_.WaitForResult()); |
| 251 | |
| 252 | EXPECT_TRUE(socket_data.at_read_eof()); |
| 253 | EXPECT_TRUE(socket_data.at_write_eof()); |
| 254 | } |
| 255 | |
| 256 | TEST_F(QuicStreamFactoryTest, CancelCreate) { |
[email protected] | 69dfd1b | 2013-06-04 22:20:12 | [diff] [blame] | 257 | MockRead reads[] = { |
[email protected] | 25c31dc | 2013-06-05 17:56:04 | [diff] [blame] | 258 | MockRead(ASYNC, OK, 0) // EOF |
[email protected] | 69dfd1b | 2013-06-04 22:20:12 | [diff] [blame] | 259 | }; |
[email protected] | 25c31dc | 2013-06-05 17:56:04 | [diff] [blame] | 260 | DeterministicSocketData socket_data(reads, arraysize(reads), NULL, 0); |
[email protected] | e13201d8 | 2012-12-12 05:00:32 | [diff] [blame] | 261 | socket_factory_.AddSocketDataProvider(&socket_data); |
| 262 | { |
| 263 | QuicStreamRequest request(&factory_); |
[email protected] | 6d1b4ed | 2013-07-10 03:57:54 | [diff] [blame] | 264 | EXPECT_EQ(ERR_IO_PENDING, request.Request(host_port_proxy_pair_, is_https_, |
[email protected] | ce542d5 | 2013-07-11 01:08:39 | [diff] [blame] | 265 | cert_verifier_.get(), net_log_, |
[email protected] | e13201d8 | 2012-12-12 05:00:32 | [diff] [blame] | 266 | callback_.callback())); |
| 267 | } |
| 268 | |
[email protected] | 25c31dc | 2013-06-05 17:56:04 | [diff] [blame] | 269 | socket_data.StopAfter(1); |
[email protected] | e13201d8 | 2012-12-12 05:00:32 | [diff] [blame] | 270 | base::RunLoop run_loop; |
| 271 | run_loop.RunUntilIdle(); |
| 272 | |
| 273 | scoped_ptr<QuicHttpStream> stream( |
| 274 | factory_.CreateIfSessionExists(host_port_proxy_pair_, net_log_)); |
| 275 | EXPECT_TRUE(stream.get()); |
| 276 | stream.reset(); |
| 277 | |
| 278 | EXPECT_TRUE(socket_data.at_read_eof()); |
| 279 | EXPECT_TRUE(socket_data.at_write_eof()); |
| 280 | } |
| 281 | |
[email protected] | 56dfb90 | 2013-01-03 23:17:55 | [diff] [blame] | 282 | TEST_F(QuicStreamFactoryTest, CloseAllSessions) { |
[email protected] | 56dfb90 | 2013-01-03 23:17:55 | [diff] [blame] | 283 | MockRead reads[] = { |
[email protected] | 0edce6a | 2013-05-08 18:02:40 | [diff] [blame] | 284 | MockRead(ASYNC, 0, 0) // EOF |
[email protected] | 56dfb90 | 2013-01-03 23:17:55 | [diff] [blame] | 285 | }; |
[email protected] | 25c31dc | 2013-06-05 17:56:04 | [diff] [blame] | 286 | DeterministicSocketData socket_data(reads, arraysize(reads), NULL, 0); |
[email protected] | 56dfb90 | 2013-01-03 23:17:55 | [diff] [blame] | 287 | socket_factory_.AddSocketDataProvider(&socket_data); |
[email protected] | 0edce6a | 2013-05-08 18:02:40 | [diff] [blame] | 288 | socket_data.StopAfter(1); |
[email protected] | 56dfb90 | 2013-01-03 23:17:55 | [diff] [blame] | 289 | |
[email protected] | 69dfd1b | 2013-06-04 22:20:12 | [diff] [blame] | 290 | MockRead reads2[] = { |
[email protected] | 25c31dc | 2013-06-05 17:56:04 | [diff] [blame] | 291 | MockRead(ASYNC, 0, 0) // EOF |
[email protected] | 69dfd1b | 2013-06-04 22:20:12 | [diff] [blame] | 292 | }; |
[email protected] | 25c31dc | 2013-06-05 17:56:04 | [diff] [blame] | 293 | DeterministicSocketData socket_data2(reads2, arraysize(reads2), NULL, 0); |
[email protected] | 56dfb90 | 2013-01-03 23:17:55 | [diff] [blame] | 294 | socket_factory_.AddSocketDataProvider(&socket_data2); |
[email protected] | 0edce6a | 2013-05-08 18:02:40 | [diff] [blame] | 295 | socket_data2.StopAfter(1); |
[email protected] | 56dfb90 | 2013-01-03 23:17:55 | [diff] [blame] | 296 | |
| 297 | QuicStreamRequest request(&factory_); |
[email protected] | 6d1b4ed | 2013-07-10 03:57:54 | [diff] [blame] | 298 | EXPECT_EQ(ERR_IO_PENDING, request.Request(host_port_proxy_pair_, is_https_, |
[email protected] | ce542d5 | 2013-07-11 01:08:39 | [diff] [blame] | 299 | cert_verifier_.get(), net_log_, |
[email protected] | 56dfb90 | 2013-01-03 23:17:55 | [diff] [blame] | 300 | callback_.callback())); |
| 301 | |
| 302 | EXPECT_EQ(OK, callback_.WaitForResult()); |
| 303 | scoped_ptr<QuicHttpStream> stream = request.ReleaseStream(); |
[email protected] | 0b2294d3 | 2013-08-02 00:46:36 | [diff] [blame] | 304 | HttpRequestInfo request_info; |
| 305 | EXPECT_EQ(OK, stream->InitializeStream(&request_info, |
| 306 | DEFAULT_PRIORITY, |
| 307 | net_log_, CompletionCallback())); |
[email protected] | 56dfb90 | 2013-01-03 23:17:55 | [diff] [blame] | 308 | |
| 309 | // Close the session and verify that stream saw the error. |
| 310 | factory_.CloseAllSessions(ERR_INTERNET_DISCONNECTED); |
| 311 | EXPECT_EQ(ERR_INTERNET_DISCONNECTED, |
| 312 | stream->ReadResponseHeaders(callback_.callback())); |
| 313 | |
| 314 | // Now attempting to request a stream to the same origin should create |
| 315 | // a new session. |
| 316 | |
| 317 | QuicStreamRequest request2(&factory_); |
[email protected] | 6d1b4ed | 2013-07-10 03:57:54 | [diff] [blame] | 318 | EXPECT_EQ(ERR_IO_PENDING, request2.Request(host_port_proxy_pair_, is_https_, |
[email protected] | ce542d5 | 2013-07-11 01:08:39 | [diff] [blame] | 319 | cert_verifier_.get(), net_log_, |
[email protected] | 56dfb90 | 2013-01-03 23:17:55 | [diff] [blame] | 320 | callback_.callback())); |
| 321 | |
| 322 | EXPECT_EQ(OK, callback_.WaitForResult()); |
| 323 | stream = request2.ReleaseStream(); |
| 324 | stream.reset(); // Will reset stream 3. |
| 325 | |
| 326 | EXPECT_TRUE(socket_data.at_read_eof()); |
| 327 | EXPECT_TRUE(socket_data.at_write_eof()); |
| 328 | EXPECT_TRUE(socket_data2.at_read_eof()); |
| 329 | EXPECT_TRUE(socket_data2.at_write_eof()); |
| 330 | } |
| 331 | |
[email protected] | f698a01 | 2013-05-06 20:18:59 | [diff] [blame] | 332 | TEST_F(QuicStreamFactoryTest, OnIPAddressChanged) { |
[email protected] | f698a01 | 2013-05-06 20:18:59 | [diff] [blame] | 333 | MockRead reads[] = { |
[email protected] | 0edce6a | 2013-05-08 18:02:40 | [diff] [blame] | 334 | MockRead(ASYNC, 0, 0) // EOF |
[email protected] | f698a01 | 2013-05-06 20:18:59 | [diff] [blame] | 335 | }; |
[email protected] | 25c31dc | 2013-06-05 17:56:04 | [diff] [blame] | 336 | DeterministicSocketData socket_data(reads, arraysize(reads), NULL, 0); |
[email protected] | f698a01 | 2013-05-06 20:18:59 | [diff] [blame] | 337 | socket_factory_.AddSocketDataProvider(&socket_data); |
[email protected] | 0edce6a | 2013-05-08 18:02:40 | [diff] [blame] | 338 | socket_data.StopAfter(1); |
[email protected] | f698a01 | 2013-05-06 20:18:59 | [diff] [blame] | 339 | |
[email protected] | 69dfd1b | 2013-06-04 22:20:12 | [diff] [blame] | 340 | MockRead reads2[] = { |
[email protected] | 25c31dc | 2013-06-05 17:56:04 | [diff] [blame] | 341 | MockRead(ASYNC, 0, 0) // EOF |
[email protected] | 69dfd1b | 2013-06-04 22:20:12 | [diff] [blame] | 342 | }; |
[email protected] | 25c31dc | 2013-06-05 17:56:04 | [diff] [blame] | 343 | DeterministicSocketData socket_data2(reads2, arraysize(reads2), NULL, 0); |
[email protected] | f698a01 | 2013-05-06 20:18:59 | [diff] [blame] | 344 | socket_factory_.AddSocketDataProvider(&socket_data2); |
[email protected] | 0edce6a | 2013-05-08 18:02:40 | [diff] [blame] | 345 | socket_data2.StopAfter(1); |
[email protected] | f698a01 | 2013-05-06 20:18:59 | [diff] [blame] | 346 | |
| 347 | QuicStreamRequest request(&factory_); |
[email protected] | 6d1b4ed | 2013-07-10 03:57:54 | [diff] [blame] | 348 | EXPECT_EQ(ERR_IO_PENDING, request.Request(host_port_proxy_pair_, is_https_, |
[email protected] | ce542d5 | 2013-07-11 01:08:39 | [diff] [blame] | 349 | cert_verifier_.get(), net_log_, |
[email protected] | f698a01 | 2013-05-06 20:18:59 | [diff] [blame] | 350 | callback_.callback())); |
| 351 | |
| 352 | EXPECT_EQ(OK, callback_.WaitForResult()); |
| 353 | scoped_ptr<QuicHttpStream> stream = request.ReleaseStream(); |
[email protected] | 0b2294d3 | 2013-08-02 00:46:36 | [diff] [blame] | 354 | HttpRequestInfo request_info; |
| 355 | EXPECT_EQ(OK, stream->InitializeStream(&request_info, |
| 356 | DEFAULT_PRIORITY, |
| 357 | net_log_, CompletionCallback())); |
[email protected] | f698a01 | 2013-05-06 20:18:59 | [diff] [blame] | 358 | |
| 359 | // Change the IP address and verify that stream saw the error. |
| 360 | factory_.OnIPAddressChanged(); |
| 361 | EXPECT_EQ(ERR_NETWORK_CHANGED, |
| 362 | stream->ReadResponseHeaders(callback_.callback())); |
[email protected] | 11c0587 | 2013-08-20 02:04:12 | [diff] [blame] | 363 | EXPECT_TRUE(factory_.require_confirmation()); |
[email protected] | f698a01 | 2013-05-06 20:18:59 | [diff] [blame] | 364 | |
| 365 | // Now attempting to request a stream to the same origin should create |
| 366 | // a new session. |
| 367 | |
| 368 | QuicStreamRequest request2(&factory_); |
[email protected] | 6d1b4ed | 2013-07-10 03:57:54 | [diff] [blame] | 369 | EXPECT_EQ(ERR_IO_PENDING, request2.Request(host_port_proxy_pair_, is_https_, |
[email protected] | ce542d5 | 2013-07-11 01:08:39 | [diff] [blame] | 370 | cert_verifier_.get(), net_log_, |
[email protected] | f698a01 | 2013-05-06 20:18:59 | [diff] [blame] | 371 | callback_.callback())); |
| 372 | |
| 373 | EXPECT_EQ(OK, callback_.WaitForResult()); |
| 374 | stream = request2.ReleaseStream(); |
| 375 | stream.reset(); // Will reset stream 3. |
| 376 | |
| 377 | EXPECT_TRUE(socket_data.at_read_eof()); |
| 378 | EXPECT_TRUE(socket_data.at_write_eof()); |
| 379 | EXPECT_TRUE(socket_data2.at_read_eof()); |
| 380 | EXPECT_TRUE(socket_data2.at_write_eof()); |
| 381 | } |
| 382 | |
[email protected] | c49ff18 | 2013-09-28 08:33:26 | [diff] [blame^] | 383 | TEST_F(QuicStreamFactoryTest, SharedCryptoConfig) { |
| 384 | HostPortProxyPair host_port_proxy_pair1(HostPortPair("r1.c.youtube.com", 80), |
| 385 | ProxyServer::Direct()); |
| 386 | |
| 387 | QuicCryptoClientConfig* crypto_config1 = |
| 388 | QuicStreamFactoryPeer::GetOrCreateCryptoConfig(&factory_, |
| 389 | host_port_proxy_pair1); |
| 390 | DCHECK(crypto_config1); |
| 391 | QuicCryptoClientConfig::CachedState* cached1 = |
| 392 | crypto_config1->LookupOrCreate(host_port_proxy_pair1.first.host()); |
| 393 | // Mutate the cached1 to have different data. |
| 394 | // TODO(rtenneti): mutate other members of CachedState. |
| 395 | cached1->set_source_address_token("c.youtube.com"); |
| 396 | |
| 397 | HostPortProxyPair host_port_proxy_pair2(HostPortPair("r2.c.youtube.com", 80), |
| 398 | ProxyServer::Direct()); |
| 399 | QuicCryptoClientConfig* crypto_config2 = |
| 400 | QuicStreamFactoryPeer::GetOrCreateCryptoConfig(&factory_, |
| 401 | host_port_proxy_pair2); |
| 402 | DCHECK(crypto_config2); |
| 403 | QuicCryptoClientConfig::CachedState* cached2 = |
| 404 | crypto_config2->LookupOrCreate(host_port_proxy_pair2.first.host()); |
| 405 | EXPECT_EQ(cached1->source_address_token(), cached2->source_address_token()); |
| 406 | } |
| 407 | |
[email protected] | e13201d8 | 2012-12-12 05:00:32 | [diff] [blame] | 408 | } // namespace test |
[email protected] | e13201d8 | 2012-12-12 05:00:32 | [diff] [blame] | 409 | } // namespace net |