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