| 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 using ::testing::Invoke; |
| 21 |
| 22 namespace net { |
| 23 |
| 24 void DeleteHttpStreamPointer(const SSLConfig& used_ssl_config, |
| 25 const ProxyInfo& used_proxy_info, |
| 26 HttpStream* stream) { |
| 27 delete stream; |
| 28 } |
| 29 |
| 30 class HttpStreamFactoryImplJobControllerTest |
| 31 : public ::testing::Test, |
| 32 public ::testing::WithParamInterface<NextProto> { |
| 33 public: |
| 34 HttpStreamFactoryImplJobControllerTest() |
| 35 : session_deps_(GetParam(), ProxyService::CreateDirect()) { |
| 36 session_deps_.enable_quic = true; |
| 37 session_ = SpdySessionDependencies::SpdyCreateSession(&session_deps_); |
| 38 factory_ = |
| 39 static_cast<HttpStreamFactoryImpl*>(session_->http_stream_factory()); |
| 40 job_controller_ = new HttpStreamFactoryImpl::JobController( |
| 41 factory_, &request_delegate_, session_.get(), &job_factory_); |
| 42 factory_->job_controller_set_.insert(base::WrapUnique(job_controller_)); |
| 43 } |
| 44 |
| 45 ~HttpStreamFactoryImplJobControllerTest() {} |
| 46 |
| 47 void SetAlternativeService(const HttpRequestInfo& request_info, |
| 48 AlternativeService alternative_service) { |
| 49 HostPortPair host_port_pair = HostPortPair::FromURL(request_info.url); |
| 50 url::SchemeHostPort server(request_info.url); |
| 51 base::Time expiration = base::Time::Now() + base::TimeDelta::FromDays(1); |
| 52 session_->http_server_properties()->SetAlternativeService( |
| 53 server, alternative_service, expiration); |
| 54 } |
| 55 |
| 56 bool IsJobControllerDeleted() { |
| 57 return factory_->job_controller_set_.empty(); |
| 58 } |
| 59 |
| 60 TestJobFactory job_factory_; |
| 61 MockHttpStreamRequestDelegate request_delegate_; |
| 62 SpdySessionDependencies session_deps_; |
| 63 std::unique_ptr<HttpNetworkSession> session_; |
| 64 HttpStreamFactoryImpl* factory_; |
| 65 HttpStreamFactoryImpl::JobController* job_controller_; |
| 66 std::unique_ptr<HttpStreamFactoryImpl::Request> request_; |
| 67 |
| 68 DISALLOW_COPY_AND_ASSIGN(HttpStreamFactoryImplJobControllerTest); |
| 69 }; |
| 70 |
| 71 INSTANTIATE_TEST_CASE_P(NextProto, |
| 72 HttpStreamFactoryImplJobControllerTest, |
| 73 testing::Values(kProtoSPDY31, kProtoHTTP2)); |
| 74 |
| 75 TEST_P(HttpStreamFactoryImplJobControllerTest, |
| 76 OnStreamFailedWithNoAlternativeJob) { |
| 77 HttpRequestInfo request_info; |
| 78 request_info.method = "GET"; |
| 79 request_info.url = GURL("http://www.google.com"); |
| 80 |
| 81 request_.reset( |
| 82 job_controller_->Start(request_info, &request_delegate_, nullptr, |
| 83 BoundNetLog(), HttpStreamRequest::HTTP_STREAM, |
| 84 DEFAULT_PRIORITY, SSLConfig(), SSLConfig())); |
| 85 |
| 86 EXPECT_TRUE(job_controller_->main_job()); |
| 87 |
| 88 // There's no other alternative job. Thus when stream failed, it should |
| 89 // notify Request of the stream failure. |
| 90 EXPECT_CALL(request_delegate_, |
| 91 OnStreamFailed(ERR_FAILED, _, SSL_FAILURE_NONE)) |
| 92 .Times(1); |
| 93 job_controller_->OnStreamFailed(job_factory_.main_job(), ERR_FAILED, |
| 94 SSLConfig(), SSL_FAILURE_NONE); |
| 95 } |
| 96 |
| 97 TEST_P(HttpStreamFactoryImplJobControllerTest, |
| 98 OnStreamReadyWithNoAlternativeJob) { |
| 99 HttpRequestInfo request_info; |
| 100 request_info.method = "GET"; |
| 101 request_info.url = GURL("http://www.google.com"); |
| 102 |
| 103 request_.reset( |
| 104 job_controller_->Start(request_info, &request_delegate_, nullptr, |
| 105 BoundNetLog(), HttpStreamRequest::HTTP_STREAM, |
| 106 DEFAULT_PRIORITY, SSLConfig(), SSLConfig())); |
| 107 EXPECT_TRUE(job_controller_->main_job()); |
| 108 |
| 109 // There's no other alternative job. Thus when stream is ready, it should |
| 110 // notify Request. |
| 111 HttpStream* http_stream = |
| 112 new HttpBasicStream(new ClientSocketHandle(), false); |
| 113 job_factory_.main_job()->SetStream(http_stream); |
| 114 |
| 115 EXPECT_CALL(request_delegate_, OnStreamReady(_, _, http_stream)) |
| 116 .WillOnce(Invoke(DeleteHttpStreamPointer)); |
| 117 job_controller_->OnStreamReady(job_factory_.main_job(), SSLConfig(), |
| 118 ProxyInfo()); |
| 119 } |
| 120 |
| 121 // Test we cancel Jobs correctly when the Request is explicitly canceled |
| 122 // before any Job is bound to Request. |
| 123 TEST_P(HttpStreamFactoryImplJobControllerTest, CancelJobsBeforeBinding) { |
| 124 HttpRequestInfo request_info; |
| 125 request_info.method = "GET"; |
| 126 request_info.url = GURL("https://www.google.com"); |
| 127 |
| 128 url::SchemeHostPort server(request_info.url); |
| 129 AlternativeService alternative_service(QUIC, server.host(), 443); |
| 130 SetAlternativeService(request_info, alternative_service); |
| 131 |
| 132 request_.reset( |
| 133 job_controller_->Start(request_info, &request_delegate_, nullptr, |
| 134 BoundNetLog(), HttpStreamRequest::HTTP_STREAM, |
| 135 DEFAULT_PRIORITY, SSLConfig(), SSLConfig())); |
| 136 EXPECT_TRUE(job_controller_->main_job()); |
| 137 EXPECT_TRUE(job_controller_->alternative_job()); |
| 138 |
| 139 // Reset the Request will cancel all the Jobs since there's no Job determined |
| 140 // to serve Request yet and JobController will notify the factory to delete |
| 141 // itself upon completion. |
| 142 request_.reset(); |
| 143 EXPECT_TRUE(IsJobControllerDeleted()); |
| 144 } |
| 145 |
| 146 TEST_P(HttpStreamFactoryImplJobControllerTest, OnStreamFailedForBothJobs) { |
| 147 HttpRequestInfo request_info; |
| 148 request_info.method = "GET"; |
| 149 request_info.url = GURL("https://www.google.com"); |
| 150 |
| 151 url::SchemeHostPort server(request_info.url); |
| 152 AlternativeService alternative_service(QUIC, server.host(), 443); |
| 153 SetAlternativeService(request_info, alternative_service); |
| 154 |
| 155 request_.reset( |
| 156 job_controller_->Start(request_info, &request_delegate_, nullptr, |
| 157 BoundNetLog(), HttpStreamRequest::HTTP_STREAM, |
| 158 DEFAULT_PRIORITY, SSLConfig(), SSLConfig())); |
| 159 EXPECT_TRUE(job_controller_->main_job()); |
| 160 EXPECT_TRUE(job_controller_->alternative_job()); |
| 161 |
| 162 // We have the main job with unknown status when the alternative job is failed |
| 163 // thus should not notify Request of the alternative job's failure. But should |
| 164 // notify the main job to mark the alternative job failed. |
| 165 EXPECT_CALL(request_delegate_, OnStreamFailed(_, _, _)).Times(0); |
| 166 EXPECT_CALL(*job_factory_.main_job(), MarkOtherJobComplete(_)).Times(1); |
| 167 job_controller_->OnStreamFailed(job_factory_.alternative_job(), ERR_FAILED, |
| 168 SSLConfig(), SSL_FAILURE_NONE); |
| 169 EXPECT_TRUE(!job_controller_->alternative_job()); |
| 170 EXPECT_TRUE(job_controller_->main_job()); |
| 171 |
| 172 // The failure of second Job should be reported to Request as there's no more |
| 173 // pending Job to serve the Request. |
| 174 EXPECT_CALL(request_delegate_, OnStreamFailed(_, _, SSL_FAILURE_UNKNOWN)) |
| 175 .Times(1); |
| 176 job_controller_->OnStreamFailed(job_factory_.main_job(), ERR_FAILED, |
| 177 SSLConfig(), SSL_FAILURE_UNKNOWN); |
| 178 } |
| 179 |
| 180 TEST_P(HttpStreamFactoryImplJobControllerTest, |
| 181 SecondJobFailsAfterFirstJobSucceeds) { |
| 182 HttpRequestInfo request_info; |
| 183 request_info.method = "GET"; |
| 184 request_info.url = GURL("https://www.google.com"); |
| 185 |
| 186 url::SchemeHostPort server(request_info.url); |
| 187 AlternativeService alternative_service(QUIC, server.host(), 443); |
| 188 SetAlternativeService(request_info, alternative_service); |
| 189 |
| 190 request_.reset( |
| 191 job_controller_->Start(request_info, &request_delegate_, nullptr, |
| 192 BoundNetLog(), HttpStreamRequest::HTTP_STREAM, |
| 193 DEFAULT_PRIORITY, SSLConfig(), SSLConfig())); |
| 194 EXPECT_TRUE(job_controller_->main_job()); |
| 195 EXPECT_TRUE(job_controller_->alternative_job()); |
| 196 |
| 197 // Main job succeeds, starts serving Request and it should report status |
| 198 // to Request. The alternative job will mark the main job complete and gets |
| 199 // orphaned. |
| 200 HttpStream* http_stream = |
| 201 new HttpBasicStream(new ClientSocketHandle(), false); |
| 202 job_factory_.main_job()->SetStream(http_stream); |
| 203 |
| 204 EXPECT_CALL(request_delegate_, OnStreamReady(_, _, http_stream)) |
| 205 .WillOnce(Invoke(DeleteHttpStreamPointer)); |
| 206 EXPECT_CALL(*job_factory_.alternative_job(), MarkOtherJobComplete(_)) |
| 207 .Times(1); |
| 208 job_controller_->OnStreamReady(job_factory_.main_job(), SSLConfig(), |
| 209 ProxyInfo()); |
| 210 |
| 211 // JobController shouldn't report the status of second job as request |
| 212 // is already successfully served. |
| 213 EXPECT_CALL(request_delegate_, OnStreamFailed(_, _, _)).Times(0); |
| 214 job_controller_->OnStreamFailed(job_factory_.alternative_job(), ERR_FAILED, |
| 215 SSLConfig(), SSL_FAILURE_NONE); |
| 216 |
| 217 // Reset the request as it's been successfully served. |
| 218 request_.reset(); |
| 219 EXPECT_TRUE(IsJobControllerDeleted()); |
| 220 } |
| 221 |
| 222 TEST_P(HttpStreamFactoryImplJobControllerTest, |
| 223 SecondJobSucceedsAfterFirstJobFailed) { |
| 224 HttpRequestInfo request_info; |
| 225 request_info.method = "GET"; |
| 226 request_info.url = GURL("https://www.google.com"); |
| 227 |
| 228 url::SchemeHostPort server(request_info.url); |
| 229 AlternativeService alternative_service(QUIC, server.host(), 443); |
| 230 SetAlternativeService(request_info, alternative_service); |
| 231 |
| 232 request_.reset( |
| 233 job_controller_->Start(request_info, &request_delegate_, nullptr, |
| 234 BoundNetLog(), HttpStreamRequest::HTTP_STREAM, |
| 235 DEFAULT_PRIORITY, SSLConfig(), SSLConfig())); |
| 236 EXPECT_TRUE(job_controller_->main_job()); |
| 237 EXPECT_TRUE(job_controller_->alternative_job()); |
| 238 |
| 239 // |main_job| fails but should not report status to Request. |
| 240 // The alternative job will mark the main job complete. |
| 241 EXPECT_CALL(request_delegate_, OnStreamFailed(_, _, _)).Times(0); |
| 242 EXPECT_CALL(*job_factory_.alternative_job(), MarkOtherJobComplete(_)) |
| 243 .Times(1); |
| 244 |
| 245 job_controller_->OnStreamFailed(job_factory_.main_job(), ERR_FAILED, |
| 246 SSLConfig(), SSL_FAILURE_NONE); |
| 247 |
| 248 // |alternative_job| succeeds and should report status to Request. |
| 249 HttpStream* http_stream = |
| 250 new HttpBasicStream(new ClientSocketHandle(), false); |
| 251 job_factory_.alternative_job()->SetStream(http_stream); |
| 252 |
| 253 EXPECT_CALL(request_delegate_, OnStreamReady(_, _, http_stream)) |
| 254 .WillOnce(Invoke(DeleteHttpStreamPointer)); |
| 255 job_controller_->OnStreamReady(job_factory_.alternative_job(), SSLConfig(), |
| 256 ProxyInfo()); |
| 257 } |
| 258 |
| 259 } // namespace net |
| OLD | NEW |