[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 | } |
[email protected] | 4d283b3 | 2013-10-17 12:57:27 | [diff] [blame] | 35 | |
| 36 | static bool HasActiveSession(QuicStreamFactory* factory, |
| 37 | const HostPortProxyPair& host_port_proxy_pair) { |
| 38 | return factory->HasActiveSession(host_port_proxy_pair); |
| 39 | } |
| 40 | |
| 41 | static QuicClientSession* GetActiveSession( |
| 42 | QuicStreamFactory* factory, |
| 43 | const HostPortProxyPair& host_port_proxy_pair) { |
| 44 | DCHECK(factory->HasActiveSession(host_port_proxy_pair)); |
| 45 | return factory->active_sessions_[host_port_proxy_pair]; |
| 46 | } |
| 47 | |
| 48 | static bool IsLiveSession(QuicStreamFactory* factory, |
| 49 | QuicClientSession* session) { |
| 50 | for (QuicStreamFactory::SessionSet::iterator it = |
| 51 | factory->all_sessions_.begin(); |
| 52 | it != factory->all_sessions_.end(); ++it) { |
| 53 | if (*it == session) |
| 54 | return true; |
| 55 | } |
| 56 | return false; |
| 57 | } |
[email protected] | c49ff18 | 2013-09-28 08:33:26 | [diff] [blame] | 58 | }; |
| 59 | |
[email protected] | e13201d8 | 2012-12-12 05:00:32 | [diff] [blame] | 60 | class QuicStreamFactoryTest : public ::testing::Test { |
| 61 | protected: |
| 62 | QuicStreamFactoryTest() |
[email protected] | 872edd9e | 2013-01-16 08:51:15 | [diff] [blame] | 63 | : clock_(new MockClock()), |
| 64 | factory_(&host_resolver_, &socket_factory_, |
[email protected] | 77c6c16 | 2013-08-17 02:57:45 | [diff] [blame] | 65 | base::WeakPtr<HttpServerProperties>(), |
[email protected] | e8ff2684 | 2013-03-22 21:02:05 | [diff] [blame] | 66 | &crypto_client_stream_factory_, |
[email protected] | 9d9e793 | 2013-02-25 18:31:05 | [diff] [blame] | 67 | &random_generator_, clock_), |
[email protected] | e13201d8 | 2012-12-12 05:00:32 | [diff] [blame] | 68 | host_port_proxy_pair_(HostPortPair("www.google.com", 443), |
[email protected] | 6d1b4ed | 2013-07-10 03:57:54 | [diff] [blame] | 69 | ProxyServer::Direct()), |
| 70 | is_https_(false), |
| 71 | cert_verifier_(CertVerifier::CreateDefault()) { |
[email protected] | 11c0587 | 2013-08-20 02:04:12 | [diff] [blame] | 72 | factory_.set_require_confirmation(false); |
[email protected] | e13201d8 | 2012-12-12 05:00:32 | [diff] [blame] | 73 | } |
| 74 | |
[email protected] | e13201d8 | 2012-12-12 05:00:32 | [diff] [blame] | 75 | scoped_ptr<QuicEncryptedPacket> ConstructRstPacket( |
| 76 | QuicPacketSequenceNumber num, |
| 77 | QuicStreamId stream_id) { |
| 78 | QuicPacketHeader header; |
[email protected] | c995c57 | 2013-01-18 05:43:20 | [diff] [blame] | 79 | header.public_header.guid = 0xDEADBEEF; |
[email protected] | 9db44391 | 2013-02-25 05:27:03 | [diff] [blame] | 80 | header.public_header.reset_flag = false; |
[email protected] | e8ff2684 | 2013-03-22 21:02:05 | [diff] [blame] | 81 | header.public_header.version_flag = true; |
[email protected] | e13201d8 | 2012-12-12 05:00:32 | [diff] [blame] | 82 | header.packet_sequence_number = num; |
[email protected] | 06ff515 | 2013-08-29 01:03:05 | [diff] [blame] | 83 | header.public_header.sequence_number_length = PACKET_1BYTE_SEQUENCE_NUMBER; |
[email protected] | 9db44391 | 2013-02-25 05:27:03 | [diff] [blame] | 84 | header.entropy_flag = false; |
[email protected] | 9db44391 | 2013-02-25 05:27:03 | [diff] [blame] | 85 | header.fec_flag = false; |
[email protected] | e13201d8 | 2012-12-12 05:00:32 | [diff] [blame] | 86 | header.fec_group = 0; |
| 87 | |
[email protected] | 6548b66 | 2013-10-23 01:38:53 | [diff] [blame] | 88 | QuicRstStreamFrame rst(stream_id, QUIC_STREAM_CANCELLED); |
[email protected] | e13201d8 | 2012-12-12 05:00:32 | [diff] [blame] | 89 | return scoped_ptr<QuicEncryptedPacket>( |
| 90 | ConstructPacket(header, QuicFrame(&rst))); |
| 91 | } |
| 92 | |
| 93 | scoped_ptr<QuicEncryptedPacket> ConstructAckPacket( |
| 94 | QuicPacketSequenceNumber largest_received, |
| 95 | QuicPacketSequenceNumber least_unacked) { |
| 96 | QuicPacketHeader header; |
[email protected] | c995c57 | 2013-01-18 05:43:20 | [diff] [blame] | 97 | header.public_header.guid = 0xDEADBEEF; |
[email protected] | 9db44391 | 2013-02-25 05:27:03 | [diff] [blame] | 98 | header.public_header.reset_flag = false; |
| 99 | header.public_header.version_flag = false; |
[email protected] | e13201d8 | 2012-12-12 05:00:32 | [diff] [blame] | 100 | header.packet_sequence_number = 2; |
[email protected] | 9db44391 | 2013-02-25 05:27:03 | [diff] [blame] | 101 | header.entropy_flag = false; |
[email protected] | 9db44391 | 2013-02-25 05:27:03 | [diff] [blame] | 102 | header.fec_flag = false; |
[email protected] | e13201d8 | 2012-12-12 05:00:32 | [diff] [blame] | 103 | header.fec_group = 0; |
| 104 | |
[email protected] | 14e8106c | 2013-03-14 16:25:33 | [diff] [blame] | 105 | QuicAckFrame ack(largest_received, QuicTime::Zero(), least_unacked); |
[email protected] | dda3e9b | 2012-12-22 21:49:25 | [diff] [blame] | 106 | QuicCongestionFeedbackFrame feedback; |
| 107 | feedback.type = kTCP; |
| 108 | feedback.tcp.accumulated_number_of_lost_packets = 0; |
| 109 | feedback.tcp.receive_window = 16000; |
| 110 | |
[email protected] | 4887809 | 2013-07-26 14:51:56 | [diff] [blame] | 111 | QuicFramer framer(QuicVersionMax(), QuicTime::Zero(), false); |
[email protected] | dda3e9b | 2012-12-22 21:49:25 | [diff] [blame] | 112 | QuicFrames frames; |
| 113 | frames.push_back(QuicFrame(&ack)); |
| 114 | frames.push_back(QuicFrame(&feedback)); |
| 115 | scoped_ptr<QuicPacket> packet( |
[email protected] | 3e60db8 | 2013-08-05 19:43:06 | [diff] [blame] | 116 | framer.BuildUnsizedDataPacket(header, frames).packet); |
[email protected] | 8ba8121 | 2013-05-03 13:11:48 | [diff] [blame] | 117 | return scoped_ptr<QuicEncryptedPacket>(framer.EncryptPacket( |
| 118 | ENCRYPTION_NONE, header.packet_sequence_number, *packet)); |
[email protected] | e13201d8 | 2012-12-12 05:00:32 | [diff] [blame] | 119 | } |
| 120 | |
[email protected] | 26f3f8e | 2012-12-13 21:07:19 | [diff] [blame] | 121 | // Returns a newly created packet to send congestion feedback data. |
| 122 | scoped_ptr<QuicEncryptedPacket> ConstructFeedbackPacket( |
| 123 | QuicPacketSequenceNumber sequence_number) { |
| 124 | QuicPacketHeader header; |
[email protected] | c995c57 | 2013-01-18 05:43:20 | [diff] [blame] | 125 | header.public_header.guid = 0xDEADBEEF; |
[email protected] | 9db44391 | 2013-02-25 05:27:03 | [diff] [blame] | 126 | header.public_header.reset_flag = false; |
| 127 | header.public_header.version_flag = false; |
[email protected] | 26f3f8e | 2012-12-13 21:07:19 | [diff] [blame] | 128 | header.packet_sequence_number = sequence_number; |
[email protected] | 9db44391 | 2013-02-25 05:27:03 | [diff] [blame] | 129 | header.entropy_flag = false; |
[email protected] | 9db44391 | 2013-02-25 05:27:03 | [diff] [blame] | 130 | header.fec_flag = false; |
[email protected] | 26f3f8e | 2012-12-13 21:07:19 | [diff] [blame] | 131 | header.fec_group = 0; |
| 132 | |
| 133 | QuicCongestionFeedbackFrame frame; |
| 134 | frame.type = kTCP; |
| 135 | frame.tcp.accumulated_number_of_lost_packets = 0; |
| 136 | frame.tcp.receive_window = 16000; |
| 137 | |
| 138 | return scoped_ptr<QuicEncryptedPacket>( |
| 139 | ConstructPacket(header, QuicFrame(&frame))); |
| 140 | } |
| 141 | |
[email protected] | e13201d8 | 2012-12-12 05:00:32 | [diff] [blame] | 142 | scoped_ptr<QuicEncryptedPacket> ConstructPacket( |
| 143 | const QuicPacketHeader& header, |
| 144 | const QuicFrame& frame) { |
[email protected] | 4887809 | 2013-07-26 14:51:56 | [diff] [blame] | 145 | QuicFramer framer(QuicVersionMax(), QuicTime::Zero(), false); |
[email protected] | e13201d8 | 2012-12-12 05:00:32 | [diff] [blame] | 146 | QuicFrames frames; |
| 147 | frames.push_back(frame); |
[email protected] | 610a7e94 | 2012-12-18 00:21:39 | [diff] [blame] | 148 | scoped_ptr<QuicPacket> packet( |
[email protected] | 3e60db8 | 2013-08-05 19:43:06 | [diff] [blame] | 149 | framer.BuildUnsizedDataPacket(header, frames).packet); |
[email protected] | 8ba8121 | 2013-05-03 13:11:48 | [diff] [blame] | 150 | return scoped_ptr<QuicEncryptedPacket>(framer.EncryptPacket( |
| 151 | ENCRYPTION_NONE, header.packet_sequence_number, *packet)); |
[email protected] | e13201d8 | 2012-12-12 05:00:32 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | MockHostResolver host_resolver_; |
[email protected] | 0edce6a | 2013-05-08 18:02:40 | [diff] [blame] | 155 | DeterministicMockClientSocketFactory socket_factory_; |
[email protected] | e8ff2684 | 2013-03-22 21:02:05 | [diff] [blame] | 156 | MockCryptoClientStreamFactory crypto_client_stream_factory_; |
[email protected] | 9558c5d3 | 2012-12-22 00:08:14 | [diff] [blame] | 157 | MockRandom random_generator_; |
[email protected] | 872edd9e | 2013-01-16 08:51:15 | [diff] [blame] | 158 | MockClock* clock_; // Owned by factory_. |
[email protected] | e13201d8 | 2012-12-12 05:00:32 | [diff] [blame] | 159 | QuicStreamFactory factory_; |
| 160 | HostPortProxyPair host_port_proxy_pair_; |
[email protected] | 6d1b4ed | 2013-07-10 03:57:54 | [diff] [blame] | 161 | bool is_https_; |
[email protected] | ce542d5 | 2013-07-11 01:08:39 | [diff] [blame] | 162 | scoped_ptr<CertVerifier> cert_verifier_; |
[email protected] | e13201d8 | 2012-12-12 05:00:32 | [diff] [blame] | 163 | BoundNetLog net_log_; |
| 164 | TestCompletionCallback callback_; |
[email protected] | e13201d8 | 2012-12-12 05:00:32 | [diff] [blame] | 165 | }; |
| 166 | |
| 167 | TEST_F(QuicStreamFactoryTest, CreateIfSessionExists) { |
| 168 | EXPECT_EQ(NULL, factory_.CreateIfSessionExists(host_port_proxy_pair_, |
| 169 | net_log_).get()); |
| 170 | } |
| 171 | |
| 172 | TEST_F(QuicStreamFactoryTest, Create) { |
[email protected] | 69dfd1b | 2013-06-04 22:20:12 | [diff] [blame] | 173 | MockRead reads[] = { |
[email protected] | 25c31dc | 2013-06-05 17:56:04 | [diff] [blame] | 174 | MockRead(ASYNC, OK, 0) // EOF |
[email protected] | 69dfd1b | 2013-06-04 22:20:12 | [diff] [blame] | 175 | }; |
[email protected] | 25c31dc | 2013-06-05 17:56:04 | [diff] [blame] | 176 | DeterministicSocketData socket_data(reads, arraysize(reads), NULL, 0); |
[email protected] | e13201d8 | 2012-12-12 05:00:32 | [diff] [blame] | 177 | socket_factory_.AddSocketDataProvider(&socket_data); |
[email protected] | 25c31dc | 2013-06-05 17:56:04 | [diff] [blame] | 178 | socket_data.StopAfter(1); |
[email protected] | e13201d8 | 2012-12-12 05:00:32 | [diff] [blame] | 179 | |
| 180 | QuicStreamRequest request(&factory_); |
[email protected] | 6d1b4ed | 2013-07-10 03:57:54 | [diff] [blame] | 181 | EXPECT_EQ(ERR_IO_PENDING, request.Request(host_port_proxy_pair_, is_https_, |
[email protected] | ce542d5 | 2013-07-11 01:08:39 | [diff] [blame] | 182 | cert_verifier_.get(), net_log_, |
[email protected] | e13201d8 | 2012-12-12 05:00:32 | [diff] [blame] | 183 | callback_.callback())); |
| 184 | |
| 185 | EXPECT_EQ(OK, callback_.WaitForResult()); |
| 186 | scoped_ptr<QuicHttpStream> stream = request.ReleaseStream(); |
[email protected] | 0edce6a | 2013-05-08 18:02:40 | [diff] [blame] | 187 | EXPECT_TRUE(stream.get()); |
[email protected] | e13201d8 | 2012-12-12 05:00:32 | [diff] [blame] | 188 | |
| 189 | // Will reset stream 3. |
| 190 | stream = factory_.CreateIfSessionExists(host_port_proxy_pair_, net_log_); |
| 191 | EXPECT_TRUE(stream.get()); |
| 192 | |
[email protected] | 6d1b4ed | 2013-07-10 03:57:54 | [diff] [blame] | 193 | // TODO(rtenneti): We should probably have a tests that HTTP and HTTPS result |
| 194 | // in streams on different sessions. |
[email protected] | e13201d8 | 2012-12-12 05:00:32 | [diff] [blame] | 195 | QuicStreamRequest request2(&factory_); |
[email protected] | 6d1b4ed | 2013-07-10 03:57:54 | [diff] [blame] | 196 | EXPECT_EQ(OK, request2.Request(host_port_proxy_pair_, is_https_, |
[email protected] | ce542d5 | 2013-07-11 01:08:39 | [diff] [blame] | 197 | cert_verifier_.get(), net_log_, |
[email protected] | e13201d8 | 2012-12-12 05:00:32 | [diff] [blame] | 198 | callback_.callback())); |
| 199 | stream = request2.ReleaseStream(); // Will reset stream 5. |
| 200 | stream.reset(); // Will reset stream 7. |
| 201 | |
| 202 | EXPECT_TRUE(socket_data.at_read_eof()); |
| 203 | EXPECT_TRUE(socket_data.at_write_eof()); |
| 204 | } |
| 205 | |
[email protected] | 4d283b3 | 2013-10-17 12:57:27 | [diff] [blame] | 206 | TEST_F(QuicStreamFactoryTest, Goaway) { |
| 207 | MockRead reads[] = { |
| 208 | MockRead(ASYNC, OK, 0) // EOF |
| 209 | }; |
| 210 | DeterministicSocketData socket_data(reads, arraysize(reads), NULL, 0); |
| 211 | socket_data.StopAfter(1); |
| 212 | socket_factory_.AddSocketDataProvider(&socket_data); |
| 213 | DeterministicSocketData socket_data2(reads, arraysize(reads), NULL, 0); |
| 214 | socket_data2.StopAfter(1); |
| 215 | socket_factory_.AddSocketDataProvider(&socket_data2); |
| 216 | |
| 217 | QuicStreamRequest request(&factory_); |
| 218 | EXPECT_EQ(ERR_IO_PENDING, request.Request(host_port_proxy_pair_, is_https_, |
| 219 | cert_verifier_.get(), net_log_, |
| 220 | callback_.callback())); |
| 221 | |
| 222 | EXPECT_EQ(OK, callback_.WaitForResult()); |
| 223 | scoped_ptr<QuicHttpStream> stream = request.ReleaseStream(); |
| 224 | EXPECT_TRUE(stream.get()); |
| 225 | |
| 226 | // Mark the session as going away. Ensure that while it is still alive |
| 227 | // that it is no longer active. |
| 228 | QuicClientSession* session = QuicStreamFactoryPeer::GetActiveSession( |
| 229 | &factory_, host_port_proxy_pair_); |
| 230 | factory_.OnSessionGoingAway(session); |
| 231 | EXPECT_EQ(true, QuicStreamFactoryPeer::IsLiveSession(&factory_, session)); |
| 232 | EXPECT_FALSE(QuicStreamFactoryPeer::HasActiveSession(&factory_, |
| 233 | host_port_proxy_pair_)); |
| 234 | EXPECT_EQ(NULL, factory_.CreateIfSessionExists(host_port_proxy_pair_, |
| 235 | net_log_).get()); |
| 236 | |
| 237 | // Create a new request for the same destination and verify that a |
| 238 | // new session is created. |
| 239 | QuicStreamRequest request2(&factory_); |
| 240 | EXPECT_EQ(ERR_IO_PENDING, request2.Request(host_port_proxy_pair_, is_https_, |
| 241 | cert_verifier_.get(), net_log_, |
| 242 | callback_.callback())); |
| 243 | EXPECT_EQ(OK, callback_.WaitForResult()); |
| 244 | scoped_ptr<QuicHttpStream> stream2 = request2.ReleaseStream(); |
| 245 | EXPECT_TRUE(stream2.get()); |
| 246 | |
| 247 | EXPECT_TRUE(QuicStreamFactoryPeer::HasActiveSession(&factory_, |
| 248 | host_port_proxy_pair_)); |
| 249 | EXPECT_NE(session, |
| 250 | QuicStreamFactoryPeer::GetActiveSession( |
| 251 | &factory_, host_port_proxy_pair_)); |
| 252 | EXPECT_EQ(true, QuicStreamFactoryPeer::IsLiveSession(&factory_, session)); |
| 253 | |
| 254 | stream2.reset(); |
| 255 | stream.reset(); |
| 256 | |
| 257 | EXPECT_TRUE(socket_data.at_read_eof()); |
| 258 | EXPECT_TRUE(socket_data.at_write_eof()); |
| 259 | } |
| 260 | |
[email protected] | 0b2294d3 | 2013-08-02 00:46:36 | [diff] [blame] | 261 | TEST_F(QuicStreamFactoryTest, MaxOpenStream) { |
| 262 | MockRead reads[] = { |
| 263 | MockRead(ASYNC, OK, 0) // EOF |
| 264 | }; |
[email protected] | 06ff515 | 2013-08-29 01:03:05 | [diff] [blame] | 265 | scoped_ptr<QuicEncryptedPacket> rst(ConstructRstPacket(1, 3)); |
| 266 | MockWrite writes[] = { |
| 267 | MockWrite(ASYNC, rst->data(), rst->length(), 1), |
| 268 | }; |
| 269 | DeterministicSocketData socket_data(reads, arraysize(reads), |
| 270 | writes, arraysize(writes)); |
[email protected] | 0b2294d3 | 2013-08-02 00:46:36 | [diff] [blame] | 271 | socket_factory_.AddSocketDataProvider(&socket_data); |
| 272 | socket_data.StopAfter(1); |
| 273 | |
| 274 | HttpRequestInfo request_info; |
| 275 | std::vector<QuicHttpStream*> streams; |
| 276 | // The MockCryptoClientStream sets max_open_streams to be |
| 277 | // 2 * kDefaultMaxStreamsPerConnection. |
| 278 | for (size_t i = 0; i < 2 * kDefaultMaxStreamsPerConnection; i++) { |
| 279 | QuicStreamRequest request(&factory_); |
| 280 | int rv = request.Request(host_port_proxy_pair_, is_https_, |
| 281 | cert_verifier_.get(), net_log_, |
| 282 | callback_.callback()); |
| 283 | if (i == 0) { |
| 284 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 285 | EXPECT_EQ(OK, callback_.WaitForResult()); |
| 286 | } else { |
| 287 | EXPECT_EQ(OK, rv); |
| 288 | } |
| 289 | scoped_ptr<QuicHttpStream> stream = request.ReleaseStream(); |
| 290 | EXPECT_TRUE(stream); |
| 291 | EXPECT_EQ(OK, stream->InitializeStream( |
| 292 | &request_info, DEFAULT_PRIORITY, net_log_, CompletionCallback())); |
| 293 | streams.push_back(stream.release()); |
| 294 | } |
| 295 | |
| 296 | QuicStreamRequest request(&factory_); |
| 297 | EXPECT_EQ(OK, request.Request(host_port_proxy_pair_, is_https_, |
| 298 | cert_verifier_.get(), net_log_, |
| 299 | CompletionCallback())); |
| 300 | scoped_ptr<QuicHttpStream> stream = request.ReleaseStream(); |
| 301 | EXPECT_TRUE(stream); |
| 302 | EXPECT_EQ(ERR_IO_PENDING, stream->InitializeStream( |
| 303 | &request_info, DEFAULT_PRIORITY, net_log_, callback_.callback())); |
| 304 | |
| 305 | // Close the first stream. |
| 306 | streams.front()->Close(false); |
| 307 | |
| 308 | ASSERT_TRUE(callback_.have_result()); |
| 309 | |
| 310 | EXPECT_EQ(OK, callback_.WaitForResult()); |
| 311 | |
| 312 | EXPECT_TRUE(socket_data.at_read_eof()); |
| 313 | EXPECT_TRUE(socket_data.at_write_eof()); |
| 314 | STLDeleteElements(&streams); |
| 315 | } |
| 316 | |
[email protected] | e13201d8 | 2012-12-12 05:00:32 | [diff] [blame] | 317 | TEST_F(QuicStreamFactoryTest, CreateError) { |
[email protected] | 0edce6a | 2013-05-08 18:02:40 | [diff] [blame] | 318 | DeterministicSocketData socket_data(NULL, 0, NULL, 0); |
[email protected] | e13201d8 | 2012-12-12 05:00:32 | [diff] [blame] | 319 | socket_factory_.AddSocketDataProvider(&socket_data); |
| 320 | |
| 321 | host_resolver_.rules()->AddSimulatedFailure("www.google.com"); |
| 322 | |
| 323 | QuicStreamRequest request(&factory_); |
[email protected] | 6d1b4ed | 2013-07-10 03:57:54 | [diff] [blame] | 324 | EXPECT_EQ(ERR_IO_PENDING, request.Request(host_port_proxy_pair_, is_https_, |
[email protected] | ce542d5 | 2013-07-11 01:08:39 | [diff] [blame] | 325 | cert_verifier_.get(), net_log_, |
[email protected] | e13201d8 | 2012-12-12 05:00:32 | [diff] [blame] | 326 | callback_.callback())); |
| 327 | |
| 328 | EXPECT_EQ(ERR_NAME_NOT_RESOLVED, callback_.WaitForResult()); |
| 329 | |
| 330 | EXPECT_TRUE(socket_data.at_read_eof()); |
| 331 | EXPECT_TRUE(socket_data.at_write_eof()); |
| 332 | } |
| 333 | |
| 334 | TEST_F(QuicStreamFactoryTest, CancelCreate) { |
[email protected] | 69dfd1b | 2013-06-04 22:20:12 | [diff] [blame] | 335 | MockRead reads[] = { |
[email protected] | 25c31dc | 2013-06-05 17:56:04 | [diff] [blame] | 336 | MockRead(ASYNC, OK, 0) // EOF |
[email protected] | 69dfd1b | 2013-06-04 22:20:12 | [diff] [blame] | 337 | }; |
[email protected] | 25c31dc | 2013-06-05 17:56:04 | [diff] [blame] | 338 | DeterministicSocketData socket_data(reads, arraysize(reads), NULL, 0); |
[email protected] | e13201d8 | 2012-12-12 05:00:32 | [diff] [blame] | 339 | socket_factory_.AddSocketDataProvider(&socket_data); |
| 340 | { |
| 341 | QuicStreamRequest request(&factory_); |
[email protected] | 6d1b4ed | 2013-07-10 03:57:54 | [diff] [blame] | 342 | EXPECT_EQ(ERR_IO_PENDING, request.Request(host_port_proxy_pair_, is_https_, |
[email protected] | ce542d5 | 2013-07-11 01:08:39 | [diff] [blame] | 343 | cert_verifier_.get(), net_log_, |
[email protected] | e13201d8 | 2012-12-12 05:00:32 | [diff] [blame] | 344 | callback_.callback())); |
| 345 | } |
| 346 | |
[email protected] | 25c31dc | 2013-06-05 17:56:04 | [diff] [blame] | 347 | socket_data.StopAfter(1); |
[email protected] | e13201d8 | 2012-12-12 05:00:32 | [diff] [blame] | 348 | base::RunLoop run_loop; |
| 349 | run_loop.RunUntilIdle(); |
| 350 | |
| 351 | scoped_ptr<QuicHttpStream> stream( |
| 352 | factory_.CreateIfSessionExists(host_port_proxy_pair_, net_log_)); |
| 353 | EXPECT_TRUE(stream.get()); |
| 354 | stream.reset(); |
| 355 | |
| 356 | EXPECT_TRUE(socket_data.at_read_eof()); |
| 357 | EXPECT_TRUE(socket_data.at_write_eof()); |
| 358 | } |
| 359 | |
[email protected] | 56dfb90 | 2013-01-03 23:17:55 | [diff] [blame] | 360 | TEST_F(QuicStreamFactoryTest, CloseAllSessions) { |
[email protected] | 56dfb90 | 2013-01-03 23:17:55 | [diff] [blame] | 361 | MockRead reads[] = { |
[email protected] | 0edce6a | 2013-05-08 18:02:40 | [diff] [blame] | 362 | MockRead(ASYNC, 0, 0) // EOF |
[email protected] | 56dfb90 | 2013-01-03 23:17:55 | [diff] [blame] | 363 | }; |
[email protected] | 25c31dc | 2013-06-05 17:56:04 | [diff] [blame] | 364 | DeterministicSocketData socket_data(reads, arraysize(reads), NULL, 0); |
[email protected] | 56dfb90 | 2013-01-03 23:17:55 | [diff] [blame] | 365 | socket_factory_.AddSocketDataProvider(&socket_data); |
[email protected] | 0edce6a | 2013-05-08 18:02:40 | [diff] [blame] | 366 | socket_data.StopAfter(1); |
[email protected] | 56dfb90 | 2013-01-03 23:17:55 | [diff] [blame] | 367 | |
[email protected] | 69dfd1b | 2013-06-04 22:20:12 | [diff] [blame] | 368 | MockRead reads2[] = { |
[email protected] | 25c31dc | 2013-06-05 17:56:04 | [diff] [blame] | 369 | MockRead(ASYNC, 0, 0) // EOF |
[email protected] | 69dfd1b | 2013-06-04 22:20:12 | [diff] [blame] | 370 | }; |
[email protected] | 25c31dc | 2013-06-05 17:56:04 | [diff] [blame] | 371 | DeterministicSocketData socket_data2(reads2, arraysize(reads2), NULL, 0); |
[email protected] | 56dfb90 | 2013-01-03 23:17:55 | [diff] [blame] | 372 | socket_factory_.AddSocketDataProvider(&socket_data2); |
[email protected] | 0edce6a | 2013-05-08 18:02:40 | [diff] [blame] | 373 | socket_data2.StopAfter(1); |
[email protected] | 56dfb90 | 2013-01-03 23:17:55 | [diff] [blame] | 374 | |
| 375 | QuicStreamRequest request(&factory_); |
[email protected] | 6d1b4ed | 2013-07-10 03:57:54 | [diff] [blame] | 376 | EXPECT_EQ(ERR_IO_PENDING, request.Request(host_port_proxy_pair_, is_https_, |
[email protected] | ce542d5 | 2013-07-11 01:08:39 | [diff] [blame] | 377 | cert_verifier_.get(), net_log_, |
[email protected] | 56dfb90 | 2013-01-03 23:17:55 | [diff] [blame] | 378 | callback_.callback())); |
| 379 | |
| 380 | EXPECT_EQ(OK, callback_.WaitForResult()); |
| 381 | scoped_ptr<QuicHttpStream> stream = request.ReleaseStream(); |
[email protected] | 0b2294d3 | 2013-08-02 00:46:36 | [diff] [blame] | 382 | HttpRequestInfo request_info; |
| 383 | EXPECT_EQ(OK, stream->InitializeStream(&request_info, |
| 384 | DEFAULT_PRIORITY, |
| 385 | net_log_, CompletionCallback())); |
[email protected] | 56dfb90 | 2013-01-03 23:17:55 | [diff] [blame] | 386 | |
| 387 | // Close the session and verify that stream saw the error. |
| 388 | factory_.CloseAllSessions(ERR_INTERNET_DISCONNECTED); |
| 389 | EXPECT_EQ(ERR_INTERNET_DISCONNECTED, |
| 390 | stream->ReadResponseHeaders(callback_.callback())); |
| 391 | |
| 392 | // Now attempting to request a stream to the same origin should create |
| 393 | // a new session. |
| 394 | |
| 395 | QuicStreamRequest request2(&factory_); |
[email protected] | 6d1b4ed | 2013-07-10 03:57:54 | [diff] [blame] | 396 | EXPECT_EQ(ERR_IO_PENDING, request2.Request(host_port_proxy_pair_, is_https_, |
[email protected] | ce542d5 | 2013-07-11 01:08:39 | [diff] [blame] | 397 | cert_verifier_.get(), net_log_, |
[email protected] | 56dfb90 | 2013-01-03 23:17:55 | [diff] [blame] | 398 | callback_.callback())); |
| 399 | |
| 400 | EXPECT_EQ(OK, callback_.WaitForResult()); |
| 401 | stream = request2.ReleaseStream(); |
| 402 | stream.reset(); // Will reset stream 3. |
| 403 | |
| 404 | EXPECT_TRUE(socket_data.at_read_eof()); |
| 405 | EXPECT_TRUE(socket_data.at_write_eof()); |
| 406 | EXPECT_TRUE(socket_data2.at_read_eof()); |
| 407 | EXPECT_TRUE(socket_data2.at_write_eof()); |
| 408 | } |
| 409 | |
[email protected] | f698a01 | 2013-05-06 20:18:59 | [diff] [blame] | 410 | TEST_F(QuicStreamFactoryTest, OnIPAddressChanged) { |
[email protected] | f698a01 | 2013-05-06 20:18:59 | [diff] [blame] | 411 | MockRead reads[] = { |
[email protected] | 0edce6a | 2013-05-08 18:02:40 | [diff] [blame] | 412 | MockRead(ASYNC, 0, 0) // EOF |
[email protected] | f698a01 | 2013-05-06 20:18:59 | [diff] [blame] | 413 | }; |
[email protected] | 25c31dc | 2013-06-05 17:56:04 | [diff] [blame] | 414 | DeterministicSocketData socket_data(reads, arraysize(reads), NULL, 0); |
[email protected] | f698a01 | 2013-05-06 20:18:59 | [diff] [blame] | 415 | socket_factory_.AddSocketDataProvider(&socket_data); |
[email protected] | 0edce6a | 2013-05-08 18:02:40 | [diff] [blame] | 416 | socket_data.StopAfter(1); |
[email protected] | f698a01 | 2013-05-06 20:18:59 | [diff] [blame] | 417 | |
[email protected] | 69dfd1b | 2013-06-04 22:20:12 | [diff] [blame] | 418 | MockRead reads2[] = { |
[email protected] | 25c31dc | 2013-06-05 17:56:04 | [diff] [blame] | 419 | MockRead(ASYNC, 0, 0) // EOF |
[email protected] | 69dfd1b | 2013-06-04 22:20:12 | [diff] [blame] | 420 | }; |
[email protected] | 25c31dc | 2013-06-05 17:56:04 | [diff] [blame] | 421 | DeterministicSocketData socket_data2(reads2, arraysize(reads2), NULL, 0); |
[email protected] | f698a01 | 2013-05-06 20:18:59 | [diff] [blame] | 422 | socket_factory_.AddSocketDataProvider(&socket_data2); |
[email protected] | 0edce6a | 2013-05-08 18:02:40 | [diff] [blame] | 423 | socket_data2.StopAfter(1); |
[email protected] | f698a01 | 2013-05-06 20:18:59 | [diff] [blame] | 424 | |
| 425 | QuicStreamRequest request(&factory_); |
[email protected] | 6d1b4ed | 2013-07-10 03:57:54 | [diff] [blame] | 426 | EXPECT_EQ(ERR_IO_PENDING, request.Request(host_port_proxy_pair_, is_https_, |
[email protected] | ce542d5 | 2013-07-11 01:08:39 | [diff] [blame] | 427 | cert_verifier_.get(), net_log_, |
[email protected] | f698a01 | 2013-05-06 20:18:59 | [diff] [blame] | 428 | callback_.callback())); |
| 429 | |
| 430 | EXPECT_EQ(OK, callback_.WaitForResult()); |
| 431 | scoped_ptr<QuicHttpStream> stream = request.ReleaseStream(); |
[email protected] | 0b2294d3 | 2013-08-02 00:46:36 | [diff] [blame] | 432 | HttpRequestInfo request_info; |
| 433 | EXPECT_EQ(OK, stream->InitializeStream(&request_info, |
| 434 | DEFAULT_PRIORITY, |
| 435 | net_log_, CompletionCallback())); |
[email protected] | f698a01 | 2013-05-06 20:18:59 | [diff] [blame] | 436 | |
| 437 | // Change the IP address and verify that stream saw the error. |
| 438 | factory_.OnIPAddressChanged(); |
| 439 | EXPECT_EQ(ERR_NETWORK_CHANGED, |
| 440 | stream->ReadResponseHeaders(callback_.callback())); |
[email protected] | 11c0587 | 2013-08-20 02:04:12 | [diff] [blame] | 441 | EXPECT_TRUE(factory_.require_confirmation()); |
[email protected] | f698a01 | 2013-05-06 20:18:59 | [diff] [blame] | 442 | |
| 443 | // Now attempting to request a stream to the same origin should create |
| 444 | // a new session. |
| 445 | |
| 446 | QuicStreamRequest request2(&factory_); |
[email protected] | 6d1b4ed | 2013-07-10 03:57:54 | [diff] [blame] | 447 | EXPECT_EQ(ERR_IO_PENDING, request2.Request(host_port_proxy_pair_, is_https_, |
[email protected] | ce542d5 | 2013-07-11 01:08:39 | [diff] [blame] | 448 | cert_verifier_.get(), net_log_, |
[email protected] | f698a01 | 2013-05-06 20:18:59 | [diff] [blame] | 449 | callback_.callback())); |
| 450 | |
| 451 | EXPECT_EQ(OK, callback_.WaitForResult()); |
| 452 | stream = request2.ReleaseStream(); |
| 453 | stream.reset(); // Will reset stream 3. |
| 454 | |
| 455 | EXPECT_TRUE(socket_data.at_read_eof()); |
| 456 | EXPECT_TRUE(socket_data.at_write_eof()); |
| 457 | EXPECT_TRUE(socket_data2.at_read_eof()); |
| 458 | EXPECT_TRUE(socket_data2.at_write_eof()); |
| 459 | } |
| 460 | |
[email protected] | b70fdb79 | 2013-10-25 19:04:14 | [diff] [blame^] | 461 | TEST_F(QuicStreamFactoryTest, SharedCryptoConfig) { |
[email protected] | c49ff18 | 2013-09-28 08:33:26 | [diff] [blame] | 462 | HostPortProxyPair host_port_proxy_pair1(HostPortPair("r1.c.youtube.com", 80), |
| 463 | ProxyServer::Direct()); |
| 464 | |
| 465 | QuicCryptoClientConfig* crypto_config1 = |
| 466 | QuicStreamFactoryPeer::GetOrCreateCryptoConfig(&factory_, |
| 467 | host_port_proxy_pair1); |
| 468 | DCHECK(crypto_config1); |
| 469 | QuicCryptoClientConfig::CachedState* cached1 = |
| 470 | crypto_config1->LookupOrCreate(host_port_proxy_pair1.first.host()); |
[email protected] | b70fdb79 | 2013-10-25 19:04:14 | [diff] [blame^] | 471 | EXPECT_FALSE(cached1->proof_valid()); |
| 472 | EXPECT_TRUE(cached1->source_address_token().empty()); |
| 473 | |
[email protected] | c49ff18 | 2013-09-28 08:33:26 | [diff] [blame] | 474 | // Mutate the cached1 to have different data. |
| 475 | // TODO(rtenneti): mutate other members of CachedState. |
| 476 | cached1->set_source_address_token("c.youtube.com"); |
[email protected] | b70fdb79 | 2013-10-25 19:04:14 | [diff] [blame^] | 477 | cached1->SetProofValid(); |
[email protected] | c49ff18 | 2013-09-28 08:33:26 | [diff] [blame] | 478 | |
| 479 | HostPortProxyPair host_port_proxy_pair2(HostPortPair("r2.c.youtube.com", 80), |
| 480 | ProxyServer::Direct()); |
| 481 | QuicCryptoClientConfig* crypto_config2 = |
| 482 | QuicStreamFactoryPeer::GetOrCreateCryptoConfig(&factory_, |
| 483 | host_port_proxy_pair2); |
| 484 | DCHECK(crypto_config2); |
| 485 | QuicCryptoClientConfig::CachedState* cached2 = |
| 486 | crypto_config2->LookupOrCreate(host_port_proxy_pair2.first.host()); |
| 487 | EXPECT_EQ(cached1->source_address_token(), cached2->source_address_token()); |
[email protected] | b70fdb79 | 2013-10-25 19:04:14 | [diff] [blame^] | 488 | EXPECT_TRUE(cached2->proof_valid()); |
| 489 | } |
| 490 | |
| 491 | TEST_F(QuicStreamFactoryTest, CryptoConfigWhenProofIsInvalid) { |
| 492 | HostPortProxyPair host_port_proxy_pair1(HostPortPair("r1.c.youtube.com", 80), |
| 493 | ProxyServer::Direct()); |
| 494 | |
| 495 | QuicCryptoClientConfig* crypto_config1 = |
| 496 | QuicStreamFactoryPeer::GetOrCreateCryptoConfig(&factory_, |
| 497 | host_port_proxy_pair1); |
| 498 | DCHECK(crypto_config1); |
| 499 | QuicCryptoClientConfig::CachedState* cached1 = |
| 500 | crypto_config1->LookupOrCreate(host_port_proxy_pair1.first.host()); |
| 501 | EXPECT_FALSE(cached1->proof_valid()); |
| 502 | EXPECT_TRUE(cached1->source_address_token().empty()); |
| 503 | |
| 504 | // Mutate the cached1 to have different data. |
| 505 | // TODO(rtenneti): mutate other members of CachedState. |
| 506 | cached1->set_source_address_token("c.youtube.com"); |
| 507 | cached1->SetProofInvalid(); |
| 508 | |
| 509 | HostPortProxyPair host_port_proxy_pair2(HostPortPair("r2.c.youtube.com", 80), |
| 510 | ProxyServer::Direct()); |
| 511 | QuicCryptoClientConfig* crypto_config2 = |
| 512 | QuicStreamFactoryPeer::GetOrCreateCryptoConfig(&factory_, |
| 513 | host_port_proxy_pair2); |
| 514 | DCHECK(crypto_config2); |
| 515 | QuicCryptoClientConfig::CachedState* cached2 = |
| 516 | crypto_config2->LookupOrCreate(host_port_proxy_pair2.first.host()); |
| 517 | EXPECT_NE(cached1->source_address_token(), cached2->source_address_token()); |
| 518 | EXPECT_TRUE(cached2->source_address_token().empty()); |
| 519 | EXPECT_FALSE(cached2->proof_valid()); |
[email protected] | c49ff18 | 2013-09-28 08:33:26 | [diff] [blame] | 520 | } |
| 521 | |
[email protected] | e13201d8 | 2012-12-12 05:00:32 | [diff] [blame] | 522 | } // namespace test |
[email protected] | e13201d8 | 2012-12-12 05:00:32 | [diff] [blame] | 523 | } // namespace net |