[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] | eed749f9 | 2013-12-23 18:57:38 | [diff] [blame] | 9 | #include "net/base/test_data_directory.h" |
[email protected] | 6d1b4ed | 2013-07-10 03:57:54 | [diff] [blame] | 10 | #include "net/cert/cert_verifier.h" |
[email protected] | f2cb3cf | 2013-03-21 01:40:53 | [diff] [blame] | 11 | #include "net/dns/mock_host_resolver.h" |
[email protected] | e13201d8 | 2012-12-12 05:00:32 | [diff] [blame] | 12 | #include "net/http/http_response_headers.h" |
| 13 | #include "net/http/http_response_info.h" |
| 14 | #include "net/http/http_util.h" |
[email protected] | c49ff18 | 2013-09-28 08:33:26 | [diff] [blame] | 15 | #include "net/quic/crypto/crypto_handshake.h" |
[email protected] | 4df6984 | 2013-02-27 06:32:16 | [diff] [blame] | 16 | #include "net/quic/crypto/quic_decrypter.h" |
| 17 | #include "net/quic/crypto/quic_encrypter.h" |
[email protected] | e13201d8 | 2012-12-12 05:00:32 | [diff] [blame] | 18 | #include "net/quic/quic_http_stream.h" |
| 19 | #include "net/quic/test_tools/mock_clock.h" |
[email protected] | e8ff2684 | 2013-03-22 21:02:05 | [diff] [blame] | 20 | #include "net/quic/test_tools/mock_crypto_client_stream_factory.h" |
[email protected] | 9558c5d3 | 2012-12-22 00:08:14 | [diff] [blame] | 21 | #include "net/quic/test_tools/mock_random.h" |
[email protected] | 1e96003 | 2013-12-20 19:00:20 | [diff] [blame] | 22 | #include "net/quic/test_tools/quic_test_packet_maker.h" |
[email protected] | e13201d8 | 2012-12-12 05:00:32 | [diff] [blame] | 23 | #include "net/quic/test_tools/quic_test_utils.h" |
| 24 | #include "net/socket/socket_test_util.h" |
[email protected] | eed749f9 | 2013-12-23 18:57:38 | [diff] [blame] | 25 | #include "net/test/cert_test_util.h" |
[email protected] | e13201d8 | 2012-12-12 05:00:32 | [diff] [blame] | 26 | #include "testing/gtest/include/gtest/gtest.h" |
| 27 | |
[email protected] | 6e12d70 | 2013-11-13 00:17:17 | [diff] [blame] | 28 | using base::StringPiece; |
| 29 | using std::string; |
| 30 | using std::vector; |
| 31 | |
[email protected] | e13201d8 | 2012-12-12 05:00:32 | [diff] [blame] | 32 | namespace net { |
[email protected] | e13201d8 | 2012-12-12 05:00:32 | [diff] [blame] | 33 | namespace test { |
| 34 | |
[email protected] | 3c77240 | 2013-12-18 21:38:11 | [diff] [blame] | 35 | namespace { |
| 36 | const char kDefaultServerHostName[] = "www.google.com"; |
| 37 | const int kDefaultServerPort = 443; |
| 38 | } // namespace anonymous |
| 39 | |
[email protected] | c49ff18 | 2013-09-28 08:33:26 | [diff] [blame] | 40 | class QuicStreamFactoryPeer { |
| 41 | public: |
| 42 | static QuicCryptoClientConfig* GetOrCreateCryptoConfig( |
| 43 | QuicStreamFactory* factory, |
| 44 | const HostPortProxyPair& host_port_proxy_pair) { |
| 45 | return factory->GetOrCreateCryptoConfig(host_port_proxy_pair); |
| 46 | } |
[email protected] | 4d283b3 | 2013-10-17 12:57:27 | [diff] [blame] | 47 | |
| 48 | static bool HasActiveSession(QuicStreamFactory* factory, |
| 49 | const HostPortProxyPair& host_port_proxy_pair) { |
| 50 | return factory->HasActiveSession(host_port_proxy_pair); |
| 51 | } |
| 52 | |
| 53 | static QuicClientSession* GetActiveSession( |
| 54 | QuicStreamFactory* factory, |
| 55 | const HostPortProxyPair& host_port_proxy_pair) { |
| 56 | DCHECK(factory->HasActiveSession(host_port_proxy_pair)); |
| 57 | return factory->active_sessions_[host_port_proxy_pair]; |
| 58 | } |
| 59 | |
| 60 | static bool IsLiveSession(QuicStreamFactory* factory, |
| 61 | QuicClientSession* session) { |
| 62 | for (QuicStreamFactory::SessionSet::iterator it = |
| 63 | factory->all_sessions_.begin(); |
| 64 | it != factory->all_sessions_.end(); ++it) { |
| 65 | if (*it == session) |
| 66 | return true; |
| 67 | } |
| 68 | return false; |
| 69 | } |
[email protected] | c49ff18 | 2013-09-28 08:33:26 | [diff] [blame] | 70 | }; |
| 71 | |
[email protected] | 1e96003 | 2013-12-20 19:00:20 | [diff] [blame] | 72 | class QuicStreamFactoryTest : public ::testing::TestWithParam<QuicVersion> { |
[email protected] | e13201d8 | 2012-12-12 05:00:32 | [diff] [blame] | 73 | protected: |
| 74 | QuicStreamFactoryTest() |
[email protected] | 457d695 | 2013-12-13 09:24:58 | [diff] [blame] | 75 | : random_generator_(0), |
[email protected] | 1e96003 | 2013-12-20 19:00:20 | [diff] [blame] | 76 | maker_(GetParam(), 0), |
[email protected] | 457d695 | 2013-12-13 09:24:58 | [diff] [blame] | 77 | clock_(new MockClock()), |
[email protected] | 872edd9e | 2013-01-16 08:51:15 | [diff] [blame] | 78 | factory_(&host_resolver_, &socket_factory_, |
[email protected] | 77c6c16 | 2013-08-17 02:57:45 | [diff] [blame] | 79 | base::WeakPtr<HttpServerProperties>(), |
[email protected] | e8ff2684 | 2013-03-22 21:02:05 | [diff] [blame] | 80 | &crypto_client_stream_factory_, |
[email protected] | 1e96003 | 2013-12-20 19:00:20 | [diff] [blame] | 81 | &random_generator_, clock_, kDefaultMaxPacketSize, |
[email protected] | c80f7c9 | 2014-02-27 13:12:02 | [diff] [blame] | 82 | SupportedVersions(GetParam()), true, true), |
[email protected] | 3c77240 | 2013-12-18 21:38:11 | [diff] [blame] | 83 | host_port_proxy_pair_(HostPortPair(kDefaultServerHostName, |
| 84 | kDefaultServerPort), |
[email protected] | 6d1b4ed | 2013-07-10 03:57:54 | [diff] [blame] | 85 | ProxyServer::Direct()), |
| 86 | is_https_(false), |
| 87 | cert_verifier_(CertVerifier::CreateDefault()) { |
[email protected] | 11c0587 | 2013-08-20 02:04:12 | [diff] [blame] | 88 | factory_.set_require_confirmation(false); |
[email protected] | e13201d8 | 2012-12-12 05:00:32 | [diff] [blame] | 89 | } |
| 90 | |
[email protected] | e13201d8 | 2012-12-12 05:00:32 | [diff] [blame] | 91 | |
[email protected] | 3c77240 | 2013-12-18 21:38:11 | [diff] [blame] | 92 | int GetSourcePortForNewSession(const HostPortProxyPair& destination) { |
| 93 | // Should only be called if there is no active session for this destination. |
| 94 | EXPECT_EQ(NULL, factory_.CreateIfSessionExists(destination, |
| 95 | net_log_).get()); |
| 96 | size_t socket_count = socket_factory_.udp_client_sockets().size(); |
| 97 | |
| 98 | MockRead reads[] = { |
| 99 | MockRead(ASYNC, OK, 0) // EOF |
| 100 | }; |
| 101 | DeterministicSocketData socket_data(reads, arraysize(reads), NULL, 0); |
| 102 | socket_data.StopAfter(1); |
| 103 | socket_factory_.AddSocketDataProvider(&socket_data); |
| 104 | |
| 105 | QuicStreamRequest request(&factory_); |
[email protected] | 974849d | 2014-02-06 01:32:59 | [diff] [blame] | 106 | EXPECT_EQ(ERR_IO_PENDING, |
| 107 | request.Request(destination, |
| 108 | is_https_, |
| 109 | "GET", |
| 110 | cert_verifier_.get(), |
| 111 | net_log_, |
| 112 | callback_.callback())); |
[email protected] | 3c77240 | 2013-12-18 21:38:11 | [diff] [blame] | 113 | |
| 114 | EXPECT_EQ(OK, callback_.WaitForResult()); |
| 115 | scoped_ptr<QuicHttpStream> stream = request.ReleaseStream(); |
| 116 | EXPECT_TRUE(stream.get()); |
| 117 | stream.reset(); |
| 118 | |
| 119 | QuicClientSession* session = QuicStreamFactoryPeer::GetActiveSession( |
| 120 | &factory_, destination); |
| 121 | |
| 122 | if (socket_count + 1 != socket_factory_.udp_client_sockets().size()) { |
| 123 | EXPECT_TRUE(false); |
| 124 | return 0; |
| 125 | } |
| 126 | |
| 127 | IPEndPoint endpoint; |
| 128 | socket_factory_. |
| 129 | udp_client_sockets()[socket_count]->GetLocalAddress(&endpoint); |
| 130 | int port = endpoint.port(); |
| 131 | |
| 132 | factory_.OnSessionClosed(session); |
| 133 | EXPECT_EQ(NULL, factory_.CreateIfSessionExists(destination, |
| 134 | net_log_).get()); |
| 135 | EXPECT_TRUE(socket_data.at_read_eof()); |
| 136 | EXPECT_TRUE(socket_data.at_write_eof()); |
| 137 | return port; |
| 138 | } |
| 139 | |
[email protected] | 459a740 | 2014-02-10 12:58:52 | [diff] [blame] | 140 | scoped_ptr<QuicEncryptedPacket> ConstructRstPacket() { |
[email protected] | 92bf17c | 2014-03-03 21:14:03 | [diff] [blame^] | 141 | QuicStreamId stream_id = 5; |
[email protected] | 459a740 | 2014-02-10 12:58:52 | [diff] [blame] | 142 | return maker_.MakeRstPacket(1, true, stream_id, QUIC_STREAM_NO_ERROR); |
| 143 | } |
| 144 | |
[email protected] | e13201d8 | 2012-12-12 05:00:32 | [diff] [blame] | 145 | MockHostResolver host_resolver_; |
[email protected] | 0edce6a | 2013-05-08 18:02:40 | [diff] [blame] | 146 | DeterministicMockClientSocketFactory socket_factory_; |
[email protected] | e8ff2684 | 2013-03-22 21:02:05 | [diff] [blame] | 147 | MockCryptoClientStreamFactory crypto_client_stream_factory_; |
[email protected] | 9558c5d3 | 2012-12-22 00:08:14 | [diff] [blame] | 148 | MockRandom random_generator_; |
[email protected] | 1e96003 | 2013-12-20 19:00:20 | [diff] [blame] | 149 | QuicTestPacketMaker maker_; |
[email protected] | 872edd9e | 2013-01-16 08:51:15 | [diff] [blame] | 150 | MockClock* clock_; // Owned by factory_. |
[email protected] | e13201d8 | 2012-12-12 05:00:32 | [diff] [blame] | 151 | QuicStreamFactory factory_; |
| 152 | HostPortProxyPair host_port_proxy_pair_; |
[email protected] | 6d1b4ed | 2013-07-10 03:57:54 | [diff] [blame] | 153 | bool is_https_; |
[email protected] | ce542d5 | 2013-07-11 01:08:39 | [diff] [blame] | 154 | scoped_ptr<CertVerifier> cert_verifier_; |
[email protected] | e13201d8 | 2012-12-12 05:00:32 | [diff] [blame] | 155 | BoundNetLog net_log_; |
| 156 | TestCompletionCallback callback_; |
[email protected] | e13201d8 | 2012-12-12 05:00:32 | [diff] [blame] | 157 | }; |
| 158 | |
[email protected] | 1e96003 | 2013-12-20 19:00:20 | [diff] [blame] | 159 | INSTANTIATE_TEST_CASE_P(Version, QuicStreamFactoryTest, |
| 160 | ::testing::ValuesIn(QuicSupportedVersions())); |
| 161 | |
| 162 | TEST_P(QuicStreamFactoryTest, CreateIfSessionExists) { |
[email protected] | e13201d8 | 2012-12-12 05:00:32 | [diff] [blame] | 163 | EXPECT_EQ(NULL, factory_.CreateIfSessionExists(host_port_proxy_pair_, |
| 164 | net_log_).get()); |
| 165 | } |
| 166 | |
[email protected] | 1e96003 | 2013-12-20 19:00:20 | [diff] [blame] | 167 | TEST_P(QuicStreamFactoryTest, Create) { |
[email protected] | 69dfd1b | 2013-06-04 22:20:12 | [diff] [blame] | 168 | MockRead reads[] = { |
[email protected] | 25c31dc | 2013-06-05 17:56:04 | [diff] [blame] | 169 | MockRead(ASYNC, OK, 0) // EOF |
[email protected] | 69dfd1b | 2013-06-04 22:20:12 | [diff] [blame] | 170 | }; |
[email protected] | 25c31dc | 2013-06-05 17:56:04 | [diff] [blame] | 171 | DeterministicSocketData socket_data(reads, arraysize(reads), NULL, 0); |
[email protected] | e13201d8 | 2012-12-12 05:00:32 | [diff] [blame] | 172 | socket_factory_.AddSocketDataProvider(&socket_data); |
[email protected] | 25c31dc | 2013-06-05 17:56:04 | [diff] [blame] | 173 | socket_data.StopAfter(1); |
[email protected] | e13201d8 | 2012-12-12 05:00:32 | [diff] [blame] | 174 | |
| 175 | QuicStreamRequest request(&factory_); |
[email protected] | 974849d | 2014-02-06 01:32:59 | [diff] [blame] | 176 | EXPECT_EQ(ERR_IO_PENDING, |
| 177 | request.Request(host_port_proxy_pair_, |
| 178 | is_https_, |
| 179 | "GET", |
| 180 | cert_verifier_.get(), |
| 181 | net_log_, |
| 182 | callback_.callback())); |
[email protected] | e13201d8 | 2012-12-12 05:00:32 | [diff] [blame] | 183 | |
| 184 | EXPECT_EQ(OK, callback_.WaitForResult()); |
| 185 | scoped_ptr<QuicHttpStream> stream = request.ReleaseStream(); |
[email protected] | 0edce6a | 2013-05-08 18:02:40 | [diff] [blame] | 186 | EXPECT_TRUE(stream.get()); |
[email protected] | e13201d8 | 2012-12-12 05:00:32 | [diff] [blame] | 187 | |
| 188 | // Will reset stream 3. |
| 189 | stream = factory_.CreateIfSessionExists(host_port_proxy_pair_, net_log_); |
| 190 | EXPECT_TRUE(stream.get()); |
| 191 | |
[email protected] | 6d1b4ed | 2013-07-10 03:57:54 | [diff] [blame] | 192 | // TODO(rtenneti): We should probably have a tests that HTTP and HTTPS result |
| 193 | // in streams on different sessions. |
[email protected] | e13201d8 | 2012-12-12 05:00:32 | [diff] [blame] | 194 | QuicStreamRequest request2(&factory_); |
[email protected] | 974849d | 2014-02-06 01:32:59 | [diff] [blame] | 195 | EXPECT_EQ(OK, |
| 196 | request2.Request(host_port_proxy_pair_, |
| 197 | is_https_, |
| 198 | "GET", |
| 199 | cert_verifier_.get(), |
| 200 | net_log_, |
| 201 | callback_.callback())); |
[email protected] | e13201d8 | 2012-12-12 05:00:32 | [diff] [blame] | 202 | stream = request2.ReleaseStream(); // Will reset stream 5. |
| 203 | stream.reset(); // Will reset stream 7. |
| 204 | |
| 205 | EXPECT_TRUE(socket_data.at_read_eof()); |
| 206 | EXPECT_TRUE(socket_data.at_write_eof()); |
| 207 | } |
| 208 | |
[email protected] | eed749f9 | 2013-12-23 18:57:38 | [diff] [blame] | 209 | TEST_P(QuicStreamFactoryTest, Pooling) { |
| 210 | MockRead reads[] = { |
| 211 | MockRead(ASYNC, OK, 0) // EOF |
| 212 | }; |
| 213 | DeterministicSocketData socket_data(reads, arraysize(reads), NULL, 0); |
| 214 | socket_factory_.AddSocketDataProvider(&socket_data); |
| 215 | socket_data.StopAfter(1); |
| 216 | |
| 217 | HostPortProxyPair server2 = HostPortProxyPair( |
| 218 | HostPortPair("mail.google.com", kDefaultServerPort), |
| 219 | host_port_proxy_pair_.second); |
| 220 | |
| 221 | host_resolver_.set_synchronous_mode(true); |
| 222 | host_resolver_.rules()->AddIPLiteralRule( |
| 223 | kDefaultServerHostName, "192.168.0.1", ""); |
| 224 | host_resolver_.rules()->AddIPLiteralRule( |
| 225 | "mail.google.com", "192.168.0.1", ""); |
| 226 | |
| 227 | QuicStreamRequest request(&factory_); |
[email protected] | 974849d | 2014-02-06 01:32:59 | [diff] [blame] | 228 | EXPECT_EQ(OK, |
| 229 | request.Request(host_port_proxy_pair_, |
| 230 | is_https_, |
| 231 | "GET", |
| 232 | cert_verifier_.get(), |
| 233 | net_log_, |
| 234 | callback_.callback())); |
[email protected] | eed749f9 | 2013-12-23 18:57:38 | [diff] [blame] | 235 | scoped_ptr<QuicHttpStream> stream = request.ReleaseStream(); |
| 236 | EXPECT_TRUE(stream.get()); |
| 237 | |
| 238 | TestCompletionCallback callback; |
| 239 | QuicStreamRequest request2(&factory_); |
[email protected] | 974849d | 2014-02-06 01:32:59 | [diff] [blame] | 240 | EXPECT_EQ(OK, |
| 241 | request2.Request(server2, |
| 242 | is_https_, |
| 243 | "GET", |
| 244 | cert_verifier_.get(), |
| 245 | net_log_, |
| 246 | callback.callback())); |
[email protected] | eed749f9 | 2013-12-23 18:57:38 | [diff] [blame] | 247 | scoped_ptr<QuicHttpStream> stream2 = request2.ReleaseStream(); |
| 248 | EXPECT_TRUE(stream2.get()); |
| 249 | |
| 250 | EXPECT_EQ( |
| 251 | QuicStreamFactoryPeer::GetActiveSession(&factory_, host_port_proxy_pair_), |
| 252 | QuicStreamFactoryPeer::GetActiveSession(&factory_, server2)); |
| 253 | |
| 254 | EXPECT_TRUE(socket_data.at_read_eof()); |
| 255 | EXPECT_TRUE(socket_data.at_write_eof()); |
| 256 | } |
| 257 | |
| 258 | TEST_P(QuicStreamFactoryTest, NoPoolingAfterGoAway) { |
| 259 | MockRead reads[] = { |
| 260 | MockRead(ASYNC, OK, 0) // EOF |
| 261 | }; |
| 262 | DeterministicSocketData socket_data1(reads, arraysize(reads), NULL, 0); |
| 263 | DeterministicSocketData socket_data2(reads, arraysize(reads), NULL, 0); |
| 264 | socket_factory_.AddSocketDataProvider(&socket_data1); |
| 265 | socket_factory_.AddSocketDataProvider(&socket_data2); |
| 266 | socket_data1.StopAfter(1); |
| 267 | socket_data2.StopAfter(1); |
| 268 | |
| 269 | HostPortProxyPair server2 = HostPortProxyPair( |
| 270 | HostPortPair("mail.google.com", kDefaultServerPort), |
| 271 | host_port_proxy_pair_.second); |
| 272 | |
| 273 | host_resolver_.set_synchronous_mode(true); |
| 274 | host_resolver_.rules()->AddIPLiteralRule( |
| 275 | kDefaultServerHostName, "192.168.0.1", ""); |
| 276 | host_resolver_.rules()->AddIPLiteralRule( |
| 277 | "mail.google.com", "192.168.0.1", ""); |
| 278 | |
| 279 | QuicStreamRequest request(&factory_); |
[email protected] | 974849d | 2014-02-06 01:32:59 | [diff] [blame] | 280 | EXPECT_EQ(OK, |
| 281 | request.Request(host_port_proxy_pair_, |
| 282 | is_https_, |
| 283 | "GET", |
| 284 | cert_verifier_.get(), |
| 285 | net_log_, |
| 286 | callback_.callback())); |
[email protected] | eed749f9 | 2013-12-23 18:57:38 | [diff] [blame] | 287 | scoped_ptr<QuicHttpStream> stream = request.ReleaseStream(); |
| 288 | EXPECT_TRUE(stream.get()); |
| 289 | |
| 290 | TestCompletionCallback callback; |
| 291 | QuicStreamRequest request2(&factory_); |
[email protected] | 974849d | 2014-02-06 01:32:59 | [diff] [blame] | 292 | EXPECT_EQ(OK, |
| 293 | request2.Request(server2, |
| 294 | is_https_, |
| 295 | "GET", |
| 296 | cert_verifier_.get(), |
| 297 | net_log_, |
| 298 | callback.callback())); |
[email protected] | eed749f9 | 2013-12-23 18:57:38 | [diff] [blame] | 299 | scoped_ptr<QuicHttpStream> stream2 = request2.ReleaseStream(); |
| 300 | EXPECT_TRUE(stream2.get()); |
| 301 | |
| 302 | factory_.OnSessionGoingAway( |
| 303 | QuicStreamFactoryPeer::GetActiveSession(&factory_, |
| 304 | host_port_proxy_pair_)); |
| 305 | EXPECT_FALSE(QuicStreamFactoryPeer::HasActiveSession(&factory_, |
| 306 | host_port_proxy_pair_)); |
| 307 | EXPECT_FALSE(QuicStreamFactoryPeer::HasActiveSession(&factory_, server2)); |
| 308 | |
| 309 | TestCompletionCallback callback3; |
| 310 | QuicStreamRequest request3(&factory_); |
[email protected] | 974849d | 2014-02-06 01:32:59 | [diff] [blame] | 311 | EXPECT_EQ(OK, |
| 312 | request3.Request(server2, |
| 313 | is_https_, |
| 314 | "GET", |
| 315 | cert_verifier_.get(), |
| 316 | net_log_, |
| 317 | callback3.callback())); |
[email protected] | eed749f9 | 2013-12-23 18:57:38 | [diff] [blame] | 318 | scoped_ptr<QuicHttpStream> stream3 = request3.ReleaseStream(); |
| 319 | EXPECT_TRUE(stream3.get()); |
| 320 | |
| 321 | EXPECT_TRUE(QuicStreamFactoryPeer::HasActiveSession(&factory_, server2)); |
| 322 | |
| 323 | EXPECT_TRUE(socket_data1.at_read_eof()); |
| 324 | EXPECT_TRUE(socket_data1.at_write_eof()); |
| 325 | EXPECT_TRUE(socket_data2.at_read_eof()); |
| 326 | EXPECT_TRUE(socket_data2.at_write_eof()); |
| 327 | } |
| 328 | |
| 329 | TEST_P(QuicStreamFactoryTest, HttpsPooling) { |
| 330 | MockRead reads[] = { |
| 331 | MockRead(ASYNC, OK, 0) // EOF |
| 332 | }; |
| 333 | DeterministicSocketData socket_data(reads, arraysize(reads), NULL, 0); |
| 334 | socket_factory_.AddSocketDataProvider(&socket_data); |
| 335 | socket_data.StopAfter(1); |
| 336 | |
| 337 | HostPortProxyPair server1(HostPortPair("www.example.org", 443), |
| 338 | ProxyServer::Direct()); |
| 339 | HostPortProxyPair server2(HostPortPair("mail.example.org", 443), |
| 340 | ProxyServer::Direct()); |
| 341 | |
| 342 | // Load a cert that is valid for: |
| 343 | // www.example.org (server1) |
| 344 | // mail.example.org (server2) |
| 345 | // www.example.com |
| 346 | base::FilePath certs_dir = GetTestCertsDirectory(); |
| 347 | scoped_refptr<X509Certificate> test_cert( |
| 348 | ImportCertFromFile(certs_dir, "spdy_pooling.pem")); |
| 349 | ASSERT_NE(static_cast<X509Certificate*>(NULL), test_cert); |
| 350 | SSLInfo ssl_info; |
| 351 | ssl_info.cert = test_cert.get(); |
| 352 | crypto_client_stream_factory_.set_ssl_info(&ssl_info); |
| 353 | |
| 354 | host_resolver_.set_synchronous_mode(true); |
| 355 | host_resolver_.rules()->AddIPLiteralRule( |
| 356 | server1.first.host(), "192.168.0.1", ""); |
| 357 | host_resolver_.rules()->AddIPLiteralRule( |
| 358 | server2.first.host(), "192.168.0.1", ""); |
| 359 | |
| 360 | QuicStreamRequest request(&factory_); |
| 361 | is_https_ = true; |
[email protected] | 974849d | 2014-02-06 01:32:59 | [diff] [blame] | 362 | EXPECT_EQ(OK, |
| 363 | request.Request(server1, |
| 364 | is_https_, |
| 365 | "GET", |
| 366 | cert_verifier_.get(), |
| 367 | net_log_, |
| 368 | callback_.callback())); |
[email protected] | eed749f9 | 2013-12-23 18:57:38 | [diff] [blame] | 369 | scoped_ptr<QuicHttpStream> stream = request.ReleaseStream(); |
| 370 | EXPECT_TRUE(stream.get()); |
| 371 | |
| 372 | TestCompletionCallback callback; |
| 373 | QuicStreamRequest request2(&factory_); |
[email protected] | 974849d | 2014-02-06 01:32:59 | [diff] [blame] | 374 | EXPECT_EQ(OK, |
| 375 | request2.Request(server2, |
| 376 | is_https_, |
| 377 | "GET", |
| 378 | cert_verifier_.get(), |
| 379 | net_log_, |
| 380 | callback_.callback())); |
[email protected] | eed749f9 | 2013-12-23 18:57:38 | [diff] [blame] | 381 | scoped_ptr<QuicHttpStream> stream2 = request2.ReleaseStream(); |
| 382 | EXPECT_TRUE(stream2.get()); |
| 383 | |
| 384 | EXPECT_EQ(QuicStreamFactoryPeer::GetActiveSession(&factory_, server1), |
| 385 | QuicStreamFactoryPeer::GetActiveSession(&factory_, server2)); |
| 386 | |
| 387 | EXPECT_TRUE(socket_data.at_read_eof()); |
| 388 | EXPECT_TRUE(socket_data.at_write_eof()); |
| 389 | } |
| 390 | |
| 391 | TEST_P(QuicStreamFactoryTest, NoHttpsPoolingWithCertMismatch) { |
| 392 | MockRead reads[] = { |
| 393 | MockRead(ASYNC, OK, 0) // EOF |
| 394 | }; |
| 395 | DeterministicSocketData socket_data1(reads, arraysize(reads), NULL, 0); |
| 396 | DeterministicSocketData socket_data2(reads, arraysize(reads), NULL, 0); |
| 397 | socket_factory_.AddSocketDataProvider(&socket_data1); |
| 398 | socket_factory_.AddSocketDataProvider(&socket_data2); |
| 399 | socket_data1.StopAfter(1); |
| 400 | socket_data2.StopAfter(1); |
| 401 | |
| 402 | HostPortProxyPair server1(HostPortPair("www.example.org", 443), |
| 403 | ProxyServer::Direct()); |
| 404 | HostPortProxyPair server2(HostPortPair("mail.google.com", 443), |
| 405 | ProxyServer::Direct()); |
| 406 | |
| 407 | // Load a cert that is valid for: |
| 408 | // www.example.org (server1) |
| 409 | // mail.example.org |
| 410 | // www.example.com |
| 411 | // But is not valid for mail.google.com (server2). |
| 412 | base::FilePath certs_dir = GetTestCertsDirectory(); |
| 413 | scoped_refptr<X509Certificate> test_cert( |
| 414 | ImportCertFromFile(certs_dir, "spdy_pooling.pem")); |
| 415 | ASSERT_NE(static_cast<X509Certificate*>(NULL), test_cert); |
| 416 | SSLInfo ssl_info; |
| 417 | ssl_info.cert = test_cert.get(); |
| 418 | crypto_client_stream_factory_.set_ssl_info(&ssl_info); |
| 419 | |
| 420 | host_resolver_.set_synchronous_mode(true); |
| 421 | host_resolver_.rules()->AddIPLiteralRule( |
| 422 | server1.first.host(), "192.168.0.1", ""); |
| 423 | host_resolver_.rules()->AddIPLiteralRule( |
| 424 | server2.first.host(), "192.168.0.1", ""); |
| 425 | |
| 426 | QuicStreamRequest request(&factory_); |
| 427 | is_https_ = true; |
[email protected] | 974849d | 2014-02-06 01:32:59 | [diff] [blame] | 428 | EXPECT_EQ(OK, |
| 429 | request.Request(server1, |
| 430 | is_https_, |
| 431 | "GET", |
| 432 | cert_verifier_.get(), |
| 433 | net_log_, |
| 434 | callback_.callback())); |
[email protected] | eed749f9 | 2013-12-23 18:57:38 | [diff] [blame] | 435 | scoped_ptr<QuicHttpStream> stream = request.ReleaseStream(); |
| 436 | EXPECT_TRUE(stream.get()); |
| 437 | |
| 438 | TestCompletionCallback callback; |
| 439 | QuicStreamRequest request2(&factory_); |
[email protected] | 974849d | 2014-02-06 01:32:59 | [diff] [blame] | 440 | EXPECT_EQ(OK, |
| 441 | request2.Request(server2, |
| 442 | is_https_, |
| 443 | "GET", |
| 444 | cert_verifier_.get(), |
| 445 | net_log_, |
| 446 | callback_.callback())); |
[email protected] | eed749f9 | 2013-12-23 18:57:38 | [diff] [blame] | 447 | scoped_ptr<QuicHttpStream> stream2 = request2.ReleaseStream(); |
| 448 | EXPECT_TRUE(stream2.get()); |
| 449 | |
| 450 | EXPECT_NE(QuicStreamFactoryPeer::GetActiveSession(&factory_, server1), |
| 451 | QuicStreamFactoryPeer::GetActiveSession(&factory_, server2)); |
| 452 | |
| 453 | EXPECT_TRUE(socket_data1.at_read_eof()); |
| 454 | EXPECT_TRUE(socket_data1.at_write_eof()); |
| 455 | EXPECT_TRUE(socket_data2.at_read_eof()); |
| 456 | EXPECT_TRUE(socket_data2.at_write_eof()); |
| 457 | } |
| 458 | |
[email protected] | 1e96003 | 2013-12-20 19:00:20 | [diff] [blame] | 459 | TEST_P(QuicStreamFactoryTest, Goaway) { |
[email protected] | 4d283b3 | 2013-10-17 12:57:27 | [diff] [blame] | 460 | MockRead reads[] = { |
| 461 | MockRead(ASYNC, OK, 0) // EOF |
| 462 | }; |
| 463 | DeterministicSocketData socket_data(reads, arraysize(reads), NULL, 0); |
| 464 | socket_data.StopAfter(1); |
| 465 | socket_factory_.AddSocketDataProvider(&socket_data); |
| 466 | DeterministicSocketData socket_data2(reads, arraysize(reads), NULL, 0); |
| 467 | socket_data2.StopAfter(1); |
| 468 | socket_factory_.AddSocketDataProvider(&socket_data2); |
| 469 | |
| 470 | QuicStreamRequest request(&factory_); |
[email protected] | 974849d | 2014-02-06 01:32:59 | [diff] [blame] | 471 | EXPECT_EQ(ERR_IO_PENDING, |
| 472 | request.Request(host_port_proxy_pair_, |
| 473 | is_https_, |
| 474 | "GET", |
| 475 | cert_verifier_.get(), |
| 476 | net_log_, |
| 477 | callback_.callback())); |
[email protected] | 4d283b3 | 2013-10-17 12:57:27 | [diff] [blame] | 478 | |
| 479 | EXPECT_EQ(OK, callback_.WaitForResult()); |
| 480 | scoped_ptr<QuicHttpStream> stream = request.ReleaseStream(); |
| 481 | EXPECT_TRUE(stream.get()); |
| 482 | |
| 483 | // Mark the session as going away. Ensure that while it is still alive |
| 484 | // that it is no longer active. |
| 485 | QuicClientSession* session = QuicStreamFactoryPeer::GetActiveSession( |
| 486 | &factory_, host_port_proxy_pair_); |
| 487 | factory_.OnSessionGoingAway(session); |
| 488 | EXPECT_EQ(true, QuicStreamFactoryPeer::IsLiveSession(&factory_, session)); |
| 489 | EXPECT_FALSE(QuicStreamFactoryPeer::HasActiveSession(&factory_, |
| 490 | host_port_proxy_pair_)); |
| 491 | EXPECT_EQ(NULL, factory_.CreateIfSessionExists(host_port_proxy_pair_, |
| 492 | net_log_).get()); |
| 493 | |
| 494 | // Create a new request for the same destination and verify that a |
| 495 | // new session is created. |
| 496 | QuicStreamRequest request2(&factory_); |
[email protected] | 974849d | 2014-02-06 01:32:59 | [diff] [blame] | 497 | EXPECT_EQ(ERR_IO_PENDING, |
| 498 | request2.Request(host_port_proxy_pair_, |
| 499 | is_https_, |
| 500 | "GET", |
| 501 | cert_verifier_.get(), |
| 502 | net_log_, |
| 503 | callback_.callback())); |
[email protected] | 4d283b3 | 2013-10-17 12:57:27 | [diff] [blame] | 504 | EXPECT_EQ(OK, callback_.WaitForResult()); |
| 505 | scoped_ptr<QuicHttpStream> stream2 = request2.ReleaseStream(); |
| 506 | EXPECT_TRUE(stream2.get()); |
| 507 | |
| 508 | EXPECT_TRUE(QuicStreamFactoryPeer::HasActiveSession(&factory_, |
| 509 | host_port_proxy_pair_)); |
| 510 | EXPECT_NE(session, |
| 511 | QuicStreamFactoryPeer::GetActiveSession( |
| 512 | &factory_, host_port_proxy_pair_)); |
| 513 | EXPECT_EQ(true, QuicStreamFactoryPeer::IsLiveSession(&factory_, session)); |
| 514 | |
| 515 | stream2.reset(); |
| 516 | stream.reset(); |
| 517 | |
| 518 | EXPECT_TRUE(socket_data.at_read_eof()); |
| 519 | EXPECT_TRUE(socket_data.at_write_eof()); |
[email protected] | 3c77240 | 2013-12-18 21:38:11 | [diff] [blame] | 520 | EXPECT_TRUE(socket_data2.at_read_eof()); |
| 521 | EXPECT_TRUE(socket_data2.at_write_eof()); |
[email protected] | 4d283b3 | 2013-10-17 12:57:27 | [diff] [blame] | 522 | } |
| 523 | |
[email protected] | 1e96003 | 2013-12-20 19:00:20 | [diff] [blame] | 524 | TEST_P(QuicStreamFactoryTest, MaxOpenStream) { |
[email protected] | 0b2294d3 | 2013-08-02 00:46:36 | [diff] [blame] | 525 | MockRead reads[] = { |
| 526 | MockRead(ASYNC, OK, 0) // EOF |
| 527 | }; |
[email protected] | 92bf17c | 2014-03-03 21:14:03 | [diff] [blame^] | 528 | QuicStreamId stream_id = 5; |
[email protected] | 1e96003 | 2013-12-20 19:00:20 | [diff] [blame] | 529 | scoped_ptr<QuicEncryptedPacket> rst( |
| 530 | maker_.MakeRstPacket(1, true, stream_id, QUIC_STREAM_CANCELLED)); |
[email protected] | 06ff515 | 2013-08-29 01:03:05 | [diff] [blame] | 531 | MockWrite writes[] = { |
| 532 | MockWrite(ASYNC, rst->data(), rst->length(), 1), |
| 533 | }; |
| 534 | DeterministicSocketData socket_data(reads, arraysize(reads), |
| 535 | writes, arraysize(writes)); |
[email protected] | 0b2294d3 | 2013-08-02 00:46:36 | [diff] [blame] | 536 | socket_factory_.AddSocketDataProvider(&socket_data); |
| 537 | socket_data.StopAfter(1); |
| 538 | |
| 539 | HttpRequestInfo request_info; |
| 540 | std::vector<QuicHttpStream*> streams; |
| 541 | // The MockCryptoClientStream sets max_open_streams to be |
| 542 | // 2 * kDefaultMaxStreamsPerConnection. |
| 543 | for (size_t i = 0; i < 2 * kDefaultMaxStreamsPerConnection; i++) { |
| 544 | QuicStreamRequest request(&factory_); |
[email protected] | 974849d | 2014-02-06 01:32:59 | [diff] [blame] | 545 | int rv = request.Request(host_port_proxy_pair_, |
| 546 | is_https_, |
| 547 | "GET", |
| 548 | cert_verifier_.get(), |
| 549 | net_log_, |
[email protected] | 0b2294d3 | 2013-08-02 00:46:36 | [diff] [blame] | 550 | callback_.callback()); |
| 551 | if (i == 0) { |
| 552 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 553 | EXPECT_EQ(OK, callback_.WaitForResult()); |
| 554 | } else { |
| 555 | EXPECT_EQ(OK, rv); |
| 556 | } |
| 557 | scoped_ptr<QuicHttpStream> stream = request.ReleaseStream(); |
| 558 | EXPECT_TRUE(stream); |
| 559 | EXPECT_EQ(OK, stream->InitializeStream( |
| 560 | &request_info, DEFAULT_PRIORITY, net_log_, CompletionCallback())); |
| 561 | streams.push_back(stream.release()); |
| 562 | } |
| 563 | |
| 564 | QuicStreamRequest request(&factory_); |
[email protected] | 974849d | 2014-02-06 01:32:59 | [diff] [blame] | 565 | EXPECT_EQ(OK, |
| 566 | request.Request(host_port_proxy_pair_, |
| 567 | is_https_, |
| 568 | "GET", |
| 569 | cert_verifier_.get(), |
| 570 | net_log_, |
| 571 | CompletionCallback())); |
[email protected] | 0b2294d3 | 2013-08-02 00:46:36 | [diff] [blame] | 572 | scoped_ptr<QuicHttpStream> stream = request.ReleaseStream(); |
| 573 | EXPECT_TRUE(stream); |
| 574 | EXPECT_EQ(ERR_IO_PENDING, stream->InitializeStream( |
| 575 | &request_info, DEFAULT_PRIORITY, net_log_, callback_.callback())); |
| 576 | |
| 577 | // Close the first stream. |
| 578 | streams.front()->Close(false); |
| 579 | |
| 580 | ASSERT_TRUE(callback_.have_result()); |
| 581 | |
| 582 | EXPECT_EQ(OK, callback_.WaitForResult()); |
| 583 | |
| 584 | EXPECT_TRUE(socket_data.at_read_eof()); |
| 585 | EXPECT_TRUE(socket_data.at_write_eof()); |
| 586 | STLDeleteElements(&streams); |
| 587 | } |
| 588 | |
[email protected] | 1e96003 | 2013-12-20 19:00:20 | [diff] [blame] | 589 | TEST_P(QuicStreamFactoryTest, ResolutionErrorInCreate) { |
[email protected] | 0edce6a | 2013-05-08 18:02:40 | [diff] [blame] | 590 | DeterministicSocketData socket_data(NULL, 0, NULL, 0); |
[email protected] | e13201d8 | 2012-12-12 05:00:32 | [diff] [blame] | 591 | socket_factory_.AddSocketDataProvider(&socket_data); |
| 592 | |
[email protected] | 3c77240 | 2013-12-18 21:38:11 | [diff] [blame] | 593 | host_resolver_.rules()->AddSimulatedFailure(kDefaultServerHostName); |
[email protected] | e13201d8 | 2012-12-12 05:00:32 | [diff] [blame] | 594 | |
| 595 | QuicStreamRequest request(&factory_); |
[email protected] | 974849d | 2014-02-06 01:32:59 | [diff] [blame] | 596 | EXPECT_EQ(ERR_IO_PENDING, |
| 597 | request.Request(host_port_proxy_pair_, |
| 598 | is_https_, |
| 599 | "GET", |
| 600 | cert_verifier_.get(), |
| 601 | net_log_, |
| 602 | callback_.callback())); |
[email protected] | e13201d8 | 2012-12-12 05:00:32 | [diff] [blame] | 603 | |
| 604 | EXPECT_EQ(ERR_NAME_NOT_RESOLVED, callback_.WaitForResult()); |
| 605 | |
| 606 | EXPECT_TRUE(socket_data.at_read_eof()); |
| 607 | EXPECT_TRUE(socket_data.at_write_eof()); |
| 608 | } |
| 609 | |
[email protected] | 1e96003 | 2013-12-20 19:00:20 | [diff] [blame] | 610 | TEST_P(QuicStreamFactoryTest, ConnectErrorInCreate) { |
[email protected] | 3c77240 | 2013-12-18 21:38:11 | [diff] [blame] | 611 | MockConnect connect(SYNCHRONOUS, ERR_ADDRESS_IN_USE); |
| 612 | DeterministicSocketData socket_data(NULL, 0, NULL, 0); |
| 613 | socket_data.set_connect_data(connect); |
| 614 | socket_factory_.AddSocketDataProvider(&socket_data); |
| 615 | socket_data.StopAfter(1); |
| 616 | |
| 617 | QuicStreamRequest request(&factory_); |
[email protected] | 974849d | 2014-02-06 01:32:59 | [diff] [blame] | 618 | EXPECT_EQ(ERR_IO_PENDING, |
| 619 | request.Request(host_port_proxy_pair_, |
| 620 | is_https_, |
| 621 | "GET", |
| 622 | cert_verifier_.get(), |
| 623 | net_log_, |
| 624 | callback_.callback())); |
[email protected] | 3c77240 | 2013-12-18 21:38:11 | [diff] [blame] | 625 | |
| 626 | EXPECT_EQ(ERR_ADDRESS_IN_USE, callback_.WaitForResult()); |
| 627 | |
| 628 | EXPECT_TRUE(socket_data.at_read_eof()); |
| 629 | EXPECT_TRUE(socket_data.at_write_eof()); |
| 630 | } |
| 631 | |
[email protected] | 1e96003 | 2013-12-20 19:00:20 | [diff] [blame] | 632 | TEST_P(QuicStreamFactoryTest, CancelCreate) { |
[email protected] | 69dfd1b | 2013-06-04 22:20:12 | [diff] [blame] | 633 | MockRead reads[] = { |
[email protected] | 25c31dc | 2013-06-05 17:56:04 | [diff] [blame] | 634 | MockRead(ASYNC, OK, 0) // EOF |
[email protected] | 69dfd1b | 2013-06-04 22:20:12 | [diff] [blame] | 635 | }; |
[email protected] | 25c31dc | 2013-06-05 17:56:04 | [diff] [blame] | 636 | DeterministicSocketData socket_data(reads, arraysize(reads), NULL, 0); |
[email protected] | e13201d8 | 2012-12-12 05:00:32 | [diff] [blame] | 637 | socket_factory_.AddSocketDataProvider(&socket_data); |
| 638 | { |
| 639 | QuicStreamRequest request(&factory_); |
[email protected] | 974849d | 2014-02-06 01:32:59 | [diff] [blame] | 640 | EXPECT_EQ(ERR_IO_PENDING, |
| 641 | request.Request(host_port_proxy_pair_, |
| 642 | is_https_, |
| 643 | "GET", |
| 644 | cert_verifier_.get(), |
| 645 | net_log_, |
| 646 | callback_.callback())); |
[email protected] | e13201d8 | 2012-12-12 05:00:32 | [diff] [blame] | 647 | } |
| 648 | |
[email protected] | 25c31dc | 2013-06-05 17:56:04 | [diff] [blame] | 649 | socket_data.StopAfter(1); |
[email protected] | e13201d8 | 2012-12-12 05:00:32 | [diff] [blame] | 650 | base::RunLoop run_loop; |
| 651 | run_loop.RunUntilIdle(); |
| 652 | |
| 653 | scoped_ptr<QuicHttpStream> stream( |
| 654 | factory_.CreateIfSessionExists(host_port_proxy_pair_, net_log_)); |
| 655 | EXPECT_TRUE(stream.get()); |
| 656 | stream.reset(); |
| 657 | |
| 658 | EXPECT_TRUE(socket_data.at_read_eof()); |
| 659 | EXPECT_TRUE(socket_data.at_write_eof()); |
| 660 | } |
| 661 | |
[email protected] | 1e96003 | 2013-12-20 19:00:20 | [diff] [blame] | 662 | TEST_P(QuicStreamFactoryTest, CreateConsistentEphemeralPort) { |
[email protected] | 3c77240 | 2013-12-18 21:38:11 | [diff] [blame] | 663 | // Sequentially connect to the default host, then another host, and then the |
| 664 | // default host. Verify that the default host gets a consistent ephemeral |
| 665 | // port, that is different from the other host's connection. |
| 666 | |
| 667 | std::string other_server_name = "other.google.com"; |
| 668 | EXPECT_NE(kDefaultServerHostName, other_server_name); |
| 669 | HostPortPair host_port_pair2(other_server_name, kDefaultServerPort); |
| 670 | HostPortProxyPair host_port_proxy_pair2(host_port_pair2, |
| 671 | host_port_proxy_pair_.second); |
| 672 | |
| 673 | int original_port = GetSourcePortForNewSession(host_port_proxy_pair_); |
| 674 | EXPECT_NE(original_port, GetSourcePortForNewSession(host_port_proxy_pair2)); |
| 675 | EXPECT_EQ(original_port, GetSourcePortForNewSession(host_port_proxy_pair_)); |
| 676 | } |
| 677 | |
[email protected] | 1e96003 | 2013-12-20 19:00:20 | [diff] [blame] | 678 | TEST_P(QuicStreamFactoryTest, CloseAllSessions) { |
[email protected] | 56dfb90 | 2013-01-03 23:17:55 | [diff] [blame] | 679 | MockRead reads[] = { |
[email protected] | 0edce6a | 2013-05-08 18:02:40 | [diff] [blame] | 680 | MockRead(ASYNC, 0, 0) // EOF |
[email protected] | 56dfb90 | 2013-01-03 23:17:55 | [diff] [blame] | 681 | }; |
[email protected] | 459a740 | 2014-02-10 12:58:52 | [diff] [blame] | 682 | scoped_ptr<QuicEncryptedPacket> rst(ConstructRstPacket()); |
| 683 | std::vector<MockWrite> writes; |
| 684 | if (GetParam() > QUIC_VERSION_13) |
| 685 | writes.push_back(MockWrite(ASYNC, rst->data(), rst->length(), 1)); |
| 686 | DeterministicSocketData socket_data(reads, arraysize(reads), |
| 687 | writes.empty() ? NULL : &writes[0], |
| 688 | writes.size()); |
[email protected] | 56dfb90 | 2013-01-03 23:17:55 | [diff] [blame] | 689 | socket_factory_.AddSocketDataProvider(&socket_data); |
[email protected] | 0edce6a | 2013-05-08 18:02:40 | [diff] [blame] | 690 | socket_data.StopAfter(1); |
[email protected] | 56dfb90 | 2013-01-03 23:17:55 | [diff] [blame] | 691 | |
[email protected] | 69dfd1b | 2013-06-04 22:20:12 | [diff] [blame] | 692 | MockRead reads2[] = { |
[email protected] | 25c31dc | 2013-06-05 17:56:04 | [diff] [blame] | 693 | MockRead(ASYNC, 0, 0) // EOF |
[email protected] | 69dfd1b | 2013-06-04 22:20:12 | [diff] [blame] | 694 | }; |
[email protected] | 25c31dc | 2013-06-05 17:56:04 | [diff] [blame] | 695 | DeterministicSocketData socket_data2(reads2, arraysize(reads2), NULL, 0); |
[email protected] | 56dfb90 | 2013-01-03 23:17:55 | [diff] [blame] | 696 | socket_factory_.AddSocketDataProvider(&socket_data2); |
[email protected] | 0edce6a | 2013-05-08 18:02:40 | [diff] [blame] | 697 | socket_data2.StopAfter(1); |
[email protected] | 56dfb90 | 2013-01-03 23:17:55 | [diff] [blame] | 698 | |
| 699 | QuicStreamRequest request(&factory_); |
[email protected] | 974849d | 2014-02-06 01:32:59 | [diff] [blame] | 700 | EXPECT_EQ(ERR_IO_PENDING, |
| 701 | request.Request(host_port_proxy_pair_, |
| 702 | is_https_, |
| 703 | "GET", |
| 704 | cert_verifier_.get(), |
| 705 | net_log_, |
| 706 | callback_.callback())); |
[email protected] | 56dfb90 | 2013-01-03 23:17:55 | [diff] [blame] | 707 | |
| 708 | EXPECT_EQ(OK, callback_.WaitForResult()); |
| 709 | scoped_ptr<QuicHttpStream> stream = request.ReleaseStream(); |
[email protected] | 0b2294d3 | 2013-08-02 00:46:36 | [diff] [blame] | 710 | HttpRequestInfo request_info; |
| 711 | EXPECT_EQ(OK, stream->InitializeStream(&request_info, |
| 712 | DEFAULT_PRIORITY, |
| 713 | net_log_, CompletionCallback())); |
[email protected] | 56dfb90 | 2013-01-03 23:17:55 | [diff] [blame] | 714 | |
| 715 | // Close the session and verify that stream saw the error. |
| 716 | factory_.CloseAllSessions(ERR_INTERNET_DISCONNECTED); |
| 717 | EXPECT_EQ(ERR_INTERNET_DISCONNECTED, |
| 718 | stream->ReadResponseHeaders(callback_.callback())); |
| 719 | |
| 720 | // Now attempting to request a stream to the same origin should create |
| 721 | // a new session. |
| 722 | |
| 723 | QuicStreamRequest request2(&factory_); |
[email protected] | 974849d | 2014-02-06 01:32:59 | [diff] [blame] | 724 | EXPECT_EQ(ERR_IO_PENDING, |
| 725 | request2.Request(host_port_proxy_pair_, |
| 726 | is_https_, |
| 727 | "GET", |
| 728 | cert_verifier_.get(), |
| 729 | net_log_, |
| 730 | callback_.callback())); |
[email protected] | 56dfb90 | 2013-01-03 23:17:55 | [diff] [blame] | 731 | |
| 732 | EXPECT_EQ(OK, callback_.WaitForResult()); |
| 733 | stream = request2.ReleaseStream(); |
| 734 | stream.reset(); // Will reset stream 3. |
| 735 | |
| 736 | EXPECT_TRUE(socket_data.at_read_eof()); |
| 737 | EXPECT_TRUE(socket_data.at_write_eof()); |
| 738 | EXPECT_TRUE(socket_data2.at_read_eof()); |
| 739 | EXPECT_TRUE(socket_data2.at_write_eof()); |
| 740 | } |
| 741 | |
[email protected] | 1e96003 | 2013-12-20 19:00:20 | [diff] [blame] | 742 | TEST_P(QuicStreamFactoryTest, OnIPAddressChanged) { |
[email protected] | f698a01 | 2013-05-06 20:18:59 | [diff] [blame] | 743 | MockRead reads[] = { |
[email protected] | 0edce6a | 2013-05-08 18:02:40 | [diff] [blame] | 744 | MockRead(ASYNC, 0, 0) // EOF |
[email protected] | f698a01 | 2013-05-06 20:18:59 | [diff] [blame] | 745 | }; |
[email protected] | 459a740 | 2014-02-10 12:58:52 | [diff] [blame] | 746 | scoped_ptr<QuicEncryptedPacket> rst(ConstructRstPacket()); |
| 747 | std::vector<MockWrite> writes; |
| 748 | if (GetParam() > QUIC_VERSION_13) |
| 749 | writes.push_back(MockWrite(ASYNC, rst->data(), rst->length(), 1)); |
| 750 | DeterministicSocketData socket_data(reads, arraysize(reads), |
| 751 | writes.empty() ? NULL : &writes[0], |
| 752 | writes.size()); |
[email protected] | f698a01 | 2013-05-06 20:18:59 | [diff] [blame] | 753 | socket_factory_.AddSocketDataProvider(&socket_data); |
[email protected] | 0edce6a | 2013-05-08 18:02:40 | [diff] [blame] | 754 | socket_data.StopAfter(1); |
[email protected] | f698a01 | 2013-05-06 20:18:59 | [diff] [blame] | 755 | |
[email protected] | 69dfd1b | 2013-06-04 22:20:12 | [diff] [blame] | 756 | MockRead reads2[] = { |
[email protected] | 25c31dc | 2013-06-05 17:56:04 | [diff] [blame] | 757 | MockRead(ASYNC, 0, 0) // EOF |
[email protected] | 69dfd1b | 2013-06-04 22:20:12 | [diff] [blame] | 758 | }; |
[email protected] | 25c31dc | 2013-06-05 17:56:04 | [diff] [blame] | 759 | DeterministicSocketData socket_data2(reads2, arraysize(reads2), NULL, 0); |
[email protected] | f698a01 | 2013-05-06 20:18:59 | [diff] [blame] | 760 | socket_factory_.AddSocketDataProvider(&socket_data2); |
[email protected] | 0edce6a | 2013-05-08 18:02:40 | [diff] [blame] | 761 | socket_data2.StopAfter(1); |
[email protected] | f698a01 | 2013-05-06 20:18:59 | [diff] [blame] | 762 | |
| 763 | QuicStreamRequest request(&factory_); |
[email protected] | 974849d | 2014-02-06 01:32:59 | [diff] [blame] | 764 | EXPECT_EQ(ERR_IO_PENDING, |
| 765 | request.Request(host_port_proxy_pair_, |
| 766 | is_https_, |
| 767 | "GET", |
| 768 | cert_verifier_.get(), |
| 769 | net_log_, |
| 770 | callback_.callback())); |
[email protected] | f698a01 | 2013-05-06 20:18:59 | [diff] [blame] | 771 | |
| 772 | EXPECT_EQ(OK, callback_.WaitForResult()); |
| 773 | scoped_ptr<QuicHttpStream> stream = request.ReleaseStream(); |
[email protected] | 0b2294d3 | 2013-08-02 00:46:36 | [diff] [blame] | 774 | HttpRequestInfo request_info; |
| 775 | EXPECT_EQ(OK, stream->InitializeStream(&request_info, |
| 776 | DEFAULT_PRIORITY, |
| 777 | net_log_, CompletionCallback())); |
[email protected] | f698a01 | 2013-05-06 20:18:59 | [diff] [blame] | 778 | |
| 779 | // Change the IP address and verify that stream saw the error. |
| 780 | factory_.OnIPAddressChanged(); |
| 781 | EXPECT_EQ(ERR_NETWORK_CHANGED, |
| 782 | stream->ReadResponseHeaders(callback_.callback())); |
[email protected] | 11c0587 | 2013-08-20 02:04:12 | [diff] [blame] | 783 | EXPECT_TRUE(factory_.require_confirmation()); |
[email protected] | f698a01 | 2013-05-06 20:18:59 | [diff] [blame] | 784 | |
| 785 | // Now attempting to request a stream to the same origin should create |
| 786 | // a new session. |
| 787 | |
| 788 | QuicStreamRequest request2(&factory_); |
[email protected] | 974849d | 2014-02-06 01:32:59 | [diff] [blame] | 789 | EXPECT_EQ(ERR_IO_PENDING, |
| 790 | request2.Request(host_port_proxy_pair_, |
| 791 | is_https_, |
| 792 | "GET", |
| 793 | cert_verifier_.get(), |
| 794 | net_log_, |
| 795 | callback_.callback())); |
[email protected] | f698a01 | 2013-05-06 20:18:59 | [diff] [blame] | 796 | |
| 797 | EXPECT_EQ(OK, callback_.WaitForResult()); |
| 798 | stream = request2.ReleaseStream(); |
| 799 | stream.reset(); // Will reset stream 3. |
| 800 | |
| 801 | EXPECT_TRUE(socket_data.at_read_eof()); |
| 802 | EXPECT_TRUE(socket_data.at_write_eof()); |
| 803 | EXPECT_TRUE(socket_data2.at_read_eof()); |
| 804 | EXPECT_TRUE(socket_data2.at_write_eof()); |
| 805 | } |
| 806 | |
[email protected] | 1e96003 | 2013-12-20 19:00:20 | [diff] [blame] | 807 | TEST_P(QuicStreamFactoryTest, OnCertAdded) { |
[email protected] | d7d1e50b | 2013-11-25 22:08:09 | [diff] [blame] | 808 | MockRead reads[] = { |
| 809 | MockRead(ASYNC, 0, 0) // EOF |
| 810 | }; |
[email protected] | 459a740 | 2014-02-10 12:58:52 | [diff] [blame] | 811 | scoped_ptr<QuicEncryptedPacket> rst(ConstructRstPacket()); |
| 812 | std::vector<MockWrite> writes; |
| 813 | if (GetParam() > QUIC_VERSION_13) |
| 814 | writes.push_back(MockWrite(ASYNC, rst->data(), rst->length(), 1)); |
| 815 | DeterministicSocketData socket_data(reads, arraysize(reads), |
| 816 | writes.empty() ? NULL : &writes[0], |
| 817 | writes.size()); |
[email protected] | d7d1e50b | 2013-11-25 22:08:09 | [diff] [blame] | 818 | socket_factory_.AddSocketDataProvider(&socket_data); |
| 819 | socket_data.StopAfter(1); |
| 820 | |
| 821 | MockRead reads2[] = { |
| 822 | MockRead(ASYNC, 0, 0) // EOF |
| 823 | }; |
| 824 | DeterministicSocketData socket_data2(reads2, arraysize(reads2), NULL, 0); |
| 825 | socket_factory_.AddSocketDataProvider(&socket_data2); |
| 826 | socket_data2.StopAfter(1); |
| 827 | |
| 828 | QuicStreamRequest request(&factory_); |
[email protected] | 974849d | 2014-02-06 01:32:59 | [diff] [blame] | 829 | EXPECT_EQ(ERR_IO_PENDING, |
| 830 | request.Request(host_port_proxy_pair_, |
| 831 | is_https_, |
| 832 | "GET", |
| 833 | cert_verifier_.get(), |
| 834 | net_log_, |
| 835 | callback_.callback())); |
[email protected] | d7d1e50b | 2013-11-25 22:08:09 | [diff] [blame] | 836 | |
| 837 | EXPECT_EQ(OK, callback_.WaitForResult()); |
| 838 | scoped_ptr<QuicHttpStream> stream = request.ReleaseStream(); |
| 839 | HttpRequestInfo request_info; |
| 840 | EXPECT_EQ(OK, stream->InitializeStream(&request_info, |
| 841 | DEFAULT_PRIORITY, |
| 842 | net_log_, CompletionCallback())); |
| 843 | |
| 844 | // Add a cert and verify that stream saw the event. |
| 845 | factory_.OnCertAdded(NULL); |
| 846 | EXPECT_EQ(ERR_CERT_DATABASE_CHANGED, |
| 847 | stream->ReadResponseHeaders(callback_.callback())); |
| 848 | EXPECT_FALSE(factory_.require_confirmation()); |
| 849 | |
| 850 | // Now attempting to request a stream to the same origin should create |
| 851 | // a new session. |
| 852 | |
| 853 | QuicStreamRequest request2(&factory_); |
[email protected] | 974849d | 2014-02-06 01:32:59 | [diff] [blame] | 854 | EXPECT_EQ(ERR_IO_PENDING, |
| 855 | request2.Request(host_port_proxy_pair_, |
| 856 | is_https_, |
| 857 | "GET", |
| 858 | cert_verifier_.get(), |
| 859 | net_log_, |
| 860 | callback_.callback())); |
[email protected] | d7d1e50b | 2013-11-25 22:08:09 | [diff] [blame] | 861 | |
| 862 | EXPECT_EQ(OK, callback_.WaitForResult()); |
| 863 | stream = request2.ReleaseStream(); |
| 864 | stream.reset(); // Will reset stream 3. |
| 865 | |
| 866 | EXPECT_TRUE(socket_data.at_read_eof()); |
| 867 | EXPECT_TRUE(socket_data.at_write_eof()); |
| 868 | EXPECT_TRUE(socket_data2.at_read_eof()); |
| 869 | EXPECT_TRUE(socket_data2.at_write_eof()); |
| 870 | } |
| 871 | |
[email protected] | 1e96003 | 2013-12-20 19:00:20 | [diff] [blame] | 872 | TEST_P(QuicStreamFactoryTest, OnCACertChanged) { |
[email protected] | d7d1e50b | 2013-11-25 22:08:09 | [diff] [blame] | 873 | MockRead reads[] = { |
| 874 | MockRead(ASYNC, 0, 0) // EOF |
| 875 | }; |
[email protected] | 459a740 | 2014-02-10 12:58:52 | [diff] [blame] | 876 | scoped_ptr<QuicEncryptedPacket> rst(ConstructRstPacket()); |
| 877 | std::vector<MockWrite> writes; |
| 878 | if (GetParam() > QUIC_VERSION_13) |
| 879 | writes.push_back(MockWrite(ASYNC, rst->data(), rst->length(), 1)); |
| 880 | DeterministicSocketData socket_data(reads, arraysize(reads), |
| 881 | writes.empty() ? NULL : &writes[0], |
| 882 | writes.size()); |
[email protected] | d7d1e50b | 2013-11-25 22:08:09 | [diff] [blame] | 883 | socket_factory_.AddSocketDataProvider(&socket_data); |
| 884 | socket_data.StopAfter(1); |
| 885 | |
| 886 | MockRead reads2[] = { |
| 887 | MockRead(ASYNC, 0, 0) // EOF |
| 888 | }; |
| 889 | DeterministicSocketData socket_data2(reads2, arraysize(reads2), NULL, 0); |
| 890 | socket_factory_.AddSocketDataProvider(&socket_data2); |
| 891 | socket_data2.StopAfter(1); |
| 892 | |
| 893 | QuicStreamRequest request(&factory_); |
[email protected] | 974849d | 2014-02-06 01:32:59 | [diff] [blame] | 894 | EXPECT_EQ(ERR_IO_PENDING, |
| 895 | request.Request(host_port_proxy_pair_, |
| 896 | is_https_, |
| 897 | "GET", |
| 898 | cert_verifier_.get(), |
| 899 | net_log_, |
| 900 | callback_.callback())); |
[email protected] | d7d1e50b | 2013-11-25 22:08:09 | [diff] [blame] | 901 | |
| 902 | EXPECT_EQ(OK, callback_.WaitForResult()); |
| 903 | scoped_ptr<QuicHttpStream> stream = request.ReleaseStream(); |
| 904 | HttpRequestInfo request_info; |
| 905 | EXPECT_EQ(OK, stream->InitializeStream(&request_info, |
| 906 | DEFAULT_PRIORITY, |
| 907 | net_log_, CompletionCallback())); |
| 908 | |
| 909 | // Change the CA cert and verify that stream saw the event. |
| 910 | factory_.OnCACertChanged(NULL); |
| 911 | EXPECT_EQ(ERR_CERT_DATABASE_CHANGED, |
| 912 | stream->ReadResponseHeaders(callback_.callback())); |
| 913 | EXPECT_FALSE(factory_.require_confirmation()); |
| 914 | |
| 915 | // Now attempting to request a stream to the same origin should create |
| 916 | // a new session. |
| 917 | |
| 918 | QuicStreamRequest request2(&factory_); |
[email protected] | 974849d | 2014-02-06 01:32:59 | [diff] [blame] | 919 | EXPECT_EQ(ERR_IO_PENDING, |
| 920 | request2.Request(host_port_proxy_pair_, |
| 921 | is_https_, |
| 922 | "GET", |
| 923 | cert_verifier_.get(), |
| 924 | net_log_, |
| 925 | callback_.callback())); |
[email protected] | d7d1e50b | 2013-11-25 22:08:09 | [diff] [blame] | 926 | |
| 927 | EXPECT_EQ(OK, callback_.WaitForResult()); |
| 928 | stream = request2.ReleaseStream(); |
| 929 | stream.reset(); // Will reset stream 3. |
| 930 | |
| 931 | EXPECT_TRUE(socket_data.at_read_eof()); |
| 932 | EXPECT_TRUE(socket_data.at_write_eof()); |
| 933 | EXPECT_TRUE(socket_data2.at_read_eof()); |
| 934 | EXPECT_TRUE(socket_data2.at_write_eof()); |
| 935 | } |
| 936 | |
[email protected] | 1e96003 | 2013-12-20 19:00:20 | [diff] [blame] | 937 | TEST_P(QuicStreamFactoryTest, SharedCryptoConfig) { |
[email protected] | 6e12d70 | 2013-11-13 00:17:17 | [diff] [blame] | 938 | vector<string> cannoncial_suffixes; |
| 939 | cannoncial_suffixes.push_back(string(".c.youtube.com")); |
| 940 | cannoncial_suffixes.push_back(string(".googlevideo.com")); |
[email protected] | c49ff18 | 2013-09-28 08:33:26 | [diff] [blame] | 941 | |
[email protected] | 6e12d70 | 2013-11-13 00:17:17 | [diff] [blame] | 942 | for (unsigned i = 0; i < cannoncial_suffixes.size(); ++i) { |
| 943 | string r1_host_name("r1"); |
| 944 | string r2_host_name("r2"); |
| 945 | r1_host_name.append(cannoncial_suffixes[i]); |
| 946 | r2_host_name.append(cannoncial_suffixes[i]); |
[email protected] | b70fdb79 | 2013-10-25 19:04:14 | [diff] [blame] | 947 | |
[email protected] | 6e12d70 | 2013-11-13 00:17:17 | [diff] [blame] | 948 | HostPortProxyPair host_port_proxy_pair1(HostPortPair(r1_host_name, 80), |
| 949 | ProxyServer::Direct()); |
[email protected] | c49ff18 | 2013-09-28 08:33:26 | [diff] [blame] | 950 | |
[email protected] | 6e12d70 | 2013-11-13 00:17:17 | [diff] [blame] | 951 | QuicCryptoClientConfig* crypto_config1 = |
| 952 | QuicStreamFactoryPeer::GetOrCreateCryptoConfig(&factory_, |
| 953 | host_port_proxy_pair1); |
| 954 | DCHECK(crypto_config1); |
| 955 | QuicCryptoClientConfig::CachedState* cached1 = |
| 956 | crypto_config1->LookupOrCreate(host_port_proxy_pair1.first.host()); |
| 957 | EXPECT_FALSE(cached1->proof_valid()); |
| 958 | EXPECT_TRUE(cached1->source_address_token().empty()); |
| 959 | |
| 960 | // Mutate the cached1 to have different data. |
| 961 | // TODO(rtenneti): mutate other members of CachedState. |
| 962 | cached1->set_source_address_token(r1_host_name); |
| 963 | cached1->SetProofValid(); |
| 964 | |
| 965 | HostPortProxyPair host_port_proxy_pair2(HostPortPair(r2_host_name, 80), |
| 966 | ProxyServer::Direct()); |
| 967 | QuicCryptoClientConfig* crypto_config2 = |
| 968 | QuicStreamFactoryPeer::GetOrCreateCryptoConfig(&factory_, |
| 969 | host_port_proxy_pair2); |
| 970 | DCHECK(crypto_config2); |
| 971 | QuicCryptoClientConfig::CachedState* cached2 = |
| 972 | crypto_config2->LookupOrCreate(host_port_proxy_pair2.first.host()); |
| 973 | EXPECT_EQ(cached1->source_address_token(), cached2->source_address_token()); |
| 974 | EXPECT_TRUE(cached2->proof_valid()); |
| 975 | } |
[email protected] | b70fdb79 | 2013-10-25 19:04:14 | [diff] [blame] | 976 | } |
| 977 | |
[email protected] | 1e96003 | 2013-12-20 19:00:20 | [diff] [blame] | 978 | TEST_P(QuicStreamFactoryTest, CryptoConfigWhenProofIsInvalid) { |
[email protected] | 6e12d70 | 2013-11-13 00:17:17 | [diff] [blame] | 979 | vector<string> cannoncial_suffixes; |
| 980 | cannoncial_suffixes.push_back(string(".c.youtube.com")); |
| 981 | cannoncial_suffixes.push_back(string(".googlevideo.com")); |
[email protected] | b70fdb79 | 2013-10-25 19:04:14 | [diff] [blame] | 982 | |
[email protected] | 6e12d70 | 2013-11-13 00:17:17 | [diff] [blame] | 983 | for (unsigned i = 0; i < cannoncial_suffixes.size(); ++i) { |
| 984 | string r3_host_name("r3"); |
| 985 | string r4_host_name("r4"); |
| 986 | r3_host_name.append(cannoncial_suffixes[i]); |
| 987 | r4_host_name.append(cannoncial_suffixes[i]); |
[email protected] | b70fdb79 | 2013-10-25 19:04:14 | [diff] [blame] | 988 | |
[email protected] | 6e12d70 | 2013-11-13 00:17:17 | [diff] [blame] | 989 | HostPortProxyPair host_port_proxy_pair1(HostPortPair(r3_host_name, 80), |
| 990 | ProxyServer::Direct()); |
[email protected] | b70fdb79 | 2013-10-25 19:04:14 | [diff] [blame] | 991 | |
[email protected] | 6e12d70 | 2013-11-13 00:17:17 | [diff] [blame] | 992 | QuicCryptoClientConfig* crypto_config1 = |
| 993 | QuicStreamFactoryPeer::GetOrCreateCryptoConfig(&factory_, |
| 994 | host_port_proxy_pair1); |
| 995 | DCHECK(crypto_config1); |
| 996 | QuicCryptoClientConfig::CachedState* cached1 = |
| 997 | crypto_config1->LookupOrCreate(host_port_proxy_pair1.first.host()); |
| 998 | EXPECT_FALSE(cached1->proof_valid()); |
| 999 | EXPECT_TRUE(cached1->source_address_token().empty()); |
| 1000 | |
| 1001 | // Mutate the cached1 to have different data. |
| 1002 | // TODO(rtenneti): mutate other members of CachedState. |
| 1003 | cached1->set_source_address_token(r3_host_name); |
| 1004 | cached1->SetProofInvalid(); |
| 1005 | |
| 1006 | HostPortProxyPair host_port_proxy_pair2(HostPortPair(r4_host_name, 80), |
| 1007 | ProxyServer::Direct()); |
| 1008 | QuicCryptoClientConfig* crypto_config2 = |
| 1009 | QuicStreamFactoryPeer::GetOrCreateCryptoConfig(&factory_, |
| 1010 | host_port_proxy_pair2); |
| 1011 | DCHECK(crypto_config2); |
| 1012 | QuicCryptoClientConfig::CachedState* cached2 = |
| 1013 | crypto_config2->LookupOrCreate(host_port_proxy_pair2.first.host()); |
| 1014 | EXPECT_NE(cached1->source_address_token(), cached2->source_address_token()); |
| 1015 | EXPECT_TRUE(cached2->source_address_token().empty()); |
| 1016 | EXPECT_FALSE(cached2->proof_valid()); |
| 1017 | } |
[email protected] | c49ff18 | 2013-09-28 08:33:26 | [diff] [blame] | 1018 | } |
| 1019 | |
[email protected] | e13201d8 | 2012-12-12 05:00:32 | [diff] [blame] | 1020 | } // namespace test |
[email protected] | e13201d8 | 2012-12-12 05:00:32 | [diff] [blame] | 1021 | } // namespace net |