Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2016 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/http/http_stream_factory_impl_job_controller.h" | |
| 6 | |
| 7 #include <memory> | |
| 8 | |
| 9 #include "net/http/http_basic_stream.h" | |
| 10 #include "net/http/http_stream_factory_impl_job.h" | |
| 11 #include "net/http/http_stream_factory_impl_request.h" | |
| 12 #include "net/http/http_stream_factory_test_util.h" | |
| 13 #include "net/proxy/proxy_info.h" | |
| 14 #include "net/proxy/proxy_service.h" | |
| 15 #include "net/spdy/spdy_test_util_common.h" | |
| 16 #include "net/ssl/ssl_failure_state.h" | |
| 17 #include "testing/gtest/include/gtest/gtest.h" | |
| 18 | |
| 19 using ::testing::_; | |
| 20 | |
| 21 namespace net { | |
| 22 | |
| 23 class TestJobControllerPeer { | |
| 24 public: | |
| 25 TestJobControllerPeer(NextProto protocol, | |
| 26 TestHttpStreamRequestDelegate* request_delegate) | |
| 27 : session_deps_(protocol, ProxyService::CreateDirect()) { | |
| 28 session_deps_.enable_quic = true; | |
| 29 session_ = SpdySessionDependencies::SpdyCreateSession(&session_deps_); | |
| 30 factory_ = | |
| 31 static_cast<HttpStreamFactoryImpl*>(session_->http_stream_factory()); | |
| 32 job_controller_ = new HttpStreamFactoryImpl::JobController( | |
| 33 factory_, request_delegate, session_.get(), &job_factory_); | |
| 34 factory_->job_controller_set_.insert(base::WrapUnique(job_controller_)); | |
| 35 } | |
| 36 | |
| 37 ~TestJobControllerPeer() {} | |
| 38 | |
| 39 TestHttpStreamFactoryImplJob* AddMainJobToFactory( | |
| 40 const HttpRequestInfo& request_info) { | |
| 41 HostPortPair host_port_pair = HostPortPair::FromURL(request_info.url); | |
| 42 GURL original_url = job_controller_->ApplyHostMappingRules(request_info.url, | |
| 43 &host_port_pair); | |
| 44 TestHttpStreamFactoryImplJob* main_job = new TestHttpStreamFactoryImplJob( | |
| 45 job_controller_, HttpStreamFactoryImpl::MAIN, session_.get(), | |
| 46 request_info, DEFAULT_PRIORITY, SSLConfig(), SSLConfig(), | |
| 47 host_port_pair, original_url, nullptr); | |
| 48 job_factory_.SetMainJob(main_job); | |
| 49 return main_job; | |
| 50 } | |
| 51 | |
| 52 TestHttpStreamFactoryImplJob* AddAlternativeJobToFactory( | |
| 53 const HttpRequestInfo& request_info, | |
| 54 AlternativeService alternative_service) { | |
| 55 HostPortPair host_port_pair = HostPortPair::FromURL(request_info.url); | |
| 56 GURL original_url = job_controller_->ApplyHostMappingRules(request_info.url, | |
| 57 &host_port_pair); | |
| 58 TestHttpStreamFactoryImplJob* alternative_job = | |
| 59 new TestHttpStreamFactoryImplJob( | |
| 60 job_controller_, HttpStreamFactoryImpl::ALTERNATIVE, session_.get(), | |
| 61 request_info, DEFAULT_PRIORITY, SSLConfig(), SSLConfig(), | |
| 62 host_port_pair, original_url, alternative_service, nullptr); | |
| 63 job_factory_.SetAlternativeJob(alternative_job); | |
| 64 | |
| 65 url::SchemeHostPort server(request_info.url); | |
| 66 base::Time expiration = base::Time::Now() + base::TimeDelta::FromDays(1); | |
| 67 session_->http_server_properties()->SetAlternativeService( | |
| 68 server, alternative_service, expiration); | |
| 69 | |
| 70 return alternative_job; | |
| 71 } | |
| 72 | |
| 73 void Start(const HttpRequestInfo& request_info, | |
| 74 HttpStreamRequest::Delegate* delegate, | |
| 75 WebSocketHandshakeStreamBase::CreateHelper* | |
| 76 websocket_handshake_stream_create_helper, | |
| 77 const BoundNetLog& net_log, | |
| 78 RequestPriority priority, | |
| 79 const SSLConfig& server_ssl_config, | |
| 80 const SSLConfig& proxy_ssl_config) { | |
| 81 request_.reset(job_controller_->Start( | |
| 82 request_info, delegate, websocket_handshake_stream_create_helper, | |
| 83 net_log, HttpStreamFactoryImpl::Request::HTTP_STREAM, priority, | |
| 84 server_ssl_config, proxy_ssl_config)); | |
| 85 } | |
| 86 | |
| 87 void CancelRequest() { request_.reset(); } | |
| 88 | |
| 89 void OnStreamFailed(HttpStreamFactoryImpl::Job* job, | |
| 90 int status, | |
| 91 const SSLConfig& used_ssl_config, | |
| 92 SSLFailureState ssl_failure_state) { | |
| 93 job_controller_->OnStreamFailed(job, status, used_ssl_config, | |
| 94 ssl_failure_state); | |
| 95 } | |
| 96 | |
| 97 void OnStreamReady(HttpStreamFactoryImpl::Job* job, | |
| 98 const SSLConfig& used_ssl_config, | |
| 99 const ProxyInfo& used_proxy_info) { | |
| 100 job_controller_->OnStreamReady(job, used_ssl_config, used_proxy_info); | |
| 101 } | |
| 102 | |
| 103 bool IsJobControllerDeleted() { | |
| 104 return factory_->job_controller_set_.empty(); | |
| 105 } | |
| 106 | |
| 107 HttpStreamFactoryImpl::Job* alternative_job() { | |
| 108 return job_controller_->alternative_job_.get(); | |
| 109 } | |
| 110 | |
| 111 HttpStreamFactoryImpl::Job* main_job() { | |
| 112 return job_controller_->main_job_.get(); | |
| 113 } | |
| 114 | |
| 115 private: | |
| 116 SpdySessionDependencies session_deps_; | |
| 117 std::unique_ptr<HttpNetworkSession> session_; | |
| 118 HttpStreamFactoryImpl* factory_; | |
| 119 TestJobFactory job_factory_; | |
| 120 HttpStreamFactoryImpl::JobController* job_controller_; | |
| 121 std::unique_ptr<HttpStreamFactoryImpl::Request> request_; | |
| 122 | |
| 123 DISALLOW_COPY_AND_ASSIGN(TestJobControllerPeer); | |
|
Ryan Hamilton
2016/06/03 00:11:24
As discussed offline, I think you can eliminate th
Zhongyi Shi
2016/06/03 23:09:21
Done.
| |
| 124 }; | |
| 125 | |
| 126 class HttpStreamFactoryImplJobControllerTest | |
| 127 : public ::testing::Test, | |
| 128 public ::testing::WithParamInterface<NextProto> { | |
| 129 public: | |
| 130 HttpStreamFactoryImplJobControllerTest() | |
| 131 : job_controller_peer_(GetParam(), &request_delegate_) {} | |
| 132 | |
| 133 ~HttpStreamFactoryImplJobControllerTest() {} | |
| 134 | |
| 135 TestJobControllerPeer job_controller_peer_; | |
| 136 TestHttpStreamRequestDelegate request_delegate_; | |
| 137 }; | |
| 138 | |
| 139 INSTANTIATE_TEST_CASE_P(NextProto, | |
| 140 HttpStreamFactoryImplJobControllerTest, | |
| 141 testing::Values(kProtoSPDY31, kProtoHTTP2)); | |
| 142 | |
| 143 TEST_P(HttpStreamFactoryImplJobControllerTest, | |
| 144 OnStreamFailedWithNoAlternativeJob) { | |
| 145 HttpRequestInfo request_info; | |
| 146 request_info.method = "GET"; | |
| 147 request_info.url = GURL("http://www.google.com"); | |
| 148 TestHttpStreamFactoryImplJob* main_job = | |
| 149 job_controller_peer_.AddMainJobToFactory(request_info); | |
| 150 | |
| 151 EXPECT_CALL(*main_job, Start(_)).Times(1); | |
| 152 job_controller_peer_.Start(request_info, &request_delegate_, nullptr, | |
| 153 BoundNetLog(), DEFAULT_PRIORITY, SSLConfig(), | |
| 154 SSLConfig()); | |
| 155 EXPECT_TRUE(job_controller_peer_.main_job()); | |
| 156 | |
| 157 // There's no other alternative job. Thus when stream failed, it should | |
| 158 // notify Request of the stream failure. | |
| 159 EXPECT_CALL(request_delegate_, | |
| 160 OnStreamFailed(ERR_FAILED, _, SSL_FAILURE_NONE)) | |
| 161 .Times(1); | |
| 162 job_controller_peer_.OnStreamFailed(main_job, ERR_FAILED, SSLConfig(), | |
| 163 SSL_FAILURE_NONE); | |
| 164 } | |
| 165 | |
| 166 TEST_P(HttpStreamFactoryImplJobControllerTest, | |
| 167 OnStreamReadyWithNoAlternativeJob) { | |
| 168 HttpRequestInfo request_info; | |
| 169 request_info.method = "GET"; | |
| 170 request_info.url = GURL("http://www.google.com"); | |
| 171 TestHttpStreamFactoryImplJob* main_job = | |
| 172 job_controller_peer_.AddMainJobToFactory(request_info); | |
| 173 | |
| 174 EXPECT_CALL(*main_job, Start(_)).Times(1); | |
| 175 job_controller_peer_.Start(request_info, &request_delegate_, nullptr, | |
| 176 BoundNetLog(), DEFAULT_PRIORITY, SSLConfig(), | |
| 177 SSLConfig()); | |
| 178 EXPECT_TRUE(job_controller_peer_.main_job()); | |
| 179 | |
| 180 // There's no other alternative job. Thus when stream is ready, it should | |
| 181 // notify Request. | |
| 182 HttpStream* http_stream = | |
| 183 new HttpBasicStream(new ClientSocketHandle(), false); | |
| 184 main_job->SetStream(http_stream); | |
| 185 | |
| 186 EXPECT_CALL(request_delegate_, OnStreamReady(_, _, _)).Times(1); | |
| 187 job_controller_peer_.OnStreamReady(main_job, SSLConfig(), ProxyInfo()); | |
| 188 } | |
| 189 | |
| 190 // Test we cancel Jobs correctly when the Request is explicitly canceled | |
| 191 // before any Job is bound to Request. | |
| 192 TEST_P(HttpStreamFactoryImplJobControllerTest, CancelJobsBeforeBinding) { | |
| 193 HttpRequestInfo request_info; | |
| 194 request_info.method = "GET"; | |
| 195 request_info.url = GURL("https://www.google.com"); | |
| 196 TestHttpStreamFactoryImplJob* main_job = | |
| 197 job_controller_peer_.AddMainJobToFactory(request_info); | |
| 198 | |
| 199 url::SchemeHostPort server(request_info.url); | |
| 200 AlternativeService alternative_service(QUIC, server.host(), 443); | |
| 201 TestHttpStreamFactoryImplJob* alternative_job = | |
| 202 job_controller_peer_.AddAlternativeJobToFactory(request_info, | |
| 203 alternative_service); | |
| 204 | |
| 205 EXPECT_CALL(*main_job, Start(_)).Times(1); | |
| 206 EXPECT_CALL(*alternative_job, Start(_)).Times(1); | |
| 207 job_controller_peer_.Start(request_info, &request_delegate_, nullptr, | |
| 208 BoundNetLog(), DEFAULT_PRIORITY, SSLConfig(), | |
| 209 SSLConfig()); | |
| 210 EXPECT_TRUE(job_controller_peer_.main_job()); | |
| 211 EXPECT_TRUE(job_controller_peer_.alternative_job()); | |
| 212 | |
| 213 // Reset the Request will cancel all the Jobs since there's no Job determined | |
| 214 // to serve Request yet and JobController will notify the factory to delete | |
| 215 // itself upon completion. | |
| 216 job_controller_peer_.CancelRequest(); | |
| 217 EXPECT_TRUE(job_controller_peer_.IsJobControllerDeleted()); | |
| 218 } | |
| 219 | |
| 220 TEST_P(HttpStreamFactoryImplJobControllerTest, OnStreamFailedForBothJobs) { | |
| 221 HttpRequestInfo request_info; | |
| 222 request_info.method = "GET"; | |
| 223 request_info.url = GURL("https://www.google.com"); | |
| 224 url::SchemeHostPort server(request_info.url); | |
| 225 AlternativeService alternative_service(QUIC, server.host(), 443); | |
| 226 | |
| 227 TestHttpStreamFactoryImplJob* main_job = | |
| 228 job_controller_peer_.AddMainJobToFactory(request_info); | |
| 229 | |
| 230 TestHttpStreamFactoryImplJob* alternative_job = | |
| 231 job_controller_peer_.AddAlternativeJobToFactory(request_info, | |
| 232 alternative_service); | |
| 233 | |
| 234 EXPECT_CALL(*main_job, Start(_)).Times(1); | |
| 235 EXPECT_CALL(*alternative_job, Start(_)).Times(1); | |
| 236 | |
| 237 job_controller_peer_.Start(request_info, &request_delegate_, nullptr, | |
| 238 BoundNetLog(), DEFAULT_PRIORITY, SSLConfig(), | |
| 239 SSLConfig()); | |
| 240 EXPECT_TRUE(job_controller_peer_.main_job()); | |
| 241 EXPECT_TRUE(job_controller_peer_.alternative_job()); | |
| 242 | |
| 243 // We have |main_job| with unknown status when |alternative_job| is failed | |
| 244 // thus should not notify Request of |alternative_job|'s failure. But should | |
| 245 // notify |main_job| to mark |alternative_job| failed. | |
| 246 EXPECT_CALL(request_delegate_, OnStreamFailed(_, _, _)).Times(0); | |
| 247 EXPECT_CALL(*main_job, MarkOtherJobComplete(_)).Times(1); | |
| 248 job_controller_peer_.OnStreamFailed(alternative_job, ERR_FAILED, SSLConfig(), | |
| 249 SSL_FAILURE_NONE); | |
| 250 EXPECT_TRUE(!job_controller_peer_.alternative_job()); | |
| 251 EXPECT_TRUE(job_controller_peer_.main_job()); | |
| 252 | |
| 253 // The failure of second Job should be reported to Request as there's no more | |
| 254 // pending Job to serve the Request. | |
| 255 EXPECT_CALL(request_delegate_, OnStreamFailed(_, _, _)).Times(1); | |
| 256 job_controller_peer_.OnStreamFailed(main_job, ERR_FAILED, SSLConfig(), | |
| 257 SSL_FAILURE_NONE); | |
| 258 } | |
| 259 | |
| 260 TEST_P(HttpStreamFactoryImplJobControllerTest, | |
| 261 SecondJobFailsAfterFirstJobSucceeds) { | |
| 262 HttpRequestInfo request_info; | |
| 263 request_info.method = "GET"; | |
| 264 request_info.url = GURL("https://www.google.com"); | |
| 265 TestHttpStreamFactoryImplJob* main_job = | |
| 266 job_controller_peer_.AddMainJobToFactory(request_info); | |
| 267 | |
| 268 url::SchemeHostPort server(request_info.url); | |
| 269 AlternativeService alternative_service(QUIC, server.host(), 443); | |
| 270 TestHttpStreamFactoryImplJob* alternative_job = | |
| 271 job_controller_peer_.AddAlternativeJobToFactory(request_info, | |
| 272 alternative_service); | |
| 273 | |
| 274 EXPECT_CALL(*main_job, Start(_)).Times(1); | |
| 275 EXPECT_CALL(*alternative_job, Start(_)).Times(1); | |
| 276 job_controller_peer_.Start(request_info, &request_delegate_, nullptr, | |
| 277 BoundNetLog(), DEFAULT_PRIORITY, SSLConfig(), | |
| 278 SSLConfig()); | |
| 279 EXPECT_TRUE(job_controller_peer_.main_job()); | |
| 280 EXPECT_TRUE(job_controller_peer_.alternative_job()); | |
| 281 | |
| 282 // Main job succeeds, starts serving Request and it should report status | |
| 283 // to Request. The alternative job will mark the main job complete and gets | |
| 284 // orphaned. | |
| 285 HttpStream* http_stream = | |
| 286 new HttpBasicStream(new ClientSocketHandle(), false); | |
| 287 main_job->SetStream(http_stream); | |
| 288 | |
| 289 EXPECT_CALL(request_delegate_, OnStreamReady(_, _, _)).Times(1); | |
| 290 EXPECT_CALL(*alternative_job, MarkOtherJobComplete(_)).Times(1); | |
| 291 job_controller_peer_.OnStreamReady(main_job, SSLConfig(), ProxyInfo()); | |
| 292 | |
| 293 // Reset the request as it's been successfully served. | |
| 294 job_controller_peer_.CancelRequest(); | |
| 295 | |
| 296 // JobController shouldn't report the status of second job as request | |
| 297 // is gone served. | |
| 298 job_controller_peer_.OnStreamFailed(alternative_job, ERR_FAILED, SSLConfig(), | |
| 299 SSL_FAILURE_NONE); | |
| 300 EXPECT_TRUE(job_controller_peer_.IsJobControllerDeleted()); | |
| 301 delete http_stream; | |
| 302 } | |
| 303 | |
| 304 TEST_P(HttpStreamFactoryImplJobControllerTest, | |
| 305 SecondJobSucceedsAfterFirstJobFailed) { | |
| 306 HttpRequestInfo request_info; | |
| 307 request_info.method = "GET"; | |
| 308 request_info.url = GURL("https://www.google.com"); | |
| 309 TestHttpStreamFactoryImplJob* main_job = | |
| 310 job_controller_peer_.AddMainJobToFactory(request_info); | |
| 311 | |
| 312 url::SchemeHostPort server(request_info.url); | |
| 313 AlternativeService alternative_service(QUIC, server.host(), 443); | |
| 314 TestHttpStreamFactoryImplJob* alternative_job = | |
| 315 job_controller_peer_.AddAlternativeJobToFactory(request_info, | |
| 316 alternative_service); | |
| 317 | |
| 318 EXPECT_CALL(*main_job, Start(_)).Times(1); | |
| 319 EXPECT_CALL(*alternative_job, Start(_)).Times(1); | |
| 320 job_controller_peer_.Start(request_info, &request_delegate_, nullptr, | |
| 321 BoundNetLog(), DEFAULT_PRIORITY, SSLConfig(), | |
| 322 SSLConfig()); | |
| 323 EXPECT_TRUE(job_controller_peer_.main_job()); | |
| 324 EXPECT_TRUE(job_controller_peer_.alternative_job()); | |
| 325 | |
| 326 // |main_job| fails but should not report status to Request. | |
| 327 // The alternative job will mark the main job complete. | |
| 328 EXPECT_CALL(request_delegate_, OnStreamFailed(_, _, _)).Times(0); | |
| 329 EXPECT_CALL(*alternative_job, MarkOtherJobComplete(_)).Times(1); | |
| 330 | |
| 331 job_controller_peer_.OnStreamFailed(main_job, ERR_FAILED, SSLConfig(), | |
| 332 SSL_FAILURE_NONE); | |
| 333 | |
| 334 // |alternative_job| succeeds and should report status to Request. | |
| 335 HttpStream* http_stream = | |
| 336 new HttpBasicStream(new ClientSocketHandle(), false); | |
| 337 alternative_job->SetStream(http_stream); | |
| 338 | |
| 339 EXPECT_CALL(request_delegate_, OnStreamReady(_, _, _)).Times(1); | |
| 340 job_controller_peer_.OnStreamReady(alternative_job, SSLConfig(), ProxyInfo()); | |
| 341 | |
| 342 delete http_stream; | |
| 343 } | |
| 344 | |
| 345 } // namespace net | |
| OLD | NEW |