| 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/url_request/url_request_test_util.h" | 16 #include "net/url_request/url_request_test_util.h" |
| 17 #include "third_party/cros_system_api/dbus/service_constants.h" | 17 #include "third_party/cros_system_api/dbus/service_constants.h" |
| 18 | 18 |
| 19 namespace chromeos { | 19 namespace chromeos { |
| 20 | 20 |
| 21 namespace { | 21 namespace { |
| 22 | 22 |
| 23 // ProxyResolutionServiceProvider will return the proxy info as a D-Bus | 23 // ProxyResolutionServiceProvider will return the proxy info as a D-Bus |
| 24 // signal, to the following signal interface and the signal name. | 24 // signal, to the following signal interface and the signal name. |
| 25 const char kReturnSignalInterface[] = "org.chromium.TestInterface"; | 25 const char kReturnSignalInterface[] = "org.chromium.TestInterface"; |
| 26 const char kReturnSignalName[] = "TestSignal"; | 26 const char kReturnSignalName[] = "TestSignal"; |
| 27 | 27 |
| 28 // Test ProxyResolverDelegate implementation. | 28 // Test ProxyResolutionServiceProvider::Delegate implementation. |
| 29 class TestProxyResolverDelegate : public ProxyResolverDelegate { | 29 class TestDelegate : public ProxyResolutionServiceProvider::Delegate { |
| 30 public: | 30 public: |
| 31 explicit TestProxyResolverDelegate( | 31 explicit TestDelegate( |
| 32 const scoped_refptr<base::SingleThreadTaskRunner>& network_task_runner) | 32 const scoped_refptr<base::SingleThreadTaskRunner>& network_task_runner) |
| 33 : request_context_getter_( | 33 : request_context_getter_( |
| 34 new net::TestURLRequestContextGetter(network_task_runner)) { | 34 new net::TestURLRequestContextGetter(network_task_runner)) { |
| 35 // Use direct connections by default. | 35 // Use direct connections by default. |
| 36 proxy_info_.UseDirect(); | 36 proxy_info_.UseDirect(); |
| 37 } | 37 } |
| 38 ~TestProxyResolverDelegate() override {} | 38 ~TestDelegate() override = default; |
| 39 | 39 |
| 40 void set_async(bool async) { async_ = async; } | 40 void set_async(bool async) { async_ = async; } |
| 41 void set_result(net::Error result) { result_ = result; } | 41 void set_result(net::Error result) { result_ = result; } |
| 42 | 42 |
| 43 const net::ProxyInfo& proxy_info() const { return proxy_info_; } | 43 const net::ProxyInfo& proxy_info() const { return proxy_info_; } |
| 44 net::ProxyInfo* mutable_proxy_info() { return &proxy_info_; } | 44 net::ProxyInfo* mutable_proxy_info() { return &proxy_info_; } |
| 45 | 45 |
| 46 // ProxyResolverDelegate: | 46 // ProxyResolutionServiceProvider::Delegate: |
| 47 scoped_refptr<net::URLRequestContextGetter> GetRequestContext() override { | 47 scoped_refptr<net::URLRequestContextGetter> GetRequestContext() override { |
| 48 return request_context_getter_; | 48 return request_context_getter_; |
| 49 } | 49 } |
| 50 int ResolveProxy(net::ProxyService* proxy_service, | 50 int ResolveProxy(net::ProxyService* proxy_service, |
| 51 const GURL& url, | 51 const GURL& url, |
| 52 net::ProxyInfo* results, | 52 net::ProxyInfo* results, |
| 53 const net::CompletionCallback& callback) override { | 53 const net::CompletionCallback& callback) override { |
| 54 EXPECT_EQ(proxy_service, | 54 EXPECT_EQ(proxy_service, |
| 55 request_context_getter_->GetURLRequestContext()->proxy_service()); | 55 request_context_getter_->GetURLRequestContext()->proxy_service()); |
| 56 *results = proxy_info_; | 56 *results = proxy_info_; |
| (...skipping 11 matching lines...) Expand all Loading... |
| 68 bool async_ = false; | 68 bool async_ = false; |
| 69 | 69 |
| 70 // Final result for ResolveProxy() to return. | 70 // Final result for ResolveProxy() to return. |
| 71 net::Error result_ = net::OK; | 71 net::Error result_ = net::OK; |
| 72 | 72 |
| 73 // Proxy info for ResolveProxy() to return. | 73 // Proxy info for ResolveProxy() to return. |
| 74 net::ProxyInfo proxy_info_; | 74 net::ProxyInfo proxy_info_; |
| 75 | 75 |
| 76 scoped_refptr<net::TestURLRequestContextGetter> request_context_getter_; | 76 scoped_refptr<net::TestURLRequestContextGetter> request_context_getter_; |
| 77 | 77 |
| 78 DISALLOW_COPY_AND_ASSIGN(TestProxyResolverDelegate); | 78 DISALLOW_COPY_AND_ASSIGN(TestDelegate); |
| 79 }; | 79 }; |
| 80 | 80 |
| 81 } // namespace | 81 } // namespace |
| 82 | 82 |
| 83 class ProxyResolutionServiceProviderTest : public testing::Test { | 83 class ProxyResolutionServiceProviderTest : public testing::Test { |
| 84 public: | 84 public: |
| 85 void SetUp() override { | 85 void SetUp() override { |
| 86 // Create the proxy resolution service with the mock bus and the mock | 86 // Create the proxy resolution service with the mock bus and the mock |
| 87 // resolver injected. | 87 // resolver injected. |
| 88 delegate_ = | 88 delegate_ = new TestDelegate(base::ThreadTaskRunnerHandle::Get()); |
| 89 new TestProxyResolverDelegate(base::ThreadTaskRunnerHandle::Get()); | 89 service_provider_ = base::MakeUnique<ProxyResolutionServiceProvider>( |
| 90 service_provider_.reset(ProxyResolutionServiceProvider::Create( | 90 std::unique_ptr<TestDelegate>(delegate_)); |
| 91 std::unique_ptr<TestProxyResolverDelegate>(delegate_))); | |
| 92 | 91 |
| 93 test_helper_.SetUp(kResolveNetworkProxy, service_provider_.get()); | 92 test_helper_.SetUp(kResolveNetworkProxy, service_provider_.get()); |
| 94 | 93 |
| 95 // Connect to the signal that will be sent to kReturnSignalInterface and | 94 // Connect to the signal that will be sent to kReturnSignalInterface and |
| 96 // kReturnSignalName. ResolveNetworkProxy() will send the result as a | 95 // kReturnSignalName. ResolveNetworkProxy() will send the result as a |
| 97 // signal. OnSignalReceived() will be called upon the delivery. | 96 // signal. OnSignalReceived() will be called upon the delivery. |
| 98 test_helper_.SetUpReturnSignal( | 97 test_helper_.SetUpReturnSignal( |
| 99 kReturnSignalInterface, | 98 kReturnSignalInterface, |
| 100 kReturnSignalName, | 99 kReturnSignalName, |
| 101 base::Bind(&ProxyResolutionServiceProviderTest::OnSignalReceived, | 100 base::Bind(&ProxyResolutionServiceProviderTest::OnSignalReceived, |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 156 | 155 |
| 157 *response_out = test_helper_.CallMethod(&method_call); | 156 *response_out = test_helper_.CallMethod(&method_call); |
| 158 base::RunLoop().RunUntilIdle(); | 157 base::RunLoop().RunUntilIdle(); |
| 159 *signal_out = std::move(signal_); | 158 *signal_out = std::move(signal_); |
| 160 } | 159 } |
| 161 | 160 |
| 162 // Information about the last D-Bus signal received by OnSignalReceived(). | 161 // Information about the last D-Bus signal received by OnSignalReceived(). |
| 163 std::unique_ptr<SignalInfo> signal_; | 162 std::unique_ptr<SignalInfo> signal_; |
| 164 | 163 |
| 165 ServiceProviderTestHelper test_helper_; | 164 ServiceProviderTestHelper test_helper_; |
| 166 std::unique_ptr<CrosDBusService::ServiceProviderInterface> service_provider_; | 165 std::unique_ptr<ProxyResolutionServiceProvider> service_provider_; |
| 167 TestProxyResolverDelegate* delegate_; // Owned by service_provider_. | 166 TestDelegate* delegate_; // Owned by |service_provider_|. |
| 168 }; | 167 }; |
| 169 | 168 |
| 170 // Tests that synchronously-resolved proxy information is returned. | 169 // Tests that synchronously-resolved proxy information is returned. |
| 171 TEST_F(ProxyResolutionServiceProviderTest, ResolveProxySync) { | 170 TEST_F(ProxyResolutionServiceProviderTest, ResolveProxySync) { |
| 172 const char kSourceURL[] = "http://www.gmail.com/"; | 171 const char kSourceURL[] = "http://www.gmail.com/"; |
| 173 | 172 |
| 174 std::unique_ptr<dbus::Response> response; | 173 std::unique_ptr<dbus::Response> response; |
| 175 std::unique_ptr<SignalInfo> signal; | 174 std::unique_ptr<SignalInfo> signal; |
| 176 CallMethod(kSourceURL, &response, &signal); | 175 CallMethod(kSourceURL, &response, &signal); |
| 177 | 176 |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 222 EXPECT_FALSE(dbus::MessageReader(response.get()).HasMoreData()); | 221 EXPECT_FALSE(dbus::MessageReader(response.get()).HasMoreData()); |
| 223 | 222 |
| 224 // The signal should contain an error. | 223 // The signal should contain an error. |
| 225 ASSERT_TRUE(signal); | 224 ASSERT_TRUE(signal); |
| 226 EXPECT_EQ(kSourceURL, signal->source_url); | 225 EXPECT_EQ(kSourceURL, signal->source_url); |
| 227 EXPECT_EQ(delegate_->proxy_info().ToPacString(), signal->proxy_info); | 226 EXPECT_EQ(delegate_->proxy_info().ToPacString(), signal->proxy_info); |
| 228 EXPECT_EQ(net::ErrorToString(kError), signal->error_message); | 227 EXPECT_EQ(net::ErrorToString(kError), signal->error_message); |
| 229 } | 228 } |
| 230 | 229 |
| 231 } // namespace chromeos | 230 } // namespace chromeos |
| OLD | NEW |