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