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