| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "chromeos/dbus/services/proxy_resolution_service_provider.h" | 5 #include "chromeos/dbus/services/proxy_resolution_service_provider.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/macros.h" | 8 #include "base/macros.h" |
| 9 #include "base/memory/ptr_util.h" | 9 #include "base/memory/ptr_util.h" |
| 10 #include "base/run_loop.h" | 10 #include "base/run_loop.h" |
| 11 #include "base/single_thread_task_runner.h" | 11 #include "base/single_thread_task_runner.h" |
| 12 #include "base/threading/thread_task_runner_handle.h" | 12 #include "base/threading/thread_task_runner_handle.h" |
| 13 #include "chromeos/dbus/services/service_provider_test_helper.h" | 13 #include "chromeos/dbus/services/service_provider_test_helper.h" |
| 14 #include "dbus/message.h" | 14 #include "dbus/message.h" |
| 15 #include "net/base/net_errors.h" | 15 #include "net/base/net_errors.h" |
| 16 #include "net/proxy/mock_proxy_resolver.h" |
| 17 #include "net/proxy/proxy_config_service_fixed.h" |
| 18 #include "net/proxy/proxy_info.h" |
| 19 #include "net/proxy/proxy_resolver.h" |
| 20 #include "net/url_request/url_request_context.h" |
| 16 #include "net/url_request/url_request_test_util.h" | 21 #include "net/url_request/url_request_test_util.h" |
| 17 #include "third_party/cros_system_api/dbus/service_constants.h" | 22 #include "third_party/cros_system_api/dbus/service_constants.h" |
| 18 | 23 |
| 19 namespace chromeos { | 24 namespace chromeos { |
| 20 | 25 |
| 21 namespace { | 26 namespace { |
| 22 | 27 |
| 23 // ProxyResolutionServiceProvider will return the proxy info as a D-Bus | 28 // ProxyResolutionServiceProvider will return the proxy info as a D-Bus |
| 24 // signal, to the following signal interface and the signal name. | 29 // signal, to the following signal interface and the signal name. |
| 25 const char kReturnSignalInterface[] = "org.chromium.TestInterface"; | 30 const char kReturnSignalInterface[] = "org.chromium.TestInterface"; |
| 26 const char kReturnSignalName[] = "TestSignal"; | 31 const char kReturnSignalName[] = "TestSignal"; |
| 27 | 32 |
| 33 // Trivial net::ProxyResolver implementation that returns canned data either |
| 34 // synchronously or asynchronously. |
| 35 class TestProxyResolver : public net::ProxyResolver { |
| 36 public: |
| 37 explicit TestProxyResolver( |
| 38 scoped_refptr<base::SingleThreadTaskRunner> network_task_runner) |
| 39 : network_task_runner_(network_task_runner) { |
| 40 proxy_info_.UseDirect(); |
| 41 } |
| 42 ~TestProxyResolver() override = default; |
| 43 |
| 44 const net::ProxyInfo& proxy_info() const { return proxy_info_; } |
| 45 net::ProxyInfo* mutable_proxy_info() { return &proxy_info_; } |
| 46 |
| 47 void set_async(bool async) { async_ = async; } |
| 48 |
| 49 // net::ProxyResolver: |
| 50 int GetProxyForURL(const GURL& url, |
| 51 net::ProxyInfo* results, |
| 52 const net::CompletionCallback& callback, |
| 53 std::unique_ptr<Request>* request, |
| 54 const net::NetLogWithSource& net_log) override { |
| 55 CHECK(network_task_runner_->BelongsToCurrentThread()); |
| 56 results->Use(proxy_info_); |
| 57 if (!async_) |
| 58 return net::OK; |
| 59 |
| 60 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 61 FROM_HERE, base::Bind(callback, net::OK)); |
| 62 return net::ERR_IO_PENDING; |
| 63 } |
| 64 |
| 65 private: |
| 66 scoped_refptr<base::SingleThreadTaskRunner> network_task_runner_; |
| 67 |
| 68 // Proxy info for GetProxyForURL() to return. |
| 69 net::ProxyInfo proxy_info_; |
| 70 |
| 71 // If true, GetProxyForURL() replies asynchronously rather than synchronously. |
| 72 bool async_ = false; |
| 73 |
| 74 DISALLOW_COPY_AND_ASSIGN(TestProxyResolver); |
| 75 }; |
| 76 |
| 77 // Trivial net::ProxyResolverFactory implementation that synchronously creates |
| 78 // net::ForwardingProxyResolvers that forward to a single passed-in resolver. |
| 79 class TestProxyResolverFactory : public net::ProxyResolverFactory { |
| 80 public: |
| 81 // Ownership of |resolver| remains with the caller. |resolver| must outlive |
| 82 // the forwarding resolvers returned by CreateProxyResolver(). |
| 83 explicit TestProxyResolverFactory(net::ProxyResolver* resolver) |
| 84 : net::ProxyResolverFactory(false /* expects_pac_bytes */), |
| 85 resolver_(resolver) {} |
| 86 ~TestProxyResolverFactory() override = default; |
| 87 |
| 88 // net::ProxyResolverFactory: |
| 89 int CreateProxyResolver( |
| 90 const scoped_refptr<net::ProxyResolverScriptData>& pac_script, |
| 91 std::unique_ptr<net::ProxyResolver>* resolver, |
| 92 const net::CompletionCallback& callback, |
| 93 std::unique_ptr<Request>* request) override { |
| 94 *resolver = base::MakeUnique<net::ForwardingProxyResolver>(resolver_); |
| 95 return net::OK; |
| 96 } |
| 97 |
| 98 private: |
| 99 net::ProxyResolver* resolver_; // Not owned. |
| 100 |
| 101 DISALLOW_COPY_AND_ASSIGN(TestProxyResolverFactory); |
| 102 }; |
| 103 |
| 28 // Test ProxyResolutionServiceProvider::Delegate implementation. | 104 // Test ProxyResolutionServiceProvider::Delegate implementation. |
| 29 class TestDelegate : public ProxyResolutionServiceProvider::Delegate { | 105 class TestDelegate : public ProxyResolutionServiceProvider::Delegate { |
| 30 public: | 106 public: |
| 31 explicit TestDelegate( | 107 explicit TestDelegate( |
| 32 const scoped_refptr<base::SingleThreadTaskRunner>& network_task_runner) | 108 const scoped_refptr<base::SingleThreadTaskRunner>& network_task_runner, |
| 33 : request_context_getter_( | 109 net::ProxyResolver* proxy_resolver) |
| 110 : proxy_resolver_(proxy_resolver), |
| 111 context_getter_( |
| 34 new net::TestURLRequestContextGetter(network_task_runner)) { | 112 new net::TestURLRequestContextGetter(network_task_runner)) { |
| 35 // Use direct connections by default. | 113 // The config's autodetect property needs to be set in order for |
| 36 proxy_info_.UseDirect(); | 114 // net::ProxyService to send requests to our resolver. |
| 115 net::ProxyConfig config = net::ProxyConfig::CreateAutoDetect(); |
| 116 proxy_service_ = base::MakeUnique<net::ProxyService>( |
| 117 base::MakeUnique<net::ProxyConfigServiceFixed>(config), |
| 118 base::MakeUnique<TestProxyResolverFactory>(proxy_resolver_), |
| 119 nullptr /* net_log */); |
| 120 context_getter_->GetURLRequestContext()->set_proxy_service( |
| 121 proxy_service_.get()); |
| 37 } | 122 } |
| 38 ~TestDelegate() override = default; | 123 ~TestDelegate() override = default; |
| 39 | 124 |
| 40 void set_async(bool async) { async_ = async; } | |
| 41 void set_result(net::Error result) { result_ = result; } | |
| 42 | |
| 43 const net::ProxyInfo& proxy_info() const { return proxy_info_; } | |
| 44 net::ProxyInfo* mutable_proxy_info() { return &proxy_info_; } | |
| 45 | |
| 46 // ProxyResolutionServiceProvider::Delegate: | 125 // ProxyResolutionServiceProvider::Delegate: |
| 47 scoped_refptr<net::URLRequestContextGetter> GetRequestContext() override { | 126 scoped_refptr<net::URLRequestContextGetter> GetRequestContext() override { |
| 48 return request_context_getter_; | 127 return context_getter_; |
| 49 } | |
| 50 int ResolveProxy(net::ProxyService* proxy_service, | |
| 51 const GURL& url, | |
| 52 net::ProxyInfo* results, | |
| 53 const net::CompletionCallback& callback) override { | |
| 54 EXPECT_EQ(proxy_service, | |
| 55 request_context_getter_->GetURLRequestContext()->proxy_service()); | |
| 56 *results = proxy_info_; | |
| 57 | |
| 58 if (!async_) | |
| 59 return result_; | |
| 60 | |
| 61 request_context_getter_->GetNetworkTaskRunner()->PostTask( | |
| 62 FROM_HERE, base::Bind(callback, result_)); | |
| 63 return net::ERR_IO_PENDING; | |
| 64 } | 128 } |
| 65 | 129 |
| 66 private: | 130 private: |
| 67 // Should ResolveProxy() run asynchronously (rather than synchronously)? | 131 net::ProxyResolver* proxy_resolver_; // Not owned. |
| 68 bool async_ = false; | 132 std::unique_ptr<net::ProxyService> proxy_service_; |
| 69 | 133 scoped_refptr<net::TestURLRequestContextGetter> context_getter_; |
| 70 // Final result for ResolveProxy() to return. | |
| 71 net::Error result_ = net::OK; | |
| 72 | |
| 73 // Proxy info for ResolveProxy() to return. | |
| 74 net::ProxyInfo proxy_info_; | |
| 75 | |
| 76 scoped_refptr<net::TestURLRequestContextGetter> request_context_getter_; | |
| 77 | 134 |
| 78 DISALLOW_COPY_AND_ASSIGN(TestDelegate); | 135 DISALLOW_COPY_AND_ASSIGN(TestDelegate); |
| 79 }; | 136 }; |
| 80 | 137 |
| 81 } // namespace | 138 } // namespace |
| 82 | 139 |
| 83 class ProxyResolutionServiceProviderTest : public testing::Test { | 140 class ProxyResolutionServiceProviderTest : public testing::Test { |
| 84 public: | 141 public: |
| 142 ProxyResolutionServiceProviderTest() = default; |
| 143 ~ProxyResolutionServiceProviderTest() override = default; |
| 144 |
| 145 // testing::Test: |
| 85 void SetUp() override { | 146 void SetUp() override { |
| 86 // Create the proxy resolution service with the mock bus and the mock | 147 proxy_resolver_ = base::MakeUnique<TestProxyResolver>( |
| 87 // resolver injected. | 148 base::ThreadTaskRunnerHandle::Get()); |
| 88 delegate_ = new TestDelegate(base::ThreadTaskRunnerHandle::Get()); | |
| 89 service_provider_ = base::MakeUnique<ProxyResolutionServiceProvider>( | 149 service_provider_ = base::MakeUnique<ProxyResolutionServiceProvider>( |
| 90 std::unique_ptr<TestDelegate>(delegate_)); | 150 base::MakeUnique<TestDelegate>(base::ThreadTaskRunnerHandle::Get(), |
| 91 | 151 proxy_resolver_.get())); |
| 92 test_helper_.SetUp(kResolveNetworkProxy, service_provider_.get()); | 152 test_helper_.SetUp(kResolveNetworkProxy, service_provider_.get()); |
| 93 } | 153 } |
| 94 | |
| 95 void TearDown() override { | 154 void TearDown() override { |
| 96 test_helper_.TearDown(); | 155 test_helper_.TearDown(); |
| 97 service_provider_.reset(); | |
| 98 } | 156 } |
| 99 | 157 |
| 100 protected: | 158 protected: |
| 101 // Arguments extracted from a D-Bus signal. | 159 // Arguments extracted from a D-Bus signal. |
| 102 struct SignalInfo { | 160 struct SignalInfo { |
| 103 std::string source_url; | 161 std::string source_url; |
| 104 std::string proxy_info; | 162 std::string proxy_info; |
| 105 std::string error_message; | 163 std::string error_message; |
| 106 }; | 164 }; |
| 107 | 165 |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 158 } | 216 } |
| 159 | 217 |
| 160 *response_out = test_helper_.CallMethod(&method_call); | 218 *response_out = test_helper_.CallMethod(&method_call); |
| 161 base::RunLoop().RunUntilIdle(); | 219 base::RunLoop().RunUntilIdle(); |
| 162 *signal_out = std::move(signal_); | 220 *signal_out = std::move(signal_); |
| 163 } | 221 } |
| 164 | 222 |
| 165 // Information about the last D-Bus signal received by OnSignalReceived(). | 223 // Information about the last D-Bus signal received by OnSignalReceived(). |
| 166 std::unique_ptr<SignalInfo> signal_; | 224 std::unique_ptr<SignalInfo> signal_; |
| 167 | 225 |
| 226 std::unique_ptr<TestProxyResolver> proxy_resolver_; |
| 227 std::unique_ptr<ProxyResolutionServiceProvider> service_provider_; |
| 168 ServiceProviderTestHelper test_helper_; | 228 ServiceProviderTestHelper test_helper_; |
| 169 std::unique_ptr<ProxyResolutionServiceProvider> service_provider_; | |
| 170 TestDelegate* delegate_; // Owned by |service_provider_|. | |
| 171 }; | 229 }; |
| 172 | 230 |
| 173 // Tests that synchronously-resolved proxy information is returned via a signal. | 231 // Tests that synchronously-resolved proxy information is returned via a signal. |
| 174 TEST_F(ProxyResolutionServiceProviderTest, SignalSync) { | 232 TEST_F(ProxyResolutionServiceProviderTest, SignalSync) { |
| 175 const char kSourceURL[] = "http://www.gmail.com/"; | 233 const char kSourceURL[] = "http://www.gmail.com/"; |
| 176 | 234 |
| 177 std::unique_ptr<dbus::Response> response; | 235 std::unique_ptr<dbus::Response> response; |
| 178 std::unique_ptr<SignalInfo> signal; | 236 std::unique_ptr<SignalInfo> signal; |
| 179 CallMethod(kSourceURL, true /* request_signal */, &response, &signal); | 237 CallMethod(kSourceURL, true /* request_signal */, &response, &signal); |
| 180 | 238 |
| 181 // An empty response should be returned. | 239 // An empty response should be returned. |
| 182 ASSERT_TRUE(response); | 240 ASSERT_TRUE(response); |
| 183 EXPECT_FALSE(dbus::MessageReader(response.get()).HasMoreData()); | 241 EXPECT_FALSE(dbus::MessageReader(response.get()).HasMoreData()); |
| 184 | 242 |
| 185 // Confirm that the signal is received successfully. | 243 // Confirm that the signal is received successfully. |
| 186 ASSERT_TRUE(signal); | 244 ASSERT_TRUE(signal); |
| 187 EXPECT_EQ(kSourceURL, signal->source_url); | 245 EXPECT_EQ(kSourceURL, signal->source_url); |
| 188 EXPECT_EQ(delegate_->proxy_info().ToPacString(), signal->proxy_info); | 246 EXPECT_EQ(proxy_resolver_->proxy_info().ToPacString(), signal->proxy_info); |
| 189 EXPECT_EQ("", signal->error_message); | 247 EXPECT_EQ("", signal->error_message); |
| 190 } | 248 } |
| 191 | 249 |
| 192 // Tests that asynchronously-resolved proxy information is returned via a | 250 // Tests that asynchronously-resolved proxy information is returned via a |
| 193 // signal. | 251 // signal. |
| 194 TEST_F(ProxyResolutionServiceProviderTest, SignalAsync) { | 252 TEST_F(ProxyResolutionServiceProviderTest, SignalAsync) { |
| 195 const char kSourceURL[] = "http://www.gmail.com/"; | 253 const char kSourceURL[] = "http://www.gmail.com/"; |
| 196 delegate_->set_async(true); | 254 proxy_resolver_->set_async(true); |
| 197 delegate_->mutable_proxy_info()->UseNamedProxy("http://localhost:8080"); | 255 proxy_resolver_->mutable_proxy_info()->UseNamedProxy("http://localhost:8080"); |
| 198 | 256 |
| 199 std::unique_ptr<dbus::Response> response; | 257 std::unique_ptr<dbus::Response> response; |
| 200 std::unique_ptr<SignalInfo> signal; | 258 std::unique_ptr<SignalInfo> signal; |
| 201 CallMethod(kSourceURL, true /* request_signal */, &response, &signal); | 259 CallMethod(kSourceURL, true /* request_signal */, &response, &signal); |
| 202 | 260 |
| 203 // An empty response should be returned. | 261 // An empty response should be returned. |
| 204 ASSERT_TRUE(response); | 262 ASSERT_TRUE(response); |
| 205 EXPECT_FALSE(dbus::MessageReader(response.get()).HasMoreData()); | 263 EXPECT_FALSE(dbus::MessageReader(response.get()).HasMoreData()); |
| 206 | 264 |
| 207 // Confirm that the signal is received successfully. | 265 // Confirm that the signal is received successfully. |
| 208 ASSERT_TRUE(signal); | 266 ASSERT_TRUE(signal); |
| 209 EXPECT_EQ(kSourceURL, signal->source_url); | 267 EXPECT_EQ(kSourceURL, signal->source_url); |
| 210 EXPECT_EQ(delegate_->proxy_info().ToPacString(), signal->proxy_info); | 268 EXPECT_EQ(proxy_resolver_->proxy_info().ToPacString(), signal->proxy_info); |
| 211 EXPECT_EQ("", signal->error_message); | 269 EXPECT_EQ("", signal->error_message); |
| 212 } | 270 } |
| 213 | 271 |
| 214 // Tests that an error received during proxy resolution is returned via a | |
| 215 // signal. | |
| 216 TEST_F(ProxyResolutionServiceProviderTest, SignalError) { | |
| 217 const char kSourceURL[] = "http://www.gmail.com/"; | |
| 218 const net::Error kError = net::ERR_FAILED; | |
| 219 delegate_->set_result(kError); | |
| 220 | |
| 221 std::unique_ptr<dbus::Response> response; | |
| 222 std::unique_ptr<SignalInfo> signal; | |
| 223 CallMethod(kSourceURL, true /* request_signal */, &response, &signal); | |
| 224 | |
| 225 // An empty response should be returned. | |
| 226 ASSERT_TRUE(response); | |
| 227 EXPECT_FALSE(dbus::MessageReader(response.get()).HasMoreData()); | |
| 228 | |
| 229 // The signal should contain an error. | |
| 230 ASSERT_TRUE(signal); | |
| 231 EXPECT_EQ(kSourceURL, signal->source_url); | |
| 232 EXPECT_EQ(delegate_->proxy_info().ToPacString(), signal->proxy_info); | |
| 233 EXPECT_EQ(net::ErrorToString(kError), signal->error_message); | |
| 234 } | |
| 235 | |
| 236 TEST_F(ProxyResolutionServiceProviderTest, ResponseSync) { | 272 TEST_F(ProxyResolutionServiceProviderTest, ResponseSync) { |
| 237 const char kSourceURL[] = "http://www.gmail.com/"; | 273 const char kSourceURL[] = "http://www.gmail.com/"; |
| 238 std::unique_ptr<dbus::Response> response; | 274 std::unique_ptr<dbus::Response> response; |
| 239 std::unique_ptr<SignalInfo> signal; | 275 std::unique_ptr<SignalInfo> signal; |
| 240 CallMethod(kSourceURL, false /* request_signal */, &response, &signal); | 276 CallMethod(kSourceURL, false /* request_signal */, &response, &signal); |
| 241 | 277 |
| 242 // The response should contain the proxy info and an empty error. | 278 // The response should contain the proxy info and an empty error. |
| 243 ASSERT_TRUE(response); | 279 ASSERT_TRUE(response); |
| 244 dbus::MessageReader reader(response.get()); | 280 dbus::MessageReader reader(response.get()); |
| 245 std::string proxy_info, error; | 281 std::string proxy_info, error; |
| 246 EXPECT_TRUE(reader.PopString(&proxy_info)); | 282 EXPECT_TRUE(reader.PopString(&proxy_info)); |
| 247 EXPECT_TRUE(reader.PopString(&error)); | 283 EXPECT_TRUE(reader.PopString(&error)); |
| 248 EXPECT_EQ(delegate_->proxy_info().ToPacString(), proxy_info); | 284 EXPECT_EQ(proxy_resolver_->proxy_info().ToPacString(), proxy_info); |
| 249 EXPECT_EQ("", error); | 285 EXPECT_EQ("", error); |
| 250 | 286 |
| 251 // No signal should've been emitted. | 287 // No signal should've been emitted. |
| 252 EXPECT_FALSE(signal); | 288 EXPECT_FALSE(signal); |
| 253 } | 289 } |
| 254 | 290 |
| 255 TEST_F(ProxyResolutionServiceProviderTest, ResponseAsync) { | 291 TEST_F(ProxyResolutionServiceProviderTest, ResponseAsync) { |
| 256 const char kSourceURL[] = "http://www.gmail.com/"; | 292 const char kSourceURL[] = "http://www.gmail.com/"; |
| 257 delegate_->set_async(true); | 293 proxy_resolver_->set_async(true); |
| 258 delegate_->mutable_proxy_info()->UseNamedProxy("http://localhost:8080"); | 294 proxy_resolver_->mutable_proxy_info()->UseNamedProxy("http://localhost:8080"); |
| 259 std::unique_ptr<dbus::Response> response; | 295 std::unique_ptr<dbus::Response> response; |
| 260 std::unique_ptr<SignalInfo> signal; | 296 std::unique_ptr<SignalInfo> signal; |
| 261 CallMethod(kSourceURL, false /* request_signal */, &response, &signal); | 297 CallMethod(kSourceURL, false /* request_signal */, &response, &signal); |
| 262 | 298 |
| 263 // The response should contain the proxy info and an empty error. | 299 // The response should contain the proxy info and an empty error. |
| 264 ASSERT_TRUE(response); | 300 ASSERT_TRUE(response); |
| 265 dbus::MessageReader reader(response.get()); | 301 dbus::MessageReader reader(response.get()); |
| 266 std::string proxy_info, error; | 302 std::string proxy_info, error; |
| 267 EXPECT_TRUE(reader.PopString(&proxy_info)); | 303 EXPECT_TRUE(reader.PopString(&proxy_info)); |
| 268 EXPECT_TRUE(reader.PopString(&error)); | 304 EXPECT_TRUE(reader.PopString(&error)); |
| 269 EXPECT_EQ(delegate_->proxy_info().ToPacString(), proxy_info); | 305 EXPECT_EQ(proxy_resolver_->proxy_info().ToPacString(), proxy_info); |
| 270 EXPECT_EQ("", error); | 306 EXPECT_EQ("", error); |
| 271 | 307 |
| 272 // No signal should've been emitted. | 308 // No signal should've been emitted. |
| 273 EXPECT_FALSE(signal); | 309 EXPECT_FALSE(signal); |
| 274 } | 310 } |
| 275 | 311 |
| 276 } // namespace chromeos | 312 } // namespace chromeos |
| OLD | NEW |