Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "net/proxy/mojo_proxy_resolver_impl.h" | 5 #include "net/proxy/mojo_proxy_resolver_impl.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 #include <utility> | 8 #include <utility> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| (...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 95 interfaces::HostResolverRequestInfoPtr request_info, | 95 interfaces::HostResolverRequestInfoPtr request_info, |
| 96 interfaces::HostResolverRequestClientPtr client) { | 96 interfaces::HostResolverRequestClientPtr client) { |
| 97 } | 97 } |
| 98 | 98 |
| 99 void TestRequestClient::OnConnectionError() { | 99 void TestRequestClient::OnConnectionError() { |
| 100 event_waiter_.NotifyEvent(CONNECTION_ERROR); | 100 event_waiter_.NotifyEvent(CONNECTION_ERROR); |
| 101 } | 101 } |
| 102 | 102 |
| 103 class MockProxyResolverV8Tracing : public ProxyResolverV8Tracing { | 103 class MockProxyResolverV8Tracing : public ProxyResolverV8Tracing { |
| 104 public: | 104 public: |
| 105 struct Request { | 105 struct Job { |
| 106 GURL url; | 106 GURL url; |
| 107 ProxyInfo* results; | 107 ProxyInfo* results; |
| 108 CompletionCallback callback; | 108 CompletionCallback callback; |
| 109 bool cancelled = false; | 109 bool cancelled = false; |
| 110 }; | 110 }; |
| 111 | |
| 112 class RequestImpl : public ProxyResolver::Request { | |
| 113 public: | |
| 114 RequestImpl(Job* job, MockProxyResolverV8Tracing* resolver) | |
| 115 : job_(job), resolver_(resolver) {} | |
| 116 | |
| 117 ~RequestImpl() override { | |
| 118 if (job_) { | |
|
eroman
2016/02/24 03:08:06
Is it ever the case that !job_ ?
From reading the
| |
| 119 job_->cancelled = true; | |
| 120 if (!resolver_->cancel_callback_.is_null()) { | |
| 121 resolver_->cancel_callback_.Run(); | |
| 122 resolver_->cancel_callback_.Reset(); | |
| 123 } | |
| 124 } | |
| 125 } | |
| 126 | |
| 127 LoadState GetLoadState() override { | |
| 128 return LOAD_STATE_RESOLVING_PROXY_FOR_URL; | |
| 129 } | |
| 130 | |
| 131 private: | |
| 132 Job* job_; | |
| 133 MockProxyResolverV8Tracing* resolver_; | |
| 134 }; | |
| 135 | |
| 111 MockProxyResolverV8Tracing() {} | 136 MockProxyResolverV8Tracing() {} |
| 112 | 137 |
| 113 // ProxyResolverV8Tracing overrides. | 138 // ProxyResolverV8Tracing overrides. |
| 114 void GetProxyForURL(const GURL& url, | 139 void GetProxyForURL(const GURL& url, |
| 115 ProxyInfo* results, | 140 ProxyInfo* results, |
| 116 const CompletionCallback& callback, | 141 const CompletionCallback& callback, |
| 117 ProxyResolver::RequestHandle* request, | 142 scoped_ptr<ProxyResolver::Request>* request, |
| 118 scoped_ptr<Bindings> bindings) override; | 143 scoped_ptr<Bindings> bindings) override; |
| 119 void CancelRequest(ProxyResolver::RequestHandle request_handle) override; | |
| 120 LoadState GetLoadState(ProxyResolver::RequestHandle request) const override; | |
| 121 | 144 |
| 122 // Wait until the mock resolver has received a CancelRequest call. | 145 // Wait until the mock resolver has received a CancelRequest call. |
| 123 void WaitForCancel(); | 146 void WaitForCancel(); |
| 124 | 147 |
| 125 const std::vector<Request>& pending_requests() { return pending_requests_; } | 148 const std::vector<scoped_ptr<Job>>& pending_jobs() { return pending_jobs_; } |
| 126 | 149 |
| 127 private: | 150 private: |
| 128 base::Closure cancel_callback_; | 151 base::Closure cancel_callback_; |
| 129 std::vector<Request> pending_requests_; | 152 std::vector<scoped_ptr<Job>> pending_jobs_; |
| 130 }; | 153 }; |
| 131 | 154 |
| 132 void MockProxyResolverV8Tracing::GetProxyForURL( | 155 void MockProxyResolverV8Tracing::GetProxyForURL( |
| 133 const GURL& url, | 156 const GURL& url, |
| 134 ProxyInfo* results, | 157 ProxyInfo* results, |
| 135 const CompletionCallback& callback, | 158 const CompletionCallback& callback, |
| 136 ProxyResolver::RequestHandle* request, | 159 scoped_ptr<ProxyResolver::Request>* request, |
| 137 scoped_ptr<Bindings> bindings) { | 160 scoped_ptr<Bindings> bindings) { |
| 138 pending_requests_.push_back(Request()); | 161 pending_jobs_.push_back(make_scoped_ptr(new Job())); |
| 139 auto& pending_request = pending_requests_.back(); | 162 auto pending_job = pending_jobs_.back().get(); |
| 140 pending_request.url = url; | 163 pending_job->url = url; |
| 141 pending_request.results = results; | 164 pending_job->results = results; |
| 142 pending_request.callback = callback; | 165 pending_job->callback = callback; |
| 143 *request = | 166 request->reset(new RequestImpl(pending_job, this)); |
| 144 reinterpret_cast<ProxyResolver::RequestHandle>(pending_requests_.size()); | |
| 145 } | 167 } |
| 146 | 168 |
| 147 void MockProxyResolverV8Tracing::CancelRequest( | |
| 148 ProxyResolver::RequestHandle request_handle) { | |
| 149 size_t id = reinterpret_cast<size_t>(request_handle) - 1; | |
| 150 pending_requests_[id].cancelled = true; | |
| 151 if (!cancel_callback_.is_null()) { | |
| 152 cancel_callback_.Run(); | |
| 153 cancel_callback_.Reset(); | |
| 154 } | |
| 155 } | |
| 156 | |
| 157 LoadState MockProxyResolverV8Tracing::GetLoadState( | |
| 158 ProxyResolver::RequestHandle request) const { | |
| 159 return LOAD_STATE_RESOLVING_PROXY_FOR_URL; | |
| 160 } | |
| 161 | 169 |
| 162 void MockProxyResolverV8Tracing::WaitForCancel() { | 170 void MockProxyResolverV8Tracing::WaitForCancel() { |
| 163 while (std::find_if(pending_requests_.begin(), pending_requests_.end(), | 171 while (std::find_if(pending_jobs_.begin(), pending_jobs_.end(), |
| 164 [](const Request& request) { | 172 [](const scoped_ptr<Job>& job) { |
| 165 return request.cancelled; | 173 return job->cancelled; |
| 166 }) != pending_requests_.end()) { | 174 }) != pending_jobs_.end()) { |
| 167 base::RunLoop run_loop; | 175 base::RunLoop run_loop; |
| 168 cancel_callback_ = run_loop.QuitClosure(); | 176 cancel_callback_ = run_loop.QuitClosure(); |
| 169 run_loop.Run(); | 177 run_loop.Run(); |
| 170 } | 178 } |
| 171 } | 179 } |
| 172 | 180 |
| 173 } // namespace | 181 } // namespace |
| 174 | 182 |
| 175 class MojoProxyResolverImplTest : public testing::Test { | 183 class MojoProxyResolverImplTest : public testing::Test { |
| 176 protected: | 184 protected: |
| 177 void SetUp() override { | 185 void SetUp() override { |
| 178 scoped_ptr<MockProxyResolverV8Tracing> mock_resolver( | 186 scoped_ptr<MockProxyResolverV8Tracing> mock_resolver( |
| 179 new MockProxyResolverV8Tracing); | 187 new MockProxyResolverV8Tracing); |
| 180 mock_proxy_resolver_ = mock_resolver.get(); | 188 mock_proxy_resolver_ = mock_resolver.get(); |
| 181 resolver_impl_.reset(new MojoProxyResolverImpl(std::move(mock_resolver))); | 189 resolver_impl_.reset(new MojoProxyResolverImpl(std::move(mock_resolver))); |
| 182 resolver_ = resolver_impl_.get(); | 190 resolver_ = resolver_impl_.get(); |
| 183 } | 191 } |
| 184 | 192 |
| 185 MockProxyResolverV8Tracing* mock_proxy_resolver_; | 193 MockProxyResolverV8Tracing* mock_proxy_resolver_; |
| 186 | 194 |
| 187 scoped_ptr<MojoProxyResolverImpl> resolver_impl_; | 195 scoped_ptr<MojoProxyResolverImpl> resolver_impl_; |
| 188 interfaces::ProxyResolver* resolver_; | 196 interfaces::ProxyResolver* resolver_; |
| 189 }; | 197 }; |
| 190 | 198 |
| 191 TEST_F(MojoProxyResolverImplTest, GetProxyForUrl) { | 199 TEST_F(MojoProxyResolverImplTest, GetProxyForUrl) { |
| 192 interfaces::ProxyResolverRequestClientPtr client_ptr; | 200 interfaces::ProxyResolverRequestClientPtr client_ptr; |
| 193 TestRequestClient client(mojo::GetProxy(&client_ptr)); | 201 TestRequestClient client(mojo::GetProxy(&client_ptr)); |
| 194 | 202 |
| 195 resolver_->GetProxyForUrl("http://example.com", std::move(client_ptr)); | 203 resolver_->GetProxyForUrl("http://example.com", std::move(client_ptr)); |
| 196 ASSERT_EQ(1u, mock_proxy_resolver_->pending_requests().size()); | 204 ASSERT_EQ(1u, mock_proxy_resolver_->pending_jobs().size()); |
| 197 const MockProxyResolverV8Tracing::Request& request = | 205 const MockProxyResolverV8Tracing::Job* job = |
| 198 mock_proxy_resolver_->pending_requests()[0]; | 206 mock_proxy_resolver_->pending_jobs()[0].get(); |
| 199 EXPECT_EQ(GURL("http://example.com"), request.url); | 207 EXPECT_EQ(GURL("http://example.com"), job->url); |
| 200 | 208 |
| 201 request.results->UsePacString( | 209 job->results->UsePacString( |
| 202 "PROXY proxy.example.com:1; " | 210 "PROXY proxy.example.com:1; " |
| 203 "SOCKS4 socks4.example.com:2; " | 211 "SOCKS4 socks4.example.com:2; " |
| 204 "SOCKS5 socks5.example.com:3; " | 212 "SOCKS5 socks5.example.com:3; " |
| 205 "HTTPS https.example.com:4; " | 213 "HTTPS https.example.com:4; " |
| 206 "QUIC quic.example.com:65000; " | 214 "QUIC quic.example.com:65000; " |
| 207 "DIRECT"); | 215 "DIRECT"); |
| 208 request.callback.Run(OK); | 216 job->callback.Run(OK); |
| 209 client.WaitForResult(); | 217 client.WaitForResult(); |
| 210 | 218 |
| 211 EXPECT_EQ(OK, client.error()); | 219 EXPECT_EQ(OK, client.error()); |
| 212 std::vector<ProxyServer> servers = | 220 std::vector<ProxyServer> servers = |
| 213 client.results().To<std::vector<ProxyServer>>(); | 221 client.results().To<std::vector<ProxyServer>>(); |
| 214 ASSERT_EQ(6u, servers.size()); | 222 ASSERT_EQ(6u, servers.size()); |
| 215 EXPECT_EQ(ProxyServer::SCHEME_HTTP, servers[0].scheme()); | 223 EXPECT_EQ(ProxyServer::SCHEME_HTTP, servers[0].scheme()); |
| 216 EXPECT_EQ("proxy.example.com", servers[0].host_port_pair().host()); | 224 EXPECT_EQ("proxy.example.com", servers[0].host_port_pair().host()); |
| 217 EXPECT_EQ(1, servers[0].host_port_pair().port()); | 225 EXPECT_EQ(1, servers[0].host_port_pair().port()); |
| 218 | 226 |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 233 EXPECT_EQ(65000, servers[4].host_port_pair().port()); | 241 EXPECT_EQ(65000, servers[4].host_port_pair().port()); |
| 234 | 242 |
| 235 EXPECT_EQ(ProxyServer::SCHEME_DIRECT, servers[5].scheme()); | 243 EXPECT_EQ(ProxyServer::SCHEME_DIRECT, servers[5].scheme()); |
| 236 } | 244 } |
| 237 | 245 |
| 238 TEST_F(MojoProxyResolverImplTest, GetProxyForUrlFailure) { | 246 TEST_F(MojoProxyResolverImplTest, GetProxyForUrlFailure) { |
| 239 interfaces::ProxyResolverRequestClientPtr client_ptr; | 247 interfaces::ProxyResolverRequestClientPtr client_ptr; |
| 240 TestRequestClient client(mojo::GetProxy(&client_ptr)); | 248 TestRequestClient client(mojo::GetProxy(&client_ptr)); |
| 241 | 249 |
| 242 resolver_->GetProxyForUrl("http://example.com", std::move(client_ptr)); | 250 resolver_->GetProxyForUrl("http://example.com", std::move(client_ptr)); |
| 243 ASSERT_EQ(1u, mock_proxy_resolver_->pending_requests().size()); | 251 ASSERT_EQ(1u, mock_proxy_resolver_->pending_jobs().size()); |
| 244 const MockProxyResolverV8Tracing::Request& request = | 252 const MockProxyResolverV8Tracing::Job* job = |
| 245 mock_proxy_resolver_->pending_requests()[0]; | 253 mock_proxy_resolver_->pending_jobs()[0].get(); |
| 246 EXPECT_EQ(GURL("http://example.com"), request.url); | 254 EXPECT_EQ(GURL("http://example.com"), job->url); |
| 247 request.callback.Run(ERR_FAILED); | 255 job->callback.Run(ERR_FAILED); |
| 248 client.WaitForResult(); | 256 client.WaitForResult(); |
| 249 | 257 |
| 250 EXPECT_EQ(ERR_FAILED, client.error()); | 258 EXPECT_EQ(ERR_FAILED, client.error()); |
| 251 std::vector<ProxyServer> proxy_servers = | 259 std::vector<ProxyServer> proxy_servers = |
| 252 client.results().To<std::vector<ProxyServer>>(); | 260 client.results().To<std::vector<ProxyServer>>(); |
| 253 EXPECT_TRUE(proxy_servers.empty()); | 261 EXPECT_TRUE(proxy_servers.empty()); |
| 254 } | 262 } |
| 255 | 263 |
| 256 TEST_F(MojoProxyResolverImplTest, GetProxyForUrlMultiple) { | 264 TEST_F(MojoProxyResolverImplTest, GetProxyForUrlMultiple) { |
| 257 interfaces::ProxyResolverRequestClientPtr client_ptr1; | 265 interfaces::ProxyResolverRequestClientPtr client_ptr1; |
| 258 TestRequestClient client1(mojo::GetProxy(&client_ptr1)); | 266 TestRequestClient client1(mojo::GetProxy(&client_ptr1)); |
| 259 interfaces::ProxyResolverRequestClientPtr client_ptr2; | 267 interfaces::ProxyResolverRequestClientPtr client_ptr2; |
| 260 TestRequestClient client2(mojo::GetProxy(&client_ptr2)); | 268 TestRequestClient client2(mojo::GetProxy(&client_ptr2)); |
| 261 | 269 |
| 262 resolver_->GetProxyForUrl("http://example.com", std::move(client_ptr1)); | 270 resolver_->GetProxyForUrl("http://example.com", std::move(client_ptr1)); |
| 263 resolver_->GetProxyForUrl("https://example.com", std::move(client_ptr2)); | 271 resolver_->GetProxyForUrl("https://example.com", std::move(client_ptr2)); |
| 264 ASSERT_EQ(2u, mock_proxy_resolver_->pending_requests().size()); | 272 ASSERT_EQ(2u, mock_proxy_resolver_->pending_jobs().size()); |
| 265 const MockProxyResolverV8Tracing::Request& request1 = | 273 const MockProxyResolverV8Tracing::Job* job1 = |
| 266 mock_proxy_resolver_->pending_requests()[0]; | 274 mock_proxy_resolver_->pending_jobs()[0].get(); |
| 267 EXPECT_EQ(GURL("http://example.com"), request1.url); | 275 EXPECT_EQ(GURL("http://example.com"), job1->url); |
| 268 const MockProxyResolverV8Tracing::Request& request2 = | 276 const MockProxyResolverV8Tracing::Job* job2 = |
| 269 mock_proxy_resolver_->pending_requests()[1]; | 277 mock_proxy_resolver_->pending_jobs()[1].get(); |
| 270 EXPECT_EQ(GURL("https://example.com"), request2.url); | 278 EXPECT_EQ(GURL("https://example.com"), job2->url); |
| 271 request1.results->UsePacString("HTTPS proxy.example.com:12345"); | 279 job1->results->UsePacString("HTTPS proxy.example.com:12345"); |
| 272 request1.callback.Run(OK); | 280 job1->callback.Run(OK); |
| 273 request2.results->UsePacString("SOCKS5 another-proxy.example.com:6789"); | 281 job2->results->UsePacString("SOCKS5 another-proxy.example.com:6789"); |
| 274 request2.callback.Run(OK); | 282 job2->callback.Run(OK); |
| 275 client1.WaitForResult(); | 283 client1.WaitForResult(); |
| 276 client2.WaitForResult(); | 284 client2.WaitForResult(); |
| 277 | 285 |
| 278 EXPECT_EQ(OK, client1.error()); | 286 EXPECT_EQ(OK, client1.error()); |
| 279 std::vector<ProxyServer> proxy_servers1 = | 287 std::vector<ProxyServer> proxy_servers1 = |
| 280 client1.results().To<std::vector<ProxyServer>>(); | 288 client1.results().To<std::vector<ProxyServer>>(); |
| 281 ASSERT_EQ(1u, proxy_servers1.size()); | 289 ASSERT_EQ(1u, proxy_servers1.size()); |
| 282 ProxyServer& server1 = proxy_servers1[0]; | 290 ProxyServer& server1 = proxy_servers1[0]; |
| 283 EXPECT_EQ(ProxyServer::SCHEME_HTTPS, server1.scheme()); | 291 EXPECT_EQ(ProxyServer::SCHEME_HTTPS, server1.scheme()); |
| 284 EXPECT_EQ("proxy.example.com", server1.host_port_pair().host()); | 292 EXPECT_EQ("proxy.example.com", server1.host_port_pair().host()); |
| 285 EXPECT_EQ(12345, server1.host_port_pair().port()); | 293 EXPECT_EQ(12345, server1.host_port_pair().port()); |
| 286 | 294 |
| 287 EXPECT_EQ(OK, client2.error()); | 295 EXPECT_EQ(OK, client2.error()); |
| 288 std::vector<ProxyServer> proxy_servers2 = | 296 std::vector<ProxyServer> proxy_servers2 = |
| 289 client2.results().To<std::vector<ProxyServer>>(); | 297 client2.results().To<std::vector<ProxyServer>>(); |
| 290 ASSERT_EQ(1u, proxy_servers1.size()); | 298 ASSERT_EQ(1u, proxy_servers1.size()); |
| 291 ProxyServer& server2 = proxy_servers2[0]; | 299 ProxyServer& server2 = proxy_servers2[0]; |
| 292 EXPECT_EQ(ProxyServer::SCHEME_SOCKS5, server2.scheme()); | 300 EXPECT_EQ(ProxyServer::SCHEME_SOCKS5, server2.scheme()); |
| 293 EXPECT_EQ("another-proxy.example.com", server2.host_port_pair().host()); | 301 EXPECT_EQ("another-proxy.example.com", server2.host_port_pair().host()); |
| 294 EXPECT_EQ(6789, server2.host_port_pair().port()); | 302 EXPECT_EQ(6789, server2.host_port_pair().port()); |
| 295 } | 303 } |
| 296 | 304 |
| 297 TEST_F(MojoProxyResolverImplTest, DestroyClient) { | 305 TEST_F(MojoProxyResolverImplTest, DestroyClient) { |
| 298 interfaces::ProxyResolverRequestClientPtr client_ptr; | 306 interfaces::ProxyResolverRequestClientPtr client_ptr; |
| 299 scoped_ptr<TestRequestClient> client( | 307 scoped_ptr<TestRequestClient> client( |
| 300 new TestRequestClient(mojo::GetProxy(&client_ptr))); | 308 new TestRequestClient(mojo::GetProxy(&client_ptr))); |
| 301 | 309 |
| 302 resolver_->GetProxyForUrl("http://example.com", std::move(client_ptr)); | 310 resolver_->GetProxyForUrl("http://example.com", std::move(client_ptr)); |
| 303 ASSERT_EQ(1u, mock_proxy_resolver_->pending_requests().size()); | 311 ASSERT_EQ(1u, mock_proxy_resolver_->pending_jobs().size()); |
| 304 const MockProxyResolverV8Tracing::Request& request = | 312 const MockProxyResolverV8Tracing::Job* job = |
| 305 mock_proxy_resolver_->pending_requests()[0]; | 313 mock_proxy_resolver_->pending_jobs()[0].get(); |
| 306 EXPECT_EQ(GURL("http://example.com"), request.url); | 314 EXPECT_EQ(GURL("http://example.com"), job->url); |
| 307 request.results->UsePacString("PROXY proxy.example.com:8080"); | 315 job->results->UsePacString("PROXY proxy.example.com:8080"); |
| 308 client.reset(); | 316 client.reset(); |
| 309 mock_proxy_resolver_->WaitForCancel(); | 317 mock_proxy_resolver_->WaitForCancel(); |
| 310 } | 318 } |
| 311 | 319 |
| 312 TEST_F(MojoProxyResolverImplTest, DestroyService) { | 320 TEST_F(MojoProxyResolverImplTest, DestroyService) { |
| 313 interfaces::ProxyResolverRequestClientPtr client_ptr; | 321 interfaces::ProxyResolverRequestClientPtr client_ptr; |
| 314 TestRequestClient client(mojo::GetProxy(&client_ptr)); | 322 TestRequestClient client(mojo::GetProxy(&client_ptr)); |
| 315 | 323 |
| 316 resolver_->GetProxyForUrl("http://example.com", std::move(client_ptr)); | 324 resolver_->GetProxyForUrl("http://example.com", std::move(client_ptr)); |
| 317 ASSERT_EQ(1u, mock_proxy_resolver_->pending_requests().size()); | 325 ASSERT_EQ(1u, mock_proxy_resolver_->pending_jobs().size()); |
| 318 resolver_impl_.reset(); | 326 resolver_impl_.reset(); |
| 319 client.event_waiter().WaitForEvent(TestRequestClient::CONNECTION_ERROR); | 327 client.event_waiter().WaitForEvent(TestRequestClient::CONNECTION_ERROR); |
| 320 } | 328 } |
| 321 | 329 |
| 322 } // namespace net | 330 } // namespace net |
| OLD | NEW |